blob: f96c0f544cb00c514b099f512400db43dde140db [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Mike Marciniszyn2e2ba092018-06-04 11:44:02 -07002 * Copyright(c) 2015 - 2018 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * 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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * 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/spinlock.h>
49
50#include "hfi.h"
51#include "mad.h"
52#include "qp.h"
Mike Marciniszyn45842ab2016-02-14 12:44:34 -080053#include "verbs_txreq.h"
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -080054#include "trace.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040055
Mike Marciniszyn77241052015-07-30 15:17:43 -040056static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
57{
58 return (gid->global.interface_id == id &&
59 (gid->global.subnet_prefix == gid_prefix ||
60 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
61}
62
63/*
64 *
65 * This should be called with the QP r_lock held.
66 *
67 * The s_lock will be acquired around the hfi1_migrate_qp() call.
68 */
Don Hiatt90397462017-05-12 09:20:20 -070069int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -040070{
71 __be64 guid;
72 unsigned long flags;
Don Hiatt90397462017-05-12 09:20:20 -070073 struct rvt_qp *qp = packet->qp;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040074 u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
Don Hiatt90397462017-05-12 09:20:20 -070075 u32 dlid = packet->dlid;
76 u32 slid = packet->slid;
77 u32 sl = packet->sl;
Sebastian Sanchez6d6b8842018-02-01 10:46:23 -080078 bool migrated = packet->migrated;
79 u16 pkey = packet->pkey;
Don Hiatt90397462017-05-12 09:20:20 -070080
81 if (qp->s_mig_state == IB_MIG_ARMED && migrated) {
82 if (!packet->grh) {
Don Hiatt5786adf32017-08-04 13:54:10 -070083 if ((rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
84 IB_AH_GRH) &&
85 (packet->etype != RHF_RCV_TYPE_BYPASS))
Don Hiatt90397462017-05-12 09:20:20 -070086 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -040087 } else {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040088 const struct ib_global_route *grh;
89
90 if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
91 IB_AH_GRH))
Don Hiatt90397462017-05-12 09:20:20 -070092 return 1;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040093 grh = rdma_ah_read_grh(&qp->alt_ah_attr);
94 guid = get_sguid(ibp, grh->sgid_index);
Don Hiatt90397462017-05-12 09:20:20 -070095 if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
Dennis Dalessandro4eb06882016-01-19 14:42:39 -080096 guid))
Don Hiatt90397462017-05-12 09:20:20 -070097 return 1;
Jubin John17fb4f22016-02-14 20:21:52 -080098 if (!gid_ok(
Don Hiatt90397462017-05-12 09:20:20 -070099 &packet->grh->sgid,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400100 grh->dgid.global.subnet_prefix,
101 grh->dgid.global.interface_id))
Don Hiatt90397462017-05-12 09:20:20 -0700102 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400103 }
Don Hiatt5786adf32017-08-04 13:54:10 -0700104 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
Don Hiatt90397462017-05-12 09:20:20 -0700105 sc5, slid))) {
Don Hiatt5786adf32017-08-04 13:54:10 -0700106 hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
107 slid, dlid);
Don Hiatt90397462017-05-12 09:20:20 -0700108 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400109 }
110 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
Don Hiatt90397462017-05-12 09:20:20 -0700111 if (slid != rdma_ah_get_dlid(&qp->alt_ah_attr) ||
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400112 ppd_from_ibp(ibp)->port !=
113 rdma_ah_get_port_num(&qp->alt_ah_attr))
Don Hiatt90397462017-05-12 09:20:20 -0700114 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400115 spin_lock_irqsave(&qp->s_lock, flags);
116 hfi1_migrate_qp(qp);
117 spin_unlock_irqrestore(&qp->s_lock, flags);
118 } else {
Don Hiatt90397462017-05-12 09:20:20 -0700119 if (!packet->grh) {
Don Hiatt5786adf32017-08-04 13:54:10 -0700120 if ((rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
121 IB_AH_GRH) &&
122 (packet->etype != RHF_RCV_TYPE_BYPASS))
Don Hiatt90397462017-05-12 09:20:20 -0700123 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400124 } else {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400125 const struct ib_global_route *grh;
126
127 if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
128 IB_AH_GRH))
Don Hiatt90397462017-05-12 09:20:20 -0700129 return 1;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400130 grh = rdma_ah_read_grh(&qp->remote_ah_attr);
131 guid = get_sguid(ibp, grh->sgid_index);
Don Hiatt90397462017-05-12 09:20:20 -0700132 if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800133 guid))
Don Hiatt90397462017-05-12 09:20:20 -0700134 return 1;
Jubin John17fb4f22016-02-14 20:21:52 -0800135 if (!gid_ok(
Don Hiatt90397462017-05-12 09:20:20 -0700136 &packet->grh->sgid,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400137 grh->dgid.global.subnet_prefix,
138 grh->dgid.global.interface_id))
Don Hiatt90397462017-05-12 09:20:20 -0700139 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400140 }
Don Hiatt5786adf32017-08-04 13:54:10 -0700141 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
Don Hiatt90397462017-05-12 09:20:20 -0700142 sc5, slid))) {
Don Hiatt5786adf32017-08-04 13:54:10 -0700143 hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
144 slid, dlid);
Don Hiatt90397462017-05-12 09:20:20 -0700145 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400146 }
147 /* Validate the SLID. See Ch. 9.6.1.5 */
Don Hiatt90397462017-05-12 09:20:20 -0700148 if ((slid != rdma_ah_get_dlid(&qp->remote_ah_attr)) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400149 ppd_from_ibp(ibp)->port != qp->port_num)
Don Hiatt90397462017-05-12 09:20:20 -0700150 return 1;
151 if (qp->s_mig_state == IB_MIG_REARM && !migrated)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400152 qp->s_mig_state = IB_MIG_ARMED;
153 }
154
155 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400156}
157
158/**
Mike Marciniszyn77241052015-07-30 15:17:43 -0400159 * hfi1_make_grh - construct a GRH header
160 * @ibp: a pointer to the IB port
161 * @hdr: a pointer to the GRH header being constructed
162 * @grh: the global route address to send to
Don Hiatt88733e32017-08-04 13:54:23 -0700163 * @hwords: size of header after grh being sent in dwords
Mike Marciniszyn77241052015-07-30 15:17:43 -0400164 * @nwords: the number of 32 bit words of data being sent
165 *
166 * Return the size of the header in 32 bit words.
167 */
168u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400169 const struct ib_global_route *grh, u32 hwords, u32 nwords)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400170{
171 hdr->version_tclass_flow =
172 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
173 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
174 (grh->flow_label << IB_GRH_FLOW_SHIFT));
Don Hiatt88733e32017-08-04 13:54:23 -0700175 hdr->paylen = cpu_to_be16((hwords + nwords) << 2);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400176 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
177 hdr->next_hdr = IB_GRH_NEXT_HDR;
178 hdr->hop_limit = grh->hop_limit;
179 /* The SGID is 32-bit aligned. */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800180 hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400181 hdr->sgid.global.interface_id =
Jakub Pawlaka6cd5f02016-10-17 04:19:30 -0700182 grh->sgid_index < HFI1_GUIDS_PER_PORT ?
183 get_sguid(ibp, grh->sgid_index) :
184 get_sguid(ibp, HFI1_PORT_GUID_INDEX);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400185 hdr->dgid = grh->dgid;
186
187 /* GRH header size in 32-bit words. */
188 return sizeof(struct ib_grh) / sizeof(u32);
189}
190
Don Hiatt30e07412017-08-04 13:54:04 -0700191#define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, \
192 hdr.ibh.u.oth.bth[2]) / 4)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400193
194/**
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700195 * build_ahg - create ahg in s_ahg
Mike Marciniszyn77241052015-07-30 15:17:43 -0400196 * @qp: a pointer to QP
197 * @npsn: the next PSN for the request/response
198 *
199 * This routine handles the AHG by allocating an ahg entry and causing the
200 * copy of the first middle.
201 *
202 * Subsequent middles use the copied entry, editing the
203 * PSN with 1 or 2 edits.
204 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800205static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400206{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800207 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800208
Mike Marciniszyn2e2ba092018-06-04 11:44:02 -0700209 if (unlikely(qp->s_flags & HFI1_S_AHG_CLEAR))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400210 clear_ahg(qp);
Mike Marciniszyn2e2ba092018-06-04 11:44:02 -0700211 if (!(qp->s_flags & HFI1_S_AHG_VALID)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400212 /* first middle that needs copy */
Mike Marciniszynd7b8ba52015-11-09 19:13:59 -0500213 if (qp->s_ahgidx < 0)
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800214 qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400215 if (qp->s_ahgidx >= 0) {
216 qp->s_ahgpsn = npsn;
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700217 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400218 /* save to protect a change in another thread */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700219 priv->s_ahg->ahgidx = qp->s_ahgidx;
Mike Marciniszyn2e2ba092018-06-04 11:44:02 -0700220 qp->s_flags |= HFI1_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400221 }
222 } else {
223 /* subsequent middle after valid */
224 if (qp->s_ahgidx >= 0) {
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700225 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
226 priv->s_ahg->ahgidx = qp->s_ahgidx;
227 priv->s_ahg->ahgcount++;
228 priv->s_ahg->ahgdesc[0] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400229 sdma_build_ahg_descriptor(
230 (__force u16)cpu_to_be16((u16)npsn),
231 BTH2_OFFSET,
232 16,
233 16);
234 if ((npsn & 0xffff0000) !=
235 (qp->s_ahgpsn & 0xffff0000)) {
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700236 priv->s_ahg->ahgcount++;
237 priv->s_ahg->ahgdesc[1] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400238 sdma_build_ahg_descriptor(
239 (__force u16)cpu_to_be16(
240 (u16)(npsn >> 16)),
241 BTH2_OFFSET,
242 0,
243 16);
244 }
245 }
246 }
247}
248
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700249static inline void hfi1_make_ruc_bth(struct rvt_qp *qp,
250 struct ib_other_headers *ohdr,
251 u32 bth0, u32 bth1, u32 bth2)
252{
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700253 ohdr->bth[0] = cpu_to_be32(bth0);
254 ohdr->bth[1] = cpu_to_be32(bth1);
255 ohdr->bth[2] = cpu_to_be32(bth2);
256}
257
Mike Marciniszyn0a0bcb02018-05-01 05:35:51 -0700258/**
259 * hfi1_make_ruc_header_16B - build a 16B header
260 * @qp: the queue pair
261 * @ohdr: a pointer to the destination header memory
262 * @bth0: bth0 passed in from the RC/UC builder
263 * @bth2: bth2 passed in from the RC/UC builder
264 * @middle: non zero implies indicates ahg "could" be used
265 * @ps: the current packet state
266 *
267 * This routine may disarm ahg under these situations:
268 * - packet needs a GRH
269 * - BECN needed
270 * - migration state not IB_MIG_MIGRATED
271 */
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700272static inline void hfi1_make_ruc_header_16B(struct rvt_qp *qp,
273 struct ib_other_headers *ohdr,
Mitko Haralanov44e43d92019-01-24 06:09:46 -0800274 u32 bth0, u32 bth1, u32 bth2,
275 int middle,
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700276 struct hfi1_pkt_state *ps)
277{
278 struct hfi1_qp_priv *priv = qp->priv;
279 struct hfi1_ibport *ibp = ps->ibp;
280 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700281 u32 slid;
282 u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
283 u8 l4 = OPA_16B_L4_IB_LOCAL;
Mitko Haralanov96362582018-02-01 10:46:07 -0800284 u8 extra_bytes = hfi1_get_16b_padding(
285 (ps->s_txreq->hdr_dwords << 2),
286 ps->s_txreq->s_cur_size);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700287 u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
288 extra_bytes + SIZE_OF_LT) >> 2);
Sebastian Sanchezca85bb12018-02-01 10:46:38 -0800289 bool becn = false;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700290
291 if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
292 hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))) {
293 struct ib_grh *grh;
294 struct ib_global_route *grd =
295 rdma_ah_retrieve_grh(&qp->remote_ah_attr);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700296 /*
297 * Ensure OPA GIDs are transformed to IB gids
298 * before creating the GRH.
299 */
300 if (grd->sgid_index == OPA_GID_INDEX)
301 grd->sgid_index = 0;
302 grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
303 l4 = OPA_16B_L4_IB_GLOBAL;
Mike Marciniszyn78d36332018-02-01 10:52:35 -0800304 ps->s_txreq->hdr_dwords +=
305 hfi1_make_grh(ibp, grh, grd,
306 ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
307 nwords);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700308 middle = 0;
309 }
310
311 if (qp->s_mig_state == IB_MIG_MIGRATED)
312 bth1 |= OPA_BTH_MIG_REQ;
313 else
314 middle = 0;
315
Mike Marciniszyn0a0bcb02018-05-01 05:35:51 -0700316 if (qp->s_flags & RVT_S_ECN) {
317 qp->s_flags &= ~RVT_S_ECN;
318 /* we recently received a FECN, so return a BECN */
319 becn = true;
320 middle = 0;
321 }
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700322 if (middle)
323 build_ahg(qp, bth2);
324 else
Mike Marciniszyn2e2ba092018-06-04 11:44:02 -0700325 qp->s_flags &= ~HFI1_S_AHG_VALID;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700326
327 bth0 |= pkey;
328 bth0 |= extra_bytes << 20;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700329 hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
330
331 if (!ppd->lid)
332 slid = be32_to_cpu(OPA_LID_PERMISSIVE);
333 else
334 slid = ppd->lid |
335 (rdma_ah_get_path_bits(&qp->remote_ah_attr) &
336 ((1 << ppd->lmc) - 1));
337
338 hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
339 slid,
340 opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr),
341 16B),
Mitko Haralanov96362582018-02-01 10:46:07 -0800342 (ps->s_txreq->hdr_dwords + nwords) >> 1,
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700343 pkey, becn, 0, l4, priv->s_sc);
344}
345
Mike Marciniszyn0a0bcb02018-05-01 05:35:51 -0700346/**
347 * hfi1_make_ruc_header_9B - build a 9B header
348 * @qp: the queue pair
349 * @ohdr: a pointer to the destination header memory
350 * @bth0: bth0 passed in from the RC/UC builder
351 * @bth2: bth2 passed in from the RC/UC builder
352 * @middle: non zero implies indicates ahg "could" be used
353 * @ps: the current packet state
354 *
355 * This routine may disarm ahg under these situations:
356 * - packet needs a GRH
357 * - BECN needed
358 * - migration state not IB_MIG_MIGRATED
359 */
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700360static inline void hfi1_make_ruc_header_9B(struct rvt_qp *qp,
361 struct ib_other_headers *ohdr,
Mitko Haralanov44e43d92019-01-24 06:09:46 -0800362 u32 bth0, u32 bth1, u32 bth2,
363 int middle,
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700364 struct hfi1_pkt_state *ps)
365{
366 struct hfi1_qp_priv *priv = qp->priv;
367 struct hfi1_ibport *ibp = ps->ibp;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700368 u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
369 u16 lrh0 = HFI1_LRH_BTH;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700370 u8 extra_bytes = -ps->s_txreq->s_cur_size & 3;
371 u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
372 extra_bytes) >> 2);
373
374 if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
375 struct ib_grh *grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700376
377 lrh0 = HFI1_LRH_GRH;
Mitko Haralanov96362582018-02-01 10:46:07 -0800378 ps->s_txreq->hdr_dwords +=
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700379 hfi1_make_grh(ibp, grh,
380 rdma_ah_read_grh(&qp->remote_ah_attr),
Mike Marciniszyn78d36332018-02-01 10:52:35 -0800381 ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
382 nwords);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700383 middle = 0;
384 }
385 lrh0 |= (priv->s_sc & 0xf) << 12 |
386 (rdma_ah_get_sl(&qp->remote_ah_attr) & 0xf) << 4;
387
388 if (qp->s_mig_state == IB_MIG_MIGRATED)
389 bth0 |= IB_BTH_MIG_REQ;
390 else
391 middle = 0;
392
Mike Marciniszyn0a0bcb02018-05-01 05:35:51 -0700393 if (qp->s_flags & RVT_S_ECN) {
394 qp->s_flags &= ~RVT_S_ECN;
395 /* we recently received a FECN, so return a BECN */
396 bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
397 middle = 0;
398 }
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700399 if (middle)
400 build_ahg(qp, bth2);
401 else
Mike Marciniszyn2e2ba092018-06-04 11:44:02 -0700402 qp->s_flags &= ~HFI1_S_AHG_VALID;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700403
404 bth0 |= pkey;
405 bth0 |= extra_bytes << 20;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700406 hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700407 hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
408 lrh0,
Mitko Haralanov96362582018-02-01 10:46:07 -0800409 ps->s_txreq->hdr_dwords + nwords,
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700410 opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr), 9B),
411 ppd_from_ibp(ibp)->lid |
412 rdma_ah_get_path_bits(&qp->remote_ah_attr));
413}
414
415typedef void (*hfi1_make_ruc_hdr)(struct rvt_qp *qp,
416 struct ib_other_headers *ohdr,
Mitko Haralanov44e43d92019-01-24 06:09:46 -0800417 u32 bth0, u32 bth1, u32 bth2, int middle,
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700418 struct hfi1_pkt_state *ps);
419
420/* We support only two types - 9B and 16B for now */
421static const hfi1_make_ruc_hdr hfi1_ruc_header_tbl[2] = {
422 [HFI1_PKT_TYPE_9B] = &hfi1_make_ruc_header_9B,
423 [HFI1_PKT_TYPE_16B] = &hfi1_make_ruc_header_16B
424};
425
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700426void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
Mitko Haralanov44e43d92019-01-24 06:09:46 -0800427 u32 bth0, u32 bth1, u32 bth2, int middle,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800428 struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400429{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800430 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400431
Mike Marciniszyn77241052015-07-30 15:17:43 -0400432 /*
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700433 * reset s_ahg/AHG fields
Mike Marciniszyn77241052015-07-30 15:17:43 -0400434 *
435 * This insures that the ahgentry/ahgcount
436 * are at a non-AHG default to protect
437 * build_verbs_tx_desc() from using
438 * an include ahgidx.
439 *
440 * build_ahg() will modify as appropriate
441 * to use the AHG feature.
442 */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700443 priv->s_ahg->tx_flags = 0;
444 priv->s_ahg->ahgcount = 0;
445 priv->s_ahg->ahgidx = 0;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700446
447 /* Make the appropriate header */
Mitko Haralanov44e43d92019-01-24 06:09:46 -0800448 hfi1_ruc_header_tbl[priv->hdr_type](qp, ohdr, bth0, bth1, bth2, middle,
449 ps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450}
451
Dean Luickb4219222015-10-26 10:28:35 -0400452/* when sending, force a reschedule every one of these periods */
453#define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
454
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700455/**
456 * schedule_send_yield - test for a yield required for QP send engine
457 * @timeout: Final time for timeout slice for jiffies
458 * @qp: a pointer to QP
459 * @ps: a pointer to a structure with commonly lookup values for
460 * the the send engine progress
461 *
462 * This routine checks if the time slice for the QP has expired
463 * for RC QPs, if so an additional work entry is queued. At this
464 * point, other QPs have an opportunity to be scheduled. It
465 * returns true if a yield is required, otherwise, false
466 * is returned.
467 */
468static bool schedule_send_yield(struct rvt_qp *qp,
469 struct hfi1_pkt_state *ps)
470{
Kaike Wanbcad2912017-07-24 07:45:37 -0700471 ps->pkts_sent = true;
472
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700473 if (unlikely(time_after(jiffies, ps->timeout))) {
474 if (!ps->in_thread ||
475 workqueue_congested(ps->cpu, ps->ppd->hfi1_wq)) {
476 spin_lock_irqsave(&qp->s_lock, ps->flags);
477 qp->s_flags &= ~RVT_S_BUSY;
478 hfi1_schedule_send(qp);
479 spin_unlock_irqrestore(&qp->s_lock, ps->flags);
480 this_cpu_inc(*ps->ppd->dd->send_schedule);
481 trace_hfi1_rc_expired_time_slice(qp, true);
482 return true;
483 }
484
485 cond_resched();
486 this_cpu_inc(*ps->ppd->dd->send_schedule);
487 ps->timeout = jiffies + ps->timeout_int;
488 }
489
490 trace_hfi1_rc_expired_time_slice(qp, false);
491 return false;
492}
493
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700494void hfi1_do_send_from_rvt(struct rvt_qp *qp)
495{
496 hfi1_do_send(qp, false);
497}
498
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800499void _hfi1_do_send(struct work_struct *work)
500{
Dennis Dalessandro5da0fc92018-09-28 07:17:09 -0700501 struct iowait_work *w = container_of(work, struct iowait_work, iowork);
502 struct rvt_qp *qp = iowait_to_qp(w->iow);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800503
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700504 hfi1_do_send(qp, true);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800505}
506
Mike Marciniszyn77241052015-07-30 15:17:43 -0400507/**
508 * hfi1_do_send - perform a send on a QP
509 * @work: contains a pointer to the QP
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700510 * @in_thread: true if in a workqueue thread
Mike Marciniszyn77241052015-07-30 15:17:43 -0400511 *
512 * Process entries in the send work queue until credit or queue is
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700513 * exhausted. Only allow one CPU to send a packet per QP.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400514 * Otherwise, two threads could send packets out of order.
515 */
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700516void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400517{
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500518 struct hfi1_pkt_state ps;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800519 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800520 int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400521
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500522 ps.dev = to_idev(qp->ibqp.device);
523 ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
524 ps.ppd = ppd_from_ibp(ps.ibp);
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700525 ps.in_thread = in_thread;
Dennis Dalessandro5da0fc92018-09-28 07:17:09 -0700526 ps.wait = iowait_get_ib_work(&priv->s_iowait);
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700527
528 trace_hfi1_rc_do_send(qp, in_thread);
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500529
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800530 switch (qp->ibqp.qp_type) {
531 case IB_QPT_RC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400532 if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
533 ~((1 << ps.ppd->lmc) - 1)) ==
534 ps.ppd->lid)) {
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -0700535 rvt_ruc_loopback(qp);
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800536 return;
537 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400538 make_req = hfi1_make_rc_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700539 ps.timeout_int = qp->timeout_jiffies;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800540 break;
541 case IB_QPT_UC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400542 if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
543 ~((1 << ps.ppd->lmc) - 1)) ==
544 ps.ppd->lid)) {
Venkata Sandeep Dhanalakota15703462018-09-26 10:44:52 -0700545 rvt_ruc_loopback(qp);
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800546 return;
547 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400548 make_req = hfi1_make_uc_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700549 ps.timeout_int = SEND_RESCHED_TIMEOUT;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800550 break;
551 default:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400552 make_req = hfi1_make_ud_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700553 ps.timeout_int = SEND_RESCHED_TIMEOUT;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800554 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400555
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700556 spin_lock_irqsave(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400557
558 /* Return if we are already busy processing a work request. */
559 if (!hfi1_send_ok(qp)) {
Dennis Dalessandro5da0fc92018-09-28 07:17:09 -0700560 if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
561 iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_IB);
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700562 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400563 return;
564 }
565
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800566 qp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400567
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700568 ps.timeout_int = ps.timeout_int / 8;
569 ps.timeout = jiffies + ps.timeout_int;
570 ps.cpu = priv->s_sde ? priv->s_sde->cpu :
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800571 cpumask_first(cpumask_of_node(ps.ppd->dd->node));
Kaike Wanbcad2912017-07-24 07:45:37 -0700572 ps.pkts_sent = false;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700573
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800574 /* insure a pre-built packet is handled */
Dennis Dalessandro5da0fc92018-09-28 07:17:09 -0700575 ps.s_txreq = get_waiting_verbs_txreq(ps.wait);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400576 do {
577 /* Check for a constructed packet to be sent. */
Mitko Haralanov96362582018-02-01 10:46:07 -0800578 if (ps.s_txreq) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700579 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400580 /*
581 * If the packet cannot be sent now, return and
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700582 * the send engine will be woken up later.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400583 */
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500584 if (hfi1_verbs_send(qp, &ps))
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800585 return;
Dennis Dalessandro5da0fc92018-09-28 07:17:09 -0700586
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800587 /* allow other tasks to run */
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700588 if (schedule_send_yield(qp, &ps))
589 return;
590
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700591 spin_lock_irqsave(&qp->s_lock, ps.flags);
Dean Luickb4219222015-10-26 10:28:35 -0400592 }
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800593 } while (make_req(qp, &ps));
Kaike Wanbcad2912017-07-24 07:45:37 -0700594 iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700595 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400596}