blob: 9e14addd690c65fca9024b7cde58aa05748a2c5e [file] [log] [blame]
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001/*
Dennis Dalessandrofe314192016-01-22 13:04:58 -08002 * Copyright(c) 2016 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>
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080054#include "qp.h"
Dennis Dalessandro515667f2016-01-22 12:50:17 -080055#include "vt.h"
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080056#include "trace.h"
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080057
Dennis Dalessandrobfbac092016-01-22 13:00:22 -080058/*
59 * Note that it is OK to post send work requests in the SQE and ERR
60 * states; rvt_do_send() will process them and generate error
61 * completions as per IB 1.2 C10-96.
62 */
63const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
64 [IB_QPS_RESET] = 0,
65 [IB_QPS_INIT] = RVT_POST_RECV_OK,
66 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
67 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
68 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
69 RVT_PROCESS_NEXT_SEND_OK,
70 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
71 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
72 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
73 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
74 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
75 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
76};
77EXPORT_SYMBOL(ib_rvt_state_ops);
78
Mike Marciniszynf2dc9cd2016-12-07 19:34:06 -080079/*
80 * Translate ib_wr_opcode into ib_wc_opcode.
81 */
82const enum ib_wc_opcode ib_rvt_wc_opcode[] = {
83 [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
84 [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
85 [IB_WR_SEND] = IB_WC_SEND,
86 [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
87 [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
88 [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
89 [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD,
90 [IB_WR_SEND_WITH_INV] = IB_WC_SEND,
91 [IB_WR_LOCAL_INV] = IB_WC_LOCAL_INV,
92 [IB_WR_REG_MR] = IB_WC_REG_MR
93};
94EXPORT_SYMBOL(ib_rvt_wc_opcode);
95
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -080096static void get_map_page(struct rvt_qpn_table *qpt,
97 struct rvt_qpn_map *map,
98 gfp_t gfp)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080099{
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800100 unsigned long page = get_zeroed_page(gfp);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800101
102 /*
103 * Free the page if someone raced with us installing it.
104 */
105
106 spin_lock(&qpt->lock);
107 if (map->page)
108 free_page(page);
109 else
110 map->page = (void *)page;
111 spin_unlock(&qpt->lock);
112}
113
114/**
115 * init_qpn_table - initialize the QP number table for a device
116 * @qpt: the QPN table
117 */
118static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
119{
120 u32 offset, i;
121 struct rvt_qpn_map *map;
122 int ret = 0;
123
Harish Chegondifef2efd2016-01-22 12:50:30 -0800124 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800125 return -EINVAL;
126
127 spin_lock_init(&qpt->lock);
128
129 qpt->last = rdi->dparms.qpn_start;
130 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
131
132 /*
133 * Drivers may want some QPs beyond what we need for verbs let them use
134 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
135 * for those. The reserved range must be *after* the range which verbs
136 * will pick from.
137 */
138
139 /* Figure out number of bit maps needed before reserved range */
140 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
141
142 /* This should always be zero */
143 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
144
145 /* Starting with the first reserved bit map */
146 map = &qpt->map[qpt->nmaps];
147
148 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
149 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
Harish Chegondifef2efd2016-01-22 12:50:30 -0800150 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800151 if (!map->page) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800152 get_map_page(qpt, map, GFP_KERNEL);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800153 if (!map->page) {
154 ret = -ENOMEM;
155 break;
156 }
157 }
158 set_bit(offset, map->page);
159 offset++;
160 if (offset == RVT_BITS_PER_PAGE) {
161 /* next page */
162 qpt->nmaps++;
163 map++;
164 offset = 0;
165 }
166 }
167 return ret;
168}
169
170/**
171 * free_qpn_table - free the QP number table for a device
172 * @qpt: the QPN table
173 */
174static void free_qpn_table(struct rvt_qpn_table *qpt)
175{
176 int i;
177
178 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
179 free_page((unsigned long)qpt->map[i].page);
180}
181
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800182/**
183 * rvt_driver_qp_init - Init driver qp resources
184 * @rdi: rvt dev strucutre
185 *
186 * Return: 0 on success
187 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800188int rvt_driver_qp_init(struct rvt_dev_info *rdi)
189{
190 int i;
191 int ret = -ENOMEM;
192
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800193 if (!rdi->dparms.qp_table_size)
194 return -EINVAL;
195
196 /*
197 * If driver is not doing any QP allocation then make sure it is
198 * providing the necessary QP functions.
199 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800200 if (!rdi->driver_f.free_all_qps ||
201 !rdi->driver_f.qp_priv_alloc ||
202 !rdi->driver_f.qp_priv_free ||
203 !rdi->driver_f.notify_qp_reset)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800204 return -EINVAL;
205
206 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800207 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
208 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800209 if (!rdi->qp_dev)
210 return -ENOMEM;
211
212 /* allocate hash table */
213 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
214 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
215 rdi->qp_dev->qp_table =
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800216 kmalloc_node(rdi->qp_dev->qp_table_size *
217 sizeof(*rdi->qp_dev->qp_table),
218 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800219 if (!rdi->qp_dev->qp_table)
220 goto no_qp_table;
221
222 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
223 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
224
225 spin_lock_init(&rdi->qp_dev->qpt_lock);
226
227 /* initialize qpn map */
228 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
229 goto fail_table;
230
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800231 spin_lock_init(&rdi->n_qps_lock);
232
233 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800234
235fail_table:
236 kfree(rdi->qp_dev->qp_table);
237 free_qpn_table(&rdi->qp_dev->qpn_table);
238
239no_qp_table:
240 kfree(rdi->qp_dev);
241
242 return ret;
243}
244
245/**
246 * free_all_qps - check for QPs still in use
247 * @qpt: the QP table to empty
248 *
249 * There should not be any QPs still in use.
250 * Free memory for table.
251 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800252static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800253{
254 unsigned long flags;
255 struct rvt_qp *qp;
256 unsigned n, qp_inuse = 0;
257 spinlock_t *ql; /* work around too long line below */
258
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800259 if (rdi->driver_f.free_all_qps)
260 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800261
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800262 qp_inuse += rvt_mcast_tree_empty(rdi);
263
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800264 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800265 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800266
267 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800268 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800269 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
270 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
271 lockdep_is_held(ql));
272 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800273
274 for (; qp; qp = rcu_dereference_protected(qp->next,
275 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800276 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800277 }
278 spin_unlock_irqrestore(ql, flags);
279 synchronize_rcu();
280 return qp_inuse;
281}
282
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800283/**
284 * rvt_qp_exit - clean up qps on device exit
285 * @rdi: rvt dev structure
286 *
287 * Check for qp leaks and free resources.
288 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800289void rvt_qp_exit(struct rvt_dev_info *rdi)
290{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800291 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800292
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800293 if (qps_inuse)
294 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
295 qps_inuse);
296 if (!rdi->qp_dev)
297 return;
298
299 kfree(rdi->qp_dev->qp_table);
300 free_qpn_table(&rdi->qp_dev->qpn_table);
301 kfree(rdi->qp_dev);
302}
303
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800304static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
305 struct rvt_qpn_map *map, unsigned off)
306{
307 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
308}
309
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800310/**
311 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
312 * IB_QPT_SMI/IB_QPT_GSI
313 *@rdi: rvt device info structure
314 *@qpt: queue pair number table pointer
315 *@port_num: IB port number, 1 based, comes from core
316 *
317 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800318 */
319static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800320 enum ib_qp_type type, u8 port_num, gfp_t gfp)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800321{
322 u32 i, offset, max_scan, qpn;
323 struct rvt_qpn_map *map;
324 u32 ret;
325
326 if (rdi->driver_f.alloc_qpn)
Ira Weinyb7b3cf42016-02-03 14:15:28 -0800327 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800328
329 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
330 unsigned n;
331
332 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800333 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800334 spin_lock(&qpt->lock);
335 if (qpt->flags & n)
336 ret = -EINVAL;
337 else
338 qpt->flags |= n;
339 spin_unlock(&qpt->lock);
340 goto bail;
341 }
342
343 qpn = qpt->last + qpt->incr;
344 if (qpn >= RVT_QPN_MAX)
345 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
346 /* offset carries bit 0 */
347 offset = qpn & RVT_BITS_PER_PAGE_MASK;
348 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
349 max_scan = qpt->nmaps - !offset;
350 for (i = 0;;) {
351 if (unlikely(!map->page)) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800352 get_map_page(qpt, map, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800353 if (unlikely(!map->page))
354 break;
355 }
356 do {
357 if (!test_and_set_bit(offset, map->page)) {
358 qpt->last = qpn;
359 ret = qpn;
360 goto bail;
361 }
362 offset += qpt->incr;
363 /*
364 * This qpn might be bogus if offset >= BITS_PER_PAGE.
365 * That is OK. It gets re-assigned below
366 */
367 qpn = mk_qpn(qpt, map, offset);
368 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
369 /*
370 * In order to keep the number of pages allocated to a
371 * minimum, we scan the all existing pages before increasing
372 * the size of the bitmap table.
373 */
374 if (++i > max_scan) {
375 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
376 break;
377 map = &qpt->map[qpt->nmaps++];
378 /* start at incr with current bit 0 */
379 offset = qpt->incr | (offset & 1);
380 } else if (map < &qpt->map[qpt->nmaps]) {
381 ++map;
382 /* start at incr with current bit 0 */
383 offset = qpt->incr | (offset & 1);
384 } else {
385 map = &qpt->map[0];
386 /* wrap to first map page, invert bit 0 */
387 offset = qpt->incr | ((offset & 1) ^ 1);
388 }
Brian Welty501edc422016-06-09 07:51:20 -0700389 /* there can be no set bits in low-order QoS bits */
390 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800391 qpn = mk_qpn(qpt, map, offset);
392 }
393
394 ret = -ENOMEM;
395
396bail:
397 return ret;
398}
399
400static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
401{
402 struct rvt_qpn_map *map;
403
404 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
405 if (map->page)
406 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
407}
408
409/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800410 * rvt_clear_mr_refs - Drop help mr refs
411 * @qp: rvt qp data structure
412 * @clr_sends: If shoudl clear send side or not
413 */
414static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
415{
416 unsigned n;
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700417 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800418
419 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
420 rvt_put_ss(&qp->s_rdma_read_sge);
421
422 rvt_put_ss(&qp->r_sge);
423
424 if (clr_sends) {
425 while (qp->s_last != qp->s_head) {
426 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
427 unsigned i;
428
429 for (i = 0; i < wqe->wr.num_sge; i++) {
430 struct rvt_sge *sge = &wqe->sg_list[i];
431
432 rvt_put_mr(sge->mr);
433 }
434 if (qp->ibqp.qp_type == IB_QPT_UD ||
435 qp->ibqp.qp_type == IB_QPT_SMI ||
436 qp->ibqp.qp_type == IB_QPT_GSI)
437 atomic_dec(&ibah_to_rvtah(
438 wqe->ud_wr.ah)->refcount);
439 if (++qp->s_last >= qp->s_size)
440 qp->s_last = 0;
441 smp_wmb(); /* see qp_set_savail */
442 }
443 if (qp->s_rdma_mr) {
444 rvt_put_mr(qp->s_rdma_mr);
445 qp->s_rdma_mr = NULL;
446 }
447 }
448
449 if (qp->ibqp.qp_type != IB_QPT_RC)
450 return;
451
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700452 for (n = 0; n < rvt_max_atomic(rdi); n++) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800453 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
454
Ira Weinyfe508272016-07-27 21:07:36 -0400455 if (e->rdma_sge.mr) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800456 rvt_put_mr(e->rdma_sge.mr);
457 e->rdma_sge.mr = NULL;
458 }
459 }
460}
461
462/**
463 * rvt_remove_qp - remove qp form table
464 * @rdi: rvt dev struct
465 * @qp: qp to remove
466 *
467 * Remove the QP from the table so it can't be found asynchronously by
468 * the receive routine.
469 */
470static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
471{
472 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
473 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
474 unsigned long flags;
475 int removed = 1;
476
477 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
478
479 if (rcu_dereference_protected(rvp->qp[0],
480 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
481 RCU_INIT_POINTER(rvp->qp[0], NULL);
482 } else if (rcu_dereference_protected(rvp->qp[1],
483 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
484 RCU_INIT_POINTER(rvp->qp[1], NULL);
485 } else {
486 struct rvt_qp *q;
487 struct rvt_qp __rcu **qpp;
488
489 removed = 0;
490 qpp = &rdi->qp_dev->qp_table[n];
491 for (; (q = rcu_dereference_protected(*qpp,
492 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
493 qpp = &q->next) {
494 if (q == qp) {
495 RCU_INIT_POINTER(*qpp,
496 rcu_dereference_protected(qp->next,
497 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
498 removed = 1;
499 trace_rvt_qpremove(qp, n);
500 break;
501 }
502 }
503 }
504
505 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
506 if (removed) {
507 synchronize_rcu();
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -0700508 rvt_put_qp(qp);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800509 }
510}
511
512/**
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700513 * rvt_init_qp - initialize the QP state to the reset state
514 * @qp: the QP to init or reinit
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800515 * @type: the QP type
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700516 *
517 * This function is called from both rvt_create_qp() and
518 * rvt_reset_qp(). The difference is that the reset
519 * patch the necessary locks to protect against concurent
520 * access.
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800521 */
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700522static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
523 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800524{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800525 qp->remote_qpn = 0;
526 qp->qkey = 0;
527 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800528 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
529 qp->s_hdrwords = 0;
530 qp->s_wqe = NULL;
531 qp->s_draining = 0;
532 qp->s_next_psn = 0;
533 qp->s_last_psn = 0;
534 qp->s_sending_psn = 0;
535 qp->s_sending_hpsn = 0;
536 qp->s_psn = 0;
537 qp->r_psn = 0;
538 qp->r_msn = 0;
539 if (type == IB_QPT_RC) {
540 qp->s_state = IB_OPCODE_RC_SEND_LAST;
541 qp->r_state = IB_OPCODE_RC_SEND_LAST;
542 } else {
543 qp->s_state = IB_OPCODE_UC_SEND_LAST;
544 qp->r_state = IB_OPCODE_UC_SEND_LAST;
545 }
546 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
547 qp->r_nak_state = 0;
548 qp->r_aflags = 0;
549 qp->r_flags = 0;
550 qp->s_head = 0;
551 qp->s_tail = 0;
552 qp->s_cur = 0;
553 qp->s_acked = 0;
554 qp->s_last = 0;
555 qp->s_ssn = 1;
556 qp->s_lsn = 0;
557 qp->s_mig_state = IB_MIG_MIGRATED;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800558 qp->r_head_ack_queue = 0;
559 qp->s_tail_ack_queue = 0;
560 qp->s_num_rd_atomic = 0;
561 if (qp->r_rq.wq) {
562 qp->r_rq.wq->head = 0;
563 qp->r_rq.wq->tail = 0;
564 }
565 qp->r_sge.num_sge = 0;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700566 atomic_set(&qp->s_reserved_used, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800567}
568
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800569/**
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700570 * rvt_reset_qp - initialize the QP state to the reset state
571 * @qp: the QP to reset
572 * @type: the QP type
573 *
574 * r_lock, s_hlock, and s_lock are required to be held by the caller
575 */
576static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
577 enum ib_qp_type type)
578 __must_hold(&qp->s_lock)
579 __must_hold(&qp->s_hlock)
580 __must_hold(&qp->r_lock)
581{
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700582 lockdep_assert_held(&qp->r_lock);
583 lockdep_assert_held(&qp->s_hlock);
584 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700585 if (qp->state != IB_QPS_RESET) {
586 qp->state = IB_QPS_RESET;
587
588 /* Let drivers flush their waitlist */
589 rdi->driver_f.flush_qp_waiters(qp);
590 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
591 spin_unlock(&qp->s_lock);
592 spin_unlock(&qp->s_hlock);
593 spin_unlock_irq(&qp->r_lock);
594
595 /* Stop the send queue and the retry timer */
596 rdi->driver_f.stop_send_queue(qp);
597
598 /* Wait for things to stop */
599 rdi->driver_f.quiesce_qp(qp);
600
601 /* take qp out the hash and wait for it to be unused */
602 rvt_remove_qp(rdi, qp);
603 wait_event(qp->wait, !atomic_read(&qp->refcount));
604
605 /* grab the lock b/c it was locked at call time */
606 spin_lock_irq(&qp->r_lock);
607 spin_lock(&qp->s_hlock);
608 spin_lock(&qp->s_lock);
609
610 rvt_clear_mr_refs(qp, 1);
611 /*
612 * Let the driver do any tear down or re-init it needs to for
613 * a qp that has been reset
614 */
615 rdi->driver_f.notify_qp_reset(qp);
616 }
617 rvt_init_qp(rdi, qp, type);
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700618 lockdep_assert_held(&qp->r_lock);
619 lockdep_assert_held(&qp->s_hlock);
620 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700621}
622
623/**
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800624 * rvt_create_qp - create a queue pair for a device
625 * @ibpd: the protection domain who's device we create the queue pair for
626 * @init_attr: the attributes of the queue pair
627 * @udata: user data for libibverbs.so
628 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800629 * Queue pair creation is mostly an rvt issue. However, drivers have their own
630 * unique idea of what queue pair numbers mean. For instance there is a reserved
631 * range for PSM.
632 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800633 * Return: the queue pair on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800634 *
635 * Called by the ib_create_qp() core verbs function.
636 */
637struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
638 struct ib_qp_init_attr *init_attr,
639 struct ib_udata *udata)
640{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800641 struct rvt_qp *qp;
642 int err;
643 struct rvt_swqe *swq = NULL;
644 size_t sz;
645 size_t sg_list_sz;
646 struct ib_qp *ret = ERR_PTR(-ENOMEM);
647 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
648 void *priv = NULL;
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800649 gfp_t gfp;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700650 size_t sqsize;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800651
652 if (!rdi)
653 return ERR_PTR(-EINVAL);
654
655 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
656 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800657 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800658 return ERR_PTR(-EINVAL);
659
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800660 /* GFP_NOIO is applicable to RC QP's only */
661
662 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
663 init_attr->qp_type != IB_QPT_RC)
664 return ERR_PTR(-EINVAL);
665
666 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
667 GFP_NOIO : GFP_KERNEL;
668
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800669 /* Check receive queue parameters if no SRQ is specified. */
670 if (!init_attr->srq) {
671 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
672 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
673 return ERR_PTR(-EINVAL);
674
675 if (init_attr->cap.max_send_sge +
676 init_attr->cap.max_send_wr +
677 init_attr->cap.max_recv_sge +
678 init_attr->cap.max_recv_wr == 0)
679 return ERR_PTR(-EINVAL);
680 }
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700681 sqsize =
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700682 init_attr->cap.max_send_wr + 1 +
683 rdi->dparms.reserved_operations;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800684 switch (init_attr->qp_type) {
685 case IB_QPT_SMI:
686 case IB_QPT_GSI:
687 if (init_attr->port_num == 0 ||
688 init_attr->port_num > ibpd->device->phys_port_cnt)
689 return ERR_PTR(-EINVAL);
690 case IB_QPT_UC:
691 case IB_QPT_RC:
692 case IB_QPT_UD:
693 sz = sizeof(struct rvt_sge) *
694 init_attr->cap.max_send_sge +
695 sizeof(struct rvt_swqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800696 if (gfp == GFP_NOIO)
697 swq = __vmalloc(
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700698 sqsize * sz,
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700699 gfp | __GFP_ZERO, PAGE_KERNEL);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800700 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700701 swq = vzalloc_node(
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700702 sqsize * sz,
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800703 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800704 if (!swq)
705 return ERR_PTR(-ENOMEM);
706
707 sz = sizeof(*qp);
708 sg_list_sz = 0;
709 if (init_attr->srq) {
710 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
711
712 if (srq->rq.max_sge > 1)
713 sg_list_sz = sizeof(*qp->r_sg_list) *
714 (srq->rq.max_sge - 1);
715 } else if (init_attr->cap.max_recv_sge > 1)
716 sg_list_sz = sizeof(*qp->r_sg_list) *
717 (init_attr->cap.max_recv_sge - 1);
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800718 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800719 if (!qp)
720 goto bail_swq;
721
722 RCU_INIT_POINTER(qp->next, NULL);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700723 if (init_attr->qp_type == IB_QPT_RC) {
724 qp->s_ack_queue =
725 kzalloc_node(
726 sizeof(*qp->s_ack_queue) *
727 rvt_max_atomic(rdi),
728 gfp,
729 rdi->dparms.node);
730 if (!qp->s_ack_queue)
731 goto bail_qp;
732 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800733
734 /*
735 * Driver needs to set up it's private QP structure and do any
736 * initialization that is needed.
737 */
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800738 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700739 if (IS_ERR(priv)) {
740 ret = priv;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800741 goto bail_qp;
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700742 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800743 qp->priv = priv;
744 qp->timeout_jiffies =
745 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
746 1000UL);
747 if (init_attr->srq) {
748 sz = 0;
749 } else {
750 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
751 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
752 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
753 sizeof(struct rvt_rwqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800754 if (udata)
755 qp->r_rq.wq = vmalloc_user(
756 sizeof(struct rvt_rwq) +
757 qp->r_rq.size * sz);
758 else if (gfp == GFP_NOIO)
759 qp->r_rq.wq = __vmalloc(
760 sizeof(struct rvt_rwq) +
761 qp->r_rq.size * sz,
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700762 gfp | __GFP_ZERO, PAGE_KERNEL);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800763 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700764 qp->r_rq.wq = vzalloc_node(
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800765 sizeof(struct rvt_rwq) +
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800766 qp->r_rq.size * sz,
767 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800768 if (!qp->r_rq.wq)
769 goto bail_driver_priv;
770 }
771
772 /*
773 * ib_create_qp() will initialize qp->ibqp
774 * except for qp->ibqp.qp_num.
775 */
776 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800777 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800778 spin_lock_init(&qp->s_lock);
779 spin_lock_init(&qp->r_rq.lock);
780 atomic_set(&qp->refcount, 0);
Jianxin Xiongd9f87232016-07-25 13:38:25 -0700781 atomic_set(&qp->local_ops_pending, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800782 init_waitqueue_head(&qp->wait);
783 init_timer(&qp->s_timer);
784 qp->s_timer.data = (unsigned long)qp;
785 INIT_LIST_HEAD(&qp->rspwait);
786 qp->state = IB_QPS_RESET;
787 qp->s_wq = swq;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700788 qp->s_size = sqsize;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800789 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800790 qp->s_max_sge = init_attr->cap.max_send_sge;
791 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
792 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
793
794 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
795 init_attr->qp_type,
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800796 init_attr->port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800797 if (err < 0) {
798 ret = ERR_PTR(err);
799 goto bail_rq_wq;
800 }
801 qp->ibqp.qp_num = err;
802 qp->port_num = init_attr->port_num;
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700803 rvt_init_qp(rdi, qp, init_attr->qp_type);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800804 break;
805
806 default:
807 /* Don't support raw QPs */
808 return ERR_PTR(-EINVAL);
809 }
810
811 init_attr->cap.max_inline_data = 0;
812
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800813 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800814 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800815 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800816 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800817 if (udata && udata->outlen >= sizeof(__u64)) {
818 if (!qp->r_rq.wq) {
819 __u64 offset = 0;
820
821 err = ib_copy_to_udata(udata, &offset,
822 sizeof(offset));
823 if (err) {
824 ret = ERR_PTR(err);
825 goto bail_qpn;
826 }
827 } else {
828 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
829
830 qp->ip = rvt_create_mmap_info(rdi, s,
831 ibpd->uobject->context,
832 qp->r_rq.wq);
833 if (!qp->ip) {
834 ret = ERR_PTR(-ENOMEM);
835 goto bail_qpn;
836 }
837
838 err = ib_copy_to_udata(udata, &qp->ip->offset,
839 sizeof(qp->ip->offset));
840 if (err) {
841 ret = ERR_PTR(err);
842 goto bail_ip;
843 }
844 }
Mike Marciniszynef086c02016-03-07 11:35:08 -0800845 qp->pid = current->pid;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800846 }
847
848 spin_lock(&rdi->n_qps_lock);
849 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
850 spin_unlock(&rdi->n_qps_lock);
851 ret = ERR_PTR(-ENOMEM);
852 goto bail_ip;
853 }
854
855 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800856 /*
857 * Maintain a busy_jiffies variable that will be added to the timeout
858 * period in mod_retry_timer and add_retry_timer. This busy jiffies
859 * is scaled by the number of rc qps created for the device to reduce
860 * the number of timeouts occurring when there is a large number of
861 * qps. busy_jiffies is incremented every rc qp scaling interval.
862 * The scaling interval is selected based on extensive performance
863 * evaluation of targeted workloads.
864 */
865 if (init_attr->qp_type == IB_QPT_RC) {
866 rdi->n_rc_qps++;
867 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
868 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800869 spin_unlock(&rdi->n_qps_lock);
870
871 if (qp->ip) {
872 spin_lock_irq(&rdi->pending_lock);
873 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
874 spin_unlock_irq(&rdi->pending_lock);
875 }
876
877 ret = &qp->ibqp;
878
879 /*
880 * We have our QP and its good, now keep track of what types of opcodes
881 * can be processed on this QP. We do this by keeping track of what the
882 * 3 high order bits of the opcode are.
883 */
884 switch (init_attr->qp_type) {
885 case IB_QPT_SMI:
886 case IB_QPT_GSI:
887 case IB_QPT_UD:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700888 qp->allowed_ops = IB_OPCODE_UD;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800889 break;
890 case IB_QPT_RC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700891 qp->allowed_ops = IB_OPCODE_RC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800892 break;
893 case IB_QPT_UC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700894 qp->allowed_ops = IB_OPCODE_UC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800895 break;
896 default:
897 ret = ERR_PTR(-EINVAL);
898 goto bail_ip;
899 }
900
901 return ret;
902
903bail_ip:
904 kref_put(&qp->ip->ref, rvt_release_mmap_info);
905
906bail_qpn:
907 free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
908
909bail_rq_wq:
Mike Marciniszyn56c8ca52016-08-16 13:26:29 -0700910 if (!qp->ip)
911 vfree(qp->r_rq.wq);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800912
913bail_driver_priv:
914 rdi->driver_f.qp_priv_free(rdi, qp);
915
916bail_qp:
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700917 kfree(qp->s_ack_queue);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800918 kfree(qp);
919
920bail_swq:
921 vfree(swq);
922
923 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800924}
925
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800926/**
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800927 * rvt_error_qp - put a QP into the error state
928 * @qp: the QP to put into the error state
929 * @err: the receive completion error to signal if a RWQE is active
930 *
931 * Flushes both send and receive work queues.
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800932 *
933 * Return: true if last WQE event should be generated.
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800934 * The QP r_lock and s_lock should be held and interrupts disabled.
935 * If we are already in error state, just return.
936 */
937int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
938{
939 struct ib_wc wc;
940 int ret = 0;
941 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
942
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700943 lockdep_assert_held(&qp->r_lock);
944 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800945 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
946 goto bail;
947
948 qp->state = IB_QPS_ERR;
949
950 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
951 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
952 del_timer(&qp->s_timer);
953 }
954
955 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
956 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
957
958 rdi->driver_f.notify_error_qp(qp);
959
960 /* Schedule the sending tasklet to drain the send work queue. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800961 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800962 rdi->driver_f.schedule_send(qp);
963
964 rvt_clear_mr_refs(qp, 0);
965
966 memset(&wc, 0, sizeof(wc));
967 wc.qp = &qp->ibqp;
968 wc.opcode = IB_WC_RECV;
969
970 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
971 wc.wr_id = qp->r_wr_id;
972 wc.status = err;
973 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
974 }
975 wc.status = IB_WC_WR_FLUSH_ERR;
976
977 if (qp->r_rq.wq) {
978 struct rvt_rwq *wq;
979 u32 head;
980 u32 tail;
981
982 spin_lock(&qp->r_rq.lock);
983
984 /* sanity check pointers before trusting them */
985 wq = qp->r_rq.wq;
986 head = wq->head;
987 if (head >= qp->r_rq.size)
988 head = 0;
989 tail = wq->tail;
990 if (tail >= qp->r_rq.size)
991 tail = 0;
992 while (tail != head) {
993 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
994 if (++tail >= qp->r_rq.size)
995 tail = 0;
996 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
997 }
998 wq->tail = tail;
999
1000 spin_unlock(&qp->r_rq.lock);
1001 } else if (qp->ibqp.event_handler) {
1002 ret = 1;
1003 }
1004
1005bail:
1006 return ret;
1007}
1008EXPORT_SYMBOL(rvt_error_qp);
1009
1010/*
1011 * Put the QP into the hash table.
1012 * The hash table holds a reference to the QP.
1013 */
1014static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1015{
1016 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1017 unsigned long flags;
1018
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001019 rvt_get_qp(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001020 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1021
1022 if (qp->ibqp.qp_num <= 1) {
1023 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1024 } else {
1025 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1026
1027 qp->next = rdi->qp_dev->qp_table[n];
1028 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1029 trace_rvt_qpinsert(qp, n);
1030 }
1031
1032 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1033}
1034
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001035/**
Parav Pandit61347fa2016-09-13 19:40:50 +05301036 * rvt_modify_qp - modify the attributes of a queue pair
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001037 * @ibqp: the queue pair who's attributes we're modifying
1038 * @attr: the new attributes
1039 * @attr_mask: the mask of attributes to modify
1040 * @udata: user data for libibverbs.so
1041 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001042 * Return: 0 on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001043 */
1044int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1045 int attr_mask, struct ib_udata *udata)
1046{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001047 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1048 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1049 enum ib_qp_state cur_state, new_state;
1050 struct ib_event ev;
1051 int lastwqe = 0;
1052 int mig = 0;
1053 int pmtu = 0; /* for gcc warning only */
1054 enum rdma_link_layer link;
1055
1056 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1057
1058 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001059 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001060 spin_lock(&qp->s_lock);
1061
1062 cur_state = attr_mask & IB_QP_CUR_STATE ?
1063 attr->cur_qp_state : qp->state;
1064 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1065
1066 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1067 attr_mask, link))
1068 goto inval;
1069
Ira Weinye85ec332016-01-22 13:04:38 -08001070 if (rdi->driver_f.check_modify_qp &&
1071 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1072 goto inval;
1073
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001074 if (attr_mask & IB_QP_AV) {
1075 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1076 goto inval;
1077 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1078 goto inval;
1079 }
1080
1081 if (attr_mask & IB_QP_ALT_PATH) {
1082 if (attr->alt_ah_attr.dlid >=
1083 be16_to_cpu(IB_MULTICAST_LID_BASE))
1084 goto inval;
1085 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1086 goto inval;
1087 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1088 goto inval;
1089 }
1090
1091 if (attr_mask & IB_QP_PKEY_INDEX)
1092 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1093 goto inval;
1094
1095 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1096 if (attr->min_rnr_timer > 31)
1097 goto inval;
1098
1099 if (attr_mask & IB_QP_PORT)
1100 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1101 qp->ibqp.qp_type == IB_QPT_GSI ||
1102 attr->port_num == 0 ||
1103 attr->port_num > ibqp->device->phys_port_cnt)
1104 goto inval;
1105
1106 if (attr_mask & IB_QP_DEST_QPN)
1107 if (attr->dest_qp_num > RVT_QPN_MASK)
1108 goto inval;
1109
1110 if (attr_mask & IB_QP_RETRY_CNT)
1111 if (attr->retry_cnt > 7)
1112 goto inval;
1113
1114 if (attr_mask & IB_QP_RNR_RETRY)
1115 if (attr->rnr_retry > 7)
1116 goto inval;
1117
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001118 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001119 * Don't allow invalid path_mtu values. OK to set greater
1120 * than the active mtu (or even the max_cap, if we have tuned
1121 * that to a small mtu. We'll set qp->path_mtu
1122 * to the lesser of requested attribute mtu and active,
1123 * for packetizing messages.
1124 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001125 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001126 if (attr_mask & IB_QP_PATH_MTU) {
1127 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1128 if (pmtu < 0)
1129 goto inval;
1130 }
1131
1132 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1133 if (attr->path_mig_state == IB_MIG_REARM) {
1134 if (qp->s_mig_state == IB_MIG_ARMED)
1135 goto inval;
1136 if (new_state != IB_QPS_RTS)
1137 goto inval;
1138 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1139 if (qp->s_mig_state == IB_MIG_REARM)
1140 goto inval;
1141 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1142 goto inval;
1143 if (qp->s_mig_state == IB_MIG_ARMED)
1144 mig = 1;
1145 } else {
1146 goto inval;
1147 }
1148 }
1149
1150 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1151 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1152 goto inval;
1153
1154 switch (new_state) {
1155 case IB_QPS_RESET:
1156 if (qp->state != IB_QPS_RESET)
1157 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1158 break;
1159
1160 case IB_QPS_RTR:
1161 /* Allow event to re-trigger if QP set to RTR more than once */
1162 qp->r_flags &= ~RVT_R_COMM_EST;
1163 qp->state = new_state;
1164 break;
1165
1166 case IB_QPS_SQD:
1167 qp->s_draining = qp->s_last != qp->s_cur;
1168 qp->state = new_state;
1169 break;
1170
1171 case IB_QPS_SQE:
1172 if (qp->ibqp.qp_type == IB_QPT_RC)
1173 goto inval;
1174 qp->state = new_state;
1175 break;
1176
1177 case IB_QPS_ERR:
1178 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1179 break;
1180
1181 default:
1182 qp->state = new_state;
1183 break;
1184 }
1185
1186 if (attr_mask & IB_QP_PKEY_INDEX)
1187 qp->s_pkey_index = attr->pkey_index;
1188
1189 if (attr_mask & IB_QP_PORT)
1190 qp->port_num = attr->port_num;
1191
1192 if (attr_mask & IB_QP_DEST_QPN)
1193 qp->remote_qpn = attr->dest_qp_num;
1194
1195 if (attr_mask & IB_QP_SQ_PSN) {
1196 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1197 qp->s_psn = qp->s_next_psn;
1198 qp->s_sending_psn = qp->s_next_psn;
1199 qp->s_last_psn = qp->s_next_psn - 1;
1200 qp->s_sending_hpsn = qp->s_last_psn;
1201 }
1202
1203 if (attr_mask & IB_QP_RQ_PSN)
1204 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1205
1206 if (attr_mask & IB_QP_ACCESS_FLAGS)
1207 qp->qp_access_flags = attr->qp_access_flags;
1208
1209 if (attr_mask & IB_QP_AV) {
1210 qp->remote_ah_attr = attr->ah_attr;
1211 qp->s_srate = attr->ah_attr.static_rate;
1212 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1213 }
1214
1215 if (attr_mask & IB_QP_ALT_PATH) {
1216 qp->alt_ah_attr = attr->alt_ah_attr;
1217 qp->s_alt_pkey_index = attr->alt_pkey_index;
1218 }
1219
1220 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1221 qp->s_mig_state = attr->path_mig_state;
1222 if (mig) {
1223 qp->remote_ah_attr = qp->alt_ah_attr;
1224 qp->port_num = qp->alt_ah_attr.port_num;
1225 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001226 }
1227 }
1228
1229 if (attr_mask & IB_QP_PATH_MTU) {
1230 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1231 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001232 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001233 }
1234
1235 if (attr_mask & IB_QP_RETRY_CNT) {
1236 qp->s_retry_cnt = attr->retry_cnt;
1237 qp->s_retry = attr->retry_cnt;
1238 }
1239
1240 if (attr_mask & IB_QP_RNR_RETRY) {
1241 qp->s_rnr_retry_cnt = attr->rnr_retry;
1242 qp->s_rnr_retry = attr->rnr_retry;
1243 }
1244
1245 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1246 qp->r_min_rnr_timer = attr->min_rnr_timer;
1247
1248 if (attr_mask & IB_QP_TIMEOUT) {
1249 qp->timeout = attr->timeout;
1250 qp->timeout_jiffies =
1251 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1252 1000UL);
1253 }
1254
1255 if (attr_mask & IB_QP_QKEY)
1256 qp->qkey = attr->qkey;
1257
1258 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1259 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1260
1261 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1262 qp->s_max_rd_atomic = attr->max_rd_atomic;
1263
Ira Weinye85ec332016-01-22 13:04:38 -08001264 if (rdi->driver_f.modify_qp)
1265 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1266
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001267 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001268 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001269 spin_unlock_irq(&qp->r_lock);
1270
1271 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1272 rvt_insert_qp(rdi, qp);
1273
1274 if (lastwqe) {
1275 ev.device = qp->ibqp.device;
1276 ev.element.qp = &qp->ibqp;
1277 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1278 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1279 }
1280 if (mig) {
1281 ev.device = qp->ibqp.device;
1282 ev.element.qp = &qp->ibqp;
1283 ev.event = IB_EVENT_PATH_MIG;
1284 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1285 }
1286 return 0;
1287
1288inval:
1289 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001290 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001291 spin_unlock_irq(&qp->r_lock);
1292 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001293}
1294
Dennis Dalessandro79a225b2016-02-14 12:11:20 -08001295/** rvt_free_qpn - Free a qpn from the bit map
1296 * @qpt: QP table
1297 * @qpn: queue pair number to free
1298 */
1299static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1300{
1301 struct rvt_qpn_map *map;
1302
1303 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1304 if (map->page)
1305 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1306}
1307
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001308/**
1309 * rvt_destroy_qp - destroy a queue pair
1310 * @ibqp: the queue pair to destroy
1311 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001312 * Note that this can be called while the QP is actively sending or
1313 * receiving!
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001314 *
1315 * Return: 0 on success.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001316 */
1317int rvt_destroy_qp(struct ib_qp *ibqp)
1318{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001319 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1320 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001321
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001322 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001323 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001324 spin_lock(&qp->s_lock);
1325 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1326 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001327 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001328 spin_unlock_irq(&qp->r_lock);
1329
1330 /* qpn is now available for use again */
1331 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1332
1333 spin_lock(&rdi->n_qps_lock);
1334 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001335 if (qp->ibqp.qp_type == IB_QPT_RC) {
1336 rdi->n_rc_qps--;
1337 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1338 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001339 spin_unlock(&rdi->n_qps_lock);
1340
1341 if (qp->ip)
1342 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1343 else
1344 vfree(qp->r_rq.wq);
1345 vfree(qp->s_wq);
1346 rdi->driver_f.qp_priv_free(rdi, qp);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001347 kfree(qp->s_ack_queue);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001348 kfree(qp);
1349 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001350}
1351
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001352/**
1353 * rvt_query_qp - query an ipbq
1354 * @ibqp: IB qp to query
1355 * @attr: attr struct to fill in
1356 * @attr_mask: attr mask ignored
1357 * @init_attr: struct to fill in
1358 *
1359 * Return: always 0
1360 */
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001361int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1362 int attr_mask, struct ib_qp_init_attr *init_attr)
1363{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001364 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1365 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1366
1367 attr->qp_state = qp->state;
1368 attr->cur_qp_state = attr->qp_state;
1369 attr->path_mtu = qp->path_mtu;
1370 attr->path_mig_state = qp->s_mig_state;
1371 attr->qkey = qp->qkey;
1372 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1373 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1374 attr->dest_qp_num = qp->remote_qpn;
1375 attr->qp_access_flags = qp->qp_access_flags;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001376 attr->cap.max_send_wr = qp->s_size - 1 -
1377 rdi->dparms.reserved_operations;
Harish Chegondi74d2d502016-01-22 13:05:04 -08001378 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1379 attr->cap.max_send_sge = qp->s_max_sge;
1380 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1381 attr->cap.max_inline_data = 0;
1382 attr->ah_attr = qp->remote_ah_attr;
1383 attr->alt_ah_attr = qp->alt_ah_attr;
1384 attr->pkey_index = qp->s_pkey_index;
1385 attr->alt_pkey_index = qp->s_alt_pkey_index;
1386 attr->en_sqd_async_notify = 0;
1387 attr->sq_draining = qp->s_draining;
1388 attr->max_rd_atomic = qp->s_max_rd_atomic;
1389 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1390 attr->min_rnr_timer = qp->r_min_rnr_timer;
1391 attr->port_num = qp->port_num;
1392 attr->timeout = qp->timeout;
1393 attr->retry_cnt = qp->s_retry_cnt;
1394 attr->rnr_retry = qp->s_rnr_retry_cnt;
1395 attr->alt_port_num = qp->alt_ah_attr.port_num;
1396 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;
1615 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) {
1688 acc = wr->opcode >= IB_WR_RDMA_READ ?
1689 IB_ACCESS_LOCAL_WRITE : 0;
1690 for (i = 0; i < wr->num_sge; i++) {
1691 u32 length = wr->sg_list[i].length;
1692 int ok;
1693
1694 if (length == 0)
1695 continue;
1696 ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1697 &wr->sg_list[i], acc);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001698 if (!ok) {
1699 ret = -EINVAL;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001700 goto bail_inval_free;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001701 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001702 wqe->length += length;
1703 j++;
1704 }
1705 wqe->wr.num_sge = j;
1706 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001707
1708 /* general part of wqe valid - allow for driver checks */
1709 if (rdi->driver_f.check_send_wqe) {
1710 ret = rdi->driver_f.check_send_wqe(qp, wqe);
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001711 if (ret < 0)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001712 goto bail_inval_free;
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001713 if (ret)
1714 *call_send = ret;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001715 }
1716
1717 log_pmtu = qp->log_pmtu;
1718 if (qp->ibqp.qp_type != IB_QPT_UC &&
1719 qp->ibqp.qp_type != IB_QPT_RC) {
1720 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1721
1722 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001723 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1724 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001725
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001726 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001727 if (local_ops_delayed)
1728 atomic_inc(&qp->local_ops_pending);
1729 else
1730 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001731 wqe->ssn = 0;
1732 wqe->psn = 0;
1733 wqe->lpsn = 0;
1734 } else {
1735 wqe->ssn = qp->s_ssn++;
1736 wqe->psn = qp->s_next_psn;
1737 wqe->lpsn = wqe->psn +
1738 (wqe->length ?
1739 ((wqe->length - 1) >> log_pmtu) :
1740 0);
1741 qp->s_next_psn = wqe->lpsn + 1;
1742 }
Harish Chegondie16689e2016-02-14 12:10:12 -08001743 trace_rvt_post_one_wr(qp, wqe);
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001744 if (unlikely(reserved_op))
1745 rvt_qp_wqe_reserve(qp, wqe);
1746 else
1747 qp->s_avail--;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001748 smp_wmb(); /* see request builders */
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001749 qp->s_head = next;
1750
1751 return 0;
1752
1753bail_inval_free:
1754 /* release mr holds */
1755 while (j) {
1756 struct rvt_sge *sge = &wqe->sg_list[--j];
1757
1758 rvt_put_mr(sge->mr);
1759 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001760 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001761}
1762
1763/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001764 * rvt_post_send - post a send on a QP
1765 * @ibqp: the QP to post the send on
1766 * @wr: the list of work requests to post
1767 * @bad_wr: the first bad WR is put here
1768 *
1769 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001770 *
1771 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001772 */
1773int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1774 struct ib_send_wr **bad_wr)
1775{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001776 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1777 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1778 unsigned long flags = 0;
1779 int call_send;
1780 unsigned nreq = 0;
1781 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001782
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001783 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001784
1785 /*
1786 * Ensure QP state is such that we can send. If not bail out early,
1787 * there is no need to do this every time we post a send.
1788 */
1789 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001790 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001791 return -EINVAL;
1792 }
1793
1794 /*
1795 * If the send queue is empty, and we only have a single WR then just go
1796 * ahead and kick the send engine into gear. Otherwise we will always
1797 * just schedule the send to happen later.
1798 */
1799 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1800
1801 for (; wr; wr = wr->next) {
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001802 err = rvt_post_one_wr(qp, wr, &call_send);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001803 if (unlikely(err)) {
1804 *bad_wr = wr;
1805 goto bail;
1806 }
1807 nreq++;
1808 }
1809bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001810 spin_unlock_irqrestore(&qp->s_hlock, flags);
1811 if (nreq) {
1812 if (call_send)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001813 rdi->driver_f.do_send(qp);
Jubin Johne6d2e012016-04-12 10:47:00 -07001814 else
1815 rdi->driver_f.schedule_send_no_lock(qp);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001816 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001817 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001818}
1819
1820/**
1821 * rvt_post_srq_receive - post a receive on a shared receive queue
1822 * @ibsrq: the SRQ to post the receive on
1823 * @wr: the list of work requests to post
1824 * @bad_wr: A pointer to the first WR to cause a problem is put here
1825 *
1826 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001827 *
1828 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001829 */
1830int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1831 struct ib_recv_wr **bad_wr)
1832{
Jubin Johnb8f881b2016-02-03 14:14:36 -08001833 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1834 struct rvt_rwq *wq;
1835 unsigned long flags;
1836
1837 for (; wr; wr = wr->next) {
1838 struct rvt_rwqe *wqe;
1839 u32 next;
1840 int i;
1841
1842 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1843 *bad_wr = wr;
1844 return -EINVAL;
1845 }
1846
1847 spin_lock_irqsave(&srq->rq.lock, flags);
1848 wq = srq->rq.wq;
1849 next = wq->head + 1;
1850 if (next >= srq->rq.size)
1851 next = 0;
1852 if (next == wq->tail) {
1853 spin_unlock_irqrestore(&srq->rq.lock, flags);
1854 *bad_wr = wr;
1855 return -ENOMEM;
1856 }
1857
1858 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1859 wqe->wr_id = wr->wr_id;
1860 wqe->num_sge = wr->num_sge;
1861 for (i = 0; i < wr->num_sge; i++)
1862 wqe->sg_list[i] = wr->sg_list[i];
1863 /* Make sure queue entry is written before the head index. */
1864 smp_wmb();
1865 wq->head = next;
1866 spin_unlock_irqrestore(&srq->rq.lock, flags);
1867 }
1868 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001869}