]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/infiniband/sw/rdmavt/qp.c
IB/rdmavt: Remove signal_supported and comments
[mirror_ubuntu-bionic-kernel.git] / drivers / infiniband / sw / rdmavt / qp.c
CommitLineData
b518d3e6 1/*
fe314195 2 * Copyright(c) 2016 Intel Corporation.
b518d3e6
DD
3 *
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
3b0b3fb3 48#include <linux/hash.h>
0acb0cc7
DD
49#include <linux/bitops.h>
50#include <linux/lockdep.h>
515667f8
DD
51#include <linux/vmalloc.h>
52#include <linux/slab.h>
53#include <rdma/ib_verbs.h>
b518d3e6 54#include "qp.h"
515667f8 55#include "vt.h"
3b0b3fb3 56#include "trace.h"
b518d3e6 57
bfbac097
DD
58/*
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
d2b8d4da
MM
79static void get_map_page(struct rvt_qpn_table *qpt,
80 struct rvt_qpn_map *map,
81 gfp_t gfp)
0acb0cc7 82{
d2b8d4da 83 unsigned long page = get_zeroed_page(gfp);
0acb0cc7
DD
84
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
fef2efd6 107 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
0acb0cc7
DD
108 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);
fef2efd6 133 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
0acb0cc7 134 if (!map->page) {
d2b8d4da 135 get_map_page(qpt, map, GFP_KERNEL);
0acb0cc7
DD
136 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
90793f71
DD
165/**
166 * rvt_driver_qp_init - Init driver qp resources
167 * @rdi: rvt dev strucutre
168 *
169 * Return: 0 on success
170 */
0acb0cc7
DD
171int rvt_driver_qp_init(struct rvt_dev_info *rdi)
172{
173 int i;
174 int ret = -ENOMEM;
175
0acb0cc7
DD
176 if (!rdi->dparms.qp_table_size)
177 return -EINVAL;
178
179 /*
180 * If driver is not doing any QP allocation then make sure it is
181 * providing the necessary QP functions.
182 */
515667f8
DD
183 if (!rdi->driver_f.free_all_qps ||
184 !rdi->driver_f.qp_priv_alloc ||
185 !rdi->driver_f.qp_priv_free ||
186 !rdi->driver_f.notify_qp_reset)
0acb0cc7
DD
187 return -EINVAL;
188
189 /* allocate parent object */
d1b697b6
MH
190 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
191 rdi->dparms.node);
0acb0cc7
DD
192 if (!rdi->qp_dev)
193 return -ENOMEM;
194
195 /* allocate hash table */
196 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
197 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
198 rdi->qp_dev->qp_table =
d1b697b6
MH
199 kmalloc_node(rdi->qp_dev->qp_table_size *
200 sizeof(*rdi->qp_dev->qp_table),
201 GFP_KERNEL, rdi->dparms.node);
0acb0cc7
DD
202 if (!rdi->qp_dev->qp_table)
203 goto no_qp_table;
204
205 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
206 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
207
208 spin_lock_init(&rdi->qp_dev->qpt_lock);
209
210 /* initialize qpn map */
211 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
212 goto fail_table;
213
515667f8
DD
214 spin_lock_init(&rdi->n_qps_lock);
215
216 return 0;
0acb0cc7
DD
217
218fail_table:
219 kfree(rdi->qp_dev->qp_table);
220 free_qpn_table(&rdi->qp_dev->qpn_table);
221
222no_qp_table:
223 kfree(rdi->qp_dev);
224
225 return ret;
226}
227
228/**
229 * free_all_qps - check for QPs still in use
230 * @qpt: the QP table to empty
231 *
232 * There should not be any QPs still in use.
233 * Free memory for table.
234 */
515667f8 235static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
0acb0cc7
DD
236{
237 unsigned long flags;
238 struct rvt_qp *qp;
239 unsigned n, qp_inuse = 0;
240 spinlock_t *ql; /* work around too long line below */
241
515667f8
DD
242 if (rdi->driver_f.free_all_qps)
243 qp_inuse = rdi->driver_f.free_all_qps(rdi);
0acb0cc7 244
4e74080b
DD
245 qp_inuse += rvt_mcast_tree_empty(rdi);
246
0acb0cc7 247 if (!rdi->qp_dev)
515667f8 248 return qp_inuse;
0acb0cc7
DD
249
250 ql = &rdi->qp_dev->qpt_lock;
515667f8 251 spin_lock_irqsave(ql, flags);
0acb0cc7
DD
252 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
253 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
254 lockdep_is_held(ql));
255 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
515667f8
DD
256
257 for (; qp; qp = rcu_dereference_protected(qp->next,
258 lockdep_is_held(ql)))
0acb0cc7 259 qp_inuse++;
0acb0cc7
DD
260 }
261 spin_unlock_irqrestore(ql, flags);
262 synchronize_rcu();
263 return qp_inuse;
264}
265
90793f71
DD
266/**
267 * rvt_qp_exit - clean up qps on device exit
268 * @rdi: rvt dev structure
269 *
270 * Check for qp leaks and free resources.
271 */
0acb0cc7
DD
272void rvt_qp_exit(struct rvt_dev_info *rdi)
273{
515667f8 274 u32 qps_inuse = rvt_free_all_qps(rdi);
0acb0cc7 275
0acb0cc7
DD
276 if (qps_inuse)
277 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
278 qps_inuse);
279 if (!rdi->qp_dev)
280 return;
281
282 kfree(rdi->qp_dev->qp_table);
283 free_qpn_table(&rdi->qp_dev->qpn_table);
284 kfree(rdi->qp_dev);
285}
286
515667f8
DD
287static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
288 struct rvt_qpn_map *map, unsigned off)
289{
290 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
291}
292
f1badc71
DD
293/**
294 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
295 * IB_QPT_SMI/IB_QPT_GSI
296 *@rdi: rvt device info structure
297 *@qpt: queue pair number table pointer
298 *@port_num: IB port number, 1 based, comes from core
299 *
300 * Return: The queue pair number
515667f8
DD
301 */
302static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
f1badc71 303 enum ib_qp_type type, u8 port_num, gfp_t gfp)
515667f8
DD
304{
305 u32 i, offset, max_scan, qpn;
306 struct rvt_qpn_map *map;
307 u32 ret;
308
309 if (rdi->driver_f.alloc_qpn)
b7b3cf44 310 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
515667f8
DD
311
312 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
313 unsigned n;
314
315 ret = type == IB_QPT_GSI;
f1badc71 316 n = 1 << (ret + 2 * (port_num - 1));
515667f8
DD
317 spin_lock(&qpt->lock);
318 if (qpt->flags & n)
319 ret = -EINVAL;
320 else
321 qpt->flags |= n;
322 spin_unlock(&qpt->lock);
323 goto bail;
324 }
325
326 qpn = qpt->last + qpt->incr;
327 if (qpn >= RVT_QPN_MAX)
328 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
329 /* offset carries bit 0 */
330 offset = qpn & RVT_BITS_PER_PAGE_MASK;
331 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
332 max_scan = qpt->nmaps - !offset;
333 for (i = 0;;) {
334 if (unlikely(!map->page)) {
d2b8d4da 335 get_map_page(qpt, map, gfp);
515667f8
DD
336 if (unlikely(!map->page))
337 break;
338 }
339 do {
340 if (!test_and_set_bit(offset, map->page)) {
341 qpt->last = qpn;
342 ret = qpn;
343 goto bail;
344 }
345 offset += qpt->incr;
346 /*
347 * This qpn might be bogus if offset >= BITS_PER_PAGE.
348 * That is OK. It gets re-assigned below
349 */
350 qpn = mk_qpn(qpt, map, offset);
351 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
352 /*
353 * In order to keep the number of pages allocated to a
354 * minimum, we scan the all existing pages before increasing
355 * the size of the bitmap table.
356 */
357 if (++i > max_scan) {
358 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
359 break;
360 map = &qpt->map[qpt->nmaps++];
361 /* start at incr with current bit 0 */
362 offset = qpt->incr | (offset & 1);
363 } else if (map < &qpt->map[qpt->nmaps]) {
364 ++map;
365 /* start at incr with current bit 0 */
366 offset = qpt->incr | (offset & 1);
367 } else {
368 map = &qpt->map[0];
369 /* wrap to first map page, invert bit 0 */
370 offset = qpt->incr | ((offset & 1) ^ 1);
371 }
372 /* there can be no bits at shift and below */
373 WARN_ON(offset & (rdi->dparms.qos_shift - 1));
374 qpn = mk_qpn(qpt, map, offset);
375 }
376
377 ret = -ENOMEM;
378
379bail:
380 return ret;
381}
382
383static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
384{
385 struct rvt_qpn_map *map;
386
387 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
388 if (map->page)
389 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
390}
391
392/**
393 * reset_qp - initialize the QP state to the reset state
394 * @qp: the QP to reset
395 * @type: the QP type
3b0b3fb3 396 * r and s lock are required to be held by the caller
515667f8 397 */
5a9cf6f2
DD
398void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
399 enum ib_qp_type type)
515667f8 400{
3b0b3fb3
DD
401 if (qp->state != IB_QPS_RESET) {
402 qp->state = IB_QPS_RESET;
403
404 /* Let drivers flush their waitlist */
405 rdi->driver_f.flush_qp_waiters(qp);
406 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
407 spin_unlock(&qp->s_lock);
46a80d62 408 spin_unlock(&qp->s_hlock);
3b0b3fb3
DD
409 spin_unlock_irq(&qp->r_lock);
410
411 /* Stop the send queue and the retry timer */
412 rdi->driver_f.stop_send_queue(qp);
3b0b3fb3
DD
413
414 /* Wait for things to stop */
415 rdi->driver_f.quiesce_qp(qp);
416
417 /* take qp out the hash and wait for it to be unused */
418 rvt_remove_qp(rdi, qp);
419 wait_event(qp->wait, !atomic_read(&qp->refcount));
420
421 /* grab the lock b/c it was locked at call time */
422 spin_lock_irq(&qp->r_lock);
46a80d62 423 spin_lock(&qp->s_hlock);
3b0b3fb3
DD
424 spin_lock(&qp->s_lock);
425
426 rvt_clear_mr_refs(qp, 1);
427 }
515667f8
DD
428
429 /*
3b0b3fb3
DD
430 * Let the driver do any tear down it needs to for a qp
431 * that has been reset
515667f8
DD
432 */
433 rdi->driver_f.notify_qp_reset(qp);
434
3b0b3fb3
DD
435 qp->remote_qpn = 0;
436 qp->qkey = 0;
437 qp->qp_access_flags = 0;
515667f8
DD
438 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
439 qp->s_hdrwords = 0;
440 qp->s_wqe = NULL;
441 qp->s_draining = 0;
442 qp->s_next_psn = 0;
443 qp->s_last_psn = 0;
444 qp->s_sending_psn = 0;
445 qp->s_sending_hpsn = 0;
446 qp->s_psn = 0;
447 qp->r_psn = 0;
448 qp->r_msn = 0;
449 if (type == IB_QPT_RC) {
450 qp->s_state = IB_OPCODE_RC_SEND_LAST;
451 qp->r_state = IB_OPCODE_RC_SEND_LAST;
452 } else {
453 qp->s_state = IB_OPCODE_UC_SEND_LAST;
454 qp->r_state = IB_OPCODE_UC_SEND_LAST;
455 }
456 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
457 qp->r_nak_state = 0;
458 qp->r_aflags = 0;
459 qp->r_flags = 0;
460 qp->s_head = 0;
461 qp->s_tail = 0;
462 qp->s_cur = 0;
463 qp->s_acked = 0;
464 qp->s_last = 0;
465 qp->s_ssn = 1;
466 qp->s_lsn = 0;
467 qp->s_mig_state = IB_MIG_MIGRATED;
468 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
469 qp->r_head_ack_queue = 0;
470 qp->s_tail_ack_queue = 0;
471 qp->s_num_rd_atomic = 0;
472 if (qp->r_rq.wq) {
473 qp->r_rq.wq->head = 0;
474 qp->r_rq.wq->tail = 0;
475 }
476 qp->r_sge.num_sge = 0;
477}
5a9cf6f2 478EXPORT_SYMBOL(rvt_reset_qp);
515667f8 479
b518d3e6
DD
480/**
481 * rvt_create_qp - create a queue pair for a device
482 * @ibpd: the protection domain who's device we create the queue pair for
483 * @init_attr: the attributes of the queue pair
484 * @udata: user data for libibverbs.so
485 *
515667f8
DD
486 * Queue pair creation is mostly an rvt issue. However, drivers have their own
487 * unique idea of what queue pair numbers mean. For instance there is a reserved
488 * range for PSM.
489 *
90793f71 490 * Return: the queue pair on success, otherwise returns an errno.
b518d3e6
DD
491 *
492 * Called by the ib_create_qp() core verbs function.
493 */
494struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
495 struct ib_qp_init_attr *init_attr,
496 struct ib_udata *udata)
497{
515667f8
DD
498 struct rvt_qp *qp;
499 int err;
500 struct rvt_swqe *swq = NULL;
501 size_t sz;
502 size_t sg_list_sz;
503 struct ib_qp *ret = ERR_PTR(-ENOMEM);
504 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
505 void *priv = NULL;
d2b8d4da 506 gfp_t gfp;
515667f8
DD
507
508 if (!rdi)
509 return ERR_PTR(-EINVAL);
510
511 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
512 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
d2b8d4da 513 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
515667f8
DD
514 return ERR_PTR(-EINVAL);
515
d2b8d4da
MM
516 /* GFP_NOIO is applicable to RC QP's only */
517
518 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
519 init_attr->qp_type != IB_QPT_RC)
520 return ERR_PTR(-EINVAL);
521
522 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
523 GFP_NOIO : GFP_KERNEL;
524
515667f8
DD
525 /* Check receive queue parameters if no SRQ is specified. */
526 if (!init_attr->srq) {
527 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
528 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
529 return ERR_PTR(-EINVAL);
530
531 if (init_attr->cap.max_send_sge +
532 init_attr->cap.max_send_wr +
533 init_attr->cap.max_recv_sge +
534 init_attr->cap.max_recv_wr == 0)
535 return ERR_PTR(-EINVAL);
536 }
537
538 switch (init_attr->qp_type) {
539 case IB_QPT_SMI:
540 case IB_QPT_GSI:
541 if (init_attr->port_num == 0 ||
542 init_attr->port_num > ibpd->device->phys_port_cnt)
543 return ERR_PTR(-EINVAL);
544 case IB_QPT_UC:
545 case IB_QPT_RC:
546 case IB_QPT_UD:
547 sz = sizeof(struct rvt_sge) *
548 init_attr->cap.max_send_sge +
549 sizeof(struct rvt_swqe);
d2b8d4da
MM
550 if (gfp == GFP_NOIO)
551 swq = __vmalloc(
552 (init_attr->cap.max_send_wr + 1) * sz,
553 gfp, PAGE_KERNEL);
554 else
d1b697b6
MH
555 swq = vmalloc_node(
556 (init_attr->cap.max_send_wr + 1) * sz,
557 rdi->dparms.node);
515667f8
DD
558 if (!swq)
559 return ERR_PTR(-ENOMEM);
560
561 sz = sizeof(*qp);
562 sg_list_sz = 0;
563 if (init_attr->srq) {
564 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
565
566 if (srq->rq.max_sge > 1)
567 sg_list_sz = sizeof(*qp->r_sg_list) *
568 (srq->rq.max_sge - 1);
569 } else if (init_attr->cap.max_recv_sge > 1)
570 sg_list_sz = sizeof(*qp->r_sg_list) *
571 (init_attr->cap.max_recv_sge - 1);
d1b697b6 572 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
515667f8
DD
573 if (!qp)
574 goto bail_swq;
575
576 RCU_INIT_POINTER(qp->next, NULL);
577
578 /*
579 * Driver needs to set up it's private QP structure and do any
580 * initialization that is needed.
581 */
d2b8d4da 582 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
515667f8
DD
583 if (!priv)
584 goto bail_qp;
585 qp->priv = priv;
586 qp->timeout_jiffies =
587 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
588 1000UL);
589 if (init_attr->srq) {
590 sz = 0;
591 } else {
592 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
593 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
594 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
595 sizeof(struct rvt_rwqe);
d2b8d4da
MM
596 if (udata)
597 qp->r_rq.wq = vmalloc_user(
598 sizeof(struct rvt_rwq) +
599 qp->r_rq.size * sz);
600 else if (gfp == GFP_NOIO)
601 qp->r_rq.wq = __vmalloc(
602 sizeof(struct rvt_rwq) +
603 qp->r_rq.size * sz,
604 gfp, PAGE_KERNEL);
605 else
d1b697b6 606 qp->r_rq.wq = vmalloc_node(
d2b8d4da 607 sizeof(struct rvt_rwq) +
d1b697b6
MH
608 qp->r_rq.size * sz,
609 rdi->dparms.node);
515667f8
DD
610 if (!qp->r_rq.wq)
611 goto bail_driver_priv;
612 }
613
614 /*
615 * ib_create_qp() will initialize qp->ibqp
616 * except for qp->ibqp.qp_num.
617 */
618 spin_lock_init(&qp->r_lock);
46a80d62 619 spin_lock_init(&qp->s_hlock);
515667f8
DD
620 spin_lock_init(&qp->s_lock);
621 spin_lock_init(&qp->r_rq.lock);
622 atomic_set(&qp->refcount, 0);
623 init_waitqueue_head(&qp->wait);
624 init_timer(&qp->s_timer);
625 qp->s_timer.data = (unsigned long)qp;
626 INIT_LIST_HEAD(&qp->rspwait);
627 qp->state = IB_QPS_RESET;
628 qp->s_wq = swq;
629 qp->s_size = init_attr->cap.max_send_wr + 1;
46a80d62 630 qp->s_avail = init_attr->cap.max_send_wr;
515667f8
DD
631 qp->s_max_sge = init_attr->cap.max_send_sge;
632 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
633 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
634
635 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
636 init_attr->qp_type,
d2b8d4da 637 init_attr->port_num, gfp);
515667f8
DD
638 if (err < 0) {
639 ret = ERR_PTR(err);
640 goto bail_rq_wq;
641 }
642 qp->ibqp.qp_num = err;
643 qp->port_num = init_attr->port_num;
5a9cf6f2 644 rvt_reset_qp(rdi, qp, init_attr->qp_type);
515667f8
DD
645 break;
646
647 default:
648 /* Don't support raw QPs */
649 return ERR_PTR(-EINVAL);
650 }
651
652 init_attr->cap.max_inline_data = 0;
653
654 /*
655 * Return the address of the RWQ as the offset to mmap.
bfbac097 656 * See rvt_mmap() for details.
515667f8
DD
657 */
658 if (udata && udata->outlen >= sizeof(__u64)) {
659 if (!qp->r_rq.wq) {
660 __u64 offset = 0;
661
662 err = ib_copy_to_udata(udata, &offset,
663 sizeof(offset));
664 if (err) {
665 ret = ERR_PTR(err);
666 goto bail_qpn;
667 }
668 } else {
669 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
670
671 qp->ip = rvt_create_mmap_info(rdi, s,
672 ibpd->uobject->context,
673 qp->r_rq.wq);
674 if (!qp->ip) {
675 ret = ERR_PTR(-ENOMEM);
676 goto bail_qpn;
677 }
678
679 err = ib_copy_to_udata(udata, &qp->ip->offset,
680 sizeof(qp->ip->offset));
681 if (err) {
682 ret = ERR_PTR(err);
683 goto bail_ip;
684 }
685 }
686 }
687
688 spin_lock(&rdi->n_qps_lock);
689 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
690 spin_unlock(&rdi->n_qps_lock);
691 ret = ERR_PTR(-ENOMEM);
692 goto bail_ip;
693 }
694
695 rdi->n_qps_allocated++;
bfee5e32
VM
696 /*
697 * Maintain a busy_jiffies variable that will be added to the timeout
698 * period in mod_retry_timer and add_retry_timer. This busy jiffies
699 * is scaled by the number of rc qps created for the device to reduce
700 * the number of timeouts occurring when there is a large number of
701 * qps. busy_jiffies is incremented every rc qp scaling interval.
702 * The scaling interval is selected based on extensive performance
703 * evaluation of targeted workloads.
704 */
705 if (init_attr->qp_type == IB_QPT_RC) {
706 rdi->n_rc_qps++;
707 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
708 }
515667f8
DD
709 spin_unlock(&rdi->n_qps_lock);
710
711 if (qp->ip) {
712 spin_lock_irq(&rdi->pending_lock);
713 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
714 spin_unlock_irq(&rdi->pending_lock);
715 }
716
717 ret = &qp->ibqp;
718
b518d3e6 719 /*
515667f8
DD
720 * We have our QP and its good, now keep track of what types of opcodes
721 * can be processed on this QP. We do this by keeping track of what the
722 * 3 high order bits of the opcode are.
b518d3e6 723 */
515667f8
DD
724 switch (init_attr->qp_type) {
725 case IB_QPT_SMI:
726 case IB_QPT_GSI:
727 case IB_QPT_UD:
728 qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & RVT_OPCODE_QP_MASK;
729 break;
730 case IB_QPT_RC:
731 qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & RVT_OPCODE_QP_MASK;
732 break;
733 case IB_QPT_UC:
734 qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & RVT_OPCODE_QP_MASK;
735 break;
736 default:
737 ret = ERR_PTR(-EINVAL);
738 goto bail_ip;
739 }
740
741 return ret;
742
743bail_ip:
744 kref_put(&qp->ip->ref, rvt_release_mmap_info);
745
746bail_qpn:
747 free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
748
749bail_rq_wq:
750 vfree(qp->r_rq.wq);
751
752bail_driver_priv:
753 rdi->driver_f.qp_priv_free(rdi, qp);
754
755bail_qp:
756 kfree(qp);
757
758bail_swq:
759 vfree(swq);
760
761 return ret;
b518d3e6
DD
762}
763
90793f71
DD
764/**
765 * rvt_clear_mr_refs - Drop help mr refs
766 * @qp: rvt qp data structure
767 * @clr_sends: If shoudl clear send side or not
768 */
3b0b3fb3
DD
769void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
770{
771 unsigned n;
772
773 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
774 rvt_put_ss(&qp->s_rdma_read_sge);
775
776 rvt_put_ss(&qp->r_sge);
777
778 if (clr_sends) {
779 while (qp->s_last != qp->s_head) {
780 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
781 unsigned i;
782
783 for (i = 0; i < wqe->wr.num_sge; i++) {
784 struct rvt_sge *sge = &wqe->sg_list[i];
785
786 rvt_put_mr(sge->mr);
787 }
788 if (qp->ibqp.qp_type == IB_QPT_UD ||
789 qp->ibqp.qp_type == IB_QPT_SMI ||
790 qp->ibqp.qp_type == IB_QPT_GSI)
791 atomic_dec(&ibah_to_rvtah(
792 wqe->ud_wr.ah)->refcount);
793 if (++qp->s_last >= qp->s_size)
794 qp->s_last = 0;
46a80d62 795 smp_wmb(); /* see qp_set_savail */
3b0b3fb3
DD
796 }
797 if (qp->s_rdma_mr) {
798 rvt_put_mr(qp->s_rdma_mr);
799 qp->s_rdma_mr = NULL;
800 }
801 }
802
803 if (qp->ibqp.qp_type != IB_QPT_RC)
804 return;
805
806 for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
807 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
808
809 if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
810 e->rdma_sge.mr) {
811 rvt_put_mr(e->rdma_sge.mr);
812 e->rdma_sge.mr = NULL;
813 }
814 }
815}
816EXPORT_SYMBOL(rvt_clear_mr_refs);
817
818/**
819 * rvt_error_qp - put a QP into the error state
820 * @qp: the QP to put into the error state
821 * @err: the receive completion error to signal if a RWQE is active
822 *
823 * Flushes both send and receive work queues.
90793f71
DD
824 *
825 * Return: true if last WQE event should be generated.
3b0b3fb3
DD
826 * The QP r_lock and s_lock should be held and interrupts disabled.
827 * If we are already in error state, just return.
828 */
829int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
830{
831 struct ib_wc wc;
832 int ret = 0;
833 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
834
835 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
836 goto bail;
837
838 qp->state = IB_QPS_ERR;
839
840 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
841 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
842 del_timer(&qp->s_timer);
843 }
844
845 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
846 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
847
848 rdi->driver_f.notify_error_qp(qp);
849
850 /* Schedule the sending tasklet to drain the send work queue. */
46a80d62 851 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
3b0b3fb3
DD
852 rdi->driver_f.schedule_send(qp);
853
854 rvt_clear_mr_refs(qp, 0);
855
856 memset(&wc, 0, sizeof(wc));
857 wc.qp = &qp->ibqp;
858 wc.opcode = IB_WC_RECV;
859
860 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
861 wc.wr_id = qp->r_wr_id;
862 wc.status = err;
863 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
864 }
865 wc.status = IB_WC_WR_FLUSH_ERR;
866
867 if (qp->r_rq.wq) {
868 struct rvt_rwq *wq;
869 u32 head;
870 u32 tail;
871
872 spin_lock(&qp->r_rq.lock);
873
874 /* sanity check pointers before trusting them */
875 wq = qp->r_rq.wq;
876 head = wq->head;
877 if (head >= qp->r_rq.size)
878 head = 0;
879 tail = wq->tail;
880 if (tail >= qp->r_rq.size)
881 tail = 0;
882 while (tail != head) {
883 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
884 if (++tail >= qp->r_rq.size)
885 tail = 0;
886 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
887 }
888 wq->tail = tail;
889
890 spin_unlock(&qp->r_rq.lock);
891 } else if (qp->ibqp.event_handler) {
892 ret = 1;
893 }
894
895bail:
896 return ret;
897}
898EXPORT_SYMBOL(rvt_error_qp);
899
900/*
901 * Put the QP into the hash table.
902 * The hash table holds a reference to the QP.
903 */
904static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
905{
906 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
907 unsigned long flags;
908
909 atomic_inc(&qp->refcount);
910 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
911
912 if (qp->ibqp.qp_num <= 1) {
913 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
914 } else {
915 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
916
917 qp->next = rdi->qp_dev->qp_table[n];
918 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
919 trace_rvt_qpinsert(qp, n);
920 }
921
922 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
923}
924
90793f71
DD
925/**
926 * rvt_remove_qp - remove qp form table
927 * @rdi: rvt dev struct
928 * @qp: qp to remove
929 *
3b0b3fb3
DD
930 * Remove the QP from the table so it can't be found asynchronously by
931 * the receive routine.
932 */
933void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
934{
935 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
936 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
937 unsigned long flags;
938 int removed = 1;
939
940 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
941
942 if (rcu_dereference_protected(rvp->qp[0],
943 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
944 RCU_INIT_POINTER(rvp->qp[0], NULL);
945 } else if (rcu_dereference_protected(rvp->qp[1],
946 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
947 RCU_INIT_POINTER(rvp->qp[1], NULL);
948 } else {
949 struct rvt_qp *q;
950 struct rvt_qp __rcu **qpp;
951
952 removed = 0;
953 qpp = &rdi->qp_dev->qp_table[n];
954 for (; (q = rcu_dereference_protected(*qpp,
955 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
956 qpp = &q->next) {
957 if (q == qp) {
958 RCU_INIT_POINTER(*qpp,
959 rcu_dereference_protected(qp->next,
960 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
961 removed = 1;
962 trace_rvt_qpremove(qp, n);
963 break;
964 }
965 }
966 }
967
968 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
969 if (removed) {
970 synchronize_rcu();
971 if (atomic_dec_and_test(&qp->refcount))
972 wake_up(&qp->wait);
973 }
974}
975EXPORT_SYMBOL(rvt_remove_qp);
976
b518d3e6
DD
977/**
978 * qib_modify_qp - modify the attributes of a queue pair
979 * @ibqp: the queue pair who's attributes we're modifying
980 * @attr: the new attributes
981 * @attr_mask: the mask of attributes to modify
982 * @udata: user data for libibverbs.so
983 *
90793f71 984 * Return: 0 on success, otherwise returns an errno.
b518d3e6
DD
985 */
986int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
987 int attr_mask, struct ib_udata *udata)
988{
3b0b3fb3
DD
989 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
990 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
991 enum ib_qp_state cur_state, new_state;
992 struct ib_event ev;
993 int lastwqe = 0;
994 int mig = 0;
995 int pmtu = 0; /* for gcc warning only */
996 enum rdma_link_layer link;
997
998 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
999
1000 spin_lock_irq(&qp->r_lock);
46a80d62 1001 spin_lock(&qp->s_hlock);
3b0b3fb3
DD
1002 spin_lock(&qp->s_lock);
1003
1004 cur_state = attr_mask & IB_QP_CUR_STATE ?
1005 attr->cur_qp_state : qp->state;
1006 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1007
1008 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1009 attr_mask, link))
1010 goto inval;
1011
e85ec33d
IW
1012 if (rdi->driver_f.check_modify_qp &&
1013 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1014 goto inval;
1015
3b0b3fb3
DD
1016 if (attr_mask & IB_QP_AV) {
1017 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1018 goto inval;
1019 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1020 goto inval;
1021 }
1022
1023 if (attr_mask & IB_QP_ALT_PATH) {
1024 if (attr->alt_ah_attr.dlid >=
1025 be16_to_cpu(IB_MULTICAST_LID_BASE))
1026 goto inval;
1027 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1028 goto inval;
1029 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1030 goto inval;
1031 }
1032
1033 if (attr_mask & IB_QP_PKEY_INDEX)
1034 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1035 goto inval;
1036
1037 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1038 if (attr->min_rnr_timer > 31)
1039 goto inval;
1040
1041 if (attr_mask & IB_QP_PORT)
1042 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1043 qp->ibqp.qp_type == IB_QPT_GSI ||
1044 attr->port_num == 0 ||
1045 attr->port_num > ibqp->device->phys_port_cnt)
1046 goto inval;
1047
1048 if (attr_mask & IB_QP_DEST_QPN)
1049 if (attr->dest_qp_num > RVT_QPN_MASK)
1050 goto inval;
1051
1052 if (attr_mask & IB_QP_RETRY_CNT)
1053 if (attr->retry_cnt > 7)
1054 goto inval;
1055
1056 if (attr_mask & IB_QP_RNR_RETRY)
1057 if (attr->rnr_retry > 7)
1058 goto inval;
1059
b518d3e6 1060 /*
3b0b3fb3
DD
1061 * Don't allow invalid path_mtu values. OK to set greater
1062 * than the active mtu (or even the max_cap, if we have tuned
1063 * that to a small mtu. We'll set qp->path_mtu
1064 * to the lesser of requested attribute mtu and active,
1065 * for packetizing messages.
1066 * Note that the QP port has to be set in INIT and MTU in RTR.
b518d3e6 1067 */
3b0b3fb3
DD
1068 if (attr_mask & IB_QP_PATH_MTU) {
1069 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1070 if (pmtu < 0)
1071 goto inval;
1072 }
1073
1074 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1075 if (attr->path_mig_state == IB_MIG_REARM) {
1076 if (qp->s_mig_state == IB_MIG_ARMED)
1077 goto inval;
1078 if (new_state != IB_QPS_RTS)
1079 goto inval;
1080 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1081 if (qp->s_mig_state == IB_MIG_REARM)
1082 goto inval;
1083 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1084 goto inval;
1085 if (qp->s_mig_state == IB_MIG_ARMED)
1086 mig = 1;
1087 } else {
1088 goto inval;
1089 }
1090 }
1091
1092 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1093 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1094 goto inval;
1095
1096 switch (new_state) {
1097 case IB_QPS_RESET:
1098 if (qp->state != IB_QPS_RESET)
1099 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1100 break;
1101
1102 case IB_QPS_RTR:
1103 /* Allow event to re-trigger if QP set to RTR more than once */
1104 qp->r_flags &= ~RVT_R_COMM_EST;
1105 qp->state = new_state;
1106 break;
1107
1108 case IB_QPS_SQD:
1109 qp->s_draining = qp->s_last != qp->s_cur;
1110 qp->state = new_state;
1111 break;
1112
1113 case IB_QPS_SQE:
1114 if (qp->ibqp.qp_type == IB_QPT_RC)
1115 goto inval;
1116 qp->state = new_state;
1117 break;
1118
1119 case IB_QPS_ERR:
1120 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1121 break;
1122
1123 default:
1124 qp->state = new_state;
1125 break;
1126 }
1127
1128 if (attr_mask & IB_QP_PKEY_INDEX)
1129 qp->s_pkey_index = attr->pkey_index;
1130
1131 if (attr_mask & IB_QP_PORT)
1132 qp->port_num = attr->port_num;
1133
1134 if (attr_mask & IB_QP_DEST_QPN)
1135 qp->remote_qpn = attr->dest_qp_num;
1136
1137 if (attr_mask & IB_QP_SQ_PSN) {
1138 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1139 qp->s_psn = qp->s_next_psn;
1140 qp->s_sending_psn = qp->s_next_psn;
1141 qp->s_last_psn = qp->s_next_psn - 1;
1142 qp->s_sending_hpsn = qp->s_last_psn;
1143 }
1144
1145 if (attr_mask & IB_QP_RQ_PSN)
1146 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1147
1148 if (attr_mask & IB_QP_ACCESS_FLAGS)
1149 qp->qp_access_flags = attr->qp_access_flags;
1150
1151 if (attr_mask & IB_QP_AV) {
1152 qp->remote_ah_attr = attr->ah_attr;
1153 qp->s_srate = attr->ah_attr.static_rate;
1154 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1155 }
1156
1157 if (attr_mask & IB_QP_ALT_PATH) {
1158 qp->alt_ah_attr = attr->alt_ah_attr;
1159 qp->s_alt_pkey_index = attr->alt_pkey_index;
1160 }
1161
1162 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1163 qp->s_mig_state = attr->path_mig_state;
1164 if (mig) {
1165 qp->remote_ah_attr = qp->alt_ah_attr;
1166 qp->port_num = qp->alt_ah_attr.port_num;
1167 qp->s_pkey_index = qp->s_alt_pkey_index;
3b0b3fb3
DD
1168 }
1169 }
1170
1171 if (attr_mask & IB_QP_PATH_MTU) {
1172 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1173 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
46a80d62 1174 qp->log_pmtu = ilog2(qp->pmtu);
3b0b3fb3
DD
1175 }
1176
1177 if (attr_mask & IB_QP_RETRY_CNT) {
1178 qp->s_retry_cnt = attr->retry_cnt;
1179 qp->s_retry = attr->retry_cnt;
1180 }
1181
1182 if (attr_mask & IB_QP_RNR_RETRY) {
1183 qp->s_rnr_retry_cnt = attr->rnr_retry;
1184 qp->s_rnr_retry = attr->rnr_retry;
1185 }
1186
1187 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1188 qp->r_min_rnr_timer = attr->min_rnr_timer;
1189
1190 if (attr_mask & IB_QP_TIMEOUT) {
1191 qp->timeout = attr->timeout;
1192 qp->timeout_jiffies =
1193 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1194 1000UL);
1195 }
1196
1197 if (attr_mask & IB_QP_QKEY)
1198 qp->qkey = attr->qkey;
1199
1200 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1201 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1202
1203 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1204 qp->s_max_rd_atomic = attr->max_rd_atomic;
1205
e85ec33d
IW
1206 if (rdi->driver_f.modify_qp)
1207 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1208
3b0b3fb3 1209 spin_unlock(&qp->s_lock);
46a80d62 1210 spin_unlock(&qp->s_hlock);
3b0b3fb3
DD
1211 spin_unlock_irq(&qp->r_lock);
1212
1213 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1214 rvt_insert_qp(rdi, qp);
1215
1216 if (lastwqe) {
1217 ev.device = qp->ibqp.device;
1218 ev.element.qp = &qp->ibqp;
1219 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1220 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1221 }
1222 if (mig) {
1223 ev.device = qp->ibqp.device;
1224 ev.element.qp = &qp->ibqp;
1225 ev.event = IB_EVENT_PATH_MIG;
1226 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1227 }
1228 return 0;
1229
1230inval:
1231 spin_unlock(&qp->s_lock);
46a80d62 1232 spin_unlock(&qp->s_hlock);
3b0b3fb3
DD
1233 spin_unlock_irq(&qp->r_lock);
1234 return -EINVAL;
b518d3e6
DD
1235}
1236
1237/**
1238 * rvt_destroy_qp - destroy a queue pair
1239 * @ibqp: the queue pair to destroy
1240 *
b518d3e6
DD
1241 * Note that this can be called while the QP is actively sending or
1242 * receiving!
90793f71
DD
1243 *
1244 * Return: 0 on success.
b518d3e6
DD
1245 */
1246int rvt_destroy_qp(struct ib_qp *ibqp)
1247{
5a17ad11
DD
1248 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1249 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
b518d3e6 1250
5a17ad11 1251 spin_lock_irq(&qp->r_lock);
46a80d62 1252 spin_lock(&qp->s_hlock);
5a17ad11
DD
1253 spin_lock(&qp->s_lock);
1254 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1255 spin_unlock(&qp->s_lock);
46a80d62 1256 spin_unlock(&qp->s_hlock);
5a17ad11
DD
1257 spin_unlock_irq(&qp->r_lock);
1258
1259 /* qpn is now available for use again */
1260 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1261
1262 spin_lock(&rdi->n_qps_lock);
1263 rdi->n_qps_allocated--;
bfee5e32
VM
1264 if (qp->ibqp.qp_type == IB_QPT_RC) {
1265 rdi->n_rc_qps--;
1266 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1267 }
5a17ad11
DD
1268 spin_unlock(&rdi->n_qps_lock);
1269
1270 if (qp->ip)
1271 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1272 else
1273 vfree(qp->r_rq.wq);
1274 vfree(qp->s_wq);
1275 rdi->driver_f.qp_priv_free(rdi, qp);
1276 kfree(qp);
1277 return 0;
b518d3e6
DD
1278}
1279
90793f71
DD
1280/**
1281 * rvt_query_qp - query an ipbq
1282 * @ibqp: IB qp to query
1283 * @attr: attr struct to fill in
1284 * @attr_mask: attr mask ignored
1285 * @init_attr: struct to fill in
1286 *
1287 * Return: always 0
1288 */
b518d3e6
DD
1289int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1290 int attr_mask, struct ib_qp_init_attr *init_attr)
1291{
74d2d500
HC
1292 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1293 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1294
1295 attr->qp_state = qp->state;
1296 attr->cur_qp_state = attr->qp_state;
1297 attr->path_mtu = qp->path_mtu;
1298 attr->path_mig_state = qp->s_mig_state;
1299 attr->qkey = qp->qkey;
1300 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1301 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1302 attr->dest_qp_num = qp->remote_qpn;
1303 attr->qp_access_flags = qp->qp_access_flags;
1304 attr->cap.max_send_wr = qp->s_size - 1;
1305 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1306 attr->cap.max_send_sge = qp->s_max_sge;
1307 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1308 attr->cap.max_inline_data = 0;
1309 attr->ah_attr = qp->remote_ah_attr;
1310 attr->alt_ah_attr = qp->alt_ah_attr;
1311 attr->pkey_index = qp->s_pkey_index;
1312 attr->alt_pkey_index = qp->s_alt_pkey_index;
1313 attr->en_sqd_async_notify = 0;
1314 attr->sq_draining = qp->s_draining;
1315 attr->max_rd_atomic = qp->s_max_rd_atomic;
1316 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1317 attr->min_rnr_timer = qp->r_min_rnr_timer;
1318 attr->port_num = qp->port_num;
1319 attr->timeout = qp->timeout;
1320 attr->retry_cnt = qp->s_retry_cnt;
1321 attr->rnr_retry = qp->s_rnr_retry_cnt;
1322 attr->alt_port_num = qp->alt_ah_attr.port_num;
1323 attr->alt_timeout = qp->alt_timeout;
1324
1325 init_attr->event_handler = qp->ibqp.event_handler;
1326 init_attr->qp_context = qp->ibqp.qp_context;
1327 init_attr->send_cq = qp->ibqp.send_cq;
1328 init_attr->recv_cq = qp->ibqp.recv_cq;
1329 init_attr->srq = qp->ibqp.srq;
1330 init_attr->cap = attr->cap;
1331 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1332 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1333 else
1334 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1335 init_attr->qp_type = qp->ibqp.qp_type;
1336 init_attr->port_num = qp->port_num;
1337 return 0;
b518d3e6 1338}
8cf4020b
DD
1339
1340/**
1341 * rvt_post_receive - post a receive on a QP
1342 * @ibqp: the QP to post the receive on
1343 * @wr: the WR to post
1344 * @bad_wr: the first bad WR is put here
1345 *
1346 * This may be called from interrupt context.
90793f71
DD
1347 *
1348 * Return: 0 on success otherwise errno
8cf4020b
DD
1349 */
1350int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1351 struct ib_recv_wr **bad_wr)
1352{
120bdafa
DD
1353 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1354 struct rvt_rwq *wq = qp->r_rq.wq;
1355 unsigned long flags;
8cf4020b 1356
120bdafa
DD
1357 /* Check that state is OK to post receive. */
1358 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1359 *bad_wr = wr;
1360 return -EINVAL;
1361 }
1362
1363 for (; wr; wr = wr->next) {
1364 struct rvt_rwqe *wqe;
1365 u32 next;
1366 int i;
1367
1368 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1369 *bad_wr = wr;
1370 return -EINVAL;
1371 }
1372
1373 spin_lock_irqsave(&qp->r_rq.lock, flags);
1374 next = wq->head + 1;
1375 if (next >= qp->r_rq.size)
1376 next = 0;
1377 if (next == wq->tail) {
1378 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1379 *bad_wr = wr;
1380 return -ENOMEM;
1381 }
1382
1383 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1384 wqe->wr_id = wr->wr_id;
1385 wqe->num_sge = wr->num_sge;
1386 for (i = 0; i < wr->num_sge; i++)
1387 wqe->sg_list[i] = wr->sg_list[i];
1388 /* Make sure queue entry is written before the head index. */
1389 smp_wmb();
1390 wq->head = next;
1391 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1392 }
1393 return 0;
8cf4020b
DD
1394}
1395
46a80d62
MM
1396/**
1397 * qp_get_savail - return number of avail send entries
1398 *
1399 * @qp - the qp
1400 *
1401 * This assumes the s_hlock is held but the s_last
1402 * qp variable is uncontrolled.
1403 */
1404static inline u32 qp_get_savail(struct rvt_qp *qp)
1405{
1406 u32 slast;
1407 u32 ret;
1408
1409 smp_read_barrier_depends(); /* see rc.c */
1410 slast = ACCESS_ONCE(qp->s_last);
1411 if (qp->s_head >= slast)
1412 ret = qp->s_size - (qp->s_head - slast);
1413 else
1414 ret = slast - qp->s_head;
1415 return ret - 1;
1416}
1417
bfbac097
DD
1418/**
1419 * rvt_post_one_wr - post one RC, UC, or UD send work request
1420 * @qp: the QP to post on
1421 * @wr: the work request to send
1422 */
1423static int rvt_post_one_wr(struct rvt_qp *qp, struct ib_send_wr *wr)
1424{
1425 struct rvt_swqe *wqe;
1426 u32 next;
1427 int i;
1428 int j;
1429 int acc;
1430 struct rvt_lkey_table *rkt;
1431 struct rvt_pd *pd;
1432 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
46a80d62
MM
1433 u8 log_pmtu;
1434 int ret;
bfbac097
DD
1435
1436 /* IB spec says that num_sge == 0 is OK. */
1437 if (unlikely(wr->num_sge > qp->s_max_sge))
1438 return -EINVAL;
1439
1440 /*
1441 * Don't allow RDMA reads or atomic operations on UC or
1442 * undefined operations.
1443 * Make sure buffer is large enough to hold the result for atomics.
1444 */
1445 if (qp->ibqp.qp_type == IB_QPT_UC) {
1446 if ((unsigned)wr->opcode >= IB_WR_RDMA_READ)
1447 return -EINVAL;
1448 } else if (qp->ibqp.qp_type != IB_QPT_RC) {
1449 /* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
1450 if (wr->opcode != IB_WR_SEND &&
1451 wr->opcode != IB_WR_SEND_WITH_IMM)
1452 return -EINVAL;
1453 /* Check UD destination address PD */
1454 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1455 return -EINVAL;
1456 } else if ((unsigned)wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
1457 return -EINVAL;
1458 } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
1459 (wr->num_sge == 0 ||
1460 wr->sg_list[0].length < sizeof(u64) ||
1461 wr->sg_list[0].addr & (sizeof(u64) - 1))) {
1462 return -EINVAL;
1463 } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
1464 return -EINVAL;
1465 }
46a80d62
MM
1466 /* check for avail */
1467 if (unlikely(!qp->s_avail)) {
1468 qp->s_avail = qp_get_savail(qp);
e16689e4
HC
1469 if (WARN_ON(qp->s_avail > (qp->s_size - 1)))
1470 rvt_pr_err(rdi,
1471 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1472 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1473 qp->s_head, qp->s_tail, qp->s_cur,
1474 qp->s_acked, qp->s_last);
46a80d62
MM
1475 if (!qp->s_avail)
1476 return -ENOMEM;
1477 }
bfbac097
DD
1478 next = qp->s_head + 1;
1479 if (next >= qp->s_size)
1480 next = 0;
60c30f57 1481
bfbac097
DD
1482 rkt = &rdi->lkey_table;
1483 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1484 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1485
1486 if (qp->ibqp.qp_type != IB_QPT_UC &&
1487 qp->ibqp.qp_type != IB_QPT_RC)
1488 memcpy(&wqe->ud_wr, ud_wr(wr), sizeof(wqe->ud_wr));
1489 else if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
1490 wr->opcode == IB_WR_RDMA_WRITE ||
1491 wr->opcode == IB_WR_RDMA_READ)
1492 memcpy(&wqe->rdma_wr, rdma_wr(wr), sizeof(wqe->rdma_wr));
1493 else if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1494 wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1495 memcpy(&wqe->atomic_wr, atomic_wr(wr), sizeof(wqe->atomic_wr));
1496 else
1497 memcpy(&wqe->wr, wr, sizeof(wqe->wr));
1498
1499 wqe->length = 0;
1500 j = 0;
1501 if (wr->num_sge) {
1502 acc = wr->opcode >= IB_WR_RDMA_READ ?
1503 IB_ACCESS_LOCAL_WRITE : 0;
1504 for (i = 0; i < wr->num_sge; i++) {
1505 u32 length = wr->sg_list[i].length;
1506 int ok;
1507
1508 if (length == 0)
1509 continue;
1510 ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1511 &wr->sg_list[i], acc);
46a80d62
MM
1512 if (!ok) {
1513 ret = -EINVAL;
bfbac097 1514 goto bail_inval_free;
46a80d62 1515 }
bfbac097
DD
1516 wqe->length += length;
1517 j++;
1518 }
1519 wqe->wr.num_sge = j;
1520 }
46a80d62
MM
1521
1522 /* general part of wqe valid - allow for driver checks */
1523 if (rdi->driver_f.check_send_wqe) {
1524 ret = rdi->driver_f.check_send_wqe(qp, wqe);
1525 if (ret)
bfbac097 1526 goto bail_inval_free;
46a80d62
MM
1527 }
1528
1529 log_pmtu = qp->log_pmtu;
1530 if (qp->ibqp.qp_type != IB_QPT_UC &&
1531 qp->ibqp.qp_type != IB_QPT_RC) {
1532 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1533
1534 log_pmtu = ah->log_pmtu;
bfbac097
DD
1535 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1536 }
46a80d62 1537
bfbac097 1538 wqe->ssn = qp->s_ssn++;
46a80d62
MM
1539 wqe->psn = qp->s_next_psn;
1540 wqe->lpsn = wqe->psn +
1541 (wqe->length ? ((wqe->length - 1) >> log_pmtu) : 0);
1542 qp->s_next_psn = wqe->lpsn + 1;
e16689e4 1543 trace_rvt_post_one_wr(qp, wqe);
46a80d62
MM
1544 smp_wmb(); /* see request builders */
1545 qp->s_avail--;
bfbac097
DD
1546 qp->s_head = next;
1547
1548 return 0;
1549
1550bail_inval_free:
1551 /* release mr holds */
1552 while (j) {
1553 struct rvt_sge *sge = &wqe->sg_list[--j];
1554
1555 rvt_put_mr(sge->mr);
1556 }
46a80d62 1557 return ret;
bfbac097
DD
1558}
1559
8cf4020b
DD
1560/**
1561 * rvt_post_send - post a send on a QP
1562 * @ibqp: the QP to post the send on
1563 * @wr: the list of work requests to post
1564 * @bad_wr: the first bad WR is put here
1565 *
1566 * This may be called from interrupt context.
90793f71
DD
1567 *
1568 * Return: 0 on success else errno
8cf4020b
DD
1569 */
1570int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1571 struct ib_send_wr **bad_wr)
1572{
bfbac097
DD
1573 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1574 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1575 unsigned long flags = 0;
1576 int call_send;
1577 unsigned nreq = 0;
1578 int err = 0;
1579
46a80d62 1580 spin_lock_irqsave(&qp->s_hlock, flags);
bfbac097 1581
8cf4020b 1582 /*
bfbac097
DD
1583 * Ensure QP state is such that we can send. If not bail out early,
1584 * there is no need to do this every time we post a send.
8cf4020b 1585 */
bfbac097 1586 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
46a80d62 1587 spin_unlock_irqrestore(&qp->s_hlock, flags);
bfbac097
DD
1588 return -EINVAL;
1589 }
8cf4020b 1590
bfbac097
DD
1591 /*
1592 * If the send queue is empty, and we only have a single WR then just go
1593 * ahead and kick the send engine into gear. Otherwise we will always
1594 * just schedule the send to happen later.
1595 */
1596 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1597
1598 for (; wr; wr = wr->next) {
1599 err = rvt_post_one_wr(qp, wr);
1600 if (unlikely(err)) {
1601 *bad_wr = wr;
1602 goto bail;
1603 }
1604 nreq++;
1605 }
1606bail:
46a80d62
MM
1607 spin_unlock_irqrestore(&qp->s_hlock, flags);
1608 if (nreq) {
1609 if (call_send)
1610 rdi->driver_f.schedule_send_no_lock(qp);
1611 else
1612 rdi->driver_f.do_send(qp);
1613 }
bfbac097 1614 return err;
8cf4020b
DD
1615}
1616
1617/**
1618 * rvt_post_srq_receive - post a receive on a shared receive queue
1619 * @ibsrq: the SRQ to post the receive on
1620 * @wr: the list of work requests to post
1621 * @bad_wr: A pointer to the first WR to cause a problem is put here
1622 *
1623 * This may be called from interrupt context.
90793f71
DD
1624 *
1625 * Return: 0 on success else errno
8cf4020b
DD
1626 */
1627int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1628 struct ib_recv_wr **bad_wr)
1629{
b8f881b9
JJ
1630 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1631 struct rvt_rwq *wq;
1632 unsigned long flags;
1633
1634 for (; wr; wr = wr->next) {
1635 struct rvt_rwqe *wqe;
1636 u32 next;
1637 int i;
1638
1639 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1640 *bad_wr = wr;
1641 return -EINVAL;
1642 }
1643
1644 spin_lock_irqsave(&srq->rq.lock, flags);
1645 wq = srq->rq.wq;
1646 next = wq->head + 1;
1647 if (next >= srq->rq.size)
1648 next = 0;
1649 if (next == wq->tail) {
1650 spin_unlock_irqrestore(&srq->rq.lock, flags);
1651 *bad_wr = wr;
1652 return -ENOMEM;
1653 }
1654
1655 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1656 wqe->wr_id = wr->wr_id;
1657 wqe->num_sge = wr->num_sge;
1658 for (i = 0; i < wr->num_sge; i++)
1659 wqe->sg_list[i] = wr->sg_list[i];
1660 /* Make sure queue entry is written before the head index. */
1661 smp_wmb();
1662 wq->head = next;
1663 spin_unlock_irqrestore(&srq->rq.lock, flags);
1664 }
1665 return 0;
8cf4020b 1666}
3b0b3fb3 1667
90793f71
DD
1668/** rvt_free_qpn - Free a qpn from the bit map
1669 * @qpt: QP table
1670 * @qpn: queue pair number to free
1671 */
3b0b3fb3
DD
1672void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1673{
1674 struct rvt_qpn_map *map;
1675
1676 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1677 if (map->page)
1678 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1679}
1680EXPORT_SYMBOL(rvt_free_qpn);
1681
90793f71
DD
1682/**
1683 * rvt_dec_qp_cnt - decrement qp count
1684 * rdi: rvt dev struct
1685 */
3b0b3fb3
DD
1686void rvt_dec_qp_cnt(struct rvt_dev_info *rdi)
1687{
1688 spin_lock(&rdi->n_qps_lock);
1689 rdi->n_qps_allocated--;
1690 spin_unlock(&rdi->n_qps_lock);
1691}
1692EXPORT_SYMBOL(rvt_dec_qp_cnt);