2 * Copyright(c) 2015, 2016 Intel Corporation.
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
48 #include <linux/net.h>
49 #include <rdma/ib_smi.h>
53 #include "verbs_txreq.h"
56 /* We support only two types - 9B and 16B for now */
57 static const hfi1_make_req hfi1_make_ud_req_tbl[2] = {
58 [HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B,
59 [HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B
63 * ud_loopback - handle send on loopback QPs
64 * @sqp: the sending QP
65 * @swqe: the send work request
67 * This is called from hfi1_make_ud_req() to forward a WQE addressed
69 * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
70 * while this is being called.
72 static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
74 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
75 struct hfi1_pportdata *ppd;
76 struct hfi1_qp_priv *priv = sqp->priv;
78 struct rdma_ah_attr *ah_attr;
80 struct rvt_sge_state ssge;
84 enum ib_qp_type sqptype, dqptype;
88 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
89 swqe->ud_wr.remote_qpn);
91 ibp->rvp.n_pkt_drops++;
96 sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
97 IB_QPT_UD : sqp->ibqp.qp_type;
98 dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
99 IB_QPT_UD : qp->ibqp.qp_type;
101 if (dqptype != sqptype ||
102 !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
103 ibp->rvp.n_pkt_drops++;
107 ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
108 ppd = ppd_from_ibp(ibp);
110 if (qp->ibqp.qp_num > 1) {
113 u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
115 pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
116 slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
117 ((1 << ppd->lmc) - 1));
118 if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
121 hfi1_bad_pkey(ibp, pkey,
122 rdma_ah_get_sl(ah_attr),
123 sqp->ibqp.qp_num, qp->ibqp.qp_num,
124 slid, rdma_ah_get_dlid(ah_attr));
130 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
131 * Qkeys with the high order bit set mean use the
132 * qkey from the QP context instead of the WR (see 10.2.5).
134 if (qp->ibqp.qp_num) {
137 qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
138 sqp->qkey : swqe->ud_wr.remote_qkey;
139 if (unlikely(qkey != qp->qkey))
140 goto drop; /* silently drop per IBTA spec */
144 * A GRH is expected to precede the data even if not
145 * present on the wire.
147 length = swqe->length;
148 memset(&wc, 0, sizeof(wc));
149 wc.byte_len = length + sizeof(struct ib_grh);
151 if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
152 wc.wc_flags = IB_WC_WITH_IMM;
153 wc.ex.imm_data = swqe->wr.ex.imm_data;
156 spin_lock_irqsave(&qp->r_lock, flags);
159 * Get the next work request entry to find where to put the data.
161 if (qp->r_flags & RVT_R_REUSE_SGE) {
162 qp->r_flags &= ~RVT_R_REUSE_SGE;
166 ret = hfi1_rvt_get_rwqe(qp, 0);
168 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
172 if (qp->ibqp.qp_num == 0)
173 ibp->rvp.n_vl15_dropped++;
177 /* Silently drop packets which are too big. */
178 if (unlikely(wc.byte_len > qp->r_len)) {
179 qp->r_flags |= RVT_R_REUSE_SGE;
180 ibp->rvp.n_pkt_drops++;
184 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
186 struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));
189 * For loopback packets with extended LIDs, the
190 * sgid_index in the GRH is 0 and the dgid is
191 * OPA GID of the sender. While creating a response
192 * to the loopback packet, IB core creates the new
193 * sgid_index from the DGID and that will be the
194 * OPA_GID_INDEX. The new dgid is from the sgid
195 * index and that will be in the IB GID format.
197 * We now have a case where the sent packet had a
198 * different sgid_index and dgid compared to the
199 * one that was received in response.
201 * Fix this inconsistency.
203 if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
204 if (grd.sgid_index == 0)
205 grd.sgid_index = OPA_GID_INDEX;
207 if (ib_is_opa_gid(&grd.dgid))
208 grd.dgid.global.interface_id =
209 cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
212 hfi1_make_grh(ibp, &grh, &grd, 0, 0);
213 hfi1_copy_sge(&qp->r_sge, &grh,
214 sizeof(grh), true, false);
215 wc.wc_flags |= IB_WC_GRH;
217 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
219 ssge.sg_list = swqe->sg_list + 1;
220 ssge.sge = *swqe->sg_list;
221 ssge.num_sge = swqe->wr.num_sge;
224 u32 len = sge->length;
228 if (len > sge->sge_length)
229 len = sge->sge_length;
230 WARN_ON_ONCE(len == 0);
231 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, true, false);
234 sge->sge_length -= len;
235 if (sge->sge_length == 0) {
237 *sge = *ssge.sg_list++;
238 } else if (sge->length == 0 && sge->mr->lkey) {
239 if (++sge->n >= RVT_SEGSZ) {
240 if (++sge->m >= sge->mr->mapsz)
245 sge->mr->map[sge->m]->segs[sge->n].vaddr;
247 sge->mr->map[sge->m]->segs[sge->n].length;
251 rvt_put_ss(&qp->r_sge);
252 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
254 wc.wr_id = qp->r_wr_id;
255 wc.status = IB_WC_SUCCESS;
256 wc.opcode = IB_WC_RECV;
258 wc.src_qp = sqp->ibqp.qp_num;
259 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
260 if (sqp->ibqp.qp_type == IB_QPT_GSI ||
261 sqp->ibqp.qp_type == IB_QPT_SMI)
262 wc.pkey_index = swqe->ud_wr.pkey_index;
264 wc.pkey_index = sqp->s_pkey_index;
268 wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
269 ((1 << ppd->lmc) - 1))) & U16_MAX;
270 /* Check for loopback when the port lid is not set */
271 if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
272 wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
273 wc.sl = rdma_ah_get_sl(ah_attr);
274 wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
275 wc.port_num = qp->port_num;
276 /* Signal completion event if the solicited bit is set. */
277 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
278 swqe->wr.send_flags & IB_SEND_SOLICITED);
279 ibp->rvp.n_loop_pkts++;
281 spin_unlock_irqrestore(&qp->r_lock, flags);
286 static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
287 struct ib_other_headers *ohdr,
288 u16 *pkey, u32 extra_bytes, bool bypass)
291 struct hfi1_ibport *ibp;
293 ibp = to_iport(qp->ibqp.device, qp->port_num);
294 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
295 ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
296 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
298 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
301 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
302 bth0 |= IB_BTH_SOLICITED;
303 bth0 |= extra_bytes << 20;
304 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
305 *pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
307 *pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
310 ohdr->bth[0] = cpu_to_be32(bth0);
311 ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
312 ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
314 * Qkeys with the high order bit set mean use the
315 * qkey from the QP context instead of the WR (see 10.2.5).
317 ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
318 qp->qkey : wqe->ud_wr.remote_qkey);
319 ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
322 void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
323 struct rvt_swqe *wqe)
325 u32 nwords, extra_bytes;
326 u16 len, slid, dlid, pkey;
329 struct hfi1_qp_priv *priv = qp->priv;
330 struct ib_other_headers *ohdr;
331 struct rdma_ah_attr *ah_attr;
332 struct hfi1_pportdata *ppd;
333 struct hfi1_ibport *ibp;
336 ibp = to_iport(qp->ibqp.device, qp->port_num);
337 ppd = ppd_from_ibp(ibp);
338 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
340 extra_bytes = -wqe->length & 3;
341 nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
342 /* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
344 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
347 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
348 grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
349 qp->s_hdrwords += hfi1_make_grh(ibp, grh,
350 rdma_ah_read_grh(ah_attr),
351 qp->s_hdrwords - 2, nwords);
353 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
356 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
359 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
360 lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
361 if (qp->ibqp.qp_type == IB_QPT_SMI) {
362 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
365 lrh0 |= (sc5 & 0xf) << 12;
369 dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
370 if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
371 slid = be16_to_cpu(IB_LID_PERMISSIVE);
373 u16 lid = (u16)ppd->lid;
376 lid |= rdma_ah_get_path_bits(ah_attr) &
377 ((1 << ppd->lmc) - 1);
380 slid = be16_to_cpu(IB_LID_PERMISSIVE);
383 hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
384 len = qp->s_hdrwords + nwords;
386 /* Setup the packet */
387 ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
388 hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
389 lrh0, len, dlid, slid);
392 void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
393 struct rvt_swqe *wqe)
395 struct hfi1_qp_priv *priv = qp->priv;
396 struct ib_other_headers *ohdr;
397 struct rdma_ah_attr *ah_attr;
398 struct hfi1_pportdata *ppd;
399 struct hfi1_ibport *ibp;
400 u32 dlid, slid, nwords, extra_bytes;
404 ibp = to_iport(qp->ibqp.device, qp->port_num);
405 ppd = ppd_from_ibp(ibp);
406 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
407 /* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */
409 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
412 /* SW provides space for CRC and LT for bypass packets. */
413 extra_bytes = hfi1_get_16b_padding((qp->s_hdrwords << 2),
415 nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
417 if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
418 hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
420 struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
422 * Ensure OPA GIDs are transformed to IB gids
423 * before creating the GRH.
425 if (grd->sgid_index == OPA_GID_INDEX) {
426 dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
430 grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
431 qp->s_hdrwords += hfi1_make_grh(ibp, grh, grd,
432 qp->s_hdrwords - 4, nwords);
433 ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
434 l4 = OPA_16B_L4_IB_GLOBAL;
436 ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
437 l4 = OPA_16B_L4_IB_LOCAL;
440 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
441 if (qp->ibqp.qp_type == IB_QPT_SMI)
446 dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
448 slid = be32_to_cpu(OPA_LID_PERMISSIVE);
450 slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
451 ((1 << ppd->lmc) - 1));
453 hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
454 /* Convert dwords to flits */
455 len = (qp->s_hdrwords + nwords) >> 1;
457 /* Setup the packet */
458 ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
459 hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
460 slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
464 * hfi1_make_ud_req - construct a UD request packet
467 * Assume s_lock is held.
469 * Return 1 if constructed; otherwise, return 0.
471 int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
473 struct hfi1_qp_priv *priv = qp->priv;
474 struct rdma_ah_attr *ah_attr;
475 struct hfi1_pportdata *ppd;
476 struct hfi1_ibport *ibp;
477 struct rvt_swqe *wqe;
481 ps->s_txreq = get_txreq(ps->dev, qp);
482 if (IS_ERR(ps->s_txreq))
485 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
486 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
488 /* We are in the error state, flush the work request. */
489 if (qp->s_last == READ_ONCE(qp->s_head))
491 /* If DMAs are in progress, we can't flush immediately. */
492 if (iowait_sdma_pending(&priv->s_iowait)) {
493 qp->s_flags |= RVT_S_WAIT_DMA;
496 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
497 hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
501 /* see post_one_send() */
502 if (qp->s_cur == READ_ONCE(qp->s_head))
505 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
506 next_cur = qp->s_cur + 1;
507 if (next_cur >= qp->s_size)
510 /* Construct the header. */
511 ibp = to_iport(qp->ibqp.device, qp->port_num);
512 ppd = ppd_from_ibp(ibp);
513 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
514 priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
515 if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
516 (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
517 lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
518 if (unlikely(!loopback &&
519 ((lid == ppd->lid) ||
520 ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
521 (qp->ibqp.qp_type == IB_QPT_GSI))))) {
522 unsigned long tflags = ps->flags;
524 * If DMAs are in progress, we can't generate
525 * a completion for the loopback packet since
526 * it would be out of order.
527 * Instead of waiting, we could queue a
528 * zero length descriptor so we get a callback.
530 if (iowait_sdma_pending(&priv->s_iowait)) {
531 qp->s_flags |= RVT_S_WAIT_DMA;
534 qp->s_cur = next_cur;
535 spin_unlock_irqrestore(&qp->s_lock, tflags);
536 ud_loopback(qp, wqe);
537 spin_lock_irqsave(&qp->s_lock, tflags);
539 hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
544 qp->s_cur = next_cur;
545 ps->s_txreq->s_cur_size = wqe->length;
546 ps->s_txreq->ss = &qp->s_sge;
547 qp->s_srate = rdma_ah_get_static_rate(ah_attr);
548 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
550 qp->s_sge.sge = wqe->sg_list[0];
551 qp->s_sge.sg_list = wqe->sg_list + 1;
552 qp->s_sge.num_sge = wqe->wr.num_sge;
553 qp->s_sge.total_len = wqe->length;
555 /* Make the appropriate header */
556 hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
557 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
558 ps->s_txreq->sde = priv->s_sde;
559 priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
560 ps->s_txreq->psc = priv->s_sendcontext;
562 priv->s_ahg->ahgcount = 0;
563 priv->s_ahg->ahgidx = 0;
564 priv->s_ahg->tx_flags = 0;
566 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
571 hfi1_put_txreq(ps->s_txreq);
576 hfi1_put_txreq(ps->s_txreq);
580 qp->s_flags &= ~RVT_S_BUSY;
586 * Hardware can't check this so we do it here.
588 * This is a slightly different algorithm than the standard pkey check. It
589 * special cases the management keys and allows for 0x7fff and 0xffff to be in
590 * the table at the same time.
592 * @returns the index found or -1 if not found
594 int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
596 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
599 if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
600 unsigned lim_idx = -1;
602 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
603 /* here we look for an exact match */
604 if (ppd->pkeys[i] == pkey)
606 if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
610 /* did not find 0xffff return 0x7fff idx if found */
611 if (pkey == FULL_MGMT_P_KEY)
618 pkey &= 0x7fff; /* remove limited/full membership bit */
620 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
621 if ((ppd->pkeys[i] & 0x7fff) == pkey)
625 * Should not get here, this means hardware failed to validate pkeys.
630 void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
631 u32 remote_qpn, u32 pkey, u32 slid, u32 dlid,
632 u8 sc5, const struct ib_grh *old_grh)
634 u64 pbc, pbc_flags = 0;
635 u32 bth0, plen, vl, hwords = 7;
638 struct hfi1_16b_header hdr;
639 struct ib_other_headers *ohdr;
640 struct pio_buf *pbuf;
641 struct send_context *ctxt = qp_to_send_context(qp, sc5);
642 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
645 /* Populate length */
646 nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
647 SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
649 struct ib_grh *grh = &hdr.u.l.grh;
651 grh->version_tclass_flow = old_grh->version_tclass_flow;
652 grh->paylen = cpu_to_be16((hwords - 4 + nwords) << 2);
653 grh->hop_limit = 0xff;
654 grh->sgid = old_grh->dgid;
655 grh->dgid = old_grh->sgid;
657 l4 = OPA_16B_L4_IB_GLOBAL;
658 hwords += sizeof(struct ib_grh) / sizeof(u32);
661 l4 = OPA_16B_L4_IB_LOCAL;
664 /* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */
665 bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
666 (hfi1_get_16b_padding(hwords << 2, 0) << 20);
667 ohdr->bth[0] = cpu_to_be32(bth0);
669 ohdr->bth[1] = cpu_to_be32(remote_qpn);
670 ohdr->bth[2] = 0; /* PSN 0 */
672 /* Convert dwords to flits */
673 len = (hwords + nwords) >> 1;
674 hfi1_make_16b_hdr(&hdr, slid, dlid, len, pkey, 1, 0, l4, sc5);
676 plen = 2 /* PBC */ + hwords + nwords;
677 pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
678 vl = sc_to_vlt(ppd->dd, sc5);
679 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
681 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
683 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
688 void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
689 u32 pkey, u32 slid, u32 dlid, u8 sc5,
690 const struct ib_grh *old_grh)
692 u64 pbc, pbc_flags = 0;
693 u32 bth0, plen, vl, hwords = 5;
695 u8 sl = ibp->sc_to_sl[sc5];
696 struct ib_header hdr;
697 struct ib_other_headers *ohdr;
698 struct pio_buf *pbuf;
699 struct send_context *ctxt = qp_to_send_context(qp, sc5);
700 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
703 struct ib_grh *grh = &hdr.u.l.grh;
705 grh->version_tclass_flow = old_grh->version_tclass_flow;
706 grh->paylen = cpu_to_be16((hwords - 2 + SIZE_OF_CRC) << 2);
707 grh->hop_limit = 0xff;
708 grh->sgid = old_grh->dgid;
709 grh->dgid = old_grh->sgid;
712 hwords += sizeof(struct ib_grh) / sizeof(u32);
718 lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
720 bth0 = pkey | (IB_OPCODE_CNP << 24);
721 ohdr->bth[0] = cpu_to_be32(bth0);
723 ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
724 ohdr->bth[2] = 0; /* PSN 0 */
726 hfi1_make_ib_hdr(&hdr, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
727 plen = 2 /* PBC */ + hwords;
728 pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
729 vl = sc_to_vlt(ppd->dd, sc5);
730 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
732 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
734 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
740 * opa_smp_check() - Do the regular pkey checking, and the additional
741 * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
742 * 9.10.25 ("SMA Packet Checks").
745 * - Checks are done using the pkey directly from the packet's BTH,
746 * and specifically _not_ the pkey that we attach to the completion,
747 * which may be different.
748 * - These checks are specifically for "non-local" SMPs (i.e., SMPs
749 * which originated on another node). SMPs which are sent from, and
750 * destined to this node are checked in opa_local_smp_check().
752 * At the point where opa_smp_check() is called, we know:
753 * - destination QP is QP0
755 * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
757 static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
758 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
760 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
763 * I don't think it's possible for us to get here with sc != 0xf,
764 * but check it to be certain.
769 if (rcv_pkey_check(ppd, pkey, sc5, slid))
773 * At this point we know (and so don't need to check again) that
774 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
775 * (see ingress_pkey_check).
777 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
778 smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
779 ingress_pkey_table_fail(ppd, pkey, slid);
784 * SMPs fall into one of four (disjoint) categories:
785 * SMA request, SMA response, SMA trap, or SMA trap repress.
786 * Our response depends, in part, on which type of SMP we're
789 * If this is an SMA response, skip the check here.
791 * If this is an SMA request or SMA trap repress:
792 * - pkey != FULL_MGMT_P_KEY =>
793 * increment port recv constraint errors, drop MAD
796 * - accept if the port is running an SM
797 * - drop MAD if it's an SMA trap
798 * - pkey == FULL_MGMT_P_KEY =>
799 * reply with unsupported method
800 * - pkey != FULL_MGMT_P_KEY =>
801 * increment port recv constraint errors, drop MAD
803 switch (smp->method) {
804 case IB_MGMT_METHOD_GET_RESP:
805 case IB_MGMT_METHOD_REPORT_RESP:
807 case IB_MGMT_METHOD_GET:
808 case IB_MGMT_METHOD_SET:
809 case IB_MGMT_METHOD_REPORT:
810 case IB_MGMT_METHOD_TRAP_REPRESS:
811 if (pkey != FULL_MGMT_P_KEY) {
812 ingress_pkey_table_fail(ppd, pkey, slid);
817 if (ibp->rvp.port_cap_flags & IB_PORT_SM)
819 if (smp->method == IB_MGMT_METHOD_TRAP)
821 if (pkey == FULL_MGMT_P_KEY) {
822 smp->status |= IB_SMP_UNSUP_METHOD;
825 ingress_pkey_table_fail(ppd, pkey, slid);
832 * hfi1_ud_rcv - receive an incoming UD packet
833 * @ibp: the port the packet came in on
834 * @hdr: the packet header
835 * @rcv_flags: flags relevant to rcv processing
836 * @data: the packet data
837 * @tlen: the packet length
838 * @qp: the QP the packet came on
840 * This is called from qp_rcv() to process an incoming UD packet
842 * Called at interrupt level.
844 void hfi1_ud_rcv(struct hfi1_packet *packet)
846 struct ib_other_headers *ohdr = packet->ohdr;
847 u32 hdrsize = packet->hlen;
852 int mgmt_pkey_idx = -1;
853 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
854 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
855 void *data = packet->payload;
856 u32 tlen = packet->tlen;
857 struct rvt_qp *qp = packet->qp;
860 u8 opcode = packet->opcode;
862 u32 dlid = packet->dlid;
863 u32 slid = packet->slid;
865 bool dlid_is_permissive;
866 bool slid_is_permissive;
868 extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
869 qkey = ib_get_qkey(ohdr);
870 src_qp = ib_get_sqpn(ohdr);
872 if (packet->etype == RHF_RCV_TYPE_BYPASS) {
874 opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);
876 pkey = hfi1_16B_get_pkey(packet->hdr);
877 dlid_is_permissive = (dlid == permissive_lid);
878 slid_is_permissive = (slid == permissive_lid);
880 pkey = ib_bth_get_pkey(ohdr);
881 dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
882 slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
884 sl_from_sc = ibp->sc_to_sl[sc5];
886 process_ecn(qp, packet, (opcode != IB_OPCODE_CNP));
888 * Get the number of bytes the message was padded by
889 * and drop incomplete packets.
891 if (unlikely(tlen < (hdrsize + extra_bytes)))
894 tlen -= hdrsize + extra_bytes;
897 * Check that the permissive LID is only used on QP0
898 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
900 if (qp->ibqp.qp_num) {
901 if (unlikely(dlid_is_permissive || slid_is_permissive))
903 if (qp->ibqp.qp_num > 1) {
904 if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
906 * Traps will not be sent for packets dropped
907 * by the HW. This is fine, as sending trap
908 * for invalid pkeys is optional according to
909 * IB spec (release 1.3, section 10.9.4)
913 src_qp, qp->ibqp.qp_num,
919 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
920 if (mgmt_pkey_idx < 0)
923 if (unlikely(qkey != qp->qkey)) /* Silent drop */
926 /* Drop invalid MAD packets (see 13.5.3.1). */
927 if (unlikely(qp->ibqp.qp_num == 1 &&
928 (tlen > 2048 || (sc5 == 0xF))))
931 /* Received on QP0, and so by definition, this is an SMP */
932 struct opa_smp *smp = (struct opa_smp *)data;
934 if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
939 if ((dlid_is_permissive || slid_is_permissive) &&
940 smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
943 /* look up SMI pkey */
944 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
945 if (mgmt_pkey_idx < 0)
949 if (qp->ibqp.qp_num > 1 &&
950 opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
951 wc.ex.imm_data = ohdr->u.ud.imm_data;
952 wc.wc_flags = IB_WC_WITH_IMM;
954 } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
962 * A GRH is expected to precede the data even if not
963 * present on the wire.
965 wc.byte_len = tlen + sizeof(struct ib_grh);
968 * Get the next work request entry to find where to put the data.
970 if (qp->r_flags & RVT_R_REUSE_SGE) {
971 qp->r_flags &= ~RVT_R_REUSE_SGE;
975 ret = hfi1_rvt_get_rwqe(qp, 0);
977 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
981 if (qp->ibqp.qp_num == 0)
982 ibp->rvp.n_vl15_dropped++;
986 /* Silently drop packets which are too big. */
987 if (unlikely(wc.byte_len > qp->r_len)) {
988 qp->r_flags |= RVT_R_REUSE_SGE;
992 hfi1_copy_sge(&qp->r_sge, packet->grh,
993 sizeof(struct ib_grh), true, false);
994 wc.wc_flags |= IB_WC_GRH;
995 } else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
998 * Assuming we only created 16B on the send side
999 * if we want to use large LIDs, since GRH was stripped
1000 * out when creating 16B, add back the GRH here.
1002 hfi1_make_ext_grh(packet, &grh, slid, dlid);
1003 hfi1_copy_sge(&qp->r_sge, &grh,
1004 sizeof(struct ib_grh), true, false);
1005 wc.wc_flags |= IB_WC_GRH;
1007 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
1009 hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
1011 rvt_put_ss(&qp->r_sge);
1012 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
1014 wc.wr_id = qp->r_wr_id;
1015 wc.status = IB_WC_SUCCESS;
1016 wc.opcode = IB_WC_RECV;
1021 if (qp->ibqp.qp_type == IB_QPT_GSI ||
1022 qp->ibqp.qp_type == IB_QPT_SMI) {
1023 if (mgmt_pkey_idx < 0) {
1024 if (net_ratelimit()) {
1025 struct hfi1_devdata *dd = ppd->dd;
1027 dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
1032 wc.pkey_index = (unsigned)mgmt_pkey_idx;
1036 if (slid_is_permissive)
1037 slid = be32_to_cpu(OPA_LID_PERMISSIVE);
1038 wc.slid = slid & U16_MAX;
1042 * Save the LMC lower bits if the destination LID is a unicast LID.
1044 wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
1045 dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
1046 wc.port_num = qp->port_num;
1047 /* Signal completion event if the solicited bit is set. */
1048 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
1050 cpu_to_be32(IB_BTH_SOLICITED)) != 0);
1054 ibp->rvp.n_pkt_drops++;