Merge remote-tracking branches 'asoc/fix/tlv320aic3x' and 'asoc/fix/wm8962' into...
[linux-drm-fsl-dcu.git] / drivers / staging / rdma / 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 <linux/gfp.h>
43
44 #include "ehca_tools.h"
45 #include "ehca_iverbs.h"
46 #include "hcp_if.h"
47
48 static unsigned int limit_uint(unsigned int value)
49 {
50         return min_t(unsigned int, value, INT_MAX);
51 }
52
53 int ehca_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
54                       struct ib_udata *uhw)
55 {
56         int i, ret = 0;
57         struct ehca_shca *shca = container_of(ibdev, struct ehca_shca,
58                                               ib_device);
59         struct hipz_query_hca *rblock;
60
61         static const u32 cap_mapping[] = {
62                 IB_DEVICE_RESIZE_MAX_WR,      HCA_CAP_WQE_RESIZE,
63                 IB_DEVICE_BAD_PKEY_CNTR,      HCA_CAP_BAD_P_KEY_CTR,
64                 IB_DEVICE_BAD_QKEY_CNTR,      HCA_CAP_Q_KEY_VIOL_CTR,
65                 IB_DEVICE_RAW_MULTI,          HCA_CAP_RAW_PACKET_MCAST,
66                 IB_DEVICE_AUTO_PATH_MIG,      HCA_CAP_AUTO_PATH_MIG,
67                 IB_DEVICE_CHANGE_PHY_PORT,    HCA_CAP_SQD_RTS_PORT_CHANGE,
68                 IB_DEVICE_UD_AV_PORT_ENFORCE, HCA_CAP_AH_PORT_NR_CHECK,
69                 IB_DEVICE_CURR_QP_STATE_MOD,  HCA_CAP_CUR_QP_STATE_MOD,
70                 IB_DEVICE_SHUTDOWN_PORT,      HCA_CAP_SHUTDOWN_PORT,
71                 IB_DEVICE_INIT_TYPE,          HCA_CAP_INIT_TYPE,
72                 IB_DEVICE_PORT_ACTIVE_EVENT,  HCA_CAP_PORT_ACTIVE_EVENT,
73         };
74
75         if (uhw->inlen || uhw->outlen)
76                 return -EINVAL;
77
78         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
79         if (!rblock) {
80                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
81                 return -ENOMEM;
82         }
83
84         if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) {
85                 ehca_err(&shca->ib_device, "Can't query device properties");
86                 ret = -EINVAL;
87                 goto query_device1;
88         }
89
90         memset(props, 0, sizeof(struct ib_device_attr));
91         props->page_size_cap   = shca->hca_cap_mr_pgsize;
92         props->fw_ver          = rblock->hw_ver;
93         props->max_mr_size     = rblock->max_mr_size;
94         props->vendor_id       = rblock->vendor_id >> 8;
95         props->vendor_part_id  = rblock->vendor_part_id >> 16;
96         props->hw_ver          = rblock->hw_ver;
97         props->max_qp          = limit_uint(rblock->max_qp);
98         props->max_qp_wr       = limit_uint(rblock->max_wqes_wq);
99         props->max_sge         = limit_uint(rblock->max_sge);
100         props->max_sge_rd      = limit_uint(rblock->max_sge_rd);
101         props->max_cq          = limit_uint(rblock->max_cq);
102         props->max_cqe         = limit_uint(rblock->max_cqe);
103         props->max_mr          = limit_uint(rblock->max_mr);
104         props->max_mw          = limit_uint(rblock->max_mw);
105         props->max_pd          = limit_uint(rblock->max_pd);
106         props->max_ah          = limit_uint(rblock->max_ah);
107         props->max_ee          = limit_uint(rblock->max_rd_ee_context);
108         props->max_rdd         = limit_uint(rblock->max_rd_domain);
109         props->max_fmr         = limit_uint(rblock->max_mr);
110         props->max_qp_rd_atom  = limit_uint(rblock->max_rr_qp);
111         props->max_ee_rd_atom  = limit_uint(rblock->max_rr_ee_context);
112         props->max_res_rd_atom = limit_uint(rblock->max_rr_hca);
113         props->max_qp_init_rd_atom = limit_uint(rblock->max_act_wqs_qp);
114         props->max_ee_init_rd_atom = limit_uint(rblock->max_act_wqs_ee_context);
115
116         if (EHCA_BMASK_GET(HCA_CAP_SRQ, shca->hca_cap)) {
117                 props->max_srq         = limit_uint(props->max_qp);
118                 props->max_srq_wr      = limit_uint(props->max_qp_wr);
119                 props->max_srq_sge     = 3;
120         }
121
122         props->max_pkeys           = 16;
123         /* Some FW versions say 0 here; insert sensible value in that case */
124         props->local_ca_ack_delay  = rblock->local_ca_ack_delay ?
125                 min_t(u8, rblock->local_ca_ack_delay, 255) : 12;
126         props->max_raw_ipv6_qp     = limit_uint(rblock->max_raw_ipv6_qp);
127         props->max_raw_ethy_qp     = limit_uint(rblock->max_raw_ethy_qp);
128         props->max_mcast_grp       = limit_uint(rblock->max_mcast_grp);
129         props->max_mcast_qp_attach = limit_uint(rblock->max_mcast_qp_attach);
130         props->max_total_mcast_qp_attach
131                 = limit_uint(rblock->max_total_mcast_qp_attach);
132
133         /* translate device capabilities */
134         props->device_cap_flags = IB_DEVICE_SYS_IMAGE_GUID |
135                 IB_DEVICE_RC_RNR_NAK_GEN | IB_DEVICE_N_NOTIFY_CQ;
136         for (i = 0; i < ARRAY_SIZE(cap_mapping); i += 2)
137                 if (rblock->hca_cap_indicators & cap_mapping[i + 1])
138                         props->device_cap_flags |= cap_mapping[i];
139
140 query_device1:
141         ehca_free_fw_ctrlblock(rblock);
142
143         return ret;
144 }
145
146 static enum ib_mtu map_mtu(struct ehca_shca *shca, u32 fw_mtu)
147 {
148         switch (fw_mtu) {
149         case 0x1:
150                 return IB_MTU_256;
151         case 0x2:
152                 return IB_MTU_512;
153         case 0x3:
154                 return IB_MTU_1024;
155         case 0x4:
156                 return IB_MTU_2048;
157         case 0x5:
158                 return IB_MTU_4096;
159         default:
160                 ehca_err(&shca->ib_device, "Unknown MTU size: %x.",
161                          fw_mtu);
162                 return 0;
163         }
164 }
165
166 static u8 map_number_of_vls(struct ehca_shca *shca, u32 vl_cap)
167 {
168         switch (vl_cap) {
169         case 0x1:
170                 return 1;
171         case 0x2:
172                 return 2;
173         case 0x3:
174                 return 4;
175         case 0x4:
176                 return 8;
177         case 0x5:
178                 return 15;
179         default:
180                 ehca_err(&shca->ib_device, "invalid Vl Capability: %x.",
181                          vl_cap);
182                 return 0;
183         }
184 }
185
186 int ehca_query_port(struct ib_device *ibdev,
187                     u8 port, struct ib_port_attr *props)
188 {
189         int ret = 0;
190         u64 h_ret;
191         struct ehca_shca *shca = container_of(ibdev, struct ehca_shca,
192                                               ib_device);
193         struct hipz_query_port *rblock;
194
195         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
196         if (!rblock) {
197                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
198                 return -ENOMEM;
199         }
200
201         h_ret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
202         if (h_ret != H_SUCCESS) {
203                 ehca_err(&shca->ib_device, "Can't query port properties");
204                 ret = -EINVAL;
205                 goto query_port1;
206         }
207
208         memset(props, 0, sizeof(struct ib_port_attr));
209
210         props->active_mtu = props->max_mtu = map_mtu(shca, rblock->max_mtu);
211         props->port_cap_flags  = rblock->capability_mask;
212         props->gid_tbl_len     = rblock->gid_tbl_len;
213         if (rblock->max_msg_sz)
214                 props->max_msg_sz      = rblock->max_msg_sz;
215         else
216                 props->max_msg_sz      = 0x1 << 31;
217         props->bad_pkey_cntr   = rblock->bad_pkey_cntr;
218         props->qkey_viol_cntr  = rblock->qkey_viol_cntr;
219         props->pkey_tbl_len    = rblock->pkey_tbl_len;
220         props->lid             = rblock->lid;
221         props->sm_lid          = rblock->sm_lid;
222         props->lmc             = rblock->lmc;
223         props->sm_sl           = rblock->sm_sl;
224         props->subnet_timeout  = rblock->subnet_timeout;
225         props->init_type_reply = rblock->init_type_reply;
226         props->max_vl_num      = map_number_of_vls(shca, rblock->vl_cap);
227
228         if (rblock->state && rblock->phys_width) {
229                 props->phys_state      = rblock->phys_pstate;
230                 props->state           = rblock->phys_state;
231                 props->active_width    = rblock->phys_width;
232                 props->active_speed    = rblock->phys_speed;
233         } else {
234                 /* old firmware releases don't report physical
235                  * port info, so use default values
236                  */
237                 props->phys_state      = 5;
238                 props->state           = rblock->state;
239                 props->active_width    = IB_WIDTH_12X;
240                 props->active_speed    = IB_SPEED_SDR;
241         }
242
243 query_port1:
244         ehca_free_fw_ctrlblock(rblock);
245
246         return ret;
247 }
248
249 int ehca_query_sma_attr(struct ehca_shca *shca,
250                         u8 port, struct ehca_sma_attr *attr)
251 {
252         int ret = 0;
253         u64 h_ret;
254         struct hipz_query_port *rblock;
255
256         rblock = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
257         if (!rblock) {
258                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
259                 return -ENOMEM;
260         }
261
262         h_ret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
263         if (h_ret != H_SUCCESS) {
264                 ehca_err(&shca->ib_device, "Can't query port properties");
265                 ret = -EINVAL;
266                 goto query_sma_attr1;
267         }
268
269         memset(attr, 0, sizeof(struct ehca_sma_attr));
270
271         attr->lid    = rblock->lid;
272         attr->lmc    = rblock->lmc;
273         attr->sm_sl  = rblock->sm_sl;
274         attr->sm_lid = rblock->sm_lid;
275
276         attr->pkey_tbl_len = rblock->pkey_tbl_len;
277         memcpy(attr->pkeys, rblock->pkey_entries, sizeof(attr->pkeys));
278
279 query_sma_attr1:
280         ehca_free_fw_ctrlblock(rblock);
281
282         return ret;
283 }
284
285 int ehca_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
286 {
287         int ret = 0;
288         u64 h_ret;
289         struct ehca_shca *shca;
290         struct hipz_query_port *rblock;
291
292         shca = container_of(ibdev, struct ehca_shca, ib_device);
293         if (index > 16) {
294                 ehca_err(&shca->ib_device, "Invalid index: %x.", index);
295                 return -EINVAL;
296         }
297
298         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
299         if (!rblock) {
300                 ehca_err(&shca->ib_device,  "Can't allocate rblock memory.");
301                 return -ENOMEM;
302         }
303
304         h_ret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
305         if (h_ret != H_SUCCESS) {
306                 ehca_err(&shca->ib_device, "Can't query port properties");
307                 ret = -EINVAL;
308                 goto query_pkey1;
309         }
310
311         memcpy(pkey, &rblock->pkey_entries + index, sizeof(u16));
312
313 query_pkey1:
314         ehca_free_fw_ctrlblock(rblock);
315
316         return ret;
317 }
318
319 int ehca_query_gid(struct ib_device *ibdev, u8 port,
320                    int index, union ib_gid *gid)
321 {
322         int ret = 0;
323         u64 h_ret;
324         struct ehca_shca *shca = container_of(ibdev, struct ehca_shca,
325                                               ib_device);
326         struct hipz_query_port *rblock;
327
328         if (index < 0 || index > 255) {
329                 ehca_err(&shca->ib_device, "Invalid index: %x.", index);
330                 return -EINVAL;
331         }
332
333         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
334         if (!rblock) {
335                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
336                 return -ENOMEM;
337         }
338
339         h_ret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
340         if (h_ret != H_SUCCESS) {
341                 ehca_err(&shca->ib_device, "Can't query port properties");
342                 ret = -EINVAL;
343                 goto query_gid1;
344         }
345
346         memcpy(&gid->raw[0], &rblock->gid_prefix, sizeof(u64));
347         memcpy(&gid->raw[8], &rblock->guid_entries[index], sizeof(u64));
348
349 query_gid1:
350         ehca_free_fw_ctrlblock(rblock);
351
352         return ret;
353 }
354
355 static const u32 allowed_port_caps = (
356         IB_PORT_SM | IB_PORT_LED_INFO_SUP | IB_PORT_CM_SUP |
357         IB_PORT_SNMP_TUNNEL_SUP | IB_PORT_DEVICE_MGMT_SUP |
358         IB_PORT_VENDOR_CLASS_SUP);
359
360 int ehca_modify_port(struct ib_device *ibdev,
361                      u8 port, int port_modify_mask,
362                      struct ib_port_modify *props)
363 {
364         int ret = 0;
365         struct ehca_shca *shca;
366         struct hipz_query_port *rblock;
367         u32 cap;
368         u64 hret;
369
370         shca = container_of(ibdev, struct ehca_shca, ib_device);
371         if ((props->set_port_cap_mask | props->clr_port_cap_mask)
372             & ~allowed_port_caps) {
373                 ehca_err(&shca->ib_device, "Non-changeable bits set in masks  "
374                          "set=%x  clr=%x  allowed=%x", props->set_port_cap_mask,
375                          props->clr_port_cap_mask, allowed_port_caps);
376                 return -EINVAL;
377         }
378
379         if (mutex_lock_interruptible(&shca->modify_mutex))
380                 return -ERESTARTSYS;
381
382         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
383         if (!rblock) {
384                 ehca_err(&shca->ib_device,  "Can't allocate rblock memory.");
385                 ret = -ENOMEM;
386                 goto modify_port1;
387         }
388
389         hret = hipz_h_query_port(shca->ipz_hca_handle, port, rblock);
390         if (hret != H_SUCCESS) {
391                 ehca_err(&shca->ib_device, "Can't query port properties");
392                 ret = -EINVAL;
393                 goto modify_port2;
394         }
395
396         cap = (rblock->capability_mask | props->set_port_cap_mask)
397                 & ~props->clr_port_cap_mask;
398
399         hret = hipz_h_modify_port(shca->ipz_hca_handle, port,
400                                   cap, props->init_type, port_modify_mask);
401         if (hret != H_SUCCESS) {
402                 ehca_err(&shca->ib_device, "Modify port failed  h_ret=%lli",
403                          hret);
404                 ret = -EINVAL;
405         }
406
407 modify_port2:
408         ehca_free_fw_ctrlblock(rblock);
409
410 modify_port1:
411         mutex_unlock(&shca->modify_mutex);
412
413         return ret;
414 }