Commit | Line | Data |
---|---|---|
77241056 MM |
1 | /* |
2 | * | |
3 | * This file is provided under a dual BSD/GPLv2 license. When using or | |
4 | * redistributing this file, you may do so under either license. | |
5 | * | |
6 | * GPL LICENSE SUMMARY | |
7 | * | |
8 | * Copyright(c) 2015 Intel Corporation. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of version 2 of the GNU General Public License as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * BSD LICENSE | |
20 | * | |
21 | * Copyright(c) 2015 Intel Corporation. | |
22 | * | |
23 | * Redistribution and use in source and binary forms, with or without | |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * | |
27 | * - Redistributions of source code must retain the above copyright | |
28 | * notice, this list of conditions and the following disclaimer. | |
29 | * - Redistributions in binary form must reproduce the above copyright | |
30 | * notice, this list of conditions and the following disclaimer in | |
31 | * the documentation and/or other materials provided with the | |
32 | * distribution. | |
33 | * - Neither the name of Intel Corporation nor the names of its | |
34 | * contributors may be used to endorse or promote products derived | |
35 | * from this software without specific prior written permission. | |
36 | * | |
37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
38 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
39 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
40 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
41 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
42 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
43 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
44 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
45 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
46 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
47 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
48 | * | |
49 | */ | |
50 | ||
51 | #include <linux/err.h> | |
52 | #include <linux/vmalloc.h> | |
53 | #include <linux/hash.h> | |
54 | #include <linux/module.h> | |
55 | #include <linux/random.h> | |
56 | #include <linux/seq_file.h> | |
ec4274f1 DD |
57 | #include <rdma/rdma_vt.h> |
58 | #include <rdma/rdmavt_qp.h> | |
77241056 MM |
59 | |
60 | #include "hfi.h" | |
61 | #include "qp.h" | |
62 | #include "trace.h" | |
45842abb | 63 | #include "verbs_txreq.h" |
77241056 | 64 | |
a2c2d608 | 65 | unsigned int hfi1_qp_table_size = 256; |
77241056 MM |
66 | module_param_named(qp_table_size, hfi1_qp_table_size, uint, S_IRUGO); |
67 | MODULE_PARM_DESC(qp_table_size, "QP table size"); | |
68 | ||
895420dd | 69 | static void flush_tx_list(struct rvt_qp *qp); |
77241056 MM |
70 | static int iowait_sleep( |
71 | struct sdma_engine *sde, | |
72 | struct iowait *wait, | |
73 | struct sdma_txreq *stx, | |
74 | unsigned seq); | |
75 | static void iowait_wakeup(struct iowait *wait, int reason); | |
a545f530 | 76 | static void iowait_sdma_drained(struct iowait *wait); |
91702b4a | 77 | static void qp_pio_drain(struct rvt_qp *qp); |
77241056 | 78 | |
1c4b7d97 DD |
79 | static inline unsigned mk_qpn(struct rvt_qpn_table *qpt, |
80 | struct rvt_qpn_map *map, unsigned off) | |
77241056 | 81 | { |
1c4b7d97 | 82 | return (map - qpt->map) * RVT_BITS_PER_PAGE + off; |
77241056 MM |
83 | } |
84 | ||
85 | /* | |
86 | * Convert the AETH credit code into the number of credits. | |
87 | */ | |
88 | static const u16 credit_table[31] = { | |
89 | 0, /* 0 */ | |
90 | 1, /* 1 */ | |
91 | 2, /* 2 */ | |
92 | 3, /* 3 */ | |
93 | 4, /* 4 */ | |
94 | 6, /* 5 */ | |
95 | 8, /* 6 */ | |
96 | 12, /* 7 */ | |
97 | 16, /* 8 */ | |
98 | 24, /* 9 */ | |
99 | 32, /* A */ | |
100 | 48, /* B */ | |
101 | 64, /* C */ | |
102 | 96, /* D */ | |
103 | 128, /* E */ | |
104 | 192, /* F */ | |
105 | 256, /* 10 */ | |
106 | 384, /* 11 */ | |
107 | 512, /* 12 */ | |
108 | 768, /* 13 */ | |
109 | 1024, /* 14 */ | |
110 | 1536, /* 15 */ | |
111 | 2048, /* 16 */ | |
112 | 3072, /* 17 */ | |
113 | 4096, /* 18 */ | |
114 | 6144, /* 19 */ | |
115 | 8192, /* 1A */ | |
116 | 12288, /* 1B */ | |
117 | 16384, /* 1C */ | |
118 | 24576, /* 1D */ | |
119 | 32768 /* 1E */ | |
120 | }; | |
121 | ||
895420dd | 122 | static void flush_tx_list(struct rvt_qp *qp) |
77241056 | 123 | { |
4c6829c5 DD |
124 | struct hfi1_qp_priv *priv = qp->priv; |
125 | ||
126 | while (!list_empty(&priv->s_iowait.tx_head)) { | |
77241056 MM |
127 | struct sdma_txreq *tx; |
128 | ||
129 | tx = list_first_entry( | |
4c6829c5 | 130 | &priv->s_iowait.tx_head, |
77241056 MM |
131 | struct sdma_txreq, |
132 | list); | |
133 | list_del_init(&tx->list); | |
134 | hfi1_put_txreq( | |
135 | container_of(tx, struct verbs_txreq, txreq)); | |
136 | } | |
137 | } | |
138 | ||
895420dd | 139 | static void flush_iowait(struct rvt_qp *qp) |
77241056 | 140 | { |
4c6829c5 | 141 | struct hfi1_qp_priv *priv = qp->priv; |
77241056 MM |
142 | struct hfi1_ibdev *dev = to_idev(qp->ibqp.device); |
143 | unsigned long flags; | |
144 | ||
145 | write_seqlock_irqsave(&dev->iowait_lock, flags); | |
4c6829c5 DD |
146 | if (!list_empty(&priv->s_iowait.list)) { |
147 | list_del_init(&priv->s_iowait.list); | |
77241056 MM |
148 | if (atomic_dec_and_test(&qp->refcount)) |
149 | wake_up(&qp->wait); | |
150 | } | |
151 | write_sequnlock_irqrestore(&dev->iowait_lock, flags); | |
152 | } | |
153 | ||
154 | static inline int opa_mtu_enum_to_int(int mtu) | |
155 | { | |
156 | switch (mtu) { | |
157 | case OPA_MTU_8192: return 8192; | |
158 | case OPA_MTU_10240: return 10240; | |
159 | default: return -1; | |
160 | } | |
161 | } | |
162 | ||
163 | /** | |
164 | * This function is what we would push to the core layer if we wanted to be a | |
165 | * "first class citizen". Instead we hide this here and rely on Verbs ULPs | |
166 | * to blindly pass the MTU enum value from the PathRecord to us. | |
167 | * | |
168 | * The actual flag used to determine "8k MTU" will change and is currently | |
169 | * unknown. | |
170 | */ | |
171 | static inline int verbs_mtu_enum_to_int(struct ib_device *dev, enum ib_mtu mtu) | |
172 | { | |
173 | int val = opa_mtu_enum_to_int((int)mtu); | |
174 | ||
175 | if (val > 0) | |
176 | return val; | |
177 | return ib_mtu_enum_to_int(mtu); | |
178 | } | |
179 | ||
ec4274f1 DD |
180 | int hfi1_check_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr, |
181 | int attr_mask, struct ib_udata *udata) | |
77241056 | 182 | { |
ec4274f1 | 183 | struct ib_qp *ibqp = &qp->ibqp; |
77241056 | 184 | struct hfi1_ibdev *dev = to_idev(ibqp->device); |
d7b8ba51 | 185 | struct hfi1_devdata *dd = dd_from_dev(dev); |
ec4274f1 | 186 | u8 sc; |
77241056 MM |
187 | |
188 | if (attr_mask & IB_QP_AV) { | |
d7b8ba51 | 189 | sc = ah_to_sc(ibqp->device, &attr->ah_attr); |
31e7af1c IW |
190 | if (sc == 0xf) |
191 | return -EINVAL; | |
192 | ||
d7b8ba51 MM |
193 | if (!qp_to_sdma_engine(qp, sc) && |
194 | dd->flags & HFI1_HAS_SEND_DMA) | |
ec4274f1 | 195 | return -EINVAL; |
721d0427 JJ |
196 | |
197 | if (!qp_to_send_context(qp, sc)) | |
198 | return -EINVAL; | |
77241056 MM |
199 | } |
200 | ||
201 | if (attr_mask & IB_QP_ALT_PATH) { | |
d7b8ba51 | 202 | sc = ah_to_sc(ibqp->device, &attr->alt_ah_attr); |
31e7af1c IW |
203 | if (sc == 0xf) |
204 | return -EINVAL; | |
205 | ||
d7b8ba51 MM |
206 | if (!qp_to_sdma_engine(qp, sc) && |
207 | dd->flags & HFI1_HAS_SEND_DMA) | |
ec4274f1 | 208 | return -EINVAL; |
721d0427 JJ |
209 | |
210 | if (!qp_to_send_context(qp, sc)) | |
211 | return -EINVAL; | |
77241056 MM |
212 | } |
213 | ||
ec4274f1 DD |
214 | return 0; |
215 | } | |
77241056 | 216 | |
ec4274f1 DD |
217 | void hfi1_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr, |
218 | int attr_mask, struct ib_udata *udata) | |
219 | { | |
220 | struct ib_qp *ibqp = &qp->ibqp; | |
221 | struct hfi1_qp_priv *priv = qp->priv; | |
77241056 MM |
222 | |
223 | if (attr_mask & IB_QP_AV) { | |
4c6829c5 DD |
224 | priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr); |
225 | priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc); | |
77241056 MM |
226 | } |
227 | ||
ec4274f1 DD |
228 | if (attr_mask & IB_QP_PATH_MIG_STATE && |
229 | attr->path_mig_state == IB_MIG_MIGRATED && | |
230 | qp->s_mig_state == IB_MIG_ARMED) { | |
231 | qp->s_flags |= RVT_S_AHG_CLEAR; | |
232 | priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr); | |
233 | priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc); | |
77241056 | 234 | } |
77241056 MM |
235 | } |
236 | ||
46a80d62 MM |
237 | /** |
238 | * hfi1_check_send_wqe - validate wqe | |
239 | * @qp - The qp | |
240 | * @wqe - The built wqe | |
241 | * | |
242 | * validate wqe. This is called | |
243 | * prior to inserting the wqe into | |
244 | * the ring but after the wqe has been | |
245 | * setup. | |
246 | * | |
247 | * Returns 0 on success, -EINVAL on failure | |
248 | * | |
249 | */ | |
250 | int hfi1_check_send_wqe(struct rvt_qp *qp, | |
251 | struct rvt_swqe *wqe) | |
31e7af1c IW |
252 | { |
253 | struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num); | |
46a80d62 | 254 | struct rvt_ah *ah; |
31e7af1c | 255 | |
46a80d62 MM |
256 | switch (qp->ibqp.qp_type) { |
257 | case IB_QPT_RC: | |
258 | case IB_QPT_UC: | |
259 | if (wqe->length > 0x80000000U) | |
260 | return -EINVAL; | |
261 | break; | |
262 | case IB_QPT_SMI: | |
263 | ah = ibah_to_rvtah(wqe->ud_wr.ah); | |
264 | if (wqe->length > (1 << ah->log_pmtu)) | |
265 | return -EINVAL; | |
266 | break; | |
267 | case IB_QPT_GSI: | |
268 | case IB_QPT_UD: | |
269 | ah = ibah_to_rvtah(wqe->ud_wr.ah); | |
270 | if (wqe->length > (1 << ah->log_pmtu)) | |
271 | return -EINVAL; | |
272 | if (ibp->sl_to_sc[ah->attr.sl] == 0xf) | |
273 | return -EINVAL; | |
274 | default: | |
275 | break; | |
31e7af1c | 276 | } |
91702b4a | 277 | return wqe->length <= piothreshold; |
31e7af1c IW |
278 | } |
279 | ||
77241056 MM |
280 | /** |
281 | * hfi1_compute_aeth - compute the AETH (syndrome + MSN) | |
282 | * @qp: the queue pair to compute the AETH for | |
283 | * | |
284 | * Returns the AETH. | |
285 | */ | |
895420dd | 286 | __be32 hfi1_compute_aeth(struct rvt_qp *qp) |
77241056 MM |
287 | { |
288 | u32 aeth = qp->r_msn & HFI1_MSN_MASK; | |
289 | ||
290 | if (qp->ibqp.srq) { | |
291 | /* | |
292 | * Shared receive queues don't generate credits. | |
293 | * Set the credit field to the invalid value. | |
294 | */ | |
295 | aeth |= HFI1_AETH_CREDIT_INVAL << HFI1_AETH_CREDIT_SHIFT; | |
296 | } else { | |
297 | u32 min, max, x; | |
298 | u32 credits; | |
895420dd | 299 | struct rvt_rwq *wq = qp->r_rq.wq; |
77241056 MM |
300 | u32 head; |
301 | u32 tail; | |
302 | ||
303 | /* sanity check pointers before trusting them */ | |
304 | head = wq->head; | |
305 | if (head >= qp->r_rq.size) | |
306 | head = 0; | |
307 | tail = wq->tail; | |
308 | if (tail >= qp->r_rq.size) | |
309 | tail = 0; | |
310 | /* | |
311 | * Compute the number of credits available (RWQEs). | |
312 | * There is a small chance that the pair of reads are | |
313 | * not atomic, which is OK, since the fuzziness is | |
314 | * resolved as further ACKs go out. | |
315 | */ | |
316 | credits = head - tail; | |
317 | if ((int)credits < 0) | |
318 | credits += qp->r_rq.size; | |
319 | /* | |
320 | * Binary search the credit table to find the code to | |
321 | * use. | |
322 | */ | |
323 | min = 0; | |
324 | max = 31; | |
325 | for (;;) { | |
326 | x = (min + max) / 2; | |
327 | if (credit_table[x] == credits) | |
328 | break; | |
329 | if (credit_table[x] > credits) | |
330 | max = x; | |
331 | else if (min == x) | |
332 | break; | |
333 | else | |
334 | min = x; | |
335 | } | |
336 | aeth |= x << HFI1_AETH_CREDIT_SHIFT; | |
337 | } | |
338 | return cpu_to_be32(aeth); | |
339 | } | |
340 | ||
46a80d62 MM |
341 | /** |
342 | * _hfi1_schedule_send - schedule progress | |
343 | * @qp: the QP | |
344 | * | |
345 | * This schedules qp progress w/o regard to the s_flags. | |
346 | * | |
347 | * It is only used in the post send, which doesn't hold | |
348 | * the s_lock. | |
349 | */ | |
350 | void _hfi1_schedule_send(struct rvt_qp *qp) | |
351 | { | |
352 | struct hfi1_qp_priv *priv = qp->priv; | |
353 | struct hfi1_ibport *ibp = | |
354 | to_iport(qp->ibqp.device, qp->port_num); | |
355 | struct hfi1_pportdata *ppd = ppd_from_ibp(ibp); | |
356 | struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device); | |
357 | ||
358 | iowait_schedule(&priv->s_iowait, ppd->hfi1_wq, | |
359 | priv->s_sde ? | |
360 | priv->s_sde->cpu : | |
361 | cpumask_first(cpumask_of_node(dd->node))); | |
362 | } | |
363 | ||
14553ca1 MM |
364 | static void qp_pio_drain(struct rvt_qp *qp) |
365 | { | |
366 | struct hfi1_ibdev *dev; | |
367 | struct hfi1_qp_priv *priv = qp->priv; | |
368 | ||
369 | if (!priv->s_sendcontext) | |
370 | return; | |
371 | dev = to_idev(qp->ibqp.device); | |
372 | while (iowait_pio_pending(&priv->s_iowait)) { | |
373 | write_seqlock_irq(&dev->iowait_lock); | |
374 | hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 1); | |
375 | write_sequnlock_irq(&dev->iowait_lock); | |
376 | iowait_pio_drain(&priv->s_iowait); | |
377 | write_seqlock_irq(&dev->iowait_lock); | |
378 | hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 0); | |
379 | write_sequnlock_irq(&dev->iowait_lock); | |
380 | } | |
381 | } | |
382 | ||
46a80d62 MM |
383 | /** |
384 | * hfi1_schedule_send - schedule progress | |
385 | * @qp: the QP | |
386 | * | |
387 | * This schedules qp progress and caller should hold | |
388 | * the s_lock. | |
389 | */ | |
390 | void hfi1_schedule_send(struct rvt_qp *qp) | |
391 | { | |
392 | if (hfi1_send_ok(qp)) | |
393 | _hfi1_schedule_send(qp); | |
394 | } | |
395 | ||
77241056 MM |
396 | /** |
397 | * hfi1_get_credit - flush the send work queue of a QP | |
398 | * @qp: the qp who's send work queue to flush | |
399 | * @aeth: the Acknowledge Extended Transport Header | |
400 | * | |
401 | * The QP s_lock should be held. | |
402 | */ | |
895420dd | 403 | void hfi1_get_credit(struct rvt_qp *qp, u32 aeth) |
77241056 MM |
404 | { |
405 | u32 credit = (aeth >> HFI1_AETH_CREDIT_SHIFT) & HFI1_AETH_CREDIT_MASK; | |
406 | ||
407 | /* | |
408 | * If the credit is invalid, we can send | |
409 | * as many packets as we like. Otherwise, we have to | |
410 | * honor the credit field. | |
411 | */ | |
412 | if (credit == HFI1_AETH_CREDIT_INVAL) { | |
54d10c1e DD |
413 | if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) { |
414 | qp->s_flags |= RVT_S_UNLIMITED_CREDIT; | |
415 | if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) { | |
416 | qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT; | |
77241056 MM |
417 | hfi1_schedule_send(qp); |
418 | } | |
419 | } | |
54d10c1e | 420 | } else if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) { |
77241056 MM |
421 | /* Compute new LSN (i.e., MSN + credit) */ |
422 | credit = (aeth + credit_table[credit]) & HFI1_MSN_MASK; | |
423 | if (cmp_msn(credit, qp->s_lsn) > 0) { | |
424 | qp->s_lsn = credit; | |
54d10c1e DD |
425 | if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) { |
426 | qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT; | |
77241056 MM |
427 | hfi1_schedule_send(qp); |
428 | } | |
429 | } | |
430 | } | |
431 | } | |
432 | ||
895420dd | 433 | void hfi1_qp_wakeup(struct rvt_qp *qp, u32 flag) |
77241056 MM |
434 | { |
435 | unsigned long flags; | |
436 | ||
437 | spin_lock_irqsave(&qp->s_lock, flags); | |
438 | if (qp->s_flags & flag) { | |
439 | qp->s_flags &= ~flag; | |
440 | trace_hfi1_qpwakeup(qp, flag); | |
441 | hfi1_schedule_send(qp); | |
442 | } | |
443 | spin_unlock_irqrestore(&qp->s_lock, flags); | |
444 | /* Notify hfi1_destroy_qp() if it is waiting. */ | |
445 | if (atomic_dec_and_test(&qp->refcount)) | |
446 | wake_up(&qp->wait); | |
447 | } | |
448 | ||
449 | static int iowait_sleep( | |
450 | struct sdma_engine *sde, | |
451 | struct iowait *wait, | |
452 | struct sdma_txreq *stx, | |
453 | unsigned seq) | |
454 | { | |
455 | struct verbs_txreq *tx = container_of(stx, struct verbs_txreq, txreq); | |
895420dd | 456 | struct rvt_qp *qp; |
4c6829c5 | 457 | struct hfi1_qp_priv *priv; |
77241056 MM |
458 | unsigned long flags; |
459 | int ret = 0; | |
460 | struct hfi1_ibdev *dev; | |
461 | ||
462 | qp = tx->qp; | |
4c6829c5 | 463 | priv = qp->priv; |
77241056 MM |
464 | |
465 | spin_lock_irqsave(&qp->s_lock, flags); | |
83693bd1 | 466 | if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) { |
77241056 MM |
467 | /* |
468 | * If we couldn't queue the DMA request, save the info | |
469 | * and try again later rather than destroying the | |
470 | * buffer and undoing the side effects of the copy. | |
471 | */ | |
472 | /* Make a common routine? */ | |
473 | dev = &sde->dd->verbs_dev; | |
474 | list_add_tail(&stx->list, &wait->tx_head); | |
475 | write_seqlock(&dev->iowait_lock); | |
476 | if (sdma_progress(sde, seq, stx)) | |
477 | goto eagain; | |
4c6829c5 | 478 | if (list_empty(&priv->s_iowait.list)) { |
77241056 MM |
479 | struct hfi1_ibport *ibp = |
480 | to_iport(qp->ibqp.device, qp->port_num); | |
481 | ||
4eb06882 | 482 | ibp->rvp.n_dmawait++; |
54d10c1e | 483 | qp->s_flags |= RVT_S_WAIT_DMA_DESC; |
4c6829c5 | 484 | list_add_tail(&priv->s_iowait.list, &sde->dmawait); |
54d10c1e | 485 | trace_hfi1_qpsleep(qp, RVT_S_WAIT_DMA_DESC); |
77241056 MM |
486 | atomic_inc(&qp->refcount); |
487 | } | |
488 | write_sequnlock(&dev->iowait_lock); | |
54d10c1e | 489 | qp->s_flags &= ~RVT_S_BUSY; |
77241056 MM |
490 | spin_unlock_irqrestore(&qp->s_lock, flags); |
491 | ret = -EBUSY; | |
492 | } else { | |
493 | spin_unlock_irqrestore(&qp->s_lock, flags); | |
494 | hfi1_put_txreq(tx); | |
495 | } | |
496 | return ret; | |
497 | eagain: | |
498 | write_sequnlock(&dev->iowait_lock); | |
499 | spin_unlock_irqrestore(&qp->s_lock, flags); | |
500 | list_del_init(&stx->list); | |
501 | return -EAGAIN; | |
502 | } | |
503 | ||
504 | static void iowait_wakeup(struct iowait *wait, int reason) | |
505 | { | |
895420dd | 506 | struct rvt_qp *qp = iowait_to_qp(wait); |
77241056 MM |
507 | |
508 | WARN_ON(reason != SDMA_AVAIL_REASON); | |
54d10c1e | 509 | hfi1_qp_wakeup(qp, RVT_S_WAIT_DMA_DESC); |
77241056 MM |
510 | } |
511 | ||
a545f530 MM |
512 | static void iowait_sdma_drained(struct iowait *wait) |
513 | { | |
514 | struct rvt_qp *qp = iowait_to_qp(wait); | |
515 | ||
516 | /* | |
517 | * This happens when the send engine notes | |
518 | * a QP in the error state and cannot | |
519 | * do the flush work until that QP's | |
520 | * sdma work has finished. | |
521 | */ | |
522 | if (qp->s_flags & RVT_S_WAIT_DMA) { | |
523 | qp->s_flags &= ~RVT_S_WAIT_DMA; | |
524 | hfi1_schedule_send(qp); | |
525 | } | |
526 | } | |
527 | ||
77241056 MM |
528 | /** |
529 | * | |
530 | * qp_to_sdma_engine - map a qp to a send engine | |
531 | * @qp: the QP | |
532 | * @sc5: the 5 bit sc | |
533 | * | |
534 | * Return: | |
535 | * A send engine for the qp or NULL for SMI type qp. | |
536 | */ | |
895420dd | 537 | struct sdma_engine *qp_to_sdma_engine(struct rvt_qp *qp, u8 sc5) |
77241056 MM |
538 | { |
539 | struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device); | |
540 | struct sdma_engine *sde; | |
541 | ||
542 | if (!(dd->flags & HFI1_HAS_SEND_DMA)) | |
543 | return NULL; | |
544 | switch (qp->ibqp.qp_type) { | |
77241056 MM |
545 | case IB_QPT_SMI: |
546 | return NULL; | |
547 | default: | |
548 | break; | |
549 | } | |
550 | sde = sdma_select_engine_sc(dd, qp->ibqp.qp_num >> dd->qos_shift, sc5); | |
551 | return sde; | |
552 | } | |
553 | ||
35f6befc JJ |
554 | /* |
555 | * qp_to_send_context - map a qp to a send context | |
556 | * @qp: the QP | |
557 | * @sc5: the 5 bit sc | |
558 | * | |
559 | * Return: | |
560 | * A send context for the qp | |
561 | */ | |
562 | struct send_context *qp_to_send_context(struct rvt_qp *qp, u8 sc5) | |
563 | { | |
564 | struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device); | |
565 | ||
566 | switch (qp->ibqp.qp_type) { | |
567 | case IB_QPT_SMI: | |
568 | /* SMA packets to VL15 */ | |
569 | return dd->vld[15].sc; | |
570 | default: | |
571 | break; | |
572 | } | |
573 | ||
574 | return pio_select_send_context_sc(dd, qp->ibqp.qp_num >> dd->qos_shift, | |
575 | sc5); | |
576 | } | |
577 | ||
77241056 MM |
578 | struct qp_iter { |
579 | struct hfi1_ibdev *dev; | |
895420dd | 580 | struct rvt_qp *qp; |
77241056 MM |
581 | int specials; |
582 | int n; | |
583 | }; | |
584 | ||
585 | struct qp_iter *qp_iter_init(struct hfi1_ibdev *dev) | |
586 | { | |
587 | struct qp_iter *iter; | |
588 | ||
589 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); | |
590 | if (!iter) | |
591 | return NULL; | |
592 | ||
593 | iter->dev = dev; | |
ec3f2c12 | 594 | iter->specials = dev->rdi.ibdev.phys_port_cnt * 2; |
77241056 MM |
595 | if (qp_iter_next(iter)) { |
596 | kfree(iter); | |
597 | return NULL; | |
598 | } | |
599 | ||
600 | return iter; | |
601 | } | |
602 | ||
603 | int qp_iter_next(struct qp_iter *iter) | |
604 | { | |
605 | struct hfi1_ibdev *dev = iter->dev; | |
606 | int n = iter->n; | |
607 | int ret = 1; | |
895420dd DD |
608 | struct rvt_qp *pqp = iter->qp; |
609 | struct rvt_qp *qp; | |
77241056 MM |
610 | |
611 | /* | |
612 | * The approach is to consider the special qps | |
613 | * as an additional table entries before the | |
614 | * real hash table. Since the qp code sets | |
615 | * the qp->next hash link to NULL, this works just fine. | |
616 | * | |
617 | * iter->specials is 2 * # ports | |
618 | * | |
619 | * n = 0..iter->specials is the special qp indices | |
620 | * | |
1c4b7d97 | 621 | * n = iter->specials..dev->rdi.qp_dev->qp_table_size+iter->specials are |
77241056 MM |
622 | * the potential hash bucket entries |
623 | * | |
624 | */ | |
1c4b7d97 | 625 | for (; n < dev->rdi.qp_dev->qp_table_size + iter->specials; n++) { |
77241056 MM |
626 | if (pqp) { |
627 | qp = rcu_dereference(pqp->next); | |
628 | } else { | |
629 | if (n < iter->specials) { | |
630 | struct hfi1_pportdata *ppd; | |
631 | struct hfi1_ibport *ibp; | |
632 | int pidx; | |
633 | ||
ec3f2c12 | 634 | pidx = n % dev->rdi.ibdev.phys_port_cnt; |
77241056 MM |
635 | ppd = &dd_from_dev(dev)->pport[pidx]; |
636 | ibp = &ppd->ibport_data; | |
637 | ||
638 | if (!(n & 1)) | |
4eb06882 | 639 | qp = rcu_dereference(ibp->rvp.qp[0]); |
77241056 | 640 | else |
4eb06882 | 641 | qp = rcu_dereference(ibp->rvp.qp[1]); |
77241056 MM |
642 | } else { |
643 | qp = rcu_dereference( | |
1c4b7d97 | 644 | dev->rdi.qp_dev->qp_table[ |
77241056 MM |
645 | (n - iter->specials)]); |
646 | } | |
647 | } | |
648 | pqp = qp; | |
649 | if (qp) { | |
650 | iter->qp = qp; | |
651 | iter->n = n; | |
652 | return 0; | |
653 | } | |
654 | } | |
655 | return ret; | |
656 | } | |
657 | ||
658 | static const char * const qp_type_str[] = { | |
659 | "SMI", "GSI", "RC", "UC", "UD", | |
660 | }; | |
661 | ||
895420dd | 662 | static int qp_idle(struct rvt_qp *qp) |
77241056 MM |
663 | { |
664 | return | |
665 | qp->s_last == qp->s_acked && | |
666 | qp->s_acked == qp->s_cur && | |
667 | qp->s_cur == qp->s_tail && | |
668 | qp->s_tail == qp->s_head; | |
669 | } | |
670 | ||
671 | void qp_iter_print(struct seq_file *s, struct qp_iter *iter) | |
672 | { | |
895420dd DD |
673 | struct rvt_swqe *wqe; |
674 | struct rvt_qp *qp = iter->qp; | |
4c6829c5 | 675 | struct hfi1_qp_priv *priv = qp->priv; |
77241056 | 676 | struct sdma_engine *sde; |
721d0427 | 677 | struct send_context *send_context; |
77241056 | 678 | |
4c6829c5 | 679 | sde = qp_to_sdma_engine(qp, priv->s_sc); |
83693bd1 | 680 | wqe = rvt_get_swqe_ptr(qp, qp->s_last); |
721d0427 | 681 | send_context = qp_to_send_context(qp, priv->s_sc); |
77241056 | 682 | seq_printf(s, |
0358a440 | 683 | "N %d %s QP%x R %u %s %u %u %u f=%x %u %u %u %u %u %u PSN %x %x %x %x %x (%u %u %u %u %u %u %u) QP%x LID %x SL %u MTU %u %u %u %u SDE %p,%u SC %p,%u CQ %u %u\n", |
77241056 MM |
684 | iter->n, |
685 | qp_idle(qp) ? "I" : "B", | |
686 | qp->ibqp.qp_num, | |
687 | atomic_read(&qp->refcount), | |
688 | qp_type_str[qp->ibqp.qp_type], | |
689 | qp->state, | |
690 | wqe ? wqe->wr.opcode : 0, | |
691 | qp->s_hdrwords, | |
692 | qp->s_flags, | |
14553ca1 MM |
693 | iowait_sdma_pending(&priv->s_iowait), |
694 | iowait_pio_pending(&priv->s_iowait), | |
4c6829c5 | 695 | !list_empty(&priv->s_iowait.list), |
77241056 MM |
696 | qp->timeout, |
697 | wqe ? wqe->ssn : 0, | |
698 | qp->s_lsn, | |
699 | qp->s_last_psn, | |
700 | qp->s_psn, qp->s_next_psn, | |
701 | qp->s_sending_psn, qp->s_sending_hpsn, | |
702 | qp->s_last, qp->s_acked, qp->s_cur, | |
703 | qp->s_tail, qp->s_head, qp->s_size, | |
3585254d | 704 | qp->s_avail, |
77241056 MM |
705 | qp->remote_qpn, |
706 | qp->remote_ah_attr.dlid, | |
707 | qp->remote_ah_attr.sl, | |
708 | qp->pmtu, | |
20658661 | 709 | qp->s_retry, |
77241056 | 710 | qp->s_retry_cnt, |
77241056 MM |
711 | qp->s_rnr_retry_cnt, |
712 | sde, | |
721d0427 | 713 | sde ? sde->this_idx : 0, |
77e7639f | 714 | send_context, |
0358a440 VM |
715 | send_context ? send_context->sw_index : 0, |
716 | ibcq_to_rvtcq(qp->ibqp.send_cq)->queue->head, | |
717 | ibcq_to_rvtcq(qp->ibqp.send_cq)->queue->tail); | |
77241056 MM |
718 | } |
719 | ||
895420dd | 720 | void qp_comm_est(struct rvt_qp *qp) |
77241056 | 721 | { |
54d10c1e | 722 | qp->r_flags |= RVT_R_COMM_EST; |
77241056 MM |
723 | if (qp->ibqp.event_handler) { |
724 | struct ib_event ev; | |
725 | ||
726 | ev.device = qp->ibqp.device; | |
727 | ev.element.qp = &qp->ibqp; | |
728 | ev.event = IB_EVENT_COMM_EST; | |
729 | qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); | |
730 | } | |
731 | } | |
c2f3ffb0 | 732 | |
a2c2d608 DD |
733 | void *qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp, |
734 | gfp_t gfp) | |
735 | { | |
736 | struct hfi1_qp_priv *priv; | |
737 | ||
377f111e | 738 | priv = kzalloc_node(sizeof(*priv), gfp, rdi->dparms.node); |
a2c2d608 DD |
739 | if (!priv) |
740 | return ERR_PTR(-ENOMEM); | |
741 | ||
742 | priv->owner = qp; | |
743 | ||
377f111e | 744 | priv->s_hdr = kzalloc_node(sizeof(*priv->s_hdr), gfp, rdi->dparms.node); |
a2c2d608 DD |
745 | if (!priv->s_hdr) { |
746 | kfree(priv); | |
747 | return ERR_PTR(-ENOMEM); | |
748 | } | |
3c9d149b | 749 | setup_timer(&priv->s_rnr_timer, hfi1_rc_rnr_retry, (unsigned long)qp); |
08279d5c | 750 | qp->s_timer.function = hfi1_rc_timeout; |
a2c2d608 DD |
751 | return priv; |
752 | } | |
753 | ||
754 | void qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp) | |
755 | { | |
756 | struct hfi1_qp_priv *priv = qp->priv; | |
757 | ||
758 | kfree(priv->s_hdr); | |
759 | kfree(priv); | |
760 | } | |
761 | ||
762 | unsigned free_all_qps(struct rvt_dev_info *rdi) | |
763 | { | |
764 | struct hfi1_ibdev *verbs_dev = container_of(rdi, | |
765 | struct hfi1_ibdev, | |
766 | rdi); | |
767 | struct hfi1_devdata *dd = container_of(verbs_dev, | |
768 | struct hfi1_devdata, | |
769 | verbs_dev); | |
770 | int n; | |
771 | unsigned qp_inuse = 0; | |
772 | ||
773 | for (n = 0; n < dd->num_pports; n++) { | |
774 | struct hfi1_ibport *ibp = &dd->pport[n].ibport_data; | |
775 | ||
a2c2d608 DD |
776 | rcu_read_lock(); |
777 | if (rcu_dereference(ibp->rvp.qp[0])) | |
778 | qp_inuse++; | |
779 | if (rcu_dereference(ibp->rvp.qp[1])) | |
780 | qp_inuse++; | |
781 | rcu_read_unlock(); | |
782 | } | |
783 | ||
784 | return qp_inuse; | |
785 | } | |
786 | ||
ec4274f1 DD |
787 | void flush_qp_waiters(struct rvt_qp *qp) |
788 | { | |
789 | flush_iowait(qp); | |
08279d5c | 790 | hfi1_stop_rc_timers(qp); |
ec4274f1 DD |
791 | } |
792 | ||
793 | void stop_send_queue(struct rvt_qp *qp) | |
794 | { | |
795 | struct hfi1_qp_priv *priv = qp->priv; | |
796 | ||
797 | cancel_work_sync(&priv->s_iowait.iowork); | |
08279d5c | 798 | hfi1_del_timers_sync(qp); |
ec4274f1 DD |
799 | } |
800 | ||
801 | void quiesce_qp(struct rvt_qp *qp) | |
802 | { | |
803 | struct hfi1_qp_priv *priv = qp->priv; | |
804 | ||
805 | iowait_sdma_drain(&priv->s_iowait); | |
14553ca1 | 806 | qp_pio_drain(qp); |
ec4274f1 DD |
807 | flush_tx_list(qp); |
808 | } | |
809 | ||
a2c2d608 DD |
810 | void notify_qp_reset(struct rvt_qp *qp) |
811 | { | |
812 | struct hfi1_qp_priv *priv = qp->priv; | |
813 | ||
814 | iowait_init( | |
815 | &priv->s_iowait, | |
816 | 1, | |
83693bd1 | 817 | _hfi1_do_send, |
a2c2d608 | 818 | iowait_sleep, |
a545f530 MM |
819 | iowait_wakeup, |
820 | iowait_sdma_drained); | |
a2c2d608 DD |
821 | priv->r_adefered = 0; |
822 | clear_ahg(qp); | |
823 | } | |
824 | ||
c2f3ffb0 MM |
825 | /* |
826 | * Switch to alternate path. | |
827 | * The QP s_lock should be held and interrupts disabled. | |
828 | */ | |
895420dd | 829 | void hfi1_migrate_qp(struct rvt_qp *qp) |
c2f3ffb0 | 830 | { |
4c6829c5 | 831 | struct hfi1_qp_priv *priv = qp->priv; |
c2f3ffb0 MM |
832 | struct ib_event ev; |
833 | ||
834 | qp->s_mig_state = IB_MIG_MIGRATED; | |
835 | qp->remote_ah_attr = qp->alt_ah_attr; | |
836 | qp->port_num = qp->alt_ah_attr.port_num; | |
837 | qp->s_pkey_index = qp->s_alt_pkey_index; | |
54d10c1e | 838 | qp->s_flags |= RVT_S_AHG_CLEAR; |
4c6829c5 DD |
839 | priv->s_sc = ah_to_sc(qp->ibqp.device, &qp->remote_ah_attr); |
840 | priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc); | |
c2f3ffb0 MM |
841 | |
842 | ev.device = qp->ibqp.device; | |
843 | ev.element.qp = &qp->ibqp; | |
844 | ev.event = IB_EVENT_PATH_MIG; | |
845 | qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); | |
846 | } | |
ec4274f1 DD |
847 | |
848 | int mtu_to_path_mtu(u32 mtu) | |
849 | { | |
850 | return mtu_to_enum(mtu, OPA_MTU_8192); | |
851 | } | |
852 | ||
853 | u32 mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu) | |
854 | { | |
855 | u32 mtu; | |
856 | struct hfi1_ibdev *verbs_dev = container_of(rdi, | |
857 | struct hfi1_ibdev, | |
858 | rdi); | |
859 | struct hfi1_devdata *dd = container_of(verbs_dev, | |
860 | struct hfi1_devdata, | |
861 | verbs_dev); | |
862 | struct hfi1_ibport *ibp; | |
863 | u8 sc, vl; | |
864 | ||
865 | ibp = &dd->pport[qp->port_num - 1].ibport_data; | |
866 | sc = ibp->sl_to_sc[qp->remote_ah_attr.sl]; | |
867 | vl = sc_to_vlt(dd, sc); | |
868 | ||
869 | mtu = verbs_mtu_enum_to_int(qp->ibqp.device, pmtu); | |
870 | if (vl < PER_VL_SEND_CONTEXTS) | |
871 | mtu = min_t(u32, mtu, dd->vld[vl].mtu); | |
872 | return mtu; | |
873 | } | |
874 | ||
875 | int get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp, | |
876 | struct ib_qp_attr *attr) | |
877 | { | |
878 | int mtu, pidx = qp->port_num - 1; | |
879 | struct hfi1_ibdev *verbs_dev = container_of(rdi, | |
880 | struct hfi1_ibdev, | |
881 | rdi); | |
882 | struct hfi1_devdata *dd = container_of(verbs_dev, | |
883 | struct hfi1_devdata, | |
884 | verbs_dev); | |
885 | mtu = verbs_mtu_enum_to_int(qp->ibqp.device, attr->path_mtu); | |
886 | if (mtu == -1) | |
887 | return -1; /* values less than 0 are error */ | |
888 | ||
889 | if (mtu > dd->pport[pidx].ibmtu) | |
890 | return mtu_to_enum(dd->pport[pidx].ibmtu, IB_MTU_2048); | |
891 | else | |
892 | return attr->path_mtu; | |
893 | } | |
894 | ||
895 | void notify_error_qp(struct rvt_qp *qp) | |
896 | { | |
897 | struct hfi1_ibdev *dev = to_idev(qp->ibqp.device); | |
898 | struct hfi1_qp_priv *priv = qp->priv; | |
899 | ||
900 | write_seqlock(&dev->iowait_lock); | |
901 | if (!list_empty(&priv->s_iowait.list) && !(qp->s_flags & RVT_S_BUSY)) { | |
902 | qp->s_flags &= ~RVT_S_ANY_WAIT_IO; | |
903 | list_del_init(&priv->s_iowait.list); | |
904 | if (atomic_dec_and_test(&qp->refcount)) | |
905 | wake_up(&qp->wait); | |
906 | } | |
907 | write_sequnlock(&dev->iowait_lock); | |
908 | ||
909 | if (!(qp->s_flags & RVT_S_BUSY)) { | |
910 | qp->s_hdrwords = 0; | |
911 | if (qp->s_rdma_mr) { | |
912 | rvt_put_mr(qp->s_rdma_mr); | |
913 | qp->s_rdma_mr = NULL; | |
914 | } | |
915 | flush_tx_list(qp); | |
916 | } | |
917 | } | |
918 | ||
0ec79e87 KW |
919 | /** |
920 | * hfi1_error_port_qps - put a port's RC/UC qps into error state | |
921 | * @ibp: the ibport. | |
922 | * @sl: the service level. | |
923 | * | |
924 | * This function places all RC/UC qps with a given service level into error | |
925 | * state. It is generally called to force upper lay apps to abandon stale qps | |
926 | * after an sl->sc mapping change. | |
927 | */ | |
928 | void hfi1_error_port_qps(struct hfi1_ibport *ibp, u8 sl) | |
929 | { | |
930 | struct rvt_qp *qp = NULL; | |
931 | struct hfi1_pportdata *ppd = ppd_from_ibp(ibp); | |
932 | struct hfi1_ibdev *dev = &ppd->dd->verbs_dev; | |
933 | int n; | |
934 | int lastwqe; | |
935 | struct ib_event ev; | |
936 | ||
937 | rcu_read_lock(); | |
938 | ||
939 | /* Deal only with RC/UC qps that use the given SL. */ | |
940 | for (n = 0; n < dev->rdi.qp_dev->qp_table_size; n++) { | |
941 | for (qp = rcu_dereference(dev->rdi.qp_dev->qp_table[n]); qp; | |
942 | qp = rcu_dereference(qp->next)) { | |
943 | if (qp->port_num == ppd->port && | |
944 | (qp->ibqp.qp_type == IB_QPT_UC || | |
945 | qp->ibqp.qp_type == IB_QPT_RC) && | |
946 | qp->remote_ah_attr.sl == sl && | |
947 | (ib_rvt_state_ops[qp->state] & | |
948 | RVT_POST_SEND_OK)) { | |
949 | spin_lock_irq(&qp->r_lock); | |
950 | spin_lock(&qp->s_hlock); | |
951 | spin_lock(&qp->s_lock); | |
952 | lastwqe = rvt_error_qp(qp, | |
953 | IB_WC_WR_FLUSH_ERR); | |
954 | spin_unlock(&qp->s_lock); | |
955 | spin_unlock(&qp->s_hlock); | |
956 | spin_unlock_irq(&qp->r_lock); | |
957 | if (lastwqe) { | |
958 | ev.device = qp->ibqp.device; | |
959 | ev.element.qp = &qp->ibqp; | |
960 | ev.event = | |
961 | IB_EVENT_QP_LAST_WQE_REACHED; | |
962 | qp->ibqp.event_handler(&ev, | |
963 | qp->ibqp.qp_context); | |
964 | } | |
965 | } | |
966 | } | |
967 | } | |
968 | ||
969 | rcu_read_unlock(); | |
970 | } |