2 * Copyright(c) 2016, 2017 Intel Corporation.
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <linux/hash.h>
49 #include <linux/bitops.h>
50 #include <linux/lockdep.h>
51 #include <linux/vmalloc.h>
52 #include <linux/slab.h>
53 #include <rdma/ib_verbs.h>
54 #include <rdma/ib_hdrs.h>
55 #include <rdma/opa_addr.h>
60 static void rvt_rc_timeout(struct timer_list *t);
63 * Convert the AETH RNR timeout code into the number of microseconds.
65 static const u32 ib_rvt_rnr_table[32] = {
66 655360, /* 00: 655.36 */
86 10240, /* 14: 10.24 */
87 15360, /* 15: 15.36 */
88 20480, /* 16: 20.48 */
89 30720, /* 17: 30.72 */
90 40960, /* 18: 40.96 */
91 61440, /* 19: 61.44 */
92 81920, /* 1A: 81.92 */
93 122880, /* 1B: 122.88 */
94 163840, /* 1C: 163.84 */
95 245760, /* 1D: 245.76 */
96 327680, /* 1E: 327.68 */
97 491520 /* 1F: 491.52 */
101 * Note that it is OK to post send work requests in the SQE and ERR
102 * states; rvt_do_send() will process them and generate error
103 * completions as per IB 1.2 C10-96.
105 const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
107 [IB_QPS_INIT] = RVT_POST_RECV_OK,
108 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
109 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
110 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
111 RVT_PROCESS_NEXT_SEND_OK,
112 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
113 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
114 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
115 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
116 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
117 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
119 EXPORT_SYMBOL(ib_rvt_state_ops);
121 static void get_map_page(struct rvt_qpn_table *qpt,
122 struct rvt_qpn_map *map)
124 unsigned long page = get_zeroed_page(GFP_KERNEL);
127 * Free the page if someone raced with us installing it.
130 spin_lock(&qpt->lock);
134 map->page = (void *)page;
135 spin_unlock(&qpt->lock);
139 * init_qpn_table - initialize the QP number table for a device
140 * @qpt: the QPN table
142 static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
145 struct rvt_qpn_map *map;
148 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
151 spin_lock_init(&qpt->lock);
153 qpt->last = rdi->dparms.qpn_start;
154 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
157 * Drivers may want some QPs beyond what we need for verbs let them use
158 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
159 * for those. The reserved range must be *after* the range which verbs
163 /* Figure out number of bit maps needed before reserved range */
164 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
166 /* This should always be zero */
167 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
169 /* Starting with the first reserved bit map */
170 map = &qpt->map[qpt->nmaps];
172 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
173 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
174 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
176 get_map_page(qpt, map);
182 set_bit(offset, map->page);
184 if (offset == RVT_BITS_PER_PAGE) {
195 * free_qpn_table - free the QP number table for a device
196 * @qpt: the QPN table
198 static void free_qpn_table(struct rvt_qpn_table *qpt)
202 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
203 free_page((unsigned long)qpt->map[i].page);
207 * rvt_driver_qp_init - Init driver qp resources
208 * @rdi: rvt dev strucutre
210 * Return: 0 on success
212 int rvt_driver_qp_init(struct rvt_dev_info *rdi)
217 if (!rdi->dparms.qp_table_size)
221 * If driver is not doing any QP allocation then make sure it is
222 * providing the necessary QP functions.
224 if (!rdi->driver_f.free_all_qps ||
225 !rdi->driver_f.qp_priv_alloc ||
226 !rdi->driver_f.qp_priv_free ||
227 !rdi->driver_f.notify_qp_reset ||
228 !rdi->driver_f.notify_restart_rc)
231 /* allocate parent object */
232 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
237 /* allocate hash table */
238 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
239 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
240 rdi->qp_dev->qp_table =
241 kmalloc_array_node(rdi->qp_dev->qp_table_size,
242 sizeof(*rdi->qp_dev->qp_table),
243 GFP_KERNEL, rdi->dparms.node);
244 if (!rdi->qp_dev->qp_table)
247 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
248 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
250 spin_lock_init(&rdi->qp_dev->qpt_lock);
252 /* initialize qpn map */
253 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
256 spin_lock_init(&rdi->n_qps_lock);
261 kfree(rdi->qp_dev->qp_table);
262 free_qpn_table(&rdi->qp_dev->qpn_table);
271 * free_all_qps - check for QPs still in use
272 * @rdi: rvt device info structure
274 * There should not be any QPs still in use.
275 * Free memory for table.
277 static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
281 unsigned n, qp_inuse = 0;
282 spinlock_t *ql; /* work around too long line below */
284 if (rdi->driver_f.free_all_qps)
285 qp_inuse = rdi->driver_f.free_all_qps(rdi);
287 qp_inuse += rvt_mcast_tree_empty(rdi);
292 ql = &rdi->qp_dev->qpt_lock;
293 spin_lock_irqsave(ql, flags);
294 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
295 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
296 lockdep_is_held(ql));
297 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
299 for (; qp; qp = rcu_dereference_protected(qp->next,
300 lockdep_is_held(ql)))
303 spin_unlock_irqrestore(ql, flags);
309 * rvt_qp_exit - clean up qps on device exit
310 * @rdi: rvt dev structure
312 * Check for qp leaks and free resources.
314 void rvt_qp_exit(struct rvt_dev_info *rdi)
316 u32 qps_inuse = rvt_free_all_qps(rdi);
319 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
324 kfree(rdi->qp_dev->qp_table);
325 free_qpn_table(&rdi->qp_dev->qpn_table);
329 static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
330 struct rvt_qpn_map *map, unsigned off)
332 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
336 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
337 * IB_QPT_SMI/IB_QPT_GSI
338 * @rdi: rvt device info structure
339 * @qpt: queue pair number table pointer
340 * @port_num: IB port number, 1 based, comes from core
342 * Return: The queue pair number
344 static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
345 enum ib_qp_type type, u8 port_num)
347 u32 i, offset, max_scan, qpn;
348 struct rvt_qpn_map *map;
351 if (rdi->driver_f.alloc_qpn)
352 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
354 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
357 ret = type == IB_QPT_GSI;
358 n = 1 << (ret + 2 * (port_num - 1));
359 spin_lock(&qpt->lock);
364 spin_unlock(&qpt->lock);
368 qpn = qpt->last + qpt->incr;
369 if (qpn >= RVT_QPN_MAX)
370 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
371 /* offset carries bit 0 */
372 offset = qpn & RVT_BITS_PER_PAGE_MASK;
373 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
374 max_scan = qpt->nmaps - !offset;
376 if (unlikely(!map->page)) {
377 get_map_page(qpt, map);
378 if (unlikely(!map->page))
382 if (!test_and_set_bit(offset, map->page)) {
389 * This qpn might be bogus if offset >= BITS_PER_PAGE.
390 * That is OK. It gets re-assigned below
392 qpn = mk_qpn(qpt, map, offset);
393 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
395 * In order to keep the number of pages allocated to a
396 * minimum, we scan the all existing pages before increasing
397 * the size of the bitmap table.
399 if (++i > max_scan) {
400 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
402 map = &qpt->map[qpt->nmaps++];
403 /* start at incr with current bit 0 */
404 offset = qpt->incr | (offset & 1);
405 } else if (map < &qpt->map[qpt->nmaps]) {
407 /* start at incr with current bit 0 */
408 offset = qpt->incr | (offset & 1);
411 /* wrap to first map page, invert bit 0 */
412 offset = qpt->incr | ((offset & 1) ^ 1);
414 /* there can be no set bits in low-order QoS bits */
415 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
416 qpn = mk_qpn(qpt, map, offset);
426 * rvt_clear_mr_refs - Drop help mr refs
427 * @qp: rvt qp data structure
428 * @clr_sends: If shoudl clear send side or not
430 static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
433 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
435 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
436 rvt_put_ss(&qp->s_rdma_read_sge);
438 rvt_put_ss(&qp->r_sge);
441 while (qp->s_last != qp->s_head) {
442 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
446 if (qp->ibqp.qp_type == IB_QPT_UD ||
447 qp->ibqp.qp_type == IB_QPT_SMI ||
448 qp->ibqp.qp_type == IB_QPT_GSI)
449 atomic_dec(&ibah_to_rvtah(
450 wqe->ud_wr.ah)->refcount);
451 if (++qp->s_last >= qp->s_size)
453 smp_wmb(); /* see qp_set_savail */
456 rvt_put_mr(qp->s_rdma_mr);
457 qp->s_rdma_mr = NULL;
461 for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) {
462 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
464 if (e->rdma_sge.mr) {
465 rvt_put_mr(e->rdma_sge.mr);
466 e->rdma_sge.mr = NULL;
472 * rvt_swqe_has_lkey - return true if lkey is used by swqe
473 * @wqe - the send wqe
476 * Test the swqe for using lkey
478 static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey)
482 for (i = 0; i < wqe->wr.num_sge; i++) {
483 struct rvt_sge *sge = &wqe->sg_list[i];
485 if (rvt_mr_has_lkey(sge->mr, lkey))
492 * rvt_qp_sends_has_lkey - return true is qp sends use lkey
496 static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey)
498 u32 s_last = qp->s_last;
500 while (s_last != qp->s_head) {
501 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last);
503 if (rvt_swqe_has_lkey(wqe, lkey))
506 if (++s_last >= qp->s_size)
510 if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey))
516 * rvt_qp_acks_has_lkey - return true if acks have lkey
520 static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey)
523 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
525 for (i = 0; qp->s_ack_queue && i < rvt_max_atomic(rdi); i++) {
526 struct rvt_ack_entry *e = &qp->s_ack_queue[i];
528 if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey))
535 * rvt_qp_mr_clean - clean up remote ops for lkey
537 * @lkey - the lkey that is being de-registered
539 * This routine checks if the lkey is being used by
542 * If so, the qp is put into an error state to elminate
543 * any references from the qp.
545 void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey)
547 bool lastwqe = false;
549 if (qp->ibqp.qp_type == IB_QPT_SMI ||
550 qp->ibqp.qp_type == IB_QPT_GSI)
551 /* avoid special QPs */
553 spin_lock_irq(&qp->r_lock);
554 spin_lock(&qp->s_hlock);
555 spin_lock(&qp->s_lock);
557 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
560 if (rvt_ss_has_lkey(&qp->r_sge, lkey) ||
561 rvt_qp_sends_has_lkey(qp, lkey) ||
562 rvt_qp_acks_has_lkey(qp, lkey))
563 lastwqe = rvt_error_qp(qp, IB_WC_LOC_PROT_ERR);
565 spin_unlock(&qp->s_lock);
566 spin_unlock(&qp->s_hlock);
567 spin_unlock_irq(&qp->r_lock);
571 ev.device = qp->ibqp.device;
572 ev.element.qp = &qp->ibqp;
573 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
574 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
579 * rvt_remove_qp - remove qp form table
580 * @rdi: rvt dev struct
583 * Remove the QP from the table so it can't be found asynchronously by
584 * the receive routine.
586 static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
588 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
589 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
593 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
595 if (rcu_dereference_protected(rvp->qp[0],
596 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
597 RCU_INIT_POINTER(rvp->qp[0], NULL);
598 } else if (rcu_dereference_protected(rvp->qp[1],
599 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
600 RCU_INIT_POINTER(rvp->qp[1], NULL);
603 struct rvt_qp __rcu **qpp;
606 qpp = &rdi->qp_dev->qp_table[n];
607 for (; (q = rcu_dereference_protected(*qpp,
608 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
611 RCU_INIT_POINTER(*qpp,
612 rcu_dereference_protected(qp->next,
613 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
615 trace_rvt_qpremove(qp, n);
621 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
629 * rvt_init_qp - initialize the QP state to the reset state
630 * @qp: the QP to init or reinit
633 * This function is called from both rvt_create_qp() and
634 * rvt_reset_qp(). The difference is that the reset
635 * patch the necessary locks to protect against concurent
638 static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
639 enum ib_qp_type type)
643 qp->qp_access_flags = 0;
644 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
650 qp->s_sending_psn = 0;
651 qp->s_sending_hpsn = 0;
655 if (type == IB_QPT_RC) {
656 qp->s_state = IB_OPCODE_RC_SEND_LAST;
657 qp->r_state = IB_OPCODE_RC_SEND_LAST;
659 qp->s_state = IB_OPCODE_UC_SEND_LAST;
660 qp->r_state = IB_OPCODE_UC_SEND_LAST;
662 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
673 qp->s_mig_state = IB_MIG_MIGRATED;
674 qp->r_head_ack_queue = 0;
675 qp->s_tail_ack_queue = 0;
676 qp->s_num_rd_atomic = 0;
678 qp->r_rq.wq->head = 0;
679 qp->r_rq.wq->tail = 0;
681 qp->r_sge.num_sge = 0;
682 atomic_set(&qp->s_reserved_used, 0);
686 * rvt_reset_qp - initialize the QP state to the reset state
687 * @qp: the QP to reset
690 * r_lock, s_hlock, and s_lock are required to be held by the caller
692 static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
693 enum ib_qp_type type)
694 __must_hold(&qp->s_lock)
695 __must_hold(&qp->s_hlock)
696 __must_hold(&qp->r_lock)
698 lockdep_assert_held(&qp->r_lock);
699 lockdep_assert_held(&qp->s_hlock);
700 lockdep_assert_held(&qp->s_lock);
701 if (qp->state != IB_QPS_RESET) {
702 qp->state = IB_QPS_RESET;
704 /* Let drivers flush their waitlist */
705 rdi->driver_f.flush_qp_waiters(qp);
706 rvt_stop_rc_timers(qp);
707 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
708 spin_unlock(&qp->s_lock);
709 spin_unlock(&qp->s_hlock);
710 spin_unlock_irq(&qp->r_lock);
712 /* Stop the send queue and the retry timer */
713 rdi->driver_f.stop_send_queue(qp);
714 rvt_del_timers_sync(qp);
715 /* Wait for things to stop */
716 rdi->driver_f.quiesce_qp(qp);
718 /* take qp out the hash and wait for it to be unused */
719 rvt_remove_qp(rdi, qp);
721 /* grab the lock b/c it was locked at call time */
722 spin_lock_irq(&qp->r_lock);
723 spin_lock(&qp->s_hlock);
724 spin_lock(&qp->s_lock);
726 rvt_clear_mr_refs(qp, 1);
728 * Let the driver do any tear down or re-init it needs to for
729 * a qp that has been reset
731 rdi->driver_f.notify_qp_reset(qp);
733 rvt_init_qp(rdi, qp, type);
734 lockdep_assert_held(&qp->r_lock);
735 lockdep_assert_held(&qp->s_hlock);
736 lockdep_assert_held(&qp->s_lock);
739 /** rvt_free_qpn - Free a qpn from the bit map
741 * @qpn: queue pair number to free
743 static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
745 struct rvt_qpn_map *map;
747 map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
749 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
753 * rvt_create_qp - create a queue pair for a device
754 * @ibpd: the protection domain who's device we create the queue pair for
755 * @init_attr: the attributes of the queue pair
756 * @udata: user data for libibverbs.so
758 * Queue pair creation is mostly an rvt issue. However, drivers have their own
759 * unique idea of what queue pair numbers mean. For instance there is a reserved
762 * Return: the queue pair on success, otherwise returns an errno.
764 * Called by the ib_create_qp() core verbs function.
766 struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
767 struct ib_qp_init_attr *init_attr,
768 struct ib_udata *udata)
772 struct rvt_swqe *swq = NULL;
775 struct ib_qp *ret = ERR_PTR(-ENOMEM);
776 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
781 return ERR_PTR(-EINVAL);
783 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
784 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
785 init_attr->create_flags)
786 return ERR_PTR(-EINVAL);
788 /* Check receive queue parameters if no SRQ is specified. */
789 if (!init_attr->srq) {
790 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
791 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
792 return ERR_PTR(-EINVAL);
794 if (init_attr->cap.max_send_sge +
795 init_attr->cap.max_send_wr +
796 init_attr->cap.max_recv_sge +
797 init_attr->cap.max_recv_wr == 0)
798 return ERR_PTR(-EINVAL);
801 init_attr->cap.max_send_wr + 1 +
802 rdi->dparms.reserved_operations;
803 switch (init_attr->qp_type) {
806 if (init_attr->port_num == 0 ||
807 init_attr->port_num > ibpd->device->phys_port_cnt)
808 return ERR_PTR(-EINVAL);
813 sz = sizeof(struct rvt_sge) *
814 init_attr->cap.max_send_sge +
815 sizeof(struct rvt_swqe);
816 swq = vzalloc_node(sqsize * sz, rdi->dparms.node);
818 return ERR_PTR(-ENOMEM);
822 if (init_attr->srq) {
823 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
825 if (srq->rq.max_sge > 1)
826 sg_list_sz = sizeof(*qp->r_sg_list) *
827 (srq->rq.max_sge - 1);
828 } else if (init_attr->cap.max_recv_sge > 1)
829 sg_list_sz = sizeof(*qp->r_sg_list) *
830 (init_attr->cap.max_recv_sge - 1);
831 qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
836 RCU_INIT_POINTER(qp->next, NULL);
837 if (init_attr->qp_type == IB_QPT_RC) {
840 sizeof(*qp->s_ack_queue) *
844 if (!qp->s_ack_queue)
847 /* initialize timers needed for rc qp */
848 timer_setup(&qp->s_timer, rvt_rc_timeout, 0);
849 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
851 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
854 * Driver needs to set up it's private QP structure and do any
855 * initialization that is needed.
857 priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
863 qp->timeout_jiffies =
864 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
866 if (init_attr->srq) {
869 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
870 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
871 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
872 sizeof(struct rvt_rwqe);
874 qp->r_rq.wq = vmalloc_user(
875 sizeof(struct rvt_rwq) +
878 qp->r_rq.wq = vzalloc_node(
879 sizeof(struct rvt_rwq) +
883 goto bail_driver_priv;
887 * ib_create_qp() will initialize qp->ibqp
888 * except for qp->ibqp.qp_num.
890 spin_lock_init(&qp->r_lock);
891 spin_lock_init(&qp->s_hlock);
892 spin_lock_init(&qp->s_lock);
893 spin_lock_init(&qp->r_rq.lock);
894 atomic_set(&qp->refcount, 0);
895 atomic_set(&qp->local_ops_pending, 0);
896 init_waitqueue_head(&qp->wait);
897 INIT_LIST_HEAD(&qp->rspwait);
898 qp->state = IB_QPS_RESET;
901 qp->s_avail = init_attr->cap.max_send_wr;
902 qp->s_max_sge = init_attr->cap.max_send_sge;
903 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
904 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
906 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
908 init_attr->port_num);
913 qp->ibqp.qp_num = err;
914 qp->port_num = init_attr->port_num;
915 rvt_init_qp(rdi, qp, init_attr->qp_type);
919 /* Don't support raw QPs */
920 return ERR_PTR(-EINVAL);
923 init_attr->cap.max_inline_data = 0;
926 * Return the address of the RWQ as the offset to mmap.
927 * See rvt_mmap() for details.
929 if (udata && udata->outlen >= sizeof(__u64)) {
933 err = ib_copy_to_udata(udata, &offset,
940 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
942 qp->ip = rvt_create_mmap_info(rdi, s,
943 ibpd->uobject->context,
946 ret = ERR_PTR(-ENOMEM);
950 err = ib_copy_to_udata(udata, &qp->ip->offset,
951 sizeof(qp->ip->offset));
957 qp->pid = current->pid;
960 spin_lock(&rdi->n_qps_lock);
961 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
962 spin_unlock(&rdi->n_qps_lock);
963 ret = ERR_PTR(-ENOMEM);
967 rdi->n_qps_allocated++;
969 * Maintain a busy_jiffies variable that will be added to the timeout
970 * period in mod_retry_timer and add_retry_timer. This busy jiffies
971 * is scaled by the number of rc qps created for the device to reduce
972 * the number of timeouts occurring when there is a large number of
973 * qps. busy_jiffies is incremented every rc qp scaling interval.
974 * The scaling interval is selected based on extensive performance
975 * evaluation of targeted workloads.
977 if (init_attr->qp_type == IB_QPT_RC) {
979 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
981 spin_unlock(&rdi->n_qps_lock);
984 spin_lock_irq(&rdi->pending_lock);
985 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
986 spin_unlock_irq(&rdi->pending_lock);
992 * We have our QP and its good, now keep track of what types of opcodes
993 * can be processed on this QP. We do this by keeping track of what the
994 * 3 high order bits of the opcode are.
996 switch (init_attr->qp_type) {
1000 qp->allowed_ops = IB_OPCODE_UD;
1003 qp->allowed_ops = IB_OPCODE_RC;
1006 qp->allowed_ops = IB_OPCODE_UC;
1009 ret = ERR_PTR(-EINVAL);
1017 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1020 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1027 rdi->driver_f.qp_priv_free(rdi, qp);
1030 kfree(qp->s_ack_queue);
1040 * rvt_error_qp - put a QP into the error state
1041 * @qp: the QP to put into the error state
1042 * @err: the receive completion error to signal if a RWQE is active
1044 * Flushes both send and receive work queues.
1046 * Return: true if last WQE event should be generated.
1047 * The QP r_lock and s_lock should be held and interrupts disabled.
1048 * If we are already in error state, just return.
1050 int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
1054 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1056 lockdep_assert_held(&qp->r_lock);
1057 lockdep_assert_held(&qp->s_lock);
1058 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
1061 qp->state = IB_QPS_ERR;
1063 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1064 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1065 del_timer(&qp->s_timer);
1068 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
1069 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
1071 rdi->driver_f.notify_error_qp(qp);
1073 /* Schedule the sending tasklet to drain the send work queue. */
1074 if (READ_ONCE(qp->s_last) != qp->s_head)
1075 rdi->driver_f.schedule_send(qp);
1077 rvt_clear_mr_refs(qp, 0);
1079 memset(&wc, 0, sizeof(wc));
1081 wc.opcode = IB_WC_RECV;
1083 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1084 wc.wr_id = qp->r_wr_id;
1086 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1088 wc.status = IB_WC_WR_FLUSH_ERR;
1095 spin_lock(&qp->r_rq.lock);
1097 /* sanity check pointers before trusting them */
1100 if (head >= qp->r_rq.size)
1103 if (tail >= qp->r_rq.size)
1105 while (tail != head) {
1106 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1107 if (++tail >= qp->r_rq.size)
1109 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1113 spin_unlock(&qp->r_rq.lock);
1114 } else if (qp->ibqp.event_handler) {
1121 EXPORT_SYMBOL(rvt_error_qp);
1124 * Put the QP into the hash table.
1125 * The hash table holds a reference to the QP.
1127 static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1129 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1130 unsigned long flags;
1133 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1135 if (qp->ibqp.qp_num <= 1) {
1136 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1138 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1140 qp->next = rdi->qp_dev->qp_table[n];
1141 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1142 trace_rvt_qpinsert(qp, n);
1145 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1149 * rvt_modify_qp - modify the attributes of a queue pair
1150 * @ibqp: the queue pair who's attributes we're modifying
1151 * @attr: the new attributes
1152 * @attr_mask: the mask of attributes to modify
1153 * @udata: user data for libibverbs.so
1155 * Return: 0 on success, otherwise returns an errno.
1157 int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1158 int attr_mask, struct ib_udata *udata)
1160 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1161 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1162 enum ib_qp_state cur_state, new_state;
1166 int pmtu = 0; /* for gcc warning only */
1167 enum rdma_link_layer link;
1170 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1172 spin_lock_irq(&qp->r_lock);
1173 spin_lock(&qp->s_hlock);
1174 spin_lock(&qp->s_lock);
1176 cur_state = attr_mask & IB_QP_CUR_STATE ?
1177 attr->cur_qp_state : qp->state;
1178 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1179 opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
1181 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1185 if (rdi->driver_f.check_modify_qp &&
1186 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1189 if (attr_mask & IB_QP_AV) {
1191 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1192 opa_get_mcast_base(OPA_MCAST_NR))
1195 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1196 be16_to_cpu(IB_MULTICAST_LID_BASE))
1200 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1204 if (attr_mask & IB_QP_ALT_PATH) {
1206 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1207 opa_get_mcast_base(OPA_MCAST_NR))
1210 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1211 be16_to_cpu(IB_MULTICAST_LID_BASE))
1215 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1217 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1221 if (attr_mask & IB_QP_PKEY_INDEX)
1222 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1225 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1226 if (attr->min_rnr_timer > 31)
1229 if (attr_mask & IB_QP_PORT)
1230 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1231 qp->ibqp.qp_type == IB_QPT_GSI ||
1232 attr->port_num == 0 ||
1233 attr->port_num > ibqp->device->phys_port_cnt)
1236 if (attr_mask & IB_QP_DEST_QPN)
1237 if (attr->dest_qp_num > RVT_QPN_MASK)
1240 if (attr_mask & IB_QP_RETRY_CNT)
1241 if (attr->retry_cnt > 7)
1244 if (attr_mask & IB_QP_RNR_RETRY)
1245 if (attr->rnr_retry > 7)
1249 * Don't allow invalid path_mtu values. OK to set greater
1250 * than the active mtu (or even the max_cap, if we have tuned
1251 * that to a small mtu. We'll set qp->path_mtu
1252 * to the lesser of requested attribute mtu and active,
1253 * for packetizing messages.
1254 * Note that the QP port has to be set in INIT and MTU in RTR.
1256 if (attr_mask & IB_QP_PATH_MTU) {
1257 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1262 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1263 if (attr->path_mig_state == IB_MIG_REARM) {
1264 if (qp->s_mig_state == IB_MIG_ARMED)
1266 if (new_state != IB_QPS_RTS)
1268 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1269 if (qp->s_mig_state == IB_MIG_REARM)
1271 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1273 if (qp->s_mig_state == IB_MIG_ARMED)
1280 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1281 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1284 switch (new_state) {
1286 if (qp->state != IB_QPS_RESET)
1287 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1291 /* Allow event to re-trigger if QP set to RTR more than once */
1292 qp->r_flags &= ~RVT_R_COMM_EST;
1293 qp->state = new_state;
1297 qp->s_draining = qp->s_last != qp->s_cur;
1298 qp->state = new_state;
1302 if (qp->ibqp.qp_type == IB_QPT_RC)
1304 qp->state = new_state;
1308 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1312 qp->state = new_state;
1316 if (attr_mask & IB_QP_PKEY_INDEX)
1317 qp->s_pkey_index = attr->pkey_index;
1319 if (attr_mask & IB_QP_PORT)
1320 qp->port_num = attr->port_num;
1322 if (attr_mask & IB_QP_DEST_QPN)
1323 qp->remote_qpn = attr->dest_qp_num;
1325 if (attr_mask & IB_QP_SQ_PSN) {
1326 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1327 qp->s_psn = qp->s_next_psn;
1328 qp->s_sending_psn = qp->s_next_psn;
1329 qp->s_last_psn = qp->s_next_psn - 1;
1330 qp->s_sending_hpsn = qp->s_last_psn;
1333 if (attr_mask & IB_QP_RQ_PSN)
1334 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1336 if (attr_mask & IB_QP_ACCESS_FLAGS)
1337 qp->qp_access_flags = attr->qp_access_flags;
1339 if (attr_mask & IB_QP_AV) {
1340 qp->remote_ah_attr = attr->ah_attr;
1341 qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
1342 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1345 if (attr_mask & IB_QP_ALT_PATH) {
1346 qp->alt_ah_attr = attr->alt_ah_attr;
1347 qp->s_alt_pkey_index = attr->alt_pkey_index;
1350 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1351 qp->s_mig_state = attr->path_mig_state;
1353 qp->remote_ah_attr = qp->alt_ah_attr;
1354 qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
1355 qp->s_pkey_index = qp->s_alt_pkey_index;
1359 if (attr_mask & IB_QP_PATH_MTU) {
1360 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1361 qp->log_pmtu = ilog2(qp->pmtu);
1364 if (attr_mask & IB_QP_RETRY_CNT) {
1365 qp->s_retry_cnt = attr->retry_cnt;
1366 qp->s_retry = attr->retry_cnt;
1369 if (attr_mask & IB_QP_RNR_RETRY) {
1370 qp->s_rnr_retry_cnt = attr->rnr_retry;
1371 qp->s_rnr_retry = attr->rnr_retry;
1374 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1375 qp->r_min_rnr_timer = attr->min_rnr_timer;
1377 if (attr_mask & IB_QP_TIMEOUT) {
1378 qp->timeout = attr->timeout;
1379 qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
1382 if (attr_mask & IB_QP_QKEY)
1383 qp->qkey = attr->qkey;
1385 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1386 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1388 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1389 qp->s_max_rd_atomic = attr->max_rd_atomic;
1391 if (rdi->driver_f.modify_qp)
1392 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1394 spin_unlock(&qp->s_lock);
1395 spin_unlock(&qp->s_hlock);
1396 spin_unlock_irq(&qp->r_lock);
1398 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1399 rvt_insert_qp(rdi, qp);
1402 ev.device = qp->ibqp.device;
1403 ev.element.qp = &qp->ibqp;
1404 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1405 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1408 ev.device = qp->ibqp.device;
1409 ev.element.qp = &qp->ibqp;
1410 ev.event = IB_EVENT_PATH_MIG;
1411 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1416 spin_unlock(&qp->s_lock);
1417 spin_unlock(&qp->s_hlock);
1418 spin_unlock_irq(&qp->r_lock);
1423 * rvt_destroy_qp - destroy a queue pair
1424 * @ibqp: the queue pair to destroy
1426 * Note that this can be called while the QP is actively sending or
1429 * Return: 0 on success.
1431 int rvt_destroy_qp(struct ib_qp *ibqp)
1433 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1434 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1436 spin_lock_irq(&qp->r_lock);
1437 spin_lock(&qp->s_hlock);
1438 spin_lock(&qp->s_lock);
1439 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1440 spin_unlock(&qp->s_lock);
1441 spin_unlock(&qp->s_hlock);
1442 spin_unlock_irq(&qp->r_lock);
1444 wait_event(qp->wait, !atomic_read(&qp->refcount));
1445 /* qpn is now available for use again */
1446 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1448 spin_lock(&rdi->n_qps_lock);
1449 rdi->n_qps_allocated--;
1450 if (qp->ibqp.qp_type == IB_QPT_RC) {
1452 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1454 spin_unlock(&rdi->n_qps_lock);
1457 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1461 rdi->driver_f.qp_priv_free(rdi, qp);
1462 kfree(qp->s_ack_queue);
1468 * rvt_query_qp - query an ipbq
1469 * @ibqp: IB qp to query
1470 * @attr: attr struct to fill in
1471 * @attr_mask: attr mask ignored
1472 * @init_attr: struct to fill in
1476 int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1477 int attr_mask, struct ib_qp_init_attr *init_attr)
1479 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1480 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1482 attr->qp_state = qp->state;
1483 attr->cur_qp_state = attr->qp_state;
1484 attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
1485 attr->path_mig_state = qp->s_mig_state;
1486 attr->qkey = qp->qkey;
1487 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1488 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1489 attr->dest_qp_num = qp->remote_qpn;
1490 attr->qp_access_flags = qp->qp_access_flags;
1491 attr->cap.max_send_wr = qp->s_size - 1 -
1492 rdi->dparms.reserved_operations;
1493 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1494 attr->cap.max_send_sge = qp->s_max_sge;
1495 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1496 attr->cap.max_inline_data = 0;
1497 attr->ah_attr = qp->remote_ah_attr;
1498 attr->alt_ah_attr = qp->alt_ah_attr;
1499 attr->pkey_index = qp->s_pkey_index;
1500 attr->alt_pkey_index = qp->s_alt_pkey_index;
1501 attr->en_sqd_async_notify = 0;
1502 attr->sq_draining = qp->s_draining;
1503 attr->max_rd_atomic = qp->s_max_rd_atomic;
1504 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1505 attr->min_rnr_timer = qp->r_min_rnr_timer;
1506 attr->port_num = qp->port_num;
1507 attr->timeout = qp->timeout;
1508 attr->retry_cnt = qp->s_retry_cnt;
1509 attr->rnr_retry = qp->s_rnr_retry_cnt;
1510 attr->alt_port_num =
1511 rdma_ah_get_port_num(&qp->alt_ah_attr);
1512 attr->alt_timeout = qp->alt_timeout;
1514 init_attr->event_handler = qp->ibqp.event_handler;
1515 init_attr->qp_context = qp->ibqp.qp_context;
1516 init_attr->send_cq = qp->ibqp.send_cq;
1517 init_attr->recv_cq = qp->ibqp.recv_cq;
1518 init_attr->srq = qp->ibqp.srq;
1519 init_attr->cap = attr->cap;
1520 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1521 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1523 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1524 init_attr->qp_type = qp->ibqp.qp_type;
1525 init_attr->port_num = qp->port_num;
1530 * rvt_post_receive - post a receive on a QP
1531 * @ibqp: the QP to post the receive on
1532 * @wr: the WR to post
1533 * @bad_wr: the first bad WR is put here
1535 * This may be called from interrupt context.
1537 * Return: 0 on success otherwise errno
1539 int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1540 struct ib_recv_wr **bad_wr)
1542 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1543 struct rvt_rwq *wq = qp->r_rq.wq;
1544 unsigned long flags;
1545 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1548 /* Check that state is OK to post receive. */
1549 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1554 for (; wr; wr = wr->next) {
1555 struct rvt_rwqe *wqe;
1559 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1564 spin_lock_irqsave(&qp->r_rq.lock, flags);
1565 next = wq->head + 1;
1566 if (next >= qp->r_rq.size)
1568 if (next == wq->tail) {
1569 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1573 if (unlikely(qp_err_flush)) {
1576 memset(&wc, 0, sizeof(wc));
1578 wc.opcode = IB_WC_RECV;
1579 wc.wr_id = wr->wr_id;
1580 wc.status = IB_WC_WR_FLUSH_ERR;
1581 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1583 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1584 wqe->wr_id = wr->wr_id;
1585 wqe->num_sge = wr->num_sge;
1586 for (i = 0; i < wr->num_sge; i++)
1587 wqe->sg_list[i] = wr->sg_list[i];
1589 * Make sure queue entry is written
1590 * before the head index.
1595 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1601 * rvt_qp_valid_operation - validate post send wr request
1603 * @post-parms - the post send table for the driver
1604 * @wr - the work request
1606 * The routine validates the operation based on the
1607 * validation table an returns the length of the operation
1608 * which can extend beyond the ib_send_bw. Operation
1609 * dependent flags key atomic operation validation.
1611 * There is an exception for UD qps that validates the pd and
1612 * overrides the length to include the additional UD specific
1615 * Returns a negative error or the length of the work request
1616 * for building the swqe.
1618 static inline int rvt_qp_valid_operation(
1620 const struct rvt_operation_params *post_parms,
1621 struct ib_send_wr *wr)
1625 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1627 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1629 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1630 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1632 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1633 (wr->num_sge == 0 ||
1634 wr->sg_list[0].length < sizeof(u64) ||
1635 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1637 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1638 !qp->s_max_rd_atomic)
1640 len = post_parms[wr->opcode].length;
1642 if (qp->ibqp.qp_type != IB_QPT_UC &&
1643 qp->ibqp.qp_type != IB_QPT_RC) {
1644 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1646 len = sizeof(struct ib_ud_wr);
1652 * rvt_qp_is_avail - determine queue capacity
1654 * @rdi: the rdmavt device
1655 * @reserved_op: is reserved operation
1657 * This assumes the s_hlock is held but the s_last
1658 * qp variable is uncontrolled.
1660 * For non reserved operations, the qp->s_avail
1663 * The return value is zero or a -ENOMEM.
1665 static inline int rvt_qp_is_avail(
1667 struct rvt_dev_info *rdi,
1674 /* see rvt_qp_wqe_unreserve() */
1675 smp_mb__before_atomic();
1676 reserved_used = atomic_read(&qp->s_reserved_used);
1677 if (unlikely(reserved_op)) {
1678 /* see rvt_qp_wqe_unreserve() */
1679 smp_mb__before_atomic();
1680 if (reserved_used >= rdi->dparms.reserved_operations)
1684 /* non-reserved operations */
1685 if (likely(qp->s_avail))
1687 slast = READ_ONCE(qp->s_last);
1688 if (qp->s_head >= slast)
1689 avail = qp->s_size - (qp->s_head - slast);
1691 avail = slast - qp->s_head;
1693 /* see rvt_qp_wqe_unreserve() */
1694 smp_mb__before_atomic();
1695 reserved_used = atomic_read(&qp->s_reserved_used);
1697 (rdi->dparms.reserved_operations - reserved_used);
1698 /* insure we don't assign a negative s_avail */
1699 if ((s32)avail <= 0)
1701 qp->s_avail = avail;
1702 if (WARN_ON(qp->s_avail >
1703 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1705 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1706 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1707 qp->s_head, qp->s_tail, qp->s_cur,
1708 qp->s_acked, qp->s_last);
1713 * rvt_post_one_wr - post one RC, UC, or UD send work request
1714 * @qp: the QP to post on
1715 * @wr: the work request to send
1717 static int rvt_post_one_wr(struct rvt_qp *qp,
1718 struct ib_send_wr *wr,
1721 struct rvt_swqe *wqe;
1726 struct rvt_lkey_table *rkt;
1728 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1733 int local_ops_delayed = 0;
1735 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1737 /* IB spec says that num_sge == 0 is OK. */
1738 if (unlikely(wr->num_sge > qp->s_max_sge))
1741 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1747 * Local operations include fast register and local invalidate.
1748 * Fast register needs to be processed immediately because the
1749 * registered lkey may be used by following work requests and the
1750 * lkey needs to be valid at the time those requests are posted.
1751 * Local invalidate can be processed immediately if fencing is
1752 * not required and no previous local invalidate ops are pending.
1753 * Signaled local operations that have been processed immediately
1754 * need to have requests with "completion only" flags set posted
1755 * to the send queue in order to generate completions.
1757 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
1758 switch (wr->opcode) {
1760 ret = rvt_fast_reg_mr(qp,
1763 reg_wr(wr)->access);
1764 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1767 case IB_WR_LOCAL_INV:
1768 if ((wr->send_flags & IB_SEND_FENCE) ||
1769 atomic_read(&qp->local_ops_pending)) {
1770 local_ops_delayed = 1;
1772 ret = rvt_invalidate_rkey(
1773 qp, wr->ex.invalidate_rkey);
1774 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1783 reserved_op = rdi->post_parms[wr->opcode].flags &
1784 RVT_OPERATION_USE_RESERVE;
1785 /* check for avail */
1786 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1789 next = qp->s_head + 1;
1790 if (next >= qp->s_size)
1793 rkt = &rdi->lkey_table;
1794 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1795 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1797 /* cplen has length from above */
1798 memcpy(&wqe->wr, wr, cplen);
1803 struct rvt_sge *last_sge = NULL;
1805 acc = wr->opcode >= IB_WR_RDMA_READ ?
1806 IB_ACCESS_LOCAL_WRITE : 0;
1807 for (i = 0; i < wr->num_sge; i++) {
1808 u32 length = wr->sg_list[i].length;
1812 ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
1813 &wr->sg_list[i], acc);
1814 if (unlikely(ret < 0))
1815 goto bail_inval_free;
1816 wqe->length += length;
1818 last_sge = &wqe->sg_list[j];
1821 wqe->wr.num_sge = j;
1824 /* general part of wqe valid - allow for driver checks */
1825 if (rdi->driver_f.check_send_wqe) {
1826 ret = rdi->driver_f.check_send_wqe(qp, wqe);
1828 goto bail_inval_free;
1833 log_pmtu = qp->log_pmtu;
1834 if (qp->ibqp.qp_type != IB_QPT_UC &&
1835 qp->ibqp.qp_type != IB_QPT_RC) {
1836 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1838 log_pmtu = ah->log_pmtu;
1839 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1842 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
1843 if (local_ops_delayed)
1844 atomic_inc(&qp->local_ops_pending);
1846 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
1851 wqe->ssn = qp->s_ssn++;
1852 wqe->psn = qp->s_next_psn;
1853 wqe->lpsn = wqe->psn +
1855 ((wqe->length - 1) >> log_pmtu) :
1857 qp->s_next_psn = wqe->lpsn + 1;
1859 if (unlikely(reserved_op)) {
1860 wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
1861 rvt_qp_wqe_reserve(qp, wqe);
1863 wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
1866 trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
1867 smp_wmb(); /* see request builders */
1873 /* release mr holds */
1875 struct rvt_sge *sge = &wqe->sg_list[--j];
1877 rvt_put_mr(sge->mr);
1883 * rvt_post_send - post a send on a QP
1884 * @ibqp: the QP to post the send on
1885 * @wr: the list of work requests to post
1886 * @bad_wr: the first bad WR is put here
1888 * This may be called from interrupt context.
1890 * Return: 0 on success else errno
1892 int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1893 struct ib_send_wr **bad_wr)
1895 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1896 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1897 unsigned long flags = 0;
1902 spin_lock_irqsave(&qp->s_hlock, flags);
1905 * Ensure QP state is such that we can send. If not bail out early,
1906 * there is no need to do this every time we post a send.
1908 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
1909 spin_unlock_irqrestore(&qp->s_hlock, flags);
1914 * If the send queue is empty, and we only have a single WR then just go
1915 * ahead and kick the send engine into gear. Otherwise we will always
1916 * just schedule the send to happen later.
1918 call_send = qp->s_head == READ_ONCE(qp->s_last) && !wr->next;
1920 for (; wr; wr = wr->next) {
1921 err = rvt_post_one_wr(qp, wr, &call_send);
1922 if (unlikely(err)) {
1929 spin_unlock_irqrestore(&qp->s_hlock, flags);
1932 rdi->driver_f.do_send(qp);
1934 rdi->driver_f.schedule_send_no_lock(qp);
1940 * rvt_post_srq_receive - post a receive on a shared receive queue
1941 * @ibsrq: the SRQ to post the receive on
1942 * @wr: the list of work requests to post
1943 * @bad_wr: A pointer to the first WR to cause a problem is put here
1945 * This may be called from interrupt context.
1947 * Return: 0 on success else errno
1949 int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1950 struct ib_recv_wr **bad_wr)
1952 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1954 unsigned long flags;
1956 for (; wr; wr = wr->next) {
1957 struct rvt_rwqe *wqe;
1961 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1966 spin_lock_irqsave(&srq->rq.lock, flags);
1968 next = wq->head + 1;
1969 if (next >= srq->rq.size)
1971 if (next == wq->tail) {
1972 spin_unlock_irqrestore(&srq->rq.lock, flags);
1977 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1978 wqe->wr_id = wr->wr_id;
1979 wqe->num_sge = wr->num_sge;
1980 for (i = 0; i < wr->num_sge; i++)
1981 wqe->sg_list[i] = wr->sg_list[i];
1982 /* Make sure queue entry is written before the head index. */
1985 spin_unlock_irqrestore(&srq->rq.lock, flags);
1991 * Validate a RWQE and fill in the SGE state.
1994 static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
1998 struct rvt_lkey_table *rkt;
2000 struct rvt_sge_state *ss;
2001 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2003 rkt = &rdi->lkey_table;
2004 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
2006 ss->sg_list = qp->r_sg_list;
2008 for (i = j = 0; i < wqe->num_sge; i++) {
2009 if (wqe->sg_list[i].length == 0)
2012 ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
2013 NULL, &wqe->sg_list[i],
2014 IB_ACCESS_LOCAL_WRITE);
2015 if (unlikely(ret <= 0))
2017 qp->r_len += wqe->sg_list[i].length;
2021 ss->total_len = qp->r_len;
2026 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
2028 rvt_put_mr(sge->mr);
2031 memset(&wc, 0, sizeof(wc));
2032 wc.wr_id = wqe->wr_id;
2033 wc.status = IB_WC_LOC_PROT_ERR;
2034 wc.opcode = IB_WC_RECV;
2036 /* Signal solicited completion event. */
2037 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
2042 * rvt_get_rwqe - copy the next RWQE into the QP's RWQE
2044 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
2046 * Return -1 if there is a local error, 0 if no RWQE is available,
2047 * otherwise return 1.
2049 * Can be called from interrupt level.
2051 int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only)
2053 unsigned long flags;
2056 struct rvt_srq *srq;
2057 struct rvt_rwqe *wqe;
2058 void (*handler)(struct ib_event *, void *);
2063 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
2064 handler = srq->ibsrq.event_handler;
2072 spin_lock_irqsave(&rq->lock, flags);
2073 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
2080 /* Validate tail before using it since it is user writable. */
2081 if (tail >= rq->size)
2083 if (unlikely(tail == wq->head)) {
2087 /* Make sure entry is read after head index is read. */
2089 wqe = rvt_get_rwqe_ptr(rq, tail);
2091 * Even though we update the tail index in memory, the verbs
2092 * consumer is not supposed to post more entries until a
2093 * completion is generated.
2095 if (++tail >= rq->size)
2098 if (!wr_id_only && !init_sge(qp, wqe)) {
2102 qp->r_wr_id = wqe->wr_id;
2105 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
2110 * Validate head pointer value and compute
2111 * the number of remaining WQEs.
2117 n += rq->size - tail;
2120 if (n < srq->limit) {
2124 spin_unlock_irqrestore(&rq->lock, flags);
2125 ev.device = qp->ibqp.device;
2126 ev.element.srq = qp->ibqp.srq;
2127 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
2128 handler(&ev, srq->ibsrq.srq_context);
2133 spin_unlock_irqrestore(&rq->lock, flags);
2137 EXPORT_SYMBOL(rvt_get_rwqe);
2140 * qp_comm_est - handle trap with QP established
2143 void rvt_comm_est(struct rvt_qp *qp)
2145 qp->r_flags |= RVT_R_COMM_EST;
2146 if (qp->ibqp.event_handler) {
2149 ev.device = qp->ibqp.device;
2150 ev.element.qp = &qp->ibqp;
2151 ev.event = IB_EVENT_COMM_EST;
2152 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2155 EXPORT_SYMBOL(rvt_comm_est);
2157 void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
2159 unsigned long flags;
2162 spin_lock_irqsave(&qp->s_lock, flags);
2163 lastwqe = rvt_error_qp(qp, err);
2164 spin_unlock_irqrestore(&qp->s_lock, flags);
2169 ev.device = qp->ibqp.device;
2170 ev.element.qp = &qp->ibqp;
2171 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2172 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2175 EXPORT_SYMBOL(rvt_rc_error);
2178 * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
2179 * @index - the index
2180 * return usec from an index into ib_rvt_rnr_table
2182 unsigned long rvt_rnr_tbl_to_usec(u32 index)
2184 return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
2186 EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
2188 static inline unsigned long rvt_aeth_to_usec(u32 aeth)
2190 return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
2191 IB_AETH_CREDIT_MASK];
2195 * rvt_add_retry_timer - add/start a retry timer
2197 * add a retry timer on the QP
2199 void rvt_add_retry_timer(struct rvt_qp *qp)
2201 struct ib_qp *ibqp = &qp->ibqp;
2202 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2204 lockdep_assert_held(&qp->s_lock);
2205 qp->s_flags |= RVT_S_TIMER;
2206 /* 4.096 usec. * (1 << qp->timeout) */
2207 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
2209 add_timer(&qp->s_timer);
2211 EXPORT_SYMBOL(rvt_add_retry_timer);
2214 * rvt_add_rnr_timer - add/start an rnr timer
2216 * @aeth - aeth of RNR timeout, simulated aeth for loopback
2217 * add an rnr timer on the QP
2219 void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
2223 lockdep_assert_held(&qp->s_lock);
2224 qp->s_flags |= RVT_S_WAIT_RNR;
2225 to = rvt_aeth_to_usec(aeth);
2226 trace_rvt_rnrnak_add(qp, to);
2227 hrtimer_start(&qp->s_rnr_timer,
2228 ns_to_ktime(1000 * to), HRTIMER_MODE_REL_PINNED);
2230 EXPORT_SYMBOL(rvt_add_rnr_timer);
2233 * rvt_stop_rc_timers - stop all timers
2235 * stop any pending timers
2237 void rvt_stop_rc_timers(struct rvt_qp *qp)
2239 lockdep_assert_held(&qp->s_lock);
2240 /* Remove QP from all timers */
2241 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2242 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2243 del_timer(&qp->s_timer);
2244 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2247 EXPORT_SYMBOL(rvt_stop_rc_timers);
2250 * rvt_stop_rnr_timer - stop an rnr timer
2253 * stop an rnr timer and return if the timer
2256 static void rvt_stop_rnr_timer(struct rvt_qp *qp)
2258 lockdep_assert_held(&qp->s_lock);
2259 /* Remove QP from rnr timer */
2260 if (qp->s_flags & RVT_S_WAIT_RNR) {
2261 qp->s_flags &= ~RVT_S_WAIT_RNR;
2262 trace_rvt_rnrnak_stop(qp, 0);
2267 * rvt_del_timers_sync - wait for any timeout routines to exit
2270 void rvt_del_timers_sync(struct rvt_qp *qp)
2272 del_timer_sync(&qp->s_timer);
2273 hrtimer_cancel(&qp->s_rnr_timer);
2275 EXPORT_SYMBOL(rvt_del_timers_sync);
2278 * This is called from s_timer for missing responses.
2280 static void rvt_rc_timeout(struct timer_list *t)
2282 struct rvt_qp *qp = from_timer(qp, t, s_timer);
2283 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2284 unsigned long flags;
2286 spin_lock_irqsave(&qp->r_lock, flags);
2287 spin_lock(&qp->s_lock);
2288 if (qp->s_flags & RVT_S_TIMER) {
2289 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
2291 qp->s_flags &= ~RVT_S_TIMER;
2292 rvp->n_rc_timeouts++;
2293 del_timer(&qp->s_timer);
2294 trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
2295 if (rdi->driver_f.notify_restart_rc)
2296 rdi->driver_f.notify_restart_rc(qp,
2299 rdi->driver_f.schedule_send(qp);
2301 spin_unlock(&qp->s_lock);
2302 spin_unlock_irqrestore(&qp->r_lock, flags);
2306 * This is called from s_timer for RNR timeouts.
2308 enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2310 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2311 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2312 unsigned long flags;
2314 spin_lock_irqsave(&qp->s_lock, flags);
2315 rvt_stop_rnr_timer(qp);
2316 trace_rvt_rnrnak_timeout(qp, 0);
2317 rdi->driver_f.schedule_send(qp);
2318 spin_unlock_irqrestore(&qp->s_lock, flags);
2319 return HRTIMER_NORESTART;
2321 EXPORT_SYMBOL(rvt_rc_rnr_retry);
2324 * rvt_qp_iter_init - initial for QP iteration
2328 * This returns an iterator suitable for iterating QPs
2331 * The @cb is a user defined callback and @v is a 64
2332 * bit value passed to and relevant for processing in the
2333 * @cb. An example use case would be to alter QP processing
2334 * based on criteria not part of the rvt_qp.
2336 * Use cases that require memory allocation to succeed
2337 * must preallocate appropriately.
2339 * Return: a pointer to an rvt_qp_iter or NULL
2341 struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
2343 void (*cb)(struct rvt_qp *qp, u64 v))
2345 struct rvt_qp_iter *i;
2347 i = kzalloc(sizeof(*i), GFP_KERNEL);
2352 /* number of special QPs (SMI/GSI) for device */
2353 i->specials = rdi->ibdev.phys_port_cnt * 2;
2359 EXPORT_SYMBOL(rvt_qp_iter_init);
2362 * rvt_qp_iter_next - return the next QP in iter
2363 * @iter - the iterator
2365 * Fine grained QP iterator suitable for use
2366 * with debugfs seq_file mechanisms.
2368 * Updates iter->qp with the current QP when the return
2371 * Return: 0 - iter->qp is valid 1 - no more QPs
2373 int rvt_qp_iter_next(struct rvt_qp_iter *iter)
2378 struct rvt_qp *pqp = iter->qp;
2380 struct rvt_dev_info *rdi = iter->rdi;
2383 * The approach is to consider the special qps
2384 * as additional table entries before the
2385 * real hash table. Since the qp code sets
2386 * the qp->next hash link to NULL, this works just fine.
2388 * iter->specials is 2 * # ports
2390 * n = 0..iter->specials is the special qp indices
2392 * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
2393 * the potential hash bucket entries
2396 for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
2398 qp = rcu_dereference(pqp->next);
2400 if (n < iter->specials) {
2401 struct rvt_ibport *rvp;
2404 pidx = n % rdi->ibdev.phys_port_cnt;
2405 rvp = rdi->ports[pidx];
2406 qp = rcu_dereference(rvp->qp[n & 1]);
2408 qp = rcu_dereference(
2409 rdi->qp_dev->qp_table[
2410 (n - iter->specials)]);
2422 EXPORT_SYMBOL(rvt_qp_iter_next);
2425 * rvt_qp_iter - iterate all QPs
2426 * @rdi - rvt devinfo
2427 * @v - a 64 bit value
2430 * This provides a way for iterating all QPs.
2432 * The @cb is a user defined callback and @v is a 64
2433 * bit value passed to and relevant for processing in the
2434 * cb. An example use case would be to alter QP processing
2435 * based on criteria not part of the rvt_qp.
2437 * The code has an internal iterator to simplify
2438 * non seq_file use cases.
2440 void rvt_qp_iter(struct rvt_dev_info *rdi,
2442 void (*cb)(struct rvt_qp *qp, u64 v))
2445 struct rvt_qp_iter i = {
2447 .specials = rdi->ibdev.phys_port_cnt * 2,
2454 ret = rvt_qp_iter_next(&i);
2465 EXPORT_SYMBOL(rvt_qp_iter);