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