blob: 200b292be63ea0eceb045198f0bc13d5db4d4771 [file] [log] [blame]
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001/*
Mike Marciniszyn5190f052018-11-28 10:22:31 -08002 * Copyright(c) 2016 - 2018 Intel Corporation.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080048#include <linux/hash.h>
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -080049#include <linux/bitops.h>
50#include <linux/lockdep.h>
Dennis Dalessandro515667f2016-01-22 12:50:17 -080051#include <linux/vmalloc.h>
52#include <linux/slab.h>
53#include <rdma/ib_verbs.h>
Don Hiatt832666c2017-02-08 05:28:25 -080054#include <rdma/ib_hdrs.h>
Don Hiatt13c19222017-08-04 13:53:51 -070055#include <rdma/opa_addr.h>
Shamir Rabinovitch89944452019-02-07 18:44:49 +020056#include <rdma/uverbs_ioctl.h>
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080057#include "qp.h"
Dennis Dalessandro515667f2016-01-22 12:50:17 -080058#include "vt.h"
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -080059#include "trace.h"
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -080060
Kamenee Arumugamf592ae32019-06-28 14:04:30 -040061#define RVT_RWQ_COUNT_THRESHOLD 16
62
Kees Cooka2930e52017-10-16 15:51:13 -070063static void rvt_rc_timeout(struct timer_list *t);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -080064
65/*
66 * Convert the AETH RNR timeout code into the number of microseconds.
67 */
68static const u32 ib_rvt_rnr_table[32] = {
69 655360, /* 00: 655.36 */
70 10, /* 01: .01 */
71 20, /* 02 .02 */
72 30, /* 03: .03 */
73 40, /* 04: .04 */
74 60, /* 05: .06 */
75 80, /* 06: .08 */
76 120, /* 07: .12 */
77 160, /* 08: .16 */
78 240, /* 09: .24 */
79 320, /* 0A: .32 */
80 480, /* 0B: .48 */
81 640, /* 0C: .64 */
82 960, /* 0D: .96 */
83 1280, /* 0E: 1.28 */
84 1920, /* 0F: 1.92 */
85 2560, /* 10: 2.56 */
86 3840, /* 11: 3.84 */
87 5120, /* 12: 5.12 */
88 7680, /* 13: 7.68 */
89 10240, /* 14: 10.24 */
90 15360, /* 15: 15.36 */
91 20480, /* 16: 20.48 */
92 30720, /* 17: 30.72 */
93 40960, /* 18: 40.96 */
94 61440, /* 19: 61.44 */
95 81920, /* 1A: 81.92 */
96 122880, /* 1B: 122.88 */
97 163840, /* 1C: 163.84 */
98 245760, /* 1D: 245.76 */
99 327680, /* 1E: 327.68 */
100 491520 /* 1F: 491.52 */
101};
102
Dennis Dalessandrobfbac092016-01-22 13:00:22 -0800103/*
104 * Note that it is OK to post send work requests in the SQE and ERR
105 * states; rvt_do_send() will process them and generate error
106 * completions as per IB 1.2 C10-96.
107 */
108const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
109 [IB_QPS_RESET] = 0,
110 [IB_QPS_INIT] = RVT_POST_RECV_OK,
111 [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
112 [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
113 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
114 RVT_PROCESS_NEXT_SEND_OK,
115 [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
116 RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
117 [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
118 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
119 [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
120 RVT_POST_SEND_OK | RVT_FLUSH_SEND,
121};
122EXPORT_SYMBOL(ib_rvt_state_ops);
123
Brian Welty019f1182018-09-26 10:44:33 -0700124/* platform specific: return the last level cache (llc) size, in KiB */
125static int rvt_wss_llc_size(void)
126{
127 /* assume that the boot CPU value is universal for all CPUs */
128 return boot_cpu_data.x86_cache_size;
129}
130
131/* platform specific: cacheless copy */
132static void cacheless_memcpy(void *dst, void *src, size_t n)
133{
134 /*
135 * Use the only available X64 cacheless copy. Add a __user cast
136 * to quiet sparse. The src agument is already in the kernel so
137 * there are no security issues. The extra fault recovery machinery
138 * is not invoked.
139 */
140 __copy_user_nocache(dst, (void __user *)src, n, 0);
141}
142
143void rvt_wss_exit(struct rvt_dev_info *rdi)
144{
145 struct rvt_wss *wss = rdi->wss;
146
147 if (!wss)
148 return;
149
150 /* coded to handle partially initialized and repeat callers */
151 kfree(wss->entries);
152 wss->entries = NULL;
153 kfree(rdi->wss);
154 rdi->wss = NULL;
155}
156
157/**
158 * rvt_wss_init - Init wss data structures
159 *
160 * Return: 0 on success
161 */
162int rvt_wss_init(struct rvt_dev_info *rdi)
163{
164 unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode;
165 unsigned int wss_threshold = rdi->dparms.wss_threshold;
166 unsigned int wss_clean_period = rdi->dparms.wss_clean_period;
167 long llc_size;
168 long llc_bits;
169 long table_size;
170 long table_bits;
171 struct rvt_wss *wss;
172 int node = rdi->dparms.node;
173
174 if (sge_copy_mode != RVT_SGE_COPY_ADAPTIVE) {
175 rdi->wss = NULL;
176 return 0;
177 }
178
179 rdi->wss = kzalloc_node(sizeof(*rdi->wss), GFP_KERNEL, node);
180 if (!rdi->wss)
181 return -ENOMEM;
182 wss = rdi->wss;
183
184 /* check for a valid percent range - default to 80 if none or invalid */
185 if (wss_threshold < 1 || wss_threshold > 100)
186 wss_threshold = 80;
187
188 /* reject a wildly large period */
189 if (wss_clean_period > 1000000)
190 wss_clean_period = 256;
191
192 /* reject a zero period */
193 if (wss_clean_period == 0)
194 wss_clean_period = 1;
195
196 /*
197 * Calculate the table size - the next power of 2 larger than the
198 * LLC size. LLC size is in KiB.
199 */
200 llc_size = rvt_wss_llc_size() * 1024;
201 table_size = roundup_pow_of_two(llc_size);
202
203 /* one bit per page in rounded up table */
204 llc_bits = llc_size / PAGE_SIZE;
205 table_bits = table_size / PAGE_SIZE;
206 wss->pages_mask = table_bits - 1;
207 wss->num_entries = table_bits / BITS_PER_LONG;
208
209 wss->threshold = (llc_bits * wss_threshold) / 100;
210 if (wss->threshold == 0)
211 wss->threshold = 1;
212
213 wss->clean_period = wss_clean_period;
214 atomic_set(&wss->clean_counter, wss_clean_period);
215
216 wss->entries = kcalloc_node(wss->num_entries, sizeof(*wss->entries),
217 GFP_KERNEL, node);
218 if (!wss->entries) {
219 rvt_wss_exit(rdi);
220 return -ENOMEM;
221 }
222
223 return 0;
224}
225
226/*
227 * Advance the clean counter. When the clean period has expired,
228 * clean an entry.
229 *
230 * This is implemented in atomics to avoid locking. Because multiple
231 * variables are involved, it can be racy which can lead to slightly
232 * inaccurate information. Since this is only a heuristic, this is
233 * OK. Any innaccuracies will clean themselves out as the counter
234 * advances. That said, it is unlikely the entry clean operation will
235 * race - the next possible racer will not start until the next clean
236 * period.
237 *
238 * The clean counter is implemented as a decrement to zero. When zero
239 * is reached an entry is cleaned.
240 */
241static void wss_advance_clean_counter(struct rvt_wss *wss)
242{
243 int entry;
244 int weight;
245 unsigned long bits;
246
247 /* become the cleaner if we decrement the counter to zero */
248 if (atomic_dec_and_test(&wss->clean_counter)) {
249 /*
250 * Set, not add, the clean period. This avoids an issue
251 * where the counter could decrement below the clean period.
252 * Doing a set can result in lost decrements, slowing the
253 * clean advance. Since this a heuristic, this possible
254 * slowdown is OK.
255 *
256 * An alternative is to loop, advancing the counter by a
257 * clean period until the result is > 0. However, this could
258 * lead to several threads keeping another in the clean loop.
259 * This could be mitigated by limiting the number of times
260 * we stay in the loop.
261 */
262 atomic_set(&wss->clean_counter, wss->clean_period);
263
264 /*
265 * Uniquely grab the entry to clean and move to next.
266 * The current entry is always the lower bits of
267 * wss.clean_entry. The table size, wss.num_entries,
268 * is always a power-of-2.
269 */
270 entry = (atomic_inc_return(&wss->clean_entry) - 1)
271 & (wss->num_entries - 1);
272
273 /* clear the entry and count the bits */
274 bits = xchg(&wss->entries[entry], 0);
275 weight = hweight64((u64)bits);
276 /* only adjust the contended total count if needed */
277 if (weight)
278 atomic_sub(weight, &wss->total_count);
279 }
280}
281
282/*
283 * Insert the given address into the working set array.
284 */
285static void wss_insert(struct rvt_wss *wss, void *address)
286{
287 u32 page = ((unsigned long)address >> PAGE_SHIFT) & wss->pages_mask;
288 u32 entry = page / BITS_PER_LONG; /* assumes this ends up a shift */
289 u32 nr = page & (BITS_PER_LONG - 1);
290
291 if (!test_and_set_bit(nr, &wss->entries[entry]))
292 atomic_inc(&wss->total_count);
293
294 wss_advance_clean_counter(wss);
295}
296
297/*
298 * Is the working set larger than the threshold?
299 */
300static inline bool wss_exceeds_threshold(struct rvt_wss *wss)
301{
302 return atomic_read(&wss->total_count) >= wss->threshold;
303}
304
Mike Marciniszynd2b8d4d2016-01-22 12:50:43 -0800305static void get_map_page(struct rvt_qpn_table *qpt,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300306 struct rvt_qpn_map *map)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800307{
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300308 unsigned long page = get_zeroed_page(GFP_KERNEL);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800309
310 /*
311 * Free the page if someone raced with us installing it.
312 */
313
314 spin_lock(&qpt->lock);
315 if (map->page)
316 free_page(page);
317 else
318 map->page = (void *)page;
319 spin_unlock(&qpt->lock);
320}
321
322/**
323 * init_qpn_table - initialize the QP number table for a device
324 * @qpt: the QPN table
325 */
326static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
327{
328 u32 offset, i;
329 struct rvt_qpn_map *map;
330 int ret = 0;
331
Harish Chegondifef2efd2016-01-22 12:50:30 -0800332 if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800333 return -EINVAL;
334
335 spin_lock_init(&qpt->lock);
336
337 qpt->last = rdi->dparms.qpn_start;
338 qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
339
340 /*
341 * Drivers may want some QPs beyond what we need for verbs let them use
342 * our qpn table. No need for two. Lets go ahead and mark the bitmaps
343 * for those. The reserved range must be *after* the range which verbs
344 * will pick from.
345 */
346
347 /* Figure out number of bit maps needed before reserved range */
348 qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
349
350 /* This should always be zero */
351 offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
352
353 /* Starting with the first reserved bit map */
354 map = &qpt->map[qpt->nmaps];
355
356 rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
357 rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
Harish Chegondifef2efd2016-01-22 12:50:30 -0800358 for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800359 if (!map->page) {
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300360 get_map_page(qpt, map);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800361 if (!map->page) {
362 ret = -ENOMEM;
363 break;
364 }
365 }
366 set_bit(offset, map->page);
367 offset++;
368 if (offset == RVT_BITS_PER_PAGE) {
369 /* next page */
370 qpt->nmaps++;
371 map++;
372 offset = 0;
373 }
374 }
375 return ret;
376}
377
378/**
379 * free_qpn_table - free the QP number table for a device
380 * @qpt: the QPN table
381 */
382static void free_qpn_table(struct rvt_qpn_table *qpt)
383{
384 int i;
385
386 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
387 free_page((unsigned long)qpt->map[i].page);
388}
389
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800390/**
391 * rvt_driver_qp_init - Init driver qp resources
392 * @rdi: rvt dev strucutre
393 *
394 * Return: 0 on success
395 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800396int rvt_driver_qp_init(struct rvt_dev_info *rdi)
397{
398 int i;
399 int ret = -ENOMEM;
400
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800401 if (!rdi->dparms.qp_table_size)
402 return -EINVAL;
403
404 /*
405 * If driver is not doing any QP allocation then make sure it is
406 * providing the necessary QP functions.
407 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800408 if (!rdi->driver_f.free_all_qps ||
409 !rdi->driver_f.qp_priv_alloc ||
410 !rdi->driver_f.qp_priv_free ||
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800411 !rdi->driver_f.notify_qp_reset ||
412 !rdi->driver_f.notify_restart_rc)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800413 return -EINVAL;
414
415 /* allocate parent object */
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800416 rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
417 rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800418 if (!rdi->qp_dev)
419 return -ENOMEM;
420
421 /* allocate hash table */
422 rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
423 rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
424 rdi->qp_dev->qp_table =
Johannes Thumshirn3c073472017-11-15 17:32:41 -0800425 kmalloc_array_node(rdi->qp_dev->qp_table_size,
Mitko Haralanovd1b697b2016-02-03 14:14:54 -0800426 sizeof(*rdi->qp_dev->qp_table),
427 GFP_KERNEL, rdi->dparms.node);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800428 if (!rdi->qp_dev->qp_table)
429 goto no_qp_table;
430
431 for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
432 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
433
434 spin_lock_init(&rdi->qp_dev->qpt_lock);
435
436 /* initialize qpn map */
437 if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
438 goto fail_table;
439
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800440 spin_lock_init(&rdi->n_qps_lock);
441
442 return 0;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800443
444fail_table:
445 kfree(rdi->qp_dev->qp_table);
446 free_qpn_table(&rdi->qp_dev->qpn_table);
447
448no_qp_table:
449 kfree(rdi->qp_dev);
450
451 return ret;
452}
453
454/**
455 * free_all_qps - check for QPs still in use
Randy Dunlap4f9a3012018-01-05 16:22:32 -0800456 * @rdi: rvt device info structure
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800457 *
458 * There should not be any QPs still in use.
459 * Free memory for table.
460 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800461static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800462{
463 unsigned long flags;
464 struct rvt_qp *qp;
465 unsigned n, qp_inuse = 0;
466 spinlock_t *ql; /* work around too long line below */
467
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800468 if (rdi->driver_f.free_all_qps)
469 qp_inuse = rdi->driver_f.free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800470
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800471 qp_inuse += rvt_mcast_tree_empty(rdi);
472
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800473 if (!rdi->qp_dev)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800474 return qp_inuse;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800475
476 ql = &rdi->qp_dev->qpt_lock;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800477 spin_lock_irqsave(ql, flags);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800478 for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
479 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
480 lockdep_is_held(ql));
481 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800482
483 for (; qp; qp = rcu_dereference_protected(qp->next,
484 lockdep_is_held(ql)))
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800485 qp_inuse++;
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800486 }
487 spin_unlock_irqrestore(ql, flags);
488 synchronize_rcu();
489 return qp_inuse;
490}
491
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800492/**
493 * rvt_qp_exit - clean up qps on device exit
494 * @rdi: rvt dev structure
495 *
496 * Check for qp leaks and free resources.
497 */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800498void rvt_qp_exit(struct rvt_dev_info *rdi)
499{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800500 u32 qps_inuse = rvt_free_all_qps(rdi);
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800501
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800502 if (qps_inuse)
503 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
504 qps_inuse);
505 if (!rdi->qp_dev)
506 return;
507
508 kfree(rdi->qp_dev->qp_table);
509 free_qpn_table(&rdi->qp_dev->qpn_table);
510 kfree(rdi->qp_dev);
511}
512
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800513static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
514 struct rvt_qpn_map *map, unsigned off)
515{
516 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
517}
518
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800519/**
520 * alloc_qpn - Allocate the next available qpn or zero/one for QP type
521 * IB_QPT_SMI/IB_QPT_GSI
Randy Dunlap4f9a3012018-01-05 16:22:32 -0800522 * @rdi: rvt device info structure
523 * @qpt: queue pair number table pointer
524 * @port_num: IB port number, 1 based, comes from core
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800525 *
526 * Return: The queue pair number
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800527 */
528static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300529 enum ib_qp_type type, u8 port_num)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800530{
531 u32 i, offset, max_scan, qpn;
532 struct rvt_qpn_map *map;
533 u32 ret;
534
535 if (rdi->driver_f.alloc_qpn)
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300536 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800537
538 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
539 unsigned n;
540
541 ret = type == IB_QPT_GSI;
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800542 n = 1 << (ret + 2 * (port_num - 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800543 spin_lock(&qpt->lock);
544 if (qpt->flags & n)
545 ret = -EINVAL;
546 else
547 qpt->flags |= n;
548 spin_unlock(&qpt->lock);
549 goto bail;
550 }
551
552 qpn = qpt->last + qpt->incr;
553 if (qpn >= RVT_QPN_MAX)
554 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
555 /* offset carries bit 0 */
556 offset = qpn & RVT_BITS_PER_PAGE_MASK;
557 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
558 max_scan = qpt->nmaps - !offset;
559 for (i = 0;;) {
560 if (unlikely(!map->page)) {
Leon Romanovsky0f4d0272017-05-23 14:38:14 +0300561 get_map_page(qpt, map);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800562 if (unlikely(!map->page))
563 break;
564 }
565 do {
566 if (!test_and_set_bit(offset, map->page)) {
567 qpt->last = qpn;
568 ret = qpn;
569 goto bail;
570 }
571 offset += qpt->incr;
572 /*
573 * This qpn might be bogus if offset >= BITS_PER_PAGE.
574 * That is OK. It gets re-assigned below
575 */
576 qpn = mk_qpn(qpt, map, offset);
577 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
578 /*
579 * In order to keep the number of pages allocated to a
580 * minimum, we scan the all existing pages before increasing
581 * the size of the bitmap table.
582 */
583 if (++i > max_scan) {
584 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
585 break;
586 map = &qpt->map[qpt->nmaps++];
587 /* start at incr with current bit 0 */
588 offset = qpt->incr | (offset & 1);
589 } else if (map < &qpt->map[qpt->nmaps]) {
590 ++map;
591 /* start at incr with current bit 0 */
592 offset = qpt->incr | (offset & 1);
593 } else {
594 map = &qpt->map[0];
595 /* wrap to first map page, invert bit 0 */
596 offset = qpt->incr | ((offset & 1) ^ 1);
597 }
Brian Welty501edc422016-06-09 07:51:20 -0700598 /* there can be no set bits in low-order QoS bits */
Mike Marciniszyn2abae622019-05-24 11:44:38 -0400599 WARN_ON(rdi->dparms.qos_shift > 1 &&
600 offset & ((BIT(rdi->dparms.qos_shift - 1) - 1) << 1));
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800601 qpn = mk_qpn(qpt, map, offset);
602 }
603
604 ret = -ENOMEM;
605
606bail:
607 return ret;
608}
609
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800610/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800611 * rvt_clear_mr_refs - Drop help mr refs
612 * @qp: rvt qp data structure
613 * @clr_sends: If shoudl clear send side or not
614 */
615static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
616{
617 unsigned n;
Mike Marciniszyn8b103e92016-05-24 12:50:40 -0700618 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800619
620 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
621 rvt_put_ss(&qp->s_rdma_read_sge);
622
623 rvt_put_ss(&qp->r_sge);
624
625 if (clr_sends) {
626 while (qp->s_last != qp->s_head) {
627 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800628
Mike Marciniszynd40f69c2019-04-12 06:41:42 -0700629 rvt_put_qp_swqe(qp, wqe);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800630 if (++qp->s_last >= qp->s_size)
631 qp->s_last = 0;
632 smp_wmb(); /* see qp_set_savail */
633 }
634 if (qp->s_rdma_mr) {
635 rvt_put_mr(qp->s_rdma_mr);
636 qp->s_rdma_mr = NULL;
637 }
638 }
639
Mike Marciniszyn0208da92017-08-28 11:24:10 -0700640 for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800641 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
642
Ira Weinyfe508272016-07-27 21:07:36 -0400643 if (e->rdma_sge.mr) {
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800644 rvt_put_mr(e->rdma_sge.mr);
645 e->rdma_sge.mr = NULL;
646 }
647 }
648}
649
650/**
Mike Marciniszyn0208da92017-08-28 11:24:10 -0700651 * rvt_swqe_has_lkey - return true if lkey is used by swqe
652 * @wqe - the send wqe
653 * @lkey - the lkey
654 *
655 * Test the swqe for using lkey
656 */
657static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey)
658{
659 int i;
660
661 for (i = 0; i < wqe->wr.num_sge; i++) {
662 struct rvt_sge *sge = &wqe->sg_list[i];
663
664 if (rvt_mr_has_lkey(sge->mr, lkey))
665 return true;
666 }
667 return false;
668}
669
670/**
671 * rvt_qp_sends_has_lkey - return true is qp sends use lkey
672 * @qp - the rvt_qp
673 * @lkey - the lkey
674 */
675static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey)
676{
677 u32 s_last = qp->s_last;
678
679 while (s_last != qp->s_head) {
680 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last);
681
682 if (rvt_swqe_has_lkey(wqe, lkey))
683 return true;
684
685 if (++s_last >= qp->s_size)
686 s_last = 0;
687 }
688 if (qp->s_rdma_mr)
689 if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey))
690 return true;
691 return false;
692}
693
694/**
695 * rvt_qp_acks_has_lkey - return true if acks have lkey
696 * @qp - the qp
697 * @lkey - the lkey
698 */
699static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey)
700{
701 int i;
702 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
703
704 for (i = 0; qp->s_ack_queue && i < rvt_max_atomic(rdi); i++) {
705 struct rvt_ack_entry *e = &qp->s_ack_queue[i];
706
707 if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey))
708 return true;
709 }
710 return false;
711}
712
713/*
714 * rvt_qp_mr_clean - clean up remote ops for lkey
715 * @qp - the qp
716 * @lkey - the lkey that is being de-registered
717 *
718 * This routine checks if the lkey is being used by
719 * the qp.
720 *
721 * If so, the qp is put into an error state to elminate
722 * any references from the qp.
723 */
724void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey)
725{
726 bool lastwqe = false;
727
728 if (qp->ibqp.qp_type == IB_QPT_SMI ||
729 qp->ibqp.qp_type == IB_QPT_GSI)
730 /* avoid special QPs */
731 return;
732 spin_lock_irq(&qp->r_lock);
733 spin_lock(&qp->s_hlock);
734 spin_lock(&qp->s_lock);
735
736 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
737 goto check_lwqe;
738
739 if (rvt_ss_has_lkey(&qp->r_sge, lkey) ||
740 rvt_qp_sends_has_lkey(qp, lkey) ||
741 rvt_qp_acks_has_lkey(qp, lkey))
742 lastwqe = rvt_error_qp(qp, IB_WC_LOC_PROT_ERR);
743check_lwqe:
744 spin_unlock(&qp->s_lock);
745 spin_unlock(&qp->s_hlock);
746 spin_unlock_irq(&qp->r_lock);
747 if (lastwqe) {
748 struct ib_event ev;
749
750 ev.device = qp->ibqp.device;
751 ev.element.qp = &qp->ibqp;
752 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
753 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
754 }
755}
756
757/**
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800758 * rvt_remove_qp - remove qp form table
759 * @rdi: rvt dev struct
760 * @qp: qp to remove
761 *
762 * Remove the QP from the table so it can't be found asynchronously by
763 * the receive routine.
764 */
765static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
766{
767 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
768 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
769 unsigned long flags;
770 int removed = 1;
771
772 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
773
774 if (rcu_dereference_protected(rvp->qp[0],
775 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
776 RCU_INIT_POINTER(rvp->qp[0], NULL);
777 } else if (rcu_dereference_protected(rvp->qp[1],
778 lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
779 RCU_INIT_POINTER(rvp->qp[1], NULL);
780 } else {
781 struct rvt_qp *q;
782 struct rvt_qp __rcu **qpp;
783
784 removed = 0;
785 qpp = &rdi->qp_dev->qp_table[n];
786 for (; (q = rcu_dereference_protected(*qpp,
787 lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
788 qpp = &q->next) {
789 if (q == qp) {
790 RCU_INIT_POINTER(*qpp,
791 rcu_dereference_protected(qp->next,
792 lockdep_is_held(&rdi->qp_dev->qpt_lock)));
793 removed = 1;
794 trace_rvt_qpremove(qp, n);
795 break;
796 }
797 }
798 }
799
800 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
801 if (removed) {
802 synchronize_rcu();
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -0700803 rvt_put_qp(qp);
Dennis Dalessandro79a225b2016-02-14 12:11:20 -0800804 }
805}
806
807/**
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -0400808 * rvt_alloc_rq - allocate memory for user or kernel buffer
809 * @rq: receive queue data structure
810 * @size: number of request queue entries
811 * @node: The NUMA node
812 * @udata: True if user data is available or not false
813 *
814 * Return: If memory allocation failed, return -ENONEM
815 * This function is used by both shared receive
816 * queues and non-shared receive queues to allocate
817 * memory.
818 */
819int rvt_alloc_rq(struct rvt_rq *rq, u32 size, int node,
820 struct ib_udata *udata)
821{
822 if (udata) {
823 rq->wq = vmalloc_user(sizeof(struct rvt_rwq) + size);
824 if (!rq->wq)
825 goto bail;
826 /* need kwq with no buffers */
827 rq->kwq = kzalloc_node(sizeof(*rq->kwq), GFP_KERNEL, node);
828 if (!rq->kwq)
829 goto bail;
830 rq->kwq->curr_wq = rq->wq->wq;
831 } else {
832 /* need kwq with buffers */
833 rq->kwq =
834 vzalloc_node(sizeof(struct rvt_krwq) + size, node);
835 if (!rq->kwq)
836 goto bail;
837 rq->kwq->curr_wq = rq->kwq->wq;
838 }
839
Kamenee Arumugamf592ae32019-06-28 14:04:30 -0400840 spin_lock_init(&rq->kwq->p_lock);
841 spin_lock_init(&rq->kwq->c_lock);
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -0400842 return 0;
843bail:
844 rvt_free_rq(rq);
845 return -ENOMEM;
846}
847
848/**
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700849 * rvt_init_qp - initialize the QP state to the reset state
850 * @qp: the QP to init or reinit
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800851 * @type: the QP type
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700852 *
853 * This function is called from both rvt_create_qp() and
854 * rvt_reset_qp(). The difference is that the reset
855 * patch the necessary locks to protect against concurent
856 * access.
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800857 */
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700858static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
859 enum ib_qp_type type)
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800860{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -0800861 qp->remote_qpn = 0;
862 qp->qkey = 0;
863 qp->qp_access_flags = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800864 qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
865 qp->s_hdrwords = 0;
866 qp->s_wqe = NULL;
867 qp->s_draining = 0;
868 qp->s_next_psn = 0;
869 qp->s_last_psn = 0;
870 qp->s_sending_psn = 0;
871 qp->s_sending_hpsn = 0;
872 qp->s_psn = 0;
873 qp->r_psn = 0;
874 qp->r_msn = 0;
875 if (type == IB_QPT_RC) {
876 qp->s_state = IB_OPCODE_RC_SEND_LAST;
877 qp->r_state = IB_OPCODE_RC_SEND_LAST;
878 } else {
879 qp->s_state = IB_OPCODE_UC_SEND_LAST;
880 qp->r_state = IB_OPCODE_UC_SEND_LAST;
881 }
882 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
883 qp->r_nak_state = 0;
884 qp->r_aflags = 0;
885 qp->r_flags = 0;
886 qp->s_head = 0;
887 qp->s_tail = 0;
888 qp->s_cur = 0;
889 qp->s_acked = 0;
890 qp->s_last = 0;
891 qp->s_ssn = 1;
892 qp->s_lsn = 0;
893 qp->s_mig_state = IB_MIG_MIGRATED;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800894 qp->r_head_ack_queue = 0;
895 qp->s_tail_ack_queue = 0;
Kaike Wan4f9264d2019-01-23 21:48:48 -0800896 qp->s_acked_ack_queue = 0;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800897 qp->s_num_rd_atomic = 0;
Kamenee Arumugamf592ae32019-06-28 14:04:30 -0400898 if (qp->r_rq.kwq)
899 qp->r_rq.kwq->count = qp->r_rq.size;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800900 qp->r_sge.num_sge = 0;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -0700901 atomic_set(&qp->s_reserved_used, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800902}
903
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800904/**
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700905 * rvt_reset_qp - initialize the QP state to the reset state
906 * @qp: the QP to reset
907 * @type: the QP type
908 *
909 * r_lock, s_hlock, and s_lock are required to be held by the caller
910 */
911static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
912 enum ib_qp_type type)
913 __must_hold(&qp->s_lock)
914 __must_hold(&qp->s_hlock)
915 __must_hold(&qp->r_lock)
916{
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700917 lockdep_assert_held(&qp->r_lock);
918 lockdep_assert_held(&qp->s_hlock);
919 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700920 if (qp->state != IB_QPS_RESET) {
921 qp->state = IB_QPS_RESET;
922
923 /* Let drivers flush their waitlist */
924 rdi->driver_f.flush_qp_waiters(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800925 rvt_stop_rc_timers(qp);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700926 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
927 spin_unlock(&qp->s_lock);
928 spin_unlock(&qp->s_hlock);
929 spin_unlock_irq(&qp->r_lock);
930
931 /* Stop the send queue and the retry timer */
932 rdi->driver_f.stop_send_queue(qp);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -0800933 rvt_del_timers_sync(qp);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700934 /* Wait for things to stop */
935 rdi->driver_f.quiesce_qp(qp);
936
937 /* take qp out the hash and wait for it to be unused */
938 rvt_remove_qp(rdi, qp);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700939
940 /* grab the lock b/c it was locked at call time */
941 spin_lock_irq(&qp->r_lock);
942 spin_lock(&qp->s_hlock);
943 spin_lock(&qp->s_lock);
944
945 rvt_clear_mr_refs(qp, 1);
946 /*
947 * Let the driver do any tear down or re-init it needs to for
948 * a qp that has been reset
949 */
950 rdi->driver_f.notify_qp_reset(qp);
951 }
952 rvt_init_qp(rdi, qp, type);
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700953 lockdep_assert_held(&qp->r_lock);
954 lockdep_assert_held(&qp->s_hlock);
955 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700956}
957
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -0700958/** rvt_free_qpn - Free a qpn from the bit map
959 * @qpt: QP table
960 * @qpn: queue pair number to free
961 */
962static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
963{
964 struct rvt_qpn_map *map;
965
Dennis Dalessandro6c31e522017-05-29 17:19:21 -0700966 map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -0700967 if (map->page)
968 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
969}
970
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -0700971/**
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800972 * rvt_create_qp - create a queue pair for a device
973 * @ibpd: the protection domain who's device we create the queue pair for
974 * @init_attr: the attributes of the queue pair
975 * @udata: user data for libibverbs.so
976 *
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800977 * Queue pair creation is mostly an rvt issue. However, drivers have their own
978 * unique idea of what queue pair numbers mean. For instance there is a reserved
979 * range for PSM.
980 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800981 * Return: the queue pair on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800982 *
983 * Called by the ib_create_qp() core verbs function.
984 */
985struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
986 struct ib_qp_init_attr *init_attr,
987 struct ib_udata *udata)
988{
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800989 struct rvt_qp *qp;
990 int err;
991 struct rvt_swqe *swq = NULL;
992 size_t sz;
993 size_t sg_list_sz;
994 struct ib_qp *ret = ERR_PTR(-ENOMEM);
995 struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
996 void *priv = NULL;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -0700997 size_t sqsize;
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800998
999 if (!rdi)
1000 return ERR_PTR(-EINVAL);
1001
Steve Wise33023fb2018-06-18 08:05:26 -07001002 if (init_attr->cap.max_send_sge > rdi->dparms.props.max_send_sge ||
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001003 init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
Leon Romanovsky0f4d0272017-05-23 14:38:14 +03001004 init_attr->create_flags)
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001005 return ERR_PTR(-EINVAL);
1006
1007 /* Check receive queue parameters if no SRQ is specified. */
1008 if (!init_attr->srq) {
Steve Wise33023fb2018-06-18 08:05:26 -07001009 if (init_attr->cap.max_recv_sge >
1010 rdi->dparms.props.max_recv_sge ||
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001011 init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
1012 return ERR_PTR(-EINVAL);
1013
1014 if (init_attr->cap.max_send_sge +
1015 init_attr->cap.max_send_wr +
1016 init_attr->cap.max_recv_sge +
1017 init_attr->cap.max_recv_wr == 0)
1018 return ERR_PTR(-EINVAL);
1019 }
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001020 sqsize =
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001021 init_attr->cap.max_send_wr + 1 +
1022 rdi->dparms.reserved_operations;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001023 switch (init_attr->qp_type) {
1024 case IB_QPT_SMI:
1025 case IB_QPT_GSI:
1026 if (init_attr->port_num == 0 ||
1027 init_attr->port_num > ibpd->device->phys_port_cnt)
1028 return ERR_PTR(-EINVAL);
Bart Van Assche2caaa232017-10-11 10:49:23 -07001029 /* fall through */
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001030 case IB_QPT_UC:
1031 case IB_QPT_RC:
1032 case IB_QPT_UD:
Gustavo A. R. Silva34755f52019-05-29 10:12:48 -05001033 sz = struct_size(swq, sg_list, init_attr->cap.max_send_sge);
Kees Cookfd7bece2018-06-12 14:27:52 -07001034 swq = vzalloc_node(array_size(sz, sqsize), rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001035 if (!swq)
1036 return ERR_PTR(-ENOMEM);
1037
1038 sz = sizeof(*qp);
1039 sg_list_sz = 0;
1040 if (init_attr->srq) {
1041 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
1042
1043 if (srq->rq.max_sge > 1)
1044 sg_list_sz = sizeof(*qp->r_sg_list) *
1045 (srq->rq.max_sge - 1);
1046 } else if (init_attr->cap.max_recv_sge > 1)
1047 sg_list_sz = sizeof(*qp->r_sg_list) *
1048 (init_attr->cap.max_recv_sge - 1);
Leon Romanovsky0f4d0272017-05-23 14:38:14 +03001049 qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
1050 rdi->dparms.node);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001051 if (!qp)
1052 goto bail_swq;
1053
1054 RCU_INIT_POINTER(qp->next, NULL);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001055 if (init_attr->qp_type == IB_QPT_RC) {
1056 qp->s_ack_queue =
Kees Cook590b5b72018-06-12 14:04:20 -07001057 kcalloc_node(rvt_max_atomic(rdi),
1058 sizeof(*qp->s_ack_queue),
1059 GFP_KERNEL,
1060 rdi->dparms.node);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001061 if (!qp->s_ack_queue)
1062 goto bail_qp;
1063 }
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001064 /* initialize timers needed for rc qp */
Kees Cooka2930e52017-10-16 15:51:13 -07001065 timer_setup(&qp->s_timer, rvt_rc_timeout, 0);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08001066 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
1067 HRTIMER_MODE_REL);
1068 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001069
1070 /*
1071 * Driver needs to set up it's private QP structure and do any
1072 * initialization that is needed.
1073 */
Leon Romanovsky0f4d0272017-05-23 14:38:14 +03001074 priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
Mike Marciniszync755f4a2016-06-22 13:29:33 -07001075 if (IS_ERR(priv)) {
1076 ret = priv;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001077 goto bail_qp;
Mike Marciniszync755f4a2016-06-22 13:29:33 -07001078 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001079 qp->priv = priv;
1080 qp->timeout_jiffies =
1081 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1082 1000UL);
1083 if (init_attr->srq) {
1084 sz = 0;
1085 } else {
1086 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
1087 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
1088 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
1089 sizeof(struct rvt_rwqe);
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001090 err = rvt_alloc_rq(&qp->r_rq, qp->r_rq.size * sz,
1091 rdi->dparms.node, udata);
1092 if (err) {
1093 ret = ERR_PTR(err);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001094 goto bail_driver_priv;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001095 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001096 }
1097
1098 /*
1099 * ib_create_qp() will initialize qp->ibqp
1100 * except for qp->ibqp.qp_num.
1101 */
1102 spin_lock_init(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001103 spin_lock_init(&qp->s_hlock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001104 spin_lock_init(&qp->s_lock);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001105 atomic_set(&qp->refcount, 0);
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001106 atomic_set(&qp->local_ops_pending, 0);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001107 init_waitqueue_head(&qp->wait);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001108 INIT_LIST_HEAD(&qp->rspwait);
1109 qp->state = IB_QPS_RESET;
1110 qp->s_wq = swq;
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001111 qp->s_size = sqsize;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001112 qp->s_avail = init_attr->cap.max_send_wr;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001113 qp->s_max_sge = init_attr->cap.max_send_sge;
1114 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
1115 qp->s_flags = RVT_S_SIGNAL_REQ_WR;
1116
1117 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
1118 init_attr->qp_type,
Leon Romanovsky0f4d0272017-05-23 14:38:14 +03001119 init_attr->port_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001120 if (err < 0) {
1121 ret = ERR_PTR(err);
1122 goto bail_rq_wq;
1123 }
1124 qp->ibqp.qp_num = err;
1125 qp->port_num = init_attr->port_num;
Mike Marciniszyn222f7a9a2016-09-06 04:37:26 -07001126 rvt_init_qp(rdi, qp, init_attr->qp_type);
Mike Marciniszyn5190f052018-11-28 10:22:31 -08001127 if (rdi->driver_f.qp_priv_init) {
1128 err = rdi->driver_f.qp_priv_init(rdi, qp, init_attr);
1129 if (err) {
1130 ret = ERR_PTR(err);
1131 goto bail_rq_wq;
1132 }
1133 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001134 break;
1135
1136 default:
1137 /* Don't support raw QPs */
1138 return ERR_PTR(-EINVAL);
1139 }
1140
1141 init_attr->cap.max_inline_data = 0;
1142
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001143 /*
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001144 * Return the address of the RWQ as the offset to mmap.
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001145 * See rvt_mmap() for details.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001146 */
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001147 if (udata && udata->outlen >= sizeof(__u64)) {
1148 if (!qp->r_rq.wq) {
1149 __u64 offset = 0;
1150
1151 err = ib_copy_to_udata(udata, &offset,
1152 sizeof(offset));
1153 if (err) {
1154 ret = ERR_PTR(err);
1155 goto bail_qpn;
1156 }
1157 } else {
1158 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
1159
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03001160 qp->ip = rvt_create_mmap_info(rdi, s, udata,
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001161 qp->r_rq.wq);
1162 if (!qp->ip) {
1163 ret = ERR_PTR(-ENOMEM);
1164 goto bail_qpn;
1165 }
1166
1167 err = ib_copy_to_udata(udata, &qp->ip->offset,
1168 sizeof(qp->ip->offset));
1169 if (err) {
1170 ret = ERR_PTR(err);
1171 goto bail_ip;
1172 }
1173 }
Mike Marciniszynef086c02016-03-07 11:35:08 -08001174 qp->pid = current->pid;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001175 }
1176
1177 spin_lock(&rdi->n_qps_lock);
1178 if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
1179 spin_unlock(&rdi->n_qps_lock);
1180 ret = ERR_PTR(-ENOMEM);
1181 goto bail_ip;
1182 }
1183
1184 rdi->n_qps_allocated++;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001185 /*
1186 * Maintain a busy_jiffies variable that will be added to the timeout
1187 * period in mod_retry_timer and add_retry_timer. This busy jiffies
1188 * is scaled by the number of rc qps created for the device to reduce
1189 * the number of timeouts occurring when there is a large number of
1190 * qps. busy_jiffies is incremented every rc qp scaling interval.
1191 * The scaling interval is selected based on extensive performance
1192 * evaluation of targeted workloads.
1193 */
1194 if (init_attr->qp_type == IB_QPT_RC) {
1195 rdi->n_rc_qps++;
1196 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1197 }
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001198 spin_unlock(&rdi->n_qps_lock);
1199
1200 if (qp->ip) {
1201 spin_lock_irq(&rdi->pending_lock);
1202 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
1203 spin_unlock_irq(&rdi->pending_lock);
1204 }
1205
1206 ret = &qp->ibqp;
1207
1208 /*
1209 * We have our QP and its good, now keep track of what types of opcodes
1210 * can be processed on this QP. We do this by keeping track of what the
1211 * 3 high order bits of the opcode are.
1212 */
1213 switch (init_attr->qp_type) {
1214 case IB_QPT_SMI:
1215 case IB_QPT_GSI:
1216 case IB_QPT_UD:
Mike Marciniszynb218f782016-04-12 11:29:20 -07001217 qp->allowed_ops = IB_OPCODE_UD;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001218 break;
1219 case IB_QPT_RC:
Mike Marciniszynb218f782016-04-12 11:29:20 -07001220 qp->allowed_ops = IB_OPCODE_RC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001221 break;
1222 case IB_QPT_UC:
Mike Marciniszynb218f782016-04-12 11:29:20 -07001223 qp->allowed_ops = IB_OPCODE_UC;
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001224 break;
1225 default:
1226 ret = ERR_PTR(-EINVAL);
1227 goto bail_ip;
1228 }
1229
1230 return ret;
1231
1232bail_ip:
Jim Foraker22dccc52016-11-01 13:44:12 -07001233 if (qp->ip)
1234 kref_put(&qp->ip->ref, rvt_release_mmap_info);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001235
1236bail_qpn:
Dennis Dalessandrob2f8a042017-05-29 17:17:28 -07001237 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001238
1239bail_rq_wq:
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001240 rvt_free_rq(&qp->r_rq);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001241
1242bail_driver_priv:
1243 rdi->driver_f.qp_priv_free(rdi, qp);
1244
1245bail_qp:
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001246 kfree(qp->s_ack_queue);
Dennis Dalessandro515667f2016-01-22 12:50:17 -08001247 kfree(qp);
1248
1249bail_swq:
1250 vfree(swq);
1251
1252 return ret;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001253}
1254
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001255/**
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001256 * rvt_error_qp - put a QP into the error state
1257 * @qp: the QP to put into the error state
1258 * @err: the receive completion error to signal if a RWQE is active
1259 *
1260 * Flushes both send and receive work queues.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001261 *
1262 * Return: true if last WQE event should be generated.
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001263 * The QP r_lock and s_lock should be held and interrupts disabled.
1264 * If we are already in error state, just return.
1265 */
1266int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
1267{
1268 struct ib_wc wc;
1269 int ret = 0;
1270 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1271
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001272 lockdep_assert_held(&qp->r_lock);
1273 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001274 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
1275 goto bail;
1276
1277 qp->state = IB_QPS_ERR;
1278
1279 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1280 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1281 del_timer(&qp->s_timer);
1282 }
1283
1284 if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
1285 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
1286
1287 rdi->driver_f.notify_error_qp(qp);
1288
1289 /* Schedule the sending tasklet to drain the send work queue. */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001290 if (READ_ONCE(qp->s_last) != qp->s_head)
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001291 rdi->driver_f.schedule_send(qp);
1292
1293 rvt_clear_mr_refs(qp, 0);
1294
1295 memset(&wc, 0, sizeof(wc));
1296 wc.qp = &qp->ibqp;
1297 wc.opcode = IB_WC_RECV;
1298
1299 if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1300 wc.wr_id = qp->r_wr_id;
1301 wc.status = err;
1302 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1303 }
1304 wc.status = IB_WC_WR_FLUSH_ERR;
1305
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001306 if (qp->r_rq.kwq) {
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001307 u32 head;
1308 u32 tail;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001309 struct rvt_rwq *wq = NULL;
1310 struct rvt_krwq *kwq = NULL;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001311
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04001312 spin_lock(&qp->r_rq.kwq->c_lock);
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001313 /* qp->ip used to validate if there is a user buffer mmaped */
1314 if (qp->ip) {
1315 wq = qp->r_rq.wq;
1316 head = RDMA_READ_UAPI_ATOMIC(wq->head);
1317 tail = RDMA_READ_UAPI_ATOMIC(wq->tail);
1318 } else {
1319 kwq = qp->r_rq.kwq;
1320 head = kwq->head;
1321 tail = kwq->tail;
1322 }
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001323 /* sanity check pointers before trusting them */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001324 if (head >= qp->r_rq.size)
1325 head = 0;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001326 if (tail >= qp->r_rq.size)
1327 tail = 0;
1328 while (tail != head) {
1329 wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1330 if (++tail >= qp->r_rq.size)
1331 tail = 0;
1332 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1333 }
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001334 if (qp->ip)
1335 RDMA_WRITE_UAPI_ATOMIC(wq->tail, tail);
1336 else
1337 kwq->tail = tail;
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04001338 spin_unlock(&qp->r_rq.kwq->c_lock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001339 } else if (qp->ibqp.event_handler) {
1340 ret = 1;
1341 }
1342
1343bail:
1344 return ret;
1345}
1346EXPORT_SYMBOL(rvt_error_qp);
1347
1348/*
1349 * Put the QP into the hash table.
1350 * The hash table holds a reference to the QP.
1351 */
1352static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1353{
1354 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1355 unsigned long flags;
1356
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001357 rvt_get_qp(qp);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001358 spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1359
1360 if (qp->ibqp.qp_num <= 1) {
1361 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1362 } else {
1363 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1364
1365 qp->next = rdi->qp_dev->qp_table[n];
1366 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1367 trace_rvt_qpinsert(qp, n);
1368 }
1369
1370 spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1371}
1372
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001373/**
Parav Pandit61347fa2016-09-13 19:40:50 +05301374 * rvt_modify_qp - modify the attributes of a queue pair
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001375 * @ibqp: the queue pair who's attributes we're modifying
1376 * @attr: the new attributes
1377 * @attr_mask: the mask of attributes to modify
1378 * @udata: user data for libibverbs.so
1379 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001380 * Return: 0 on success, otherwise returns an errno.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001381 */
1382int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1383 int attr_mask, struct ib_udata *udata)
1384{
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001385 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1386 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1387 enum ib_qp_state cur_state, new_state;
1388 struct ib_event ev;
1389 int lastwqe = 0;
1390 int mig = 0;
1391 int pmtu = 0; /* for gcc warning only */
Don Hiatt13c19222017-08-04 13:53:51 -07001392 int opa_ah;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001393
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001394 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001395 spin_lock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001396 spin_lock(&qp->s_lock);
1397
1398 cur_state = attr_mask & IB_QP_CUR_STATE ?
1399 attr->cur_qp_state : qp->state;
1400 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
Don Hiatt13c19222017-08-04 13:53:51 -07001401 opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001402
1403 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
Kamal Heibd31131b2018-10-02 16:11:21 +03001404 attr_mask))
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001405 goto inval;
1406
Ira Weinye85ec332016-01-22 13:04:38 -08001407 if (rdi->driver_f.check_modify_qp &&
1408 rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1409 goto inval;
1410
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001411 if (attr_mask & IB_QP_AV) {
Don Hiatt13c19222017-08-04 13:53:51 -07001412 if (opa_ah) {
1413 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1414 opa_get_mcast_base(OPA_MCAST_NR))
1415 goto inval;
1416 } else {
1417 if (rdma_ah_get_dlid(&attr->ah_attr) >=
1418 be16_to_cpu(IB_MULTICAST_LID_BASE))
1419 goto inval;
1420 }
1421
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001422 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1423 goto inval;
1424 }
1425
1426 if (attr_mask & IB_QP_ALT_PATH) {
Don Hiatt13c19222017-08-04 13:53:51 -07001427 if (opa_ah) {
1428 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1429 opa_get_mcast_base(OPA_MCAST_NR))
1430 goto inval;
1431 } else {
1432 if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1433 be16_to_cpu(IB_MULTICAST_LID_BASE))
1434 goto inval;
1435 }
1436
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001437 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1438 goto inval;
1439 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1440 goto inval;
1441 }
1442
1443 if (attr_mask & IB_QP_PKEY_INDEX)
1444 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1445 goto inval;
1446
1447 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1448 if (attr->min_rnr_timer > 31)
1449 goto inval;
1450
1451 if (attr_mask & IB_QP_PORT)
1452 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1453 qp->ibqp.qp_type == IB_QPT_GSI ||
1454 attr->port_num == 0 ||
1455 attr->port_num > ibqp->device->phys_port_cnt)
1456 goto inval;
1457
1458 if (attr_mask & IB_QP_DEST_QPN)
1459 if (attr->dest_qp_num > RVT_QPN_MASK)
1460 goto inval;
1461
1462 if (attr_mask & IB_QP_RETRY_CNT)
1463 if (attr->retry_cnt > 7)
1464 goto inval;
1465
1466 if (attr_mask & IB_QP_RNR_RETRY)
1467 if (attr->rnr_retry > 7)
1468 goto inval;
1469
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001470 /*
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001471 * Don't allow invalid path_mtu values. OK to set greater
1472 * than the active mtu (or even the max_cap, if we have tuned
1473 * that to a small mtu. We'll set qp->path_mtu
1474 * to the lesser of requested attribute mtu and active,
1475 * for packetizing messages.
1476 * Note that the QP port has to be set in INIT and MTU in RTR.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001477 */
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001478 if (attr_mask & IB_QP_PATH_MTU) {
1479 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1480 if (pmtu < 0)
1481 goto inval;
1482 }
1483
1484 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1485 if (attr->path_mig_state == IB_MIG_REARM) {
1486 if (qp->s_mig_state == IB_MIG_ARMED)
1487 goto inval;
1488 if (new_state != IB_QPS_RTS)
1489 goto inval;
1490 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1491 if (qp->s_mig_state == IB_MIG_REARM)
1492 goto inval;
1493 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1494 goto inval;
1495 if (qp->s_mig_state == IB_MIG_ARMED)
1496 mig = 1;
1497 } else {
1498 goto inval;
1499 }
1500 }
1501
1502 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1503 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1504 goto inval;
1505
1506 switch (new_state) {
1507 case IB_QPS_RESET:
1508 if (qp->state != IB_QPS_RESET)
1509 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1510 break;
1511
1512 case IB_QPS_RTR:
1513 /* Allow event to re-trigger if QP set to RTR more than once */
1514 qp->r_flags &= ~RVT_R_COMM_EST;
1515 qp->state = new_state;
1516 break;
1517
1518 case IB_QPS_SQD:
1519 qp->s_draining = qp->s_last != qp->s_cur;
1520 qp->state = new_state;
1521 break;
1522
1523 case IB_QPS_SQE:
1524 if (qp->ibqp.qp_type == IB_QPT_RC)
1525 goto inval;
1526 qp->state = new_state;
1527 break;
1528
1529 case IB_QPS_ERR:
1530 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1531 break;
1532
1533 default:
1534 qp->state = new_state;
1535 break;
1536 }
1537
1538 if (attr_mask & IB_QP_PKEY_INDEX)
1539 qp->s_pkey_index = attr->pkey_index;
1540
1541 if (attr_mask & IB_QP_PORT)
1542 qp->port_num = attr->port_num;
1543
1544 if (attr_mask & IB_QP_DEST_QPN)
1545 qp->remote_qpn = attr->dest_qp_num;
1546
1547 if (attr_mask & IB_QP_SQ_PSN) {
1548 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1549 qp->s_psn = qp->s_next_psn;
1550 qp->s_sending_psn = qp->s_next_psn;
1551 qp->s_last_psn = qp->s_next_psn - 1;
1552 qp->s_sending_hpsn = qp->s_last_psn;
1553 }
1554
1555 if (attr_mask & IB_QP_RQ_PSN)
1556 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1557
1558 if (attr_mask & IB_QP_ACCESS_FLAGS)
1559 qp->qp_access_flags = attr->qp_access_flags;
1560
1561 if (attr_mask & IB_QP_AV) {
Jason Gunthorped97099f2018-06-13 10:22:05 +03001562 rdma_replace_ah_attr(&qp->remote_ah_attr, &attr->ah_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001563 qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001564 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1565 }
1566
1567 if (attr_mask & IB_QP_ALT_PATH) {
Jason Gunthorped97099f2018-06-13 10:22:05 +03001568 rdma_replace_ah_attr(&qp->alt_ah_attr, &attr->alt_ah_attr);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001569 qp->s_alt_pkey_index = attr->alt_pkey_index;
1570 }
1571
1572 if (attr_mask & IB_QP_PATH_MIG_STATE) {
1573 qp->s_mig_state = attr->path_mig_state;
1574 if (mig) {
1575 qp->remote_ah_attr = qp->alt_ah_attr;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001576 qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001577 qp->s_pkey_index = qp->s_alt_pkey_index;
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001578 }
1579 }
1580
1581 if (attr_mask & IB_QP_PATH_MTU) {
1582 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001583 qp->log_pmtu = ilog2(qp->pmtu);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001584 }
1585
1586 if (attr_mask & IB_QP_RETRY_CNT) {
1587 qp->s_retry_cnt = attr->retry_cnt;
1588 qp->s_retry = attr->retry_cnt;
1589 }
1590
1591 if (attr_mask & IB_QP_RNR_RETRY) {
1592 qp->s_rnr_retry_cnt = attr->rnr_retry;
1593 qp->s_rnr_retry = attr->rnr_retry;
1594 }
1595
1596 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1597 qp->r_min_rnr_timer = attr->min_rnr_timer;
1598
1599 if (attr_mask & IB_QP_TIMEOUT) {
1600 qp->timeout = attr->timeout;
Kaike Wana25ce422017-06-17 10:37:26 -07001601 qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001602 }
1603
1604 if (attr_mask & IB_QP_QKEY)
1605 qp->qkey = attr->qkey;
1606
1607 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1608 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1609
1610 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1611 qp->s_max_rd_atomic = attr->max_rd_atomic;
1612
Ira Weinye85ec332016-01-22 13:04:38 -08001613 if (rdi->driver_f.modify_qp)
1614 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1615
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001616 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001617 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001618 spin_unlock_irq(&qp->r_lock);
1619
1620 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1621 rvt_insert_qp(rdi, qp);
1622
1623 if (lastwqe) {
1624 ev.device = qp->ibqp.device;
1625 ev.element.qp = &qp->ibqp;
1626 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1627 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1628 }
1629 if (mig) {
1630 ev.device = qp->ibqp.device;
1631 ev.element.qp = &qp->ibqp;
1632 ev.event = IB_EVENT_PATH_MIG;
1633 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1634 }
1635 return 0;
1636
1637inval:
1638 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001639 spin_unlock(&qp->s_hlock);
Dennis Dalessandro3b0b3fb2016-01-22 13:00:35 -08001640 spin_unlock_irq(&qp->r_lock);
1641 return -EINVAL;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001642}
1643
1644/**
1645 * rvt_destroy_qp - destroy a queue pair
1646 * @ibqp: the queue pair to destroy
1647 *
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001648 * Note that this can be called while the QP is actively sending or
1649 * receiving!
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001650 *
1651 * Return: 0 on success.
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001652 */
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03001653int rvt_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001654{
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001655 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1656 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001657
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001658 spin_lock_irq(&qp->r_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001659 spin_lock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001660 spin_lock(&qp->s_lock);
1661 rvt_reset_qp(rdi, qp, ibqp->qp_type);
1662 spin_unlock(&qp->s_lock);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001663 spin_unlock(&qp->s_hlock);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001664 spin_unlock_irq(&qp->r_lock);
1665
Alex Estrinf9586ab2017-10-09 12:38:33 -07001666 wait_event(qp->wait, !atomic_read(&qp->refcount));
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001667 /* qpn is now available for use again */
1668 rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1669
1670 spin_lock(&rdi->n_qps_lock);
1671 rdi->n_qps_allocated--;
Vennila Megavannanbfee5e32016-02-09 14:29:49 -08001672 if (qp->ibqp.qp_type == IB_QPT_RC) {
1673 rdi->n_rc_qps--;
1674 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1675 }
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001676 spin_unlock(&rdi->n_qps_lock);
1677
1678 if (qp->ip)
1679 kref_put(&qp->ip->ref, rvt_release_mmap_info);
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001680 kvfree(qp->r_rq.kwq);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001681 rdi->driver_f.qp_priv_free(rdi, qp);
Mike Marciniszyn8b103e92016-05-24 12:50:40 -07001682 kfree(qp->s_ack_queue);
Jason Gunthorped97099f2018-06-13 10:22:05 +03001683 rdma_destroy_ah_attr(&qp->remote_ah_attr);
1684 rdma_destroy_ah_attr(&qp->alt_ah_attr);
Kaike Wan838b6fd2019-01-23 19:30:07 -08001685 vfree(qp->s_wq);
Dennis Dalessandro5a17ad12016-01-22 13:00:42 -08001686 kfree(qp);
1687 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001688}
1689
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001690/**
1691 * rvt_query_qp - query an ipbq
1692 * @ibqp: IB qp to query
1693 * @attr: attr struct to fill in
1694 * @attr_mask: attr mask ignored
1695 * @init_attr: struct to fill in
1696 *
1697 * Return: always 0
1698 */
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001699int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1700 int attr_mask, struct ib_qp_init_attr *init_attr)
1701{
Harish Chegondi74d2d502016-01-22 13:05:04 -08001702 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1703 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1704
1705 attr->qp_state = qp->state;
1706 attr->cur_qp_state = attr->qp_state;
Sebastian Sanchez16570d32017-08-04 13:52:20 -07001707 attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
Harish Chegondi74d2d502016-01-22 13:05:04 -08001708 attr->path_mig_state = qp->s_mig_state;
1709 attr->qkey = qp->qkey;
1710 attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1711 attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1712 attr->dest_qp_num = qp->remote_qpn;
1713 attr->qp_access_flags = qp->qp_access_flags;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001714 attr->cap.max_send_wr = qp->s_size - 1 -
1715 rdi->dparms.reserved_operations;
Harish Chegondi74d2d502016-01-22 13:05:04 -08001716 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1717 attr->cap.max_send_sge = qp->s_max_sge;
1718 attr->cap.max_recv_sge = qp->r_rq.max_sge;
1719 attr->cap.max_inline_data = 0;
1720 attr->ah_attr = qp->remote_ah_attr;
1721 attr->alt_ah_attr = qp->alt_ah_attr;
1722 attr->pkey_index = qp->s_pkey_index;
1723 attr->alt_pkey_index = qp->s_alt_pkey_index;
1724 attr->en_sqd_async_notify = 0;
1725 attr->sq_draining = qp->s_draining;
1726 attr->max_rd_atomic = qp->s_max_rd_atomic;
1727 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1728 attr->min_rnr_timer = qp->r_min_rnr_timer;
1729 attr->port_num = qp->port_num;
1730 attr->timeout = qp->timeout;
1731 attr->retry_cnt = qp->s_retry_cnt;
1732 attr->rnr_retry = qp->s_rnr_retry_cnt;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001733 attr->alt_port_num =
1734 rdma_ah_get_port_num(&qp->alt_ah_attr);
Harish Chegondi74d2d502016-01-22 13:05:04 -08001735 attr->alt_timeout = qp->alt_timeout;
1736
1737 init_attr->event_handler = qp->ibqp.event_handler;
1738 init_attr->qp_context = qp->ibqp.qp_context;
1739 init_attr->send_cq = qp->ibqp.send_cq;
1740 init_attr->recv_cq = qp->ibqp.recv_cq;
1741 init_attr->srq = qp->ibqp.srq;
1742 init_attr->cap = attr->cap;
1743 if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1744 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1745 else
1746 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1747 init_attr->qp_type = qp->ibqp.qp_type;
1748 init_attr->port_num = qp->port_num;
1749 return 0;
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -08001750}
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001751
1752/**
1753 * rvt_post_receive - post a receive on a QP
1754 * @ibqp: the QP to post the receive on
1755 * @wr: the WR to post
1756 * @bad_wr: the first bad WR is put here
1757 *
1758 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08001759 *
1760 * Return: 0 on success otherwise errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001761 */
Bart Van Assched34ac5c2018-07-18 09:25:32 -07001762int rvt_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
1763 const struct ib_recv_wr **bad_wr)
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001764{
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001765 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001766 struct rvt_krwq *wq = qp->r_rq.kwq;
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001767 unsigned long flags;
Alex Estrin000a8302016-03-07 11:35:51 -08001768 int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1769 !qp->ibqp.srq;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001770
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001771 /* Check that state is OK to post receive. */
1772 if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1773 *bad_wr = wr;
1774 return -EINVAL;
1775 }
1776
1777 for (; wr; wr = wr->next) {
1778 struct rvt_rwqe *wqe;
1779 u32 next;
1780 int i;
1781
1782 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1783 *bad_wr = wr;
1784 return -EINVAL;
1785 }
1786
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04001787 spin_lock_irqsave(&qp->r_rq.kwq->p_lock, flags);
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001788 next = wq->head + 1;
1789 if (next >= qp->r_rq.size)
1790 next = 0;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001791 if (next == READ_ONCE(wq->tail)) {
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04001792 spin_unlock_irqrestore(&qp->r_rq.kwq->p_lock, flags);
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001793 *bad_wr = wr;
1794 return -ENOMEM;
1795 }
Alex Estrin000a8302016-03-07 11:35:51 -08001796 if (unlikely(qp_err_flush)) {
1797 struct ib_wc wc;
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001798
Alex Estrin000a8302016-03-07 11:35:51 -08001799 memset(&wc, 0, sizeof(wc));
1800 wc.qp = &qp->ibqp;
1801 wc.opcode = IB_WC_RECV;
1802 wc.wr_id = wr->wr_id;
1803 wc.status = IB_WC_WR_FLUSH_ERR;
1804 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1805 } else {
1806 wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1807 wqe->wr_id = wr->wr_id;
1808 wqe->num_sge = wr->num_sge;
1809 for (i = 0; i < wr->num_sge; i++)
1810 wqe->sg_list[i] = wr->sg_list[i];
1811 /*
1812 * Make sure queue entry is written
1813 * before the head index.
1814 */
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04001815 smp_store_release(&wq->head, next);
Alex Estrin000a8302016-03-07 11:35:51 -08001816 }
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04001817 spin_unlock_irqrestore(&qp->r_rq.kwq->p_lock, flags);
Dennis Dalessandro120bdaf2016-01-22 13:00:48 -08001818 }
1819 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08001820}
1821
1822/**
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001823 * rvt_qp_valid_operation - validate post send wr request
1824 * @qp - the qp
1825 * @post-parms - the post send table for the driver
1826 * @wr - the work request
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001827 *
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001828 * The routine validates the operation based on the
1829 * validation table an returns the length of the operation
1830 * which can extend beyond the ib_send_bw. Operation
1831 * dependent flags key atomic operation validation.
1832 *
1833 * There is an exception for UD qps that validates the pd and
1834 * overrides the length to include the additional UD specific
1835 * length.
1836 *
1837 * Returns a negative error or the length of the work request
1838 * for building the swqe.
1839 */
1840static inline int rvt_qp_valid_operation(
1841 struct rvt_qp *qp,
1842 const struct rvt_operation_params *post_parms,
Bart Van Asschef696bf62018-07-18 09:25:14 -07001843 const struct ib_send_wr *wr)
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001844{
1845 int len;
1846
1847 if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1848 return -EINVAL;
1849 if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1850 return -EINVAL;
1851 if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1852 ibpd_to_rvtpd(qp->ibqp.pd)->user)
1853 return -EINVAL;
1854 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1855 (wr->num_sge == 0 ||
1856 wr->sg_list[0].length < sizeof(u64) ||
1857 wr->sg_list[0].addr & (sizeof(u64) - 1)))
1858 return -EINVAL;
1859 if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1860 !qp->s_max_rd_atomic)
1861 return -EINVAL;
1862 len = post_parms[wr->opcode].length;
1863 /* UD specific */
1864 if (qp->ibqp.qp_type != IB_QPT_UC &&
1865 qp->ibqp.qp_type != IB_QPT_RC) {
1866 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1867 return -EINVAL;
1868 len = sizeof(struct ib_ud_wr);
1869 }
1870 return len;
1871}
1872
1873/**
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001874 * rvt_qp_is_avail - determine queue capacity
Randy Dunlap4f9a3012018-01-05 16:22:32 -08001875 * @qp: the qp
1876 * @rdi: the rdmavt device
1877 * @reserved_op: is reserved operation
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001878 *
1879 * This assumes the s_hlock is held but the s_last
1880 * qp variable is uncontrolled.
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001881 *
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001882 * For non reserved operations, the qp->s_avail
1883 * may be changed.
1884 *
1885 * The return value is zero or a -ENOMEM.
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001886 */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001887static inline int rvt_qp_is_avail(
1888 struct rvt_qp *qp,
1889 struct rvt_dev_info *rdi,
1890 bool reserved_op)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001891{
1892 u32 slast;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001893 u32 avail;
1894 u32 reserved_used;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001895
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001896 /* see rvt_qp_wqe_unreserve() */
1897 smp_mb__before_atomic();
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001898 if (unlikely(reserved_op)) {
1899 /* see rvt_qp_wqe_unreserve() */
Mike Marciniszyn4a9ceb72019-06-13 08:30:52 -04001900 reserved_used = atomic_read(&qp->s_reserved_used);
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001901 if (reserved_used >= rdi->dparms.reserved_operations)
1902 return -ENOMEM;
1903 return 0;
1904 }
1905 /* non-reserved operations */
1906 if (likely(qp->s_avail))
1907 return 0;
Mike Marciniszyn4a9ceb72019-06-13 08:30:52 -04001908 /* See rvt_qp_complete_swqe() */
1909 slast = smp_load_acquire(&qp->s_last);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001910 if (qp->s_head >= slast)
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001911 avail = qp->s_size - (qp->s_head - slast);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001912 else
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001913 avail = slast - qp->s_head;
1914
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001915 reserved_used = atomic_read(&qp->s_reserved_used);
1916 avail = avail - 1 -
1917 (rdi->dparms.reserved_operations - reserved_used);
1918 /* insure we don't assign a negative s_avail */
1919 if ((s32)avail <= 0)
1920 return -ENOMEM;
1921 qp->s_avail = avail;
1922 if (WARN_ON(qp->s_avail >
1923 (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1924 rvt_pr_err(rdi,
1925 "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1926 qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1927 qp->s_head, qp->s_tail, qp->s_cur,
1928 qp->s_acked, qp->s_last);
1929 return 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001930}
1931
1932/**
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001933 * rvt_post_one_wr - post one RC, UC, or UD send work request
1934 * @qp: the QP to post on
1935 * @wr: the work request to send
1936 */
Mike Marciniszyn91702b42016-02-14 12:45:44 -08001937static int rvt_post_one_wr(struct rvt_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07001938 const struct ib_send_wr *wr,
Michael J. Ruhl0b79b272018-09-10 09:49:27 -07001939 bool *call_send)
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001940{
1941 struct rvt_swqe *wqe;
1942 u32 next;
1943 int i;
1944 int j;
1945 int acc;
1946 struct rvt_lkey_table *rkt;
1947 struct rvt_pd *pd;
1948 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001949 u8 log_pmtu;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07001950 int ret;
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001951 size_t cplen;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07001952 bool reserved_op;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001953 int local_ops_delayed = 0;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001954
Mike Marciniszynafcf8f72016-07-01 16:02:07 -07001955 BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1956
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08001957 /* IB spec says that num_sge == 0 is OK. */
1958 if (unlikely(wr->num_sge > qp->s_max_sge))
1959 return -EINVAL;
1960
Mike Marciniszyn2821c502016-07-01 16:02:24 -07001961 ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1962 if (ret < 0)
1963 return ret;
1964 cplen = ret;
1965
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001966 /*
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001967 * Local operations include fast register and local invalidate.
1968 * Fast register needs to be processed immediately because the
1969 * registered lkey may be used by following work requests and the
1970 * lkey needs to be valid at the time those requests are posted.
1971 * Local invalidate can be processed immediately if fencing is
1972 * not required and no previous local invalidate ops are pending.
1973 * Signaled local operations that have been processed immediately
1974 * need to have requests with "completion only" flags set posted
1975 * to the send queue in order to generate completions.
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001976 */
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001977 if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001978 switch (wr->opcode) {
1979 case IB_WR_REG_MR:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001980 ret = rvt_fast_reg_mr(qp,
1981 reg_wr(wr)->mr,
1982 reg_wr(wr)->key,
1983 reg_wr(wr)->access);
1984 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1985 return ret;
1986 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001987 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07001988 if ((wr->send_flags & IB_SEND_FENCE) ||
1989 atomic_read(&qp->local_ops_pending)) {
1990 local_ops_delayed = 1;
1991 } else {
1992 ret = rvt_invalidate_rkey(
1993 qp, wr->ex.invalidate_rkey);
1994 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1995 return ret;
1996 }
1997 break;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07001998 default:
1999 return -EINVAL;
2000 }
2001 }
2002
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07002003 reserved_op = rdi->post_parms[wr->opcode].flags &
2004 RVT_OPERATION_USE_RESERVE;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002005 /* check for avail */
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07002006 ret = rvt_qp_is_avail(qp, rdi, reserved_op);
2007 if (ret)
2008 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002009 next = qp->s_head + 1;
2010 if (next >= qp->s_size)
2011 next = 0;
Ira Weiny60c30f52016-02-03 14:14:45 -08002012
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002013 rkt = &rdi->lkey_table;
2014 pd = ibpd_to_rvtpd(qp->ibqp.pd);
2015 wqe = rvt_get_swqe_ptr(qp, qp->s_head);
2016
Mike Marciniszyn2821c502016-07-01 16:02:24 -07002017 /* cplen has length from above */
2018 memcpy(&wqe->wr, wr, cplen);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002019
2020 wqe->length = 0;
2021 j = 0;
2022 if (wr->num_sge) {
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07002023 struct rvt_sge *last_sge = NULL;
2024
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002025 acc = wr->opcode >= IB_WR_RDMA_READ ?
2026 IB_ACCESS_LOCAL_WRITE : 0;
2027 for (i = 0; i < wr->num_sge; i++) {
2028 u32 length = wr->sg_list[i].length;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002029
2030 if (length == 0)
2031 continue;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07002032 ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
2033 &wr->sg_list[i], acc);
2034 if (unlikely(ret < 0))
2035 goto bail_inval_free;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002036 wqe->length += length;
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07002037 if (ret)
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07002038 last_sge = &wqe->sg_list[j];
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -07002039 j += ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002040 }
2041 wqe->wr.num_sge = j;
2042 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002043
Kaike Wand205a06a2018-09-26 10:26:44 -07002044 /*
2045 * Calculate and set SWQE PSN values prior to handing it off
2046 * to the driver's check routine. This give the driver the
2047 * opportunity to adjust PSN values based on internal checks.
2048 */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002049 log_pmtu = qp->log_pmtu;
Mike Marciniszyn52cdbcc2019-04-11 07:16:23 -07002050 if (qp->allowed_ops == IB_OPCODE_UD) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002051 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
2052
2053 log_pmtu = ah->log_pmtu;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002054 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
2055 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002056
Jianxin Xiongd9f87232016-07-25 13:38:25 -07002057 if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -07002058 if (local_ops_delayed)
2059 atomic_inc(&qp->local_ops_pending);
2060 else
2061 wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
Jianxin Xiongd9f87232016-07-25 13:38:25 -07002062 wqe->ssn = 0;
2063 wqe->psn = 0;
2064 wqe->lpsn = 0;
2065 } else {
2066 wqe->ssn = qp->s_ssn++;
2067 wqe->psn = qp->s_next_psn;
2068 wqe->lpsn = wqe->psn +
2069 (wqe->length ?
2070 ((wqe->length - 1) >> log_pmtu) :
2071 0);
Jianxin Xiongd9f87232016-07-25 13:38:25 -07002072 }
Kaike Wand205a06a2018-09-26 10:26:44 -07002073
2074 /* general part of wqe valid - allow for driver checks */
2075 if (rdi->driver_f.setup_wqe) {
2076 ret = rdi->driver_f.setup_wqe(qp, wqe, call_send);
2077 if (ret < 0)
2078 goto bail_inval_free_ref;
2079 }
2080
2081 if (!(rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL))
2082 qp->s_next_psn = wqe->lpsn + 1;
2083
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07002084 if (unlikely(reserved_op)) {
2085 wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07002086 rvt_qp_wqe_reserve(qp, wqe);
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07002087 } else {
2088 wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
Mike Marciniszyn856cc4c2016-07-25 13:39:39 -07002089 qp->s_avail--;
Mike Marciniszyn44dcfa42017-03-20 17:26:01 -07002090 }
Mike Marciniszyn14fe13f2017-05-12 09:20:31 -07002091 trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002092 smp_wmb(); /* see request builders */
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002093 qp->s_head = next;
2094
2095 return 0;
2096
Kaike Wand205a06a2018-09-26 10:26:44 -07002097bail_inval_free_ref:
Mike Marciniszyn52cdbcc2019-04-11 07:16:23 -07002098 if (qp->allowed_ops == IB_OPCODE_UD)
Kaike Wand205a06a2018-09-26 10:26:44 -07002099 atomic_dec(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002100bail_inval_free:
2101 /* release mr holds */
2102 while (j) {
2103 struct rvt_sge *sge = &wqe->sg_list[--j];
2104
2105 rvt_put_mr(sge->mr);
2106 }
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002107 return ret;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002108}
2109
2110/**
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08002111 * rvt_post_send - post a send on a QP
2112 * @ibqp: the QP to post the send on
2113 * @wr: the list of work requests to post
2114 * @bad_wr: the first bad WR is put here
2115 *
2116 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08002117 *
2118 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08002119 */
Bart Van Assched34ac5c2018-07-18 09:25:32 -07002120int rvt_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
2121 const struct ib_send_wr **bad_wr)
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08002122{
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002123 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
2124 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2125 unsigned long flags = 0;
Michael J. Ruhl0b79b272018-09-10 09:49:27 -07002126 bool call_send;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002127 unsigned nreq = 0;
2128 int err = 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08002129
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002130 spin_lock_irqsave(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002131
2132 /*
2133 * Ensure QP state is such that we can send. If not bail out early,
2134 * there is no need to do this every time we post a send.
2135 */
2136 if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002137 spin_unlock_irqrestore(&qp->s_hlock, flags);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002138 return -EINVAL;
2139 }
2140
2141 /*
2142 * If the send queue is empty, and we only have a single WR then just go
2143 * ahead and kick the send engine into gear. Otherwise we will always
2144 * just schedule the send to happen later.
2145 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002146 call_send = qp->s_head == READ_ONCE(qp->s_last) && !wr->next;
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002147
2148 for (; wr; wr = wr->next) {
Mike Marciniszyn91702b42016-02-14 12:45:44 -08002149 err = rvt_post_one_wr(qp, wr, &call_send);
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002150 if (unlikely(err)) {
2151 *bad_wr = wr;
2152 goto bail;
2153 }
2154 nreq++;
2155 }
2156bail:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002157 spin_unlock_irqrestore(&qp->s_hlock, flags);
2158 if (nreq) {
Michael J. Ruhl0b79b272018-09-10 09:49:27 -07002159 /*
2160 * Only call do_send if there is exactly one packet, and the
2161 * driver said it was ok.
2162 */
2163 if (nreq == 1 && call_send)
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002164 rdi->driver_f.do_send(qp);
Jubin Johne6d2e012016-04-12 10:47:00 -07002165 else
2166 rdi->driver_f.schedule_send_no_lock(qp);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08002167 }
Dennis Dalessandrobfbac092016-01-22 13:00:22 -08002168 return err;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08002169}
2170
2171/**
2172 * rvt_post_srq_receive - post a receive on a shared receive queue
2173 * @ibsrq: the SRQ to post the receive on
2174 * @wr: the list of work requests to post
2175 * @bad_wr: A pointer to the first WR to cause a problem is put here
2176 *
2177 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -08002178 *
2179 * Return: 0 on success else errno
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08002180 */
Bart Van Assched34ac5c2018-07-18 09:25:32 -07002181int rvt_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
2182 const struct ib_recv_wr **bad_wr)
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08002183{
Jubin Johnb8f881b2016-02-03 14:14:36 -08002184 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002185 struct rvt_krwq *wq;
Jubin Johnb8f881b2016-02-03 14:14:36 -08002186 unsigned long flags;
2187
2188 for (; wr; wr = wr->next) {
2189 struct rvt_rwqe *wqe;
2190 u32 next;
2191 int i;
2192
2193 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
2194 *bad_wr = wr;
2195 return -EINVAL;
2196 }
2197
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002198 spin_lock_irqsave(&srq->rq.kwq->p_lock, flags);
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002199 wq = srq->rq.kwq;
Jubin Johnb8f881b2016-02-03 14:14:36 -08002200 next = wq->head + 1;
2201 if (next >= srq->rq.size)
2202 next = 0;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002203 if (next == READ_ONCE(wq->tail)) {
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002204 spin_unlock_irqrestore(&srq->rq.kwq->p_lock, flags);
Jubin Johnb8f881b2016-02-03 14:14:36 -08002205 *bad_wr = wr;
2206 return -ENOMEM;
2207 }
2208
2209 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
2210 wqe->wr_id = wr->wr_id;
2211 wqe->num_sge = wr->num_sge;
2212 for (i = 0; i < wr->num_sge; i++)
2213 wqe->sg_list[i] = wr->sg_list[i];
2214 /* Make sure queue entry is written before the head index. */
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002215 smp_store_release(&wq->head, next);
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002216 spin_unlock_irqrestore(&srq->rq.kwq->p_lock, flags);
Jubin Johnb8f881b2016-02-03 14:14:36 -08002217 }
2218 return 0;
Dennis Dalessandro8cf40202016-01-06 10:01:17 -08002219}
Brian Weltybeb5a042017-02-08 05:27:01 -08002220
Brian Welty832369f2018-05-02 06:44:03 -07002221/*
2222 * Validate a RWQE and fill in the SGE state.
2223 * Return 1 if OK.
2224 */
2225static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
2226{
2227 int i, j, ret;
2228 struct ib_wc wc;
2229 struct rvt_lkey_table *rkt;
2230 struct rvt_pd *pd;
2231 struct rvt_sge_state *ss;
2232 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2233
2234 rkt = &rdi->lkey_table;
2235 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
2236 ss = &qp->r_sge;
2237 ss->sg_list = qp->r_sg_list;
2238 qp->r_len = 0;
2239 for (i = j = 0; i < wqe->num_sge; i++) {
2240 if (wqe->sg_list[i].length == 0)
2241 continue;
2242 /* Check LKEY */
2243 ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
2244 NULL, &wqe->sg_list[i],
2245 IB_ACCESS_LOCAL_WRITE);
2246 if (unlikely(ret <= 0))
2247 goto bad_lkey;
2248 qp->r_len += wqe->sg_list[i].length;
2249 j++;
2250 }
2251 ss->num_sge = j;
2252 ss->total_len = qp->r_len;
2253 return 1;
2254
2255bad_lkey:
2256 while (j) {
2257 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
2258
2259 rvt_put_mr(sge->mr);
2260 }
2261 ss->num_sge = 0;
2262 memset(&wc, 0, sizeof(wc));
2263 wc.wr_id = wqe->wr_id;
2264 wc.status = IB_WC_LOC_PROT_ERR;
2265 wc.opcode = IB_WC_RECV;
2266 wc.qp = &qp->ibqp;
2267 /* Signal solicited completion event. */
2268 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
2269 return 0;
2270}
2271
2272/**
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002273 * get_count - count numbers of request work queue entries
2274 * in circular buffer
2275 * @rq: data structure for request queue entry
2276 * @tail: tail indices of the circular buffer
2277 * @head: head indices of the circular buffer
2278 *
2279 * Return - total number of entries in the circular buffer
2280 */
2281static u32 get_count(struct rvt_rq *rq, u32 tail, u32 head)
2282{
2283 u32 count;
2284
2285 count = head;
2286
2287 if (count >= rq->size)
2288 count = 0;
2289 if (count < tail)
2290 count += rq->size - tail;
2291 else
2292 count -= tail;
2293
2294 return count;
2295}
2296
2297/**
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002298 * get_rvt_head - get head indices of the circular buffer
2299 * @rq: data structure for request queue entry
2300 * @ip: the QP
2301 *
2302 * Return - head index value
2303 */
2304static inline u32 get_rvt_head(struct rvt_rq *rq, void *ip)
2305{
2306 u32 head;
2307
2308 if (ip)
2309 head = RDMA_READ_UAPI_ATOMIC(rq->wq->head);
2310 else
2311 head = rq->kwq->head;
2312
2313 return head;
2314}
2315
2316/**
Brian Welty832369f2018-05-02 06:44:03 -07002317 * rvt_get_rwqe - copy the next RWQE into the QP's RWQE
2318 * @qp: the QP
2319 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
2320 *
2321 * Return -1 if there is a local error, 0 if no RWQE is available,
2322 * otherwise return 1.
2323 *
2324 * Can be called from interrupt level.
2325 */
2326int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only)
2327{
2328 unsigned long flags;
2329 struct rvt_rq *rq;
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002330 struct rvt_krwq *kwq = NULL;
Brian Welty832369f2018-05-02 06:44:03 -07002331 struct rvt_rwq *wq;
2332 struct rvt_srq *srq;
2333 struct rvt_rwqe *wqe;
2334 void (*handler)(struct ib_event *, void *);
2335 u32 tail;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002336 u32 head;
Brian Welty832369f2018-05-02 06:44:03 -07002337 int ret;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002338 void *ip = NULL;
Brian Welty832369f2018-05-02 06:44:03 -07002339
2340 if (qp->ibqp.srq) {
2341 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
2342 handler = srq->ibsrq.event_handler;
2343 rq = &srq->rq;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002344 ip = srq->ip;
Brian Welty832369f2018-05-02 06:44:03 -07002345 } else {
2346 srq = NULL;
2347 handler = NULL;
2348 rq = &qp->r_rq;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002349 ip = qp->ip;
Brian Welty832369f2018-05-02 06:44:03 -07002350 }
2351
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002352 spin_lock_irqsave(&rq->kwq->c_lock, flags);
Brian Welty832369f2018-05-02 06:44:03 -07002353 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
2354 ret = 0;
2355 goto unlock;
2356 }
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002357 kwq = rq->kwq;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002358 if (ip) {
2359 wq = rq->wq;
2360 tail = RDMA_READ_UAPI_ATOMIC(wq->tail);
2361 } else {
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002362 tail = kwq->tail;
2363 }
Brian Welty832369f2018-05-02 06:44:03 -07002364
Brian Welty832369f2018-05-02 06:44:03 -07002365 /* Validate tail before using it since it is user writable. */
2366 if (tail >= rq->size)
2367 tail = 0;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002368
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002369 if (kwq->count < RVT_RWQ_COUNT_THRESHOLD) {
2370 head = get_rvt_head(rq, ip);
2371 kwq->count = get_count(rq, tail, head);
2372 }
2373 if (unlikely(kwq->count == 0)) {
Brian Welty832369f2018-05-02 06:44:03 -07002374 ret = 0;
2375 goto unlock;
2376 }
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002377 /* Make sure entry is read after the count is read. */
Brian Welty832369f2018-05-02 06:44:03 -07002378 smp_rmb();
2379 wqe = rvt_get_rwqe_ptr(rq, tail);
2380 /*
2381 * Even though we update the tail index in memory, the verbs
2382 * consumer is not supposed to post more entries until a
2383 * completion is generated.
2384 */
2385 if (++tail >= rq->size)
2386 tail = 0;
Kamenee Arumugamdabac6e2019-06-28 14:04:24 -04002387 if (ip)
2388 RDMA_WRITE_UAPI_ATOMIC(wq->tail, tail);
2389 else
2390 kwq->tail = tail;
Brian Welty832369f2018-05-02 06:44:03 -07002391 if (!wr_id_only && !init_sge(qp, wqe)) {
2392 ret = -1;
2393 goto unlock;
2394 }
2395 qp->r_wr_id = wqe->wr_id;
2396
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002397 kwq->count--;
Brian Welty832369f2018-05-02 06:44:03 -07002398 ret = 1;
2399 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
2400 if (handler) {
Brian Welty832369f2018-05-02 06:44:03 -07002401 /*
2402 * Validate head pointer value and compute
2403 * the number of remaining WQEs.
2404 */
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002405 if (kwq->count < srq->limit) {
2406 kwq->count = get_count(rq, tail, get_rvt_head(rq, ip));
2407 if (kwq->count < srq->limit) {
2408 struct ib_event ev;
Brian Welty832369f2018-05-02 06:44:03 -07002409
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002410 srq->limit = 0;
2411 spin_unlock_irqrestore(&rq->kwq->c_lock, flags);
2412 ev.device = qp->ibqp.device;
2413 ev.element.srq = qp->ibqp.srq;
2414 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
2415 handler(&ev, srq->ibsrq.srq_context);
2416 goto bail;
2417 }
Brian Welty832369f2018-05-02 06:44:03 -07002418 }
2419 }
2420unlock:
Kamenee Arumugamf592ae32019-06-28 14:04:30 -04002421 spin_unlock_irqrestore(&rq->kwq->c_lock, flags);
Brian Welty832369f2018-05-02 06:44:03 -07002422bail:
2423 return ret;
2424}
2425EXPORT_SYMBOL(rvt_get_rwqe);
2426
Brian Weltybeb5a042017-02-08 05:27:01 -08002427/**
2428 * qp_comm_est - handle trap with QP established
2429 * @qp: the QP
2430 */
2431void rvt_comm_est(struct rvt_qp *qp)
2432{
2433 qp->r_flags |= RVT_R_COMM_EST;
2434 if (qp->ibqp.event_handler) {
2435 struct ib_event ev;
2436
2437 ev.device = qp->ibqp.device;
2438 ev.element.qp = &qp->ibqp;
2439 ev.event = IB_EVENT_COMM_EST;
2440 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2441 }
2442}
2443EXPORT_SYMBOL(rvt_comm_est);
2444
2445void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
2446{
2447 unsigned long flags;
2448 int lastwqe;
2449
2450 spin_lock_irqsave(&qp->s_lock, flags);
2451 lastwqe = rvt_error_qp(qp, err);
2452 spin_unlock_irqrestore(&qp->s_lock, flags);
2453
2454 if (lastwqe) {
2455 struct ib_event ev;
2456
2457 ev.device = qp->ibqp.device;
2458 ev.element.qp = &qp->ibqp;
2459 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2460 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2461 }
2462}
2463EXPORT_SYMBOL(rvt_rc_error);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002464
Don Hiatt881fccb2017-02-08 05:28:19 -08002465/*
2466 * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
2467 * @index - the index
2468 * return usec from an index into ib_rvt_rnr_table
2469 */
2470unsigned long rvt_rnr_tbl_to_usec(u32 index)
2471{
Don Hiatt832666c2017-02-08 05:28:25 -08002472 return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
Don Hiatt881fccb2017-02-08 05:28:19 -08002473}
2474EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
2475
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002476static inline unsigned long rvt_aeth_to_usec(u32 aeth)
2477{
Don Hiatt832666c2017-02-08 05:28:25 -08002478 return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
2479 IB_AETH_CREDIT_MASK];
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002480}
2481
2482/*
Kaike Wan039cd3d2019-01-23 19:31:57 -08002483 * rvt_add_retry_timer_ext - add/start a retry timer
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002484 * @qp - the QP
Kaike Wan039cd3d2019-01-23 19:31:57 -08002485 * @shift - timeout shift to wait for multiple packets
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002486 * add a retry timer on the QP
2487 */
Kaike Wan039cd3d2019-01-23 19:31:57 -08002488void rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift)
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002489{
2490 struct ib_qp *ibqp = &qp->ibqp;
2491 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2492
2493 lockdep_assert_held(&qp->s_lock);
2494 qp->s_flags |= RVT_S_TIMER;
2495 /* 4.096 usec. * (1 << qp->timeout) */
Kaike Wan039cd3d2019-01-23 19:31:57 -08002496 qp->s_timer.expires = jiffies + rdi->busy_jiffies +
2497 (qp->timeout_jiffies << shift);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002498 add_timer(&qp->s_timer);
2499}
Kaike Wan039cd3d2019-01-23 19:31:57 -08002500EXPORT_SYMBOL(rvt_add_retry_timer_ext);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002501
2502/**
2503 * rvt_add_rnr_timer - add/start an rnr timer
2504 * @qp - the QP
2505 * @aeth - aeth of RNR timeout, simulated aeth for loopback
2506 * add an rnr timer on the QP
2507 */
2508void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
2509{
2510 u32 to;
2511
2512 lockdep_assert_held(&qp->s_lock);
2513 qp->s_flags |= RVT_S_WAIT_RNR;
2514 to = rvt_aeth_to_usec(aeth);
Kaike Wan57f6b662017-12-18 19:57:28 -08002515 trace_rvt_rnrnak_add(qp, to);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002516 hrtimer_start(&qp->s_rnr_timer,
Mike Marciniszyn3ce459c2018-05-15 18:31:24 -07002517 ns_to_ktime(1000 * to), HRTIMER_MODE_REL_PINNED);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002518}
2519EXPORT_SYMBOL(rvt_add_rnr_timer);
2520
2521/**
2522 * rvt_stop_rc_timers - stop all timers
2523 * @qp - the QP
2524 * stop any pending timers
2525 */
2526void rvt_stop_rc_timers(struct rvt_qp *qp)
2527{
2528 lockdep_assert_held(&qp->s_lock);
2529 /* Remove QP from all timers */
2530 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2531 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2532 del_timer(&qp->s_timer);
2533 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2534 }
2535}
2536EXPORT_SYMBOL(rvt_stop_rc_timers);
2537
2538/**
2539 * rvt_stop_rnr_timer - stop an rnr timer
2540 * @qp - the QP
2541 *
2542 * stop an rnr timer and return if the timer
2543 * had been pending.
2544 */
Kaike Wan57f6b662017-12-18 19:57:28 -08002545static void rvt_stop_rnr_timer(struct rvt_qp *qp)
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002546{
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002547 lockdep_assert_held(&qp->s_lock);
2548 /* Remove QP from rnr timer */
2549 if (qp->s_flags & RVT_S_WAIT_RNR) {
2550 qp->s_flags &= ~RVT_S_WAIT_RNR;
Kaike Wan57f6b662017-12-18 19:57:28 -08002551 trace_rvt_rnrnak_stop(qp, 0);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002552 }
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002553}
2554
2555/**
2556 * rvt_del_timers_sync - wait for any timeout routines to exit
2557 * @qp - the QP
2558 */
2559void rvt_del_timers_sync(struct rvt_qp *qp)
2560{
2561 del_timer_sync(&qp->s_timer);
2562 hrtimer_cancel(&qp->s_rnr_timer);
2563}
2564EXPORT_SYMBOL(rvt_del_timers_sync);
2565
2566/**
2567 * This is called from s_timer for missing responses.
2568 */
Kees Cooka2930e52017-10-16 15:51:13 -07002569static void rvt_rc_timeout(struct timer_list *t)
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002570{
Kees Cooka2930e52017-10-16 15:51:13 -07002571 struct rvt_qp *qp = from_timer(qp, t, s_timer);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002572 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2573 unsigned long flags;
2574
2575 spin_lock_irqsave(&qp->r_lock, flags);
2576 spin_lock(&qp->s_lock);
2577 if (qp->s_flags & RVT_S_TIMER) {
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002578 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
2579
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002580 qp->s_flags &= ~RVT_S_TIMER;
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002581 rvp->n_rc_timeouts++;
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002582 del_timer(&qp->s_timer);
Sebastian Sanchez5f14e4e2017-03-20 17:25:55 -07002583 trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002584 if (rdi->driver_f.notify_restart_rc)
2585 rdi->driver_f.notify_restart_rc(qp,
2586 qp->s_last_psn + 1,
2587 1);
2588 rdi->driver_f.schedule_send(qp);
2589 }
2590 spin_unlock(&qp->s_lock);
2591 spin_unlock_irqrestore(&qp->r_lock, flags);
2592}
2593
2594/*
2595 * This is called from s_timer for RNR timeouts.
2596 */
2597enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2598{
2599 struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2600 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2601 unsigned long flags;
2602
2603 spin_lock_irqsave(&qp->s_lock, flags);
2604 rvt_stop_rnr_timer(qp);
Kaike Wan57f6b662017-12-18 19:57:28 -08002605 trace_rvt_rnrnak_timeout(qp, 0);
Venkata Sandeep Dhanalakota11a10d42017-02-08 05:27:13 -08002606 rdi->driver_f.schedule_send(qp);
2607 spin_unlock_irqrestore(&qp->s_lock, flags);
2608 return HRTIMER_NORESTART;
2609}
2610EXPORT_SYMBOL(rvt_rc_rnr_retry);
Mike Marciniszyn4734b4f2017-08-28 11:23:45 -07002611
2612/**
2613 * rvt_qp_iter_init - initial for QP iteration
Randy Dunlap4f9a3012018-01-05 16:22:32 -08002614 * @rdi: rvt devinfo
2615 * @v: u64 value
Mike Marciniszyn4734b4f2017-08-28 11:23:45 -07002616 *
2617 * This returns an iterator suitable for iterating QPs
2618 * in the system.
2619 *
2620 * The @cb is a user defined callback and @v is a 64
2621 * bit value passed to and relevant for processing in the
2622 * @cb. An example use case would be to alter QP processing
2623 * based on criteria not part of the rvt_qp.
2624 *
2625 * Use cases that require memory allocation to succeed
2626 * must preallocate appropriately.
2627 *
2628 * Return: a pointer to an rvt_qp_iter or NULL
2629 */
2630struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
2631 u64 v,
2632 void (*cb)(struct rvt_qp *qp, u64 v))
2633{
2634 struct rvt_qp_iter *i;
2635
2636 i = kzalloc(sizeof(*i), GFP_KERNEL);
2637 if (!i)
2638 return NULL;
2639
2640 i->rdi = rdi;
2641 /* number of special QPs (SMI/GSI) for device */
2642 i->specials = rdi->ibdev.phys_port_cnt * 2;
2643 i->v = v;
2644 i->cb = cb;
2645
2646 return i;
2647}
2648EXPORT_SYMBOL(rvt_qp_iter_init);
2649
2650/**
2651 * rvt_qp_iter_next - return the next QP in iter
2652 * @iter - the iterator
2653 *
2654 * Fine grained QP iterator suitable for use
2655 * with debugfs seq_file mechanisms.
2656 *
2657 * Updates iter->qp with the current QP when the return
2658 * value is 0.
2659 *
2660 * Return: 0 - iter->qp is valid 1 - no more QPs
2661 */
2662int rvt_qp_iter_next(struct rvt_qp_iter *iter)
2663 __must_hold(RCU)
2664{
2665 int n = iter->n;
2666 int ret = 1;
2667 struct rvt_qp *pqp = iter->qp;
2668 struct rvt_qp *qp;
2669 struct rvt_dev_info *rdi = iter->rdi;
2670
2671 /*
2672 * The approach is to consider the special qps
2673 * as additional table entries before the
2674 * real hash table. Since the qp code sets
2675 * the qp->next hash link to NULL, this works just fine.
2676 *
2677 * iter->specials is 2 * # ports
2678 *
2679 * n = 0..iter->specials is the special qp indices
2680 *
2681 * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
2682 * the potential hash bucket entries
2683 *
2684 */
2685 for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
2686 if (pqp) {
2687 qp = rcu_dereference(pqp->next);
2688 } else {
2689 if (n < iter->specials) {
2690 struct rvt_ibport *rvp;
2691 int pidx;
2692
2693 pidx = n % rdi->ibdev.phys_port_cnt;
2694 rvp = rdi->ports[pidx];
2695 qp = rcu_dereference(rvp->qp[n & 1]);
2696 } else {
2697 qp = rcu_dereference(
2698 rdi->qp_dev->qp_table[
2699 (n - iter->specials)]);
2700 }
2701 }
2702 pqp = qp;
2703 if (qp) {
2704 iter->qp = qp;
2705 iter->n = n;
2706 return 0;
2707 }
2708 }
2709 return ret;
2710}
2711EXPORT_SYMBOL(rvt_qp_iter_next);
2712
2713/**
2714 * rvt_qp_iter - iterate all QPs
2715 * @rdi - rvt devinfo
2716 * @v - a 64 bit value
2717 * @cb - a callback
2718 *
2719 * This provides a way for iterating all QPs.
2720 *
2721 * The @cb is a user defined callback and @v is a 64
2722 * bit value passed to and relevant for processing in the
2723 * cb. An example use case would be to alter QP processing
2724 * based on criteria not part of the rvt_qp.
2725 *
2726 * The code has an internal iterator to simplify
2727 * non seq_file use cases.
2728 */
2729void rvt_qp_iter(struct rvt_dev_info *rdi,
2730 u64 v,
2731 void (*cb)(struct rvt_qp *qp, u64 v))
2732{
2733 int ret;
2734 struct rvt_qp_iter i = {
2735 .rdi = rdi,
2736 .specials = rdi->ibdev.phys_port_cnt * 2,
2737 .v = v,
2738 .cb = cb
2739 };
2740
2741 rcu_read_lock();
2742 do {
2743 ret = rvt_qp_iter_next(&i);
2744 if (!ret) {
2745 rvt_get_qp(i.qp);
2746 rcu_read_unlock();
2747 i.cb(i.qp, i.v);
2748 rcu_read_lock();
2749 rvt_put_qp(i.qp);
2750 }
2751 } while (!ret);
2752 rcu_read_unlock();
2753}
2754EXPORT_SYMBOL(rvt_qp_iter);
Brian Welty019f1182018-09-26 10:44:33 -07002755
Venkata Sandeep Dhanalakota116aa032018-09-26 10:44:42 -07002756/*
2757 * This should be called with s_lock held.
2758 */
2759void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
2760 enum ib_wc_status status)
2761{
2762 u32 old_last, last;
Mike Marciniszyn4a9ceb72019-06-13 08:30:52 -04002763 struct rvt_dev_info *rdi;
Venkata Sandeep Dhanalakota116aa032018-09-26 10:44:42 -07002764
2765 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
2766 return;
Mike Marciniszyn4a9ceb72019-06-13 08:30:52 -04002767 rdi = ib_to_rvt(qp->ibqp.device);
Venkata Sandeep Dhanalakota116aa032018-09-26 10:44:42 -07002768
Mike Marciniszyn4a9ceb72019-06-13 08:30:52 -04002769 old_last = qp->s_last;
2770 trace_rvt_qp_send_completion(qp, wqe, old_last);
2771 last = rvt_qp_complete_swqe(qp, wqe, rdi->wc_opcode[wqe->wr.opcode],
2772 status);
Venkata Sandeep Dhanalakota116aa032018-09-26 10:44:42 -07002773 if (qp->s_acked == old_last)
2774 qp->s_acked = last;
2775 if (qp->s_cur == old_last)
2776 qp->s_cur = last;
2777 if (qp->s_tail == old_last)
2778 qp->s_tail = last;
2779 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
2780 qp->s_draining = 0;
2781}
2782EXPORT_SYMBOL(rvt_send_complete);
2783
Brian Welty019f1182018-09-26 10:44:33 -07002784/**
2785 * rvt_copy_sge - copy data to SGE memory
2786 * @qp: associated QP
2787 * @ss: the SGE state
2788 * @data: the data to copy
2789 * @length: the length of the data
2790 * @release: boolean to release MR
2791 * @copy_last: do a separate copy of the last 8 bytes
2792 */
2793void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,
2794 void *data, u32 length,
2795 bool release, bool copy_last)
2796{
2797 struct rvt_sge *sge = &ss->sge;
2798 int i;
2799 bool in_last = false;
2800 bool cacheless_copy = false;
2801 struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2802 struct rvt_wss *wss = rdi->wss;
2803 unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode;
2804
2805 if (sge_copy_mode == RVT_SGE_COPY_CACHELESS) {
2806 cacheless_copy = length >= PAGE_SIZE;
2807 } else if (sge_copy_mode == RVT_SGE_COPY_ADAPTIVE) {
2808 if (length >= PAGE_SIZE) {
2809 /*
2810 * NOTE: this *assumes*:
2811 * o The first vaddr is the dest.
2812 * o If multiple pages, then vaddr is sequential.
2813 */
2814 wss_insert(wss, sge->vaddr);
2815 if (length >= (2 * PAGE_SIZE))
2816 wss_insert(wss, (sge->vaddr + PAGE_SIZE));
2817
2818 cacheless_copy = wss_exceeds_threshold(wss);
2819 } else {
2820 wss_advance_clean_counter(wss);
2821 }
2822 }
2823
2824 if (copy_last) {
2825 if (length > 8) {
2826 length -= 8;
2827 } else {
2828 copy_last = false;
2829 in_last = true;
2830 }
2831 }
2832
2833again:
2834 while (length) {
2835 u32 len = rvt_get_sge_length(sge, length);
2836
2837 WARN_ON_ONCE(len == 0);
2838 if (unlikely(in_last)) {
2839 /* enforce byte transfer ordering */
2840 for (i = 0; i < len; i++)
2841 ((u8 *)sge->vaddr)[i] = ((u8 *)data)[i];
2842 } else if (cacheless_copy) {
2843 cacheless_memcpy(sge->vaddr, data, len);
2844 } else {
2845 memcpy(sge->vaddr, data, len);
2846 }
2847 rvt_update_sge(ss, len, release);
2848 data += len;
2849 length -= len;
2850 }
2851
2852 if (copy_last) {
2853 copy_last = false;
2854 in_last = true;
2855 length = 8;
2856 goto again;
2857 }
2858}
2859EXPORT_SYMBOL(rvt_copy_sge);
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07002860
Michael J. Ruhld757c602019-02-26 08:45:25 -08002861static enum ib_wc_status loopback_qp_drop(struct rvt_ibport *rvp,
2862 struct rvt_qp *sqp)
2863{
2864 rvp->n_pkt_drops++;
2865 /*
2866 * For RC, the requester would timeout and retry so
2867 * shortcut the timeouts and just signal too many retries.
2868 */
2869 return sqp->ibqp.qp_type == IB_QPT_RC ?
2870 IB_WC_RETRY_EXC_ERR : IB_WC_SUCCESS;
2871}
2872
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07002873/**
2874 * ruc_loopback - handle UC and RC loopback requests
2875 * @sqp: the sending QP
2876 *
2877 * This is called from rvt_do_send() to forward a WQE addressed to the same HFI
2878 * Note that although we are single threaded due to the send engine, we still
2879 * have to protect against post_send(). We don't have to worry about
2880 * receive interrupts since this is a connected protocol and all packets
2881 * will pass through here.
2882 */
2883void rvt_ruc_loopback(struct rvt_qp *sqp)
2884{
2885 struct rvt_ibport *rvp = NULL;
2886 struct rvt_dev_info *rdi = ib_to_rvt(sqp->ibqp.device);
2887 struct rvt_qp *qp;
2888 struct rvt_swqe *wqe;
2889 struct rvt_sge *sge;
2890 unsigned long flags;
2891 struct ib_wc wc;
2892 u64 sdata;
2893 atomic64_t *maddr;
2894 enum ib_wc_status send_status;
2895 bool release;
2896 int ret;
2897 bool copy_last = false;
2898 int local_ops = 0;
2899
2900 rcu_read_lock();
2901 rvp = rdi->ports[sqp->port_num - 1];
2902
2903 /*
2904 * Note that we check the responder QP state after
2905 * checking the requester's state.
2906 */
2907
2908 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), rvp,
2909 sqp->remote_qpn);
2910
2911 spin_lock_irqsave(&sqp->s_lock, flags);
2912
2913 /* Return if we are already busy processing a work request. */
2914 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
2915 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
2916 goto unlock;
2917
2918 sqp->s_flags |= RVT_S_BUSY;
2919
2920again:
2921 if (sqp->s_last == READ_ONCE(sqp->s_head))
2922 goto clr_busy;
2923 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
2924
2925 /* Return if it is not OK to start a new work request. */
2926 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
2927 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
2928 goto clr_busy;
2929 /* We are in the error state, flush the work request. */
2930 send_status = IB_WC_WR_FLUSH_ERR;
2931 goto flush_send;
2932 }
2933
2934 /*
2935 * We can rely on the entry not changing without the s_lock
2936 * being held until we update s_last.
2937 * We increment s_cur to indicate s_last is in progress.
2938 */
2939 if (sqp->s_last == sqp->s_cur) {
2940 if (++sqp->s_cur >= sqp->s_size)
2941 sqp->s_cur = 0;
2942 }
2943 spin_unlock_irqrestore(&sqp->s_lock, flags);
2944
Michael J. Ruhld757c602019-02-26 08:45:25 -08002945 if (!qp) {
2946 send_status = loopback_qp_drop(rvp, sqp);
2947 goto serr_no_r_lock;
2948 }
2949 spin_lock_irqsave(&qp->r_lock, flags);
2950 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07002951 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
Michael J. Ruhld757c602019-02-26 08:45:25 -08002952 send_status = loopback_qp_drop(rvp, sqp);
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07002953 goto serr;
2954 }
2955
2956 memset(&wc, 0, sizeof(wc));
2957 send_status = IB_WC_SUCCESS;
2958
2959 release = true;
2960 sqp->s_sge.sge = wqe->sg_list[0];
2961 sqp->s_sge.sg_list = wqe->sg_list + 1;
2962 sqp->s_sge.num_sge = wqe->wr.num_sge;
2963 sqp->s_len = wqe->length;
2964 switch (wqe->wr.opcode) {
2965 case IB_WR_REG_MR:
2966 goto send_comp;
2967
2968 case IB_WR_LOCAL_INV:
2969 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
2970 if (rvt_invalidate_rkey(sqp,
2971 wqe->wr.ex.invalidate_rkey))
2972 send_status = IB_WC_LOC_PROT_ERR;
2973 local_ops = 1;
2974 }
2975 goto send_comp;
2976
2977 case IB_WR_SEND_WITH_INV:
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07002978 case IB_WR_SEND_WITH_IMM:
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07002979 case IB_WR_SEND:
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07002980 ret = rvt_get_rwqe(qp, false);
2981 if (ret < 0)
2982 goto op_err;
2983 if (!ret)
2984 goto rnr_nak;
Mike Marciniszyn09ce351d2019-01-17 12:42:16 -08002985 if (wqe->length > qp->r_len)
2986 goto inv_err;
Mike Marciniszyn38bbc9f2019-02-26 08:45:16 -08002987 switch (wqe->wr.opcode) {
2988 case IB_WR_SEND_WITH_INV:
2989 if (!rvt_invalidate_rkey(qp,
2990 wqe->wr.ex.invalidate_rkey)) {
2991 wc.wc_flags = IB_WC_WITH_INVALIDATE;
2992 wc.ex.invalidate_rkey =
2993 wqe->wr.ex.invalidate_rkey;
2994 }
2995 break;
2996 case IB_WR_SEND_WITH_IMM:
2997 wc.wc_flags = IB_WC_WITH_IMM;
2998 wc.ex.imm_data = wqe->wr.ex.imm_data;
2999 break;
3000 default:
3001 break;
3002 }
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07003003 break;
3004
3005 case IB_WR_RDMA_WRITE_WITH_IMM:
3006 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
3007 goto inv_err;
3008 wc.wc_flags = IB_WC_WITH_IMM;
3009 wc.ex.imm_data = wqe->wr.ex.imm_data;
3010 ret = rvt_get_rwqe(qp, true);
3011 if (ret < 0)
3012 goto op_err;
3013 if (!ret)
3014 goto rnr_nak;
3015 /* skip copy_last set and qp_access_flags recheck */
3016 goto do_write;
3017 case IB_WR_RDMA_WRITE:
3018 copy_last = rvt_is_user_qp(qp);
3019 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
3020 goto inv_err;
3021do_write:
3022 if (wqe->length == 0)
3023 break;
3024 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
3025 wqe->rdma_wr.remote_addr,
3026 wqe->rdma_wr.rkey,
3027 IB_ACCESS_REMOTE_WRITE)))
3028 goto acc_err;
3029 qp->r_sge.sg_list = NULL;
3030 qp->r_sge.num_sge = 1;
3031 qp->r_sge.total_len = wqe->length;
3032 break;
3033
3034 case IB_WR_RDMA_READ:
3035 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
3036 goto inv_err;
3037 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
3038 wqe->rdma_wr.remote_addr,
3039 wqe->rdma_wr.rkey,
3040 IB_ACCESS_REMOTE_READ)))
3041 goto acc_err;
3042 release = false;
3043 sqp->s_sge.sg_list = NULL;
3044 sqp->s_sge.num_sge = 1;
3045 qp->r_sge.sge = wqe->sg_list[0];
3046 qp->r_sge.sg_list = wqe->sg_list + 1;
3047 qp->r_sge.num_sge = wqe->wr.num_sge;
3048 qp->r_sge.total_len = wqe->length;
3049 break;
3050
3051 case IB_WR_ATOMIC_CMP_AND_SWP:
3052 case IB_WR_ATOMIC_FETCH_AND_ADD:
3053 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
3054 goto inv_err;
3055 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
3056 wqe->atomic_wr.remote_addr,
3057 wqe->atomic_wr.rkey,
3058 IB_ACCESS_REMOTE_ATOMIC)))
3059 goto acc_err;
3060 /* Perform atomic OP and save result. */
3061 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
3062 sdata = wqe->atomic_wr.compare_add;
3063 *(u64 *)sqp->s_sge.sge.vaddr =
3064 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
3065 (u64)atomic64_add_return(sdata, maddr) - sdata :
3066 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
3067 sdata, wqe->atomic_wr.swap);
3068 rvt_put_mr(qp->r_sge.sge.mr);
3069 qp->r_sge.num_sge = 0;
3070 goto send_comp;
3071
3072 default:
3073 send_status = IB_WC_LOC_QP_OP_ERR;
3074 goto serr;
3075 }
3076
3077 sge = &sqp->s_sge.sge;
3078 while (sqp->s_len) {
Michael J. Ruhldb421a52019-01-23 19:08:29 -08003079 u32 len = rvt_get_sge_length(sge, sqp->s_len);
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07003080
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07003081 WARN_ON_ONCE(len == 0);
3082 rvt_copy_sge(qp, &qp->r_sge, sge->vaddr,
3083 len, release, copy_last);
Michael J. Ruhldb421a52019-01-23 19:08:29 -08003084 rvt_update_sge(&sqp->s_sge, len, !release);
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07003085 sqp->s_len -= len;
3086 }
3087 if (release)
3088 rvt_put_ss(&qp->r_sge);
3089
3090 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
3091 goto send_comp;
3092
3093 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
3094 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
3095 else
3096 wc.opcode = IB_WC_RECV;
3097 wc.wr_id = qp->r_wr_id;
3098 wc.status = IB_WC_SUCCESS;
3099 wc.byte_len = wqe->length;
3100 wc.qp = &qp->ibqp;
3101 wc.src_qp = qp->remote_qpn;
3102 wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
3103 wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
3104 wc.port_num = 1;
3105 /* Signal completion event if the solicited bit is set. */
3106 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
3107 wqe->wr.send_flags & IB_SEND_SOLICITED);
3108
3109send_comp:
Michael J. Ruhld757c602019-02-26 08:45:25 -08003110 spin_unlock_irqrestore(&qp->r_lock, flags);
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07003111 spin_lock_irqsave(&sqp->s_lock, flags);
3112 rvp->n_loop_pkts++;
3113flush_send:
3114 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
3115 rvt_send_complete(sqp, wqe, send_status);
3116 if (local_ops) {
3117 atomic_dec(&sqp->local_ops_pending);
3118 local_ops = 0;
3119 }
3120 goto again;
3121
3122rnr_nak:
3123 /* Handle RNR NAK */
3124 if (qp->ibqp.qp_type == IB_QPT_UC)
3125 goto send_comp;
3126 rvp->n_rnr_naks++;
3127 /*
3128 * Note: we don't need the s_lock held since the BUSY flag
3129 * makes this single threaded.
3130 */
3131 if (sqp->s_rnr_retry == 0) {
3132 send_status = IB_WC_RNR_RETRY_EXC_ERR;
3133 goto serr;
3134 }
3135 if (sqp->s_rnr_retry_cnt < 7)
3136 sqp->s_rnr_retry--;
Michael J. Ruhld757c602019-02-26 08:45:25 -08003137 spin_unlock_irqrestore(&qp->r_lock, flags);
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07003138 spin_lock_irqsave(&sqp->s_lock, flags);
3139 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
3140 goto clr_busy;
3141 rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
3142 IB_AETH_CREDIT_SHIFT);
3143 goto clr_busy;
3144
3145op_err:
3146 send_status = IB_WC_REM_OP_ERR;
3147 wc.status = IB_WC_LOC_QP_OP_ERR;
3148 goto err;
3149
3150inv_err:
Mike Marciniszyn09ce351d2019-01-17 12:42:16 -08003151 send_status =
3152 sqp->ibqp.qp_type == IB_QPT_RC ?
3153 IB_WC_REM_INV_REQ_ERR :
3154 IB_WC_SUCCESS;
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07003155 wc.status = IB_WC_LOC_QP_OP_ERR;
3156 goto err;
3157
3158acc_err:
3159 send_status = IB_WC_REM_ACCESS_ERR;
3160 wc.status = IB_WC_LOC_PROT_ERR;
3161err:
3162 /* responder goes to error state */
3163 rvt_rc_error(qp, wc.status);
3164
3165serr:
Michael J. Ruhld757c602019-02-26 08:45:25 -08003166 spin_unlock_irqrestore(&qp->r_lock, flags);
3167serr_no_r_lock:
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -07003168 spin_lock_irqsave(&sqp->s_lock, flags);
3169 rvt_send_complete(sqp, wqe, send_status);
3170 if (sqp->ibqp.qp_type == IB_QPT_RC) {
3171 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
3172
3173 sqp->s_flags &= ~RVT_S_BUSY;
3174 spin_unlock_irqrestore(&sqp->s_lock, flags);
3175 if (lastwqe) {
3176 struct ib_event ev;
3177
3178 ev.device = sqp->ibqp.device;
3179 ev.element.qp = &sqp->ibqp;
3180 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
3181 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
3182 }
3183 goto done;
3184 }
3185clr_busy:
3186 sqp->s_flags &= ~RVT_S_BUSY;
3187unlock:
3188 spin_unlock_irqrestore(&sqp->s_lock, flags);
3189done:
3190 rcu_read_unlock();
3191}
3192EXPORT_SYMBOL(rvt_ruc_loopback);