1 /*
2 * Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2004 Infinicon Corporation. All rights reserved.
4 * Copyright (c) 2004 Intel Corporation. All rights reserved.
5 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
6 * Copyright (c) 2004 Voltaire Corporation. All rights reserved.
7 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
8 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
9 *
10 * This software is available to you under a choice of one of two
11 * licenses. You may choose to be licensed under the terms of the GNU
12 * General Public License (GPL) Version 2, available from the file
13 * COPYING in the main directory of this source tree, or the
14 * OpenIB.org BSD license below:
15 *
16 * Redistribution and use in source and binary forms, with or
17 * without modification, are permitted provided that the following
18 * conditions are met:
19 *
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer.
23 *
24 * - Redistributions in binary form must reproduce the above
25 * copyright notice, this list of conditions and the following
26 * disclaimer in the documentation and/or other materials
27 * provided with the distribution.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * SOFTWARE.
37 */
38
39 #include <linux/errno.h>
40 #include <linux/err.h>
41 #include <linux/export.h>
42 #include <linux/string.h>
43 #include <linux/slab.h>
44 #include <linux/in.h>
45 #include <linux/in6.h>
46 #include <net/addrconf.h>
47 #include <linux/security.h>
48
49 #include <rdma/ib_verbs.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_addr.h>
52 #include <rdma/rw.h>
53
54 #include "core_priv.h"
55
56 static const char * const ib_events[] = {
57 [IB_EVENT_CQ_ERR] = "CQ error",
58 [IB_EVENT_QP_FATAL] = "QP fatal error",
59 [IB_EVENT_QP_REQ_ERR] = "QP request error",
60 [IB_EVENT_QP_ACCESS_ERR] = "QP access error",
61 [IB_EVENT_COMM_EST] = "communication established",
62 [IB_EVENT_SQ_DRAINED] = "send queue drained",
63 [IB_EVENT_PATH_MIG] = "path migration successful",
64 [IB_EVENT_PATH_MIG_ERR] = "path migration error",
65 [IB_EVENT_DEVICE_FATAL] = "device fatal error",
66 [IB_EVENT_PORT_ACTIVE] = "port active",
67 [IB_EVENT_PORT_ERR] = "port error",
68 [IB_EVENT_LID_CHANGE] = "LID change",
69 [IB_EVENT_PKEY_CHANGE] = "P_key change",
70 [IB_EVENT_SM_CHANGE] = "SM change",
71 [IB_EVENT_SRQ_ERR] = "SRQ error",
72 [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached",
73 [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached",
74 [IB_EVENT_CLIENT_REREGISTER] = "client reregister",
75 [IB_EVENT_GID_CHANGE] = "GID changed",
76 };
77
ib_event_msg(enum ib_event_type event)78 const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
79 {
80 size_t index = event;
81
82 return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
83 ib_events[index] : "unrecognized event";
84 }
85 EXPORT_SYMBOL(ib_event_msg);
86
87 static const char * const wc_statuses[] = {
88 [IB_WC_SUCCESS] = "success",
89 [IB_WC_LOC_LEN_ERR] = "local length error",
90 [IB_WC_LOC_QP_OP_ERR] = "local QP operation error",
91 [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error",
92 [IB_WC_LOC_PROT_ERR] = "local protection error",
93 [IB_WC_WR_FLUSH_ERR] = "WR flushed",
94 [IB_WC_MW_BIND_ERR] = "memory management operation error",
95 [IB_WC_BAD_RESP_ERR] = "bad response error",
96 [IB_WC_LOC_ACCESS_ERR] = "local access error",
97 [IB_WC_REM_INV_REQ_ERR] = "invalid request error",
98 [IB_WC_REM_ACCESS_ERR] = "remote access error",
99 [IB_WC_REM_OP_ERR] = "remote operation error",
100 [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded",
101 [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded",
102 [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error",
103 [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request",
104 [IB_WC_REM_ABORT_ERR] = "operation aborted",
105 [IB_WC_INV_EECN_ERR] = "invalid EE context number",
106 [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state",
107 [IB_WC_FATAL_ERR] = "fatal error",
108 [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error",
109 [IB_WC_GENERAL_ERR] = "general error",
110 };
111
ib_wc_status_msg(enum ib_wc_status status)112 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
113 {
114 size_t index = status;
115
116 return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
117 wc_statuses[index] : "unrecognized status";
118 }
119 EXPORT_SYMBOL(ib_wc_status_msg);
120
ib_rate_to_mult(enum ib_rate rate)121 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
122 {
123 switch (rate) {
124 case IB_RATE_2_5_GBPS: return 1;
125 case IB_RATE_5_GBPS: return 2;
126 case IB_RATE_10_GBPS: return 4;
127 case IB_RATE_20_GBPS: return 8;
128 case IB_RATE_30_GBPS: return 12;
129 case IB_RATE_40_GBPS: return 16;
130 case IB_RATE_60_GBPS: return 24;
131 case IB_RATE_80_GBPS: return 32;
132 case IB_RATE_120_GBPS: return 48;
133 default: return -1;
134 }
135 }
136 EXPORT_SYMBOL(ib_rate_to_mult);
137
mult_to_ib_rate(int mult)138 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
139 {
140 switch (mult) {
141 case 1: return IB_RATE_2_5_GBPS;
142 case 2: return IB_RATE_5_GBPS;
143 case 4: return IB_RATE_10_GBPS;
144 case 8: return IB_RATE_20_GBPS;
145 case 12: return IB_RATE_30_GBPS;
146 case 16: return IB_RATE_40_GBPS;
147 case 24: return IB_RATE_60_GBPS;
148 case 32: return IB_RATE_80_GBPS;
149 case 48: return IB_RATE_120_GBPS;
150 default: return IB_RATE_PORT_CURRENT;
151 }
152 }
153 EXPORT_SYMBOL(mult_to_ib_rate);
154
ib_rate_to_mbps(enum ib_rate rate)155 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
156 {
157 switch (rate) {
158 case IB_RATE_2_5_GBPS: return 2500;
159 case IB_RATE_5_GBPS: return 5000;
160 case IB_RATE_10_GBPS: return 10000;
161 case IB_RATE_20_GBPS: return 20000;
162 case IB_RATE_30_GBPS: return 30000;
163 case IB_RATE_40_GBPS: return 40000;
164 case IB_RATE_60_GBPS: return 60000;
165 case IB_RATE_80_GBPS: return 80000;
166 case IB_RATE_120_GBPS: return 120000;
167 case IB_RATE_14_GBPS: return 14062;
168 case IB_RATE_56_GBPS: return 56250;
169 case IB_RATE_112_GBPS: return 112500;
170 case IB_RATE_168_GBPS: return 168750;
171 case IB_RATE_25_GBPS: return 25781;
172 case IB_RATE_100_GBPS: return 103125;
173 case IB_RATE_200_GBPS: return 206250;
174 case IB_RATE_300_GBPS: return 309375;
175 default: return -1;
176 }
177 }
178 EXPORT_SYMBOL(ib_rate_to_mbps);
179
180 __attribute_const__ enum rdma_transport_type
rdma_node_get_transport(enum rdma_node_type node_type)181 rdma_node_get_transport(enum rdma_node_type node_type)
182 {
183
184 if (node_type == RDMA_NODE_USNIC)
185 return RDMA_TRANSPORT_USNIC;
186 if (node_type == RDMA_NODE_USNIC_UDP)
187 return RDMA_TRANSPORT_USNIC_UDP;
188 if (node_type == RDMA_NODE_RNIC)
189 return RDMA_TRANSPORT_IWARP;
190
191 return RDMA_TRANSPORT_IB;
192 }
193 EXPORT_SYMBOL(rdma_node_get_transport);
194
rdma_port_get_link_layer(struct ib_device * device,u8 port_num)195 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
196 {
197 enum rdma_transport_type lt;
198 if (device->get_link_layer)
199 return device->get_link_layer(device, port_num);
200
201 lt = rdma_node_get_transport(device->node_type);
202 if (lt == RDMA_TRANSPORT_IB)
203 return IB_LINK_LAYER_INFINIBAND;
204
205 return IB_LINK_LAYER_ETHERNET;
206 }
207 EXPORT_SYMBOL(rdma_port_get_link_layer);
208
209 /* Protection domains */
210
211 /**
212 * ib_alloc_pd - Allocates an unused protection domain.
213 * @device: The device on which to allocate the protection domain.
214 *
215 * A protection domain object provides an association between QPs, shared
216 * receive queues, address handles, memory regions, and memory windows.
217 *
218 * Every PD has a local_dma_lkey which can be used as the lkey value for local
219 * memory operations.
220 */
__ib_alloc_pd(struct ib_device * device,unsigned int flags,const char * caller)221 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
222 const char *caller)
223 {
224 struct ib_pd *pd;
225 int mr_access_flags = 0;
226
227 pd = device->alloc_pd(device, NULL, NULL);
228 if (IS_ERR(pd))
229 return pd;
230
231 pd->device = device;
232 pd->uobject = NULL;
233 pd->__internal_mr = NULL;
234 atomic_set(&pd->usecnt, 0);
235 pd->flags = flags;
236
237 if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
238 pd->local_dma_lkey = device->local_dma_lkey;
239 else
240 mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
241
242 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
243 pr_warn("%s: enabling unsafe global rkey\n", caller);
244 mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
245 }
246
247 if (mr_access_flags) {
248 struct ib_mr *mr;
249
250 mr = pd->device->get_dma_mr(pd, mr_access_flags);
251 if (IS_ERR(mr)) {
252 ib_dealloc_pd(pd);
253 return ERR_CAST(mr);
254 }
255
256 mr->device = pd->device;
257 mr->pd = pd;
258 mr->uobject = NULL;
259 mr->need_inval = false;
260
261 pd->__internal_mr = mr;
262
263 if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
264 pd->local_dma_lkey = pd->__internal_mr->lkey;
265
266 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
267 pd->unsafe_global_rkey = pd->__internal_mr->rkey;
268 }
269
270 return pd;
271 }
272 EXPORT_SYMBOL(__ib_alloc_pd);
273
274 /**
275 * ib_dealloc_pd - Deallocates a protection domain.
276 * @pd: The protection domain to deallocate.
277 *
278 * It is an error to call this function while any resources in the pd still
279 * exist. The caller is responsible to synchronously destroy them and
280 * guarantee no new allocations will happen.
281 */
ib_dealloc_pd(struct ib_pd * pd)282 void ib_dealloc_pd(struct ib_pd *pd)
283 {
284 int ret;
285
286 if (pd->__internal_mr) {
287 ret = pd->device->dereg_mr(pd->__internal_mr);
288 WARN_ON(ret);
289 pd->__internal_mr = NULL;
290 }
291
292 /* uverbs manipulates usecnt with proper locking, while the kabi
293 requires the caller to guarantee we can't race here. */
294 WARN_ON(atomic_read(&pd->usecnt));
295
296 /* Making delalloc_pd a void return is a WIP, no driver should return
297 an error here. */
298 ret = pd->device->dealloc_pd(pd);
299 WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
300 }
301 EXPORT_SYMBOL(ib_dealloc_pd);
302
303 /* Address handles */
304
rdma_create_ah(struct ib_pd * pd,struct rdma_ah_attr * ah_attr)305 struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr)
306 {
307 struct ib_ah *ah;
308
309 ah = pd->device->create_ah(pd, ah_attr, NULL);
310
311 if (!IS_ERR(ah)) {
312 ah->device = pd->device;
313 ah->pd = pd;
314 ah->uobject = NULL;
315 ah->type = ah_attr->type;
316 atomic_inc(&pd->usecnt);
317 }
318
319 return ah;
320 }
321 EXPORT_SYMBOL(rdma_create_ah);
322
ib_get_rdma_header_version(const union rdma_network_hdr * hdr)323 int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
324 {
325 const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
326 struct iphdr ip4h_checked;
327 const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
328
329 /* If it's IPv6, the version must be 6, otherwise, the first
330 * 20 bytes (before the IPv4 header) are garbled.
331 */
332 if (ip6h->version != 6)
333 return (ip4h->version == 4) ? 4 : 0;
334 /* version may be 6 or 4 because the first 20 bytes could be garbled */
335
336 /* RoCE v2 requires no options, thus header length
337 * must be 5 words
338 */
339 if (ip4h->ihl != 5)
340 return 6;
341
342 /* Verify checksum.
343 * We can't write on scattered buffers so we need to copy to
344 * temp buffer.
345 */
346 memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
347 ip4h_checked.check = 0;
348 ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
349 /* if IPv4 header checksum is OK, believe it */
350 if (ip4h->check == ip4h_checked.check)
351 return 4;
352 return 6;
353 }
354 EXPORT_SYMBOL(ib_get_rdma_header_version);
355
ib_get_net_type_by_grh(struct ib_device * device,u8 port_num,const struct ib_grh * grh)356 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
357 u8 port_num,
358 const struct ib_grh *grh)
359 {
360 int grh_version;
361
362 if (rdma_protocol_ib(device, port_num))
363 return RDMA_NETWORK_IB;
364
365 grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
366
367 if (grh_version == 4)
368 return RDMA_NETWORK_IPV4;
369
370 if (grh->next_hdr == IPPROTO_UDP)
371 return RDMA_NETWORK_IPV6;
372
373 return RDMA_NETWORK_ROCE_V1;
374 }
375
376 struct find_gid_index_context {
377 u16 vlan_id;
378 enum ib_gid_type gid_type;
379 };
380
find_gid_index(const union ib_gid * gid,const struct ib_gid_attr * gid_attr,void * context)381 static bool find_gid_index(const union ib_gid *gid,
382 const struct ib_gid_attr *gid_attr,
383 void *context)
384 {
385 struct find_gid_index_context *ctx =
386 (struct find_gid_index_context *)context;
387
388 if (ctx->gid_type != gid_attr->gid_type)
389 return false;
390
391 if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) ||
392 (is_vlan_dev(gid_attr->ndev) &&
393 vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id))
394 return false;
395
396 return true;
397 }
398
get_sgid_index_from_eth(struct ib_device * device,u8 port_num,u16 vlan_id,const union ib_gid * sgid,enum ib_gid_type gid_type,u16 * gid_index)399 static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num,
400 u16 vlan_id, const union ib_gid *sgid,
401 enum ib_gid_type gid_type,
402 u16 *gid_index)
403 {
404 struct find_gid_index_context context = {.vlan_id = vlan_id,
405 .gid_type = gid_type};
406
407 return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index,
408 &context, gid_index);
409 }
410
ib_get_gids_from_rdma_hdr(const union rdma_network_hdr * hdr,enum rdma_network_type net_type,union ib_gid * sgid,union ib_gid * dgid)411 int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
412 enum rdma_network_type net_type,
413 union ib_gid *sgid, union ib_gid *dgid)
414 {
415 struct sockaddr_in src_in;
416 struct sockaddr_in dst_in;
417 __be32 src_saddr, dst_saddr;
418
419 if (!sgid || !dgid)
420 return -EINVAL;
421
422 if (net_type == RDMA_NETWORK_IPV4) {
423 memcpy(&src_in.sin_addr.s_addr,
424 &hdr->roce4grh.saddr, 4);
425 memcpy(&dst_in.sin_addr.s_addr,
426 &hdr->roce4grh.daddr, 4);
427 src_saddr = src_in.sin_addr.s_addr;
428 dst_saddr = dst_in.sin_addr.s_addr;
429 ipv6_addr_set_v4mapped(src_saddr,
430 (struct in6_addr *)sgid);
431 ipv6_addr_set_v4mapped(dst_saddr,
432 (struct in6_addr *)dgid);
433 return 0;
434 } else if (net_type == RDMA_NETWORK_IPV6 ||
435 net_type == RDMA_NETWORK_IB) {
436 *dgid = hdr->ibgrh.dgid;
437 *sgid = hdr->ibgrh.sgid;
438 return 0;
439 } else {
440 return -EINVAL;
441 }
442 }
443 EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
444
445 /*
446 * This function creates ah from the incoming packet.
447 * Incoming packet has dgid of the receiver node on which this code is
448 * getting executed and, sgid contains the GID of the sender.
449 *
450 * When resolving mac address of destination, the arrived dgid is used
451 * as sgid and, sgid is used as dgid because sgid contains destinations
452 * GID whom to respond to.
453 *
454 * This is why when calling rdma_addr_find_l2_eth_by_grh() function, the
455 * position of arguments dgid and sgid do not match the order of the
456 * parameters.
457 */
ib_init_ah_from_wc(struct ib_device * device,u8 port_num,const struct ib_wc * wc,const struct ib_grh * grh,struct rdma_ah_attr * ah_attr)458 int ib_init_ah_from_wc(struct ib_device *device, u8 port_num,
459 const struct ib_wc *wc, const struct ib_grh *grh,
460 struct rdma_ah_attr *ah_attr)
461 {
462 u32 flow_class;
463 u16 gid_index;
464 int ret;
465 enum rdma_network_type net_type = RDMA_NETWORK_IB;
466 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
467 int hoplimit = 0xff;
468 union ib_gid dgid;
469 union ib_gid sgid;
470
471 might_sleep();
472
473 memset(ah_attr, 0, sizeof *ah_attr);
474 ah_attr->type = rdma_ah_find_type(device, port_num);
475 if (rdma_cap_eth_ah(device, port_num)) {
476 if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
477 net_type = wc->network_hdr_type;
478 else
479 net_type = ib_get_net_type_by_grh(device, port_num, grh);
480 gid_type = ib_network_to_gid_type(net_type);
481 }
482 ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
483 &sgid, &dgid);
484 if (ret)
485 return ret;
486
487 if (rdma_protocol_roce(device, port_num)) {
488 int if_index = 0;
489 u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
490 wc->vlan_id : 0xffff;
491 struct net_device *idev;
492 struct net_device *resolved_dev;
493
494 if (!(wc->wc_flags & IB_WC_GRH))
495 return -EPROTOTYPE;
496
497 if (!device->get_netdev)
498 return -EOPNOTSUPP;
499
500 idev = device->get_netdev(device, port_num);
501 if (!idev)
502 return -ENODEV;
503
504 ret = rdma_addr_find_l2_eth_by_grh(&dgid, &sgid,
505 ah_attr->roce.dmac,
506 wc->wc_flags & IB_WC_WITH_VLAN ?
507 NULL : &vlan_id,
508 &if_index, &hoplimit);
509 if (ret) {
510 dev_put(idev);
511 return ret;
512 }
513
514 resolved_dev = dev_get_by_index(&init_net, if_index);
515 rcu_read_lock();
516 if (resolved_dev != idev && !rdma_is_upper_dev_rcu(idev,
517 resolved_dev))
518 ret = -EHOSTUNREACH;
519 rcu_read_unlock();
520 dev_put(idev);
521 dev_put(resolved_dev);
522 if (ret)
523 return ret;
524
525 ret = get_sgid_index_from_eth(device, port_num, vlan_id,
526 &dgid, gid_type, &gid_index);
527 if (ret)
528 return ret;
529 }
530
531 rdma_ah_set_dlid(ah_attr, wc->slid);
532 rdma_ah_set_sl(ah_attr, wc->sl);
533 rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
534 rdma_ah_set_port_num(ah_attr, port_num);
535
536 if (wc->wc_flags & IB_WC_GRH) {
537 if (!rdma_cap_eth_ah(device, port_num)) {
538 if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
539 ret = ib_find_cached_gid_by_port(device, &dgid,
540 IB_GID_TYPE_IB,
541 port_num, NULL,
542 &gid_index);
543 if (ret)
544 return ret;
545 } else {
546 gid_index = 0;
547 }
548 }
549
550 flow_class = be32_to_cpu(grh->version_tclass_flow);
551 rdma_ah_set_grh(ah_attr, &sgid,
552 flow_class & 0xFFFFF,
553 (u8)gid_index, hoplimit,
554 (flow_class >> 20) & 0xFF);
555
556 }
557 return 0;
558 }
559 EXPORT_SYMBOL(ib_init_ah_from_wc);
560
ib_create_ah_from_wc(struct ib_pd * pd,const struct ib_wc * wc,const struct ib_grh * grh,u8 port_num)561 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
562 const struct ib_grh *grh, u8 port_num)
563 {
564 struct rdma_ah_attr ah_attr;
565 int ret;
566
567 ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
568 if (ret)
569 return ERR_PTR(ret);
570
571 return rdma_create_ah(pd, &ah_attr);
572 }
573 EXPORT_SYMBOL(ib_create_ah_from_wc);
574
rdma_modify_ah(struct ib_ah * ah,struct rdma_ah_attr * ah_attr)575 int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
576 {
577 if (ah->type != ah_attr->type)
578 return -EINVAL;
579
580 return ah->device->modify_ah ?
581 ah->device->modify_ah(ah, ah_attr) :
582 -ENOSYS;
583 }
584 EXPORT_SYMBOL(rdma_modify_ah);
585
rdma_query_ah(struct ib_ah * ah,struct rdma_ah_attr * ah_attr)586 int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
587 {
588 return ah->device->query_ah ?
589 ah->device->query_ah(ah, ah_attr) :
590 -ENOSYS;
591 }
592 EXPORT_SYMBOL(rdma_query_ah);
593
rdma_destroy_ah(struct ib_ah * ah)594 int rdma_destroy_ah(struct ib_ah *ah)
595 {
596 struct ib_pd *pd;
597 int ret;
598
599 pd = ah->pd;
600 ret = ah->device->destroy_ah(ah);
601 if (!ret)
602 atomic_dec(&pd->usecnt);
603
604 return ret;
605 }
606 EXPORT_SYMBOL(rdma_destroy_ah);
607
608 /* Shared receive queues */
609
ib_create_srq(struct ib_pd * pd,struct ib_srq_init_attr * srq_init_attr)610 struct ib_srq *ib_create_srq(struct ib_pd *pd,
611 struct ib_srq_init_attr *srq_init_attr)
612 {
613 struct ib_srq *srq;
614
615 if (!pd->device->create_srq)
616 return ERR_PTR(-ENOSYS);
617
618 srq = pd->device->create_srq(pd, srq_init_attr, NULL);
619
620 if (!IS_ERR(srq)) {
621 srq->device = pd->device;
622 srq->pd = pd;
623 srq->uobject = NULL;
624 srq->event_handler = srq_init_attr->event_handler;
625 srq->srq_context = srq_init_attr->srq_context;
626 srq->srq_type = srq_init_attr->srq_type;
627 if (ib_srq_has_cq(srq->srq_type)) {
628 srq->ext.cq = srq_init_attr->ext.cq;
629 atomic_inc(&srq->ext.cq->usecnt);
630 }
631 if (srq->srq_type == IB_SRQT_XRC) {
632 srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
633 atomic_inc(&srq->ext.xrc.xrcd->usecnt);
634 }
635 atomic_inc(&pd->usecnt);
636 atomic_set(&srq->usecnt, 0);
637 }
638
639 return srq;
640 }
641 EXPORT_SYMBOL(ib_create_srq);
642
ib_modify_srq(struct ib_srq * srq,struct ib_srq_attr * srq_attr,enum ib_srq_attr_mask srq_attr_mask)643 int ib_modify_srq(struct ib_srq *srq,
644 struct ib_srq_attr *srq_attr,
645 enum ib_srq_attr_mask srq_attr_mask)
646 {
647 return srq->device->modify_srq ?
648 srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
649 -ENOSYS;
650 }
651 EXPORT_SYMBOL(ib_modify_srq);
652
ib_query_srq(struct ib_srq * srq,struct ib_srq_attr * srq_attr)653 int ib_query_srq(struct ib_srq *srq,
654 struct ib_srq_attr *srq_attr)
655 {
656 return srq->device->query_srq ?
657 srq->device->query_srq(srq, srq_attr) : -ENOSYS;
658 }
659 EXPORT_SYMBOL(ib_query_srq);
660
ib_destroy_srq(struct ib_srq * srq)661 int ib_destroy_srq(struct ib_srq *srq)
662 {
663 struct ib_pd *pd;
664 enum ib_srq_type srq_type;
665 struct ib_xrcd *uninitialized_var(xrcd);
666 struct ib_cq *uninitialized_var(cq);
667 int ret;
668
669 if (atomic_read(&srq->usecnt))
670 return -EBUSY;
671
672 pd = srq->pd;
673 srq_type = srq->srq_type;
674 if (ib_srq_has_cq(srq_type))
675 cq = srq->ext.cq;
676 if (srq_type == IB_SRQT_XRC)
677 xrcd = srq->ext.xrc.xrcd;
678
679 ret = srq->device->destroy_srq(srq);
680 if (!ret) {
681 atomic_dec(&pd->usecnt);
682 if (srq_type == IB_SRQT_XRC)
683 atomic_dec(&xrcd->usecnt);
684 if (ib_srq_has_cq(srq_type))
685 atomic_dec(&cq->usecnt);
686 }
687
688 return ret;
689 }
690 EXPORT_SYMBOL(ib_destroy_srq);
691
692 /* Queue pairs */
693
__ib_shared_qp_event_handler(struct ib_event * event,void * context)694 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
695 {
696 struct ib_qp *qp = context;
697 unsigned long flags;
698
699 spin_lock_irqsave(&qp->device->event_handler_lock, flags);
700 list_for_each_entry(event->element.qp, &qp->open_list, open_list)
701 if (event->element.qp->event_handler)
702 event->element.qp->event_handler(event, event->element.qp->qp_context);
703 spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
704 }
705
__ib_insert_xrcd_qp(struct ib_xrcd * xrcd,struct ib_qp * qp)706 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
707 {
708 mutex_lock(&xrcd->tgt_qp_mutex);
709 list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
710 mutex_unlock(&xrcd->tgt_qp_mutex);
711 }
712
__ib_open_qp(struct ib_qp * real_qp,void (* event_handler)(struct ib_event *,void *),void * qp_context)713 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
714 void (*event_handler)(struct ib_event *, void *),
715 void *qp_context)
716 {
717 struct ib_qp *qp;
718 unsigned long flags;
719 int err;
720
721 qp = kzalloc(sizeof *qp, GFP_KERNEL);
722 if (!qp)
723 return ERR_PTR(-ENOMEM);
724
725 qp->real_qp = real_qp;
726 err = ib_open_shared_qp_security(qp, real_qp->device);
727 if (err) {
728 kfree(qp);
729 return ERR_PTR(err);
730 }
731
732 qp->real_qp = real_qp;
733 atomic_inc(&real_qp->usecnt);
734 qp->device = real_qp->device;
735 qp->event_handler = event_handler;
736 qp->qp_context = qp_context;
737 qp->qp_num = real_qp->qp_num;
738 qp->qp_type = real_qp->qp_type;
739
740 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
741 list_add(&qp->open_list, &real_qp->open_list);
742 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
743
744 return qp;
745 }
746
ib_open_qp(struct ib_xrcd * xrcd,struct ib_qp_open_attr * qp_open_attr)747 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
748 struct ib_qp_open_attr *qp_open_attr)
749 {
750 struct ib_qp *qp, *real_qp;
751
752 if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
753 return ERR_PTR(-EINVAL);
754
755 qp = ERR_PTR(-EINVAL);
756 mutex_lock(&xrcd->tgt_qp_mutex);
757 list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
758 if (real_qp->qp_num == qp_open_attr->qp_num) {
759 qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
760 qp_open_attr->qp_context);
761 break;
762 }
763 }
764 mutex_unlock(&xrcd->tgt_qp_mutex);
765 return qp;
766 }
767 EXPORT_SYMBOL(ib_open_qp);
768
create_xrc_qp(struct ib_qp * qp,struct ib_qp_init_attr * qp_init_attr)769 static struct ib_qp *create_xrc_qp(struct ib_qp *qp,
770 struct ib_qp_init_attr *qp_init_attr)
771 {
772 struct ib_qp *real_qp = qp;
773
774 qp->event_handler = __ib_shared_qp_event_handler;
775 qp->qp_context = qp;
776 qp->pd = NULL;
777 qp->send_cq = qp->recv_cq = NULL;
778 qp->srq = NULL;
779 qp->xrcd = qp_init_attr->xrcd;
780 atomic_inc(&qp_init_attr->xrcd->usecnt);
781 INIT_LIST_HEAD(&qp->open_list);
782
783 qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
784 qp_init_attr->qp_context);
785 if (IS_ERR(qp))
786 return qp;
787
788 __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
789 return qp;
790 }
791
ib_create_qp(struct ib_pd * pd,struct ib_qp_init_attr * qp_init_attr)792 struct ib_qp *ib_create_qp(struct ib_pd *pd,
793 struct ib_qp_init_attr *qp_init_attr)
794 {
795 struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
796 struct ib_qp *qp;
797 int ret;
798
799 if (qp_init_attr->rwq_ind_tbl &&
800 (qp_init_attr->recv_cq ||
801 qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
802 qp_init_attr->cap.max_recv_sge))
803 return ERR_PTR(-EINVAL);
804
805 /*
806 * If the callers is using the RDMA API calculate the resources
807 * needed for the RDMA READ/WRITE operations.
808 *
809 * Note that these callers need to pass in a port number.
810 */
811 if (qp_init_attr->cap.max_rdma_ctxs)
812 rdma_rw_init_qp(device, qp_init_attr);
813
814 qp = device->create_qp(pd, qp_init_attr, NULL);
815 if (IS_ERR(qp))
816 return qp;
817
818 ret = ib_create_qp_security(qp, device);
819 if (ret)
820 goto err;
821
822 qp->device = device;
823 qp->real_qp = qp;
824 qp->uobject = NULL;
825 qp->qp_type = qp_init_attr->qp_type;
826 qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
827
828 atomic_set(&qp->usecnt, 0);
829 qp->mrs_used = 0;
830 spin_lock_init(&qp->mr_lock);
831 INIT_LIST_HEAD(&qp->rdma_mrs);
832 INIT_LIST_HEAD(&qp->sig_mrs);
833 qp->port = 0;
834
835 if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
836 struct ib_qp *xrc_qp = create_xrc_qp(qp, qp_init_attr);
837
838 if (IS_ERR(xrc_qp)) {
839 ret = PTR_ERR(xrc_qp);
840 goto err;
841 }
842 return xrc_qp;
843 }
844
845 qp->event_handler = qp_init_attr->event_handler;
846 qp->qp_context = qp_init_attr->qp_context;
847 if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
848 qp->recv_cq = NULL;
849 qp->srq = NULL;
850 } else {
851 qp->recv_cq = qp_init_attr->recv_cq;
852 if (qp_init_attr->recv_cq)
853 atomic_inc(&qp_init_attr->recv_cq->usecnt);
854 qp->srq = qp_init_attr->srq;
855 if (qp->srq)
856 atomic_inc(&qp_init_attr->srq->usecnt);
857 }
858
859 qp->pd = pd;
860 qp->send_cq = qp_init_attr->send_cq;
861 qp->xrcd = NULL;
862
863 atomic_inc(&pd->usecnt);
864 if (qp_init_attr->send_cq)
865 atomic_inc(&qp_init_attr->send_cq->usecnt);
866 if (qp_init_attr->rwq_ind_tbl)
867 atomic_inc(&qp->rwq_ind_tbl->usecnt);
868
869 if (qp_init_attr->cap.max_rdma_ctxs) {
870 ret = rdma_rw_init_mrs(qp, qp_init_attr);
871 if (ret)
872 goto err;
873 }
874
875 /*
876 * Note: all hw drivers guarantee that max_send_sge is lower than
877 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
878 * max_send_sge <= max_sge_rd.
879 */
880 qp->max_write_sge = qp_init_attr->cap.max_send_sge;
881 qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
882 device->attrs.max_sge_rd);
883
884 return qp;
885
886 err:
887 ib_destroy_qp(qp);
888 return ERR_PTR(ret);
889
890 }
891 EXPORT_SYMBOL(ib_create_qp);
892
893 static const struct {
894 int valid;
895 enum ib_qp_attr_mask req_param[IB_QPT_MAX];
896 enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
897 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
898 [IB_QPS_RESET] = {
899 [IB_QPS_RESET] = { .valid = 1 },
900 [IB_QPS_INIT] = {
901 .valid = 1,
902 .req_param = {
903 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
904 IB_QP_PORT |
905 IB_QP_QKEY),
906 [IB_QPT_RAW_PACKET] = IB_QP_PORT,
907 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
908 IB_QP_PORT |
909 IB_QP_ACCESS_FLAGS),
910 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
911 IB_QP_PORT |
912 IB_QP_ACCESS_FLAGS),
913 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
914 IB_QP_PORT |
915 IB_QP_ACCESS_FLAGS),
916 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
917 IB_QP_PORT |
918 IB_QP_ACCESS_FLAGS),
919 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
920 IB_QP_QKEY),
921 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
922 IB_QP_QKEY),
923 }
924 },
925 },
926 [IB_QPS_INIT] = {
927 [IB_QPS_RESET] = { .valid = 1 },
928 [IB_QPS_ERR] = { .valid = 1 },
929 [IB_QPS_INIT] = {
930 .valid = 1,
931 .opt_param = {
932 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
933 IB_QP_PORT |
934 IB_QP_QKEY),
935 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
936 IB_QP_PORT |
937 IB_QP_ACCESS_FLAGS),
938 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
939 IB_QP_PORT |
940 IB_QP_ACCESS_FLAGS),
941 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
942 IB_QP_PORT |
943 IB_QP_ACCESS_FLAGS),
944 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
945 IB_QP_PORT |
946 IB_QP_ACCESS_FLAGS),
947 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
948 IB_QP_QKEY),
949 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
950 IB_QP_QKEY),
951 }
952 },
953 [IB_QPS_RTR] = {
954 .valid = 1,
955 .req_param = {
956 [IB_QPT_UC] = (IB_QP_AV |
957 IB_QP_PATH_MTU |
958 IB_QP_DEST_QPN |
959 IB_QP_RQ_PSN),
960 [IB_QPT_RC] = (IB_QP_AV |
961 IB_QP_PATH_MTU |
962 IB_QP_DEST_QPN |
963 IB_QP_RQ_PSN |
964 IB_QP_MAX_DEST_RD_ATOMIC |
965 IB_QP_MIN_RNR_TIMER),
966 [IB_QPT_XRC_INI] = (IB_QP_AV |
967 IB_QP_PATH_MTU |
968 IB_QP_DEST_QPN |
969 IB_QP_RQ_PSN),
970 [IB_QPT_XRC_TGT] = (IB_QP_AV |
971 IB_QP_PATH_MTU |
972 IB_QP_DEST_QPN |
973 IB_QP_RQ_PSN |
974 IB_QP_MAX_DEST_RD_ATOMIC |
975 IB_QP_MIN_RNR_TIMER),
976 },
977 .opt_param = {
978 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
979 IB_QP_QKEY),
980 [IB_QPT_UC] = (IB_QP_ALT_PATH |
981 IB_QP_ACCESS_FLAGS |
982 IB_QP_PKEY_INDEX),
983 [IB_QPT_RC] = (IB_QP_ALT_PATH |
984 IB_QP_ACCESS_FLAGS |
985 IB_QP_PKEY_INDEX),
986 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
987 IB_QP_ACCESS_FLAGS |
988 IB_QP_PKEY_INDEX),
989 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
990 IB_QP_ACCESS_FLAGS |
991 IB_QP_PKEY_INDEX),
992 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
993 IB_QP_QKEY),
994 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
995 IB_QP_QKEY),
996 },
997 },
998 },
999 [IB_QPS_RTR] = {
1000 [IB_QPS_RESET] = { .valid = 1 },
1001 [IB_QPS_ERR] = { .valid = 1 },
1002 [IB_QPS_RTS] = {
1003 .valid = 1,
1004 .req_param = {
1005 [IB_QPT_UD] = IB_QP_SQ_PSN,
1006 [IB_QPT_UC] = IB_QP_SQ_PSN,
1007 [IB_QPT_RC] = (IB_QP_TIMEOUT |
1008 IB_QP_RETRY_CNT |
1009 IB_QP_RNR_RETRY |
1010 IB_QP_SQ_PSN |
1011 IB_QP_MAX_QP_RD_ATOMIC),
1012 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
1013 IB_QP_RETRY_CNT |
1014 IB_QP_RNR_RETRY |
1015 IB_QP_SQ_PSN |
1016 IB_QP_MAX_QP_RD_ATOMIC),
1017 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
1018 IB_QP_SQ_PSN),
1019 [IB_QPT_SMI] = IB_QP_SQ_PSN,
1020 [IB_QPT_GSI] = IB_QP_SQ_PSN,
1021 },
1022 .opt_param = {
1023 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1024 IB_QP_QKEY),
1025 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1026 IB_QP_ALT_PATH |
1027 IB_QP_ACCESS_FLAGS |
1028 IB_QP_PATH_MIG_STATE),
1029 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1030 IB_QP_ALT_PATH |
1031 IB_QP_ACCESS_FLAGS |
1032 IB_QP_MIN_RNR_TIMER |
1033 IB_QP_PATH_MIG_STATE),
1034 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1035 IB_QP_ALT_PATH |
1036 IB_QP_ACCESS_FLAGS |
1037 IB_QP_PATH_MIG_STATE),
1038 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1039 IB_QP_ALT_PATH |
1040 IB_QP_ACCESS_FLAGS |
1041 IB_QP_MIN_RNR_TIMER |
1042 IB_QP_PATH_MIG_STATE),
1043 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1044 IB_QP_QKEY),
1045 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1046 IB_QP_QKEY),
1047 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1048 }
1049 }
1050 },
1051 [IB_QPS_RTS] = {
1052 [IB_QPS_RESET] = { .valid = 1 },
1053 [IB_QPS_ERR] = { .valid = 1 },
1054 [IB_QPS_RTS] = {
1055 .valid = 1,
1056 .opt_param = {
1057 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1058 IB_QP_QKEY),
1059 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1060 IB_QP_ACCESS_FLAGS |
1061 IB_QP_ALT_PATH |
1062 IB_QP_PATH_MIG_STATE),
1063 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1064 IB_QP_ACCESS_FLAGS |
1065 IB_QP_ALT_PATH |
1066 IB_QP_PATH_MIG_STATE |
1067 IB_QP_MIN_RNR_TIMER),
1068 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1069 IB_QP_ACCESS_FLAGS |
1070 IB_QP_ALT_PATH |
1071 IB_QP_PATH_MIG_STATE),
1072 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1073 IB_QP_ACCESS_FLAGS |
1074 IB_QP_ALT_PATH |
1075 IB_QP_PATH_MIG_STATE |
1076 IB_QP_MIN_RNR_TIMER),
1077 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1078 IB_QP_QKEY),
1079 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1080 IB_QP_QKEY),
1081 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1082 }
1083 },
1084 [IB_QPS_SQD] = {
1085 .valid = 1,
1086 .opt_param = {
1087 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1088 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1089 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1090 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1091 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1092 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1093 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1094 }
1095 },
1096 },
1097 [IB_QPS_SQD] = {
1098 [IB_QPS_RESET] = { .valid = 1 },
1099 [IB_QPS_ERR] = { .valid = 1 },
1100 [IB_QPS_RTS] = {
1101 .valid = 1,
1102 .opt_param = {
1103 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1104 IB_QP_QKEY),
1105 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1106 IB_QP_ALT_PATH |
1107 IB_QP_ACCESS_FLAGS |
1108 IB_QP_PATH_MIG_STATE),
1109 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1110 IB_QP_ALT_PATH |
1111 IB_QP_ACCESS_FLAGS |
1112 IB_QP_MIN_RNR_TIMER |
1113 IB_QP_PATH_MIG_STATE),
1114 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1115 IB_QP_ALT_PATH |
1116 IB_QP_ACCESS_FLAGS |
1117 IB_QP_PATH_MIG_STATE),
1118 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1119 IB_QP_ALT_PATH |
1120 IB_QP_ACCESS_FLAGS |
1121 IB_QP_MIN_RNR_TIMER |
1122 IB_QP_PATH_MIG_STATE),
1123 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1124 IB_QP_QKEY),
1125 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1126 IB_QP_QKEY),
1127 }
1128 },
1129 [IB_QPS_SQD] = {
1130 .valid = 1,
1131 .opt_param = {
1132 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1133 IB_QP_QKEY),
1134 [IB_QPT_UC] = (IB_QP_AV |
1135 IB_QP_ALT_PATH |
1136 IB_QP_ACCESS_FLAGS |
1137 IB_QP_PKEY_INDEX |
1138 IB_QP_PATH_MIG_STATE),
1139 [IB_QPT_RC] = (IB_QP_PORT |
1140 IB_QP_AV |
1141 IB_QP_TIMEOUT |
1142 IB_QP_RETRY_CNT |
1143 IB_QP_RNR_RETRY |
1144 IB_QP_MAX_QP_RD_ATOMIC |
1145 IB_QP_MAX_DEST_RD_ATOMIC |
1146 IB_QP_ALT_PATH |
1147 IB_QP_ACCESS_FLAGS |
1148 IB_QP_PKEY_INDEX |
1149 IB_QP_MIN_RNR_TIMER |
1150 IB_QP_PATH_MIG_STATE),
1151 [IB_QPT_XRC_INI] = (IB_QP_PORT |
1152 IB_QP_AV |
1153 IB_QP_TIMEOUT |
1154 IB_QP_RETRY_CNT |
1155 IB_QP_RNR_RETRY |
1156 IB_QP_MAX_QP_RD_ATOMIC |
1157 IB_QP_ALT_PATH |
1158 IB_QP_ACCESS_FLAGS |
1159 IB_QP_PKEY_INDEX |
1160 IB_QP_PATH_MIG_STATE),
1161 [IB_QPT_XRC_TGT] = (IB_QP_PORT |
1162 IB_QP_AV |
1163 IB_QP_TIMEOUT |
1164 IB_QP_MAX_DEST_RD_ATOMIC |
1165 IB_QP_ALT_PATH |
1166 IB_QP_ACCESS_FLAGS |
1167 IB_QP_PKEY_INDEX |
1168 IB_QP_MIN_RNR_TIMER |
1169 IB_QP_PATH_MIG_STATE),
1170 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1171 IB_QP_QKEY),
1172 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1173 IB_QP_QKEY),
1174 }
1175 }
1176 },
1177 [IB_QPS_SQE] = {
1178 [IB_QPS_RESET] = { .valid = 1 },
1179 [IB_QPS_ERR] = { .valid = 1 },
1180 [IB_QPS_RTS] = {
1181 .valid = 1,
1182 .opt_param = {
1183 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1184 IB_QP_QKEY),
1185 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1186 IB_QP_ACCESS_FLAGS),
1187 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1188 IB_QP_QKEY),
1189 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1190 IB_QP_QKEY),
1191 }
1192 }
1193 },
1194 [IB_QPS_ERR] = {
1195 [IB_QPS_RESET] = { .valid = 1 },
1196 [IB_QPS_ERR] = { .valid = 1 }
1197 }
1198 };
1199
ib_modify_qp_is_ok(enum ib_qp_state cur_state,enum ib_qp_state next_state,enum ib_qp_type type,enum ib_qp_attr_mask mask,enum rdma_link_layer ll)1200 int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1201 enum ib_qp_type type, enum ib_qp_attr_mask mask,
1202 enum rdma_link_layer ll)
1203 {
1204 enum ib_qp_attr_mask req_param, opt_param;
1205
1206 if (cur_state < 0 || cur_state > IB_QPS_ERR ||
1207 next_state < 0 || next_state > IB_QPS_ERR)
1208 return 0;
1209
1210 if (mask & IB_QP_CUR_STATE &&
1211 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1212 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1213 return 0;
1214
1215 if (!qp_state_table[cur_state][next_state].valid)
1216 return 0;
1217
1218 req_param = qp_state_table[cur_state][next_state].req_param[type];
1219 opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1220
1221 if ((mask & req_param) != req_param)
1222 return 0;
1223
1224 if (mask & ~(req_param | opt_param | IB_QP_STATE))
1225 return 0;
1226
1227 return 1;
1228 }
1229 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1230
ib_resolve_eth_dmac(struct ib_device * device,struct rdma_ah_attr * ah_attr)1231 int ib_resolve_eth_dmac(struct ib_device *device,
1232 struct rdma_ah_attr *ah_attr)
1233 {
1234 int ret = 0;
1235 struct ib_global_route *grh;
1236
1237 if (!rdma_is_port_valid(device, rdma_ah_get_port_num(ah_attr)))
1238 return -EINVAL;
1239
1240 if (ah_attr->type != RDMA_AH_ATTR_TYPE_ROCE)
1241 return 0;
1242
1243 grh = rdma_ah_retrieve_grh(ah_attr);
1244
1245 if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw)) {
1246 rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
1247 ah_attr->roce.dmac);
1248 return 0;
1249 }
1250 if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1251 if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1252 __be32 addr = 0;
1253
1254 memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
1255 ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
1256 } else {
1257 ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
1258 (char *)ah_attr->roce.dmac);
1259 }
1260 } else {
1261 union ib_gid sgid;
1262 struct ib_gid_attr sgid_attr;
1263 int ifindex;
1264 int hop_limit;
1265
1266 ret = ib_query_gid(device,
1267 rdma_ah_get_port_num(ah_attr),
1268 grh->sgid_index,
1269 &sgid, &sgid_attr);
1270
1271 if (ret || !sgid_attr.ndev) {
1272 if (!ret)
1273 ret = -ENXIO;
1274 goto out;
1275 }
1276
1277 ifindex = sgid_attr.ndev->ifindex;
1278
1279 ret =
1280 rdma_addr_find_l2_eth_by_grh(&sgid, &grh->dgid,
1281 ah_attr->roce.dmac,
1282 NULL, &ifindex, &hop_limit);
1283
1284 dev_put(sgid_attr.ndev);
1285
1286 grh->hop_limit = hop_limit;
1287 }
1288 out:
1289 return ret;
1290 }
1291 EXPORT_SYMBOL(ib_resolve_eth_dmac);
1292
1293 /**
1294 * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
1295 * @ib_qp: The QP to modify.
1296 * @attr: On input, specifies the QP attributes to modify. On output,
1297 * the current values of selected QP attributes are returned.
1298 * @attr_mask: A bit-mask used to specify which attributes of the QP
1299 * are being modified.
1300 * @udata: pointer to user's input output buffer information
1301 * are being modified.
1302 * It returns 0 on success and returns appropriate error code on error.
1303 */
ib_modify_qp_with_udata(struct ib_qp * ib_qp,struct ib_qp_attr * attr,int attr_mask,struct ib_udata * udata)1304 int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
1305 int attr_mask, struct ib_udata *udata)
1306 {
1307 struct ib_qp *qp = ib_qp->real_qp;
1308 int ret;
1309
1310 if (attr_mask & IB_QP_AV) {
1311 ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
1312 if (ret)
1313 return ret;
1314 }
1315 ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
1316 if (!ret && (attr_mask & IB_QP_PORT))
1317 qp->port = attr->port_num;
1318
1319 return ret;
1320 }
1321 EXPORT_SYMBOL(ib_modify_qp_with_udata);
1322
ib_get_eth_speed(struct ib_device * dev,u8 port_num,u8 * speed,u8 * width)1323 int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
1324 {
1325 int rc;
1326 u32 netdev_speed;
1327 struct net_device *netdev;
1328 struct ethtool_link_ksettings lksettings;
1329
1330 if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
1331 return -EINVAL;
1332
1333 if (!dev->get_netdev)
1334 return -EOPNOTSUPP;
1335
1336 netdev = dev->get_netdev(dev, port_num);
1337 if (!netdev)
1338 return -ENODEV;
1339
1340 rtnl_lock();
1341 rc = __ethtool_get_link_ksettings(netdev, &lksettings);
1342 rtnl_unlock();
1343
1344 dev_put(netdev);
1345
1346 if (!rc) {
1347 netdev_speed = lksettings.base.speed;
1348 } else {
1349 netdev_speed = SPEED_1000;
1350 pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
1351 netdev_speed);
1352 }
1353
1354 if (netdev_speed <= SPEED_1000) {
1355 *width = IB_WIDTH_1X;
1356 *speed = IB_SPEED_SDR;
1357 } else if (netdev_speed <= SPEED_10000) {
1358 *width = IB_WIDTH_1X;
1359 *speed = IB_SPEED_FDR10;
1360 } else if (netdev_speed <= SPEED_20000) {
1361 *width = IB_WIDTH_4X;
1362 *speed = IB_SPEED_DDR;
1363 } else if (netdev_speed <= SPEED_25000) {
1364 *width = IB_WIDTH_1X;
1365 *speed = IB_SPEED_EDR;
1366 } else if (netdev_speed <= SPEED_40000) {
1367 *width = IB_WIDTH_4X;
1368 *speed = IB_SPEED_FDR10;
1369 } else {
1370 *width = IB_WIDTH_4X;
1371 *speed = IB_SPEED_EDR;
1372 }
1373
1374 return 0;
1375 }
1376 EXPORT_SYMBOL(ib_get_eth_speed);
1377
ib_modify_qp(struct ib_qp * qp,struct ib_qp_attr * qp_attr,int qp_attr_mask)1378 int ib_modify_qp(struct ib_qp *qp,
1379 struct ib_qp_attr *qp_attr,
1380 int qp_attr_mask)
1381 {
1382 return ib_modify_qp_with_udata(qp, qp_attr, qp_attr_mask, NULL);
1383 }
1384 EXPORT_SYMBOL(ib_modify_qp);
1385
ib_query_qp(struct ib_qp * qp,struct ib_qp_attr * qp_attr,int qp_attr_mask,struct ib_qp_init_attr * qp_init_attr)1386 int ib_query_qp(struct ib_qp *qp,
1387 struct ib_qp_attr *qp_attr,
1388 int qp_attr_mask,
1389 struct ib_qp_init_attr *qp_init_attr)
1390 {
1391 return qp->device->query_qp ?
1392 qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
1393 -ENOSYS;
1394 }
1395 EXPORT_SYMBOL(ib_query_qp);
1396
ib_close_qp(struct ib_qp * qp)1397 int ib_close_qp(struct ib_qp *qp)
1398 {
1399 struct ib_qp *real_qp;
1400 unsigned long flags;
1401
1402 real_qp = qp->real_qp;
1403 if (real_qp == qp)
1404 return -EINVAL;
1405
1406 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1407 list_del(&qp->open_list);
1408 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1409
1410 atomic_dec(&real_qp->usecnt);
1411 if (qp->qp_sec)
1412 ib_close_shared_qp_security(qp->qp_sec);
1413 kfree(qp);
1414
1415 return 0;
1416 }
1417 EXPORT_SYMBOL(ib_close_qp);
1418
__ib_destroy_shared_qp(struct ib_qp * qp)1419 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1420 {
1421 struct ib_xrcd *xrcd;
1422 struct ib_qp *real_qp;
1423 int ret;
1424
1425 real_qp = qp->real_qp;
1426 xrcd = real_qp->xrcd;
1427
1428 mutex_lock(&xrcd->tgt_qp_mutex);
1429 ib_close_qp(qp);
1430 if (atomic_read(&real_qp->usecnt) == 0)
1431 list_del(&real_qp->xrcd_list);
1432 else
1433 real_qp = NULL;
1434 mutex_unlock(&xrcd->tgt_qp_mutex);
1435
1436 if (real_qp) {
1437 ret = ib_destroy_qp(real_qp);
1438 if (!ret)
1439 atomic_dec(&xrcd->usecnt);
1440 else
1441 __ib_insert_xrcd_qp(xrcd, real_qp);
1442 }
1443
1444 return 0;
1445 }
1446
ib_destroy_qp(struct ib_qp * qp)1447 int ib_destroy_qp(struct ib_qp *qp)
1448 {
1449 struct ib_pd *pd;
1450 struct ib_cq *scq, *rcq;
1451 struct ib_srq *srq;
1452 struct ib_rwq_ind_table *ind_tbl;
1453 struct ib_qp_security *sec;
1454 int ret;
1455
1456 WARN_ON_ONCE(qp->mrs_used > 0);
1457
1458 if (atomic_read(&qp->usecnt))
1459 return -EBUSY;
1460
1461 if (qp->real_qp != qp)
1462 return __ib_destroy_shared_qp(qp);
1463
1464 pd = qp->pd;
1465 scq = qp->send_cq;
1466 rcq = qp->recv_cq;
1467 srq = qp->srq;
1468 ind_tbl = qp->rwq_ind_tbl;
1469 sec = qp->qp_sec;
1470 if (sec)
1471 ib_destroy_qp_security_begin(sec);
1472
1473 if (!qp->uobject)
1474 rdma_rw_cleanup_mrs(qp);
1475
1476 ret = qp->device->destroy_qp(qp);
1477 if (!ret) {
1478 if (pd)
1479 atomic_dec(&pd->usecnt);
1480 if (scq)
1481 atomic_dec(&scq->usecnt);
1482 if (rcq)
1483 atomic_dec(&rcq->usecnt);
1484 if (srq)
1485 atomic_dec(&srq->usecnt);
1486 if (ind_tbl)
1487 atomic_dec(&ind_tbl->usecnt);
1488 if (sec)
1489 ib_destroy_qp_security_end(sec);
1490 } else {
1491 if (sec)
1492 ib_destroy_qp_security_abort(sec);
1493 }
1494
1495 return ret;
1496 }
1497 EXPORT_SYMBOL(ib_destroy_qp);
1498
1499 /* Completion queues */
1500
ib_create_cq(struct ib_device * device,ib_comp_handler comp_handler,void (* event_handler)(struct ib_event *,void *),void * cq_context,const struct ib_cq_init_attr * cq_attr)1501 struct ib_cq *ib_create_cq(struct ib_device *device,
1502 ib_comp_handler comp_handler,
1503 void (*event_handler)(struct ib_event *, void *),
1504 void *cq_context,
1505 const struct ib_cq_init_attr *cq_attr)
1506 {
1507 struct ib_cq *cq;
1508
1509 cq = device->create_cq(device, cq_attr, NULL, NULL);
1510
1511 if (!IS_ERR(cq)) {
1512 cq->device = device;
1513 cq->uobject = NULL;
1514 cq->comp_handler = comp_handler;
1515 cq->event_handler = event_handler;
1516 cq->cq_context = cq_context;
1517 atomic_set(&cq->usecnt, 0);
1518 }
1519
1520 return cq;
1521 }
1522 EXPORT_SYMBOL(ib_create_cq);
1523
ib_modify_cq(struct ib_cq * cq,u16 cq_count,u16 cq_period)1524 int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1525 {
1526 return cq->device->modify_cq ?
1527 cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
1528 }
1529 EXPORT_SYMBOL(ib_modify_cq);
1530
ib_destroy_cq(struct ib_cq * cq)1531 int ib_destroy_cq(struct ib_cq *cq)
1532 {
1533 if (atomic_read(&cq->usecnt))
1534 return -EBUSY;
1535
1536 return cq->device->destroy_cq(cq);
1537 }
1538 EXPORT_SYMBOL(ib_destroy_cq);
1539
ib_resize_cq(struct ib_cq * cq,int cqe)1540 int ib_resize_cq(struct ib_cq *cq, int cqe)
1541 {
1542 return cq->device->resize_cq ?
1543 cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
1544 }
1545 EXPORT_SYMBOL(ib_resize_cq);
1546
1547 /* Memory regions */
1548
ib_dereg_mr(struct ib_mr * mr)1549 int ib_dereg_mr(struct ib_mr *mr)
1550 {
1551 struct ib_pd *pd = mr->pd;
1552 int ret;
1553
1554 ret = mr->device->dereg_mr(mr);
1555 if (!ret)
1556 atomic_dec(&pd->usecnt);
1557
1558 return ret;
1559 }
1560 EXPORT_SYMBOL(ib_dereg_mr);
1561
1562 /**
1563 * ib_alloc_mr() - Allocates a memory region
1564 * @pd: protection domain associated with the region
1565 * @mr_type: memory region type
1566 * @max_num_sg: maximum sg entries available for registration.
1567 *
1568 * Notes:
1569 * Memory registeration page/sg lists must not exceed max_num_sg.
1570 * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
1571 * max_num_sg * used_page_size.
1572 *
1573 */
ib_alloc_mr(struct ib_pd * pd,enum ib_mr_type mr_type,u32 max_num_sg)1574 struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
1575 enum ib_mr_type mr_type,
1576 u32 max_num_sg)
1577 {
1578 struct ib_mr *mr;
1579
1580 if (!pd->device->alloc_mr)
1581 return ERR_PTR(-ENOSYS);
1582
1583 mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
1584 if (!IS_ERR(mr)) {
1585 mr->device = pd->device;
1586 mr->pd = pd;
1587 mr->uobject = NULL;
1588 atomic_inc(&pd->usecnt);
1589 mr->need_inval = false;
1590 }
1591
1592 return mr;
1593 }
1594 EXPORT_SYMBOL(ib_alloc_mr);
1595
1596 /* "Fast" memory regions */
1597
ib_alloc_fmr(struct ib_pd * pd,int mr_access_flags,struct ib_fmr_attr * fmr_attr)1598 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1599 int mr_access_flags,
1600 struct ib_fmr_attr *fmr_attr)
1601 {
1602 struct ib_fmr *fmr;
1603
1604 if (!pd->device->alloc_fmr)
1605 return ERR_PTR(-ENOSYS);
1606
1607 fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
1608 if (!IS_ERR(fmr)) {
1609 fmr->device = pd->device;
1610 fmr->pd = pd;
1611 atomic_inc(&pd->usecnt);
1612 }
1613
1614 return fmr;
1615 }
1616 EXPORT_SYMBOL(ib_alloc_fmr);
1617
ib_unmap_fmr(struct list_head * fmr_list)1618 int ib_unmap_fmr(struct list_head *fmr_list)
1619 {
1620 struct ib_fmr *fmr;
1621
1622 if (list_empty(fmr_list))
1623 return 0;
1624
1625 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
1626 return fmr->device->unmap_fmr(fmr_list);
1627 }
1628 EXPORT_SYMBOL(ib_unmap_fmr);
1629
ib_dealloc_fmr(struct ib_fmr * fmr)1630 int ib_dealloc_fmr(struct ib_fmr *fmr)
1631 {
1632 struct ib_pd *pd;
1633 int ret;
1634
1635 pd = fmr->pd;
1636 ret = fmr->device->dealloc_fmr(fmr);
1637 if (!ret)
1638 atomic_dec(&pd->usecnt);
1639
1640 return ret;
1641 }
1642 EXPORT_SYMBOL(ib_dealloc_fmr);
1643
1644 /* Multicast groups */
1645
is_valid_mcast_lid(struct ib_qp * qp,u16 lid)1646 static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
1647 {
1648 struct ib_qp_init_attr init_attr = {};
1649 struct ib_qp_attr attr = {};
1650 int num_eth_ports = 0;
1651 int port;
1652
1653 /* If QP state >= init, it is assigned to a port and we can check this
1654 * port only.
1655 */
1656 if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
1657 if (attr.qp_state >= IB_QPS_INIT) {
1658 if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
1659 IB_LINK_LAYER_INFINIBAND)
1660 return true;
1661 goto lid_check;
1662 }
1663 }
1664
1665 /* Can't get a quick answer, iterate over all ports */
1666 for (port = 0; port < qp->device->phys_port_cnt; port++)
1667 if (rdma_port_get_link_layer(qp->device, port) !=
1668 IB_LINK_LAYER_INFINIBAND)
1669 num_eth_ports++;
1670
1671 /* If we have at lease one Ethernet port, RoCE annex declares that
1672 * multicast LID should be ignored. We can't tell at this step if the
1673 * QP belongs to an IB or Ethernet port.
1674 */
1675 if (num_eth_ports)
1676 return true;
1677
1678 /* If all the ports are IB, we can check according to IB spec. */
1679 lid_check:
1680 return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
1681 lid == be16_to_cpu(IB_LID_PERMISSIVE));
1682 }
1683
ib_attach_mcast(struct ib_qp * qp,union ib_gid * gid,u16 lid)1684 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1685 {
1686 int ret;
1687
1688 if (!qp->device->attach_mcast)
1689 return -ENOSYS;
1690
1691 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
1692 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
1693 return -EINVAL;
1694
1695 ret = qp->device->attach_mcast(qp, gid, lid);
1696 if (!ret)
1697 atomic_inc(&qp->usecnt);
1698 return ret;
1699 }
1700 EXPORT_SYMBOL(ib_attach_mcast);
1701
ib_detach_mcast(struct ib_qp * qp,union ib_gid * gid,u16 lid)1702 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1703 {
1704 int ret;
1705
1706 if (!qp->device->detach_mcast)
1707 return -ENOSYS;
1708
1709 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
1710 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
1711 return -EINVAL;
1712
1713 ret = qp->device->detach_mcast(qp, gid, lid);
1714 if (!ret)
1715 atomic_dec(&qp->usecnt);
1716 return ret;
1717 }
1718 EXPORT_SYMBOL(ib_detach_mcast);
1719
ib_alloc_xrcd(struct ib_device * device)1720 struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device)
1721 {
1722 struct ib_xrcd *xrcd;
1723
1724 if (!device->alloc_xrcd)
1725 return ERR_PTR(-ENOSYS);
1726
1727 xrcd = device->alloc_xrcd(device, NULL, NULL);
1728 if (!IS_ERR(xrcd)) {
1729 xrcd->device = device;
1730 xrcd->inode = NULL;
1731 atomic_set(&xrcd->usecnt, 0);
1732 mutex_init(&xrcd->tgt_qp_mutex);
1733 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
1734 }
1735
1736 return xrcd;
1737 }
1738 EXPORT_SYMBOL(ib_alloc_xrcd);
1739
ib_dealloc_xrcd(struct ib_xrcd * xrcd)1740 int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1741 {
1742 struct ib_qp *qp;
1743 int ret;
1744
1745 if (atomic_read(&xrcd->usecnt))
1746 return -EBUSY;
1747
1748 while (!list_empty(&xrcd->tgt_qp_list)) {
1749 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
1750 ret = ib_destroy_qp(qp);
1751 if (ret)
1752 return ret;
1753 }
1754
1755 return xrcd->device->dealloc_xrcd(xrcd);
1756 }
1757 EXPORT_SYMBOL(ib_dealloc_xrcd);
1758
1759 /**
1760 * ib_create_wq - Creates a WQ associated with the specified protection
1761 * domain.
1762 * @pd: The protection domain associated with the WQ.
1763 * @wq_init_attr: A list of initial attributes required to create the
1764 * WQ. If WQ creation succeeds, then the attributes are updated to
1765 * the actual capabilities of the created WQ.
1766 *
1767 * wq_init_attr->max_wr and wq_init_attr->max_sge determine
1768 * the requested size of the WQ, and set to the actual values allocated
1769 * on return.
1770 * If ib_create_wq() succeeds, then max_wr and max_sge will always be
1771 * at least as large as the requested values.
1772 */
ib_create_wq(struct ib_pd * pd,struct ib_wq_init_attr * wq_attr)1773 struct ib_wq *ib_create_wq(struct ib_pd *pd,
1774 struct ib_wq_init_attr *wq_attr)
1775 {
1776 struct ib_wq *wq;
1777
1778 if (!pd->device->create_wq)
1779 return ERR_PTR(-ENOSYS);
1780
1781 wq = pd->device->create_wq(pd, wq_attr, NULL);
1782 if (!IS_ERR(wq)) {
1783 wq->event_handler = wq_attr->event_handler;
1784 wq->wq_context = wq_attr->wq_context;
1785 wq->wq_type = wq_attr->wq_type;
1786 wq->cq = wq_attr->cq;
1787 wq->device = pd->device;
1788 wq->pd = pd;
1789 wq->uobject = NULL;
1790 atomic_inc(&pd->usecnt);
1791 atomic_inc(&wq_attr->cq->usecnt);
1792 atomic_set(&wq->usecnt, 0);
1793 }
1794 return wq;
1795 }
1796 EXPORT_SYMBOL(ib_create_wq);
1797
1798 /**
1799 * ib_destroy_wq - Destroys the specified WQ.
1800 * @wq: The WQ to destroy.
1801 */
ib_destroy_wq(struct ib_wq * wq)1802 int ib_destroy_wq(struct ib_wq *wq)
1803 {
1804 int err;
1805 struct ib_cq *cq = wq->cq;
1806 struct ib_pd *pd = wq->pd;
1807
1808 if (atomic_read(&wq->usecnt))
1809 return -EBUSY;
1810
1811 err = wq->device->destroy_wq(wq);
1812 if (!err) {
1813 atomic_dec(&pd->usecnt);
1814 atomic_dec(&cq->usecnt);
1815 }
1816 return err;
1817 }
1818 EXPORT_SYMBOL(ib_destroy_wq);
1819
1820 /**
1821 * ib_modify_wq - Modifies the specified WQ.
1822 * @wq: The WQ to modify.
1823 * @wq_attr: On input, specifies the WQ attributes to modify.
1824 * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
1825 * are being modified.
1826 * On output, the current values of selected WQ attributes are returned.
1827 */
ib_modify_wq(struct ib_wq * wq,struct ib_wq_attr * wq_attr,u32 wq_attr_mask)1828 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
1829 u32 wq_attr_mask)
1830 {
1831 int err;
1832
1833 if (!wq->device->modify_wq)
1834 return -ENOSYS;
1835
1836 err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
1837 return err;
1838 }
1839 EXPORT_SYMBOL(ib_modify_wq);
1840
1841 /*
1842 * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
1843 * @device: The device on which to create the rwq indirection table.
1844 * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
1845 * create the Indirection Table.
1846 *
1847 * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
1848 * than the created ib_rwq_ind_table object and the caller is responsible
1849 * for its memory allocation/free.
1850 */
ib_create_rwq_ind_table(struct ib_device * device,struct ib_rwq_ind_table_init_attr * init_attr)1851 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
1852 struct ib_rwq_ind_table_init_attr *init_attr)
1853 {
1854 struct ib_rwq_ind_table *rwq_ind_table;
1855 int i;
1856 u32 table_size;
1857
1858 if (!device->create_rwq_ind_table)
1859 return ERR_PTR(-ENOSYS);
1860
1861 table_size = (1 << init_attr->log_ind_tbl_size);
1862 rwq_ind_table = device->create_rwq_ind_table(device,
1863 init_attr, NULL);
1864 if (IS_ERR(rwq_ind_table))
1865 return rwq_ind_table;
1866
1867 rwq_ind_table->ind_tbl = init_attr->ind_tbl;
1868 rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
1869 rwq_ind_table->device = device;
1870 rwq_ind_table->uobject = NULL;
1871 atomic_set(&rwq_ind_table->usecnt, 0);
1872
1873 for (i = 0; i < table_size; i++)
1874 atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
1875
1876 return rwq_ind_table;
1877 }
1878 EXPORT_SYMBOL(ib_create_rwq_ind_table);
1879
1880 /*
1881 * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
1882 * @wq_ind_table: The Indirection Table to destroy.
1883 */
ib_destroy_rwq_ind_table(struct ib_rwq_ind_table * rwq_ind_table)1884 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
1885 {
1886 int err, i;
1887 u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
1888 struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
1889
1890 if (atomic_read(&rwq_ind_table->usecnt))
1891 return -EBUSY;
1892
1893 err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
1894 if (!err) {
1895 for (i = 0; i < table_size; i++)
1896 atomic_dec(&ind_tbl[i]->usecnt);
1897 }
1898
1899 return err;
1900 }
1901 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
1902
ib_create_flow(struct ib_qp * qp,struct ib_flow_attr * flow_attr,int domain)1903 struct ib_flow *ib_create_flow(struct ib_qp *qp,
1904 struct ib_flow_attr *flow_attr,
1905 int domain)
1906 {
1907 struct ib_flow *flow_id;
1908 if (!qp->device->create_flow)
1909 return ERR_PTR(-ENOSYS);
1910
1911 flow_id = qp->device->create_flow(qp, flow_attr, domain);
1912 if (!IS_ERR(flow_id)) {
1913 atomic_inc(&qp->usecnt);
1914 flow_id->qp = qp;
1915 }
1916 return flow_id;
1917 }
1918 EXPORT_SYMBOL(ib_create_flow);
1919
ib_destroy_flow(struct ib_flow * flow_id)1920 int ib_destroy_flow(struct ib_flow *flow_id)
1921 {
1922 int err;
1923 struct ib_qp *qp = flow_id->qp;
1924
1925 err = qp->device->destroy_flow(flow_id);
1926 if (!err)
1927 atomic_dec(&qp->usecnt);
1928 return err;
1929 }
1930 EXPORT_SYMBOL(ib_destroy_flow);
1931
ib_check_mr_status(struct ib_mr * mr,u32 check_mask,struct ib_mr_status * mr_status)1932 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
1933 struct ib_mr_status *mr_status)
1934 {
1935 return mr->device->check_mr_status ?
1936 mr->device->check_mr_status(mr, check_mask, mr_status) : -ENOSYS;
1937 }
1938 EXPORT_SYMBOL(ib_check_mr_status);
1939
ib_set_vf_link_state(struct ib_device * device,int vf,u8 port,int state)1940 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
1941 int state)
1942 {
1943 if (!device->set_vf_link_state)
1944 return -ENOSYS;
1945
1946 return device->set_vf_link_state(device, vf, port, state);
1947 }
1948 EXPORT_SYMBOL(ib_set_vf_link_state);
1949
ib_get_vf_config(struct ib_device * device,int vf,u8 port,struct ifla_vf_info * info)1950 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
1951 struct ifla_vf_info *info)
1952 {
1953 if (!device->get_vf_config)
1954 return -ENOSYS;
1955
1956 return device->get_vf_config(device, vf, port, info);
1957 }
1958 EXPORT_SYMBOL(ib_get_vf_config);
1959
ib_get_vf_stats(struct ib_device * device,int vf,u8 port,struct ifla_vf_stats * stats)1960 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
1961 struct ifla_vf_stats *stats)
1962 {
1963 if (!device->get_vf_stats)
1964 return -ENOSYS;
1965
1966 return device->get_vf_stats(device, vf, port, stats);
1967 }
1968 EXPORT_SYMBOL(ib_get_vf_stats);
1969
ib_set_vf_guid(struct ib_device * device,int vf,u8 port,u64 guid,int type)1970 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
1971 int type)
1972 {
1973 if (!device->set_vf_guid)
1974 return -ENOSYS;
1975
1976 return device->set_vf_guid(device, vf, port, guid, type);
1977 }
1978 EXPORT_SYMBOL(ib_set_vf_guid);
1979
1980 /**
1981 * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
1982 * and set it the memory region.
1983 * @mr: memory region
1984 * @sg: dma mapped scatterlist
1985 * @sg_nents: number of entries in sg
1986 * @sg_offset: offset in bytes into sg
1987 * @page_size: page vector desired page size
1988 *
1989 * Constraints:
1990 * - The first sg element is allowed to have an offset.
1991 * - Each sg element must either be aligned to page_size or virtually
1992 * contiguous to the previous element. In case an sg element has a
1993 * non-contiguous offset, the mapping prefix will not include it.
1994 * - The last sg element is allowed to have length less than page_size.
1995 * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
1996 * then only max_num_sg entries will be mapped.
1997 * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
1998 * constraints holds and the page_size argument is ignored.
1999 *
2000 * Returns the number of sg elements that were mapped to the memory region.
2001 *
2002 * After this completes successfully, the memory region
2003 * is ready for registration.
2004 */
ib_map_mr_sg(struct ib_mr * mr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset,unsigned int page_size)2005 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
2006 unsigned int *sg_offset, unsigned int page_size)
2007 {
2008 if (unlikely(!mr->device->map_mr_sg))
2009 return -ENOSYS;
2010
2011 mr->page_size = page_size;
2012
2013 return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
2014 }
2015 EXPORT_SYMBOL(ib_map_mr_sg);
2016
2017 /**
2018 * ib_sg_to_pages() - Convert the largest prefix of a sg list
2019 * to a page vector
2020 * @mr: memory region
2021 * @sgl: dma mapped scatterlist
2022 * @sg_nents: number of entries in sg
2023 * @sg_offset_p: IN: start offset in bytes into sg
2024 * OUT: offset in bytes for element n of the sg of the first
2025 * byte that has not been processed where n is the return
2026 * value of this function.
2027 * @set_page: driver page assignment function pointer
2028 *
2029 * Core service helper for drivers to convert the largest
2030 * prefix of given sg list to a page vector. The sg list
2031 * prefix converted is the prefix that meet the requirements
2032 * of ib_map_mr_sg.
2033 *
2034 * Returns the number of sg elements that were assigned to
2035 * a page vector.
2036 */
ib_sg_to_pages(struct ib_mr * mr,struct scatterlist * sgl,int sg_nents,unsigned int * sg_offset_p,int (* set_page)(struct ib_mr *,u64))2037 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
2038 unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
2039 {
2040 struct scatterlist *sg;
2041 u64 last_end_dma_addr = 0;
2042 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2043 unsigned int last_page_off = 0;
2044 u64 page_mask = ~((u64)mr->page_size - 1);
2045 int i, ret;
2046
2047 if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
2048 return -EINVAL;
2049
2050 mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
2051 mr->length = 0;
2052
2053 for_each_sg(sgl, sg, sg_nents, i) {
2054 u64 dma_addr = sg_dma_address(sg) + sg_offset;
2055 u64 prev_addr = dma_addr;
2056 unsigned int dma_len = sg_dma_len(sg) - sg_offset;
2057 u64 end_dma_addr = dma_addr + dma_len;
2058 u64 page_addr = dma_addr & page_mask;
2059
2060 /*
2061 * For the second and later elements, check whether either the
2062 * end of element i-1 or the start of element i is not aligned
2063 * on a page boundary.
2064 */
2065 if (i && (last_page_off != 0 || page_addr != dma_addr)) {
2066 /* Stop mapping if there is a gap. */
2067 if (last_end_dma_addr != dma_addr)
2068 break;
2069
2070 /*
2071 * Coalesce this element with the last. If it is small
2072 * enough just update mr->length. Otherwise start
2073 * mapping from the next page.
2074 */
2075 goto next_page;
2076 }
2077
2078 do {
2079 ret = set_page(mr, page_addr);
2080 if (unlikely(ret < 0)) {
2081 sg_offset = prev_addr - sg_dma_address(sg);
2082 mr->length += prev_addr - dma_addr;
2083 if (sg_offset_p)
2084 *sg_offset_p = sg_offset;
2085 return i || sg_offset ? i : ret;
2086 }
2087 prev_addr = page_addr;
2088 next_page:
2089 page_addr += mr->page_size;
2090 } while (page_addr < end_dma_addr);
2091
2092 mr->length += dma_len;
2093 last_end_dma_addr = end_dma_addr;
2094 last_page_off = end_dma_addr & ~page_mask;
2095
2096 sg_offset = 0;
2097 }
2098
2099 if (sg_offset_p)
2100 *sg_offset_p = 0;
2101 return i;
2102 }
2103 EXPORT_SYMBOL(ib_sg_to_pages);
2104
2105 struct ib_drain_cqe {
2106 struct ib_cqe cqe;
2107 struct completion done;
2108 };
2109
ib_drain_qp_done(struct ib_cq * cq,struct ib_wc * wc)2110 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
2111 {
2112 struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
2113 cqe);
2114
2115 complete(&cqe->done);
2116 }
2117
2118 /*
2119 * Post a WR and block until its completion is reaped for the SQ.
2120 */
__ib_drain_sq(struct ib_qp * qp)2121 static void __ib_drain_sq(struct ib_qp *qp)
2122 {
2123 struct ib_cq *cq = qp->send_cq;
2124 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2125 struct ib_drain_cqe sdrain;
2126 struct ib_send_wr *bad_swr;
2127 struct ib_rdma_wr swr = {
2128 .wr = {
2129 .next = NULL,
2130 { .wr_cqe = &sdrain.cqe, },
2131 .opcode = IB_WR_RDMA_WRITE,
2132 },
2133 };
2134 int ret;
2135
2136 sdrain.cqe.done = ib_drain_qp_done;
2137 init_completion(&sdrain.done);
2138
2139 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2140 if (ret) {
2141 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2142 return;
2143 }
2144
2145 ret = ib_post_send(qp, &swr.wr, &bad_swr);
2146 if (ret) {
2147 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2148 return;
2149 }
2150
2151 if (cq->poll_ctx == IB_POLL_DIRECT)
2152 while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2153 ib_process_cq_direct(cq, -1);
2154 else
2155 wait_for_completion(&sdrain.done);
2156 }
2157
2158 /*
2159 * Post a WR and block until its completion is reaped for the RQ.
2160 */
__ib_drain_rq(struct ib_qp * qp)2161 static void __ib_drain_rq(struct ib_qp *qp)
2162 {
2163 struct ib_cq *cq = qp->recv_cq;
2164 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2165 struct ib_drain_cqe rdrain;
2166 struct ib_recv_wr rwr = {}, *bad_rwr;
2167 int ret;
2168
2169 rwr.wr_cqe = &rdrain.cqe;
2170 rdrain.cqe.done = ib_drain_qp_done;
2171 init_completion(&rdrain.done);
2172
2173 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2174 if (ret) {
2175 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2176 return;
2177 }
2178
2179 ret = ib_post_recv(qp, &rwr, &bad_rwr);
2180 if (ret) {
2181 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2182 return;
2183 }
2184
2185 if (cq->poll_ctx == IB_POLL_DIRECT)
2186 while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2187 ib_process_cq_direct(cq, -1);
2188 else
2189 wait_for_completion(&rdrain.done);
2190 }
2191
2192 /**
2193 * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2194 * application.
2195 * @qp: queue pair to drain
2196 *
2197 * If the device has a provider-specific drain function, then
2198 * call that. Otherwise call the generic drain function
2199 * __ib_drain_sq().
2200 *
2201 * The caller must:
2202 *
2203 * ensure there is room in the CQ and SQ for the drain work request and
2204 * completion.
2205 *
2206 * allocate the CQ using ib_alloc_cq().
2207 *
2208 * ensure that there are no other contexts that are posting WRs concurrently.
2209 * Otherwise the drain is not guaranteed.
2210 */
ib_drain_sq(struct ib_qp * qp)2211 void ib_drain_sq(struct ib_qp *qp)
2212 {
2213 if (qp->device->drain_sq)
2214 qp->device->drain_sq(qp);
2215 else
2216 __ib_drain_sq(qp);
2217 }
2218 EXPORT_SYMBOL(ib_drain_sq);
2219
2220 /**
2221 * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2222 * application.
2223 * @qp: queue pair to drain
2224 *
2225 * If the device has a provider-specific drain function, then
2226 * call that. Otherwise call the generic drain function
2227 * __ib_drain_rq().
2228 *
2229 * The caller must:
2230 *
2231 * ensure there is room in the CQ and RQ for the drain work request and
2232 * completion.
2233 *
2234 * allocate the CQ using ib_alloc_cq().
2235 *
2236 * ensure that there are no other contexts that are posting WRs concurrently.
2237 * Otherwise the drain is not guaranteed.
2238 */
ib_drain_rq(struct ib_qp * qp)2239 void ib_drain_rq(struct ib_qp *qp)
2240 {
2241 if (qp->device->drain_rq)
2242 qp->device->drain_rq(qp);
2243 else
2244 __ib_drain_rq(qp);
2245 }
2246 EXPORT_SYMBOL(ib_drain_rq);
2247
2248 /**
2249 * ib_drain_qp() - Block until all CQEs have been consumed by the
2250 * application on both the RQ and SQ.
2251 * @qp: queue pair to drain
2252 *
2253 * The caller must:
2254 *
2255 * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2256 * and completions.
2257 *
2258 * allocate the CQs using ib_alloc_cq().
2259 *
2260 * ensure that there are no other contexts that are posting WRs concurrently.
2261 * Otherwise the drain is not guaranteed.
2262 */
ib_drain_qp(struct ib_qp * qp)2263 void ib_drain_qp(struct ib_qp *qp)
2264 {
2265 ib_drain_sq(qp);
2266 if (!qp->srq)
2267 ib_drain_rq(qp);
2268 }
2269 EXPORT_SYMBOL(ib_drain_qp);
2270