2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <rdma/ib_mad.h>
35 #include <rdma/ib_user_verbs.h>
36 #include <linux/utsname.h>
38 #include "ipath_kernel.h"
39 #include "ipath_verbs.h"
40 #include "ipath_common.h"
42 /* Not static, because we don't want the compiler removing it */
43 const char ipath_verbs_version[] = "ipath_verbs " IPATH_IDSTR;
45 static unsigned int ib_ipath_qp_table_size = 251;
46 module_param_named(qp_table_size, ib_ipath_qp_table_size, uint, S_IRUGO);
47 MODULE_PARM_DESC(qp_table_size, "QP table size");
49 unsigned int ib_ipath_lkey_table_size = 12;
50 module_param_named(lkey_table_size, ib_ipath_lkey_table_size, uint,
52 MODULE_PARM_DESC(lkey_table_size,
53 "LKEY table size in bits (2^n, 1 <= n <= 23)");
55 unsigned int ib_ipath_debug; /* debug mask */
56 module_param_named(debug, ib_ipath_debug, uint, S_IWUSR | S_IRUGO);
57 MODULE_PARM_DESC(debug, "Verbs debug mask");
59 static unsigned int ib_ipath_max_pds = 0xFFFF;
60 module_param_named(max_pds, ib_ipath_max_pds, uint, S_IWUSR | S_IRUGO);
61 MODULE_PARM_DESC(max_pds,
62 "Maximum number of protection domains to support");
64 static unsigned int ib_ipath_max_ahs = 0xFFFF;
65 module_param_named(max_ahs, ib_ipath_max_ahs, uint, S_IWUSR | S_IRUGO);
66 MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
68 unsigned int ib_ipath_max_cqes = 0x2FFFF;
69 module_param_named(max_cqes, ib_ipath_max_cqes, uint, S_IWUSR | S_IRUGO);
70 MODULE_PARM_DESC(max_cqes,
71 "Maximum number of completion queue entries to support");
73 unsigned int ib_ipath_max_cqs = 0x1FFFF;
74 module_param_named(max_cqs, ib_ipath_max_cqs, uint, S_IWUSR | S_IRUGO);
75 MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
77 unsigned int ib_ipath_max_qp_wrs = 0x3FFF;
78 module_param_named(max_qp_wrs, ib_ipath_max_qp_wrs, uint,
80 MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
82 unsigned int ib_ipath_max_sges = 0x60;
83 module_param_named(max_sges, ib_ipath_max_sges, uint, S_IWUSR | S_IRUGO);
84 MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
86 unsigned int ib_ipath_max_mcast_grps = 16384;
87 module_param_named(max_mcast_grps, ib_ipath_max_mcast_grps, uint,
89 MODULE_PARM_DESC(max_mcast_grps,
90 "Maximum number of multicast groups to support");
92 unsigned int ib_ipath_max_mcast_qp_attached = 16;
93 module_param_named(max_mcast_qp_attached, ib_ipath_max_mcast_qp_attached,
94 uint, S_IWUSR | S_IRUGO);
95 MODULE_PARM_DESC(max_mcast_qp_attached,
96 "Maximum number of attached QPs to support");
98 unsigned int ib_ipath_max_srqs = 1024;
99 module_param_named(max_srqs, ib_ipath_max_srqs, uint, S_IWUSR | S_IRUGO);
100 MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
102 unsigned int ib_ipath_max_srq_sges = 128;
103 module_param_named(max_srq_sges, ib_ipath_max_srq_sges,
104 uint, S_IWUSR | S_IRUGO);
105 MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
107 unsigned int ib_ipath_max_srq_wrs = 0x1FFFF;
108 module_param_named(max_srq_wrs, ib_ipath_max_srq_wrs,
109 uint, S_IWUSR | S_IRUGO);
110 MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
112 MODULE_LICENSE("GPL");
113 MODULE_AUTHOR("QLogic <support@pathscale.com>");
114 MODULE_DESCRIPTION("QLogic InfiniPath driver");
116 const int ib_ipath_state_ops[IB_QPS_ERR + 1] = {
118 [IB_QPS_INIT] = IPATH_POST_RECV_OK,
119 [IB_QPS_RTR] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
120 [IB_QPS_RTS] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
121 IPATH_POST_SEND_OK | IPATH_PROCESS_SEND_OK,
122 [IB_QPS_SQD] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
124 [IB_QPS_SQE] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
129 * Translate ib_wr_opcode into ib_wc_opcode.
131 const enum ib_wc_opcode ib_ipath_wc_opcode[] = {
132 [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
133 [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
134 [IB_WR_SEND] = IB_WC_SEND,
135 [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
136 [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
137 [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
138 [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
144 static __be64 sys_image_guid;
147 * ipath_copy_sge - copy data to SGE memory
149 * @data: the data to copy
150 * @length: the length of the data
152 void ipath_copy_sge(struct ipath_sge_state *ss, void *data, u32 length)
154 struct ipath_sge *sge = &ss->sge;
157 u32 len = sge->length;
162 memcpy(sge->vaddr, data, len);
165 sge->sge_length -= len;
166 if (sge->sge_length == 0) {
168 *sge = *ss->sg_list++;
169 } else if (sge->length == 0 && sge->mr != NULL) {
170 if (++sge->n >= IPATH_SEGSZ) {
171 if (++sge->m >= sge->mr->mapsz)
176 sge->mr->map[sge->m]->segs[sge->n].vaddr;
178 sge->mr->map[sge->m]->segs[sge->n].length;
186 * ipath_skip_sge - skip over SGE memory - XXX almost dup of prev func
188 * @length: the number of bytes to skip
190 void ipath_skip_sge(struct ipath_sge_state *ss, u32 length)
192 struct ipath_sge *sge = &ss->sge;
195 u32 len = sge->length;
202 sge->sge_length -= len;
203 if (sge->sge_length == 0) {
205 *sge = *ss->sg_list++;
206 } else if (sge->length == 0 && sge->mr != NULL) {
207 if (++sge->n >= IPATH_SEGSZ) {
208 if (++sge->m >= sge->mr->mapsz)
213 sge->mr->map[sge->m]->segs[sge->n].vaddr;
215 sge->mr->map[sge->m]->segs[sge->n].length;
222 * ipath_post_send - post a send on a QP
223 * @ibqp: the QP to post the send on
224 * @wr: the list of work requests to post
225 * @bad_wr: the first bad WR is put here
227 * This may be called from interrupt context.
229 static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
230 struct ib_send_wr **bad_wr)
232 struct ipath_qp *qp = to_iqp(ibqp);
235 /* Check that state is OK to post send. */
236 if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK)) {
242 for (; wr; wr = wr->next) {
243 switch (qp->ibqp.qp_type) {
246 err = ipath_post_ruc_send(qp, wr);
252 err = ipath_post_ud_send(qp, wr);
269 * ipath_post_receive - post a receive on a QP
270 * @ibqp: the QP to post the receive on
271 * @wr: the WR to post
272 * @bad_wr: the first bad WR is put here
274 * This may be called from interrupt context.
276 static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
277 struct ib_recv_wr **bad_wr)
279 struct ipath_qp *qp = to_iqp(ibqp);
283 /* Check that state is OK to post receive. */
284 if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK)) {
290 for (; wr; wr = wr->next) {
291 struct ipath_rwqe *wqe;
295 if (wr->num_sge > qp->r_rq.max_sge) {
301 spin_lock_irqsave(&qp->r_rq.lock, flags);
302 next = qp->r_rq.head + 1;
303 if (next >= qp->r_rq.size)
305 if (next == qp->r_rq.tail) {
306 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
312 wqe = get_rwqe_ptr(&qp->r_rq, qp->r_rq.head);
313 wqe->wr_id = wr->wr_id;
314 wqe->sg_list[0].mr = NULL;
315 wqe->sg_list[0].vaddr = NULL;
316 wqe->sg_list[0].length = 0;
317 wqe->sg_list[0].sge_length = 0;
319 for (i = 0, j = 0; i < wr->num_sge; i++) {
321 if (to_ipd(qp->ibqp.pd)->user &&
322 wr->sg_list[i].lkey == 0) {
323 spin_unlock_irqrestore(&qp->r_rq.lock,
329 if (wr->sg_list[i].length == 0)
332 &to_idev(qp->ibqp.device)->lk_table,
333 &wqe->sg_list[j], &wr->sg_list[i],
334 IB_ACCESS_LOCAL_WRITE)) {
335 spin_unlock_irqrestore(&qp->r_rq.lock,
341 wqe->length += wr->sg_list[i].length;
345 qp->r_rq.head = next;
346 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
355 * ipath_qp_rcv - processing an incoming packet on a QP
356 * @dev: the device the packet came on
357 * @hdr: the packet header
358 * @has_grh: true if the packet has a GRH
359 * @data: the packet data
360 * @tlen: the packet length
361 * @qp: the QP the packet came on
363 * This is called from ipath_ib_rcv() to process an incoming packet
365 * Called at interrupt level.
367 static void ipath_qp_rcv(struct ipath_ibdev *dev,
368 struct ipath_ib_header *hdr, int has_grh,
369 void *data, u32 tlen, struct ipath_qp *qp)
371 /* Check for valid receive state. */
372 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
377 switch (qp->ibqp.qp_type) {
381 ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
385 ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
389 ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
398 * ipath_ib_rcv - process and incoming packet
399 * @arg: the device pointer
400 * @rhdr: the header of the packet
401 * @data: the packet data
402 * @tlen: the packet length
404 * This is called from ipath_kreceive() to process an incoming packet at
405 * interrupt level. Tlen is the length of the header + data + CRC in bytes.
407 static void ipath_ib_rcv(void *arg, void *rhdr, void *data, u32 tlen)
409 struct ipath_ibdev *dev = (struct ipath_ibdev *) arg;
410 struct ipath_ib_header *hdr = rhdr;
411 struct ipath_other_headers *ohdr;
418 if (unlikely(dev == NULL))
421 if (unlikely(tlen < 24)) { /* LRH+BTH+CRC */
426 /* Check for a valid destination LID (see ch. 7.11.1). */
427 lid = be16_to_cpu(hdr->lrh[1]);
428 if (lid < IPATH_MULTICAST_LID_BASE) {
429 lid &= ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
430 if (unlikely(lid != ipath_layer_get_lid(dev->dd))) {
437 lnh = be16_to_cpu(hdr->lrh[0]) & 3;
438 if (lnh == IPATH_LRH_BTH)
440 else if (lnh == IPATH_LRH_GRH)
441 ohdr = &hdr->u.l.oth;
447 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
448 dev->opstats[opcode].n_bytes += tlen;
449 dev->opstats[opcode].n_packets++;
451 /* Get the destination QP number. */
452 qp_num = be32_to_cpu(ohdr->bth[1]) & IPATH_QPN_MASK;
453 if (qp_num == IPATH_MULTICAST_QPN) {
454 struct ipath_mcast *mcast;
455 struct ipath_mcast_qp *p;
457 mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
462 dev->n_multicast_rcv++;
463 list_for_each_entry_rcu(p, &mcast->qp_list, list)
464 ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
467 * Notify ipath_multicast_detach() if it is waiting for us
470 if (atomic_dec_return(&mcast->refcount) <= 1)
471 wake_up(&mcast->wait);
473 qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
475 dev->n_unicast_rcv++;
476 ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
479 * Notify ipath_destroy_qp() if it is waiting
482 if (atomic_dec_and_test(&qp->refcount))
492 * ipath_ib_timer - verbs timer
493 * @arg: the device pointer
495 * This is called from ipath_do_rcv_timer() at interrupt level to check for
496 * QPs which need retransmits and to collect performance numbers.
498 static void ipath_ib_timer(void *arg)
500 struct ipath_ibdev *dev = (struct ipath_ibdev *) arg;
501 struct ipath_qp *resend = NULL;
502 struct list_head *last;
509 spin_lock_irqsave(&dev->pending_lock, flags);
510 /* Start filling the next pending queue. */
511 if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
512 dev->pending_index = 0;
513 /* Save any requests still in the new queue, they have timed out. */
514 last = &dev->pending[dev->pending_index];
515 while (!list_empty(last)) {
516 qp = list_entry(last->next, struct ipath_qp, timerwait);
517 list_del_init(&qp->timerwait);
518 qp->timer_next = resend;
520 atomic_inc(&qp->refcount);
522 last = &dev->rnrwait;
523 if (!list_empty(last)) {
524 qp = list_entry(last->next, struct ipath_qp, timerwait);
525 if (--qp->s_rnr_timeout == 0) {
527 list_del_init(&qp->timerwait);
528 tasklet_hi_schedule(&qp->s_task);
529 if (list_empty(last))
531 qp = list_entry(last->next, struct ipath_qp,
533 } while (qp->s_rnr_timeout == 0);
537 * We should only be in the started state if pma_sample_start != 0
539 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
540 --dev->pma_sample_start == 0) {
541 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
542 ipath_layer_snapshot_counters(dev->dd, &dev->ipath_sword,
546 &dev->ipath_xmit_wait);
548 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
549 if (dev->pma_sample_interval == 0) {
550 u64 ta, tb, tc, td, te;
552 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
553 ipath_layer_snapshot_counters(dev->dd, &ta, &tb,
556 dev->ipath_sword = ta - dev->ipath_sword;
557 dev->ipath_rword = tb - dev->ipath_rword;
558 dev->ipath_spkts = tc - dev->ipath_spkts;
559 dev->ipath_rpkts = td - dev->ipath_rpkts;
560 dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
563 dev->pma_sample_interval--;
565 spin_unlock_irqrestore(&dev->pending_lock, flags);
567 /* XXX What if timer fires again while this is running? */
568 for (qp = resend; qp != NULL; qp = qp->timer_next) {
571 spin_lock_irqsave(&qp->s_lock, flags);
572 if (qp->s_last != qp->s_tail && qp->state == IB_QPS_RTS) {
574 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
576 spin_unlock_irqrestore(&qp->s_lock, flags);
578 /* Notify ipath_destroy_qp() if it is waiting. */
579 if (atomic_dec_and_test(&qp->refcount))
585 * ipath_ib_piobufavail - callback when a PIO buffer is available
586 * @arg: the device pointer
588 * This is called from ipath_intr() at interrupt level when a PIO buffer is
589 * available after ipath_verbs_send() returned an error that no buffers were
590 * available. Return 1 if we consumed all the PIO buffers and we still have
591 * QPs waiting for buffers (for now, just do a tasklet_hi_schedule and
594 static int ipath_ib_piobufavail(void *arg)
596 struct ipath_ibdev *dev = (struct ipath_ibdev *) arg;
603 spin_lock_irqsave(&dev->pending_lock, flags);
604 while (!list_empty(&dev->piowait)) {
605 qp = list_entry(dev->piowait.next, struct ipath_qp,
607 list_del_init(&qp->piowait);
608 tasklet_hi_schedule(&qp->s_task);
610 spin_unlock_irqrestore(&dev->pending_lock, flags);
616 static int ipath_query_device(struct ib_device *ibdev,
617 struct ib_device_attr *props)
619 struct ipath_ibdev *dev = to_idev(ibdev);
621 memset(props, 0, sizeof(*props));
623 props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
624 IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
625 IB_DEVICE_SYS_IMAGE_GUID;
626 props->page_size_cap = PAGE_SIZE;
627 props->vendor_id = ipath_layer_get_vendorid(dev->dd);
628 props->vendor_part_id = ipath_layer_get_deviceid(dev->dd);
629 props->hw_ver = ipath_layer_get_pcirev(dev->dd);
631 props->sys_image_guid = dev->sys_image_guid;
633 props->max_mr_size = ~0ull;
634 props->max_qp = dev->qp_table.max;
635 props->max_qp_wr = ib_ipath_max_qp_wrs;
636 props->max_sge = ib_ipath_max_sges;
637 props->max_cq = ib_ipath_max_cqs;
638 props->max_ah = ib_ipath_max_ahs;
639 props->max_cqe = ib_ipath_max_cqes;
640 props->max_mr = dev->lk_table.max;
641 props->max_pd = ib_ipath_max_pds;
642 props->max_qp_rd_atom = 1;
643 props->max_qp_init_rd_atom = 1;
644 /* props->max_res_rd_atom */
645 props->max_srq = ib_ipath_max_srqs;
646 props->max_srq_wr = ib_ipath_max_srq_wrs;
647 props->max_srq_sge = ib_ipath_max_srq_sges;
648 /* props->local_ca_ack_delay */
649 props->atomic_cap = IB_ATOMIC_HCA;
650 props->max_pkeys = ipath_layer_get_npkeys(dev->dd);
651 props->max_mcast_grp = ib_ipath_max_mcast_grps;
652 props->max_mcast_qp_attach = ib_ipath_max_mcast_qp_attached;
653 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
654 props->max_mcast_grp;
659 const u8 ipath_cvt_physportstate[16] = {
660 [INFINIPATH_IBCS_LT_STATE_DISABLED] = 3,
661 [INFINIPATH_IBCS_LT_STATE_LINKUP] = 5,
662 [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = 2,
663 [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = 2,
664 [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = 1,
665 [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = 1,
666 [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] = 4,
667 [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] = 4,
668 [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] = 4,
669 [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = 4,
670 [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] = 6,
671 [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] = 6,
672 [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] = 6,
675 static int ipath_query_port(struct ib_device *ibdev,
676 u8 port, struct ib_port_attr *props)
678 struct ipath_ibdev *dev = to_idev(ibdev);
680 u16 lid = ipath_layer_get_lid(dev->dd);
683 memset(props, 0, sizeof(*props));
684 props->lid = lid ? lid : __constant_be16_to_cpu(IB_LID_PERMISSIVE);
685 props->lmc = dev->mkeyprot_resv_lmc & 7;
686 props->sm_lid = dev->sm_lid;
687 props->sm_sl = dev->sm_sl;
688 ibcstat = ipath_layer_get_lastibcstat(dev->dd);
689 props->state = ((ibcstat >> 4) & 0x3) + 1;
690 /* See phys_state_show() */
691 props->phys_state = ipath_cvt_physportstate[
692 ipath_layer_get_lastibcstat(dev->dd) & 0xf];
693 props->port_cap_flags = dev->port_cap_flags;
694 props->gid_tbl_len = 1;
695 props->max_msg_sz = 0x80000000;
696 props->pkey_tbl_len = ipath_layer_get_npkeys(dev->dd);
697 props->bad_pkey_cntr = ipath_layer_get_cr_errpkey(dev->dd) -
698 dev->z_pkey_violations;
699 props->qkey_viol_cntr = dev->qkey_violations;
700 props->active_width = IB_WIDTH_4X;
701 /* See rate_show() */
702 props->active_speed = 1; /* Regular 10Mbs speed. */
703 props->max_vl_num = 1; /* VLCap = VL0 */
704 props->init_type_reply = 0;
706 props->max_mtu = IB_MTU_4096;
707 switch (ipath_layer_get_ibmtu(dev->dd)) {
726 props->active_mtu = mtu;
727 props->subnet_timeout = dev->subnet_timeout;
732 static int ipath_modify_device(struct ib_device *device,
733 int device_modify_mask,
734 struct ib_device_modify *device_modify)
738 if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
739 IB_DEVICE_MODIFY_NODE_DESC)) {
744 if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
745 memcpy(device->node_desc, device_modify->node_desc, 64);
747 if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
748 to_idev(device)->sys_image_guid =
749 cpu_to_be64(device_modify->sys_image_guid);
757 static int ipath_modify_port(struct ib_device *ibdev,
758 u8 port, int port_modify_mask,
759 struct ib_port_modify *props)
761 struct ipath_ibdev *dev = to_idev(ibdev);
763 dev->port_cap_flags |= props->set_port_cap_mask;
764 dev->port_cap_flags &= ~props->clr_port_cap_mask;
765 if (port_modify_mask & IB_PORT_SHUTDOWN)
766 ipath_layer_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
767 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
768 dev->qkey_violations = 0;
772 static int ipath_query_gid(struct ib_device *ibdev, u8 port,
773 int index, union ib_gid *gid)
775 struct ipath_ibdev *dev = to_idev(ibdev);
782 gid->global.subnet_prefix = dev->gid_prefix;
783 gid->global.interface_id = ipath_layer_get_guid(dev->dd);
791 static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
792 struct ib_ucontext *context,
793 struct ib_udata *udata)
795 struct ipath_ibdev *dev = to_idev(ibdev);
800 * This is actually totally arbitrary. Some correctness tests
801 * assume there's a maximum number of PDs that can be allocated.
802 * We don't actually have this limit, but we fail the test if
803 * we allow allocations of more than we report for this value.
806 if (dev->n_pds_allocated == ib_ipath_max_pds) {
807 ret = ERR_PTR(-ENOMEM);
811 pd = kmalloc(sizeof *pd, GFP_KERNEL);
813 ret = ERR_PTR(-ENOMEM);
817 dev->n_pds_allocated++;
819 /* ib_alloc_pd() will initialize pd->ibpd. */
820 pd->user = udata != NULL;
828 static int ipath_dealloc_pd(struct ib_pd *ibpd)
830 struct ipath_pd *pd = to_ipd(ibpd);
831 struct ipath_ibdev *dev = to_idev(ibpd->device);
833 dev->n_pds_allocated--;
841 * ipath_create_ah - create an address handle
842 * @pd: the protection domain
843 * @ah_attr: the attributes of the AH
845 * This may be called from interrupt context.
847 static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
848 struct ib_ah_attr *ah_attr)
852 struct ipath_ibdev *dev = to_idev(pd->device);
854 if (dev->n_ahs_allocated == ib_ipath_max_ahs) {
855 ret = ERR_PTR(-ENOMEM);
859 /* A multicast address requires a GRH (see ch. 8.4.1). */
860 if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
861 ah_attr->dlid != IPATH_PERMISSIVE_LID &&
862 !(ah_attr->ah_flags & IB_AH_GRH)) {
863 ret = ERR_PTR(-EINVAL);
867 if (ah_attr->dlid == 0) {
868 ret = ERR_PTR(-EINVAL);
872 if (ah_attr->port_num < 1 ||
873 ah_attr->port_num > pd->device->phys_port_cnt) {
874 ret = ERR_PTR(-EINVAL);
878 ah = kmalloc(sizeof *ah, GFP_ATOMIC);
880 ret = ERR_PTR(-ENOMEM);
884 dev->n_ahs_allocated++;
886 /* ib_create_ah() will initialize ah->ibah. */
896 * ipath_destroy_ah - destroy an address handle
897 * @ibah: the AH to destroy
899 * This may be called from interrupt context.
901 static int ipath_destroy_ah(struct ib_ah *ibah)
903 struct ipath_ibdev *dev = to_idev(ibah->device);
904 struct ipath_ah *ah = to_iah(ibah);
906 dev->n_ahs_allocated--;
913 static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
915 struct ipath_ah *ah = to_iah(ibah);
922 static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
925 struct ipath_ibdev *dev = to_idev(ibdev);
928 if (index >= ipath_layer_get_npkeys(dev->dd)) {
933 *pkey = ipath_layer_get_pkey(dev->dd, index);
942 * ipath_alloc_ucontext - allocate a ucontest
943 * @ibdev: the infiniband device
944 * @udata: not used by the InfiniPath driver
947 static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
948 struct ib_udata *udata)
950 struct ipath_ucontext *context;
951 struct ib_ucontext *ret;
953 context = kmalloc(sizeof *context, GFP_KERNEL);
955 ret = ERR_PTR(-ENOMEM);
959 ret = &context->ibucontext;
965 static int ipath_dealloc_ucontext(struct ib_ucontext *context)
967 kfree(to_iucontext(context));
971 static int ipath_verbs_register_sysfs(struct ib_device *dev);
974 * ipath_register_ib_device - register our device with the infiniband core
975 * @unit: the device number to register
976 * @dd: the device data structure
977 * Return the allocated ipath_ibdev pointer or NULL on error.
979 static void *ipath_register_ib_device(int unit, struct ipath_devdata *dd)
981 struct ipath_layer_counters cntrs;
982 struct ipath_ibdev *idev;
983 struct ib_device *dev;
986 idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
992 /* Only need to initialize non-zero fields. */
993 spin_lock_init(&idev->qp_table.lock);
994 spin_lock_init(&idev->lk_table.lock);
995 idev->sm_lid = __constant_be16_to_cpu(IB_LID_PERMISSIVE);
996 /* Set the prefix to the default value (see ch. 4.1.1) */
997 idev->gid_prefix = __constant_cpu_to_be64(0xfe80000000000000ULL);
999 ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
1004 * The top ib_ipath_lkey_table_size bits are used to index the
1005 * table. The lower 8 bits can be owned by the user (copied from
1006 * the LKEY). The remaining bits act as a generation number or tag.
1008 idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
1009 idev->lk_table.table = kzalloc(idev->lk_table.max *
1010 sizeof(*idev->lk_table.table),
1012 if (idev->lk_table.table == NULL) {
1016 spin_lock_init(&idev->pending_lock);
1017 INIT_LIST_HEAD(&idev->pending[0]);
1018 INIT_LIST_HEAD(&idev->pending[1]);
1019 INIT_LIST_HEAD(&idev->pending[2]);
1020 INIT_LIST_HEAD(&idev->piowait);
1021 INIT_LIST_HEAD(&idev->rnrwait);
1022 idev->pending_index = 0;
1023 idev->port_cap_flags =
1024 IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
1025 idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1026 idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1027 idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1028 idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
1029 idev->pma_counter_select[5] = IB_PMA_PORT_XMIT_WAIT;
1030 idev->link_width_enabled = 3; /* 1x or 4x */
1032 /* Snapshot current HW counters to "clear" them. */
1033 ipath_layer_get_counters(dd, &cntrs);
1034 idev->z_symbol_error_counter = cntrs.symbol_error_counter;
1035 idev->z_link_error_recovery_counter =
1036 cntrs.link_error_recovery_counter;
1037 idev->z_link_downed_counter = cntrs.link_downed_counter;
1038 idev->z_port_rcv_errors = cntrs.port_rcv_errors;
1039 idev->z_port_rcv_remphys_errors =
1040 cntrs.port_rcv_remphys_errors;
1041 idev->z_port_xmit_discards = cntrs.port_xmit_discards;
1042 idev->z_port_xmit_data = cntrs.port_xmit_data;
1043 idev->z_port_rcv_data = cntrs.port_rcv_data;
1044 idev->z_port_xmit_packets = cntrs.port_xmit_packets;
1045 idev->z_port_rcv_packets = cntrs.port_rcv_packets;
1046 idev->z_local_link_integrity_errors =
1047 cntrs.local_link_integrity_errors;
1048 idev->z_excessive_buffer_overrun_errors =
1049 cntrs.excessive_buffer_overrun_errors;
1052 * The system image GUID is supposed to be the same for all
1053 * IB HCAs in a single system but since there can be other
1054 * device types in the system, we can't be sure this is unique.
1056 if (!sys_image_guid)
1057 sys_image_guid = ipath_layer_get_guid(dd);
1058 idev->sys_image_guid = sys_image_guid;
1059 idev->ib_unit = unit;
1062 strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
1063 dev->owner = THIS_MODULE;
1064 dev->node_guid = ipath_layer_get_guid(dd);
1065 dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
1066 dev->uverbs_cmd_mask =
1067 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
1068 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
1069 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
1070 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
1071 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
1072 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
1073 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
1074 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
1075 (1ull << IB_USER_VERBS_CMD_REG_MR) |
1076 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
1077 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1078 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
1079 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
1080 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1081 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
1082 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
1083 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1084 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
1085 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
1086 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
1087 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
1088 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
1089 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
1090 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
1091 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
1092 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
1093 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
1094 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
1095 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
1096 dev->node_type = IB_NODE_CA;
1097 dev->phys_port_cnt = 1;
1098 dev->dma_device = ipath_layer_get_device(dd);
1099 dev->class_dev.dev = dev->dma_device;
1100 dev->query_device = ipath_query_device;
1101 dev->modify_device = ipath_modify_device;
1102 dev->query_port = ipath_query_port;
1103 dev->modify_port = ipath_modify_port;
1104 dev->query_pkey = ipath_query_pkey;
1105 dev->query_gid = ipath_query_gid;
1106 dev->alloc_ucontext = ipath_alloc_ucontext;
1107 dev->dealloc_ucontext = ipath_dealloc_ucontext;
1108 dev->alloc_pd = ipath_alloc_pd;
1109 dev->dealloc_pd = ipath_dealloc_pd;
1110 dev->create_ah = ipath_create_ah;
1111 dev->destroy_ah = ipath_destroy_ah;
1112 dev->query_ah = ipath_query_ah;
1113 dev->create_srq = ipath_create_srq;
1114 dev->modify_srq = ipath_modify_srq;
1115 dev->query_srq = ipath_query_srq;
1116 dev->destroy_srq = ipath_destroy_srq;
1117 dev->create_qp = ipath_create_qp;
1118 dev->modify_qp = ipath_modify_qp;
1119 dev->query_qp = ipath_query_qp;
1120 dev->destroy_qp = ipath_destroy_qp;
1121 dev->post_send = ipath_post_send;
1122 dev->post_recv = ipath_post_receive;
1123 dev->post_srq_recv = ipath_post_srq_receive;
1124 dev->create_cq = ipath_create_cq;
1125 dev->destroy_cq = ipath_destroy_cq;
1126 dev->resize_cq = ipath_resize_cq;
1127 dev->poll_cq = ipath_poll_cq;
1128 dev->req_notify_cq = ipath_req_notify_cq;
1129 dev->get_dma_mr = ipath_get_dma_mr;
1130 dev->reg_phys_mr = ipath_reg_phys_mr;
1131 dev->reg_user_mr = ipath_reg_user_mr;
1132 dev->dereg_mr = ipath_dereg_mr;
1133 dev->alloc_fmr = ipath_alloc_fmr;
1134 dev->map_phys_fmr = ipath_map_phys_fmr;
1135 dev->unmap_fmr = ipath_unmap_fmr;
1136 dev->dealloc_fmr = ipath_dealloc_fmr;
1137 dev->attach_mcast = ipath_multicast_attach;
1138 dev->detach_mcast = ipath_multicast_detach;
1139 dev->process_mad = ipath_process_mad;
1141 snprintf(dev->node_desc, sizeof(dev->node_desc),
1142 IPATH_IDSTR " %s kernel_SMA", system_utsname.nodename);
1144 ret = ib_register_device(dev);
1148 if (ipath_verbs_register_sysfs(dev))
1151 ipath_layer_enable_timer(dd);
1156 ib_unregister_device(dev);
1158 kfree(idev->lk_table.table);
1160 kfree(idev->qp_table.table);
1162 ib_dealloc_device(dev);
1163 _VERBS_ERROR("ib_ipath%d cannot register verbs (%d)!\n",
1171 static void ipath_unregister_ib_device(void *arg)
1173 struct ipath_ibdev *dev = (struct ipath_ibdev *) arg;
1174 struct ib_device *ibdev = &dev->ibdev;
1176 ipath_layer_disable_timer(dev->dd);
1178 ib_unregister_device(ibdev);
1180 if (!list_empty(&dev->pending[0]) ||
1181 !list_empty(&dev->pending[1]) ||
1182 !list_empty(&dev->pending[2]))
1183 _VERBS_ERROR("ipath%d pending list not empty!\n",
1185 if (!list_empty(&dev->piowait))
1186 _VERBS_ERROR("ipath%d piowait list not empty!\n",
1188 if (!list_empty(&dev->rnrwait))
1189 _VERBS_ERROR("ipath%d rnrwait list not empty!\n",
1191 if (!ipath_mcast_tree_empty())
1192 _VERBS_ERROR("ipath%d multicast table memory leak!\n",
1195 * Note that ipath_unregister_ib_device() can be called before all
1196 * the QPs are destroyed!
1198 ipath_free_all_qps(&dev->qp_table);
1199 kfree(dev->qp_table.table);
1200 kfree(dev->lk_table.table);
1201 ib_dealloc_device(ibdev);
1204 static int __init ipath_verbs_init(void)
1206 return ipath_verbs_register(ipath_register_ib_device,
1207 ipath_unregister_ib_device,
1208 ipath_ib_piobufavail, ipath_ib_rcv,
1212 static void __exit ipath_verbs_cleanup(void)
1214 ipath_verbs_unregister();
1217 static ssize_t show_rev(struct class_device *cdev, char *buf)
1219 struct ipath_ibdev *dev =
1220 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1222 return sprintf(buf, "%x\n", ipath_layer_get_pcirev(dev->dd));
1225 static ssize_t show_hca(struct class_device *cdev, char *buf)
1227 struct ipath_ibdev *dev =
1228 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1231 ret = ipath_layer_get_boardname(dev->dd, buf, 128);
1241 static ssize_t show_stats(struct class_device *cdev, char *buf)
1243 struct ipath_ibdev *dev =
1244 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1262 dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
1263 dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
1264 dev->n_other_naks, dev->n_timeouts,
1265 dev->n_rdma_dup_busy, dev->n_piowait,
1266 dev->n_no_piobuf, dev->n_pkt_drops, dev->n_wqe_errs);
1267 for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
1268 const struct ipath_opcode_stats *si = &dev->opstats[i];
1270 if (!si->n_packets && !si->n_bytes)
1272 len += sprintf(buf + len, "%02x %llu/%llu\n", i,
1273 (unsigned long long) si->n_packets,
1274 (unsigned long long) si->n_bytes);
1279 static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1280 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
1281 static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
1282 static CLASS_DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
1284 static struct class_device_attribute *ipath_class_attributes[] = {
1285 &class_device_attr_hw_rev,
1286 &class_device_attr_hca_type,
1287 &class_device_attr_board_id,
1288 &class_device_attr_stats
1291 static int ipath_verbs_register_sysfs(struct ib_device *dev)
1296 for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
1297 if (class_device_create_file(&dev->class_dev,
1298 ipath_class_attributes[i])) {
1309 module_init(ipath_verbs_init);
1310 module_exit(ipath_verbs_cleanup);