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