blob: 0b97598136b4e062467175584f44b73358d3ed95 [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
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -080058static void rvt_rc_timeout(unsigned long arg);
59
60/*
61 * Convert the AETH RNR timeout code into the number of microseconds.
62 */
63static const u32 ib_rvt_rnr_table[32] = {
64 655360, /* 00: 655.36 */
65 10, /* 01: .01 */
66 20, /* 02 .02 */
67 30, /* 03: .03 */
68 40, /* 04: .04 */
69 60, /* 05: .06 */
70 80, /* 06: .08 */
71 120, /* 07: .12 */
72 160, /* 08: .16 */
73 240, /* 09: .24 */
74 320, /* 0A: .32 */
75 480, /* 0B: .48 */
76 640, /* 0C: .64 */
77 960, /* 0D: .96 */
78 1280, /* 0E: 1.28 */
79 1920, /* 0F: 1.92 */
80 2560, /* 10: 2.56 */
81 3840, /* 11: 3.84 */
82 5120, /* 12: 5.12 */
83 7680, /* 13: 7.68 */
84 10240, /* 14: 10.24 */
85 15360, /* 15: 15.36 */
86 20480, /* 16: 20.48 */
87 30720, /* 17: 30.72 */
88 40960, /* 18: 40.96 */
89 61440, /* 19: 61.44 */
90 81920, /* 1A: 81.92 */
91 122880, /* 1B: 122.88 */
92 163840, /* 1C: 163.84 */
93 245760, /* 1D: 245.76 */
94 327680, /* 1E: 327.68 */
95 491520 /* 1F: 491.52 */
96};
97
Dennis Dalessandrobfbac092016-01-22 13:00:22 -080098/*
99 * Note that it is OK to post send work requests in the SQE and ERR
100 * states; rvt_do_send() will process them and generate error
101 * completions as per IB 1.2 C10-96.
102 */
103const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
104 [IB_QPS_RESET] = 0,
105 [IB_QPS_INIT] = RVT_POST_RECV_OK,
106 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
107 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
108 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
109 RVT_PROCESS_NEXT_SEND_OK,
110 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
111 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
112 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
113 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
114 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
115 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
116};
117EXPORT_SYMBOL(ib_rvt_state_ops);
118
Mike Marciniszynf2dc9cd2016-12-07 19:34:06 -0800119/*
120 * Translate ib_wr_opcode into ib_wc_opcode.
121 */
122const enum ib_wc_opcode ib_rvt_wc_opcode[] = {
123 [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
124 [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
125 [IB_WR_SEND] = IB_WC_SEND,
126 [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
127 [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
128 [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
129 [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD,
130 [IB_WR_SEND_WITH_INV] = IB_WC_SEND,
131 [IB_WR_LOCAL_INV] = IB_WC_LOCAL_INV,
132 [IB_WR_REG_MR] = IB_WC_REG_MR
133};
134EXPORT_SYMBOL(ib_rvt_wc_opcode);
135
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800136static void get_map_page(struct rvt_qpn_table *qpt,
137 struct rvt_qpn_map *map,
138 gfp_t gfp)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800139{
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800140 unsigned long page = get_zeroed_page(gfp);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800141
142 /*
143 * Free the page if someone raced with us installing it.
144 */
145
146 spin_lock(&qpt->lock);
147 if (map->page)
148 free_page(page);
149 else
150 map->page = (void *)page;
151 spin_unlock(&qpt->lock);
152}
153
154/**
155 * init_qpn_table - initialize the QP number table for a device
156 * @qpt: the QPN table
157 */
158static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
159{
160 u32 offset, i;
161 struct rvt_qpn_map *map;
162 int ret = 0;
163
Harish Chegondifef2efd2016-01-22 12:50:30 -0800164 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800165 return -EINVAL;
166
167 spin_lock_init(&qpt->lock);
168
169 qpt->last = rdi->dparms.qpn_start;
170 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
171
172 /*
173 * Drivers may want some QPs beyond what we need for verbs let them use
174 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
175 * for those. The reserved range must be *after* the range which verbs
176 * will pick from.
177 */
178
179 /* Figure out number of bit maps needed before reserved range */
180 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
181
182 /* This should always be zero */
183 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
184
185 /* Starting with the first reserved bit map */
186 map = &qpt->map[qpt->nmaps];
187
188 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
189 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
Harish Chegondifef2efd2016-01-22 12:50:30 -0800190 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800191 if (!map->page) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800192 get_map_page(qpt, map, GFP_KERNEL);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800193 if (!map->page) {
194 ret = -ENOMEM;
195 break;
196 }
197 }
198 set_bit(offset, map->page);
199 offset++;
200 if (offset == RVT_BITS_PER_PAGE) {
201 /* next page */
202 qpt->nmaps++;
203 map++;
204 offset = 0;
205 }
206 }
207 return ret;
208}
209
210/**
211 * free_qpn_table - free the QP number table for a device
212 * @qpt: the QPN table
213 */
214static void free_qpn_table(struct rvt_qpn_table *qpt)
215{
216 int i;
217
218 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
219 free_page((unsigned long)qpt->map[i].page);
220}
221
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800222/**
223 * rvt_driver_qp_init - Init driver qp resources
224 * @rdi: rvt dev strucutre
225 *
226 * Return: 0 on success
227 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800228int rvt_driver_qp_init(struct rvt_dev_info *rdi)
229{
230 int i;
231 int ret = -ENOMEM;
232
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800233 if (!rdi->dparms.qp_table_size)
234 return -EINVAL;
235
236 /*
237 * If driver is not doing any QP allocation then make sure it is
238 * providing the necessary QP functions.
239 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800240 if (!rdi->driver_f.free_all_qps ||
241 !rdi->driver_f.qp_priv_alloc ||
242 !rdi->driver_f.qp_priv_free ||
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800243 !rdi->driver_f.notify_qp_reset ||
244 !rdi->driver_f.notify_restart_rc)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800245 return -EINVAL;
246
247 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800248 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
249 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800250 if (!rdi->qp_dev)
251 return -ENOMEM;
252
253 /* allocate hash table */
254 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
255 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
256 rdi->qp_dev->qp_table =
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800257 kmalloc_node(rdi->qp_dev->qp_table_size *
258 sizeof(*rdi->qp_dev->qp_table),
259 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800260 if (!rdi->qp_dev->qp_table)
261 goto no_qp_table;
262
263 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
264 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
265
266 spin_lock_init(&rdi->qp_dev->qpt_lock);
267
268 /* initialize qpn map */
269 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
270 goto fail_table;
271
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800272 spin_lock_init(&rdi->n_qps_lock);
273
274 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800275
276fail_table:
277 kfree(rdi->qp_dev->qp_table);
278 free_qpn_table(&rdi->qp_dev->qpn_table);
279
280no_qp_table:
281 kfree(rdi->qp_dev);
282
283 return ret;
284}
285
286/**
287 * free_all_qps - check for QPs still in use
288 * @qpt: the QP table to empty
289 *
290 * There should not be any QPs still in use.
291 * Free memory for table.
292 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800293static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800294{
295 unsigned long flags;
296 struct rvt_qp *qp;
297 unsigned n, qp_inuse = 0;
298 spinlock_t *ql; /* work around too long line below */
299
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800300 if (rdi->driver_f.free_all_qps)
301 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800302
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800303 qp_inuse += rvt_mcast_tree_empty(rdi);
304
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800305 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800306 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800307
308 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800309 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800310 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
311 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
312 lockdep_is_held(ql));
313 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800314
315 for (; qp; qp = rcu_dereference_protected(qp->next,
316 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800317 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800318 }
319 spin_unlock_irqrestore(ql, flags);
320 synchronize_rcu();
321 return qp_inuse;
322}
323
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800324/**
325 * rvt_qp_exit - clean up qps on device exit
326 * @rdi: rvt dev structure
327 *
328 * Check for qp leaks and free resources.
329 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800330void rvt_qp_exit(struct rvt_dev_info *rdi)
331{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800332 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800333
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800334 if (qps_inuse)
335 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
336 qps_inuse);
337 if (!rdi->qp_dev)
338 return;
339
340 kfree(rdi->qp_dev->qp_table);
341 free_qpn_table(&rdi->qp_dev->qpn_table);
342 kfree(rdi->qp_dev);
343}
344
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800345static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
346 struct rvt_qpn_map *map, unsigned off)
347{
348 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
349}
350
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800351/**
352 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
353 * IB_QPT_SMI/IB_QPT_GSI
354 *@rdi: rvt device info structure
355 *@qpt: queue pair number table pointer
356 *@port_num: IB port number, 1 based, comes from core
357 *
358 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800359 */
360static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800361 enum ib_qp_type type, u8 port_num, gfp_t gfp)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800362{
363 u32 i, offset, max_scan, qpn;
364 struct rvt_qpn_map *map;
365 u32 ret;
366
367 if (rdi->driver_f.alloc_qpn)
Ira Weinyb7b3cf42016-02-03 14:15:28 -0800368 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800369
370 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
371 unsigned n;
372
373 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800374 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800375 spin_lock(&qpt->lock);
376 if (qpt->flags & n)
377 ret = -EINVAL;
378 else
379 qpt->flags |= n;
380 spin_unlock(&qpt->lock);
381 goto bail;
382 }
383
384 qpn = qpt->last + qpt->incr;
385 if (qpn >= RVT_QPN_MAX)
386 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
387 /* offset carries bit 0 */
388 offset = qpn & RVT_BITS_PER_PAGE_MASK;
389 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
390 max_scan = qpt->nmaps - !offset;
391 for (i = 0;;) {
392 if (unlikely(!map->page)) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800393 get_map_page(qpt, map, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800394 if (unlikely(!map->page))
395 break;
396 }
397 do {
398 if (!test_and_set_bit(offset, map->page)) {
399 qpt->last = qpn;
400 ret = qpn;
401 goto bail;
402 }
403 offset += qpt->incr;
404 /*
405 * This qpn might be bogus if offset >= BITS_PER_PAGE.
406 * That is OK. It gets re-assigned below
407 */
408 qpn = mk_qpn(qpt, map, offset);
409 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
410 /*
411 * In order to keep the number of pages allocated to a
412 * minimum, we scan the all existing pages before increasing
413 * the size of the bitmap table.
414 */
415 if (++i > max_scan) {
416 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
417 break;
418 map = &qpt->map[qpt->nmaps++];
419 /* start at incr with current bit 0 */
420 offset = qpt->incr | (offset & 1);
421 } else if (map < &qpt->map[qpt->nmaps]) {
422 ++map;
423 /* start at incr with current bit 0 */
424 offset = qpt->incr | (offset & 1);
425 } else {
426 map = &qpt->map[0];
427 /* wrap to first map page, invert bit 0 */
428 offset = qpt->incr | ((offset & 1) ^ 1);
429 }
Brian Welty501edc422016-06-09 07:51:20 -0700430 /* there can be no set bits in low-order QoS bits */
431 WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800432 qpn = mk_qpn(qpt, map, offset);
433 }
434
435 ret = -ENOMEM;
436
437bail:
438 return ret;
439}
440
441static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
442{
443 struct rvt_qpn_map *map;
444
445 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
446 if (map->page)
447 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
448}
449
450/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800451 * rvt_clear_mr_refs - Drop help mr refs
452 * @qp: rvt qp data structure
453 * @clr_sends: If shoudl clear send side or not
454 */
455static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
456{
457 unsigned n;
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700458 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800459
460 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
461 rvt_put_ss(&qp->s_rdma_read_sge);
462
463 rvt_put_ss(&qp->r_sge);
464
465 if (clr_sends) {
466 while (qp->s_last != qp->s_head) {
467 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
468 unsigned i;
469
470 for (i = 0; i < wqe->wr.num_sge; i++) {
471 struct rvt_sge *sge = &wqe->sg_list[i];
472
473 rvt_put_mr(sge->mr);
474 }
475 if (qp->ibqp.qp_type == IB_QPT_UD ||
476 qp->ibqp.qp_type == IB_QPT_SMI ||
477 qp->ibqp.qp_type == IB_QPT_GSI)
478 atomic_dec(&ibah_to_rvtah(
479 wqe->ud_wr.ah)->refcount);
480 if (++qp->s_last >= qp->s_size)
481 qp->s_last = 0;
482 smp_wmb(); /* see qp_set_savail */
483 }
484 if (qp->s_rdma_mr) {
485 rvt_put_mr(qp->s_rdma_mr);
486 qp->s_rdma_mr = NULL;
487 }
488 }
489
490 if (qp->ibqp.qp_type != IB_QPT_RC)
491 return;
492
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700493 for (n = 0; n < rvt_max_atomic(rdi); n++) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800494 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
495
Ira Weinyfe508272016-07-27 21:07:36 -0400496 if (e->rdma_sge.mr) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800497 rvt_put_mr(e->rdma_sge.mr);
498 e->rdma_sge.mr = NULL;
499 }
500 }
501}
502
503/**
504 * rvt_remove_qp - remove qp form table
505 * @rdi: rvt dev struct
506 * @qp: qp to remove
507 *
508 * Remove the QP from the table so it can't be found asynchronously by
509 * the receive routine.
510 */
511static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
512{
513 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
514 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
515 unsigned long flags;
516 int removed = 1;
517
518 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
519
520 if (rcu_dereference_protected(rvp->qp[0],
521 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
522 RCU_INIT_POINTER(rvp->qp[0], NULL);
523 } else if (rcu_dereference_protected(rvp->qp[1],
524 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
525 RCU_INIT_POINTER(rvp->qp[1], NULL);
526 } else {
527 struct rvt_qp *q;
528 struct rvt_qp __rcu **qpp;
529
530 removed = 0;
531 qpp = &rdi->qp_dev->qp_table[n];
532 for (; (q = rcu_dereference_protected(*qpp,
533 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
534 qpp = &q->next) {
535 if (q == qp) {
536 RCU_INIT_POINTER(*qpp,
537 rcu_dereference_protected(qp->next,
538 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
539 removed = 1;
540 trace_rvt_qpremove(qp, n);
541 break;
542 }
543 }
544 }
545
546 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
547 if (removed) {
548 synchronize_rcu();
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -0700549 rvt_put_qp(qp);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800550 }
551}
552
553/**
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700554 * rvt_init_qp - initialize the QP state to the reset state
555 * @qp: the QP to init or reinit
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800556 * @type: the QP type
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700557 *
558 * This function is called from both rvt_create_qp() and
559 * rvt_reset_qp(). The difference is that the reset
560 * patch the necessary locks to protect against concurent
561 * access.
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800562 */
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700563static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
564 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800565{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800566 qp->remote_qpn = 0;
567 qp->qkey = 0;
568 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800569 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
570 qp->s_hdrwords = 0;
571 qp->s_wqe = NULL;
572 qp->s_draining = 0;
573 qp->s_next_psn = 0;
574 qp->s_last_psn = 0;
575 qp->s_sending_psn = 0;
576 qp->s_sending_hpsn = 0;
577 qp->s_psn = 0;
578 qp->r_psn = 0;
579 qp->r_msn = 0;
580 if (type == IB_QPT_RC) {
581 qp->s_state = IB_OPCODE_RC_SEND_LAST;
582 qp->r_state = IB_OPCODE_RC_SEND_LAST;
583 } else {
584 qp->s_state = IB_OPCODE_UC_SEND_LAST;
585 qp->r_state = IB_OPCODE_UC_SEND_LAST;
586 }
587 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
588 qp->r_nak_state = 0;
589 qp->r_aflags = 0;
590 qp->r_flags = 0;
591 qp->s_head = 0;
592 qp->s_tail = 0;
593 qp->s_cur = 0;
594 qp->s_acked = 0;
595 qp->s_last = 0;
596 qp->s_ssn = 1;
597 qp->s_lsn = 0;
598 qp->s_mig_state = IB_MIG_MIGRATED;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800599 qp->r_head_ack_queue = 0;
600 qp->s_tail_ack_queue = 0;
601 qp->s_num_rd_atomic = 0;
602 if (qp->r_rq.wq) {
603 qp->r_rq.wq->head = 0;
604 qp->r_rq.wq->tail = 0;
605 }
606 qp->r_sge.num_sge = 0;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700607 atomic_set(&qp->s_reserved_used, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800608}
609
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800610/**
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700611 * rvt_reset_qp - initialize the QP state to the reset state
612 * @qp: the QP to reset
613 * @type: the QP type
614 *
615 * r_lock, s_hlock, and s_lock are required to be held by the caller
616 */
617static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
618 enum ib_qp_type type)
619 __must_hold(&qp->s_lock)
620 __must_hold(&qp->s_hlock)
621 __must_hold(&qp->r_lock)
622{
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700623 lockdep_assert_held(&qp->r_lock);
624 lockdep_assert_held(&qp->s_hlock);
625 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700626 if (qp->state != IB_QPS_RESET) {
627 qp->state = IB_QPS_RESET;
628
629 /* Let drivers flush their waitlist */
630 rdi->driver_f.flush_qp_waiters(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800631 rvt_stop_rc_timers(qp);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700632 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
633 spin_unlock(&qp->s_lock);
634 spin_unlock(&qp->s_hlock);
635 spin_unlock_irq(&qp->r_lock);
636
637 /* Stop the send queue and the retry timer */
638 rdi->driver_f.stop_send_queue(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800639 rvt_del_timers_sync(qp);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700640 /* Wait for things to stop */
641 rdi->driver_f.quiesce_qp(qp);
642
643 /* take qp out the hash and wait for it to be unused */
644 rvt_remove_qp(rdi, qp);
645 wait_event(qp->wait, !atomic_read(&qp->refcount));
646
647 /* grab the lock b/c it was locked at call time */
648 spin_lock_irq(&qp->r_lock);
649 spin_lock(&qp->s_hlock);
650 spin_lock(&qp->s_lock);
651
652 rvt_clear_mr_refs(qp, 1);
653 /*
654 * Let the driver do any tear down or re-init it needs to for
655 * a qp that has been reset
656 */
657 rdi->driver_f.notify_qp_reset(qp);
658 }
659 rvt_init_qp(rdi, qp, type);
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700660 lockdep_assert_held(&qp->r_lock);
661 lockdep_assert_held(&qp->s_hlock);
662 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700663}
664
665/**
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800666 * rvt_create_qp - create a queue pair for a device
667 * @ibpd: the protection domain who's device we create the queue pair for
668 * @init_attr: the attributes of the queue pair
669 * @udata: user data for libibverbs.so
670 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800671 * Queue pair creation is mostly an rvt issue. However, drivers have their own
672 * unique idea of what queue pair numbers mean. For instance there is a reserved
673 * range for PSM.
674 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800675 * Return: the queue pair on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800676 *
677 * Called by the ib_create_qp() core verbs function.
678 */
679struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
680 struct ib_qp_init_attr *init_attr,
681 struct ib_udata *udata)
682{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800683 struct rvt_qp *qp;
684 int err;
685 struct rvt_swqe *swq = NULL;
686 size_t sz;
687 size_t sg_list_sz;
688 struct ib_qp *ret = ERR_PTR(-ENOMEM);
689 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
690 void *priv = NULL;
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800691 gfp_t gfp;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700692 size_t sqsize;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800693
694 if (!rdi)
695 return ERR_PTR(-EINVAL);
696
697 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
698 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800699 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800700 return ERR_PTR(-EINVAL);
701
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800702 /* GFP_NOIO is applicable to RC QP's only */
703
704 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
705 init_attr->qp_type != IB_QPT_RC)
706 return ERR_PTR(-EINVAL);
707
708 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
709 GFP_NOIO : GFP_KERNEL;
710
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800711 /* Check receive queue parameters if no SRQ is specified. */
712 if (!init_attr->srq) {
713 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
714 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
715 return ERR_PTR(-EINVAL);
716
717 if (init_attr->cap.max_send_sge +
718 init_attr->cap.max_send_wr +
719 init_attr->cap.max_recv_sge +
720 init_attr->cap.max_recv_wr == 0)
721 return ERR_PTR(-EINVAL);
722 }
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700723 sqsize =
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700724 init_attr->cap.max_send_wr + 1 +
725 rdi->dparms.reserved_operations;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800726 switch (init_attr->qp_type) {
727 case IB_QPT_SMI:
728 case IB_QPT_GSI:
729 if (init_attr->port_num == 0 ||
730 init_attr->port_num > ibpd->device->phys_port_cnt)
731 return ERR_PTR(-EINVAL);
732 case IB_QPT_UC:
733 case IB_QPT_RC:
734 case IB_QPT_UD:
735 sz = sizeof(struct rvt_sge) *
736 init_attr->cap.max_send_sge +
737 sizeof(struct rvt_swqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800738 if (gfp == GFP_NOIO)
739 swq = __vmalloc(
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700740 sqsize * sz,
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700741 gfp | __GFP_ZERO, PAGE_KERNEL);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800742 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700743 swq = vzalloc_node(
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700744 sqsize * sz,
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800745 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800746 if (!swq)
747 return ERR_PTR(-ENOMEM);
748
749 sz = sizeof(*qp);
750 sg_list_sz = 0;
751 if (init_attr->srq) {
752 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
753
754 if (srq->rq.max_sge > 1)
755 sg_list_sz = sizeof(*qp->r_sg_list) *
756 (srq->rq.max_sge - 1);
757 } else if (init_attr->cap.max_recv_sge > 1)
758 sg_list_sz = sizeof(*qp->r_sg_list) *
759 (init_attr->cap.max_recv_sge - 1);
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800760 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800761 if (!qp)
762 goto bail_swq;
763
764 RCU_INIT_POINTER(qp->next, NULL);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700765 if (init_attr->qp_type == IB_QPT_RC) {
766 qp->s_ack_queue =
767 kzalloc_node(
768 sizeof(*qp->s_ack_queue) *
769 rvt_max_atomic(rdi),
770 gfp,
771 rdi->dparms.node);
772 if (!qp->s_ack_queue)
773 goto bail_qp;
774 }
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800775 /* initialize timers needed for rc qp */
776 setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
777 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
778 HRTIMER_MODE_REL);
779 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800780
781 /*
782 * Driver needs to set up it's private QP structure and do any
783 * initialization that is needed.
784 */
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800785 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700786 if (IS_ERR(priv)) {
787 ret = priv;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800788 goto bail_qp;
Mike Marciniszync755f4a2016-06-22 13:29:33 -0700789 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800790 qp->priv = priv;
791 qp->timeout_jiffies =
792 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
793 1000UL);
794 if (init_attr->srq) {
795 sz = 0;
796 } else {
797 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
798 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
799 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
800 sizeof(struct rvt_rwqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800801 if (udata)
802 qp->r_rq.wq = vmalloc_user(
803 sizeof(struct rvt_rwq) +
804 qp->r_rq.size * sz);
805 else if (gfp == GFP_NOIO)
806 qp->r_rq.wq = __vmalloc(
807 sizeof(struct rvt_rwq) +
808 qp->r_rq.size * sz,
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700809 gfp | __GFP_ZERO, PAGE_KERNEL);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800810 else
Mike Marciniszyn654b6432016-05-19 05:21:25 -0700811 qp->r_rq.wq = vzalloc_node(
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800812 sizeof(struct rvt_rwq) +
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800813 qp->r_rq.size * sz,
814 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800815 if (!qp->r_rq.wq)
816 goto bail_driver_priv;
817 }
818
819 /*
820 * ib_create_qp() will initialize qp->ibqp
821 * except for qp->ibqp.qp_num.
822 */
823 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800824 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800825 spin_lock_init(&qp->s_lock);
826 spin_lock_init(&qp->r_rq.lock);
827 atomic_set(&qp->refcount, 0);
Jianxin Xiongd9f87232016-07-25 13:38:25 -0700828 atomic_set(&qp->local_ops_pending, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800829 init_waitqueue_head(&qp->wait);
830 init_timer(&qp->s_timer);
831 qp->s_timer.data = (unsigned long)qp;
832 INIT_LIST_HEAD(&qp->rspwait);
833 qp->state = IB_QPS_RESET;
834 qp->s_wq = swq;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700835 qp->s_size = sqsize;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800836 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800837 qp->s_max_sge = init_attr->cap.max_send_sge;
838 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
839 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
840
841 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
842 init_attr->qp_type,
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800843 init_attr->port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800844 if (err < 0) {
845 ret = ERR_PTR(err);
846 goto bail_rq_wq;
847 }
848 qp->ibqp.qp_num = err;
849 qp->port_num = init_attr->port_num;
Mike Marciniszyn222f7a92016-09-06 04:37:26 -0700850 rvt_init_qp(rdi, qp, init_attr->qp_type);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800851 break;
852
853 default:
854 /* Don't support raw QPs */
855 return ERR_PTR(-EINVAL);
856 }
857
858 init_attr->cap.max_inline_data = 0;
859
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800860 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800861 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800862 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800863 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800864 if (udata && udata->outlen >= sizeof(__u64)) {
865 if (!qp->r_rq.wq) {
866 __u64 offset = 0;
867
868 err = ib_copy_to_udata(udata, &offset,
869 sizeof(offset));
870 if (err) {
871 ret = ERR_PTR(err);
872 goto bail_qpn;
873 }
874 } else {
875 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
876
877 qp->ip = rvt_create_mmap_info(rdi, s,
878 ibpd->uobject->context,
879 qp->r_rq.wq);
880 if (!qp->ip) {
881 ret = ERR_PTR(-ENOMEM);
882 goto bail_qpn;
883 }
884
885 err = ib_copy_to_udata(udata, &qp->ip->offset,
886 sizeof(qp->ip->offset));
887 if (err) {
888 ret = ERR_PTR(err);
889 goto bail_ip;
890 }
891 }
Mike Marciniszynef086c02016-03-07 11:35:08 -0800892 qp->pid = current->pid;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800893 }
894
895 spin_lock(&rdi->n_qps_lock);
896 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
897 spin_unlock(&rdi->n_qps_lock);
898 ret = ERR_PTR(-ENOMEM);
899 goto bail_ip;
900 }
901
902 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800903 /*
904 * Maintain a busy_jiffies variable that will be added to the timeout
905 * period in mod_retry_timer and add_retry_timer. This busy jiffies
906 * is scaled by the number of rc qps created for the device to reduce
907 * the number of timeouts occurring when there is a large number of
908 * qps. busy_jiffies is incremented every rc qp scaling interval.
909 * The scaling interval is selected based on extensive performance
910 * evaluation of targeted workloads.
911 */
912 if (init_attr->qp_type == IB_QPT_RC) {
913 rdi->n_rc_qps++;
914 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
915 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800916 spin_unlock(&rdi->n_qps_lock);
917
918 if (qp->ip) {
919 spin_lock_irq(&rdi->pending_lock);
920 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
921 spin_unlock_irq(&rdi->pending_lock);
922 }
923
924 ret = &qp->ibqp;
925
926 /*
927 * We have our QP and its good, now keep track of what types of opcodes
928 * can be processed on this QP. We do this by keeping track of what the
929 * 3 high order bits of the opcode are.
930 */
931 switch (init_attr->qp_type) {
932 case IB_QPT_SMI:
933 case IB_QPT_GSI:
934 case IB_QPT_UD:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700935 qp->allowed_ops = IB_OPCODE_UD;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800936 break;
937 case IB_QPT_RC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700938 qp->allowed_ops = IB_OPCODE_RC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800939 break;
940 case IB_QPT_UC:
Mike Marciniszynb218f782016-04-12 11:29:20 -0700941 qp->allowed_ops = IB_OPCODE_UC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800942 break;
943 default:
944 ret = ERR_PTR(-EINVAL);
945 goto bail_ip;
946 }
947
948 return ret;
949
950bail_ip:
Jim Foraker22dccc52016-11-01 13:44:12 -0700951 if (qp->ip)
952 kref_put(&qp->ip->ref, rvt_release_mmap_info);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800953
954bail_qpn:
955 free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
956
957bail_rq_wq:
Mike Marciniszyn56c8ca52016-08-16 13:26:29 -0700958 if (!qp->ip)
959 vfree(qp->r_rq.wq);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800960
961bail_driver_priv:
962 rdi->driver_f.qp_priv_free(rdi, qp);
963
964bail_qp:
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700965 kfree(qp->s_ack_queue);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800966 kfree(qp);
967
968bail_swq:
969 vfree(swq);
970
971 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800972}
973
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800974/**
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800975 * rvt_error_qp - put a QP into the error state
976 * @qp: the QP to put into the error state
977 * @err: the receive completion error to signal if a RWQE is active
978 *
979 * Flushes both send and receive work queues.
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800980 *
981 * Return: true if last WQE event should be generated.
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800982 * The QP r_lock and s_lock should be held and interrupts disabled.
983 * If we are already in error state, just return.
984 */
985int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
986{
987 struct ib_wc wc;
988 int ret = 0;
989 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
990
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700991 lockdep_assert_held(&qp->r_lock);
992 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800993 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
994 goto bail;
995
996 qp->state = IB_QPS_ERR;
997
998 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
999 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1000 del_timer(&qp->s_timer);
1001 }
1002
1003 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
1004 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
1005
1006 rdi->driver_f.notify_error_qp(qp);
1007
1008 /* Schedule the sending tasklet to drain the send work queue. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001009 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001010 rdi->driver_f.schedule_send(qp);
1011
1012 rvt_clear_mr_refs(qp, 0);
1013
1014 memset(&wc, 0, sizeof(wc));
1015 wc.qp = &qp->ibqp;
1016 wc.opcode = IB_WC_RECV;
1017
1018 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1019 wc.wr_id = qp->r_wr_id;
1020 wc.status = err;
1021 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1022 }
1023 wc.status = IB_WC_WR_FLUSH_ERR;
1024
1025 if (qp->r_rq.wq) {
1026 struct rvt_rwq *wq;
1027 u32 head;
1028 u32 tail;
1029
1030 spin_lock(&qp->r_rq.lock);
1031
1032 /* sanity check pointers before trusting them */
1033 wq = qp->r_rq.wq;
1034 head = wq->head;
1035 if (head >= qp->r_rq.size)
1036 head = 0;
1037 tail = wq->tail;
1038 if (tail >= qp->r_rq.size)
1039 tail = 0;
1040 while (tail != head) {
1041 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1042 if (++tail >= qp->r_rq.size)
1043 tail = 0;
1044 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1045 }
1046 wq->tail = tail;
1047
1048 spin_unlock(&qp->r_rq.lock);
1049 } else if (qp->ibqp.event_handler) {
1050 ret = 1;
1051 }
1052
1053bail:
1054 return ret;
1055}
1056EXPORT_SYMBOL(rvt_error_qp);
1057
1058/*
1059 * Put the QP into the hash table.
1060 * The hash table holds a reference to the QP.
1061 */
1062static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1063{
1064 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1065 unsigned long flags;
1066
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001067 rvt_get_qp(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001068 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1069
1070 if (qp->ibqp.qp_num <= 1) {
1071 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1072 } else {
1073 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1074
1075 qp->next = rdi->qp_dev->qp_table[n];
1076 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1077 trace_rvt_qpinsert(qp, n);
1078 }
1079
1080 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1081}
1082
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001083/**
Parav Pandit61347fa2016-09-13 19:40:50 +05301084 * rvt_modify_qp - modify the attributes of a queue pair
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001085 * @ibqp: the queue pair who's attributes we're modifying
1086 * @attr: the new attributes
1087 * @attr_mask: the mask of attributes to modify
1088 * @udata: user data for libibverbs.so
1089 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001090 * Return: 0 on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001091 */
1092int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1093 int attr_mask, struct ib_udata *udata)
1094{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001095 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1096 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1097 enum ib_qp_state cur_state, new_state;
1098 struct ib_event ev;
1099 int lastwqe = 0;
1100 int mig = 0;
1101 int pmtu = 0; /* for gcc warning only */
1102 enum rdma_link_layer link;
1103
1104 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1105
1106 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001107 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001108 spin_lock(&qp->s_lock);
1109
1110 cur_state = attr_mask & IB_QP_CUR_STATE ?
1111 attr->cur_qp_state : qp->state;
1112 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1113
1114 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1115 attr_mask, link))
1116 goto inval;
1117
Ira Weinye85ec332016-01-22 13:04:38 -08001118 if (rdi->driver_f.check_modify_qp &&
1119 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1120 goto inval;
1121
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001122 if (attr_mask & IB_QP_AV) {
1123 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1124 goto inval;
1125 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1126 goto inval;
1127 }
1128
1129 if (attr_mask & IB_QP_ALT_PATH) {
1130 if (attr->alt_ah_attr.dlid >=
1131 be16_to_cpu(IB_MULTICAST_LID_BASE))
1132 goto inval;
1133 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1134 goto inval;
1135 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1136 goto inval;
1137 }
1138
1139 if (attr_mask & IB_QP_PKEY_INDEX)
1140 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1141 goto inval;
1142
1143 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1144 if (attr->min_rnr_timer > 31)
1145 goto inval;
1146
1147 if (attr_mask & IB_QP_PORT)
1148 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1149 qp->ibqp.qp_type == IB_QPT_GSI ||
1150 attr->port_num == 0 ||
1151 attr->port_num > ibqp->device->phys_port_cnt)
1152 goto inval;
1153
1154 if (attr_mask & IB_QP_DEST_QPN)
1155 if (attr->dest_qp_num > RVT_QPN_MASK)
1156 goto inval;
1157
1158 if (attr_mask & IB_QP_RETRY_CNT)
1159 if (attr->retry_cnt > 7)
1160 goto inval;
1161
1162 if (attr_mask & IB_QP_RNR_RETRY)
1163 if (attr->rnr_retry > 7)
1164 goto inval;
1165
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001166 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001167 * Don't allow invalid path_mtu values. OK to set greater
1168 * than the active mtu (or even the max_cap, if we have tuned
1169 * that to a small mtu. We'll set qp->path_mtu
1170 * to the lesser of requested attribute mtu and active,
1171 * for packetizing messages.
1172 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001173 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001174 if (attr_mask & IB_QP_PATH_MTU) {
1175 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1176 if (pmtu < 0)
1177 goto inval;
1178 }
1179
1180 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1181 if (attr->path_mig_state == IB_MIG_REARM) {
1182 if (qp->s_mig_state == IB_MIG_ARMED)
1183 goto inval;
1184 if (new_state != IB_QPS_RTS)
1185 goto inval;
1186 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1187 if (qp->s_mig_state == IB_MIG_REARM)
1188 goto inval;
1189 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1190 goto inval;
1191 if (qp->s_mig_state == IB_MIG_ARMED)
1192 mig = 1;
1193 } else {
1194 goto inval;
1195 }
1196 }
1197
1198 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1199 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1200 goto inval;
1201
1202 switch (new_state) {
1203 case IB_QPS_RESET:
1204 if (qp->state != IB_QPS_RESET)
1205 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1206 break;
1207
1208 case IB_QPS_RTR:
1209 /* Allow event to re-trigger if QP set to RTR more than once */
1210 qp->r_flags &= ~RVT_R_COMM_EST;
1211 qp->state = new_state;
1212 break;
1213
1214 case IB_QPS_SQD:
1215 qp->s_draining = qp->s_last != qp->s_cur;
1216 qp->state = new_state;
1217 break;
1218
1219 case IB_QPS_SQE:
1220 if (qp->ibqp.qp_type == IB_QPT_RC)
1221 goto inval;
1222 qp->state = new_state;
1223 break;
1224
1225 case IB_QPS_ERR:
1226 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1227 break;
1228
1229 default:
1230 qp->state = new_state;
1231 break;
1232 }
1233
1234 if (attr_mask & IB_QP_PKEY_INDEX)
1235 qp->s_pkey_index = attr->pkey_index;
1236
1237 if (attr_mask & IB_QP_PORT)
1238 qp->port_num = attr->port_num;
1239
1240 if (attr_mask & IB_QP_DEST_QPN)
1241 qp->remote_qpn = attr->dest_qp_num;
1242
1243 if (attr_mask & IB_QP_SQ_PSN) {
1244 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1245 qp->s_psn = qp->s_next_psn;
1246 qp->s_sending_psn = qp->s_next_psn;
1247 qp->s_last_psn = qp->s_next_psn - 1;
1248 qp->s_sending_hpsn = qp->s_last_psn;
1249 }
1250
1251 if (attr_mask & IB_QP_RQ_PSN)
1252 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1253
1254 if (attr_mask & IB_QP_ACCESS_FLAGS)
1255 qp->qp_access_flags = attr->qp_access_flags;
1256
1257 if (attr_mask & IB_QP_AV) {
1258 qp->remote_ah_attr = attr->ah_attr;
1259 qp->s_srate = attr->ah_attr.static_rate;
1260 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1261 }
1262
1263 if (attr_mask & IB_QP_ALT_PATH) {
1264 qp->alt_ah_attr = attr->alt_ah_attr;
1265 qp->s_alt_pkey_index = attr->alt_pkey_index;
1266 }
1267
1268 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1269 qp->s_mig_state = attr->path_mig_state;
1270 if (mig) {
1271 qp->remote_ah_attr = qp->alt_ah_attr;
1272 qp->port_num = qp->alt_ah_attr.port_num;
1273 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001274 }
1275 }
1276
1277 if (attr_mask & IB_QP_PATH_MTU) {
1278 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1279 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001280 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001281 }
1282
1283 if (attr_mask & IB_QP_RETRY_CNT) {
1284 qp->s_retry_cnt = attr->retry_cnt;
1285 qp->s_retry = attr->retry_cnt;
1286 }
1287
1288 if (attr_mask & IB_QP_RNR_RETRY) {
1289 qp->s_rnr_retry_cnt = attr->rnr_retry;
1290 qp->s_rnr_retry = attr->rnr_retry;
1291 }
1292
1293 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1294 qp->r_min_rnr_timer = attr->min_rnr_timer;
1295
1296 if (attr_mask & IB_QP_TIMEOUT) {
1297 qp->timeout = attr->timeout;
1298 qp->timeout_jiffies =
1299 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1300 1000UL);
1301 }
1302
1303 if (attr_mask & IB_QP_QKEY)
1304 qp->qkey = attr->qkey;
1305
1306 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1307 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1308
1309 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1310 qp->s_max_rd_atomic = attr->max_rd_atomic;
1311
Ira Weinye85ec332016-01-22 13:04:38 -08001312 if (rdi->driver_f.modify_qp)
1313 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1314
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001315 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001316 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001317 spin_unlock_irq(&qp->r_lock);
1318
1319 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1320 rvt_insert_qp(rdi, qp);
1321
1322 if (lastwqe) {
1323 ev.device = qp->ibqp.device;
1324 ev.element.qp = &qp->ibqp;
1325 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1326 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1327 }
1328 if (mig) {
1329 ev.device = qp->ibqp.device;
1330 ev.element.qp = &qp->ibqp;
1331 ev.event = IB_EVENT_PATH_MIG;
1332 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1333 }
1334 return 0;
1335
1336inval:
1337 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001338 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001339 spin_unlock_irq(&qp->r_lock);
1340 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001341}
1342
Dennis Dalessandro79a225b2016-02-14 12:11:20 -08001343/** rvt_free_qpn - Free a qpn from the bit map
1344 * @qpt: QP table
1345 * @qpn: queue pair number to free
1346 */
1347static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1348{
1349 struct rvt_qpn_map *map;
1350
1351 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1352 if (map->page)
1353 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1354}
1355
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001356/**
1357 * rvt_destroy_qp - destroy a queue pair
1358 * @ibqp: the queue pair to destroy
1359 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001360 * Note that this can be called while the QP is actively sending or
1361 * receiving!
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001362 *
1363 * Return: 0 on success.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001364 */
1365int rvt_destroy_qp(struct ib_qp *ibqp)
1366{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001367 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1368 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001369
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001370 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001371 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001372 spin_lock(&qp->s_lock);
1373 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1374 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001375 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001376 spin_unlock_irq(&qp->r_lock);
1377
1378 /* qpn is now available for use again */
1379 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1380
1381 spin_lock(&rdi->n_qps_lock);
1382 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001383 if (qp->ibqp.qp_type == IB_QPT_RC) {
1384 rdi->n_rc_qps--;
1385 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1386 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001387 spin_unlock(&rdi->n_qps_lock);
1388
1389 if (qp->ip)
1390 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1391 else
1392 vfree(qp->r_rq.wq);
1393 vfree(qp->s_wq);
1394 rdi->driver_f.qp_priv_free(rdi, qp);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001395 kfree(qp->s_ack_queue);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001396 kfree(qp);
1397 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001398}
1399
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001400/**
1401 * rvt_query_qp - query an ipbq
1402 * @ibqp: IB qp to query
1403 * @attr: attr struct to fill in
1404 * @attr_mask: attr mask ignored
1405 * @init_attr: struct to fill in
1406 *
1407 * Return: always 0
1408 */
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001409int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1410 int attr_mask, struct ib_qp_init_attr *init_attr)
1411{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001412 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1413 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1414
1415 attr->qp_state = qp->state;
1416 attr->cur_qp_state = attr->qp_state;
1417 attr->path_mtu = qp->path_mtu;
1418 attr->path_mig_state = qp->s_mig_state;
1419 attr->qkey = qp->qkey;
1420 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1421 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1422 attr->dest_qp_num = qp->remote_qpn;
1423 attr->qp_access_flags = qp->qp_access_flags;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001424 attr->cap.max_send_wr = qp->s_size - 1 -
1425 rdi->dparms.reserved_operations;
Harish Chegondi74d2d502016-01-22 13:05:04 -08001426 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1427 attr->cap.max_send_sge = qp->s_max_sge;
1428 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1429 attr->cap.max_inline_data = 0;
1430 attr->ah_attr = qp->remote_ah_attr;
1431 attr->alt_ah_attr = qp->alt_ah_attr;
1432 attr->pkey_index = qp->s_pkey_index;
1433 attr->alt_pkey_index = qp->s_alt_pkey_index;
1434 attr->en_sqd_async_notify = 0;
1435 attr->sq_draining = qp->s_draining;
1436 attr->max_rd_atomic = qp->s_max_rd_atomic;
1437 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1438 attr->min_rnr_timer = qp->r_min_rnr_timer;
1439 attr->port_num = qp->port_num;
1440 attr->timeout = qp->timeout;
1441 attr->retry_cnt = qp->s_retry_cnt;
1442 attr->rnr_retry = qp->s_rnr_retry_cnt;
1443 attr->alt_port_num = qp->alt_ah_attr.port_num;
1444 attr->alt_timeout = qp->alt_timeout;
1445
1446 init_attr->event_handler = qp->ibqp.event_handler;
1447 init_attr->qp_context = qp->ibqp.qp_context;
1448 init_attr->send_cq = qp->ibqp.send_cq;
1449 init_attr->recv_cq = qp->ibqp.recv_cq;
1450 init_attr->srq = qp->ibqp.srq;
1451 init_attr->cap = attr->cap;
1452 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1453 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1454 else
1455 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1456 init_attr->qp_type = qp->ibqp.qp_type;
1457 init_attr->port_num = qp->port_num;
1458 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001459}
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001460
1461/**
1462 * rvt_post_receive - post a receive on a QP
1463 * @ibqp: the QP to post the receive on
1464 * @wr: the WR to post
1465 * @bad_wr: the first bad WR is put here
1466 *
1467 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001468 *
1469 * Return: 0 on success otherwise errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001470 */
1471int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1472 struct ib_recv_wr **bad_wr)
1473{
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001474 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1475 struct rvt_rwq *wq = qp->r_rq.wq;
1476 unsigned long flags;
Alex Estrin000a8302016-03-07 11:35:51 -08001477 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1478 !qp->ibqp.srq;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001479
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001480 /* Check that state is OK to post receive. */
1481 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1482 *bad_wr = wr;
1483 return -EINVAL;
1484 }
1485
1486 for (; wr; wr = wr->next) {
1487 struct rvt_rwqe *wqe;
1488 u32 next;
1489 int i;
1490
1491 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1492 *bad_wr = wr;
1493 return -EINVAL;
1494 }
1495
1496 spin_lock_irqsave(&qp->r_rq.lock, flags);
1497 next = wq->head + 1;
1498 if (next >= qp->r_rq.size)
1499 next = 0;
1500 if (next == wq->tail) {
1501 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1502 *bad_wr = wr;
1503 return -ENOMEM;
1504 }
Alex Estrin000a8302016-03-07 11:35:51 -08001505 if (unlikely(qp_err_flush)) {
1506 struct ib_wc wc;
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001507
Alex Estrin000a8302016-03-07 11:35:51 -08001508 memset(&wc, 0, sizeof(wc));
1509 wc.qp = &qp->ibqp;
1510 wc.opcode = IB_WC_RECV;
1511 wc.wr_id = wr->wr_id;
1512 wc.status = IB_WC_WR_FLUSH_ERR;
1513 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1514 } else {
1515 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1516 wqe->wr_id = wr->wr_id;
1517 wqe->num_sge = wr->num_sge;
1518 for (i = 0; i < wr->num_sge; i++)
1519 wqe->sg_list[i] = wr->sg_list[i];
1520 /*
1521 * Make sure queue entry is written
1522 * before the head index.
1523 */
1524 smp_wmb();
1525 wq->head = next;
1526 }
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001527 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1528 }
1529 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001530}
1531
1532/**
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001533 * rvt_qp_valid_operation - validate post send wr request
1534 * @qp - the qp
1535 * @post-parms - the post send table for the driver
1536 * @wr - the work request
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001537 *
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001538 * The routine validates the operation based on the
1539 * validation table an returns the length of the operation
1540 * which can extend beyond the ib_send_bw. Operation
1541 * dependent flags key atomic operation validation.
1542 *
1543 * There is an exception for UD qps that validates the pd and
1544 * overrides the length to include the additional UD specific
1545 * length.
1546 *
1547 * Returns a negative error or the length of the work request
1548 * for building the swqe.
1549 */
1550static inline int rvt_qp_valid_operation(
1551 struct rvt_qp *qp,
1552 const struct rvt_operation_params *post_parms,
1553 struct ib_send_wr *wr)
1554{
1555 int len;
1556
1557 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1558 return -EINVAL;
1559 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1560 return -EINVAL;
1561 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1562 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1563 return -EINVAL;
1564 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1565 (wr->num_sge == 0 ||
1566 wr->sg_list[0].length < sizeof(u64) ||
1567 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1568 return -EINVAL;
1569 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1570 !qp->s_max_rd_atomic)
1571 return -EINVAL;
1572 len = post_parms[wr->opcode].length;
1573 /* UD specific */
1574 if (qp->ibqp.qp_type != IB_QPT_UC &&
1575 qp->ibqp.qp_type != IB_QPT_RC) {
1576 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1577 return -EINVAL;
1578 len = sizeof(struct ib_ud_wr);
1579 }
1580 return len;
1581}
1582
1583/**
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001584 * rvt_qp_is_avail - determine queue capacity
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001585 * @qp - the qp
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001586 * @rdi - the rdmavt device
1587 * @reserved_op - is reserved operation
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001588 *
1589 * This assumes the s_hlock is held but the s_last
1590 * qp variable is uncontrolled.
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001591 *
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001592 * For non reserved operations, the qp->s_avail
1593 * may be changed.
1594 *
1595 * The return value is zero or a -ENOMEM.
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001596 */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001597static inline int rvt_qp_is_avail(
1598 struct rvt_qp *qp,
1599 struct rvt_dev_info *rdi,
1600 bool reserved_op)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001601{
1602 u32 slast;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001603 u32 avail;
1604 u32 reserved_used;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001605
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001606 /* see rvt_qp_wqe_unreserve() */
1607 smp_mb__before_atomic();
1608 reserved_used = atomic_read(&qp->s_reserved_used);
1609 if (unlikely(reserved_op)) {
1610 /* see rvt_qp_wqe_unreserve() */
1611 smp_mb__before_atomic();
1612 if (reserved_used >= rdi->dparms.reserved_operations)
1613 return -ENOMEM;
1614 return 0;
1615 }
1616 /* non-reserved operations */
1617 if (likely(qp->s_avail))
1618 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001619 smp_read_barrier_depends(); /* see rc.c */
1620 slast = ACCESS_ONCE(qp->s_last);
1621 if (qp->s_head >= slast)
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001622 avail = qp->s_size - (qp->s_head - slast);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001623 else
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001624 avail = slast - qp->s_head;
1625
1626 /* see rvt_qp_wqe_unreserve() */
1627 smp_mb__before_atomic();
1628 reserved_used = atomic_read(&qp->s_reserved_used);
1629 avail = avail - 1 -
1630 (rdi->dparms.reserved_operations - reserved_used);
1631 /* insure we don't assign a negative s_avail */
1632 if ((s32)avail <= 0)
1633 return -ENOMEM;
1634 qp->s_avail = avail;
1635 if (WARN_ON(qp->s_avail >
1636 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1637 rvt_pr_err(rdi,
1638 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1639 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1640 qp->s_head, qp->s_tail, qp->s_cur,
1641 qp->s_acked, qp->s_last);
1642 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001643}
1644
1645/**
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001646 * rvt_post_one_wr - post one RC, UC, or UD send work request
1647 * @qp: the QP to post on
1648 * @wr: the work request to send
1649 */
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001650static int rvt_post_one_wr(struct rvt_qp *qp,
1651 struct ib_send_wr *wr,
1652 int *call_send)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001653{
1654 struct rvt_swqe *wqe;
1655 u32 next;
1656 int i;
1657 int j;
1658 int acc;
1659 struct rvt_lkey_table *rkt;
1660 struct rvt_pd *pd;
1661 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001662 u8 log_pmtu;
1663 int ret;
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001664 size_t cplen;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001665 bool reserved_op;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001666 int local_ops_delayed = 0;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001667
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001668 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1669
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001670 /* IB spec says that num_sge == 0 is OK. */
1671 if (unlikely(wr->num_sge > qp->s_max_sge))
1672 return -EINVAL;
1673
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001674 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1675 if (ret < 0)
1676 return ret;
1677 cplen = ret;
1678
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001679 /*
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001680 * Local operations include fast register and local invalidate.
1681 * Fast register needs to be processed immediately because the
1682 * registered lkey may be used by following work requests and the
1683 * lkey needs to be valid at the time those requests are posted.
1684 * Local invalidate can be processed immediately if fencing is
1685 * not required and no previous local invalidate ops are pending.
1686 * Signaled local operations that have been processed immediately
1687 * need to have requests with "completion only" flags set posted
1688 * to the send queue in order to generate completions.
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001689 */
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001690 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001691 switch (wr->opcode) {
1692 case IB_WR_REG_MR:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001693 ret = rvt_fast_reg_mr(qp,
1694 reg_wr(wr)->mr,
1695 reg_wr(wr)->key,
1696 reg_wr(wr)->access);
1697 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1698 return ret;
1699 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001700 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001701 if ((wr->send_flags & IB_SEND_FENCE) ||
1702 atomic_read(&qp->local_ops_pending)) {
1703 local_ops_delayed = 1;
1704 } else {
1705 ret = rvt_invalidate_rkey(
1706 qp, wr->ex.invalidate_rkey);
1707 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1708 return ret;
1709 }
1710 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001711 default:
1712 return -EINVAL;
1713 }
1714 }
1715
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001716 reserved_op = rdi->post_parms[wr->opcode].flags &
1717 RVT_OPERATION_USE_RESERVE;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001718 /* check for avail */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001719 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1720 if (ret)
1721 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001722 next = qp->s_head + 1;
1723 if (next >= qp->s_size)
1724 next = 0;
Ira Weiny60c30f52016-02-03 14:14:45 -08001725
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001726 rkt = &rdi->lkey_table;
1727 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1728 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1729
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001730 /* cplen has length from above */
1731 memcpy(&wqe->wr, wr, cplen);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001732
1733 wqe->length = 0;
1734 j = 0;
1735 if (wr->num_sge) {
1736 acc = wr->opcode >= IB_WR_RDMA_READ ?
1737 IB_ACCESS_LOCAL_WRITE : 0;
1738 for (i = 0; i < wr->num_sge; i++) {
1739 u32 length = wr->sg_list[i].length;
1740 int ok;
1741
1742 if (length == 0)
1743 continue;
1744 ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1745 &wr->sg_list[i], acc);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001746 if (!ok) {
1747 ret = -EINVAL;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001748 goto bail_inval_free;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001749 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001750 wqe->length += length;
1751 j++;
1752 }
1753 wqe->wr.num_sge = j;
1754 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001755
1756 /* general part of wqe valid - allow for driver checks */
1757 if (rdi->driver_f.check_send_wqe) {
1758 ret = rdi->driver_f.check_send_wqe(qp, wqe);
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001759 if (ret < 0)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001760 goto bail_inval_free;
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001761 if (ret)
1762 *call_send = ret;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001763 }
1764
1765 log_pmtu = qp->log_pmtu;
1766 if (qp->ibqp.qp_type != IB_QPT_UC &&
1767 qp->ibqp.qp_type != IB_QPT_RC) {
1768 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1769
1770 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001771 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1772 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001773
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001774 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001775 if (local_ops_delayed)
1776 atomic_inc(&qp->local_ops_pending);
1777 else
1778 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001779 wqe->ssn = 0;
1780 wqe->psn = 0;
1781 wqe->lpsn = 0;
1782 } else {
1783 wqe->ssn = qp->s_ssn++;
1784 wqe->psn = qp->s_next_psn;
1785 wqe->lpsn = wqe->psn +
1786 (wqe->length ?
1787 ((wqe->length - 1) >> log_pmtu) :
1788 0);
1789 qp->s_next_psn = wqe->lpsn + 1;
1790 }
Harish Chegondie16689e2016-02-14 12:10:12 -08001791 trace_rvt_post_one_wr(qp, wqe);
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001792 if (unlikely(reserved_op))
1793 rvt_qp_wqe_reserve(qp, wqe);
1794 else
1795 qp->s_avail--;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001796 smp_wmb(); /* see request builders */
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001797 qp->s_head = next;
1798
1799 return 0;
1800
1801bail_inval_free:
1802 /* release mr holds */
1803 while (j) {
1804 struct rvt_sge *sge = &wqe->sg_list[--j];
1805
1806 rvt_put_mr(sge->mr);
1807 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001808 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001809}
1810
1811/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001812 * rvt_post_send - post a send on a QP
1813 * @ibqp: the QP to post the send on
1814 * @wr: the list of work requests to post
1815 * @bad_wr: the first bad WR is put here
1816 *
1817 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001818 *
1819 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001820 */
1821int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1822 struct ib_send_wr **bad_wr)
1823{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001824 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1825 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1826 unsigned long flags = 0;
1827 int call_send;
1828 unsigned nreq = 0;
1829 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001830
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001831 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001832
1833 /*
1834 * Ensure QP state is such that we can send. If not bail out early,
1835 * there is no need to do this every time we post a send.
1836 */
1837 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001838 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001839 return -EINVAL;
1840 }
1841
1842 /*
1843 * If the send queue is empty, and we only have a single WR then just go
1844 * ahead and kick the send engine into gear. Otherwise we will always
1845 * just schedule the send to happen later.
1846 */
1847 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1848
1849 for (; wr; wr = wr->next) {
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001850 err = rvt_post_one_wr(qp, wr, &call_send);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001851 if (unlikely(err)) {
1852 *bad_wr = wr;
1853 goto bail;
1854 }
1855 nreq++;
1856 }
1857bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001858 spin_unlock_irqrestore(&qp->s_hlock, flags);
1859 if (nreq) {
1860 if (call_send)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001861 rdi->driver_f.do_send(qp);
Jubin Johne6d2e012016-04-12 10:47:00 -07001862 else
1863 rdi->driver_f.schedule_send_no_lock(qp);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001864 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001865 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001866}
1867
1868/**
1869 * rvt_post_srq_receive - post a receive on a shared receive queue
1870 * @ibsrq: the SRQ to post the receive on
1871 * @wr: the list of work requests to post
1872 * @bad_wr: A pointer to the first WR to cause a problem is put here
1873 *
1874 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001875 *
1876 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001877 */
1878int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1879 struct ib_recv_wr **bad_wr)
1880{
Jubin Johnb8f881b2016-02-03 14:14:36 -08001881 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1882 struct rvt_rwq *wq;
1883 unsigned long flags;
1884
1885 for (; wr; wr = wr->next) {
1886 struct rvt_rwqe *wqe;
1887 u32 next;
1888 int i;
1889
1890 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1891 *bad_wr = wr;
1892 return -EINVAL;
1893 }
1894
1895 spin_lock_irqsave(&srq->rq.lock, flags);
1896 wq = srq->rq.wq;
1897 next = wq->head + 1;
1898 if (next >= srq->rq.size)
1899 next = 0;
1900 if (next == wq->tail) {
1901 spin_unlock_irqrestore(&srq->rq.lock, flags);
1902 *bad_wr = wr;
1903 return -ENOMEM;
1904 }
1905
1906 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1907 wqe->wr_id = wr->wr_id;
1908 wqe->num_sge = wr->num_sge;
1909 for (i = 0; i < wr->num_sge; i++)
1910 wqe->sg_list[i] = wr->sg_list[i];
1911 /* Make sure queue entry is written before the head index. */
1912 smp_wmb();
1913 wq->head = next;
1914 spin_unlock_irqrestore(&srq->rq.lock, flags);
1915 }
1916 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001917}
Brian Weltybeb5a042017-02-08 05:27:01 -08001918
1919/**
1920 * qp_comm_est - handle trap with QP established
1921 * @qp: the QP
1922 */
1923void rvt_comm_est(struct rvt_qp *qp)
1924{
1925 qp->r_flags |= RVT_R_COMM_EST;
1926 if (qp->ibqp.event_handler) {
1927 struct ib_event ev;
1928
1929 ev.device = qp->ibqp.device;
1930 ev.element.qp = &qp->ibqp;
1931 ev.event = IB_EVENT_COMM_EST;
1932 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1933 }
1934}
1935EXPORT_SYMBOL(rvt_comm_est);
1936
1937void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
1938{
1939 unsigned long flags;
1940 int lastwqe;
1941
1942 spin_lock_irqsave(&qp->s_lock, flags);
1943 lastwqe = rvt_error_qp(qp, err);
1944 spin_unlock_irqrestore(&qp->s_lock, flags);
1945
1946 if (lastwqe) {
1947 struct ib_event ev;
1948
1949 ev.device = qp->ibqp.device;
1950 ev.element.qp = &qp->ibqp;
1951 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1952 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1953 }
1954}
1955EXPORT_SYMBOL(rvt_rc_error);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001956
Don Hiatt881fccb2017-02-08 05:28:19 -08001957/*
1958 * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
1959 * @index - the index
1960 * return usec from an index into ib_rvt_rnr_table
1961 */
1962unsigned long rvt_rnr_tbl_to_usec(u32 index)
1963{
1964 return ib_rvt_rnr_table[(index & RVT_AETH_CREDIT_MASK)];
1965}
1966EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
1967
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001968static inline unsigned long rvt_aeth_to_usec(u32 aeth)
1969{
1970 return ib_rvt_rnr_table[(aeth >> RVT_AETH_CREDIT_SHIFT) &
1971 RVT_AETH_CREDIT_MASK];
1972}
1973
1974/*
1975 * rvt_add_retry_timer - add/start a retry timer
1976 * @qp - the QP
1977 * add a retry timer on the QP
1978 */
1979void rvt_add_retry_timer(struct rvt_qp *qp)
1980{
1981 struct ib_qp *ibqp = &qp->ibqp;
1982 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1983
1984 lockdep_assert_held(&qp->s_lock);
1985 qp->s_flags |= RVT_S_TIMER;
1986 /* 4.096 usec. * (1 << qp->timeout) */
1987 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
1988 rdi->busy_jiffies;
1989 add_timer(&qp->s_timer);
1990}
1991EXPORT_SYMBOL(rvt_add_retry_timer);
1992
1993/**
1994 * rvt_add_rnr_timer - add/start an rnr timer
1995 * @qp - the QP
1996 * @aeth - aeth of RNR timeout, simulated aeth for loopback
1997 * add an rnr timer on the QP
1998 */
1999void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
2000{
2001 u32 to;
2002
2003 lockdep_assert_held(&qp->s_lock);
2004 qp->s_flags |= RVT_S_WAIT_RNR;
2005 to = rvt_aeth_to_usec(aeth);
2006 hrtimer_start(&qp->s_rnr_timer,
2007 ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
2008}
2009EXPORT_SYMBOL(rvt_add_rnr_timer);
2010
2011/**
2012 * rvt_stop_rc_timers - stop all timers
2013 * @qp - the QP
2014 * stop any pending timers
2015 */
2016void rvt_stop_rc_timers(struct rvt_qp *qp)
2017{
2018 lockdep_assert_held(&qp->s_lock);
2019 /* Remove QP from all timers */
2020 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2021 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2022 del_timer(&qp->s_timer);
2023 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2024 }
2025}
2026EXPORT_SYMBOL(rvt_stop_rc_timers);
2027
2028/**
2029 * rvt_stop_rnr_timer - stop an rnr timer
2030 * @qp - the QP
2031 *
2032 * stop an rnr timer and return if the timer
2033 * had been pending.
2034 */
2035static int rvt_stop_rnr_timer(struct rvt_qp *qp)
2036{
2037 int rval = 0;
2038
2039 lockdep_assert_held(&qp->s_lock);
2040 /* Remove QP from rnr timer */
2041 if (qp->s_flags & RVT_S_WAIT_RNR) {
2042 qp->s_flags &= ~RVT_S_WAIT_RNR;
2043 rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
2044 }
2045 return rval;
2046}
2047
2048/**
2049 * rvt_del_timers_sync - wait for any timeout routines to exit
2050 * @qp - the QP
2051 */
2052void rvt_del_timers_sync(struct rvt_qp *qp)
2053{
2054 del_timer_sync(&qp->s_timer);
2055 hrtimer_cancel(&qp->s_rnr_timer);
2056}
2057EXPORT_SYMBOL(rvt_del_timers_sync);
2058
2059/**
2060 * This is called from s_timer for missing responses.
2061 */
2062static void rvt_rc_timeout(unsigned long arg)
2063{
2064 struct rvt_qp *qp = (struct rvt_qp *)arg;
2065 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2066 unsigned long flags;
2067
2068 spin_lock_irqsave(&qp->r_lock, flags);
2069 spin_lock(&qp->s_lock);
2070 if (qp->s_flags & RVT_S_TIMER) {
2071 qp->s_flags &= ~RVT_S_TIMER;
2072 del_timer(&qp->s_timer);
2073 if (rdi->driver_f.notify_restart_rc)
2074 rdi->driver_f.notify_restart_rc(qp,
2075 qp->s_last_psn + 1,
2076 1);
2077 rdi->driver_f.schedule_send(qp);
2078 }
2079 spin_unlock(&qp->s_lock);
2080 spin_unlock_irqrestore(&qp->r_lock, flags);
2081}
2082
2083/*
2084 * This is called from s_timer for RNR timeouts.
2085 */
2086enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2087{
2088 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2089 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2090 unsigned long flags;
2091
2092 spin_lock_irqsave(&qp->s_lock, flags);
2093 rvt_stop_rnr_timer(qp);
2094 rdi->driver_f.schedule_send(qp);
2095 spin_unlock_irqrestore(&qp->s_lock, flags);
2096 return HRTIMER_NORESTART;
2097}
2098EXPORT_SYMBOL(rvt_rc_rnr_retry);