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
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
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
38 * $Id: verbs.c 1349 2004-12-16 21:09:43Z roland $
41 #include <linux/errno.h>
42 #include <linux/err.h>
43 #include <linux/string.h>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_cache.h>
48 int ib_rate_to_mult(enum ib_rate 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;
63 EXPORT_SYMBOL(ib_rate_to_mult);
65 enum ib_rate mult_to_ib_rate(int 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 enum rdma_transport_type
83 rdma_node_get_transport(enum rdma_node_type node_type)
87 case RDMA_NODE_IB_SWITCH:
88 case RDMA_NODE_IB_ROUTER:
89 return RDMA_TRANSPORT_IB;
91 return RDMA_TRANSPORT_IWARP;
97 EXPORT_SYMBOL(rdma_node_get_transport);
99 /* Protection domains */
101 struct ib_pd *ib_alloc_pd(struct ib_device *device)
105 pd = device->alloc_pd(device, NULL, NULL);
110 atomic_set(&pd->usecnt, 0);
115 EXPORT_SYMBOL(ib_alloc_pd);
117 int ib_dealloc_pd(struct ib_pd *pd)
119 if (atomic_read(&pd->usecnt))
122 return pd->device->dealloc_pd(pd);
124 EXPORT_SYMBOL(ib_dealloc_pd);
126 /* Address handles */
128 struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
132 ah = pd->device->create_ah(pd, ah_attr);
135 ah->device = pd->device;
138 atomic_inc(&pd->usecnt);
143 EXPORT_SYMBOL(ib_create_ah);
145 int ib_init_ah_from_wc(struct ib_device *device, u8 port_num, struct ib_wc *wc,
146 struct ib_grh *grh, struct ib_ah_attr *ah_attr)
152 memset(ah_attr, 0, sizeof *ah_attr);
153 ah_attr->dlid = wc->slid;
154 ah_attr->sl = wc->sl;
155 ah_attr->src_path_bits = wc->dlid_path_bits;
156 ah_attr->port_num = port_num;
158 if (wc->wc_flags & IB_WC_GRH) {
159 ah_attr->ah_flags = IB_AH_GRH;
160 ah_attr->grh.dgid = grh->sgid;
162 ret = ib_find_cached_gid(device, &grh->dgid, &port_num,
167 ah_attr->grh.sgid_index = (u8) gid_index;
168 flow_class = be32_to_cpu(grh->version_tclass_flow);
169 ah_attr->grh.flow_label = flow_class & 0xFFFFF;
170 ah_attr->grh.hop_limit = 0xFF;
171 ah_attr->grh.traffic_class = (flow_class >> 20) & 0xFF;
175 EXPORT_SYMBOL(ib_init_ah_from_wc);
177 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, struct ib_wc *wc,
178 struct ib_grh *grh, u8 port_num)
180 struct ib_ah_attr ah_attr;
183 ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
187 return ib_create_ah(pd, &ah_attr);
189 EXPORT_SYMBOL(ib_create_ah_from_wc);
191 int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
193 return ah->device->modify_ah ?
194 ah->device->modify_ah(ah, ah_attr) :
197 EXPORT_SYMBOL(ib_modify_ah);
199 int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
201 return ah->device->query_ah ?
202 ah->device->query_ah(ah, ah_attr) :
205 EXPORT_SYMBOL(ib_query_ah);
207 int ib_destroy_ah(struct ib_ah *ah)
213 ret = ah->device->destroy_ah(ah);
215 atomic_dec(&pd->usecnt);
219 EXPORT_SYMBOL(ib_destroy_ah);
221 /* Shared receive queues */
223 struct ib_srq *ib_create_srq(struct ib_pd *pd,
224 struct ib_srq_init_attr *srq_init_attr)
228 if (!pd->device->create_srq)
229 return ERR_PTR(-ENOSYS);
231 srq = pd->device->create_srq(pd, srq_init_attr, NULL);
234 srq->device = pd->device;
237 srq->event_handler = srq_init_attr->event_handler;
238 srq->srq_context = srq_init_attr->srq_context;
239 atomic_inc(&pd->usecnt);
240 atomic_set(&srq->usecnt, 0);
245 EXPORT_SYMBOL(ib_create_srq);
247 int ib_modify_srq(struct ib_srq *srq,
248 struct ib_srq_attr *srq_attr,
249 enum ib_srq_attr_mask srq_attr_mask)
251 return srq->device->modify_srq ?
252 srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
255 EXPORT_SYMBOL(ib_modify_srq);
257 int ib_query_srq(struct ib_srq *srq,
258 struct ib_srq_attr *srq_attr)
260 return srq->device->query_srq ?
261 srq->device->query_srq(srq, srq_attr) : -ENOSYS;
263 EXPORT_SYMBOL(ib_query_srq);
265 int ib_destroy_srq(struct ib_srq *srq)
270 if (atomic_read(&srq->usecnt))
275 ret = srq->device->destroy_srq(srq);
277 atomic_dec(&pd->usecnt);
281 EXPORT_SYMBOL(ib_destroy_srq);
285 struct ib_qp *ib_create_qp(struct ib_pd *pd,
286 struct ib_qp_init_attr *qp_init_attr)
290 qp = pd->device->create_qp(pd, qp_init_attr, NULL);
293 qp->device = pd->device;
295 qp->send_cq = qp_init_attr->send_cq;
296 qp->recv_cq = qp_init_attr->recv_cq;
297 qp->srq = qp_init_attr->srq;
299 qp->event_handler = qp_init_attr->event_handler;
300 qp->qp_context = qp_init_attr->qp_context;
301 qp->qp_type = qp_init_attr->qp_type;
302 atomic_inc(&pd->usecnt);
303 atomic_inc(&qp_init_attr->send_cq->usecnt);
304 atomic_inc(&qp_init_attr->recv_cq->usecnt);
305 if (qp_init_attr->srq)
306 atomic_inc(&qp_init_attr->srq->usecnt);
311 EXPORT_SYMBOL(ib_create_qp);
313 static const struct {
315 enum ib_qp_attr_mask req_param[IB_QPT_RAW_ETY + 1];
316 enum ib_qp_attr_mask opt_param[IB_QPT_RAW_ETY + 1];
317 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
319 [IB_QPS_RESET] = { .valid = 1 },
320 [IB_QPS_ERR] = { .valid = 1 },
324 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
327 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
330 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
333 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
335 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
341 [IB_QPS_RESET] = { .valid = 1 },
342 [IB_QPS_ERR] = { .valid = 1 },
346 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
349 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
352 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
355 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
357 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
364 [IB_QPT_UC] = (IB_QP_AV |
368 [IB_QPT_RC] = (IB_QP_AV |
372 IB_QP_MAX_DEST_RD_ATOMIC |
373 IB_QP_MIN_RNR_TIMER),
376 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
378 [IB_QPT_UC] = (IB_QP_ALT_PATH |
381 [IB_QPT_RC] = (IB_QP_ALT_PATH |
384 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
386 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
392 [IB_QPS_RESET] = { .valid = 1 },
393 [IB_QPS_ERR] = { .valid = 1 },
397 [IB_QPT_UD] = IB_QP_SQ_PSN,
398 [IB_QPT_UC] = IB_QP_SQ_PSN,
399 [IB_QPT_RC] = (IB_QP_TIMEOUT |
403 IB_QP_MAX_QP_RD_ATOMIC),
404 [IB_QPT_SMI] = IB_QP_SQ_PSN,
405 [IB_QPT_GSI] = IB_QP_SQ_PSN,
408 [IB_QPT_UD] = (IB_QP_CUR_STATE |
410 [IB_QPT_UC] = (IB_QP_CUR_STATE |
413 IB_QP_PATH_MIG_STATE),
414 [IB_QPT_RC] = (IB_QP_CUR_STATE |
417 IB_QP_MIN_RNR_TIMER |
418 IB_QP_PATH_MIG_STATE),
419 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
421 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
427 [IB_QPS_RESET] = { .valid = 1 },
428 [IB_QPS_ERR] = { .valid = 1 },
432 [IB_QPT_UD] = (IB_QP_CUR_STATE |
434 [IB_QPT_UC] = (IB_QP_CUR_STATE |
437 IB_QP_PATH_MIG_STATE),
438 [IB_QPT_RC] = (IB_QP_CUR_STATE |
441 IB_QP_PATH_MIG_STATE |
442 IB_QP_MIN_RNR_TIMER),
443 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
445 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
452 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
453 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
454 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
455 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
456 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
461 [IB_QPS_RESET] = { .valid = 1 },
462 [IB_QPS_ERR] = { .valid = 1 },
466 [IB_QPT_UD] = (IB_QP_CUR_STATE |
468 [IB_QPT_UC] = (IB_QP_CUR_STATE |
471 IB_QP_PATH_MIG_STATE),
472 [IB_QPT_RC] = (IB_QP_CUR_STATE |
475 IB_QP_MIN_RNR_TIMER |
476 IB_QP_PATH_MIG_STATE),
477 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
479 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
486 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
488 [IB_QPT_UC] = (IB_QP_AV |
492 IB_QP_PATH_MIG_STATE),
493 [IB_QPT_RC] = (IB_QP_PORT |
498 IB_QP_MAX_QP_RD_ATOMIC |
499 IB_QP_MAX_DEST_RD_ATOMIC |
503 IB_QP_MIN_RNR_TIMER |
504 IB_QP_PATH_MIG_STATE),
505 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
507 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
513 [IB_QPS_RESET] = { .valid = 1 },
514 [IB_QPS_ERR] = { .valid = 1 },
518 [IB_QPT_UD] = (IB_QP_CUR_STATE |
520 [IB_QPT_UC] = (IB_QP_CUR_STATE |
522 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
524 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
530 [IB_QPS_RESET] = { .valid = 1 },
531 [IB_QPS_ERR] = { .valid = 1 }
535 int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
536 enum ib_qp_type type, enum ib_qp_attr_mask mask)
538 enum ib_qp_attr_mask req_param, opt_param;
540 if (cur_state < 0 || cur_state > IB_QPS_ERR ||
541 next_state < 0 || next_state > IB_QPS_ERR)
544 if (mask & IB_QP_CUR_STATE &&
545 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
546 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
549 if (!qp_state_table[cur_state][next_state].valid)
552 req_param = qp_state_table[cur_state][next_state].req_param[type];
553 opt_param = qp_state_table[cur_state][next_state].opt_param[type];
555 if ((mask & req_param) != req_param)
558 if (mask & ~(req_param | opt_param | IB_QP_STATE))
563 EXPORT_SYMBOL(ib_modify_qp_is_ok);
565 int ib_modify_qp(struct ib_qp *qp,
566 struct ib_qp_attr *qp_attr,
569 return qp->device->modify_qp(qp, qp_attr, qp_attr_mask, NULL);
571 EXPORT_SYMBOL(ib_modify_qp);
573 int ib_query_qp(struct ib_qp *qp,
574 struct ib_qp_attr *qp_attr,
576 struct ib_qp_init_attr *qp_init_attr)
578 return qp->device->query_qp ?
579 qp->device->query_qp(qp, qp_attr, qp_attr_mask, qp_init_attr) :
582 EXPORT_SYMBOL(ib_query_qp);
584 int ib_destroy_qp(struct ib_qp *qp)
587 struct ib_cq *scq, *rcq;
596 ret = qp->device->destroy_qp(qp);
598 atomic_dec(&pd->usecnt);
599 atomic_dec(&scq->usecnt);
600 atomic_dec(&rcq->usecnt);
602 atomic_dec(&srq->usecnt);
607 EXPORT_SYMBOL(ib_destroy_qp);
609 /* Completion queues */
611 struct ib_cq *ib_create_cq(struct ib_device *device,
612 ib_comp_handler comp_handler,
613 void (*event_handler)(struct ib_event *, void *),
614 void *cq_context, int cqe, int comp_vector)
618 cq = device->create_cq(device, cqe, comp_vector, NULL, NULL);
623 cq->comp_handler = comp_handler;
624 cq->event_handler = event_handler;
625 cq->cq_context = cq_context;
626 atomic_set(&cq->usecnt, 0);
631 EXPORT_SYMBOL(ib_create_cq);
633 int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
635 return cq->device->modify_cq ?
636 cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
638 EXPORT_SYMBOL(ib_modify_cq);
640 int ib_destroy_cq(struct ib_cq *cq)
642 if (atomic_read(&cq->usecnt))
645 return cq->device->destroy_cq(cq);
647 EXPORT_SYMBOL(ib_destroy_cq);
649 int ib_resize_cq(struct ib_cq *cq, int cqe)
651 return cq->device->resize_cq ?
652 cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
654 EXPORT_SYMBOL(ib_resize_cq);
658 struct ib_mr *ib_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
662 mr = pd->device->get_dma_mr(pd, mr_access_flags);
665 mr->device = pd->device;
668 atomic_inc(&pd->usecnt);
669 atomic_set(&mr->usecnt, 0);
674 EXPORT_SYMBOL(ib_get_dma_mr);
676 struct ib_mr *ib_reg_phys_mr(struct ib_pd *pd,
677 struct ib_phys_buf *phys_buf_array,
684 if (!pd->device->reg_phys_mr)
685 return ERR_PTR(-ENOSYS);
687 mr = pd->device->reg_phys_mr(pd, phys_buf_array, num_phys_buf,
688 mr_access_flags, iova_start);
691 mr->device = pd->device;
694 atomic_inc(&pd->usecnt);
695 atomic_set(&mr->usecnt, 0);
700 EXPORT_SYMBOL(ib_reg_phys_mr);
702 int ib_rereg_phys_mr(struct ib_mr *mr,
705 struct ib_phys_buf *phys_buf_array,
710 struct ib_pd *old_pd;
713 if (!mr->device->rereg_phys_mr)
716 if (atomic_read(&mr->usecnt))
721 ret = mr->device->rereg_phys_mr(mr, mr_rereg_mask, pd,
722 phys_buf_array, num_phys_buf,
723 mr_access_flags, iova_start);
725 if (!ret && (mr_rereg_mask & IB_MR_REREG_PD)) {
726 atomic_dec(&old_pd->usecnt);
727 atomic_inc(&pd->usecnt);
732 EXPORT_SYMBOL(ib_rereg_phys_mr);
734 int ib_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
736 return mr->device->query_mr ?
737 mr->device->query_mr(mr, mr_attr) : -ENOSYS;
739 EXPORT_SYMBOL(ib_query_mr);
741 int ib_dereg_mr(struct ib_mr *mr)
746 if (atomic_read(&mr->usecnt))
750 ret = mr->device->dereg_mr(mr);
752 atomic_dec(&pd->usecnt);
756 EXPORT_SYMBOL(ib_dereg_mr);
760 struct ib_mw *ib_alloc_mw(struct ib_pd *pd)
764 if (!pd->device->alloc_mw)
765 return ERR_PTR(-ENOSYS);
767 mw = pd->device->alloc_mw(pd);
769 mw->device = pd->device;
772 atomic_inc(&pd->usecnt);
777 EXPORT_SYMBOL(ib_alloc_mw);
779 int ib_dealloc_mw(struct ib_mw *mw)
785 ret = mw->device->dealloc_mw(mw);
787 atomic_dec(&pd->usecnt);
791 EXPORT_SYMBOL(ib_dealloc_mw);
793 /* "Fast" memory regions */
795 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
797 struct ib_fmr_attr *fmr_attr)
801 if (!pd->device->alloc_fmr)
802 return ERR_PTR(-ENOSYS);
804 fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
806 fmr->device = pd->device;
808 atomic_inc(&pd->usecnt);
813 EXPORT_SYMBOL(ib_alloc_fmr);
815 int ib_unmap_fmr(struct list_head *fmr_list)
819 if (list_empty(fmr_list))
822 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
823 return fmr->device->unmap_fmr(fmr_list);
825 EXPORT_SYMBOL(ib_unmap_fmr);
827 int ib_dealloc_fmr(struct ib_fmr *fmr)
833 ret = fmr->device->dealloc_fmr(fmr);
835 atomic_dec(&pd->usecnt);
839 EXPORT_SYMBOL(ib_dealloc_fmr);
841 /* Multicast groups */
843 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
845 if (!qp->device->attach_mcast)
847 if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
850 return qp->device->attach_mcast(qp, gid, lid);
852 EXPORT_SYMBOL(ib_attach_mcast);
854 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
856 if (!qp->device->detach_mcast)
858 if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
861 return qp->device->detach_mcast(qp, gid, lid);
863 EXPORT_SYMBOL(ib_detach_mcast);