blob: acd4400b0092d37cbadbd9896bf5c49254b64180 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
David Brazdil0f672f62019-12-10 10:32:29 +00002 * Copyright(c) 2015 - 2019 Intel Corporation.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 *
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
48#include <linux/err.h>
49#include <linux/vmalloc.h>
50#include <linux/hash.h>
51#include <linux/module.h>
52#include <linux/seq_file.h>
53#include <rdma/rdma_vt.h>
54#include <rdma/rdmavt_qp.h>
55#include <rdma/ib_verbs.h>
56
57#include "hfi.h"
58#include "qp.h"
59#include "trace.h"
60#include "verbs_txreq.h"
61
62unsigned int hfi1_qp_table_size = 256;
63module_param_named(qp_table_size, hfi1_qp_table_size, uint, S_IRUGO);
64MODULE_PARM_DESC(qp_table_size, "QP table size");
65
66static void flush_tx_list(struct rvt_qp *qp);
67static int iowait_sleep(
68 struct sdma_engine *sde,
David Brazdil0f672f62019-12-10 10:32:29 +000069 struct iowait_work *wait,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 struct sdma_txreq *stx,
71 unsigned int seq,
72 bool pkts_sent);
73static void iowait_wakeup(struct iowait *wait, int reason);
74static void iowait_sdma_drained(struct iowait *wait);
75static void qp_pio_drain(struct rvt_qp *qp);
76
77const struct rvt_operation_params hfi1_post_parms[RVT_OPERATION_MAX] = {
78[IB_WR_RDMA_WRITE] = {
79 .length = sizeof(struct ib_rdma_wr),
80 .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
81},
82
83[IB_WR_RDMA_READ] = {
84 .length = sizeof(struct ib_rdma_wr),
85 .qpt_support = BIT(IB_QPT_RC),
86 .flags = RVT_OPERATION_ATOMIC,
87},
88
89[IB_WR_ATOMIC_CMP_AND_SWP] = {
90 .length = sizeof(struct ib_atomic_wr),
91 .qpt_support = BIT(IB_QPT_RC),
92 .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
93},
94
95[IB_WR_ATOMIC_FETCH_AND_ADD] = {
96 .length = sizeof(struct ib_atomic_wr),
97 .qpt_support = BIT(IB_QPT_RC),
98 .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
99},
100
101[IB_WR_RDMA_WRITE_WITH_IMM] = {
102 .length = sizeof(struct ib_rdma_wr),
103 .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
104},
105
106[IB_WR_SEND] = {
107 .length = sizeof(struct ib_send_wr),
108 .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
109 BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
110},
111
112[IB_WR_SEND_WITH_IMM] = {
113 .length = sizeof(struct ib_send_wr),
114 .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
115 BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
116},
117
118[IB_WR_REG_MR] = {
119 .length = sizeof(struct ib_reg_wr),
120 .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
121 .flags = RVT_OPERATION_LOCAL,
122},
123
124[IB_WR_LOCAL_INV] = {
125 .length = sizeof(struct ib_send_wr),
126 .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
127 .flags = RVT_OPERATION_LOCAL,
128},
129
130[IB_WR_SEND_WITH_INV] = {
131 .length = sizeof(struct ib_send_wr),
132 .qpt_support = BIT(IB_QPT_RC),
133},
134
David Brazdil0f672f62019-12-10 10:32:29 +0000135[IB_WR_OPFN] = {
136 .length = sizeof(struct ib_atomic_wr),
137 .qpt_support = BIT(IB_QPT_RC),
138 .flags = RVT_OPERATION_USE_RESERVE,
139},
140
141[IB_WR_TID_RDMA_WRITE] = {
142 .length = sizeof(struct ib_rdma_wr),
143 .qpt_support = BIT(IB_QPT_RC),
144 .flags = RVT_OPERATION_IGN_RNR_CNT,
145},
146
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147};
148
David Brazdil0f672f62019-12-10 10:32:29 +0000149static void flush_list_head(struct list_head *l)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150{
David Brazdil0f672f62019-12-10 10:32:29 +0000151 while (!list_empty(l)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000152 struct sdma_txreq *tx;
153
154 tx = list_first_entry(
David Brazdil0f672f62019-12-10 10:32:29 +0000155 l,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156 struct sdma_txreq,
157 list);
158 list_del_init(&tx->list);
159 hfi1_put_txreq(
160 container_of(tx, struct verbs_txreq, txreq));
161 }
162}
163
David Brazdil0f672f62019-12-10 10:32:29 +0000164static void flush_tx_list(struct rvt_qp *qp)
165{
166 struct hfi1_qp_priv *priv = qp->priv;
167
168 flush_list_head(&iowait_get_ib_work(&priv->s_iowait)->tx_head);
169 flush_list_head(&iowait_get_tid_work(&priv->s_iowait)->tx_head);
170}
171
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172static void flush_iowait(struct rvt_qp *qp)
173{
174 struct hfi1_qp_priv *priv = qp->priv;
175 unsigned long flags;
176 seqlock_t *lock = priv->s_iowait.lock;
177
178 if (!lock)
179 return;
180 write_seqlock_irqsave(lock, flags);
181 if (!list_empty(&priv->s_iowait.list)) {
182 list_del_init(&priv->s_iowait.list);
183 priv->s_iowait.lock = NULL;
184 rvt_put_qp(qp);
185 }
186 write_sequnlock_irqrestore(lock, flags);
187}
188
189static inline int opa_mtu_enum_to_int(int mtu)
190{
191 switch (mtu) {
192 case OPA_MTU_8192: return 8192;
193 case OPA_MTU_10240: return 10240;
194 default: return -1;
195 }
196}
197
198/**
199 * This function is what we would push to the core layer if we wanted to be a
200 * "first class citizen". Instead we hide this here and rely on Verbs ULPs
201 * to blindly pass the MTU enum value from the PathRecord to us.
202 */
203static inline int verbs_mtu_enum_to_int(struct ib_device *dev, enum ib_mtu mtu)
204{
205 int val;
206
207 /* Constraining 10KB packets to 8KB packets */
208 if (mtu == (enum ib_mtu)OPA_MTU_10240)
209 mtu = OPA_MTU_8192;
210 val = opa_mtu_enum_to_int((int)mtu);
211 if (val > 0)
212 return val;
213 return ib_mtu_enum_to_int(mtu);
214}
215
216int hfi1_check_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
217 int attr_mask, struct ib_udata *udata)
218{
219 struct ib_qp *ibqp = &qp->ibqp;
220 struct hfi1_ibdev *dev = to_idev(ibqp->device);
221 struct hfi1_devdata *dd = dd_from_dev(dev);
222 u8 sc;
223
224 if (attr_mask & IB_QP_AV) {
225 sc = ah_to_sc(ibqp->device, &attr->ah_attr);
226 if (sc == 0xf)
227 return -EINVAL;
228
229 if (!qp_to_sdma_engine(qp, sc) &&
230 dd->flags & HFI1_HAS_SEND_DMA)
231 return -EINVAL;
232
233 if (!qp_to_send_context(qp, sc))
234 return -EINVAL;
235 }
236
237 if (attr_mask & IB_QP_ALT_PATH) {
238 sc = ah_to_sc(ibqp->device, &attr->alt_ah_attr);
239 if (sc == 0xf)
240 return -EINVAL;
241
242 if (!qp_to_sdma_engine(qp, sc) &&
243 dd->flags & HFI1_HAS_SEND_DMA)
244 return -EINVAL;
245
246 if (!qp_to_send_context(qp, sc))
247 return -EINVAL;
248 }
249
250 return 0;
251}
252
253/*
254 * qp_set_16b - Set the hdr_type based on whether the slid or the
255 * dlid in the connection is extended. Only applicable for RC and UC
256 * QPs. UD QPs determine this on the fly from the ah in the wqe
257 */
258static inline void qp_set_16b(struct rvt_qp *qp)
259{
260 struct hfi1_pportdata *ppd;
261 struct hfi1_ibport *ibp;
262 struct hfi1_qp_priv *priv = qp->priv;
263
264 /* Update ah_attr to account for extended LIDs */
265 hfi1_update_ah_attr(qp->ibqp.device, &qp->remote_ah_attr);
266
267 /* Create 32 bit LIDs */
268 hfi1_make_opa_lid(&qp->remote_ah_attr);
269
270 if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH))
271 return;
272
273 ibp = to_iport(qp->ibqp.device, qp->port_num);
274 ppd = ppd_from_ibp(ibp);
275 priv->hdr_type = hfi1_get_hdr_type(ppd->lid, &qp->remote_ah_attr);
276}
277
278void hfi1_modify_qp(struct rvt_qp *qp, struct ib_qp_attr *attr,
279 int attr_mask, struct ib_udata *udata)
280{
281 struct ib_qp *ibqp = &qp->ibqp;
282 struct hfi1_qp_priv *priv = qp->priv;
283
284 if (attr_mask & IB_QP_AV) {
285 priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
286 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
287 priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
288 qp_set_16b(qp);
289 }
290
291 if (attr_mask & IB_QP_PATH_MIG_STATE &&
292 attr->path_mig_state == IB_MIG_MIGRATED &&
293 qp->s_mig_state == IB_MIG_ARMED) {
294 qp->s_flags |= HFI1_S_AHG_CLEAR;
295 priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
296 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
297 priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
298 qp_set_16b(qp);
299 }
David Brazdil0f672f62019-12-10 10:32:29 +0000300
301 opfn_qp_init(qp, attr, attr_mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302}
303
304/**
David Brazdil0f672f62019-12-10 10:32:29 +0000305 * hfi1_setup_wqe - set up the wqe
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306 * @qp - The qp
307 * @wqe - The built wqe
David Brazdil0f672f62019-12-10 10:32:29 +0000308 * @call_send - Determine if the send should be posted or scheduled.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000309 *
David Brazdil0f672f62019-12-10 10:32:29 +0000310 * Perform setup of the wqe. This is called
311 * prior to inserting the wqe into the ring but after
312 * the wqe has been setup by RDMAVT. This function
313 * allows the driver the opportunity to perform
314 * validation and additional setup of the wqe.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 *
316 * Returns 0 on success, -EINVAL on failure
317 *
318 */
David Brazdil0f672f62019-12-10 10:32:29 +0000319int hfi1_setup_wqe(struct rvt_qp *qp, struct rvt_swqe *wqe, bool *call_send)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000320{
321 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
322 struct rvt_ah *ah;
David Brazdil0f672f62019-12-10 10:32:29 +0000323 struct hfi1_pportdata *ppd;
324 struct hfi1_devdata *dd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325
326 switch (qp->ibqp.qp_type) {
327 case IB_QPT_RC:
David Brazdil0f672f62019-12-10 10:32:29 +0000328 hfi1_setup_tid_rdma_wqe(qp, wqe);
329 /* fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330 case IB_QPT_UC:
331 if (wqe->length > 0x80000000U)
332 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +0000333 if (wqe->length > qp->pmtu)
334 *call_send = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335 break;
336 case IB_QPT_SMI:
David Brazdil0f672f62019-12-10 10:32:29 +0000337 /*
338 * SM packets should exclusively use VL15 and their SL is
339 * ignored (IBTA v1.3, Section 3.5.8.2). Therefore, when ah
340 * is created, SL is 0 in most cases and as a result some
341 * fields (vl and pmtu) in ah may not be set correctly,
342 * depending on the SL2SC and SC2VL tables at the time.
343 */
344 ppd = ppd_from_ibp(ibp);
345 dd = dd_from_ppd(ppd);
346 if (wqe->length > dd->vld[15].mtu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000347 return -EINVAL;
348 break;
349 case IB_QPT_GSI:
350 case IB_QPT_UD:
David Brazdil0f672f62019-12-10 10:32:29 +0000351 ah = rvt_get_swqe_ah(wqe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352 if (wqe->length > (1 << ah->log_pmtu))
353 return -EINVAL;
354 if (ibp->sl_to_sc[rdma_ah_get_sl(&ah->attr)] == 0xf)
355 return -EINVAL;
356 default:
357 break;
358 }
David Brazdil0f672f62019-12-10 10:32:29 +0000359
360 /*
361 * System latency between send and schedule is large enough that
362 * forcing call_send to true for piothreshold packets is necessary.
363 */
364 if (wqe->length <= piothreshold)
365 *call_send = true;
366 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000367}
368
369/**
370 * _hfi1_schedule_send - schedule progress
371 * @qp: the QP
372 *
373 * This schedules qp progress w/o regard to the s_flags.
374 *
375 * It is only used in the post send, which doesn't hold
376 * the s_lock.
377 */
David Brazdil0f672f62019-12-10 10:32:29 +0000378bool _hfi1_schedule_send(struct rvt_qp *qp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000379{
380 struct hfi1_qp_priv *priv = qp->priv;
381 struct hfi1_ibport *ibp =
382 to_iport(qp->ibqp.device, qp->port_num);
383 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
Olivier Deprez0e641232021-09-23 10:07:05 +0200384 struct hfi1_devdata *dd = ppd->dd;
385
386 if (dd->flags & HFI1_SHUTDOWN)
387 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388
David Brazdil0f672f62019-12-10 10:32:29 +0000389 return iowait_schedule(&priv->s_iowait, ppd->hfi1_wq,
390 priv->s_sde ?
391 priv->s_sde->cpu :
392 cpumask_first(cpumask_of_node(dd->node)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393}
394
395static void qp_pio_drain(struct rvt_qp *qp)
396{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000397 struct hfi1_qp_priv *priv = qp->priv;
398
399 if (!priv->s_sendcontext)
400 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000401 while (iowait_pio_pending(&priv->s_iowait)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000402 write_seqlock_irq(&priv->s_sendcontext->waitlock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000403 hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 1);
David Brazdil0f672f62019-12-10 10:32:29 +0000404 write_sequnlock_irq(&priv->s_sendcontext->waitlock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000405 iowait_pio_drain(&priv->s_iowait);
David Brazdil0f672f62019-12-10 10:32:29 +0000406 write_seqlock_irq(&priv->s_sendcontext->waitlock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000407 hfi1_sc_wantpiobuf_intr(priv->s_sendcontext, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000408 write_sequnlock_irq(&priv->s_sendcontext->waitlock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000409 }
410}
411
412/**
413 * hfi1_schedule_send - schedule progress
414 * @qp: the QP
415 *
416 * This schedules qp progress and caller should hold
417 * the s_lock.
David Brazdil0f672f62019-12-10 10:32:29 +0000418 * @return true if the first leg is scheduled;
419 * false if the first leg is not scheduled.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000420 */
David Brazdil0f672f62019-12-10 10:32:29 +0000421bool hfi1_schedule_send(struct rvt_qp *qp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422{
423 lockdep_assert_held(&qp->s_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000424 if (hfi1_send_ok(qp)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000425 _hfi1_schedule_send(qp);
David Brazdil0f672f62019-12-10 10:32:29 +0000426 return true;
427 }
428 if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
429 iowait_set_flag(&((struct hfi1_qp_priv *)qp->priv)->s_iowait,
430 IOWAIT_PENDING_IB);
431 return false;
432}
433
434static void hfi1_qp_schedule(struct rvt_qp *qp)
435{
436 struct hfi1_qp_priv *priv = qp->priv;
437 bool ret;
438
439 if (iowait_flag_set(&priv->s_iowait, IOWAIT_PENDING_IB)) {
440 ret = hfi1_schedule_send(qp);
441 if (ret)
442 iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_IB);
443 }
444 if (iowait_flag_set(&priv->s_iowait, IOWAIT_PENDING_TID)) {
445 ret = hfi1_schedule_tid_send(qp);
446 if (ret)
447 iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
448 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449}
450
451void hfi1_qp_wakeup(struct rvt_qp *qp, u32 flag)
452{
453 unsigned long flags;
454
455 spin_lock_irqsave(&qp->s_lock, flags);
456 if (qp->s_flags & flag) {
457 qp->s_flags &= ~flag;
458 trace_hfi1_qpwakeup(qp, flag);
David Brazdil0f672f62019-12-10 10:32:29 +0000459 hfi1_qp_schedule(qp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000460 }
461 spin_unlock_irqrestore(&qp->s_lock, flags);
462 /* Notify hfi1_destroy_qp() if it is waiting. */
463 rvt_put_qp(qp);
464}
465
David Brazdil0f672f62019-12-10 10:32:29 +0000466void hfi1_qp_unbusy(struct rvt_qp *qp, struct iowait_work *wait)
467{
468 struct hfi1_qp_priv *priv = qp->priv;
469
470 if (iowait_set_work_flag(wait) == IOWAIT_IB_SE) {
471 qp->s_flags &= ~RVT_S_BUSY;
472 /*
473 * If we are sending a first-leg packet from the second leg,
474 * we need to clear the busy flag from priv->s_flags to
475 * avoid a race condition when the qp wakes up before
476 * the call to hfi1_verbs_send() returns to the second
477 * leg. In that case, the second leg will terminate without
478 * being re-scheduled, resulting in failure to send TID RDMA
479 * WRITE DATA and TID RDMA ACK packets.
480 */
481 if (priv->s_flags & HFI1_S_TID_BUSY_SET) {
482 priv->s_flags &= ~(HFI1_S_TID_BUSY_SET |
483 RVT_S_BUSY);
484 iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
485 }
486 } else {
487 priv->s_flags &= ~RVT_S_BUSY;
488 }
489}
490
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491static int iowait_sleep(
492 struct sdma_engine *sde,
David Brazdil0f672f62019-12-10 10:32:29 +0000493 struct iowait_work *wait,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000494 struct sdma_txreq *stx,
495 uint seq,
496 bool pkts_sent)
497{
498 struct verbs_txreq *tx = container_of(stx, struct verbs_txreq, txreq);
499 struct rvt_qp *qp;
500 struct hfi1_qp_priv *priv;
501 unsigned long flags;
502 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503
504 qp = tx->qp;
505 priv = qp->priv;
506
507 spin_lock_irqsave(&qp->s_lock, flags);
508 if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
509 /*
510 * If we couldn't queue the DMA request, save the info
511 * and try again later rather than destroying the
512 * buffer and undoing the side effects of the copy.
513 */
514 /* Make a common routine? */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000515 list_add_tail(&stx->list, &wait->tx_head);
David Brazdil0f672f62019-12-10 10:32:29 +0000516 write_seqlock(&sde->waitlock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517 if (sdma_progress(sde, seq, stx))
518 goto eagain;
519 if (list_empty(&priv->s_iowait.list)) {
520 struct hfi1_ibport *ibp =
521 to_iport(qp->ibqp.device, qp->port_num);
522
523 ibp->rvp.n_dmawait++;
524 qp->s_flags |= RVT_S_WAIT_DMA_DESC;
David Brazdil0f672f62019-12-10 10:32:29 +0000525 iowait_get_priority(&priv->s_iowait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000526 iowait_queue(pkts_sent, &priv->s_iowait,
527 &sde->dmawait);
David Brazdil0f672f62019-12-10 10:32:29 +0000528 priv->s_iowait.lock = &sde->waitlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000529 trace_hfi1_qpsleep(qp, RVT_S_WAIT_DMA_DESC);
530 rvt_get_qp(qp);
531 }
David Brazdil0f672f62019-12-10 10:32:29 +0000532 write_sequnlock(&sde->waitlock);
533 hfi1_qp_unbusy(qp, wait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 spin_unlock_irqrestore(&qp->s_lock, flags);
535 ret = -EBUSY;
536 } else {
537 spin_unlock_irqrestore(&qp->s_lock, flags);
538 hfi1_put_txreq(tx);
539 }
540 return ret;
541eagain:
David Brazdil0f672f62019-12-10 10:32:29 +0000542 write_sequnlock(&sde->waitlock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000543 spin_unlock_irqrestore(&qp->s_lock, flags);
544 list_del_init(&stx->list);
545 return -EAGAIN;
546}
547
548static void iowait_wakeup(struct iowait *wait, int reason)
549{
550 struct rvt_qp *qp = iowait_to_qp(wait);
551
552 WARN_ON(reason != SDMA_AVAIL_REASON);
553 hfi1_qp_wakeup(qp, RVT_S_WAIT_DMA_DESC);
554}
555
556static void iowait_sdma_drained(struct iowait *wait)
557{
558 struct rvt_qp *qp = iowait_to_qp(wait);
559 unsigned long flags;
560
561 /*
562 * This happens when the send engine notes
563 * a QP in the error state and cannot
564 * do the flush work until that QP's
565 * sdma work has finished.
566 */
567 spin_lock_irqsave(&qp->s_lock, flags);
568 if (qp->s_flags & RVT_S_WAIT_DMA) {
569 qp->s_flags &= ~RVT_S_WAIT_DMA;
570 hfi1_schedule_send(qp);
571 }
572 spin_unlock_irqrestore(&qp->s_lock, flags);
573}
574
David Brazdil0f672f62019-12-10 10:32:29 +0000575static void hfi1_init_priority(struct iowait *w)
576{
577 struct rvt_qp *qp = iowait_to_qp(w);
578 struct hfi1_qp_priv *priv = qp->priv;
579
580 if (qp->s_flags & RVT_S_ACK_PENDING)
581 w->priority++;
582 if (priv->s_flags & RVT_S_ACK_PENDING)
583 w->priority++;
584}
585
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000586/**
587 * qp_to_sdma_engine - map a qp to a send engine
588 * @qp: the QP
589 * @sc5: the 5 bit sc
590 *
591 * Return:
592 * A send engine for the qp or NULL for SMI type qp.
593 */
594struct sdma_engine *qp_to_sdma_engine(struct rvt_qp *qp, u8 sc5)
595{
596 struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
597 struct sdma_engine *sde;
598
599 if (!(dd->flags & HFI1_HAS_SEND_DMA))
600 return NULL;
601 switch (qp->ibqp.qp_type) {
602 case IB_QPT_SMI:
603 return NULL;
604 default:
605 break;
606 }
607 sde = sdma_select_engine_sc(dd, qp->ibqp.qp_num >> dd->qos_shift, sc5);
608 return sde;
609}
610
611/*
612 * qp_to_send_context - map a qp to a send context
613 * @qp: the QP
614 * @sc5: the 5 bit sc
615 *
616 * Return:
617 * A send context for the qp
618 */
619struct send_context *qp_to_send_context(struct rvt_qp *qp, u8 sc5)
620{
621 struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
622
623 switch (qp->ibqp.qp_type) {
624 case IB_QPT_SMI:
625 /* SMA packets to VL15 */
626 return dd->vld[15].sc;
627 default:
628 break;
629 }
630
631 return pio_select_send_context_sc(dd, qp->ibqp.qp_num >> dd->qos_shift,
632 sc5);
633}
634
635static const char * const qp_type_str[] = {
636 "SMI", "GSI", "RC", "UC", "UD",
637};
638
639static int qp_idle(struct rvt_qp *qp)
640{
641 return
642 qp->s_last == qp->s_acked &&
643 qp->s_acked == qp->s_cur &&
644 qp->s_cur == qp->s_tail &&
645 qp->s_tail == qp->s_head;
646}
647
648/**
649 * qp_iter_print - print the qp information to seq_file
650 * @s: the seq_file to emit the qp information on
651 * @iter: the iterator for the qp hash list
652 */
653void qp_iter_print(struct seq_file *s, struct rvt_qp_iter *iter)
654{
655 struct rvt_swqe *wqe;
656 struct rvt_qp *qp = iter->qp;
657 struct hfi1_qp_priv *priv = qp->priv;
658 struct sdma_engine *sde;
659 struct send_context *send_context;
660 struct rvt_ack_entry *e = NULL;
661 struct rvt_srq *srq = qp->ibqp.srq ?
662 ibsrq_to_rvtsrq(qp->ibqp.srq) : NULL;
663
664 sde = qp_to_sdma_engine(qp, priv->s_sc);
665 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
666 send_context = qp_to_send_context(qp, priv->s_sc);
667 if (qp->s_ack_queue)
668 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
669 seq_printf(s,
670 "N %d %s QP %x R %u %s %u %u f=%x %u %u %u %u %u %u SPSN %x %x %x %x %x RPSN %x S(%u %u %u %u %u %u %u) R(%u %u %u) RQP %x LID %x SL %u MTU %u %u %u %u %u SDE %p,%u SC %p,%u SCQ %u %u PID %d OS %x %x E %x %x %x RNR %d %s %d\n",
671 iter->n,
672 qp_idle(qp) ? "I" : "B",
673 qp->ibqp.qp_num,
674 atomic_read(&qp->refcount),
675 qp_type_str[qp->ibqp.qp_type],
676 qp->state,
677 wqe ? wqe->wr.opcode : 0,
678 qp->s_flags,
679 iowait_sdma_pending(&priv->s_iowait),
680 iowait_pio_pending(&priv->s_iowait),
681 !list_empty(&priv->s_iowait.list),
682 qp->timeout,
683 wqe ? wqe->ssn : 0,
684 qp->s_lsn,
685 qp->s_last_psn,
686 qp->s_psn, qp->s_next_psn,
687 qp->s_sending_psn, qp->s_sending_hpsn,
688 qp->r_psn,
689 qp->s_last, qp->s_acked, qp->s_cur,
690 qp->s_tail, qp->s_head, qp->s_size,
691 qp->s_avail,
692 /* ack_queue ring pointers, size */
693 qp->s_tail_ack_queue, qp->r_head_ack_queue,
694 rvt_max_atomic(&to_idev(qp->ibqp.device)->rdi),
695 /* remote QP info */
696 qp->remote_qpn,
697 rdma_ah_get_dlid(&qp->remote_ah_attr),
698 rdma_ah_get_sl(&qp->remote_ah_attr),
699 qp->pmtu,
700 qp->s_retry,
701 qp->s_retry_cnt,
702 qp->s_rnr_retry_cnt,
703 qp->s_rnr_retry,
704 sde,
705 sde ? sde->this_idx : 0,
706 send_context,
707 send_context ? send_context->sw_index : 0,
David Brazdil0f672f62019-12-10 10:32:29 +0000708 ib_cq_head(qp->ibqp.send_cq),
709 ib_cq_tail(qp->ibqp.send_cq),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000710 qp->pid,
711 qp->s_state,
712 qp->s_ack_state,
713 /* ack queue information */
714 e ? e->opcode : 0,
715 e ? e->psn : 0,
716 e ? e->lpsn : 0,
717 qp->r_min_rnr_timer,
718 srq ? "SRQ" : "RQ",
719 srq ? srq->rq.size : qp->r_rq.size
720 );
721}
722
723void *qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp)
724{
725 struct hfi1_qp_priv *priv;
726
727 priv = kzalloc_node(sizeof(*priv), GFP_KERNEL, rdi->dparms.node);
728 if (!priv)
729 return ERR_PTR(-ENOMEM);
730
731 priv->owner = qp;
732
733 priv->s_ahg = kzalloc_node(sizeof(*priv->s_ahg), GFP_KERNEL,
734 rdi->dparms.node);
735 if (!priv->s_ahg) {
736 kfree(priv);
737 return ERR_PTR(-ENOMEM);
738 }
739 iowait_init(
740 &priv->s_iowait,
741 1,
742 _hfi1_do_send,
David Brazdil0f672f62019-12-10 10:32:29 +0000743 _hfi1_do_tid_send,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000744 iowait_sleep,
745 iowait_wakeup,
David Brazdil0f672f62019-12-10 10:32:29 +0000746 iowait_sdma_drained,
747 hfi1_init_priority);
748 /* Init to a value to start the running average correctly */
749 priv->s_running_pkt_size = piothreshold / 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000750 return priv;
751}
752
753void qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
754{
755 struct hfi1_qp_priv *priv = qp->priv;
756
David Brazdil0f672f62019-12-10 10:32:29 +0000757 hfi1_qp_priv_tid_free(rdi, qp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000758 kfree(priv->s_ahg);
759 kfree(priv);
760}
761
762unsigned 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
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
787void flush_qp_waiters(struct rvt_qp *qp)
788{
789 lockdep_assert_held(&qp->s_lock);
790 flush_iowait(qp);
David Brazdil0f672f62019-12-10 10:32:29 +0000791 hfi1_tid_rdma_flush_wait(qp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000792}
793
794void stop_send_queue(struct rvt_qp *qp)
795{
796 struct hfi1_qp_priv *priv = qp->priv;
797
David Brazdil0f672f62019-12-10 10:32:29 +0000798 iowait_cancel_work(&priv->s_iowait);
799 if (cancel_work_sync(&priv->tid_rdma.trigger_work))
800 rvt_put_qp(qp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000801}
802
803void quiesce_qp(struct rvt_qp *qp)
804{
805 struct hfi1_qp_priv *priv = qp->priv;
806
David Brazdil0f672f62019-12-10 10:32:29 +0000807 hfi1_del_tid_reap_timer(qp);
808 hfi1_del_tid_retry_timer(qp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000809 iowait_sdma_drain(&priv->s_iowait);
810 qp_pio_drain(qp);
811 flush_tx_list(qp);
812}
813
814void notify_qp_reset(struct rvt_qp *qp)
815{
David Brazdil0f672f62019-12-10 10:32:29 +0000816 hfi1_qp_kern_exp_rcv_clear_all(qp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000817 qp->r_adefered = 0;
818 clear_ahg(qp);
David Brazdil0f672f62019-12-10 10:32:29 +0000819
820 /* Clear any OPFN state */
821 if (qp->ibqp.qp_type == IB_QPT_RC)
822 opfn_conn_error(qp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000823}
824
825/*
826 * Switch to alternate path.
827 * The QP s_lock should be held and interrupts disabled.
828 */
829void hfi1_migrate_qp(struct rvt_qp *qp)
830{
831 struct hfi1_qp_priv *priv = qp->priv;
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 = rdma_ah_get_port_num(&qp->alt_ah_attr);
837 qp->s_pkey_index = qp->s_alt_pkey_index;
838 qp->s_flags |= HFI1_S_AHG_CLEAR;
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);
841 qp_set_16b(qp);
842
843 ev.device = qp->ibqp.device;
844 ev.element.qp = &qp->ibqp;
845 ev.event = IB_EVENT_PATH_MIG;
846 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
847}
848
849int mtu_to_path_mtu(u32 mtu)
850{
851 return mtu_to_enum(mtu, OPA_MTU_8192);
852}
853
854u32 mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
855{
856 u32 mtu;
857 struct hfi1_ibdev *verbs_dev = container_of(rdi,
858 struct hfi1_ibdev,
859 rdi);
860 struct hfi1_devdata *dd = container_of(verbs_dev,
861 struct hfi1_devdata,
862 verbs_dev);
863 struct hfi1_ibport *ibp;
864 u8 sc, vl;
865
866 ibp = &dd->pport[qp->port_num - 1].ibport_data;
867 sc = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
868 vl = sc_to_vlt(dd, sc);
869
870 mtu = verbs_mtu_enum_to_int(qp->ibqp.device, pmtu);
871 if (vl < PER_VL_SEND_CONTEXTS)
872 mtu = min_t(u32, mtu, dd->vld[vl].mtu);
873 return mtu;
874}
875
876int get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
877 struct ib_qp_attr *attr)
878{
879 int mtu, pidx = qp->port_num - 1;
880 struct hfi1_ibdev *verbs_dev = container_of(rdi,
881 struct hfi1_ibdev,
882 rdi);
883 struct hfi1_devdata *dd = container_of(verbs_dev,
884 struct hfi1_devdata,
885 verbs_dev);
886 mtu = verbs_mtu_enum_to_int(qp->ibqp.device, attr->path_mtu);
887 if (mtu == -1)
888 return -1; /* values less than 0 are error */
889
890 if (mtu > dd->pport[pidx].ibmtu)
891 return mtu_to_enum(dd->pport[pidx].ibmtu, IB_MTU_2048);
892 else
893 return attr->path_mtu;
894}
895
896void notify_error_qp(struct rvt_qp *qp)
897{
898 struct hfi1_qp_priv *priv = qp->priv;
899 seqlock_t *lock = priv->s_iowait.lock;
900
901 if (lock) {
902 write_seqlock(lock);
903 if (!list_empty(&priv->s_iowait.list) &&
David Brazdil0f672f62019-12-10 10:32:29 +0000904 !(qp->s_flags & RVT_S_BUSY) &&
905 !(priv->s_flags & RVT_S_BUSY)) {
906 qp->s_flags &= ~HFI1_S_ANY_WAIT_IO;
907 iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_IB);
908 iowait_clear_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000909 list_del_init(&priv->s_iowait.list);
910 priv->s_iowait.lock = NULL;
911 rvt_put_qp(qp);
912 }
913 write_sequnlock(lock);
914 }
915
David Brazdil0f672f62019-12-10 10:32:29 +0000916 if (!(qp->s_flags & RVT_S_BUSY) && !(priv->s_flags & RVT_S_BUSY)) {
917 qp->s_hdrwords = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000918 if (qp->s_rdma_mr) {
919 rvt_put_mr(qp->s_rdma_mr);
920 qp->s_rdma_mr = NULL;
921 }
922 flush_tx_list(qp);
923 }
924}
925
926/**
927 * hfi1_qp_iter_cb - callback for iterator
928 * @qp - the qp
929 * @v - the sl in low bits of v
930 *
931 * This is called from the iterator callback to work
932 * on an individual qp.
933 */
934static void hfi1_qp_iter_cb(struct rvt_qp *qp, u64 v)
935{
936 int lastwqe;
937 struct ib_event ev;
938 struct hfi1_ibport *ibp =
939 to_iport(qp->ibqp.device, qp->port_num);
940 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
941 u8 sl = (u8)v;
942
943 if (qp->port_num != ppd->port ||
944 (qp->ibqp.qp_type != IB_QPT_UC &&
945 qp->ibqp.qp_type != IB_QPT_RC) ||
946 rdma_ah_get_sl(&qp->remote_ah_attr) != sl ||
947 !(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))
948 return;
949
950 spin_lock_irq(&qp->r_lock);
951 spin_lock(&qp->s_hlock);
952 spin_lock(&qp->s_lock);
953 lastwqe = rvt_error_qp(qp, 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 = IB_EVENT_QP_LAST_WQE_REACHED;
961 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
962 }
963}
964
965/**
966 * hfi1_error_port_qps - put a port's RC/UC qps into error state
967 * @ibp: the ibport.
968 * @sl: the service level.
969 *
970 * This function places all RC/UC qps with a given service level into error
971 * state. It is generally called to force upper lay apps to abandon stale qps
972 * after an sl->sc mapping change.
973 */
974void hfi1_error_port_qps(struct hfi1_ibport *ibp, u8 sl)
975{
976 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
977 struct hfi1_ibdev *dev = &ppd->dd->verbs_dev;
978
979 rvt_qp_iter(&dev->rdi, sl, hfi1_qp_iter_cb);
980}