blob: 5809562fefdafc9399a70c88c62c681bf7813b8e [file] [log] [blame]
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001/*
Dennis Dalessandrofe314192016-01-22 13:04:58 -08002 * Copyright(c) 2016 Intel Corporation.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080048#include <linux/hash.h>
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080049#include <linux/bitops.h>
50#include <linux/lockdep.h>
Dennis Dalessandro515667f2016-01-22 12:50:17 -080051#include <linux/vmalloc.h>
52#include <linux/slab.h>
53#include <rdma/ib_verbs.h>
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080054#include "qp.h"
Dennis Dalessandro515667f2016-01-22 12:50:17 -080055#include "vt.h"
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080056#include "trace.h"
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080057
Dennis Dalessandrobfbac092016-01-22 13:00:22 -080058/*
59 * Note that it is OK to post send work requests in the SQE and ERR
60 * states; rvt_do_send() will process them and generate error
61 * completions as per IB 1.2 C10-96.
62 */
63const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
64 [IB_QPS_RESET] = 0,
65 [IB_QPS_INIT] = RVT_POST_RECV_OK,
66 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
67 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
68 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
69 RVT_PROCESS_NEXT_SEND_OK,
70 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
71 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
72 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
73 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
74 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
75 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
76};
77EXPORT_SYMBOL(ib_rvt_state_ops);
78
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -080079static void get_map_page(struct rvt_qpn_table *qpt,
80 struct rvt_qpn_map *map,
81 gfp_t gfp)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080082{
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -080083 unsigned long page = get_zeroed_page(gfp);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080084
85 /*
86 * Free the page if someone raced with us installing it.
87 */
88
89 spin_lock(&qpt->lock);
90 if (map->page)
91 free_page(page);
92 else
93 map->page = (void *)page;
94 spin_unlock(&qpt->lock);
95}
96
97/**
98 * init_qpn_table - initialize the QP number table for a device
99 * @qpt: the QPN table
100 */
101static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
102{
103 u32 offset, i;
104 struct rvt_qpn_map *map;
105 int ret = 0;
106
Harish Chegondifef2efd2016-01-22 12:50:30 -0800107 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800108 return -EINVAL;
109
110 spin_lock_init(&qpt->lock);
111
112 qpt->last = rdi->dparms.qpn_start;
113 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
114
115 /*
116 * Drivers may want some QPs beyond what we need for verbs let them use
117 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
118 * for those. The reserved range must be *after* the range which verbs
119 * will pick from.
120 */
121
122 /* Figure out number of bit maps needed before reserved range */
123 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
124
125 /* This should always be zero */
126 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
127
128 /* Starting with the first reserved bit map */
129 map = &qpt->map[qpt->nmaps];
130
131 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
132 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
Harish Chegondifef2efd2016-01-22 12:50:30 -0800133 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800134 if (!map->page) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800135 get_map_page(qpt, map, GFP_KERNEL);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800136 if (!map->page) {
137 ret = -ENOMEM;
138 break;
139 }
140 }
141 set_bit(offset, map->page);
142 offset++;
143 if (offset == RVT_BITS_PER_PAGE) {
144 /* next page */
145 qpt->nmaps++;
146 map++;
147 offset = 0;
148 }
149 }
150 return ret;
151}
152
153/**
154 * free_qpn_table - free the QP number table for a device
155 * @qpt: the QPN table
156 */
157static void free_qpn_table(struct rvt_qpn_table *qpt)
158{
159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
162 free_page((unsigned long)qpt->map[i].page);
163}
164
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800165/**
166 * rvt_driver_qp_init - Init driver qp resources
167 * @rdi: rvt dev strucutre
168 *
169 * Return: 0 on success
170 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800171int rvt_driver_qp_init(struct rvt_dev_info *rdi)
172{
173 int i;
174 int ret = -ENOMEM;
175
176 if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER) {
177 rvt_pr_info(rdi, "Driver is doing QP init.\n");
178 return 0;
179 }
180
181 if (!rdi->dparms.qp_table_size)
182 return -EINVAL;
183
184 /*
185 * If driver is not doing any QP allocation then make sure it is
186 * providing the necessary QP functions.
187 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800188 if (!rdi->driver_f.free_all_qps ||
189 !rdi->driver_f.qp_priv_alloc ||
190 !rdi->driver_f.qp_priv_free ||
191 !rdi->driver_f.notify_qp_reset)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800192 return -EINVAL;
193
194 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800195 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
196 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800197 if (!rdi->qp_dev)
198 return -ENOMEM;
199
200 /* allocate hash table */
201 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
202 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
203 rdi->qp_dev->qp_table =
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800204 kmalloc_node(rdi->qp_dev->qp_table_size *
205 sizeof(*rdi->qp_dev->qp_table),
206 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800207 if (!rdi->qp_dev->qp_table)
208 goto no_qp_table;
209
210 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
211 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
212
213 spin_lock_init(&rdi->qp_dev->qpt_lock);
214
215 /* initialize qpn map */
216 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
217 goto fail_table;
218
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800219 spin_lock_init(&rdi->n_qps_lock);
220
221 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800222
223fail_table:
224 kfree(rdi->qp_dev->qp_table);
225 free_qpn_table(&rdi->qp_dev->qpn_table);
226
227no_qp_table:
228 kfree(rdi->qp_dev);
229
230 return ret;
231}
232
233/**
234 * free_all_qps - check for QPs still in use
235 * @qpt: the QP table to empty
236 *
237 * There should not be any QPs still in use.
238 * Free memory for table.
239 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800240static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800241{
242 unsigned long flags;
243 struct rvt_qp *qp;
244 unsigned n, qp_inuse = 0;
245 spinlock_t *ql; /* work around too long line below */
246
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800247 if (rdi->driver_f.free_all_qps)
248 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800249
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800250 qp_inuse += rvt_mcast_tree_empty(rdi);
251
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800252 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800253 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800254
255 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800256 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800257 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
258 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
259 lockdep_is_held(ql));
260 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800261
262 for (; qp; qp = rcu_dereference_protected(qp->next,
263 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800264 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800265 }
266 spin_unlock_irqrestore(ql, flags);
267 synchronize_rcu();
268 return qp_inuse;
269}
270
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800271/**
272 * rvt_qp_exit - clean up qps on device exit
273 * @rdi: rvt dev structure
274 *
275 * Check for qp leaks and free resources.
276 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800277void rvt_qp_exit(struct rvt_dev_info *rdi)
278{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800279 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800280
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800281 if (qps_inuse)
282 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
283 qps_inuse);
284 if (!rdi->qp_dev)
285 return;
286
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800287 if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER)
288 return; /* driver did the qp init so nothing else to do */
289
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800290 kfree(rdi->qp_dev->qp_table);
291 free_qpn_table(&rdi->qp_dev->qpn_table);
292 kfree(rdi->qp_dev);
293}
294
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800295static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
296 struct rvt_qpn_map *map, unsigned off)
297{
298 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
299}
300
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800301/**
302 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
303 * IB_QPT_SMI/IB_QPT_GSI
304 *@rdi: rvt device info structure
305 *@qpt: queue pair number table pointer
306 *@port_num: IB port number, 1 based, comes from core
307 *
308 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800309 */
310static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800311 enum ib_qp_type type, u8 port_num, gfp_t gfp)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800312{
313 u32 i, offset, max_scan, qpn;
314 struct rvt_qpn_map *map;
315 u32 ret;
316
317 if (rdi->driver_f.alloc_qpn)
Ira Weinyb7b3cf42016-02-03 14:15:28 -0800318 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800319
320 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
321 unsigned n;
322
323 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800324 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800325 spin_lock(&qpt->lock);
326 if (qpt->flags & n)
327 ret = -EINVAL;
328 else
329 qpt->flags |= n;
330 spin_unlock(&qpt->lock);
331 goto bail;
332 }
333
334 qpn = qpt->last + qpt->incr;
335 if (qpn >= RVT_QPN_MAX)
336 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
337 /* offset carries bit 0 */
338 offset = qpn & RVT_BITS_PER_PAGE_MASK;
339 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
340 max_scan = qpt->nmaps - !offset;
341 for (i = 0;;) {
342 if (unlikely(!map->page)) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800343 get_map_page(qpt, map, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800344 if (unlikely(!map->page))
345 break;
346 }
347 do {
348 if (!test_and_set_bit(offset, map->page)) {
349 qpt->last = qpn;
350 ret = qpn;
351 goto bail;
352 }
353 offset += qpt->incr;
354 /*
355 * This qpn might be bogus if offset >= BITS_PER_PAGE.
356 * That is OK. It gets re-assigned below
357 */
358 qpn = mk_qpn(qpt, map, offset);
359 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
360 /*
361 * In order to keep the number of pages allocated to a
362 * minimum, we scan the all existing pages before increasing
363 * the size of the bitmap table.
364 */
365 if (++i > max_scan) {
366 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
367 break;
368 map = &qpt->map[qpt->nmaps++];
369 /* start at incr with current bit 0 */
370 offset = qpt->incr | (offset & 1);
371 } else if (map < &qpt->map[qpt->nmaps]) {
372 ++map;
373 /* start at incr with current bit 0 */
374 offset = qpt->incr | (offset & 1);
375 } else {
376 map = &qpt->map[0];
377 /* wrap to first map page, invert bit 0 */
378 offset = qpt->incr | ((offset & 1) ^ 1);
379 }
380 /* there can be no bits at shift and below */
381 WARN_ON(offset & (rdi->dparms.qos_shift - 1));
382 qpn = mk_qpn(qpt, map, offset);
383 }
384
385 ret = -ENOMEM;
386
387bail:
388 return ret;
389}
390
391static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
392{
393 struct rvt_qpn_map *map;
394
395 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
396 if (map->page)
397 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
398}
399
400/**
401 * reset_qp - initialize the QP state to the reset state
402 * @qp: the QP to reset
403 * @type: the QP type
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800404 * r and s lock are required to be held by the caller
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800405 */
Dennis Dalessandro5a9cf6f2016-01-22 12:50:24 -0800406void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
407 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800408{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800409 if (qp->state != IB_QPS_RESET) {
410 qp->state = IB_QPS_RESET;
411
412 /* Let drivers flush their waitlist */
413 rdi->driver_f.flush_qp_waiters(qp);
414 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
415 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800416 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800417 spin_unlock_irq(&qp->r_lock);
418
419 /* Stop the send queue and the retry timer */
420 rdi->driver_f.stop_send_queue(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800421
422 /* Wait for things to stop */
423 rdi->driver_f.quiesce_qp(qp);
424
425 /* take qp out the hash and wait for it to be unused */
426 rvt_remove_qp(rdi, qp);
427 wait_event(qp->wait, !atomic_read(&qp->refcount));
428
429 /* grab the lock b/c it was locked at call time */
430 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800431 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800432 spin_lock(&qp->s_lock);
433
434 rvt_clear_mr_refs(qp, 1);
435 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800436
437 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800438 * Let the driver do any tear down it needs to for a qp
439 * that has been reset
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800440 */
441 rdi->driver_f.notify_qp_reset(qp);
442
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800443 qp->remote_qpn = 0;
444 qp->qkey = 0;
445 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800446 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
447 qp->s_hdrwords = 0;
448 qp->s_wqe = NULL;
449 qp->s_draining = 0;
450 qp->s_next_psn = 0;
451 qp->s_last_psn = 0;
452 qp->s_sending_psn = 0;
453 qp->s_sending_hpsn = 0;
454 qp->s_psn = 0;
455 qp->r_psn = 0;
456 qp->r_msn = 0;
457 if (type == IB_QPT_RC) {
458 qp->s_state = IB_OPCODE_RC_SEND_LAST;
459 qp->r_state = IB_OPCODE_RC_SEND_LAST;
460 } else {
461 qp->s_state = IB_OPCODE_UC_SEND_LAST;
462 qp->r_state = IB_OPCODE_UC_SEND_LAST;
463 }
464 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
465 qp->r_nak_state = 0;
466 qp->r_aflags = 0;
467 qp->r_flags = 0;
468 qp->s_head = 0;
469 qp->s_tail = 0;
470 qp->s_cur = 0;
471 qp->s_acked = 0;
472 qp->s_last = 0;
473 qp->s_ssn = 1;
474 qp->s_lsn = 0;
475 qp->s_mig_state = IB_MIG_MIGRATED;
476 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
477 qp->r_head_ack_queue = 0;
478 qp->s_tail_ack_queue = 0;
479 qp->s_num_rd_atomic = 0;
480 if (qp->r_rq.wq) {
481 qp->r_rq.wq->head = 0;
482 qp->r_rq.wq->tail = 0;
483 }
484 qp->r_sge.num_sge = 0;
485}
Dennis Dalessandro5a9cf6f2016-01-22 12:50:24 -0800486EXPORT_SYMBOL(rvt_reset_qp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800487
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800488/**
489 * rvt_create_qp - create a queue pair for a device
490 * @ibpd: the protection domain who's device we create the queue pair for
491 * @init_attr: the attributes of the queue pair
492 * @udata: user data for libibverbs.so
493 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800494 * Queue pair creation is mostly an rvt issue. However, drivers have their own
495 * unique idea of what queue pair numbers mean. For instance there is a reserved
496 * range for PSM.
497 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800498 * Return: the queue pair on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800499 *
500 * Called by the ib_create_qp() core verbs function.
501 */
502struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
503 struct ib_qp_init_attr *init_attr,
504 struct ib_udata *udata)
505{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800506 struct rvt_qp *qp;
507 int err;
508 struct rvt_swqe *swq = NULL;
509 size_t sz;
510 size_t sg_list_sz;
511 struct ib_qp *ret = ERR_PTR(-ENOMEM);
512 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
513 void *priv = NULL;
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800514 gfp_t gfp;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800515
516 if (!rdi)
517 return ERR_PTR(-EINVAL);
518
519 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
520 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800521 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800522 return ERR_PTR(-EINVAL);
523
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800524 /* GFP_NOIO is applicable to RC QP's only */
525
526 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
527 init_attr->qp_type != IB_QPT_RC)
528 return ERR_PTR(-EINVAL);
529
530 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
531 GFP_NOIO : GFP_KERNEL;
532
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800533 /* Check receive queue parameters if no SRQ is specified. */
534 if (!init_attr->srq) {
535 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
536 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
537 return ERR_PTR(-EINVAL);
538
539 if (init_attr->cap.max_send_sge +
540 init_attr->cap.max_send_wr +
541 init_attr->cap.max_recv_sge +
542 init_attr->cap.max_recv_wr == 0)
543 return ERR_PTR(-EINVAL);
544 }
545
546 switch (init_attr->qp_type) {
547 case IB_QPT_SMI:
548 case IB_QPT_GSI:
549 if (init_attr->port_num == 0 ||
550 init_attr->port_num > ibpd->device->phys_port_cnt)
551 return ERR_PTR(-EINVAL);
552 case IB_QPT_UC:
553 case IB_QPT_RC:
554 case IB_QPT_UD:
555 sz = sizeof(struct rvt_sge) *
556 init_attr->cap.max_send_sge +
557 sizeof(struct rvt_swqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800558 if (gfp == GFP_NOIO)
559 swq = __vmalloc(
560 (init_attr->cap.max_send_wr + 1) * sz,
561 gfp, PAGE_KERNEL);
562 else
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800563 swq = vmalloc_node(
564 (init_attr->cap.max_send_wr + 1) * sz,
565 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800566 if (!swq)
567 return ERR_PTR(-ENOMEM);
568
569 sz = sizeof(*qp);
570 sg_list_sz = 0;
571 if (init_attr->srq) {
572 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
573
574 if (srq->rq.max_sge > 1)
575 sg_list_sz = sizeof(*qp->r_sg_list) *
576 (srq->rq.max_sge - 1);
577 } else if (init_attr->cap.max_recv_sge > 1)
578 sg_list_sz = sizeof(*qp->r_sg_list) *
579 (init_attr->cap.max_recv_sge - 1);
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800580 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800581 if (!qp)
582 goto bail_swq;
583
584 RCU_INIT_POINTER(qp->next, NULL);
585
586 /*
587 * Driver needs to set up it's private QP structure and do any
588 * initialization that is needed.
589 */
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800590 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800591 if (!priv)
592 goto bail_qp;
593 qp->priv = priv;
594 qp->timeout_jiffies =
595 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
596 1000UL);
597 if (init_attr->srq) {
598 sz = 0;
599 } else {
600 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
601 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
602 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
603 sizeof(struct rvt_rwqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800604 if (udata)
605 qp->r_rq.wq = vmalloc_user(
606 sizeof(struct rvt_rwq) +
607 qp->r_rq.size * sz);
608 else if (gfp == GFP_NOIO)
609 qp->r_rq.wq = __vmalloc(
610 sizeof(struct rvt_rwq) +
611 qp->r_rq.size * sz,
612 gfp, PAGE_KERNEL);
613 else
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800614 qp->r_rq.wq = vmalloc_node(
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800615 sizeof(struct rvt_rwq) +
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800616 qp->r_rq.size * sz,
617 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800618 if (!qp->r_rq.wq)
619 goto bail_driver_priv;
620 }
621
622 /*
623 * ib_create_qp() will initialize qp->ibqp
624 * except for qp->ibqp.qp_num.
625 */
626 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800627 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800628 spin_lock_init(&qp->s_lock);
629 spin_lock_init(&qp->r_rq.lock);
630 atomic_set(&qp->refcount, 0);
631 init_waitqueue_head(&qp->wait);
632 init_timer(&qp->s_timer);
633 qp->s_timer.data = (unsigned long)qp;
634 INIT_LIST_HEAD(&qp->rspwait);
635 qp->state = IB_QPS_RESET;
636 qp->s_wq = swq;
637 qp->s_size = init_attr->cap.max_send_wr + 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800638 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800639 qp->s_max_sge = init_attr->cap.max_send_sge;
640 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
641 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
642
643 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
644 init_attr->qp_type,
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800645 init_attr->port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800646 if (err < 0) {
647 ret = ERR_PTR(err);
648 goto bail_rq_wq;
649 }
650 qp->ibqp.qp_num = err;
651 qp->port_num = init_attr->port_num;
Dennis Dalessandro5a9cf6f2016-01-22 12:50:24 -0800652 rvt_reset_qp(rdi, qp, init_attr->qp_type);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800653 break;
654
655 default:
656 /* Don't support raw QPs */
657 return ERR_PTR(-EINVAL);
658 }
659
660 init_attr->cap.max_inline_data = 0;
661
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800662 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800663 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800664 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800665 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800666 if (udata && udata->outlen >= sizeof(__u64)) {
667 if (!qp->r_rq.wq) {
668 __u64 offset = 0;
669
670 err = ib_copy_to_udata(udata, &offset,
671 sizeof(offset));
672 if (err) {
673 ret = ERR_PTR(err);
674 goto bail_qpn;
675 }
676 } else {
677 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
678
679 qp->ip = rvt_create_mmap_info(rdi, s,
680 ibpd->uobject->context,
681 qp->r_rq.wq);
682 if (!qp->ip) {
683 ret = ERR_PTR(-ENOMEM);
684 goto bail_qpn;
685 }
686
687 err = ib_copy_to_udata(udata, &qp->ip->offset,
688 sizeof(qp->ip->offset));
689 if (err) {
690 ret = ERR_PTR(err);
691 goto bail_ip;
692 }
693 }
694 }
695
696 spin_lock(&rdi->n_qps_lock);
697 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
698 spin_unlock(&rdi->n_qps_lock);
699 ret = ERR_PTR(-ENOMEM);
700 goto bail_ip;
701 }
702
703 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800704 /*
705 * Maintain a busy_jiffies variable that will be added to the timeout
706 * period in mod_retry_timer and add_retry_timer. This busy jiffies
707 * is scaled by the number of rc qps created for the device to reduce
708 * the number of timeouts occurring when there is a large number of
709 * qps. busy_jiffies is incremented every rc qp scaling interval.
710 * The scaling interval is selected based on extensive performance
711 * evaluation of targeted workloads.
712 */
713 if (init_attr->qp_type == IB_QPT_RC) {
714 rdi->n_rc_qps++;
715 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
716 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800717 spin_unlock(&rdi->n_qps_lock);
718
719 if (qp->ip) {
720 spin_lock_irq(&rdi->pending_lock);
721 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
722 spin_unlock_irq(&rdi->pending_lock);
723 }
724
725 ret = &qp->ibqp;
726
727 /*
728 * We have our QP and its good, now keep track of what types of opcodes
729 * can be processed on this QP. We do this by keeping track of what the
730 * 3 high order bits of the opcode are.
731 */
732 switch (init_attr->qp_type) {
733 case IB_QPT_SMI:
734 case IB_QPT_GSI:
735 case IB_QPT_UD:
736 qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & RVT_OPCODE_QP_MASK;
737 break;
738 case IB_QPT_RC:
739 qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & RVT_OPCODE_QP_MASK;
740 break;
741 case IB_QPT_UC:
742 qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & RVT_OPCODE_QP_MASK;
743 break;
744 default:
745 ret = ERR_PTR(-EINVAL);
746 goto bail_ip;
747 }
748
749 return ret;
750
751bail_ip:
752 kref_put(&qp->ip->ref, rvt_release_mmap_info);
753
754bail_qpn:
755 free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
756
757bail_rq_wq:
758 vfree(qp->r_rq.wq);
759
760bail_driver_priv:
761 rdi->driver_f.qp_priv_free(rdi, qp);
762
763bail_qp:
764 kfree(qp);
765
766bail_swq:
767 vfree(swq);
768
769 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800770}
771
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800772/**
773 * rvt_clear_mr_refs - Drop help mr refs
774 * @qp: rvt qp data structure
775 * @clr_sends: If shoudl clear send side or not
776 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800777void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
778{
779 unsigned n;
780
781 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
782 rvt_put_ss(&qp->s_rdma_read_sge);
783
784 rvt_put_ss(&qp->r_sge);
785
786 if (clr_sends) {
787 while (qp->s_last != qp->s_head) {
788 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
789 unsigned i;
790
791 for (i = 0; i < wqe->wr.num_sge; i++) {
792 struct rvt_sge *sge = &wqe->sg_list[i];
793
794 rvt_put_mr(sge->mr);
795 }
796 if (qp->ibqp.qp_type == IB_QPT_UD ||
797 qp->ibqp.qp_type == IB_QPT_SMI ||
798 qp->ibqp.qp_type == IB_QPT_GSI)
799 atomic_dec(&ibah_to_rvtah(
800 wqe->ud_wr.ah)->refcount);
801 if (++qp->s_last >= qp->s_size)
802 qp->s_last = 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800803 smp_wmb(); /* see qp_set_savail */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800804 }
805 if (qp->s_rdma_mr) {
806 rvt_put_mr(qp->s_rdma_mr);
807 qp->s_rdma_mr = NULL;
808 }
809 }
810
811 if (qp->ibqp.qp_type != IB_QPT_RC)
812 return;
813
814 for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
815 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
816
817 if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
818 e->rdma_sge.mr) {
819 rvt_put_mr(e->rdma_sge.mr);
820 e->rdma_sge.mr = NULL;
821 }
822 }
823}
824EXPORT_SYMBOL(rvt_clear_mr_refs);
825
826/**
827 * rvt_error_qp - put a QP into the error state
828 * @qp: the QP to put into the error state
829 * @err: the receive completion error to signal if a RWQE is active
830 *
831 * Flushes both send and receive work queues.
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800832 *
833 * Return: true if last WQE event should be generated.
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800834 * The QP r_lock and s_lock should be held and interrupts disabled.
835 * If we are already in error state, just return.
836 */
837int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
838{
839 struct ib_wc wc;
840 int ret = 0;
841 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
842
843 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
844 goto bail;
845
846 qp->state = IB_QPS_ERR;
847
848 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
849 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
850 del_timer(&qp->s_timer);
851 }
852
853 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
854 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
855
856 rdi->driver_f.notify_error_qp(qp);
857
858 /* Schedule the sending tasklet to drain the send work queue. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800859 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800860 rdi->driver_f.schedule_send(qp);
861
862 rvt_clear_mr_refs(qp, 0);
863
864 memset(&wc, 0, sizeof(wc));
865 wc.qp = &qp->ibqp;
866 wc.opcode = IB_WC_RECV;
867
868 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
869 wc.wr_id = qp->r_wr_id;
870 wc.status = err;
871 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
872 }
873 wc.status = IB_WC_WR_FLUSH_ERR;
874
875 if (qp->r_rq.wq) {
876 struct rvt_rwq *wq;
877 u32 head;
878 u32 tail;
879
880 spin_lock(&qp->r_rq.lock);
881
882 /* sanity check pointers before trusting them */
883 wq = qp->r_rq.wq;
884 head = wq->head;
885 if (head >= qp->r_rq.size)
886 head = 0;
887 tail = wq->tail;
888 if (tail >= qp->r_rq.size)
889 tail = 0;
890 while (tail != head) {
891 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
892 if (++tail >= qp->r_rq.size)
893 tail = 0;
894 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
895 }
896 wq->tail = tail;
897
898 spin_unlock(&qp->r_rq.lock);
899 } else if (qp->ibqp.event_handler) {
900 ret = 1;
901 }
902
903bail:
904 return ret;
905}
906EXPORT_SYMBOL(rvt_error_qp);
907
908/*
909 * Put the QP into the hash table.
910 * The hash table holds a reference to the QP.
911 */
912static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
913{
914 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
915 unsigned long flags;
916
917 atomic_inc(&qp->refcount);
918 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
919
920 if (qp->ibqp.qp_num <= 1) {
921 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
922 } else {
923 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
924
925 qp->next = rdi->qp_dev->qp_table[n];
926 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
927 trace_rvt_qpinsert(qp, n);
928 }
929
930 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
931}
932
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800933/**
934 * rvt_remove_qp - remove qp form table
935 * @rdi: rvt dev struct
936 * @qp: qp to remove
937 *
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800938 * Remove the QP from the table so it can't be found asynchronously by
939 * the receive routine.
940 */
941void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
942{
943 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
944 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
945 unsigned long flags;
946 int removed = 1;
947
948 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
949
950 if (rcu_dereference_protected(rvp->qp[0],
951 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
952 RCU_INIT_POINTER(rvp->qp[0], NULL);
953 } else if (rcu_dereference_protected(rvp->qp[1],
954 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
955 RCU_INIT_POINTER(rvp->qp[1], NULL);
956 } else {
957 struct rvt_qp *q;
958 struct rvt_qp __rcu **qpp;
959
960 removed = 0;
961 qpp = &rdi->qp_dev->qp_table[n];
962 for (; (q = rcu_dereference_protected(*qpp,
963 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
964 qpp = &q->next) {
965 if (q == qp) {
966 RCU_INIT_POINTER(*qpp,
967 rcu_dereference_protected(qp->next,
968 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
969 removed = 1;
970 trace_rvt_qpremove(qp, n);
971 break;
972 }
973 }
974 }
975
976 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
977 if (removed) {
978 synchronize_rcu();
979 if (atomic_dec_and_test(&qp->refcount))
980 wake_up(&qp->wait);
981 }
982}
983EXPORT_SYMBOL(rvt_remove_qp);
984
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800985/**
986 * qib_modify_qp - modify the attributes of a queue pair
987 * @ibqp: the queue pair who's attributes we're modifying
988 * @attr: the new attributes
989 * @attr_mask: the mask of attributes to modify
990 * @udata: user data for libibverbs.so
991 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800992 * Return: 0 on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800993 */
994int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
995 int attr_mask, struct ib_udata *udata)
996{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800997 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
998 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
999 enum ib_qp_state cur_state, new_state;
1000 struct ib_event ev;
1001 int lastwqe = 0;
1002 int mig = 0;
1003 int pmtu = 0; /* for gcc warning only */
1004 enum rdma_link_layer link;
1005
1006 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1007
1008 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001009 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001010 spin_lock(&qp->s_lock);
1011
1012 cur_state = attr_mask & IB_QP_CUR_STATE ?
1013 attr->cur_qp_state : qp->state;
1014 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1015
1016 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1017 attr_mask, link))
1018 goto inval;
1019
Ira Weinye85ec332016-01-22 13:04:38 -08001020 if (rdi->driver_f.check_modify_qp &&
1021 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1022 goto inval;
1023
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001024 if (attr_mask & IB_QP_AV) {
1025 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1026 goto inval;
1027 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1028 goto inval;
1029 }
1030
1031 if (attr_mask & IB_QP_ALT_PATH) {
1032 if (attr->alt_ah_attr.dlid >=
1033 be16_to_cpu(IB_MULTICAST_LID_BASE))
1034 goto inval;
1035 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1036 goto inval;
1037 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1038 goto inval;
1039 }
1040
1041 if (attr_mask & IB_QP_PKEY_INDEX)
1042 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1043 goto inval;
1044
1045 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1046 if (attr->min_rnr_timer > 31)
1047 goto inval;
1048
1049 if (attr_mask & IB_QP_PORT)
1050 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1051 qp->ibqp.qp_type == IB_QPT_GSI ||
1052 attr->port_num == 0 ||
1053 attr->port_num > ibqp->device->phys_port_cnt)
1054 goto inval;
1055
1056 if (attr_mask & IB_QP_DEST_QPN)
1057 if (attr->dest_qp_num > RVT_QPN_MASK)
1058 goto inval;
1059
1060 if (attr_mask & IB_QP_RETRY_CNT)
1061 if (attr->retry_cnt > 7)
1062 goto inval;
1063
1064 if (attr_mask & IB_QP_RNR_RETRY)
1065 if (attr->rnr_retry > 7)
1066 goto inval;
1067
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001068 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001069 * Don't allow invalid path_mtu values. OK to set greater
1070 * than the active mtu (or even the max_cap, if we have tuned
1071 * that to a small mtu. We'll set qp->path_mtu
1072 * to the lesser of requested attribute mtu and active,
1073 * for packetizing messages.
1074 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001075 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001076 if (attr_mask & IB_QP_PATH_MTU) {
1077 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1078 if (pmtu < 0)
1079 goto inval;
1080 }
1081
1082 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1083 if (attr->path_mig_state == IB_MIG_REARM) {
1084 if (qp->s_mig_state == IB_MIG_ARMED)
1085 goto inval;
1086 if (new_state != IB_QPS_RTS)
1087 goto inval;
1088 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1089 if (qp->s_mig_state == IB_MIG_REARM)
1090 goto inval;
1091 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1092 goto inval;
1093 if (qp->s_mig_state == IB_MIG_ARMED)
1094 mig = 1;
1095 } else {
1096 goto inval;
1097 }
1098 }
1099
1100 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1101 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1102 goto inval;
1103
1104 switch (new_state) {
1105 case IB_QPS_RESET:
1106 if (qp->state != IB_QPS_RESET)
1107 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1108 break;
1109
1110 case IB_QPS_RTR:
1111 /* Allow event to re-trigger if QP set to RTR more than once */
1112 qp->r_flags &= ~RVT_R_COMM_EST;
1113 qp->state = new_state;
1114 break;
1115
1116 case IB_QPS_SQD:
1117 qp->s_draining = qp->s_last != qp->s_cur;
1118 qp->state = new_state;
1119 break;
1120
1121 case IB_QPS_SQE:
1122 if (qp->ibqp.qp_type == IB_QPT_RC)
1123 goto inval;
1124 qp->state = new_state;
1125 break;
1126
1127 case IB_QPS_ERR:
1128 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1129 break;
1130
1131 default:
1132 qp->state = new_state;
1133 break;
1134 }
1135
1136 if (attr_mask & IB_QP_PKEY_INDEX)
1137 qp->s_pkey_index = attr->pkey_index;
1138
1139 if (attr_mask & IB_QP_PORT)
1140 qp->port_num = attr->port_num;
1141
1142 if (attr_mask & IB_QP_DEST_QPN)
1143 qp->remote_qpn = attr->dest_qp_num;
1144
1145 if (attr_mask & IB_QP_SQ_PSN) {
1146 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1147 qp->s_psn = qp->s_next_psn;
1148 qp->s_sending_psn = qp->s_next_psn;
1149 qp->s_last_psn = qp->s_next_psn - 1;
1150 qp->s_sending_hpsn = qp->s_last_psn;
1151 }
1152
1153 if (attr_mask & IB_QP_RQ_PSN)
1154 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1155
1156 if (attr_mask & IB_QP_ACCESS_FLAGS)
1157 qp->qp_access_flags = attr->qp_access_flags;
1158
1159 if (attr_mask & IB_QP_AV) {
1160 qp->remote_ah_attr = attr->ah_attr;
1161 qp->s_srate = attr->ah_attr.static_rate;
1162 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1163 }
1164
1165 if (attr_mask & IB_QP_ALT_PATH) {
1166 qp->alt_ah_attr = attr->alt_ah_attr;
1167 qp->s_alt_pkey_index = attr->alt_pkey_index;
1168 }
1169
1170 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1171 qp->s_mig_state = attr->path_mig_state;
1172 if (mig) {
1173 qp->remote_ah_attr = qp->alt_ah_attr;
1174 qp->port_num = qp->alt_ah_attr.port_num;
1175 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001176 }
1177 }
1178
1179 if (attr_mask & IB_QP_PATH_MTU) {
1180 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1181 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001182 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001183 }
1184
1185 if (attr_mask & IB_QP_RETRY_CNT) {
1186 qp->s_retry_cnt = attr->retry_cnt;
1187 qp->s_retry = attr->retry_cnt;
1188 }
1189
1190 if (attr_mask & IB_QP_RNR_RETRY) {
1191 qp->s_rnr_retry_cnt = attr->rnr_retry;
1192 qp->s_rnr_retry = attr->rnr_retry;
1193 }
1194
1195 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1196 qp->r_min_rnr_timer = attr->min_rnr_timer;
1197
1198 if (attr_mask & IB_QP_TIMEOUT) {
1199 qp->timeout = attr->timeout;
1200 qp->timeout_jiffies =
1201 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1202 1000UL);
1203 }
1204
1205 if (attr_mask & IB_QP_QKEY)
1206 qp->qkey = attr->qkey;
1207
1208 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1209 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1210
1211 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1212 qp->s_max_rd_atomic = attr->max_rd_atomic;
1213
Ira Weinye85ec332016-01-22 13:04:38 -08001214 if (rdi->driver_f.modify_qp)
1215 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1216
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001217 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001218 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001219 spin_unlock_irq(&qp->r_lock);
1220
1221 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1222 rvt_insert_qp(rdi, qp);
1223
1224 if (lastwqe) {
1225 ev.device = qp->ibqp.device;
1226 ev.element.qp = &qp->ibqp;
1227 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1228 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1229 }
1230 if (mig) {
1231 ev.device = qp->ibqp.device;
1232 ev.element.qp = &qp->ibqp;
1233 ev.event = IB_EVENT_PATH_MIG;
1234 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1235 }
1236 return 0;
1237
1238inval:
1239 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001240 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001241 spin_unlock_irq(&qp->r_lock);
1242 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001243}
1244
1245/**
1246 * rvt_destroy_qp - destroy a queue pair
1247 * @ibqp: the queue pair to destroy
1248 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001249 * Note that this can be called while the QP is actively sending or
1250 * receiving!
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001251 *
1252 * Return: 0 on success.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001253 */
1254int rvt_destroy_qp(struct ib_qp *ibqp)
1255{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001256 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1257 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001258
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001259 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001260 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001261 spin_lock(&qp->s_lock);
1262 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1263 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001264 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001265 spin_unlock_irq(&qp->r_lock);
1266
1267 /* qpn is now available for use again */
1268 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1269
1270 spin_lock(&rdi->n_qps_lock);
1271 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001272 if (qp->ibqp.qp_type == IB_QPT_RC) {
1273 rdi->n_rc_qps--;
1274 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1275 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001276 spin_unlock(&rdi->n_qps_lock);
1277
1278 if (qp->ip)
1279 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1280 else
1281 vfree(qp->r_rq.wq);
1282 vfree(qp->s_wq);
1283 rdi->driver_f.qp_priv_free(rdi, qp);
1284 kfree(qp);
1285 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001286}
1287
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001288/**
1289 * rvt_query_qp - query an ipbq
1290 * @ibqp: IB qp to query
1291 * @attr: attr struct to fill in
1292 * @attr_mask: attr mask ignored
1293 * @init_attr: struct to fill in
1294 *
1295 * Return: always 0
1296 */
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001297int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1298 int attr_mask, struct ib_qp_init_attr *init_attr)
1299{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001300 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1301 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1302
1303 attr->qp_state = qp->state;
1304 attr->cur_qp_state = attr->qp_state;
1305 attr->path_mtu = qp->path_mtu;
1306 attr->path_mig_state = qp->s_mig_state;
1307 attr->qkey = qp->qkey;
1308 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1309 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1310 attr->dest_qp_num = qp->remote_qpn;
1311 attr->qp_access_flags = qp->qp_access_flags;
1312 attr->cap.max_send_wr = qp->s_size - 1;
1313 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1314 attr->cap.max_send_sge = qp->s_max_sge;
1315 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1316 attr->cap.max_inline_data = 0;
1317 attr->ah_attr = qp->remote_ah_attr;
1318 attr->alt_ah_attr = qp->alt_ah_attr;
1319 attr->pkey_index = qp->s_pkey_index;
1320 attr->alt_pkey_index = qp->s_alt_pkey_index;
1321 attr->en_sqd_async_notify = 0;
1322 attr->sq_draining = qp->s_draining;
1323 attr->max_rd_atomic = qp->s_max_rd_atomic;
1324 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1325 attr->min_rnr_timer = qp->r_min_rnr_timer;
1326 attr->port_num = qp->port_num;
1327 attr->timeout = qp->timeout;
1328 attr->retry_cnt = qp->s_retry_cnt;
1329 attr->rnr_retry = qp->s_rnr_retry_cnt;
1330 attr->alt_port_num = qp->alt_ah_attr.port_num;
1331 attr->alt_timeout = qp->alt_timeout;
1332
1333 init_attr->event_handler = qp->ibqp.event_handler;
1334 init_attr->qp_context = qp->ibqp.qp_context;
1335 init_attr->send_cq = qp->ibqp.send_cq;
1336 init_attr->recv_cq = qp->ibqp.recv_cq;
1337 init_attr->srq = qp->ibqp.srq;
1338 init_attr->cap = attr->cap;
1339 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1340 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1341 else
1342 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1343 init_attr->qp_type = qp->ibqp.qp_type;
1344 init_attr->port_num = qp->port_num;
1345 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001346}
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001347
1348/**
1349 * rvt_post_receive - post a receive on a QP
1350 * @ibqp: the QP to post the receive on
1351 * @wr: the WR to post
1352 * @bad_wr: the first bad WR is put here
1353 *
1354 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001355 *
1356 * Return: 0 on success otherwise errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001357 */
1358int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1359 struct ib_recv_wr **bad_wr)
1360{
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001361 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1362 struct rvt_rwq *wq = qp->r_rq.wq;
1363 unsigned long flags;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001364
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001365 /* Check that state is OK to post receive. */
1366 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1367 *bad_wr = wr;
1368 return -EINVAL;
1369 }
1370
1371 for (; wr; wr = wr->next) {
1372 struct rvt_rwqe *wqe;
1373 u32 next;
1374 int i;
1375
1376 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1377 *bad_wr = wr;
1378 return -EINVAL;
1379 }
1380
1381 spin_lock_irqsave(&qp->r_rq.lock, flags);
1382 next = wq->head + 1;
1383 if (next >= qp->r_rq.size)
1384 next = 0;
1385 if (next == wq->tail) {
1386 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1387 *bad_wr = wr;
1388 return -ENOMEM;
1389 }
1390
1391 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1392 wqe->wr_id = wr->wr_id;
1393 wqe->num_sge = wr->num_sge;
1394 for (i = 0; i < wr->num_sge; i++)
1395 wqe->sg_list[i] = wr->sg_list[i];
1396 /* Make sure queue entry is written before the head index. */
1397 smp_wmb();
1398 wq->head = next;
1399 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1400 }
1401 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001402}
1403
1404/**
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001405 * qp_get_savail - return number of avail send entries
1406 *
1407 * @qp - the qp
1408 *
1409 * This assumes the s_hlock is held but the s_last
1410 * qp variable is uncontrolled.
1411 */
1412static inline u32 qp_get_savail(struct rvt_qp *qp)
1413{
1414 u32 slast;
1415 u32 ret;
1416
1417 smp_read_barrier_depends(); /* see rc.c */
1418 slast = ACCESS_ONCE(qp->s_last);
1419 if (qp->s_head >= slast)
1420 ret = qp->s_size - (qp->s_head - slast);
1421 else
1422 ret = slast - qp->s_head;
1423 return ret - 1;
1424}
1425
1426/**
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001427 * rvt_post_one_wr - post one RC, UC, or UD send work request
1428 * @qp: the QP to post on
1429 * @wr: the work request to send
1430 */
1431static int rvt_post_one_wr(struct rvt_qp *qp, struct ib_send_wr *wr)
1432{
1433 struct rvt_swqe *wqe;
1434 u32 next;
1435 int i;
1436 int j;
1437 int acc;
1438 struct rvt_lkey_table *rkt;
1439 struct rvt_pd *pd;
1440 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001441 u8 log_pmtu;
1442 int ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001443
1444 /* IB spec says that num_sge == 0 is OK. */
1445 if (unlikely(wr->num_sge > qp->s_max_sge))
1446 return -EINVAL;
1447
1448 /*
1449 * Don't allow RDMA reads or atomic operations on UC or
1450 * undefined operations.
1451 * Make sure buffer is large enough to hold the result for atomics.
1452 */
1453 if (qp->ibqp.qp_type == IB_QPT_UC) {
1454 if ((unsigned)wr->opcode >= IB_WR_RDMA_READ)
1455 return -EINVAL;
1456 } else if (qp->ibqp.qp_type != IB_QPT_RC) {
1457 /* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
1458 if (wr->opcode != IB_WR_SEND &&
1459 wr->opcode != IB_WR_SEND_WITH_IMM)
1460 return -EINVAL;
1461 /* Check UD destination address PD */
1462 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1463 return -EINVAL;
1464 } else if ((unsigned)wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
1465 return -EINVAL;
1466 } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
1467 (wr->num_sge == 0 ||
1468 wr->sg_list[0].length < sizeof(u64) ||
1469 wr->sg_list[0].addr & (sizeof(u64) - 1))) {
1470 return -EINVAL;
1471 } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
1472 return -EINVAL;
1473 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001474 /* check for avail */
1475 if (unlikely(!qp->s_avail)) {
1476 qp->s_avail = qp_get_savail(qp);
Harish Chegondie16689e2016-02-14 12:10:12 -08001477 if (WARN_ON(qp->s_avail > (qp->s_size - 1)))
1478 rvt_pr_err(rdi,
1479 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1480 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1481 qp->s_head, qp->s_tail, qp->s_cur,
1482 qp->s_acked, qp->s_last);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001483 if (!qp->s_avail)
1484 return -ENOMEM;
1485 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001486 next = qp->s_head + 1;
1487 if (next >= qp->s_size)
1488 next = 0;
Ira Weiny60c30f52016-02-03 14:14:45 -08001489
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001490 rkt = &rdi->lkey_table;
1491 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1492 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1493
1494 if (qp->ibqp.qp_type != IB_QPT_UC &&
1495 qp->ibqp.qp_type != IB_QPT_RC)
1496 memcpy(&wqe->ud_wr, ud_wr(wr), sizeof(wqe->ud_wr));
1497 else if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
1498 wr->opcode == IB_WR_RDMA_WRITE ||
1499 wr->opcode == IB_WR_RDMA_READ)
1500 memcpy(&wqe->rdma_wr, rdma_wr(wr), sizeof(wqe->rdma_wr));
1501 else if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1502 wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1503 memcpy(&wqe->atomic_wr, atomic_wr(wr), sizeof(wqe->atomic_wr));
1504 else
1505 memcpy(&wqe->wr, wr, sizeof(wqe->wr));
1506
1507 wqe->length = 0;
1508 j = 0;
1509 if (wr->num_sge) {
1510 acc = wr->opcode >= IB_WR_RDMA_READ ?
1511 IB_ACCESS_LOCAL_WRITE : 0;
1512 for (i = 0; i < wr->num_sge; i++) {
1513 u32 length = wr->sg_list[i].length;
1514 int ok;
1515
1516 if (length == 0)
1517 continue;
1518 ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1519 &wr->sg_list[i], acc);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001520 if (!ok) {
1521 ret = -EINVAL;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001522 goto bail_inval_free;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001523 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001524 wqe->length += length;
1525 j++;
1526 }
1527 wqe->wr.num_sge = j;
1528 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001529
1530 /* general part of wqe valid - allow for driver checks */
1531 if (rdi->driver_f.check_send_wqe) {
1532 ret = rdi->driver_f.check_send_wqe(qp, wqe);
1533 if (ret)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001534 goto bail_inval_free;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001535 }
1536
1537 log_pmtu = qp->log_pmtu;
1538 if (qp->ibqp.qp_type != IB_QPT_UC &&
1539 qp->ibqp.qp_type != IB_QPT_RC) {
1540 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1541
1542 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001543 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1544 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001545
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001546 wqe->ssn = qp->s_ssn++;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001547 wqe->psn = qp->s_next_psn;
1548 wqe->lpsn = wqe->psn +
1549 (wqe->length ? ((wqe->length - 1) >> log_pmtu) : 0);
1550 qp->s_next_psn = wqe->lpsn + 1;
Harish Chegondie16689e2016-02-14 12:10:12 -08001551 trace_rvt_post_one_wr(qp, wqe);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001552 smp_wmb(); /* see request builders */
1553 qp->s_avail--;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001554 qp->s_head = next;
1555
1556 return 0;
1557
1558bail_inval_free:
1559 /* release mr holds */
1560 while (j) {
1561 struct rvt_sge *sge = &wqe->sg_list[--j];
1562
1563 rvt_put_mr(sge->mr);
1564 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001565 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001566}
1567
1568/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001569 * rvt_post_send - post a send on a QP
1570 * @ibqp: the QP to post the send on
1571 * @wr: the list of work requests to post
1572 * @bad_wr: the first bad WR is put here
1573 *
1574 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001575 *
1576 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001577 */
1578int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1579 struct ib_send_wr **bad_wr)
1580{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001581 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1582 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1583 unsigned long flags = 0;
1584 int call_send;
1585 unsigned nreq = 0;
1586 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001587
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001588 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001589
1590 /*
1591 * Ensure QP state is such that we can send. If not bail out early,
1592 * there is no need to do this every time we post a send.
1593 */
1594 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001595 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001596 return -EINVAL;
1597 }
1598
1599 /*
1600 * If the send queue is empty, and we only have a single WR then just go
1601 * ahead and kick the send engine into gear. Otherwise we will always
1602 * just schedule the send to happen later.
1603 */
1604 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1605
1606 for (; wr; wr = wr->next) {
1607 err = rvt_post_one_wr(qp, wr);
1608 if (unlikely(err)) {
1609 *bad_wr = wr;
1610 goto bail;
1611 }
1612 nreq++;
1613 }
1614bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001615 spin_unlock_irqrestore(&qp->s_hlock, flags);
1616 if (nreq) {
1617 if (call_send)
1618 rdi->driver_f.schedule_send_no_lock(qp);
1619 else
1620 rdi->driver_f.do_send(qp);
1621 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001622 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001623}
1624
1625/**
1626 * rvt_post_srq_receive - post a receive on a shared receive queue
1627 * @ibsrq: the SRQ to post the receive on
1628 * @wr: the list of work requests to post
1629 * @bad_wr: A pointer to the first WR to cause a problem is put here
1630 *
1631 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001632 *
1633 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001634 */
1635int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1636 struct ib_recv_wr **bad_wr)
1637{
Jubin Johnb8f881b2016-02-03 14:14:36 -08001638 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1639 struct rvt_rwq *wq;
1640 unsigned long flags;
1641
1642 for (; wr; wr = wr->next) {
1643 struct rvt_rwqe *wqe;
1644 u32 next;
1645 int i;
1646
1647 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1648 *bad_wr = wr;
1649 return -EINVAL;
1650 }
1651
1652 spin_lock_irqsave(&srq->rq.lock, flags);
1653 wq = srq->rq.wq;
1654 next = wq->head + 1;
1655 if (next >= srq->rq.size)
1656 next = 0;
1657 if (next == wq->tail) {
1658 spin_unlock_irqrestore(&srq->rq.lock, flags);
1659 *bad_wr = wr;
1660 return -ENOMEM;
1661 }
1662
1663 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1664 wqe->wr_id = wr->wr_id;
1665 wqe->num_sge = wr->num_sge;
1666 for (i = 0; i < wr->num_sge; i++)
1667 wqe->sg_list[i] = wr->sg_list[i];
1668 /* Make sure queue entry is written before the head index. */
1669 smp_wmb();
1670 wq->head = next;
1671 spin_unlock_irqrestore(&srq->rq.lock, flags);
1672 }
1673 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001674}
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001675
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001676/** rvt_free_qpn - Free a qpn from the bit map
1677 * @qpt: QP table
1678 * @qpn: queue pair number to free
1679 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001680void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1681{
1682 struct rvt_qpn_map *map;
1683
1684 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1685 if (map->page)
1686 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1687}
1688EXPORT_SYMBOL(rvt_free_qpn);
1689
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001690/**
1691 * rvt_dec_qp_cnt - decrement qp count
1692 * rdi: rvt dev struct
1693 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001694void rvt_dec_qp_cnt(struct rvt_dev_info *rdi)
1695{
1696 spin_lock(&rdi->n_qps_lock);
1697 rdi->n_qps_allocated--;
1698 spin_unlock(&rdi->n_qps_lock);
1699}
1700EXPORT_SYMBOL(rvt_dec_qp_cnt);