2 * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
4 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/err.h>
36 #include <linux/vmalloc.h>
37 #include <linux/jhash.h>
41 #define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)
42 #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
44 static inline unsigned mk_qpn(struct qib_qpn_table
*qpt
,
45 struct qpn_map
*map
, unsigned off
)
47 return (map
- qpt
->map
) * BITS_PER_PAGE
+ off
;
50 static inline unsigned find_next_offset(struct qib_qpn_table
*qpt
,
51 struct qpn_map
*map
, unsigned off
,
56 if (((off
& qpt
->mask
) >> 1) >= n
)
57 off
= (off
| qpt
->mask
) + 2;
59 off
= find_next_zero_bit(map
->page
, BITS_PER_PAGE
, off
);
64 * Convert the AETH credit code into the number of credits.
66 static u32 credit_table
[31] = {
100 static void get_map_page(struct qib_qpn_table
*qpt
, struct qpn_map
*map
)
102 unsigned long page
= get_zeroed_page(GFP_KERNEL
);
105 * Free the page if someone raced with us installing it.
108 spin_lock(&qpt
->lock
);
112 map
->page
= (void *)page
;
113 spin_unlock(&qpt
->lock
);
117 * Allocate the next available QPN or
118 * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
120 static int alloc_qpn(struct qib_devdata
*dd
, struct qib_qpn_table
*qpt
,
121 enum ib_qp_type type
, u8 port
)
123 u32 i
, offset
, max_scan
, qpn
;
127 if (type
== IB_QPT_SMI
|| type
== IB_QPT_GSI
) {
130 ret
= type
== IB_QPT_GSI
;
131 n
= 1 << (ret
+ 2 * (port
- 1));
132 spin_lock(&qpt
->lock
);
137 spin_unlock(&qpt
->lock
);
144 if (qpt
->mask
&& ((qpn
& qpt
->mask
) >> 1) >= dd
->n_krcv_queues
)
145 qpn
= (qpn
| qpt
->mask
) + 2;
146 offset
= qpn
& BITS_PER_PAGE_MASK
;
147 map
= &qpt
->map
[qpn
/ BITS_PER_PAGE
];
148 max_scan
= qpt
->nmaps
- !offset
;
150 if (unlikely(!map
->page
)) {
151 get_map_page(qpt
, map
);
152 if (unlikely(!map
->page
))
156 if (!test_and_set_bit(offset
, map
->page
)) {
161 offset
= find_next_offset(qpt
, map
, offset
,
163 qpn
= mk_qpn(qpt
, map
, offset
);
165 * This test differs from alloc_pidmap().
166 * If find_next_offset() does find a zero
167 * bit, we don't need to check for QPN
168 * wrapping around past our starting QPN.
169 * We just need to be sure we don't loop
172 } while (offset
< BITS_PER_PAGE
&& qpn
< QPN_MAX
);
174 * In order to keep the number of pages allocated to a
175 * minimum, we scan the all existing pages before increasing
176 * the size of the bitmap table.
178 if (++i
> max_scan
) {
179 if (qpt
->nmaps
== QPNMAP_ENTRIES
)
181 map
= &qpt
->map
[qpt
->nmaps
++];
183 } else if (map
< &qpt
->map
[qpt
->nmaps
]) {
190 qpn
= mk_qpn(qpt
, map
, offset
);
199 static void free_qpn(struct qib_qpn_table
*qpt
, u32 qpn
)
203 map
= qpt
->map
+ qpn
/ BITS_PER_PAGE
;
205 clear_bit(qpn
& BITS_PER_PAGE_MASK
, map
->page
);
208 static inline unsigned qpn_hash(struct qib_ibdev
*dev
, u32 qpn
)
210 return jhash_1word(qpn
, dev
->qp_rnd
) &
211 (dev
->qp_table_size
- 1);
216 * Put the QP into the hash table.
217 * The hash table holds a reference to the QP.
219 static void insert_qp(struct qib_ibdev
*dev
, struct qib_qp
*qp
)
221 struct qib_ibport
*ibp
= to_iport(qp
->ibqp
.device
, qp
->port_num
);
223 unsigned n
= qpn_hash(dev
, qp
->ibqp
.qp_num
);
225 spin_lock_irqsave(&dev
->qpt_lock
, flags
);
226 atomic_inc(&qp
->refcount
);
228 if (qp
->ibqp
.qp_num
== 0)
229 rcu_assign_pointer(ibp
->qp0
, qp
);
230 else if (qp
->ibqp
.qp_num
== 1)
231 rcu_assign_pointer(ibp
->qp1
, qp
);
233 qp
->next
= dev
->qp_table
[n
];
234 rcu_assign_pointer(dev
->qp_table
[n
], qp
);
237 spin_unlock_irqrestore(&dev
->qpt_lock
, flags
);
242 * Remove the QP from the table so it can't be found asynchronously by
243 * the receive interrupt routine.
245 static void remove_qp(struct qib_ibdev
*dev
, struct qib_qp
*qp
)
247 struct qib_ibport
*ibp
= to_iport(qp
->ibqp
.device
, qp
->port_num
);
248 unsigned n
= qpn_hash(dev
, qp
->ibqp
.qp_num
);
251 spin_lock_irqsave(&dev
->qpt_lock
, flags
);
253 if (ibp
->qp0
== qp
) {
254 atomic_dec(&qp
->refcount
);
255 rcu_assign_pointer(ibp
->qp0
, NULL
);
256 } else if (ibp
->qp1
== qp
) {
257 atomic_dec(&qp
->refcount
);
258 rcu_assign_pointer(ibp
->qp1
, NULL
);
260 struct qib_qp
*q
, **qpp
;
262 qpp
= &dev
->qp_table
[n
];
263 for (; (q
= *qpp
) != NULL
; qpp
= &q
->next
)
265 atomic_dec(&qp
->refcount
);
266 rcu_assign_pointer(*qpp
, qp
->next
);
272 spin_unlock_irqrestore(&dev
->qpt_lock
, flags
);
277 * qib_free_all_qps - check for QPs still in use
278 * @qpt: the QP table to empty
280 * There should not be any QPs still in use.
281 * Free memory for table.
283 unsigned qib_free_all_qps(struct qib_devdata
*dd
)
285 struct qib_ibdev
*dev
= &dd
->verbs_dev
;
288 unsigned n
, qp_inuse
= 0;
290 for (n
= 0; n
< dd
->num_pports
; n
++) {
291 struct qib_ibport
*ibp
= &dd
->pport
[n
].ibport_data
;
293 if (!qib_mcast_tree_empty(ibp
))
296 if (rcu_dereference(ibp
->qp0
))
298 if (rcu_dereference(ibp
->qp1
))
303 spin_lock_irqsave(&dev
->qpt_lock
, flags
);
304 for (n
= 0; n
< dev
->qp_table_size
; n
++) {
305 qp
= dev
->qp_table
[n
];
306 rcu_assign_pointer(dev
->qp_table
[n
], NULL
);
308 for (; qp
; qp
= qp
->next
)
311 spin_unlock_irqrestore(&dev
->qpt_lock
, flags
);
318 * qib_lookup_qpn - return the QP with the given QPN
320 * @qpn: the QP number to look up
322 * The caller is responsible for decrementing the QP reference count
325 struct qib_qp
*qib_lookup_qpn(struct qib_ibport
*ibp
, u32 qpn
)
327 struct qib_qp
*qp
= NULL
;
329 if (unlikely(qpn
<= 1)) {
332 qp
= rcu_dereference(ibp
->qp0
);
334 qp
= rcu_dereference(ibp
->qp1
);
336 struct qib_ibdev
*dev
= &ppd_from_ibp(ibp
)->dd
->verbs_dev
;
337 unsigned n
= qpn_hash(dev
, qpn
);
340 for (qp
= dev
->qp_table
[n
]; rcu_dereference(qp
); qp
= qp
->next
)
341 if (qp
->ibqp
.qp_num
== qpn
)
345 if (unlikely(!atomic_inc_not_zero(&qp
->refcount
)))
353 * qib_reset_qp - initialize the QP state to the reset state
354 * @qp: the QP to reset
357 static void qib_reset_qp(struct qib_qp
*qp
, enum ib_qp_type type
)
361 qp
->qp_access_flags
= 0;
362 atomic_set(&qp
->s_dma_busy
, 0);
363 qp
->s_flags
&= QIB_S_SIGNAL_REQ_WR
;
369 qp
->s_sending_psn
= 0;
370 qp
->s_sending_hpsn
= 0;
374 if (type
== IB_QPT_RC
) {
375 qp
->s_state
= IB_OPCODE_RC_SEND_LAST
;
376 qp
->r_state
= IB_OPCODE_RC_SEND_LAST
;
378 qp
->s_state
= IB_OPCODE_UC_SEND_LAST
;
379 qp
->r_state
= IB_OPCODE_UC_SEND_LAST
;
381 qp
->s_ack_state
= IB_OPCODE_RC_ACKNOWLEDGE
;
392 qp
->s_mig_state
= IB_MIG_MIGRATED
;
393 memset(qp
->s_ack_queue
, 0, sizeof(qp
->s_ack_queue
));
394 qp
->r_head_ack_queue
= 0;
395 qp
->s_tail_ack_queue
= 0;
396 qp
->s_num_rd_atomic
= 0;
398 qp
->r_rq
.wq
->head
= 0;
399 qp
->r_rq
.wq
->tail
= 0;
401 qp
->r_sge
.num_sge
= 0;
404 static void clear_mr_refs(struct qib_qp
*qp
, int clr_sends
)
408 if (test_and_clear_bit(QIB_R_REWIND_SGE
, &qp
->r_aflags
))
409 while (qp
->s_rdma_read_sge
.num_sge
) {
410 atomic_dec(&qp
->s_rdma_read_sge
.sge
.mr
->refcount
);
411 if (--qp
->s_rdma_read_sge
.num_sge
)
412 qp
->s_rdma_read_sge
.sge
=
413 *qp
->s_rdma_read_sge
.sg_list
++;
416 while (qp
->r_sge
.num_sge
) {
417 atomic_dec(&qp
->r_sge
.sge
.mr
->refcount
);
418 if (--qp
->r_sge
.num_sge
)
419 qp
->r_sge
.sge
= *qp
->r_sge
.sg_list
++;
423 while (qp
->s_last
!= qp
->s_head
) {
424 struct qib_swqe
*wqe
= get_swqe_ptr(qp
, qp
->s_last
);
427 for (i
= 0; i
< wqe
->wr
.num_sge
; i
++) {
428 struct qib_sge
*sge
= &wqe
->sg_list
[i
];
430 atomic_dec(&sge
->mr
->refcount
);
432 if (qp
->ibqp
.qp_type
== IB_QPT_UD
||
433 qp
->ibqp
.qp_type
== IB_QPT_SMI
||
434 qp
->ibqp
.qp_type
== IB_QPT_GSI
)
435 atomic_dec(&to_iah(wqe
->wr
.wr
.ud
.ah
)->refcount
);
436 if (++qp
->s_last
>= qp
->s_size
)
440 atomic_dec(&qp
->s_rdma_mr
->refcount
);
441 qp
->s_rdma_mr
= NULL
;
445 if (qp
->ibqp
.qp_type
!= IB_QPT_RC
)
448 for (n
= 0; n
< ARRAY_SIZE(qp
->s_ack_queue
); n
++) {
449 struct qib_ack_entry
*e
= &qp
->s_ack_queue
[n
];
451 if (e
->opcode
== IB_OPCODE_RC_RDMA_READ_REQUEST
&&
453 atomic_dec(&e
->rdma_sge
.mr
->refcount
);
454 e
->rdma_sge
.mr
= NULL
;
460 * qib_error_qp - put a QP into the error state
461 * @qp: the QP to put into the error state
462 * @err: the receive completion error to signal if a RWQE is active
464 * Flushes both send and receive work queues.
465 * Returns true if last WQE event should be generated.
466 * The QP r_lock and s_lock should be held and interrupts disabled.
467 * If we are already in error state, just return.
469 int qib_error_qp(struct qib_qp
*qp
, enum ib_wc_status err
)
471 struct qib_ibdev
*dev
= to_idev(qp
->ibqp
.device
);
475 if (qp
->state
== IB_QPS_ERR
|| qp
->state
== IB_QPS_RESET
)
478 qp
->state
= IB_QPS_ERR
;
480 if (qp
->s_flags
& (QIB_S_TIMER
| QIB_S_WAIT_RNR
)) {
481 qp
->s_flags
&= ~(QIB_S_TIMER
| QIB_S_WAIT_RNR
);
482 del_timer(&qp
->s_timer
);
485 if (qp
->s_flags
& QIB_S_ANY_WAIT_SEND
)
486 qp
->s_flags
&= ~QIB_S_ANY_WAIT_SEND
;
488 spin_lock(&dev
->pending_lock
);
489 if (!list_empty(&qp
->iowait
) && !(qp
->s_flags
& QIB_S_BUSY
)) {
490 qp
->s_flags
&= ~QIB_S_ANY_WAIT_IO
;
491 list_del_init(&qp
->iowait
);
493 spin_unlock(&dev
->pending_lock
);
495 if (!(qp
->s_flags
& QIB_S_BUSY
)) {
498 atomic_dec(&qp
->s_rdma_mr
->refcount
);
499 qp
->s_rdma_mr
= NULL
;
502 qib_put_txreq(qp
->s_tx
);
507 /* Schedule the sending tasklet to drain the send work queue. */
508 if (qp
->s_last
!= qp
->s_head
)
509 qib_schedule_send(qp
);
511 clear_mr_refs(qp
, 0);
513 memset(&wc
, 0, sizeof(wc
));
515 wc
.opcode
= IB_WC_RECV
;
517 if (test_and_clear_bit(QIB_R_WRID_VALID
, &qp
->r_aflags
)) {
518 wc
.wr_id
= qp
->r_wr_id
;
520 qib_cq_enter(to_icq(qp
->ibqp
.recv_cq
), &wc
, 1);
522 wc
.status
= IB_WC_WR_FLUSH_ERR
;
529 spin_lock(&qp
->r_rq
.lock
);
531 /* sanity check pointers before trusting them */
534 if (head
>= qp
->r_rq
.size
)
537 if (tail
>= qp
->r_rq
.size
)
539 while (tail
!= head
) {
540 wc
.wr_id
= get_rwqe_ptr(&qp
->r_rq
, tail
)->wr_id
;
541 if (++tail
>= qp
->r_rq
.size
)
543 qib_cq_enter(to_icq(qp
->ibqp
.recv_cq
), &wc
, 1);
547 spin_unlock(&qp
->r_rq
.lock
);
548 } else if (qp
->ibqp
.event_handler
)
556 * qib_modify_qp - modify the attributes of a queue pair
557 * @ibqp: the queue pair who's attributes we're modifying
558 * @attr: the new attributes
559 * @attr_mask: the mask of attributes to modify
560 * @udata: user data for libibverbs.so
562 * Returns 0 on success, otherwise returns an errno.
564 int qib_modify_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
565 int attr_mask
, struct ib_udata
*udata
)
567 struct qib_ibdev
*dev
= to_idev(ibqp
->device
);
568 struct qib_qp
*qp
= to_iqp(ibqp
);
569 enum ib_qp_state cur_state
, new_state
;
574 u32 pmtu
= 0; /* for gcc warning only */
576 spin_lock_irq(&qp
->r_lock
);
577 spin_lock(&qp
->s_lock
);
579 cur_state
= attr_mask
& IB_QP_CUR_STATE
?
580 attr
->cur_qp_state
: qp
->state
;
581 new_state
= attr_mask
& IB_QP_STATE
? attr
->qp_state
: cur_state
;
583 if (!ib_modify_qp_is_ok(cur_state
, new_state
, ibqp
->qp_type
,
587 if (attr_mask
& IB_QP_AV
) {
588 if (attr
->ah_attr
.dlid
>= QIB_MULTICAST_LID_BASE
)
590 if (qib_check_ah(qp
->ibqp
.device
, &attr
->ah_attr
))
594 if (attr_mask
& IB_QP_ALT_PATH
) {
595 if (attr
->alt_ah_attr
.dlid
>= QIB_MULTICAST_LID_BASE
)
597 if (qib_check_ah(qp
->ibqp
.device
, &attr
->alt_ah_attr
))
599 if (attr
->alt_pkey_index
>= qib_get_npkeys(dd_from_dev(dev
)))
603 if (attr_mask
& IB_QP_PKEY_INDEX
)
604 if (attr
->pkey_index
>= qib_get_npkeys(dd_from_dev(dev
)))
607 if (attr_mask
& IB_QP_MIN_RNR_TIMER
)
608 if (attr
->min_rnr_timer
> 31)
611 if (attr_mask
& IB_QP_PORT
)
612 if (qp
->ibqp
.qp_type
== IB_QPT_SMI
||
613 qp
->ibqp
.qp_type
== IB_QPT_GSI
||
614 attr
->port_num
== 0 ||
615 attr
->port_num
> ibqp
->device
->phys_port_cnt
)
618 if (attr_mask
& IB_QP_DEST_QPN
)
619 if (attr
->dest_qp_num
> QIB_QPN_MASK
)
622 if (attr_mask
& IB_QP_RETRY_CNT
)
623 if (attr
->retry_cnt
> 7)
626 if (attr_mask
& IB_QP_RNR_RETRY
)
627 if (attr
->rnr_retry
> 7)
631 * Don't allow invalid path_mtu values. OK to set greater
632 * than the active mtu (or even the max_cap, if we have tuned
633 * that to a small mtu. We'll set qp->path_mtu
634 * to the lesser of requested attribute mtu and active,
635 * for packetizing messages.
636 * Note that the QP port has to be set in INIT and MTU in RTR.
638 if (attr_mask
& IB_QP_PATH_MTU
) {
639 struct qib_devdata
*dd
= dd_from_dev(dev
);
640 int mtu
, pidx
= qp
->port_num
- 1;
642 mtu
= ib_mtu_enum_to_int(attr
->path_mtu
);
645 if (mtu
> dd
->pport
[pidx
].ibmtu
) {
646 switch (dd
->pport
[pidx
].ibmtu
) {
666 pmtu
= attr
->path_mtu
;
669 if (attr_mask
& IB_QP_PATH_MIG_STATE
) {
670 if (attr
->path_mig_state
== IB_MIG_REARM
) {
671 if (qp
->s_mig_state
== IB_MIG_ARMED
)
673 if (new_state
!= IB_QPS_RTS
)
675 } else if (attr
->path_mig_state
== IB_MIG_MIGRATED
) {
676 if (qp
->s_mig_state
== IB_MIG_REARM
)
678 if (new_state
!= IB_QPS_RTS
&& new_state
!= IB_QPS_SQD
)
680 if (qp
->s_mig_state
== IB_MIG_ARMED
)
686 if (attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
)
687 if (attr
->max_dest_rd_atomic
> QIB_MAX_RDMA_ATOMIC
)
692 if (qp
->state
!= IB_QPS_RESET
) {
693 qp
->state
= IB_QPS_RESET
;
694 spin_lock(&dev
->pending_lock
);
695 if (!list_empty(&qp
->iowait
))
696 list_del_init(&qp
->iowait
);
697 spin_unlock(&dev
->pending_lock
);
698 qp
->s_flags
&= ~(QIB_S_TIMER
| QIB_S_ANY_WAIT
);
699 spin_unlock(&qp
->s_lock
);
700 spin_unlock_irq(&qp
->r_lock
);
701 /* Stop the sending work queue and retry timer */
702 cancel_work_sync(&qp
->s_work
);
703 del_timer_sync(&qp
->s_timer
);
704 wait_event(qp
->wait_dma
, !atomic_read(&qp
->s_dma_busy
));
706 qib_put_txreq(qp
->s_tx
);
710 wait_event(qp
->wait
, !atomic_read(&qp
->refcount
));
711 spin_lock_irq(&qp
->r_lock
);
712 spin_lock(&qp
->s_lock
);
713 clear_mr_refs(qp
, 1);
714 qib_reset_qp(qp
, ibqp
->qp_type
);
719 /* Allow event to retrigger if QP set to RTR more than once */
720 qp
->r_flags
&= ~QIB_R_COMM_EST
;
721 qp
->state
= new_state
;
725 qp
->s_draining
= qp
->s_last
!= qp
->s_cur
;
726 qp
->state
= new_state
;
730 if (qp
->ibqp
.qp_type
== IB_QPT_RC
)
732 qp
->state
= new_state
;
736 lastwqe
= qib_error_qp(qp
, IB_WC_WR_FLUSH_ERR
);
740 qp
->state
= new_state
;
744 if (attr_mask
& IB_QP_PKEY_INDEX
)
745 qp
->s_pkey_index
= attr
->pkey_index
;
747 if (attr_mask
& IB_QP_PORT
)
748 qp
->port_num
= attr
->port_num
;
750 if (attr_mask
& IB_QP_DEST_QPN
)
751 qp
->remote_qpn
= attr
->dest_qp_num
;
753 if (attr_mask
& IB_QP_SQ_PSN
) {
754 qp
->s_next_psn
= attr
->sq_psn
& QIB_PSN_MASK
;
755 qp
->s_psn
= qp
->s_next_psn
;
756 qp
->s_sending_psn
= qp
->s_next_psn
;
757 qp
->s_last_psn
= qp
->s_next_psn
- 1;
758 qp
->s_sending_hpsn
= qp
->s_last_psn
;
761 if (attr_mask
& IB_QP_RQ_PSN
)
762 qp
->r_psn
= attr
->rq_psn
& QIB_PSN_MASK
;
764 if (attr_mask
& IB_QP_ACCESS_FLAGS
)
765 qp
->qp_access_flags
= attr
->qp_access_flags
;
767 if (attr_mask
& IB_QP_AV
) {
768 qp
->remote_ah_attr
= attr
->ah_attr
;
769 qp
->s_srate
= attr
->ah_attr
.static_rate
;
772 if (attr_mask
& IB_QP_ALT_PATH
) {
773 qp
->alt_ah_attr
= attr
->alt_ah_attr
;
774 qp
->s_alt_pkey_index
= attr
->alt_pkey_index
;
777 if (attr_mask
& IB_QP_PATH_MIG_STATE
) {
778 qp
->s_mig_state
= attr
->path_mig_state
;
780 qp
->remote_ah_attr
= qp
->alt_ah_attr
;
781 qp
->port_num
= qp
->alt_ah_attr
.port_num
;
782 qp
->s_pkey_index
= qp
->s_alt_pkey_index
;
786 if (attr_mask
& IB_QP_PATH_MTU
) {
788 qp
->pmtu
= ib_mtu_enum_to_int(pmtu
);
791 if (attr_mask
& IB_QP_RETRY_CNT
) {
792 qp
->s_retry_cnt
= attr
->retry_cnt
;
793 qp
->s_retry
= attr
->retry_cnt
;
796 if (attr_mask
& IB_QP_RNR_RETRY
) {
797 qp
->s_rnr_retry_cnt
= attr
->rnr_retry
;
798 qp
->s_rnr_retry
= attr
->rnr_retry
;
801 if (attr_mask
& IB_QP_MIN_RNR_TIMER
)
802 qp
->r_min_rnr_timer
= attr
->min_rnr_timer
;
804 if (attr_mask
& IB_QP_TIMEOUT
) {
805 qp
->timeout
= attr
->timeout
;
806 qp
->timeout_jiffies
=
807 usecs_to_jiffies((4096UL * (1UL << qp
->timeout
)) /
811 if (attr_mask
& IB_QP_QKEY
)
812 qp
->qkey
= attr
->qkey
;
814 if (attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
)
815 qp
->r_max_rd_atomic
= attr
->max_dest_rd_atomic
;
817 if (attr_mask
& IB_QP_MAX_QP_RD_ATOMIC
)
818 qp
->s_max_rd_atomic
= attr
->max_rd_atomic
;
820 spin_unlock(&qp
->s_lock
);
821 spin_unlock_irq(&qp
->r_lock
);
823 if (cur_state
== IB_QPS_RESET
&& new_state
== IB_QPS_INIT
)
827 ev
.device
= qp
->ibqp
.device
;
828 ev
.element
.qp
= &qp
->ibqp
;
829 ev
.event
= IB_EVENT_QP_LAST_WQE_REACHED
;
830 qp
->ibqp
.event_handler(&ev
, qp
->ibqp
.qp_context
);
833 ev
.device
= qp
->ibqp
.device
;
834 ev
.element
.qp
= &qp
->ibqp
;
835 ev
.event
= IB_EVENT_PATH_MIG
;
836 qp
->ibqp
.event_handler(&ev
, qp
->ibqp
.qp_context
);
842 spin_unlock(&qp
->s_lock
);
843 spin_unlock_irq(&qp
->r_lock
);
850 int qib_query_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
851 int attr_mask
, struct ib_qp_init_attr
*init_attr
)
853 struct qib_qp
*qp
= to_iqp(ibqp
);
855 attr
->qp_state
= qp
->state
;
856 attr
->cur_qp_state
= attr
->qp_state
;
857 attr
->path_mtu
= qp
->path_mtu
;
858 attr
->path_mig_state
= qp
->s_mig_state
;
859 attr
->qkey
= qp
->qkey
;
860 attr
->rq_psn
= qp
->r_psn
& QIB_PSN_MASK
;
861 attr
->sq_psn
= qp
->s_next_psn
& QIB_PSN_MASK
;
862 attr
->dest_qp_num
= qp
->remote_qpn
;
863 attr
->qp_access_flags
= qp
->qp_access_flags
;
864 attr
->cap
.max_send_wr
= qp
->s_size
- 1;
865 attr
->cap
.max_recv_wr
= qp
->ibqp
.srq
? 0 : qp
->r_rq
.size
- 1;
866 attr
->cap
.max_send_sge
= qp
->s_max_sge
;
867 attr
->cap
.max_recv_sge
= qp
->r_rq
.max_sge
;
868 attr
->cap
.max_inline_data
= 0;
869 attr
->ah_attr
= qp
->remote_ah_attr
;
870 attr
->alt_ah_attr
= qp
->alt_ah_attr
;
871 attr
->pkey_index
= qp
->s_pkey_index
;
872 attr
->alt_pkey_index
= qp
->s_alt_pkey_index
;
873 attr
->en_sqd_async_notify
= 0;
874 attr
->sq_draining
= qp
->s_draining
;
875 attr
->max_rd_atomic
= qp
->s_max_rd_atomic
;
876 attr
->max_dest_rd_atomic
= qp
->r_max_rd_atomic
;
877 attr
->min_rnr_timer
= qp
->r_min_rnr_timer
;
878 attr
->port_num
= qp
->port_num
;
879 attr
->timeout
= qp
->timeout
;
880 attr
->retry_cnt
= qp
->s_retry_cnt
;
881 attr
->rnr_retry
= qp
->s_rnr_retry_cnt
;
882 attr
->alt_port_num
= qp
->alt_ah_attr
.port_num
;
883 attr
->alt_timeout
= qp
->alt_timeout
;
885 init_attr
->event_handler
= qp
->ibqp
.event_handler
;
886 init_attr
->qp_context
= qp
->ibqp
.qp_context
;
887 init_attr
->send_cq
= qp
->ibqp
.send_cq
;
888 init_attr
->recv_cq
= qp
->ibqp
.recv_cq
;
889 init_attr
->srq
= qp
->ibqp
.srq
;
890 init_attr
->cap
= attr
->cap
;
891 if (qp
->s_flags
& QIB_S_SIGNAL_REQ_WR
)
892 init_attr
->sq_sig_type
= IB_SIGNAL_REQ_WR
;
894 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
895 init_attr
->qp_type
= qp
->ibqp
.qp_type
;
896 init_attr
->port_num
= qp
->port_num
;
901 * qib_compute_aeth - compute the AETH (syndrome + MSN)
902 * @qp: the queue pair to compute the AETH for
906 __be32
qib_compute_aeth(struct qib_qp
*qp
)
908 u32 aeth
= qp
->r_msn
& QIB_MSN_MASK
;
912 * Shared receive queues don't generate credits.
913 * Set the credit field to the invalid value.
915 aeth
|= QIB_AETH_CREDIT_INVAL
<< QIB_AETH_CREDIT_SHIFT
;
919 struct qib_rwq
*wq
= qp
->r_rq
.wq
;
923 /* sanity check pointers before trusting them */
925 if (head
>= qp
->r_rq
.size
)
928 if (tail
>= qp
->r_rq
.size
)
931 * Compute the number of credits available (RWQEs).
932 * XXX Not holding the r_rq.lock here so there is a small
933 * chance that the pair of reads are not atomic.
935 credits
= head
- tail
;
936 if ((int)credits
< 0)
937 credits
+= qp
->r_rq
.size
;
939 * Binary search the credit table to find the code to
946 if (credit_table
[x
] == credits
)
948 if (credit_table
[x
] > credits
)
955 aeth
|= x
<< QIB_AETH_CREDIT_SHIFT
;
957 return cpu_to_be32(aeth
);
961 * qib_create_qp - create a queue pair for a device
962 * @ibpd: the protection domain who's device we create the queue pair for
963 * @init_attr: the attributes of the queue pair
964 * @udata: user data for libibverbs.so
966 * Returns the queue pair on success, otherwise returns an errno.
968 * Called by the ib_create_qp() core verbs function.
970 struct ib_qp
*qib_create_qp(struct ib_pd
*ibpd
,
971 struct ib_qp_init_attr
*init_attr
,
972 struct ib_udata
*udata
)
976 struct qib_swqe
*swq
= NULL
;
977 struct qib_ibdev
*dev
;
978 struct qib_devdata
*dd
;
983 if (init_attr
->cap
.max_send_sge
> ib_qib_max_sges
||
984 init_attr
->cap
.max_send_wr
> ib_qib_max_qp_wrs
) {
985 ret
= ERR_PTR(-EINVAL
);
989 /* Check receive queue parameters if no SRQ is specified. */
990 if (!init_attr
->srq
) {
991 if (init_attr
->cap
.max_recv_sge
> ib_qib_max_sges
||
992 init_attr
->cap
.max_recv_wr
> ib_qib_max_qp_wrs
) {
993 ret
= ERR_PTR(-EINVAL
);
996 if (init_attr
->cap
.max_send_sge
+
997 init_attr
->cap
.max_send_wr
+
998 init_attr
->cap
.max_recv_sge
+
999 init_attr
->cap
.max_recv_wr
== 0) {
1000 ret
= ERR_PTR(-EINVAL
);
1005 switch (init_attr
->qp_type
) {
1008 if (init_attr
->port_num
== 0 ||
1009 init_attr
->port_num
> ibpd
->device
->phys_port_cnt
) {
1010 ret
= ERR_PTR(-EINVAL
);
1016 sz
= sizeof(struct qib_sge
) *
1017 init_attr
->cap
.max_send_sge
+
1018 sizeof(struct qib_swqe
);
1019 swq
= vmalloc((init_attr
->cap
.max_send_wr
+ 1) * sz
);
1021 ret
= ERR_PTR(-ENOMEM
);
1026 if (init_attr
->srq
) {
1027 struct qib_srq
*srq
= to_isrq(init_attr
->srq
);
1029 if (srq
->rq
.max_sge
> 1)
1030 sg_list_sz
= sizeof(*qp
->r_sg_list
) *
1031 (srq
->rq
.max_sge
- 1);
1032 } else if (init_attr
->cap
.max_recv_sge
> 1)
1033 sg_list_sz
= sizeof(*qp
->r_sg_list
) *
1034 (init_attr
->cap
.max_recv_sge
- 1);
1035 qp
= kzalloc(sz
+ sg_list_sz
, GFP_KERNEL
);
1037 ret
= ERR_PTR(-ENOMEM
);
1040 RCU_INIT_POINTER(qp
->next
, NULL
);
1041 qp
->timeout_jiffies
=
1042 usecs_to_jiffies((4096UL * (1UL << qp
->timeout
)) /
1047 qp
->r_rq
.size
= init_attr
->cap
.max_recv_wr
+ 1;
1048 qp
->r_rq
.max_sge
= init_attr
->cap
.max_recv_sge
;
1049 sz
= (sizeof(struct ib_sge
) * qp
->r_rq
.max_sge
) +
1050 sizeof(struct qib_rwqe
);
1051 qp
->r_rq
.wq
= vmalloc_user(sizeof(struct qib_rwq
) +
1052 qp
->r_rq
.size
* sz
);
1054 ret
= ERR_PTR(-ENOMEM
);
1060 * ib_create_qp() will initialize qp->ibqp
1061 * except for qp->ibqp.qp_num.
1063 spin_lock_init(&qp
->r_lock
);
1064 spin_lock_init(&qp
->s_lock
);
1065 spin_lock_init(&qp
->r_rq
.lock
);
1066 atomic_set(&qp
->refcount
, 0);
1067 init_waitqueue_head(&qp
->wait
);
1068 init_waitqueue_head(&qp
->wait_dma
);
1069 init_timer(&qp
->s_timer
);
1070 qp
->s_timer
.data
= (unsigned long)qp
;
1071 INIT_WORK(&qp
->s_work
, qib_do_send
);
1072 INIT_LIST_HEAD(&qp
->iowait
);
1073 INIT_LIST_HEAD(&qp
->rspwait
);
1074 qp
->state
= IB_QPS_RESET
;
1076 qp
->s_size
= init_attr
->cap
.max_send_wr
+ 1;
1077 qp
->s_max_sge
= init_attr
->cap
.max_send_sge
;
1078 if (init_attr
->sq_sig_type
== IB_SIGNAL_REQ_WR
)
1079 qp
->s_flags
= QIB_S_SIGNAL_REQ_WR
;
1080 dev
= to_idev(ibpd
->device
);
1081 dd
= dd_from_dev(dev
);
1082 err
= alloc_qpn(dd
, &dev
->qpn_table
, init_attr
->qp_type
,
1083 init_attr
->port_num
);
1089 qp
->ibqp
.qp_num
= err
;
1090 qp
->port_num
= init_attr
->port_num
;
1091 qib_reset_qp(qp
, init_attr
->qp_type
);
1095 /* Don't support raw QPs */
1096 ret
= ERR_PTR(-ENOSYS
);
1100 init_attr
->cap
.max_inline_data
= 0;
1103 * Return the address of the RWQ as the offset to mmap.
1104 * See qib_mmap() for details.
1106 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
1110 err
= ib_copy_to_udata(udata
, &offset
,
1117 u32 s
= sizeof(struct qib_rwq
) + qp
->r_rq
.size
* sz
;
1119 qp
->ip
= qib_create_mmap_info(dev
, s
,
1120 ibpd
->uobject
->context
,
1123 ret
= ERR_PTR(-ENOMEM
);
1127 err
= ib_copy_to_udata(udata
, &(qp
->ip
->offset
),
1128 sizeof(qp
->ip
->offset
));
1136 spin_lock(&dev
->n_qps_lock
);
1137 if (dev
->n_qps_allocated
== ib_qib_max_qps
) {
1138 spin_unlock(&dev
->n_qps_lock
);
1139 ret
= ERR_PTR(-ENOMEM
);
1143 dev
->n_qps_allocated
++;
1144 spin_unlock(&dev
->n_qps_lock
);
1147 spin_lock_irq(&dev
->pending_lock
);
1148 list_add(&qp
->ip
->pending_mmaps
, &dev
->pending_mmaps
);
1149 spin_unlock_irq(&dev
->pending_lock
);
1157 kref_put(&qp
->ip
->ref
, qib_release_mmap_info
);
1160 free_qpn(&dev
->qpn_table
, qp
->ibqp
.qp_num
);
1170 * qib_destroy_qp - destroy a queue pair
1171 * @ibqp: the queue pair to destroy
1173 * Returns 0 on success.
1175 * Note that this can be called while the QP is actively sending or
1178 int qib_destroy_qp(struct ib_qp
*ibqp
)
1180 struct qib_qp
*qp
= to_iqp(ibqp
);
1181 struct qib_ibdev
*dev
= to_idev(ibqp
->device
);
1183 /* Make sure HW and driver activity is stopped. */
1184 spin_lock_irq(&qp
->s_lock
);
1185 if (qp
->state
!= IB_QPS_RESET
) {
1186 qp
->state
= IB_QPS_RESET
;
1187 spin_lock(&dev
->pending_lock
);
1188 if (!list_empty(&qp
->iowait
))
1189 list_del_init(&qp
->iowait
);
1190 spin_unlock(&dev
->pending_lock
);
1191 qp
->s_flags
&= ~(QIB_S_TIMER
| QIB_S_ANY_WAIT
);
1192 spin_unlock_irq(&qp
->s_lock
);
1193 cancel_work_sync(&qp
->s_work
);
1194 del_timer_sync(&qp
->s_timer
);
1195 wait_event(qp
->wait_dma
, !atomic_read(&qp
->s_dma_busy
));
1197 qib_put_txreq(qp
->s_tx
);
1201 wait_event(qp
->wait
, !atomic_read(&qp
->refcount
));
1202 clear_mr_refs(qp
, 1);
1204 spin_unlock_irq(&qp
->s_lock
);
1206 /* all user's cleaned up, mark it available */
1207 free_qpn(&dev
->qpn_table
, qp
->ibqp
.qp_num
);
1208 spin_lock(&dev
->n_qps_lock
);
1209 dev
->n_qps_allocated
--;
1210 spin_unlock(&dev
->n_qps_lock
);
1213 kref_put(&qp
->ip
->ref
, qib_release_mmap_info
);
1222 * qib_init_qpn_table - initialize the QP number table for a device
1223 * @qpt: the QPN table
1225 void qib_init_qpn_table(struct qib_devdata
*dd
, struct qib_qpn_table
*qpt
)
1227 spin_lock_init(&qpt
->lock
);
1228 qpt
->last
= 1; /* start with QPN 2 */
1230 qpt
->mask
= dd
->qpn_mask
;
1234 * qib_free_qpn_table - free the QP number table for a device
1235 * @qpt: the QPN table
1237 void qib_free_qpn_table(struct qib_qpn_table
*qpt
)
1241 for (i
= 0; i
< ARRAY_SIZE(qpt
->map
); i
++)
1242 if (qpt
->map
[i
].page
)
1243 free_page((unsigned long) qpt
->map
[i
].page
);
1247 * qib_get_credit - flush the send work queue of a QP
1248 * @qp: the qp who's send work queue to flush
1249 * @aeth: the Acknowledge Extended Transport Header
1251 * The QP s_lock should be held.
1253 void qib_get_credit(struct qib_qp
*qp
, u32 aeth
)
1255 u32 credit
= (aeth
>> QIB_AETH_CREDIT_SHIFT
) & QIB_AETH_CREDIT_MASK
;
1258 * If the credit is invalid, we can send
1259 * as many packets as we like. Otherwise, we have to
1260 * honor the credit field.
1262 if (credit
== QIB_AETH_CREDIT_INVAL
) {
1263 if (!(qp
->s_flags
& QIB_S_UNLIMITED_CREDIT
)) {
1264 qp
->s_flags
|= QIB_S_UNLIMITED_CREDIT
;
1265 if (qp
->s_flags
& QIB_S_WAIT_SSN_CREDIT
) {
1266 qp
->s_flags
&= ~QIB_S_WAIT_SSN_CREDIT
;
1267 qib_schedule_send(qp
);
1270 } else if (!(qp
->s_flags
& QIB_S_UNLIMITED_CREDIT
)) {
1271 /* Compute new LSN (i.e., MSN + credit) */
1272 credit
= (aeth
+ credit_table
[credit
]) & QIB_MSN_MASK
;
1273 if (qib_cmp24(credit
, qp
->s_lsn
) > 0) {
1275 if (qp
->s_flags
& QIB_S_WAIT_SSN_CREDIT
) {
1276 qp
->s_flags
&= ~QIB_S_WAIT_SSN_CREDIT
;
1277 qib_schedule_send(qp
);