2 * Copyright(c) 2016 - 2018 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>
56 #include <rdma/uverbs_ioctl.h>
61 static void rvt_rc_timeout(struct timer_list *t);
64 * Convert the AETH RNR timeout code into the number of microseconds.
66 static const u32 ib_rvt_rnr_table[32] = {
67 655360, /* 00: 655.36 */
87 10240, /* 14: 10.24 */
88 15360, /* 15: 15.36 */
89 20480, /* 16: 20.48 */
90 30720, /* 17: 30.72 */
91 40960, /* 18: 40.96 */
92 61440, /* 19: 61.44 */
93 81920, /* 1A: 81.92 */
94 122880, /* 1B: 122.88 */
95 163840, /* 1C: 163.84 */
96 245760, /* 1D: 245.76 */
97 327680, /* 1E: 327.68 */
98 491520 /* 1F: 491.52 */
102 * Note that it is OK to post send work requests in the SQE and ERR
103 * states; rvt_do_send() will process them and generate error
104 * completions as per IB 1.2 C10-96.
106 const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
108 [IB_QPS_INIT] = RVT_POST_RECV_OK,
109 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
110 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
111 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
112 RVT_PROCESS_NEXT_SEND_OK,
113 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
114 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
115 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
116 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
117 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
118 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
120 EXPORT_SYMBOL(ib_rvt_state_ops);
122 /* platform specific: return the last level cache (llc) size, in KiB */
123 static int rvt_wss_llc_size(void)
125 /* assume that the boot CPU value is universal for all CPUs */
126 return boot_cpu_data.x86_cache_size;
129 /* platform specific: cacheless copy */
130 static void cacheless_memcpy(void *dst, void *src, size_t n)
133 * Use the only available X64 cacheless copy. Add a __user cast
134 * to quiet sparse. The src agument is already in the kernel so
135 * there are no security issues. The extra fault recovery machinery
138 __copy_user_nocache(dst, (void __user *)src, n, 0);
141 void rvt_wss_exit(struct rvt_dev_info *rdi)
143 struct rvt_wss *wss = rdi->wss;
148 /* coded to handle partially initialized and repeat callers */
156 * rvt_wss_init - Init wss data structures
158 * Return: 0 on success
160 int rvt_wss_init(struct rvt_dev_info *rdi)
162 unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode;
163 unsigned int wss_threshold = rdi->dparms.wss_threshold;
164 unsigned int wss_clean_period = rdi->dparms.wss_clean_period;
170 int node = rdi->dparms.node;
172 if (sge_copy_mode != RVT_SGE_COPY_ADAPTIVE) {
177 rdi->wss = kzalloc_node(sizeof(*rdi->wss), GFP_KERNEL, node);
182 /* check for a valid percent range - default to 80 if none or invalid */
183 if (wss_threshold < 1 || wss_threshold > 100)
186 /* reject a wildly large period */
187 if (wss_clean_period > 1000000)
188 wss_clean_period = 256;
190 /* reject a zero period */
191 if (wss_clean_period == 0)
192 wss_clean_period = 1;
195 * Calculate the table size - the next power of 2 larger than the
196 * LLC size. LLC size is in KiB.
198 llc_size = rvt_wss_llc_size() * 1024;
199 table_size = roundup_pow_of_two(llc_size);
201 /* one bit per page in rounded up table */
202 llc_bits = llc_size / PAGE_SIZE;
203 table_bits = table_size / PAGE_SIZE;
204 wss->pages_mask = table_bits - 1;
205 wss->num_entries = table_bits / BITS_PER_LONG;
207 wss->threshold = (llc_bits * wss_threshold) / 100;
208 if (wss->threshold == 0)
211 wss->clean_period = wss_clean_period;
212 atomic_set(&wss->clean_counter, wss_clean_period);
214 wss->entries = kcalloc_node(wss->num_entries, sizeof(*wss->entries),
225 * Advance the clean counter. When the clean period has expired,
228 * This is implemented in atomics to avoid locking. Because multiple
229 * variables are involved, it can be racy which can lead to slightly
230 * inaccurate information. Since this is only a heuristic, this is
231 * OK. Any innaccuracies will clean themselves out as the counter
232 * advances. That said, it is unlikely the entry clean operation will
233 * race - the next possible racer will not start until the next clean
236 * The clean counter is implemented as a decrement to zero. When zero
237 * is reached an entry is cleaned.
239 static void wss_advance_clean_counter(struct rvt_wss *wss)
245 /* become the cleaner if we decrement the counter to zero */
246 if (atomic_dec_and_test(&wss->clean_counter)) {
248 * Set, not add, the clean period. This avoids an issue
249 * where the counter could decrement below the clean period.
250 * Doing a set can result in lost decrements, slowing the
251 * clean advance. Since this a heuristic, this possible
254 * An alternative is to loop, advancing the counter by a
255 * clean period until the result is > 0. However, this could
256 * lead to several threads keeping another in the clean loop.
257 * This could be mitigated by limiting the number of times
258 * we stay in the loop.
260 atomic_set(&wss->clean_counter, wss->clean_period);
263 * Uniquely grab the entry to clean and move to next.
264 * The current entry is always the lower bits of
265 * wss.clean_entry. The table size, wss.num_entries,
266 * is always a power-of-2.
268 entry = (atomic_inc_return(&wss->clean_entry) - 1)
269 & (wss->num_entries - 1);
271 /* clear the entry and count the bits */
272 bits = xchg(&wss->entries[entry], 0);
273 weight = hweight64((u64)bits);
274 /* only adjust the contended total count if needed */
276 atomic_sub(weight, &wss->total_count);
281 * Insert the given address into the working set array.
283 static void wss_insert(struct rvt_wss *wss, void *address)
285 u32 page = ((unsigned long)address >> PAGE_SHIFT) & wss->pages_mask;
286 u32 entry = page / BITS_PER_LONG; /* assumes this ends up a shift */
287 u32 nr = page & (BITS_PER_LONG - 1);
289 if (!test_and_set_bit(nr, &wss->entries[entry]))
290 atomic_inc(&wss->total_count);
292 wss_advance_clean_counter(wss);
296 * Is the working set larger than the threshold?
298 static inline bool wss_exceeds_threshold(struct rvt_wss *wss)
300 return atomic_read(&wss->total_count) >= wss->threshold;
303 static void get_map_page(struct rvt_qpn_table *qpt,
304 struct rvt_qpn_map *map)
306 unsigned long page = get_zeroed_page(GFP_KERNEL);
309 * Free the page if someone raced with us installing it.
312 spin_lock(&qpt->lock);
316 map->page = (void *)page;
317 spin_unlock(&qpt->lock);
321 * init_qpn_table - initialize the QP number table for a device
322 * @qpt: the QPN table
324 static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
327 struct rvt_qpn_map *map;
330 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
333 spin_lock_init(&qpt->lock);
335 qpt->last = rdi->dparms.qpn_start;
336 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
339 * Drivers may want some QPs beyond what we need for verbs let them use
340 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
341 * for those. The reserved range must be *after* the range which verbs
345 /* Figure out number of bit maps needed before reserved range */
346 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
348 /* This should always be zero */
349 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
351 /* Starting with the first reserved bit map */
352 map = &qpt->map[qpt->nmaps];
354 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
355 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
356 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
358 get_map_page(qpt, map);
364 set_bit(offset, map->page);
366 if (offset == RVT_BITS_PER_PAGE) {
377 * free_qpn_table - free the QP number table for a device
378 * @qpt: the QPN table
380 static void free_qpn_table(struct rvt_qpn_table *qpt)
384 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
385 free_page((unsigned long)qpt->map[i].page);
389 * rvt_driver_qp_init - Init driver qp resources
390 * @rdi: rvt dev strucutre
392 * Return: 0 on success
394 int rvt_driver_qp_init(struct rvt_dev_info *rdi)
399 if (!rdi->dparms.qp_table_size)
403 * If driver is not doing any QP allocation then make sure it is
404 * providing the necessary QP functions.
406 if (!rdi->driver_f.free_all_qps ||
407 !rdi->driver_f.qp_priv_alloc ||
408 !rdi->driver_f.qp_priv_free ||
409 !rdi->driver_f.notify_qp_reset ||
410 !rdi->driver_f.notify_restart_rc)
413 /* allocate parent object */
414 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
419 /* allocate hash table */
420 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
421 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
422 rdi->qp_dev->qp_table =
423 kmalloc_array_node(rdi->qp_dev->qp_table_size,
424 sizeof(*rdi->qp_dev->qp_table),
425 GFP_KERNEL, rdi->dparms.node);
426 if (!rdi->qp_dev->qp_table)
429 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
430 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
432 spin_lock_init(&rdi->qp_dev->qpt_lock);
434 /* initialize qpn map */
435 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
438 spin_lock_init(&rdi->n_qps_lock);
443 kfree(rdi->qp_dev->qp_table);
444 free_qpn_table(&rdi->qp_dev->qpn_table);
453 * free_all_qps - check for QPs still in use
454 * @rdi: rvt device info structure
456 * There should not be any QPs still in use.
457 * Free memory for table.
459 static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
463 unsigned n, qp_inuse = 0;
464 spinlock_t *ql; /* work around too long line below */
466 if (rdi->driver_f.free_all_qps)
467 qp_inuse = rdi->driver_f.free_all_qps(rdi);
469 qp_inuse += rvt_mcast_tree_empty(rdi);
474 ql = &rdi->qp_dev->qpt_lock;
475 spin_lock_irqsave(ql, flags);
476 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
477 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
478 lockdep_is_held(ql));
479 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
481 for (; qp; qp = rcu_dereference_protected(qp->next,
482 lockdep_is_held(ql)))
485 spin_unlock_irqrestore(ql, flags);
491 * rvt_qp_exit - clean up qps on device exit
492 * @rdi: rvt dev structure
494 * Check for qp leaks and free resources.
496 void rvt_qp_exit(struct rvt_dev_info *rdi)
498 u32 qps_inuse = rvt_free_all_qps(rdi);
501 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
506 kfree(rdi->qp_dev->qp_table);
507 free_qpn_table(&rdi->qp_dev->qpn_table);
511 static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
512 struct rvt_qpn_map *map, unsigned off)
514 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
518 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
519 * IB_QPT_SMI/IB_QPT_GSI
520 * @rdi: rvt device info structure
521 * @qpt: queue pair number table pointer
522 * @port_num: IB port number, 1 based, comes from core
524 * Return: The queue pair number
526 static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
527 enum ib_qp_type type, u8 port_num)
529 u32 i, offset, max_scan, qpn;
530 struct rvt_qpn_map *map;
533 if (rdi->driver_f.alloc_qpn)
534 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
536 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
539 ret = type == IB_QPT_GSI;
540 n = 1 << (ret + 2 * (port_num - 1));
541 spin_lock(&qpt->lock);
546 spin_unlock(&qpt->lock);
550 qpn = qpt->last + qpt->incr;
551 if (qpn >= RVT_QPN_MAX)
552 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
553 /* offset carries bit 0 */
554 offset = qpn & RVT_BITS_PER_PAGE_MASK;
555 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
556 max_scan = qpt->nmaps - !offset;
558 if (unlikely(!map->page)) {
559 get_map_page(qpt, map);
560 if (unlikely(!map->page))
564 if (!test_and_set_bit(offset, map->page)) {
571 * This qpn might be bogus if offset >= BITS_PER_PAGE.
572 * That is OK. It gets re-assigned below
574 qpn = mk_qpn(qpt, map, offset);
575 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
577 * In order to keep the number of pages allocated to a
578 * minimum, we scan the all existing pages before increasing
579 * the size of the bitmap table.
581 if (++i > max_scan) {
582 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
584 map = &qpt->map[qpt->nmaps++];
585 /* start at incr with current bit 0 */
586 offset = qpt->incr | (offset & 1);
587 } else if (map < &qpt->map[qpt->nmaps]) {
589 /* start at incr with current bit 0 */
590 offset = qpt->incr | (offset & 1);
593 /* wrap to first map page, invert bit 0 */
594 offset = qpt->incr | ((offset & 1) ^ 1);
596 /* there can be no set bits in low-order QoS bits */
597 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
598 qpn = mk_qpn(qpt, map, offset);
608 * rvt_clear_mr_refs - Drop help mr refs
609 * @qp: rvt qp data structure
610 * @clr_sends: If shoudl clear send side or not
612 static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
615 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
617 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
618 rvt_put_ss(&qp->s_rdma_read_sge);
620 rvt_put_ss(&qp->r_sge);
623 while (qp->s_last != qp->s_head) {
624 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
628 if (qp->ibqp.qp_type == IB_QPT_UD ||
629 qp->ibqp.qp_type == IB_QPT_SMI ||
630 qp->ibqp.qp_type == IB_QPT_GSI)
631 atomic_dec(&ibah_to_rvtah(
632 wqe->ud_wr.ah)->refcount);
633 if (++qp->s_last >= qp->s_size)
635 smp_wmb(); /* see qp_set_savail */
638 rvt_put_mr(qp->s_rdma_mr);
639 qp->s_rdma_mr = NULL;
643 for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) {
644 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
646 if (e->rdma_sge.mr) {
647 rvt_put_mr(e->rdma_sge.mr);
648 e->rdma_sge.mr = NULL;
654 * rvt_swqe_has_lkey - return true if lkey is used by swqe
655 * @wqe - the send wqe
658 * Test the swqe for using lkey
660 static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey)
664 for (i = 0; i < wqe->wr.num_sge; i++) {
665 struct rvt_sge *sge = &wqe->sg_list[i];
667 if (rvt_mr_has_lkey(sge->mr, lkey))
674 * rvt_qp_sends_has_lkey - return true is qp sends use lkey
678 static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey)
680 u32 s_last = qp->s_last;
682 while (s_last != qp->s_head) {
683 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last);
685 if (rvt_swqe_has_lkey(wqe, lkey))
688 if (++s_last >= qp->s_size)
692 if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey))
698 * rvt_qp_acks_has_lkey - return true if acks have lkey
702 static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey)
705 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
707 for (i = 0; qp->s_ack_queue && i < rvt_max_atomic(rdi); i++) {
708 struct rvt_ack_entry *e = &qp->s_ack_queue[i];
710 if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey))
717 * rvt_qp_mr_clean - clean up remote ops for lkey
719 * @lkey - the lkey that is being de-registered
721 * This routine checks if the lkey is being used by
724 * If so, the qp is put into an error state to elminate
725 * any references from the qp.
727 void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey)
729 bool lastwqe = false;
731 if (qp->ibqp.qp_type == IB_QPT_SMI ||
732 qp->ibqp.qp_type == IB_QPT_GSI)
733 /* avoid special QPs */
735 spin_lock_irq(&qp->r_lock);
736 spin_lock(&qp->s_hlock);
737 spin_lock(&qp->s_lock);
739 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
742 if (rvt_ss_has_lkey(&qp->r_sge, lkey) ||
743 rvt_qp_sends_has_lkey(qp, lkey) ||
744 rvt_qp_acks_has_lkey(qp, lkey))
745 lastwqe = rvt_error_qp(qp, IB_WC_LOC_PROT_ERR);
747 spin_unlock(&qp->s_lock);
748 spin_unlock(&qp->s_hlock);
749 spin_unlock_irq(&qp->r_lock);
753 ev.device = qp->ibqp.device;
754 ev.element.qp = &qp->ibqp;
755 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
756 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
761 * rvt_remove_qp - remove qp form table
762 * @rdi: rvt dev struct
765 * Remove the QP from the table so it can't be found asynchronously by
766 * the receive routine.
768 static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
770 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
771 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
775 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
777 if (rcu_dereference_protected(rvp->qp[0],
778 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
779 RCU_INIT_POINTER(rvp->qp[0], NULL);
780 } else if (rcu_dereference_protected(rvp->qp[1],
781 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
782 RCU_INIT_POINTER(rvp->qp[1], NULL);
785 struct rvt_qp __rcu **qpp;
788 qpp = &rdi->qp_dev->qp_table[n];
789 for (; (q = rcu_dereference_protected(*qpp,
790 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
793 RCU_INIT_POINTER(*qpp,
794 rcu_dereference_protected(qp->next,
795 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
797 trace_rvt_qpremove(qp, n);
803 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
811 * rvt_init_qp - initialize the QP state to the reset state
812 * @qp: the QP to init or reinit
815 * This function is called from both rvt_create_qp() and
816 * rvt_reset_qp(). The difference is that the reset
817 * patch the necessary locks to protect against concurent
820 static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
821 enum ib_qp_type type)
825 qp->qp_access_flags = 0;
826 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
832 qp->s_sending_psn = 0;
833 qp->s_sending_hpsn = 0;
837 if (type == IB_QPT_RC) {
838 qp->s_state = IB_OPCODE_RC_SEND_LAST;
839 qp->r_state = IB_OPCODE_RC_SEND_LAST;
841 qp->s_state = IB_OPCODE_UC_SEND_LAST;
842 qp->r_state = IB_OPCODE_UC_SEND_LAST;
844 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
855 qp->s_mig_state = IB_MIG_MIGRATED;
856 qp->r_head_ack_queue = 0;
857 qp->s_tail_ack_queue = 0;
858 qp->s_acked_ack_queue = 0;
859 qp->s_num_rd_atomic = 0;
861 qp->r_rq.wq->head = 0;
862 qp->r_rq.wq->tail = 0;
864 qp->r_sge.num_sge = 0;
865 atomic_set(&qp->s_reserved_used, 0);
869 * rvt_reset_qp - initialize the QP state to the reset state
870 * @qp: the QP to reset
873 * r_lock, s_hlock, and s_lock are required to be held by the caller
875 static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
876 enum ib_qp_type type)
877 __must_hold(&qp->s_lock)
878 __must_hold(&qp->s_hlock)
879 __must_hold(&qp->r_lock)
881 lockdep_assert_held(&qp->r_lock);
882 lockdep_assert_held(&qp->s_hlock);
883 lockdep_assert_held(&qp->s_lock);
884 if (qp->state != IB_QPS_RESET) {
885 qp->state = IB_QPS_RESET;
887 /* Let drivers flush their waitlist */
888 rdi->driver_f.flush_qp_waiters(qp);
889 rvt_stop_rc_timers(qp);
890 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
891 spin_unlock(&qp->s_lock);
892 spin_unlock(&qp->s_hlock);
893 spin_unlock_irq(&qp->r_lock);
895 /* Stop the send queue and the retry timer */
896 rdi->driver_f.stop_send_queue(qp);
897 rvt_del_timers_sync(qp);
898 /* Wait for things to stop */
899 rdi->driver_f.quiesce_qp(qp);
901 /* take qp out the hash and wait for it to be unused */
902 rvt_remove_qp(rdi, qp);
904 /* grab the lock b/c it was locked at call time */
905 spin_lock_irq(&qp->r_lock);
906 spin_lock(&qp->s_hlock);
907 spin_lock(&qp->s_lock);
909 rvt_clear_mr_refs(qp, 1);
911 * Let the driver do any tear down or re-init it needs to for
912 * a qp that has been reset
914 rdi->driver_f.notify_qp_reset(qp);
916 rvt_init_qp(rdi, qp, type);
917 lockdep_assert_held(&qp->r_lock);
918 lockdep_assert_held(&qp->s_hlock);
919 lockdep_assert_held(&qp->s_lock);
922 /** rvt_free_qpn - Free a qpn from the bit map
924 * @qpn: queue pair number to free
926 static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
928 struct rvt_qpn_map *map;
930 map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
932 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
936 * rvt_create_qp - create a queue pair for a device
937 * @ibpd: the protection domain who's device we create the queue pair for
938 * @init_attr: the attributes of the queue pair
939 * @udata: user data for libibverbs.so
941 * Queue pair creation is mostly an rvt issue. However, drivers have their own
942 * unique idea of what queue pair numbers mean. For instance there is a reserved
945 * Return: the queue pair on success, otherwise returns an errno.
947 * Called by the ib_create_qp() core verbs function.
949 struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
950 struct ib_qp_init_attr *init_attr,
951 struct ib_udata *udata)
955 struct rvt_swqe *swq = NULL;
958 struct ib_qp *ret = ERR_PTR(-ENOMEM);
959 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
960 struct rvt_ucontext *ucontext = rdma_udata_to_drv_context(
961 udata, struct rvt_ucontext, ibucontext);
966 return ERR_PTR(-EINVAL);
968 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_send_sge ||
969 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
970 init_attr->create_flags)
971 return ERR_PTR(-EINVAL);
973 /* Check receive queue parameters if no SRQ is specified. */
974 if (!init_attr->srq) {
975 if (init_attr->cap.max_recv_sge >
976 rdi->dparms.props.max_recv_sge ||
977 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
978 return ERR_PTR(-EINVAL);
980 if (init_attr->cap.max_send_sge +
981 init_attr->cap.max_send_wr +
982 init_attr->cap.max_recv_sge +
983 init_attr->cap.max_recv_wr == 0)
984 return ERR_PTR(-EINVAL);
987 init_attr->cap.max_send_wr + 1 +
988 rdi->dparms.reserved_operations;
989 switch (init_attr->qp_type) {
992 if (init_attr->port_num == 0 ||
993 init_attr->port_num > ibpd->device->phys_port_cnt)
994 return ERR_PTR(-EINVAL);
999 sz = sizeof(struct rvt_sge) *
1000 init_attr->cap.max_send_sge +
1001 sizeof(struct rvt_swqe);
1002 swq = vzalloc_node(array_size(sz, sqsize), rdi->dparms.node);
1004 return ERR_PTR(-ENOMEM);
1008 if (init_attr->srq) {
1009 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
1011 if (srq->rq.max_sge > 1)
1012 sg_list_sz = sizeof(*qp->r_sg_list) *
1013 (srq->rq.max_sge - 1);
1014 } else if (init_attr->cap.max_recv_sge > 1)
1015 sg_list_sz = sizeof(*qp->r_sg_list) *
1016 (init_attr->cap.max_recv_sge - 1);
1017 qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
1022 RCU_INIT_POINTER(qp->next, NULL);
1023 if (init_attr->qp_type == IB_QPT_RC) {
1025 kcalloc_node(rvt_max_atomic(rdi),
1026 sizeof(*qp->s_ack_queue),
1029 if (!qp->s_ack_queue)
1032 /* initialize timers needed for rc qp */
1033 timer_setup(&qp->s_timer, rvt_rc_timeout, 0);
1034 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
1036 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
1039 * Driver needs to set up it's private QP structure and do any
1040 * initialization that is needed.
1042 priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
1048 qp->timeout_jiffies =
1049 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1051 if (init_attr->srq) {
1054 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
1055 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
1056 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
1057 sizeof(struct rvt_rwqe);
1059 qp->r_rq.wq = vmalloc_user(
1060 sizeof(struct rvt_rwq) +
1061 qp->r_rq.size * sz);
1063 qp->r_rq.wq = vzalloc_node(
1064 sizeof(struct rvt_rwq) +
1068 goto bail_driver_priv;
1072 * ib_create_qp() will initialize qp->ibqp
1073 * except for qp->ibqp.qp_num.
1075 spin_lock_init(&qp->r_lock);
1076 spin_lock_init(&qp->s_hlock);
1077 spin_lock_init(&qp->s_lock);
1078 spin_lock_init(&qp->r_rq.lock);
1079 atomic_set(&qp->refcount, 0);
1080 atomic_set(&qp->local_ops_pending, 0);
1081 init_waitqueue_head(&qp->wait);
1082 INIT_LIST_HEAD(&qp->rspwait);
1083 qp->state = IB_QPS_RESET;
1085 qp->s_size = sqsize;
1086 qp->s_avail = init_attr->cap.max_send_wr;
1087 qp->s_max_sge = init_attr->cap.max_send_sge;
1088 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
1089 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
1091 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
1093 init_attr->port_num);
1098 qp->ibqp.qp_num = err;
1099 qp->port_num = init_attr->port_num;
1100 rvt_init_qp(rdi, qp, init_attr->qp_type);
1101 if (rdi->driver_f.qp_priv_init) {
1102 err = rdi->driver_f.qp_priv_init(rdi, qp, init_attr);
1111 /* Don't support raw QPs */
1112 return ERR_PTR(-EINVAL);
1115 init_attr->cap.max_inline_data = 0;
1118 * Return the address of the RWQ as the offset to mmap.
1119 * See rvt_mmap() for details.
1121 if (udata && udata->outlen >= sizeof(__u64)) {
1125 err = ib_copy_to_udata(udata, &offset,
1132 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
1134 qp->ip = rvt_create_mmap_info(rdi, s,
1135 &ucontext->ibucontext,
1138 ret = ERR_PTR(-ENOMEM);
1142 err = ib_copy_to_udata(udata, &qp->ip->offset,
1143 sizeof(qp->ip->offset));
1149 qp->pid = current->pid;
1152 spin_lock(&rdi->n_qps_lock);
1153 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
1154 spin_unlock(&rdi->n_qps_lock);
1155 ret = ERR_PTR(-ENOMEM);
1159 rdi->n_qps_allocated++;
1161 * Maintain a busy_jiffies variable that will be added to the timeout
1162 * period in mod_retry_timer and add_retry_timer. This busy jiffies
1163 * is scaled by the number of rc qps created for the device to reduce
1164 * the number of timeouts occurring when there is a large number of
1165 * qps. busy_jiffies is incremented every rc qp scaling interval.
1166 * The scaling interval is selected based on extensive performance
1167 * evaluation of targeted workloads.
1169 if (init_attr->qp_type == IB_QPT_RC) {
1171 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1173 spin_unlock(&rdi->n_qps_lock);
1176 spin_lock_irq(&rdi->pending_lock);
1177 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
1178 spin_unlock_irq(&rdi->pending_lock);
1184 * We have our QP and its good, now keep track of what types of opcodes
1185 * can be processed on this QP. We do this by keeping track of what the
1186 * 3 high order bits of the opcode are.
1188 switch (init_attr->qp_type) {
1192 qp->allowed_ops = IB_OPCODE_UD;
1195 qp->allowed_ops = IB_OPCODE_RC;
1198 qp->allowed_ops = IB_OPCODE_UC;
1201 ret = ERR_PTR(-EINVAL);
1209 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1212 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1219 rdi->driver_f.qp_priv_free(rdi, qp);
1222 kfree(qp->s_ack_queue);
1232 * rvt_error_qp - put a QP into the error state
1233 * @qp: the QP to put into the error state
1234 * @err: the receive completion error to signal if a RWQE is active
1236 * Flushes both send and receive work queues.
1238 * Return: true if last WQE event should be generated.
1239 * The QP r_lock and s_lock should be held and interrupts disabled.
1240 * If we are already in error state, just return.
1242 int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
1246 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1248 lockdep_assert_held(&qp->r_lock);
1249 lockdep_assert_held(&qp->s_lock);
1250 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
1253 qp->state = IB_QPS_ERR;
1255 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1256 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1257 del_timer(&qp->s_timer);
1260 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
1261 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
1263 rdi->driver_f.notify_error_qp(qp);
1265 /* Schedule the sending tasklet to drain the send work queue. */
1266 if (READ_ONCE(qp->s_last) != qp->s_head)
1267 rdi->driver_f.schedule_send(qp);
1269 rvt_clear_mr_refs(qp, 0);
1271 memset(&wc, 0, sizeof(wc));
1273 wc.opcode = IB_WC_RECV;
1275 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1276 wc.wr_id = qp->r_wr_id;
1278 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1280 wc.status = IB_WC_WR_FLUSH_ERR;
1287 spin_lock(&qp->r_rq.lock);
1289 /* sanity check pointers before trusting them */
1292 if (head >= qp->r_rq.size)
1295 if (tail >= qp->r_rq.size)
1297 while (tail != head) {
1298 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1299 if (++tail >= qp->r_rq.size)
1301 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1305 spin_unlock(&qp->r_rq.lock);
1306 } else if (qp->ibqp.event_handler) {
1313 EXPORT_SYMBOL(rvt_error_qp);
1316 * Put the QP into the hash table.
1317 * The hash table holds a reference to the QP.
1319 static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1321 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1322 unsigned long flags;
1325 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1327 if (qp->ibqp.qp_num <= 1) {
1328 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1330 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1332 qp->next = rdi->qp_dev->qp_table[n];
1333 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1334 trace_rvt_qpinsert(qp, n);
1337 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1341 * rvt_modify_qp - modify the attributes of a queue pair
1342 * @ibqp: the queue pair who's attributes we're modifying
1343 * @attr: the new attributes
1344 * @attr_mask: the mask of attributes to modify
1345 * @udata: user data for libibverbs.so
1347 * Return: 0 on success, otherwise returns an errno.
1349 int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1350 int attr_mask, struct ib_udata *udata)
1352 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1353 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1354 enum ib_qp_state cur_state, new_state;
1358 int pmtu = 0; /* for gcc warning only */
1361 spin_lock_irq(&qp->r_lock);
1362 spin_lock(&qp->s_hlock);
1363 spin_lock(&qp->s_lock);
1365 cur_state = attr_mask & IB_QP_CUR_STATE ?
1366 attr->cur_qp_state : qp->state;
1367 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1368 opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
1370 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1374 if (rdi->driver_f.check_modify_qp &&
1375 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1378 if (attr_mask & IB_QP_AV) {
1380 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1381 opa_get_mcast_base(OPA_MCAST_NR))
1384 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1385 be16_to_cpu(IB_MULTICAST_LID_BASE))
1389 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1393 if (attr_mask & IB_QP_ALT_PATH) {
1395 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1396 opa_get_mcast_base(OPA_MCAST_NR))
1399 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1400 be16_to_cpu(IB_MULTICAST_LID_BASE))
1404 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1406 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1410 if (attr_mask & IB_QP_PKEY_INDEX)
1411 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1414 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1415 if (attr->min_rnr_timer > 31)
1418 if (attr_mask & IB_QP_PORT)
1419 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1420 qp->ibqp.qp_type == IB_QPT_GSI ||
1421 attr->port_num == 0 ||
1422 attr->port_num > ibqp->device->phys_port_cnt)
1425 if (attr_mask & IB_QP_DEST_QPN)
1426 if (attr->dest_qp_num > RVT_QPN_MASK)
1429 if (attr_mask & IB_QP_RETRY_CNT)
1430 if (attr->retry_cnt > 7)
1433 if (attr_mask & IB_QP_RNR_RETRY)
1434 if (attr->rnr_retry > 7)
1438 * Don't allow invalid path_mtu values. OK to set greater
1439 * than the active mtu (or even the max_cap, if we have tuned
1440 * that to a small mtu. We'll set qp->path_mtu
1441 * to the lesser of requested attribute mtu and active,
1442 * for packetizing messages.
1443 * Note that the QP port has to be set in INIT and MTU in RTR.
1445 if (attr_mask & IB_QP_PATH_MTU) {
1446 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1451 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1452 if (attr->path_mig_state == IB_MIG_REARM) {
1453 if (qp->s_mig_state == IB_MIG_ARMED)
1455 if (new_state != IB_QPS_RTS)
1457 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1458 if (qp->s_mig_state == IB_MIG_REARM)
1460 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1462 if (qp->s_mig_state == IB_MIG_ARMED)
1469 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1470 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1473 switch (new_state) {
1475 if (qp->state != IB_QPS_RESET)
1476 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1480 /* Allow event to re-trigger if QP set to RTR more than once */
1481 qp->r_flags &= ~RVT_R_COMM_EST;
1482 qp->state = new_state;
1486 qp->s_draining = qp->s_last != qp->s_cur;
1487 qp->state = new_state;
1491 if (qp->ibqp.qp_type == IB_QPT_RC)
1493 qp->state = new_state;
1497 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1501 qp->state = new_state;
1505 if (attr_mask & IB_QP_PKEY_INDEX)
1506 qp->s_pkey_index = attr->pkey_index;
1508 if (attr_mask & IB_QP_PORT)
1509 qp->port_num = attr->port_num;
1511 if (attr_mask & IB_QP_DEST_QPN)
1512 qp->remote_qpn = attr->dest_qp_num;
1514 if (attr_mask & IB_QP_SQ_PSN) {
1515 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1516 qp->s_psn = qp->s_next_psn;
1517 qp->s_sending_psn = qp->s_next_psn;
1518 qp->s_last_psn = qp->s_next_psn - 1;
1519 qp->s_sending_hpsn = qp->s_last_psn;
1522 if (attr_mask & IB_QP_RQ_PSN)
1523 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1525 if (attr_mask & IB_QP_ACCESS_FLAGS)
1526 qp->qp_access_flags = attr->qp_access_flags;
1528 if (attr_mask & IB_QP_AV) {
1529 rdma_replace_ah_attr(&qp->remote_ah_attr, &attr->ah_attr);
1530 qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
1531 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1534 if (attr_mask & IB_QP_ALT_PATH) {
1535 rdma_replace_ah_attr(&qp->alt_ah_attr, &attr->alt_ah_attr);
1536 qp->s_alt_pkey_index = attr->alt_pkey_index;
1539 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1540 qp->s_mig_state = attr->path_mig_state;
1542 qp->remote_ah_attr = qp->alt_ah_attr;
1543 qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
1544 qp->s_pkey_index = qp->s_alt_pkey_index;
1548 if (attr_mask & IB_QP_PATH_MTU) {
1549 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1550 qp->log_pmtu = ilog2(qp->pmtu);
1553 if (attr_mask & IB_QP_RETRY_CNT) {
1554 qp->s_retry_cnt = attr->retry_cnt;
1555 qp->s_retry = attr->retry_cnt;
1558 if (attr_mask & IB_QP_RNR_RETRY) {
1559 qp->s_rnr_retry_cnt = attr->rnr_retry;
1560 qp->s_rnr_retry = attr->rnr_retry;
1563 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1564 qp->r_min_rnr_timer = attr->min_rnr_timer;
1566 if (attr_mask & IB_QP_TIMEOUT) {
1567 qp->timeout = attr->timeout;
1568 qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
1571 if (attr_mask & IB_QP_QKEY)
1572 qp->qkey = attr->qkey;
1574 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1575 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1577 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1578 qp->s_max_rd_atomic = attr->max_rd_atomic;
1580 if (rdi->driver_f.modify_qp)
1581 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1583 spin_unlock(&qp->s_lock);
1584 spin_unlock(&qp->s_hlock);
1585 spin_unlock_irq(&qp->r_lock);
1587 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1588 rvt_insert_qp(rdi, qp);
1591 ev.device = qp->ibqp.device;
1592 ev.element.qp = &qp->ibqp;
1593 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1594 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1597 ev.device = qp->ibqp.device;
1598 ev.element.qp = &qp->ibqp;
1599 ev.event = IB_EVENT_PATH_MIG;
1600 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1605 spin_unlock(&qp->s_lock);
1606 spin_unlock(&qp->s_hlock);
1607 spin_unlock_irq(&qp->r_lock);
1612 * rvt_destroy_qp - destroy a queue pair
1613 * @ibqp: the queue pair to destroy
1615 * Note that this can be called while the QP is actively sending or
1618 * Return: 0 on success.
1620 int rvt_destroy_qp(struct ib_qp *ibqp)
1622 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1623 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1625 spin_lock_irq(&qp->r_lock);
1626 spin_lock(&qp->s_hlock);
1627 spin_lock(&qp->s_lock);
1628 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1629 spin_unlock(&qp->s_lock);
1630 spin_unlock(&qp->s_hlock);
1631 spin_unlock_irq(&qp->r_lock);
1633 wait_event(qp->wait, !atomic_read(&qp->refcount));
1634 /* qpn is now available for use again */
1635 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1637 spin_lock(&rdi->n_qps_lock);
1638 rdi->n_qps_allocated--;
1639 if (qp->ibqp.qp_type == IB_QPT_RC) {
1641 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1643 spin_unlock(&rdi->n_qps_lock);
1646 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1649 rdi->driver_f.qp_priv_free(rdi, qp);
1650 kfree(qp->s_ack_queue);
1651 rdma_destroy_ah_attr(&qp->remote_ah_attr);
1652 rdma_destroy_ah_attr(&qp->alt_ah_attr);
1659 * rvt_query_qp - query an ipbq
1660 * @ibqp: IB qp to query
1661 * @attr: attr struct to fill in
1662 * @attr_mask: attr mask ignored
1663 * @init_attr: struct to fill in
1667 int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1668 int attr_mask, struct ib_qp_init_attr *init_attr)
1670 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1671 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1673 attr->qp_state = qp->state;
1674 attr->cur_qp_state = attr->qp_state;
1675 attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
1676 attr->path_mig_state = qp->s_mig_state;
1677 attr->qkey = qp->qkey;
1678 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1679 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1680 attr->dest_qp_num = qp->remote_qpn;
1681 attr->qp_access_flags = qp->qp_access_flags;
1682 attr->cap.max_send_wr = qp->s_size - 1 -
1683 rdi->dparms.reserved_operations;
1684 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1685 attr->cap.max_send_sge = qp->s_max_sge;
1686 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1687 attr->cap.max_inline_data = 0;
1688 attr->ah_attr = qp->remote_ah_attr;
1689 attr->alt_ah_attr = qp->alt_ah_attr;
1690 attr->pkey_index = qp->s_pkey_index;
1691 attr->alt_pkey_index = qp->s_alt_pkey_index;
1692 attr->en_sqd_async_notify = 0;
1693 attr->sq_draining = qp->s_draining;
1694 attr->max_rd_atomic = qp->s_max_rd_atomic;
1695 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1696 attr->min_rnr_timer = qp->r_min_rnr_timer;
1697 attr->port_num = qp->port_num;
1698 attr->timeout = qp->timeout;
1699 attr->retry_cnt = qp->s_retry_cnt;
1700 attr->rnr_retry = qp->s_rnr_retry_cnt;
1701 attr->alt_port_num =
1702 rdma_ah_get_port_num(&qp->alt_ah_attr);
1703 attr->alt_timeout = qp->alt_timeout;
1705 init_attr->event_handler = qp->ibqp.event_handler;
1706 init_attr->qp_context = qp->ibqp.qp_context;
1707 init_attr->send_cq = qp->ibqp.send_cq;
1708 init_attr->recv_cq = qp->ibqp.recv_cq;
1709 init_attr->srq = qp->ibqp.srq;
1710 init_attr->cap = attr->cap;
1711 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1712 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1714 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1715 init_attr->qp_type = qp->ibqp.qp_type;
1716 init_attr->port_num = qp->port_num;
1721 * rvt_post_receive - post a receive on a QP
1722 * @ibqp: the QP to post the receive on
1723 * @wr: the WR to post
1724 * @bad_wr: the first bad WR is put here
1726 * This may be called from interrupt context.
1728 * Return: 0 on success otherwise errno
1730 int rvt_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
1731 const struct ib_recv_wr **bad_wr)
1733 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1734 struct rvt_rwq *wq = qp->r_rq.wq;
1735 unsigned long flags;
1736 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1739 /* Check that state is OK to post receive. */
1740 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1745 for (; wr; wr = wr->next) {
1746 struct rvt_rwqe *wqe;
1750 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1755 spin_lock_irqsave(&qp->r_rq.lock, flags);
1756 next = wq->head + 1;
1757 if (next >= qp->r_rq.size)
1759 if (next == wq->tail) {
1760 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1764 if (unlikely(qp_err_flush)) {
1767 memset(&wc, 0, sizeof(wc));
1769 wc.opcode = IB_WC_RECV;
1770 wc.wr_id = wr->wr_id;
1771 wc.status = IB_WC_WR_FLUSH_ERR;
1772 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1774 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1775 wqe->wr_id = wr->wr_id;
1776 wqe->num_sge = wr->num_sge;
1777 for (i = 0; i < wr->num_sge; i++)
1778 wqe->sg_list[i] = wr->sg_list[i];
1780 * Make sure queue entry is written
1781 * before the head index.
1786 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1792 * rvt_qp_valid_operation - validate post send wr request
1794 * @post-parms - the post send table for the driver
1795 * @wr - the work request
1797 * The routine validates the operation based on the
1798 * validation table an returns the length of the operation
1799 * which can extend beyond the ib_send_bw. Operation
1800 * dependent flags key atomic operation validation.
1802 * There is an exception for UD qps that validates the pd and
1803 * overrides the length to include the additional UD specific
1806 * Returns a negative error or the length of the work request
1807 * for building the swqe.
1809 static inline int rvt_qp_valid_operation(
1811 const struct rvt_operation_params *post_parms,
1812 const struct ib_send_wr *wr)
1816 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1818 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1820 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1821 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1823 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1824 (wr->num_sge == 0 ||
1825 wr->sg_list[0].length < sizeof(u64) ||
1826 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1828 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1829 !qp->s_max_rd_atomic)
1831 len = post_parms[wr->opcode].length;
1833 if (qp->ibqp.qp_type != IB_QPT_UC &&
1834 qp->ibqp.qp_type != IB_QPT_RC) {
1835 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1837 len = sizeof(struct ib_ud_wr);
1843 * rvt_qp_is_avail - determine queue capacity
1845 * @rdi: the rdmavt device
1846 * @reserved_op: is reserved operation
1848 * This assumes the s_hlock is held but the s_last
1849 * qp variable is uncontrolled.
1851 * For non reserved operations, the qp->s_avail
1854 * The return value is zero or a -ENOMEM.
1856 static inline int rvt_qp_is_avail(
1858 struct rvt_dev_info *rdi,
1865 /* see rvt_qp_wqe_unreserve() */
1866 smp_mb__before_atomic();
1867 reserved_used = atomic_read(&qp->s_reserved_used);
1868 if (unlikely(reserved_op)) {
1869 /* see rvt_qp_wqe_unreserve() */
1870 smp_mb__before_atomic();
1871 if (reserved_used >= rdi->dparms.reserved_operations)
1875 /* non-reserved operations */
1876 if (likely(qp->s_avail))
1878 slast = READ_ONCE(qp->s_last);
1879 if (qp->s_head >= slast)
1880 avail = qp->s_size - (qp->s_head - slast);
1882 avail = slast - qp->s_head;
1884 /* see rvt_qp_wqe_unreserve() */
1885 smp_mb__before_atomic();
1886 reserved_used = atomic_read(&qp->s_reserved_used);
1888 (rdi->dparms.reserved_operations - reserved_used);
1889 /* insure we don't assign a negative s_avail */
1890 if ((s32)avail <= 0)
1892 qp->s_avail = avail;
1893 if (WARN_ON(qp->s_avail >
1894 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1896 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1897 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1898 qp->s_head, qp->s_tail, qp->s_cur,
1899 qp->s_acked, qp->s_last);
1904 * rvt_post_one_wr - post one RC, UC, or UD send work request
1905 * @qp: the QP to post on
1906 * @wr: the work request to send
1908 static int rvt_post_one_wr(struct rvt_qp *qp,
1909 const struct ib_send_wr *wr,
1912 struct rvt_swqe *wqe;
1917 struct rvt_lkey_table *rkt;
1919 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1924 int local_ops_delayed = 0;
1926 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1928 /* IB spec says that num_sge == 0 is OK. */
1929 if (unlikely(wr->num_sge > qp->s_max_sge))
1932 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1938 * Local operations include fast register and local invalidate.
1939 * Fast register needs to be processed immediately because the
1940 * registered lkey may be used by following work requests and the
1941 * lkey needs to be valid at the time those requests are posted.
1942 * Local invalidate can be processed immediately if fencing is
1943 * not required and no previous local invalidate ops are pending.
1944 * Signaled local operations that have been processed immediately
1945 * need to have requests with "completion only" flags set posted
1946 * to the send queue in order to generate completions.
1948 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
1949 switch (wr->opcode) {
1951 ret = rvt_fast_reg_mr(qp,
1954 reg_wr(wr)->access);
1955 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1958 case IB_WR_LOCAL_INV:
1959 if ((wr->send_flags & IB_SEND_FENCE) ||
1960 atomic_read(&qp->local_ops_pending)) {
1961 local_ops_delayed = 1;
1963 ret = rvt_invalidate_rkey(
1964 qp, wr->ex.invalidate_rkey);
1965 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1974 reserved_op = rdi->post_parms[wr->opcode].flags &
1975 RVT_OPERATION_USE_RESERVE;
1976 /* check for avail */
1977 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1980 next = qp->s_head + 1;
1981 if (next >= qp->s_size)
1984 rkt = &rdi->lkey_table;
1985 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1986 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1988 /* cplen has length from above */
1989 memcpy(&wqe->wr, wr, cplen);
1994 struct rvt_sge *last_sge = NULL;
1996 acc = wr->opcode >= IB_WR_RDMA_READ ?
1997 IB_ACCESS_LOCAL_WRITE : 0;
1998 for (i = 0; i < wr->num_sge; i++) {
1999 u32 length = wr->sg_list[i].length;
2003 ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
2004 &wr->sg_list[i], acc);
2005 if (unlikely(ret < 0))
2006 goto bail_inval_free;
2007 wqe->length += length;
2009 last_sge = &wqe->sg_list[j];
2012 wqe->wr.num_sge = j;
2016 * Calculate and set SWQE PSN values prior to handing it off
2017 * to the driver's check routine. This give the driver the
2018 * opportunity to adjust PSN values based on internal checks.
2020 log_pmtu = qp->log_pmtu;
2021 if (qp->ibqp.qp_type != IB_QPT_UC &&
2022 qp->ibqp.qp_type != IB_QPT_RC) {
2023 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
2025 log_pmtu = ah->log_pmtu;
2026 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
2029 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
2030 if (local_ops_delayed)
2031 atomic_inc(&qp->local_ops_pending);
2033 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
2038 wqe->ssn = qp->s_ssn++;
2039 wqe->psn = qp->s_next_psn;
2040 wqe->lpsn = wqe->psn +
2042 ((wqe->length - 1) >> log_pmtu) :
2046 /* general part of wqe valid - allow for driver checks */
2047 if (rdi->driver_f.setup_wqe) {
2048 ret = rdi->driver_f.setup_wqe(qp, wqe, call_send);
2050 goto bail_inval_free_ref;
2053 if (!(rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL))
2054 qp->s_next_psn = wqe->lpsn + 1;
2056 if (unlikely(reserved_op)) {
2057 wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
2058 rvt_qp_wqe_reserve(qp, wqe);
2060 wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
2063 trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
2064 smp_wmb(); /* see request builders */
2069 bail_inval_free_ref:
2070 if (qp->ibqp.qp_type != IB_QPT_UC &&
2071 qp->ibqp.qp_type != IB_QPT_RC)
2072 atomic_dec(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
2074 /* release mr holds */
2076 struct rvt_sge *sge = &wqe->sg_list[--j];
2078 rvt_put_mr(sge->mr);
2084 * rvt_post_send - post a send on a QP
2085 * @ibqp: the QP to post the send on
2086 * @wr: the list of work requests to post
2087 * @bad_wr: the first bad WR is put here
2089 * This may be called from interrupt context.
2091 * Return: 0 on success else errno
2093 int rvt_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
2094 const struct ib_send_wr **bad_wr)
2096 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
2097 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2098 unsigned long flags = 0;
2103 spin_lock_irqsave(&qp->s_hlock, flags);
2106 * Ensure QP state is such that we can send. If not bail out early,
2107 * there is no need to do this every time we post a send.
2109 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
2110 spin_unlock_irqrestore(&qp->s_hlock, flags);
2115 * If the send queue is empty, and we only have a single WR then just go
2116 * ahead and kick the send engine into gear. Otherwise we will always
2117 * just schedule the send to happen later.
2119 call_send = qp->s_head == READ_ONCE(qp->s_last) && !wr->next;
2121 for (; wr; wr = wr->next) {
2122 err = rvt_post_one_wr(qp, wr, &call_send);
2123 if (unlikely(err)) {
2130 spin_unlock_irqrestore(&qp->s_hlock, flags);
2133 * Only call do_send if there is exactly one packet, and the
2134 * driver said it was ok.
2136 if (nreq == 1 && call_send)
2137 rdi->driver_f.do_send(qp);
2139 rdi->driver_f.schedule_send_no_lock(qp);
2145 * rvt_post_srq_receive - post a receive on a shared receive queue
2146 * @ibsrq: the SRQ to post the receive on
2147 * @wr: the list of work requests to post
2148 * @bad_wr: A pointer to the first WR to cause a problem is put here
2150 * This may be called from interrupt context.
2152 * Return: 0 on success else errno
2154 int rvt_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
2155 const struct ib_recv_wr **bad_wr)
2157 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
2159 unsigned long flags;
2161 for (; wr; wr = wr->next) {
2162 struct rvt_rwqe *wqe;
2166 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
2171 spin_lock_irqsave(&srq->rq.lock, flags);
2173 next = wq->head + 1;
2174 if (next >= srq->rq.size)
2176 if (next == wq->tail) {
2177 spin_unlock_irqrestore(&srq->rq.lock, flags);
2182 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
2183 wqe->wr_id = wr->wr_id;
2184 wqe->num_sge = wr->num_sge;
2185 for (i = 0; i < wr->num_sge; i++)
2186 wqe->sg_list[i] = wr->sg_list[i];
2187 /* Make sure queue entry is written before the head index. */
2190 spin_unlock_irqrestore(&srq->rq.lock, flags);
2196 * Validate a RWQE and fill in the SGE state.
2199 static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
2203 struct rvt_lkey_table *rkt;
2205 struct rvt_sge_state *ss;
2206 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2208 rkt = &rdi->lkey_table;
2209 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
2211 ss->sg_list = qp->r_sg_list;
2213 for (i = j = 0; i < wqe->num_sge; i++) {
2214 if (wqe->sg_list[i].length == 0)
2217 ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
2218 NULL, &wqe->sg_list[i],
2219 IB_ACCESS_LOCAL_WRITE);
2220 if (unlikely(ret <= 0))
2222 qp->r_len += wqe->sg_list[i].length;
2226 ss->total_len = qp->r_len;
2231 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
2233 rvt_put_mr(sge->mr);
2236 memset(&wc, 0, sizeof(wc));
2237 wc.wr_id = wqe->wr_id;
2238 wc.status = IB_WC_LOC_PROT_ERR;
2239 wc.opcode = IB_WC_RECV;
2241 /* Signal solicited completion event. */
2242 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
2247 * rvt_get_rwqe - copy the next RWQE into the QP's RWQE
2249 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
2251 * Return -1 if there is a local error, 0 if no RWQE is available,
2252 * otherwise return 1.
2254 * Can be called from interrupt level.
2256 int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only)
2258 unsigned long flags;
2261 struct rvt_srq *srq;
2262 struct rvt_rwqe *wqe;
2263 void (*handler)(struct ib_event *, void *);
2268 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
2269 handler = srq->ibsrq.event_handler;
2277 spin_lock_irqsave(&rq->lock, flags);
2278 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
2285 /* Validate tail before using it since it is user writable. */
2286 if (tail >= rq->size)
2288 if (unlikely(tail == wq->head)) {
2292 /* Make sure entry is read after head index is read. */
2294 wqe = rvt_get_rwqe_ptr(rq, tail);
2296 * Even though we update the tail index in memory, the verbs
2297 * consumer is not supposed to post more entries until a
2298 * completion is generated.
2300 if (++tail >= rq->size)
2303 if (!wr_id_only && !init_sge(qp, wqe)) {
2307 qp->r_wr_id = wqe->wr_id;
2310 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
2315 * Validate head pointer value and compute
2316 * the number of remaining WQEs.
2322 n += rq->size - tail;
2325 if (n < srq->limit) {
2329 spin_unlock_irqrestore(&rq->lock, flags);
2330 ev.device = qp->ibqp.device;
2331 ev.element.srq = qp->ibqp.srq;
2332 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
2333 handler(&ev, srq->ibsrq.srq_context);
2338 spin_unlock_irqrestore(&rq->lock, flags);
2342 EXPORT_SYMBOL(rvt_get_rwqe);
2345 * qp_comm_est - handle trap with QP established
2348 void rvt_comm_est(struct rvt_qp *qp)
2350 qp->r_flags |= RVT_R_COMM_EST;
2351 if (qp->ibqp.event_handler) {
2354 ev.device = qp->ibqp.device;
2355 ev.element.qp = &qp->ibqp;
2356 ev.event = IB_EVENT_COMM_EST;
2357 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2360 EXPORT_SYMBOL(rvt_comm_est);
2362 void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
2364 unsigned long flags;
2367 spin_lock_irqsave(&qp->s_lock, flags);
2368 lastwqe = rvt_error_qp(qp, err);
2369 spin_unlock_irqrestore(&qp->s_lock, flags);
2374 ev.device = qp->ibqp.device;
2375 ev.element.qp = &qp->ibqp;
2376 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2377 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2380 EXPORT_SYMBOL(rvt_rc_error);
2383 * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
2384 * @index - the index
2385 * return usec from an index into ib_rvt_rnr_table
2387 unsigned long rvt_rnr_tbl_to_usec(u32 index)
2389 return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
2391 EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
2393 static inline unsigned long rvt_aeth_to_usec(u32 aeth)
2395 return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
2396 IB_AETH_CREDIT_MASK];
2400 * rvt_add_retry_timer_ext - add/start a retry timer
2402 * @shift - timeout shift to wait for multiple packets
2403 * add a retry timer on the QP
2405 void rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift)
2407 struct ib_qp *ibqp = &qp->ibqp;
2408 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2410 lockdep_assert_held(&qp->s_lock);
2411 qp->s_flags |= RVT_S_TIMER;
2412 /* 4.096 usec. * (1 << qp->timeout) */
2413 qp->s_timer.expires = jiffies + rdi->busy_jiffies +
2414 (qp->timeout_jiffies << shift);
2415 add_timer(&qp->s_timer);
2417 EXPORT_SYMBOL(rvt_add_retry_timer_ext);
2420 * rvt_add_rnr_timer - add/start an rnr timer
2422 * @aeth - aeth of RNR timeout, simulated aeth for loopback
2423 * add an rnr timer on the QP
2425 void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
2429 lockdep_assert_held(&qp->s_lock);
2430 qp->s_flags |= RVT_S_WAIT_RNR;
2431 to = rvt_aeth_to_usec(aeth);
2432 trace_rvt_rnrnak_add(qp, to);
2433 hrtimer_start(&qp->s_rnr_timer,
2434 ns_to_ktime(1000 * to), HRTIMER_MODE_REL_PINNED);
2436 EXPORT_SYMBOL(rvt_add_rnr_timer);
2439 * rvt_stop_rc_timers - stop all timers
2441 * stop any pending timers
2443 void rvt_stop_rc_timers(struct rvt_qp *qp)
2445 lockdep_assert_held(&qp->s_lock);
2446 /* Remove QP from all timers */
2447 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2448 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2449 del_timer(&qp->s_timer);
2450 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2453 EXPORT_SYMBOL(rvt_stop_rc_timers);
2456 * rvt_stop_rnr_timer - stop an rnr timer
2459 * stop an rnr timer and return if the timer
2462 static void rvt_stop_rnr_timer(struct rvt_qp *qp)
2464 lockdep_assert_held(&qp->s_lock);
2465 /* Remove QP from rnr timer */
2466 if (qp->s_flags & RVT_S_WAIT_RNR) {
2467 qp->s_flags &= ~RVT_S_WAIT_RNR;
2468 trace_rvt_rnrnak_stop(qp, 0);
2473 * rvt_del_timers_sync - wait for any timeout routines to exit
2476 void rvt_del_timers_sync(struct rvt_qp *qp)
2478 del_timer_sync(&qp->s_timer);
2479 hrtimer_cancel(&qp->s_rnr_timer);
2481 EXPORT_SYMBOL(rvt_del_timers_sync);
2484 * This is called from s_timer for missing responses.
2486 static void rvt_rc_timeout(struct timer_list *t)
2488 struct rvt_qp *qp = from_timer(qp, t, s_timer);
2489 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2490 unsigned long flags;
2492 spin_lock_irqsave(&qp->r_lock, flags);
2493 spin_lock(&qp->s_lock);
2494 if (qp->s_flags & RVT_S_TIMER) {
2495 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
2497 qp->s_flags &= ~RVT_S_TIMER;
2498 rvp->n_rc_timeouts++;
2499 del_timer(&qp->s_timer);
2500 trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
2501 if (rdi->driver_f.notify_restart_rc)
2502 rdi->driver_f.notify_restart_rc(qp,
2505 rdi->driver_f.schedule_send(qp);
2507 spin_unlock(&qp->s_lock);
2508 spin_unlock_irqrestore(&qp->r_lock, flags);
2512 * This is called from s_timer for RNR timeouts.
2514 enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2516 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2517 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2518 unsigned long flags;
2520 spin_lock_irqsave(&qp->s_lock, flags);
2521 rvt_stop_rnr_timer(qp);
2522 trace_rvt_rnrnak_timeout(qp, 0);
2523 rdi->driver_f.schedule_send(qp);
2524 spin_unlock_irqrestore(&qp->s_lock, flags);
2525 return HRTIMER_NORESTART;
2527 EXPORT_SYMBOL(rvt_rc_rnr_retry);
2530 * rvt_qp_iter_init - initial for QP iteration
2534 * This returns an iterator suitable for iterating QPs
2537 * The @cb is a user defined callback and @v is a 64
2538 * bit value passed to and relevant for processing in the
2539 * @cb. An example use case would be to alter QP processing
2540 * based on criteria not part of the rvt_qp.
2542 * Use cases that require memory allocation to succeed
2543 * must preallocate appropriately.
2545 * Return: a pointer to an rvt_qp_iter or NULL
2547 struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
2549 void (*cb)(struct rvt_qp *qp, u64 v))
2551 struct rvt_qp_iter *i;
2553 i = kzalloc(sizeof(*i), GFP_KERNEL);
2558 /* number of special QPs (SMI/GSI) for device */
2559 i->specials = rdi->ibdev.phys_port_cnt * 2;
2565 EXPORT_SYMBOL(rvt_qp_iter_init);
2568 * rvt_qp_iter_next - return the next QP in iter
2569 * @iter - the iterator
2571 * Fine grained QP iterator suitable for use
2572 * with debugfs seq_file mechanisms.
2574 * Updates iter->qp with the current QP when the return
2577 * Return: 0 - iter->qp is valid 1 - no more QPs
2579 int rvt_qp_iter_next(struct rvt_qp_iter *iter)
2584 struct rvt_qp *pqp = iter->qp;
2586 struct rvt_dev_info *rdi = iter->rdi;
2589 * The approach is to consider the special qps
2590 * as additional table entries before the
2591 * real hash table. Since the qp code sets
2592 * the qp->next hash link to NULL, this works just fine.
2594 * iter->specials is 2 * # ports
2596 * n = 0..iter->specials is the special qp indices
2598 * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
2599 * the potential hash bucket entries
2602 for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
2604 qp = rcu_dereference(pqp->next);
2606 if (n < iter->specials) {
2607 struct rvt_ibport *rvp;
2610 pidx = n % rdi->ibdev.phys_port_cnt;
2611 rvp = rdi->ports[pidx];
2612 qp = rcu_dereference(rvp->qp[n & 1]);
2614 qp = rcu_dereference(
2615 rdi->qp_dev->qp_table[
2616 (n - iter->specials)]);
2628 EXPORT_SYMBOL(rvt_qp_iter_next);
2631 * rvt_qp_iter - iterate all QPs
2632 * @rdi - rvt devinfo
2633 * @v - a 64 bit value
2636 * This provides a way for iterating all QPs.
2638 * The @cb is a user defined callback and @v is a 64
2639 * bit value passed to and relevant for processing in the
2640 * cb. An example use case would be to alter QP processing
2641 * based on criteria not part of the rvt_qp.
2643 * The code has an internal iterator to simplify
2644 * non seq_file use cases.
2646 void rvt_qp_iter(struct rvt_dev_info *rdi,
2648 void (*cb)(struct rvt_qp *qp, u64 v))
2651 struct rvt_qp_iter i = {
2653 .specials = rdi->ibdev.phys_port_cnt * 2,
2660 ret = rvt_qp_iter_next(&i);
2671 EXPORT_SYMBOL(rvt_qp_iter);
2674 * This should be called with s_lock held.
2676 void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
2677 enum ib_wc_status status)
2680 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2682 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
2687 trace_rvt_qp_send_completion(qp, wqe, last);
2688 if (++last >= qp->s_size)
2690 trace_rvt_qp_send_completion(qp, wqe, last);
2692 /* See post_send() */
2695 if (qp->ibqp.qp_type == IB_QPT_UD ||
2696 qp->ibqp.qp_type == IB_QPT_SMI ||
2697 qp->ibqp.qp_type == IB_QPT_GSI)
2698 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
2700 rvt_qp_swqe_complete(qp,
2702 rdi->wc_opcode[wqe->wr.opcode],
2705 if (qp->s_acked == old_last)
2707 if (qp->s_cur == old_last)
2709 if (qp->s_tail == old_last)
2711 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
2714 EXPORT_SYMBOL(rvt_send_complete);
2717 * rvt_copy_sge - copy data to SGE memory
2718 * @qp: associated QP
2719 * @ss: the SGE state
2720 * @data: the data to copy
2721 * @length: the length of the data
2722 * @release: boolean to release MR
2723 * @copy_last: do a separate copy of the last 8 bytes
2725 void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,
2726 void *data, u32 length,
2727 bool release, bool copy_last)
2729 struct rvt_sge *sge = &ss->sge;
2731 bool in_last = false;
2732 bool cacheless_copy = false;
2733 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2734 struct rvt_wss *wss = rdi->wss;
2735 unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode;
2737 if (sge_copy_mode == RVT_SGE_COPY_CACHELESS) {
2738 cacheless_copy = length >= PAGE_SIZE;
2739 } else if (sge_copy_mode == RVT_SGE_COPY_ADAPTIVE) {
2740 if (length >= PAGE_SIZE) {
2742 * NOTE: this *assumes*:
2743 * o The first vaddr is the dest.
2744 * o If multiple pages, then vaddr is sequential.
2746 wss_insert(wss, sge->vaddr);
2747 if (length >= (2 * PAGE_SIZE))
2748 wss_insert(wss, (sge->vaddr + PAGE_SIZE));
2750 cacheless_copy = wss_exceeds_threshold(wss);
2752 wss_advance_clean_counter(wss);
2767 u32 len = rvt_get_sge_length(sge, length);
2769 WARN_ON_ONCE(len == 0);
2770 if (unlikely(in_last)) {
2771 /* enforce byte transfer ordering */
2772 for (i = 0; i < len; i++)
2773 ((u8 *)sge->vaddr)[i] = ((u8 *)data)[i];
2774 } else if (cacheless_copy) {
2775 cacheless_memcpy(sge->vaddr, data, len);
2777 memcpy(sge->vaddr, data, len);
2779 rvt_update_sge(ss, len, release);
2791 EXPORT_SYMBOL(rvt_copy_sge);
2793 static enum ib_wc_status loopback_qp_drop(struct rvt_ibport *rvp,
2798 * For RC, the requester would timeout and retry so
2799 * shortcut the timeouts and just signal too many retries.
2801 return sqp->ibqp.qp_type == IB_QPT_RC ?
2802 IB_WC_RETRY_EXC_ERR : IB_WC_SUCCESS;
2806 * ruc_loopback - handle UC and RC loopback requests
2807 * @sqp: the sending QP
2809 * This is called from rvt_do_send() to forward a WQE addressed to the same HFI
2810 * Note that although we are single threaded due to the send engine, we still
2811 * have to protect against post_send(). We don't have to worry about
2812 * receive interrupts since this is a connected protocol and all packets
2813 * will pass through here.
2815 void rvt_ruc_loopback(struct rvt_qp *sqp)
2817 struct rvt_ibport *rvp = NULL;
2818 struct rvt_dev_info *rdi = ib_to_rvt(sqp->ibqp.device);
2820 struct rvt_swqe *wqe;
2821 struct rvt_sge *sge;
2822 unsigned long flags;
2826 enum ib_wc_status send_status;
2829 bool copy_last = false;
2833 rvp = rdi->ports[sqp->port_num - 1];
2836 * Note that we check the responder QP state after
2837 * checking the requester's state.
2840 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), rvp,
2843 spin_lock_irqsave(&sqp->s_lock, flags);
2845 /* Return if we are already busy processing a work request. */
2846 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
2847 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
2850 sqp->s_flags |= RVT_S_BUSY;
2853 if (sqp->s_last == READ_ONCE(sqp->s_head))
2855 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
2857 /* Return if it is not OK to start a new work request. */
2858 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
2859 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
2861 /* We are in the error state, flush the work request. */
2862 send_status = IB_WC_WR_FLUSH_ERR;
2867 * We can rely on the entry not changing without the s_lock
2868 * being held until we update s_last.
2869 * We increment s_cur to indicate s_last is in progress.
2871 if (sqp->s_last == sqp->s_cur) {
2872 if (++sqp->s_cur >= sqp->s_size)
2875 spin_unlock_irqrestore(&sqp->s_lock, flags);
2878 send_status = loopback_qp_drop(rvp, sqp);
2879 goto serr_no_r_lock;
2881 spin_lock_irqsave(&qp->r_lock, flags);
2882 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
2883 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
2884 send_status = loopback_qp_drop(rvp, sqp);
2888 memset(&wc, 0, sizeof(wc));
2889 send_status = IB_WC_SUCCESS;
2892 sqp->s_sge.sge = wqe->sg_list[0];
2893 sqp->s_sge.sg_list = wqe->sg_list + 1;
2894 sqp->s_sge.num_sge = wqe->wr.num_sge;
2895 sqp->s_len = wqe->length;
2896 switch (wqe->wr.opcode) {
2900 case IB_WR_LOCAL_INV:
2901 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
2902 if (rvt_invalidate_rkey(sqp,
2903 wqe->wr.ex.invalidate_rkey))
2904 send_status = IB_WC_LOC_PROT_ERR;
2909 case IB_WR_SEND_WITH_INV:
2910 case IB_WR_SEND_WITH_IMM:
2912 ret = rvt_get_rwqe(qp, false);
2917 if (wqe->length > qp->r_len)
2919 switch (wqe->wr.opcode) {
2920 case IB_WR_SEND_WITH_INV:
2921 if (!rvt_invalidate_rkey(qp,
2922 wqe->wr.ex.invalidate_rkey)) {
2923 wc.wc_flags = IB_WC_WITH_INVALIDATE;
2924 wc.ex.invalidate_rkey =
2925 wqe->wr.ex.invalidate_rkey;
2928 case IB_WR_SEND_WITH_IMM:
2929 wc.wc_flags = IB_WC_WITH_IMM;
2930 wc.ex.imm_data = wqe->wr.ex.imm_data;
2937 case IB_WR_RDMA_WRITE_WITH_IMM:
2938 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2940 wc.wc_flags = IB_WC_WITH_IMM;
2941 wc.ex.imm_data = wqe->wr.ex.imm_data;
2942 ret = rvt_get_rwqe(qp, true);
2947 /* skip copy_last set and qp_access_flags recheck */
2949 case IB_WR_RDMA_WRITE:
2950 copy_last = rvt_is_user_qp(qp);
2951 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2954 if (wqe->length == 0)
2956 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
2957 wqe->rdma_wr.remote_addr,
2959 IB_ACCESS_REMOTE_WRITE)))
2961 qp->r_sge.sg_list = NULL;
2962 qp->r_sge.num_sge = 1;
2963 qp->r_sge.total_len = wqe->length;
2966 case IB_WR_RDMA_READ:
2967 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2969 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
2970 wqe->rdma_wr.remote_addr,
2972 IB_ACCESS_REMOTE_READ)))
2975 sqp->s_sge.sg_list = NULL;
2976 sqp->s_sge.num_sge = 1;
2977 qp->r_sge.sge = wqe->sg_list[0];
2978 qp->r_sge.sg_list = wqe->sg_list + 1;
2979 qp->r_sge.num_sge = wqe->wr.num_sge;
2980 qp->r_sge.total_len = wqe->length;
2983 case IB_WR_ATOMIC_CMP_AND_SWP:
2984 case IB_WR_ATOMIC_FETCH_AND_ADD:
2985 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2987 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2988 wqe->atomic_wr.remote_addr,
2989 wqe->atomic_wr.rkey,
2990 IB_ACCESS_REMOTE_ATOMIC)))
2992 /* Perform atomic OP and save result. */
2993 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
2994 sdata = wqe->atomic_wr.compare_add;
2995 *(u64 *)sqp->s_sge.sge.vaddr =
2996 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
2997 (u64)atomic64_add_return(sdata, maddr) - sdata :
2998 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
2999 sdata, wqe->atomic_wr.swap);
3000 rvt_put_mr(qp->r_sge.sge.mr);
3001 qp->r_sge.num_sge = 0;
3005 send_status = IB_WC_LOC_QP_OP_ERR;
3009 sge = &sqp->s_sge.sge;
3010 while (sqp->s_len) {
3011 u32 len = rvt_get_sge_length(sge, sqp->s_len);
3013 WARN_ON_ONCE(len == 0);
3014 rvt_copy_sge(qp, &qp->r_sge, sge->vaddr,
3015 len, release, copy_last);
3016 rvt_update_sge(&sqp->s_sge, len, !release);
3020 rvt_put_ss(&qp->r_sge);
3022 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
3025 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
3026 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
3028 wc.opcode = IB_WC_RECV;
3029 wc.wr_id = qp->r_wr_id;
3030 wc.status = IB_WC_SUCCESS;
3031 wc.byte_len = wqe->length;
3033 wc.src_qp = qp->remote_qpn;
3034 wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
3035 wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
3037 /* Signal completion event if the solicited bit is set. */
3038 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
3039 wqe->wr.send_flags & IB_SEND_SOLICITED);
3042 spin_unlock_irqrestore(&qp->r_lock, flags);
3043 spin_lock_irqsave(&sqp->s_lock, flags);
3046 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
3047 rvt_send_complete(sqp, wqe, send_status);
3049 atomic_dec(&sqp->local_ops_pending);
3055 /* Handle RNR NAK */
3056 if (qp->ibqp.qp_type == IB_QPT_UC)
3060 * Note: we don't need the s_lock held since the BUSY flag
3061 * makes this single threaded.
3063 if (sqp->s_rnr_retry == 0) {
3064 send_status = IB_WC_RNR_RETRY_EXC_ERR;
3067 if (sqp->s_rnr_retry_cnt < 7)
3069 spin_unlock_irqrestore(&qp->r_lock, flags);
3070 spin_lock_irqsave(&sqp->s_lock, flags);
3071 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
3073 rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
3074 IB_AETH_CREDIT_SHIFT);
3078 send_status = IB_WC_REM_OP_ERR;
3079 wc.status = IB_WC_LOC_QP_OP_ERR;
3084 sqp->ibqp.qp_type == IB_QPT_RC ?
3085 IB_WC_REM_INV_REQ_ERR :
3087 wc.status = IB_WC_LOC_QP_OP_ERR;
3091 send_status = IB_WC_REM_ACCESS_ERR;
3092 wc.status = IB_WC_LOC_PROT_ERR;
3094 /* responder goes to error state */
3095 rvt_rc_error(qp, wc.status);
3098 spin_unlock_irqrestore(&qp->r_lock, flags);
3100 spin_lock_irqsave(&sqp->s_lock, flags);
3101 rvt_send_complete(sqp, wqe, send_status);
3102 if (sqp->ibqp.qp_type == IB_QPT_RC) {
3103 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
3105 sqp->s_flags &= ~RVT_S_BUSY;
3106 spin_unlock_irqrestore(&sqp->s_lock, flags);
3110 ev.device = sqp->ibqp.device;
3111 ev.element.qp = &sqp->ibqp;
3112 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
3113 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
3118 sqp->s_flags &= ~RVT_S_BUSY;
3120 spin_unlock_irqrestore(&sqp->s_lock, flags);
3124 EXPORT_SYMBOL(rvt_ruc_loopback);