1 /* 2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved. 3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved. 4 * 5 * This software is available to you under a choice of one of two 6 * licenses. You may choose to be licensed under the terms of the GNU 7 * General Public License (GPL) Version 2, available from the file 8 * COPYING in the main directory of this source tree, or the 9 * OpenIB.org BSD license below: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 #include <linux/spinlock.h> 35 #include <rdma/ib_smi.h> 36 37 #include "qib.h" 38 #include "qib_mad.h" 39 40 /* 41 * Convert the AETH RNR timeout code into the number of microseconds. 42 */ 43 const u32 ib_qib_rnr_table[32] = { 44 655360, /* 00: 655.36 */ 45 10, /* 01: .01 */ 46 20, /* 02 .02 */ 47 30, /* 03: .03 */ 48 40, /* 04: .04 */ 49 60, /* 05: .06 */ 50 80, /* 06: .08 */ 51 120, /* 07: .12 */ 52 160, /* 08: .16 */ 53 240, /* 09: .24 */ 54 320, /* 0A: .32 */ 55 480, /* 0B: .48 */ 56 640, /* 0C: .64 */ 57 960, /* 0D: .96 */ 58 1280, /* 0E: 1.28 */ 59 1920, /* 0F: 1.92 */ 60 2560, /* 10: 2.56 */ 61 3840, /* 11: 3.84 */ 62 5120, /* 12: 5.12 */ 63 7680, /* 13: 7.68 */ 64 10240, /* 14: 10.24 */ 65 15360, /* 15: 15.36 */ 66 20480, /* 16: 20.48 */ 67 30720, /* 17: 30.72 */ 68 40960, /* 18: 40.96 */ 69 61440, /* 19: 61.44 */ 70 81920, /* 1A: 81.92 */ 71 122880, /* 1B: 122.88 */ 72 163840, /* 1C: 163.84 */ 73 245760, /* 1D: 245.76 */ 74 327680, /* 1E: 327.68 */ 75 491520 /* 1F: 491.52 */ 76 }; 77 78 /* 79 * Validate a RWQE and fill in the SGE state. 80 * Return 1 if OK. 81 */ 82 static int qib_init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe) 83 { 84 int i, j, ret; 85 struct ib_wc wc; 86 struct rvt_lkey_table *rkt; 87 struct rvt_pd *pd; 88 struct rvt_sge_state *ss; 89 90 rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table; 91 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd); 92 ss = &qp->r_sge; 93 ss->sg_list = qp->r_sg_list; 94 qp->r_len = 0; 95 for (i = j = 0; i < wqe->num_sge; i++) { 96 if (wqe->sg_list[i].length == 0) 97 continue; 98 /* Check LKEY */ 99 if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge, 100 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE)) 101 goto bad_lkey; 102 qp->r_len += wqe->sg_list[i].length; 103 j++; 104 } 105 ss->num_sge = j; 106 ss->total_len = qp->r_len; 107 ret = 1; 108 goto bail; 109 110 bad_lkey: 111 while (j) { 112 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge; 113 114 rvt_put_mr(sge->mr); 115 } 116 ss->num_sge = 0; 117 memset(&wc, 0, sizeof(wc)); 118 wc.wr_id = wqe->wr_id; 119 wc.status = IB_WC_LOC_PROT_ERR; 120 wc.opcode = IB_WC_RECV; 121 wc.qp = &qp->ibqp; 122 /* Signal solicited completion event. */ 123 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1); 124 ret = 0; 125 bail: 126 return ret; 127 } 128 129 /** 130 * qib_get_rwqe - copy the next RWQE into the QP's RWQE 131 * @qp: the QP 132 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge 133 * 134 * Return -1 if there is a local error, 0 if no RWQE is available, 135 * otherwise return 1. 136 * 137 * Can be called from interrupt level. 138 */ 139 int qib_get_rwqe(struct rvt_qp *qp, int wr_id_only) 140 { 141 unsigned long flags; 142 struct rvt_rq *rq; 143 struct rvt_rwq *wq; 144 struct rvt_srq *srq; 145 struct rvt_rwqe *wqe; 146 void (*handler)(struct ib_event *, void *); 147 u32 tail; 148 int ret; 149 150 if (qp->ibqp.srq) { 151 srq = ibsrq_to_rvtsrq(qp->ibqp.srq); 152 handler = srq->ibsrq.event_handler; 153 rq = &srq->rq; 154 } else { 155 srq = NULL; 156 handler = NULL; 157 rq = &qp->r_rq; 158 } 159 160 spin_lock_irqsave(&rq->lock, flags); 161 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) { 162 ret = 0; 163 goto unlock; 164 } 165 166 wq = rq->wq; 167 tail = wq->tail; 168 /* Validate tail before using it since it is user writable. */ 169 if (tail >= rq->size) 170 tail = 0; 171 if (unlikely(tail == wq->head)) { 172 ret = 0; 173 goto unlock; 174 } 175 /* Make sure entry is read after head index is read. */ 176 smp_rmb(); 177 wqe = rvt_get_rwqe_ptr(rq, tail); 178 /* 179 * Even though we update the tail index in memory, the verbs 180 * consumer is not supposed to post more entries until a 181 * completion is generated. 182 */ 183 if (++tail >= rq->size) 184 tail = 0; 185 wq->tail = tail; 186 if (!wr_id_only && !qib_init_sge(qp, wqe)) { 187 ret = -1; 188 goto unlock; 189 } 190 qp->r_wr_id = wqe->wr_id; 191 192 ret = 1; 193 set_bit(RVT_R_WRID_VALID, &qp->r_aflags); 194 if (handler) { 195 u32 n; 196 197 /* 198 * Validate head pointer value and compute 199 * the number of remaining WQEs. 200 */ 201 n = wq->head; 202 if (n >= rq->size) 203 n = 0; 204 if (n < tail) 205 n += rq->size - tail; 206 else 207 n -= tail; 208 if (n < srq->limit) { 209 struct ib_event ev; 210 211 srq->limit = 0; 212 spin_unlock_irqrestore(&rq->lock, flags); 213 ev.device = qp->ibqp.device; 214 ev.element.srq = qp->ibqp.srq; 215 ev.event = IB_EVENT_SRQ_LIMIT_REACHED; 216 handler(&ev, srq->ibsrq.srq_context); 217 goto bail; 218 } 219 } 220 unlock: 221 spin_unlock_irqrestore(&rq->lock, flags); 222 bail: 223 return ret; 224 } 225 226 /* 227 * Switch to alternate path. 228 * The QP s_lock should be held and interrupts disabled. 229 */ 230 void qib_migrate_qp(struct rvt_qp *qp) 231 { 232 struct ib_event ev; 233 234 qp->s_mig_state = IB_MIG_MIGRATED; 235 qp->remote_ah_attr = qp->alt_ah_attr; 236 qp->port_num = qp->alt_ah_attr.port_num; 237 qp->s_pkey_index = qp->s_alt_pkey_index; 238 239 ev.device = qp->ibqp.device; 240 ev.element.qp = &qp->ibqp; 241 ev.event = IB_EVENT_PATH_MIG; 242 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); 243 } 244 245 static __be64 get_sguid(struct qib_ibport *ibp, unsigned index) 246 { 247 if (!index) { 248 struct qib_pportdata *ppd = ppd_from_ibp(ibp); 249 250 return ppd->guid; 251 } 252 return ibp->guids[index - 1]; 253 } 254 255 static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id) 256 { 257 return (gid->global.interface_id == id && 258 (gid->global.subnet_prefix == gid_prefix || 259 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX)); 260 } 261 262 /* 263 * 264 * This should be called with the QP r_lock held. 265 * 266 * The s_lock will be acquired around the qib_migrate_qp() call. 267 */ 268 int qib_ruc_check_hdr(struct qib_ibport *ibp, struct qib_ib_header *hdr, 269 int has_grh, struct rvt_qp *qp, u32 bth0) 270 { 271 __be64 guid; 272 unsigned long flags; 273 274 if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) { 275 if (!has_grh) { 276 if (qp->alt_ah_attr.ah_flags & IB_AH_GRH) 277 goto err; 278 } else { 279 if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH)) 280 goto err; 281 guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index); 282 if (!gid_ok(&hdr->u.l.grh.dgid, 283 ibp->rvp.gid_prefix, guid)) 284 goto err; 285 if (!gid_ok(&hdr->u.l.grh.sgid, 286 qp->alt_ah_attr.grh.dgid.global.subnet_prefix, 287 qp->alt_ah_attr.grh.dgid.global.interface_id)) 288 goto err; 289 } 290 if (!qib_pkey_ok((u16)bth0, 291 qib_get_pkey(ibp, qp->s_alt_pkey_index))) { 292 qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY, 293 (u16)bth0, 294 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF, 295 0, qp->ibqp.qp_num, 296 hdr->lrh[3], hdr->lrh[1]); 297 goto err; 298 } 299 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */ 300 if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid || 301 ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num) 302 goto err; 303 spin_lock_irqsave(&qp->s_lock, flags); 304 qib_migrate_qp(qp); 305 spin_unlock_irqrestore(&qp->s_lock, flags); 306 } else { 307 if (!has_grh) { 308 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH) 309 goto err; 310 } else { 311 if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) 312 goto err; 313 guid = get_sguid(ibp, 314 qp->remote_ah_attr.grh.sgid_index); 315 if (!gid_ok(&hdr->u.l.grh.dgid, 316 ibp->rvp.gid_prefix, guid)) 317 goto err; 318 if (!gid_ok(&hdr->u.l.grh.sgid, 319 qp->remote_ah_attr.grh.dgid.global.subnet_prefix, 320 qp->remote_ah_attr.grh.dgid.global.interface_id)) 321 goto err; 322 } 323 if (!qib_pkey_ok((u16)bth0, 324 qib_get_pkey(ibp, qp->s_pkey_index))) { 325 qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY, 326 (u16)bth0, 327 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF, 328 0, qp->ibqp.qp_num, 329 hdr->lrh[3], hdr->lrh[1]); 330 goto err; 331 } 332 /* Validate the SLID. See Ch. 9.6.1.5 */ 333 if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid || 334 ppd_from_ibp(ibp)->port != qp->port_num) 335 goto err; 336 if (qp->s_mig_state == IB_MIG_REARM && 337 !(bth0 & IB_BTH_MIG_REQ)) 338 qp->s_mig_state = IB_MIG_ARMED; 339 } 340 341 return 0; 342 343 err: 344 return 1; 345 } 346 347 /** 348 * qib_ruc_loopback - handle UC and RC lookback requests 349 * @sqp: the sending QP 350 * 351 * This is called from qib_do_send() to 352 * forward a WQE addressed to the same HCA. 353 * Note that although we are single threaded due to the tasklet, we still 354 * have to protect against post_send(). We don't have to worry about 355 * receive interrupts since this is a connected protocol and all packets 356 * will pass through here. 357 */ 358 static void qib_ruc_loopback(struct rvt_qp *sqp) 359 { 360 struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num); 361 struct qib_pportdata *ppd = ppd_from_ibp(ibp); 362 struct qib_devdata *dd = ppd->dd; 363 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi; 364 struct rvt_qp *qp; 365 struct rvt_swqe *wqe; 366 struct rvt_sge *sge; 367 unsigned long flags; 368 struct ib_wc wc; 369 u64 sdata; 370 atomic64_t *maddr; 371 enum ib_wc_status send_status; 372 int release; 373 int ret; 374 375 rcu_read_lock(); 376 /* 377 * Note that we check the responder QP state after 378 * checking the requester's state. 379 */ 380 qp = rvt_lookup_qpn(rdi, &ibp->rvp, sqp->remote_qpn); 381 if (!qp) 382 goto done; 383 384 spin_lock_irqsave(&sqp->s_lock, flags); 385 386 /* Return if we are already busy processing a work request. */ 387 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) || 388 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND)) 389 goto unlock; 390 391 sqp->s_flags |= RVT_S_BUSY; 392 393 again: 394 smp_read_barrier_depends(); /* see post_one_send() */ 395 if (sqp->s_last == ACCESS_ONCE(sqp->s_head)) 396 goto clr_busy; 397 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last); 398 399 /* Return if it is not OK to start a new work reqeust. */ 400 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) { 401 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND)) 402 goto clr_busy; 403 /* We are in the error state, flush the work request. */ 404 send_status = IB_WC_WR_FLUSH_ERR; 405 goto flush_send; 406 } 407 408 /* 409 * We can rely on the entry not changing without the s_lock 410 * being held until we update s_last. 411 * We increment s_cur to indicate s_last is in progress. 412 */ 413 if (sqp->s_last == sqp->s_cur) { 414 if (++sqp->s_cur >= sqp->s_size) 415 sqp->s_cur = 0; 416 } 417 spin_unlock_irqrestore(&sqp->s_lock, flags); 418 419 if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) || 420 qp->ibqp.qp_type != sqp->ibqp.qp_type) { 421 ibp->rvp.n_pkt_drops++; 422 /* 423 * For RC, the requester would timeout and retry so 424 * shortcut the timeouts and just signal too many retries. 425 */ 426 if (sqp->ibqp.qp_type == IB_QPT_RC) 427 send_status = IB_WC_RETRY_EXC_ERR; 428 else 429 send_status = IB_WC_SUCCESS; 430 goto serr; 431 } 432 433 memset(&wc, 0, sizeof(wc)); 434 send_status = IB_WC_SUCCESS; 435 436 release = 1; 437 sqp->s_sge.sge = wqe->sg_list[0]; 438 sqp->s_sge.sg_list = wqe->sg_list + 1; 439 sqp->s_sge.num_sge = wqe->wr.num_sge; 440 sqp->s_len = wqe->length; 441 switch (wqe->wr.opcode) { 442 case IB_WR_SEND_WITH_IMM: 443 wc.wc_flags = IB_WC_WITH_IMM; 444 wc.ex.imm_data = wqe->wr.ex.imm_data; 445 /* FALLTHROUGH */ 446 case IB_WR_SEND: 447 ret = qib_get_rwqe(qp, 0); 448 if (ret < 0) 449 goto op_err; 450 if (!ret) 451 goto rnr_nak; 452 break; 453 454 case IB_WR_RDMA_WRITE_WITH_IMM: 455 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE))) 456 goto inv_err; 457 wc.wc_flags = IB_WC_WITH_IMM; 458 wc.ex.imm_data = wqe->wr.ex.imm_data; 459 ret = qib_get_rwqe(qp, 1); 460 if (ret < 0) 461 goto op_err; 462 if (!ret) 463 goto rnr_nak; 464 /* FALLTHROUGH */ 465 case IB_WR_RDMA_WRITE: 466 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE))) 467 goto inv_err; 468 if (wqe->length == 0) 469 break; 470 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length, 471 wqe->rdma_wr.remote_addr, 472 wqe->rdma_wr.rkey, 473 IB_ACCESS_REMOTE_WRITE))) 474 goto acc_err; 475 qp->r_sge.sg_list = NULL; 476 qp->r_sge.num_sge = 1; 477 qp->r_sge.total_len = wqe->length; 478 break; 479 480 case IB_WR_RDMA_READ: 481 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ))) 482 goto inv_err; 483 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length, 484 wqe->rdma_wr.remote_addr, 485 wqe->rdma_wr.rkey, 486 IB_ACCESS_REMOTE_READ))) 487 goto acc_err; 488 release = 0; 489 sqp->s_sge.sg_list = NULL; 490 sqp->s_sge.num_sge = 1; 491 qp->r_sge.sge = wqe->sg_list[0]; 492 qp->r_sge.sg_list = wqe->sg_list + 1; 493 qp->r_sge.num_sge = wqe->wr.num_sge; 494 qp->r_sge.total_len = wqe->length; 495 break; 496 497 case IB_WR_ATOMIC_CMP_AND_SWP: 498 case IB_WR_ATOMIC_FETCH_AND_ADD: 499 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC))) 500 goto inv_err; 501 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64), 502 wqe->atomic_wr.remote_addr, 503 wqe->atomic_wr.rkey, 504 IB_ACCESS_REMOTE_ATOMIC))) 505 goto acc_err; 506 /* Perform atomic OP and save result. */ 507 maddr = (atomic64_t *) qp->r_sge.sge.vaddr; 508 sdata = wqe->atomic_wr.compare_add; 509 *(u64 *) sqp->s_sge.sge.vaddr = 510 (wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ? 511 (u64) atomic64_add_return(sdata, maddr) - sdata : 512 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr, 513 sdata, wqe->atomic_wr.swap); 514 rvt_put_mr(qp->r_sge.sge.mr); 515 qp->r_sge.num_sge = 0; 516 goto send_comp; 517 518 default: 519 send_status = IB_WC_LOC_QP_OP_ERR; 520 goto serr; 521 } 522 523 sge = &sqp->s_sge.sge; 524 while (sqp->s_len) { 525 u32 len = sqp->s_len; 526 527 if (len > sge->length) 528 len = sge->length; 529 if (len > sge->sge_length) 530 len = sge->sge_length; 531 BUG_ON(len == 0); 532 qib_copy_sge(&qp->r_sge, sge->vaddr, len, release); 533 sge->vaddr += len; 534 sge->length -= len; 535 sge->sge_length -= len; 536 if (sge->sge_length == 0) { 537 if (!release) 538 rvt_put_mr(sge->mr); 539 if (--sqp->s_sge.num_sge) 540 *sge = *sqp->s_sge.sg_list++; 541 } else if (sge->length == 0 && sge->mr->lkey) { 542 if (++sge->n >= RVT_SEGSZ) { 543 if (++sge->m >= sge->mr->mapsz) 544 break; 545 sge->n = 0; 546 } 547 sge->vaddr = 548 sge->mr->map[sge->m]->segs[sge->n].vaddr; 549 sge->length = 550 sge->mr->map[sge->m]->segs[sge->n].length; 551 } 552 sqp->s_len -= len; 553 } 554 if (release) 555 rvt_put_ss(&qp->r_sge); 556 557 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) 558 goto send_comp; 559 560 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM) 561 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM; 562 else 563 wc.opcode = IB_WC_RECV; 564 wc.wr_id = qp->r_wr_id; 565 wc.status = IB_WC_SUCCESS; 566 wc.byte_len = wqe->length; 567 wc.qp = &qp->ibqp; 568 wc.src_qp = qp->remote_qpn; 569 wc.slid = qp->remote_ah_attr.dlid; 570 wc.sl = qp->remote_ah_attr.sl; 571 wc.port_num = 1; 572 /* Signal completion event if the solicited bit is set. */ 573 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 574 wqe->wr.send_flags & IB_SEND_SOLICITED); 575 576 send_comp: 577 spin_lock_irqsave(&sqp->s_lock, flags); 578 ibp->rvp.n_loop_pkts++; 579 flush_send: 580 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt; 581 qib_send_complete(sqp, wqe, send_status); 582 goto again; 583 584 rnr_nak: 585 /* Handle RNR NAK */ 586 if (qp->ibqp.qp_type == IB_QPT_UC) 587 goto send_comp; 588 ibp->rvp.n_rnr_naks++; 589 /* 590 * Note: we don't need the s_lock held since the BUSY flag 591 * makes this single threaded. 592 */ 593 if (sqp->s_rnr_retry == 0) { 594 send_status = IB_WC_RNR_RETRY_EXC_ERR; 595 goto serr; 596 } 597 if (sqp->s_rnr_retry_cnt < 7) 598 sqp->s_rnr_retry--; 599 spin_lock_irqsave(&sqp->s_lock, flags); 600 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK)) 601 goto clr_busy; 602 sqp->s_flags |= RVT_S_WAIT_RNR; 603 sqp->s_timer.function = qib_rc_rnr_retry; 604 sqp->s_timer.expires = jiffies + 605 usecs_to_jiffies(ib_qib_rnr_table[qp->r_min_rnr_timer]); 606 add_timer(&sqp->s_timer); 607 goto clr_busy; 608 609 op_err: 610 send_status = IB_WC_REM_OP_ERR; 611 wc.status = IB_WC_LOC_QP_OP_ERR; 612 goto err; 613 614 inv_err: 615 send_status = IB_WC_REM_INV_REQ_ERR; 616 wc.status = IB_WC_LOC_QP_OP_ERR; 617 goto err; 618 619 acc_err: 620 send_status = IB_WC_REM_ACCESS_ERR; 621 wc.status = IB_WC_LOC_PROT_ERR; 622 err: 623 /* responder goes to error state */ 624 qib_rc_error(qp, wc.status); 625 626 serr: 627 spin_lock_irqsave(&sqp->s_lock, flags); 628 qib_send_complete(sqp, wqe, send_status); 629 if (sqp->ibqp.qp_type == IB_QPT_RC) { 630 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR); 631 632 sqp->s_flags &= ~RVT_S_BUSY; 633 spin_unlock_irqrestore(&sqp->s_lock, flags); 634 if (lastwqe) { 635 struct ib_event ev; 636 637 ev.device = sqp->ibqp.device; 638 ev.element.qp = &sqp->ibqp; 639 ev.event = IB_EVENT_QP_LAST_WQE_REACHED; 640 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context); 641 } 642 goto done; 643 } 644 clr_busy: 645 sqp->s_flags &= ~RVT_S_BUSY; 646 unlock: 647 spin_unlock_irqrestore(&sqp->s_lock, flags); 648 done: 649 rcu_read_unlock(); 650 } 651 652 /** 653 * qib_make_grh - construct a GRH header 654 * @ibp: a pointer to the IB port 655 * @hdr: a pointer to the GRH header being constructed 656 * @grh: the global route address to send to 657 * @hwords: the number of 32 bit words of header being sent 658 * @nwords: the number of 32 bit words of data being sent 659 * 660 * Return the size of the header in 32 bit words. 661 */ 662 u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr, 663 struct ib_global_route *grh, u32 hwords, u32 nwords) 664 { 665 hdr->version_tclass_flow = 666 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) | 667 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) | 668 (grh->flow_label << IB_GRH_FLOW_SHIFT)); 669 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2); 670 /* next_hdr is defined by C8-7 in ch. 8.4.1 */ 671 hdr->next_hdr = IB_GRH_NEXT_HDR; 672 hdr->hop_limit = grh->hop_limit; 673 /* The SGID is 32-bit aligned. */ 674 hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix; 675 hdr->sgid.global.interface_id = grh->sgid_index ? 676 ibp->guids[grh->sgid_index - 1] : ppd_from_ibp(ibp)->guid; 677 hdr->dgid = grh->dgid; 678 679 /* GRH header size in 32-bit words. */ 680 return sizeof(struct ib_grh) / sizeof(u32); 681 } 682 683 void qib_make_ruc_header(struct rvt_qp *qp, struct qib_other_headers *ohdr, 684 u32 bth0, u32 bth2) 685 { 686 struct qib_qp_priv *priv = qp->priv; 687 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num); 688 u16 lrh0; 689 u32 nwords; 690 u32 extra_bytes; 691 692 /* Construct the header. */ 693 extra_bytes = -qp->s_cur_size & 3; 694 nwords = (qp->s_cur_size + extra_bytes) >> 2; 695 lrh0 = QIB_LRH_BTH; 696 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) { 697 qp->s_hdrwords += qib_make_grh(ibp, &priv->s_hdr->u.l.grh, 698 &qp->remote_ah_attr.grh, 699 qp->s_hdrwords, nwords); 700 lrh0 = QIB_LRH_GRH; 701 } 702 lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 | 703 qp->remote_ah_attr.sl << 4; 704 priv->s_hdr->lrh[0] = cpu_to_be16(lrh0); 705 priv->s_hdr->lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid); 706 priv->s_hdr->lrh[2] = 707 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC); 708 priv->s_hdr->lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid | 709 qp->remote_ah_attr.src_path_bits); 710 bth0 |= qib_get_pkey(ibp, qp->s_pkey_index); 711 bth0 |= extra_bytes << 20; 712 if (qp->s_mig_state == IB_MIG_MIGRATED) 713 bth0 |= IB_BTH_MIG_REQ; 714 ohdr->bth[0] = cpu_to_be32(bth0); 715 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn); 716 ohdr->bth[2] = cpu_to_be32(bth2); 717 this_cpu_inc(ibp->pmastats->n_unicast_xmit); 718 } 719 720 void _qib_do_send(struct work_struct *work) 721 { 722 struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv, 723 s_work); 724 struct rvt_qp *qp = priv->owner; 725 726 qib_do_send(qp); 727 } 728 729 /** 730 * qib_do_send - perform a send on a QP 731 * @qp: pointer to the QP 732 * 733 * Process entries in the send work queue until credit or queue is 734 * exhausted. Only allow one CPU to send a packet per QP (tasklet). 735 * Otherwise, two threads could send packets out of order. 736 */ 737 void qib_do_send(struct rvt_qp *qp) 738 { 739 struct qib_qp_priv *priv = qp->priv; 740 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num); 741 struct qib_pportdata *ppd = ppd_from_ibp(ibp); 742 int (*make_req)(struct rvt_qp *qp, unsigned long *flags); 743 unsigned long flags; 744 745 if ((qp->ibqp.qp_type == IB_QPT_RC || 746 qp->ibqp.qp_type == IB_QPT_UC) && 747 (qp->remote_ah_attr.dlid & ~((1 << ppd->lmc) - 1)) == ppd->lid) { 748 qib_ruc_loopback(qp); 749 return; 750 } 751 752 if (qp->ibqp.qp_type == IB_QPT_RC) 753 make_req = qib_make_rc_req; 754 else if (qp->ibqp.qp_type == IB_QPT_UC) 755 make_req = qib_make_uc_req; 756 else 757 make_req = qib_make_ud_req; 758 759 spin_lock_irqsave(&qp->s_lock, flags); 760 761 /* Return if we are already busy processing a work request. */ 762 if (!qib_send_ok(qp)) { 763 spin_unlock_irqrestore(&qp->s_lock, flags); 764 return; 765 } 766 767 qp->s_flags |= RVT_S_BUSY; 768 769 do { 770 /* Check for a constructed packet to be sent. */ 771 if (qp->s_hdrwords != 0) { 772 spin_unlock_irqrestore(&qp->s_lock, flags); 773 /* 774 * If the packet cannot be sent now, return and 775 * the send tasklet will be woken up later. 776 */ 777 if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords, 778 qp->s_cur_sge, qp->s_cur_size)) 779 return; 780 /* Record that s_hdr is empty. */ 781 qp->s_hdrwords = 0; 782 spin_lock_irqsave(&qp->s_lock, flags); 783 } 784 } while (make_req(qp, &flags)); 785 786 spin_unlock_irqrestore(&qp->s_lock, flags); 787 } 788 789 /* 790 * This should be called with s_lock held. 791 */ 792 void qib_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe, 793 enum ib_wc_status status) 794 { 795 u32 old_last, last; 796 unsigned i; 797 798 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND)) 799 return; 800 801 last = qp->s_last; 802 old_last = last; 803 if (++last >= qp->s_size) 804 last = 0; 805 qp->s_last = last; 806 /* See post_send() */ 807 barrier(); 808 for (i = 0; i < wqe->wr.num_sge; i++) { 809 struct rvt_sge *sge = &wqe->sg_list[i]; 810 811 rvt_put_mr(sge->mr); 812 } 813 if (qp->ibqp.qp_type == IB_QPT_UD || 814 qp->ibqp.qp_type == IB_QPT_SMI || 815 qp->ibqp.qp_type == IB_QPT_GSI) 816 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount); 817 818 /* See ch. 11.2.4.1 and 10.7.3.1 */ 819 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) || 820 (wqe->wr.send_flags & IB_SEND_SIGNALED) || 821 status != IB_WC_SUCCESS) { 822 struct ib_wc wc; 823 824 memset(&wc, 0, sizeof(wc)); 825 wc.wr_id = wqe->wr.wr_id; 826 wc.status = status; 827 wc.opcode = ib_qib_wc_opcode[wqe->wr.opcode]; 828 wc.qp = &qp->ibqp; 829 if (status == IB_WC_SUCCESS) 830 wc.byte_len = wqe->length; 831 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc, 832 status != IB_WC_SUCCESS); 833 } 834 835 if (qp->s_acked == old_last) 836 qp->s_acked = last; 837 if (qp->s_cur == old_last) 838 qp->s_cur = last; 839 if (qp->s_tail == old_last) 840 qp->s_tail = last; 841 if (qp->state == IB_QPS_SQD && last == qp->s_cur) 842 qp->s_draining = 0; 843 } 844