]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/infiniband/sw/rdmavt/qp.c
IB/rdmavt: Adding timer logic to rdmavt
[mirror_ubuntu-artful-kernel.git] / drivers / infiniband / sw / rdmavt / qp.c
CommitLineData
b518d3e6 1/*
fe314195 2 * Copyright(c) 2016 Intel Corporation.
b518d3e6
DD
3 *
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
3b0b3fb3 48#include <linux/hash.h>
0acb0cc7
DD
49#include <linux/bitops.h>
50#include <linux/lockdep.h>
515667f8
DD
51#include <linux/vmalloc.h>
52#include <linux/slab.h>
53#include <rdma/ib_verbs.h>
b518d3e6 54#include "qp.h"
515667f8 55#include "vt.h"
3b0b3fb3 56#include "trace.h"
b518d3e6 57
11a10d4b
VSD
58static void rvt_rc_timeout(unsigned long arg);
59
60/*
61 * Convert the AETH RNR timeout code into the number of microseconds.
62 */
63static const u32 ib_rvt_rnr_table[32] = {
64 655360, /* 00: 655.36 */
65 10, /* 01: .01 */
66 20, /* 02 .02 */
67 30, /* 03: .03 */
68 40, /* 04: .04 */
69 60, /* 05: .06 */
70 80, /* 06: .08 */
71 120, /* 07: .12 */
72 160, /* 08: .16 */
73 240, /* 09: .24 */
74 320, /* 0A: .32 */
75 480, /* 0B: .48 */
76 640, /* 0C: .64 */
77 960, /* 0D: .96 */
78 1280, /* 0E: 1.28 */
79 1920, /* 0F: 1.92 */
80 2560, /* 10: 2.56 */
81 3840, /* 11: 3.84 */
82 5120, /* 12: 5.12 */
83 7680, /* 13: 7.68 */
84 10240, /* 14: 10.24 */
85 15360, /* 15: 15.36 */
86 20480, /* 16: 20.48 */
87 30720, /* 17: 30.72 */
88 40960, /* 18: 40.96 */
89 61440, /* 19: 61.44 */
90 81920, /* 1A: 81.92 */
91 122880, /* 1B: 122.88 */
92 163840, /* 1C: 163.84 */
93 245760, /* 1D: 245.76 */
94 327680, /* 1E: 327.68 */
95 491520 /* 1F: 491.52 */
96};
97
bfbac097
DD
98/*
99 * Note that it is OK to post send work requests in the SQE and ERR
100 * states; rvt_do_send() will process them and generate error
101 * completions as per IB 1.2 C10-96.
102 */
103const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
104 [IB_QPS_RESET] = 0,
105 [IB_QPS_INIT] = RVT_POST_RECV_OK,
106 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
107 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
108 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
109 RVT_PROCESS_NEXT_SEND_OK,
110 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
111 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
112 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
113 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
114 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
115 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
116};
117EXPORT_SYMBOL(ib_rvt_state_ops);
118
f2dc9cdc
MM
119/*
120 * Translate ib_wr_opcode into ib_wc_opcode.
121 */
122const enum ib_wc_opcode ib_rvt_wc_opcode[] = {
123 [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
124 [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
125 [IB_WR_SEND] = IB_WC_SEND,
126 [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
127 [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
128 [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
129 [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD,
130 [IB_WR_SEND_WITH_INV] = IB_WC_SEND,
131 [IB_WR_LOCAL_INV] = IB_WC_LOCAL_INV,
132 [IB_WR_REG_MR] = IB_WC_REG_MR
133};
134EXPORT_SYMBOL(ib_rvt_wc_opcode);
135
d2b8d4da
MM
136static void get_map_page(struct rvt_qpn_table *qpt,
137 struct rvt_qpn_map *map,
138 gfp_t gfp)
0acb0cc7 139{
d2b8d4da 140 unsigned long page = get_zeroed_page(gfp);
0acb0cc7
DD
141
142 /*
143 * Free the page if someone raced with us installing it.
144 */
145
146 spin_lock(&qpt->lock);
147 if (map->page)
148 free_page(page);
149 else
150 map->page = (void *)page;
151 spin_unlock(&qpt->lock);
152}
153
154/**
155 * init_qpn_table - initialize the QP number table for a device
156 * @qpt: the QPN table
157 */
158static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
159{
160 u32 offset, i;
161 struct rvt_qpn_map *map;
162 int ret = 0;
163
fef2efd6 164 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
0acb0cc7
DD
165 return -EINVAL;
166
167 spin_lock_init(&qpt->lock);
168
169 qpt->last = rdi->dparms.qpn_start;
170 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
171
172 /*
173 * Drivers may want some QPs beyond what we need for verbs let them use
174 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
175 * for those. The reserved range must be *after* the range which verbs
176 * will pick from.
177 */
178
179 /* Figure out number of bit maps needed before reserved range */
180 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
181
182 /* This should always be zero */
183 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
184
185 /* Starting with the first reserved bit map */
186 map = &qpt->map[qpt->nmaps];
187
188 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
189 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
fef2efd6 190 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
0acb0cc7 191 if (!map->page) {
d2b8d4da 192 get_map_page(qpt, map, GFP_KERNEL);
0acb0cc7
DD
193 if (!map->page) {
194 ret = -ENOMEM;
195 break;
196 }
197 }
198 set_bit(offset, map->page);
199 offset++;
200 if (offset == RVT_BITS_PER_PAGE) {
201 /* next page */
202 qpt->nmaps++;
203 map++;
204 offset = 0;
205 }
206 }
207 return ret;
208}
209
210/**
211 * free_qpn_table - free the QP number table for a device
212 * @qpt: the QPN table
213 */
214static void free_qpn_table(struct rvt_qpn_table *qpt)
215{
216 int i;
217
218 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
219 free_page((unsigned long)qpt->map[i].page);
220}
221
90793f71
DD
222/**
223 * rvt_driver_qp_init - Init driver qp resources
224 * @rdi: rvt dev strucutre
225 *
226 * Return: 0 on success
227 */
0acb0cc7
DD
228int rvt_driver_qp_init(struct rvt_dev_info *rdi)
229{
230 int i;
231 int ret = -ENOMEM;
232
0acb0cc7
DD
233 if (!rdi->dparms.qp_table_size)
234 return -EINVAL;
235
236 /*
237 * If driver is not doing any QP allocation then make sure it is
238 * providing the necessary QP functions.
239 */
515667f8
DD
240 if (!rdi->driver_f.free_all_qps ||
241 !rdi->driver_f.qp_priv_alloc ||
242 !rdi->driver_f.qp_priv_free ||
11a10d4b
VSD
243 !rdi->driver_f.notify_qp_reset ||
244 !rdi->driver_f.notify_restart_rc)
0acb0cc7
DD
245 return -EINVAL;
246
247 /* allocate parent object */
d1b697b6
MH
248 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
249 rdi->dparms.node);
0acb0cc7
DD
250 if (!rdi->qp_dev)
251 return -ENOMEM;
252
253 /* allocate hash table */
254 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
255 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
256 rdi->qp_dev->qp_table =
d1b697b6
MH
257 kmalloc_node(rdi->qp_dev->qp_table_size *
258 sizeof(*rdi->qp_dev->qp_table),
259 GFP_KERNEL, rdi->dparms.node);
0acb0cc7
DD
260 if (!rdi->qp_dev->qp_table)
261 goto no_qp_table;
262
263 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
264 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
265
266 spin_lock_init(&rdi->qp_dev->qpt_lock);
267
268 /* initialize qpn map */
269 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
270 goto fail_table;
271
515667f8
DD
272 spin_lock_init(&rdi->n_qps_lock);
273
274 return 0;
0acb0cc7
DD
275
276fail_table:
277 kfree(rdi->qp_dev->qp_table);
278 free_qpn_table(&rdi->qp_dev->qpn_table);
279
280no_qp_table:
281 kfree(rdi->qp_dev);
282
283 return ret;
284}
285
286/**
287 * free_all_qps - check for QPs still in use
288 * @qpt: the QP table to empty
289 *
290 * There should not be any QPs still in use.
291 * Free memory for table.
292 */
515667f8 293static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
0acb0cc7
DD
294{
295 unsigned long flags;
296 struct rvt_qp *qp;
297 unsigned n, qp_inuse = 0;
298 spinlock_t *ql; /* work around too long line below */
299
515667f8
DD
300 if (rdi->driver_f.free_all_qps)
301 qp_inuse = rdi->driver_f.free_all_qps(rdi);
0acb0cc7 302
4e74080b
DD
303 qp_inuse += rvt_mcast_tree_empty(rdi);
304
0acb0cc7 305 if (!rdi->qp_dev)
515667f8 306 return qp_inuse;
0acb0cc7
DD
307
308 ql = &rdi->qp_dev->qpt_lock;
515667f8 309 spin_lock_irqsave(ql, flags);
0acb0cc7
DD
310 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
311 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
312 lockdep_is_held(ql));
313 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
515667f8
DD
314
315 for (; qp; qp = rcu_dereference_protected(qp->next,
316 lockdep_is_held(ql)))
0acb0cc7 317 qp_inuse++;
0acb0cc7
DD
318 }
319 spin_unlock_irqrestore(ql, flags);
320 synchronize_rcu();
321 return qp_inuse;
322}
323
90793f71
DD
324/**
325 * rvt_qp_exit - clean up qps on device exit
326 * @rdi: rvt dev structure
327 *
328 * Check for qp leaks and free resources.
329 */
0acb0cc7
DD
330void rvt_qp_exit(struct rvt_dev_info *rdi)
331{
515667f8 332 u32 qps_inuse = rvt_free_all_qps(rdi);
0acb0cc7 333
0acb0cc7
DD
334 if (qps_inuse)
335 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
336 qps_inuse);
337 if (!rdi->qp_dev)
338 return;
339
340 kfree(rdi->qp_dev->qp_table);
341 free_qpn_table(&rdi->qp_dev->qpn_table);
342 kfree(rdi->qp_dev);
343}
344
515667f8
DD
345static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
346 struct rvt_qpn_map *map, unsigned off)
347{
348 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
349}
350
f1badc71
DD
351/**
352 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
353 * IB_QPT_SMI/IB_QPT_GSI
354 *@rdi: rvt device info structure
355 *@qpt: queue pair number table pointer
356 *@port_num: IB port number, 1 based, comes from core
357 *
358 * Return: The queue pair number
515667f8
DD
359 */
360static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
f1badc71 361 enum ib_qp_type type, u8 port_num, gfp_t gfp)
515667f8
DD
362{
363 u32 i, offset, max_scan, qpn;
364 struct rvt_qpn_map *map;
365 u32 ret;
366
367 if (rdi->driver_f.alloc_qpn)
b7b3cf44 368 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
515667f8
DD
369
370 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
371 unsigned n;
372
373 ret = type == IB_QPT_GSI;
f1badc71 374 n = 1 << (ret + 2 * (port_num - 1));
515667f8
DD
375 spin_lock(&qpt->lock);
376 if (qpt->flags & n)
377 ret = -EINVAL;
378 else
379 qpt->flags |= n;
380 spin_unlock(&qpt->lock);
381 goto bail;
382 }
383
384 qpn = qpt->last + qpt->incr;
385 if (qpn >= RVT_QPN_MAX)
386 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
387 /* offset carries bit 0 */
388 offset = qpn & RVT_BITS_PER_PAGE_MASK;
389 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
390 max_scan = qpt->nmaps - !offset;
391 for (i = 0;;) {
392 if (unlikely(!map->page)) {
d2b8d4da 393 get_map_page(qpt, map, gfp);
515667f8
DD
394 if (unlikely(!map->page))
395 break;
396 }
397 do {
398 if (!test_and_set_bit(offset, map->page)) {
399 qpt->last = qpn;
400 ret = qpn;
401 goto bail;
402 }
403 offset += qpt->incr;
404 /*
405 * This qpn might be bogus if offset >= BITS_PER_PAGE.
406 * That is OK. It gets re-assigned below
407 */
408 qpn = mk_qpn(qpt, map, offset);
409 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
410 /*
411 * In order to keep the number of pages allocated to a
412 * minimum, we scan the all existing pages before increasing
413 * the size of the bitmap table.
414 */
415 if (++i > max_scan) {
416 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
417 break;
418 map = &qpt->map[qpt->nmaps++];
419 /* start at incr with current bit 0 */
420 offset = qpt->incr | (offset & 1);
421 } else if (map < &qpt->map[qpt->nmaps]) {
422 ++map;
423 /* start at incr with current bit 0 */
424 offset = qpt->incr | (offset & 1);
425 } else {
426 map = &qpt->map[0];
427 /* wrap to first map page, invert bit 0 */
428 offset = qpt->incr | ((offset & 1) ^ 1);
429 }
501edc42
BW
430 /* there can be no set bits in low-order QoS bits */
431 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
515667f8
DD
432 qpn = mk_qpn(qpt, map, offset);
433 }
434
435 ret = -ENOMEM;
436
437bail:
438 return ret;
439}
440
441static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
442{
443 struct rvt_qpn_map *map;
444
445 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
446 if (map->page)
447 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
448}
449
79a225be
DD
450/**
451 * rvt_clear_mr_refs - Drop help mr refs
452 * @qp: rvt qp data structure
453 * @clr_sends: If shoudl clear send side or not
454 */
455static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
456{
457 unsigned n;
8b103e9c 458 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
79a225be
DD
459
460 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
461 rvt_put_ss(&qp->s_rdma_read_sge);
462
463 rvt_put_ss(&qp->r_sge);
464
465 if (clr_sends) {
466 while (qp->s_last != qp->s_head) {
467 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
468 unsigned i;
469
470 for (i = 0; i < wqe->wr.num_sge; i++) {
471 struct rvt_sge *sge = &wqe->sg_list[i];
472
473 rvt_put_mr(sge->mr);
474 }
475 if (qp->ibqp.qp_type == IB_QPT_UD ||
476 qp->ibqp.qp_type == IB_QPT_SMI ||
477 qp->ibqp.qp_type == IB_QPT_GSI)
478 atomic_dec(&ibah_to_rvtah(
479 wqe->ud_wr.ah)->refcount);
480 if (++qp->s_last >= qp->s_size)
481 qp->s_last = 0;
482 smp_wmb(); /* see qp_set_savail */
483 }
484 if (qp->s_rdma_mr) {
485 rvt_put_mr(qp->s_rdma_mr);
486 qp->s_rdma_mr = NULL;
487 }
488 }
489
490 if (qp->ibqp.qp_type != IB_QPT_RC)
491 return;
492
8b103e9c 493 for (n = 0; n < rvt_max_atomic(rdi); n++) {
79a225be
DD
494 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
495
fe508272 496 if (e->rdma_sge.mr) {
79a225be
DD
497 rvt_put_mr(e->rdma_sge.mr);
498 e->rdma_sge.mr = NULL;
499 }
500 }
501}
502
503/**
504 * rvt_remove_qp - remove qp form table
505 * @rdi: rvt dev struct
506 * @qp: qp to remove
507 *
508 * Remove the QP from the table so it can't be found asynchronously by
509 * the receive routine.
510 */
511static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
512{
513 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
514 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
515 unsigned long flags;
516 int removed = 1;
517
518 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
519
520 if (rcu_dereference_protected(rvp->qp[0],
521 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
522 RCU_INIT_POINTER(rvp->qp[0], NULL);
523 } else if (rcu_dereference_protected(rvp->qp[1],
524 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
525 RCU_INIT_POINTER(rvp->qp[1], NULL);
526 } else {
527 struct rvt_qp *q;
528 struct rvt_qp __rcu **qpp;
529
530 removed = 0;
531 qpp = &rdi->qp_dev->qp_table[n];
532 for (; (q = rcu_dereference_protected(*qpp,
533 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
534 qpp = &q->next) {
535 if (q == qp) {
536 RCU_INIT_POINTER(*qpp,
537 rcu_dereference_protected(qp->next,
538 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
539 removed = 1;
540 trace_rvt_qpremove(qp, n);
541 break;
542 }
543 }
544 }
545
546 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
547 if (removed) {
548 synchronize_rcu();
4d6f85c3 549 rvt_put_qp(qp);
79a225be
DD
550 }
551}
552
515667f8 553/**
222f7a9a
MM
554 * rvt_init_qp - initialize the QP state to the reset state
555 * @qp: the QP to init or reinit
515667f8 556 * @type: the QP type
222f7a9a
MM
557 *
558 * This function is called from both rvt_create_qp() and
559 * rvt_reset_qp(). The difference is that the reset
560 * patch the necessary locks to protect against concurent
561 * access.
515667f8 562 */
222f7a9a
MM
563static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
564 enum ib_qp_type type)
515667f8 565{
3b0b3fb3
DD
566 qp->remote_qpn = 0;
567 qp->qkey = 0;
568 qp->qp_access_flags = 0;
515667f8
DD
569 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
570 qp->s_hdrwords = 0;
571 qp->s_wqe = NULL;
572 qp->s_draining = 0;
573 qp->s_next_psn = 0;
574 qp->s_last_psn = 0;
575 qp->s_sending_psn = 0;
576 qp->s_sending_hpsn = 0;
577 qp->s_psn = 0;
578 qp->r_psn = 0;
579 qp->r_msn = 0;
580 if (type == IB_QPT_RC) {
581 qp->s_state = IB_OPCODE_RC_SEND_LAST;
582 qp->r_state = IB_OPCODE_RC_SEND_LAST;
583 } else {
584 qp->s_state = IB_OPCODE_UC_SEND_LAST;
585 qp->r_state = IB_OPCODE_UC_SEND_LAST;
586 }
587 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
588 qp->r_nak_state = 0;
589 qp->r_aflags = 0;
590 qp->r_flags = 0;
591 qp->s_head = 0;
592 qp->s_tail = 0;
593 qp->s_cur = 0;
594 qp->s_acked = 0;
595 qp->s_last = 0;
596 qp->s_ssn = 1;
597 qp->s_lsn = 0;
598 qp->s_mig_state = IB_MIG_MIGRATED;
515667f8
DD
599 qp->r_head_ack_queue = 0;
600 qp->s_tail_ack_queue = 0;
601 qp->s_num_rd_atomic = 0;
602 if (qp->r_rq.wq) {
603 qp->r_rq.wq->head = 0;
604 qp->r_rq.wq->tail = 0;
605 }
606 qp->r_sge.num_sge = 0;
856cc4c2 607 atomic_set(&qp->s_reserved_used, 0);
515667f8
DD
608}
609
222f7a9a
MM
610/**
611 * rvt_reset_qp - initialize the QP state to the reset state
612 * @qp: the QP to reset
613 * @type: the QP type
614 *
615 * r_lock, s_hlock, and s_lock are required to be held by the caller
616 */
617static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
618 enum ib_qp_type type)
619 __must_hold(&qp->s_lock)
620 __must_hold(&qp->s_hlock)
621 __must_hold(&qp->r_lock)
622{
68e78b3d
MM
623 lockdep_assert_held(&qp->r_lock);
624 lockdep_assert_held(&qp->s_hlock);
625 lockdep_assert_held(&qp->s_lock);
222f7a9a
MM
626 if (qp->state != IB_QPS_RESET) {
627 qp->state = IB_QPS_RESET;
628
629 /* Let drivers flush their waitlist */
630 rdi->driver_f.flush_qp_waiters(qp);
11a10d4b 631 rvt_stop_rc_timers(qp);
222f7a9a
MM
632 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
633 spin_unlock(&qp->s_lock);
634 spin_unlock(&qp->s_hlock);
635 spin_unlock_irq(&qp->r_lock);
636
637 /* Stop the send queue and the retry timer */
638 rdi->driver_f.stop_send_queue(qp);
11a10d4b 639 rvt_del_timers_sync(qp);
222f7a9a
MM
640 /* Wait for things to stop */
641 rdi->driver_f.quiesce_qp(qp);
642
643 /* take qp out the hash and wait for it to be unused */
644 rvt_remove_qp(rdi, qp);
645 wait_event(qp->wait, !atomic_read(&qp->refcount));
646
647 /* grab the lock b/c it was locked at call time */
648 spin_lock_irq(&qp->r_lock);
649 spin_lock(&qp->s_hlock);
650 spin_lock(&qp->s_lock);
651
652 rvt_clear_mr_refs(qp, 1);
653 /*
654 * Let the driver do any tear down or re-init it needs to for
655 * a qp that has been reset
656 */
657 rdi->driver_f.notify_qp_reset(qp);
658 }
659 rvt_init_qp(rdi, qp, type);
68e78b3d
MM
660 lockdep_assert_held(&qp->r_lock);
661 lockdep_assert_held(&qp->s_hlock);
662 lockdep_assert_held(&qp->s_lock);
222f7a9a
MM
663}
664
b518d3e6
DD
665/**
666 * rvt_create_qp - create a queue pair for a device
667 * @ibpd: the protection domain who's device we create the queue pair for
668 * @init_attr: the attributes of the queue pair
669 * @udata: user data for libibverbs.so
670 *
515667f8
DD
671 * Queue pair creation is mostly an rvt issue. However, drivers have their own
672 * unique idea of what queue pair numbers mean. For instance there is a reserved
673 * range for PSM.
674 *
90793f71 675 * Return: the queue pair on success, otherwise returns an errno.
b518d3e6
DD
676 *
677 * Called by the ib_create_qp() core verbs function.
678 */
679struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
680 struct ib_qp_init_attr *init_attr,
681 struct ib_udata *udata)
682{
515667f8
DD
683 struct rvt_qp *qp;
684 int err;
685 struct rvt_swqe *swq = NULL;
686 size_t sz;
687 size_t sg_list_sz;
688 struct ib_qp *ret = ERR_PTR(-ENOMEM);
689 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
690 void *priv = NULL;
d2b8d4da 691 gfp_t gfp;
afcf8f76 692 size_t sqsize;
515667f8
DD
693
694 if (!rdi)
695 return ERR_PTR(-EINVAL);
696
697 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
698 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
d2b8d4da 699 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
515667f8
DD
700 return ERR_PTR(-EINVAL);
701
d2b8d4da
MM
702 /* GFP_NOIO is applicable to RC QP's only */
703
704 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
705 init_attr->qp_type != IB_QPT_RC)
706 return ERR_PTR(-EINVAL);
707
708 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
709 GFP_NOIO : GFP_KERNEL;
710
515667f8
DD
711 /* Check receive queue parameters if no SRQ is specified. */
712 if (!init_attr->srq) {
713 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
714 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
715 return ERR_PTR(-EINVAL);
716
717 if (init_attr->cap.max_send_sge +
718 init_attr->cap.max_send_wr +
719 init_attr->cap.max_recv_sge +
720 init_attr->cap.max_recv_wr == 0)
721 return ERR_PTR(-EINVAL);
722 }
afcf8f76 723 sqsize =
856cc4c2
MM
724 init_attr->cap.max_send_wr + 1 +
725 rdi->dparms.reserved_operations;
515667f8
DD
726 switch (init_attr->qp_type) {
727 case IB_QPT_SMI:
728 case IB_QPT_GSI:
729 if (init_attr->port_num == 0 ||
730 init_attr->port_num > ibpd->device->phys_port_cnt)
731 return ERR_PTR(-EINVAL);
732 case IB_QPT_UC:
733 case IB_QPT_RC:
734 case IB_QPT_UD:
735 sz = sizeof(struct rvt_sge) *
736 init_attr->cap.max_send_sge +
737 sizeof(struct rvt_swqe);
d2b8d4da
MM
738 if (gfp == GFP_NOIO)
739 swq = __vmalloc(
afcf8f76 740 sqsize * sz,
654b6436 741 gfp | __GFP_ZERO, PAGE_KERNEL);
d2b8d4da 742 else
654b6436 743 swq = vzalloc_node(
afcf8f76 744 sqsize * sz,
d1b697b6 745 rdi->dparms.node);
515667f8
DD
746 if (!swq)
747 return ERR_PTR(-ENOMEM);
748
749 sz = sizeof(*qp);
750 sg_list_sz = 0;
751 if (init_attr->srq) {
752 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
753
754 if (srq->rq.max_sge > 1)
755 sg_list_sz = sizeof(*qp->r_sg_list) *
756 (srq->rq.max_sge - 1);
757 } else if (init_attr->cap.max_recv_sge > 1)
758 sg_list_sz = sizeof(*qp->r_sg_list) *
759 (init_attr->cap.max_recv_sge - 1);
d1b697b6 760 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
515667f8
DD
761 if (!qp)
762 goto bail_swq;
763
764 RCU_INIT_POINTER(qp->next, NULL);
8b103e9c
MM
765 if (init_attr->qp_type == IB_QPT_RC) {
766 qp->s_ack_queue =
767 kzalloc_node(
768 sizeof(*qp->s_ack_queue) *
769 rvt_max_atomic(rdi),
770 gfp,
771 rdi->dparms.node);
772 if (!qp->s_ack_queue)
773 goto bail_qp;
774 }
11a10d4b
VSD
775 /* initialize timers needed for rc qp */
776 setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
777 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
778 HRTIMER_MODE_REL);
779 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
515667f8
DD
780
781 /*
782 * Driver needs to set up it's private QP structure and do any
783 * initialization that is needed.
784 */
d2b8d4da 785 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
c755f4af
MM
786 if (IS_ERR(priv)) {
787 ret = priv;
515667f8 788 goto bail_qp;
c755f4af 789 }
515667f8
DD
790 qp->priv = priv;
791 qp->timeout_jiffies =
792 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
793 1000UL);
794 if (init_attr->srq) {
795 sz = 0;
796 } else {
797 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
798 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
799 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
800 sizeof(struct rvt_rwqe);
d2b8d4da
MM
801 if (udata)
802 qp->r_rq.wq = vmalloc_user(
803 sizeof(struct rvt_rwq) +
804 qp->r_rq.size * sz);
805 else if (gfp == GFP_NOIO)
806 qp->r_rq.wq = __vmalloc(
807 sizeof(struct rvt_rwq) +
808 qp->r_rq.size * sz,
654b6436 809 gfp | __GFP_ZERO, PAGE_KERNEL);
d2b8d4da 810 else
654b6436 811 qp->r_rq.wq = vzalloc_node(
d2b8d4da 812 sizeof(struct rvt_rwq) +
d1b697b6
MH
813 qp->r_rq.size * sz,
814 rdi->dparms.node);
515667f8
DD
815 if (!qp->r_rq.wq)
816 goto bail_driver_priv;
817 }
818
819 /*
820 * ib_create_qp() will initialize qp->ibqp
821 * except for qp->ibqp.qp_num.
822 */
823 spin_lock_init(&qp->r_lock);
46a80d62 824 spin_lock_init(&qp->s_hlock);
515667f8
DD
825 spin_lock_init(&qp->s_lock);
826 spin_lock_init(&qp->r_rq.lock);
827 atomic_set(&qp->refcount, 0);
d9f87239 828 atomic_set(&qp->local_ops_pending, 0);
515667f8
DD
829 init_waitqueue_head(&qp->wait);
830 init_timer(&qp->s_timer);
831 qp->s_timer.data = (unsigned long)qp;
832 INIT_LIST_HEAD(&qp->rspwait);
833 qp->state = IB_QPS_RESET;
834 qp->s_wq = swq;
afcf8f76 835 qp->s_size = sqsize;
46a80d62 836 qp->s_avail = init_attr->cap.max_send_wr;
515667f8
DD
837 qp->s_max_sge = init_attr->cap.max_send_sge;
838 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
839 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
840
841 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
842 init_attr->qp_type,
d2b8d4da 843 init_attr->port_num, gfp);
515667f8
DD
844 if (err < 0) {
845 ret = ERR_PTR(err);
846 goto bail_rq_wq;
847 }
848 qp->ibqp.qp_num = err;
849 qp->port_num = init_attr->port_num;
222f7a9a 850 rvt_init_qp(rdi, qp, init_attr->qp_type);
515667f8
DD
851 break;
852
853 default:
854 /* Don't support raw QPs */
855 return ERR_PTR(-EINVAL);
856 }
857
858 init_attr->cap.max_inline_data = 0;
859
860 /*
861 * Return the address of the RWQ as the offset to mmap.
bfbac097 862 * See rvt_mmap() for details.
515667f8
DD
863 */
864 if (udata && udata->outlen >= sizeof(__u64)) {
865 if (!qp->r_rq.wq) {
866 __u64 offset = 0;
867
868 err = ib_copy_to_udata(udata, &offset,
869 sizeof(offset));
870 if (err) {
871 ret = ERR_PTR(err);
872 goto bail_qpn;
873 }
874 } else {
875 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
876
877 qp->ip = rvt_create_mmap_info(rdi, s,
878 ibpd->uobject->context,
879 qp->r_rq.wq);
880 if (!qp->ip) {
881 ret = ERR_PTR(-ENOMEM);
882 goto bail_qpn;
883 }
884
885 err = ib_copy_to_udata(udata, &qp->ip->offset,
886 sizeof(qp->ip->offset));
887 if (err) {
888 ret = ERR_PTR(err);
889 goto bail_ip;
890 }
891 }
ef086c0d 892 qp->pid = current->pid;
515667f8
DD
893 }
894
895 spin_lock(&rdi->n_qps_lock);
896 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
897 spin_unlock(&rdi->n_qps_lock);
898 ret = ERR_PTR(-ENOMEM);
899 goto bail_ip;
900 }
901
902 rdi->n_qps_allocated++;
bfee5e32
VM
903 /*
904 * Maintain a busy_jiffies variable that will be added to the timeout
905 * period in mod_retry_timer and add_retry_timer. This busy jiffies
906 * is scaled by the number of rc qps created for the device to reduce
907 * the number of timeouts occurring when there is a large number of
908 * qps. busy_jiffies is incremented every rc qp scaling interval.
909 * The scaling interval is selected based on extensive performance
910 * evaluation of targeted workloads.
911 */
912 if (init_attr->qp_type == IB_QPT_RC) {
913 rdi->n_rc_qps++;
914 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
915 }
515667f8
DD
916 spin_unlock(&rdi->n_qps_lock);
917
918 if (qp->ip) {
919 spin_lock_irq(&rdi->pending_lock);
920 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
921 spin_unlock_irq(&rdi->pending_lock);
922 }
923
924 ret = &qp->ibqp;
925
b518d3e6 926 /*
515667f8
DD
927 * We have our QP and its good, now keep track of what types of opcodes
928 * can be processed on this QP. We do this by keeping track of what the
929 * 3 high order bits of the opcode are.
b518d3e6 930 */
515667f8
DD
931 switch (init_attr->qp_type) {
932 case IB_QPT_SMI:
933 case IB_QPT_GSI:
934 case IB_QPT_UD:
b218f786 935 qp->allowed_ops = IB_OPCODE_UD;
515667f8
DD
936 break;
937 case IB_QPT_RC:
b218f786 938 qp->allowed_ops = IB_OPCODE_RC;
515667f8
DD
939 break;
940 case IB_QPT_UC:
b218f786 941 qp->allowed_ops = IB_OPCODE_UC;
515667f8
DD
942 break;
943 default:
944 ret = ERR_PTR(-EINVAL);
945 goto bail_ip;
946 }
947
948 return ret;
949
950bail_ip:
22dccc54
JF
951 if (qp->ip)
952 kref_put(&qp->ip->ref, rvt_release_mmap_info);
515667f8
DD
953
954bail_qpn:
955 free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
956
957bail_rq_wq:
56c8ca51
MM
958 if (!qp->ip)
959 vfree(qp->r_rq.wq);
515667f8
DD
960
961bail_driver_priv:
962 rdi->driver_f.qp_priv_free(rdi, qp);
963
964bail_qp:
8b103e9c 965 kfree(qp->s_ack_queue);
515667f8
DD
966 kfree(qp);
967
968bail_swq:
969 vfree(swq);
970
971 return ret;
b518d3e6
DD
972}
973
3b0b3fb3
DD
974/**
975 * rvt_error_qp - put a QP into the error state
976 * @qp: the QP to put into the error state
977 * @err: the receive completion error to signal if a RWQE is active
978 *
979 * Flushes both send and receive work queues.
90793f71
DD
980 *
981 * Return: true if last WQE event should be generated.
3b0b3fb3
DD
982 * The QP r_lock and s_lock should be held and interrupts disabled.
983 * If we are already in error state, just return.
984 */
985int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
986{
987 struct ib_wc wc;
988 int ret = 0;
989 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
990
68e78b3d
MM
991 lockdep_assert_held(&qp->r_lock);
992 lockdep_assert_held(&qp->s_lock);
3b0b3fb3
DD
993 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
994 goto bail;
995
996 qp->state = IB_QPS_ERR;
997
998 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
999 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1000 del_timer(&qp->s_timer);
1001 }
1002
1003 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
1004 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
1005
1006 rdi->driver_f.notify_error_qp(qp);
1007
1008 /* Schedule the sending tasklet to drain the send work queue. */
46a80d62 1009 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
3b0b3fb3
DD
1010 rdi->driver_f.schedule_send(qp);
1011
1012 rvt_clear_mr_refs(qp, 0);
1013
1014 memset(&wc, 0, sizeof(wc));
1015 wc.qp = &qp->ibqp;
1016 wc.opcode = IB_WC_RECV;
1017
1018 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1019 wc.wr_id = qp->r_wr_id;
1020 wc.status = err;
1021 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1022 }
1023 wc.status = IB_WC_WR_FLUSH_ERR;
1024
1025 if (qp->r_rq.wq) {
1026 struct rvt_rwq *wq;
1027 u32 head;
1028 u32 tail;
1029
1030 spin_lock(&qp->r_rq.lock);
1031
1032 /* sanity check pointers before trusting them */
1033 wq = qp->r_rq.wq;
1034 head = wq->head;
1035 if (head >= qp->r_rq.size)
1036 head = 0;
1037 tail = wq->tail;
1038 if (tail >= qp->r_rq.size)
1039 tail = 0;
1040 while (tail != head) {
1041 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1042 if (++tail >= qp->r_rq.size)
1043 tail = 0;
1044 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1045 }
1046 wq->tail = tail;
1047
1048 spin_unlock(&qp->r_rq.lock);
1049 } else if (qp->ibqp.event_handler) {
1050 ret = 1;
1051 }
1052
1053bail:
1054 return ret;
1055}
1056EXPORT_SYMBOL(rvt_error_qp);
1057
1058/*
1059 * Put the QP into the hash table.
1060 * The hash table holds a reference to the QP.
1061 */
1062static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1063{
1064 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1065 unsigned long flags;
1066
4d6f85c3 1067 rvt_get_qp(qp);
3b0b3fb3
DD
1068 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1069
1070 if (qp->ibqp.qp_num <= 1) {
1071 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1072 } else {
1073 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1074
1075 qp->next = rdi->qp_dev->qp_table[n];
1076 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1077 trace_rvt_qpinsert(qp, n);
1078 }
1079
1080 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1081}
1082
b518d3e6 1083/**
61347fa6 1084 * rvt_modify_qp - modify the attributes of a queue pair
b518d3e6
DD
1085 * @ibqp: the queue pair who's attributes we're modifying
1086 * @attr: the new attributes
1087 * @attr_mask: the mask of attributes to modify
1088 * @udata: user data for libibverbs.so
1089 *
90793f71 1090 * Return: 0 on success, otherwise returns an errno.
b518d3e6
DD
1091 */
1092int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1093 int attr_mask, struct ib_udata *udata)
1094{
3b0b3fb3
DD
1095 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1096 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1097 enum ib_qp_state cur_state, new_state;
1098 struct ib_event ev;
1099 int lastwqe = 0;
1100 int mig = 0;
1101 int pmtu = 0; /* for gcc warning only */
1102 enum rdma_link_layer link;
1103
1104 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1105
1106 spin_lock_irq(&qp->r_lock);
46a80d62 1107 spin_lock(&qp->s_hlock);
3b0b3fb3
DD
1108 spin_lock(&qp->s_lock);
1109
1110 cur_state = attr_mask & IB_QP_CUR_STATE ?
1111 attr->cur_qp_state : qp->state;
1112 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1113
1114 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1115 attr_mask, link))
1116 goto inval;
1117
e85ec33d
IW
1118 if (rdi->driver_f.check_modify_qp &&
1119 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1120 goto inval;
1121
3b0b3fb3
DD
1122 if (attr_mask & IB_QP_AV) {
1123 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1124 goto inval;
1125 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1126 goto inval;
1127 }
1128
1129 if (attr_mask & IB_QP_ALT_PATH) {
1130 if (attr->alt_ah_attr.dlid >=
1131 be16_to_cpu(IB_MULTICAST_LID_BASE))
1132 goto inval;
1133 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1134 goto inval;
1135 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1136 goto inval;
1137 }
1138
1139 if (attr_mask & IB_QP_PKEY_INDEX)
1140 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1141 goto inval;
1142
1143 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1144 if (attr->min_rnr_timer > 31)
1145 goto inval;
1146
1147 if (attr_mask & IB_QP_PORT)
1148 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1149 qp->ibqp.qp_type == IB_QPT_GSI ||
1150 attr->port_num == 0 ||
1151 attr->port_num > ibqp->device->phys_port_cnt)
1152 goto inval;
1153
1154 if (attr_mask & IB_QP_DEST_QPN)
1155 if (attr->dest_qp_num > RVT_QPN_MASK)
1156 goto inval;
1157
1158 if (attr_mask & IB_QP_RETRY_CNT)
1159 if (attr->retry_cnt > 7)
1160 goto inval;
1161
1162 if (attr_mask & IB_QP_RNR_RETRY)
1163 if (attr->rnr_retry > 7)
1164 goto inval;
1165
b518d3e6 1166 /*
3b0b3fb3
DD
1167 * Don't allow invalid path_mtu values. OK to set greater
1168 * than the active mtu (or even the max_cap, if we have tuned
1169 * that to a small mtu. We'll set qp->path_mtu
1170 * to the lesser of requested attribute mtu and active,
1171 * for packetizing messages.
1172 * Note that the QP port has to be set in INIT and MTU in RTR.
b518d3e6 1173 */
3b0b3fb3
DD
1174 if (attr_mask & IB_QP_PATH_MTU) {
1175 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1176 if (pmtu < 0)
1177 goto inval;
1178 }
1179
1180 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1181 if (attr->path_mig_state == IB_MIG_REARM) {
1182 if (qp->s_mig_state == IB_MIG_ARMED)
1183 goto inval;
1184 if (new_state != IB_QPS_RTS)
1185 goto inval;
1186 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1187 if (qp->s_mig_state == IB_MIG_REARM)
1188 goto inval;
1189 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1190 goto inval;
1191 if (qp->s_mig_state == IB_MIG_ARMED)
1192 mig = 1;
1193 } else {
1194 goto inval;
1195 }
1196 }
1197
1198 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1199 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1200 goto inval;
1201
1202 switch (new_state) {
1203 case IB_QPS_RESET:
1204 if (qp->state != IB_QPS_RESET)
1205 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1206 break;
1207
1208 case IB_QPS_RTR:
1209 /* Allow event to re-trigger if QP set to RTR more than once */
1210 qp->r_flags &= ~RVT_R_COMM_EST;
1211 qp->state = new_state;
1212 break;
1213
1214 case IB_QPS_SQD:
1215 qp->s_draining = qp->s_last != qp->s_cur;
1216 qp->state = new_state;
1217 break;
1218
1219 case IB_QPS_SQE:
1220 if (qp->ibqp.qp_type == IB_QPT_RC)
1221 goto inval;
1222 qp->state = new_state;
1223 break;
1224
1225 case IB_QPS_ERR:
1226 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1227 break;
1228
1229 default:
1230 qp->state = new_state;
1231 break;
1232 }
1233
1234 if (attr_mask & IB_QP_PKEY_INDEX)
1235 qp->s_pkey_index = attr->pkey_index;
1236
1237 if (attr_mask & IB_QP_PORT)
1238 qp->port_num = attr->port_num;
1239
1240 if (attr_mask & IB_QP_DEST_QPN)
1241 qp->remote_qpn = attr->dest_qp_num;
1242
1243 if (attr_mask & IB_QP_SQ_PSN) {
1244 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1245 qp->s_psn = qp->s_next_psn;
1246 qp->s_sending_psn = qp->s_next_psn;
1247 qp->s_last_psn = qp->s_next_psn - 1;
1248 qp->s_sending_hpsn = qp->s_last_psn;
1249 }
1250
1251 if (attr_mask & IB_QP_RQ_PSN)
1252 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1253
1254 if (attr_mask & IB_QP_ACCESS_FLAGS)
1255 qp->qp_access_flags = attr->qp_access_flags;
1256
1257 if (attr_mask & IB_QP_AV) {
1258 qp->remote_ah_attr = attr->ah_attr;
1259 qp->s_srate = attr->ah_attr.static_rate;
1260 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1261 }
1262
1263 if (attr_mask & IB_QP_ALT_PATH) {
1264 qp->alt_ah_attr = attr->alt_ah_attr;
1265 qp->s_alt_pkey_index = attr->alt_pkey_index;
1266 }
1267
1268 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1269 qp->s_mig_state = attr->path_mig_state;
1270 if (mig) {
1271 qp->remote_ah_attr = qp->alt_ah_attr;
1272 qp->port_num = qp->alt_ah_attr.port_num;
1273 qp->s_pkey_index = qp->s_alt_pkey_index;
3b0b3fb3
DD
1274 }
1275 }
1276
1277 if (attr_mask & IB_QP_PATH_MTU) {
1278 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1279 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
46a80d62 1280 qp->log_pmtu = ilog2(qp->pmtu);
3b0b3fb3
DD
1281 }
1282
1283 if (attr_mask & IB_QP_RETRY_CNT) {
1284 qp->s_retry_cnt = attr->retry_cnt;
1285 qp->s_retry = attr->retry_cnt;
1286 }
1287
1288 if (attr_mask & IB_QP_RNR_RETRY) {
1289 qp->s_rnr_retry_cnt = attr->rnr_retry;
1290 qp->s_rnr_retry = attr->rnr_retry;
1291 }
1292
1293 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1294 qp->r_min_rnr_timer = attr->min_rnr_timer;
1295
1296 if (attr_mask & IB_QP_TIMEOUT) {
1297 qp->timeout = attr->timeout;
1298 qp->timeout_jiffies =
1299 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1300 1000UL);
1301 }
1302
1303 if (attr_mask & IB_QP_QKEY)
1304 qp->qkey = attr->qkey;
1305
1306 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1307 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1308
1309 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1310 qp->s_max_rd_atomic = attr->max_rd_atomic;
1311
e85ec33d
IW
1312 if (rdi->driver_f.modify_qp)
1313 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1314
3b0b3fb3 1315 spin_unlock(&qp->s_lock);
46a80d62 1316 spin_unlock(&qp->s_hlock);
3b0b3fb3
DD
1317 spin_unlock_irq(&qp->r_lock);
1318
1319 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1320 rvt_insert_qp(rdi, qp);
1321
1322 if (lastwqe) {
1323 ev.device = qp->ibqp.device;
1324 ev.element.qp = &qp->ibqp;
1325 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1326 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1327 }
1328 if (mig) {
1329 ev.device = qp->ibqp.device;
1330 ev.element.qp = &qp->ibqp;
1331 ev.event = IB_EVENT_PATH_MIG;
1332 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1333 }
1334 return 0;
1335
1336inval:
1337 spin_unlock(&qp->s_lock);
46a80d62 1338 spin_unlock(&qp->s_hlock);
3b0b3fb3
DD
1339 spin_unlock_irq(&qp->r_lock);
1340 return -EINVAL;
b518d3e6
DD
1341}
1342
79a225be
DD
1343/** rvt_free_qpn - Free a qpn from the bit map
1344 * @qpt: QP table
1345 * @qpn: queue pair number to free
1346 */
1347static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1348{
1349 struct rvt_qpn_map *map;
1350
1351 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1352 if (map->page)
1353 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1354}
1355
b518d3e6
DD
1356/**
1357 * rvt_destroy_qp - destroy a queue pair
1358 * @ibqp: the queue pair to destroy
1359 *
b518d3e6
DD
1360 * Note that this can be called while the QP is actively sending or
1361 * receiving!
90793f71
DD
1362 *
1363 * Return: 0 on success.
b518d3e6
DD
1364 */
1365int rvt_destroy_qp(struct ib_qp *ibqp)
1366{
5a17ad11
DD
1367 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1368 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
b518d3e6 1369
5a17ad11 1370 spin_lock_irq(&qp->r_lock);
46a80d62 1371 spin_lock(&qp->s_hlock);
5a17ad11
DD
1372 spin_lock(&qp->s_lock);
1373 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1374 spin_unlock(&qp->s_lock);
46a80d62 1375 spin_unlock(&qp->s_hlock);
5a17ad11
DD
1376 spin_unlock_irq(&qp->r_lock);
1377
1378 /* qpn is now available for use again */
1379 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1380
1381 spin_lock(&rdi->n_qps_lock);
1382 rdi->n_qps_allocated--;
bfee5e32
VM
1383 if (qp->ibqp.qp_type == IB_QPT_RC) {
1384 rdi->n_rc_qps--;
1385 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1386 }
5a17ad11
DD
1387 spin_unlock(&rdi->n_qps_lock);
1388
1389 if (qp->ip)
1390 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1391 else
1392 vfree(qp->r_rq.wq);
1393 vfree(qp->s_wq);
1394 rdi->driver_f.qp_priv_free(rdi, qp);
8b103e9c 1395 kfree(qp->s_ack_queue);
5a17ad11
DD
1396 kfree(qp);
1397 return 0;
b518d3e6
DD
1398}
1399
90793f71
DD
1400/**
1401 * rvt_query_qp - query an ipbq
1402 * @ibqp: IB qp to query
1403 * @attr: attr struct to fill in
1404 * @attr_mask: attr mask ignored
1405 * @init_attr: struct to fill in
1406 *
1407 * Return: always 0
1408 */
b518d3e6
DD
1409int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1410 int attr_mask, struct ib_qp_init_attr *init_attr)
1411{
74d2d500
HC
1412 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1413 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1414
1415 attr->qp_state = qp->state;
1416 attr->cur_qp_state = attr->qp_state;
1417 attr->path_mtu = qp->path_mtu;
1418 attr->path_mig_state = qp->s_mig_state;
1419 attr->qkey = qp->qkey;
1420 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1421 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1422 attr->dest_qp_num = qp->remote_qpn;
1423 attr->qp_access_flags = qp->qp_access_flags;
856cc4c2
MM
1424 attr->cap.max_send_wr = qp->s_size - 1 -
1425 rdi->dparms.reserved_operations;
74d2d500
HC
1426 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1427 attr->cap.max_send_sge = qp->s_max_sge;
1428 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1429 attr->cap.max_inline_data = 0;
1430 attr->ah_attr = qp->remote_ah_attr;
1431 attr->alt_ah_attr = qp->alt_ah_attr;
1432 attr->pkey_index = qp->s_pkey_index;
1433 attr->alt_pkey_index = qp->s_alt_pkey_index;
1434 attr->en_sqd_async_notify = 0;
1435 attr->sq_draining = qp->s_draining;
1436 attr->max_rd_atomic = qp->s_max_rd_atomic;
1437 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1438 attr->min_rnr_timer = qp->r_min_rnr_timer;
1439 attr->port_num = qp->port_num;
1440 attr->timeout = qp->timeout;
1441 attr->retry_cnt = qp->s_retry_cnt;
1442 attr->rnr_retry = qp->s_rnr_retry_cnt;
1443 attr->alt_port_num = qp->alt_ah_attr.port_num;
1444 attr->alt_timeout = qp->alt_timeout;
1445
1446 init_attr->event_handler = qp->ibqp.event_handler;
1447 init_attr->qp_context = qp->ibqp.qp_context;
1448 init_attr->send_cq = qp->ibqp.send_cq;
1449 init_attr->recv_cq = qp->ibqp.recv_cq;
1450 init_attr->srq = qp->ibqp.srq;
1451 init_attr->cap = attr->cap;
1452 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1453 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1454 else
1455 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1456 init_attr->qp_type = qp->ibqp.qp_type;
1457 init_attr->port_num = qp->port_num;
1458 return 0;
b518d3e6 1459}
8cf4020b
DD
1460
1461/**
1462 * rvt_post_receive - post a receive on a QP
1463 * @ibqp: the QP to post the receive on
1464 * @wr: the WR to post
1465 * @bad_wr: the first bad WR is put here
1466 *
1467 * This may be called from interrupt context.
90793f71
DD
1468 *
1469 * Return: 0 on success otherwise errno
8cf4020b
DD
1470 */
1471int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1472 struct ib_recv_wr **bad_wr)
1473{
120bdafa
DD
1474 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1475 struct rvt_rwq *wq = qp->r_rq.wq;
1476 unsigned long flags;
000a830e
AE
1477 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1478 !qp->ibqp.srq;
8cf4020b 1479
120bdafa
DD
1480 /* Check that state is OK to post receive. */
1481 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1482 *bad_wr = wr;
1483 return -EINVAL;
1484 }
1485
1486 for (; wr; wr = wr->next) {
1487 struct rvt_rwqe *wqe;
1488 u32 next;
1489 int i;
1490
1491 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1492 *bad_wr = wr;
1493 return -EINVAL;
1494 }
1495
1496 spin_lock_irqsave(&qp->r_rq.lock, flags);
1497 next = wq->head + 1;
1498 if (next >= qp->r_rq.size)
1499 next = 0;
1500 if (next == wq->tail) {
1501 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1502 *bad_wr = wr;
1503 return -ENOMEM;
1504 }
000a830e
AE
1505 if (unlikely(qp_err_flush)) {
1506 struct ib_wc wc;
1507
1508 memset(&wc, 0, sizeof(wc));
1509 wc.qp = &qp->ibqp;
1510 wc.opcode = IB_WC_RECV;
1511 wc.wr_id = wr->wr_id;
1512 wc.status = IB_WC_WR_FLUSH_ERR;
1513 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1514 } else {
1515 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1516 wqe->wr_id = wr->wr_id;
1517 wqe->num_sge = wr->num_sge;
1518 for (i = 0; i < wr->num_sge; i++)
1519 wqe->sg_list[i] = wr->sg_list[i];
1520 /*
1521 * Make sure queue entry is written
1522 * before the head index.
1523 */
1524 smp_wmb();
1525 wq->head = next;
1526 }
120bdafa
DD
1527 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1528 }
1529 return 0;
8cf4020b
DD
1530}
1531
46a80d62 1532/**
afcf8f76
MM
1533 * rvt_qp_valid_operation - validate post send wr request
1534 * @qp - the qp
1535 * @post-parms - the post send table for the driver
1536 * @wr - the work request
46a80d62 1537 *
afcf8f76
MM
1538 * The routine validates the operation based on the
1539 * validation table an returns the length of the operation
1540 * which can extend beyond the ib_send_bw. Operation
1541 * dependent flags key atomic operation validation.
1542 *
1543 * There is an exception for UD qps that validates the pd and
1544 * overrides the length to include the additional UD specific
1545 * length.
1546 *
1547 * Returns a negative error or the length of the work request
1548 * for building the swqe.
1549 */
1550static inline int rvt_qp_valid_operation(
1551 struct rvt_qp *qp,
1552 const struct rvt_operation_params *post_parms,
1553 struct ib_send_wr *wr)
1554{
1555 int len;
1556
1557 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1558 return -EINVAL;
1559 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1560 return -EINVAL;
1561 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1562 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1563 return -EINVAL;
1564 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1565 (wr->num_sge == 0 ||
1566 wr->sg_list[0].length < sizeof(u64) ||
1567 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1568 return -EINVAL;
1569 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1570 !qp->s_max_rd_atomic)
1571 return -EINVAL;
1572 len = post_parms[wr->opcode].length;
1573 /* UD specific */
1574 if (qp->ibqp.qp_type != IB_QPT_UC &&
1575 qp->ibqp.qp_type != IB_QPT_RC) {
1576 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1577 return -EINVAL;
1578 len = sizeof(struct ib_ud_wr);
1579 }
1580 return len;
1581}
1582
1583/**
856cc4c2 1584 * rvt_qp_is_avail - determine queue capacity
46a80d62 1585 * @qp - the qp
856cc4c2
MM
1586 * @rdi - the rdmavt device
1587 * @reserved_op - is reserved operation
46a80d62
MM
1588 *
1589 * This assumes the s_hlock is held but the s_last
1590 * qp variable is uncontrolled.
afcf8f76 1591 *
856cc4c2
MM
1592 * For non reserved operations, the qp->s_avail
1593 * may be changed.
1594 *
1595 * The return value is zero or a -ENOMEM.
46a80d62 1596 */
856cc4c2
MM
1597static inline int rvt_qp_is_avail(
1598 struct rvt_qp *qp,
1599 struct rvt_dev_info *rdi,
1600 bool reserved_op)
46a80d62
MM
1601{
1602 u32 slast;
856cc4c2
MM
1603 u32 avail;
1604 u32 reserved_used;
1605
1606 /* see rvt_qp_wqe_unreserve() */
1607 smp_mb__before_atomic();
1608 reserved_used = atomic_read(&qp->s_reserved_used);
1609 if (unlikely(reserved_op)) {
1610 /* see rvt_qp_wqe_unreserve() */
1611 smp_mb__before_atomic();
1612 if (reserved_used >= rdi->dparms.reserved_operations)
1613 return -ENOMEM;
1614 return 0;
1615 }
1616 /* non-reserved operations */
1617 if (likely(qp->s_avail))
1618 return 0;
46a80d62
MM
1619 smp_read_barrier_depends(); /* see rc.c */
1620 slast = ACCESS_ONCE(qp->s_last);
1621 if (qp->s_head >= slast)
856cc4c2 1622 avail = qp->s_size - (qp->s_head - slast);
46a80d62 1623 else
856cc4c2
MM
1624 avail = slast - qp->s_head;
1625
1626 /* see rvt_qp_wqe_unreserve() */
1627 smp_mb__before_atomic();
1628 reserved_used = atomic_read(&qp->s_reserved_used);
1629 avail = avail - 1 -
1630 (rdi->dparms.reserved_operations - reserved_used);
1631 /* insure we don't assign a negative s_avail */
1632 if ((s32)avail <= 0)
1633 return -ENOMEM;
1634 qp->s_avail = avail;
1635 if (WARN_ON(qp->s_avail >
1636 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1637 rvt_pr_err(rdi,
1638 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1639 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1640 qp->s_head, qp->s_tail, qp->s_cur,
1641 qp->s_acked, qp->s_last);
1642 return 0;
46a80d62
MM
1643}
1644
bfbac097
DD
1645/**
1646 * rvt_post_one_wr - post one RC, UC, or UD send work request
1647 * @qp: the QP to post on
1648 * @wr: the work request to send
1649 */
91702b4a
MM
1650static int rvt_post_one_wr(struct rvt_qp *qp,
1651 struct ib_send_wr *wr,
1652 int *call_send)
bfbac097
DD
1653{
1654 struct rvt_swqe *wqe;
1655 u32 next;
1656 int i;
1657 int j;
1658 int acc;
1659 struct rvt_lkey_table *rkt;
1660 struct rvt_pd *pd;
1661 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
46a80d62
MM
1662 u8 log_pmtu;
1663 int ret;
2821c509 1664 size_t cplen;
856cc4c2 1665 bool reserved_op;
d9b13c20 1666 int local_ops_delayed = 0;
bfbac097 1667
afcf8f76
MM
1668 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1669
bfbac097
DD
1670 /* IB spec says that num_sge == 0 is OK. */
1671 if (unlikely(wr->num_sge > qp->s_max_sge))
1672 return -EINVAL;
1673
2821c509
MM
1674 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1675 if (ret < 0)
1676 return ret;
1677 cplen = ret;
1678
d9f87239 1679 /*
d9b13c20
JX
1680 * Local operations include fast register and local invalidate.
1681 * Fast register needs to be processed immediately because the
1682 * registered lkey may be used by following work requests and the
1683 * lkey needs to be valid at the time those requests are posted.
1684 * Local invalidate can be processed immediately if fencing is
1685 * not required and no previous local invalidate ops are pending.
1686 * Signaled local operations that have been processed immediately
1687 * need to have requests with "completion only" flags set posted
1688 * to the send queue in order to generate completions.
d9f87239 1689 */
d9b13c20 1690 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
d9f87239
JX
1691 switch (wr->opcode) {
1692 case IB_WR_REG_MR:
d9b13c20
JX
1693 ret = rvt_fast_reg_mr(qp,
1694 reg_wr(wr)->mr,
1695 reg_wr(wr)->key,
1696 reg_wr(wr)->access);
1697 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1698 return ret;
1699 break;
d9f87239 1700 case IB_WR_LOCAL_INV:
d9b13c20
JX
1701 if ((wr->send_flags & IB_SEND_FENCE) ||
1702 atomic_read(&qp->local_ops_pending)) {
1703 local_ops_delayed = 1;
1704 } else {
1705 ret = rvt_invalidate_rkey(
1706 qp, wr->ex.invalidate_rkey);
1707 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1708 return ret;
1709 }
1710 break;
d9f87239
JX
1711 default:
1712 return -EINVAL;
1713 }
1714 }
1715
856cc4c2
MM
1716 reserved_op = rdi->post_parms[wr->opcode].flags &
1717 RVT_OPERATION_USE_RESERVE;
46a80d62 1718 /* check for avail */
856cc4c2
MM
1719 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1720 if (ret)
1721 return ret;
bfbac097
DD
1722 next = qp->s_head + 1;
1723 if (next >= qp->s_size)
1724 next = 0;
60c30f57 1725
bfbac097
DD
1726 rkt = &rdi->lkey_table;
1727 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1728 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1729
2821c509
MM
1730 /* cplen has length from above */
1731 memcpy(&wqe->wr, wr, cplen);
bfbac097
DD
1732
1733 wqe->length = 0;
1734 j = 0;
1735 if (wr->num_sge) {
1736 acc = wr->opcode >= IB_WR_RDMA_READ ?
1737 IB_ACCESS_LOCAL_WRITE : 0;
1738 for (i = 0; i < wr->num_sge; i++) {
1739 u32 length = wr->sg_list[i].length;
1740 int ok;
1741
1742 if (length == 0)
1743 continue;
1744 ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1745 &wr->sg_list[i], acc);
46a80d62
MM
1746 if (!ok) {
1747 ret = -EINVAL;
bfbac097 1748 goto bail_inval_free;
46a80d62 1749 }
bfbac097
DD
1750 wqe->length += length;
1751 j++;
1752 }
1753 wqe->wr.num_sge = j;
1754 }
46a80d62
MM
1755
1756 /* general part of wqe valid - allow for driver checks */
1757 if (rdi->driver_f.check_send_wqe) {
1758 ret = rdi->driver_f.check_send_wqe(qp, wqe);
91702b4a 1759 if (ret < 0)
bfbac097 1760 goto bail_inval_free;
91702b4a
MM
1761 if (ret)
1762 *call_send = ret;
46a80d62
MM
1763 }
1764
1765 log_pmtu = qp->log_pmtu;
1766 if (qp->ibqp.qp_type != IB_QPT_UC &&
1767 qp->ibqp.qp_type != IB_QPT_RC) {
1768 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1769
1770 log_pmtu = ah->log_pmtu;
bfbac097
DD
1771 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1772 }
46a80d62 1773
d9f87239 1774 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
d9b13c20
JX
1775 if (local_ops_delayed)
1776 atomic_inc(&qp->local_ops_pending);
1777 else
1778 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
d9f87239
JX
1779 wqe->ssn = 0;
1780 wqe->psn = 0;
1781 wqe->lpsn = 0;
1782 } else {
1783 wqe->ssn = qp->s_ssn++;
1784 wqe->psn = qp->s_next_psn;
1785 wqe->lpsn = wqe->psn +
1786 (wqe->length ?
1787 ((wqe->length - 1) >> log_pmtu) :
1788 0);
1789 qp->s_next_psn = wqe->lpsn + 1;
1790 }
e16689e4 1791 trace_rvt_post_one_wr(qp, wqe);
856cc4c2
MM
1792 if (unlikely(reserved_op))
1793 rvt_qp_wqe_reserve(qp, wqe);
1794 else
1795 qp->s_avail--;
46a80d62 1796 smp_wmb(); /* see request builders */
bfbac097
DD
1797 qp->s_head = next;
1798
1799 return 0;
1800
1801bail_inval_free:
1802 /* release mr holds */
1803 while (j) {
1804 struct rvt_sge *sge = &wqe->sg_list[--j];
1805
1806 rvt_put_mr(sge->mr);
1807 }
46a80d62 1808 return ret;
bfbac097
DD
1809}
1810
8cf4020b
DD
1811/**
1812 * rvt_post_send - post a send on a QP
1813 * @ibqp: the QP to post the send on
1814 * @wr: the list of work requests to post
1815 * @bad_wr: the first bad WR is put here
1816 *
1817 * This may be called from interrupt context.
90793f71
DD
1818 *
1819 * Return: 0 on success else errno
8cf4020b
DD
1820 */
1821int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1822 struct ib_send_wr **bad_wr)
1823{
bfbac097
DD
1824 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1825 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1826 unsigned long flags = 0;
1827 int call_send;
1828 unsigned nreq = 0;
1829 int err = 0;
1830
46a80d62 1831 spin_lock_irqsave(&qp->s_hlock, flags);
bfbac097 1832
8cf4020b 1833 /*
bfbac097
DD
1834 * Ensure QP state is such that we can send. If not bail out early,
1835 * there is no need to do this every time we post a send.
8cf4020b 1836 */
bfbac097 1837 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
46a80d62 1838 spin_unlock_irqrestore(&qp->s_hlock, flags);
bfbac097
DD
1839 return -EINVAL;
1840 }
8cf4020b 1841
bfbac097
DD
1842 /*
1843 * If the send queue is empty, and we only have a single WR then just go
1844 * ahead and kick the send engine into gear. Otherwise we will always
1845 * just schedule the send to happen later.
1846 */
1847 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1848
1849 for (; wr; wr = wr->next) {
91702b4a 1850 err = rvt_post_one_wr(qp, wr, &call_send);
bfbac097
DD
1851 if (unlikely(err)) {
1852 *bad_wr = wr;
1853 goto bail;
1854 }
1855 nreq++;
1856 }
1857bail:
46a80d62
MM
1858 spin_unlock_irqrestore(&qp->s_hlock, flags);
1859 if (nreq) {
1860 if (call_send)
46a80d62 1861 rdi->driver_f.do_send(qp);
e6d2e017
JJ
1862 else
1863 rdi->driver_f.schedule_send_no_lock(qp);
46a80d62 1864 }
bfbac097 1865 return err;
8cf4020b
DD
1866}
1867
1868/**
1869 * rvt_post_srq_receive - post a receive on a shared receive queue
1870 * @ibsrq: the SRQ to post the receive on
1871 * @wr: the list of work requests to post
1872 * @bad_wr: A pointer to the first WR to cause a problem is put here
1873 *
1874 * This may be called from interrupt context.
90793f71
DD
1875 *
1876 * Return: 0 on success else errno
8cf4020b
DD
1877 */
1878int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1879 struct ib_recv_wr **bad_wr)
1880{
b8f881b9
JJ
1881 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1882 struct rvt_rwq *wq;
1883 unsigned long flags;
1884
1885 for (; wr; wr = wr->next) {
1886 struct rvt_rwqe *wqe;
1887 u32 next;
1888 int i;
1889
1890 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1891 *bad_wr = wr;
1892 return -EINVAL;
1893 }
1894
1895 spin_lock_irqsave(&srq->rq.lock, flags);
1896 wq = srq->rq.wq;
1897 next = wq->head + 1;
1898 if (next >= srq->rq.size)
1899 next = 0;
1900 if (next == wq->tail) {
1901 spin_unlock_irqrestore(&srq->rq.lock, flags);
1902 *bad_wr = wr;
1903 return -ENOMEM;
1904 }
1905
1906 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1907 wqe->wr_id = wr->wr_id;
1908 wqe->num_sge = wr->num_sge;
1909 for (i = 0; i < wr->num_sge; i++)
1910 wqe->sg_list[i] = wr->sg_list[i];
1911 /* Make sure queue entry is written before the head index. */
1912 smp_wmb();
1913 wq->head = next;
1914 spin_unlock_irqrestore(&srq->rq.lock, flags);
1915 }
1916 return 0;
8cf4020b 1917}
beb5a042
BW
1918
1919/**
1920 * qp_comm_est - handle trap with QP established
1921 * @qp: the QP
1922 */
1923void rvt_comm_est(struct rvt_qp *qp)
1924{
1925 qp->r_flags |= RVT_R_COMM_EST;
1926 if (qp->ibqp.event_handler) {
1927 struct ib_event ev;
1928
1929 ev.device = qp->ibqp.device;
1930 ev.element.qp = &qp->ibqp;
1931 ev.event = IB_EVENT_COMM_EST;
1932 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1933 }
1934}
1935EXPORT_SYMBOL(rvt_comm_est);
1936
1937void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
1938{
1939 unsigned long flags;
1940 int lastwqe;
1941
1942 spin_lock_irqsave(&qp->s_lock, flags);
1943 lastwqe = rvt_error_qp(qp, err);
1944 spin_unlock_irqrestore(&qp->s_lock, flags);
1945
1946 if (lastwqe) {
1947 struct ib_event ev;
1948
1949 ev.device = qp->ibqp.device;
1950 ev.element.qp = &qp->ibqp;
1951 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1952 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1953 }
1954}
1955EXPORT_SYMBOL(rvt_rc_error);
11a10d4b
VSD
1956
1957static inline unsigned long rvt_aeth_to_usec(u32 aeth)
1958{
1959 return ib_rvt_rnr_table[(aeth >> RVT_AETH_CREDIT_SHIFT) &
1960 RVT_AETH_CREDIT_MASK];
1961}
1962
1963/*
1964 * rvt_add_retry_timer - add/start a retry timer
1965 * @qp - the QP
1966 * add a retry timer on the QP
1967 */
1968void rvt_add_retry_timer(struct rvt_qp *qp)
1969{
1970 struct ib_qp *ibqp = &qp->ibqp;
1971 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1972
1973 lockdep_assert_held(&qp->s_lock);
1974 qp->s_flags |= RVT_S_TIMER;
1975 /* 4.096 usec. * (1 << qp->timeout) */
1976 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
1977 rdi->busy_jiffies;
1978 add_timer(&qp->s_timer);
1979}
1980EXPORT_SYMBOL(rvt_add_retry_timer);
1981
1982/**
1983 * rvt_add_rnr_timer - add/start an rnr timer
1984 * @qp - the QP
1985 * @aeth - aeth of RNR timeout, simulated aeth for loopback
1986 * add an rnr timer on the QP
1987 */
1988void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
1989{
1990 u32 to;
1991
1992 lockdep_assert_held(&qp->s_lock);
1993 qp->s_flags |= RVT_S_WAIT_RNR;
1994 to = rvt_aeth_to_usec(aeth);
1995 hrtimer_start(&qp->s_rnr_timer,
1996 ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
1997}
1998EXPORT_SYMBOL(rvt_add_rnr_timer);
1999
2000/**
2001 * rvt_stop_rc_timers - stop all timers
2002 * @qp - the QP
2003 * stop any pending timers
2004 */
2005void rvt_stop_rc_timers(struct rvt_qp *qp)
2006{
2007 lockdep_assert_held(&qp->s_lock);
2008 /* Remove QP from all timers */
2009 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2010 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2011 del_timer(&qp->s_timer);
2012 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2013 }
2014}
2015EXPORT_SYMBOL(rvt_stop_rc_timers);
2016
2017/**
2018 * rvt_stop_rnr_timer - stop an rnr timer
2019 * @qp - the QP
2020 *
2021 * stop an rnr timer and return if the timer
2022 * had been pending.
2023 */
2024static int rvt_stop_rnr_timer(struct rvt_qp *qp)
2025{
2026 int rval = 0;
2027
2028 lockdep_assert_held(&qp->s_lock);
2029 /* Remove QP from rnr timer */
2030 if (qp->s_flags & RVT_S_WAIT_RNR) {
2031 qp->s_flags &= ~RVT_S_WAIT_RNR;
2032 rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
2033 }
2034 return rval;
2035}
2036
2037/**
2038 * rvt_del_timers_sync - wait for any timeout routines to exit
2039 * @qp - the QP
2040 */
2041void rvt_del_timers_sync(struct rvt_qp *qp)
2042{
2043 del_timer_sync(&qp->s_timer);
2044 hrtimer_cancel(&qp->s_rnr_timer);
2045}
2046EXPORT_SYMBOL(rvt_del_timers_sync);
2047
2048/**
2049 * This is called from s_timer for missing responses.
2050 */
2051static void rvt_rc_timeout(unsigned long arg)
2052{
2053 struct rvt_qp *qp = (struct rvt_qp *)arg;
2054 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2055 unsigned long flags;
2056
2057 spin_lock_irqsave(&qp->r_lock, flags);
2058 spin_lock(&qp->s_lock);
2059 if (qp->s_flags & RVT_S_TIMER) {
2060 qp->s_flags &= ~RVT_S_TIMER;
2061 del_timer(&qp->s_timer);
2062 if (rdi->driver_f.notify_restart_rc)
2063 rdi->driver_f.notify_restart_rc(qp,
2064 qp->s_last_psn + 1,
2065 1);
2066 rdi->driver_f.schedule_send(qp);
2067 }
2068 spin_unlock(&qp->s_lock);
2069 spin_unlock_irqrestore(&qp->r_lock, flags);
2070}
2071
2072/*
2073 * This is called from s_timer for RNR timeouts.
2074 */
2075enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2076{
2077 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2078 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2079 unsigned long flags;
2080
2081 spin_lock_irqsave(&qp->s_lock, flags);
2082 rvt_stop_rnr_timer(qp);
2083 rdi->driver_f.schedule_send(qp);
2084 spin_unlock_irqrestore(&qp->s_lock, flags);
2085 return HRTIMER_NORESTART;
2086}
2087EXPORT_SYMBOL(rvt_rc_rnr_retry);