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