• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_cache.h>
47 
ib_rate_to_mult(enum ib_rate rate)48 int ib_rate_to_mult(enum ib_rate rate)
49 {
50 	switch (rate) {
51 	case IB_RATE_2_5_GBPS: return  1;
52 	case IB_RATE_5_GBPS:   return  2;
53 	case IB_RATE_10_GBPS:  return  4;
54 	case IB_RATE_20_GBPS:  return  8;
55 	case IB_RATE_30_GBPS:  return 12;
56 	case IB_RATE_40_GBPS:  return 16;
57 	case IB_RATE_60_GBPS:  return 24;
58 	case IB_RATE_80_GBPS:  return 32;
59 	case IB_RATE_120_GBPS: return 48;
60 	default:	       return -1;
61 	}
62 }
63 EXPORT_SYMBOL(ib_rate_to_mult);
64 
mult_to_ib_rate(int mult)65 enum ib_rate mult_to_ib_rate(int mult)
66 {
67 	switch (mult) {
68 	case 1:  return IB_RATE_2_5_GBPS;
69 	case 2:  return IB_RATE_5_GBPS;
70 	case 4:  return IB_RATE_10_GBPS;
71 	case 8:  return IB_RATE_20_GBPS;
72 	case 12: return IB_RATE_30_GBPS;
73 	case 16: return IB_RATE_40_GBPS;
74 	case 24: return IB_RATE_60_GBPS;
75 	case 32: return IB_RATE_80_GBPS;
76 	case 48: return IB_RATE_120_GBPS;
77 	default: return IB_RATE_PORT_CURRENT;
78 	}
79 }
80 EXPORT_SYMBOL(mult_to_ib_rate);
81 
ib_rate_to_mbps(enum ib_rate rate)82 int ib_rate_to_mbps(enum ib_rate rate)
83 {
84 	switch (rate) {
85 	case IB_RATE_2_5_GBPS: return 2500;
86 	case IB_RATE_5_GBPS:   return 5000;
87 	case IB_RATE_10_GBPS:  return 10000;
88 	case IB_RATE_20_GBPS:  return 20000;
89 	case IB_RATE_30_GBPS:  return 30000;
90 	case IB_RATE_40_GBPS:  return 40000;
91 	case IB_RATE_60_GBPS:  return 60000;
92 	case IB_RATE_80_GBPS:  return 80000;
93 	case IB_RATE_120_GBPS: return 120000;
94 	case IB_RATE_14_GBPS:  return 14062;
95 	case IB_RATE_56_GBPS:  return 56250;
96 	case IB_RATE_112_GBPS: return 112500;
97 	case IB_RATE_168_GBPS: return 168750;
98 	case IB_RATE_25_GBPS:  return 25781;
99 	case IB_RATE_100_GBPS: return 103125;
100 	case IB_RATE_200_GBPS: return 206250;
101 	case IB_RATE_300_GBPS: return 309375;
102 	default:	       return -1;
103 	}
104 }
105 EXPORT_SYMBOL(ib_rate_to_mbps);
106 
107 enum rdma_transport_type
rdma_node_get_transport(enum rdma_node_type node_type)108 rdma_node_get_transport(enum rdma_node_type node_type)
109 {
110 	switch (node_type) {
111 	case RDMA_NODE_IB_CA:
112 	case RDMA_NODE_IB_SWITCH:
113 	case RDMA_NODE_IB_ROUTER:
114 		return RDMA_TRANSPORT_IB;
115 	case RDMA_NODE_RNIC:
116 		return RDMA_TRANSPORT_IWARP;
117 	default:
118 		BUG();
119 		return 0;
120 	}
121 }
122 EXPORT_SYMBOL(rdma_node_get_transport);
123 
rdma_port_get_link_layer(struct ib_device * device,u8 port_num)124 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
125 {
126 	if (device->get_link_layer)
127 		return device->get_link_layer(device, port_num);
128 
129 	switch (rdma_node_get_transport(device->node_type)) {
130 	case RDMA_TRANSPORT_IB:
131 		return IB_LINK_LAYER_INFINIBAND;
132 	case RDMA_TRANSPORT_IWARP:
133 		return IB_LINK_LAYER_ETHERNET;
134 	default:
135 		return IB_LINK_LAYER_UNSPECIFIED;
136 	}
137 }
138 EXPORT_SYMBOL(rdma_port_get_link_layer);
139 
140 /* Protection domains */
141 
ib_alloc_pd(struct ib_device * device)142 struct ib_pd *ib_alloc_pd(struct ib_device *device)
143 {
144 	struct ib_pd *pd;
145 
146 	pd = device->alloc_pd(device, NULL, NULL);
147 
148 	if (!IS_ERR(pd)) {
149 		pd->device  = device;
150 		pd->uobject = NULL;
151 		atomic_set(&pd->usecnt, 0);
152 	}
153 
154 	return pd;
155 }
156 EXPORT_SYMBOL(ib_alloc_pd);
157 
ib_dealloc_pd(struct ib_pd * pd)158 int ib_dealloc_pd(struct ib_pd *pd)
159 {
160 	if (atomic_read(&pd->usecnt))
161 		return -EBUSY;
162 
163 	return pd->device->dealloc_pd(pd);
164 }
165 EXPORT_SYMBOL(ib_dealloc_pd);
166 
167 /* Address handles */
168 
ib_create_ah(struct ib_pd * pd,struct ib_ah_attr * ah_attr)169 struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
170 {
171 	struct ib_ah *ah;
172 
173 	ah = pd->device->create_ah(pd, ah_attr);
174 
175 	if (!IS_ERR(ah)) {
176 		ah->device  = pd->device;
177 		ah->pd      = pd;
178 		ah->uobject = NULL;
179 		atomic_inc(&pd->usecnt);
180 	}
181 
182 	return ah;
183 }
184 EXPORT_SYMBOL(ib_create_ah);
185 
ib_init_ah_from_wc(struct ib_device * device,u8 port_num,struct ib_wc * wc,struct ib_grh * grh,struct ib_ah_attr * ah_attr)186 int ib_init_ah_from_wc(struct ib_device *device, u8 port_num, struct ib_wc *wc,
187 		       struct ib_grh *grh, struct ib_ah_attr *ah_attr)
188 {
189 	u32 flow_class;
190 	u16 gid_index;
191 	int ret;
192 
193 	memset(ah_attr, 0, sizeof *ah_attr);
194 	ah_attr->dlid = wc->slid;
195 	ah_attr->sl = wc->sl;
196 	ah_attr->src_path_bits = wc->dlid_path_bits;
197 	ah_attr->port_num = port_num;
198 
199 	if (wc->wc_flags & IB_WC_GRH) {
200 		ah_attr->ah_flags = IB_AH_GRH;
201 		ah_attr->grh.dgid = grh->sgid;
202 
203 		ret = ib_find_cached_gid(device, &grh->dgid, &port_num,
204 					 &gid_index);
205 		if (ret)
206 			return ret;
207 
208 		ah_attr->grh.sgid_index = (u8) gid_index;
209 		flow_class = be32_to_cpu(grh->version_tclass_flow);
210 		ah_attr->grh.flow_label = flow_class & 0xFFFFF;
211 		ah_attr->grh.hop_limit = 0xFF;
212 		ah_attr->grh.traffic_class = (flow_class >> 20) & 0xFF;
213 	}
214 	return 0;
215 }
216 EXPORT_SYMBOL(ib_init_ah_from_wc);
217 
ib_create_ah_from_wc(struct ib_pd * pd,struct ib_wc * wc,struct ib_grh * grh,u8 port_num)218 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, struct ib_wc *wc,
219 				   struct ib_grh *grh, u8 port_num)
220 {
221 	struct ib_ah_attr ah_attr;
222 	int ret;
223 
224 	ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
225 	if (ret)
226 		return ERR_PTR(ret);
227 
228 	return ib_create_ah(pd, &ah_attr);
229 }
230 EXPORT_SYMBOL(ib_create_ah_from_wc);
231 
ib_modify_ah(struct ib_ah * ah,struct ib_ah_attr * ah_attr)232 int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
233 {
234 	return ah->device->modify_ah ?
235 		ah->device->modify_ah(ah, ah_attr) :
236 		-ENOSYS;
237 }
238 EXPORT_SYMBOL(ib_modify_ah);
239 
ib_query_ah(struct ib_ah * ah,struct ib_ah_attr * ah_attr)240 int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
241 {
242 	return ah->device->query_ah ?
243 		ah->device->query_ah(ah, ah_attr) :
244 		-ENOSYS;
245 }
246 EXPORT_SYMBOL(ib_query_ah);
247 
ib_destroy_ah(struct ib_ah * ah)248 int ib_destroy_ah(struct ib_ah *ah)
249 {
250 	struct ib_pd *pd;
251 	int ret;
252 
253 	pd = ah->pd;
254 	ret = ah->device->destroy_ah(ah);
255 	if (!ret)
256 		atomic_dec(&pd->usecnt);
257 
258 	return ret;
259 }
260 EXPORT_SYMBOL(ib_destroy_ah);
261 
262 /* Shared receive queues */
263 
ib_create_srq(struct ib_pd * pd,struct ib_srq_init_attr * srq_init_attr)264 struct ib_srq *ib_create_srq(struct ib_pd *pd,
265 			     struct ib_srq_init_attr *srq_init_attr)
266 {
267 	struct ib_srq *srq;
268 
269 	if (!pd->device->create_srq)
270 		return ERR_PTR(-ENOSYS);
271 
272 	srq = pd->device->create_srq(pd, srq_init_attr, NULL);
273 
274 	if (!IS_ERR(srq)) {
275 		srq->device    	   = pd->device;
276 		srq->pd        	   = pd;
277 		srq->uobject       = NULL;
278 		srq->event_handler = srq_init_attr->event_handler;
279 		srq->srq_context   = srq_init_attr->srq_context;
280 		srq->srq_type      = srq_init_attr->srq_type;
281 		if (srq->srq_type == IB_SRQT_XRC) {
282 			srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
283 			srq->ext.xrc.cq   = srq_init_attr->ext.xrc.cq;
284 			atomic_inc(&srq->ext.xrc.xrcd->usecnt);
285 			atomic_inc(&srq->ext.xrc.cq->usecnt);
286 		}
287 		atomic_inc(&pd->usecnt);
288 		atomic_set(&srq->usecnt, 0);
289 	}
290 
291 	return srq;
292 }
293 EXPORT_SYMBOL(ib_create_srq);
294 
ib_modify_srq(struct ib_srq * srq,struct ib_srq_attr * srq_attr,enum ib_srq_attr_mask srq_attr_mask)295 int ib_modify_srq(struct ib_srq *srq,
296 		  struct ib_srq_attr *srq_attr,
297 		  enum ib_srq_attr_mask srq_attr_mask)
298 {
299 	return srq->device->modify_srq ?
300 		srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
301 		-ENOSYS;
302 }
303 EXPORT_SYMBOL(ib_modify_srq);
304 
ib_query_srq(struct ib_srq * srq,struct ib_srq_attr * srq_attr)305 int ib_query_srq(struct ib_srq *srq,
306 		 struct ib_srq_attr *srq_attr)
307 {
308 	return srq->device->query_srq ?
309 		srq->device->query_srq(srq, srq_attr) : -ENOSYS;
310 }
311 EXPORT_SYMBOL(ib_query_srq);
312 
ib_destroy_srq(struct ib_srq * srq)313 int ib_destroy_srq(struct ib_srq *srq)
314 {
315 	struct ib_pd *pd;
316 	enum ib_srq_type srq_type;
317 	struct ib_xrcd *uninitialized_var(xrcd);
318 	struct ib_cq *uninitialized_var(cq);
319 	int ret;
320 
321 	if (atomic_read(&srq->usecnt))
322 		return -EBUSY;
323 
324 	pd = srq->pd;
325 	srq_type = srq->srq_type;
326 	if (srq_type == IB_SRQT_XRC) {
327 		xrcd = srq->ext.xrc.xrcd;
328 		cq = srq->ext.xrc.cq;
329 	}
330 
331 	ret = srq->device->destroy_srq(srq);
332 	if (!ret) {
333 		atomic_dec(&pd->usecnt);
334 		if (srq_type == IB_SRQT_XRC) {
335 			atomic_dec(&xrcd->usecnt);
336 			atomic_dec(&cq->usecnt);
337 		}
338 	}
339 
340 	return ret;
341 }
342 EXPORT_SYMBOL(ib_destroy_srq);
343 
344 /* Queue pairs */
345 
__ib_shared_qp_event_handler(struct ib_event * event,void * context)346 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
347 {
348 	struct ib_qp *qp = context;
349 
350 	list_for_each_entry(event->element.qp, &qp->open_list, open_list)
351 		if (event->element.qp->event_handler)
352 			event->element.qp->event_handler(event, event->element.qp->qp_context);
353 }
354 
__ib_insert_xrcd_qp(struct ib_xrcd * xrcd,struct ib_qp * qp)355 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
356 {
357 	mutex_lock(&xrcd->tgt_qp_mutex);
358 	list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
359 	mutex_unlock(&xrcd->tgt_qp_mutex);
360 }
361 
__ib_open_qp(struct ib_qp * real_qp,void (* event_handler)(struct ib_event *,void *),void * qp_context)362 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
363 				  void (*event_handler)(struct ib_event *, void *),
364 				  void *qp_context)
365 {
366 	struct ib_qp *qp;
367 	unsigned long flags;
368 
369 	qp = kzalloc(sizeof *qp, GFP_KERNEL);
370 	if (!qp)
371 		return ERR_PTR(-ENOMEM);
372 
373 	qp->real_qp = real_qp;
374 	atomic_inc(&real_qp->usecnt);
375 	qp->device = real_qp->device;
376 	qp->event_handler = event_handler;
377 	qp->qp_context = qp_context;
378 	qp->qp_num = real_qp->qp_num;
379 	qp->qp_type = real_qp->qp_type;
380 
381 	spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
382 	list_add(&qp->open_list, &real_qp->open_list);
383 	spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
384 
385 	return qp;
386 }
387 
ib_open_qp(struct ib_xrcd * xrcd,struct ib_qp_open_attr * qp_open_attr)388 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
389 			 struct ib_qp_open_attr *qp_open_attr)
390 {
391 	struct ib_qp *qp, *real_qp;
392 
393 	if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
394 		return ERR_PTR(-EINVAL);
395 
396 	qp = ERR_PTR(-EINVAL);
397 	mutex_lock(&xrcd->tgt_qp_mutex);
398 	list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
399 		if (real_qp->qp_num == qp_open_attr->qp_num) {
400 			qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
401 					  qp_open_attr->qp_context);
402 			break;
403 		}
404 	}
405 	mutex_unlock(&xrcd->tgt_qp_mutex);
406 	return qp;
407 }
408 EXPORT_SYMBOL(ib_open_qp);
409 
ib_create_qp(struct ib_pd * pd,struct ib_qp_init_attr * qp_init_attr)410 struct ib_qp *ib_create_qp(struct ib_pd *pd,
411 			   struct ib_qp_init_attr *qp_init_attr)
412 {
413 	struct ib_qp *qp, *real_qp;
414 	struct ib_device *device;
415 
416 	device = pd ? pd->device : qp_init_attr->xrcd->device;
417 	qp = device->create_qp(pd, qp_init_attr, NULL);
418 
419 	if (!IS_ERR(qp)) {
420 		qp->device     = device;
421 		qp->real_qp    = qp;
422 		qp->uobject    = NULL;
423 		qp->qp_type    = qp_init_attr->qp_type;
424 
425 		atomic_set(&qp->usecnt, 0);
426 		if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
427 			qp->event_handler = __ib_shared_qp_event_handler;
428 			qp->qp_context = qp;
429 			qp->pd = NULL;
430 			qp->send_cq = qp->recv_cq = NULL;
431 			qp->srq = NULL;
432 			qp->xrcd = qp_init_attr->xrcd;
433 			atomic_inc(&qp_init_attr->xrcd->usecnt);
434 			INIT_LIST_HEAD(&qp->open_list);
435 
436 			real_qp = qp;
437 			qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
438 					  qp_init_attr->qp_context);
439 			if (!IS_ERR(qp))
440 				__ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
441 			else
442 				real_qp->device->destroy_qp(real_qp);
443 		} else {
444 			qp->event_handler = qp_init_attr->event_handler;
445 			qp->qp_context = qp_init_attr->qp_context;
446 			if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
447 				qp->recv_cq = NULL;
448 				qp->srq = NULL;
449 			} else {
450 				qp->recv_cq = qp_init_attr->recv_cq;
451 				atomic_inc(&qp_init_attr->recv_cq->usecnt);
452 				qp->srq = qp_init_attr->srq;
453 				if (qp->srq)
454 					atomic_inc(&qp_init_attr->srq->usecnt);
455 			}
456 
457 			qp->pd	    = pd;
458 			qp->send_cq = qp_init_attr->send_cq;
459 			qp->xrcd    = NULL;
460 
461 			atomic_inc(&pd->usecnt);
462 			atomic_inc(&qp_init_attr->send_cq->usecnt);
463 		}
464 	}
465 
466 	return qp;
467 }
468 EXPORT_SYMBOL(ib_create_qp);
469 
470 static const struct {
471 	int			valid;
472 	enum ib_qp_attr_mask	req_param[IB_QPT_MAX];
473 	enum ib_qp_attr_mask	opt_param[IB_QPT_MAX];
474 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
475 	[IB_QPS_RESET] = {
476 		[IB_QPS_RESET] = { .valid = 1 },
477 		[IB_QPS_INIT]  = {
478 			.valid = 1,
479 			.req_param = {
480 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
481 						IB_QP_PORT			|
482 						IB_QP_QKEY),
483 				[IB_QPT_RAW_PACKET] = IB_QP_PORT,
484 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
485 						IB_QP_PORT			|
486 						IB_QP_ACCESS_FLAGS),
487 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
488 						IB_QP_PORT			|
489 						IB_QP_ACCESS_FLAGS),
490 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
491 						IB_QP_PORT			|
492 						IB_QP_ACCESS_FLAGS),
493 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
494 						IB_QP_PORT			|
495 						IB_QP_ACCESS_FLAGS),
496 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
497 						IB_QP_QKEY),
498 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
499 						IB_QP_QKEY),
500 			}
501 		},
502 	},
503 	[IB_QPS_INIT]  = {
504 		[IB_QPS_RESET] = { .valid = 1 },
505 		[IB_QPS_ERR] =   { .valid = 1 },
506 		[IB_QPS_INIT]  = {
507 			.valid = 1,
508 			.opt_param = {
509 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
510 						IB_QP_PORT			|
511 						IB_QP_QKEY),
512 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
513 						IB_QP_PORT			|
514 						IB_QP_ACCESS_FLAGS),
515 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
516 						IB_QP_PORT			|
517 						IB_QP_ACCESS_FLAGS),
518 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
519 						IB_QP_PORT			|
520 						IB_QP_ACCESS_FLAGS),
521 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
522 						IB_QP_PORT			|
523 						IB_QP_ACCESS_FLAGS),
524 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
525 						IB_QP_QKEY),
526 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
527 						IB_QP_QKEY),
528 			}
529 		},
530 		[IB_QPS_RTR]   = {
531 			.valid = 1,
532 			.req_param = {
533 				[IB_QPT_UC]  = (IB_QP_AV			|
534 						IB_QP_PATH_MTU			|
535 						IB_QP_DEST_QPN			|
536 						IB_QP_RQ_PSN),
537 				[IB_QPT_RC]  = (IB_QP_AV			|
538 						IB_QP_PATH_MTU			|
539 						IB_QP_DEST_QPN			|
540 						IB_QP_RQ_PSN			|
541 						IB_QP_MAX_DEST_RD_ATOMIC	|
542 						IB_QP_MIN_RNR_TIMER),
543 				[IB_QPT_XRC_INI] = (IB_QP_AV			|
544 						IB_QP_PATH_MTU			|
545 						IB_QP_DEST_QPN			|
546 						IB_QP_RQ_PSN),
547 				[IB_QPT_XRC_TGT] = (IB_QP_AV			|
548 						IB_QP_PATH_MTU			|
549 						IB_QP_DEST_QPN			|
550 						IB_QP_RQ_PSN			|
551 						IB_QP_MAX_DEST_RD_ATOMIC	|
552 						IB_QP_MIN_RNR_TIMER),
553 			},
554 			.opt_param = {
555 				 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
556 						 IB_QP_QKEY),
557 				 [IB_QPT_UC]  = (IB_QP_ALT_PATH			|
558 						 IB_QP_ACCESS_FLAGS		|
559 						 IB_QP_PKEY_INDEX),
560 				 [IB_QPT_RC]  = (IB_QP_ALT_PATH			|
561 						 IB_QP_ACCESS_FLAGS		|
562 						 IB_QP_PKEY_INDEX),
563 				 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH		|
564 						 IB_QP_ACCESS_FLAGS		|
565 						 IB_QP_PKEY_INDEX),
566 				 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH		|
567 						 IB_QP_ACCESS_FLAGS		|
568 						 IB_QP_PKEY_INDEX),
569 				 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
570 						 IB_QP_QKEY),
571 				 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
572 						 IB_QP_QKEY),
573 			 }
574 		}
575 	},
576 	[IB_QPS_RTR]   = {
577 		[IB_QPS_RESET] = { .valid = 1 },
578 		[IB_QPS_ERR] =   { .valid = 1 },
579 		[IB_QPS_RTS]   = {
580 			.valid = 1,
581 			.req_param = {
582 				[IB_QPT_UD]  = IB_QP_SQ_PSN,
583 				[IB_QPT_UC]  = IB_QP_SQ_PSN,
584 				[IB_QPT_RC]  = (IB_QP_TIMEOUT			|
585 						IB_QP_RETRY_CNT			|
586 						IB_QP_RNR_RETRY			|
587 						IB_QP_SQ_PSN			|
588 						IB_QP_MAX_QP_RD_ATOMIC),
589 				[IB_QPT_XRC_INI] = (IB_QP_TIMEOUT		|
590 						IB_QP_RETRY_CNT			|
591 						IB_QP_RNR_RETRY			|
592 						IB_QP_SQ_PSN			|
593 						IB_QP_MAX_QP_RD_ATOMIC),
594 				[IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT		|
595 						IB_QP_SQ_PSN),
596 				[IB_QPT_SMI] = IB_QP_SQ_PSN,
597 				[IB_QPT_GSI] = IB_QP_SQ_PSN,
598 			},
599 			.opt_param = {
600 				 [IB_QPT_UD]  = (IB_QP_CUR_STATE		|
601 						 IB_QP_QKEY),
602 				 [IB_QPT_UC]  = (IB_QP_CUR_STATE		|
603 						 IB_QP_ALT_PATH			|
604 						 IB_QP_ACCESS_FLAGS		|
605 						 IB_QP_PATH_MIG_STATE),
606 				 [IB_QPT_RC]  = (IB_QP_CUR_STATE		|
607 						 IB_QP_ALT_PATH			|
608 						 IB_QP_ACCESS_FLAGS		|
609 						 IB_QP_MIN_RNR_TIMER		|
610 						 IB_QP_PATH_MIG_STATE),
611 				 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
612 						 IB_QP_ALT_PATH			|
613 						 IB_QP_ACCESS_FLAGS		|
614 						 IB_QP_PATH_MIG_STATE),
615 				 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
616 						 IB_QP_ALT_PATH			|
617 						 IB_QP_ACCESS_FLAGS		|
618 						 IB_QP_MIN_RNR_TIMER		|
619 						 IB_QP_PATH_MIG_STATE),
620 				 [IB_QPT_SMI] = (IB_QP_CUR_STATE		|
621 						 IB_QP_QKEY),
622 				 [IB_QPT_GSI] = (IB_QP_CUR_STATE		|
623 						 IB_QP_QKEY),
624 			 }
625 		}
626 	},
627 	[IB_QPS_RTS]   = {
628 		[IB_QPS_RESET] = { .valid = 1 },
629 		[IB_QPS_ERR] =   { .valid = 1 },
630 		[IB_QPS_RTS]   = {
631 			.valid = 1,
632 			.opt_param = {
633 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
634 						IB_QP_QKEY),
635 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
636 						IB_QP_ACCESS_FLAGS		|
637 						IB_QP_ALT_PATH			|
638 						IB_QP_PATH_MIG_STATE),
639 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
640 						IB_QP_ACCESS_FLAGS		|
641 						IB_QP_ALT_PATH			|
642 						IB_QP_PATH_MIG_STATE		|
643 						IB_QP_MIN_RNR_TIMER),
644 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
645 						IB_QP_ACCESS_FLAGS		|
646 						IB_QP_ALT_PATH			|
647 						IB_QP_PATH_MIG_STATE),
648 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
649 						IB_QP_ACCESS_FLAGS		|
650 						IB_QP_ALT_PATH			|
651 						IB_QP_PATH_MIG_STATE		|
652 						IB_QP_MIN_RNR_TIMER),
653 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
654 						IB_QP_QKEY),
655 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
656 						IB_QP_QKEY),
657 			}
658 		},
659 		[IB_QPS_SQD]   = {
660 			.valid = 1,
661 			.opt_param = {
662 				[IB_QPT_UD]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
663 				[IB_QPT_UC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
664 				[IB_QPT_RC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
665 				[IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
666 				[IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
667 				[IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
668 				[IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
669 			}
670 		},
671 	},
672 	[IB_QPS_SQD]   = {
673 		[IB_QPS_RESET] = { .valid = 1 },
674 		[IB_QPS_ERR] =   { .valid = 1 },
675 		[IB_QPS_RTS]   = {
676 			.valid = 1,
677 			.opt_param = {
678 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
679 						IB_QP_QKEY),
680 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
681 						IB_QP_ALT_PATH			|
682 						IB_QP_ACCESS_FLAGS		|
683 						IB_QP_PATH_MIG_STATE),
684 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
685 						IB_QP_ALT_PATH			|
686 						IB_QP_ACCESS_FLAGS		|
687 						IB_QP_MIN_RNR_TIMER		|
688 						IB_QP_PATH_MIG_STATE),
689 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
690 						IB_QP_ALT_PATH			|
691 						IB_QP_ACCESS_FLAGS		|
692 						IB_QP_PATH_MIG_STATE),
693 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
694 						IB_QP_ALT_PATH			|
695 						IB_QP_ACCESS_FLAGS		|
696 						IB_QP_MIN_RNR_TIMER		|
697 						IB_QP_PATH_MIG_STATE),
698 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
699 						IB_QP_QKEY),
700 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
701 						IB_QP_QKEY),
702 			}
703 		},
704 		[IB_QPS_SQD]   = {
705 			.valid = 1,
706 			.opt_param = {
707 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
708 						IB_QP_QKEY),
709 				[IB_QPT_UC]  = (IB_QP_AV			|
710 						IB_QP_ALT_PATH			|
711 						IB_QP_ACCESS_FLAGS		|
712 						IB_QP_PKEY_INDEX		|
713 						IB_QP_PATH_MIG_STATE),
714 				[IB_QPT_RC]  = (IB_QP_PORT			|
715 						IB_QP_AV			|
716 						IB_QP_TIMEOUT			|
717 						IB_QP_RETRY_CNT			|
718 						IB_QP_RNR_RETRY			|
719 						IB_QP_MAX_QP_RD_ATOMIC		|
720 						IB_QP_MAX_DEST_RD_ATOMIC	|
721 						IB_QP_ALT_PATH			|
722 						IB_QP_ACCESS_FLAGS		|
723 						IB_QP_PKEY_INDEX		|
724 						IB_QP_MIN_RNR_TIMER		|
725 						IB_QP_PATH_MIG_STATE),
726 				[IB_QPT_XRC_INI] = (IB_QP_PORT			|
727 						IB_QP_AV			|
728 						IB_QP_TIMEOUT			|
729 						IB_QP_RETRY_CNT			|
730 						IB_QP_RNR_RETRY			|
731 						IB_QP_MAX_QP_RD_ATOMIC		|
732 						IB_QP_ALT_PATH			|
733 						IB_QP_ACCESS_FLAGS		|
734 						IB_QP_PKEY_INDEX		|
735 						IB_QP_PATH_MIG_STATE),
736 				[IB_QPT_XRC_TGT] = (IB_QP_PORT			|
737 						IB_QP_AV			|
738 						IB_QP_TIMEOUT			|
739 						IB_QP_MAX_DEST_RD_ATOMIC	|
740 						IB_QP_ALT_PATH			|
741 						IB_QP_ACCESS_FLAGS		|
742 						IB_QP_PKEY_INDEX		|
743 						IB_QP_MIN_RNR_TIMER		|
744 						IB_QP_PATH_MIG_STATE),
745 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
746 						IB_QP_QKEY),
747 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
748 						IB_QP_QKEY),
749 			}
750 		}
751 	},
752 	[IB_QPS_SQE]   = {
753 		[IB_QPS_RESET] = { .valid = 1 },
754 		[IB_QPS_ERR] =   { .valid = 1 },
755 		[IB_QPS_RTS]   = {
756 			.valid = 1,
757 			.opt_param = {
758 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
759 						IB_QP_QKEY),
760 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
761 						IB_QP_ACCESS_FLAGS),
762 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
763 						IB_QP_QKEY),
764 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
765 						IB_QP_QKEY),
766 			}
767 		}
768 	},
769 	[IB_QPS_ERR] = {
770 		[IB_QPS_RESET] = { .valid = 1 },
771 		[IB_QPS_ERR] =   { .valid = 1 }
772 	}
773 };
774 
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)775 int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
776 		       enum ib_qp_type type, enum ib_qp_attr_mask mask)
777 {
778 	enum ib_qp_attr_mask req_param, opt_param;
779 
780 	if (cur_state  < 0 || cur_state  > IB_QPS_ERR ||
781 	    next_state < 0 || next_state > IB_QPS_ERR)
782 		return 0;
783 
784 	if (mask & IB_QP_CUR_STATE  &&
785 	    cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
786 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
787 		return 0;
788 
789 	if (!qp_state_table[cur_state][next_state].valid)
790 		return 0;
791 
792 	req_param = qp_state_table[cur_state][next_state].req_param[type];
793 	opt_param = qp_state_table[cur_state][next_state].opt_param[type];
794 
795 	if ((mask & req_param) != req_param)
796 		return 0;
797 
798 	if (mask & ~(req_param | opt_param | IB_QP_STATE))
799 		return 0;
800 
801 	return 1;
802 }
803 EXPORT_SYMBOL(ib_modify_qp_is_ok);
804 
ib_modify_qp(struct ib_qp * qp,struct ib_qp_attr * qp_attr,int qp_attr_mask)805 int ib_modify_qp(struct ib_qp *qp,
806 		 struct ib_qp_attr *qp_attr,
807 		 int qp_attr_mask)
808 {
809 	return qp->device->modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
810 }
811 EXPORT_SYMBOL(ib_modify_qp);
812 
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)813 int ib_query_qp(struct ib_qp *qp,
814 		struct ib_qp_attr *qp_attr,
815 		int qp_attr_mask,
816 		struct ib_qp_init_attr *qp_init_attr)
817 {
818 	return qp->device->query_qp ?
819 		qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
820 		-ENOSYS;
821 }
822 EXPORT_SYMBOL(ib_query_qp);
823 
ib_close_qp(struct ib_qp * qp)824 int ib_close_qp(struct ib_qp *qp)
825 {
826 	struct ib_qp *real_qp;
827 	unsigned long flags;
828 
829 	real_qp = qp->real_qp;
830 	if (real_qp == qp)
831 		return -EINVAL;
832 
833 	spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
834 	list_del(&qp->open_list);
835 	spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
836 
837 	atomic_dec(&real_qp->usecnt);
838 	kfree(qp);
839 
840 	return 0;
841 }
842 EXPORT_SYMBOL(ib_close_qp);
843 
__ib_destroy_shared_qp(struct ib_qp * qp)844 static int __ib_destroy_shared_qp(struct ib_qp *qp)
845 {
846 	struct ib_xrcd *xrcd;
847 	struct ib_qp *real_qp;
848 	int ret;
849 
850 	real_qp = qp->real_qp;
851 	xrcd = real_qp->xrcd;
852 
853 	mutex_lock(&xrcd->tgt_qp_mutex);
854 	ib_close_qp(qp);
855 	if (atomic_read(&real_qp->usecnt) == 0)
856 		list_del(&real_qp->xrcd_list);
857 	else
858 		real_qp = NULL;
859 	mutex_unlock(&xrcd->tgt_qp_mutex);
860 
861 	if (real_qp) {
862 		ret = ib_destroy_qp(real_qp);
863 		if (!ret)
864 			atomic_dec(&xrcd->usecnt);
865 		else
866 			__ib_insert_xrcd_qp(xrcd, real_qp);
867 	}
868 
869 	return 0;
870 }
871 
ib_destroy_qp(struct ib_qp * qp)872 int ib_destroy_qp(struct ib_qp *qp)
873 {
874 	struct ib_pd *pd;
875 	struct ib_cq *scq, *rcq;
876 	struct ib_srq *srq;
877 	int ret;
878 
879 	if (atomic_read(&qp->usecnt))
880 		return -EBUSY;
881 
882 	if (qp->real_qp != qp)
883 		return __ib_destroy_shared_qp(qp);
884 
885 	pd   = qp->pd;
886 	scq  = qp->send_cq;
887 	rcq  = qp->recv_cq;
888 	srq  = qp->srq;
889 
890 	ret = qp->device->destroy_qp(qp);
891 	if (!ret) {
892 		if (pd)
893 			atomic_dec(&pd->usecnt);
894 		if (scq)
895 			atomic_dec(&scq->usecnt);
896 		if (rcq)
897 			atomic_dec(&rcq->usecnt);
898 		if (srq)
899 			atomic_dec(&srq->usecnt);
900 	}
901 
902 	return ret;
903 }
904 EXPORT_SYMBOL(ib_destroy_qp);
905 
906 /* Completion queues */
907 
ib_create_cq(struct ib_device * device,ib_comp_handler comp_handler,void (* event_handler)(struct ib_event *,void *),void * cq_context,int cqe,int comp_vector)908 struct ib_cq *ib_create_cq(struct ib_device *device,
909 			   ib_comp_handler comp_handler,
910 			   void (*event_handler)(struct ib_event *, void *),
911 			   void *cq_context, int cqe, int comp_vector)
912 {
913 	struct ib_cq *cq;
914 
915 	cq = device->create_cq(device, cqe, comp_vector, NULL, NULL);
916 
917 	if (!IS_ERR(cq)) {
918 		cq->device        = device;
919 		cq->uobject       = NULL;
920 		cq->comp_handler  = comp_handler;
921 		cq->event_handler = event_handler;
922 		cq->cq_context    = cq_context;
923 		atomic_set(&cq->usecnt, 0);
924 	}
925 
926 	return cq;
927 }
928 EXPORT_SYMBOL(ib_create_cq);
929 
ib_modify_cq(struct ib_cq * cq,u16 cq_count,u16 cq_period)930 int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
931 {
932 	return cq->device->modify_cq ?
933 		cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
934 }
935 EXPORT_SYMBOL(ib_modify_cq);
936 
ib_destroy_cq(struct ib_cq * cq)937 int ib_destroy_cq(struct ib_cq *cq)
938 {
939 	if (atomic_read(&cq->usecnt))
940 		return -EBUSY;
941 
942 	return cq->device->destroy_cq(cq);
943 }
944 EXPORT_SYMBOL(ib_destroy_cq);
945 
ib_resize_cq(struct ib_cq * cq,int cqe)946 int ib_resize_cq(struct ib_cq *cq, int cqe)
947 {
948 	return cq->device->resize_cq ?
949 		cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
950 }
951 EXPORT_SYMBOL(ib_resize_cq);
952 
953 /* Memory regions */
954 
ib_get_dma_mr(struct ib_pd * pd,int mr_access_flags)955 struct ib_mr *ib_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
956 {
957 	struct ib_mr *mr;
958 
959 	mr = pd->device->get_dma_mr(pd, mr_access_flags);
960 
961 	if (!IS_ERR(mr)) {
962 		mr->device  = pd->device;
963 		mr->pd      = pd;
964 		mr->uobject = NULL;
965 		atomic_inc(&pd->usecnt);
966 		atomic_set(&mr->usecnt, 0);
967 	}
968 
969 	return mr;
970 }
971 EXPORT_SYMBOL(ib_get_dma_mr);
972 
ib_reg_phys_mr(struct ib_pd * pd,struct ib_phys_buf * phys_buf_array,int num_phys_buf,int mr_access_flags,u64 * iova_start)973 struct ib_mr *ib_reg_phys_mr(struct ib_pd *pd,
974 			     struct ib_phys_buf *phys_buf_array,
975 			     int num_phys_buf,
976 			     int mr_access_flags,
977 			     u64 *iova_start)
978 {
979 	struct ib_mr *mr;
980 
981 	if (!pd->device->reg_phys_mr)
982 		return ERR_PTR(-ENOSYS);
983 
984 	mr = pd->device->reg_phys_mr(pd, phys_buf_array, num_phys_buf,
985 				     mr_access_flags, iova_start);
986 
987 	if (!IS_ERR(mr)) {
988 		mr->device  = pd->device;
989 		mr->pd      = pd;
990 		mr->uobject = NULL;
991 		atomic_inc(&pd->usecnt);
992 		atomic_set(&mr->usecnt, 0);
993 	}
994 
995 	return mr;
996 }
997 EXPORT_SYMBOL(ib_reg_phys_mr);
998 
ib_rereg_phys_mr(struct ib_mr * mr,int mr_rereg_mask,struct ib_pd * pd,struct ib_phys_buf * phys_buf_array,int num_phys_buf,int mr_access_flags,u64 * iova_start)999 int ib_rereg_phys_mr(struct ib_mr *mr,
1000 		     int mr_rereg_mask,
1001 		     struct ib_pd *pd,
1002 		     struct ib_phys_buf *phys_buf_array,
1003 		     int num_phys_buf,
1004 		     int mr_access_flags,
1005 		     u64 *iova_start)
1006 {
1007 	struct ib_pd *old_pd;
1008 	int ret;
1009 
1010 	if (!mr->device->rereg_phys_mr)
1011 		return -ENOSYS;
1012 
1013 	if (atomic_read(&mr->usecnt))
1014 		return -EBUSY;
1015 
1016 	old_pd = mr->pd;
1017 
1018 	ret = mr->device->rereg_phys_mr(mr, mr_rereg_mask, pd,
1019 					phys_buf_array, num_phys_buf,
1020 					mr_access_flags, iova_start);
1021 
1022 	if (!ret && (mr_rereg_mask & IB_MR_REREG_PD)) {
1023 		atomic_dec(&old_pd->usecnt);
1024 		atomic_inc(&pd->usecnt);
1025 	}
1026 
1027 	return ret;
1028 }
1029 EXPORT_SYMBOL(ib_rereg_phys_mr);
1030 
ib_query_mr(struct ib_mr * mr,struct ib_mr_attr * mr_attr)1031 int ib_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
1032 {
1033 	return mr->device->query_mr ?
1034 		mr->device->query_mr(mr, mr_attr) : -ENOSYS;
1035 }
1036 EXPORT_SYMBOL(ib_query_mr);
1037 
ib_dereg_mr(struct ib_mr * mr)1038 int ib_dereg_mr(struct ib_mr *mr)
1039 {
1040 	struct ib_pd *pd;
1041 	int ret;
1042 
1043 	if (atomic_read(&mr->usecnt))
1044 		return -EBUSY;
1045 
1046 	pd = mr->pd;
1047 	ret = mr->device->dereg_mr(mr);
1048 	if (!ret)
1049 		atomic_dec(&pd->usecnt);
1050 
1051 	return ret;
1052 }
1053 EXPORT_SYMBOL(ib_dereg_mr);
1054 
ib_alloc_fast_reg_mr(struct ib_pd * pd,int max_page_list_len)1055 struct ib_mr *ib_alloc_fast_reg_mr(struct ib_pd *pd, int max_page_list_len)
1056 {
1057 	struct ib_mr *mr;
1058 
1059 	if (!pd->device->alloc_fast_reg_mr)
1060 		return ERR_PTR(-ENOSYS);
1061 
1062 	mr = pd->device->alloc_fast_reg_mr(pd, max_page_list_len);
1063 
1064 	if (!IS_ERR(mr)) {
1065 		mr->device  = pd->device;
1066 		mr->pd      = pd;
1067 		mr->uobject = NULL;
1068 		atomic_inc(&pd->usecnt);
1069 		atomic_set(&mr->usecnt, 0);
1070 	}
1071 
1072 	return mr;
1073 }
1074 EXPORT_SYMBOL(ib_alloc_fast_reg_mr);
1075 
ib_alloc_fast_reg_page_list(struct ib_device * device,int max_page_list_len)1076 struct ib_fast_reg_page_list *ib_alloc_fast_reg_page_list(struct ib_device *device,
1077 							  int max_page_list_len)
1078 {
1079 	struct ib_fast_reg_page_list *page_list;
1080 
1081 	if (!device->alloc_fast_reg_page_list)
1082 		return ERR_PTR(-ENOSYS);
1083 
1084 	page_list = device->alloc_fast_reg_page_list(device, max_page_list_len);
1085 
1086 	if (!IS_ERR(page_list)) {
1087 		page_list->device = device;
1088 		page_list->max_page_list_len = max_page_list_len;
1089 	}
1090 
1091 	return page_list;
1092 }
1093 EXPORT_SYMBOL(ib_alloc_fast_reg_page_list);
1094 
ib_free_fast_reg_page_list(struct ib_fast_reg_page_list * page_list)1095 void ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list)
1096 {
1097 	page_list->device->free_fast_reg_page_list(page_list);
1098 }
1099 EXPORT_SYMBOL(ib_free_fast_reg_page_list);
1100 
1101 /* Memory windows */
1102 
ib_alloc_mw(struct ib_pd * pd,enum ib_mw_type type)1103 struct ib_mw *ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type)
1104 {
1105 	struct ib_mw *mw;
1106 
1107 	if (!pd->device->alloc_mw)
1108 		return ERR_PTR(-ENOSYS);
1109 
1110 	mw = pd->device->alloc_mw(pd, type);
1111 	if (!IS_ERR(mw)) {
1112 		mw->device  = pd->device;
1113 		mw->pd      = pd;
1114 		mw->uobject = NULL;
1115 		mw->type    = type;
1116 		atomic_inc(&pd->usecnt);
1117 	}
1118 
1119 	return mw;
1120 }
1121 EXPORT_SYMBOL(ib_alloc_mw);
1122 
ib_dealloc_mw(struct ib_mw * mw)1123 int ib_dealloc_mw(struct ib_mw *mw)
1124 {
1125 	struct ib_pd *pd;
1126 	int ret;
1127 
1128 	pd = mw->pd;
1129 	ret = mw->device->dealloc_mw(mw);
1130 	if (!ret)
1131 		atomic_dec(&pd->usecnt);
1132 
1133 	return ret;
1134 }
1135 EXPORT_SYMBOL(ib_dealloc_mw);
1136 
1137 /* "Fast" memory regions */
1138 
ib_alloc_fmr(struct ib_pd * pd,int mr_access_flags,struct ib_fmr_attr * fmr_attr)1139 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1140 			    int mr_access_flags,
1141 			    struct ib_fmr_attr *fmr_attr)
1142 {
1143 	struct ib_fmr *fmr;
1144 
1145 	if (!pd->device->alloc_fmr)
1146 		return ERR_PTR(-ENOSYS);
1147 
1148 	fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
1149 	if (!IS_ERR(fmr)) {
1150 		fmr->device = pd->device;
1151 		fmr->pd     = pd;
1152 		atomic_inc(&pd->usecnt);
1153 	}
1154 
1155 	return fmr;
1156 }
1157 EXPORT_SYMBOL(ib_alloc_fmr);
1158 
ib_unmap_fmr(struct list_head * fmr_list)1159 int ib_unmap_fmr(struct list_head *fmr_list)
1160 {
1161 	struct ib_fmr *fmr;
1162 
1163 	if (list_empty(fmr_list))
1164 		return 0;
1165 
1166 	fmr = list_entry(fmr_list->next, struct ib_fmr, list);
1167 	return fmr->device->unmap_fmr(fmr_list);
1168 }
1169 EXPORT_SYMBOL(ib_unmap_fmr);
1170 
ib_dealloc_fmr(struct ib_fmr * fmr)1171 int ib_dealloc_fmr(struct ib_fmr *fmr)
1172 {
1173 	struct ib_pd *pd;
1174 	int ret;
1175 
1176 	pd = fmr->pd;
1177 	ret = fmr->device->dealloc_fmr(fmr);
1178 	if (!ret)
1179 		atomic_dec(&pd->usecnt);
1180 
1181 	return ret;
1182 }
1183 EXPORT_SYMBOL(ib_dealloc_fmr);
1184 
1185 /* Multicast groups */
1186 
ib_attach_mcast(struct ib_qp * qp,union ib_gid * gid,u16 lid)1187 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1188 {
1189 	int ret;
1190 
1191 	if (!qp->device->attach_mcast)
1192 		return -ENOSYS;
1193 	if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
1194 		return -EINVAL;
1195 
1196 	ret = qp->device->attach_mcast(qp, gid, lid);
1197 	if (!ret)
1198 		atomic_inc(&qp->usecnt);
1199 	return ret;
1200 }
1201 EXPORT_SYMBOL(ib_attach_mcast);
1202 
ib_detach_mcast(struct ib_qp * qp,union ib_gid * gid,u16 lid)1203 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1204 {
1205 	int ret;
1206 
1207 	if (!qp->device->detach_mcast)
1208 		return -ENOSYS;
1209 	if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
1210 		return -EINVAL;
1211 
1212 	ret = qp->device->detach_mcast(qp, gid, lid);
1213 	if (!ret)
1214 		atomic_dec(&qp->usecnt);
1215 	return ret;
1216 }
1217 EXPORT_SYMBOL(ib_detach_mcast);
1218 
ib_alloc_xrcd(struct ib_device * device)1219 struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device)
1220 {
1221 	struct ib_xrcd *xrcd;
1222 
1223 	if (!device->alloc_xrcd)
1224 		return ERR_PTR(-ENOSYS);
1225 
1226 	xrcd = device->alloc_xrcd(device, NULL, NULL);
1227 	if (!IS_ERR(xrcd)) {
1228 		xrcd->device = device;
1229 		xrcd->inode = NULL;
1230 		atomic_set(&xrcd->usecnt, 0);
1231 		mutex_init(&xrcd->tgt_qp_mutex);
1232 		INIT_LIST_HEAD(&xrcd->tgt_qp_list);
1233 	}
1234 
1235 	return xrcd;
1236 }
1237 EXPORT_SYMBOL(ib_alloc_xrcd);
1238 
ib_dealloc_xrcd(struct ib_xrcd * xrcd)1239 int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1240 {
1241 	struct ib_qp *qp;
1242 	int ret;
1243 
1244 	if (atomic_read(&xrcd->usecnt))
1245 		return -EBUSY;
1246 
1247 	while (!list_empty(&xrcd->tgt_qp_list)) {
1248 		qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
1249 		ret = ib_destroy_qp(qp);
1250 		if (ret)
1251 			return ret;
1252 	}
1253 
1254 	return xrcd->device->dealloc_xrcd(xrcd);
1255 }
1256 EXPORT_SYMBOL(ib_dealloc_xrcd);
1257