Commit | Line | Data |
---|---|---|
b518d3e6 | 1 | /* |
7f90a5a0 | 2 | * Copyright(c) 2016 - 2020 Intel Corporation. |
b518d3e6 DD |
3 | * |
4 | * This file is provided under a dual BSD/GPLv2 license. When using or | |
5 | * redistributing this file, you may do so under either license. | |
6 | * | |
7 | * GPL LICENSE SUMMARY | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of version 2 of the GNU General Public License as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * BSD LICENSE | |
19 | * | |
20 | * Redistribution and use in source and binary forms, with or without | |
21 | * modification, are permitted provided that the following conditions | |
22 | * are met: | |
23 | * | |
24 | * - Redistributions of source code must retain the above copyright | |
25 | * notice, this list of conditions and the following disclaimer. | |
26 | * - Redistributions in binary form must reproduce the above copyright | |
27 | * notice, this list of conditions and the following disclaimer in | |
28 | * the documentation and/or other materials provided with the | |
29 | * distribution. | |
30 | * - Neither the name of Intel Corporation nor the names of its | |
31 | * contributors may be used to endorse or promote products derived | |
32 | * from this software without specific prior written permission. | |
33 | * | |
34 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
35 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
36 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
37 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
38 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
39 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
40 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
41 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
42 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
43 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
44 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
45 | * | |
46 | */ | |
47 | ||
3b0b3fb3 | 48 | #include <linux/hash.h> |
0acb0cc7 DD |
49 | #include <linux/bitops.h> |
50 | #include <linux/lockdep.h> | |
515667f8 DD |
51 | #include <linux/vmalloc.h> |
52 | #include <linux/slab.h> | |
53 | #include <rdma/ib_verbs.h> | |
832666c1 | 54 | #include <rdma/ib_hdrs.h> |
13c19222 | 55 | #include <rdma/opa_addr.h> |
89944450 | 56 | #include <rdma/uverbs_ioctl.h> |
b518d3e6 | 57 | #include "qp.h" |
515667f8 | 58 | #include "vt.h" |
3b0b3fb3 | 59 | #include "trace.h" |
b518d3e6 | 60 | |
f592ae3c KA |
61 | #define RVT_RWQ_COUNT_THRESHOLD 16 |
62 | ||
a2930e5c | 63 | static void rvt_rc_timeout(struct timer_list *t); |
f92e4871 KW |
64 | static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, |
65 | enum ib_qp_type type); | |
11a10d4b VSD |
66 | |
67 | /* | |
68 | * Convert the AETH RNR timeout code into the number of microseconds. | |
69 | */ | |
70 | static const u32 ib_rvt_rnr_table[32] = { | |
71 | 655360, /* 00: 655.36 */ | |
72 | 10, /* 01: .01 */ | |
73 | 20, /* 02 .02 */ | |
74 | 30, /* 03: .03 */ | |
75 | 40, /* 04: .04 */ | |
76 | 60, /* 05: .06 */ | |
77 | 80, /* 06: .08 */ | |
78 | 120, /* 07: .12 */ | |
79 | 160, /* 08: .16 */ | |
80 | 240, /* 09: .24 */ | |
81 | 320, /* 0A: .32 */ | |
82 | 480, /* 0B: .48 */ | |
83 | 640, /* 0C: .64 */ | |
84 | 960, /* 0D: .96 */ | |
85 | 1280, /* 0E: 1.28 */ | |
86 | 1920, /* 0F: 1.92 */ | |
87 | 2560, /* 10: 2.56 */ | |
88 | 3840, /* 11: 3.84 */ | |
89 | 5120, /* 12: 5.12 */ | |
90 | 7680, /* 13: 7.68 */ | |
91 | 10240, /* 14: 10.24 */ | |
92 | 15360, /* 15: 15.36 */ | |
93 | 20480, /* 16: 20.48 */ | |
94 | 30720, /* 17: 30.72 */ | |
95 | 40960, /* 18: 40.96 */ | |
96 | 61440, /* 19: 61.44 */ | |
97 | 81920, /* 1A: 81.92 */ | |
98 | 122880, /* 1B: 122.88 */ | |
99 | 163840, /* 1C: 163.84 */ | |
100 | 245760, /* 1D: 245.76 */ | |
101 | 327680, /* 1E: 327.68 */ | |
102 | 491520 /* 1F: 491.52 */ | |
103 | }; | |
104 | ||
bfbac097 DD |
105 | /* |
106 | * Note that it is OK to post send work requests in the SQE and ERR | |
107 | * states; rvt_do_send() will process them and generate error | |
108 | * completions as per IB 1.2 C10-96. | |
109 | */ | |
110 | const int ib_rvt_state_ops[IB_QPS_ERR + 1] = { | |
111 | [IB_QPS_RESET] = 0, | |
112 | [IB_QPS_INIT] = RVT_POST_RECV_OK, | |
113 | [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK, | |
114 | [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK | | |
115 | RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK | | |
116 | RVT_PROCESS_NEXT_SEND_OK, | |
117 | [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK | | |
118 | RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK, | |
119 | [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK | | |
120 | RVT_POST_SEND_OK | RVT_FLUSH_SEND, | |
121 | [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV | | |
122 | RVT_POST_SEND_OK | RVT_FLUSH_SEND, | |
123 | }; | |
124 | EXPORT_SYMBOL(ib_rvt_state_ops); | |
125 | ||
019f118b BW |
126 | /* platform specific: return the last level cache (llc) size, in KiB */ |
127 | static int rvt_wss_llc_size(void) | |
128 | { | |
129 | /* assume that the boot CPU value is universal for all CPUs */ | |
130 | return boot_cpu_data.x86_cache_size; | |
131 | } | |
132 | ||
133 | /* platform specific: cacheless copy */ | |
134 | static void cacheless_memcpy(void *dst, void *src, size_t n) | |
135 | { | |
136 | /* | |
137 | * Use the only available X64 cacheless copy. Add a __user cast | |
138 | * to quiet sparse. The src agument is already in the kernel so | |
139 | * there are no security issues. The extra fault recovery machinery | |
140 | * is not invoked. | |
141 | */ | |
142 | __copy_user_nocache(dst, (void __user *)src, n, 0); | |
143 | } | |
144 | ||
145 | void rvt_wss_exit(struct rvt_dev_info *rdi) | |
146 | { | |
147 | struct rvt_wss *wss = rdi->wss; | |
148 | ||
149 | if (!wss) | |
150 | return; | |
151 | ||
152 | /* coded to handle partially initialized and repeat callers */ | |
153 | kfree(wss->entries); | |
154 | wss->entries = NULL; | |
155 | kfree(rdi->wss); | |
156 | rdi->wss = NULL; | |
157 | } | |
158 | ||
159 | /** | |
160 | * rvt_wss_init - Init wss data structures | |
161 | * | |
162 | * Return: 0 on success | |
163 | */ | |
164 | int rvt_wss_init(struct rvt_dev_info *rdi) | |
165 | { | |
166 | unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode; | |
167 | unsigned int wss_threshold = rdi->dparms.wss_threshold; | |
168 | unsigned int wss_clean_period = rdi->dparms.wss_clean_period; | |
169 | long llc_size; | |
170 | long llc_bits; | |
171 | long table_size; | |
172 | long table_bits; | |
173 | struct rvt_wss *wss; | |
174 | int node = rdi->dparms.node; | |
175 | ||
176 | if (sge_copy_mode != RVT_SGE_COPY_ADAPTIVE) { | |
177 | rdi->wss = NULL; | |
178 | return 0; | |
179 | } | |
180 | ||
181 | rdi->wss = kzalloc_node(sizeof(*rdi->wss), GFP_KERNEL, node); | |
182 | if (!rdi->wss) | |
183 | return -ENOMEM; | |
184 | wss = rdi->wss; | |
185 | ||
186 | /* check for a valid percent range - default to 80 if none or invalid */ | |
187 | if (wss_threshold < 1 || wss_threshold > 100) | |
188 | wss_threshold = 80; | |
189 | ||
190 | /* reject a wildly large period */ | |
191 | if (wss_clean_period > 1000000) | |
192 | wss_clean_period = 256; | |
193 | ||
194 | /* reject a zero period */ | |
195 | if (wss_clean_period == 0) | |
196 | wss_clean_period = 1; | |
197 | ||
198 | /* | |
199 | * Calculate the table size - the next power of 2 larger than the | |
200 | * LLC size. LLC size is in KiB. | |
201 | */ | |
202 | llc_size = rvt_wss_llc_size() * 1024; | |
203 | table_size = roundup_pow_of_two(llc_size); | |
204 | ||
205 | /* one bit per page in rounded up table */ | |
206 | llc_bits = llc_size / PAGE_SIZE; | |
207 | table_bits = table_size / PAGE_SIZE; | |
208 | wss->pages_mask = table_bits - 1; | |
209 | wss->num_entries = table_bits / BITS_PER_LONG; | |
210 | ||
211 | wss->threshold = (llc_bits * wss_threshold) / 100; | |
212 | if (wss->threshold == 0) | |
213 | wss->threshold = 1; | |
214 | ||
215 | wss->clean_period = wss_clean_period; | |
216 | atomic_set(&wss->clean_counter, wss_clean_period); | |
217 | ||
218 | wss->entries = kcalloc_node(wss->num_entries, sizeof(*wss->entries), | |
219 | GFP_KERNEL, node); | |
220 | if (!wss->entries) { | |
221 | rvt_wss_exit(rdi); | |
222 | return -ENOMEM; | |
223 | } | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Advance the clean counter. When the clean period has expired, | |
230 | * clean an entry. | |
231 | * | |
232 | * This is implemented in atomics to avoid locking. Because multiple | |
233 | * variables are involved, it can be racy which can lead to slightly | |
234 | * inaccurate information. Since this is only a heuristic, this is | |
235 | * OK. Any innaccuracies will clean themselves out as the counter | |
236 | * advances. That said, it is unlikely the entry clean operation will | |
237 | * race - the next possible racer will not start until the next clean | |
238 | * period. | |
239 | * | |
240 | * The clean counter is implemented as a decrement to zero. When zero | |
241 | * is reached an entry is cleaned. | |
242 | */ | |
243 | static void wss_advance_clean_counter(struct rvt_wss *wss) | |
244 | { | |
245 | int entry; | |
246 | int weight; | |
247 | unsigned long bits; | |
248 | ||
249 | /* become the cleaner if we decrement the counter to zero */ | |
250 | if (atomic_dec_and_test(&wss->clean_counter)) { | |
251 | /* | |
252 | * Set, not add, the clean period. This avoids an issue | |
253 | * where the counter could decrement below the clean period. | |
254 | * Doing a set can result in lost decrements, slowing the | |
255 | * clean advance. Since this a heuristic, this possible | |
256 | * slowdown is OK. | |
257 | * | |
258 | * An alternative is to loop, advancing the counter by a | |
259 | * clean period until the result is > 0. However, this could | |
260 | * lead to several threads keeping another in the clean loop. | |
261 | * This could be mitigated by limiting the number of times | |
262 | * we stay in the loop. | |
263 | */ | |
264 | atomic_set(&wss->clean_counter, wss->clean_period); | |
265 | ||
266 | /* | |
267 | * Uniquely grab the entry to clean and move to next. | |
268 | * The current entry is always the lower bits of | |
269 | * wss.clean_entry. The table size, wss.num_entries, | |
270 | * is always a power-of-2. | |
271 | */ | |
272 | entry = (atomic_inc_return(&wss->clean_entry) - 1) | |
273 | & (wss->num_entries - 1); | |
274 | ||
275 | /* clear the entry and count the bits */ | |
276 | bits = xchg(&wss->entries[entry], 0); | |
277 | weight = hweight64((u64)bits); | |
278 | /* only adjust the contended total count if needed */ | |
279 | if (weight) | |
280 | atomic_sub(weight, &wss->total_count); | |
281 | } | |
282 | } | |
283 | ||
284 | /* | |
285 | * Insert the given address into the working set array. | |
286 | */ | |
287 | static void wss_insert(struct rvt_wss *wss, void *address) | |
288 | { | |
289 | u32 page = ((unsigned long)address >> PAGE_SHIFT) & wss->pages_mask; | |
290 | u32 entry = page / BITS_PER_LONG; /* assumes this ends up a shift */ | |
291 | u32 nr = page & (BITS_PER_LONG - 1); | |
292 | ||
293 | if (!test_and_set_bit(nr, &wss->entries[entry])) | |
294 | atomic_inc(&wss->total_count); | |
295 | ||
296 | wss_advance_clean_counter(wss); | |
297 | } | |
298 | ||
299 | /* | |
300 | * Is the working set larger than the threshold? | |
301 | */ | |
302 | static inline bool wss_exceeds_threshold(struct rvt_wss *wss) | |
303 | { | |
304 | return atomic_read(&wss->total_count) >= wss->threshold; | |
305 | } | |
306 | ||
d2b8d4da | 307 | static void get_map_page(struct rvt_qpn_table *qpt, |
0f4d027c | 308 | struct rvt_qpn_map *map) |
0acb0cc7 | 309 | { |
0f4d027c | 310 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
0acb0cc7 DD |
311 | |
312 | /* | |
313 | * Free the page if someone raced with us installing it. | |
314 | */ | |
315 | ||
316 | spin_lock(&qpt->lock); | |
317 | if (map->page) | |
318 | free_page(page); | |
319 | else | |
320 | map->page = (void *)page; | |
321 | spin_unlock(&qpt->lock); | |
322 | } | |
323 | ||
324 | /** | |
325 | * init_qpn_table - initialize the QP number table for a device | |
326 | * @qpt: the QPN table | |
327 | */ | |
328 | static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt) | |
329 | { | |
330 | u32 offset, i; | |
331 | struct rvt_qpn_map *map; | |
332 | int ret = 0; | |
333 | ||
fef2efd6 | 334 | if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start)) |
0acb0cc7 DD |
335 | return -EINVAL; |
336 | ||
337 | spin_lock_init(&qpt->lock); | |
338 | ||
339 | qpt->last = rdi->dparms.qpn_start; | |
340 | qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift; | |
341 | ||
342 | /* | |
343 | * Drivers may want some QPs beyond what we need for verbs let them use | |
344 | * our qpn table. No need for two. Lets go ahead and mark the bitmaps | |
345 | * for those. The reserved range must be *after* the range which verbs | |
346 | * will pick from. | |
347 | */ | |
348 | ||
349 | /* Figure out number of bit maps needed before reserved range */ | |
350 | qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE; | |
351 | ||
352 | /* This should always be zero */ | |
353 | offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK; | |
354 | ||
355 | /* Starting with the first reserved bit map */ | |
356 | map = &qpt->map[qpt->nmaps]; | |
357 | ||
358 | rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n", | |
359 | rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end); | |
fef2efd6 | 360 | for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) { |
0acb0cc7 | 361 | if (!map->page) { |
0f4d027c | 362 | get_map_page(qpt, map); |
0acb0cc7 DD |
363 | if (!map->page) { |
364 | ret = -ENOMEM; | |
365 | break; | |
366 | } | |
367 | } | |
368 | set_bit(offset, map->page); | |
369 | offset++; | |
370 | if (offset == RVT_BITS_PER_PAGE) { | |
371 | /* next page */ | |
372 | qpt->nmaps++; | |
373 | map++; | |
374 | offset = 0; | |
375 | } | |
376 | } | |
377 | return ret; | |
378 | } | |
379 | ||
380 | /** | |
381 | * free_qpn_table - free the QP number table for a device | |
382 | * @qpt: the QPN table | |
383 | */ | |
384 | static void free_qpn_table(struct rvt_qpn_table *qpt) | |
385 | { | |
386 | int i; | |
387 | ||
388 | for (i = 0; i < ARRAY_SIZE(qpt->map); i++) | |
389 | free_page((unsigned long)qpt->map[i].page); | |
390 | } | |
391 | ||
90793f71 DD |
392 | /** |
393 | * rvt_driver_qp_init - Init driver qp resources | |
394 | * @rdi: rvt dev strucutre | |
395 | * | |
396 | * Return: 0 on success | |
397 | */ | |
0acb0cc7 DD |
398 | int rvt_driver_qp_init(struct rvt_dev_info *rdi) |
399 | { | |
400 | int i; | |
401 | int ret = -ENOMEM; | |
402 | ||
0acb0cc7 DD |
403 | if (!rdi->dparms.qp_table_size) |
404 | return -EINVAL; | |
405 | ||
406 | /* | |
407 | * If driver is not doing any QP allocation then make sure it is | |
408 | * providing the necessary QP functions. | |
409 | */ | |
515667f8 DD |
410 | if (!rdi->driver_f.free_all_qps || |
411 | !rdi->driver_f.qp_priv_alloc || | |
412 | !rdi->driver_f.qp_priv_free || | |
11a10d4b VSD |
413 | !rdi->driver_f.notify_qp_reset || |
414 | !rdi->driver_f.notify_restart_rc) | |
0acb0cc7 DD |
415 | return -EINVAL; |
416 | ||
417 | /* allocate parent object */ | |
d1b697b6 MH |
418 | rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL, |
419 | rdi->dparms.node); | |
0acb0cc7 DD |
420 | if (!rdi->qp_dev) |
421 | return -ENOMEM; | |
422 | ||
423 | /* allocate hash table */ | |
424 | rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size; | |
425 | rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size); | |
426 | rdi->qp_dev->qp_table = | |
3c073478 | 427 | kmalloc_array_node(rdi->qp_dev->qp_table_size, |
d1b697b6 MH |
428 | sizeof(*rdi->qp_dev->qp_table), |
429 | GFP_KERNEL, rdi->dparms.node); | |
0acb0cc7 DD |
430 | if (!rdi->qp_dev->qp_table) |
431 | goto no_qp_table; | |
432 | ||
433 | for (i = 0; i < rdi->qp_dev->qp_table_size; i++) | |
434 | RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL); | |
435 | ||
436 | spin_lock_init(&rdi->qp_dev->qpt_lock); | |
437 | ||
438 | /* initialize qpn map */ | |
439 | if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table)) | |
440 | goto fail_table; | |
441 | ||
515667f8 DD |
442 | spin_lock_init(&rdi->n_qps_lock); |
443 | ||
444 | return 0; | |
0acb0cc7 DD |
445 | |
446 | fail_table: | |
447 | kfree(rdi->qp_dev->qp_table); | |
448 | free_qpn_table(&rdi->qp_dev->qpn_table); | |
449 | ||
450 | no_qp_table: | |
451 | kfree(rdi->qp_dev); | |
452 | ||
453 | return ret; | |
454 | } | |
455 | ||
456 | /** | |
f92e4871 KW |
457 | * rvt_free_qp_cb - callback function to reset a qp |
458 | * @qp: the qp to reset | |
459 | * @v: a 64-bit value | |
460 | * | |
461 | * This function resets the qp and removes it from the | |
462 | * qp hash table. | |
463 | */ | |
464 | static void rvt_free_qp_cb(struct rvt_qp *qp, u64 v) | |
465 | { | |
466 | unsigned int *qp_inuse = (unsigned int *)v; | |
467 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); | |
468 | ||
469 | /* Reset the qp and remove it from the qp hash list */ | |
470 | rvt_reset_qp(rdi, qp, qp->ibqp.qp_type); | |
471 | ||
472 | /* Increment the qp_inuse count */ | |
473 | (*qp_inuse)++; | |
474 | } | |
475 | ||
476 | /** | |
477 | * rvt_free_all_qps - check for QPs still in use | |
4f9a3018 | 478 | * @rdi: rvt device info structure |
0acb0cc7 DD |
479 | * |
480 | * There should not be any QPs still in use. | |
481 | * Free memory for table. | |
f92e4871 | 482 | * Return the number of QPs still in use. |
0acb0cc7 | 483 | */ |
515667f8 | 484 | static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi) |
0acb0cc7 | 485 | { |
f92e4871 | 486 | unsigned int qp_inuse = 0; |
0acb0cc7 | 487 | |
4e74080b DD |
488 | qp_inuse += rvt_mcast_tree_empty(rdi); |
489 | ||
f92e4871 KW |
490 | rvt_qp_iter(rdi, (u64)&qp_inuse, rvt_free_qp_cb); |
491 | ||
0acb0cc7 DD |
492 | return qp_inuse; |
493 | } | |
494 | ||
90793f71 DD |
495 | /** |
496 | * rvt_qp_exit - clean up qps on device exit | |
497 | * @rdi: rvt dev structure | |
498 | * | |
499 | * Check for qp leaks and free resources. | |
500 | */ | |
0acb0cc7 DD |
501 | void rvt_qp_exit(struct rvt_dev_info *rdi) |
502 | { | |
515667f8 | 503 | u32 qps_inuse = rvt_free_all_qps(rdi); |
0acb0cc7 | 504 | |
0acb0cc7 DD |
505 | if (qps_inuse) |
506 | rvt_pr_err(rdi, "QP memory leak! %u still in use\n", | |
507 | qps_inuse); | |
508 | if (!rdi->qp_dev) | |
509 | return; | |
510 | ||
511 | kfree(rdi->qp_dev->qp_table); | |
512 | free_qpn_table(&rdi->qp_dev->qpn_table); | |
513 | kfree(rdi->qp_dev); | |
514 | } | |
515 | ||
515667f8 DD |
516 | static inline unsigned mk_qpn(struct rvt_qpn_table *qpt, |
517 | struct rvt_qpn_map *map, unsigned off) | |
518 | { | |
519 | return (map - qpt->map) * RVT_BITS_PER_PAGE + off; | |
520 | } | |
521 | ||
f1badc71 DD |
522 | /** |
523 | * alloc_qpn - Allocate the next available qpn or zero/one for QP type | |
524 | * IB_QPT_SMI/IB_QPT_GSI | |
4f9a3018 RD |
525 | * @rdi: rvt device info structure |
526 | * @qpt: queue pair number table pointer | |
527 | * @port_num: IB port number, 1 based, comes from core | |
7f90a5a0 | 528 | * @exclude_prefix: prefix of special queue pair number being allocated |
f1badc71 DD |
529 | * |
530 | * Return: The queue pair number | |
515667f8 DD |
531 | */ |
532 | static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt, | |
7f90a5a0 | 533 | enum ib_qp_type type, u8 port_num, u8 exclude_prefix) |
515667f8 DD |
534 | { |
535 | u32 i, offset, max_scan, qpn; | |
536 | struct rvt_qpn_map *map; | |
537 | u32 ret; | |
7f90a5a0 GL |
538 | u32 max_qpn = exclude_prefix == RVT_AIP_QP_PREFIX ? |
539 | RVT_AIP_QPN_MAX : RVT_QPN_MAX; | |
515667f8 DD |
540 | |
541 | if (rdi->driver_f.alloc_qpn) | |
0f4d027c | 542 | return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num); |
515667f8 DD |
543 | |
544 | if (type == IB_QPT_SMI || type == IB_QPT_GSI) { | |
545 | unsigned n; | |
546 | ||
547 | ret = type == IB_QPT_GSI; | |
f1badc71 | 548 | n = 1 << (ret + 2 * (port_num - 1)); |
515667f8 DD |
549 | spin_lock(&qpt->lock); |
550 | if (qpt->flags & n) | |
551 | ret = -EINVAL; | |
552 | else | |
553 | qpt->flags |= n; | |
554 | spin_unlock(&qpt->lock); | |
555 | goto bail; | |
556 | } | |
557 | ||
558 | qpn = qpt->last + qpt->incr; | |
7f90a5a0 | 559 | if (qpn >= max_qpn) |
515667f8 DD |
560 | qpn = qpt->incr | ((qpt->last & 1) ^ 1); |
561 | /* offset carries bit 0 */ | |
562 | offset = qpn & RVT_BITS_PER_PAGE_MASK; | |
563 | map = &qpt->map[qpn / RVT_BITS_PER_PAGE]; | |
564 | max_scan = qpt->nmaps - !offset; | |
565 | for (i = 0;;) { | |
566 | if (unlikely(!map->page)) { | |
0f4d027c | 567 | get_map_page(qpt, map); |
515667f8 DD |
568 | if (unlikely(!map->page)) |
569 | break; | |
570 | } | |
571 | do { | |
572 | if (!test_and_set_bit(offset, map->page)) { | |
573 | qpt->last = qpn; | |
574 | ret = qpn; | |
575 | goto bail; | |
576 | } | |
577 | offset += qpt->incr; | |
578 | /* | |
579 | * This qpn might be bogus if offset >= BITS_PER_PAGE. | |
580 | * That is OK. It gets re-assigned below | |
581 | */ | |
582 | qpn = mk_qpn(qpt, map, offset); | |
583 | } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX); | |
584 | /* | |
585 | * In order to keep the number of pages allocated to a | |
586 | * minimum, we scan the all existing pages before increasing | |
587 | * the size of the bitmap table. | |
588 | */ | |
589 | if (++i > max_scan) { | |
590 | if (qpt->nmaps == RVT_QPNMAP_ENTRIES) | |
591 | break; | |
592 | map = &qpt->map[qpt->nmaps++]; | |
593 | /* start at incr with current bit 0 */ | |
594 | offset = qpt->incr | (offset & 1); | |
595 | } else if (map < &qpt->map[qpt->nmaps]) { | |
596 | ++map; | |
597 | /* start at incr with current bit 0 */ | |
598 | offset = qpt->incr | (offset & 1); | |
599 | } else { | |
600 | map = &qpt->map[0]; | |
601 | /* wrap to first map page, invert bit 0 */ | |
602 | offset = qpt->incr | ((offset & 1) ^ 1); | |
603 | } | |
501edc42 | 604 | /* there can be no set bits in low-order QoS bits */ |
2abae62a MM |
605 | WARN_ON(rdi->dparms.qos_shift > 1 && |
606 | offset & ((BIT(rdi->dparms.qos_shift - 1) - 1) << 1)); | |
515667f8 DD |
607 | qpn = mk_qpn(qpt, map, offset); |
608 | } | |
609 | ||
610 | ret = -ENOMEM; | |
611 | ||
612 | bail: | |
613 | return ret; | |
614 | } | |
615 | ||
79a225be DD |
616 | /** |
617 | * rvt_clear_mr_refs - Drop help mr refs | |
618 | * @qp: rvt qp data structure | |
619 | * @clr_sends: If shoudl clear send side or not | |
620 | */ | |
621 | static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends) | |
622 | { | |
623 | unsigned n; | |
8b103e9c | 624 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); |
79a225be DD |
625 | |
626 | if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags)) | |
627 | rvt_put_ss(&qp->s_rdma_read_sge); | |
628 | ||
629 | rvt_put_ss(&qp->r_sge); | |
630 | ||
631 | if (clr_sends) { | |
632 | while (qp->s_last != qp->s_head) { | |
633 | struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last); | |
79a225be | 634 | |
d40f69c9 | 635 | rvt_put_qp_swqe(qp, wqe); |
79a225be DD |
636 | if (++qp->s_last >= qp->s_size) |
637 | qp->s_last = 0; | |
638 | smp_wmb(); /* see qp_set_savail */ | |
639 | } | |
640 | if (qp->s_rdma_mr) { | |
641 | rvt_put_mr(qp->s_rdma_mr); | |
642 | qp->s_rdma_mr = NULL; | |
643 | } | |
644 | } | |
645 | ||
0208da90 | 646 | for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) { |
79a225be DD |
647 | struct rvt_ack_entry *e = &qp->s_ack_queue[n]; |
648 | ||
fe508272 | 649 | if (e->rdma_sge.mr) { |
79a225be DD |
650 | rvt_put_mr(e->rdma_sge.mr); |
651 | e->rdma_sge.mr = NULL; | |
652 | } | |
653 | } | |
654 | } | |
655 | ||
0208da90 MM |
656 | /** |
657 | * rvt_swqe_has_lkey - return true if lkey is used by swqe | |
658 | * @wqe - the send wqe | |
659 | * @lkey - the lkey | |
660 | * | |
661 | * Test the swqe for using lkey | |
662 | */ | |
663 | static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey) | |
664 | { | |
665 | int i; | |
666 | ||
667 | for (i = 0; i < wqe->wr.num_sge; i++) { | |
668 | struct rvt_sge *sge = &wqe->sg_list[i]; | |
669 | ||
670 | if (rvt_mr_has_lkey(sge->mr, lkey)) | |
671 | return true; | |
672 | } | |
673 | return false; | |
674 | } | |
675 | ||
676 | /** | |
677 | * rvt_qp_sends_has_lkey - return true is qp sends use lkey | |
678 | * @qp - the rvt_qp | |
679 | * @lkey - the lkey | |
680 | */ | |
681 | static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey) | |
682 | { | |
683 | u32 s_last = qp->s_last; | |
684 | ||
685 | while (s_last != qp->s_head) { | |
686 | struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last); | |
687 | ||
688 | if (rvt_swqe_has_lkey(wqe, lkey)) | |
689 | return true; | |
690 | ||
691 | if (++s_last >= qp->s_size) | |
692 | s_last = 0; | |
693 | } | |
694 | if (qp->s_rdma_mr) | |
695 | if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey)) | |
696 | return true; | |
697 | return false; | |
698 | } | |
699 | ||
700 | /** | |
701 | * rvt_qp_acks_has_lkey - return true if acks have lkey | |
702 | * @qp - the qp | |
703 | * @lkey - the lkey | |
704 | */ | |
705 | static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey) | |
706 | { | |
707 | int i; | |
708 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); | |
709 | ||
710 | for (i = 0; qp->s_ack_queue && i < rvt_max_atomic(rdi); i++) { | |
711 | struct rvt_ack_entry *e = &qp->s_ack_queue[i]; | |
712 | ||
713 | if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey)) | |
714 | return true; | |
715 | } | |
716 | return false; | |
717 | } | |
718 | ||
719 | /* | |
720 | * rvt_qp_mr_clean - clean up remote ops for lkey | |
721 | * @qp - the qp | |
722 | * @lkey - the lkey that is being de-registered | |
723 | * | |
724 | * This routine checks if the lkey is being used by | |
725 | * the qp. | |
726 | * | |
727 | * If so, the qp is put into an error state to elminate | |
728 | * any references from the qp. | |
729 | */ | |
730 | void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey) | |
731 | { | |
732 | bool lastwqe = false; | |
733 | ||
734 | if (qp->ibqp.qp_type == IB_QPT_SMI || | |
735 | qp->ibqp.qp_type == IB_QPT_GSI) | |
736 | /* avoid special QPs */ | |
737 | return; | |
738 | spin_lock_irq(&qp->r_lock); | |
739 | spin_lock(&qp->s_hlock); | |
740 | spin_lock(&qp->s_lock); | |
741 | ||
742 | if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET) | |
743 | goto check_lwqe; | |
744 | ||
745 | if (rvt_ss_has_lkey(&qp->r_sge, lkey) || | |
746 | rvt_qp_sends_has_lkey(qp, lkey) || | |
747 | rvt_qp_acks_has_lkey(qp, lkey)) | |
748 | lastwqe = rvt_error_qp(qp, IB_WC_LOC_PROT_ERR); | |
749 | check_lwqe: | |
750 | spin_unlock(&qp->s_lock); | |
751 | spin_unlock(&qp->s_hlock); | |
752 | spin_unlock_irq(&qp->r_lock); | |
753 | if (lastwqe) { | |
754 | struct ib_event ev; | |
755 | ||
756 | ev.device = qp->ibqp.device; | |
757 | ev.element.qp = &qp->ibqp; | |
758 | ev.event = IB_EVENT_QP_LAST_WQE_REACHED; | |
759 | qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); | |
760 | } | |
761 | } | |
762 | ||
79a225be DD |
763 | /** |
764 | * rvt_remove_qp - remove qp form table | |
765 | * @rdi: rvt dev struct | |
766 | * @qp: qp to remove | |
767 | * | |
768 | * Remove the QP from the table so it can't be found asynchronously by | |
769 | * the receive routine. | |
770 | */ | |
771 | static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp) | |
772 | { | |
773 | struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1]; | |
774 | u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits); | |
775 | unsigned long flags; | |
776 | int removed = 1; | |
777 | ||
778 | spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags); | |
779 | ||
780 | if (rcu_dereference_protected(rvp->qp[0], | |
781 | lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) { | |
782 | RCU_INIT_POINTER(rvp->qp[0], NULL); | |
783 | } else if (rcu_dereference_protected(rvp->qp[1], | |
784 | lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) { | |
785 | RCU_INIT_POINTER(rvp->qp[1], NULL); | |
786 | } else { | |
787 | struct rvt_qp *q; | |
788 | struct rvt_qp __rcu **qpp; | |
789 | ||
790 | removed = 0; | |
791 | qpp = &rdi->qp_dev->qp_table[n]; | |
792 | for (; (q = rcu_dereference_protected(*qpp, | |
793 | lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL; | |
794 | qpp = &q->next) { | |
795 | if (q == qp) { | |
796 | RCU_INIT_POINTER(*qpp, | |
797 | rcu_dereference_protected(qp->next, | |
798 | lockdep_is_held(&rdi->qp_dev->qpt_lock))); | |
799 | removed = 1; | |
800 | trace_rvt_qpremove(qp, n); | |
801 | break; | |
802 | } | |
803 | } | |
804 | } | |
805 | ||
806 | spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags); | |
807 | if (removed) { | |
808 | synchronize_rcu(); | |
4d6f85c3 | 809 | rvt_put_qp(qp); |
79a225be DD |
810 | } |
811 | } | |
812 | ||
dabac6e4 KA |
813 | /** |
814 | * rvt_alloc_rq - allocate memory for user or kernel buffer | |
815 | * @rq: receive queue data structure | |
816 | * @size: number of request queue entries | |
817 | * @node: The NUMA node | |
818 | * @udata: True if user data is available or not false | |
819 | * | |
820 | * Return: If memory allocation failed, return -ENONEM | |
821 | * This function is used by both shared receive | |
822 | * queues and non-shared receive queues to allocate | |
823 | * memory. | |
824 | */ | |
825 | int rvt_alloc_rq(struct rvt_rq *rq, u32 size, int node, | |
826 | struct ib_udata *udata) | |
827 | { | |
828 | if (udata) { | |
829 | rq->wq = vmalloc_user(sizeof(struct rvt_rwq) + size); | |
830 | if (!rq->wq) | |
831 | goto bail; | |
832 | /* need kwq with no buffers */ | |
833 | rq->kwq = kzalloc_node(sizeof(*rq->kwq), GFP_KERNEL, node); | |
834 | if (!rq->kwq) | |
835 | goto bail; | |
836 | rq->kwq->curr_wq = rq->wq->wq; | |
837 | } else { | |
838 | /* need kwq with buffers */ | |
839 | rq->kwq = | |
840 | vzalloc_node(sizeof(struct rvt_krwq) + size, node); | |
841 | if (!rq->kwq) | |
842 | goto bail; | |
843 | rq->kwq->curr_wq = rq->kwq->wq; | |
844 | } | |
845 | ||
f592ae3c KA |
846 | spin_lock_init(&rq->kwq->p_lock); |
847 | spin_lock_init(&rq->kwq->c_lock); | |
dabac6e4 KA |
848 | return 0; |
849 | bail: | |
850 | rvt_free_rq(rq); | |
851 | return -ENOMEM; | |
852 | } | |
853 | ||
515667f8 | 854 | /** |
222f7a9a MM |
855 | * rvt_init_qp - initialize the QP state to the reset state |
856 | * @qp: the QP to init or reinit | |
515667f8 | 857 | * @type: the QP type |
222f7a9a MM |
858 | * |
859 | * This function is called from both rvt_create_qp() and | |
860 | * rvt_reset_qp(). The difference is that the reset | |
861 | * patch the necessary locks to protect against concurent | |
862 | * access. | |
515667f8 | 863 | */ |
222f7a9a MM |
864 | static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, |
865 | enum ib_qp_type type) | |
515667f8 | 866 | { |
3b0b3fb3 DD |
867 | qp->remote_qpn = 0; |
868 | qp->qkey = 0; | |
869 | qp->qp_access_flags = 0; | |
515667f8 DD |
870 | qp->s_flags &= RVT_S_SIGNAL_REQ_WR; |
871 | qp->s_hdrwords = 0; | |
872 | qp->s_wqe = NULL; | |
873 | qp->s_draining = 0; | |
874 | qp->s_next_psn = 0; | |
875 | qp->s_last_psn = 0; | |
876 | qp->s_sending_psn = 0; | |
877 | qp->s_sending_hpsn = 0; | |
878 | qp->s_psn = 0; | |
879 | qp->r_psn = 0; | |
880 | qp->r_msn = 0; | |
881 | if (type == IB_QPT_RC) { | |
882 | qp->s_state = IB_OPCODE_RC_SEND_LAST; | |
883 | qp->r_state = IB_OPCODE_RC_SEND_LAST; | |
884 | } else { | |
885 | qp->s_state = IB_OPCODE_UC_SEND_LAST; | |
886 | qp->r_state = IB_OPCODE_UC_SEND_LAST; | |
887 | } | |
888 | qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; | |
889 | qp->r_nak_state = 0; | |
890 | qp->r_aflags = 0; | |
891 | qp->r_flags = 0; | |
892 | qp->s_head = 0; | |
893 | qp->s_tail = 0; | |
894 | qp->s_cur = 0; | |
895 | qp->s_acked = 0; | |
896 | qp->s_last = 0; | |
897 | qp->s_ssn = 1; | |
898 | qp->s_lsn = 0; | |
899 | qp->s_mig_state = IB_MIG_MIGRATED; | |
515667f8 DD |
900 | qp->r_head_ack_queue = 0; |
901 | qp->s_tail_ack_queue = 0; | |
4f9264d1 | 902 | qp->s_acked_ack_queue = 0; |
515667f8 | 903 | qp->s_num_rd_atomic = 0; |
515667f8 | 904 | qp->r_sge.num_sge = 0; |
856cc4c2 | 905 | atomic_set(&qp->s_reserved_used, 0); |
515667f8 DD |
906 | } |
907 | ||
222f7a9a | 908 | /** |
f92e4871 | 909 | * _rvt_reset_qp - initialize the QP state to the reset state |
222f7a9a MM |
910 | * @qp: the QP to reset |
911 | * @type: the QP type | |
912 | * | |
913 | * r_lock, s_hlock, and s_lock are required to be held by the caller | |
914 | */ | |
f92e4871 KW |
915 | static void _rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, |
916 | enum ib_qp_type type) | |
222f7a9a MM |
917 | __must_hold(&qp->s_lock) |
918 | __must_hold(&qp->s_hlock) | |
919 | __must_hold(&qp->r_lock) | |
920 | { | |
68e78b3d MM |
921 | lockdep_assert_held(&qp->r_lock); |
922 | lockdep_assert_held(&qp->s_hlock); | |
923 | lockdep_assert_held(&qp->s_lock); | |
222f7a9a MM |
924 | if (qp->state != IB_QPS_RESET) { |
925 | qp->state = IB_QPS_RESET; | |
926 | ||
927 | /* Let drivers flush their waitlist */ | |
928 | rdi->driver_f.flush_qp_waiters(qp); | |
11a10d4b | 929 | rvt_stop_rc_timers(qp); |
222f7a9a MM |
930 | qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT); |
931 | spin_unlock(&qp->s_lock); | |
932 | spin_unlock(&qp->s_hlock); | |
933 | spin_unlock_irq(&qp->r_lock); | |
934 | ||
935 | /* Stop the send queue and the retry timer */ | |
936 | rdi->driver_f.stop_send_queue(qp); | |
11a10d4b | 937 | rvt_del_timers_sync(qp); |
222f7a9a MM |
938 | /* Wait for things to stop */ |
939 | rdi->driver_f.quiesce_qp(qp); | |
940 | ||
941 | /* take qp out the hash and wait for it to be unused */ | |
942 | rvt_remove_qp(rdi, qp); | |
222f7a9a MM |
943 | |
944 | /* grab the lock b/c it was locked at call time */ | |
945 | spin_lock_irq(&qp->r_lock); | |
946 | spin_lock(&qp->s_hlock); | |
947 | spin_lock(&qp->s_lock); | |
948 | ||
949 | rvt_clear_mr_refs(qp, 1); | |
950 | /* | |
951 | * Let the driver do any tear down or re-init it needs to for | |
952 | * a qp that has been reset | |
953 | */ | |
954 | rdi->driver_f.notify_qp_reset(qp); | |
955 | } | |
956 | rvt_init_qp(rdi, qp, type); | |
68e78b3d MM |
957 | lockdep_assert_held(&qp->r_lock); |
958 | lockdep_assert_held(&qp->s_hlock); | |
959 | lockdep_assert_held(&qp->s_lock); | |
222f7a9a MM |
960 | } |
961 | ||
f92e4871 KW |
962 | /** |
963 | * rvt_reset_qp - initialize the QP state to the reset state | |
964 | * @rdi: the device info | |
965 | * @qp: the QP to reset | |
966 | * @type: the QP type | |
967 | * | |
968 | * This is the wrapper function to acquire the r_lock, s_hlock, and s_lock | |
969 | * before calling _rvt_reset_qp(). | |
970 | */ | |
971 | static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, | |
972 | enum ib_qp_type type) | |
973 | { | |
974 | spin_lock_irq(&qp->r_lock); | |
975 | spin_lock(&qp->s_hlock); | |
976 | spin_lock(&qp->s_lock); | |
977 | _rvt_reset_qp(rdi, qp, type); | |
978 | spin_unlock(&qp->s_lock); | |
979 | spin_unlock(&qp->s_hlock); | |
980 | spin_unlock_irq(&qp->r_lock); | |
981 | } | |
982 | ||
b2f8a04e DD |
983 | /** rvt_free_qpn - Free a qpn from the bit map |
984 | * @qpt: QP table | |
985 | * @qpn: queue pair number to free | |
986 | */ | |
987 | static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn) | |
988 | { | |
989 | struct rvt_qpn_map *map; | |
990 | ||
7f90a5a0 GL |
991 | if ((qpn & RVT_AIP_QP_PREFIX_MASK) == RVT_AIP_QP_BASE) |
992 | qpn &= RVT_AIP_QP_SUFFIX; | |
993 | ||
6c31e528 | 994 | map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE; |
b2f8a04e DD |
995 | if (map->page) |
996 | clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page); | |
997 | } | |
998 | ||
fe2ac047 MR |
999 | /** |
1000 | * get_allowed_ops - Given a QP type return the appropriate allowed OP | |
1001 | * @type: valid, supported, QP type | |
1002 | */ | |
1003 | static u8 get_allowed_ops(enum ib_qp_type type) | |
1004 | { | |
1005 | return type == IB_QPT_RC ? IB_OPCODE_RC : type == IB_QPT_UC ? | |
1006 | IB_OPCODE_UC : IB_OPCODE_UD; | |
1007 | } | |
1008 | ||
d310c4bf MR |
1009 | /** |
1010 | * free_ud_wq_attr - Clean up AH attribute cache for UD QPs | |
1011 | * @qp: Valid QP with allowed_ops set | |
1012 | * | |
1013 | * The rvt_swqe data structure being used is a union, so this is | |
1014 | * only valid for UD QPs. | |
1015 | */ | |
1016 | static void free_ud_wq_attr(struct rvt_qp *qp) | |
1017 | { | |
1018 | struct rvt_swqe *wqe; | |
1019 | int i; | |
1020 | ||
1021 | for (i = 0; qp->allowed_ops == IB_OPCODE_UD && i < qp->s_size; i++) { | |
1022 | wqe = rvt_get_swqe_ptr(qp, i); | |
1023 | kfree(wqe->ud_wr.attr); | |
1024 | wqe->ud_wr.attr = NULL; | |
1025 | } | |
1026 | } | |
1027 | ||
1028 | /** | |
1029 | * alloc_ud_wq_attr - AH attribute cache for UD QPs | |
1030 | * @qp: Valid QP with allowed_ops set | |
1031 | * @node: Numa node for allocation | |
1032 | * | |
1033 | * The rvt_swqe data structure being used is a union, so this is | |
1034 | * only valid for UD QPs. | |
1035 | */ | |
1036 | static int alloc_ud_wq_attr(struct rvt_qp *qp, int node) | |
1037 | { | |
1038 | struct rvt_swqe *wqe; | |
1039 | int i; | |
1040 | ||
1041 | for (i = 0; qp->allowed_ops == IB_OPCODE_UD && i < qp->s_size; i++) { | |
1042 | wqe = rvt_get_swqe_ptr(qp, i); | |
1043 | wqe->ud_wr.attr = kzalloc_node(sizeof(*wqe->ud_wr.attr), | |
1044 | GFP_KERNEL, node); | |
1045 | if (!wqe->ud_wr.attr) { | |
1046 | free_ud_wq_attr(qp); | |
1047 | return -ENOMEM; | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | return 0; | |
1052 | } | |
1053 | ||
b518d3e6 DD |
1054 | /** |
1055 | * rvt_create_qp - create a queue pair for a device | |
1056 | * @ibpd: the protection domain who's device we create the queue pair for | |
1057 | * @init_attr: the attributes of the queue pair | |
1058 | * @udata: user data for libibverbs.so | |
1059 | * | |
515667f8 DD |
1060 | * Queue pair creation is mostly an rvt issue. However, drivers have their own |
1061 | * unique idea of what queue pair numbers mean. For instance there is a reserved | |
1062 | * range for PSM. | |
1063 | * | |
90793f71 | 1064 | * Return: the queue pair on success, otherwise returns an errno. |
b518d3e6 DD |
1065 | * |
1066 | * Called by the ib_create_qp() core verbs function. | |
1067 | */ | |
1068 | struct ib_qp *rvt_create_qp(struct ib_pd *ibpd, | |
1069 | struct ib_qp_init_attr *init_attr, | |
1070 | struct ib_udata *udata) | |
1071 | { | |
515667f8 DD |
1072 | struct rvt_qp *qp; |
1073 | int err; | |
1074 | struct rvt_swqe *swq = NULL; | |
1075 | size_t sz; | |
1076 | size_t sg_list_sz; | |
1077 | struct ib_qp *ret = ERR_PTR(-ENOMEM); | |
1078 | struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device); | |
1079 | void *priv = NULL; | |
afcf8f76 | 1080 | size_t sqsize; |
7f90a5a0 | 1081 | u8 exclude_prefix = 0; |
515667f8 DD |
1082 | |
1083 | if (!rdi) | |
1084 | return ERR_PTR(-EINVAL); | |
1085 | ||
33023fb8 | 1086 | if (init_attr->cap.max_send_sge > rdi->dparms.props.max_send_sge || |
515667f8 | 1087 | init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr || |
7f90a5a0 GL |
1088 | (init_attr->create_flags && |
1089 | init_attr->create_flags != IB_QP_CREATE_NETDEV_USE)) | |
515667f8 DD |
1090 | return ERR_PTR(-EINVAL); |
1091 | ||
1092 | /* Check receive queue parameters if no SRQ is specified. */ | |
1093 | if (!init_attr->srq) { | |
33023fb8 SW |
1094 | if (init_attr->cap.max_recv_sge > |
1095 | rdi->dparms.props.max_recv_sge || | |
515667f8 DD |
1096 | init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr) |
1097 | return ERR_PTR(-EINVAL); | |
1098 | ||
1099 | if (init_attr->cap.max_send_sge + | |
1100 | init_attr->cap.max_send_wr + | |
1101 | init_attr->cap.max_recv_sge + | |
1102 | init_attr->cap.max_recv_wr == 0) | |
1103 | return ERR_PTR(-EINVAL); | |
1104 | } | |
afcf8f76 | 1105 | sqsize = |
856cc4c2 MM |
1106 | init_attr->cap.max_send_wr + 1 + |
1107 | rdi->dparms.reserved_operations; | |
515667f8 DD |
1108 | switch (init_attr->qp_type) { |
1109 | case IB_QPT_SMI: | |
1110 | case IB_QPT_GSI: | |
1111 | if (init_attr->port_num == 0 || | |
1112 | init_attr->port_num > ibpd->device->phys_port_cnt) | |
1113 | return ERR_PTR(-EINVAL); | |
df561f66 | 1114 | fallthrough; |
515667f8 DD |
1115 | case IB_QPT_UC: |
1116 | case IB_QPT_RC: | |
1117 | case IB_QPT_UD: | |
34755f59 | 1118 | sz = struct_size(swq, sg_list, init_attr->cap.max_send_sge); |
fd7beced | 1119 | swq = vzalloc_node(array_size(sz, sqsize), rdi->dparms.node); |
515667f8 DD |
1120 | if (!swq) |
1121 | return ERR_PTR(-ENOMEM); | |
1122 | ||
1123 | sz = sizeof(*qp); | |
1124 | sg_list_sz = 0; | |
1125 | if (init_attr->srq) { | |
1126 | struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq); | |
1127 | ||
1128 | if (srq->rq.max_sge > 1) | |
1129 | sg_list_sz = sizeof(*qp->r_sg_list) * | |
1130 | (srq->rq.max_sge - 1); | |
1131 | } else if (init_attr->cap.max_recv_sge > 1) | |
1132 | sg_list_sz = sizeof(*qp->r_sg_list) * | |
1133 | (init_attr->cap.max_recv_sge - 1); | |
0f4d027c LR |
1134 | qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL, |
1135 | rdi->dparms.node); | |
515667f8 DD |
1136 | if (!qp) |
1137 | goto bail_swq; | |
fe2ac047 | 1138 | qp->allowed_ops = get_allowed_ops(init_attr->qp_type); |
515667f8 DD |
1139 | |
1140 | RCU_INIT_POINTER(qp->next, NULL); | |
8b103e9c MM |
1141 | if (init_attr->qp_type == IB_QPT_RC) { |
1142 | qp->s_ack_queue = | |
590b5b7d KC |
1143 | kcalloc_node(rvt_max_atomic(rdi), |
1144 | sizeof(*qp->s_ack_queue), | |
1145 | GFP_KERNEL, | |
1146 | rdi->dparms.node); | |
8b103e9c MM |
1147 | if (!qp->s_ack_queue) |
1148 | goto bail_qp; | |
1149 | } | |
11a10d4b | 1150 | /* initialize timers needed for rc qp */ |
a2930e5c | 1151 | timer_setup(&qp->s_timer, rvt_rc_timeout, 0); |
11a10d4b VSD |
1152 | hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC, |
1153 | HRTIMER_MODE_REL); | |
1154 | qp->s_rnr_timer.function = rvt_rc_rnr_retry; | |
515667f8 DD |
1155 | |
1156 | /* | |
1157 | * Driver needs to set up it's private QP structure and do any | |
1158 | * initialization that is needed. | |
1159 | */ | |
0f4d027c | 1160 | priv = rdi->driver_f.qp_priv_alloc(rdi, qp); |
c755f4af MM |
1161 | if (IS_ERR(priv)) { |
1162 | ret = priv; | |
515667f8 | 1163 | goto bail_qp; |
c755f4af | 1164 | } |
515667f8 DD |
1165 | qp->priv = priv; |
1166 | qp->timeout_jiffies = | |
1167 | usecs_to_jiffies((4096UL * (1UL << qp->timeout)) / | |
1168 | 1000UL); | |
1169 | if (init_attr->srq) { | |
1170 | sz = 0; | |
1171 | } else { | |
1172 | qp->r_rq.size = init_attr->cap.max_recv_wr + 1; | |
1173 | qp->r_rq.max_sge = init_attr->cap.max_recv_sge; | |
1174 | sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) + | |
1175 | sizeof(struct rvt_rwqe); | |
dabac6e4 KA |
1176 | err = rvt_alloc_rq(&qp->r_rq, qp->r_rq.size * sz, |
1177 | rdi->dparms.node, udata); | |
1178 | if (err) { | |
1179 | ret = ERR_PTR(err); | |
515667f8 | 1180 | goto bail_driver_priv; |
dabac6e4 | 1181 | } |
515667f8 DD |
1182 | } |
1183 | ||
1184 | /* | |
1185 | * ib_create_qp() will initialize qp->ibqp | |
1186 | * except for qp->ibqp.qp_num. | |
1187 | */ | |
1188 | spin_lock_init(&qp->r_lock); | |
46a80d62 | 1189 | spin_lock_init(&qp->s_hlock); |
515667f8 | 1190 | spin_lock_init(&qp->s_lock); |
515667f8 | 1191 | atomic_set(&qp->refcount, 0); |
d9f87239 | 1192 | atomic_set(&qp->local_ops_pending, 0); |
515667f8 | 1193 | init_waitqueue_head(&qp->wait); |
515667f8 DD |
1194 | INIT_LIST_HEAD(&qp->rspwait); |
1195 | qp->state = IB_QPS_RESET; | |
1196 | qp->s_wq = swq; | |
afcf8f76 | 1197 | qp->s_size = sqsize; |
46a80d62 | 1198 | qp->s_avail = init_attr->cap.max_send_wr; |
515667f8 DD |
1199 | qp->s_max_sge = init_attr->cap.max_send_sge; |
1200 | if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR) | |
1201 | qp->s_flags = RVT_S_SIGNAL_REQ_WR; | |
d310c4bf MR |
1202 | err = alloc_ud_wq_attr(qp, rdi->dparms.node); |
1203 | if (err) { | |
1204 | ret = (ERR_PTR(err)); | |
90a239ee | 1205 | goto bail_rq_rvt; |
d310c4bf | 1206 | } |
515667f8 | 1207 | |
7f90a5a0 GL |
1208 | if (init_attr->create_flags & IB_QP_CREATE_NETDEV_USE) |
1209 | exclude_prefix = RVT_AIP_QP_PREFIX; | |
1210 | ||
515667f8 DD |
1211 | err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table, |
1212 | init_attr->qp_type, | |
7f90a5a0 GL |
1213 | init_attr->port_num, |
1214 | exclude_prefix); | |
515667f8 DD |
1215 | if (err < 0) { |
1216 | ret = ERR_PTR(err); | |
1217 | goto bail_rq_wq; | |
1218 | } | |
1219 | qp->ibqp.qp_num = err; | |
7f90a5a0 GL |
1220 | if (init_attr->create_flags & IB_QP_CREATE_NETDEV_USE) |
1221 | qp->ibqp.qp_num |= RVT_AIP_QP_BASE; | |
515667f8 | 1222 | qp->port_num = init_attr->port_num; |
222f7a9a | 1223 | rvt_init_qp(rdi, qp, init_attr->qp_type); |
5190f052 MM |
1224 | if (rdi->driver_f.qp_priv_init) { |
1225 | err = rdi->driver_f.qp_priv_init(rdi, qp, init_attr); | |
1226 | if (err) { | |
1227 | ret = ERR_PTR(err); | |
1228 | goto bail_rq_wq; | |
1229 | } | |
1230 | } | |
515667f8 DD |
1231 | break; |
1232 | ||
1233 | default: | |
1234 | /* Don't support raw QPs */ | |
bb8865f4 | 1235 | return ERR_PTR(-EOPNOTSUPP); |
515667f8 DD |
1236 | } |
1237 | ||
1238 | init_attr->cap.max_inline_data = 0; | |
1239 | ||
1240 | /* | |
1241 | * Return the address of the RWQ as the offset to mmap. | |
bfbac097 | 1242 | * See rvt_mmap() for details. |
515667f8 DD |
1243 | */ |
1244 | if (udata && udata->outlen >= sizeof(__u64)) { | |
1245 | if (!qp->r_rq.wq) { | |
1246 | __u64 offset = 0; | |
1247 | ||
1248 | err = ib_copy_to_udata(udata, &offset, | |
1249 | sizeof(offset)); | |
1250 | if (err) { | |
1251 | ret = ERR_PTR(err); | |
1252 | goto bail_qpn; | |
1253 | } | |
1254 | } else { | |
1255 | u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz; | |
1256 | ||
ff23dfa1 | 1257 | qp->ip = rvt_create_mmap_info(rdi, s, udata, |
515667f8 | 1258 | qp->r_rq.wq); |
47c370c1 SM |
1259 | if (IS_ERR(qp->ip)) { |
1260 | ret = ERR_CAST(qp->ip); | |
515667f8 DD |
1261 | goto bail_qpn; |
1262 | } | |
1263 | ||
1264 | err = ib_copy_to_udata(udata, &qp->ip->offset, | |
1265 | sizeof(qp->ip->offset)); | |
1266 | if (err) { | |
1267 | ret = ERR_PTR(err); | |
1268 | goto bail_ip; | |
1269 | } | |
1270 | } | |
ef086c0d | 1271 | qp->pid = current->pid; |
515667f8 DD |
1272 | } |
1273 | ||
1274 | spin_lock(&rdi->n_qps_lock); | |
1275 | if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) { | |
1276 | spin_unlock(&rdi->n_qps_lock); | |
1277 | ret = ERR_PTR(-ENOMEM); | |
1278 | goto bail_ip; | |
1279 | } | |
1280 | ||
1281 | rdi->n_qps_allocated++; | |
bfee5e32 VM |
1282 | /* |
1283 | * Maintain a busy_jiffies variable that will be added to the timeout | |
1284 | * period in mod_retry_timer and add_retry_timer. This busy jiffies | |
1285 | * is scaled by the number of rc qps created for the device to reduce | |
1286 | * the number of timeouts occurring when there is a large number of | |
1287 | * qps. busy_jiffies is incremented every rc qp scaling interval. | |
1288 | * The scaling interval is selected based on extensive performance | |
1289 | * evaluation of targeted workloads. | |
1290 | */ | |
1291 | if (init_attr->qp_type == IB_QPT_RC) { | |
1292 | rdi->n_rc_qps++; | |
1293 | rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL; | |
1294 | } | |
515667f8 DD |
1295 | spin_unlock(&rdi->n_qps_lock); |
1296 | ||
1297 | if (qp->ip) { | |
1298 | spin_lock_irq(&rdi->pending_lock); | |
1299 | list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps); | |
1300 | spin_unlock_irq(&rdi->pending_lock); | |
1301 | } | |
1302 | ||
1303 | ret = &qp->ibqp; | |
1304 | ||
515667f8 DD |
1305 | return ret; |
1306 | ||
1307 | bail_ip: | |
22dccc54 JF |
1308 | if (qp->ip) |
1309 | kref_put(&qp->ip->ref, rvt_release_mmap_info); | |
515667f8 DD |
1310 | |
1311 | bail_qpn: | |
b2f8a04e | 1312 | rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num); |
515667f8 DD |
1313 | |
1314 | bail_rq_wq: | |
d310c4bf | 1315 | free_ud_wq_attr(qp); |
515667f8 | 1316 | |
90a239ee AP |
1317 | bail_rq_rvt: |
1318 | rvt_free_rq(&qp->r_rq); | |
1319 | ||
515667f8 DD |
1320 | bail_driver_priv: |
1321 | rdi->driver_f.qp_priv_free(rdi, qp); | |
1322 | ||
1323 | bail_qp: | |
8b103e9c | 1324 | kfree(qp->s_ack_queue); |
515667f8 DD |
1325 | kfree(qp); |
1326 | ||
1327 | bail_swq: | |
1328 | vfree(swq); | |
1329 | ||
1330 | return ret; | |
b518d3e6 DD |
1331 | } |
1332 | ||
3b0b3fb3 DD |
1333 | /** |
1334 | * rvt_error_qp - put a QP into the error state | |
1335 | * @qp: the QP to put into the error state | |
1336 | * @err: the receive completion error to signal if a RWQE is active | |
1337 | * | |
1338 | * Flushes both send and receive work queues. | |
90793f71 DD |
1339 | * |
1340 | * Return: true if last WQE event should be generated. | |
3b0b3fb3 DD |
1341 | * The QP r_lock and s_lock should be held and interrupts disabled. |
1342 | * If we are already in error state, just return. | |
1343 | */ | |
1344 | int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err) | |
1345 | { | |
1346 | struct ib_wc wc; | |
1347 | int ret = 0; | |
1348 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); | |
1349 | ||
68e78b3d MM |
1350 | lockdep_assert_held(&qp->r_lock); |
1351 | lockdep_assert_held(&qp->s_lock); | |
3b0b3fb3 DD |
1352 | if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET) |
1353 | goto bail; | |
1354 | ||
1355 | qp->state = IB_QPS_ERR; | |
1356 | ||
1357 | if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) { | |
1358 | qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR); | |
1359 | del_timer(&qp->s_timer); | |
1360 | } | |
1361 | ||
1362 | if (qp->s_flags & RVT_S_ANY_WAIT_SEND) | |
1363 | qp->s_flags &= ~RVT_S_ANY_WAIT_SEND; | |
1364 | ||
1365 | rdi->driver_f.notify_error_qp(qp); | |
1366 | ||
1367 | /* Schedule the sending tasklet to drain the send work queue. */ | |
6aa7de05 | 1368 | if (READ_ONCE(qp->s_last) != qp->s_head) |
3b0b3fb3 DD |
1369 | rdi->driver_f.schedule_send(qp); |
1370 | ||
1371 | rvt_clear_mr_refs(qp, 0); | |
1372 | ||
1373 | memset(&wc, 0, sizeof(wc)); | |
1374 | wc.qp = &qp->ibqp; | |
1375 | wc.opcode = IB_WC_RECV; | |
1376 | ||
1377 | if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) { | |
1378 | wc.wr_id = qp->r_wr_id; | |
1379 | wc.status = err; | |
1380 | rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1); | |
1381 | } | |
1382 | wc.status = IB_WC_WR_FLUSH_ERR; | |
1383 | ||
dabac6e4 | 1384 | if (qp->r_rq.kwq) { |
3b0b3fb3 DD |
1385 | u32 head; |
1386 | u32 tail; | |
dabac6e4 KA |
1387 | struct rvt_rwq *wq = NULL; |
1388 | struct rvt_krwq *kwq = NULL; | |
3b0b3fb3 | 1389 | |
f592ae3c | 1390 | spin_lock(&qp->r_rq.kwq->c_lock); |
dabac6e4 KA |
1391 | /* qp->ip used to validate if there is a user buffer mmaped */ |
1392 | if (qp->ip) { | |
1393 | wq = qp->r_rq.wq; | |
1394 | head = RDMA_READ_UAPI_ATOMIC(wq->head); | |
1395 | tail = RDMA_READ_UAPI_ATOMIC(wq->tail); | |
1396 | } else { | |
1397 | kwq = qp->r_rq.kwq; | |
1398 | head = kwq->head; | |
1399 | tail = kwq->tail; | |
1400 | } | |
3b0b3fb3 | 1401 | /* sanity check pointers before trusting them */ |
3b0b3fb3 DD |
1402 | if (head >= qp->r_rq.size) |
1403 | head = 0; | |
3b0b3fb3 DD |
1404 | if (tail >= qp->r_rq.size) |
1405 | tail = 0; | |
1406 | while (tail != head) { | |
1407 | wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id; | |
1408 | if (++tail >= qp->r_rq.size) | |
1409 | tail = 0; | |
1410 | rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1); | |
1411 | } | |
dabac6e4 KA |
1412 | if (qp->ip) |
1413 | RDMA_WRITE_UAPI_ATOMIC(wq->tail, tail); | |
1414 | else | |
1415 | kwq->tail = tail; | |
f592ae3c | 1416 | spin_unlock(&qp->r_rq.kwq->c_lock); |
3b0b3fb3 DD |
1417 | } else if (qp->ibqp.event_handler) { |
1418 | ret = 1; | |
1419 | } | |
1420 | ||
1421 | bail: | |
1422 | return ret; | |
1423 | } | |
1424 | EXPORT_SYMBOL(rvt_error_qp); | |
1425 | ||
1426 | /* | |
1427 | * Put the QP into the hash table. | |
1428 | * The hash table holds a reference to the QP. | |
1429 | */ | |
1430 | static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp) | |
1431 | { | |
1432 | struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1]; | |
1433 | unsigned long flags; | |
1434 | ||
4d6f85c3 | 1435 | rvt_get_qp(qp); |
3b0b3fb3 DD |
1436 | spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags); |
1437 | ||
1438 | if (qp->ibqp.qp_num <= 1) { | |
1439 | rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp); | |
1440 | } else { | |
1441 | u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits); | |
1442 | ||
1443 | qp->next = rdi->qp_dev->qp_table[n]; | |
1444 | rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp); | |
1445 | trace_rvt_qpinsert(qp, n); | |
1446 | } | |
1447 | ||
1448 | spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags); | |
1449 | } | |
1450 | ||
b518d3e6 | 1451 | /** |
61347fa6 | 1452 | * rvt_modify_qp - modify the attributes of a queue pair |
b518d3e6 DD |
1453 | * @ibqp: the queue pair who's attributes we're modifying |
1454 | * @attr: the new attributes | |
1455 | * @attr_mask: the mask of attributes to modify | |
1456 | * @udata: user data for libibverbs.so | |
1457 | * | |
90793f71 | 1458 | * Return: 0 on success, otherwise returns an errno. |
b518d3e6 DD |
1459 | */ |
1460 | int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, | |
1461 | int attr_mask, struct ib_udata *udata) | |
1462 | { | |
3b0b3fb3 DD |
1463 | struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device); |
1464 | struct rvt_qp *qp = ibqp_to_rvtqp(ibqp); | |
1465 | enum ib_qp_state cur_state, new_state; | |
1466 | struct ib_event ev; | |
1467 | int lastwqe = 0; | |
1468 | int mig = 0; | |
1469 | int pmtu = 0; /* for gcc warning only */ | |
13c19222 | 1470 | int opa_ah; |
3b0b3fb3 | 1471 | |
26e990ba JG |
1472 | if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS) |
1473 | return -EOPNOTSUPP; | |
1474 | ||
3b0b3fb3 | 1475 | spin_lock_irq(&qp->r_lock); |
46a80d62 | 1476 | spin_lock(&qp->s_hlock); |
3b0b3fb3 DD |
1477 | spin_lock(&qp->s_lock); |
1478 | ||
1479 | cur_state = attr_mask & IB_QP_CUR_STATE ? | |
1480 | attr->cur_qp_state : qp->state; | |
1481 | new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state; | |
13c19222 | 1482 | opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num); |
3b0b3fb3 DD |
1483 | |
1484 | if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, | |
d31131bb | 1485 | attr_mask)) |
3b0b3fb3 DD |
1486 | goto inval; |
1487 | ||
e85ec33d IW |
1488 | if (rdi->driver_f.check_modify_qp && |
1489 | rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata)) | |
1490 | goto inval; | |
1491 | ||
3b0b3fb3 | 1492 | if (attr_mask & IB_QP_AV) { |
13c19222 DH |
1493 | if (opa_ah) { |
1494 | if (rdma_ah_get_dlid(&attr->ah_attr) >= | |
1495 | opa_get_mcast_base(OPA_MCAST_NR)) | |
1496 | goto inval; | |
1497 | } else { | |
1498 | if (rdma_ah_get_dlid(&attr->ah_attr) >= | |
1499 | be16_to_cpu(IB_MULTICAST_LID_BASE)) | |
1500 | goto inval; | |
1501 | } | |
1502 | ||
3b0b3fb3 DD |
1503 | if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr)) |
1504 | goto inval; | |
1505 | } | |
1506 | ||
1507 | if (attr_mask & IB_QP_ALT_PATH) { | |
13c19222 DH |
1508 | if (opa_ah) { |
1509 | if (rdma_ah_get_dlid(&attr->alt_ah_attr) >= | |
1510 | opa_get_mcast_base(OPA_MCAST_NR)) | |
1511 | goto inval; | |
1512 | } else { | |
1513 | if (rdma_ah_get_dlid(&attr->alt_ah_attr) >= | |
1514 | be16_to_cpu(IB_MULTICAST_LID_BASE)) | |
1515 | goto inval; | |
1516 | } | |
1517 | ||
3b0b3fb3 DD |
1518 | if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr)) |
1519 | goto inval; | |
1520 | if (attr->alt_pkey_index >= rvt_get_npkeys(rdi)) | |
1521 | goto inval; | |
1522 | } | |
1523 | ||
1524 | if (attr_mask & IB_QP_PKEY_INDEX) | |
1525 | if (attr->pkey_index >= rvt_get_npkeys(rdi)) | |
1526 | goto inval; | |
1527 | ||
1528 | if (attr_mask & IB_QP_MIN_RNR_TIMER) | |
1529 | if (attr->min_rnr_timer > 31) | |
1530 | goto inval; | |
1531 | ||
1532 | if (attr_mask & IB_QP_PORT) | |
1533 | if (qp->ibqp.qp_type == IB_QPT_SMI || | |
1534 | qp->ibqp.qp_type == IB_QPT_GSI || | |
1535 | attr->port_num == 0 || | |
1536 | attr->port_num > ibqp->device->phys_port_cnt) | |
1537 | goto inval; | |
1538 | ||
1539 | if (attr_mask & IB_QP_DEST_QPN) | |
1540 | if (attr->dest_qp_num > RVT_QPN_MASK) | |
1541 | goto inval; | |
1542 | ||
1543 | if (attr_mask & IB_QP_RETRY_CNT) | |
1544 | if (attr->retry_cnt > 7) | |
1545 | goto inval; | |
1546 | ||
1547 | if (attr_mask & IB_QP_RNR_RETRY) | |
1548 | if (attr->rnr_retry > 7) | |
1549 | goto inval; | |
1550 | ||
b518d3e6 | 1551 | /* |
3b0b3fb3 DD |
1552 | * Don't allow invalid path_mtu values. OK to set greater |
1553 | * than the active mtu (or even the max_cap, if we have tuned | |
1554 | * that to a small mtu. We'll set qp->path_mtu | |
1555 | * to the lesser of requested attribute mtu and active, | |
1556 | * for packetizing messages. | |
1557 | * Note that the QP port has to be set in INIT and MTU in RTR. | |
b518d3e6 | 1558 | */ |
3b0b3fb3 DD |
1559 | if (attr_mask & IB_QP_PATH_MTU) { |
1560 | pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr); | |
1561 | if (pmtu < 0) | |
1562 | goto inval; | |
1563 | } | |
1564 | ||
1565 | if (attr_mask & IB_QP_PATH_MIG_STATE) { | |
1566 | if (attr->path_mig_state == IB_MIG_REARM) { | |
1567 | if (qp->s_mig_state == IB_MIG_ARMED) | |
1568 | goto inval; | |
1569 | if (new_state != IB_QPS_RTS) | |
1570 | goto inval; | |
1571 | } else if (attr->path_mig_state == IB_MIG_MIGRATED) { | |
1572 | if (qp->s_mig_state == IB_MIG_REARM) | |
1573 | goto inval; | |
1574 | if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD) | |
1575 | goto inval; | |
1576 | if (qp->s_mig_state == IB_MIG_ARMED) | |
1577 | mig = 1; | |
1578 | } else { | |
1579 | goto inval; | |
1580 | } | |
1581 | } | |
1582 | ||
1583 | if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) | |
1584 | if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic) | |
1585 | goto inval; | |
1586 | ||
1587 | switch (new_state) { | |
1588 | case IB_QPS_RESET: | |
1589 | if (qp->state != IB_QPS_RESET) | |
f92e4871 | 1590 | _rvt_reset_qp(rdi, qp, ibqp->qp_type); |
3b0b3fb3 DD |
1591 | break; |
1592 | ||
1593 | case IB_QPS_RTR: | |
1594 | /* Allow event to re-trigger if QP set to RTR more than once */ | |
1595 | qp->r_flags &= ~RVT_R_COMM_EST; | |
1596 | qp->state = new_state; | |
1597 | break; | |
1598 | ||
1599 | case IB_QPS_SQD: | |
1600 | qp->s_draining = qp->s_last != qp->s_cur; | |
1601 | qp->state = new_state; | |
1602 | break; | |
1603 | ||
1604 | case IB_QPS_SQE: | |
1605 | if (qp->ibqp.qp_type == IB_QPT_RC) | |
1606 | goto inval; | |
1607 | qp->state = new_state; | |
1608 | break; | |
1609 | ||
1610 | case IB_QPS_ERR: | |
1611 | lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR); | |
1612 | break; | |
1613 | ||
1614 | default: | |
1615 | qp->state = new_state; | |
1616 | break; | |
1617 | } | |
1618 | ||
1619 | if (attr_mask & IB_QP_PKEY_INDEX) | |
1620 | qp->s_pkey_index = attr->pkey_index; | |
1621 | ||
1622 | if (attr_mask & IB_QP_PORT) | |
1623 | qp->port_num = attr->port_num; | |
1624 | ||
1625 | if (attr_mask & IB_QP_DEST_QPN) | |
1626 | qp->remote_qpn = attr->dest_qp_num; | |
1627 | ||
1628 | if (attr_mask & IB_QP_SQ_PSN) { | |
1629 | qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask; | |
1630 | qp->s_psn = qp->s_next_psn; | |
1631 | qp->s_sending_psn = qp->s_next_psn; | |
1632 | qp->s_last_psn = qp->s_next_psn - 1; | |
1633 | qp->s_sending_hpsn = qp->s_last_psn; | |
1634 | } | |
1635 | ||
1636 | if (attr_mask & IB_QP_RQ_PSN) | |
1637 | qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask; | |
1638 | ||
1639 | if (attr_mask & IB_QP_ACCESS_FLAGS) | |
1640 | qp->qp_access_flags = attr->qp_access_flags; | |
1641 | ||
1642 | if (attr_mask & IB_QP_AV) { | |
d97099fe | 1643 | rdma_replace_ah_attr(&qp->remote_ah_attr, &attr->ah_attr); |
d8966fcd | 1644 | qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr); |
3b0b3fb3 DD |
1645 | qp->srate_mbps = ib_rate_to_mbps(qp->s_srate); |
1646 | } | |
1647 | ||
1648 | if (attr_mask & IB_QP_ALT_PATH) { | |
d97099fe | 1649 | rdma_replace_ah_attr(&qp->alt_ah_attr, &attr->alt_ah_attr); |
3b0b3fb3 DD |
1650 | qp->s_alt_pkey_index = attr->alt_pkey_index; |
1651 | } | |
1652 | ||
1653 | if (attr_mask & IB_QP_PATH_MIG_STATE) { | |
1654 | qp->s_mig_state = attr->path_mig_state; | |
1655 | if (mig) { | |
1656 | qp->remote_ah_attr = qp->alt_ah_attr; | |
d8966fcd | 1657 | qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr); |
3b0b3fb3 | 1658 | qp->s_pkey_index = qp->s_alt_pkey_index; |
3b0b3fb3 DD |
1659 | } |
1660 | } | |
1661 | ||
1662 | if (attr_mask & IB_QP_PATH_MTU) { | |
1663 | qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu); | |
46a80d62 | 1664 | qp->log_pmtu = ilog2(qp->pmtu); |
3b0b3fb3 DD |
1665 | } |
1666 | ||
1667 | if (attr_mask & IB_QP_RETRY_CNT) { | |
1668 | qp->s_retry_cnt = attr->retry_cnt; | |
1669 | qp->s_retry = attr->retry_cnt; | |
1670 | } | |
1671 | ||
1672 | if (attr_mask & IB_QP_RNR_RETRY) { | |
1673 | qp->s_rnr_retry_cnt = attr->rnr_retry; | |
1674 | qp->s_rnr_retry = attr->rnr_retry; | |
1675 | } | |
1676 | ||
1677 | if (attr_mask & IB_QP_MIN_RNR_TIMER) | |
1678 | qp->r_min_rnr_timer = attr->min_rnr_timer; | |
1679 | ||
1680 | if (attr_mask & IB_QP_TIMEOUT) { | |
1681 | qp->timeout = attr->timeout; | |
a25ce427 | 1682 | qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout); |
3b0b3fb3 DD |
1683 | } |
1684 | ||
1685 | if (attr_mask & IB_QP_QKEY) | |
1686 | qp->qkey = attr->qkey; | |
1687 | ||
1688 | if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) | |
1689 | qp->r_max_rd_atomic = attr->max_dest_rd_atomic; | |
1690 | ||
1691 | if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) | |
1692 | qp->s_max_rd_atomic = attr->max_rd_atomic; | |
1693 | ||
e85ec33d IW |
1694 | if (rdi->driver_f.modify_qp) |
1695 | rdi->driver_f.modify_qp(qp, attr, attr_mask, udata); | |
1696 | ||
3b0b3fb3 | 1697 | spin_unlock(&qp->s_lock); |
46a80d62 | 1698 | spin_unlock(&qp->s_hlock); |
3b0b3fb3 DD |
1699 | spin_unlock_irq(&qp->r_lock); |
1700 | ||
1701 | if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) | |
1702 | rvt_insert_qp(rdi, qp); | |
1703 | ||
1704 | if (lastwqe) { | |
1705 | ev.device = qp->ibqp.device; | |
1706 | ev.element.qp = &qp->ibqp; | |
1707 | ev.event = IB_EVENT_QP_LAST_WQE_REACHED; | |
1708 | qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); | |
1709 | } | |
1710 | if (mig) { | |
1711 | ev.device = qp->ibqp.device; | |
1712 | ev.element.qp = &qp->ibqp; | |
1713 | ev.event = IB_EVENT_PATH_MIG; | |
1714 | qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); | |
1715 | } | |
1716 | return 0; | |
1717 | ||
1718 | inval: | |
1719 | spin_unlock(&qp->s_lock); | |
46a80d62 | 1720 | spin_unlock(&qp->s_hlock); |
3b0b3fb3 DD |
1721 | spin_unlock_irq(&qp->r_lock); |
1722 | return -EINVAL; | |
b518d3e6 DD |
1723 | } |
1724 | ||
1725 | /** | |
1726 | * rvt_destroy_qp - destroy a queue pair | |
1727 | * @ibqp: the queue pair to destroy | |
1728 | * | |
b518d3e6 DD |
1729 | * Note that this can be called while the QP is actively sending or |
1730 | * receiving! | |
90793f71 DD |
1731 | * |
1732 | * Return: 0 on success. | |
b518d3e6 | 1733 | */ |
c4367a26 | 1734 | int rvt_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) |
b518d3e6 | 1735 | { |
5a17ad11 DD |
1736 | struct rvt_qp *qp = ibqp_to_rvtqp(ibqp); |
1737 | struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device); | |
b518d3e6 | 1738 | |
5a17ad11 | 1739 | rvt_reset_qp(rdi, qp, ibqp->qp_type); |
5a17ad11 | 1740 | |
f9586abf | 1741 | wait_event(qp->wait, !atomic_read(&qp->refcount)); |
5a17ad11 DD |
1742 | /* qpn is now available for use again */ |
1743 | rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num); | |
1744 | ||
1745 | spin_lock(&rdi->n_qps_lock); | |
1746 | rdi->n_qps_allocated--; | |
bfee5e32 VM |
1747 | if (qp->ibqp.qp_type == IB_QPT_RC) { |
1748 | rdi->n_rc_qps--; | |
1749 | rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL; | |
1750 | } | |
5a17ad11 DD |
1751 | spin_unlock(&rdi->n_qps_lock); |
1752 | ||
1753 | if (qp->ip) | |
1754 | kref_put(&qp->ip->ref, rvt_release_mmap_info); | |
dabac6e4 | 1755 | kvfree(qp->r_rq.kwq); |
5a17ad11 | 1756 | rdi->driver_f.qp_priv_free(rdi, qp); |
8b103e9c | 1757 | kfree(qp->s_ack_queue); |
d97099fe JG |
1758 | rdma_destroy_ah_attr(&qp->remote_ah_attr); |
1759 | rdma_destroy_ah_attr(&qp->alt_ah_attr); | |
d310c4bf | 1760 | free_ud_wq_attr(qp); |
838b6fd2 | 1761 | vfree(qp->s_wq); |
5a17ad11 DD |
1762 | kfree(qp); |
1763 | return 0; | |
b518d3e6 DD |
1764 | } |
1765 | ||
90793f71 DD |
1766 | /** |
1767 | * rvt_query_qp - query an ipbq | |
1768 | * @ibqp: IB qp to query | |
1769 | * @attr: attr struct to fill in | |
1770 | * @attr_mask: attr mask ignored | |
1771 | * @init_attr: struct to fill in | |
1772 | * | |
1773 | * Return: always 0 | |
1774 | */ | |
b518d3e6 DD |
1775 | int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
1776 | int attr_mask, struct ib_qp_init_attr *init_attr) | |
1777 | { | |
74d2d500 HC |
1778 | struct rvt_qp *qp = ibqp_to_rvtqp(ibqp); |
1779 | struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device); | |
1780 | ||
1781 | attr->qp_state = qp->state; | |
1782 | attr->cur_qp_state = attr->qp_state; | |
16570d3d | 1783 | attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu); |
74d2d500 HC |
1784 | attr->path_mig_state = qp->s_mig_state; |
1785 | attr->qkey = qp->qkey; | |
1786 | attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask; | |
1787 | attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask; | |
1788 | attr->dest_qp_num = qp->remote_qpn; | |
1789 | attr->qp_access_flags = qp->qp_access_flags; | |
856cc4c2 MM |
1790 | attr->cap.max_send_wr = qp->s_size - 1 - |
1791 | rdi->dparms.reserved_operations; | |
74d2d500 HC |
1792 | attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1; |
1793 | attr->cap.max_send_sge = qp->s_max_sge; | |
1794 | attr->cap.max_recv_sge = qp->r_rq.max_sge; | |
1795 | attr->cap.max_inline_data = 0; | |
1796 | attr->ah_attr = qp->remote_ah_attr; | |
1797 | attr->alt_ah_attr = qp->alt_ah_attr; | |
1798 | attr->pkey_index = qp->s_pkey_index; | |
1799 | attr->alt_pkey_index = qp->s_alt_pkey_index; | |
1800 | attr->en_sqd_async_notify = 0; | |
1801 | attr->sq_draining = qp->s_draining; | |
1802 | attr->max_rd_atomic = qp->s_max_rd_atomic; | |
1803 | attr->max_dest_rd_atomic = qp->r_max_rd_atomic; | |
1804 | attr->min_rnr_timer = qp->r_min_rnr_timer; | |
1805 | attr->port_num = qp->port_num; | |
1806 | attr->timeout = qp->timeout; | |
1807 | attr->retry_cnt = qp->s_retry_cnt; | |
1808 | attr->rnr_retry = qp->s_rnr_retry_cnt; | |
d8966fcd DC |
1809 | attr->alt_port_num = |
1810 | rdma_ah_get_port_num(&qp->alt_ah_attr); | |
74d2d500 HC |
1811 | attr->alt_timeout = qp->alt_timeout; |
1812 | ||
1813 | init_attr->event_handler = qp->ibqp.event_handler; | |
1814 | init_attr->qp_context = qp->ibqp.qp_context; | |
1815 | init_attr->send_cq = qp->ibqp.send_cq; | |
1816 | init_attr->recv_cq = qp->ibqp.recv_cq; | |
1817 | init_attr->srq = qp->ibqp.srq; | |
1818 | init_attr->cap = attr->cap; | |
1819 | if (qp->s_flags & RVT_S_SIGNAL_REQ_WR) | |
1820 | init_attr->sq_sig_type = IB_SIGNAL_REQ_WR; | |
1821 | else | |
1822 | init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; | |
1823 | init_attr->qp_type = qp->ibqp.qp_type; | |
1824 | init_attr->port_num = qp->port_num; | |
1825 | return 0; | |
b518d3e6 | 1826 | } |
8cf4020b DD |
1827 | |
1828 | /** | |
1829 | * rvt_post_receive - post a receive on a QP | |
1830 | * @ibqp: the QP to post the receive on | |
1831 | * @wr: the WR to post | |
1832 | * @bad_wr: the first bad WR is put here | |
1833 | * | |
1834 | * This may be called from interrupt context. | |
90793f71 DD |
1835 | * |
1836 | * Return: 0 on success otherwise errno | |
8cf4020b | 1837 | */ |
d34ac5cd BVA |
1838 | int rvt_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, |
1839 | const struct ib_recv_wr **bad_wr) | |
8cf4020b | 1840 | { |
120bdafa | 1841 | struct rvt_qp *qp = ibqp_to_rvtqp(ibqp); |
dabac6e4 | 1842 | struct rvt_krwq *wq = qp->r_rq.kwq; |
120bdafa | 1843 | unsigned long flags; |
000a830e AE |
1844 | int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) && |
1845 | !qp->ibqp.srq; | |
8cf4020b | 1846 | |
120bdafa DD |
1847 | /* Check that state is OK to post receive. */ |
1848 | if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) { | |
1849 | *bad_wr = wr; | |
1850 | return -EINVAL; | |
1851 | } | |
1852 | ||
1853 | for (; wr; wr = wr->next) { | |
1854 | struct rvt_rwqe *wqe; | |
1855 | u32 next; | |
1856 | int i; | |
1857 | ||
1858 | if ((unsigned)wr->num_sge > qp->r_rq.max_sge) { | |
1859 | *bad_wr = wr; | |
1860 | return -EINVAL; | |
1861 | } | |
1862 | ||
f592ae3c | 1863 | spin_lock_irqsave(&qp->r_rq.kwq->p_lock, flags); |
120bdafa DD |
1864 | next = wq->head + 1; |
1865 | if (next >= qp->r_rq.size) | |
1866 | next = 0; | |
dabac6e4 | 1867 | if (next == READ_ONCE(wq->tail)) { |
f592ae3c | 1868 | spin_unlock_irqrestore(&qp->r_rq.kwq->p_lock, flags); |
120bdafa DD |
1869 | *bad_wr = wr; |
1870 | return -ENOMEM; | |
1871 | } | |
000a830e AE |
1872 | if (unlikely(qp_err_flush)) { |
1873 | struct ib_wc wc; | |
1874 | ||
1875 | memset(&wc, 0, sizeof(wc)); | |
1876 | wc.qp = &qp->ibqp; | |
1877 | wc.opcode = IB_WC_RECV; | |
1878 | wc.wr_id = wr->wr_id; | |
1879 | wc.status = IB_WC_WR_FLUSH_ERR; | |
1880 | rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1); | |
1881 | } else { | |
1882 | wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head); | |
1883 | wqe->wr_id = wr->wr_id; | |
1884 | wqe->num_sge = wr->num_sge; | |
f10ff380 JG |
1885 | for (i = 0; i < wr->num_sge; i++) { |
1886 | wqe->sg_list[i].addr = wr->sg_list[i].addr; | |
1887 | wqe->sg_list[i].length = wr->sg_list[i].length; | |
1888 | wqe->sg_list[i].lkey = wr->sg_list[i].lkey; | |
1889 | } | |
000a830e AE |
1890 | /* |
1891 | * Make sure queue entry is written | |
1892 | * before the head index. | |
1893 | */ | |
dabac6e4 | 1894 | smp_store_release(&wq->head, next); |
000a830e | 1895 | } |
f592ae3c | 1896 | spin_unlock_irqrestore(&qp->r_rq.kwq->p_lock, flags); |
120bdafa DD |
1897 | } |
1898 | return 0; | |
8cf4020b DD |
1899 | } |
1900 | ||
46a80d62 | 1901 | /** |
afcf8f76 MM |
1902 | * rvt_qp_valid_operation - validate post send wr request |
1903 | * @qp - the qp | |
1904 | * @post-parms - the post send table for the driver | |
1905 | * @wr - the work request | |
46a80d62 | 1906 | * |
afcf8f76 MM |
1907 | * The routine validates the operation based on the |
1908 | * validation table an returns the length of the operation | |
1909 | * which can extend beyond the ib_send_bw. Operation | |
1910 | * dependent flags key atomic operation validation. | |
1911 | * | |
1912 | * There is an exception for UD qps that validates the pd and | |
1913 | * overrides the length to include the additional UD specific | |
1914 | * length. | |
1915 | * | |
1916 | * Returns a negative error or the length of the work request | |
1917 | * for building the swqe. | |
1918 | */ | |
1919 | static inline int rvt_qp_valid_operation( | |
1920 | struct rvt_qp *qp, | |
1921 | const struct rvt_operation_params *post_parms, | |
f696bf6d | 1922 | const struct ib_send_wr *wr) |
afcf8f76 MM |
1923 | { |
1924 | int len; | |
1925 | ||
1926 | if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length) | |
1927 | return -EINVAL; | |
1928 | if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type))) | |
1929 | return -EINVAL; | |
1930 | if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) && | |
1931 | ibpd_to_rvtpd(qp->ibqp.pd)->user) | |
1932 | return -EINVAL; | |
1933 | if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE && | |
1934 | (wr->num_sge == 0 || | |
1935 | wr->sg_list[0].length < sizeof(u64) || | |
1936 | wr->sg_list[0].addr & (sizeof(u64) - 1))) | |
1937 | return -EINVAL; | |
1938 | if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC && | |
1939 | !qp->s_max_rd_atomic) | |
1940 | return -EINVAL; | |
1941 | len = post_parms[wr->opcode].length; | |
1942 | /* UD specific */ | |
1943 | if (qp->ibqp.qp_type != IB_QPT_UC && | |
1944 | qp->ibqp.qp_type != IB_QPT_RC) { | |
1945 | if (qp->ibqp.pd != ud_wr(wr)->ah->pd) | |
1946 | return -EINVAL; | |
1947 | len = sizeof(struct ib_ud_wr); | |
1948 | } | |
1949 | return len; | |
1950 | } | |
1951 | ||
1952 | /** | |
856cc4c2 | 1953 | * rvt_qp_is_avail - determine queue capacity |
4f9a3018 RD |
1954 | * @qp: the qp |
1955 | * @rdi: the rdmavt device | |
1956 | * @reserved_op: is reserved operation | |
46a80d62 MM |
1957 | * |
1958 | * This assumes the s_hlock is held but the s_last | |
1959 | * qp variable is uncontrolled. | |
afcf8f76 | 1960 | * |
856cc4c2 MM |
1961 | * For non reserved operations, the qp->s_avail |
1962 | * may be changed. | |
1963 | * | |
1964 | * The return value is zero or a -ENOMEM. | |
46a80d62 | 1965 | */ |
856cc4c2 MM |
1966 | static inline int rvt_qp_is_avail( |
1967 | struct rvt_qp *qp, | |
1968 | struct rvt_dev_info *rdi, | |
1969 | bool reserved_op) | |
46a80d62 MM |
1970 | { |
1971 | u32 slast; | |
856cc4c2 MM |
1972 | u32 avail; |
1973 | u32 reserved_used; | |
1974 | ||
1975 | /* see rvt_qp_wqe_unreserve() */ | |
1976 | smp_mb__before_atomic(); | |
856cc4c2 MM |
1977 | if (unlikely(reserved_op)) { |
1978 | /* see rvt_qp_wqe_unreserve() */ | |
4a9ceb7d | 1979 | reserved_used = atomic_read(&qp->s_reserved_used); |
856cc4c2 MM |
1980 | if (reserved_used >= rdi->dparms.reserved_operations) |
1981 | return -ENOMEM; | |
1982 | return 0; | |
1983 | } | |
1984 | /* non-reserved operations */ | |
1985 | if (likely(qp->s_avail)) | |
1986 | return 0; | |
4a9ceb7d MM |
1987 | /* See rvt_qp_complete_swqe() */ |
1988 | slast = smp_load_acquire(&qp->s_last); | |
46a80d62 | 1989 | if (qp->s_head >= slast) |
856cc4c2 | 1990 | avail = qp->s_size - (qp->s_head - slast); |
46a80d62 | 1991 | else |
856cc4c2 MM |
1992 | avail = slast - qp->s_head; |
1993 | ||
856cc4c2 MM |
1994 | reserved_used = atomic_read(&qp->s_reserved_used); |
1995 | avail = avail - 1 - | |
1996 | (rdi->dparms.reserved_operations - reserved_used); | |
1997 | /* insure we don't assign a negative s_avail */ | |
1998 | if ((s32)avail <= 0) | |
1999 | return -ENOMEM; | |
2000 | qp->s_avail = avail; | |
2001 | if (WARN_ON(qp->s_avail > | |
2002 | (qp->s_size - 1 - rdi->dparms.reserved_operations))) | |
2003 | rvt_pr_err(rdi, | |
2004 | "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u", | |
2005 | qp->ibqp.qp_num, qp->s_size, qp->s_avail, | |
2006 | qp->s_head, qp->s_tail, qp->s_cur, | |
2007 | qp->s_acked, qp->s_last); | |
2008 | return 0; | |
46a80d62 MM |
2009 | } |
2010 | ||
bfbac097 DD |
2011 | /** |
2012 | * rvt_post_one_wr - post one RC, UC, or UD send work request | |
2013 | * @qp: the QP to post on | |
2014 | * @wr: the work request to send | |
2015 | */ | |
91702b4a | 2016 | static int rvt_post_one_wr(struct rvt_qp *qp, |
f696bf6d | 2017 | const struct ib_send_wr *wr, |
0b79b277 | 2018 | bool *call_send) |
bfbac097 DD |
2019 | { |
2020 | struct rvt_swqe *wqe; | |
2021 | u32 next; | |
2022 | int i; | |
2023 | int j; | |
2024 | int acc; | |
2025 | struct rvt_lkey_table *rkt; | |
2026 | struct rvt_pd *pd; | |
2027 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); | |
46a80d62 | 2028 | u8 log_pmtu; |
3ffea7d8 | 2029 | int ret; |
2821c509 | 2030 | size_t cplen; |
856cc4c2 | 2031 | bool reserved_op; |
d9b13c20 | 2032 | int local_ops_delayed = 0; |
bfbac097 | 2033 | |
afcf8f76 MM |
2034 | BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE)); |
2035 | ||
bfbac097 DD |
2036 | /* IB spec says that num_sge == 0 is OK. */ |
2037 | if (unlikely(wr->num_sge > qp->s_max_sge)) | |
2038 | return -EINVAL; | |
2039 | ||
2821c509 MM |
2040 | ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr); |
2041 | if (ret < 0) | |
2042 | return ret; | |
2043 | cplen = ret; | |
2044 | ||
d9f87239 | 2045 | /* |
d9b13c20 JX |
2046 | * Local operations include fast register and local invalidate. |
2047 | * Fast register needs to be processed immediately because the | |
2048 | * registered lkey may be used by following work requests and the | |
2049 | * lkey needs to be valid at the time those requests are posted. | |
2050 | * Local invalidate can be processed immediately if fencing is | |
2051 | * not required and no previous local invalidate ops are pending. | |
2052 | * Signaled local operations that have been processed immediately | |
2053 | * need to have requests with "completion only" flags set posted | |
2054 | * to the send queue in order to generate completions. | |
d9f87239 | 2055 | */ |
d9b13c20 | 2056 | if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) { |
d9f87239 JX |
2057 | switch (wr->opcode) { |
2058 | case IB_WR_REG_MR: | |
d9b13c20 JX |
2059 | ret = rvt_fast_reg_mr(qp, |
2060 | reg_wr(wr)->mr, | |
2061 | reg_wr(wr)->key, | |
2062 | reg_wr(wr)->access); | |
2063 | if (ret || !(wr->send_flags & IB_SEND_SIGNALED)) | |
2064 | return ret; | |
2065 | break; | |
d9f87239 | 2066 | case IB_WR_LOCAL_INV: |
d9b13c20 JX |
2067 | if ((wr->send_flags & IB_SEND_FENCE) || |
2068 | atomic_read(&qp->local_ops_pending)) { | |
2069 | local_ops_delayed = 1; | |
2070 | } else { | |
2071 | ret = rvt_invalidate_rkey( | |
2072 | qp, wr->ex.invalidate_rkey); | |
2073 | if (ret || !(wr->send_flags & IB_SEND_SIGNALED)) | |
2074 | return ret; | |
2075 | } | |
2076 | break; | |
d9f87239 JX |
2077 | default: |
2078 | return -EINVAL; | |
2079 | } | |
2080 | } | |
2081 | ||
856cc4c2 MM |
2082 | reserved_op = rdi->post_parms[wr->opcode].flags & |
2083 | RVT_OPERATION_USE_RESERVE; | |
46a80d62 | 2084 | /* check for avail */ |
856cc4c2 MM |
2085 | ret = rvt_qp_is_avail(qp, rdi, reserved_op); |
2086 | if (ret) | |
2087 | return ret; | |
bfbac097 DD |
2088 | next = qp->s_head + 1; |
2089 | if (next >= qp->s_size) | |
2090 | next = 0; | |
60c30f57 | 2091 | |
bfbac097 DD |
2092 | rkt = &rdi->lkey_table; |
2093 | pd = ibpd_to_rvtpd(qp->ibqp.pd); | |
2094 | wqe = rvt_get_swqe_ptr(qp, qp->s_head); | |
2095 | ||
2821c509 MM |
2096 | /* cplen has length from above */ |
2097 | memcpy(&wqe->wr, wr, cplen); | |
bfbac097 DD |
2098 | |
2099 | wqe->length = 0; | |
2100 | j = 0; | |
2101 | if (wr->num_sge) { | |
14fe13fc MM |
2102 | struct rvt_sge *last_sge = NULL; |
2103 | ||
bfbac097 DD |
2104 | acc = wr->opcode >= IB_WR_RDMA_READ ? |
2105 | IB_ACCESS_LOCAL_WRITE : 0; | |
2106 | for (i = 0; i < wr->num_sge; i++) { | |
2107 | u32 length = wr->sg_list[i].length; | |
bfbac097 DD |
2108 | |
2109 | if (length == 0) | |
2110 | continue; | |
3ffea7d8 MM |
2111 | ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge, |
2112 | &wr->sg_list[i], acc); | |
2113 | if (unlikely(ret < 0)) | |
2114 | goto bail_inval_free; | |
bfbac097 | 2115 | wqe->length += length; |
3ffea7d8 | 2116 | if (ret) |
14fe13fc | 2117 | last_sge = &wqe->sg_list[j]; |
3ffea7d8 | 2118 | j += ret; |
bfbac097 DD |
2119 | } |
2120 | wqe->wr.num_sge = j; | |
2121 | } | |
46a80d62 | 2122 | |
d205a06a KW |
2123 | /* |
2124 | * Calculate and set SWQE PSN values prior to handing it off | |
2125 | * to the driver's check routine. This give the driver the | |
2126 | * opportunity to adjust PSN values based on internal checks. | |
2127 | */ | |
46a80d62 | 2128 | log_pmtu = qp->log_pmtu; |
52cdbcc2 | 2129 | if (qp->allowed_ops == IB_OPCODE_UD) { |
2b0ad2da | 2130 | struct rvt_ah *ah = rvt_get_swqe_ah(wqe); |
46a80d62 MM |
2131 | |
2132 | log_pmtu = ah->log_pmtu; | |
d310c4bf | 2133 | rdma_copy_ah_attr(wqe->ud_wr.attr, &ah->attr); |
bfbac097 | 2134 | } |
46a80d62 | 2135 | |
d9f87239 | 2136 | if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) { |
d9b13c20 JX |
2137 | if (local_ops_delayed) |
2138 | atomic_inc(&qp->local_ops_pending); | |
2139 | else | |
2140 | wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY; | |
d9f87239 JX |
2141 | wqe->ssn = 0; |
2142 | wqe->psn = 0; | |
2143 | wqe->lpsn = 0; | |
2144 | } else { | |
2145 | wqe->ssn = qp->s_ssn++; | |
2146 | wqe->psn = qp->s_next_psn; | |
2147 | wqe->lpsn = wqe->psn + | |
2148 | (wqe->length ? | |
2149 | ((wqe->length - 1) >> log_pmtu) : | |
2150 | 0); | |
d9f87239 | 2151 | } |
d205a06a KW |
2152 | |
2153 | /* general part of wqe valid - allow for driver checks */ | |
2154 | if (rdi->driver_f.setup_wqe) { | |
2155 | ret = rdi->driver_f.setup_wqe(qp, wqe, call_send); | |
2156 | if (ret < 0) | |
2157 | goto bail_inval_free_ref; | |
2158 | } | |
2159 | ||
2160 | if (!(rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) | |
2161 | qp->s_next_psn = wqe->lpsn + 1; | |
2162 | ||
44dcfa4b MM |
2163 | if (unlikely(reserved_op)) { |
2164 | wqe->wr.send_flags |= RVT_SEND_RESERVE_USED; | |
856cc4c2 | 2165 | rvt_qp_wqe_reserve(qp, wqe); |
44dcfa4b MM |
2166 | } else { |
2167 | wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED; | |
856cc4c2 | 2168 | qp->s_avail--; |
44dcfa4b | 2169 | } |
14fe13fc | 2170 | trace_rvt_post_one_wr(qp, wqe, wr->num_sge); |
46a80d62 | 2171 | smp_wmb(); /* see request builders */ |
bfbac097 DD |
2172 | qp->s_head = next; |
2173 | ||
2174 | return 0; | |
2175 | ||
d205a06a | 2176 | bail_inval_free_ref: |
52cdbcc2 | 2177 | if (qp->allowed_ops == IB_OPCODE_UD) |
d310c4bf | 2178 | rdma_destroy_ah_attr(wqe->ud_wr.attr); |
bfbac097 DD |
2179 | bail_inval_free: |
2180 | /* release mr holds */ | |
2181 | while (j) { | |
2182 | struct rvt_sge *sge = &wqe->sg_list[--j]; | |
2183 | ||
2184 | rvt_put_mr(sge->mr); | |
2185 | } | |
46a80d62 | 2186 | return ret; |
bfbac097 DD |
2187 | } |
2188 | ||
8cf4020b DD |
2189 | /** |
2190 | * rvt_post_send - post a send on a QP | |
2191 | * @ibqp: the QP to post the send on | |
2192 | * @wr: the list of work requests to post | |
2193 | * @bad_wr: the first bad WR is put here | |
2194 | * | |
2195 | * This may be called from interrupt context. | |
90793f71 DD |
2196 | * |
2197 | * Return: 0 on success else errno | |
8cf4020b | 2198 | */ |
d34ac5cd BVA |
2199 | int rvt_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, |
2200 | const struct ib_send_wr **bad_wr) | |
8cf4020b | 2201 | { |
bfbac097 DD |
2202 | struct rvt_qp *qp = ibqp_to_rvtqp(ibqp); |
2203 | struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device); | |
2204 | unsigned long flags = 0; | |
0b79b277 | 2205 | bool call_send; |
bfbac097 DD |
2206 | unsigned nreq = 0; |
2207 | int err = 0; | |
2208 | ||
46a80d62 | 2209 | spin_lock_irqsave(&qp->s_hlock, flags); |
bfbac097 | 2210 | |
8cf4020b | 2211 | /* |
bfbac097 DD |
2212 | * Ensure QP state is such that we can send. If not bail out early, |
2213 | * there is no need to do this every time we post a send. | |
8cf4020b | 2214 | */ |
bfbac097 | 2215 | if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) { |
46a80d62 | 2216 | spin_unlock_irqrestore(&qp->s_hlock, flags); |
bfbac097 DD |
2217 | return -EINVAL; |
2218 | } | |
8cf4020b | 2219 | |
bfbac097 DD |
2220 | /* |
2221 | * If the send queue is empty, and we only have a single WR then just go | |
2222 | * ahead and kick the send engine into gear. Otherwise we will always | |
2223 | * just schedule the send to happen later. | |
2224 | */ | |
6aa7de05 | 2225 | call_send = qp->s_head == READ_ONCE(qp->s_last) && !wr->next; |
bfbac097 DD |
2226 | |
2227 | for (; wr; wr = wr->next) { | |
91702b4a | 2228 | err = rvt_post_one_wr(qp, wr, &call_send); |
bfbac097 DD |
2229 | if (unlikely(err)) { |
2230 | *bad_wr = wr; | |
2231 | goto bail; | |
2232 | } | |
2233 | nreq++; | |
2234 | } | |
2235 | bail: | |
46a80d62 MM |
2236 | spin_unlock_irqrestore(&qp->s_hlock, flags); |
2237 | if (nreq) { | |
0b79b277 MR |
2238 | /* |
2239 | * Only call do_send if there is exactly one packet, and the | |
2240 | * driver said it was ok. | |
2241 | */ | |
2242 | if (nreq == 1 && call_send) | |
46a80d62 | 2243 | rdi->driver_f.do_send(qp); |
e6d2e017 JJ |
2244 | else |
2245 | rdi->driver_f.schedule_send_no_lock(qp); | |
46a80d62 | 2246 | } |
bfbac097 | 2247 | return err; |
8cf4020b DD |
2248 | } |
2249 | ||
2250 | /** | |
2251 | * rvt_post_srq_receive - post a receive on a shared receive queue | |
2252 | * @ibsrq: the SRQ to post the receive on | |
2253 | * @wr: the list of work requests to post | |
2254 | * @bad_wr: A pointer to the first WR to cause a problem is put here | |
2255 | * | |
2256 | * This may be called from interrupt context. | |
90793f71 DD |
2257 | * |
2258 | * Return: 0 on success else errno | |
8cf4020b | 2259 | */ |
d34ac5cd BVA |
2260 | int rvt_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr, |
2261 | const struct ib_recv_wr **bad_wr) | |
8cf4020b | 2262 | { |
b8f881b9 | 2263 | struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq); |
dabac6e4 | 2264 | struct rvt_krwq *wq; |
b8f881b9 JJ |
2265 | unsigned long flags; |
2266 | ||
2267 | for (; wr; wr = wr->next) { | |
2268 | struct rvt_rwqe *wqe; | |
2269 | u32 next; | |
2270 | int i; | |
2271 | ||
2272 | if ((unsigned)wr->num_sge > srq->rq.max_sge) { | |
2273 | *bad_wr = wr; | |
2274 | return -EINVAL; | |
2275 | } | |
2276 | ||
f592ae3c | 2277 | spin_lock_irqsave(&srq->rq.kwq->p_lock, flags); |
dabac6e4 | 2278 | wq = srq->rq.kwq; |
b8f881b9 JJ |
2279 | next = wq->head + 1; |
2280 | if (next >= srq->rq.size) | |
2281 | next = 0; | |
dabac6e4 | 2282 | if (next == READ_ONCE(wq->tail)) { |
f592ae3c | 2283 | spin_unlock_irqrestore(&srq->rq.kwq->p_lock, flags); |
b8f881b9 JJ |
2284 | *bad_wr = wr; |
2285 | return -ENOMEM; | |
2286 | } | |
2287 | ||
2288 | wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head); | |
2289 | wqe->wr_id = wr->wr_id; | |
2290 | wqe->num_sge = wr->num_sge; | |
f10ff380 JG |
2291 | for (i = 0; i < wr->num_sge; i++) { |
2292 | wqe->sg_list[i].addr = wr->sg_list[i].addr; | |
2293 | wqe->sg_list[i].length = wr->sg_list[i].length; | |
2294 | wqe->sg_list[i].lkey = wr->sg_list[i].lkey; | |
2295 | } | |
b8f881b9 | 2296 | /* Make sure queue entry is written before the head index. */ |
dabac6e4 | 2297 | smp_store_release(&wq->head, next); |
f592ae3c | 2298 | spin_unlock_irqrestore(&srq->rq.kwq->p_lock, flags); |
b8f881b9 JJ |
2299 | } |
2300 | return 0; | |
8cf4020b | 2301 | } |
beb5a042 | 2302 | |
f10ff380 JG |
2303 | /* |
2304 | * rvt used the internal kernel struct as part of its ABI, for now make sure | |
2305 | * the kernel struct does not change layout. FIXME: rvt should never cast the | |
2306 | * user struct to a kernel struct. | |
2307 | */ | |
2308 | static struct ib_sge *rvt_cast_sge(struct rvt_wqe_sge *sge) | |
2309 | { | |
2310 | BUILD_BUG_ON(offsetof(struct ib_sge, addr) != | |
2311 | offsetof(struct rvt_wqe_sge, addr)); | |
2312 | BUILD_BUG_ON(offsetof(struct ib_sge, length) != | |
2313 | offsetof(struct rvt_wqe_sge, length)); | |
2314 | BUILD_BUG_ON(offsetof(struct ib_sge, lkey) != | |
2315 | offsetof(struct rvt_wqe_sge, lkey)); | |
2316 | return (struct ib_sge *)sge; | |
2317 | } | |
2318 | ||
832369fa BW |
2319 | /* |
2320 | * Validate a RWQE and fill in the SGE state. | |
2321 | * Return 1 if OK. | |
2322 | */ | |
2323 | static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe) | |
2324 | { | |
2325 | int i, j, ret; | |
2326 | struct ib_wc wc; | |
2327 | struct rvt_lkey_table *rkt; | |
2328 | struct rvt_pd *pd; | |
2329 | struct rvt_sge_state *ss; | |
2330 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); | |
2331 | ||
2332 | rkt = &rdi->lkey_table; | |
2333 | pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd); | |
2334 | ss = &qp->r_sge; | |
2335 | ss->sg_list = qp->r_sg_list; | |
2336 | qp->r_len = 0; | |
2337 | for (i = j = 0; i < wqe->num_sge; i++) { | |
2338 | if (wqe->sg_list[i].length == 0) | |
2339 | continue; | |
2340 | /* Check LKEY */ | |
2341 | ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge, | |
f10ff380 | 2342 | NULL, rvt_cast_sge(&wqe->sg_list[i]), |
832369fa BW |
2343 | IB_ACCESS_LOCAL_WRITE); |
2344 | if (unlikely(ret <= 0)) | |
2345 | goto bad_lkey; | |
2346 | qp->r_len += wqe->sg_list[i].length; | |
2347 | j++; | |
2348 | } | |
2349 | ss->num_sge = j; | |
2350 | ss->total_len = qp->r_len; | |
2351 | return 1; | |
2352 | ||
2353 | bad_lkey: | |
2354 | while (j) { | |
2355 | struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge; | |
2356 | ||
2357 | rvt_put_mr(sge->mr); | |
2358 | } | |
2359 | ss->num_sge = 0; | |
2360 | memset(&wc, 0, sizeof(wc)); | |
2361 | wc.wr_id = wqe->wr_id; | |
2362 | wc.status = IB_WC_LOC_PROT_ERR; | |
2363 | wc.opcode = IB_WC_RECV; | |
2364 | wc.qp = &qp->ibqp; | |
2365 | /* Signal solicited completion event. */ | |
2366 | rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1); | |
2367 | return 0; | |
2368 | } | |
2369 | ||
dabac6e4 KA |
2370 | /** |
2371 | * get_rvt_head - get head indices of the circular buffer | |
2372 | * @rq: data structure for request queue entry | |
2373 | * @ip: the QP | |
2374 | * | |
2375 | * Return - head index value | |
2376 | */ | |
2377 | static inline u32 get_rvt_head(struct rvt_rq *rq, void *ip) | |
2378 | { | |
2379 | u32 head; | |
2380 | ||
2381 | if (ip) | |
2382 | head = RDMA_READ_UAPI_ATOMIC(rq->wq->head); | |
2383 | else | |
2384 | head = rq->kwq->head; | |
2385 | ||
2386 | return head; | |
2387 | } | |
2388 | ||
832369fa BW |
2389 | /** |
2390 | * rvt_get_rwqe - copy the next RWQE into the QP's RWQE | |
2391 | * @qp: the QP | |
2392 | * @wr_id_only: update qp->r_wr_id only, not qp->r_sge | |
2393 | * | |
2394 | * Return -1 if there is a local error, 0 if no RWQE is available, | |
2395 | * otherwise return 1. | |
2396 | * | |
2397 | * Can be called from interrupt level. | |
2398 | */ | |
2399 | int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only) | |
2400 | { | |
2401 | unsigned long flags; | |
2402 | struct rvt_rq *rq; | |
f592ae3c | 2403 | struct rvt_krwq *kwq = NULL; |
832369fa BW |
2404 | struct rvt_rwq *wq; |
2405 | struct rvt_srq *srq; | |
2406 | struct rvt_rwqe *wqe; | |
2407 | void (*handler)(struct ib_event *, void *); | |
2408 | u32 tail; | |
dabac6e4 | 2409 | u32 head; |
832369fa | 2410 | int ret; |
dabac6e4 | 2411 | void *ip = NULL; |
832369fa BW |
2412 | |
2413 | if (qp->ibqp.srq) { | |
2414 | srq = ibsrq_to_rvtsrq(qp->ibqp.srq); | |
2415 | handler = srq->ibsrq.event_handler; | |
2416 | rq = &srq->rq; | |
dabac6e4 | 2417 | ip = srq->ip; |
832369fa BW |
2418 | } else { |
2419 | srq = NULL; | |
2420 | handler = NULL; | |
2421 | rq = &qp->r_rq; | |
dabac6e4 | 2422 | ip = qp->ip; |
832369fa BW |
2423 | } |
2424 | ||
f592ae3c | 2425 | spin_lock_irqsave(&rq->kwq->c_lock, flags); |
832369fa BW |
2426 | if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) { |
2427 | ret = 0; | |
2428 | goto unlock; | |
2429 | } | |
f592ae3c | 2430 | kwq = rq->kwq; |
dabac6e4 KA |
2431 | if (ip) { |
2432 | wq = rq->wq; | |
2433 | tail = RDMA_READ_UAPI_ATOMIC(wq->tail); | |
2434 | } else { | |
dabac6e4 KA |
2435 | tail = kwq->tail; |
2436 | } | |
832369fa | 2437 | |
832369fa BW |
2438 | /* Validate tail before using it since it is user writable. */ |
2439 | if (tail >= rq->size) | |
2440 | tail = 0; | |
dabac6e4 | 2441 | |
f592ae3c KA |
2442 | if (kwq->count < RVT_RWQ_COUNT_THRESHOLD) { |
2443 | head = get_rvt_head(rq, ip); | |
54a485e9 | 2444 | kwq->count = rvt_get_rq_count(rq, head, tail); |
f592ae3c KA |
2445 | } |
2446 | if (unlikely(kwq->count == 0)) { | |
832369fa BW |
2447 | ret = 0; |
2448 | goto unlock; | |
2449 | } | |
dabac6e4 | 2450 | /* Make sure entry is read after the count is read. */ |
832369fa BW |
2451 | smp_rmb(); |
2452 | wqe = rvt_get_rwqe_ptr(rq, tail); | |
2453 | /* | |
2454 | * Even though we update the tail index in memory, the verbs | |
2455 | * consumer is not supposed to post more entries until a | |
2456 | * completion is generated. | |
2457 | */ | |
2458 | if (++tail >= rq->size) | |
2459 | tail = 0; | |
dabac6e4 KA |
2460 | if (ip) |
2461 | RDMA_WRITE_UAPI_ATOMIC(wq->tail, tail); | |
2462 | else | |
2463 | kwq->tail = tail; | |
832369fa BW |
2464 | if (!wr_id_only && !init_sge(qp, wqe)) { |
2465 | ret = -1; | |
2466 | goto unlock; | |
2467 | } | |
2468 | qp->r_wr_id = wqe->wr_id; | |
2469 | ||
f592ae3c | 2470 | kwq->count--; |
832369fa BW |
2471 | ret = 1; |
2472 | set_bit(RVT_R_WRID_VALID, &qp->r_aflags); | |
2473 | if (handler) { | |
832369fa BW |
2474 | /* |
2475 | * Validate head pointer value and compute | |
2476 | * the number of remaining WQEs. | |
2477 | */ | |
f592ae3c | 2478 | if (kwq->count < srq->limit) { |
54a485e9 MM |
2479 | kwq->count = |
2480 | rvt_get_rq_count(rq, | |
2481 | get_rvt_head(rq, ip), tail); | |
f592ae3c KA |
2482 | if (kwq->count < srq->limit) { |
2483 | struct ib_event ev; | |
2484 | ||
2485 | srq->limit = 0; | |
2486 | spin_unlock_irqrestore(&rq->kwq->c_lock, flags); | |
2487 | ev.device = qp->ibqp.device; | |
2488 | ev.element.srq = qp->ibqp.srq; | |
2489 | ev.event = IB_EVENT_SRQ_LIMIT_REACHED; | |
2490 | handler(&ev, srq->ibsrq.srq_context); | |
2491 | goto bail; | |
2492 | } | |
832369fa BW |
2493 | } |
2494 | } | |
2495 | unlock: | |
f592ae3c | 2496 | spin_unlock_irqrestore(&rq->kwq->c_lock, flags); |
832369fa BW |
2497 | bail: |
2498 | return ret; | |
2499 | } | |
2500 | EXPORT_SYMBOL(rvt_get_rwqe); | |
2501 | ||
beb5a042 BW |
2502 | /** |
2503 | * qp_comm_est - handle trap with QP established | |
2504 | * @qp: the QP | |
2505 | */ | |
2506 | void rvt_comm_est(struct rvt_qp *qp) | |
2507 | { | |
2508 | qp->r_flags |= RVT_R_COMM_EST; | |
2509 | if (qp->ibqp.event_handler) { | |
2510 | struct ib_event ev; | |
2511 | ||
2512 | ev.device = qp->ibqp.device; | |
2513 | ev.element.qp = &qp->ibqp; | |
2514 | ev.event = IB_EVENT_COMM_EST; | |
2515 | qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); | |
2516 | } | |
2517 | } | |
2518 | EXPORT_SYMBOL(rvt_comm_est); | |
2519 | ||
2520 | void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err) | |
2521 | { | |
2522 | unsigned long flags; | |
2523 | int lastwqe; | |
2524 | ||
2525 | spin_lock_irqsave(&qp->s_lock, flags); | |
2526 | lastwqe = rvt_error_qp(qp, err); | |
2527 | spin_unlock_irqrestore(&qp->s_lock, flags); | |
2528 | ||
2529 | if (lastwqe) { | |
2530 | struct ib_event ev; | |
2531 | ||
2532 | ev.device = qp->ibqp.device; | |
2533 | ev.element.qp = &qp->ibqp; | |
2534 | ev.event = IB_EVENT_QP_LAST_WQE_REACHED; | |
2535 | qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); | |
2536 | } | |
2537 | } | |
2538 | EXPORT_SYMBOL(rvt_rc_error); | |
11a10d4b | 2539 | |
881fccb8 DH |
2540 | /* |
2541 | * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table | |
2542 | * @index - the index | |
2543 | * return usec from an index into ib_rvt_rnr_table | |
2544 | */ | |
2545 | unsigned long rvt_rnr_tbl_to_usec(u32 index) | |
2546 | { | |
832666c1 | 2547 | return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)]; |
881fccb8 DH |
2548 | } |
2549 | EXPORT_SYMBOL(rvt_rnr_tbl_to_usec); | |
2550 | ||
11a10d4b VSD |
2551 | static inline unsigned long rvt_aeth_to_usec(u32 aeth) |
2552 | { | |
832666c1 DH |
2553 | return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) & |
2554 | IB_AETH_CREDIT_MASK]; | |
11a10d4b VSD |
2555 | } |
2556 | ||
2557 | /* | |
039cd3da | 2558 | * rvt_add_retry_timer_ext - add/start a retry timer |
11a10d4b | 2559 | * @qp - the QP |
039cd3da | 2560 | * @shift - timeout shift to wait for multiple packets |
11a10d4b VSD |
2561 | * add a retry timer on the QP |
2562 | */ | |
039cd3da | 2563 | void rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift) |
11a10d4b VSD |
2564 | { |
2565 | struct ib_qp *ibqp = &qp->ibqp; | |
2566 | struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device); | |
2567 | ||
2568 | lockdep_assert_held(&qp->s_lock); | |
2569 | qp->s_flags |= RVT_S_TIMER; | |
2570 | /* 4.096 usec. * (1 << qp->timeout) */ | |
039cd3da KW |
2571 | qp->s_timer.expires = jiffies + rdi->busy_jiffies + |
2572 | (qp->timeout_jiffies << shift); | |
11a10d4b VSD |
2573 | add_timer(&qp->s_timer); |
2574 | } | |
039cd3da | 2575 | EXPORT_SYMBOL(rvt_add_retry_timer_ext); |
11a10d4b VSD |
2576 | |
2577 | /** | |
7c21072d | 2578 | * rvt_add_rnr_timer - add/start an rnr timer on the QP |
2579 | * @qp: the QP | |
2580 | * @aeth: aeth of RNR timeout, simulated aeth for loopback | |
11a10d4b VSD |
2581 | */ |
2582 | void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth) | |
2583 | { | |
2584 | u32 to; | |
2585 | ||
2586 | lockdep_assert_held(&qp->s_lock); | |
2587 | qp->s_flags |= RVT_S_WAIT_RNR; | |
2588 | to = rvt_aeth_to_usec(aeth); | |
57f6b663 | 2589 | trace_rvt_rnrnak_add(qp, to); |
11a10d4b | 2590 | hrtimer_start(&qp->s_rnr_timer, |
3ce459cd | 2591 | ns_to_ktime(1000 * to), HRTIMER_MODE_REL_PINNED); |
11a10d4b VSD |
2592 | } |
2593 | EXPORT_SYMBOL(rvt_add_rnr_timer); | |
2594 | ||
2595 | /** | |
2596 | * rvt_stop_rc_timers - stop all timers | |
7c21072d | 2597 | * @qp: the QP |
11a10d4b VSD |
2598 | * stop any pending timers |
2599 | */ | |
2600 | void rvt_stop_rc_timers(struct rvt_qp *qp) | |
2601 | { | |
2602 | lockdep_assert_held(&qp->s_lock); | |
2603 | /* Remove QP from all timers */ | |
2604 | if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) { | |
2605 | qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR); | |
2606 | del_timer(&qp->s_timer); | |
2607 | hrtimer_try_to_cancel(&qp->s_rnr_timer); | |
2608 | } | |
2609 | } | |
2610 | EXPORT_SYMBOL(rvt_stop_rc_timers); | |
2611 | ||
2612 | /** | |
2613 | * rvt_stop_rnr_timer - stop an rnr timer | |
2614 | * @qp - the QP | |
2615 | * | |
2616 | * stop an rnr timer and return if the timer | |
2617 | * had been pending. | |
2618 | */ | |
57f6b663 | 2619 | static void rvt_stop_rnr_timer(struct rvt_qp *qp) |
11a10d4b | 2620 | { |
11a10d4b VSD |
2621 | lockdep_assert_held(&qp->s_lock); |
2622 | /* Remove QP from rnr timer */ | |
2623 | if (qp->s_flags & RVT_S_WAIT_RNR) { | |
2624 | qp->s_flags &= ~RVT_S_WAIT_RNR; | |
57f6b663 | 2625 | trace_rvt_rnrnak_stop(qp, 0); |
11a10d4b | 2626 | } |
11a10d4b VSD |
2627 | } |
2628 | ||
2629 | /** | |
2630 | * rvt_del_timers_sync - wait for any timeout routines to exit | |
7c21072d | 2631 | * @qp: the QP |
11a10d4b VSD |
2632 | */ |
2633 | void rvt_del_timers_sync(struct rvt_qp *qp) | |
2634 | { | |
2635 | del_timer_sync(&qp->s_timer); | |
2636 | hrtimer_cancel(&qp->s_rnr_timer); | |
2637 | } | |
2638 | EXPORT_SYMBOL(rvt_del_timers_sync); | |
2639 | ||
7c21072d | 2640 | /* |
11a10d4b VSD |
2641 | * This is called from s_timer for missing responses. |
2642 | */ | |
a2930e5c | 2643 | static void rvt_rc_timeout(struct timer_list *t) |
11a10d4b | 2644 | { |
a2930e5c | 2645 | struct rvt_qp *qp = from_timer(qp, t, s_timer); |
11a10d4b VSD |
2646 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); |
2647 | unsigned long flags; | |
2648 | ||
2649 | spin_lock_irqsave(&qp->r_lock, flags); | |
2650 | spin_lock(&qp->s_lock); | |
2651 | if (qp->s_flags & RVT_S_TIMER) { | |
5f14e4e6 SS |
2652 | struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1]; |
2653 | ||
11a10d4b | 2654 | qp->s_flags &= ~RVT_S_TIMER; |
5f14e4e6 | 2655 | rvp->n_rc_timeouts++; |
11a10d4b | 2656 | del_timer(&qp->s_timer); |
5f14e4e6 | 2657 | trace_rvt_rc_timeout(qp, qp->s_last_psn + 1); |
11a10d4b VSD |
2658 | if (rdi->driver_f.notify_restart_rc) |
2659 | rdi->driver_f.notify_restart_rc(qp, | |
2660 | qp->s_last_psn + 1, | |
2661 | 1); | |
2662 | rdi->driver_f.schedule_send(qp); | |
2663 | } | |
2664 | spin_unlock(&qp->s_lock); | |
2665 | spin_unlock_irqrestore(&qp->r_lock, flags); | |
2666 | } | |
2667 | ||
2668 | /* | |
2669 | * This is called from s_timer for RNR timeouts. | |
2670 | */ | |
2671 | enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t) | |
2672 | { | |
2673 | struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer); | |
2674 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); | |
2675 | unsigned long flags; | |
2676 | ||
2677 | spin_lock_irqsave(&qp->s_lock, flags); | |
2678 | rvt_stop_rnr_timer(qp); | |
57f6b663 | 2679 | trace_rvt_rnrnak_timeout(qp, 0); |
11a10d4b VSD |
2680 | rdi->driver_f.schedule_send(qp); |
2681 | spin_unlock_irqrestore(&qp->s_lock, flags); | |
2682 | return HRTIMER_NORESTART; | |
2683 | } | |
2684 | EXPORT_SYMBOL(rvt_rc_rnr_retry); | |
4734b4f4 MM |
2685 | |
2686 | /** | |
2687 | * rvt_qp_iter_init - initial for QP iteration | |
4f9a3018 RD |
2688 | * @rdi: rvt devinfo |
2689 | * @v: u64 value | |
7c21072d | 2690 | * @cb: user-defined callback |
4734b4f4 MM |
2691 | * |
2692 | * This returns an iterator suitable for iterating QPs | |
2693 | * in the system. | |
2694 | * | |
7c21072d | 2695 | * The @cb is a user-defined callback and @v is a 64-bit |
2696 | * value passed to and relevant for processing in the | |
4734b4f4 MM |
2697 | * @cb. An example use case would be to alter QP processing |
2698 | * based on criteria not part of the rvt_qp. | |
2699 | * | |
2700 | * Use cases that require memory allocation to succeed | |
2701 | * must preallocate appropriately. | |
2702 | * | |
2703 | * Return: a pointer to an rvt_qp_iter or NULL | |
2704 | */ | |
2705 | struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi, | |
2706 | u64 v, | |
2707 | void (*cb)(struct rvt_qp *qp, u64 v)) | |
2708 | { | |
2709 | struct rvt_qp_iter *i; | |
2710 | ||
2711 | i = kzalloc(sizeof(*i), GFP_KERNEL); | |
2712 | if (!i) | |
2713 | return NULL; | |
2714 | ||
2715 | i->rdi = rdi; | |
2716 | /* number of special QPs (SMI/GSI) for device */ | |
2717 | i->specials = rdi->ibdev.phys_port_cnt * 2; | |
2718 | i->v = v; | |
2719 | i->cb = cb; | |
2720 | ||
2721 | return i; | |
2722 | } | |
2723 | EXPORT_SYMBOL(rvt_qp_iter_init); | |
2724 | ||
2725 | /** | |
2726 | * rvt_qp_iter_next - return the next QP in iter | |
7c21072d | 2727 | * @iter: the iterator |
4734b4f4 MM |
2728 | * |
2729 | * Fine grained QP iterator suitable for use | |
2730 | * with debugfs seq_file mechanisms. | |
2731 | * | |
2732 | * Updates iter->qp with the current QP when the return | |
2733 | * value is 0. | |
2734 | * | |
2735 | * Return: 0 - iter->qp is valid 1 - no more QPs | |
2736 | */ | |
2737 | int rvt_qp_iter_next(struct rvt_qp_iter *iter) | |
2738 | __must_hold(RCU) | |
2739 | { | |
2740 | int n = iter->n; | |
2741 | int ret = 1; | |
2742 | struct rvt_qp *pqp = iter->qp; | |
2743 | struct rvt_qp *qp; | |
2744 | struct rvt_dev_info *rdi = iter->rdi; | |
2745 | ||
2746 | /* | |
2747 | * The approach is to consider the special qps | |
2748 | * as additional table entries before the | |
2749 | * real hash table. Since the qp code sets | |
2750 | * the qp->next hash link to NULL, this works just fine. | |
2751 | * | |
2752 | * iter->specials is 2 * # ports | |
2753 | * | |
2754 | * n = 0..iter->specials is the special qp indices | |
2755 | * | |
2756 | * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are | |
2757 | * the potential hash bucket entries | |
2758 | * | |
2759 | */ | |
2760 | for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) { | |
2761 | if (pqp) { | |
2762 | qp = rcu_dereference(pqp->next); | |
2763 | } else { | |
2764 | if (n < iter->specials) { | |
2765 | struct rvt_ibport *rvp; | |
2766 | int pidx; | |
2767 | ||
2768 | pidx = n % rdi->ibdev.phys_port_cnt; | |
2769 | rvp = rdi->ports[pidx]; | |
2770 | qp = rcu_dereference(rvp->qp[n & 1]); | |
2771 | } else { | |
2772 | qp = rcu_dereference( | |
2773 | rdi->qp_dev->qp_table[ | |
2774 | (n - iter->specials)]); | |
2775 | } | |
2776 | } | |
2777 | pqp = qp; | |
2778 | if (qp) { | |
2779 | iter->qp = qp; | |
2780 | iter->n = n; | |
2781 | return 0; | |
2782 | } | |
2783 | } | |
2784 | return ret; | |
2785 | } | |
2786 | EXPORT_SYMBOL(rvt_qp_iter_next); | |
2787 | ||
2788 | /** | |
2789 | * rvt_qp_iter - iterate all QPs | |
7c21072d | 2790 | * @rdi: rvt devinfo |
2791 | * @v: a 64-bit value | |
2792 | * @cb: a callback | |
4734b4f4 MM |
2793 | * |
2794 | * This provides a way for iterating all QPs. | |
2795 | * | |
7c21072d | 2796 | * The @cb is a user-defined callback and @v is a 64-bit |
2797 | * value passed to and relevant for processing in the | |
4734b4f4 MM |
2798 | * cb. An example use case would be to alter QP processing |
2799 | * based on criteria not part of the rvt_qp. | |
2800 | * | |
2801 | * The code has an internal iterator to simplify | |
2802 | * non seq_file use cases. | |
2803 | */ | |
2804 | void rvt_qp_iter(struct rvt_dev_info *rdi, | |
2805 | u64 v, | |
2806 | void (*cb)(struct rvt_qp *qp, u64 v)) | |
2807 | { | |
2808 | int ret; | |
2809 | struct rvt_qp_iter i = { | |
2810 | .rdi = rdi, | |
2811 | .specials = rdi->ibdev.phys_port_cnt * 2, | |
2812 | .v = v, | |
2813 | .cb = cb | |
2814 | }; | |
2815 | ||
2816 | rcu_read_lock(); | |
2817 | do { | |
2818 | ret = rvt_qp_iter_next(&i); | |
2819 | if (!ret) { | |
2820 | rvt_get_qp(i.qp); | |
2821 | rcu_read_unlock(); | |
2822 | i.cb(i.qp, i.v); | |
2823 | rcu_read_lock(); | |
2824 | rvt_put_qp(i.qp); | |
2825 | } | |
2826 | } while (!ret); | |
2827 | rcu_read_unlock(); | |
2828 | } | |
2829 | EXPORT_SYMBOL(rvt_qp_iter); | |
019f118b | 2830 | |
116aa033 VSD |
2831 | /* |
2832 | * This should be called with s_lock held. | |
2833 | */ | |
2834 | void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe, | |
2835 | enum ib_wc_status status) | |
2836 | { | |
2837 | u32 old_last, last; | |
4a9ceb7d | 2838 | struct rvt_dev_info *rdi; |
116aa033 VSD |
2839 | |
2840 | if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND)) | |
2841 | return; | |
4a9ceb7d | 2842 | rdi = ib_to_rvt(qp->ibqp.device); |
116aa033 | 2843 | |
4a9ceb7d MM |
2844 | old_last = qp->s_last; |
2845 | trace_rvt_qp_send_completion(qp, wqe, old_last); | |
2846 | last = rvt_qp_complete_swqe(qp, wqe, rdi->wc_opcode[wqe->wr.opcode], | |
2847 | status); | |
116aa033 VSD |
2848 | if (qp->s_acked == old_last) |
2849 | qp->s_acked = last; | |
2850 | if (qp->s_cur == old_last) | |
2851 | qp->s_cur = last; | |
2852 | if (qp->s_tail == old_last) | |
2853 | qp->s_tail = last; | |
2854 | if (qp->state == IB_QPS_SQD && last == qp->s_cur) | |
2855 | qp->s_draining = 0; | |
2856 | } | |
2857 | EXPORT_SYMBOL(rvt_send_complete); | |
2858 | ||
019f118b BW |
2859 | /** |
2860 | * rvt_copy_sge - copy data to SGE memory | |
2861 | * @qp: associated QP | |
2862 | * @ss: the SGE state | |
2863 | * @data: the data to copy | |
2864 | * @length: the length of the data | |
2865 | * @release: boolean to release MR | |
2866 | * @copy_last: do a separate copy of the last 8 bytes | |
2867 | */ | |
2868 | void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss, | |
2869 | void *data, u32 length, | |
2870 | bool release, bool copy_last) | |
2871 | { | |
2872 | struct rvt_sge *sge = &ss->sge; | |
2873 | int i; | |
2874 | bool in_last = false; | |
2875 | bool cacheless_copy = false; | |
2876 | struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device); | |
2877 | struct rvt_wss *wss = rdi->wss; | |
2878 | unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode; | |
2879 | ||
2880 | if (sge_copy_mode == RVT_SGE_COPY_CACHELESS) { | |
2881 | cacheless_copy = length >= PAGE_SIZE; | |
2882 | } else if (sge_copy_mode == RVT_SGE_COPY_ADAPTIVE) { | |
2883 | if (length >= PAGE_SIZE) { | |
2884 | /* | |
2885 | * NOTE: this *assumes*: | |
2886 | * o The first vaddr is the dest. | |
2887 | * o If multiple pages, then vaddr is sequential. | |
2888 | */ | |
2889 | wss_insert(wss, sge->vaddr); | |
2890 | if (length >= (2 * PAGE_SIZE)) | |
2891 | wss_insert(wss, (sge->vaddr + PAGE_SIZE)); | |
2892 | ||
2893 | cacheless_copy = wss_exceeds_threshold(wss); | |
2894 | } else { | |
2895 | wss_advance_clean_counter(wss); | |
2896 | } | |
2897 | } | |
2898 | ||
2899 | if (copy_last) { | |
2900 | if (length > 8) { | |
2901 | length -= 8; | |
2902 | } else { | |
2903 | copy_last = false; | |
2904 | in_last = true; | |
2905 | } | |
2906 | } | |
2907 | ||
2908 | again: | |
2909 | while (length) { | |
2910 | u32 len = rvt_get_sge_length(sge, length); | |
2911 | ||
2912 | WARN_ON_ONCE(len == 0); | |
2913 | if (unlikely(in_last)) { | |
2914 | /* enforce byte transfer ordering */ | |
2915 | for (i = 0; i < len; i++) | |
2916 | ((u8 *)sge->vaddr)[i] = ((u8 *)data)[i]; | |
2917 | } else if (cacheless_copy) { | |
2918 | cacheless_memcpy(sge->vaddr, data, len); | |
2919 | } else { | |
2920 | memcpy(sge->vaddr, data, len); | |
2921 | } | |
2922 | rvt_update_sge(ss, len, release); | |
2923 | data += len; | |
2924 | length -= len; | |
2925 | } | |
2926 | ||
2927 | if (copy_last) { | |
2928 | copy_last = false; | |
2929 | in_last = true; | |
2930 | length = 8; | |
2931 | goto again; | |
2932 | } | |
2933 | } | |
2934 | EXPORT_SYMBOL(rvt_copy_sge); | |
15703461 | 2935 | |
d757c60e MR |
2936 | static enum ib_wc_status loopback_qp_drop(struct rvt_ibport *rvp, |
2937 | struct rvt_qp *sqp) | |
2938 | { | |
2939 | rvp->n_pkt_drops++; | |
2940 | /* | |
2941 | * For RC, the requester would timeout and retry so | |
2942 | * shortcut the timeouts and just signal too many retries. | |
2943 | */ | |
2944 | return sqp->ibqp.qp_type == IB_QPT_RC ? | |
2945 | IB_WC_RETRY_EXC_ERR : IB_WC_SUCCESS; | |
2946 | } | |
2947 | ||
15703461 VSD |
2948 | /** |
2949 | * ruc_loopback - handle UC and RC loopback requests | |
2950 | * @sqp: the sending QP | |
2951 | * | |
2952 | * This is called from rvt_do_send() to forward a WQE addressed to the same HFI | |
2953 | * Note that although we are single threaded due to the send engine, we still | |
2954 | * have to protect against post_send(). We don't have to worry about | |
2955 | * receive interrupts since this is a connected protocol and all packets | |
2956 | * will pass through here. | |
2957 | */ | |
2958 | void rvt_ruc_loopback(struct rvt_qp *sqp) | |
2959 | { | |
2960 | struct rvt_ibport *rvp = NULL; | |
2961 | struct rvt_dev_info *rdi = ib_to_rvt(sqp->ibqp.device); | |
2962 | struct rvt_qp *qp; | |
2963 | struct rvt_swqe *wqe; | |
2964 | struct rvt_sge *sge; | |
2965 | unsigned long flags; | |
2966 | struct ib_wc wc; | |
2967 | u64 sdata; | |
2968 | atomic64_t *maddr; | |
2969 | enum ib_wc_status send_status; | |
2970 | bool release; | |
2971 | int ret; | |
2972 | bool copy_last = false; | |
2973 | int local_ops = 0; | |
2974 | ||
2975 | rcu_read_lock(); | |
2976 | rvp = rdi->ports[sqp->port_num - 1]; | |
2977 | ||
2978 | /* | |
2979 | * Note that we check the responder QP state after | |
2980 | * checking the requester's state. | |
2981 | */ | |
2982 | ||
2983 | qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), rvp, | |
2984 | sqp->remote_qpn); | |
2985 | ||
2986 | spin_lock_irqsave(&sqp->s_lock, flags); | |
2987 | ||
2988 | /* Return if we are already busy processing a work request. */ | |
2989 | if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) || | |
2990 | !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND)) | |
2991 | goto unlock; | |
2992 | ||
2993 | sqp->s_flags |= RVT_S_BUSY; | |
2994 | ||
2995 | again: | |
2996 | if (sqp->s_last == READ_ONCE(sqp->s_head)) | |
2997 | goto clr_busy; | |
2998 | wqe = rvt_get_swqe_ptr(sqp, sqp->s_last); | |
2999 | ||
3000 | /* Return if it is not OK to start a new work request. */ | |
3001 | if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) { | |
3002 | if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND)) | |
3003 | goto clr_busy; | |
3004 | /* We are in the error state, flush the work request. */ | |
3005 | send_status = IB_WC_WR_FLUSH_ERR; | |
3006 | goto flush_send; | |
3007 | } | |
3008 | ||
3009 | /* | |
3010 | * We can rely on the entry not changing without the s_lock | |
3011 | * being held until we update s_last. | |
3012 | * We increment s_cur to indicate s_last is in progress. | |
3013 | */ | |
3014 | if (sqp->s_last == sqp->s_cur) { | |
3015 | if (++sqp->s_cur >= sqp->s_size) | |
3016 | sqp->s_cur = 0; | |
3017 | } | |
3018 | spin_unlock_irqrestore(&sqp->s_lock, flags); | |
3019 | ||
d757c60e MR |
3020 | if (!qp) { |
3021 | send_status = loopback_qp_drop(rvp, sqp); | |
3022 | goto serr_no_r_lock; | |
3023 | } | |
3024 | spin_lock_irqsave(&qp->r_lock, flags); | |
3025 | if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) || | |
15703461 | 3026 | qp->ibqp.qp_type != sqp->ibqp.qp_type) { |
d757c60e | 3027 | send_status = loopback_qp_drop(rvp, sqp); |
15703461 VSD |
3028 | goto serr; |
3029 | } | |
3030 | ||
3031 | memset(&wc, 0, sizeof(wc)); | |
3032 | send_status = IB_WC_SUCCESS; | |
3033 | ||
3034 | release = true; | |
3035 | sqp->s_sge.sge = wqe->sg_list[0]; | |
3036 | sqp->s_sge.sg_list = wqe->sg_list + 1; | |
3037 | sqp->s_sge.num_sge = wqe->wr.num_sge; | |
3038 | sqp->s_len = wqe->length; | |
3039 | switch (wqe->wr.opcode) { | |
3040 | case IB_WR_REG_MR: | |
3041 | goto send_comp; | |
3042 | ||
3043 | case IB_WR_LOCAL_INV: | |
3044 | if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) { | |
3045 | if (rvt_invalidate_rkey(sqp, | |
3046 | wqe->wr.ex.invalidate_rkey)) | |
3047 | send_status = IB_WC_LOC_PROT_ERR; | |
3048 | local_ops = 1; | |
3049 | } | |
3050 | goto send_comp; | |
3051 | ||
3052 | case IB_WR_SEND_WITH_INV: | |
15703461 | 3053 | case IB_WR_SEND_WITH_IMM: |
15703461 | 3054 | case IB_WR_SEND: |
15703461 VSD |
3055 | ret = rvt_get_rwqe(qp, false); |
3056 | if (ret < 0) | |
3057 | goto op_err; | |
3058 | if (!ret) | |
3059 | goto rnr_nak; | |
09ce351d MM |
3060 | if (wqe->length > qp->r_len) |
3061 | goto inv_err; | |
38bbc9f0 MM |
3062 | switch (wqe->wr.opcode) { |
3063 | case IB_WR_SEND_WITH_INV: | |
3064 | if (!rvt_invalidate_rkey(qp, | |
3065 | wqe->wr.ex.invalidate_rkey)) { | |
3066 | wc.wc_flags = IB_WC_WITH_INVALIDATE; | |
3067 | wc.ex.invalidate_rkey = | |
3068 | wqe->wr.ex.invalidate_rkey; | |
3069 | } | |
3070 | break; | |
3071 | case IB_WR_SEND_WITH_IMM: | |
3072 | wc.wc_flags = IB_WC_WITH_IMM; | |
3073 | wc.ex.imm_data = wqe->wr.ex.imm_data; | |
3074 | break; | |
3075 | default: | |
3076 | break; | |
3077 | } | |
15703461 VSD |
3078 | break; |
3079 | ||
3080 | case IB_WR_RDMA_WRITE_WITH_IMM: | |
3081 | if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE))) | |
3082 | goto inv_err; | |
3083 | wc.wc_flags = IB_WC_WITH_IMM; | |
3084 | wc.ex.imm_data = wqe->wr.ex.imm_data; | |
3085 | ret = rvt_get_rwqe(qp, true); | |
3086 | if (ret < 0) | |
3087 | goto op_err; | |
3088 | if (!ret) | |
3089 | goto rnr_nak; | |
3090 | /* skip copy_last set and qp_access_flags recheck */ | |
3091 | goto do_write; | |
3092 | case IB_WR_RDMA_WRITE: | |
3093 | copy_last = rvt_is_user_qp(qp); | |
3094 | if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE))) | |
3095 | goto inv_err; | |
3096 | do_write: | |
3097 | if (wqe->length == 0) | |
3098 | break; | |
3099 | if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length, | |
3100 | wqe->rdma_wr.remote_addr, | |
3101 | wqe->rdma_wr.rkey, | |
3102 | IB_ACCESS_REMOTE_WRITE))) | |
3103 | goto acc_err; | |
3104 | qp->r_sge.sg_list = NULL; | |
3105 | qp->r_sge.num_sge = 1; | |
3106 | qp->r_sge.total_len = wqe->length; | |
3107 | break; | |
3108 | ||
3109 | case IB_WR_RDMA_READ: | |
3110 | if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ))) | |
3111 | goto inv_err; | |
3112 | if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length, | |
3113 | wqe->rdma_wr.remote_addr, | |
3114 | wqe->rdma_wr.rkey, | |
3115 | IB_ACCESS_REMOTE_READ))) | |
3116 | goto acc_err; | |
3117 | release = false; | |
3118 | sqp->s_sge.sg_list = NULL; | |
3119 | sqp->s_sge.num_sge = 1; | |
3120 | qp->r_sge.sge = wqe->sg_list[0]; | |
3121 | qp->r_sge.sg_list = wqe->sg_list + 1; | |
3122 | qp->r_sge.num_sge = wqe->wr.num_sge; | |
3123 | qp->r_sge.total_len = wqe->length; | |
3124 | break; | |
3125 | ||
3126 | case IB_WR_ATOMIC_CMP_AND_SWP: | |
3127 | case IB_WR_ATOMIC_FETCH_AND_ADD: | |
3128 | if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC))) | |
3129 | goto inv_err; | |
3130 | if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64), | |
3131 | wqe->atomic_wr.remote_addr, | |
3132 | wqe->atomic_wr.rkey, | |
3133 | IB_ACCESS_REMOTE_ATOMIC))) | |
3134 | goto acc_err; | |
3135 | /* Perform atomic OP and save result. */ | |
3136 | maddr = (atomic64_t *)qp->r_sge.sge.vaddr; | |
3137 | sdata = wqe->atomic_wr.compare_add; | |
3138 | *(u64 *)sqp->s_sge.sge.vaddr = | |
3139 | (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ? | |
3140 | (u64)atomic64_add_return(sdata, maddr) - sdata : | |
3141 | (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr, | |
3142 | sdata, wqe->atomic_wr.swap); | |
3143 | rvt_put_mr(qp->r_sge.sge.mr); | |
3144 | qp->r_sge.num_sge = 0; | |
3145 | goto send_comp; | |
3146 | ||
3147 | default: | |
3148 | send_status = IB_WC_LOC_QP_OP_ERR; | |
3149 | goto serr; | |
3150 | } | |
3151 | ||
3152 | sge = &sqp->s_sge.sge; | |
3153 | while (sqp->s_len) { | |
db421a54 | 3154 | u32 len = rvt_get_sge_length(sge, sqp->s_len); |
15703461 | 3155 | |
15703461 VSD |
3156 | WARN_ON_ONCE(len == 0); |
3157 | rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, | |
3158 | len, release, copy_last); | |
db421a54 | 3159 | rvt_update_sge(&sqp->s_sge, len, !release); |
15703461 VSD |
3160 | sqp->s_len -= len; |
3161 | } | |
3162 | if (release) | |
3163 | rvt_put_ss(&qp->r_sge); | |
3164 | ||
3165 | if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) | |
3166 | goto send_comp; | |
3167 | ||
3168 | if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM) | |
3169 | wc.opcode = IB_WC_RECV_RDMA_WITH_IMM; | |
3170 | else | |
3171 | wc.opcode = IB_WC_RECV; | |
3172 | wc.wr_id = qp->r_wr_id; | |
3173 | wc.status = IB_WC_SUCCESS; | |
3174 | wc.byte_len = wqe->length; | |
3175 | wc.qp = &qp->ibqp; | |
3176 | wc.src_qp = qp->remote_qpn; | |
3177 | wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX; | |
3178 | wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr); | |
3179 | wc.port_num = 1; | |
3180 | /* Signal completion event if the solicited bit is set. */ | |
5136bfea | 3181 | rvt_recv_cq(qp, &wc, wqe->wr.send_flags & IB_SEND_SOLICITED); |
15703461 VSD |
3182 | |
3183 | send_comp: | |
d757c60e | 3184 | spin_unlock_irqrestore(&qp->r_lock, flags); |
15703461 VSD |
3185 | spin_lock_irqsave(&sqp->s_lock, flags); |
3186 | rvp->n_loop_pkts++; | |
3187 | flush_send: | |
3188 | sqp->s_rnr_retry = sqp->s_rnr_retry_cnt; | |
3189 | rvt_send_complete(sqp, wqe, send_status); | |
3190 | if (local_ops) { | |
3191 | atomic_dec(&sqp->local_ops_pending); | |
3192 | local_ops = 0; | |
3193 | } | |
3194 | goto again; | |
3195 | ||
3196 | rnr_nak: | |
3197 | /* Handle RNR NAK */ | |
3198 | if (qp->ibqp.qp_type == IB_QPT_UC) | |
3199 | goto send_comp; | |
3200 | rvp->n_rnr_naks++; | |
3201 | /* | |
3202 | * Note: we don't need the s_lock held since the BUSY flag | |
3203 | * makes this single threaded. | |
3204 | */ | |
3205 | if (sqp->s_rnr_retry == 0) { | |
3206 | send_status = IB_WC_RNR_RETRY_EXC_ERR; | |
3207 | goto serr; | |
3208 | } | |
3209 | if (sqp->s_rnr_retry_cnt < 7) | |
3210 | sqp->s_rnr_retry--; | |
d757c60e | 3211 | spin_unlock_irqrestore(&qp->r_lock, flags); |
15703461 VSD |
3212 | spin_lock_irqsave(&sqp->s_lock, flags); |
3213 | if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK)) | |
3214 | goto clr_busy; | |
3215 | rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer << | |
3216 | IB_AETH_CREDIT_SHIFT); | |
3217 | goto clr_busy; | |
3218 | ||
3219 | op_err: | |
3220 | send_status = IB_WC_REM_OP_ERR; | |
3221 | wc.status = IB_WC_LOC_QP_OP_ERR; | |
3222 | goto err; | |
3223 | ||
3224 | inv_err: | |
09ce351d MM |
3225 | send_status = |
3226 | sqp->ibqp.qp_type == IB_QPT_RC ? | |
3227 | IB_WC_REM_INV_REQ_ERR : | |
3228 | IB_WC_SUCCESS; | |
15703461 VSD |
3229 | wc.status = IB_WC_LOC_QP_OP_ERR; |
3230 | goto err; | |
3231 | ||
3232 | acc_err: | |
3233 | send_status = IB_WC_REM_ACCESS_ERR; | |
3234 | wc.status = IB_WC_LOC_PROT_ERR; | |
3235 | err: | |
3236 | /* responder goes to error state */ | |
3237 | rvt_rc_error(qp, wc.status); | |
3238 | ||
3239 | serr: | |
d757c60e MR |
3240 | spin_unlock_irqrestore(&qp->r_lock, flags); |
3241 | serr_no_r_lock: | |
15703461 VSD |
3242 | spin_lock_irqsave(&sqp->s_lock, flags); |
3243 | rvt_send_complete(sqp, wqe, send_status); | |
3244 | if (sqp->ibqp.qp_type == IB_QPT_RC) { | |
3245 | int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR); | |
3246 | ||
3247 | sqp->s_flags &= ~RVT_S_BUSY; | |
3248 | spin_unlock_irqrestore(&sqp->s_lock, flags); | |
3249 | if (lastwqe) { | |
3250 | struct ib_event ev; | |
3251 | ||
3252 | ev.device = sqp->ibqp.device; | |
3253 | ev.element.qp = &sqp->ibqp; | |
3254 | ev.event = IB_EVENT_QP_LAST_WQE_REACHED; | |
3255 | sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context); | |
3256 | } | |
3257 | goto done; | |
3258 | } | |
3259 | clr_busy: | |
3260 | sqp->s_flags &= ~RVT_S_BUSY; | |
3261 | unlock: | |
3262 | spin_unlock_irqrestore(&sqp->s_lock, flags); | |
3263 | done: | |
3264 | rcu_read_unlock(); | |
3265 | } | |
3266 | EXPORT_SYMBOL(rvt_ruc_loopback); |