blob: d7dabdfaab8da01d8cff84c5d5aeae6c91414072 [file] [log] [blame]
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001/*
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002 * Copyright(c) 2016, 2017 Intel Corporation.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080048#include <linux/hash.h>
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080049#include <linux/bitops.h>
50#include <linux/lockdep.h>
Dennis Dalessandro515667f2016-01-22 12:50:17 -080051#include <linux/vmalloc.h>
52#include <linux/slab.h>
53#include <rdma/ib_verbs.h>
Don Hiatt832666c2017-02-08 05:28:25 -080054#include <rdma/ib_hdrs.h>
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080055#include "qp.h"
Dennis Dalessandro515667f2016-01-22 12:50:17 -080056#include "vt.h"
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080057#include "trace.h"
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080058
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -080059static void rvt_rc_timeout(unsigned long arg);
60
61/*
62 * Convert the AETH RNR timeout code into the number of microseconds.
63 */
64static const u32 ib_rvt_rnr_table[32] = {
65 655360, /* 00: 655.36 */
66 10, /* 01: .01 */
67 20, /* 02 .02 */
68 30, /* 03: .03 */
69 40, /* 04: .04 */
70 60, /* 05: .06 */
71 80, /* 06: .08 */
72 120, /* 07: .12 */
73 160, /* 08: .16 */
74 240, /* 09: .24 */
75 320, /* 0A: .32 */
76 480, /* 0B: .48 */
77 640, /* 0C: .64 */
78 960, /* 0D: .96 */
79 1280, /* 0E: 1.28 */
80 1920, /* 0F: 1.92 */
81 2560, /* 10: 2.56 */
82 3840, /* 11: 3.84 */
83 5120, /* 12: 5.12 */
84 7680, /* 13: 7.68 */
85 10240, /* 14: 10.24 */
86 15360, /* 15: 15.36 */
87 20480, /* 16: 20.48 */
88 30720, /* 17: 30.72 */
89 40960, /* 18: 40.96 */
90 61440, /* 19: 61.44 */
91 81920, /* 1A: 81.92 */
92 122880, /* 1B: 122.88 */
93 163840, /* 1C: 163.84 */
94 245760, /* 1D: 245.76 */
95 327680, /* 1E: 327.68 */
96 491520 /* 1F: 491.52 */
97};
98
Dennis Dalessandrobfbac092016-01-22 13:00:22 -080099/*
100 * Note that it is OK to post send work requests in the SQE and ERR
101 * states; rvt_do_send() will process them and generate error
102 * completions as per IB 1.2 C10-96.
103 */
104const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
105 [IB_QPS_RESET] = 0,
106 [IB_QPS_INIT] = RVT_POST_RECV_OK,
107 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
108 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
109 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
110 RVT_PROCESS_NEXT_SEND_OK,
111 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
112 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
113 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
114 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
115 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
116 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
117};
118EXPORT_SYMBOL(ib_rvt_state_ops);
119
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800120static void get_map_page(struct rvt_qpn_table *qpt,
121 struct rvt_qpn_map *map,
122 gfp_t gfp)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800123{
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800124 unsigned long page = get_zeroed_page(gfp);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800125
126 /*
127 * Free the page if someone raced with us installing it.
128 */
129
130 spin_lock(&qpt->lock);
131 if (map->page)
132 free_page(page);
133 else
134 map->page = (void *)page;
135 spin_unlock(&qpt->lock);
136}
137
138/**
139 * init_qpn_table - initialize the QP number table for a device
140 * @qpt: the QPN table
141 */
142static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
143{
144 u32 offset, i;
145 struct rvt_qpn_map *map;
146 int ret = 0;
147
Harish Chegondifef2efd2016-01-22 12:50:30 -0800148 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800149 return -EINVAL;
150
151 spin_lock_init(&qpt->lock);
152
153 qpt->last = rdi->dparms.qpn_start;
154 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
155
156 /*
157 * Drivers may want some QPs beyond what we need for verbs let them use
158 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
159 * for those. The reserved range must be *after* the range which verbs
160 * will pick from.
161 */
162
163 /* Figure out number of bit maps needed before reserved range */
164 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
165
166 /* This should always be zero */
167 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
168
169 /* Starting with the first reserved bit map */
170 map = &qpt->map[qpt->nmaps];
171
172 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
173 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
Harish Chegondifef2efd2016-01-22 12:50:30 -0800174 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800175 if (!map->page) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800176 get_map_page(qpt, map, GFP_KERNEL);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800177 if (!map->page) {
178 ret = -ENOMEM;
179 break;
180 }
181 }
182 set_bit(offset, map->page);
183 offset++;
184 if (offset == RVT_BITS_PER_PAGE) {
185 /* next page */
186 qpt->nmaps++;
187 map++;
188 offset = 0;
189 }
190 }
191 return ret;
192}
193
194/**
195 * free_qpn_table - free the QP number table for a device
196 * @qpt: the QPN table
197 */
198static void free_qpn_table(struct rvt_qpn_table *qpt)
199{
200 int i;
201
202 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
203 free_page((unsigned long)qpt->map[i].page);
204}
205
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800206/**
207 * rvt_driver_qp_init - Init driver qp resources
208 * @rdi: rvt dev strucutre
209 *
210 * Return: 0 on success
211 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800212int rvt_driver_qp_init(struct rvt_dev_info *rdi)
213{
214 int i;
215 int ret = -ENOMEM;
216
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800217 if (!rdi->dparms.qp_table_size)
218 return -EINVAL;
219
220 /*
221 * If driver is not doing any QP allocation then make sure it is
222 * providing the necessary QP functions.
223 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800224 if (!rdi->driver_f.free_all_qps ||
225 !rdi->driver_f.qp_priv_alloc ||
226 !rdi->driver_f.qp_priv_free ||
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800227 !rdi->driver_f.notify_qp_reset ||
228 !rdi->driver_f.notify_restart_rc)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800229 return -EINVAL;
230
231 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800232 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
233 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800234 if (!rdi->qp_dev)
235 return -ENOMEM;
236
237 /* allocate hash table */
238 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
239 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
240 rdi->qp_dev->qp_table =
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800241 kmalloc_node(rdi->qp_dev->qp_table_size *
242 sizeof(*rdi->qp_dev->qp_table),
243 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800244 if (!rdi->qp_dev->qp_table)
245 goto no_qp_table;
246
247 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
248 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
249
250 spin_lock_init(&rdi->qp_dev->qpt_lock);
251
252 /* initialize qpn map */
253 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
254 goto fail_table;
255
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800256 spin_lock_init(&rdi->n_qps_lock);
257
258 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800259
260fail_table:
261 kfree(rdi->qp_dev->qp_table);
262 free_qpn_table(&rdi->qp_dev->qpn_table);
263
264no_qp_table:
265 kfree(rdi->qp_dev);
266
267 return ret;
268}
269
270/**
271 * free_all_qps - check for QPs still in use
272 * @qpt: the QP table to empty
273 *
274 * There should not be any QPs still in use.
275 * Free memory for table.
276 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800277static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800278{
279 unsigned long flags;
280 struct rvt_qp *qp;
281 unsigned n, qp_inuse = 0;
282 spinlock_t *ql; /* work around too long line below */
283
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800284 if (rdi->driver_f.free_all_qps)
285 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800286
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800287 qp_inuse += rvt_mcast_tree_empty(rdi);
288
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800289 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800290 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800291
292 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800293 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800294 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
295 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
296 lockdep_is_held(ql));
297 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800298
299 for (; qp; qp = rcu_dereference_protected(qp->next,
300 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800301 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800302 }
303 spin_unlock_irqrestore(ql, flags);
304 synchronize_rcu();
305 return qp_inuse;
306}
307
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800308/**
309 * rvt_qp_exit - clean up qps on device exit
310 * @rdi: rvt dev structure
311 *
312 * Check for qp leaks and free resources.
313 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800314void rvt_qp_exit(struct rvt_dev_info *rdi)
315{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800316 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800317
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800318 if (qps_inuse)
319 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
320 qps_inuse);
321 if (!rdi->qp_dev)
322 return;
323
324 kfree(rdi->qp_dev->qp_table);
325 free_qpn_table(&rdi->qp_dev->qpn_table);
326 kfree(rdi->qp_dev);
327}
328
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800329static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
330 struct rvt_qpn_map *map, unsigned off)
331{
332 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
333}
334
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800335/**
336 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
337 * IB_QPT_SMI/IB_QPT_GSI
338 *@rdi: rvt device info structure
339 *@qpt: queue pair number table pointer
340 *@port_num: IB port number, 1 based, comes from core
341 *
342 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800343 */
344static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800345 enum ib_qp_type type, u8 port_num, gfp_t gfp)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800346{
347 u32 i, offset, max_scan, qpn;
348 struct rvt_qpn_map *map;
349 u32 ret;
350
351 if (rdi->driver_f.alloc_qpn)
Ira Weinyb7b3cf42016-02-03 14:15:28 -0800352 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800353
354 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
355 unsigned n;
356
357 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800358 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800359 spin_lock(&qpt->lock);
360 if (qpt->flags & n)
361 ret = -EINVAL;
362 else
363 qpt->flags |= n;
364 spin_unlock(&qpt->lock);
365 goto bail;
366 }
367
368 qpn = qpt->last + qpt->incr;
369 if (qpn >= RVT_QPN_MAX)
370 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
371 /* offset carries bit 0 */
372 offset = qpn & RVT_BITS_PER_PAGE_MASK;
373 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
374 max_scan = qpt->nmaps - !offset;
375 for (i = 0;;) {
376 if (unlikely(!map->page)) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800377 get_map_page(qpt, map, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800378 if (unlikely(!map->page))
379 break;
380 }
381 do {
382 if (!test_and_set_bit(offset, map->page)) {
383 qpt->last = qpn;
384 ret = qpn;
385 goto bail;
386 }
387 offset += qpt->incr;
388 /*
389 * This qpn might be bogus if offset >= BITS_PER_PAGE.
390 * That is OK. It gets re-assigned below
391 */
392 qpn = mk_qpn(qpt, map, offset);
393 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
394 /*
395 * In order to keep the number of pages allocated to a
396 * minimum, we scan the all existing pages before increasing
397 * the size of the bitmap table.
398 */
399 if (++i > max_scan) {
400 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
401 break;
402 map = &qpt->map[qpt->nmaps++];
403 /* start at incr with current bit 0 */
404 offset = qpt->incr | (offset & 1);
405 } else if (map < &qpt->map[qpt->nmaps]) {
406 ++map;
407 /* start at incr with current bit 0 */
408 offset = qpt->incr | (offset & 1);
409 } else {
410 map = &qpt->map[0];
411 /* wrap to first map page, invert bit 0 */
412 offset = qpt->incr | ((offset & 1) ^ 1);
413 }
Brian Welty501edc422016-06-09 07:51:20 -0700414 /* there can be no set bits in low-order QoS bits */
415 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800416 qpn = mk_qpn(qpt, map, offset);
417 }
418
419 ret = -ENOMEM;
420
421bail:
422 return ret;
423}
424
425static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
426{
427 struct rvt_qpn_map *map;
428
429 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
430 if (map->page)
431 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
432}
433
434/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800435 * rvt_clear_mr_refs - Drop help mr refs
436 * @qp: rvt qp data structure
437 * @clr_sends: If shoudl clear send side or not
438 */
439static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
440{
441 unsigned n;
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700442 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800443
444 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
445 rvt_put_ss(&qp->s_rdma_read_sge);
446
447 rvt_put_ss(&qp->r_sge);
448
449 if (clr_sends) {
450 while (qp->s_last != qp->s_head) {
451 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
452 unsigned i;
453
454 for (i = 0; i < wqe->wr.num_sge; i++) {
455 struct rvt_sge *sge = &wqe->sg_list[i];
456
457 rvt_put_mr(sge->mr);
458 }
459 if (qp->ibqp.qp_type == IB_QPT_UD ||
460 qp->ibqp.qp_type == IB_QPT_SMI ||
461 qp->ibqp.qp_type == IB_QPT_GSI)
462 atomic_dec(&ibah_to_rvtah(
463 wqe->ud_wr.ah)->refcount);
464 if (++qp->s_last >= qp->s_size)
465 qp->s_last = 0;
466 smp_wmb(); /* see qp_set_savail */
467 }
468 if (qp->s_rdma_mr) {
469 rvt_put_mr(qp->s_rdma_mr);
470 qp->s_rdma_mr = NULL;
471 }
472 }
473
474 if (qp->ibqp.qp_type != IB_QPT_RC)
475 return;
476
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700477 for (n = 0; n < rvt_max_atomic(rdi); n++) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800478 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
479
Ira Weinyfe508272016-07-27 21:07:36 -0400480 if (e->rdma_sge.mr) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800481 rvt_put_mr(e->rdma_sge.mr);
482 e->rdma_sge.mr = NULL;
483 }
484 }
485}
486
487/**
488 * rvt_remove_qp - remove qp form table
489 * @rdi: rvt dev struct
490 * @qp: qp to remove
491 *
492 * Remove the QP from the table so it can't be found asynchronously by
493 * the receive routine.
494 */
495static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
496{
497 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
498 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
499 unsigned long flags;
500 int removed = 1;
501
502 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
503
504 if (rcu_dereference_protected(rvp->qp[0],
505 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
506 RCU_INIT_POINTER(rvp->qp[0], NULL);
507 } else if (rcu_dereference_protected(rvp->qp[1],
508 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
509 RCU_INIT_POINTER(rvp->qp[1], NULL);
510 } else {
511 struct rvt_qp *q;
512 struct rvt_qp __rcu **qpp;
513
514 removed = 0;
515 qpp = &rdi->qp_dev->qp_table[n];
516 for (; (q = rcu_dereference_protected(*qpp,
517 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
518 qpp = &q->next) {
519 if (q == qp) {
520 RCU_INIT_POINTER(*qpp,
521 rcu_dereference_protected(qp->next,
522 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
523 removed = 1;
524 trace_rvt_qpremove(qp, n);
525 break;
526 }
527 }
528 }
529
530 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
531 if (removed) {
532 synchronize_rcu();
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -0700533 rvt_put_qp(qp);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800534 }
535}
536
537/**
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700538 * rvt_init_qp - initialize the QP state to the reset state
539 * @qp: the QP to init or reinit
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800540 * @type: the QP type
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700541 *
542 * This function is called from both rvt_create_qp() and
543 * rvt_reset_qp(). The difference is that the reset
544 * patch the necessary locks to protect against concurent
545 * access.
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800546 */
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700547static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
548 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800549{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800550 qp->remote_qpn = 0;
551 qp->qkey = 0;
552 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800553 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
554 qp->s_hdrwords = 0;
555 qp->s_wqe = NULL;
556 qp->s_draining = 0;
557 qp->s_next_psn = 0;
558 qp->s_last_psn = 0;
559 qp->s_sending_psn = 0;
560 qp->s_sending_hpsn = 0;
561 qp->s_psn = 0;
562 qp->r_psn = 0;
563 qp->r_msn = 0;
564 if (type == IB_QPT_RC) {
565 qp->s_state = IB_OPCODE_RC_SEND_LAST;
566 qp->r_state = IB_OPCODE_RC_SEND_LAST;
567 } else {
568 qp->s_state = IB_OPCODE_UC_SEND_LAST;
569 qp->r_state = IB_OPCODE_UC_SEND_LAST;
570 }
571 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
572 qp->r_nak_state = 0;
573 qp->r_aflags = 0;
574 qp->r_flags = 0;
575 qp->s_head = 0;
576 qp->s_tail = 0;
577 qp->s_cur = 0;
578 qp->s_acked = 0;
579 qp->s_last = 0;
580 qp->s_ssn = 1;
581 qp->s_lsn = 0;
582 qp->s_mig_state = IB_MIG_MIGRATED;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800583 qp->r_head_ack_queue = 0;
584 qp->s_tail_ack_queue = 0;
585 qp->s_num_rd_atomic = 0;
586 if (qp->r_rq.wq) {
587 qp->r_rq.wq->head = 0;
588 qp->r_rq.wq->tail = 0;
589 }
590 qp->r_sge.num_sge = 0;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700591 atomic_set(&qp->s_reserved_used, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800592}
593
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800594/**
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700595 * rvt_reset_qp - initialize the QP state to the reset state
596 * @qp: the QP to reset
597 * @type: the QP type
598 *
599 * r_lock, s_hlock, and s_lock are required to be held by the caller
600 */
601static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
602 enum ib_qp_type type)
603 __must_hold(&qp->s_lock)
604 __must_hold(&qp->s_hlock)
605 __must_hold(&qp->r_lock)
606{
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700607 lockdep_assert_held(&qp->r_lock);
608 lockdep_assert_held(&qp->s_hlock);
609 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700610 if (qp->state != IB_QPS_RESET) {
611 qp->state = IB_QPS_RESET;
612
613 /* Let drivers flush their waitlist */
614 rdi->driver_f.flush_qp_waiters(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800615 rvt_stop_rc_timers(qp);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700616 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
617 spin_unlock(&qp->s_lock);
618 spin_unlock(&qp->s_hlock);
619 spin_unlock_irq(&qp->r_lock);
620
621 /* Stop the send queue and the retry timer */
622 rdi->driver_f.stop_send_queue(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800623 rvt_del_timers_sync(qp);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700624 /* Wait for things to stop */
625 rdi->driver_f.quiesce_qp(qp);
626
627 /* take qp out the hash and wait for it to be unused */
628 rvt_remove_qp(rdi, qp);
629 wait_event(qp->wait, !atomic_read(&qp->refcount));
630
631 /* grab the lock b/c it was locked at call time */
632 spin_lock_irq(&qp->r_lock);
633 spin_lock(&qp->s_hlock);
634 spin_lock(&qp->s_lock);
635
636 rvt_clear_mr_refs(qp, 1);
637 /*
638 * Let the driver do any tear down or re-init it needs to for
639 * a qp that has been reset
640 */
641 rdi->driver_f.notify_qp_reset(qp);
642 }
643 rvt_init_qp(rdi, qp, type);
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700644 lockdep_assert_held(&qp->r_lock);
645 lockdep_assert_held(&qp->s_hlock);
646 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700647}
648
649/**
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800650 * rvt_create_qp - create a queue pair for a device
651 * @ibpd: the protection domain who's device we create the queue pair for
652 * @init_attr: the attributes of the queue pair
653 * @udata: user data for libibverbs.so
654 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800655 * Queue pair creation is mostly an rvt issue. However, drivers have their own
656 * unique idea of what queue pair numbers mean. For instance there is a reserved
657 * range for PSM.
658 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800659 * Return: the queue pair on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800660 *
661 * Called by the ib_create_qp() core verbs function.
662 */
663struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
664 struct ib_qp_init_attr *init_attr,
665 struct ib_udata *udata)
666{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800667 struct rvt_qp *qp;
668 int err;
669 struct rvt_swqe *swq = NULL;
670 size_t sz;
671 size_t sg_list_sz;
672 struct ib_qp *ret = ERR_PTR(-ENOMEM);
673 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
674 void *priv = NULL;
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800675 gfp_t gfp;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700676 size_t sqsize;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800677
678 if (!rdi)
679 return ERR_PTR(-EINVAL);
680
681 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
682 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800683 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800684 return ERR_PTR(-EINVAL);
685
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800686 /* GFP_NOIO is applicable to RC QP's only */
687
688 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
689 init_attr->qp_type != IB_QPT_RC)
690 return ERR_PTR(-EINVAL);
691
692 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
693 GFP_NOIO : GFP_KERNEL;
694
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800695 /* Check receive queue parameters if no SRQ is specified. */
696 if (!init_attr->srq) {
697 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
698 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
699 return ERR_PTR(-EINVAL);
700
701 if (init_attr->cap.max_send_sge +
702 init_attr->cap.max_send_wr +
703 init_attr->cap.max_recv_sge +
704 init_attr->cap.max_recv_wr == 0)
705 return ERR_PTR(-EINVAL);
706 }
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700707 sqsize =
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700708 init_attr->cap.max_send_wr + 1 +
709 rdi->dparms.reserved_operations;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800710 switch (init_attr->qp_type) {
711 case IB_QPT_SMI:
712 case IB_QPT_GSI:
713 if (init_attr->port_num == 0 ||
714 init_attr->port_num > ibpd->device->phys_port_cnt)
715 return ERR_PTR(-EINVAL);
716 case IB_QPT_UC:
717 case IB_QPT_RC:
718 case IB_QPT_UD:
719 sz = sizeof(struct rvt_sge) *
720 init_attr->cap.max_send_sge +
721 sizeof(struct rvt_swqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800722 if (gfp == GFP_NOIO)
723 swq = __vmalloc(
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700724 sqsize * sz,
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700725 gfp | __GFP_ZERO, PAGE_KERNEL);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800726 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700727 swq = vzalloc_node(
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700728 sqsize * sz,
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800729 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800730 if (!swq)
731 return ERR_PTR(-ENOMEM);
732
733 sz = sizeof(*qp);
734 sg_list_sz = 0;
735 if (init_attr->srq) {
736 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
737
738 if (srq->rq.max_sge > 1)
739 sg_list_sz = sizeof(*qp->r_sg_list) *
740 (srq->rq.max_sge - 1);
741 } else if (init_attr->cap.max_recv_sge > 1)
742 sg_list_sz = sizeof(*qp->r_sg_list) *
743 (init_attr->cap.max_recv_sge - 1);
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800744 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800745 if (!qp)
746 goto bail_swq;
747
748 RCU_INIT_POINTER(qp->next, NULL);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700749 if (init_attr->qp_type == IB_QPT_RC) {
750 qp->s_ack_queue =
751 kzalloc_node(
752 sizeof(*qp->s_ack_queue) *
753 rvt_max_atomic(rdi),
754 gfp,
755 rdi->dparms.node);
756 if (!qp->s_ack_queue)
757 goto bail_qp;
758 }
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800759 /* initialize timers needed for rc qp */
760 setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
761 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
762 HRTIMER_MODE_REL);
763 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800764
765 /*
766 * Driver needs to set up it's private QP structure and do any
767 * initialization that is needed.
768 */
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800769 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700770 if (IS_ERR(priv)) {
771 ret = priv;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800772 goto bail_qp;
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700773 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800774 qp->priv = priv;
775 qp->timeout_jiffies =
776 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
777 1000UL);
778 if (init_attr->srq) {
779 sz = 0;
780 } else {
781 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
782 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
783 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
784 sizeof(struct rvt_rwqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800785 if (udata)
786 qp->r_rq.wq = vmalloc_user(
787 sizeof(struct rvt_rwq) +
788 qp->r_rq.size * sz);
789 else if (gfp == GFP_NOIO)
790 qp->r_rq.wq = __vmalloc(
791 sizeof(struct rvt_rwq) +
792 qp->r_rq.size * sz,
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700793 gfp | __GFP_ZERO, PAGE_KERNEL);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800794 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700795 qp->r_rq.wq = vzalloc_node(
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800796 sizeof(struct rvt_rwq) +
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800797 qp->r_rq.size * sz,
798 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800799 if (!qp->r_rq.wq)
800 goto bail_driver_priv;
801 }
802
803 /*
804 * ib_create_qp() will initialize qp->ibqp
805 * except for qp->ibqp.qp_num.
806 */
807 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800808 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800809 spin_lock_init(&qp->s_lock);
810 spin_lock_init(&qp->r_rq.lock);
811 atomic_set(&qp->refcount, 0);
Jianxin Xiongd9f87232016-07-25 13:38:25 -0700812 atomic_set(&qp->local_ops_pending, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800813 init_waitqueue_head(&qp->wait);
814 init_timer(&qp->s_timer);
815 qp->s_timer.data = (unsigned long)qp;
816 INIT_LIST_HEAD(&qp->rspwait);
817 qp->state = IB_QPS_RESET;
818 qp->s_wq = swq;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700819 qp->s_size = sqsize;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800820 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800821 qp->s_max_sge = init_attr->cap.max_send_sge;
822 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
823 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
824
825 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
826 init_attr->qp_type,
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800827 init_attr->port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800828 if (err < 0) {
829 ret = ERR_PTR(err);
830 goto bail_rq_wq;
831 }
832 qp->ibqp.qp_num = err;
833 qp->port_num = init_attr->port_num;
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700834 rvt_init_qp(rdi, qp, init_attr->qp_type);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800835 break;
836
837 default:
838 /* Don't support raw QPs */
839 return ERR_PTR(-EINVAL);
840 }
841
842 init_attr->cap.max_inline_data = 0;
843
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800844 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800845 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800846 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800847 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800848 if (udata && udata->outlen >= sizeof(__u64)) {
849 if (!qp->r_rq.wq) {
850 __u64 offset = 0;
851
852 err = ib_copy_to_udata(udata, &offset,
853 sizeof(offset));
854 if (err) {
855 ret = ERR_PTR(err);
856 goto bail_qpn;
857 }
858 } else {
859 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
860
861 qp->ip = rvt_create_mmap_info(rdi, s,
862 ibpd->uobject->context,
863 qp->r_rq.wq);
864 if (!qp->ip) {
865 ret = ERR_PTR(-ENOMEM);
866 goto bail_qpn;
867 }
868
869 err = ib_copy_to_udata(udata, &qp->ip->offset,
870 sizeof(qp->ip->offset));
871 if (err) {
872 ret = ERR_PTR(err);
873 goto bail_ip;
874 }
875 }
Mike Marciniszynef086c02016-03-07 11:35:08 -0800876 qp->pid = current->pid;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800877 }
878
879 spin_lock(&rdi->n_qps_lock);
880 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
881 spin_unlock(&rdi->n_qps_lock);
882 ret = ERR_PTR(-ENOMEM);
883 goto bail_ip;
884 }
885
886 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800887 /*
888 * Maintain a busy_jiffies variable that will be added to the timeout
889 * period in mod_retry_timer and add_retry_timer. This busy jiffies
890 * is scaled by the number of rc qps created for the device to reduce
891 * the number of timeouts occurring when there is a large number of
892 * qps. busy_jiffies is incremented every rc qp scaling interval.
893 * The scaling interval is selected based on extensive performance
894 * evaluation of targeted workloads.
895 */
896 if (init_attr->qp_type == IB_QPT_RC) {
897 rdi->n_rc_qps++;
898 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
899 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800900 spin_unlock(&rdi->n_qps_lock);
901
902 if (qp->ip) {
903 spin_lock_irq(&rdi->pending_lock);
904 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
905 spin_unlock_irq(&rdi->pending_lock);
906 }
907
908 ret = &qp->ibqp;
909
910 /*
911 * We have our QP and its good, now keep track of what types of opcodes
912 * can be processed on this QP. We do this by keeping track of what the
913 * 3 high order bits of the opcode are.
914 */
915 switch (init_attr->qp_type) {
916 case IB_QPT_SMI:
917 case IB_QPT_GSI:
918 case IB_QPT_UD:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700919 qp->allowed_ops = IB_OPCODE_UD;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800920 break;
921 case IB_QPT_RC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700922 qp->allowed_ops = IB_OPCODE_RC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800923 break;
924 case IB_QPT_UC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700925 qp->allowed_ops = IB_OPCODE_UC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800926 break;
927 default:
928 ret = ERR_PTR(-EINVAL);
929 goto bail_ip;
930 }
931
932 return ret;
933
934bail_ip:
Jim Foraker22dccc52016-11-01 13:44:12 -0700935 if (qp->ip)
936 kref_put(&qp->ip->ref, rvt_release_mmap_info);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800937
938bail_qpn:
939 free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
940
941bail_rq_wq:
Mike Marciniszyn56c8ca52016-08-16 13:26:29 -0700942 if (!qp->ip)
943 vfree(qp->r_rq.wq);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800944
945bail_driver_priv:
946 rdi->driver_f.qp_priv_free(rdi, qp);
947
948bail_qp:
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700949 kfree(qp->s_ack_queue);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800950 kfree(qp);
951
952bail_swq:
953 vfree(swq);
954
955 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800956}
957
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800958/**
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800959 * rvt_error_qp - put a QP into the error state
960 * @qp: the QP to put into the error state
961 * @err: the receive completion error to signal if a RWQE is active
962 *
963 * Flushes both send and receive work queues.
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800964 *
965 * Return: true if last WQE event should be generated.
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800966 * The QP r_lock and s_lock should be held and interrupts disabled.
967 * If we are already in error state, just return.
968 */
969int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
970{
971 struct ib_wc wc;
972 int ret = 0;
973 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
974
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700975 lockdep_assert_held(&qp->r_lock);
976 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800977 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
978 goto bail;
979
980 qp->state = IB_QPS_ERR;
981
982 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
983 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
984 del_timer(&qp->s_timer);
985 }
986
987 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
988 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
989
990 rdi->driver_f.notify_error_qp(qp);
991
992 /* Schedule the sending tasklet to drain the send work queue. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800993 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800994 rdi->driver_f.schedule_send(qp);
995
996 rvt_clear_mr_refs(qp, 0);
997
998 memset(&wc, 0, sizeof(wc));
999 wc.qp = &qp->ibqp;
1000 wc.opcode = IB_WC_RECV;
1001
1002 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1003 wc.wr_id = qp->r_wr_id;
1004 wc.status = err;
1005 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1006 }
1007 wc.status = IB_WC_WR_FLUSH_ERR;
1008
1009 if (qp->r_rq.wq) {
1010 struct rvt_rwq *wq;
1011 u32 head;
1012 u32 tail;
1013
1014 spin_lock(&qp->r_rq.lock);
1015
1016 /* sanity check pointers before trusting them */
1017 wq = qp->r_rq.wq;
1018 head = wq->head;
1019 if (head >= qp->r_rq.size)
1020 head = 0;
1021 tail = wq->tail;
1022 if (tail >= qp->r_rq.size)
1023 tail = 0;
1024 while (tail != head) {
1025 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1026 if (++tail >= qp->r_rq.size)
1027 tail = 0;
1028 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1029 }
1030 wq->tail = tail;
1031
1032 spin_unlock(&qp->r_rq.lock);
1033 } else if (qp->ibqp.event_handler) {
1034 ret = 1;
1035 }
1036
1037bail:
1038 return ret;
1039}
1040EXPORT_SYMBOL(rvt_error_qp);
1041
1042/*
1043 * Put the QP into the hash table.
1044 * The hash table holds a reference to the QP.
1045 */
1046static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1047{
1048 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1049 unsigned long flags;
1050
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001051 rvt_get_qp(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001052 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1053
1054 if (qp->ibqp.qp_num <= 1) {
1055 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1056 } else {
1057 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1058
1059 qp->next = rdi->qp_dev->qp_table[n];
1060 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1061 trace_rvt_qpinsert(qp, n);
1062 }
1063
1064 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1065}
1066
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001067/**
Parav Pandit61347fa2016-09-13 19:40:50 +05301068 * rvt_modify_qp - modify the attributes of a queue pair
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001069 * @ibqp: the queue pair who's attributes we're modifying
1070 * @attr: the new attributes
1071 * @attr_mask: the mask of attributes to modify
1072 * @udata: user data for libibverbs.so
1073 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001074 * Return: 0 on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001075 */
1076int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1077 int attr_mask, struct ib_udata *udata)
1078{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001079 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1080 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1081 enum ib_qp_state cur_state, new_state;
1082 struct ib_event ev;
1083 int lastwqe = 0;
1084 int mig = 0;
1085 int pmtu = 0; /* for gcc warning only */
1086 enum rdma_link_layer link;
1087
1088 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1089
1090 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001091 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001092 spin_lock(&qp->s_lock);
1093
1094 cur_state = attr_mask & IB_QP_CUR_STATE ?
1095 attr->cur_qp_state : qp->state;
1096 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1097
1098 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1099 attr_mask, link))
1100 goto inval;
1101
Ira Weinye85ec332016-01-22 13:04:38 -08001102 if (rdi->driver_f.check_modify_qp &&
1103 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1104 goto inval;
1105
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001106 if (attr_mask & IB_QP_AV) {
1107 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1108 goto inval;
1109 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1110 goto inval;
1111 }
1112
1113 if (attr_mask & IB_QP_ALT_PATH) {
1114 if (attr->alt_ah_attr.dlid >=
1115 be16_to_cpu(IB_MULTICAST_LID_BASE))
1116 goto inval;
1117 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1118 goto inval;
1119 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1120 goto inval;
1121 }
1122
1123 if (attr_mask & IB_QP_PKEY_INDEX)
1124 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1125 goto inval;
1126
1127 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1128 if (attr->min_rnr_timer > 31)
1129 goto inval;
1130
1131 if (attr_mask & IB_QP_PORT)
1132 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1133 qp->ibqp.qp_type == IB_QPT_GSI ||
1134 attr->port_num == 0 ||
1135 attr->port_num > ibqp->device->phys_port_cnt)
1136 goto inval;
1137
1138 if (attr_mask & IB_QP_DEST_QPN)
1139 if (attr->dest_qp_num > RVT_QPN_MASK)
1140 goto inval;
1141
1142 if (attr_mask & IB_QP_RETRY_CNT)
1143 if (attr->retry_cnt > 7)
1144 goto inval;
1145
1146 if (attr_mask & IB_QP_RNR_RETRY)
1147 if (attr->rnr_retry > 7)
1148 goto inval;
1149
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001150 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001151 * Don't allow invalid path_mtu values. OK to set greater
1152 * than the active mtu (or even the max_cap, if we have tuned
1153 * that to a small mtu. We'll set qp->path_mtu
1154 * to the lesser of requested attribute mtu and active,
1155 * for packetizing messages.
1156 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001157 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001158 if (attr_mask & IB_QP_PATH_MTU) {
1159 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1160 if (pmtu < 0)
1161 goto inval;
1162 }
1163
1164 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1165 if (attr->path_mig_state == IB_MIG_REARM) {
1166 if (qp->s_mig_state == IB_MIG_ARMED)
1167 goto inval;
1168 if (new_state != IB_QPS_RTS)
1169 goto inval;
1170 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1171 if (qp->s_mig_state == IB_MIG_REARM)
1172 goto inval;
1173 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1174 goto inval;
1175 if (qp->s_mig_state == IB_MIG_ARMED)
1176 mig = 1;
1177 } else {
1178 goto inval;
1179 }
1180 }
1181
1182 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1183 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1184 goto inval;
1185
1186 switch (new_state) {
1187 case IB_QPS_RESET:
1188 if (qp->state != IB_QPS_RESET)
1189 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1190 break;
1191
1192 case IB_QPS_RTR:
1193 /* Allow event to re-trigger if QP set to RTR more than once */
1194 qp->r_flags &= ~RVT_R_COMM_EST;
1195 qp->state = new_state;
1196 break;
1197
1198 case IB_QPS_SQD:
1199 qp->s_draining = qp->s_last != qp->s_cur;
1200 qp->state = new_state;
1201 break;
1202
1203 case IB_QPS_SQE:
1204 if (qp->ibqp.qp_type == IB_QPT_RC)
1205 goto inval;
1206 qp->state = new_state;
1207 break;
1208
1209 case IB_QPS_ERR:
1210 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1211 break;
1212
1213 default:
1214 qp->state = new_state;
1215 break;
1216 }
1217
1218 if (attr_mask & IB_QP_PKEY_INDEX)
1219 qp->s_pkey_index = attr->pkey_index;
1220
1221 if (attr_mask & IB_QP_PORT)
1222 qp->port_num = attr->port_num;
1223
1224 if (attr_mask & IB_QP_DEST_QPN)
1225 qp->remote_qpn = attr->dest_qp_num;
1226
1227 if (attr_mask & IB_QP_SQ_PSN) {
1228 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1229 qp->s_psn = qp->s_next_psn;
1230 qp->s_sending_psn = qp->s_next_psn;
1231 qp->s_last_psn = qp->s_next_psn - 1;
1232 qp->s_sending_hpsn = qp->s_last_psn;
1233 }
1234
1235 if (attr_mask & IB_QP_RQ_PSN)
1236 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1237
1238 if (attr_mask & IB_QP_ACCESS_FLAGS)
1239 qp->qp_access_flags = attr->qp_access_flags;
1240
1241 if (attr_mask & IB_QP_AV) {
1242 qp->remote_ah_attr = attr->ah_attr;
1243 qp->s_srate = attr->ah_attr.static_rate;
1244 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1245 }
1246
1247 if (attr_mask & IB_QP_ALT_PATH) {
1248 qp->alt_ah_attr = attr->alt_ah_attr;
1249 qp->s_alt_pkey_index = attr->alt_pkey_index;
1250 }
1251
1252 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1253 qp->s_mig_state = attr->path_mig_state;
1254 if (mig) {
1255 qp->remote_ah_attr = qp->alt_ah_attr;
1256 qp->port_num = qp->alt_ah_attr.port_num;
1257 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001258 }
1259 }
1260
1261 if (attr_mask & IB_QP_PATH_MTU) {
1262 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1263 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001264 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001265 }
1266
1267 if (attr_mask & IB_QP_RETRY_CNT) {
1268 qp->s_retry_cnt = attr->retry_cnt;
1269 qp->s_retry = attr->retry_cnt;
1270 }
1271
1272 if (attr_mask & IB_QP_RNR_RETRY) {
1273 qp->s_rnr_retry_cnt = attr->rnr_retry;
1274 qp->s_rnr_retry = attr->rnr_retry;
1275 }
1276
1277 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1278 qp->r_min_rnr_timer = attr->min_rnr_timer;
1279
1280 if (attr_mask & IB_QP_TIMEOUT) {
1281 qp->timeout = attr->timeout;
1282 qp->timeout_jiffies =
1283 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1284 1000UL);
1285 }
1286
1287 if (attr_mask & IB_QP_QKEY)
1288 qp->qkey = attr->qkey;
1289
1290 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1291 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1292
1293 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1294 qp->s_max_rd_atomic = attr->max_rd_atomic;
1295
Ira Weinye85ec332016-01-22 13:04:38 -08001296 if (rdi->driver_f.modify_qp)
1297 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1298
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001299 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001300 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001301 spin_unlock_irq(&qp->r_lock);
1302
1303 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1304 rvt_insert_qp(rdi, qp);
1305
1306 if (lastwqe) {
1307 ev.device = qp->ibqp.device;
1308 ev.element.qp = &qp->ibqp;
1309 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1310 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1311 }
1312 if (mig) {
1313 ev.device = qp->ibqp.device;
1314 ev.element.qp = &qp->ibqp;
1315 ev.event = IB_EVENT_PATH_MIG;
1316 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1317 }
1318 return 0;
1319
1320inval:
1321 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001322 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001323 spin_unlock_irq(&qp->r_lock);
1324 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001325}
1326
Dennis Dalessandro79a225b2016-02-14 12:11:20 -08001327/** rvt_free_qpn - Free a qpn from the bit map
1328 * @qpt: QP table
1329 * @qpn: queue pair number to free
1330 */
1331static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1332{
1333 struct rvt_qpn_map *map;
1334
1335 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1336 if (map->page)
1337 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1338}
1339
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001340/**
1341 * rvt_destroy_qp - destroy a queue pair
1342 * @ibqp: the queue pair to destroy
1343 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001344 * Note that this can be called while the QP is actively sending or
1345 * receiving!
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001346 *
1347 * Return: 0 on success.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001348 */
1349int rvt_destroy_qp(struct ib_qp *ibqp)
1350{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001351 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1352 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001353
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001354 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001355 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001356 spin_lock(&qp->s_lock);
1357 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1358 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001359 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001360 spin_unlock_irq(&qp->r_lock);
1361
1362 /* qpn is now available for use again */
1363 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1364
1365 spin_lock(&rdi->n_qps_lock);
1366 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001367 if (qp->ibqp.qp_type == IB_QPT_RC) {
1368 rdi->n_rc_qps--;
1369 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1370 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001371 spin_unlock(&rdi->n_qps_lock);
1372
1373 if (qp->ip)
1374 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1375 else
1376 vfree(qp->r_rq.wq);
1377 vfree(qp->s_wq);
1378 rdi->driver_f.qp_priv_free(rdi, qp);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001379 kfree(qp->s_ack_queue);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001380 kfree(qp);
1381 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001382}
1383
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001384/**
1385 * rvt_query_qp - query an ipbq
1386 * @ibqp: IB qp to query
1387 * @attr: attr struct to fill in
1388 * @attr_mask: attr mask ignored
1389 * @init_attr: struct to fill in
1390 *
1391 * Return: always 0
1392 */
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001393int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1394 int attr_mask, struct ib_qp_init_attr *init_attr)
1395{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001396 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1397 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1398
1399 attr->qp_state = qp->state;
1400 attr->cur_qp_state = attr->qp_state;
1401 attr->path_mtu = qp->path_mtu;
1402 attr->path_mig_state = qp->s_mig_state;
1403 attr->qkey = qp->qkey;
1404 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1405 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1406 attr->dest_qp_num = qp->remote_qpn;
1407 attr->qp_access_flags = qp->qp_access_flags;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001408 attr->cap.max_send_wr = qp->s_size - 1 -
1409 rdi->dparms.reserved_operations;
Harish Chegondi74d2d502016-01-22 13:05:04 -08001410 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1411 attr->cap.max_send_sge = qp->s_max_sge;
1412 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1413 attr->cap.max_inline_data = 0;
1414 attr->ah_attr = qp->remote_ah_attr;
1415 attr->alt_ah_attr = qp->alt_ah_attr;
1416 attr->pkey_index = qp->s_pkey_index;
1417 attr->alt_pkey_index = qp->s_alt_pkey_index;
1418 attr->en_sqd_async_notify = 0;
1419 attr->sq_draining = qp->s_draining;
1420 attr->max_rd_atomic = qp->s_max_rd_atomic;
1421 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1422 attr->min_rnr_timer = qp->r_min_rnr_timer;
1423 attr->port_num = qp->port_num;
1424 attr->timeout = qp->timeout;
1425 attr->retry_cnt = qp->s_retry_cnt;
1426 attr->rnr_retry = qp->s_rnr_retry_cnt;
1427 attr->alt_port_num = qp->alt_ah_attr.port_num;
1428 attr->alt_timeout = qp->alt_timeout;
1429
1430 init_attr->event_handler = qp->ibqp.event_handler;
1431 init_attr->qp_context = qp->ibqp.qp_context;
1432 init_attr->send_cq = qp->ibqp.send_cq;
1433 init_attr->recv_cq = qp->ibqp.recv_cq;
1434 init_attr->srq = qp->ibqp.srq;
1435 init_attr->cap = attr->cap;
1436 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1437 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1438 else
1439 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1440 init_attr->qp_type = qp->ibqp.qp_type;
1441 init_attr->port_num = qp->port_num;
1442 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001443}
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001444
1445/**
1446 * rvt_post_receive - post a receive on a QP
1447 * @ibqp: the QP to post the receive on
1448 * @wr: the WR to post
1449 * @bad_wr: the first bad WR is put here
1450 *
1451 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001452 *
1453 * Return: 0 on success otherwise errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001454 */
1455int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1456 struct ib_recv_wr **bad_wr)
1457{
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001458 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1459 struct rvt_rwq *wq = qp->r_rq.wq;
1460 unsigned long flags;
Alex Estrin000a8302016-03-07 11:35:51 -08001461 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1462 !qp->ibqp.srq;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001463
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001464 /* Check that state is OK to post receive. */
1465 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1466 *bad_wr = wr;
1467 return -EINVAL;
1468 }
1469
1470 for (; wr; wr = wr->next) {
1471 struct rvt_rwqe *wqe;
1472 u32 next;
1473 int i;
1474
1475 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1476 *bad_wr = wr;
1477 return -EINVAL;
1478 }
1479
1480 spin_lock_irqsave(&qp->r_rq.lock, flags);
1481 next = wq->head + 1;
1482 if (next >= qp->r_rq.size)
1483 next = 0;
1484 if (next == wq->tail) {
1485 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1486 *bad_wr = wr;
1487 return -ENOMEM;
1488 }
Alex Estrin000a8302016-03-07 11:35:51 -08001489 if (unlikely(qp_err_flush)) {
1490 struct ib_wc wc;
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001491
Alex Estrin000a8302016-03-07 11:35:51 -08001492 memset(&wc, 0, sizeof(wc));
1493 wc.qp = &qp->ibqp;
1494 wc.opcode = IB_WC_RECV;
1495 wc.wr_id = wr->wr_id;
1496 wc.status = IB_WC_WR_FLUSH_ERR;
1497 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1498 } else {
1499 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1500 wqe->wr_id = wr->wr_id;
1501 wqe->num_sge = wr->num_sge;
1502 for (i = 0; i < wr->num_sge; i++)
1503 wqe->sg_list[i] = wr->sg_list[i];
1504 /*
1505 * Make sure queue entry is written
1506 * before the head index.
1507 */
1508 smp_wmb();
1509 wq->head = next;
1510 }
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001511 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1512 }
1513 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001514}
1515
1516/**
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001517 * rvt_qp_valid_operation - validate post send wr request
1518 * @qp - the qp
1519 * @post-parms - the post send table for the driver
1520 * @wr - the work request
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001521 *
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001522 * The routine validates the operation based on the
1523 * validation table an returns the length of the operation
1524 * which can extend beyond the ib_send_bw. Operation
1525 * dependent flags key atomic operation validation.
1526 *
1527 * There is an exception for UD qps that validates the pd and
1528 * overrides the length to include the additional UD specific
1529 * length.
1530 *
1531 * Returns a negative error or the length of the work request
1532 * for building the swqe.
1533 */
1534static inline int rvt_qp_valid_operation(
1535 struct rvt_qp *qp,
1536 const struct rvt_operation_params *post_parms,
1537 struct ib_send_wr *wr)
1538{
1539 int len;
1540
1541 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1542 return -EINVAL;
1543 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1544 return -EINVAL;
1545 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1546 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1547 return -EINVAL;
1548 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1549 (wr->num_sge == 0 ||
1550 wr->sg_list[0].length < sizeof(u64) ||
1551 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1552 return -EINVAL;
1553 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1554 !qp->s_max_rd_atomic)
1555 return -EINVAL;
1556 len = post_parms[wr->opcode].length;
1557 /* UD specific */
1558 if (qp->ibqp.qp_type != IB_QPT_UC &&
1559 qp->ibqp.qp_type != IB_QPT_RC) {
1560 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1561 return -EINVAL;
1562 len = sizeof(struct ib_ud_wr);
1563 }
1564 return len;
1565}
1566
1567/**
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001568 * rvt_qp_is_avail - determine queue capacity
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001569 * @qp - the qp
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001570 * @rdi - the rdmavt device
1571 * @reserved_op - is reserved operation
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001572 *
1573 * This assumes the s_hlock is held but the s_last
1574 * qp variable is uncontrolled.
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001575 *
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001576 * For non reserved operations, the qp->s_avail
1577 * may be changed.
1578 *
1579 * The return value is zero or a -ENOMEM.
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001580 */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001581static inline int rvt_qp_is_avail(
1582 struct rvt_qp *qp,
1583 struct rvt_dev_info *rdi,
1584 bool reserved_op)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001585{
1586 u32 slast;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001587 u32 avail;
1588 u32 reserved_used;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001589
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001590 /* see rvt_qp_wqe_unreserve() */
1591 smp_mb__before_atomic();
1592 reserved_used = atomic_read(&qp->s_reserved_used);
1593 if (unlikely(reserved_op)) {
1594 /* see rvt_qp_wqe_unreserve() */
1595 smp_mb__before_atomic();
1596 if (reserved_used >= rdi->dparms.reserved_operations)
1597 return -ENOMEM;
1598 return 0;
1599 }
1600 /* non-reserved operations */
1601 if (likely(qp->s_avail))
1602 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001603 smp_read_barrier_depends(); /* see rc.c */
1604 slast = ACCESS_ONCE(qp->s_last);
1605 if (qp->s_head >= slast)
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001606 avail = qp->s_size - (qp->s_head - slast);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001607 else
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001608 avail = slast - qp->s_head;
1609
1610 /* see rvt_qp_wqe_unreserve() */
1611 smp_mb__before_atomic();
1612 reserved_used = atomic_read(&qp->s_reserved_used);
1613 avail = avail - 1 -
1614 (rdi->dparms.reserved_operations - reserved_used);
1615 /* insure we don't assign a negative s_avail */
1616 if ((s32)avail <= 0)
1617 return -ENOMEM;
1618 qp->s_avail = avail;
1619 if (WARN_ON(qp->s_avail >
1620 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1621 rvt_pr_err(rdi,
1622 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1623 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1624 qp->s_head, qp->s_tail, qp->s_cur,
1625 qp->s_acked, qp->s_last);
1626 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001627}
1628
1629/**
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001630 * rvt_post_one_wr - post one RC, UC, or UD send work request
1631 * @qp: the QP to post on
1632 * @wr: the work request to send
1633 */
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001634static int rvt_post_one_wr(struct rvt_qp *qp,
1635 struct ib_send_wr *wr,
1636 int *call_send)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001637{
1638 struct rvt_swqe *wqe;
1639 u32 next;
1640 int i;
1641 int j;
1642 int acc;
1643 struct rvt_lkey_table *rkt;
1644 struct rvt_pd *pd;
1645 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001646 u8 log_pmtu;
1647 int ret;
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001648 size_t cplen;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001649 bool reserved_op;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001650 int local_ops_delayed = 0;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001651
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001652 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1653
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001654 /* IB spec says that num_sge == 0 is OK. */
1655 if (unlikely(wr->num_sge > qp->s_max_sge))
1656 return -EINVAL;
1657
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001658 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1659 if (ret < 0)
1660 return ret;
1661 cplen = ret;
1662
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001663 /*
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001664 * Local operations include fast register and local invalidate.
1665 * Fast register needs to be processed immediately because the
1666 * registered lkey may be used by following work requests and the
1667 * lkey needs to be valid at the time those requests are posted.
1668 * Local invalidate can be processed immediately if fencing is
1669 * not required and no previous local invalidate ops are pending.
1670 * Signaled local operations that have been processed immediately
1671 * need to have requests with "completion only" flags set posted
1672 * to the send queue in order to generate completions.
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001673 */
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001674 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001675 switch (wr->opcode) {
1676 case IB_WR_REG_MR:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001677 ret = rvt_fast_reg_mr(qp,
1678 reg_wr(wr)->mr,
1679 reg_wr(wr)->key,
1680 reg_wr(wr)->access);
1681 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1682 return ret;
1683 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001684 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001685 if ((wr->send_flags & IB_SEND_FENCE) ||
1686 atomic_read(&qp->local_ops_pending)) {
1687 local_ops_delayed = 1;
1688 } else {
1689 ret = rvt_invalidate_rkey(
1690 qp, wr->ex.invalidate_rkey);
1691 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1692 return ret;
1693 }
1694 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001695 default:
1696 return -EINVAL;
1697 }
1698 }
1699
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001700 reserved_op = rdi->post_parms[wr->opcode].flags &
1701 RVT_OPERATION_USE_RESERVE;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001702 /* check for avail */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001703 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1704 if (ret)
1705 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001706 next = qp->s_head + 1;
1707 if (next >= qp->s_size)
1708 next = 0;
Ira Weiny60c30f52016-02-03 14:14:45 -08001709
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001710 rkt = &rdi->lkey_table;
1711 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1712 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1713
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001714 /* cplen has length from above */
1715 memcpy(&wqe->wr, wr, cplen);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001716
1717 wqe->length = 0;
1718 j = 0;
1719 if (wr->num_sge) {
1720 acc = wr->opcode >= IB_WR_RDMA_READ ?
1721 IB_ACCESS_LOCAL_WRITE : 0;
1722 for (i = 0; i < wr->num_sge; i++) {
1723 u32 length = wr->sg_list[i].length;
1724 int ok;
1725
1726 if (length == 0)
1727 continue;
1728 ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1729 &wr->sg_list[i], acc);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001730 if (!ok) {
1731 ret = -EINVAL;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001732 goto bail_inval_free;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001733 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001734 wqe->length += length;
1735 j++;
1736 }
1737 wqe->wr.num_sge = j;
1738 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001739
1740 /* general part of wqe valid - allow for driver checks */
1741 if (rdi->driver_f.check_send_wqe) {
1742 ret = rdi->driver_f.check_send_wqe(qp, wqe);
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001743 if (ret < 0)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001744 goto bail_inval_free;
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001745 if (ret)
1746 *call_send = ret;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001747 }
1748
1749 log_pmtu = qp->log_pmtu;
1750 if (qp->ibqp.qp_type != IB_QPT_UC &&
1751 qp->ibqp.qp_type != IB_QPT_RC) {
1752 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1753
1754 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001755 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1756 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001757
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001758 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001759 if (local_ops_delayed)
1760 atomic_inc(&qp->local_ops_pending);
1761 else
1762 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001763 wqe->ssn = 0;
1764 wqe->psn = 0;
1765 wqe->lpsn = 0;
1766 } else {
1767 wqe->ssn = qp->s_ssn++;
1768 wqe->psn = qp->s_next_psn;
1769 wqe->lpsn = wqe->psn +
1770 (wqe->length ?
1771 ((wqe->length - 1) >> log_pmtu) :
1772 0);
1773 qp->s_next_psn = wqe->lpsn + 1;
1774 }
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001775 if (unlikely(reserved_op))
1776 rvt_qp_wqe_reserve(qp, wqe);
1777 else
1778 qp->s_avail--;
Mike Marciniszyn2a1b7c82017-03-20 17:25:10 -07001779 trace_rvt_post_one_wr(qp, wqe);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001780 smp_wmb(); /* see request builders */
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001781 qp->s_head = next;
1782
1783 return 0;
1784
1785bail_inval_free:
1786 /* release mr holds */
1787 while (j) {
1788 struct rvt_sge *sge = &wqe->sg_list[--j];
1789
1790 rvt_put_mr(sge->mr);
1791 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001792 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001793}
1794
1795/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001796 * rvt_post_send - post a send on a QP
1797 * @ibqp: the QP to post the send on
1798 * @wr: the list of work requests to post
1799 * @bad_wr: the first bad WR is put here
1800 *
1801 * This may be called from interrupt context.
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_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1806 struct ib_send_wr **bad_wr)
1807{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001808 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1809 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1810 unsigned long flags = 0;
1811 int call_send;
1812 unsigned nreq = 0;
1813 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001814
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001815 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001816
1817 /*
1818 * Ensure QP state is such that we can send. If not bail out early,
1819 * there is no need to do this every time we post a send.
1820 */
1821 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001822 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001823 return -EINVAL;
1824 }
1825
1826 /*
1827 * If the send queue is empty, and we only have a single WR then just go
1828 * ahead and kick the send engine into gear. Otherwise we will always
1829 * just schedule the send to happen later.
1830 */
1831 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1832
1833 for (; wr; wr = wr->next) {
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001834 err = rvt_post_one_wr(qp, wr, &call_send);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001835 if (unlikely(err)) {
1836 *bad_wr = wr;
1837 goto bail;
1838 }
1839 nreq++;
1840 }
1841bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001842 spin_unlock_irqrestore(&qp->s_hlock, flags);
1843 if (nreq) {
1844 if (call_send)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001845 rdi->driver_f.do_send(qp);
Jubin Johne6d2e012016-04-12 10:47:00 -07001846 else
1847 rdi->driver_f.schedule_send_no_lock(qp);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001848 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001849 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001850}
1851
1852/**
1853 * rvt_post_srq_receive - post a receive on a shared receive queue
1854 * @ibsrq: the SRQ to post the receive on
1855 * @wr: the list of work requests to post
1856 * @bad_wr: A pointer to the first WR to cause a problem is put here
1857 *
1858 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001859 *
1860 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001861 */
1862int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1863 struct ib_recv_wr **bad_wr)
1864{
Jubin Johnb8f881b2016-02-03 14:14:36 -08001865 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1866 struct rvt_rwq *wq;
1867 unsigned long flags;
1868
1869 for (; wr; wr = wr->next) {
1870 struct rvt_rwqe *wqe;
1871 u32 next;
1872 int i;
1873
1874 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1875 *bad_wr = wr;
1876 return -EINVAL;
1877 }
1878
1879 spin_lock_irqsave(&srq->rq.lock, flags);
1880 wq = srq->rq.wq;
1881 next = wq->head + 1;
1882 if (next >= srq->rq.size)
1883 next = 0;
1884 if (next == wq->tail) {
1885 spin_unlock_irqrestore(&srq->rq.lock, flags);
1886 *bad_wr = wr;
1887 return -ENOMEM;
1888 }
1889
1890 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1891 wqe->wr_id = wr->wr_id;
1892 wqe->num_sge = wr->num_sge;
1893 for (i = 0; i < wr->num_sge; i++)
1894 wqe->sg_list[i] = wr->sg_list[i];
1895 /* Make sure queue entry is written before the head index. */
1896 smp_wmb();
1897 wq->head = next;
1898 spin_unlock_irqrestore(&srq->rq.lock, flags);
1899 }
1900 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001901}
Brian Weltybeb5a042017-02-08 05:27:01 -08001902
1903/**
1904 * qp_comm_est - handle trap with QP established
1905 * @qp: the QP
1906 */
1907void rvt_comm_est(struct rvt_qp *qp)
1908{
1909 qp->r_flags |= RVT_R_COMM_EST;
1910 if (qp->ibqp.event_handler) {
1911 struct ib_event ev;
1912
1913 ev.device = qp->ibqp.device;
1914 ev.element.qp = &qp->ibqp;
1915 ev.event = IB_EVENT_COMM_EST;
1916 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1917 }
1918}
1919EXPORT_SYMBOL(rvt_comm_est);
1920
1921void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
1922{
1923 unsigned long flags;
1924 int lastwqe;
1925
1926 spin_lock_irqsave(&qp->s_lock, flags);
1927 lastwqe = rvt_error_qp(qp, err);
1928 spin_unlock_irqrestore(&qp->s_lock, flags);
1929
1930 if (lastwqe) {
1931 struct ib_event ev;
1932
1933 ev.device = qp->ibqp.device;
1934 ev.element.qp = &qp->ibqp;
1935 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1936 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1937 }
1938}
1939EXPORT_SYMBOL(rvt_rc_error);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001940
Don Hiatt881fccb2017-02-08 05:28:19 -08001941/*
1942 * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
1943 * @index - the index
1944 * return usec from an index into ib_rvt_rnr_table
1945 */
1946unsigned long rvt_rnr_tbl_to_usec(u32 index)
1947{
Don Hiatt832666c2017-02-08 05:28:25 -08001948 return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
Don Hiatt881fccb2017-02-08 05:28:19 -08001949}
1950EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
1951
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001952static inline unsigned long rvt_aeth_to_usec(u32 aeth)
1953{
Don Hiatt832666c2017-02-08 05:28:25 -08001954 return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
1955 IB_AETH_CREDIT_MASK];
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001956}
1957
1958/*
1959 * rvt_add_retry_timer - add/start a retry timer
1960 * @qp - the QP
1961 * add a retry timer on the QP
1962 */
1963void rvt_add_retry_timer(struct rvt_qp *qp)
1964{
1965 struct ib_qp *ibqp = &qp->ibqp;
1966 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1967
1968 lockdep_assert_held(&qp->s_lock);
1969 qp->s_flags |= RVT_S_TIMER;
1970 /* 4.096 usec. * (1 << qp->timeout) */
1971 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
1972 rdi->busy_jiffies;
1973 add_timer(&qp->s_timer);
1974}
1975EXPORT_SYMBOL(rvt_add_retry_timer);
1976
1977/**
1978 * rvt_add_rnr_timer - add/start an rnr timer
1979 * @qp - the QP
1980 * @aeth - aeth of RNR timeout, simulated aeth for loopback
1981 * add an rnr timer on the QP
1982 */
1983void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
1984{
1985 u32 to;
1986
1987 lockdep_assert_held(&qp->s_lock);
1988 qp->s_flags |= RVT_S_WAIT_RNR;
1989 to = rvt_aeth_to_usec(aeth);
1990 hrtimer_start(&qp->s_rnr_timer,
1991 ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
1992}
1993EXPORT_SYMBOL(rvt_add_rnr_timer);
1994
1995/**
1996 * rvt_stop_rc_timers - stop all timers
1997 * @qp - the QP
1998 * stop any pending timers
1999 */
2000void rvt_stop_rc_timers(struct rvt_qp *qp)
2001{
2002 lockdep_assert_held(&qp->s_lock);
2003 /* Remove QP from all timers */
2004 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2005 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2006 del_timer(&qp->s_timer);
2007 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2008 }
2009}
2010EXPORT_SYMBOL(rvt_stop_rc_timers);
2011
2012/**
2013 * rvt_stop_rnr_timer - stop an rnr timer
2014 * @qp - the QP
2015 *
2016 * stop an rnr timer and return if the timer
2017 * had been pending.
2018 */
2019static int rvt_stop_rnr_timer(struct rvt_qp *qp)
2020{
2021 int rval = 0;
2022
2023 lockdep_assert_held(&qp->s_lock);
2024 /* Remove QP from rnr timer */
2025 if (qp->s_flags & RVT_S_WAIT_RNR) {
2026 qp->s_flags &= ~RVT_S_WAIT_RNR;
2027 rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
2028 }
2029 return rval;
2030}
2031
2032/**
2033 * rvt_del_timers_sync - wait for any timeout routines to exit
2034 * @qp - the QP
2035 */
2036void rvt_del_timers_sync(struct rvt_qp *qp)
2037{
2038 del_timer_sync(&qp->s_timer);
2039 hrtimer_cancel(&qp->s_rnr_timer);
2040}
2041EXPORT_SYMBOL(rvt_del_timers_sync);
2042
2043/**
2044 * This is called from s_timer for missing responses.
2045 */
2046static void rvt_rc_timeout(unsigned long arg)
2047{
2048 struct rvt_qp *qp = (struct rvt_qp *)arg;
2049 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2050 unsigned long flags;
2051
2052 spin_lock_irqsave(&qp->r_lock, flags);
2053 spin_lock(&qp->s_lock);
2054 if (qp->s_flags & RVT_S_TIMER) {
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002055 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
2056
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002057 qp->s_flags &= ~RVT_S_TIMER;
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002058 rvp->n_rc_timeouts++;
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002059 del_timer(&qp->s_timer);
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002060 trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002061 if (rdi->driver_f.notify_restart_rc)
2062 rdi->driver_f.notify_restart_rc(qp,
2063 qp->s_last_psn + 1,
2064 1);
2065 rdi->driver_f.schedule_send(qp);
2066 }
2067 spin_unlock(&qp->s_lock);
2068 spin_unlock_irqrestore(&qp->r_lock, flags);
2069}
2070
2071/*
2072 * This is called from s_timer for RNR timeouts.
2073 */
2074enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2075{
2076 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2077 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2078 unsigned long flags;
2079
2080 spin_lock_irqsave(&qp->s_lock, flags);
2081 rvt_stop_rnr_timer(qp);
2082 rdi->driver_f.schedule_send(qp);
2083 spin_unlock_irqrestore(&qp->s_lock, flags);
2084 return HRTIMER_NORESTART;
2085}
2086EXPORT_SYMBOL(rvt_rc_rnr_retry);