blob: 367c78618019b691cc1a0d9bfb5dab25d60f4197 [file] [log] [blame]
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001/*
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002 * Copyright(c) 2016, 2017 Intel Corporation.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08003 *
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.
6 *
7 * GPL LICENSE SUMMARY
8 *
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.
12 *
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.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
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
29 * distribution.
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.
33 *
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.
45 *
46 */
47
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080048#include <linux/hash.h>
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080049#include <linux/bitops.h>
50#include <linux/lockdep.h>
Dennis Dalessandro515667f2016-01-22 12:50:17 -080051#include <linux/vmalloc.h>
52#include <linux/slab.h>
53#include <rdma/ib_verbs.h>
Don Hiatt832666c2017-02-08 05:28:25 -080054#include <rdma/ib_hdrs.h>
Don Hiatt13c19222017-08-04 13:53:51 -070055#include <rdma/opa_addr.h>
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080056#include "qp.h"
Dennis Dalessandro515667f2016-01-22 12:50:17 -080057#include "vt.h"
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080058#include "trace.h"
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080059
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -080060static void rvt_rc_timeout(unsigned long arg);
61
62/*
63 * Convert the AETH RNR timeout code into the number of microseconds.
64 */
65static const u32 ib_rvt_rnr_table[32] = {
66 655360, /* 00: 655.36 */
67 10, /* 01: .01 */
68 20, /* 02 .02 */
69 30, /* 03: .03 */
70 40, /* 04: .04 */
71 60, /* 05: .06 */
72 80, /* 06: .08 */
73 120, /* 07: .12 */
74 160, /* 08: .16 */
75 240, /* 09: .24 */
76 320, /* 0A: .32 */
77 480, /* 0B: .48 */
78 640, /* 0C: .64 */
79 960, /* 0D: .96 */
80 1280, /* 0E: 1.28 */
81 1920, /* 0F: 1.92 */
82 2560, /* 10: 2.56 */
83 3840, /* 11: 3.84 */
84 5120, /* 12: 5.12 */
85 7680, /* 13: 7.68 */
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 */
98};
99
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800100/*
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.
104 */
105const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
106 [IB_QPS_RESET] = 0,
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,
118};
119EXPORT_SYMBOL(ib_rvt_state_ops);
120
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800121static void get_map_page(struct rvt_qpn_table *qpt,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300122 struct rvt_qpn_map *map)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800123{
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300124 unsigned long page = get_zeroed_page(GFP_KERNEL);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800125
126 /*
127 * Free the page if someone raced with us installing it.
128 */
129
130 spin_lock(&qpt->lock);
131 if (map->page)
132 free_page(page);
133 else
134 map->page = (void *)page;
135 spin_unlock(&qpt->lock);
136}
137
138/**
139 * init_qpn_table - initialize the QP number table for a device
140 * @qpt: the QPN table
141 */
142static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
143{
144 u32 offset, i;
145 struct rvt_qpn_map *map;
146 int ret = 0;
147
Harish Chegondifef2efd2016-01-22 12:50:30 -0800148 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800149 return -EINVAL;
150
151 spin_lock_init(&qpt->lock);
152
153 qpt->last = rdi->dparms.qpn_start;
154 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
155
156 /*
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
160 * will pick from.
161 */
162
163 /* Figure out number of bit maps needed before reserved range */
164 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
165
166 /* This should always be zero */
167 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
168
169 /* Starting with the first reserved bit map */
170 map = &qpt->map[qpt->nmaps];
171
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);
Harish Chegondifef2efd2016-01-22 12:50:30 -0800174 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800175 if (!map->page) {
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300176 get_map_page(qpt, map);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800177 if (!map->page) {
178 ret = -ENOMEM;
179 break;
180 }
181 }
182 set_bit(offset, map->page);
183 offset++;
184 if (offset == RVT_BITS_PER_PAGE) {
185 /* next page */
186 qpt->nmaps++;
187 map++;
188 offset = 0;
189 }
190 }
191 return ret;
192}
193
194/**
195 * free_qpn_table - free the QP number table for a device
196 * @qpt: the QPN table
197 */
198static void free_qpn_table(struct rvt_qpn_table *qpt)
199{
200 int i;
201
202 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
203 free_page((unsigned long)qpt->map[i].page);
204}
205
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800206/**
207 * rvt_driver_qp_init - Init driver qp resources
208 * @rdi: rvt dev strucutre
209 *
210 * Return: 0 on success
211 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800212int rvt_driver_qp_init(struct rvt_dev_info *rdi)
213{
214 int i;
215 int ret = -ENOMEM;
216
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800217 if (!rdi->dparms.qp_table_size)
218 return -EINVAL;
219
220 /*
221 * If driver is not doing any QP allocation then make sure it is
222 * providing the necessary QP functions.
223 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800224 if (!rdi->driver_f.free_all_qps ||
225 !rdi->driver_f.qp_priv_alloc ||
226 !rdi->driver_f.qp_priv_free ||
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800227 !rdi->driver_f.notify_qp_reset ||
228 !rdi->driver_f.notify_restart_rc)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800229 return -EINVAL;
230
231 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800232 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
233 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800234 if (!rdi->qp_dev)
235 return -ENOMEM;
236
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 =
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800241 kmalloc_node(rdi->qp_dev->qp_table_size *
242 sizeof(*rdi->qp_dev->qp_table),
243 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800244 if (!rdi->qp_dev->qp_table)
245 goto no_qp_table;
246
247 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
248 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
249
250 spin_lock_init(&rdi->qp_dev->qpt_lock);
251
252 /* initialize qpn map */
253 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
254 goto fail_table;
255
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800256 spin_lock_init(&rdi->n_qps_lock);
257
258 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800259
260fail_table:
261 kfree(rdi->qp_dev->qp_table);
262 free_qpn_table(&rdi->qp_dev->qpn_table);
263
264no_qp_table:
265 kfree(rdi->qp_dev);
266
267 return ret;
268}
269
270/**
271 * free_all_qps - check for QPs still in use
272 * @qpt: the QP table to empty
273 *
274 * There should not be any QPs still in use.
275 * Free memory for table.
276 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800277static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800278{
279 unsigned long flags;
280 struct rvt_qp *qp;
281 unsigned n, qp_inuse = 0;
282 spinlock_t *ql; /* work around too long line below */
283
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800284 if (rdi->driver_f.free_all_qps)
285 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800286
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800287 qp_inuse += rvt_mcast_tree_empty(rdi);
288
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800289 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800290 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800291
292 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800293 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800294 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);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800298
299 for (; qp; qp = rcu_dereference_protected(qp->next,
300 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800301 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800302 }
303 spin_unlock_irqrestore(ql, flags);
304 synchronize_rcu();
305 return qp_inuse;
306}
307
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800308/**
309 * rvt_qp_exit - clean up qps on device exit
310 * @rdi: rvt dev structure
311 *
312 * Check for qp leaks and free resources.
313 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800314void rvt_qp_exit(struct rvt_dev_info *rdi)
315{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800316 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800317
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800318 if (qps_inuse)
319 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
320 qps_inuse);
321 if (!rdi->qp_dev)
322 return;
323
324 kfree(rdi->qp_dev->qp_table);
325 free_qpn_table(&rdi->qp_dev->qpn_table);
326 kfree(rdi->qp_dev);
327}
328
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800329static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
330 struct rvt_qpn_map *map, unsigned off)
331{
332 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
333}
334
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800335/**
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
341 *
342 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800343 */
344static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300345 enum ib_qp_type type, u8 port_num)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800346{
347 u32 i, offset, max_scan, qpn;
348 struct rvt_qpn_map *map;
349 u32 ret;
350
351 if (rdi->driver_f.alloc_qpn)
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300352 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800353
354 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
355 unsigned n;
356
357 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800358 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800359 spin_lock(&qpt->lock);
360 if (qpt->flags & n)
361 ret = -EINVAL;
362 else
363 qpt->flags |= n;
364 spin_unlock(&qpt->lock);
365 goto bail;
366 }
367
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;
375 for (i = 0;;) {
376 if (unlikely(!map->page)) {
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300377 get_map_page(qpt, map);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800378 if (unlikely(!map->page))
379 break;
380 }
381 do {
382 if (!test_and_set_bit(offset, map->page)) {
383 qpt->last = qpn;
384 ret = qpn;
385 goto bail;
386 }
387 offset += qpt->incr;
388 /*
389 * This qpn might be bogus if offset >= BITS_PER_PAGE.
390 * That is OK. It gets re-assigned below
391 */
392 qpn = mk_qpn(qpt, map, offset);
393 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
394 /*
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.
398 */
399 if (++i > max_scan) {
400 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
401 break;
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]) {
406 ++map;
407 /* start at incr with current bit 0 */
408 offset = qpt->incr | (offset & 1);
409 } else {
410 map = &qpt->map[0];
411 /* wrap to first map page, invert bit 0 */
412 offset = qpt->incr | ((offset & 1) ^ 1);
413 }
Brian Welty501edc422016-06-09 07:51:20 -0700414 /* there can be no set bits in low-order QoS bits */
415 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800416 qpn = mk_qpn(qpt, map, offset);
417 }
418
419 ret = -ENOMEM;
420
421bail:
422 return ret;
423}
424
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800425/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800426 * rvt_clear_mr_refs - Drop help mr refs
427 * @qp: rvt qp data structure
428 * @clr_sends: If shoudl clear send side or not
429 */
430static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
431{
432 unsigned n;
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700433 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800434
435 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
436 rvt_put_ss(&qp->s_rdma_read_sge);
437
438 rvt_put_ss(&qp->r_sge);
439
440 if (clr_sends) {
441 while (qp->s_last != qp->s_head) {
442 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800443
Mike Marciniszyn3aaee8a2017-08-21 18:26:14 -0700444 rvt_put_swqe(wqe);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800445
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800446 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)
452 qp->s_last = 0;
453 smp_wmb(); /* see qp_set_savail */
454 }
455 if (qp->s_rdma_mr) {
456 rvt_put_mr(qp->s_rdma_mr);
457 qp->s_rdma_mr = NULL;
458 }
459 }
460
Mike Marciniszyn0208da92017-08-28 11:24:10 -0700461 for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800462 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
463
Ira Weinyfe508272016-07-27 21:07:36 -0400464 if (e->rdma_sge.mr) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800465 rvt_put_mr(e->rdma_sge.mr);
466 e->rdma_sge.mr = NULL;
467 }
468 }
469}
470
471/**
Mike Marciniszyn0208da92017-08-28 11:24:10 -0700472 * rvt_swqe_has_lkey - return true if lkey is used by swqe
473 * @wqe - the send wqe
474 * @lkey - the lkey
475 *
476 * Test the swqe for using lkey
477 */
478static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey)
479{
480 int i;
481
482 for (i = 0; i < wqe->wr.num_sge; i++) {
483 struct rvt_sge *sge = &wqe->sg_list[i];
484
485 if (rvt_mr_has_lkey(sge->mr, lkey))
486 return true;
487 }
488 return false;
489}
490
491/**
492 * rvt_qp_sends_has_lkey - return true is qp sends use lkey
493 * @qp - the rvt_qp
494 * @lkey - the lkey
495 */
496static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey)
497{
498 u32 s_last = qp->s_last;
499
500 while (s_last != qp->s_head) {
501 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last);
502
503 if (rvt_swqe_has_lkey(wqe, lkey))
504 return true;
505
506 if (++s_last >= qp->s_size)
507 s_last = 0;
508 }
509 if (qp->s_rdma_mr)
510 if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey))
511 return true;
512 return false;
513}
514
515/**
516 * rvt_qp_acks_has_lkey - return true if acks have lkey
517 * @qp - the qp
518 * @lkey - the lkey
519 */
520static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey)
521{
522 int i;
523 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
524
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];
527
528 if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey))
529 return true;
530 }
531 return false;
532}
533
534/*
535 * rvt_qp_mr_clean - clean up remote ops for lkey
536 * @qp - the qp
537 * @lkey - the lkey that is being de-registered
538 *
539 * This routine checks if the lkey is being used by
540 * the qp.
541 *
542 * If so, the qp is put into an error state to elminate
543 * any references from the qp.
544 */
545void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey)
546{
547 bool lastwqe = false;
548
549 if (qp->ibqp.qp_type == IB_QPT_SMI ||
550 qp->ibqp.qp_type == IB_QPT_GSI)
551 /* avoid special QPs */
552 return;
553 spin_lock_irq(&qp->r_lock);
554 spin_lock(&qp->s_hlock);
555 spin_lock(&qp->s_lock);
556
557 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
558 goto check_lwqe;
559
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);
564check_lwqe:
565 spin_unlock(&qp->s_lock);
566 spin_unlock(&qp->s_hlock);
567 spin_unlock_irq(&qp->r_lock);
568 if (lastwqe) {
569 struct ib_event ev;
570
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);
575 }
576}
577
578/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800579 * rvt_remove_qp - remove qp form table
580 * @rdi: rvt dev struct
581 * @qp: qp to remove
582 *
583 * Remove the QP from the table so it can't be found asynchronously by
584 * the receive routine.
585 */
586static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
587{
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);
590 unsigned long flags;
591 int removed = 1;
592
593 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
594
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);
601 } else {
602 struct rvt_qp *q;
603 struct rvt_qp __rcu **qpp;
604
605 removed = 0;
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;
609 qpp = &q->next) {
610 if (q == qp) {
611 RCU_INIT_POINTER(*qpp,
612 rcu_dereference_protected(qp->next,
613 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
614 removed = 1;
615 trace_rvt_qpremove(qp, n);
616 break;
617 }
618 }
619 }
620
621 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
622 if (removed) {
623 synchronize_rcu();
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -0700624 rvt_put_qp(qp);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800625 }
626}
627
628/**
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700629 * rvt_init_qp - initialize the QP state to the reset state
630 * @qp: the QP to init or reinit
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800631 * @type: the QP type
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700632 *
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
636 * access.
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800637 */
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700638static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
639 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800640{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800641 qp->remote_qpn = 0;
642 qp->qkey = 0;
643 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800644 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
645 qp->s_hdrwords = 0;
646 qp->s_wqe = NULL;
647 qp->s_draining = 0;
648 qp->s_next_psn = 0;
649 qp->s_last_psn = 0;
650 qp->s_sending_psn = 0;
651 qp->s_sending_hpsn = 0;
652 qp->s_psn = 0;
653 qp->r_psn = 0;
654 qp->r_msn = 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;
658 } else {
659 qp->s_state = IB_OPCODE_UC_SEND_LAST;
660 qp->r_state = IB_OPCODE_UC_SEND_LAST;
661 }
662 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
663 qp->r_nak_state = 0;
664 qp->r_aflags = 0;
665 qp->r_flags = 0;
666 qp->s_head = 0;
667 qp->s_tail = 0;
668 qp->s_cur = 0;
669 qp->s_acked = 0;
670 qp->s_last = 0;
671 qp->s_ssn = 1;
672 qp->s_lsn = 0;
673 qp->s_mig_state = IB_MIG_MIGRATED;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800674 qp->r_head_ack_queue = 0;
675 qp->s_tail_ack_queue = 0;
676 qp->s_num_rd_atomic = 0;
677 if (qp->r_rq.wq) {
678 qp->r_rq.wq->head = 0;
679 qp->r_rq.wq->tail = 0;
680 }
681 qp->r_sge.num_sge = 0;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700682 atomic_set(&qp->s_reserved_used, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800683}
684
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800685/**
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700686 * rvt_reset_qp - initialize the QP state to the reset state
687 * @qp: the QP to reset
688 * @type: the QP type
689 *
690 * r_lock, s_hlock, and s_lock are required to be held by the caller
691 */
692static 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)
697{
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700698 lockdep_assert_held(&qp->r_lock);
699 lockdep_assert_held(&qp->s_hlock);
700 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700701 if (qp->state != IB_QPS_RESET) {
702 qp->state = IB_QPS_RESET;
703
704 /* Let drivers flush their waitlist */
705 rdi->driver_f.flush_qp_waiters(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800706 rvt_stop_rc_timers(qp);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700707 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);
711
712 /* Stop the send queue and the retry timer */
713 rdi->driver_f.stop_send_queue(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800714 rvt_del_timers_sync(qp);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700715 /* Wait for things to stop */
716 rdi->driver_f.quiesce_qp(qp);
717
718 /* take qp out the hash and wait for it to be unused */
719 rvt_remove_qp(rdi, qp);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700720
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);
725
726 rvt_clear_mr_refs(qp, 1);
727 /*
728 * Let the driver do any tear down or re-init it needs to for
729 * a qp that has been reset
730 */
731 rdi->driver_f.notify_qp_reset(qp);
732 }
733 rvt_init_qp(rdi, qp, type);
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700734 lockdep_assert_held(&qp->r_lock);
735 lockdep_assert_held(&qp->s_hlock);
736 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700737}
738
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -0700739/** rvt_free_qpn - Free a qpn from the bit map
740 * @qpt: QP table
741 * @qpn: queue pair number to free
742 */
743static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
744{
745 struct rvt_qpn_map *map;
746
Dennis Dalessandro6c31e522017-05-29 17:19:21 -0700747 map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -0700748 if (map->page)
749 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
750}
751
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700752/**
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800753 * 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
757 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800758 * 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
760 * range for PSM.
761 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800762 * Return: the queue pair on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800763 *
764 * Called by the ib_create_qp() core verbs function.
765 */
766struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
767 struct ib_qp_init_attr *init_attr,
768 struct ib_udata *udata)
769{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800770 struct rvt_qp *qp;
771 int err;
772 struct rvt_swqe *swq = NULL;
773 size_t sz;
774 size_t sg_list_sz;
775 struct ib_qp *ret = ERR_PTR(-ENOMEM);
776 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
777 void *priv = NULL;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700778 size_t sqsize;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800779
780 if (!rdi)
781 return ERR_PTR(-EINVAL);
782
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 ||
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300785 init_attr->create_flags)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800786 return ERR_PTR(-EINVAL);
787
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);
793
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);
799 }
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700800 sqsize =
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700801 init_attr->cap.max_send_wr + 1 +
802 rdi->dparms.reserved_operations;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800803 switch (init_attr->qp_type) {
804 case IB_QPT_SMI:
805 case IB_QPT_GSI:
806 if (init_attr->port_num == 0 ||
807 init_attr->port_num > ibpd->device->phys_port_cnt)
808 return ERR_PTR(-EINVAL);
809 case IB_QPT_UC:
810 case IB_QPT_RC:
811 case IB_QPT_UD:
812 sz = sizeof(struct rvt_sge) *
813 init_attr->cap.max_send_sge +
814 sizeof(struct rvt_swqe);
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300815 swq = vzalloc_node(sqsize * sz, rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800816 if (!swq)
817 return ERR_PTR(-ENOMEM);
818
819 sz = sizeof(*qp);
820 sg_list_sz = 0;
821 if (init_attr->srq) {
822 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
823
824 if (srq->rq.max_sge > 1)
825 sg_list_sz = sizeof(*qp->r_sg_list) *
826 (srq->rq.max_sge - 1);
827 } else if (init_attr->cap.max_recv_sge > 1)
828 sg_list_sz = sizeof(*qp->r_sg_list) *
829 (init_attr->cap.max_recv_sge - 1);
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300830 qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
831 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800832 if (!qp)
833 goto bail_swq;
834
835 RCU_INIT_POINTER(qp->next, NULL);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700836 if (init_attr->qp_type == IB_QPT_RC) {
837 qp->s_ack_queue =
838 kzalloc_node(
839 sizeof(*qp->s_ack_queue) *
840 rvt_max_atomic(rdi),
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300841 GFP_KERNEL,
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700842 rdi->dparms.node);
843 if (!qp->s_ack_queue)
844 goto bail_qp;
845 }
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800846 /* initialize timers needed for rc qp */
847 setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
848 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
849 HRTIMER_MODE_REL);
850 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800851
852 /*
853 * Driver needs to set up it's private QP structure and do any
854 * initialization that is needed.
855 */
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300856 priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700857 if (IS_ERR(priv)) {
858 ret = priv;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800859 goto bail_qp;
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700860 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800861 qp->priv = priv;
862 qp->timeout_jiffies =
863 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
864 1000UL);
865 if (init_attr->srq) {
866 sz = 0;
867 } else {
868 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
869 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
870 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
871 sizeof(struct rvt_rwqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800872 if (udata)
873 qp->r_rq.wq = vmalloc_user(
874 sizeof(struct rvt_rwq) +
875 qp->r_rq.size * sz);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800876 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700877 qp->r_rq.wq = vzalloc_node(
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800878 sizeof(struct rvt_rwq) +
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800879 qp->r_rq.size * sz,
880 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800881 if (!qp->r_rq.wq)
882 goto bail_driver_priv;
883 }
884
885 /*
886 * ib_create_qp() will initialize qp->ibqp
887 * except for qp->ibqp.qp_num.
888 */
889 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800890 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800891 spin_lock_init(&qp->s_lock);
892 spin_lock_init(&qp->r_rq.lock);
893 atomic_set(&qp->refcount, 0);
Jianxin Xiongd9f87232016-07-25 13:38:25 -0700894 atomic_set(&qp->local_ops_pending, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800895 init_waitqueue_head(&qp->wait);
896 init_timer(&qp->s_timer);
897 qp->s_timer.data = (unsigned long)qp;
898 INIT_LIST_HEAD(&qp->rspwait);
899 qp->state = IB_QPS_RESET;
900 qp->s_wq = swq;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700901 qp->s_size = sqsize;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800902 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800903 qp->s_max_sge = init_attr->cap.max_send_sge;
904 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
905 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
906
907 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
908 init_attr->qp_type,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300909 init_attr->port_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800910 if (err < 0) {
911 ret = ERR_PTR(err);
912 goto bail_rq_wq;
913 }
914 qp->ibqp.qp_num = err;
915 qp->port_num = init_attr->port_num;
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700916 rvt_init_qp(rdi, qp, init_attr->qp_type);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800917 break;
918
919 default:
920 /* Don't support raw QPs */
921 return ERR_PTR(-EINVAL);
922 }
923
924 init_attr->cap.max_inline_data = 0;
925
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800926 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800927 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800928 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800929 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800930 if (udata && udata->outlen >= sizeof(__u64)) {
931 if (!qp->r_rq.wq) {
932 __u64 offset = 0;
933
934 err = ib_copy_to_udata(udata, &offset,
935 sizeof(offset));
936 if (err) {
937 ret = ERR_PTR(err);
938 goto bail_qpn;
939 }
940 } else {
941 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
942
943 qp->ip = rvt_create_mmap_info(rdi, s,
944 ibpd->uobject->context,
945 qp->r_rq.wq);
946 if (!qp->ip) {
947 ret = ERR_PTR(-ENOMEM);
948 goto bail_qpn;
949 }
950
951 err = ib_copy_to_udata(udata, &qp->ip->offset,
952 sizeof(qp->ip->offset));
953 if (err) {
954 ret = ERR_PTR(err);
955 goto bail_ip;
956 }
957 }
Mike Marciniszynef086c02016-03-07 11:35:08 -0800958 qp->pid = current->pid;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800959 }
960
961 spin_lock(&rdi->n_qps_lock);
962 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
963 spin_unlock(&rdi->n_qps_lock);
964 ret = ERR_PTR(-ENOMEM);
965 goto bail_ip;
966 }
967
968 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800969 /*
970 * Maintain a busy_jiffies variable that will be added to the timeout
971 * period in mod_retry_timer and add_retry_timer. This busy jiffies
972 * is scaled by the number of rc qps created for the device to reduce
973 * the number of timeouts occurring when there is a large number of
974 * qps. busy_jiffies is incremented every rc qp scaling interval.
975 * The scaling interval is selected based on extensive performance
976 * evaluation of targeted workloads.
977 */
978 if (init_attr->qp_type == IB_QPT_RC) {
979 rdi->n_rc_qps++;
980 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
981 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800982 spin_unlock(&rdi->n_qps_lock);
983
984 if (qp->ip) {
985 spin_lock_irq(&rdi->pending_lock);
986 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
987 spin_unlock_irq(&rdi->pending_lock);
988 }
989
990 ret = &qp->ibqp;
991
992 /*
993 * We have our QP and its good, now keep track of what types of opcodes
994 * can be processed on this QP. We do this by keeping track of what the
995 * 3 high order bits of the opcode are.
996 */
997 switch (init_attr->qp_type) {
998 case IB_QPT_SMI:
999 case IB_QPT_GSI:
1000 case IB_QPT_UD:
Mike Marciniszynb218f782016-04-12 11:29:20 -07001001 qp->allowed_ops = IB_OPCODE_UD;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001002 break;
1003 case IB_QPT_RC:
Mike Marciniszynb218f782016-04-12 11:29:20 -07001004 qp->allowed_ops = IB_OPCODE_RC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001005 break;
1006 case IB_QPT_UC:
Mike Marciniszynb218f782016-04-12 11:29:20 -07001007 qp->allowed_ops = IB_OPCODE_UC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001008 break;
1009 default:
1010 ret = ERR_PTR(-EINVAL);
1011 goto bail_ip;
1012 }
1013
1014 return ret;
1015
1016bail_ip:
Jim Foraker22dccc52016-11-01 13:44:12 -07001017 if (qp->ip)
1018 kref_put(&qp->ip->ref, rvt_release_mmap_info);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001019
1020bail_qpn:
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -07001021 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001022
1023bail_rq_wq:
Mike Marciniszyn56c8ca52016-08-16 13:26:29 -07001024 if (!qp->ip)
1025 vfree(qp->r_rq.wq);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001026
1027bail_driver_priv:
1028 rdi->driver_f.qp_priv_free(rdi, qp);
1029
1030bail_qp:
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001031 kfree(qp->s_ack_queue);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001032 kfree(qp);
1033
1034bail_swq:
1035 vfree(swq);
1036
1037 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001038}
1039
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001040/**
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001041 * rvt_error_qp - put a QP into the error state
1042 * @qp: the QP to put into the error state
1043 * @err: the receive completion error to signal if a RWQE is active
1044 *
1045 * Flushes both send and receive work queues.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001046 *
1047 * Return: true if last WQE event should be generated.
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001048 * The QP r_lock and s_lock should be held and interrupts disabled.
1049 * If we are already in error state, just return.
1050 */
1051int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
1052{
1053 struct ib_wc wc;
1054 int ret = 0;
1055 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1056
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001057 lockdep_assert_held(&qp->r_lock);
1058 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001059 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
1060 goto bail;
1061
1062 qp->state = IB_QPS_ERR;
1063
1064 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1065 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1066 del_timer(&qp->s_timer);
1067 }
1068
1069 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
1070 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
1071
1072 rdi->driver_f.notify_error_qp(qp);
1073
1074 /* Schedule the sending tasklet to drain the send work queue. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001075 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001076 rdi->driver_f.schedule_send(qp);
1077
1078 rvt_clear_mr_refs(qp, 0);
1079
1080 memset(&wc, 0, sizeof(wc));
1081 wc.qp = &qp->ibqp;
1082 wc.opcode = IB_WC_RECV;
1083
1084 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1085 wc.wr_id = qp->r_wr_id;
1086 wc.status = err;
1087 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1088 }
1089 wc.status = IB_WC_WR_FLUSH_ERR;
1090
1091 if (qp->r_rq.wq) {
1092 struct rvt_rwq *wq;
1093 u32 head;
1094 u32 tail;
1095
1096 spin_lock(&qp->r_rq.lock);
1097
1098 /* sanity check pointers before trusting them */
1099 wq = qp->r_rq.wq;
1100 head = wq->head;
1101 if (head >= qp->r_rq.size)
1102 head = 0;
1103 tail = wq->tail;
1104 if (tail >= qp->r_rq.size)
1105 tail = 0;
1106 while (tail != head) {
1107 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1108 if (++tail >= qp->r_rq.size)
1109 tail = 0;
1110 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1111 }
1112 wq->tail = tail;
1113
1114 spin_unlock(&qp->r_rq.lock);
1115 } else if (qp->ibqp.event_handler) {
1116 ret = 1;
1117 }
1118
1119bail:
1120 return ret;
1121}
1122EXPORT_SYMBOL(rvt_error_qp);
1123
1124/*
1125 * Put the QP into the hash table.
1126 * The hash table holds a reference to the QP.
1127 */
1128static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1129{
1130 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1131 unsigned long flags;
1132
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001133 rvt_get_qp(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001134 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1135
1136 if (qp->ibqp.qp_num <= 1) {
1137 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1138 } else {
1139 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1140
1141 qp->next = rdi->qp_dev->qp_table[n];
1142 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1143 trace_rvt_qpinsert(qp, n);
1144 }
1145
1146 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1147}
1148
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001149/**
Parav Pandit61347fa2016-09-13 19:40:50 +05301150 * rvt_modify_qp - modify the attributes of a queue pair
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001151 * @ibqp: the queue pair who's attributes we're modifying
1152 * @attr: the new attributes
1153 * @attr_mask: the mask of attributes to modify
1154 * @udata: user data for libibverbs.so
1155 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001156 * Return: 0 on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001157 */
1158int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1159 int attr_mask, struct ib_udata *udata)
1160{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001161 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1162 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1163 enum ib_qp_state cur_state, new_state;
1164 struct ib_event ev;
1165 int lastwqe = 0;
1166 int mig = 0;
1167 int pmtu = 0; /* for gcc warning only */
1168 enum rdma_link_layer link;
Don Hiatt13c19222017-08-04 13:53:51 -07001169 int opa_ah;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001170
1171 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1172
1173 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001174 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001175 spin_lock(&qp->s_lock);
1176
1177 cur_state = attr_mask & IB_QP_CUR_STATE ?
1178 attr->cur_qp_state : qp->state;
1179 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
Don Hiatt13c19222017-08-04 13:53:51 -07001180 opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001181
1182 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1183 attr_mask, link))
1184 goto inval;
1185
Ira Weinye85ec332016-01-22 13:04:38 -08001186 if (rdi->driver_f.check_modify_qp &&
1187 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1188 goto inval;
1189
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001190 if (attr_mask & IB_QP_AV) {
Don Hiatt13c19222017-08-04 13:53:51 -07001191 if (opa_ah) {
1192 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1193 opa_get_mcast_base(OPA_MCAST_NR))
1194 goto inval;
1195 } else {
1196 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1197 be16_to_cpu(IB_MULTICAST_LID_BASE))
1198 goto inval;
1199 }
1200
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001201 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1202 goto inval;
1203 }
1204
1205 if (attr_mask & IB_QP_ALT_PATH) {
Don Hiatt13c19222017-08-04 13:53:51 -07001206 if (opa_ah) {
1207 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1208 opa_get_mcast_base(OPA_MCAST_NR))
1209 goto inval;
1210 } else {
1211 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1212 be16_to_cpu(IB_MULTICAST_LID_BASE))
1213 goto inval;
1214 }
1215
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001216 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1217 goto inval;
1218 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1219 goto inval;
1220 }
1221
1222 if (attr_mask & IB_QP_PKEY_INDEX)
1223 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1224 goto inval;
1225
1226 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1227 if (attr->min_rnr_timer > 31)
1228 goto inval;
1229
1230 if (attr_mask & IB_QP_PORT)
1231 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1232 qp->ibqp.qp_type == IB_QPT_GSI ||
1233 attr->port_num == 0 ||
1234 attr->port_num > ibqp->device->phys_port_cnt)
1235 goto inval;
1236
1237 if (attr_mask & IB_QP_DEST_QPN)
1238 if (attr->dest_qp_num > RVT_QPN_MASK)
1239 goto inval;
1240
1241 if (attr_mask & IB_QP_RETRY_CNT)
1242 if (attr->retry_cnt > 7)
1243 goto inval;
1244
1245 if (attr_mask & IB_QP_RNR_RETRY)
1246 if (attr->rnr_retry > 7)
1247 goto inval;
1248
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001249 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001250 * Don't allow invalid path_mtu values. OK to set greater
1251 * than the active mtu (or even the max_cap, if we have tuned
1252 * that to a small mtu. We'll set qp->path_mtu
1253 * to the lesser of requested attribute mtu and active,
1254 * for packetizing messages.
1255 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001256 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001257 if (attr_mask & IB_QP_PATH_MTU) {
1258 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1259 if (pmtu < 0)
1260 goto inval;
1261 }
1262
1263 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1264 if (attr->path_mig_state == IB_MIG_REARM) {
1265 if (qp->s_mig_state == IB_MIG_ARMED)
1266 goto inval;
1267 if (new_state != IB_QPS_RTS)
1268 goto inval;
1269 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1270 if (qp->s_mig_state == IB_MIG_REARM)
1271 goto inval;
1272 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1273 goto inval;
1274 if (qp->s_mig_state == IB_MIG_ARMED)
1275 mig = 1;
1276 } else {
1277 goto inval;
1278 }
1279 }
1280
1281 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1282 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1283 goto inval;
1284
1285 switch (new_state) {
1286 case IB_QPS_RESET:
1287 if (qp->state != IB_QPS_RESET)
1288 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1289 break;
1290
1291 case IB_QPS_RTR:
1292 /* Allow event to re-trigger if QP set to RTR more than once */
1293 qp->r_flags &= ~RVT_R_COMM_EST;
1294 qp->state = new_state;
1295 break;
1296
1297 case IB_QPS_SQD:
1298 qp->s_draining = qp->s_last != qp->s_cur;
1299 qp->state = new_state;
1300 break;
1301
1302 case IB_QPS_SQE:
1303 if (qp->ibqp.qp_type == IB_QPT_RC)
1304 goto inval;
1305 qp->state = new_state;
1306 break;
1307
1308 case IB_QPS_ERR:
1309 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1310 break;
1311
1312 default:
1313 qp->state = new_state;
1314 break;
1315 }
1316
1317 if (attr_mask & IB_QP_PKEY_INDEX)
1318 qp->s_pkey_index = attr->pkey_index;
1319
1320 if (attr_mask & IB_QP_PORT)
1321 qp->port_num = attr->port_num;
1322
1323 if (attr_mask & IB_QP_DEST_QPN)
1324 qp->remote_qpn = attr->dest_qp_num;
1325
1326 if (attr_mask & IB_QP_SQ_PSN) {
1327 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1328 qp->s_psn = qp->s_next_psn;
1329 qp->s_sending_psn = qp->s_next_psn;
1330 qp->s_last_psn = qp->s_next_psn - 1;
1331 qp->s_sending_hpsn = qp->s_last_psn;
1332 }
1333
1334 if (attr_mask & IB_QP_RQ_PSN)
1335 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1336
1337 if (attr_mask & IB_QP_ACCESS_FLAGS)
1338 qp->qp_access_flags = attr->qp_access_flags;
1339
1340 if (attr_mask & IB_QP_AV) {
1341 qp->remote_ah_attr = attr->ah_attr;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001342 qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001343 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1344 }
1345
1346 if (attr_mask & IB_QP_ALT_PATH) {
1347 qp->alt_ah_attr = attr->alt_ah_attr;
1348 qp->s_alt_pkey_index = attr->alt_pkey_index;
1349 }
1350
1351 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1352 qp->s_mig_state = attr->path_mig_state;
1353 if (mig) {
1354 qp->remote_ah_attr = qp->alt_ah_attr;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001355 qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001356 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001357 }
1358 }
1359
1360 if (attr_mask & IB_QP_PATH_MTU) {
1361 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001362 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001363 }
1364
1365 if (attr_mask & IB_QP_RETRY_CNT) {
1366 qp->s_retry_cnt = attr->retry_cnt;
1367 qp->s_retry = attr->retry_cnt;
1368 }
1369
1370 if (attr_mask & IB_QP_RNR_RETRY) {
1371 qp->s_rnr_retry_cnt = attr->rnr_retry;
1372 qp->s_rnr_retry = attr->rnr_retry;
1373 }
1374
1375 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1376 qp->r_min_rnr_timer = attr->min_rnr_timer;
1377
1378 if (attr_mask & IB_QP_TIMEOUT) {
1379 qp->timeout = attr->timeout;
Kaike Wana25ce422017-06-17 10:37:26 -07001380 qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001381 }
1382
1383 if (attr_mask & IB_QP_QKEY)
1384 qp->qkey = attr->qkey;
1385
1386 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1387 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1388
1389 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1390 qp->s_max_rd_atomic = attr->max_rd_atomic;
1391
Ira Weinye85ec332016-01-22 13:04:38 -08001392 if (rdi->driver_f.modify_qp)
1393 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1394
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001395 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001396 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001397 spin_unlock_irq(&qp->r_lock);
1398
1399 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1400 rvt_insert_qp(rdi, qp);
1401
1402 if (lastwqe) {
1403 ev.device = qp->ibqp.device;
1404 ev.element.qp = &qp->ibqp;
1405 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1406 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1407 }
1408 if (mig) {
1409 ev.device = qp->ibqp.device;
1410 ev.element.qp = &qp->ibqp;
1411 ev.event = IB_EVENT_PATH_MIG;
1412 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1413 }
1414 return 0;
1415
1416inval:
1417 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001418 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001419 spin_unlock_irq(&qp->r_lock);
1420 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001421}
1422
1423/**
1424 * rvt_destroy_qp - destroy a queue pair
1425 * @ibqp: the queue pair to destroy
1426 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001427 * Note that this can be called while the QP is actively sending or
1428 * receiving!
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001429 *
1430 * Return: 0 on success.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001431 */
1432int rvt_destroy_qp(struct ib_qp *ibqp)
1433{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001434 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1435 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001436
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001437 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001438 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001439 spin_lock(&qp->s_lock);
1440 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1441 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001442 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001443 spin_unlock_irq(&qp->r_lock);
1444
Alex Estrinf9586ab2017-10-09 12:38:33 -07001445 wait_event(qp->wait, !atomic_read(&qp->refcount));
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001446 /* qpn is now available for use again */
1447 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1448
1449 spin_lock(&rdi->n_qps_lock);
1450 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001451 if (qp->ibqp.qp_type == IB_QPT_RC) {
1452 rdi->n_rc_qps--;
1453 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1454 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001455 spin_unlock(&rdi->n_qps_lock);
1456
1457 if (qp->ip)
1458 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1459 else
1460 vfree(qp->r_rq.wq);
1461 vfree(qp->s_wq);
1462 rdi->driver_f.qp_priv_free(rdi, qp);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001463 kfree(qp->s_ack_queue);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001464 kfree(qp);
1465 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001466}
1467
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001468/**
1469 * rvt_query_qp - query an ipbq
1470 * @ibqp: IB qp to query
1471 * @attr: attr struct to fill in
1472 * @attr_mask: attr mask ignored
1473 * @init_attr: struct to fill in
1474 *
1475 * Return: always 0
1476 */
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001477int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1478 int attr_mask, struct ib_qp_init_attr *init_attr)
1479{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001480 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1481 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1482
1483 attr->qp_state = qp->state;
1484 attr->cur_qp_state = attr->qp_state;
Sebastian Sanchez16570d32017-08-04 13:52:20 -07001485 attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Harish Chegondi74d2d502016-01-22 13:05:04 -08001486 attr->path_mig_state = qp->s_mig_state;
1487 attr->qkey = qp->qkey;
1488 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1489 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1490 attr->dest_qp_num = qp->remote_qpn;
1491 attr->qp_access_flags = qp->qp_access_flags;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001492 attr->cap.max_send_wr = qp->s_size - 1 -
1493 rdi->dparms.reserved_operations;
Harish Chegondi74d2d502016-01-22 13:05:04 -08001494 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1495 attr->cap.max_send_sge = qp->s_max_sge;
1496 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1497 attr->cap.max_inline_data = 0;
1498 attr->ah_attr = qp->remote_ah_attr;
1499 attr->alt_ah_attr = qp->alt_ah_attr;
1500 attr->pkey_index = qp->s_pkey_index;
1501 attr->alt_pkey_index = qp->s_alt_pkey_index;
1502 attr->en_sqd_async_notify = 0;
1503 attr->sq_draining = qp->s_draining;
1504 attr->max_rd_atomic = qp->s_max_rd_atomic;
1505 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1506 attr->min_rnr_timer = qp->r_min_rnr_timer;
1507 attr->port_num = qp->port_num;
1508 attr->timeout = qp->timeout;
1509 attr->retry_cnt = qp->s_retry_cnt;
1510 attr->rnr_retry = qp->s_rnr_retry_cnt;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001511 attr->alt_port_num =
1512 rdma_ah_get_port_num(&qp->alt_ah_attr);
Harish Chegondi74d2d502016-01-22 13:05:04 -08001513 attr->alt_timeout = qp->alt_timeout;
1514
1515 init_attr->event_handler = qp->ibqp.event_handler;
1516 init_attr->qp_context = qp->ibqp.qp_context;
1517 init_attr->send_cq = qp->ibqp.send_cq;
1518 init_attr->recv_cq = qp->ibqp.recv_cq;
1519 init_attr->srq = qp->ibqp.srq;
1520 init_attr->cap = attr->cap;
1521 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1522 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1523 else
1524 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1525 init_attr->qp_type = qp->ibqp.qp_type;
1526 init_attr->port_num = qp->port_num;
1527 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001528}
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001529
1530/**
1531 * rvt_post_receive - post a receive on a QP
1532 * @ibqp: the QP to post the receive on
1533 * @wr: the WR to post
1534 * @bad_wr: the first bad WR is put here
1535 *
1536 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001537 *
1538 * Return: 0 on success otherwise errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001539 */
1540int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1541 struct ib_recv_wr **bad_wr)
1542{
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001543 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1544 struct rvt_rwq *wq = qp->r_rq.wq;
1545 unsigned long flags;
Alex Estrin000a8302016-03-07 11:35:51 -08001546 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1547 !qp->ibqp.srq;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001548
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001549 /* Check that state is OK to post receive. */
1550 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1551 *bad_wr = wr;
1552 return -EINVAL;
1553 }
1554
1555 for (; wr; wr = wr->next) {
1556 struct rvt_rwqe *wqe;
1557 u32 next;
1558 int i;
1559
1560 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1561 *bad_wr = wr;
1562 return -EINVAL;
1563 }
1564
1565 spin_lock_irqsave(&qp->r_rq.lock, flags);
1566 next = wq->head + 1;
1567 if (next >= qp->r_rq.size)
1568 next = 0;
1569 if (next == wq->tail) {
1570 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1571 *bad_wr = wr;
1572 return -ENOMEM;
1573 }
Alex Estrin000a8302016-03-07 11:35:51 -08001574 if (unlikely(qp_err_flush)) {
1575 struct ib_wc wc;
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001576
Alex Estrin000a8302016-03-07 11:35:51 -08001577 memset(&wc, 0, sizeof(wc));
1578 wc.qp = &qp->ibqp;
1579 wc.opcode = IB_WC_RECV;
1580 wc.wr_id = wr->wr_id;
1581 wc.status = IB_WC_WR_FLUSH_ERR;
1582 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1583 } else {
1584 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1585 wqe->wr_id = wr->wr_id;
1586 wqe->num_sge = wr->num_sge;
1587 for (i = 0; i < wr->num_sge; i++)
1588 wqe->sg_list[i] = wr->sg_list[i];
1589 /*
1590 * Make sure queue entry is written
1591 * before the head index.
1592 */
1593 smp_wmb();
1594 wq->head = next;
1595 }
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001596 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1597 }
1598 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001599}
1600
1601/**
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001602 * rvt_qp_valid_operation - validate post send wr request
1603 * @qp - the qp
1604 * @post-parms - the post send table for the driver
1605 * @wr - the work request
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001606 *
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001607 * The routine validates the operation based on the
1608 * validation table an returns the length of the operation
1609 * which can extend beyond the ib_send_bw. Operation
1610 * dependent flags key atomic operation validation.
1611 *
1612 * There is an exception for UD qps that validates the pd and
1613 * overrides the length to include the additional UD specific
1614 * length.
1615 *
1616 * Returns a negative error or the length of the work request
1617 * for building the swqe.
1618 */
1619static inline int rvt_qp_valid_operation(
1620 struct rvt_qp *qp,
1621 const struct rvt_operation_params *post_parms,
1622 struct ib_send_wr *wr)
1623{
1624 int len;
1625
1626 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1627 return -EINVAL;
1628 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1629 return -EINVAL;
1630 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1631 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1632 return -EINVAL;
1633 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1634 (wr->num_sge == 0 ||
1635 wr->sg_list[0].length < sizeof(u64) ||
1636 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1637 return -EINVAL;
1638 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1639 !qp->s_max_rd_atomic)
1640 return -EINVAL;
1641 len = post_parms[wr->opcode].length;
1642 /* UD specific */
1643 if (qp->ibqp.qp_type != IB_QPT_UC &&
1644 qp->ibqp.qp_type != IB_QPT_RC) {
1645 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1646 return -EINVAL;
1647 len = sizeof(struct ib_ud_wr);
1648 }
1649 return len;
1650}
1651
1652/**
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001653 * rvt_qp_is_avail - determine queue capacity
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001654 * @qp - the qp
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001655 * @rdi - the rdmavt device
1656 * @reserved_op - is reserved operation
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001657 *
1658 * This assumes the s_hlock is held but the s_last
1659 * qp variable is uncontrolled.
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001660 *
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001661 * For non reserved operations, the qp->s_avail
1662 * may be changed.
1663 *
1664 * The return value is zero or a -ENOMEM.
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001665 */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001666static inline int rvt_qp_is_avail(
1667 struct rvt_qp *qp,
1668 struct rvt_dev_info *rdi,
1669 bool reserved_op)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001670{
1671 u32 slast;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001672 u32 avail;
1673 u32 reserved_used;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001674
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001675 /* see rvt_qp_wqe_unreserve() */
1676 smp_mb__before_atomic();
1677 reserved_used = atomic_read(&qp->s_reserved_used);
1678 if (unlikely(reserved_op)) {
1679 /* see rvt_qp_wqe_unreserve() */
1680 smp_mb__before_atomic();
1681 if (reserved_used >= rdi->dparms.reserved_operations)
1682 return -ENOMEM;
1683 return 0;
1684 }
1685 /* non-reserved operations */
1686 if (likely(qp->s_avail))
1687 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001688 smp_read_barrier_depends(); /* see rc.c */
1689 slast = ACCESS_ONCE(qp->s_last);
1690 if (qp->s_head >= slast)
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001691 avail = qp->s_size - (qp->s_head - slast);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001692 else
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001693 avail = slast - qp->s_head;
1694
1695 /* see rvt_qp_wqe_unreserve() */
1696 smp_mb__before_atomic();
1697 reserved_used = atomic_read(&qp->s_reserved_used);
1698 avail = avail - 1 -
1699 (rdi->dparms.reserved_operations - reserved_used);
1700 /* insure we don't assign a negative s_avail */
1701 if ((s32)avail <= 0)
1702 return -ENOMEM;
1703 qp->s_avail = avail;
1704 if (WARN_ON(qp->s_avail >
1705 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1706 rvt_pr_err(rdi,
1707 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1708 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1709 qp->s_head, qp->s_tail, qp->s_cur,
1710 qp->s_acked, qp->s_last);
1711 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001712}
1713
1714/**
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001715 * rvt_post_one_wr - post one RC, UC, or UD send work request
1716 * @qp: the QP to post on
1717 * @wr: the work request to send
1718 */
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001719static int rvt_post_one_wr(struct rvt_qp *qp,
1720 struct ib_send_wr *wr,
1721 int *call_send)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001722{
1723 struct rvt_swqe *wqe;
1724 u32 next;
1725 int i;
1726 int j;
1727 int acc;
1728 struct rvt_lkey_table *rkt;
1729 struct rvt_pd *pd;
1730 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001731 u8 log_pmtu;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001732 int ret;
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001733 size_t cplen;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001734 bool reserved_op;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001735 int local_ops_delayed = 0;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001736
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001737 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1738
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001739 /* IB spec says that num_sge == 0 is OK. */
1740 if (unlikely(wr->num_sge > qp->s_max_sge))
1741 return -EINVAL;
1742
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001743 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1744 if (ret < 0)
1745 return ret;
1746 cplen = ret;
1747
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001748 /*
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001749 * Local operations include fast register and local invalidate.
1750 * Fast register needs to be processed immediately because the
1751 * registered lkey may be used by following work requests and the
1752 * lkey needs to be valid at the time those requests are posted.
1753 * Local invalidate can be processed immediately if fencing is
1754 * not required and no previous local invalidate ops are pending.
1755 * Signaled local operations that have been processed immediately
1756 * need to have requests with "completion only" flags set posted
1757 * to the send queue in order to generate completions.
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001758 */
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001759 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001760 switch (wr->opcode) {
1761 case IB_WR_REG_MR:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001762 ret = rvt_fast_reg_mr(qp,
1763 reg_wr(wr)->mr,
1764 reg_wr(wr)->key,
1765 reg_wr(wr)->access);
1766 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1767 return ret;
1768 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001769 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001770 if ((wr->send_flags & IB_SEND_FENCE) ||
1771 atomic_read(&qp->local_ops_pending)) {
1772 local_ops_delayed = 1;
1773 } else {
1774 ret = rvt_invalidate_rkey(
1775 qp, wr->ex.invalidate_rkey);
1776 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1777 return ret;
1778 }
1779 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001780 default:
1781 return -EINVAL;
1782 }
1783 }
1784
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001785 reserved_op = rdi->post_parms[wr->opcode].flags &
1786 RVT_OPERATION_USE_RESERVE;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001787 /* check for avail */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001788 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1789 if (ret)
1790 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001791 next = qp->s_head + 1;
1792 if (next >= qp->s_size)
1793 next = 0;
Ira Weiny60c30f52016-02-03 14:14:45 -08001794
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001795 rkt = &rdi->lkey_table;
1796 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1797 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1798
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001799 /* cplen has length from above */
1800 memcpy(&wqe->wr, wr, cplen);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001801
1802 wqe->length = 0;
1803 j = 0;
1804 if (wr->num_sge) {
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07001805 struct rvt_sge *last_sge = NULL;
1806
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001807 acc = wr->opcode >= IB_WR_RDMA_READ ?
1808 IB_ACCESS_LOCAL_WRITE : 0;
1809 for (i = 0; i < wr->num_sge; i++) {
1810 u32 length = wr->sg_list[i].length;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001811
1812 if (length == 0)
1813 continue;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001814 ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
1815 &wr->sg_list[i], acc);
1816 if (unlikely(ret < 0))
1817 goto bail_inval_free;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001818 wqe->length += length;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001819 if (ret)
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07001820 last_sge = &wqe->sg_list[j];
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001821 j += ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001822 }
1823 wqe->wr.num_sge = j;
1824 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001825
1826 /* general part of wqe valid - allow for driver checks */
1827 if (rdi->driver_f.check_send_wqe) {
1828 ret = rdi->driver_f.check_send_wqe(qp, wqe);
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001829 if (ret < 0)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001830 goto bail_inval_free;
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001831 if (ret)
1832 *call_send = ret;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001833 }
1834
1835 log_pmtu = qp->log_pmtu;
1836 if (qp->ibqp.qp_type != IB_QPT_UC &&
1837 qp->ibqp.qp_type != IB_QPT_RC) {
1838 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1839
1840 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001841 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1842 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001843
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001844 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001845 if (local_ops_delayed)
1846 atomic_inc(&qp->local_ops_pending);
1847 else
1848 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001849 wqe->ssn = 0;
1850 wqe->psn = 0;
1851 wqe->lpsn = 0;
1852 } else {
1853 wqe->ssn = qp->s_ssn++;
1854 wqe->psn = qp->s_next_psn;
1855 wqe->lpsn = wqe->psn +
1856 (wqe->length ?
1857 ((wqe->length - 1) >> log_pmtu) :
1858 0);
1859 qp->s_next_psn = wqe->lpsn + 1;
1860 }
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07001861 if (unlikely(reserved_op)) {
1862 wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001863 rvt_qp_wqe_reserve(qp, wqe);
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07001864 } else {
1865 wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001866 qp->s_avail--;
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07001867 }
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07001868 trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001869 smp_wmb(); /* see request builders */
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001870 qp->s_head = next;
1871
1872 return 0;
1873
1874bail_inval_free:
1875 /* release mr holds */
1876 while (j) {
1877 struct rvt_sge *sge = &wqe->sg_list[--j];
1878
1879 rvt_put_mr(sge->mr);
1880 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001881 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001882}
1883
1884/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001885 * rvt_post_send - post a send on a QP
1886 * @ibqp: the QP to post the send on
1887 * @wr: the list of work requests to post
1888 * @bad_wr: the first bad WR is put here
1889 *
1890 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001891 *
1892 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001893 */
1894int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1895 struct ib_send_wr **bad_wr)
1896{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001897 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1898 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1899 unsigned long flags = 0;
1900 int call_send;
1901 unsigned nreq = 0;
1902 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001903
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001904 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001905
1906 /*
1907 * Ensure QP state is such that we can send. If not bail out early,
1908 * there is no need to do this every time we post a send.
1909 */
1910 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001911 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001912 return -EINVAL;
1913 }
1914
1915 /*
1916 * If the send queue is empty, and we only have a single WR then just go
1917 * ahead and kick the send engine into gear. Otherwise we will always
1918 * just schedule the send to happen later.
1919 */
1920 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1921
1922 for (; wr; wr = wr->next) {
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001923 err = rvt_post_one_wr(qp, wr, &call_send);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001924 if (unlikely(err)) {
1925 *bad_wr = wr;
1926 goto bail;
1927 }
1928 nreq++;
1929 }
1930bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001931 spin_unlock_irqrestore(&qp->s_hlock, flags);
1932 if (nreq) {
1933 if (call_send)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001934 rdi->driver_f.do_send(qp);
Jubin Johne6d2e012016-04-12 10:47:00 -07001935 else
1936 rdi->driver_f.schedule_send_no_lock(qp);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001937 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001938 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001939}
1940
1941/**
1942 * rvt_post_srq_receive - post a receive on a shared receive queue
1943 * @ibsrq: the SRQ to post the receive on
1944 * @wr: the list of work requests to post
1945 * @bad_wr: A pointer to the first WR to cause a problem is put here
1946 *
1947 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001948 *
1949 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001950 */
1951int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1952 struct ib_recv_wr **bad_wr)
1953{
Jubin Johnb8f881b2016-02-03 14:14:36 -08001954 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1955 struct rvt_rwq *wq;
1956 unsigned long flags;
1957
1958 for (; wr; wr = wr->next) {
1959 struct rvt_rwqe *wqe;
1960 u32 next;
1961 int i;
1962
1963 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1964 *bad_wr = wr;
1965 return -EINVAL;
1966 }
1967
1968 spin_lock_irqsave(&srq->rq.lock, flags);
1969 wq = srq->rq.wq;
1970 next = wq->head + 1;
1971 if (next >= srq->rq.size)
1972 next = 0;
1973 if (next == wq->tail) {
1974 spin_unlock_irqrestore(&srq->rq.lock, flags);
1975 *bad_wr = wr;
1976 return -ENOMEM;
1977 }
1978
1979 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1980 wqe->wr_id = wr->wr_id;
1981 wqe->num_sge = wr->num_sge;
1982 for (i = 0; i < wr->num_sge; i++)
1983 wqe->sg_list[i] = wr->sg_list[i];
1984 /* Make sure queue entry is written before the head index. */
1985 smp_wmb();
1986 wq->head = next;
1987 spin_unlock_irqrestore(&srq->rq.lock, flags);
1988 }
1989 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001990}
Brian Weltybeb5a042017-02-08 05:27:01 -08001991
1992/**
1993 * qp_comm_est - handle trap with QP established
1994 * @qp: the QP
1995 */
1996void rvt_comm_est(struct rvt_qp *qp)
1997{
1998 qp->r_flags |= RVT_R_COMM_EST;
1999 if (qp->ibqp.event_handler) {
2000 struct ib_event ev;
2001
2002 ev.device = qp->ibqp.device;
2003 ev.element.qp = &qp->ibqp;
2004 ev.event = IB_EVENT_COMM_EST;
2005 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2006 }
2007}
2008EXPORT_SYMBOL(rvt_comm_est);
2009
2010void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
2011{
2012 unsigned long flags;
2013 int lastwqe;
2014
2015 spin_lock_irqsave(&qp->s_lock, flags);
2016 lastwqe = rvt_error_qp(qp, err);
2017 spin_unlock_irqrestore(&qp->s_lock, flags);
2018
2019 if (lastwqe) {
2020 struct ib_event ev;
2021
2022 ev.device = qp->ibqp.device;
2023 ev.element.qp = &qp->ibqp;
2024 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2025 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2026 }
2027}
2028EXPORT_SYMBOL(rvt_rc_error);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002029
Don Hiatt881fccb2017-02-08 05:28:19 -08002030/*
2031 * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
2032 * @index - the index
2033 * return usec from an index into ib_rvt_rnr_table
2034 */
2035unsigned long rvt_rnr_tbl_to_usec(u32 index)
2036{
Don Hiatt832666c2017-02-08 05:28:25 -08002037 return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
Don Hiatt881fccb2017-02-08 05:28:19 -08002038}
2039EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
2040
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002041static inline unsigned long rvt_aeth_to_usec(u32 aeth)
2042{
Don Hiatt832666c2017-02-08 05:28:25 -08002043 return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
2044 IB_AETH_CREDIT_MASK];
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002045}
2046
2047/*
2048 * rvt_add_retry_timer - add/start a retry timer
2049 * @qp - the QP
2050 * add a retry timer on the QP
2051 */
2052void rvt_add_retry_timer(struct rvt_qp *qp)
2053{
2054 struct ib_qp *ibqp = &qp->ibqp;
2055 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2056
2057 lockdep_assert_held(&qp->s_lock);
2058 qp->s_flags |= RVT_S_TIMER;
2059 /* 4.096 usec. * (1 << qp->timeout) */
2060 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
2061 rdi->busy_jiffies;
2062 add_timer(&qp->s_timer);
2063}
2064EXPORT_SYMBOL(rvt_add_retry_timer);
2065
2066/**
2067 * rvt_add_rnr_timer - add/start an rnr timer
2068 * @qp - the QP
2069 * @aeth - aeth of RNR timeout, simulated aeth for loopback
2070 * add an rnr timer on the QP
2071 */
2072void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
2073{
2074 u32 to;
2075
2076 lockdep_assert_held(&qp->s_lock);
2077 qp->s_flags |= RVT_S_WAIT_RNR;
2078 to = rvt_aeth_to_usec(aeth);
2079 hrtimer_start(&qp->s_rnr_timer,
2080 ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
2081}
2082EXPORT_SYMBOL(rvt_add_rnr_timer);
2083
2084/**
2085 * rvt_stop_rc_timers - stop all timers
2086 * @qp - the QP
2087 * stop any pending timers
2088 */
2089void rvt_stop_rc_timers(struct rvt_qp *qp)
2090{
2091 lockdep_assert_held(&qp->s_lock);
2092 /* Remove QP from all timers */
2093 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2094 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2095 del_timer(&qp->s_timer);
2096 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2097 }
2098}
2099EXPORT_SYMBOL(rvt_stop_rc_timers);
2100
2101/**
2102 * rvt_stop_rnr_timer - stop an rnr timer
2103 * @qp - the QP
2104 *
2105 * stop an rnr timer and return if the timer
2106 * had been pending.
2107 */
2108static int rvt_stop_rnr_timer(struct rvt_qp *qp)
2109{
2110 int rval = 0;
2111
2112 lockdep_assert_held(&qp->s_lock);
2113 /* Remove QP from rnr timer */
2114 if (qp->s_flags & RVT_S_WAIT_RNR) {
2115 qp->s_flags &= ~RVT_S_WAIT_RNR;
2116 rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
2117 }
2118 return rval;
2119}
2120
2121/**
2122 * rvt_del_timers_sync - wait for any timeout routines to exit
2123 * @qp - the QP
2124 */
2125void rvt_del_timers_sync(struct rvt_qp *qp)
2126{
2127 del_timer_sync(&qp->s_timer);
2128 hrtimer_cancel(&qp->s_rnr_timer);
2129}
2130EXPORT_SYMBOL(rvt_del_timers_sync);
2131
2132/**
2133 * This is called from s_timer for missing responses.
2134 */
2135static void rvt_rc_timeout(unsigned long arg)
2136{
2137 struct rvt_qp *qp = (struct rvt_qp *)arg;
2138 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2139 unsigned long flags;
2140
2141 spin_lock_irqsave(&qp->r_lock, flags);
2142 spin_lock(&qp->s_lock);
2143 if (qp->s_flags & RVT_S_TIMER) {
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002144 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
2145
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002146 qp->s_flags &= ~RVT_S_TIMER;
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002147 rvp->n_rc_timeouts++;
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002148 del_timer(&qp->s_timer);
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002149 trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002150 if (rdi->driver_f.notify_restart_rc)
2151 rdi->driver_f.notify_restart_rc(qp,
2152 qp->s_last_psn + 1,
2153 1);
2154 rdi->driver_f.schedule_send(qp);
2155 }
2156 spin_unlock(&qp->s_lock);
2157 spin_unlock_irqrestore(&qp->r_lock, flags);
2158}
2159
2160/*
2161 * This is called from s_timer for RNR timeouts.
2162 */
2163enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2164{
2165 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2166 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2167 unsigned long flags;
2168
2169 spin_lock_irqsave(&qp->s_lock, flags);
2170 rvt_stop_rnr_timer(qp);
2171 rdi->driver_f.schedule_send(qp);
2172 spin_unlock_irqrestore(&qp->s_lock, flags);
2173 return HRTIMER_NORESTART;
2174}
2175EXPORT_SYMBOL(rvt_rc_rnr_retry);
Mike Marciniszyn4734b4f2017-08-28 11:23:45 -07002176
2177/**
2178 * rvt_qp_iter_init - initial for QP iteration
2179 * @rdi - rvt devinfo
2180 * @v - u64 value
2181 *
2182 * This returns an iterator suitable for iterating QPs
2183 * in the system.
2184 *
2185 * The @cb is a user defined callback and @v is a 64
2186 * bit value passed to and relevant for processing in the
2187 * @cb. An example use case would be to alter QP processing
2188 * based on criteria not part of the rvt_qp.
2189 *
2190 * Use cases that require memory allocation to succeed
2191 * must preallocate appropriately.
2192 *
2193 * Return: a pointer to an rvt_qp_iter or NULL
2194 */
2195struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
2196 u64 v,
2197 void (*cb)(struct rvt_qp *qp, u64 v))
2198{
2199 struct rvt_qp_iter *i;
2200
2201 i = kzalloc(sizeof(*i), GFP_KERNEL);
2202 if (!i)
2203 return NULL;
2204
2205 i->rdi = rdi;
2206 /* number of special QPs (SMI/GSI) for device */
2207 i->specials = rdi->ibdev.phys_port_cnt * 2;
2208 i->v = v;
2209 i->cb = cb;
2210
2211 return i;
2212}
2213EXPORT_SYMBOL(rvt_qp_iter_init);
2214
2215/**
2216 * rvt_qp_iter_next - return the next QP in iter
2217 * @iter - the iterator
2218 *
2219 * Fine grained QP iterator suitable for use
2220 * with debugfs seq_file mechanisms.
2221 *
2222 * Updates iter->qp with the current QP when the return
2223 * value is 0.
2224 *
2225 * Return: 0 - iter->qp is valid 1 - no more QPs
2226 */
2227int rvt_qp_iter_next(struct rvt_qp_iter *iter)
2228 __must_hold(RCU)
2229{
2230 int n = iter->n;
2231 int ret = 1;
2232 struct rvt_qp *pqp = iter->qp;
2233 struct rvt_qp *qp;
2234 struct rvt_dev_info *rdi = iter->rdi;
2235
2236 /*
2237 * The approach is to consider the special qps
2238 * as additional table entries before the
2239 * real hash table. Since the qp code sets
2240 * the qp->next hash link to NULL, this works just fine.
2241 *
2242 * iter->specials is 2 * # ports
2243 *
2244 * n = 0..iter->specials is the special qp indices
2245 *
2246 * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
2247 * the potential hash bucket entries
2248 *
2249 */
2250 for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
2251 if (pqp) {
2252 qp = rcu_dereference(pqp->next);
2253 } else {
2254 if (n < iter->specials) {
2255 struct rvt_ibport *rvp;
2256 int pidx;
2257
2258 pidx = n % rdi->ibdev.phys_port_cnt;
2259 rvp = rdi->ports[pidx];
2260 qp = rcu_dereference(rvp->qp[n & 1]);
2261 } else {
2262 qp = rcu_dereference(
2263 rdi->qp_dev->qp_table[
2264 (n - iter->specials)]);
2265 }
2266 }
2267 pqp = qp;
2268 if (qp) {
2269 iter->qp = qp;
2270 iter->n = n;
2271 return 0;
2272 }
2273 }
2274 return ret;
2275}
2276EXPORT_SYMBOL(rvt_qp_iter_next);
2277
2278/**
2279 * rvt_qp_iter - iterate all QPs
2280 * @rdi - rvt devinfo
2281 * @v - a 64 bit value
2282 * @cb - a callback
2283 *
2284 * This provides a way for iterating all QPs.
2285 *
2286 * The @cb is a user defined callback and @v is a 64
2287 * bit value passed to and relevant for processing in the
2288 * cb. An example use case would be to alter QP processing
2289 * based on criteria not part of the rvt_qp.
2290 *
2291 * The code has an internal iterator to simplify
2292 * non seq_file use cases.
2293 */
2294void rvt_qp_iter(struct rvt_dev_info *rdi,
2295 u64 v,
2296 void (*cb)(struct rvt_qp *qp, u64 v))
2297{
2298 int ret;
2299 struct rvt_qp_iter i = {
2300 .rdi = rdi,
2301 .specials = rdi->ibdev.phys_port_cnt * 2,
2302 .v = v,
2303 .cb = cb
2304 };
2305
2306 rcu_read_lock();
2307 do {
2308 ret = rvt_qp_iter_next(&i);
2309 if (!ret) {
2310 rvt_get_qp(i.qp);
2311 rcu_read_unlock();
2312 i.cb(i.qp, i.v);
2313 rcu_read_lock();
2314 rvt_put_qp(i.qp);
2315 }
2316 } while (!ret);
2317 rcu_read_unlock();
2318}
2319EXPORT_SYMBOL(rvt_qp_iter);