blob: 673e2f6742157ab997ce5e41cbf2b91936d9c476 [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 Marciniszynd2b8d4d2016-01-22 12:50:43 -080079static void get_map_page(struct rvt_qpn_table *qpt,
80 struct rvt_qpn_map *map,
81 gfp_t gfp)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080082{
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -080083 unsigned long page = get_zeroed_page(gfp);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080084
85 /*
86 * Free the page if someone raced with us installing it.
87 */
88
89 spin_lock(&qpt->lock);
90 if (map->page)
91 free_page(page);
92 else
93 map->page = (void *)page;
94 spin_unlock(&qpt->lock);
95}
96
97/**
98 * init_qpn_table - initialize the QP number table for a device
99 * @qpt: the QPN table
100 */
101static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
102{
103 u32 offset, i;
104 struct rvt_qpn_map *map;
105 int ret = 0;
106
Harish Chegondifef2efd2016-01-22 12:50:30 -0800107 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800108 return -EINVAL;
109
110 spin_lock_init(&qpt->lock);
111
112 qpt->last = rdi->dparms.qpn_start;
113 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
114
115 /*
116 * Drivers may want some QPs beyond what we need for verbs let them use
117 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
118 * for those. The reserved range must be *after* the range which verbs
119 * will pick from.
120 */
121
122 /* Figure out number of bit maps needed before reserved range */
123 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
124
125 /* This should always be zero */
126 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
127
128 /* Starting with the first reserved bit map */
129 map = &qpt->map[qpt->nmaps];
130
131 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
132 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
Harish Chegondifef2efd2016-01-22 12:50:30 -0800133 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800134 if (!map->page) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800135 get_map_page(qpt, map, GFP_KERNEL);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800136 if (!map->page) {
137 ret = -ENOMEM;
138 break;
139 }
140 }
141 set_bit(offset, map->page);
142 offset++;
143 if (offset == RVT_BITS_PER_PAGE) {
144 /* next page */
145 qpt->nmaps++;
146 map++;
147 offset = 0;
148 }
149 }
150 return ret;
151}
152
153/**
154 * free_qpn_table - free the QP number table for a device
155 * @qpt: the QPN table
156 */
157static void free_qpn_table(struct rvt_qpn_table *qpt)
158{
159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
162 free_page((unsigned long)qpt->map[i].page);
163}
164
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800165/**
166 * rvt_driver_qp_init - Init driver qp resources
167 * @rdi: rvt dev strucutre
168 *
169 * Return: 0 on success
170 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800171int rvt_driver_qp_init(struct rvt_dev_info *rdi)
172{
173 int i;
174 int ret = -ENOMEM;
175
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800176 if (!rdi->dparms.qp_table_size)
177 return -EINVAL;
178
179 /*
180 * If driver is not doing any QP allocation then make sure it is
181 * providing the necessary QP functions.
182 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800183 if (!rdi->driver_f.free_all_qps ||
184 !rdi->driver_f.qp_priv_alloc ||
185 !rdi->driver_f.qp_priv_free ||
186 !rdi->driver_f.notify_qp_reset)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800187 return -EINVAL;
188
189 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800190 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
191 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800192 if (!rdi->qp_dev)
193 return -ENOMEM;
194
195 /* allocate hash table */
196 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
197 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
198 rdi->qp_dev->qp_table =
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800199 kmalloc_node(rdi->qp_dev->qp_table_size *
200 sizeof(*rdi->qp_dev->qp_table),
201 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800202 if (!rdi->qp_dev->qp_table)
203 goto no_qp_table;
204
205 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
206 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
207
208 spin_lock_init(&rdi->qp_dev->qpt_lock);
209
210 /* initialize qpn map */
211 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
212 goto fail_table;
213
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800214 spin_lock_init(&rdi->n_qps_lock);
215
216 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800217
218fail_table:
219 kfree(rdi->qp_dev->qp_table);
220 free_qpn_table(&rdi->qp_dev->qpn_table);
221
222no_qp_table:
223 kfree(rdi->qp_dev);
224
225 return ret;
226}
227
228/**
229 * free_all_qps - check for QPs still in use
230 * @qpt: the QP table to empty
231 *
232 * There should not be any QPs still in use.
233 * Free memory for table.
234 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800235static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800236{
237 unsigned long flags;
238 struct rvt_qp *qp;
239 unsigned n, qp_inuse = 0;
240 spinlock_t *ql; /* work around too long line below */
241
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800242 if (rdi->driver_f.free_all_qps)
243 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800244
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800245 qp_inuse += rvt_mcast_tree_empty(rdi);
246
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800247 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800248 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800249
250 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800251 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800252 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
253 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
254 lockdep_is_held(ql));
255 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800256
257 for (; qp; qp = rcu_dereference_protected(qp->next,
258 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800259 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800260 }
261 spin_unlock_irqrestore(ql, flags);
262 synchronize_rcu();
263 return qp_inuse;
264}
265
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800266/**
267 * rvt_qp_exit - clean up qps on device exit
268 * @rdi: rvt dev structure
269 *
270 * Check for qp leaks and free resources.
271 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800272void rvt_qp_exit(struct rvt_dev_info *rdi)
273{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800274 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800275
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800276 if (qps_inuse)
277 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
278 qps_inuse);
279 if (!rdi->qp_dev)
280 return;
281
282 kfree(rdi->qp_dev->qp_table);
283 free_qpn_table(&rdi->qp_dev->qpn_table);
284 kfree(rdi->qp_dev);
285}
286
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800287static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
288 struct rvt_qpn_map *map, unsigned off)
289{
290 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
291}
292
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800293/**
294 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
295 * IB_QPT_SMI/IB_QPT_GSI
296 *@rdi: rvt device info structure
297 *@qpt: queue pair number table pointer
298 *@port_num: IB port number, 1 based, comes from core
299 *
300 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800301 */
302static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800303 enum ib_qp_type type, u8 port_num, gfp_t gfp)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800304{
305 u32 i, offset, max_scan, qpn;
306 struct rvt_qpn_map *map;
307 u32 ret;
308
309 if (rdi->driver_f.alloc_qpn)
Ira Weinyb7b3cf42016-02-03 14:15:28 -0800310 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800311
312 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
313 unsigned n;
314
315 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800316 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800317 spin_lock(&qpt->lock);
318 if (qpt->flags & n)
319 ret = -EINVAL;
320 else
321 qpt->flags |= n;
322 spin_unlock(&qpt->lock);
323 goto bail;
324 }
325
326 qpn = qpt->last + qpt->incr;
327 if (qpn >= RVT_QPN_MAX)
328 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
329 /* offset carries bit 0 */
330 offset = qpn & RVT_BITS_PER_PAGE_MASK;
331 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
332 max_scan = qpt->nmaps - !offset;
333 for (i = 0;;) {
334 if (unlikely(!map->page)) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800335 get_map_page(qpt, map, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800336 if (unlikely(!map->page))
337 break;
338 }
339 do {
340 if (!test_and_set_bit(offset, map->page)) {
341 qpt->last = qpn;
342 ret = qpn;
343 goto bail;
344 }
345 offset += qpt->incr;
346 /*
347 * This qpn might be bogus if offset >= BITS_PER_PAGE.
348 * That is OK. It gets re-assigned below
349 */
350 qpn = mk_qpn(qpt, map, offset);
351 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
352 /*
353 * In order to keep the number of pages allocated to a
354 * minimum, we scan the all existing pages before increasing
355 * the size of the bitmap table.
356 */
357 if (++i > max_scan) {
358 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
359 break;
360 map = &qpt->map[qpt->nmaps++];
361 /* start at incr with current bit 0 */
362 offset = qpt->incr | (offset & 1);
363 } else if (map < &qpt->map[qpt->nmaps]) {
364 ++map;
365 /* start at incr with current bit 0 */
366 offset = qpt->incr | (offset & 1);
367 } else {
368 map = &qpt->map[0];
369 /* wrap to first map page, invert bit 0 */
370 offset = qpt->incr | ((offset & 1) ^ 1);
371 }
Brian Welty501edc422016-06-09 07:51:20 -0700372 /* there can be no set bits in low-order QoS bits */
373 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800374 qpn = mk_qpn(qpt, map, offset);
375 }
376
377 ret = -ENOMEM;
378
379bail:
380 return ret;
381}
382
383static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
384{
385 struct rvt_qpn_map *map;
386
387 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
388 if (map->page)
389 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
390}
391
392/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800393 * rvt_clear_mr_refs - Drop help mr refs
394 * @qp: rvt qp data structure
395 * @clr_sends: If shoudl clear send side or not
396 */
397static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
398{
399 unsigned n;
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700400 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800401
402 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
403 rvt_put_ss(&qp->s_rdma_read_sge);
404
405 rvt_put_ss(&qp->r_sge);
406
407 if (clr_sends) {
408 while (qp->s_last != qp->s_head) {
409 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
410 unsigned i;
411
412 for (i = 0; i < wqe->wr.num_sge; i++) {
413 struct rvt_sge *sge = &wqe->sg_list[i];
414
415 rvt_put_mr(sge->mr);
416 }
417 if (qp->ibqp.qp_type == IB_QPT_UD ||
418 qp->ibqp.qp_type == IB_QPT_SMI ||
419 qp->ibqp.qp_type == IB_QPT_GSI)
420 atomic_dec(&ibah_to_rvtah(
421 wqe->ud_wr.ah)->refcount);
422 if (++qp->s_last >= qp->s_size)
423 qp->s_last = 0;
424 smp_wmb(); /* see qp_set_savail */
425 }
426 if (qp->s_rdma_mr) {
427 rvt_put_mr(qp->s_rdma_mr);
428 qp->s_rdma_mr = NULL;
429 }
430 }
431
432 if (qp->ibqp.qp_type != IB_QPT_RC)
433 return;
434
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700435 for (n = 0; n < rvt_max_atomic(rdi); n++) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800436 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
437
Ira Weinyfe508272016-07-27 21:07:36 -0400438 if (e->rdma_sge.mr) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800439 rvt_put_mr(e->rdma_sge.mr);
440 e->rdma_sge.mr = NULL;
441 }
442 }
443}
444
445/**
446 * rvt_remove_qp - remove qp form table
447 * @rdi: rvt dev struct
448 * @qp: qp to remove
449 *
450 * Remove the QP from the table so it can't be found asynchronously by
451 * the receive routine.
452 */
453static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
454{
455 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
456 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
457 unsigned long flags;
458 int removed = 1;
459
460 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
461
462 if (rcu_dereference_protected(rvp->qp[0],
463 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
464 RCU_INIT_POINTER(rvp->qp[0], NULL);
465 } else if (rcu_dereference_protected(rvp->qp[1],
466 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
467 RCU_INIT_POINTER(rvp->qp[1], NULL);
468 } else {
469 struct rvt_qp *q;
470 struct rvt_qp __rcu **qpp;
471
472 removed = 0;
473 qpp = &rdi->qp_dev->qp_table[n];
474 for (; (q = rcu_dereference_protected(*qpp,
475 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
476 qpp = &q->next) {
477 if (q == qp) {
478 RCU_INIT_POINTER(*qpp,
479 rcu_dereference_protected(qp->next,
480 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
481 removed = 1;
482 trace_rvt_qpremove(qp, n);
483 break;
484 }
485 }
486 }
487
488 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
489 if (removed) {
490 synchronize_rcu();
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -0700491 rvt_put_qp(qp);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800492 }
493}
494
495/**
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700496 * rvt_init_qp - initialize the QP state to the reset state
497 * @qp: the QP to init or reinit
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800498 * @type: the QP type
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700499 *
500 * This function is called from both rvt_create_qp() and
501 * rvt_reset_qp(). The difference is that the reset
502 * patch the necessary locks to protect against concurent
503 * access.
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800504 */
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700505static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
506 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800507{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800508 qp->remote_qpn = 0;
509 qp->qkey = 0;
510 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800511 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
512 qp->s_hdrwords = 0;
513 qp->s_wqe = NULL;
514 qp->s_draining = 0;
515 qp->s_next_psn = 0;
516 qp->s_last_psn = 0;
517 qp->s_sending_psn = 0;
518 qp->s_sending_hpsn = 0;
519 qp->s_psn = 0;
520 qp->r_psn = 0;
521 qp->r_msn = 0;
522 if (type == IB_QPT_RC) {
523 qp->s_state = IB_OPCODE_RC_SEND_LAST;
524 qp->r_state = IB_OPCODE_RC_SEND_LAST;
525 } else {
526 qp->s_state = IB_OPCODE_UC_SEND_LAST;
527 qp->r_state = IB_OPCODE_UC_SEND_LAST;
528 }
529 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
530 qp->r_nak_state = 0;
531 qp->r_aflags = 0;
532 qp->r_flags = 0;
533 qp->s_head = 0;
534 qp->s_tail = 0;
535 qp->s_cur = 0;
536 qp->s_acked = 0;
537 qp->s_last = 0;
538 qp->s_ssn = 1;
539 qp->s_lsn = 0;
540 qp->s_mig_state = IB_MIG_MIGRATED;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800541 qp->r_head_ack_queue = 0;
542 qp->s_tail_ack_queue = 0;
543 qp->s_num_rd_atomic = 0;
544 if (qp->r_rq.wq) {
545 qp->r_rq.wq->head = 0;
546 qp->r_rq.wq->tail = 0;
547 }
548 qp->r_sge.num_sge = 0;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700549 atomic_set(&qp->s_reserved_used, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800550}
551
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800552/**
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700553 * rvt_reset_qp - initialize the QP state to the reset state
554 * @qp: the QP to reset
555 * @type: the QP type
556 *
557 * r_lock, s_hlock, and s_lock are required to be held by the caller
558 */
559static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
560 enum ib_qp_type type)
561 __must_hold(&qp->s_lock)
562 __must_hold(&qp->s_hlock)
563 __must_hold(&qp->r_lock)
564{
565 if (qp->state != IB_QPS_RESET) {
566 qp->state = IB_QPS_RESET;
567
568 /* Let drivers flush their waitlist */
569 rdi->driver_f.flush_qp_waiters(qp);
570 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
571 spin_unlock(&qp->s_lock);
572 spin_unlock(&qp->s_hlock);
573 spin_unlock_irq(&qp->r_lock);
574
575 /* Stop the send queue and the retry timer */
576 rdi->driver_f.stop_send_queue(qp);
577
578 /* Wait for things to stop */
579 rdi->driver_f.quiesce_qp(qp);
580
581 /* take qp out the hash and wait for it to be unused */
582 rvt_remove_qp(rdi, qp);
583 wait_event(qp->wait, !atomic_read(&qp->refcount));
584
585 /* grab the lock b/c it was locked at call time */
586 spin_lock_irq(&qp->r_lock);
587 spin_lock(&qp->s_hlock);
588 spin_lock(&qp->s_lock);
589
590 rvt_clear_mr_refs(qp, 1);
591 /*
592 * Let the driver do any tear down or re-init it needs to for
593 * a qp that has been reset
594 */
595 rdi->driver_f.notify_qp_reset(qp);
596 }
597 rvt_init_qp(rdi, qp, type);
598}
599
600/**
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800601 * rvt_create_qp - create a queue pair for a device
602 * @ibpd: the protection domain who's device we create the queue pair for
603 * @init_attr: the attributes of the queue pair
604 * @udata: user data for libibverbs.so
605 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800606 * Queue pair creation is mostly an rvt issue. However, drivers have their own
607 * unique idea of what queue pair numbers mean. For instance there is a reserved
608 * range for PSM.
609 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800610 * Return: the queue pair on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800611 *
612 * Called by the ib_create_qp() core verbs function.
613 */
614struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
615 struct ib_qp_init_attr *init_attr,
616 struct ib_udata *udata)
617{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800618 struct rvt_qp *qp;
619 int err;
620 struct rvt_swqe *swq = NULL;
621 size_t sz;
622 size_t sg_list_sz;
623 struct ib_qp *ret = ERR_PTR(-ENOMEM);
624 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
625 void *priv = NULL;
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800626 gfp_t gfp;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700627 size_t sqsize;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800628
629 if (!rdi)
630 return ERR_PTR(-EINVAL);
631
632 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
633 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800634 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800635 return ERR_PTR(-EINVAL);
636
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800637 /* GFP_NOIO is applicable to RC QP's only */
638
639 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
640 init_attr->qp_type != IB_QPT_RC)
641 return ERR_PTR(-EINVAL);
642
643 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
644 GFP_NOIO : GFP_KERNEL;
645
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800646 /* Check receive queue parameters if no SRQ is specified. */
647 if (!init_attr->srq) {
648 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
649 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
650 return ERR_PTR(-EINVAL);
651
652 if (init_attr->cap.max_send_sge +
653 init_attr->cap.max_send_wr +
654 init_attr->cap.max_recv_sge +
655 init_attr->cap.max_recv_wr == 0)
656 return ERR_PTR(-EINVAL);
657 }
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700658 sqsize =
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700659 init_attr->cap.max_send_wr + 1 +
660 rdi->dparms.reserved_operations;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800661 switch (init_attr->qp_type) {
662 case IB_QPT_SMI:
663 case IB_QPT_GSI:
664 if (init_attr->port_num == 0 ||
665 init_attr->port_num > ibpd->device->phys_port_cnt)
666 return ERR_PTR(-EINVAL);
667 case IB_QPT_UC:
668 case IB_QPT_RC:
669 case IB_QPT_UD:
670 sz = sizeof(struct rvt_sge) *
671 init_attr->cap.max_send_sge +
672 sizeof(struct rvt_swqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800673 if (gfp == GFP_NOIO)
674 swq = __vmalloc(
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700675 sqsize * sz,
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700676 gfp | __GFP_ZERO, PAGE_KERNEL);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800677 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700678 swq = vzalloc_node(
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700679 sqsize * sz,
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800680 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800681 if (!swq)
682 return ERR_PTR(-ENOMEM);
683
684 sz = sizeof(*qp);
685 sg_list_sz = 0;
686 if (init_attr->srq) {
687 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
688
689 if (srq->rq.max_sge > 1)
690 sg_list_sz = sizeof(*qp->r_sg_list) *
691 (srq->rq.max_sge - 1);
692 } else if (init_attr->cap.max_recv_sge > 1)
693 sg_list_sz = sizeof(*qp->r_sg_list) *
694 (init_attr->cap.max_recv_sge - 1);
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800695 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800696 if (!qp)
697 goto bail_swq;
698
699 RCU_INIT_POINTER(qp->next, NULL);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700700 if (init_attr->qp_type == IB_QPT_RC) {
701 qp->s_ack_queue =
702 kzalloc_node(
703 sizeof(*qp->s_ack_queue) *
704 rvt_max_atomic(rdi),
705 gfp,
706 rdi->dparms.node);
707 if (!qp->s_ack_queue)
708 goto bail_qp;
709 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800710
711 /*
712 * Driver needs to set up it's private QP structure and do any
713 * initialization that is needed.
714 */
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800715 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700716 if (IS_ERR(priv)) {
717 ret = priv;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800718 goto bail_qp;
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700719 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800720 qp->priv = priv;
721 qp->timeout_jiffies =
722 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
723 1000UL);
724 if (init_attr->srq) {
725 sz = 0;
726 } else {
727 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
728 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
729 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
730 sizeof(struct rvt_rwqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800731 if (udata)
732 qp->r_rq.wq = vmalloc_user(
733 sizeof(struct rvt_rwq) +
734 qp->r_rq.size * sz);
735 else if (gfp == GFP_NOIO)
736 qp->r_rq.wq = __vmalloc(
737 sizeof(struct rvt_rwq) +
738 qp->r_rq.size * sz,
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700739 gfp | __GFP_ZERO, PAGE_KERNEL);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800740 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700741 qp->r_rq.wq = vzalloc_node(
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800742 sizeof(struct rvt_rwq) +
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800743 qp->r_rq.size * sz,
744 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800745 if (!qp->r_rq.wq)
746 goto bail_driver_priv;
747 }
748
749 /*
750 * ib_create_qp() will initialize qp->ibqp
751 * except for qp->ibqp.qp_num.
752 */
753 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800754 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800755 spin_lock_init(&qp->s_lock);
756 spin_lock_init(&qp->r_rq.lock);
757 atomic_set(&qp->refcount, 0);
Jianxin Xiongd9f87232016-07-25 13:38:25 -0700758 atomic_set(&qp->local_ops_pending, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800759 init_waitqueue_head(&qp->wait);
760 init_timer(&qp->s_timer);
761 qp->s_timer.data = (unsigned long)qp;
762 INIT_LIST_HEAD(&qp->rspwait);
763 qp->state = IB_QPS_RESET;
764 qp->s_wq = swq;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700765 qp->s_size = sqsize;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800766 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800767 qp->s_max_sge = init_attr->cap.max_send_sge;
768 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
769 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
770
771 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
772 init_attr->qp_type,
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800773 init_attr->port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800774 if (err < 0) {
775 ret = ERR_PTR(err);
776 goto bail_rq_wq;
777 }
778 qp->ibqp.qp_num = err;
779 qp->port_num = init_attr->port_num;
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700780 rvt_init_qp(rdi, qp, init_attr->qp_type);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800781 break;
782
783 default:
784 /* Don't support raw QPs */
785 return ERR_PTR(-EINVAL);
786 }
787
788 init_attr->cap.max_inline_data = 0;
789
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800790 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800791 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800792 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800793 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800794 if (udata && udata->outlen >= sizeof(__u64)) {
795 if (!qp->r_rq.wq) {
796 __u64 offset = 0;
797
798 err = ib_copy_to_udata(udata, &offset,
799 sizeof(offset));
800 if (err) {
801 ret = ERR_PTR(err);
802 goto bail_qpn;
803 }
804 } else {
805 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
806
807 qp->ip = rvt_create_mmap_info(rdi, s,
808 ibpd->uobject->context,
809 qp->r_rq.wq);
810 if (!qp->ip) {
811 ret = ERR_PTR(-ENOMEM);
812 goto bail_qpn;
813 }
814
815 err = ib_copy_to_udata(udata, &qp->ip->offset,
816 sizeof(qp->ip->offset));
817 if (err) {
818 ret = ERR_PTR(err);
819 goto bail_ip;
820 }
821 }
Mike Marciniszynef086c02016-03-07 11:35:08 -0800822 qp->pid = current->pid;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800823 }
824
825 spin_lock(&rdi->n_qps_lock);
826 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
827 spin_unlock(&rdi->n_qps_lock);
828 ret = ERR_PTR(-ENOMEM);
829 goto bail_ip;
830 }
831
832 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800833 /*
834 * Maintain a busy_jiffies variable that will be added to the timeout
835 * period in mod_retry_timer and add_retry_timer. This busy jiffies
836 * is scaled by the number of rc qps created for the device to reduce
837 * the number of timeouts occurring when there is a large number of
838 * qps. busy_jiffies is incremented every rc qp scaling interval.
839 * The scaling interval is selected based on extensive performance
840 * evaluation of targeted workloads.
841 */
842 if (init_attr->qp_type == IB_QPT_RC) {
843 rdi->n_rc_qps++;
844 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
845 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800846 spin_unlock(&rdi->n_qps_lock);
847
848 if (qp->ip) {
849 spin_lock_irq(&rdi->pending_lock);
850 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
851 spin_unlock_irq(&rdi->pending_lock);
852 }
853
854 ret = &qp->ibqp;
855
856 /*
857 * We have our QP and its good, now keep track of what types of opcodes
858 * can be processed on this QP. We do this by keeping track of what the
859 * 3 high order bits of the opcode are.
860 */
861 switch (init_attr->qp_type) {
862 case IB_QPT_SMI:
863 case IB_QPT_GSI:
864 case IB_QPT_UD:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700865 qp->allowed_ops = IB_OPCODE_UD;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800866 break;
867 case IB_QPT_RC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700868 qp->allowed_ops = IB_OPCODE_RC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800869 break;
870 case IB_QPT_UC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700871 qp->allowed_ops = IB_OPCODE_UC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800872 break;
873 default:
874 ret = ERR_PTR(-EINVAL);
875 goto bail_ip;
876 }
877
878 return ret;
879
880bail_ip:
881 kref_put(&qp->ip->ref, rvt_release_mmap_info);
882
883bail_qpn:
884 free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
885
886bail_rq_wq:
Mike Marciniszyn56c8ca52016-08-16 13:26:29 -0700887 if (!qp->ip)
888 vfree(qp->r_rq.wq);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800889
890bail_driver_priv:
891 rdi->driver_f.qp_priv_free(rdi, qp);
892
893bail_qp:
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700894 kfree(qp->s_ack_queue);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800895 kfree(qp);
896
897bail_swq:
898 vfree(swq);
899
900 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800901}
902
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800903/**
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800904 * rvt_error_qp - put a QP into the error state
905 * @qp: the QP to put into the error state
906 * @err: the receive completion error to signal if a RWQE is active
907 *
908 * Flushes both send and receive work queues.
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800909 *
910 * Return: true if last WQE event should be generated.
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800911 * The QP r_lock and s_lock should be held and interrupts disabled.
912 * If we are already in error state, just return.
913 */
914int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
915{
916 struct ib_wc wc;
917 int ret = 0;
918 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
919
920 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
921 goto bail;
922
923 qp->state = IB_QPS_ERR;
924
925 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
926 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
927 del_timer(&qp->s_timer);
928 }
929
930 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
931 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
932
933 rdi->driver_f.notify_error_qp(qp);
934
935 /* Schedule the sending tasklet to drain the send work queue. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800936 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800937 rdi->driver_f.schedule_send(qp);
938
939 rvt_clear_mr_refs(qp, 0);
940
941 memset(&wc, 0, sizeof(wc));
942 wc.qp = &qp->ibqp;
943 wc.opcode = IB_WC_RECV;
944
945 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
946 wc.wr_id = qp->r_wr_id;
947 wc.status = err;
948 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
949 }
950 wc.status = IB_WC_WR_FLUSH_ERR;
951
952 if (qp->r_rq.wq) {
953 struct rvt_rwq *wq;
954 u32 head;
955 u32 tail;
956
957 spin_lock(&qp->r_rq.lock);
958
959 /* sanity check pointers before trusting them */
960 wq = qp->r_rq.wq;
961 head = wq->head;
962 if (head >= qp->r_rq.size)
963 head = 0;
964 tail = wq->tail;
965 if (tail >= qp->r_rq.size)
966 tail = 0;
967 while (tail != head) {
968 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
969 if (++tail >= qp->r_rq.size)
970 tail = 0;
971 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
972 }
973 wq->tail = tail;
974
975 spin_unlock(&qp->r_rq.lock);
976 } else if (qp->ibqp.event_handler) {
977 ret = 1;
978 }
979
980bail:
981 return ret;
982}
983EXPORT_SYMBOL(rvt_error_qp);
984
985/*
986 * Put the QP into the hash table.
987 * The hash table holds a reference to the QP.
988 */
989static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
990{
991 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
992 unsigned long flags;
993
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -0700994 rvt_get_qp(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800995 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
996
997 if (qp->ibqp.qp_num <= 1) {
998 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
999 } else {
1000 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1001
1002 qp->next = rdi->qp_dev->qp_table[n];
1003 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1004 trace_rvt_qpinsert(qp, n);
1005 }
1006
1007 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1008}
1009
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001010/**
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001011 * qib_modify_qp - modify the attributes of a queue pair
1012 * @ibqp: the queue pair who's attributes we're modifying
1013 * @attr: the new attributes
1014 * @attr_mask: the mask of attributes to modify
1015 * @udata: user data for libibverbs.so
1016 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001017 * Return: 0 on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001018 */
1019int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1020 int attr_mask, struct ib_udata *udata)
1021{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001022 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1023 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1024 enum ib_qp_state cur_state, new_state;
1025 struct ib_event ev;
1026 int lastwqe = 0;
1027 int mig = 0;
1028 int pmtu = 0; /* for gcc warning only */
1029 enum rdma_link_layer link;
1030
1031 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1032
1033 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001034 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001035 spin_lock(&qp->s_lock);
1036
1037 cur_state = attr_mask & IB_QP_CUR_STATE ?
1038 attr->cur_qp_state : qp->state;
1039 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1040
1041 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1042 attr_mask, link))
1043 goto inval;
1044
Ira Weinye85ec332016-01-22 13:04:38 -08001045 if (rdi->driver_f.check_modify_qp &&
1046 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1047 goto inval;
1048
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001049 if (attr_mask & IB_QP_AV) {
1050 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1051 goto inval;
1052 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1053 goto inval;
1054 }
1055
1056 if (attr_mask & IB_QP_ALT_PATH) {
1057 if (attr->alt_ah_attr.dlid >=
1058 be16_to_cpu(IB_MULTICAST_LID_BASE))
1059 goto inval;
1060 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1061 goto inval;
1062 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1063 goto inval;
1064 }
1065
1066 if (attr_mask & IB_QP_PKEY_INDEX)
1067 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1068 goto inval;
1069
1070 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1071 if (attr->min_rnr_timer > 31)
1072 goto inval;
1073
1074 if (attr_mask & IB_QP_PORT)
1075 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1076 qp->ibqp.qp_type == IB_QPT_GSI ||
1077 attr->port_num == 0 ||
1078 attr->port_num > ibqp->device->phys_port_cnt)
1079 goto inval;
1080
1081 if (attr_mask & IB_QP_DEST_QPN)
1082 if (attr->dest_qp_num > RVT_QPN_MASK)
1083 goto inval;
1084
1085 if (attr_mask & IB_QP_RETRY_CNT)
1086 if (attr->retry_cnt > 7)
1087 goto inval;
1088
1089 if (attr_mask & IB_QP_RNR_RETRY)
1090 if (attr->rnr_retry > 7)
1091 goto inval;
1092
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001093 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001094 * Don't allow invalid path_mtu values. OK to set greater
1095 * than the active mtu (or even the max_cap, if we have tuned
1096 * that to a small mtu. We'll set qp->path_mtu
1097 * to the lesser of requested attribute mtu and active,
1098 * for packetizing messages.
1099 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001100 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001101 if (attr_mask & IB_QP_PATH_MTU) {
1102 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1103 if (pmtu < 0)
1104 goto inval;
1105 }
1106
1107 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1108 if (attr->path_mig_state == IB_MIG_REARM) {
1109 if (qp->s_mig_state == IB_MIG_ARMED)
1110 goto inval;
1111 if (new_state != IB_QPS_RTS)
1112 goto inval;
1113 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1114 if (qp->s_mig_state == IB_MIG_REARM)
1115 goto inval;
1116 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1117 goto inval;
1118 if (qp->s_mig_state == IB_MIG_ARMED)
1119 mig = 1;
1120 } else {
1121 goto inval;
1122 }
1123 }
1124
1125 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1126 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1127 goto inval;
1128
1129 switch (new_state) {
1130 case IB_QPS_RESET:
1131 if (qp->state != IB_QPS_RESET)
1132 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1133 break;
1134
1135 case IB_QPS_RTR:
1136 /* Allow event to re-trigger if QP set to RTR more than once */
1137 qp->r_flags &= ~RVT_R_COMM_EST;
1138 qp->state = new_state;
1139 break;
1140
1141 case IB_QPS_SQD:
1142 qp->s_draining = qp->s_last != qp->s_cur;
1143 qp->state = new_state;
1144 break;
1145
1146 case IB_QPS_SQE:
1147 if (qp->ibqp.qp_type == IB_QPT_RC)
1148 goto inval;
1149 qp->state = new_state;
1150 break;
1151
1152 case IB_QPS_ERR:
1153 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1154 break;
1155
1156 default:
1157 qp->state = new_state;
1158 break;
1159 }
1160
1161 if (attr_mask & IB_QP_PKEY_INDEX)
1162 qp->s_pkey_index = attr->pkey_index;
1163
1164 if (attr_mask & IB_QP_PORT)
1165 qp->port_num = attr->port_num;
1166
1167 if (attr_mask & IB_QP_DEST_QPN)
1168 qp->remote_qpn = attr->dest_qp_num;
1169
1170 if (attr_mask & IB_QP_SQ_PSN) {
1171 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1172 qp->s_psn = qp->s_next_psn;
1173 qp->s_sending_psn = qp->s_next_psn;
1174 qp->s_last_psn = qp->s_next_psn - 1;
1175 qp->s_sending_hpsn = qp->s_last_psn;
1176 }
1177
1178 if (attr_mask & IB_QP_RQ_PSN)
1179 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1180
1181 if (attr_mask & IB_QP_ACCESS_FLAGS)
1182 qp->qp_access_flags = attr->qp_access_flags;
1183
1184 if (attr_mask & IB_QP_AV) {
1185 qp->remote_ah_attr = attr->ah_attr;
1186 qp->s_srate = attr->ah_attr.static_rate;
1187 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1188 }
1189
1190 if (attr_mask & IB_QP_ALT_PATH) {
1191 qp->alt_ah_attr = attr->alt_ah_attr;
1192 qp->s_alt_pkey_index = attr->alt_pkey_index;
1193 }
1194
1195 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1196 qp->s_mig_state = attr->path_mig_state;
1197 if (mig) {
1198 qp->remote_ah_attr = qp->alt_ah_attr;
1199 qp->port_num = qp->alt_ah_attr.port_num;
1200 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001201 }
1202 }
1203
1204 if (attr_mask & IB_QP_PATH_MTU) {
1205 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1206 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001207 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001208 }
1209
1210 if (attr_mask & IB_QP_RETRY_CNT) {
1211 qp->s_retry_cnt = attr->retry_cnt;
1212 qp->s_retry = attr->retry_cnt;
1213 }
1214
1215 if (attr_mask & IB_QP_RNR_RETRY) {
1216 qp->s_rnr_retry_cnt = attr->rnr_retry;
1217 qp->s_rnr_retry = attr->rnr_retry;
1218 }
1219
1220 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1221 qp->r_min_rnr_timer = attr->min_rnr_timer;
1222
1223 if (attr_mask & IB_QP_TIMEOUT) {
1224 qp->timeout = attr->timeout;
1225 qp->timeout_jiffies =
1226 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1227 1000UL);
1228 }
1229
1230 if (attr_mask & IB_QP_QKEY)
1231 qp->qkey = attr->qkey;
1232
1233 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1234 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1235
1236 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1237 qp->s_max_rd_atomic = attr->max_rd_atomic;
1238
Ira Weinye85ec332016-01-22 13:04:38 -08001239 if (rdi->driver_f.modify_qp)
1240 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1241
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001242 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001243 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001244 spin_unlock_irq(&qp->r_lock);
1245
1246 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1247 rvt_insert_qp(rdi, qp);
1248
1249 if (lastwqe) {
1250 ev.device = qp->ibqp.device;
1251 ev.element.qp = &qp->ibqp;
1252 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1253 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1254 }
1255 if (mig) {
1256 ev.device = qp->ibqp.device;
1257 ev.element.qp = &qp->ibqp;
1258 ev.event = IB_EVENT_PATH_MIG;
1259 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1260 }
1261 return 0;
1262
1263inval:
1264 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001265 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001266 spin_unlock_irq(&qp->r_lock);
1267 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001268}
1269
Dennis Dalessandro79a225b2016-02-14 12:11:20 -08001270/** rvt_free_qpn - Free a qpn from the bit map
1271 * @qpt: QP table
1272 * @qpn: queue pair number to free
1273 */
1274static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1275{
1276 struct rvt_qpn_map *map;
1277
1278 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1279 if (map->page)
1280 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1281}
1282
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001283/**
1284 * rvt_destroy_qp - destroy a queue pair
1285 * @ibqp: the queue pair to destroy
1286 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001287 * Note that this can be called while the QP is actively sending or
1288 * receiving!
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001289 *
1290 * Return: 0 on success.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001291 */
1292int rvt_destroy_qp(struct ib_qp *ibqp)
1293{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001294 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1295 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001296
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001297 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001298 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001299 spin_lock(&qp->s_lock);
1300 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1301 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001302 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001303 spin_unlock_irq(&qp->r_lock);
1304
1305 /* qpn is now available for use again */
1306 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1307
1308 spin_lock(&rdi->n_qps_lock);
1309 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001310 if (qp->ibqp.qp_type == IB_QPT_RC) {
1311 rdi->n_rc_qps--;
1312 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1313 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001314 spin_unlock(&rdi->n_qps_lock);
1315
1316 if (qp->ip)
1317 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1318 else
1319 vfree(qp->r_rq.wq);
1320 vfree(qp->s_wq);
1321 rdi->driver_f.qp_priv_free(rdi, qp);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001322 kfree(qp->s_ack_queue);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001323 kfree(qp);
1324 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001325}
1326
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001327/**
1328 * rvt_query_qp - query an ipbq
1329 * @ibqp: IB qp to query
1330 * @attr: attr struct to fill in
1331 * @attr_mask: attr mask ignored
1332 * @init_attr: struct to fill in
1333 *
1334 * Return: always 0
1335 */
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001336int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1337 int attr_mask, struct ib_qp_init_attr *init_attr)
1338{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001339 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1340 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1341
1342 attr->qp_state = qp->state;
1343 attr->cur_qp_state = attr->qp_state;
1344 attr->path_mtu = qp->path_mtu;
1345 attr->path_mig_state = qp->s_mig_state;
1346 attr->qkey = qp->qkey;
1347 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1348 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1349 attr->dest_qp_num = qp->remote_qpn;
1350 attr->qp_access_flags = qp->qp_access_flags;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001351 attr->cap.max_send_wr = qp->s_size - 1 -
1352 rdi->dparms.reserved_operations;
Harish Chegondi74d2d502016-01-22 13:05:04 -08001353 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1354 attr->cap.max_send_sge = qp->s_max_sge;
1355 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1356 attr->cap.max_inline_data = 0;
1357 attr->ah_attr = qp->remote_ah_attr;
1358 attr->alt_ah_attr = qp->alt_ah_attr;
1359 attr->pkey_index = qp->s_pkey_index;
1360 attr->alt_pkey_index = qp->s_alt_pkey_index;
1361 attr->en_sqd_async_notify = 0;
1362 attr->sq_draining = qp->s_draining;
1363 attr->max_rd_atomic = qp->s_max_rd_atomic;
1364 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1365 attr->min_rnr_timer = qp->r_min_rnr_timer;
1366 attr->port_num = qp->port_num;
1367 attr->timeout = qp->timeout;
1368 attr->retry_cnt = qp->s_retry_cnt;
1369 attr->rnr_retry = qp->s_rnr_retry_cnt;
1370 attr->alt_port_num = qp->alt_ah_attr.port_num;
1371 attr->alt_timeout = qp->alt_timeout;
1372
1373 init_attr->event_handler = qp->ibqp.event_handler;
1374 init_attr->qp_context = qp->ibqp.qp_context;
1375 init_attr->send_cq = qp->ibqp.send_cq;
1376 init_attr->recv_cq = qp->ibqp.recv_cq;
1377 init_attr->srq = qp->ibqp.srq;
1378 init_attr->cap = attr->cap;
1379 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1380 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1381 else
1382 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1383 init_attr->qp_type = qp->ibqp.qp_type;
1384 init_attr->port_num = qp->port_num;
1385 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001386}
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001387
1388/**
1389 * rvt_post_receive - post a receive on a QP
1390 * @ibqp: the QP to post the receive on
1391 * @wr: the WR to post
1392 * @bad_wr: the first bad WR is put here
1393 *
1394 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001395 *
1396 * Return: 0 on success otherwise errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001397 */
1398int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1399 struct ib_recv_wr **bad_wr)
1400{
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001401 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1402 struct rvt_rwq *wq = qp->r_rq.wq;
1403 unsigned long flags;
Alex Estrin000a8302016-03-07 11:35:51 -08001404 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1405 !qp->ibqp.srq;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001406
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001407 /* Check that state is OK to post receive. */
1408 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1409 *bad_wr = wr;
1410 return -EINVAL;
1411 }
1412
1413 for (; wr; wr = wr->next) {
1414 struct rvt_rwqe *wqe;
1415 u32 next;
1416 int i;
1417
1418 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1419 *bad_wr = wr;
1420 return -EINVAL;
1421 }
1422
1423 spin_lock_irqsave(&qp->r_rq.lock, flags);
1424 next = wq->head + 1;
1425 if (next >= qp->r_rq.size)
1426 next = 0;
1427 if (next == wq->tail) {
1428 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1429 *bad_wr = wr;
1430 return -ENOMEM;
1431 }
Alex Estrin000a8302016-03-07 11:35:51 -08001432 if (unlikely(qp_err_flush)) {
1433 struct ib_wc wc;
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001434
Alex Estrin000a8302016-03-07 11:35:51 -08001435 memset(&wc, 0, sizeof(wc));
1436 wc.qp = &qp->ibqp;
1437 wc.opcode = IB_WC_RECV;
1438 wc.wr_id = wr->wr_id;
1439 wc.status = IB_WC_WR_FLUSH_ERR;
1440 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1441 } else {
1442 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1443 wqe->wr_id = wr->wr_id;
1444 wqe->num_sge = wr->num_sge;
1445 for (i = 0; i < wr->num_sge; i++)
1446 wqe->sg_list[i] = wr->sg_list[i];
1447 /*
1448 * Make sure queue entry is written
1449 * before the head index.
1450 */
1451 smp_wmb();
1452 wq->head = next;
1453 }
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001454 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1455 }
1456 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001457}
1458
1459/**
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001460 * rvt_qp_valid_operation - validate post send wr request
1461 * @qp - the qp
1462 * @post-parms - the post send table for the driver
1463 * @wr - the work request
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001464 *
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001465 * The routine validates the operation based on the
1466 * validation table an returns the length of the operation
1467 * which can extend beyond the ib_send_bw. Operation
1468 * dependent flags key atomic operation validation.
1469 *
1470 * There is an exception for UD qps that validates the pd and
1471 * overrides the length to include the additional UD specific
1472 * length.
1473 *
1474 * Returns a negative error or the length of the work request
1475 * for building the swqe.
1476 */
1477static inline int rvt_qp_valid_operation(
1478 struct rvt_qp *qp,
1479 const struct rvt_operation_params *post_parms,
1480 struct ib_send_wr *wr)
1481{
1482 int len;
1483
1484 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1485 return -EINVAL;
1486 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1487 return -EINVAL;
1488 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1489 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1490 return -EINVAL;
1491 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1492 (wr->num_sge == 0 ||
1493 wr->sg_list[0].length < sizeof(u64) ||
1494 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1495 return -EINVAL;
1496 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1497 !qp->s_max_rd_atomic)
1498 return -EINVAL;
1499 len = post_parms[wr->opcode].length;
1500 /* UD specific */
1501 if (qp->ibqp.qp_type != IB_QPT_UC &&
1502 qp->ibqp.qp_type != IB_QPT_RC) {
1503 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1504 return -EINVAL;
1505 len = sizeof(struct ib_ud_wr);
1506 }
1507 return len;
1508}
1509
1510/**
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001511 * rvt_qp_is_avail - determine queue capacity
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001512 * @qp - the qp
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001513 * @rdi - the rdmavt device
1514 * @reserved_op - is reserved operation
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001515 *
1516 * This assumes the s_hlock is held but the s_last
1517 * qp variable is uncontrolled.
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001518 *
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001519 * For non reserved operations, the qp->s_avail
1520 * may be changed.
1521 *
1522 * The return value is zero or a -ENOMEM.
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001523 */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001524static inline int rvt_qp_is_avail(
1525 struct rvt_qp *qp,
1526 struct rvt_dev_info *rdi,
1527 bool reserved_op)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001528{
1529 u32 slast;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001530 u32 avail;
1531 u32 reserved_used;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001532
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001533 /* see rvt_qp_wqe_unreserve() */
1534 smp_mb__before_atomic();
1535 reserved_used = atomic_read(&qp->s_reserved_used);
1536 if (unlikely(reserved_op)) {
1537 /* see rvt_qp_wqe_unreserve() */
1538 smp_mb__before_atomic();
1539 if (reserved_used >= rdi->dparms.reserved_operations)
1540 return -ENOMEM;
1541 return 0;
1542 }
1543 /* non-reserved operations */
1544 if (likely(qp->s_avail))
1545 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001546 smp_read_barrier_depends(); /* see rc.c */
1547 slast = ACCESS_ONCE(qp->s_last);
1548 if (qp->s_head >= slast)
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001549 avail = qp->s_size - (qp->s_head - slast);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001550 else
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001551 avail = slast - qp->s_head;
1552
1553 /* see rvt_qp_wqe_unreserve() */
1554 smp_mb__before_atomic();
1555 reserved_used = atomic_read(&qp->s_reserved_used);
1556 avail = avail - 1 -
1557 (rdi->dparms.reserved_operations - reserved_used);
1558 /* insure we don't assign a negative s_avail */
1559 if ((s32)avail <= 0)
1560 return -ENOMEM;
1561 qp->s_avail = avail;
1562 if (WARN_ON(qp->s_avail >
1563 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1564 rvt_pr_err(rdi,
1565 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1566 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1567 qp->s_head, qp->s_tail, qp->s_cur,
1568 qp->s_acked, qp->s_last);
1569 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001570}
1571
1572/**
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001573 * rvt_post_one_wr - post one RC, UC, or UD send work request
1574 * @qp: the QP to post on
1575 * @wr: the work request to send
1576 */
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001577static int rvt_post_one_wr(struct rvt_qp *qp,
1578 struct ib_send_wr *wr,
1579 int *call_send)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001580{
1581 struct rvt_swqe *wqe;
1582 u32 next;
1583 int i;
1584 int j;
1585 int acc;
1586 struct rvt_lkey_table *rkt;
1587 struct rvt_pd *pd;
1588 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001589 u8 log_pmtu;
1590 int ret;
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001591 size_t cplen;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001592 bool reserved_op;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001593 int local_ops_delayed = 0;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001594
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001595 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1596
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001597 /* IB spec says that num_sge == 0 is OK. */
1598 if (unlikely(wr->num_sge > qp->s_max_sge))
1599 return -EINVAL;
1600
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001601 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1602 if (ret < 0)
1603 return ret;
1604 cplen = ret;
1605
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001606 /*
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001607 * Local operations include fast register and local invalidate.
1608 * Fast register needs to be processed immediately because the
1609 * registered lkey may be used by following work requests and the
1610 * lkey needs to be valid at the time those requests are posted.
1611 * Local invalidate can be processed immediately if fencing is
1612 * not required and no previous local invalidate ops are pending.
1613 * Signaled local operations that have been processed immediately
1614 * need to have requests with "completion only" flags set posted
1615 * to the send queue in order to generate completions.
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001616 */
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001617 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001618 switch (wr->opcode) {
1619 case IB_WR_REG_MR:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001620 ret = rvt_fast_reg_mr(qp,
1621 reg_wr(wr)->mr,
1622 reg_wr(wr)->key,
1623 reg_wr(wr)->access);
1624 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1625 return ret;
1626 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001627 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001628 if ((wr->send_flags & IB_SEND_FENCE) ||
1629 atomic_read(&qp->local_ops_pending)) {
1630 local_ops_delayed = 1;
1631 } else {
1632 ret = rvt_invalidate_rkey(
1633 qp, wr->ex.invalidate_rkey);
1634 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1635 return ret;
1636 }
1637 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001638 default:
1639 return -EINVAL;
1640 }
1641 }
1642
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001643 reserved_op = rdi->post_parms[wr->opcode].flags &
1644 RVT_OPERATION_USE_RESERVE;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001645 /* check for avail */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001646 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1647 if (ret)
1648 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001649 next = qp->s_head + 1;
1650 if (next >= qp->s_size)
1651 next = 0;
Ira Weiny60c30f52016-02-03 14:14:45 -08001652
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001653 rkt = &rdi->lkey_table;
1654 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1655 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1656
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001657 /* cplen has length from above */
1658 memcpy(&wqe->wr, wr, cplen);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001659
1660 wqe->length = 0;
1661 j = 0;
1662 if (wr->num_sge) {
1663 acc = wr->opcode >= IB_WR_RDMA_READ ?
1664 IB_ACCESS_LOCAL_WRITE : 0;
1665 for (i = 0; i < wr->num_sge; i++) {
1666 u32 length = wr->sg_list[i].length;
1667 int ok;
1668
1669 if (length == 0)
1670 continue;
1671 ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1672 &wr->sg_list[i], acc);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001673 if (!ok) {
1674 ret = -EINVAL;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001675 goto bail_inval_free;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001676 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001677 wqe->length += length;
1678 j++;
1679 }
1680 wqe->wr.num_sge = j;
1681 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001682
1683 /* general part of wqe valid - allow for driver checks */
1684 if (rdi->driver_f.check_send_wqe) {
1685 ret = rdi->driver_f.check_send_wqe(qp, wqe);
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001686 if (ret < 0)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001687 goto bail_inval_free;
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001688 if (ret)
1689 *call_send = ret;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001690 }
1691
1692 log_pmtu = qp->log_pmtu;
1693 if (qp->ibqp.qp_type != IB_QPT_UC &&
1694 qp->ibqp.qp_type != IB_QPT_RC) {
1695 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1696
1697 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001698 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1699 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001700
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001701 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001702 if (local_ops_delayed)
1703 atomic_inc(&qp->local_ops_pending);
1704 else
1705 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001706 wqe->ssn = 0;
1707 wqe->psn = 0;
1708 wqe->lpsn = 0;
1709 } else {
1710 wqe->ssn = qp->s_ssn++;
1711 wqe->psn = qp->s_next_psn;
1712 wqe->lpsn = wqe->psn +
1713 (wqe->length ?
1714 ((wqe->length - 1) >> log_pmtu) :
1715 0);
1716 qp->s_next_psn = wqe->lpsn + 1;
1717 }
Harish Chegondie16689e2016-02-14 12:10:12 -08001718 trace_rvt_post_one_wr(qp, wqe);
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001719 if (unlikely(reserved_op))
1720 rvt_qp_wqe_reserve(qp, wqe);
1721 else
1722 qp->s_avail--;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001723 smp_wmb(); /* see request builders */
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001724 qp->s_head = next;
1725
1726 return 0;
1727
1728bail_inval_free:
1729 /* release mr holds */
1730 while (j) {
1731 struct rvt_sge *sge = &wqe->sg_list[--j];
1732
1733 rvt_put_mr(sge->mr);
1734 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001735 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001736}
1737
1738/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001739 * rvt_post_send - post a send on a QP
1740 * @ibqp: the QP to post the send on
1741 * @wr: the list of work requests to post
1742 * @bad_wr: the first bad WR is put here
1743 *
1744 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001745 *
1746 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001747 */
1748int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1749 struct ib_send_wr **bad_wr)
1750{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001751 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1752 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1753 unsigned long flags = 0;
1754 int call_send;
1755 unsigned nreq = 0;
1756 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001757
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001758 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001759
1760 /*
1761 * Ensure QP state is such that we can send. If not bail out early,
1762 * there is no need to do this every time we post a send.
1763 */
1764 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001765 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001766 return -EINVAL;
1767 }
1768
1769 /*
1770 * If the send queue is empty, and we only have a single WR then just go
1771 * ahead and kick the send engine into gear. Otherwise we will always
1772 * just schedule the send to happen later.
1773 */
1774 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1775
1776 for (; wr; wr = wr->next) {
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001777 err = rvt_post_one_wr(qp, wr, &call_send);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001778 if (unlikely(err)) {
1779 *bad_wr = wr;
1780 goto bail;
1781 }
1782 nreq++;
1783 }
1784bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001785 spin_unlock_irqrestore(&qp->s_hlock, flags);
1786 if (nreq) {
1787 if (call_send)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001788 rdi->driver_f.do_send(qp);
Jubin Johne6d2e012016-04-12 10:47:00 -07001789 else
1790 rdi->driver_f.schedule_send_no_lock(qp);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001791 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001792 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001793}
1794
1795/**
1796 * rvt_post_srq_receive - post a receive on a shared receive queue
1797 * @ibsrq: the SRQ to post the receive on
1798 * @wr: the list of work requests to post
1799 * @bad_wr: A pointer to the first WR to cause a problem is put here
1800 *
1801 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001802 *
1803 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001804 */
1805int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1806 struct ib_recv_wr **bad_wr)
1807{
Jubin Johnb8f881b2016-02-03 14:14:36 -08001808 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1809 struct rvt_rwq *wq;
1810 unsigned long flags;
1811
1812 for (; wr; wr = wr->next) {
1813 struct rvt_rwqe *wqe;
1814 u32 next;
1815 int i;
1816
1817 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1818 *bad_wr = wr;
1819 return -EINVAL;
1820 }
1821
1822 spin_lock_irqsave(&srq->rq.lock, flags);
1823 wq = srq->rq.wq;
1824 next = wq->head + 1;
1825 if (next >= srq->rq.size)
1826 next = 0;
1827 if (next == wq->tail) {
1828 spin_unlock_irqrestore(&srq->rq.lock, flags);
1829 *bad_wr = wr;
1830 return -ENOMEM;
1831 }
1832
1833 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1834 wqe->wr_id = wr->wr_id;
1835 wqe->num_sge = wr->num_sge;
1836 for (i = 0; i < wr->num_sge; i++)
1837 wqe->sg_list[i] = wr->sg_list[i];
1838 /* Make sure queue entry is written before the head index. */
1839 smp_wmb();
1840 wq->head = next;
1841 spin_unlock_irqrestore(&srq->rq.lock, flags);
1842 }
1843 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001844}