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