i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / infiniband / core / verbs.c
blob575b78045aafd1b5a861f9fad753f0108dd7e5d0
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.
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:
16 * Redistribution and use in source and binary forms, with or
17 * without modification, are permitted provided that the following
18 * conditions are met:
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer.
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.
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.
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>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_cache.h>
48 int ib_rate_to_mult(enum ib_rate rate)
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;
63 EXPORT_SYMBOL(ib_rate_to_mult);
65 enum ib_rate mult_to_ib_rate(int mult)
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;
80 EXPORT_SYMBOL(mult_to_ib_rate);
82 int ib_rate_to_mbps(enum ib_rate rate)
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;
105 EXPORT_SYMBOL(ib_rate_to_mbps);
107 enum rdma_transport_type
108 rdma_node_get_transport(enum rdma_node_type node_type)
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;
122 EXPORT_SYMBOL(rdma_node_get_transport);
124 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
126 if (device->get_link_layer)
127 return device->get_link_layer(device, port_num);
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;
138 EXPORT_SYMBOL(rdma_port_get_link_layer);
140 /* Protection domains */
142 struct ib_pd *ib_alloc_pd(struct ib_device *device)
144 struct ib_pd *pd;
146 pd = device->alloc_pd(device, NULL, NULL);
148 if (!IS_ERR(pd)) {
149 pd->device = device;
150 pd->uobject = NULL;
151 atomic_set(&pd->usecnt, 0);
154 return pd;
156 EXPORT_SYMBOL(ib_alloc_pd);
158 int ib_dealloc_pd(struct ib_pd *pd)
160 if (atomic_read(&pd->usecnt))
161 return -EBUSY;
163 return pd->device->dealloc_pd(pd);
165 EXPORT_SYMBOL(ib_dealloc_pd);
167 /* Address handles */
169 struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
171 struct ib_ah *ah;
173 ah = pd->device->create_ah(pd, ah_attr);
175 if (!IS_ERR(ah)) {
176 ah->device = pd->device;
177 ah->pd = pd;
178 ah->uobject = NULL;
179 atomic_inc(&pd->usecnt);
182 return ah;
184 EXPORT_SYMBOL(ib_create_ah);
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)
189 u32 flow_class;
190 u16 gid_index;
191 int ret;
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;
199 if (wc->wc_flags & IB_WC_GRH) {
200 ah_attr->ah_flags = IB_AH_GRH;
201 ah_attr->grh.dgid = grh->sgid;
203 ret = ib_find_cached_gid(device, &grh->dgid, &port_num,
204 &gid_index);
205 if (ret)
206 return ret;
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;
214 return 0;
216 EXPORT_SYMBOL(ib_init_ah_from_wc);
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)
221 struct ib_ah_attr ah_attr;
222 int ret;
224 ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
225 if (ret)
226 return ERR_PTR(ret);
228 return ib_create_ah(pd, &ah_attr);
230 EXPORT_SYMBOL(ib_create_ah_from_wc);
232 int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
234 return ah->device->modify_ah ?
235 ah->device->modify_ah(ah, ah_attr) :
236 -ENOSYS;
238 EXPORT_SYMBOL(ib_modify_ah);
240 int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
242 return ah->device->query_ah ?
243 ah->device->query_ah(ah, ah_attr) :
244 -ENOSYS;
246 EXPORT_SYMBOL(ib_query_ah);
248 int ib_destroy_ah(struct ib_ah *ah)
250 struct ib_pd *pd;
251 int ret;
253 pd = ah->pd;
254 ret = ah->device->destroy_ah(ah);
255 if (!ret)
256 atomic_dec(&pd->usecnt);
258 return ret;
260 EXPORT_SYMBOL(ib_destroy_ah);
262 /* Shared receive queues */
264 struct ib_srq *ib_create_srq(struct ib_pd *pd,
265 struct ib_srq_init_attr *srq_init_attr)
267 struct ib_srq *srq;
269 if (!pd->device->create_srq)
270 return ERR_PTR(-ENOSYS);
272 srq = pd->device->create_srq(pd, srq_init_attr, NULL);
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);
287 atomic_inc(&pd->usecnt);
288 atomic_set(&srq->usecnt, 0);
291 return srq;
293 EXPORT_SYMBOL(ib_create_srq);
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)
299 return srq->device->modify_srq ?
300 srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
301 -ENOSYS;
303 EXPORT_SYMBOL(ib_modify_srq);
305 int ib_query_srq(struct ib_srq *srq,
306 struct ib_srq_attr *srq_attr)
308 return srq->device->query_srq ?
309 srq->device->query_srq(srq, srq_attr) : -ENOSYS;
311 EXPORT_SYMBOL(ib_query_srq);
313 int ib_destroy_srq(struct ib_srq *srq)
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;
321 if (atomic_read(&srq->usecnt))
322 return -EBUSY;
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;
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);
340 return ret;
342 EXPORT_SYMBOL(ib_destroy_srq);
344 /* Queue pairs */
346 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
348 struct ib_qp *qp = context;
350 list_for_each_entry(event->element.qp, &qp->open_list, open_list)
351 event->element.qp->event_handler(event, event->element.qp->qp_context);
354 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
356 mutex_lock(&xrcd->tgt_qp_mutex);
357 list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
358 mutex_unlock(&xrcd->tgt_qp_mutex);
361 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
362 void (*event_handler)(struct ib_event *, void *),
363 void *qp_context)
365 struct ib_qp *qp;
366 unsigned long flags;
368 qp = kzalloc(sizeof *qp, GFP_KERNEL);
369 if (!qp)
370 return ERR_PTR(-ENOMEM);
372 qp->real_qp = real_qp;
373 atomic_inc(&real_qp->usecnt);
374 qp->device = real_qp->device;
375 qp->event_handler = event_handler;
376 qp->qp_context = qp_context;
377 qp->qp_num = real_qp->qp_num;
378 qp->qp_type = real_qp->qp_type;
380 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
381 list_add(&qp->open_list, &real_qp->open_list);
382 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
384 return qp;
387 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
388 struct ib_qp_open_attr *qp_open_attr)
390 struct ib_qp *qp, *real_qp;
392 if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
393 return ERR_PTR(-EINVAL);
395 qp = ERR_PTR(-EINVAL);
396 mutex_lock(&xrcd->tgt_qp_mutex);
397 list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
398 if (real_qp->qp_num == qp_open_attr->qp_num) {
399 qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
400 qp_open_attr->qp_context);
401 break;
404 mutex_unlock(&xrcd->tgt_qp_mutex);
405 return qp;
407 EXPORT_SYMBOL(ib_open_qp);
409 struct ib_qp *ib_create_qp(struct ib_pd *pd,
410 struct ib_qp_init_attr *qp_init_attr)
412 struct ib_qp *qp, *real_qp;
413 struct ib_device *device;
415 device = pd ? pd->device : qp_init_attr->xrcd->device;
416 qp = device->create_qp(pd, qp_init_attr, NULL);
418 if (!IS_ERR(qp)) {
419 qp->device = device;
420 qp->real_qp = qp;
421 qp->uobject = NULL;
422 qp->qp_type = qp_init_attr->qp_type;
424 atomic_set(&qp->usecnt, 0);
425 if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
426 qp->event_handler = __ib_shared_qp_event_handler;
427 qp->qp_context = qp;
428 qp->pd = NULL;
429 qp->send_cq = qp->recv_cq = NULL;
430 qp->srq = NULL;
431 qp->xrcd = qp_init_attr->xrcd;
432 atomic_inc(&qp_init_attr->xrcd->usecnt);
433 INIT_LIST_HEAD(&qp->open_list);
435 real_qp = qp;
436 qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
437 qp_init_attr->qp_context);
438 if (!IS_ERR(qp))
439 __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
440 else
441 real_qp->device->destroy_qp(real_qp);
442 } else {
443 qp->event_handler = qp_init_attr->event_handler;
444 qp->qp_context = qp_init_attr->qp_context;
445 if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
446 qp->recv_cq = NULL;
447 qp->srq = NULL;
448 } else {
449 qp->recv_cq = qp_init_attr->recv_cq;
450 atomic_inc(&qp_init_attr->recv_cq->usecnt);
451 qp->srq = qp_init_attr->srq;
452 if (qp->srq)
453 atomic_inc(&qp_init_attr->srq->usecnt);
456 qp->pd = pd;
457 qp->send_cq = qp_init_attr->send_cq;
458 qp->xrcd = NULL;
460 atomic_inc(&pd->usecnt);
461 atomic_inc(&qp_init_attr->send_cq->usecnt);
465 return qp;
467 EXPORT_SYMBOL(ib_create_qp);
469 static const struct {
470 int valid;
471 enum ib_qp_attr_mask req_param[IB_QPT_MAX];
472 enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
473 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
474 [IB_QPS_RESET] = {
475 [IB_QPS_RESET] = { .valid = 1 },
476 [IB_QPS_INIT] = {
477 .valid = 1,
478 .req_param = {
479 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
480 IB_QP_PORT |
481 IB_QP_QKEY),
482 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
483 IB_QP_PORT |
484 IB_QP_ACCESS_FLAGS),
485 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
486 IB_QP_PORT |
487 IB_QP_ACCESS_FLAGS),
488 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
489 IB_QP_PORT |
490 IB_QP_ACCESS_FLAGS),
491 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
492 IB_QP_PORT |
493 IB_QP_ACCESS_FLAGS),
494 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
495 IB_QP_QKEY),
496 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
497 IB_QP_QKEY),
501 [IB_QPS_INIT] = {
502 [IB_QPS_RESET] = { .valid = 1 },
503 [IB_QPS_ERR] = { .valid = 1 },
504 [IB_QPS_INIT] = {
505 .valid = 1,
506 .opt_param = {
507 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
508 IB_QP_PORT |
509 IB_QP_QKEY),
510 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
511 IB_QP_PORT |
512 IB_QP_ACCESS_FLAGS),
513 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
514 IB_QP_PORT |
515 IB_QP_ACCESS_FLAGS),
516 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
517 IB_QP_PORT |
518 IB_QP_ACCESS_FLAGS),
519 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
520 IB_QP_PORT |
521 IB_QP_ACCESS_FLAGS),
522 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
523 IB_QP_QKEY),
524 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
525 IB_QP_QKEY),
528 [IB_QPS_RTR] = {
529 .valid = 1,
530 .req_param = {
531 [IB_QPT_UC] = (IB_QP_AV |
532 IB_QP_PATH_MTU |
533 IB_QP_DEST_QPN |
534 IB_QP_RQ_PSN),
535 [IB_QPT_RC] = (IB_QP_AV |
536 IB_QP_PATH_MTU |
537 IB_QP_DEST_QPN |
538 IB_QP_RQ_PSN |
539 IB_QP_MAX_DEST_RD_ATOMIC |
540 IB_QP_MIN_RNR_TIMER),
541 [IB_QPT_XRC_INI] = (IB_QP_AV |
542 IB_QP_PATH_MTU |
543 IB_QP_DEST_QPN |
544 IB_QP_RQ_PSN),
545 [IB_QPT_XRC_TGT] = (IB_QP_AV |
546 IB_QP_PATH_MTU |
547 IB_QP_DEST_QPN |
548 IB_QP_RQ_PSN |
549 IB_QP_MAX_DEST_RD_ATOMIC |
550 IB_QP_MIN_RNR_TIMER),
552 .opt_param = {
553 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
554 IB_QP_QKEY),
555 [IB_QPT_UC] = (IB_QP_ALT_PATH |
556 IB_QP_ACCESS_FLAGS |
557 IB_QP_PKEY_INDEX),
558 [IB_QPT_RC] = (IB_QP_ALT_PATH |
559 IB_QP_ACCESS_FLAGS |
560 IB_QP_PKEY_INDEX),
561 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
562 IB_QP_ACCESS_FLAGS |
563 IB_QP_PKEY_INDEX),
564 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
565 IB_QP_ACCESS_FLAGS |
566 IB_QP_PKEY_INDEX),
567 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
568 IB_QP_QKEY),
569 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
570 IB_QP_QKEY),
574 [IB_QPS_RTR] = {
575 [IB_QPS_RESET] = { .valid = 1 },
576 [IB_QPS_ERR] = { .valid = 1 },
577 [IB_QPS_RTS] = {
578 .valid = 1,
579 .req_param = {
580 [IB_QPT_UD] = IB_QP_SQ_PSN,
581 [IB_QPT_UC] = IB_QP_SQ_PSN,
582 [IB_QPT_RC] = (IB_QP_TIMEOUT |
583 IB_QP_RETRY_CNT |
584 IB_QP_RNR_RETRY |
585 IB_QP_SQ_PSN |
586 IB_QP_MAX_QP_RD_ATOMIC),
587 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
588 IB_QP_RETRY_CNT |
589 IB_QP_RNR_RETRY |
590 IB_QP_SQ_PSN |
591 IB_QP_MAX_QP_RD_ATOMIC),
592 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
593 IB_QP_SQ_PSN),
594 [IB_QPT_SMI] = IB_QP_SQ_PSN,
595 [IB_QPT_GSI] = IB_QP_SQ_PSN,
597 .opt_param = {
598 [IB_QPT_UD] = (IB_QP_CUR_STATE |
599 IB_QP_QKEY),
600 [IB_QPT_UC] = (IB_QP_CUR_STATE |
601 IB_QP_ALT_PATH |
602 IB_QP_ACCESS_FLAGS |
603 IB_QP_PATH_MIG_STATE),
604 [IB_QPT_RC] = (IB_QP_CUR_STATE |
605 IB_QP_ALT_PATH |
606 IB_QP_ACCESS_FLAGS |
607 IB_QP_MIN_RNR_TIMER |
608 IB_QP_PATH_MIG_STATE),
609 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
610 IB_QP_ALT_PATH |
611 IB_QP_ACCESS_FLAGS |
612 IB_QP_PATH_MIG_STATE),
613 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
614 IB_QP_ALT_PATH |
615 IB_QP_ACCESS_FLAGS |
616 IB_QP_MIN_RNR_TIMER |
617 IB_QP_PATH_MIG_STATE),
618 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
619 IB_QP_QKEY),
620 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
621 IB_QP_QKEY),
625 [IB_QPS_RTS] = {
626 [IB_QPS_RESET] = { .valid = 1 },
627 [IB_QPS_ERR] = { .valid = 1 },
628 [IB_QPS_RTS] = {
629 .valid = 1,
630 .opt_param = {
631 [IB_QPT_UD] = (IB_QP_CUR_STATE |
632 IB_QP_QKEY),
633 [IB_QPT_UC] = (IB_QP_CUR_STATE |
634 IB_QP_ACCESS_FLAGS |
635 IB_QP_ALT_PATH |
636 IB_QP_PATH_MIG_STATE),
637 [IB_QPT_RC] = (IB_QP_CUR_STATE |
638 IB_QP_ACCESS_FLAGS |
639 IB_QP_ALT_PATH |
640 IB_QP_PATH_MIG_STATE |
641 IB_QP_MIN_RNR_TIMER),
642 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
643 IB_QP_ACCESS_FLAGS |
644 IB_QP_ALT_PATH |
645 IB_QP_PATH_MIG_STATE),
646 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
647 IB_QP_ACCESS_FLAGS |
648 IB_QP_ALT_PATH |
649 IB_QP_PATH_MIG_STATE |
650 IB_QP_MIN_RNR_TIMER),
651 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
652 IB_QP_QKEY),
653 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
654 IB_QP_QKEY),
657 [IB_QPS_SQD] = {
658 .valid = 1,
659 .opt_param = {
660 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
661 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
662 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
663 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
664 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
665 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
666 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
670 [IB_QPS_SQD] = {
671 [IB_QPS_RESET] = { .valid = 1 },
672 [IB_QPS_ERR] = { .valid = 1 },
673 [IB_QPS_RTS] = {
674 .valid = 1,
675 .opt_param = {
676 [IB_QPT_UD] = (IB_QP_CUR_STATE |
677 IB_QP_QKEY),
678 [IB_QPT_UC] = (IB_QP_CUR_STATE |
679 IB_QP_ALT_PATH |
680 IB_QP_ACCESS_FLAGS |
681 IB_QP_PATH_MIG_STATE),
682 [IB_QPT_RC] = (IB_QP_CUR_STATE |
683 IB_QP_ALT_PATH |
684 IB_QP_ACCESS_FLAGS |
685 IB_QP_MIN_RNR_TIMER |
686 IB_QP_PATH_MIG_STATE),
687 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
688 IB_QP_ALT_PATH |
689 IB_QP_ACCESS_FLAGS |
690 IB_QP_PATH_MIG_STATE),
691 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
692 IB_QP_ALT_PATH |
693 IB_QP_ACCESS_FLAGS |
694 IB_QP_MIN_RNR_TIMER |
695 IB_QP_PATH_MIG_STATE),
696 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
697 IB_QP_QKEY),
698 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
699 IB_QP_QKEY),
702 [IB_QPS_SQD] = {
703 .valid = 1,
704 .opt_param = {
705 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
706 IB_QP_QKEY),
707 [IB_QPT_UC] = (IB_QP_AV |
708 IB_QP_ALT_PATH |
709 IB_QP_ACCESS_FLAGS |
710 IB_QP_PKEY_INDEX |
711 IB_QP_PATH_MIG_STATE),
712 [IB_QPT_RC] = (IB_QP_PORT |
713 IB_QP_AV |
714 IB_QP_TIMEOUT |
715 IB_QP_RETRY_CNT |
716 IB_QP_RNR_RETRY |
717 IB_QP_MAX_QP_RD_ATOMIC |
718 IB_QP_MAX_DEST_RD_ATOMIC |
719 IB_QP_ALT_PATH |
720 IB_QP_ACCESS_FLAGS |
721 IB_QP_PKEY_INDEX |
722 IB_QP_MIN_RNR_TIMER |
723 IB_QP_PATH_MIG_STATE),
724 [IB_QPT_XRC_INI] = (IB_QP_PORT |
725 IB_QP_AV |
726 IB_QP_TIMEOUT |
727 IB_QP_RETRY_CNT |
728 IB_QP_RNR_RETRY |
729 IB_QP_MAX_QP_RD_ATOMIC |
730 IB_QP_ALT_PATH |
731 IB_QP_ACCESS_FLAGS |
732 IB_QP_PKEY_INDEX |
733 IB_QP_PATH_MIG_STATE),
734 [IB_QPT_XRC_TGT] = (IB_QP_PORT |
735 IB_QP_AV |
736 IB_QP_TIMEOUT |
737 IB_QP_MAX_DEST_RD_ATOMIC |
738 IB_QP_ALT_PATH |
739 IB_QP_ACCESS_FLAGS |
740 IB_QP_PKEY_INDEX |
741 IB_QP_MIN_RNR_TIMER |
742 IB_QP_PATH_MIG_STATE),
743 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
744 IB_QP_QKEY),
745 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
746 IB_QP_QKEY),
750 [IB_QPS_SQE] = {
751 [IB_QPS_RESET] = { .valid = 1 },
752 [IB_QPS_ERR] = { .valid = 1 },
753 [IB_QPS_RTS] = {
754 .valid = 1,
755 .opt_param = {
756 [IB_QPT_UD] = (IB_QP_CUR_STATE |
757 IB_QP_QKEY),
758 [IB_QPT_UC] = (IB_QP_CUR_STATE |
759 IB_QP_ACCESS_FLAGS),
760 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
761 IB_QP_QKEY),
762 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
763 IB_QP_QKEY),
767 [IB_QPS_ERR] = {
768 [IB_QPS_RESET] = { .valid = 1 },
769 [IB_QPS_ERR] = { .valid = 1 }
773 int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
774 enum ib_qp_type type, enum ib_qp_attr_mask mask)
776 enum ib_qp_attr_mask req_param, opt_param;
778 if (cur_state < 0 || cur_state > IB_QPS_ERR ||
779 next_state < 0 || next_state > IB_QPS_ERR)
780 return 0;
782 if (mask & IB_QP_CUR_STATE &&
783 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
784 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
785 return 0;
787 if (!qp_state_table[cur_state][next_state].valid)
788 return 0;
790 req_param = qp_state_table[cur_state][next_state].req_param[type];
791 opt_param = qp_state_table[cur_state][next_state].opt_param[type];
793 if ((mask & req_param) != req_param)
794 return 0;
796 if (mask & ~(req_param | opt_param | IB_QP_STATE))
797 return 0;
799 return 1;
801 EXPORT_SYMBOL(ib_modify_qp_is_ok);
803 int ib_modify_qp(struct ib_qp *qp,
804 struct ib_qp_attr *qp_attr,
805 int qp_attr_mask)
807 return qp->device->modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
809 EXPORT_SYMBOL(ib_modify_qp);
811 int ib_query_qp(struct ib_qp *qp,
812 struct ib_qp_attr *qp_attr,
813 int qp_attr_mask,
814 struct ib_qp_init_attr *qp_init_attr)
816 return qp->device->query_qp ?
817 qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
818 -ENOSYS;
820 EXPORT_SYMBOL(ib_query_qp);
822 int ib_close_qp(struct ib_qp *qp)
824 struct ib_qp *real_qp;
825 unsigned long flags;
827 real_qp = qp->real_qp;
828 if (real_qp == qp)
829 return -EINVAL;
831 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
832 list_del(&qp->open_list);
833 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
835 atomic_dec(&real_qp->usecnt);
836 kfree(qp);
838 return 0;
840 EXPORT_SYMBOL(ib_close_qp);
842 static int __ib_destroy_shared_qp(struct ib_qp *qp)
844 struct ib_xrcd *xrcd;
845 struct ib_qp *real_qp;
846 int ret;
848 real_qp = qp->real_qp;
849 xrcd = real_qp->xrcd;
851 mutex_lock(&xrcd->tgt_qp_mutex);
852 ib_close_qp(qp);
853 if (atomic_read(&real_qp->usecnt) == 0)
854 list_del(&real_qp->xrcd_list);
855 else
856 real_qp = NULL;
857 mutex_unlock(&xrcd->tgt_qp_mutex);
859 if (real_qp) {
860 ret = ib_destroy_qp(real_qp);
861 if (!ret)
862 atomic_dec(&xrcd->usecnt);
863 else
864 __ib_insert_xrcd_qp(xrcd, real_qp);
867 return 0;
870 int ib_destroy_qp(struct ib_qp *qp)
872 struct ib_pd *pd;
873 struct ib_cq *scq, *rcq;
874 struct ib_srq *srq;
875 int ret;
877 if (atomic_read(&qp->usecnt))
878 return -EBUSY;
880 if (qp->real_qp != qp)
881 return __ib_destroy_shared_qp(qp);
883 pd = qp->pd;
884 scq = qp->send_cq;
885 rcq = qp->recv_cq;
886 srq = qp->srq;
888 ret = qp->device->destroy_qp(qp);
889 if (!ret) {
890 if (pd)
891 atomic_dec(&pd->usecnt);
892 if (scq)
893 atomic_dec(&scq->usecnt);
894 if (rcq)
895 atomic_dec(&rcq->usecnt);
896 if (srq)
897 atomic_dec(&srq->usecnt);
900 return ret;
902 EXPORT_SYMBOL(ib_destroy_qp);
904 /* Completion queues */
906 struct ib_cq *ib_create_cq(struct ib_device *device,
907 ib_comp_handler comp_handler,
908 void (*event_handler)(struct ib_event *, void *),
909 void *cq_context, int cqe, int comp_vector)
911 struct ib_cq *cq;
913 cq = device->create_cq(device, cqe, comp_vector, NULL, NULL);
915 if (!IS_ERR(cq)) {
916 cq->device = device;
917 cq->uobject = NULL;
918 cq->comp_handler = comp_handler;
919 cq->event_handler = event_handler;
920 cq->cq_context = cq_context;
921 atomic_set(&cq->usecnt, 0);
924 return cq;
926 EXPORT_SYMBOL(ib_create_cq);
928 int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
930 return cq->device->modify_cq ?
931 cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
933 EXPORT_SYMBOL(ib_modify_cq);
935 int ib_destroy_cq(struct ib_cq *cq)
937 if (atomic_read(&cq->usecnt))
938 return -EBUSY;
940 return cq->device->destroy_cq(cq);
942 EXPORT_SYMBOL(ib_destroy_cq);
944 int ib_resize_cq(struct ib_cq *cq, int cqe)
946 return cq->device->resize_cq ?
947 cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
949 EXPORT_SYMBOL(ib_resize_cq);
951 /* Memory regions */
953 struct ib_mr *ib_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
955 struct ib_mr *mr;
957 mr = pd->device->get_dma_mr(pd, mr_access_flags);
959 if (!IS_ERR(mr)) {
960 mr->device = pd->device;
961 mr->pd = pd;
962 mr->uobject = NULL;
963 atomic_inc(&pd->usecnt);
964 atomic_set(&mr->usecnt, 0);
967 return mr;
969 EXPORT_SYMBOL(ib_get_dma_mr);
971 struct ib_mr *ib_reg_phys_mr(struct ib_pd *pd,
972 struct ib_phys_buf *phys_buf_array,
973 int num_phys_buf,
974 int mr_access_flags,
975 u64 *iova_start)
977 struct ib_mr *mr;
979 if (!pd->device->reg_phys_mr)
980 return ERR_PTR(-ENOSYS);
982 mr = pd->device->reg_phys_mr(pd, phys_buf_array, num_phys_buf,
983 mr_access_flags, iova_start);
985 if (!IS_ERR(mr)) {
986 mr->device = pd->device;
987 mr->pd = pd;
988 mr->uobject = NULL;
989 atomic_inc(&pd->usecnt);
990 atomic_set(&mr->usecnt, 0);
993 return mr;
995 EXPORT_SYMBOL(ib_reg_phys_mr);
997 int ib_rereg_phys_mr(struct ib_mr *mr,
998 int mr_rereg_mask,
999 struct ib_pd *pd,
1000 struct ib_phys_buf *phys_buf_array,
1001 int num_phys_buf,
1002 int mr_access_flags,
1003 u64 *iova_start)
1005 struct ib_pd *old_pd;
1006 int ret;
1008 if (!mr->device->rereg_phys_mr)
1009 return -ENOSYS;
1011 if (atomic_read(&mr->usecnt))
1012 return -EBUSY;
1014 old_pd = mr->pd;
1016 ret = mr->device->rereg_phys_mr(mr, mr_rereg_mask, pd,
1017 phys_buf_array, num_phys_buf,
1018 mr_access_flags, iova_start);
1020 if (!ret && (mr_rereg_mask & IB_MR_REREG_PD)) {
1021 atomic_dec(&old_pd->usecnt);
1022 atomic_inc(&pd->usecnt);
1025 return ret;
1027 EXPORT_SYMBOL(ib_rereg_phys_mr);
1029 int ib_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
1031 return mr->device->query_mr ?
1032 mr->device->query_mr(mr, mr_attr) : -ENOSYS;
1034 EXPORT_SYMBOL(ib_query_mr);
1036 int ib_dereg_mr(struct ib_mr *mr)
1038 struct ib_pd *pd;
1039 int ret;
1041 if (atomic_read(&mr->usecnt))
1042 return -EBUSY;
1044 pd = mr->pd;
1045 ret = mr->device->dereg_mr(mr);
1046 if (!ret)
1047 atomic_dec(&pd->usecnt);
1049 return ret;
1051 EXPORT_SYMBOL(ib_dereg_mr);
1053 struct ib_mr *ib_alloc_fast_reg_mr(struct ib_pd *pd, int max_page_list_len)
1055 struct ib_mr *mr;
1057 if (!pd->device->alloc_fast_reg_mr)
1058 return ERR_PTR(-ENOSYS);
1060 mr = pd->device->alloc_fast_reg_mr(pd, max_page_list_len);
1062 if (!IS_ERR(mr)) {
1063 mr->device = pd->device;
1064 mr->pd = pd;
1065 mr->uobject = NULL;
1066 atomic_inc(&pd->usecnt);
1067 atomic_set(&mr->usecnt, 0);
1070 return mr;
1072 EXPORT_SYMBOL(ib_alloc_fast_reg_mr);
1074 struct ib_fast_reg_page_list *ib_alloc_fast_reg_page_list(struct ib_device *device,
1075 int max_page_list_len)
1077 struct ib_fast_reg_page_list *page_list;
1079 if (!device->alloc_fast_reg_page_list)
1080 return ERR_PTR(-ENOSYS);
1082 page_list = device->alloc_fast_reg_page_list(device, max_page_list_len);
1084 if (!IS_ERR(page_list)) {
1085 page_list->device = device;
1086 page_list->max_page_list_len = max_page_list_len;
1089 return page_list;
1091 EXPORT_SYMBOL(ib_alloc_fast_reg_page_list);
1093 void ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list)
1095 page_list->device->free_fast_reg_page_list(page_list);
1097 EXPORT_SYMBOL(ib_free_fast_reg_page_list);
1099 /* Memory windows */
1101 struct ib_mw *ib_alloc_mw(struct ib_pd *pd)
1103 struct ib_mw *mw;
1105 if (!pd->device->alloc_mw)
1106 return ERR_PTR(-ENOSYS);
1108 mw = pd->device->alloc_mw(pd);
1109 if (!IS_ERR(mw)) {
1110 mw->device = pd->device;
1111 mw->pd = pd;
1112 mw->uobject = NULL;
1113 atomic_inc(&pd->usecnt);
1116 return mw;
1118 EXPORT_SYMBOL(ib_alloc_mw);
1120 int ib_dealloc_mw(struct ib_mw *mw)
1122 struct ib_pd *pd;
1123 int ret;
1125 pd = mw->pd;
1126 ret = mw->device->dealloc_mw(mw);
1127 if (!ret)
1128 atomic_dec(&pd->usecnt);
1130 return ret;
1132 EXPORT_SYMBOL(ib_dealloc_mw);
1134 /* "Fast" memory regions */
1136 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1137 int mr_access_flags,
1138 struct ib_fmr_attr *fmr_attr)
1140 struct ib_fmr *fmr;
1142 if (!pd->device->alloc_fmr)
1143 return ERR_PTR(-ENOSYS);
1145 fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
1146 if (!IS_ERR(fmr)) {
1147 fmr->device = pd->device;
1148 fmr->pd = pd;
1149 atomic_inc(&pd->usecnt);
1152 return fmr;
1154 EXPORT_SYMBOL(ib_alloc_fmr);
1156 int ib_unmap_fmr(struct list_head *fmr_list)
1158 struct ib_fmr *fmr;
1160 if (list_empty(fmr_list))
1161 return 0;
1163 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
1164 return fmr->device->unmap_fmr(fmr_list);
1166 EXPORT_SYMBOL(ib_unmap_fmr);
1168 int ib_dealloc_fmr(struct ib_fmr *fmr)
1170 struct ib_pd *pd;
1171 int ret;
1173 pd = fmr->pd;
1174 ret = fmr->device->dealloc_fmr(fmr);
1175 if (!ret)
1176 atomic_dec(&pd->usecnt);
1178 return ret;
1180 EXPORT_SYMBOL(ib_dealloc_fmr);
1182 /* Multicast groups */
1184 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1186 if (!qp->device->attach_mcast)
1187 return -ENOSYS;
1188 if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
1189 return -EINVAL;
1191 return qp->device->attach_mcast(qp, gid, lid);
1193 EXPORT_SYMBOL(ib_attach_mcast);
1195 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1197 if (!qp->device->detach_mcast)
1198 return -ENOSYS;
1199 if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
1200 return -EINVAL;
1202 return qp->device->detach_mcast(qp, gid, lid);
1204 EXPORT_SYMBOL(ib_detach_mcast);
1206 struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device)
1208 struct ib_xrcd *xrcd;
1210 if (!device->alloc_xrcd)
1211 return ERR_PTR(-ENOSYS);
1213 xrcd = device->alloc_xrcd(device, NULL, NULL);
1214 if (!IS_ERR(xrcd)) {
1215 xrcd->device = device;
1216 xrcd->inode = NULL;
1217 atomic_set(&xrcd->usecnt, 0);
1218 mutex_init(&xrcd->tgt_qp_mutex);
1219 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
1222 return xrcd;
1224 EXPORT_SYMBOL(ib_alloc_xrcd);
1226 int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1228 struct ib_qp *qp;
1229 int ret;
1231 if (atomic_read(&xrcd->usecnt))
1232 return -EBUSY;
1234 while (!list_empty(&xrcd->tgt_qp_list)) {
1235 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
1236 ret = ib_destroy_qp(qp);
1237 if (ret)
1238 return ret;
1241 return xrcd->device->dealloc_xrcd(xrcd);
1243 EXPORT_SYMBOL(ib_dealloc_xrcd);