blob: 522404ac7c3856843a9d5820b8e17ee8fdf9d9b9 [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
165int rvt_driver_qp_init(struct rvt_dev_info *rdi)
166{
167 int i;
168 int ret = -ENOMEM;
169
170 if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER) {
171 rvt_pr_info(rdi, "Driver is doing QP init.\n");
172 return 0;
173 }
174
175 if (!rdi->dparms.qp_table_size)
176 return -EINVAL;
177
178 /*
179 * If driver is not doing any QP allocation then make sure it is
180 * providing the necessary QP functions.
181 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800182 if (!rdi->driver_f.free_all_qps ||
183 !rdi->driver_f.qp_priv_alloc ||
184 !rdi->driver_f.qp_priv_free ||
185 !rdi->driver_f.notify_qp_reset)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800186 return -EINVAL;
187
188 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800189 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
190 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800191 if (!rdi->qp_dev)
192 return -ENOMEM;
193
194 /* allocate hash table */
195 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
196 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
197 rdi->qp_dev->qp_table =
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800198 kmalloc_node(rdi->qp_dev->qp_table_size *
199 sizeof(*rdi->qp_dev->qp_table),
200 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800201 if (!rdi->qp_dev->qp_table)
202 goto no_qp_table;
203
204 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
205 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
206
207 spin_lock_init(&rdi->qp_dev->qpt_lock);
208
209 /* initialize qpn map */
210 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
211 goto fail_table;
212
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800213 spin_lock_init(&rdi->n_qps_lock);
214
215 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800216
217fail_table:
218 kfree(rdi->qp_dev->qp_table);
219 free_qpn_table(&rdi->qp_dev->qpn_table);
220
221no_qp_table:
222 kfree(rdi->qp_dev);
223
224 return ret;
225}
226
227/**
228 * free_all_qps - check for QPs still in use
229 * @qpt: the QP table to empty
230 *
231 * There should not be any QPs still in use.
232 * Free memory for table.
233 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800234static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800235{
236 unsigned long flags;
237 struct rvt_qp *qp;
238 unsigned n, qp_inuse = 0;
239 spinlock_t *ql; /* work around too long line below */
240
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800241 if (rdi->driver_f.free_all_qps)
242 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800243
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800244 qp_inuse += rvt_mcast_tree_empty(rdi);
245
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800246 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800247 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800248
249 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800250 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800251 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
252 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
253 lockdep_is_held(ql));
254 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800255
256 for (; qp; qp = rcu_dereference_protected(qp->next,
257 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800258 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800259 }
260 spin_unlock_irqrestore(ql, flags);
261 synchronize_rcu();
262 return qp_inuse;
263}
264
265void rvt_qp_exit(struct rvt_dev_info *rdi)
266{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800267 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800268
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800269 if (qps_inuse)
270 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
271 qps_inuse);
272 if (!rdi->qp_dev)
273 return;
274
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800275 if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER)
276 return; /* driver did the qp init so nothing else to do */
277
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800278 kfree(rdi->qp_dev->qp_table);
279 free_qpn_table(&rdi->qp_dev->qpn_table);
280 kfree(rdi->qp_dev);
281}
282
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800283static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
284 struct rvt_qpn_map *map, unsigned off)
285{
286 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
287}
288
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800289/**
290 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
291 * IB_QPT_SMI/IB_QPT_GSI
292 *@rdi: rvt device info structure
293 *@qpt: queue pair number table pointer
294 *@port_num: IB port number, 1 based, comes from core
295 *
296 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800297 */
298static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800299 enum ib_qp_type type, u8 port_num, gfp_t gfp)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800300{
301 u32 i, offset, max_scan, qpn;
302 struct rvt_qpn_map *map;
303 u32 ret;
304
305 if (rdi->driver_f.alloc_qpn)
Ira Weinyb7b3cf42016-02-03 14:15:28 -0800306 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800307
308 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
309 unsigned n;
310
311 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800312 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800313 spin_lock(&qpt->lock);
314 if (qpt->flags & n)
315 ret = -EINVAL;
316 else
317 qpt->flags |= n;
318 spin_unlock(&qpt->lock);
319 goto bail;
320 }
321
322 qpn = qpt->last + qpt->incr;
323 if (qpn >= RVT_QPN_MAX)
324 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
325 /* offset carries bit 0 */
326 offset = qpn & RVT_BITS_PER_PAGE_MASK;
327 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
328 max_scan = qpt->nmaps - !offset;
329 for (i = 0;;) {
330 if (unlikely(!map->page)) {
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800331 get_map_page(qpt, map, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800332 if (unlikely(!map->page))
333 break;
334 }
335 do {
336 if (!test_and_set_bit(offset, map->page)) {
337 qpt->last = qpn;
338 ret = qpn;
339 goto bail;
340 }
341 offset += qpt->incr;
342 /*
343 * This qpn might be bogus if offset >= BITS_PER_PAGE.
344 * That is OK. It gets re-assigned below
345 */
346 qpn = mk_qpn(qpt, map, offset);
347 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
348 /*
349 * In order to keep the number of pages allocated to a
350 * minimum, we scan the all existing pages before increasing
351 * the size of the bitmap table.
352 */
353 if (++i > max_scan) {
354 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
355 break;
356 map = &qpt->map[qpt->nmaps++];
357 /* start at incr with current bit 0 */
358 offset = qpt->incr | (offset & 1);
359 } else if (map < &qpt->map[qpt->nmaps]) {
360 ++map;
361 /* start at incr with current bit 0 */
362 offset = qpt->incr | (offset & 1);
363 } else {
364 map = &qpt->map[0];
365 /* wrap to first map page, invert bit 0 */
366 offset = qpt->incr | ((offset & 1) ^ 1);
367 }
368 /* there can be no bits at shift and below */
369 WARN_ON(offset & (rdi->dparms.qos_shift - 1));
370 qpn = mk_qpn(qpt, map, offset);
371 }
372
373 ret = -ENOMEM;
374
375bail:
376 return ret;
377}
378
379static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
380{
381 struct rvt_qpn_map *map;
382
383 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
384 if (map->page)
385 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
386}
387
388/**
389 * reset_qp - initialize the QP state to the reset state
390 * @qp: the QP to reset
391 * @type: the QP type
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800392 * r and s lock are required to be held by the caller
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800393 */
Dennis Dalessandro5a9cf6f2016-01-22 12:50:24 -0800394void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
395 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800396{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800397 if (qp->state != IB_QPS_RESET) {
398 qp->state = IB_QPS_RESET;
399
400 /* Let drivers flush their waitlist */
401 rdi->driver_f.flush_qp_waiters(qp);
402 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
403 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800404 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800405 spin_unlock_irq(&qp->r_lock);
406
407 /* Stop the send queue and the retry timer */
408 rdi->driver_f.stop_send_queue(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800409
410 /* Wait for things to stop */
411 rdi->driver_f.quiesce_qp(qp);
412
413 /* take qp out the hash and wait for it to be unused */
414 rvt_remove_qp(rdi, qp);
415 wait_event(qp->wait, !atomic_read(&qp->refcount));
416
417 /* grab the lock b/c it was locked at call time */
418 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800419 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800420 spin_lock(&qp->s_lock);
421
422 rvt_clear_mr_refs(qp, 1);
423 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800424
425 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800426 * Let the driver do any tear down it needs to for a qp
427 * that has been reset
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800428 */
429 rdi->driver_f.notify_qp_reset(qp);
430
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800431 qp->remote_qpn = 0;
432 qp->qkey = 0;
433 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800434 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
435 qp->s_hdrwords = 0;
436 qp->s_wqe = NULL;
437 qp->s_draining = 0;
438 qp->s_next_psn = 0;
439 qp->s_last_psn = 0;
440 qp->s_sending_psn = 0;
441 qp->s_sending_hpsn = 0;
442 qp->s_psn = 0;
443 qp->r_psn = 0;
444 qp->r_msn = 0;
445 if (type == IB_QPT_RC) {
446 qp->s_state = IB_OPCODE_RC_SEND_LAST;
447 qp->r_state = IB_OPCODE_RC_SEND_LAST;
448 } else {
449 qp->s_state = IB_OPCODE_UC_SEND_LAST;
450 qp->r_state = IB_OPCODE_UC_SEND_LAST;
451 }
452 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
453 qp->r_nak_state = 0;
454 qp->r_aflags = 0;
455 qp->r_flags = 0;
456 qp->s_head = 0;
457 qp->s_tail = 0;
458 qp->s_cur = 0;
459 qp->s_acked = 0;
460 qp->s_last = 0;
461 qp->s_ssn = 1;
462 qp->s_lsn = 0;
463 qp->s_mig_state = IB_MIG_MIGRATED;
464 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
465 qp->r_head_ack_queue = 0;
466 qp->s_tail_ack_queue = 0;
467 qp->s_num_rd_atomic = 0;
468 if (qp->r_rq.wq) {
469 qp->r_rq.wq->head = 0;
470 qp->r_rq.wq->tail = 0;
471 }
472 qp->r_sge.num_sge = 0;
473}
Dennis Dalessandro5a9cf6f2016-01-22 12:50:24 -0800474EXPORT_SYMBOL(rvt_reset_qp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800475
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800476/**
477 * rvt_create_qp - create a queue pair for a device
478 * @ibpd: the protection domain who's device we create the queue pair for
479 * @init_attr: the attributes of the queue pair
480 * @udata: user data for libibverbs.so
481 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800482 * Queue pair creation is mostly an rvt issue. However, drivers have their own
483 * unique idea of what queue pair numbers mean. For instance there is a reserved
484 * range for PSM.
485 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800486 * Returns the queue pair on success, otherwise returns an errno.
487 *
488 * Called by the ib_create_qp() core verbs function.
489 */
490struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
491 struct ib_qp_init_attr *init_attr,
492 struct ib_udata *udata)
493{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800494 struct rvt_qp *qp;
495 int err;
496 struct rvt_swqe *swq = NULL;
497 size_t sz;
498 size_t sg_list_sz;
499 struct ib_qp *ret = ERR_PTR(-ENOMEM);
500 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
501 void *priv = NULL;
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800502 gfp_t gfp;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800503
504 if (!rdi)
505 return ERR_PTR(-EINVAL);
506
507 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
508 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800509 init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800510 return ERR_PTR(-EINVAL);
511
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800512 /* GFP_NOIO is applicable to RC QP's only */
513
514 if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
515 init_attr->qp_type != IB_QPT_RC)
516 return ERR_PTR(-EINVAL);
517
518 gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
519 GFP_NOIO : GFP_KERNEL;
520
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800521 /* Check receive queue parameters if no SRQ is specified. */
522 if (!init_attr->srq) {
523 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
524 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
525 return ERR_PTR(-EINVAL);
526
527 if (init_attr->cap.max_send_sge +
528 init_attr->cap.max_send_wr +
529 init_attr->cap.max_recv_sge +
530 init_attr->cap.max_recv_wr == 0)
531 return ERR_PTR(-EINVAL);
532 }
533
534 switch (init_attr->qp_type) {
535 case IB_QPT_SMI:
536 case IB_QPT_GSI:
537 if (init_attr->port_num == 0 ||
538 init_attr->port_num > ibpd->device->phys_port_cnt)
539 return ERR_PTR(-EINVAL);
540 case IB_QPT_UC:
541 case IB_QPT_RC:
542 case IB_QPT_UD:
543 sz = sizeof(struct rvt_sge) *
544 init_attr->cap.max_send_sge +
545 sizeof(struct rvt_swqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800546 if (gfp == GFP_NOIO)
547 swq = __vmalloc(
548 (init_attr->cap.max_send_wr + 1) * sz,
549 gfp, PAGE_KERNEL);
550 else
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800551 swq = vmalloc_node(
552 (init_attr->cap.max_send_wr + 1) * sz,
553 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800554 if (!swq)
555 return ERR_PTR(-ENOMEM);
556
557 sz = sizeof(*qp);
558 sg_list_sz = 0;
559 if (init_attr->srq) {
560 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
561
562 if (srq->rq.max_sge > 1)
563 sg_list_sz = sizeof(*qp->r_sg_list) *
564 (srq->rq.max_sge - 1);
565 } else if (init_attr->cap.max_recv_sge > 1)
566 sg_list_sz = sizeof(*qp->r_sg_list) *
567 (init_attr->cap.max_recv_sge - 1);
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800568 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800569 if (!qp)
570 goto bail_swq;
571
572 RCU_INIT_POINTER(qp->next, NULL);
573
574 /*
575 * Driver needs to set up it's private QP structure and do any
576 * initialization that is needed.
577 */
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800578 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800579 if (!priv)
580 goto bail_qp;
581 qp->priv = priv;
582 qp->timeout_jiffies =
583 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
584 1000UL);
585 if (init_attr->srq) {
586 sz = 0;
587 } else {
588 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
589 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
590 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
591 sizeof(struct rvt_rwqe);
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800592 if (udata)
593 qp->r_rq.wq = vmalloc_user(
594 sizeof(struct rvt_rwq) +
595 qp->r_rq.size * sz);
596 else if (gfp == GFP_NOIO)
597 qp->r_rq.wq = __vmalloc(
598 sizeof(struct rvt_rwq) +
599 qp->r_rq.size * sz,
600 gfp, PAGE_KERNEL);
601 else
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800602 qp->r_rq.wq = vmalloc_node(
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800603 sizeof(struct rvt_rwq) +
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800604 qp->r_rq.size * sz,
605 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800606 if (!qp->r_rq.wq)
607 goto bail_driver_priv;
608 }
609
610 /*
611 * ib_create_qp() will initialize qp->ibqp
612 * except for qp->ibqp.qp_num.
613 */
614 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800615 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800616 spin_lock_init(&qp->s_lock);
617 spin_lock_init(&qp->r_rq.lock);
618 atomic_set(&qp->refcount, 0);
619 init_waitqueue_head(&qp->wait);
620 init_timer(&qp->s_timer);
621 qp->s_timer.data = (unsigned long)qp;
622 INIT_LIST_HEAD(&qp->rspwait);
623 qp->state = IB_QPS_RESET;
624 qp->s_wq = swq;
625 qp->s_size = init_attr->cap.max_send_wr + 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800626 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800627 qp->s_max_sge = init_attr->cap.max_send_sge;
628 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
629 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
630
631 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
632 init_attr->qp_type,
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800633 init_attr->port_num, gfp);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800634 if (err < 0) {
635 ret = ERR_PTR(err);
636 goto bail_rq_wq;
637 }
638 qp->ibqp.qp_num = err;
639 qp->port_num = init_attr->port_num;
Dennis Dalessandro5a9cf6f2016-01-22 12:50:24 -0800640 rvt_reset_qp(rdi, qp, init_attr->qp_type);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800641 break;
642
643 default:
644 /* Don't support raw QPs */
645 return ERR_PTR(-EINVAL);
646 }
647
648 init_attr->cap.max_inline_data = 0;
649
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800650 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800651 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800652 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800653 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800654 if (udata && udata->outlen >= sizeof(__u64)) {
655 if (!qp->r_rq.wq) {
656 __u64 offset = 0;
657
658 err = ib_copy_to_udata(udata, &offset,
659 sizeof(offset));
660 if (err) {
661 ret = ERR_PTR(err);
662 goto bail_qpn;
663 }
664 } else {
665 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
666
667 qp->ip = rvt_create_mmap_info(rdi, s,
668 ibpd->uobject->context,
669 qp->r_rq.wq);
670 if (!qp->ip) {
671 ret = ERR_PTR(-ENOMEM);
672 goto bail_qpn;
673 }
674
675 err = ib_copy_to_udata(udata, &qp->ip->offset,
676 sizeof(qp->ip->offset));
677 if (err) {
678 ret = ERR_PTR(err);
679 goto bail_ip;
680 }
681 }
682 }
683
684 spin_lock(&rdi->n_qps_lock);
685 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
686 spin_unlock(&rdi->n_qps_lock);
687 ret = ERR_PTR(-ENOMEM);
688 goto bail_ip;
689 }
690
691 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800692 /*
693 * Maintain a busy_jiffies variable that will be added to the timeout
694 * period in mod_retry_timer and add_retry_timer. This busy jiffies
695 * is scaled by the number of rc qps created for the device to reduce
696 * the number of timeouts occurring when there is a large number of
697 * qps. busy_jiffies is incremented every rc qp scaling interval.
698 * The scaling interval is selected based on extensive performance
699 * evaluation of targeted workloads.
700 */
701 if (init_attr->qp_type == IB_QPT_RC) {
702 rdi->n_rc_qps++;
703 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
704 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800705 spin_unlock(&rdi->n_qps_lock);
706
707 if (qp->ip) {
708 spin_lock_irq(&rdi->pending_lock);
709 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
710 spin_unlock_irq(&rdi->pending_lock);
711 }
712
713 ret = &qp->ibqp;
714
715 /*
716 * We have our QP and its good, now keep track of what types of opcodes
717 * can be processed on this QP. We do this by keeping track of what the
718 * 3 high order bits of the opcode are.
719 */
720 switch (init_attr->qp_type) {
721 case IB_QPT_SMI:
722 case IB_QPT_GSI:
723 case IB_QPT_UD:
724 qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & RVT_OPCODE_QP_MASK;
725 break;
726 case IB_QPT_RC:
727 qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & RVT_OPCODE_QP_MASK;
728 break;
729 case IB_QPT_UC:
730 qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & RVT_OPCODE_QP_MASK;
731 break;
732 default:
733 ret = ERR_PTR(-EINVAL);
734 goto bail_ip;
735 }
736
737 return ret;
738
739bail_ip:
740 kref_put(&qp->ip->ref, rvt_release_mmap_info);
741
742bail_qpn:
743 free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
744
745bail_rq_wq:
746 vfree(qp->r_rq.wq);
747
748bail_driver_priv:
749 rdi->driver_f.qp_priv_free(rdi, qp);
750
751bail_qp:
752 kfree(qp);
753
754bail_swq:
755 vfree(swq);
756
757 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800758}
759
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800760void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
761{
762 unsigned n;
763
764 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
765 rvt_put_ss(&qp->s_rdma_read_sge);
766
767 rvt_put_ss(&qp->r_sge);
768
769 if (clr_sends) {
770 while (qp->s_last != qp->s_head) {
771 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
772 unsigned i;
773
774 for (i = 0; i < wqe->wr.num_sge; i++) {
775 struct rvt_sge *sge = &wqe->sg_list[i];
776
777 rvt_put_mr(sge->mr);
778 }
779 if (qp->ibqp.qp_type == IB_QPT_UD ||
780 qp->ibqp.qp_type == IB_QPT_SMI ||
781 qp->ibqp.qp_type == IB_QPT_GSI)
782 atomic_dec(&ibah_to_rvtah(
783 wqe->ud_wr.ah)->refcount);
784 if (++qp->s_last >= qp->s_size)
785 qp->s_last = 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800786 smp_wmb(); /* see qp_set_savail */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800787 }
788 if (qp->s_rdma_mr) {
789 rvt_put_mr(qp->s_rdma_mr);
790 qp->s_rdma_mr = NULL;
791 }
792 }
793
794 if (qp->ibqp.qp_type != IB_QPT_RC)
795 return;
796
797 for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
798 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
799
800 if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
801 e->rdma_sge.mr) {
802 rvt_put_mr(e->rdma_sge.mr);
803 e->rdma_sge.mr = NULL;
804 }
805 }
806}
807EXPORT_SYMBOL(rvt_clear_mr_refs);
808
809/**
810 * rvt_error_qp - put a QP into the error state
811 * @qp: the QP to put into the error state
812 * @err: the receive completion error to signal if a RWQE is active
813 *
814 * Flushes both send and receive work queues.
815 * Returns true if last WQE event should be generated.
816 * The QP r_lock and s_lock should be held and interrupts disabled.
817 * If we are already in error state, just return.
818 */
819int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
820{
821 struct ib_wc wc;
822 int ret = 0;
823 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
824
825 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
826 goto bail;
827
828 qp->state = IB_QPS_ERR;
829
830 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
831 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
832 del_timer(&qp->s_timer);
833 }
834
835 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
836 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
837
838 rdi->driver_f.notify_error_qp(qp);
839
840 /* Schedule the sending tasklet to drain the send work queue. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800841 if (ACCESS_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800842 rdi->driver_f.schedule_send(qp);
843
844 rvt_clear_mr_refs(qp, 0);
845
846 memset(&wc, 0, sizeof(wc));
847 wc.qp = &qp->ibqp;
848 wc.opcode = IB_WC_RECV;
849
850 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
851 wc.wr_id = qp->r_wr_id;
852 wc.status = err;
853 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
854 }
855 wc.status = IB_WC_WR_FLUSH_ERR;
856
857 if (qp->r_rq.wq) {
858 struct rvt_rwq *wq;
859 u32 head;
860 u32 tail;
861
862 spin_lock(&qp->r_rq.lock);
863
864 /* sanity check pointers before trusting them */
865 wq = qp->r_rq.wq;
866 head = wq->head;
867 if (head >= qp->r_rq.size)
868 head = 0;
869 tail = wq->tail;
870 if (tail >= qp->r_rq.size)
871 tail = 0;
872 while (tail != head) {
873 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
874 if (++tail >= qp->r_rq.size)
875 tail = 0;
876 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
877 }
878 wq->tail = tail;
879
880 spin_unlock(&qp->r_rq.lock);
881 } else if (qp->ibqp.event_handler) {
882 ret = 1;
883 }
884
885bail:
886 return ret;
887}
888EXPORT_SYMBOL(rvt_error_qp);
889
890/*
891 * Put the QP into the hash table.
892 * The hash table holds a reference to the QP.
893 */
894static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
895{
896 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
897 unsigned long flags;
898
899 atomic_inc(&qp->refcount);
900 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
901
902 if (qp->ibqp.qp_num <= 1) {
903 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
904 } else {
905 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
906
907 qp->next = rdi->qp_dev->qp_table[n];
908 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
909 trace_rvt_qpinsert(qp, n);
910 }
911
912 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
913}
914
915/*
916 * Remove the QP from the table so it can't be found asynchronously by
917 * the receive routine.
918 */
919void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
920{
921 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
922 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
923 unsigned long flags;
924 int removed = 1;
925
926 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
927
928 if (rcu_dereference_protected(rvp->qp[0],
929 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
930 RCU_INIT_POINTER(rvp->qp[0], NULL);
931 } else if (rcu_dereference_protected(rvp->qp[1],
932 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
933 RCU_INIT_POINTER(rvp->qp[1], NULL);
934 } else {
935 struct rvt_qp *q;
936 struct rvt_qp __rcu **qpp;
937
938 removed = 0;
939 qpp = &rdi->qp_dev->qp_table[n];
940 for (; (q = rcu_dereference_protected(*qpp,
941 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
942 qpp = &q->next) {
943 if (q == qp) {
944 RCU_INIT_POINTER(*qpp,
945 rcu_dereference_protected(qp->next,
946 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
947 removed = 1;
948 trace_rvt_qpremove(qp, n);
949 break;
950 }
951 }
952 }
953
954 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
955 if (removed) {
956 synchronize_rcu();
957 if (atomic_dec_and_test(&qp->refcount))
958 wake_up(&qp->wait);
959 }
960}
961EXPORT_SYMBOL(rvt_remove_qp);
962
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800963/**
964 * qib_modify_qp - modify the attributes of a queue pair
965 * @ibqp: the queue pair who's attributes we're modifying
966 * @attr: the new attributes
967 * @attr_mask: the mask of attributes to modify
968 * @udata: user data for libibverbs.so
969 *
970 * Returns 0 on success, otherwise returns an errno.
971 */
972int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
973 int attr_mask, struct ib_udata *udata)
974{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800975 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
976 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
977 enum ib_qp_state cur_state, new_state;
978 struct ib_event ev;
979 int lastwqe = 0;
980 int mig = 0;
981 int pmtu = 0; /* for gcc warning only */
982 enum rdma_link_layer link;
983
984 link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
985
986 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800987 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800988 spin_lock(&qp->s_lock);
989
990 cur_state = attr_mask & IB_QP_CUR_STATE ?
991 attr->cur_qp_state : qp->state;
992 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
993
994 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
995 attr_mask, link))
996 goto inval;
997
Ira Weinye85ec332016-01-22 13:04:38 -0800998 if (rdi->driver_f.check_modify_qp &&
999 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1000 goto inval;
1001
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001002 if (attr_mask & IB_QP_AV) {
1003 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1004 goto inval;
1005 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1006 goto inval;
1007 }
1008
1009 if (attr_mask & IB_QP_ALT_PATH) {
1010 if (attr->alt_ah_attr.dlid >=
1011 be16_to_cpu(IB_MULTICAST_LID_BASE))
1012 goto inval;
1013 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1014 goto inval;
1015 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1016 goto inval;
1017 }
1018
1019 if (attr_mask & IB_QP_PKEY_INDEX)
1020 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1021 goto inval;
1022
1023 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1024 if (attr->min_rnr_timer > 31)
1025 goto inval;
1026
1027 if (attr_mask & IB_QP_PORT)
1028 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1029 qp->ibqp.qp_type == IB_QPT_GSI ||
1030 attr->port_num == 0 ||
1031 attr->port_num > ibqp->device->phys_port_cnt)
1032 goto inval;
1033
1034 if (attr_mask & IB_QP_DEST_QPN)
1035 if (attr->dest_qp_num > RVT_QPN_MASK)
1036 goto inval;
1037
1038 if (attr_mask & IB_QP_RETRY_CNT)
1039 if (attr->retry_cnt > 7)
1040 goto inval;
1041
1042 if (attr_mask & IB_QP_RNR_RETRY)
1043 if (attr->rnr_retry > 7)
1044 goto inval;
1045
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001046 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001047 * Don't allow invalid path_mtu values. OK to set greater
1048 * than the active mtu (or even the max_cap, if we have tuned
1049 * that to a small mtu. We'll set qp->path_mtu
1050 * to the lesser of requested attribute mtu and active,
1051 * for packetizing messages.
1052 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001053 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001054 if (attr_mask & IB_QP_PATH_MTU) {
1055 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1056 if (pmtu < 0)
1057 goto inval;
1058 }
1059
1060 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1061 if (attr->path_mig_state == IB_MIG_REARM) {
1062 if (qp->s_mig_state == IB_MIG_ARMED)
1063 goto inval;
1064 if (new_state != IB_QPS_RTS)
1065 goto inval;
1066 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1067 if (qp->s_mig_state == IB_MIG_REARM)
1068 goto inval;
1069 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1070 goto inval;
1071 if (qp->s_mig_state == IB_MIG_ARMED)
1072 mig = 1;
1073 } else {
1074 goto inval;
1075 }
1076 }
1077
1078 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1079 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1080 goto inval;
1081
1082 switch (new_state) {
1083 case IB_QPS_RESET:
1084 if (qp->state != IB_QPS_RESET)
1085 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1086 break;
1087
1088 case IB_QPS_RTR:
1089 /* Allow event to re-trigger if QP set to RTR more than once */
1090 qp->r_flags &= ~RVT_R_COMM_EST;
1091 qp->state = new_state;
1092 break;
1093
1094 case IB_QPS_SQD:
1095 qp->s_draining = qp->s_last != qp->s_cur;
1096 qp->state = new_state;
1097 break;
1098
1099 case IB_QPS_SQE:
1100 if (qp->ibqp.qp_type == IB_QPT_RC)
1101 goto inval;
1102 qp->state = new_state;
1103 break;
1104
1105 case IB_QPS_ERR:
1106 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1107 break;
1108
1109 default:
1110 qp->state = new_state;
1111 break;
1112 }
1113
1114 if (attr_mask & IB_QP_PKEY_INDEX)
1115 qp->s_pkey_index = attr->pkey_index;
1116
1117 if (attr_mask & IB_QP_PORT)
1118 qp->port_num = attr->port_num;
1119
1120 if (attr_mask & IB_QP_DEST_QPN)
1121 qp->remote_qpn = attr->dest_qp_num;
1122
1123 if (attr_mask & IB_QP_SQ_PSN) {
1124 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1125 qp->s_psn = qp->s_next_psn;
1126 qp->s_sending_psn = qp->s_next_psn;
1127 qp->s_last_psn = qp->s_next_psn - 1;
1128 qp->s_sending_hpsn = qp->s_last_psn;
1129 }
1130
1131 if (attr_mask & IB_QP_RQ_PSN)
1132 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1133
1134 if (attr_mask & IB_QP_ACCESS_FLAGS)
1135 qp->qp_access_flags = attr->qp_access_flags;
1136
1137 if (attr_mask & IB_QP_AV) {
1138 qp->remote_ah_attr = attr->ah_attr;
1139 qp->s_srate = attr->ah_attr.static_rate;
1140 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1141 }
1142
1143 if (attr_mask & IB_QP_ALT_PATH) {
1144 qp->alt_ah_attr = attr->alt_ah_attr;
1145 qp->s_alt_pkey_index = attr->alt_pkey_index;
1146 }
1147
1148 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1149 qp->s_mig_state = attr->path_mig_state;
1150 if (mig) {
1151 qp->remote_ah_attr = qp->alt_ah_attr;
1152 qp->port_num = qp->alt_ah_attr.port_num;
1153 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001154 }
1155 }
1156
1157 if (attr_mask & IB_QP_PATH_MTU) {
1158 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1159 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001160 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001161 }
1162
1163 if (attr_mask & IB_QP_RETRY_CNT) {
1164 qp->s_retry_cnt = attr->retry_cnt;
1165 qp->s_retry = attr->retry_cnt;
1166 }
1167
1168 if (attr_mask & IB_QP_RNR_RETRY) {
1169 qp->s_rnr_retry_cnt = attr->rnr_retry;
1170 qp->s_rnr_retry = attr->rnr_retry;
1171 }
1172
1173 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1174 qp->r_min_rnr_timer = attr->min_rnr_timer;
1175
1176 if (attr_mask & IB_QP_TIMEOUT) {
1177 qp->timeout = attr->timeout;
1178 qp->timeout_jiffies =
1179 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1180 1000UL);
1181 }
1182
1183 if (attr_mask & IB_QP_QKEY)
1184 qp->qkey = attr->qkey;
1185
1186 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1187 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1188
1189 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1190 qp->s_max_rd_atomic = attr->max_rd_atomic;
1191
Ira Weinye85ec332016-01-22 13:04:38 -08001192 if (rdi->driver_f.modify_qp)
1193 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1194
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001195 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001196 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001197 spin_unlock_irq(&qp->r_lock);
1198
1199 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1200 rvt_insert_qp(rdi, qp);
1201
1202 if (lastwqe) {
1203 ev.device = qp->ibqp.device;
1204 ev.element.qp = &qp->ibqp;
1205 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1206 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1207 }
1208 if (mig) {
1209 ev.device = qp->ibqp.device;
1210 ev.element.qp = &qp->ibqp;
1211 ev.event = IB_EVENT_PATH_MIG;
1212 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1213 }
1214 return 0;
1215
1216inval:
1217 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 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001221}
1222
1223/**
1224 * rvt_destroy_qp - destroy a queue pair
1225 * @ibqp: the queue pair to destroy
1226 *
1227 * Returns 0 on success.
1228 *
1229 * Note that this can be called while the QP is actively sending or
1230 * receiving!
1231 */
1232int rvt_destroy_qp(struct ib_qp *ibqp)
1233{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001234 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1235 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001236
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001237 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001238 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001239 spin_lock(&qp->s_lock);
1240 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1241 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001242 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001243 spin_unlock_irq(&qp->r_lock);
1244
1245 /* qpn is now available for use again */
1246 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1247
1248 spin_lock(&rdi->n_qps_lock);
1249 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001250 if (qp->ibqp.qp_type == IB_QPT_RC) {
1251 rdi->n_rc_qps--;
1252 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1253 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001254 spin_unlock(&rdi->n_qps_lock);
1255
1256 if (qp->ip)
1257 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1258 else
1259 vfree(qp->r_rq.wq);
1260 vfree(qp->s_wq);
1261 rdi->driver_f.qp_priv_free(rdi, qp);
1262 kfree(qp);
1263 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001264}
1265
1266int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1267 int attr_mask, struct ib_qp_init_attr *init_attr)
1268{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001269 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1270 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1271
1272 attr->qp_state = qp->state;
1273 attr->cur_qp_state = attr->qp_state;
1274 attr->path_mtu = qp->path_mtu;
1275 attr->path_mig_state = qp->s_mig_state;
1276 attr->qkey = qp->qkey;
1277 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1278 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1279 attr->dest_qp_num = qp->remote_qpn;
1280 attr->qp_access_flags = qp->qp_access_flags;
1281 attr->cap.max_send_wr = qp->s_size - 1;
1282 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1283 attr->cap.max_send_sge = qp->s_max_sge;
1284 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1285 attr->cap.max_inline_data = 0;
1286 attr->ah_attr = qp->remote_ah_attr;
1287 attr->alt_ah_attr = qp->alt_ah_attr;
1288 attr->pkey_index = qp->s_pkey_index;
1289 attr->alt_pkey_index = qp->s_alt_pkey_index;
1290 attr->en_sqd_async_notify = 0;
1291 attr->sq_draining = qp->s_draining;
1292 attr->max_rd_atomic = qp->s_max_rd_atomic;
1293 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1294 attr->min_rnr_timer = qp->r_min_rnr_timer;
1295 attr->port_num = qp->port_num;
1296 attr->timeout = qp->timeout;
1297 attr->retry_cnt = qp->s_retry_cnt;
1298 attr->rnr_retry = qp->s_rnr_retry_cnt;
1299 attr->alt_port_num = qp->alt_ah_attr.port_num;
1300 attr->alt_timeout = qp->alt_timeout;
1301
1302 init_attr->event_handler = qp->ibqp.event_handler;
1303 init_attr->qp_context = qp->ibqp.qp_context;
1304 init_attr->send_cq = qp->ibqp.send_cq;
1305 init_attr->recv_cq = qp->ibqp.recv_cq;
1306 init_attr->srq = qp->ibqp.srq;
1307 init_attr->cap = attr->cap;
1308 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1309 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1310 else
1311 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1312 init_attr->qp_type = qp->ibqp.qp_type;
1313 init_attr->port_num = qp->port_num;
1314 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001315}
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001316
1317/**
1318 * rvt_post_receive - post a receive on a QP
1319 * @ibqp: the QP to post the receive on
1320 * @wr: the WR to post
1321 * @bad_wr: the first bad WR is put here
1322 *
1323 * This may be called from interrupt context.
1324 */
1325int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1326 struct ib_recv_wr **bad_wr)
1327{
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001328 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1329 struct rvt_rwq *wq = qp->r_rq.wq;
1330 unsigned long flags;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001331
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001332 /* Check that state is OK to post receive. */
1333 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1334 *bad_wr = wr;
1335 return -EINVAL;
1336 }
1337
1338 for (; wr; wr = wr->next) {
1339 struct rvt_rwqe *wqe;
1340 u32 next;
1341 int i;
1342
1343 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1344 *bad_wr = wr;
1345 return -EINVAL;
1346 }
1347
1348 spin_lock_irqsave(&qp->r_rq.lock, flags);
1349 next = wq->head + 1;
1350 if (next >= qp->r_rq.size)
1351 next = 0;
1352 if (next == wq->tail) {
1353 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1354 *bad_wr = wr;
1355 return -ENOMEM;
1356 }
1357
1358 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1359 wqe->wr_id = wr->wr_id;
1360 wqe->num_sge = wr->num_sge;
1361 for (i = 0; i < wr->num_sge; i++)
1362 wqe->sg_list[i] = wr->sg_list[i];
1363 /* Make sure queue entry is written before the head index. */
1364 smp_wmb();
1365 wq->head = next;
1366 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1367 }
1368 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001369}
1370
1371/**
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001372 * qp_get_savail - return number of avail send entries
1373 *
1374 * @qp - the qp
1375 *
1376 * This assumes the s_hlock is held but the s_last
1377 * qp variable is uncontrolled.
1378 */
1379static inline u32 qp_get_savail(struct rvt_qp *qp)
1380{
1381 u32 slast;
1382 u32 ret;
1383
1384 smp_read_barrier_depends(); /* see rc.c */
1385 slast = ACCESS_ONCE(qp->s_last);
1386 if (qp->s_head >= slast)
1387 ret = qp->s_size - (qp->s_head - slast);
1388 else
1389 ret = slast - qp->s_head;
1390 return ret - 1;
1391}
1392
1393/**
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001394 * rvt_post_one_wr - post one RC, UC, or UD send work request
1395 * @qp: the QP to post on
1396 * @wr: the work request to send
1397 */
1398static int rvt_post_one_wr(struct rvt_qp *qp, struct ib_send_wr *wr)
1399{
1400 struct rvt_swqe *wqe;
1401 u32 next;
1402 int i;
1403 int j;
1404 int acc;
1405 struct rvt_lkey_table *rkt;
1406 struct rvt_pd *pd;
1407 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001408 u8 log_pmtu;
1409 int ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001410
1411 /* IB spec says that num_sge == 0 is OK. */
1412 if (unlikely(wr->num_sge > qp->s_max_sge))
1413 return -EINVAL;
1414
1415 /*
1416 * Don't allow RDMA reads or atomic operations on UC or
1417 * undefined operations.
1418 * Make sure buffer is large enough to hold the result for atomics.
1419 */
1420 if (qp->ibqp.qp_type == IB_QPT_UC) {
1421 if ((unsigned)wr->opcode >= IB_WR_RDMA_READ)
1422 return -EINVAL;
1423 } else if (qp->ibqp.qp_type != IB_QPT_RC) {
1424 /* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
1425 if (wr->opcode != IB_WR_SEND &&
1426 wr->opcode != IB_WR_SEND_WITH_IMM)
1427 return -EINVAL;
1428 /* Check UD destination address PD */
1429 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1430 return -EINVAL;
1431 } else if ((unsigned)wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
1432 return -EINVAL;
1433 } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
1434 (wr->num_sge == 0 ||
1435 wr->sg_list[0].length < sizeof(u64) ||
1436 wr->sg_list[0].addr & (sizeof(u64) - 1))) {
1437 return -EINVAL;
1438 } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
1439 return -EINVAL;
1440 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001441 /* check for avail */
1442 if (unlikely(!qp->s_avail)) {
1443 qp->s_avail = qp_get_savail(qp);
1444 WARN_ON(qp->s_avail > (qp->s_size - 1));
1445 if (!qp->s_avail)
1446 return -ENOMEM;
1447 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001448 next = qp->s_head + 1;
1449 if (next >= qp->s_size)
1450 next = 0;
Ira Weiny60c30f52016-02-03 14:14:45 -08001451
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001452 rkt = &rdi->lkey_table;
1453 pd = ibpd_to_rvtpd(qp->ibqp.pd);
1454 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1455
1456 if (qp->ibqp.qp_type != IB_QPT_UC &&
1457 qp->ibqp.qp_type != IB_QPT_RC)
1458 memcpy(&wqe->ud_wr, ud_wr(wr), sizeof(wqe->ud_wr));
1459 else if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
1460 wr->opcode == IB_WR_RDMA_WRITE ||
1461 wr->opcode == IB_WR_RDMA_READ)
1462 memcpy(&wqe->rdma_wr, rdma_wr(wr), sizeof(wqe->rdma_wr));
1463 else if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1464 wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1465 memcpy(&wqe->atomic_wr, atomic_wr(wr), sizeof(wqe->atomic_wr));
1466 else
1467 memcpy(&wqe->wr, wr, sizeof(wqe->wr));
1468
1469 wqe->length = 0;
1470 j = 0;
1471 if (wr->num_sge) {
1472 acc = wr->opcode >= IB_WR_RDMA_READ ?
1473 IB_ACCESS_LOCAL_WRITE : 0;
1474 for (i = 0; i < wr->num_sge; i++) {
1475 u32 length = wr->sg_list[i].length;
1476 int ok;
1477
1478 if (length == 0)
1479 continue;
1480 ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1481 &wr->sg_list[i], acc);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001482 if (!ok) {
1483 ret = -EINVAL;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001484 goto bail_inval_free;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001485 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001486 wqe->length += length;
1487 j++;
1488 }
1489 wqe->wr.num_sge = j;
1490 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001491
1492 /* general part of wqe valid - allow for driver checks */
1493 if (rdi->driver_f.check_send_wqe) {
1494 ret = rdi->driver_f.check_send_wqe(qp, wqe);
1495 if (ret)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001496 goto bail_inval_free;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001497 }
1498
1499 log_pmtu = qp->log_pmtu;
1500 if (qp->ibqp.qp_type != IB_QPT_UC &&
1501 qp->ibqp.qp_type != IB_QPT_RC) {
1502 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1503
1504 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001505 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1506 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001507
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001508 wqe->ssn = qp->s_ssn++;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001509 wqe->psn = qp->s_next_psn;
1510 wqe->lpsn = wqe->psn +
1511 (wqe->length ? ((wqe->length - 1) >> log_pmtu) : 0);
1512 qp->s_next_psn = wqe->lpsn + 1;
1513 smp_wmb(); /* see request builders */
1514 qp->s_avail--;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001515 qp->s_head = next;
1516
1517 return 0;
1518
1519bail_inval_free:
1520 /* release mr holds */
1521 while (j) {
1522 struct rvt_sge *sge = &wqe->sg_list[--j];
1523
1524 rvt_put_mr(sge->mr);
1525 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001526 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001527}
1528
1529/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001530 * rvt_post_send - post a send on a QP
1531 * @ibqp: the QP to post the send on
1532 * @wr: the list of work requests to post
1533 * @bad_wr: the first bad WR is put here
1534 *
1535 * This may be called from interrupt context.
1536 */
1537int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1538 struct ib_send_wr **bad_wr)
1539{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001540 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1541 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1542 unsigned long flags = 0;
1543 int call_send;
1544 unsigned nreq = 0;
1545 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001546
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001547 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001548
1549 /*
1550 * Ensure QP state is such that we can send. If not bail out early,
1551 * there is no need to do this every time we post a send.
1552 */
1553 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001554 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001555 return -EINVAL;
1556 }
1557
1558 /*
1559 * If the send queue is empty, and we only have a single WR then just go
1560 * ahead and kick the send engine into gear. Otherwise we will always
1561 * just schedule the send to happen later.
1562 */
1563 call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1564
1565 for (; wr; wr = wr->next) {
1566 err = rvt_post_one_wr(qp, wr);
1567 if (unlikely(err)) {
1568 *bad_wr = wr;
1569 goto bail;
1570 }
1571 nreq++;
1572 }
1573bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001574 spin_unlock_irqrestore(&qp->s_hlock, flags);
1575 if (nreq) {
1576 if (call_send)
1577 rdi->driver_f.schedule_send_no_lock(qp);
1578 else
1579 rdi->driver_f.do_send(qp);
1580 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001581 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001582}
1583
1584/**
1585 * rvt_post_srq_receive - post a receive on a shared receive queue
1586 * @ibsrq: the SRQ to post the receive on
1587 * @wr: the list of work requests to post
1588 * @bad_wr: A pointer to the first WR to cause a problem is put here
1589 *
1590 * This may be called from interrupt context.
1591 */
1592int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1593 struct ib_recv_wr **bad_wr)
1594{
Jubin Johnb8f881b2016-02-03 14:14:36 -08001595 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1596 struct rvt_rwq *wq;
1597 unsigned long flags;
1598
1599 for (; wr; wr = wr->next) {
1600 struct rvt_rwqe *wqe;
1601 u32 next;
1602 int i;
1603
1604 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1605 *bad_wr = wr;
1606 return -EINVAL;
1607 }
1608
1609 spin_lock_irqsave(&srq->rq.lock, flags);
1610 wq = srq->rq.wq;
1611 next = wq->head + 1;
1612 if (next >= srq->rq.size)
1613 next = 0;
1614 if (next == wq->tail) {
1615 spin_unlock_irqrestore(&srq->rq.lock, flags);
1616 *bad_wr = wr;
1617 return -ENOMEM;
1618 }
1619
1620 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1621 wqe->wr_id = wr->wr_id;
1622 wqe->num_sge = wr->num_sge;
1623 for (i = 0; i < wr->num_sge; i++)
1624 wqe->sg_list[i] = wr->sg_list[i];
1625 /* Make sure queue entry is written before the head index. */
1626 smp_wmb();
1627 wq->head = next;
1628 spin_unlock_irqrestore(&srq->rq.lock, flags);
1629 }
1630 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001631}
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001632
1633void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1634{
1635 struct rvt_qpn_map *map;
1636
1637 map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1638 if (map->page)
1639 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1640}
1641EXPORT_SYMBOL(rvt_free_qpn);
1642
1643void rvt_dec_qp_cnt(struct rvt_dev_info *rdi)
1644{
1645 spin_lock(&rdi->n_qps_lock);
1646 rdi->n_qps_allocated--;
1647 spin_unlock(&rdi->n_qps_lock);
1648}
1649EXPORT_SYMBOL(rvt_dec_qp_cnt);