Merge branch 'fix/hda' into topic/hda
[linux-2.6] / drivers / infiniband / hw / ehca / ehca_hca.c
1 /*
2  *  IBM eServer eHCA Infiniband device driver for Linux on POWER
3  *
4  *  HCA query functions
5  *
6  *  Authors: Heiko J Schick <schickhj@de.ibm.com>
7  *           Christoph Raisch <raisch@de.ibm.com>
8  *
9  *  Copyright (c) 2005 IBM Corporation
10  *
11  *  All rights reserved.
12  *
13  *  This source code is distributed under a dual license of GPL v2.0 and OpenIB
14  *  BSD.
15  *
16  * OpenIB BSD License
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are met:
20  *
21  * Redistributions of source code must retain the above copyright notice, this
22  * list of conditions and the following disclaimer.
23  *
24  * Redistributions in binary form must reproduce the above copyright notice,
25  * this list of conditions and the following disclaimer in the documentation
26  * and/or other materials
27  * provided with the distribution.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
37  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 #include "ehca_tools.h"
43 #include "ehca_iverbs.h"
44 #include "hcp_if.h"
45
46 static unsigned int limit_uint(unsigned int value)
47 {
48         return min_t(unsigned int, value, INT_MAX);
49 }
50
51 int ehca_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
52 {
53         int i, ret = 0;
54         struct ehca_shca *shca = container_of(ibdev, struct ehca_shca,
55                                               ib_device);
56         struct hipz_query_hca *rblock;
57
58         static const u32 cap_mapping[] = {
59                 IB_DEVICE_RESIZE_MAX_WR,      HCA_CAP_WQE_RESIZE,
60                 IB_DEVICE_BAD_PKEY_CNTR,      HCA_CAP_BAD_P_KEY_CTR,
61                 IB_DEVICE_BAD_QKEY_CNTR,      HCA_CAP_Q_KEY_VIOL_CTR,
62                 IB_DEVICE_RAW_MULTI,          HCA_CAP_RAW_PACKET_MCAST,
63                 IB_DEVICE_AUTO_PATH_MIG,      HCA_CAP_AUTO_PATH_MIG,
64                 IB_DEVICE_CHANGE_PHY_PORT,    HCA_CAP_SQD_RTS_PORT_CHANGE,
65                 IB_DEVICE_UD_AV_PORT_ENFORCE, HCA_CAP_AH_PORT_NR_CHECK,
66                 IB_DEVICE_CURR_QP_STATE_MOD,  HCA_CAP_CUR_QP_STATE_MOD,
67                 IB_DEVICE_SHUTDOWN_PORT,      HCA_CAP_SHUTDOWN_PORT,
68                 IB_DEVICE_INIT_TYPE,          HCA_CAP_INIT_TYPE,
69                 IB_DEVICE_PORT_ACTIVE_EVENT,  HCA_CAP_PORT_ACTIVE_EVENT,
70         };
71
72         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
73         if (!rblock) {
74                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
75                 return -ENOMEM;
76         }
77
78         if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) {
79                 ehca_err(&shca->ib_device, "Can't query device properties");
80                 ret = -EINVAL;
81                 goto query_device1;
82         }
83
84         memset(props, 0, sizeof(struct ib_device_attr));
85         props->page_size_cap   = shca->hca_cap_mr_pgsize;
86         props->fw_ver          = rblock->hw_ver;
87         props->max_mr_size     = rblock->max_mr_size;
88         props->vendor_id       = rblock->vendor_id >> 8;
89         props->vendor_part_id  = rblock->vendor_part_id >> 16;
90         props->hw_ver          = rblock->hw_ver;
91         props->max_qp          = limit_uint(rblock->max_qp);
92         props->max_qp_wr       = limit_uint(rblock->max_wqes_wq);
93         props->max_sge         = limit_uint(rblock->max_sge);
94         props->max_sge_rd      = limit_uint(rblock->max_sge_rd);
95         props->max_cq          = limit_uint(rblock->max_cq);
96         props->max_cqe         = limit_uint(rblock->max_cqe);
97         props->max_mr          = limit_uint(rblock->max_mr);
98         props->max_mw          = limit_uint(rblock->max_mw);
99         props->max_pd          = limit_uint(rblock->max_pd);
100         props->max_ah          = limit_uint(rblock->max_ah);
101         props->max_ee          = limit_uint(rblock->max_rd_ee_context);
102         props->max_rdd         = limit_uint(rblock->max_rd_domain);
103         props->max_fmr         = limit_uint(rblock->max_mr);
104         props->max_qp_rd_atom  = limit_uint(rblock->max_rr_qp);
105         props->max_ee_rd_atom  = limit_uint(rblock->max_rr_ee_context);
106         props->max_res_rd_atom = limit_uint(rblock->max_rr_hca);
107         props->max_qp_init_rd_atom = limit_uint(rblock->max_act_wqs_qp);
108         props->max_ee_init_rd_atom = limit_uint(rblock->max_act_wqs_ee_context);
109
110         if (EHCA_BMASK_GET(HCA_CAP_SRQ, shca->hca_cap)) {
111                 props->max_srq         = limit_uint(props->max_qp);
112                 props->max_srq_wr      = limit_uint(props->max_qp_wr);
113                 props->max_srq_sge     = 3;
114         }
115
116         props->max_pkeys           = 16;
117         /* Some FW versions say 0 here; insert sensible value in that case */
118         props->local_ca_ack_delay  = rblock->local_ca_ack_delay ?
119                 min_t(u8, rblock->local_ca_ack_delay, 255) : 12;
120         props->max_raw_ipv6_qp     = limit_uint(rblock->max_raw_ipv6_qp);
121         props->max_raw_ethy_qp     = limit_uint(rblock->max_raw_ethy_qp);
122         props->max_mcast_grp       = limit_uint(rblock->max_mcast_grp);
123         props->max_mcast_qp_attach = limit_uint(rblock->max_mcast_qp_attach);
124         props->max_total_mcast_qp_attach
125                 = limit_uint(rblock->max_total_mcast_qp_attach);
126
127         /* translate device capabilities */
128         props->device_cap_flags = IB_DEVICE_SYS_IMAGE_GUID |
129                 IB_DEVICE_RC_RNR_NAK_GEN | IB_DEVICE_N_NOTIFY_CQ;
130         for (i = 0; i < ARRAY_SIZE(cap_mapping); i += 2)
131                 if (rblock->hca_cap_indicators & cap_mapping[i + 1])
132                         props->device_cap_flags |= cap_mapping[i];
133
134 query_device1:
135         ehca_free_fw_ctrlblock(rblock);
136
137         return ret;
138 }
139
140 static enum ib_mtu map_mtu(struct ehca_shca *shca, u32 fw_mtu)
141 {
142         switch (fw_mtu) {
143         case 0x1:
144                 return IB_MTU_256;
145         case 0x2:
146                 return IB_MTU_512;
147         case 0x3:
148                 return IB_MTU_1024;
149         case 0x4:
150                 return IB_MTU_2048;
151         case 0x5:
152                 return IB_MTU_4096;
153         default:
154                 ehca_err(&shca->ib_device, "Unknown MTU size: %x.",
155                          fw_mtu);
156                 return 0;
157         }
158 }
159
160 static u8 map_number_of_vls(struct ehca_shca *shca, u32 vl_cap)
161 {
162         switch (vl_cap) {
163         case 0x1:
164                 return 1;
165         case 0x2:
166                 return 2;
167         case 0x3:
168                 return 4;
169         case 0x4:
170                 return 8;
171         case 0x5:
172                 return 15;
173         default:
174                 ehca_err(&shca->ib_device, "invalid Vl Capability: %x.",
175                          vl_cap);
176                 return 0;
177         }
178 }
179
180 int ehca_query_port(struct ib_device *ibdev,
181                     u8 port, struct ib_port_attr *props)
182 {
183         int ret = 0;
184         u64 h_ret;
185         struct ehca_shca *shca = container_of(ibdev, struct ehca_shca,
186                                               ib_device);
187         struct hipz_query_port *rblock;
188
189         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
190         if (!rblock) {
191                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
192                 return -ENOMEM;
193         }
194
195         h_ret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
196         if (h_ret != H_SUCCESS) {
197                 ehca_err(&shca->ib_device, "Can't query port properties");
198                 ret = -EINVAL;
199                 goto query_port1;
200         }
201
202         memset(props, 0, sizeof(struct ib_port_attr));
203
204         props->active_mtu = props->max_mtu = map_mtu(shca, rblock->max_mtu);
205         props->port_cap_flags  = rblock->capability_mask;
206         props->gid_tbl_len     = rblock->gid_tbl_len;
207         if (rblock->max_msg_sz)
208                 props->max_msg_sz      = rblock->max_msg_sz;
209         else
210                 props->max_msg_sz      = 0x1 << 31;
211         props->bad_pkey_cntr   = rblock->bad_pkey_cntr;
212         props->qkey_viol_cntr  = rblock->qkey_viol_cntr;
213         props->pkey_tbl_len    = rblock->pkey_tbl_len;
214         props->lid             = rblock->lid;
215         props->sm_lid          = rblock->sm_lid;
216         props->lmc             = rblock->lmc;
217         props->sm_sl           = rblock->sm_sl;
218         props->subnet_timeout  = rblock->subnet_timeout;
219         props->init_type_reply = rblock->init_type_reply;
220         props->max_vl_num      = map_number_of_vls(shca, rblock->vl_cap);
221
222         if (rblock->state && rblock->phys_width) {
223                 props->phys_state      = rblock->phys_pstate;
224                 props->state           = rblock->phys_state;
225                 props->active_width    = rblock->phys_width;
226                 props->active_speed    = rblock->phys_speed;
227         } else {
228                 /* old firmware releases don't report physical
229                  * port info, so use default values
230                  */
231                 props->phys_state      = 5;
232                 props->state           = rblock->state;
233                 props->active_width    = IB_WIDTH_12X;
234                 props->active_speed    = 0x1;
235         }
236
237 query_port1:
238         ehca_free_fw_ctrlblock(rblock);
239
240         return ret;
241 }
242
243 int ehca_query_sma_attr(struct ehca_shca *shca,
244                         u8 port, struct ehca_sma_attr *attr)
245 {
246         int ret = 0;
247         u64 h_ret;
248         struct hipz_query_port *rblock;
249
250         rblock = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
251         if (!rblock) {
252                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
253                 return -ENOMEM;
254         }
255
256         h_ret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
257         if (h_ret != H_SUCCESS) {
258                 ehca_err(&shca->ib_device, "Can't query port properties");
259                 ret = -EINVAL;
260                 goto query_sma_attr1;
261         }
262
263         memset(attr, 0, sizeof(struct ehca_sma_attr));
264
265         attr->lid    = rblock->lid;
266         attr->lmc    = rblock->lmc;
267         attr->sm_sl  = rblock->sm_sl;
268         attr->sm_lid = rblock->sm_lid;
269
270         attr->pkey_tbl_len = rblock->pkey_tbl_len;
271         memcpy(attr->pkeys, rblock->pkey_entries, sizeof(attr->pkeys));
272
273 query_sma_attr1:
274         ehca_free_fw_ctrlblock(rblock);
275
276         return ret;
277 }
278
279 int ehca_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
280 {
281         int ret = 0;
282         u64 h_ret;
283         struct ehca_shca *shca;
284         struct hipz_query_port *rblock;
285
286         shca = container_of(ibdev, struct ehca_shca, ib_device);
287         if (index > 16) {
288                 ehca_err(&shca->ib_device, "Invalid index: %x.", index);
289                 return -EINVAL;
290         }
291
292         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
293         if (!rblock) {
294                 ehca_err(&shca->ib_device,  "Can't allocate rblock memory.");
295                 return -ENOMEM;
296         }
297
298         h_ret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
299         if (h_ret != H_SUCCESS) {
300                 ehca_err(&shca->ib_device, "Can't query port properties");
301                 ret = -EINVAL;
302                 goto query_pkey1;
303         }
304
305         memcpy(pkey, &rblock->pkey_entries + index, sizeof(u16));
306
307 query_pkey1:
308         ehca_free_fw_ctrlblock(rblock);
309
310         return ret;
311 }
312
313 int ehca_query_gid(struct ib_device *ibdev, u8 port,
314                    int index, union ib_gid *gid)
315 {
316         int ret = 0;
317         u64 h_ret;
318         struct ehca_shca *shca = container_of(ibdev, struct ehca_shca,
319                                               ib_device);
320         struct hipz_query_port *rblock;
321
322         if (index > 255) {
323                 ehca_err(&shca->ib_device, "Invalid index: %x.", index);
324                 return -EINVAL;
325         }
326
327         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
328         if (!rblock) {
329                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
330                 return -ENOMEM;
331         }
332
333         h_ret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
334         if (h_ret != H_SUCCESS) {
335                 ehca_err(&shca->ib_device, "Can't query port properties");
336                 ret = -EINVAL;
337                 goto query_gid1;
338         }
339
340         memcpy(&gid->raw[0], &rblock->gid_prefix, sizeof(u64));
341         memcpy(&gid->raw[8], &rblock->guid_entries[index], sizeof(u64));
342
343 query_gid1:
344         ehca_free_fw_ctrlblock(rblock);
345
346         return ret;
347 }
348
349 static const u32 allowed_port_caps = (
350         IB_PORT_SM | IB_PORT_LED_INFO_SUP | IB_PORT_CM_SUP |
351         IB_PORT_SNMP_TUNNEL_SUP | IB_PORT_DEVICE_MGMT_SUP |
352         IB_PORT_VENDOR_CLASS_SUP);
353
354 int ehca_modify_port(struct ib_device *ibdev,
355                      u8 port, int port_modify_mask,
356                      struct ib_port_modify *props)
357 {
358         int ret = 0;
359         struct ehca_shca *shca;
360         struct hipz_query_port *rblock;
361         u32 cap;
362         u64 hret;
363
364         shca = container_of(ibdev, struct ehca_shca, ib_device);
365         if ((props->set_port_cap_mask | props->clr_port_cap_mask)
366             & ~allowed_port_caps) {
367                 ehca_err(&shca->ib_device, "Non-changeable bits set in masks  "
368                          "set=%x  clr=%x  allowed=%x", props->set_port_cap_mask,
369                          props->clr_port_cap_mask, allowed_port_caps);
370                 return -EINVAL;
371         }
372
373         if (mutex_lock_interruptible(&shca->modify_mutex))
374                 return -ERESTARTSYS;
375
376         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
377         if (!rblock) {
378                 ehca_err(&shca->ib_device,  "Can't allocate rblock memory.");
379                 ret = -ENOMEM;
380                 goto modify_port1;
381         }
382
383         hret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
384         if (hret != H_SUCCESS) {
385                 ehca_err(&shca->ib_device, "Can't query port properties");
386                 ret = -EINVAL;
387                 goto modify_port2;
388         }
389
390         cap = (rblock->capability_mask | props->set_port_cap_mask)
391                 & ~props->clr_port_cap_mask;
392
393         hret = hipz_h_modify_port(shca->ipz_hca_handle, port,
394                                   cap, props->init_type, port_modify_mask);
395         if (hret != H_SUCCESS) {
396                 ehca_err(&shca->ib_device, "Modify port failed  h_ret=%li",
397                          hret);
398                 ret = -EINVAL;
399         }
400
401 modify_port2:
402         ehca_free_fw_ctrlblock(rblock);
403
404 modify_port1:
405         mutex_unlock(&shca->modify_mutex);
406
407         return ret;
408 }