1 /* 2 * Copyright(c) 2015 - 2018 Intel Corporation. 3 * 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/net.h> 49 #include <rdma/ib_smi.h> 50 51 #include "hfi.h" 52 #include "mad.h" 53 #include "verbs_txreq.h" 54 #include "trace_ibhdrs.h" 55 #include "qp.h" 56 57 /* We support only two types - 9B and 16B for now */ 58 static const hfi1_make_req hfi1_make_ud_req_tbl[2] = { 59 [HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B, 60 [HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B 61 }; 62 63 /** 64 * ud_loopback - handle send on loopback QPs 65 * @sqp: the sending QP 66 * @swqe: the send work request 67 * 68 * This is called from hfi1_make_ud_req() to forward a WQE addressed 69 * to the same HFI. 70 * Note that the receive interrupt handler may be calling hfi1_ud_rcv() 71 * while this is being called. 72 */ 73 static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe) 74 { 75 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num); 76 struct hfi1_pportdata *ppd; 77 struct hfi1_qp_priv *priv = sqp->priv; 78 struct rvt_qp *qp; 79 struct rdma_ah_attr *ah_attr; 80 unsigned long flags; 81 struct rvt_sge_state ssge; 82 struct rvt_sge *sge; 83 struct ib_wc wc; 84 u32 length; 85 enum ib_qp_type sqptype, dqptype; 86 87 rcu_read_lock(); 88 89 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp, 90 swqe->ud_wr.remote_qpn); 91 if (!qp) { 92 ibp->rvp.n_pkt_drops++; 93 rcu_read_unlock(); 94 return; 95 } 96 97 sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ? 98 IB_QPT_UD : sqp->ibqp.qp_type; 99 dqptype = qp->ibqp.qp_type == IB_QPT_GSI ? 100 IB_QPT_UD : qp->ibqp.qp_type; 101 102 if (dqptype != sqptype || 103 !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) { 104 ibp->rvp.n_pkt_drops++; 105 goto drop; 106 } 107 108 ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr; 109 ppd = ppd_from_ibp(ibp); 110 111 if (qp->ibqp.qp_num > 1) { 112 u16 pkey; 113 u32 slid; 114 u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)]; 115 116 pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index); 117 slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) & 118 ((1 << ppd->lmc) - 1)); 119 if (unlikely(ingress_pkey_check(ppd, pkey, sc5, 120 qp->s_pkey_index, 121 slid, false))) { 122 hfi1_bad_pkey(ibp, pkey, 123 rdma_ah_get_sl(ah_attr), 124 sqp->ibqp.qp_num, qp->ibqp.qp_num, 125 slid, rdma_ah_get_dlid(ah_attr)); 126 goto drop; 127 } 128 } 129 130 /* 131 * Check that the qkey matches (except for QP0, see 9.6.1.4.1). 132 * Qkeys with the high order bit set mean use the 133 * qkey from the QP context instead of the WR (see 10.2.5). 134 */ 135 if (qp->ibqp.qp_num) { 136 u32 qkey; 137 138 qkey = (int)swqe->ud_wr.remote_qkey < 0 ? 139 sqp->qkey : swqe->ud_wr.remote_qkey; 140 if (unlikely(qkey != qp->qkey)) 141 goto drop; /* silently drop per IBTA spec */ 142 } 143 144 /* 145 * A GRH is expected to precede the data even if not 146 * present on the wire. 147 */ 148 length = swqe->length; 149 memset(&wc, 0, sizeof(wc)); 150 wc.byte_len = length + sizeof(struct ib_grh); 151 152 if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) { 153 wc.wc_flags = IB_WC_WITH_IMM; 154 wc.ex.imm_data = swqe->wr.ex.imm_data; 155 } 156 157 spin_lock_irqsave(&qp->r_lock, flags); 158 159 /* 160 * Get the next work request entry to find where to put the data. 161 */ 162 if (qp->r_flags & RVT_R_REUSE_SGE) { 163 qp->r_flags &= ~RVT_R_REUSE_SGE; 164 } else { 165 int ret; 166 167 ret = rvt_get_rwqe(qp, false); 168 if (ret < 0) { 169 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR); 170 goto bail_unlock; 171 } 172 if (!ret) { 173 if (qp->ibqp.qp_num == 0) 174 ibp->rvp.n_vl15_dropped++; 175 goto bail_unlock; 176 } 177 } 178 /* Silently drop packets which are too big. */ 179 if (unlikely(wc.byte_len > qp->r_len)) { 180 qp->r_flags |= RVT_R_REUSE_SGE; 181 ibp->rvp.n_pkt_drops++; 182 goto bail_unlock; 183 } 184 185 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) { 186 struct ib_grh grh; 187 struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr)); 188 189 /* 190 * For loopback packets with extended LIDs, the 191 * sgid_index in the GRH is 0 and the dgid is 192 * OPA GID of the sender. While creating a response 193 * to the loopback packet, IB core creates the new 194 * sgid_index from the DGID and that will be the 195 * OPA_GID_INDEX. The new dgid is from the sgid 196 * index and that will be in the IB GID format. 197 * 198 * We now have a case where the sent packet had a 199 * different sgid_index and dgid compared to the 200 * one that was received in response. 201 * 202 * Fix this inconsistency. 203 */ 204 if (priv->hdr_type == HFI1_PKT_TYPE_16B) { 205 if (grd.sgid_index == 0) 206 grd.sgid_index = OPA_GID_INDEX; 207 208 if (ib_is_opa_gid(&grd.dgid)) 209 grd.dgid.global.interface_id = 210 cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]); 211 } 212 213 hfi1_make_grh(ibp, &grh, &grd, 0, 0); 214 rvt_copy_sge(qp, &qp->r_sge, &grh, 215 sizeof(grh), true, false); 216 wc.wc_flags |= IB_WC_GRH; 217 } else { 218 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true); 219 } 220 ssge.sg_list = swqe->sg_list + 1; 221 ssge.sge = *swqe->sg_list; 222 ssge.num_sge = swqe->wr.num_sge; 223 sge = &ssge.sge; 224 while (length) { 225 u32 len = sge->length; 226 227 if (len > length) 228 len = length; 229 if (len > sge->sge_length) 230 len = sge->sge_length; 231 WARN_ON_ONCE(len == 0); 232 rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false); 233 sge->vaddr += len; 234 sge->length -= len; 235 sge->sge_length -= len; 236 if (sge->sge_length == 0) { 237 if (--ssge.num_sge) 238 *sge = *ssge.sg_list++; 239 } else if (sge->length == 0 && sge->mr->lkey) { 240 if (++sge->n >= RVT_SEGSZ) { 241 if (++sge->m >= sge->mr->mapsz) 242 break; 243 sge->n = 0; 244 } 245 sge->vaddr = 246 sge->mr->map[sge->m]->segs[sge->n].vaddr; 247 sge->length = 248 sge->mr->map[sge->m]->segs[sge->n].length; 249 } 250 length -= len; 251 } 252 rvt_put_ss(&qp->r_sge); 253 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) 254 goto bail_unlock; 255 wc.wr_id = qp->r_wr_id; 256 wc.status = IB_WC_SUCCESS; 257 wc.opcode = IB_WC_RECV; 258 wc.qp = &qp->ibqp; 259 wc.src_qp = sqp->ibqp.qp_num; 260 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) { 261 if (sqp->ibqp.qp_type == IB_QPT_GSI || 262 sqp->ibqp.qp_type == IB_QPT_SMI) 263 wc.pkey_index = swqe->ud_wr.pkey_index; 264 else 265 wc.pkey_index = sqp->s_pkey_index; 266 } else { 267 wc.pkey_index = 0; 268 } 269 wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) & 270 ((1 << ppd->lmc) - 1))) & U16_MAX; 271 /* Check for loopback when the port lid is not set */ 272 if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI) 273 wc.slid = be16_to_cpu(IB_LID_PERMISSIVE); 274 wc.sl = rdma_ah_get_sl(ah_attr); 275 wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1); 276 wc.port_num = qp->port_num; 277 /* Signal completion event if the solicited bit is set. */ 278 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 279 swqe->wr.send_flags & IB_SEND_SOLICITED); 280 ibp->rvp.n_loop_pkts++; 281 bail_unlock: 282 spin_unlock_irqrestore(&qp->r_lock, flags); 283 drop: 284 rcu_read_unlock(); 285 } 286 287 static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe, 288 struct ib_other_headers *ohdr, 289 u16 *pkey, u32 extra_bytes, bool bypass) 290 { 291 u32 bth0; 292 struct hfi1_ibport *ibp; 293 294 ibp = to_iport(qp->ibqp.device, qp->port_num); 295 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) { 296 ohdr->u.ud.imm_data = wqe->wr.ex.imm_data; 297 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24; 298 } else { 299 bth0 = IB_OPCODE_UD_SEND_ONLY << 24; 300 } 301 302 if (wqe->wr.send_flags & IB_SEND_SOLICITED) 303 bth0 |= IB_BTH_SOLICITED; 304 bth0 |= extra_bytes << 20; 305 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) 306 *pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index); 307 else 308 *pkey = hfi1_get_pkey(ibp, qp->s_pkey_index); 309 if (!bypass) 310 bth0 |= *pkey; 311 ohdr->bth[0] = cpu_to_be32(bth0); 312 ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn); 313 ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn)); 314 /* 315 * Qkeys with the high order bit set mean use the 316 * qkey from the QP context instead of the WR (see 10.2.5). 317 */ 318 ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ? 319 qp->qkey : wqe->ud_wr.remote_qkey); 320 ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num); 321 } 322 323 void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps, 324 struct rvt_swqe *wqe) 325 { 326 u32 nwords, extra_bytes; 327 u16 len, slid, dlid, pkey; 328 u16 lrh0 = 0; 329 u8 sc5; 330 struct hfi1_qp_priv *priv = qp->priv; 331 struct ib_other_headers *ohdr; 332 struct rdma_ah_attr *ah_attr; 333 struct hfi1_pportdata *ppd; 334 struct hfi1_ibport *ibp; 335 struct ib_grh *grh; 336 337 ibp = to_iport(qp->ibqp.device, qp->port_num); 338 ppd = ppd_from_ibp(ibp); 339 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr; 340 341 extra_bytes = -wqe->length & 3; 342 nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC; 343 /* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */ 344 ps->s_txreq->hdr_dwords = 7; 345 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) 346 ps->s_txreq->hdr_dwords++; 347 348 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) { 349 grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh; 350 ps->s_txreq->hdr_dwords += 351 hfi1_make_grh(ibp, grh, rdma_ah_read_grh(ah_attr), 352 ps->s_txreq->hdr_dwords - LRH_9B_DWORDS, 353 nwords); 354 lrh0 = HFI1_LRH_GRH; 355 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth; 356 } else { 357 lrh0 = HFI1_LRH_BTH; 358 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth; 359 } 360 361 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)]; 362 lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4; 363 if (qp->ibqp.qp_type == IB_QPT_SMI) { 364 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */ 365 priv->s_sc = 0xf; 366 } else { 367 lrh0 |= (sc5 & 0xf) << 12; 368 priv->s_sc = sc5; 369 } 370 371 dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B); 372 if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) { 373 slid = be16_to_cpu(IB_LID_PERMISSIVE); 374 } else { 375 u16 lid = (u16)ppd->lid; 376 377 if (lid) { 378 lid |= rdma_ah_get_path_bits(ah_attr) & 379 ((1 << ppd->lmc) - 1); 380 slid = lid; 381 } else { 382 slid = be16_to_cpu(IB_LID_PERMISSIVE); 383 } 384 } 385 hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false); 386 len = ps->s_txreq->hdr_dwords + nwords; 387 388 /* Setup the packet */ 389 ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B; 390 hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh, 391 lrh0, len, dlid, slid); 392 } 393 394 void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps, 395 struct rvt_swqe *wqe) 396 { 397 struct hfi1_qp_priv *priv = qp->priv; 398 struct ib_other_headers *ohdr; 399 struct rdma_ah_attr *ah_attr; 400 struct hfi1_pportdata *ppd; 401 struct hfi1_ibport *ibp; 402 u32 dlid, slid, nwords, extra_bytes; 403 u32 dest_qp = wqe->ud_wr.remote_qpn; 404 u32 src_qp = qp->ibqp.qp_num; 405 u16 len, pkey; 406 u8 l4, sc5; 407 bool is_mgmt = false; 408 409 ibp = to_iport(qp->ibqp.device, qp->port_num); 410 ppd = ppd_from_ibp(ibp); 411 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr; 412 413 /* 414 * Build 16B Management Packet if either the destination 415 * or source queue pair number is 0 or 1. 416 */ 417 if (dest_qp == 0 || src_qp == 0 || dest_qp == 1 || src_qp == 1) { 418 /* header size in dwords 16B LRH+L4_FM = (16+8)/4. */ 419 ps->s_txreq->hdr_dwords = 6; 420 is_mgmt = true; 421 } else { 422 /* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */ 423 ps->s_txreq->hdr_dwords = 9; 424 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) 425 ps->s_txreq->hdr_dwords++; 426 } 427 428 /* SW provides space for CRC and LT for bypass packets. */ 429 extra_bytes = hfi1_get_16b_padding((ps->s_txreq->hdr_dwords << 2), 430 wqe->length); 431 nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC; 432 433 if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) && 434 hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) { 435 struct ib_grh *grh; 436 struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr); 437 /* 438 * Ensure OPA GIDs are transformed to IB gids 439 * before creating the GRH. 440 */ 441 if (grd->sgid_index == OPA_GID_INDEX) { 442 dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n", 443 grd->sgid_index); 444 grd->sgid_index = 0; 445 } 446 grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh; 447 ps->s_txreq->hdr_dwords += hfi1_make_grh( 448 ibp, grh, grd, 449 ps->s_txreq->hdr_dwords - LRH_16B_DWORDS, 450 nwords); 451 ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth; 452 l4 = OPA_16B_L4_IB_GLOBAL; 453 } else { 454 ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth; 455 l4 = OPA_16B_L4_IB_LOCAL; 456 } 457 458 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)]; 459 if (qp->ibqp.qp_type == IB_QPT_SMI) 460 priv->s_sc = 0xf; 461 else 462 priv->s_sc = sc5; 463 464 dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B); 465 if (!ppd->lid) 466 slid = be32_to_cpu(OPA_LID_PERMISSIVE); 467 else 468 slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) & 469 ((1 << ppd->lmc) - 1)); 470 471 if (is_mgmt) { 472 l4 = OPA_16B_L4_FM; 473 pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index); 474 hfi1_16B_set_qpn(&ps->s_txreq->phdr.hdr.opah.u.mgmt, 475 dest_qp, src_qp); 476 } else { 477 hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true); 478 } 479 /* Convert dwords to flits */ 480 len = (ps->s_txreq->hdr_dwords + nwords) >> 1; 481 482 /* Setup the packet */ 483 ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B; 484 hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah, 485 slid, dlid, len, pkey, 0, 0, l4, priv->s_sc); 486 } 487 488 /** 489 * hfi1_make_ud_req - construct a UD request packet 490 * @qp: the QP 491 * 492 * Assume s_lock is held. 493 * 494 * Return 1 if constructed; otherwise, return 0. 495 */ 496 int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps) 497 { 498 struct hfi1_qp_priv *priv = qp->priv; 499 struct rdma_ah_attr *ah_attr; 500 struct hfi1_pportdata *ppd; 501 struct hfi1_ibport *ibp; 502 struct rvt_swqe *wqe; 503 int next_cur; 504 u32 lid; 505 506 ps->s_txreq = get_txreq(ps->dev, qp); 507 if (!ps->s_txreq) 508 goto bail_no_tx; 509 510 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) { 511 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND)) 512 goto bail; 513 /* We are in the error state, flush the work request. */ 514 if (qp->s_last == READ_ONCE(qp->s_head)) 515 goto bail; 516 /* If DMAs are in progress, we can't flush immediately. */ 517 if (iowait_sdma_pending(&priv->s_iowait)) { 518 qp->s_flags |= RVT_S_WAIT_DMA; 519 goto bail; 520 } 521 wqe = rvt_get_swqe_ptr(qp, qp->s_last); 522 rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR); 523 goto done_free_tx; 524 } 525 526 /* see post_one_send() */ 527 if (qp->s_cur == READ_ONCE(qp->s_head)) 528 goto bail; 529 530 wqe = rvt_get_swqe_ptr(qp, qp->s_cur); 531 next_cur = qp->s_cur + 1; 532 if (next_cur >= qp->s_size) 533 next_cur = 0; 534 535 /* Construct the header. */ 536 ibp = to_iport(qp->ibqp.device, qp->port_num); 537 ppd = ppd_from_ibp(ibp); 538 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr; 539 priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr); 540 if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) || 541 (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) { 542 lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1); 543 if (unlikely(!loopback && 544 ((lid == ppd->lid) || 545 ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) && 546 (qp->ibqp.qp_type == IB_QPT_GSI))))) { 547 unsigned long tflags = ps->flags; 548 /* 549 * If DMAs are in progress, we can't generate 550 * a completion for the loopback packet since 551 * it would be out of order. 552 * Instead of waiting, we could queue a 553 * zero length descriptor so we get a callback. 554 */ 555 if (iowait_sdma_pending(&priv->s_iowait)) { 556 qp->s_flags |= RVT_S_WAIT_DMA; 557 goto bail; 558 } 559 qp->s_cur = next_cur; 560 spin_unlock_irqrestore(&qp->s_lock, tflags); 561 ud_loopback(qp, wqe); 562 spin_lock_irqsave(&qp->s_lock, tflags); 563 ps->flags = tflags; 564 rvt_send_complete(qp, wqe, IB_WC_SUCCESS); 565 goto done_free_tx; 566 } 567 } 568 569 qp->s_cur = next_cur; 570 ps->s_txreq->s_cur_size = wqe->length; 571 ps->s_txreq->ss = &qp->s_sge; 572 qp->s_srate = rdma_ah_get_static_rate(ah_attr); 573 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate); 574 qp->s_wqe = wqe; 575 qp->s_sge.sge = wqe->sg_list[0]; 576 qp->s_sge.sg_list = wqe->sg_list + 1; 577 qp->s_sge.num_sge = wqe->wr.num_sge; 578 qp->s_sge.total_len = wqe->length; 579 580 /* Make the appropriate header */ 581 hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe); 582 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc); 583 ps->s_txreq->sde = priv->s_sde; 584 priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc); 585 ps->s_txreq->psc = priv->s_sendcontext; 586 /* disarm any ahg */ 587 priv->s_ahg->ahgcount = 0; 588 priv->s_ahg->ahgidx = 0; 589 priv->s_ahg->tx_flags = 0; 590 591 return 1; 592 593 done_free_tx: 594 hfi1_put_txreq(ps->s_txreq); 595 ps->s_txreq = NULL; 596 return 1; 597 598 bail: 599 hfi1_put_txreq(ps->s_txreq); 600 601 bail_no_tx: 602 ps->s_txreq = NULL; 603 qp->s_flags &= ~RVT_S_BUSY; 604 return 0; 605 } 606 607 /* 608 * Hardware can't check this so we do it here. 609 * 610 * This is a slightly different algorithm than the standard pkey check. It 611 * special cases the management keys and allows for 0x7fff and 0xffff to be in 612 * the table at the same time. 613 * 614 * @returns the index found or -1 if not found 615 */ 616 int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey) 617 { 618 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp); 619 unsigned i; 620 621 if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) { 622 unsigned lim_idx = -1; 623 624 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) { 625 /* here we look for an exact match */ 626 if (ppd->pkeys[i] == pkey) 627 return i; 628 if (ppd->pkeys[i] == LIM_MGMT_P_KEY) 629 lim_idx = i; 630 } 631 632 /* did not find 0xffff return 0x7fff idx if found */ 633 if (pkey == FULL_MGMT_P_KEY) 634 return lim_idx; 635 636 /* no match... */ 637 return -1; 638 } 639 640 pkey &= 0x7fff; /* remove limited/full membership bit */ 641 642 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) 643 if ((ppd->pkeys[i] & 0x7fff) == pkey) 644 return i; 645 646 /* 647 * Should not get here, this means hardware failed to validate pkeys. 648 */ 649 return -1; 650 } 651 652 void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp, 653 u32 remote_qpn, u16 pkey, u32 slid, u32 dlid, 654 u8 sc5, const struct ib_grh *old_grh) 655 { 656 u64 pbc, pbc_flags = 0; 657 u32 bth0, plen, vl, hwords = 7; 658 u16 len; 659 u8 l4; 660 struct hfi1_opa_header hdr; 661 struct ib_other_headers *ohdr; 662 struct pio_buf *pbuf; 663 struct send_context *ctxt = qp_to_send_context(qp, sc5); 664 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp); 665 u32 nwords; 666 667 hdr.hdr_type = HFI1_PKT_TYPE_16B; 668 /* Populate length */ 669 nwords = ((hfi1_get_16b_padding(hwords << 2, 0) + 670 SIZE_OF_LT) >> 2) + SIZE_OF_CRC; 671 if (old_grh) { 672 struct ib_grh *grh = &hdr.opah.u.l.grh; 673 674 grh->version_tclass_flow = old_grh->version_tclass_flow; 675 grh->paylen = cpu_to_be16( 676 (hwords - LRH_16B_DWORDS + nwords) << 2); 677 grh->hop_limit = 0xff; 678 grh->sgid = old_grh->dgid; 679 grh->dgid = old_grh->sgid; 680 ohdr = &hdr.opah.u.l.oth; 681 l4 = OPA_16B_L4_IB_GLOBAL; 682 hwords += sizeof(struct ib_grh) / sizeof(u32); 683 } else { 684 ohdr = &hdr.opah.u.oth; 685 l4 = OPA_16B_L4_IB_LOCAL; 686 } 687 688 /* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */ 689 bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) | 690 (hfi1_get_16b_padding(hwords << 2, 0) << 20); 691 ohdr->bth[0] = cpu_to_be32(bth0); 692 693 ohdr->bth[1] = cpu_to_be32(remote_qpn); 694 ohdr->bth[2] = 0; /* PSN 0 */ 695 696 /* Convert dwords to flits */ 697 len = (hwords + nwords) >> 1; 698 hfi1_make_16b_hdr(&hdr.opah, slid, dlid, len, pkey, 1, 0, l4, sc5); 699 700 plen = 2 /* PBC */ + hwords + nwords; 701 pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC; 702 vl = sc_to_vlt(ppd->dd, sc5); 703 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen); 704 if (ctxt) { 705 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL); 706 if (pbuf) { 707 trace_pio_output_ibhdr(ppd->dd, &hdr, sc5); 708 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc, 709 &hdr, hwords); 710 } 711 } 712 } 713 714 void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn, 715 u16 pkey, u32 slid, u32 dlid, u8 sc5, 716 const struct ib_grh *old_grh) 717 { 718 u64 pbc, pbc_flags = 0; 719 u32 bth0, plen, vl, hwords = 5; 720 u16 lrh0; 721 u8 sl = ibp->sc_to_sl[sc5]; 722 struct hfi1_opa_header hdr; 723 struct ib_other_headers *ohdr; 724 struct pio_buf *pbuf; 725 struct send_context *ctxt = qp_to_send_context(qp, sc5); 726 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp); 727 728 hdr.hdr_type = HFI1_PKT_TYPE_9B; 729 if (old_grh) { 730 struct ib_grh *grh = &hdr.ibh.u.l.grh; 731 732 grh->version_tclass_flow = old_grh->version_tclass_flow; 733 grh->paylen = cpu_to_be16( 734 (hwords - LRH_9B_DWORDS + SIZE_OF_CRC) << 2); 735 grh->hop_limit = 0xff; 736 grh->sgid = old_grh->dgid; 737 grh->dgid = old_grh->sgid; 738 ohdr = &hdr.ibh.u.l.oth; 739 lrh0 = HFI1_LRH_GRH; 740 hwords += sizeof(struct ib_grh) / sizeof(u32); 741 } else { 742 ohdr = &hdr.ibh.u.oth; 743 lrh0 = HFI1_LRH_BTH; 744 } 745 746 lrh0 |= (sc5 & 0xf) << 12 | sl << 4; 747 748 bth0 = pkey | (IB_OPCODE_CNP << 24); 749 ohdr->bth[0] = cpu_to_be32(bth0); 750 751 ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT)); 752 ohdr->bth[2] = 0; /* PSN 0 */ 753 754 hfi1_make_ib_hdr(&hdr.ibh, lrh0, hwords + SIZE_OF_CRC, dlid, slid); 755 plen = 2 /* PBC */ + hwords; 756 pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT); 757 vl = sc_to_vlt(ppd->dd, sc5); 758 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen); 759 if (ctxt) { 760 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL); 761 if (pbuf) { 762 trace_pio_output_ibhdr(ppd->dd, &hdr, sc5); 763 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc, 764 &hdr, hwords); 765 } 766 } 767 } 768 769 /* 770 * opa_smp_check() - Do the regular pkey checking, and the additional 771 * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section 772 * 9.10.25 ("SMA Packet Checks"). 773 * 774 * Note that: 775 * - Checks are done using the pkey directly from the packet's BTH, 776 * and specifically _not_ the pkey that we attach to the completion, 777 * which may be different. 778 * - These checks are specifically for "non-local" SMPs (i.e., SMPs 779 * which originated on another node). SMPs which are sent from, and 780 * destined to this node are checked in opa_local_smp_check(). 781 * 782 * At the point where opa_smp_check() is called, we know: 783 * - destination QP is QP0 784 * 785 * opa_smp_check() returns 0 if all checks succeed, 1 otherwise. 786 */ 787 static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5, 788 struct rvt_qp *qp, u16 slid, struct opa_smp *smp) 789 { 790 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp); 791 792 /* 793 * I don't think it's possible for us to get here with sc != 0xf, 794 * but check it to be certain. 795 */ 796 if (sc5 != 0xf) 797 return 1; 798 799 if (rcv_pkey_check(ppd, pkey, sc5, slid)) 800 return 1; 801 802 /* 803 * At this point we know (and so don't need to check again) that 804 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY 805 * (see ingress_pkey_check). 806 */ 807 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE && 808 smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) { 809 ingress_pkey_table_fail(ppd, pkey, slid); 810 return 1; 811 } 812 813 /* 814 * SMPs fall into one of four (disjoint) categories: 815 * SMA request, SMA response, SMA trap, or SMA trap repress. 816 * Our response depends, in part, on which type of SMP we're 817 * processing. 818 * 819 * If this is an SMA response, skip the check here. 820 * 821 * If this is an SMA request or SMA trap repress: 822 * - pkey != FULL_MGMT_P_KEY => 823 * increment port recv constraint errors, drop MAD 824 * 825 * Otherwise: 826 * - accept if the port is running an SM 827 * - drop MAD if it's an SMA trap 828 * - pkey == FULL_MGMT_P_KEY => 829 * reply with unsupported method 830 * - pkey != FULL_MGMT_P_KEY => 831 * increment port recv constraint errors, drop MAD 832 */ 833 switch (smp->method) { 834 case IB_MGMT_METHOD_GET_RESP: 835 case IB_MGMT_METHOD_REPORT_RESP: 836 break; 837 case IB_MGMT_METHOD_GET: 838 case IB_MGMT_METHOD_SET: 839 case IB_MGMT_METHOD_REPORT: 840 case IB_MGMT_METHOD_TRAP_REPRESS: 841 if (pkey != FULL_MGMT_P_KEY) { 842 ingress_pkey_table_fail(ppd, pkey, slid); 843 return 1; 844 } 845 break; 846 default: 847 if (ibp->rvp.port_cap_flags & IB_PORT_SM) 848 return 0; 849 if (smp->method == IB_MGMT_METHOD_TRAP) 850 return 1; 851 if (pkey == FULL_MGMT_P_KEY) { 852 smp->status |= IB_SMP_UNSUP_METHOD; 853 return 0; 854 } 855 ingress_pkey_table_fail(ppd, pkey, slid); 856 return 1; 857 } 858 return 0; 859 } 860 861 /** 862 * hfi1_ud_rcv - receive an incoming UD packet 863 * @ibp: the port the packet came in on 864 * @hdr: the packet header 865 * @rcv_flags: flags relevant to rcv processing 866 * @data: the packet data 867 * @tlen: the packet length 868 * @qp: the QP the packet came on 869 * 870 * This is called from qp_rcv() to process an incoming UD packet 871 * for the given QP. 872 * Called at interrupt level. 873 */ 874 void hfi1_ud_rcv(struct hfi1_packet *packet) 875 { 876 u32 hdrsize = packet->hlen; 877 struct ib_wc wc; 878 u32 src_qp; 879 u16 pkey; 880 int mgmt_pkey_idx = -1; 881 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd); 882 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp); 883 void *data = packet->payload; 884 u32 tlen = packet->tlen; 885 struct rvt_qp *qp = packet->qp; 886 u8 sc5 = packet->sc; 887 u8 sl_from_sc; 888 u8 opcode = packet->opcode; 889 u8 sl = packet->sl; 890 u32 dlid = packet->dlid; 891 u32 slid = packet->slid; 892 u8 extra_bytes; 893 u8 l4 = 0; 894 bool dlid_is_permissive; 895 bool slid_is_permissive; 896 bool solicited = false; 897 898 extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2); 899 900 if (packet->etype == RHF_RCV_TYPE_BYPASS) { 901 u32 permissive_lid = 902 opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B); 903 904 l4 = hfi1_16B_get_l4(packet->hdr); 905 pkey = hfi1_16B_get_pkey(packet->hdr); 906 dlid_is_permissive = (dlid == permissive_lid); 907 slid_is_permissive = (slid == permissive_lid); 908 } else { 909 pkey = ib_bth_get_pkey(packet->ohdr); 910 dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE)); 911 slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE)); 912 } 913 sl_from_sc = ibp->sc_to_sl[sc5]; 914 915 if (likely(l4 != OPA_16B_L4_FM)) { 916 src_qp = ib_get_sqpn(packet->ohdr); 917 solicited = ib_bth_is_solicited(packet->ohdr); 918 } else { 919 src_qp = hfi1_16B_get_src_qpn(packet->mgmt); 920 } 921 922 process_ecn(qp, packet); 923 /* 924 * Get the number of bytes the message was padded by 925 * and drop incomplete packets. 926 */ 927 if (unlikely(tlen < (hdrsize + extra_bytes))) 928 goto drop; 929 930 tlen -= hdrsize + extra_bytes; 931 932 /* 933 * Check that the permissive LID is only used on QP0 934 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1). 935 */ 936 if (qp->ibqp.qp_num) { 937 if (unlikely(dlid_is_permissive || slid_is_permissive)) 938 goto drop; 939 if (qp->ibqp.qp_num > 1) { 940 if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) { 941 /* 942 * Traps will not be sent for packets dropped 943 * by the HW. This is fine, as sending trap 944 * for invalid pkeys is optional according to 945 * IB spec (release 1.3, section 10.9.4) 946 */ 947 hfi1_bad_pkey(ibp, 948 pkey, sl, 949 src_qp, qp->ibqp.qp_num, 950 slid, dlid); 951 return; 952 } 953 } else { 954 /* GSI packet */ 955 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey); 956 if (mgmt_pkey_idx < 0) 957 goto drop; 958 } 959 if (unlikely(l4 != OPA_16B_L4_FM && 960 ib_get_qkey(packet->ohdr) != qp->qkey)) 961 return; /* Silent drop */ 962 963 /* Drop invalid MAD packets (see 13.5.3.1). */ 964 if (unlikely(qp->ibqp.qp_num == 1 && 965 (tlen > 2048 || (sc5 == 0xF)))) 966 goto drop; 967 } else { 968 /* Received on QP0, and so by definition, this is an SMP */ 969 struct opa_smp *smp = (struct opa_smp *)data; 970 971 if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp)) 972 goto drop; 973 974 if (tlen > 2048) 975 goto drop; 976 if ((dlid_is_permissive || slid_is_permissive) && 977 smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) 978 goto drop; 979 980 /* look up SMI pkey */ 981 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey); 982 if (mgmt_pkey_idx < 0) 983 goto drop; 984 } 985 986 if (qp->ibqp.qp_num > 1 && 987 opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) { 988 wc.ex.imm_data = packet->ohdr->u.ud.imm_data; 989 wc.wc_flags = IB_WC_WITH_IMM; 990 tlen -= sizeof(u32); 991 } else if (opcode == IB_OPCODE_UD_SEND_ONLY) { 992 wc.ex.imm_data = 0; 993 wc.wc_flags = 0; 994 } else { 995 goto drop; 996 } 997 998 /* 999 * A GRH is expected to precede the data even if not 1000 * present on the wire. 1001 */ 1002 wc.byte_len = tlen + sizeof(struct ib_grh); 1003 1004 /* 1005 * Get the next work request entry to find where to put the data. 1006 */ 1007 if (qp->r_flags & RVT_R_REUSE_SGE) { 1008 qp->r_flags &= ~RVT_R_REUSE_SGE; 1009 } else { 1010 int ret; 1011 1012 ret = rvt_get_rwqe(qp, false); 1013 if (ret < 0) { 1014 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR); 1015 return; 1016 } 1017 if (!ret) { 1018 if (qp->ibqp.qp_num == 0) 1019 ibp->rvp.n_vl15_dropped++; 1020 return; 1021 } 1022 } 1023 /* Silently drop packets which are too big. */ 1024 if (unlikely(wc.byte_len > qp->r_len)) { 1025 qp->r_flags |= RVT_R_REUSE_SGE; 1026 goto drop; 1027 } 1028 if (packet->grh) { 1029 rvt_copy_sge(qp, &qp->r_sge, packet->grh, 1030 sizeof(struct ib_grh), true, false); 1031 wc.wc_flags |= IB_WC_GRH; 1032 } else if (packet->etype == RHF_RCV_TYPE_BYPASS) { 1033 struct ib_grh grh; 1034 /* 1035 * Assuming we only created 16B on the send side 1036 * if we want to use large LIDs, since GRH was stripped 1037 * out when creating 16B, add back the GRH here. 1038 */ 1039 hfi1_make_ext_grh(packet, &grh, slid, dlid); 1040 rvt_copy_sge(qp, &qp->r_sge, &grh, 1041 sizeof(struct ib_grh), true, false); 1042 wc.wc_flags |= IB_WC_GRH; 1043 } else { 1044 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true); 1045 } 1046 rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh), 1047 true, false); 1048 rvt_put_ss(&qp->r_sge); 1049 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) 1050 return; 1051 wc.wr_id = qp->r_wr_id; 1052 wc.status = IB_WC_SUCCESS; 1053 wc.opcode = IB_WC_RECV; 1054 wc.vendor_err = 0; 1055 wc.qp = &qp->ibqp; 1056 wc.src_qp = src_qp; 1057 1058 if (qp->ibqp.qp_type == IB_QPT_GSI || 1059 qp->ibqp.qp_type == IB_QPT_SMI) { 1060 if (mgmt_pkey_idx < 0) { 1061 if (net_ratelimit()) { 1062 struct hfi1_devdata *dd = ppd->dd; 1063 1064 dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n", 1065 qp->ibqp.qp_type); 1066 mgmt_pkey_idx = 0; 1067 } 1068 } 1069 wc.pkey_index = (unsigned)mgmt_pkey_idx; 1070 } else { 1071 wc.pkey_index = 0; 1072 } 1073 if (slid_is_permissive) 1074 slid = be32_to_cpu(OPA_LID_PERMISSIVE); 1075 wc.slid = slid & U16_MAX; 1076 wc.sl = sl_from_sc; 1077 1078 /* 1079 * Save the LMC lower bits if the destination LID is a unicast LID. 1080 */ 1081 wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 : 1082 dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1); 1083 wc.port_num = qp->port_num; 1084 /* Signal completion event if the solicited bit is set. */ 1085 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, solicited); 1086 return; 1087 1088 drop: 1089 ibp->rvp.n_pkt_drops++; 1090 } 1091