blob: eb0c3d60c584f10f0b5eb2373b6668eb16cd2c16 [file] [log] [blame]
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001/*
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002 * Copyright(c) 2016, 2017 Intel Corporation.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080048#include <linux/hash.h>
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080049#include <linux/bitops.h>
50#include <linux/lockdep.h>
Dennis Dalessandro515667f2016-01-22 12:50:17 -080051#include <linux/vmalloc.h>
52#include <linux/slab.h>
53#include <rdma/ib_verbs.h>
Don Hiatt832666c2017-02-08 05:28:25 -080054#include <rdma/ib_hdrs.h>
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080055#include "qp.h"
Dennis Dalessandro515667f2016-01-22 12:50:17 -080056#include "vt.h"
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080057#include "trace.h"
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080058
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -080059static 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
Dennis Dalessandrobfbac092016-01-22 13:00:22 -080099/*
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
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800120static void get_map_page(struct rvt_qpn_table *qpt,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300121 struct rvt_qpn_map *map)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800122{
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300123 unsigned long page = get_zeroed_page(GFP_KERNEL);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800124
125 /*
126 * Free the page if someone raced with us installing it.
127 */
128
129 spin_lock(&qpt->lock);
130 if (map->page)
131 free_page(page);
132 else
133 map->page = (void *)page;
134 spin_unlock(&qpt->lock);
135}
136
137/**
138 * init_qpn_table - initialize the QP number table for a device
139 * @qpt: the QPN table
140 */
141static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
142{
143 u32 offset, i;
144 struct rvt_qpn_map *map;
145 int ret = 0;
146
Harish Chegondifef2efd2016-01-22 12:50:30 -0800147 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800148 return -EINVAL;
149
150 spin_lock_init(&qpt->lock);
151
152 qpt->last = rdi->dparms.qpn_start;
153 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
154
155 /*
156 * Drivers may want some QPs beyond what we need for verbs let them use
157 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
158 * for those. The reserved range must be *after* the range which verbs
159 * will pick from.
160 */
161
162 /* Figure out number of bit maps needed before reserved range */
163 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
164
165 /* This should always be zero */
166 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
167
168 /* Starting with the first reserved bit map */
169 map = &qpt->map[qpt->nmaps];
170
171 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
172 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
Harish Chegondifef2efd2016-01-22 12:50:30 -0800173 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800174 if (!map->page) {
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300175 get_map_page(qpt, map);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800176 if (!map->page) {
177 ret = -ENOMEM;
178 break;
179 }
180 }
181 set_bit(offset, map->page);
182 offset++;
183 if (offset == RVT_BITS_PER_PAGE) {
184 /* next page */
185 qpt->nmaps++;
186 map++;
187 offset = 0;
188 }
189 }
190 return ret;
191}
192
193/**
194 * free_qpn_table - free the QP number table for a device
195 * @qpt: the QPN table
196 */
197static void free_qpn_table(struct rvt_qpn_table *qpt)
198{
199 int i;
200
201 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
202 free_page((unsigned long)qpt->map[i].page);
203}
204
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800205/**
206 * rvt_driver_qp_init - Init driver qp resources
207 * @rdi: rvt dev strucutre
208 *
209 * Return: 0 on success
210 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800211int rvt_driver_qp_init(struct rvt_dev_info *rdi)
212{
213 int i;
214 int ret = -ENOMEM;
215
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800216 if (!rdi->dparms.qp_table_size)
217 return -EINVAL;
218
219 /*
220 * If driver is not doing any QP allocation then make sure it is
221 * providing the necessary QP functions.
222 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800223 if (!rdi->driver_f.free_all_qps ||
224 !rdi->driver_f.qp_priv_alloc ||
225 !rdi->driver_f.qp_priv_free ||
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800226 !rdi->driver_f.notify_qp_reset ||
227 !rdi->driver_f.notify_restart_rc)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800228 return -EINVAL;
229
230 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800231 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
232 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800233 if (!rdi->qp_dev)
234 return -ENOMEM;
235
236 /* allocate hash table */
237 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
238 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
239 rdi->qp_dev->qp_table =
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800240 kmalloc_node(rdi->qp_dev->qp_table_size *
241 sizeof(*rdi->qp_dev->qp_table),
242 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800243 if (!rdi->qp_dev->qp_table)
244 goto no_qp_table;
245
246 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
247 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
248
249 spin_lock_init(&rdi->qp_dev->qpt_lock);
250
251 /* initialize qpn map */
252 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
253 goto fail_table;
254
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800255 spin_lock_init(&rdi->n_qps_lock);
256
257 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800258
259fail_table:
260 kfree(rdi->qp_dev->qp_table);
261 free_qpn_table(&rdi->qp_dev->qpn_table);
262
263no_qp_table:
264 kfree(rdi->qp_dev);
265
266 return ret;
267}
268
269/**
270 * free_all_qps - check for QPs still in use
271 * @qpt: the QP table to empty
272 *
273 * There should not be any QPs still in use.
274 * Free memory for table.
275 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800276static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800277{
278 unsigned long flags;
279 struct rvt_qp *qp;
280 unsigned n, qp_inuse = 0;
281 spinlock_t *ql; /* work around too long line below */
282
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800283 if (rdi->driver_f.free_all_qps)
284 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800285
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800286 qp_inuse += rvt_mcast_tree_empty(rdi);
287
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800288 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800289 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800290
291 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800292 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800293 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
294 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
295 lockdep_is_held(ql));
296 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800297
298 for (; qp; qp = rcu_dereference_protected(qp->next,
299 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800300 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800301 }
302 spin_unlock_irqrestore(ql, flags);
303 synchronize_rcu();
304 return qp_inuse;
305}
306
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800307/**
308 * rvt_qp_exit - clean up qps on device exit
309 * @rdi: rvt dev structure
310 *
311 * Check for qp leaks and free resources.
312 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800313void rvt_qp_exit(struct rvt_dev_info *rdi)
314{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800315 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800316
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800317 if (qps_inuse)
318 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
319 qps_inuse);
320 if (!rdi->qp_dev)
321 return;
322
323 kfree(rdi->qp_dev->qp_table);
324 free_qpn_table(&rdi->qp_dev->qpn_table);
325 kfree(rdi->qp_dev);
326}
327
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800328static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
329 struct rvt_qpn_map *map, unsigned off)
330{
331 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
332}
333
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800334/**
335 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
336 * IB_QPT_SMI/IB_QPT_GSI
337 *@rdi: rvt device info structure
338 *@qpt: queue pair number table pointer
339 *@port_num: IB port number, 1 based, comes from core
340 *
341 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800342 */
343static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300344 enum ib_qp_type type, u8 port_num)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800345{
346 u32 i, offset, max_scan, qpn;
347 struct rvt_qpn_map *map;
348 u32 ret;
349
350 if (rdi->driver_f.alloc_qpn)
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300351 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800352
353 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
354 unsigned n;
355
356 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800357 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800358 spin_lock(&qpt->lock);
359 if (qpt->flags & n)
360 ret = -EINVAL;
361 else
362 qpt->flags |= n;
363 spin_unlock(&qpt->lock);
364 goto bail;
365 }
366
367 qpn = qpt->last + qpt->incr;
368 if (qpn >= RVT_QPN_MAX)
369 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
370 /* offset carries bit 0 */
371 offset = qpn & RVT_BITS_PER_PAGE_MASK;
372 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
373 max_scan = qpt->nmaps - !offset;
374 for (i = 0;;) {
375 if (unlikely(!map->page)) {
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300376 get_map_page(qpt, map);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800377 if (unlikely(!map->page))
378 break;
379 }
380 do {
381 if (!test_and_set_bit(offset, map->page)) {
382 qpt->last = qpn;
383 ret = qpn;
384 goto bail;
385 }
386 offset += qpt->incr;
387 /*
388 * This qpn might be bogus if offset >= BITS_PER_PAGE.
389 * That is OK. It gets re-assigned below
390 */
391 qpn = mk_qpn(qpt, map, offset);
392 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
393 /*
394 * In order to keep the number of pages allocated to a
395 * minimum, we scan the all existing pages before increasing
396 * the size of the bitmap table.
397 */
398 if (++i > max_scan) {
399 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
400 break;
401 map = &qpt->map[qpt->nmaps++];
402 /* start at incr with current bit 0 */
403 offset = qpt->incr | (offset & 1);
404 } else if (map < &qpt->map[qpt->nmaps]) {
405 ++map;
406 /* start at incr with current bit 0 */
407 offset = qpt->incr | (offset & 1);
408 } else {
409 map = &qpt->map[0];
410 /* wrap to first map page, invert bit 0 */
411 offset = qpt->incr | ((offset & 1) ^ 1);
412 }
Brian Welty501edc422016-06-09 07:51:20 -0700413 /* there can be no set bits in low-order QoS bits */
414 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800415 qpn = mk_qpn(qpt, map, offset);
416 }
417
418 ret = -ENOMEM;
419
420bail:
421 return ret;
422}
423
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800424/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800425 * rvt_clear_mr_refs - Drop help mr refs
426 * @qp: rvt qp data structure
427 * @clr_sends: If shoudl clear send side or not
428 */
429static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
430{
431 unsigned n;
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700432 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800433
434 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
435 rvt_put_ss(&qp->s_rdma_read_sge);
436
437 rvt_put_ss(&qp->r_sge);
438
439 if (clr_sends) {
440 while (qp->s_last != qp->s_head) {
441 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
442 unsigned i;
443
444 for (i = 0; i < wqe->wr.num_sge; i++) {
445 struct rvt_sge *sge = &wqe->sg_list[i];
446
447 rvt_put_mr(sge->mr);
448 }
449 if (qp->ibqp.qp_type == IB_QPT_UD ||
450 qp->ibqp.qp_type == IB_QPT_SMI ||
451 qp->ibqp.qp_type == IB_QPT_GSI)
452 atomic_dec(&ibah_to_rvtah(
453 wqe->ud_wr.ah)->refcount);
454 if (++qp->s_last >= qp->s_size)
455 qp->s_last = 0;
456 smp_wmb(); /* see qp_set_savail */
457 }
458 if (qp->s_rdma_mr) {
459 rvt_put_mr(qp->s_rdma_mr);
460 qp->s_rdma_mr = NULL;
461 }
462 }
463
464 if (qp->ibqp.qp_type != IB_QPT_RC)
465 return;
466
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700467 for (n = 0; n < rvt_max_atomic(rdi); n++) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800468 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
469
Ira Weinyfe508272016-07-27 21:07:36 -0400470 if (e->rdma_sge.mr) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800471 rvt_put_mr(e->rdma_sge.mr);
472 e->rdma_sge.mr = NULL;
473 }
474 }
475}
476
477/**
478 * rvt_remove_qp - remove qp form table
479 * @rdi: rvt dev struct
480 * @qp: qp to remove
481 *
482 * Remove the QP from the table so it can't be found asynchronously by
483 * the receive routine.
484 */
485static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
486{
487 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
488 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
489 unsigned long flags;
490 int removed = 1;
491
492 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
493
494 if (rcu_dereference_protected(rvp->qp[0],
495 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
496 RCU_INIT_POINTER(rvp->qp[0], NULL);
497 } else if (rcu_dereference_protected(rvp->qp[1],
498 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
499 RCU_INIT_POINTER(rvp->qp[1], NULL);
500 } else {
501 struct rvt_qp *q;
502 struct rvt_qp __rcu **qpp;
503
504 removed = 0;
505 qpp = &rdi->qp_dev->qp_table[n];
506 for (; (q = rcu_dereference_protected(*qpp,
507 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
508 qpp = &q->next) {
509 if (q == qp) {
510 RCU_INIT_POINTER(*qpp,
511 rcu_dereference_protected(qp->next,
512 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
513 removed = 1;
514 trace_rvt_qpremove(qp, n);
515 break;
516 }
517 }
518 }
519
520 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
521 if (removed) {
522 synchronize_rcu();
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -0700523 rvt_put_qp(qp);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800524 }
525}
526
527/**
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700528 * rvt_init_qp - initialize the QP state to the reset state
529 * @qp: the QP to init or reinit
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800530 * @type: the QP type
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700531 *
532 * This function is called from both rvt_create_qp() and
533 * rvt_reset_qp(). The difference is that the reset
534 * patch the necessary locks to protect against concurent
535 * access.
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800536 */
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700537static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
538 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800539{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800540 qp->remote_qpn = 0;
541 qp->qkey = 0;
542 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800543 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
544 qp->s_hdrwords = 0;
545 qp->s_wqe = NULL;
546 qp->s_draining = 0;
547 qp->s_next_psn = 0;
548 qp->s_last_psn = 0;
549 qp->s_sending_psn = 0;
550 qp->s_sending_hpsn = 0;
551 qp->s_psn = 0;
552 qp->r_psn = 0;
553 qp->r_msn = 0;
554 if (type == IB_QPT_RC) {
555 qp->s_state = IB_OPCODE_RC_SEND_LAST;
556 qp->r_state = IB_OPCODE_RC_SEND_LAST;
557 } else {
558 qp->s_state = IB_OPCODE_UC_SEND_LAST;
559 qp->r_state = IB_OPCODE_UC_SEND_LAST;
560 }
561 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
562 qp->r_nak_state = 0;
563 qp->r_aflags = 0;
564 qp->r_flags = 0;
565 qp->s_head = 0;
566 qp->s_tail = 0;
567 qp->s_cur = 0;
568 qp->s_acked = 0;
569 qp->s_last = 0;
570 qp->s_ssn = 1;
571 qp->s_lsn = 0;
572 qp->s_mig_state = IB_MIG_MIGRATED;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800573 qp->r_head_ack_queue = 0;
574 qp->s_tail_ack_queue = 0;
575 qp->s_num_rd_atomic = 0;
576 if (qp->r_rq.wq) {
577 qp->r_rq.wq->head = 0;
578 qp->r_rq.wq->tail = 0;
579 }
580 qp->r_sge.num_sge = 0;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700581 atomic_set(&qp->s_reserved_used, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800582}
583
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800584/**
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700585 * rvt_reset_qp - initialize the QP state to the reset state
586 * @qp: the QP to reset
587 * @type: the QP type
588 *
589 * r_lock, s_hlock, and s_lock are required to be held by the caller
590 */
591static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
592 enum ib_qp_type type)
593 __must_hold(&qp->s_lock)
594 __must_hold(&qp->s_hlock)
595 __must_hold(&qp->r_lock)
596{
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700597 lockdep_assert_held(&qp->r_lock);
598 lockdep_assert_held(&qp->s_hlock);
599 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700600 if (qp->state != IB_QPS_RESET) {
601 qp->state = IB_QPS_RESET;
602
603 /* Let drivers flush their waitlist */
604 rdi->driver_f.flush_qp_waiters(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800605 rvt_stop_rc_timers(qp);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700606 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
607 spin_unlock(&qp->s_lock);
608 spin_unlock(&qp->s_hlock);
609 spin_unlock_irq(&qp->r_lock);
610
611 /* Stop the send queue and the retry timer */
612 rdi->driver_f.stop_send_queue(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800613 rvt_del_timers_sync(qp);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700614 /* Wait for things to stop */
615 rdi->driver_f.quiesce_qp(qp);
616
617 /* take qp out the hash and wait for it to be unused */
618 rvt_remove_qp(rdi, qp);
619 wait_event(qp->wait, !atomic_read(&qp->refcount));
620
621 /* grab the lock b/c it was locked at call time */
622 spin_lock_irq(&qp->r_lock);
623 spin_lock(&qp->s_hlock);
624 spin_lock(&qp->s_lock);
625
626 rvt_clear_mr_refs(qp, 1);
627 /*
628 * Let the driver do any tear down or re-init it needs to for
629 * a qp that has been reset
630 */
631 rdi->driver_f.notify_qp_reset(qp);
632 }
633 rvt_init_qp(rdi, qp, type);
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700634 lockdep_assert_held(&qp->r_lock);
635 lockdep_assert_held(&qp->s_hlock);
636 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700637}
638
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -0700639/** rvt_free_qpn - Free a qpn from the bit map
640 * @qpt: QP table
641 * @qpn: queue pair number to free
642 */
643static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
644{
645 struct rvt_qpn_map *map;
646
Dennis Dalessandro6c31e522017-05-29 17:19:21 -0700647 map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -0700648 if (map->page)
649 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
650}
651
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700652/**
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800653 * rvt_create_qp - create a queue pair for a device
654 * @ibpd: the protection domain who's device we create the queue pair for
655 * @init_attr: the attributes of the queue pair
656 * @udata: user data for libibverbs.so
657 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800658 * Queue pair creation is mostly an rvt issue. However, drivers have their own
659 * unique idea of what queue pair numbers mean. For instance there is a reserved
660 * range for PSM.
661 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800662 * Return: the queue pair on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800663 *
664 * Called by the ib_create_qp() core verbs function.
665 */
666struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
667 struct ib_qp_init_attr *init_attr,
668 struct ib_udata *udata)
669{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800670 struct rvt_qp *qp;
671 int err;
672 struct rvt_swqe *swq = NULL;
673 size_t sz;
674 size_t sg_list_sz;
675 struct ib_qp *ret = ERR_PTR(-ENOMEM);
676 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
677 void *priv = NULL;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700678 size_t sqsize;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800679
680 if (!rdi)
681 return ERR_PTR(-EINVAL);
682
683 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
684 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300685 init_attr->create_flags)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800686 return ERR_PTR(-EINVAL);
687
688 /* Check receive queue parameters if no SRQ is specified. */
689 if (!init_attr->srq) {
690 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
691 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
692 return ERR_PTR(-EINVAL);
693
694 if (init_attr->cap.max_send_sge +
695 init_attr->cap.max_send_wr +
696 init_attr->cap.max_recv_sge +
697 init_attr->cap.max_recv_wr == 0)
698 return ERR_PTR(-EINVAL);
699 }
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700700 sqsize =
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700701 init_attr->cap.max_send_wr + 1 +
702 rdi->dparms.reserved_operations;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800703 switch (init_attr->qp_type) {
704 case IB_QPT_SMI:
705 case IB_QPT_GSI:
706 if (init_attr->port_num == 0 ||
707 init_attr->port_num > ibpd->device->phys_port_cnt)
708 return ERR_PTR(-EINVAL);
709 case IB_QPT_UC:
710 case IB_QPT_RC:
711 case IB_QPT_UD:
712 sz = sizeof(struct rvt_sge) *
713 init_attr->cap.max_send_sge +
714 sizeof(struct rvt_swqe);
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300715 swq = vzalloc_node(sqsize * sz, rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800716 if (!swq)
717 return ERR_PTR(-ENOMEM);
718
719 sz = sizeof(*qp);
720 sg_list_sz = 0;
721 if (init_attr->srq) {
722 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
723
724 if (srq->rq.max_sge > 1)
725 sg_list_sz = sizeof(*qp->r_sg_list) *
726 (srq->rq.max_sge - 1);
727 } else if (init_attr->cap.max_recv_sge > 1)
728 sg_list_sz = sizeof(*qp->r_sg_list) *
729 (init_attr->cap.max_recv_sge - 1);
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300730 qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
731 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800732 if (!qp)
733 goto bail_swq;
734
735 RCU_INIT_POINTER(qp->next, NULL);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700736 if (init_attr->qp_type == IB_QPT_RC) {
737 qp->s_ack_queue =
738 kzalloc_node(
739 sizeof(*qp->s_ack_queue) *
740 rvt_max_atomic(rdi),
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300741 GFP_KERNEL,
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700742 rdi->dparms.node);
743 if (!qp->s_ack_queue)
744 goto bail_qp;
745 }
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800746 /* initialize timers needed for rc qp */
747 setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
748 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
749 HRTIMER_MODE_REL);
750 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800751
752 /*
753 * Driver needs to set up it's private QP structure and do any
754 * initialization that is needed.
755 */
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300756 priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700757 if (IS_ERR(priv)) {
758 ret = priv;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800759 goto bail_qp;
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700760 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800761 qp->priv = priv;
762 qp->timeout_jiffies =
763 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
764 1000UL);
765 if (init_attr->srq) {
766 sz = 0;
767 } else {
768 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
769 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
770 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
771 sizeof(struct rvt_rwqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800772 if (udata)
773 qp->r_rq.wq = vmalloc_user(
774 sizeof(struct rvt_rwq) +
775 qp->r_rq.size * sz);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800776 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700777 qp->r_rq.wq = vzalloc_node(
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800778 sizeof(struct rvt_rwq) +
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800779 qp->r_rq.size * sz,
780 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800781 if (!qp->r_rq.wq)
782 goto bail_driver_priv;
783 }
784
785 /*
786 * ib_create_qp() will initialize qp->ibqp
787 * except for qp->ibqp.qp_num.
788 */
789 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800790 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800791 spin_lock_init(&qp->s_lock);
792 spin_lock_init(&qp->r_rq.lock);
793 atomic_set(&qp->refcount, 0);
Jianxin Xiongd9f87232016-07-25 13:38:25 -0700794 atomic_set(&qp->local_ops_pending, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800795 init_waitqueue_head(&qp->wait);
796 init_timer(&qp->s_timer);
797 qp->s_timer.data = (unsigned long)qp;
798 INIT_LIST_HEAD(&qp->rspwait);
799 qp->state = IB_QPS_RESET;
800 qp->s_wq = swq;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700801 qp->s_size = sqsize;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800802 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800803 qp->s_max_sge = init_attr->cap.max_send_sge;
804 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
805 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
806
807 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
808 init_attr->qp_type,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300809 init_attr->port_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800810 if (err < 0) {
811 ret = ERR_PTR(err);
812 goto bail_rq_wq;
813 }
814 qp->ibqp.qp_num = err;
815 qp->port_num = init_attr->port_num;
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700816 rvt_init_qp(rdi, qp, init_attr->qp_type);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800817 break;
818
819 default:
820 /* Don't support raw QPs */
821 return ERR_PTR(-EINVAL);
822 }
823
824 init_attr->cap.max_inline_data = 0;
825
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800826 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800827 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800828 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800829 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800830 if (udata && udata->outlen >= sizeof(__u64)) {
831 if (!qp->r_rq.wq) {
832 __u64 offset = 0;
833
834 err = ib_copy_to_udata(udata, &offset,
835 sizeof(offset));
836 if (err) {
837 ret = ERR_PTR(err);
838 goto bail_qpn;
839 }
840 } else {
841 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
842
843 qp->ip = rvt_create_mmap_info(rdi, s,
844 ibpd->uobject->context,
845 qp->r_rq.wq);
846 if (!qp->ip) {
847 ret = ERR_PTR(-ENOMEM);
848 goto bail_qpn;
849 }
850
851 err = ib_copy_to_udata(udata, &qp->ip->offset,
852 sizeof(qp->ip->offset));
853 if (err) {
854 ret = ERR_PTR(err);
855 goto bail_ip;
856 }
857 }
Mike Marciniszynef086c02016-03-07 11:35:08 -0800858 qp->pid = current->pid;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800859 }
860
861 spin_lock(&rdi->n_qps_lock);
862 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
863 spin_unlock(&rdi->n_qps_lock);
864 ret = ERR_PTR(-ENOMEM);
865 goto bail_ip;
866 }
867
868 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800869 /*
870 * Maintain a busy_jiffies variable that will be added to the timeout
871 * period in mod_retry_timer and add_retry_timer. This busy jiffies
872 * is scaled by the number of rc qps created for the device to reduce
873 * the number of timeouts occurring when there is a large number of
874 * qps. busy_jiffies is incremented every rc qp scaling interval.
875 * The scaling interval is selected based on extensive performance
876 * evaluation of targeted workloads.
877 */
878 if (init_attr->qp_type == IB_QPT_RC) {
879 rdi->n_rc_qps++;
880 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
881 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800882 spin_unlock(&rdi->n_qps_lock);
883
884 if (qp->ip) {
885 spin_lock_irq(&rdi->pending_lock);
886 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
887 spin_unlock_irq(&rdi->pending_lock);
888 }
889
890 ret = &qp->ibqp;
891
892 /*
893 * We have our QP and its good, now keep track of what types of opcodes
894 * can be processed on this QP. We do this by keeping track of what the
895 * 3 high order bits of the opcode are.
896 */
897 switch (init_attr->qp_type) {
898 case IB_QPT_SMI:
899 case IB_QPT_GSI:
900 case IB_QPT_UD:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700901 qp->allowed_ops = IB_OPCODE_UD;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800902 break;
903 case IB_QPT_RC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700904 qp->allowed_ops = IB_OPCODE_RC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800905 break;
906 case IB_QPT_UC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700907 qp->allowed_ops = IB_OPCODE_UC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800908 break;
909 default:
910 ret = ERR_PTR(-EINVAL);
911 goto bail_ip;
912 }
913
914 return ret;
915
916bail_ip:
Jim Foraker22dccc52016-11-01 13:44:12 -0700917 if (qp->ip)
918 kref_put(&qp->ip->ref, rvt_release_mmap_info);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800919
920bail_qpn:
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -0700921 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800922
923bail_rq_wq:
Mike Marciniszyn56c8ca52016-08-16 13:26:29 -0700924 if (!qp->ip)
925 vfree(qp->r_rq.wq);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800926
927bail_driver_priv:
928 rdi->driver_f.qp_priv_free(rdi, qp);
929
930bail_qp:
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700931 kfree(qp->s_ack_queue);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800932 kfree(qp);
933
934bail_swq:
935 vfree(swq);
936
937 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800938}
939
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800940/**
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800941 * rvt_error_qp - put a QP into the error state
942 * @qp: the QP to put into the error state
943 * @err: the receive completion error to signal if a RWQE is active
944 *
945 * Flushes both send and receive work queues.
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800946 *
947 * Return: true if last WQE event should be generated.
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800948 * The QP r_lock and s_lock should be held and interrupts disabled.
949 * If we are already in error state, just return.
950 */
951int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
952{
953 struct ib_wc wc;
954 int ret = 0;
955 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
956
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700957 lockdep_assert_held(&qp->r_lock);
958 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800959 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
960 goto bail;
961
962 qp->state = IB_QPS_ERR;
963
964 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
965 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
966 del_timer(&qp->s_timer);
967 }
968
969 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
970 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
971
972 rdi->driver_f.notify_error_qp(qp);
973
974 /* Schedule the sending tasklet to drain the send work queue. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800975 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800976 rdi->driver_f.schedule_send(qp);
977
978 rvt_clear_mr_refs(qp, 0);
979
980 memset(&wc, 0, sizeof(wc));
981 wc.qp = &qp->ibqp;
982 wc.opcode = IB_WC_RECV;
983
984 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
985 wc.wr_id = qp->r_wr_id;
986 wc.status = err;
987 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
988 }
989 wc.status = IB_WC_WR_FLUSH_ERR;
990
991 if (qp->r_rq.wq) {
992 struct rvt_rwq *wq;
993 u32 head;
994 u32 tail;
995
996 spin_lock(&qp->r_rq.lock);
997
998 /* sanity check pointers before trusting them */
999 wq = qp->r_rq.wq;
1000 head = wq->head;
1001 if (head >= qp->r_rq.size)
1002 head = 0;
1003 tail = wq->tail;
1004 if (tail >= qp->r_rq.size)
1005 tail = 0;
1006 while (tail != head) {
1007 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1008 if (++tail >= qp->r_rq.size)
1009 tail = 0;
1010 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1011 }
1012 wq->tail = tail;
1013
1014 spin_unlock(&qp->r_rq.lock);
1015 } else if (qp->ibqp.event_handler) {
1016 ret = 1;
1017 }
1018
1019bail:
1020 return ret;
1021}
1022EXPORT_SYMBOL(rvt_error_qp);
1023
1024/*
1025 * Put the QP into the hash table.
1026 * The hash table holds a reference to the QP.
1027 */
1028static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1029{
1030 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1031 unsigned long flags;
1032
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001033 rvt_get_qp(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001034 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1035
1036 if (qp->ibqp.qp_num <= 1) {
1037 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1038 } else {
1039 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1040
1041 qp->next = rdi->qp_dev->qp_table[n];
1042 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1043 trace_rvt_qpinsert(qp, n);
1044 }
1045
1046 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1047}
1048
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001049/**
Parav Pandit61347fa2016-09-13 19:40:50 +05301050 * rvt_modify_qp - modify the attributes of a queue pair
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001051 * @ibqp: the queue pair who's attributes we're modifying
1052 * @attr: the new attributes
1053 * @attr_mask: the mask of attributes to modify
1054 * @udata: user data for libibverbs.so
1055 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001056 * Return: 0 on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001057 */
1058int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1059 int attr_mask, struct ib_udata *udata)
1060{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001061 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1062 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1063 enum ib_qp_state cur_state, new_state;
1064 struct ib_event ev;
1065 int lastwqe = 0;
1066 int mig = 0;
1067 int pmtu = 0; /* for gcc warning only */
1068 enum rdma_link_layer link;
1069
1070 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1071
1072 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001073 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001074 spin_lock(&qp->s_lock);
1075
1076 cur_state = attr_mask & IB_QP_CUR_STATE ?
1077 attr->cur_qp_state : qp->state;
1078 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1079
1080 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1081 attr_mask, link))
1082 goto inval;
1083
Ira Weinye85ec332016-01-22 13:04:38 -08001084 if (rdi->driver_f.check_modify_qp &&
1085 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1086 goto inval;
1087
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001088 if (attr_mask & IB_QP_AV) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001089 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1090 be16_to_cpu(IB_MULTICAST_LID_BASE))
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001091 goto inval;
1092 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1093 goto inval;
1094 }
1095
1096 if (attr_mask & IB_QP_ALT_PATH) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001097 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001098 be16_to_cpu(IB_MULTICAST_LID_BASE))
1099 goto inval;
1100 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1101 goto inval;
1102 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1103 goto inval;
1104 }
1105
1106 if (attr_mask & IB_QP_PKEY_INDEX)
1107 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1108 goto inval;
1109
1110 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1111 if (attr->min_rnr_timer > 31)
1112 goto inval;
1113
1114 if (attr_mask & IB_QP_PORT)
1115 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1116 qp->ibqp.qp_type == IB_QPT_GSI ||
1117 attr->port_num == 0 ||
1118 attr->port_num > ibqp->device->phys_port_cnt)
1119 goto inval;
1120
1121 if (attr_mask & IB_QP_DEST_QPN)
1122 if (attr->dest_qp_num > RVT_QPN_MASK)
1123 goto inval;
1124
1125 if (attr_mask & IB_QP_RETRY_CNT)
1126 if (attr->retry_cnt > 7)
1127 goto inval;
1128
1129 if (attr_mask & IB_QP_RNR_RETRY)
1130 if (attr->rnr_retry > 7)
1131 goto inval;
1132
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001133 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001134 * Don't allow invalid path_mtu values. OK to set greater
1135 * than the active mtu (or even the max_cap, if we have tuned
1136 * that to a small mtu. We'll set qp->path_mtu
1137 * to the lesser of requested attribute mtu and active,
1138 * for packetizing messages.
1139 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001140 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001141 if (attr_mask & IB_QP_PATH_MTU) {
1142 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1143 if (pmtu < 0)
1144 goto inval;
1145 }
1146
1147 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1148 if (attr->path_mig_state == IB_MIG_REARM) {
1149 if (qp->s_mig_state == IB_MIG_ARMED)
1150 goto inval;
1151 if (new_state != IB_QPS_RTS)
1152 goto inval;
1153 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1154 if (qp->s_mig_state == IB_MIG_REARM)
1155 goto inval;
1156 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1157 goto inval;
1158 if (qp->s_mig_state == IB_MIG_ARMED)
1159 mig = 1;
1160 } else {
1161 goto inval;
1162 }
1163 }
1164
1165 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1166 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1167 goto inval;
1168
1169 switch (new_state) {
1170 case IB_QPS_RESET:
1171 if (qp->state != IB_QPS_RESET)
1172 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1173 break;
1174
1175 case IB_QPS_RTR:
1176 /* Allow event to re-trigger if QP set to RTR more than once */
1177 qp->r_flags &= ~RVT_R_COMM_EST;
1178 qp->state = new_state;
1179 break;
1180
1181 case IB_QPS_SQD:
1182 qp->s_draining = qp->s_last != qp->s_cur;
1183 qp->state = new_state;
1184 break;
1185
1186 case IB_QPS_SQE:
1187 if (qp->ibqp.qp_type == IB_QPT_RC)
1188 goto inval;
1189 qp->state = new_state;
1190 break;
1191
1192 case IB_QPS_ERR:
1193 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1194 break;
1195
1196 default:
1197 qp->state = new_state;
1198 break;
1199 }
1200
1201 if (attr_mask & IB_QP_PKEY_INDEX)
1202 qp->s_pkey_index = attr->pkey_index;
1203
1204 if (attr_mask & IB_QP_PORT)
1205 qp->port_num = attr->port_num;
1206
1207 if (attr_mask & IB_QP_DEST_QPN)
1208 qp->remote_qpn = attr->dest_qp_num;
1209
1210 if (attr_mask & IB_QP_SQ_PSN) {
1211 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1212 qp->s_psn = qp->s_next_psn;
1213 qp->s_sending_psn = qp->s_next_psn;
1214 qp->s_last_psn = qp->s_next_psn - 1;
1215 qp->s_sending_hpsn = qp->s_last_psn;
1216 }
1217
1218 if (attr_mask & IB_QP_RQ_PSN)
1219 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1220
1221 if (attr_mask & IB_QP_ACCESS_FLAGS)
1222 qp->qp_access_flags = attr->qp_access_flags;
1223
1224 if (attr_mask & IB_QP_AV) {
1225 qp->remote_ah_attr = attr->ah_attr;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001226 qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001227 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1228 }
1229
1230 if (attr_mask & IB_QP_ALT_PATH) {
1231 qp->alt_ah_attr = attr->alt_ah_attr;
1232 qp->s_alt_pkey_index = attr->alt_pkey_index;
1233 }
1234
1235 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1236 qp->s_mig_state = attr->path_mig_state;
1237 if (mig) {
1238 qp->remote_ah_attr = qp->alt_ah_attr;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001239 qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001240 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001241 }
1242 }
1243
1244 if (attr_mask & IB_QP_PATH_MTU) {
1245 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001246 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001247 }
1248
1249 if (attr_mask & IB_QP_RETRY_CNT) {
1250 qp->s_retry_cnt = attr->retry_cnt;
1251 qp->s_retry = attr->retry_cnt;
1252 }
1253
1254 if (attr_mask & IB_QP_RNR_RETRY) {
1255 qp->s_rnr_retry_cnt = attr->rnr_retry;
1256 qp->s_rnr_retry = attr->rnr_retry;
1257 }
1258
1259 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1260 qp->r_min_rnr_timer = attr->min_rnr_timer;
1261
1262 if (attr_mask & IB_QP_TIMEOUT) {
1263 qp->timeout = attr->timeout;
Kaike Wana25ce422017-06-17 10:37:26 -07001264 qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001265 }
1266
1267 if (attr_mask & IB_QP_QKEY)
1268 qp->qkey = attr->qkey;
1269
1270 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1271 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1272
1273 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1274 qp->s_max_rd_atomic = attr->max_rd_atomic;
1275
Ira Weinye85ec332016-01-22 13:04:38 -08001276 if (rdi->driver_f.modify_qp)
1277 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1278
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001279 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001280 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001281 spin_unlock_irq(&qp->r_lock);
1282
1283 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1284 rvt_insert_qp(rdi, qp);
1285
1286 if (lastwqe) {
1287 ev.device = qp->ibqp.device;
1288 ev.element.qp = &qp->ibqp;
1289 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1290 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1291 }
1292 if (mig) {
1293 ev.device = qp->ibqp.device;
1294 ev.element.qp = &qp->ibqp;
1295 ev.event = IB_EVENT_PATH_MIG;
1296 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1297 }
1298 return 0;
1299
1300inval:
1301 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001302 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001303 spin_unlock_irq(&qp->r_lock);
1304 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001305}
1306
1307/**
1308 * rvt_destroy_qp - destroy a queue pair
1309 * @ibqp: the queue pair to destroy
1310 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001311 * Note that this can be called while the QP is actively sending or
1312 * receiving!
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001313 *
1314 * Return: 0 on success.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001315 */
1316int rvt_destroy_qp(struct ib_qp *ibqp)
1317{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001318 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1319 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001320
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001321 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001322 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001323 spin_lock(&qp->s_lock);
1324 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1325 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001326 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001327 spin_unlock_irq(&qp->r_lock);
1328
1329 /* qpn is now available for use again */
1330 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1331
1332 spin_lock(&rdi->n_qps_lock);
1333 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001334 if (qp->ibqp.qp_type == IB_QPT_RC) {
1335 rdi->n_rc_qps--;
1336 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1337 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001338 spin_unlock(&rdi->n_qps_lock);
1339
1340 if (qp->ip)
1341 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1342 else
1343 vfree(qp->r_rq.wq);
1344 vfree(qp->s_wq);
1345 rdi->driver_f.qp_priv_free(rdi, qp);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001346 kfree(qp->s_ack_queue);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001347 kfree(qp);
1348 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001349}
1350
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001351/**
1352 * rvt_query_qp - query an ipbq
1353 * @ibqp: IB qp to query
1354 * @attr: attr struct to fill in
1355 * @attr_mask: attr mask ignored
1356 * @init_attr: struct to fill in
1357 *
1358 * Return: always 0
1359 */
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001360int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1361 int attr_mask, struct ib_qp_init_attr *init_attr)
1362{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001363 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1364 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1365
1366 attr->qp_state = qp->state;
1367 attr->cur_qp_state = attr->qp_state;
Sebastian Sanchez16570d32017-08-04 13:52:20 -07001368 attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Harish Chegondi74d2d502016-01-22 13:05:04 -08001369 attr->path_mig_state = qp->s_mig_state;
1370 attr->qkey = qp->qkey;
1371 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1372 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1373 attr->dest_qp_num = qp->remote_qpn;
1374 attr->qp_access_flags = qp->qp_access_flags;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001375 attr->cap.max_send_wr = qp->s_size - 1 -
1376 rdi->dparms.reserved_operations;
Harish Chegondi74d2d502016-01-22 13:05:04 -08001377 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1378 attr->cap.max_send_sge = qp->s_max_sge;
1379 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1380 attr->cap.max_inline_data = 0;
1381 attr->ah_attr = qp->remote_ah_attr;
1382 attr->alt_ah_attr = qp->alt_ah_attr;
1383 attr->pkey_index = qp->s_pkey_index;
1384 attr->alt_pkey_index = qp->s_alt_pkey_index;
1385 attr->en_sqd_async_notify = 0;
1386 attr->sq_draining = qp->s_draining;
1387 attr->max_rd_atomic = qp->s_max_rd_atomic;
1388 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1389 attr->min_rnr_timer = qp->r_min_rnr_timer;
1390 attr->port_num = qp->port_num;
1391 attr->timeout = qp->timeout;
1392 attr->retry_cnt = qp->s_retry_cnt;
1393 attr->rnr_retry = qp->s_rnr_retry_cnt;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001394 attr->alt_port_num =
1395 rdma_ah_get_port_num(&qp->alt_ah_attr);
Harish Chegondi74d2d502016-01-22 13:05:04 -08001396 attr->alt_timeout = qp->alt_timeout;
1397
1398 init_attr->event_handler = qp->ibqp.event_handler;
1399 init_attr->qp_context = qp->ibqp.qp_context;
1400 init_attr->send_cq = qp->ibqp.send_cq;
1401 init_attr->recv_cq = qp->ibqp.recv_cq;
1402 init_attr->srq = qp->ibqp.srq;
1403 init_attr->cap = attr->cap;
1404 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1405 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1406 else
1407 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1408 init_attr->qp_type = qp->ibqp.qp_type;
1409 init_attr->port_num = qp->port_num;
1410 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001411}
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001412
1413/**
1414 * rvt_post_receive - post a receive on a QP
1415 * @ibqp: the QP to post the receive on
1416 * @wr: the WR to post
1417 * @bad_wr: the first bad WR is put here
1418 *
1419 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001420 *
1421 * Return: 0 on success otherwise errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001422 */
1423int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1424 struct ib_recv_wr **bad_wr)
1425{
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001426 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1427 struct rvt_rwq *wq = qp->r_rq.wq;
1428 unsigned long flags;
Alex Estrin000a8302016-03-07 11:35:51 -08001429 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1430 !qp->ibqp.srq;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001431
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001432 /* Check that state is OK to post receive. */
1433 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1434 *bad_wr = wr;
1435 return -EINVAL;
1436 }
1437
1438 for (; wr; wr = wr->next) {
1439 struct rvt_rwqe *wqe;
1440 u32 next;
1441 int i;
1442
1443 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1444 *bad_wr = wr;
1445 return -EINVAL;
1446 }
1447
1448 spin_lock_irqsave(&qp->r_rq.lock, flags);
1449 next = wq->head + 1;
1450 if (next >= qp->r_rq.size)
1451 next = 0;
1452 if (next == wq->tail) {
1453 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1454 *bad_wr = wr;
1455 return -ENOMEM;
1456 }
Alex Estrin000a8302016-03-07 11:35:51 -08001457 if (unlikely(qp_err_flush)) {
1458 struct ib_wc wc;
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001459
Alex Estrin000a8302016-03-07 11:35:51 -08001460 memset(&wc, 0, sizeof(wc));
1461 wc.qp = &qp->ibqp;
1462 wc.opcode = IB_WC_RECV;
1463 wc.wr_id = wr->wr_id;
1464 wc.status = IB_WC_WR_FLUSH_ERR;
1465 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1466 } else {
1467 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1468 wqe->wr_id = wr->wr_id;
1469 wqe->num_sge = wr->num_sge;
1470 for (i = 0; i < wr->num_sge; i++)
1471 wqe->sg_list[i] = wr->sg_list[i];
1472 /*
1473 * Make sure queue entry is written
1474 * before the head index.
1475 */
1476 smp_wmb();
1477 wq->head = next;
1478 }
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001479 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1480 }
1481 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001482}
1483
1484/**
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001485 * rvt_qp_valid_operation - validate post send wr request
1486 * @qp - the qp
1487 * @post-parms - the post send table for the driver
1488 * @wr - the work request
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001489 *
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001490 * The routine validates the operation based on the
1491 * validation table an returns the length of the operation
1492 * which can extend beyond the ib_send_bw. Operation
1493 * dependent flags key atomic operation validation.
1494 *
1495 * There is an exception for UD qps that validates the pd and
1496 * overrides the length to include the additional UD specific
1497 * length.
1498 *
1499 * Returns a negative error or the length of the work request
1500 * for building the swqe.
1501 */
1502static inline int rvt_qp_valid_operation(
1503 struct rvt_qp *qp,
1504 const struct rvt_operation_params *post_parms,
1505 struct ib_send_wr *wr)
1506{
1507 int len;
1508
1509 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1510 return -EINVAL;
1511 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1512 return -EINVAL;
1513 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1514 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1515 return -EINVAL;
1516 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1517 (wr->num_sge == 0 ||
1518 wr->sg_list[0].length < sizeof(u64) ||
1519 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1520 return -EINVAL;
1521 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1522 !qp->s_max_rd_atomic)
1523 return -EINVAL;
1524 len = post_parms[wr->opcode].length;
1525 /* UD specific */
1526 if (qp->ibqp.qp_type != IB_QPT_UC &&
1527 qp->ibqp.qp_type != IB_QPT_RC) {
1528 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1529 return -EINVAL;
1530 len = sizeof(struct ib_ud_wr);
1531 }
1532 return len;
1533}
1534
1535/**
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001536 * rvt_qp_is_avail - determine queue capacity
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001537 * @qp - the qp
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001538 * @rdi - the rdmavt device
1539 * @reserved_op - is reserved operation
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001540 *
1541 * This assumes the s_hlock is held but the s_last
1542 * qp variable is uncontrolled.
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001543 *
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001544 * For non reserved operations, the qp->s_avail
1545 * may be changed.
1546 *
1547 * The return value is zero or a -ENOMEM.
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001548 */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001549static inline int rvt_qp_is_avail(
1550 struct rvt_qp *qp,
1551 struct rvt_dev_info *rdi,
1552 bool reserved_op)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001553{
1554 u32 slast;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001555 u32 avail;
1556 u32 reserved_used;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001557
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001558 /* see rvt_qp_wqe_unreserve() */
1559 smp_mb__before_atomic();
1560 reserved_used = atomic_read(&qp->s_reserved_used);
1561 if (unlikely(reserved_op)) {
1562 /* see rvt_qp_wqe_unreserve() */
1563 smp_mb__before_atomic();
1564 if (reserved_used >= rdi->dparms.reserved_operations)
1565 return -ENOMEM;
1566 return 0;
1567 }
1568 /* non-reserved operations */
1569 if (likely(qp->s_avail))
1570 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001571 smp_read_barrier_depends(); /* see rc.c */
1572 slast = ACCESS_ONCE(qp->s_last);
1573 if (qp->s_head >= slast)
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001574 avail = qp->s_size - (qp->s_head - slast);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001575 else
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001576 avail = slast - qp->s_head;
1577
1578 /* see rvt_qp_wqe_unreserve() */
1579 smp_mb__before_atomic();
1580 reserved_used = atomic_read(&qp->s_reserved_used);
1581 avail = avail - 1 -
1582 (rdi->dparms.reserved_operations - reserved_used);
1583 /* insure we don't assign a negative s_avail */
1584 if ((s32)avail <= 0)
1585 return -ENOMEM;
1586 qp->s_avail = avail;
1587 if (WARN_ON(qp->s_avail >
1588 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1589 rvt_pr_err(rdi,
1590 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1591 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1592 qp->s_head, qp->s_tail, qp->s_cur,
1593 qp->s_acked, qp->s_last);
1594 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001595}
1596
1597/**
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001598 * rvt_post_one_wr - post one RC, UC, or UD send work request
1599 * @qp: the QP to post on
1600 * @wr: the work request to send
1601 */
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001602static int rvt_post_one_wr(struct rvt_qp *qp,
1603 struct ib_send_wr *wr,
1604 int *call_send)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001605{
1606 struct rvt_swqe *wqe;
1607 u32 next;
1608 int i;
1609 int j;
1610 int acc;
1611 struct rvt_lkey_table *rkt;
1612 struct rvt_pd *pd;
1613 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001614 u8 log_pmtu;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001615 int ret;
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001616 size_t cplen;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001617 bool reserved_op;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001618 int local_ops_delayed = 0;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001619
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001620 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1621
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001622 /* IB spec says that num_sge == 0 is OK. */
1623 if (unlikely(wr->num_sge > qp->s_max_sge))
1624 return -EINVAL;
1625
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001626 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1627 if (ret < 0)
1628 return ret;
1629 cplen = ret;
1630
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001631 /*
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001632 * Local operations include fast register and local invalidate.
1633 * Fast register needs to be processed immediately because the
1634 * registered lkey may be used by following work requests and the
1635 * lkey needs to be valid at the time those requests are posted.
1636 * Local invalidate can be processed immediately if fencing is
1637 * not required and no previous local invalidate ops are pending.
1638 * Signaled local operations that have been processed immediately
1639 * need to have requests with "completion only" flags set posted
1640 * to the send queue in order to generate completions.
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001641 */
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001642 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001643 switch (wr->opcode) {
1644 case IB_WR_REG_MR:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001645 ret = rvt_fast_reg_mr(qp,
1646 reg_wr(wr)->mr,
1647 reg_wr(wr)->key,
1648 reg_wr(wr)->access);
1649 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1650 return ret;
1651 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001652 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001653 if ((wr->send_flags & IB_SEND_FENCE) ||
1654 atomic_read(&qp->local_ops_pending)) {
1655 local_ops_delayed = 1;
1656 } else {
1657 ret = rvt_invalidate_rkey(
1658 qp, wr->ex.invalidate_rkey);
1659 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1660 return ret;
1661 }
1662 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001663 default:
1664 return -EINVAL;
1665 }
1666 }
1667
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001668 reserved_op = rdi->post_parms[wr->opcode].flags &
1669 RVT_OPERATION_USE_RESERVE;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001670 /* check for avail */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001671 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1672 if (ret)
1673 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001674 next = qp->s_head + 1;
1675 if (next >= qp->s_size)
1676 next = 0;
Ira Weiny60c30f52016-02-03 14:14:45 -08001677
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001678 rkt = &rdi->lkey_table;
1679 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1680 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1681
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001682 /* cplen has length from above */
1683 memcpy(&wqe->wr, wr, cplen);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001684
1685 wqe->length = 0;
1686 j = 0;
1687 if (wr->num_sge) {
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07001688 struct rvt_sge *last_sge = NULL;
1689
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001690 acc = wr->opcode >= IB_WR_RDMA_READ ?
1691 IB_ACCESS_LOCAL_WRITE : 0;
1692 for (i = 0; i < wr->num_sge; i++) {
1693 u32 length = wr->sg_list[i].length;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001694
1695 if (length == 0)
1696 continue;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001697 ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
1698 &wr->sg_list[i], acc);
1699 if (unlikely(ret < 0))
1700 goto bail_inval_free;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001701 wqe->length += length;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001702 if (ret)
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07001703 last_sge = &wqe->sg_list[j];
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001704 j += ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001705 }
1706 wqe->wr.num_sge = j;
1707 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001708
1709 /* general part of wqe valid - allow for driver checks */
1710 if (rdi->driver_f.check_send_wqe) {
1711 ret = rdi->driver_f.check_send_wqe(qp, wqe);
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001712 if (ret < 0)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001713 goto bail_inval_free;
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001714 if (ret)
1715 *call_send = ret;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001716 }
1717
1718 log_pmtu = qp->log_pmtu;
1719 if (qp->ibqp.qp_type != IB_QPT_UC &&
1720 qp->ibqp.qp_type != IB_QPT_RC) {
1721 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1722
1723 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001724 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1725 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001726
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001727 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001728 if (local_ops_delayed)
1729 atomic_inc(&qp->local_ops_pending);
1730 else
1731 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001732 wqe->ssn = 0;
1733 wqe->psn = 0;
1734 wqe->lpsn = 0;
1735 } else {
1736 wqe->ssn = qp->s_ssn++;
1737 wqe->psn = qp->s_next_psn;
1738 wqe->lpsn = wqe->psn +
1739 (wqe->length ?
1740 ((wqe->length - 1) >> log_pmtu) :
1741 0);
1742 qp->s_next_psn = wqe->lpsn + 1;
1743 }
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07001744 if (unlikely(reserved_op)) {
1745 wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001746 rvt_qp_wqe_reserve(qp, wqe);
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07001747 } else {
1748 wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001749 qp->s_avail--;
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07001750 }
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07001751 trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001752 smp_wmb(); /* see request builders */
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001753 qp->s_head = next;
1754
1755 return 0;
1756
1757bail_inval_free:
1758 /* release mr holds */
1759 while (j) {
1760 struct rvt_sge *sge = &wqe->sg_list[--j];
1761
1762 rvt_put_mr(sge->mr);
1763 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001764 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001765}
1766
1767/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001768 * rvt_post_send - post a send on a QP
1769 * @ibqp: the QP to post the send on
1770 * @wr: the list of work requests to post
1771 * @bad_wr: the first bad WR is put here
1772 *
1773 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001774 *
1775 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001776 */
1777int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1778 struct ib_send_wr **bad_wr)
1779{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001780 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1781 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1782 unsigned long flags = 0;
1783 int call_send;
1784 unsigned nreq = 0;
1785 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001786
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001787 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001788
1789 /*
1790 * Ensure QP state is such that we can send. If not bail out early,
1791 * there is no need to do this every time we post a send.
1792 */
1793 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001794 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001795 return -EINVAL;
1796 }
1797
1798 /*
1799 * If the send queue is empty, and we only have a single WR then just go
1800 * ahead and kick the send engine into gear. Otherwise we will always
1801 * just schedule the send to happen later.
1802 */
1803 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1804
1805 for (; wr; wr = wr->next) {
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001806 err = rvt_post_one_wr(qp, wr, &call_send);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001807 if (unlikely(err)) {
1808 *bad_wr = wr;
1809 goto bail;
1810 }
1811 nreq++;
1812 }
1813bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001814 spin_unlock_irqrestore(&qp->s_hlock, flags);
1815 if (nreq) {
1816 if (call_send)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001817 rdi->driver_f.do_send(qp);
Jubin Johne6d2e012016-04-12 10:47:00 -07001818 else
1819 rdi->driver_f.schedule_send_no_lock(qp);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001820 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001821 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001822}
1823
1824/**
1825 * rvt_post_srq_receive - post a receive on a shared receive queue
1826 * @ibsrq: the SRQ to post the receive on
1827 * @wr: the list of work requests to post
1828 * @bad_wr: A pointer to the first WR to cause a problem is put here
1829 *
1830 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001831 *
1832 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001833 */
1834int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1835 struct ib_recv_wr **bad_wr)
1836{
Jubin Johnb8f881b2016-02-03 14:14:36 -08001837 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1838 struct rvt_rwq *wq;
1839 unsigned long flags;
1840
1841 for (; wr; wr = wr->next) {
1842 struct rvt_rwqe *wqe;
1843 u32 next;
1844 int i;
1845
1846 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1847 *bad_wr = wr;
1848 return -EINVAL;
1849 }
1850
1851 spin_lock_irqsave(&srq->rq.lock, flags);
1852 wq = srq->rq.wq;
1853 next = wq->head + 1;
1854 if (next >= srq->rq.size)
1855 next = 0;
1856 if (next == wq->tail) {
1857 spin_unlock_irqrestore(&srq->rq.lock, flags);
1858 *bad_wr = wr;
1859 return -ENOMEM;
1860 }
1861
1862 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1863 wqe->wr_id = wr->wr_id;
1864 wqe->num_sge = wr->num_sge;
1865 for (i = 0; i < wr->num_sge; i++)
1866 wqe->sg_list[i] = wr->sg_list[i];
1867 /* Make sure queue entry is written before the head index. */
1868 smp_wmb();
1869 wq->head = next;
1870 spin_unlock_irqrestore(&srq->rq.lock, flags);
1871 }
1872 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001873}
Brian Weltybeb5a042017-02-08 05:27:01 -08001874
1875/**
1876 * qp_comm_est - handle trap with QP established
1877 * @qp: the QP
1878 */
1879void rvt_comm_est(struct rvt_qp *qp)
1880{
1881 qp->r_flags |= RVT_R_COMM_EST;
1882 if (qp->ibqp.event_handler) {
1883 struct ib_event ev;
1884
1885 ev.device = qp->ibqp.device;
1886 ev.element.qp = &qp->ibqp;
1887 ev.event = IB_EVENT_COMM_EST;
1888 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1889 }
1890}
1891EXPORT_SYMBOL(rvt_comm_est);
1892
1893void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
1894{
1895 unsigned long flags;
1896 int lastwqe;
1897
1898 spin_lock_irqsave(&qp->s_lock, flags);
1899 lastwqe = rvt_error_qp(qp, err);
1900 spin_unlock_irqrestore(&qp->s_lock, flags);
1901
1902 if (lastwqe) {
1903 struct ib_event ev;
1904
1905 ev.device = qp->ibqp.device;
1906 ev.element.qp = &qp->ibqp;
1907 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1908 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1909 }
1910}
1911EXPORT_SYMBOL(rvt_rc_error);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001912
Don Hiatt881fccb2017-02-08 05:28:19 -08001913/*
1914 * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
1915 * @index - the index
1916 * return usec from an index into ib_rvt_rnr_table
1917 */
1918unsigned long rvt_rnr_tbl_to_usec(u32 index)
1919{
Don Hiatt832666c2017-02-08 05:28:25 -08001920 return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
Don Hiatt881fccb2017-02-08 05:28:19 -08001921}
1922EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
1923
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001924static inline unsigned long rvt_aeth_to_usec(u32 aeth)
1925{
Don Hiatt832666c2017-02-08 05:28:25 -08001926 return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
1927 IB_AETH_CREDIT_MASK];
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001928}
1929
1930/*
1931 * rvt_add_retry_timer - add/start a retry timer
1932 * @qp - the QP
1933 * add a retry timer on the QP
1934 */
1935void rvt_add_retry_timer(struct rvt_qp *qp)
1936{
1937 struct ib_qp *ibqp = &qp->ibqp;
1938 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1939
1940 lockdep_assert_held(&qp->s_lock);
1941 qp->s_flags |= RVT_S_TIMER;
1942 /* 4.096 usec. * (1 << qp->timeout) */
1943 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
1944 rdi->busy_jiffies;
1945 add_timer(&qp->s_timer);
1946}
1947EXPORT_SYMBOL(rvt_add_retry_timer);
1948
1949/**
1950 * rvt_add_rnr_timer - add/start an rnr timer
1951 * @qp - the QP
1952 * @aeth - aeth of RNR timeout, simulated aeth for loopback
1953 * add an rnr timer on the QP
1954 */
1955void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
1956{
1957 u32 to;
1958
1959 lockdep_assert_held(&qp->s_lock);
1960 qp->s_flags |= RVT_S_WAIT_RNR;
1961 to = rvt_aeth_to_usec(aeth);
1962 hrtimer_start(&qp->s_rnr_timer,
1963 ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
1964}
1965EXPORT_SYMBOL(rvt_add_rnr_timer);
1966
1967/**
1968 * rvt_stop_rc_timers - stop all timers
1969 * @qp - the QP
1970 * stop any pending timers
1971 */
1972void rvt_stop_rc_timers(struct rvt_qp *qp)
1973{
1974 lockdep_assert_held(&qp->s_lock);
1975 /* Remove QP from all timers */
1976 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1977 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1978 del_timer(&qp->s_timer);
1979 hrtimer_try_to_cancel(&qp->s_rnr_timer);
1980 }
1981}
1982EXPORT_SYMBOL(rvt_stop_rc_timers);
1983
1984/**
1985 * rvt_stop_rnr_timer - stop an rnr timer
1986 * @qp - the QP
1987 *
1988 * stop an rnr timer and return if the timer
1989 * had been pending.
1990 */
1991static int rvt_stop_rnr_timer(struct rvt_qp *qp)
1992{
1993 int rval = 0;
1994
1995 lockdep_assert_held(&qp->s_lock);
1996 /* Remove QP from rnr timer */
1997 if (qp->s_flags & RVT_S_WAIT_RNR) {
1998 qp->s_flags &= ~RVT_S_WAIT_RNR;
1999 rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
2000 }
2001 return rval;
2002}
2003
2004/**
2005 * rvt_del_timers_sync - wait for any timeout routines to exit
2006 * @qp - the QP
2007 */
2008void rvt_del_timers_sync(struct rvt_qp *qp)
2009{
2010 del_timer_sync(&qp->s_timer);
2011 hrtimer_cancel(&qp->s_rnr_timer);
2012}
2013EXPORT_SYMBOL(rvt_del_timers_sync);
2014
2015/**
2016 * This is called from s_timer for missing responses.
2017 */
2018static void rvt_rc_timeout(unsigned long arg)
2019{
2020 struct rvt_qp *qp = (struct rvt_qp *)arg;
2021 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2022 unsigned long flags;
2023
2024 spin_lock_irqsave(&qp->r_lock, flags);
2025 spin_lock(&qp->s_lock);
2026 if (qp->s_flags & RVT_S_TIMER) {
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002027 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
2028
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002029 qp->s_flags &= ~RVT_S_TIMER;
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002030 rvp->n_rc_timeouts++;
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002031 del_timer(&qp->s_timer);
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002032 trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002033 if (rdi->driver_f.notify_restart_rc)
2034 rdi->driver_f.notify_restart_rc(qp,
2035 qp->s_last_psn + 1,
2036 1);
2037 rdi->driver_f.schedule_send(qp);
2038 }
2039 spin_unlock(&qp->s_lock);
2040 spin_unlock_irqrestore(&qp->r_lock, flags);
2041}
2042
2043/*
2044 * This is called from s_timer for RNR timeouts.
2045 */
2046enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2047{
2048 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2049 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2050 unsigned long flags;
2051
2052 spin_lock_irqsave(&qp->s_lock, flags);
2053 rvt_stop_rnr_timer(qp);
2054 rdi->driver_f.schedule_send(qp);
2055 spin_unlock_irqrestore(&qp->s_lock, flags);
2056 return HRTIMER_NORESTART;
2057}
2058EXPORT_SYMBOL(rvt_rc_rnr_retry);