ipath_ud.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <rdma/ib_smi.h>
  33. #include "ipath_verbs.h"
  34. #include "ips_common.h"
  35. /**
  36. * ipath_ud_loopback - handle send on loopback QPs
  37. * @sqp: the QP
  38. * @ss: the SGE state
  39. * @length: the length of the data to send
  40. * @wr: the work request
  41. * @wc: the work completion entry
  42. *
  43. * This is called from ipath_post_ud_send() to forward a WQE addressed
  44. * to the same HCA.
  45. */
  46. static void ipath_ud_loopback(struct ipath_qp *sqp, struct ipath_sge_state *ss,
  47. u32 length, struct ib_send_wr *wr, struct ib_wc *wc)
  48. {
  49. struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
  50. struct ipath_qp *qp;
  51. struct ib_ah_attr *ah_attr;
  52. unsigned long flags;
  53. struct ipath_rq *rq;
  54. struct ipath_srq *srq;
  55. struct ipath_sge_state rsge;
  56. struct ipath_sge *sge;
  57. struct ipath_rwqe *wqe;
  58. qp = ipath_lookup_qpn(&dev->qp_table, wr->wr.ud.remote_qpn);
  59. if (!qp)
  60. return;
  61. /*
  62. * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
  63. * Qkeys with the high order bit set mean use the
  64. * qkey from the QP context instead of the WR (see 10.2.5).
  65. */
  66. if (unlikely(qp->ibqp.qp_num &&
  67. ((int) wr->wr.ud.remote_qkey < 0
  68. ? qp->qkey : wr->wr.ud.remote_qkey) != qp->qkey)) {
  69. /* XXX OK to lose a count once in a while. */
  70. dev->qkey_violations++;
  71. dev->n_pkt_drops++;
  72. goto done;
  73. }
  74. /*
  75. * A GRH is expected to preceed the data even if not
  76. * present on the wire.
  77. */
  78. wc->byte_len = length + sizeof(struct ib_grh);
  79. if (wr->opcode == IB_WR_SEND_WITH_IMM) {
  80. wc->wc_flags = IB_WC_WITH_IMM;
  81. wc->imm_data = wr->imm_data;
  82. } else {
  83. wc->wc_flags = 0;
  84. wc->imm_data = 0;
  85. }
  86. /*
  87. * Get the next work request entry to find where to put the data.
  88. * Note that it is safe to drop the lock after changing rq->tail
  89. * since ipath_post_receive() won't fill the empty slot.
  90. */
  91. if (qp->ibqp.srq) {
  92. srq = to_isrq(qp->ibqp.srq);
  93. rq = &srq->rq;
  94. } else {
  95. srq = NULL;
  96. rq = &qp->r_rq;
  97. }
  98. spin_lock_irqsave(&rq->lock, flags);
  99. if (rq->tail == rq->head) {
  100. spin_unlock_irqrestore(&rq->lock, flags);
  101. dev->n_pkt_drops++;
  102. goto done;
  103. }
  104. /* Silently drop packets which are too big. */
  105. wqe = get_rwqe_ptr(rq, rq->tail);
  106. if (wc->byte_len > wqe->length) {
  107. spin_unlock_irqrestore(&rq->lock, flags);
  108. dev->n_pkt_drops++;
  109. goto done;
  110. }
  111. wc->wr_id = wqe->wr_id;
  112. rsge.sge = wqe->sg_list[0];
  113. rsge.sg_list = wqe->sg_list + 1;
  114. rsge.num_sge = wqe->num_sge;
  115. if (++rq->tail >= rq->size)
  116. rq->tail = 0;
  117. if (srq && srq->ibsrq.event_handler) {
  118. u32 n;
  119. if (rq->head < rq->tail)
  120. n = rq->size + rq->head - rq->tail;
  121. else
  122. n = rq->head - rq->tail;
  123. if (n < srq->limit) {
  124. struct ib_event ev;
  125. srq->limit = 0;
  126. spin_unlock_irqrestore(&rq->lock, flags);
  127. ev.device = qp->ibqp.device;
  128. ev.element.srq = qp->ibqp.srq;
  129. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  130. srq->ibsrq.event_handler(&ev,
  131. srq->ibsrq.srq_context);
  132. } else
  133. spin_unlock_irqrestore(&rq->lock, flags);
  134. } else
  135. spin_unlock_irqrestore(&rq->lock, flags);
  136. ah_attr = &to_iah(wr->wr.ud.ah)->attr;
  137. if (ah_attr->ah_flags & IB_AH_GRH) {
  138. ipath_copy_sge(&rsge, &ah_attr->grh, sizeof(struct ib_grh));
  139. wc->wc_flags |= IB_WC_GRH;
  140. } else
  141. ipath_skip_sge(&rsge, sizeof(struct ib_grh));
  142. sge = &ss->sge;
  143. while (length) {
  144. u32 len = sge->length;
  145. if (len > length)
  146. len = length;
  147. BUG_ON(len == 0);
  148. ipath_copy_sge(&rsge, sge->vaddr, len);
  149. sge->vaddr += len;
  150. sge->length -= len;
  151. sge->sge_length -= len;
  152. if (sge->sge_length == 0) {
  153. if (--ss->num_sge)
  154. *sge = *ss->sg_list++;
  155. } else if (sge->length == 0 && sge->mr != NULL) {
  156. if (++sge->n >= IPATH_SEGSZ) {
  157. if (++sge->m >= sge->mr->mapsz)
  158. break;
  159. sge->n = 0;
  160. }
  161. sge->vaddr =
  162. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  163. sge->length =
  164. sge->mr->map[sge->m]->segs[sge->n].length;
  165. }
  166. length -= len;
  167. }
  168. wc->status = IB_WC_SUCCESS;
  169. wc->opcode = IB_WC_RECV;
  170. wc->vendor_err = 0;
  171. wc->qp_num = qp->ibqp.qp_num;
  172. wc->src_qp = sqp->ibqp.qp_num;
  173. /* XXX do we know which pkey matched? Only needed for GSI. */
  174. wc->pkey_index = 0;
  175. wc->slid = ipath_layer_get_lid(dev->dd) |
  176. (ah_attr->src_path_bits &
  177. ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1));
  178. wc->sl = ah_attr->sl;
  179. wc->dlid_path_bits =
  180. ah_attr->dlid & ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  181. /* Signal completion event if the solicited bit is set. */
  182. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), wc,
  183. wr->send_flags & IB_SEND_SOLICITED);
  184. done:
  185. if (atomic_dec_and_test(&qp->refcount))
  186. wake_up(&qp->wait);
  187. }
  188. /**
  189. * ipath_post_ud_send - post a UD send on QP
  190. * @qp: the QP
  191. * @wr: the work request
  192. *
  193. * Note that we actually send the data as it is posted instead of putting
  194. * the request into a ring buffer. If we wanted to use a ring buffer,
  195. * we would need to save a reference to the destination address in the SWQE.
  196. */
  197. int ipath_post_ud_send(struct ipath_qp *qp, struct ib_send_wr *wr)
  198. {
  199. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  200. struct ipath_other_headers *ohdr;
  201. struct ib_ah_attr *ah_attr;
  202. struct ipath_sge_state ss;
  203. struct ipath_sge *sg_list;
  204. struct ib_wc wc;
  205. u32 hwords;
  206. u32 nwords;
  207. u32 len;
  208. u32 extra_bytes;
  209. u32 bth0;
  210. u16 lrh0;
  211. u16 lid;
  212. int i;
  213. int ret;
  214. if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK)) {
  215. ret = 0;
  216. goto bail;
  217. }
  218. /* IB spec says that num_sge == 0 is OK. */
  219. if (wr->num_sge > qp->s_max_sge) {
  220. ret = -EINVAL;
  221. goto bail;
  222. }
  223. if (wr->num_sge > 1) {
  224. sg_list = kmalloc((qp->s_max_sge - 1) * sizeof(*sg_list),
  225. GFP_ATOMIC);
  226. if (!sg_list) {
  227. ret = -ENOMEM;
  228. goto bail;
  229. }
  230. } else
  231. sg_list = NULL;
  232. /* Check the buffer to send. */
  233. ss.sg_list = sg_list;
  234. ss.sge.mr = NULL;
  235. ss.sge.vaddr = NULL;
  236. ss.sge.length = 0;
  237. ss.sge.sge_length = 0;
  238. ss.num_sge = 0;
  239. len = 0;
  240. for (i = 0; i < wr->num_sge; i++) {
  241. /* Check LKEY */
  242. if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
  243. ret = -EINVAL;
  244. goto bail;
  245. }
  246. if (wr->sg_list[i].length == 0)
  247. continue;
  248. if (!ipath_lkey_ok(&dev->lk_table, ss.num_sge ?
  249. sg_list + ss.num_sge - 1 : &ss.sge,
  250. &wr->sg_list[i], 0)) {
  251. ret = -EINVAL;
  252. goto bail;
  253. }
  254. len += wr->sg_list[i].length;
  255. ss.num_sge++;
  256. }
  257. extra_bytes = (4 - len) & 3;
  258. nwords = (len + extra_bytes) >> 2;
  259. /* Construct the header. */
  260. ah_attr = &to_iah(wr->wr.ud.ah)->attr;
  261. if (ah_attr->dlid == 0) {
  262. ret = -EINVAL;
  263. goto bail;
  264. }
  265. if (ah_attr->dlid >= IPS_MULTICAST_LID_BASE) {
  266. if (ah_attr->dlid != IPS_PERMISSIVE_LID)
  267. dev->n_multicast_xmit++;
  268. else
  269. dev->n_unicast_xmit++;
  270. } else {
  271. dev->n_unicast_xmit++;
  272. lid = ah_attr->dlid &
  273. ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  274. if (unlikely(lid == ipath_layer_get_lid(dev->dd))) {
  275. /*
  276. * Pass in an uninitialized ib_wc to save stack
  277. * space.
  278. */
  279. ipath_ud_loopback(qp, &ss, len, wr, &wc);
  280. goto done;
  281. }
  282. }
  283. if (ah_attr->ah_flags & IB_AH_GRH) {
  284. /* Header size in 32-bit words. */
  285. hwords = 17;
  286. lrh0 = IPS_LRH_GRH;
  287. ohdr = &qp->s_hdr.u.l.oth;
  288. qp->s_hdr.u.l.grh.version_tclass_flow =
  289. cpu_to_be32((6 << 28) |
  290. (ah_attr->grh.traffic_class << 20) |
  291. ah_attr->grh.flow_label);
  292. qp->s_hdr.u.l.grh.paylen =
  293. cpu_to_be16(((wr->opcode ==
  294. IB_WR_SEND_WITH_IMM ? 6 : 5) +
  295. nwords + SIZE_OF_CRC) << 2);
  296. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  297. qp->s_hdr.u.l.grh.next_hdr = 0x1B;
  298. qp->s_hdr.u.l.grh.hop_limit = ah_attr->grh.hop_limit;
  299. /* The SGID is 32-bit aligned. */
  300. qp->s_hdr.u.l.grh.sgid.global.subnet_prefix =
  301. dev->gid_prefix;
  302. qp->s_hdr.u.l.grh.sgid.global.interface_id =
  303. ipath_layer_get_guid(dev->dd);
  304. qp->s_hdr.u.l.grh.dgid = ah_attr->grh.dgid;
  305. /*
  306. * Don't worry about sending to locally attached multicast
  307. * QPs. It is unspecified by the spec. what happens.
  308. */
  309. } else {
  310. /* Header size in 32-bit words. */
  311. hwords = 7;
  312. lrh0 = IPS_LRH_BTH;
  313. ohdr = &qp->s_hdr.u.oth;
  314. }
  315. if (wr->opcode == IB_WR_SEND_WITH_IMM) {
  316. ohdr->u.ud.imm_data = wr->imm_data;
  317. wc.imm_data = wr->imm_data;
  318. hwords += 1;
  319. bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
  320. } else if (wr->opcode == IB_WR_SEND) {
  321. wc.imm_data = 0;
  322. bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
  323. } else {
  324. ret = -EINVAL;
  325. goto bail;
  326. }
  327. lrh0 |= ah_attr->sl << 4;
  328. if (qp->ibqp.qp_type == IB_QPT_SMI)
  329. lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
  330. qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
  331. qp->s_hdr.lrh[1] = cpu_to_be16(ah_attr->dlid); /* DEST LID */
  332. qp->s_hdr.lrh[2] = cpu_to_be16(hwords + nwords + SIZE_OF_CRC);
  333. lid = ipath_layer_get_lid(dev->dd);
  334. if (lid) {
  335. lid |= ah_attr->src_path_bits &
  336. ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  337. qp->s_hdr.lrh[3] = cpu_to_be16(lid);
  338. } else
  339. qp->s_hdr.lrh[3] = IB_LID_PERMISSIVE;
  340. if (wr->send_flags & IB_SEND_SOLICITED)
  341. bth0 |= 1 << 23;
  342. bth0 |= extra_bytes << 20;
  343. bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? IPS_DEFAULT_P_KEY :
  344. ipath_layer_get_pkey(dev->dd, qp->s_pkey_index);
  345. ohdr->bth[0] = cpu_to_be32(bth0);
  346. /*
  347. * Use the multicast QP if the destination LID is a multicast LID.
  348. */
  349. ohdr->bth[1] = ah_attr->dlid >= IPS_MULTICAST_LID_BASE &&
  350. ah_attr->dlid != IPS_PERMISSIVE_LID ?
  351. __constant_cpu_to_be32(IPS_MULTICAST_QPN) :
  352. cpu_to_be32(wr->wr.ud.remote_qpn);
  353. /* XXX Could lose a PSN count but not worth locking */
  354. ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & IPS_PSN_MASK);
  355. /*
  356. * Qkeys with the high order bit set mean use the
  357. * qkey from the QP context instead of the WR (see 10.2.5).
  358. */
  359. ohdr->u.ud.deth[0] = cpu_to_be32((int)wr->wr.ud.remote_qkey < 0 ?
  360. qp->qkey : wr->wr.ud.remote_qkey);
  361. ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
  362. if (ipath_verbs_send(dev->dd, hwords, (u32 *) &qp->s_hdr,
  363. len, &ss))
  364. dev->n_no_piobuf++;
  365. done:
  366. /* Queue the completion status entry. */
  367. if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &qp->s_flags) ||
  368. (wr->send_flags & IB_SEND_SIGNALED)) {
  369. wc.wr_id = wr->wr_id;
  370. wc.status = IB_WC_SUCCESS;
  371. wc.vendor_err = 0;
  372. wc.opcode = IB_WC_SEND;
  373. wc.byte_len = len;
  374. wc.qp_num = qp->ibqp.qp_num;
  375. wc.src_qp = 0;
  376. wc.wc_flags = 0;
  377. /* XXX initialize other fields? */
  378. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
  379. }
  380. kfree(sg_list);
  381. ret = 0;
  382. bail:
  383. return ret;
  384. }
  385. /**
  386. * ipath_ud_rcv - receive an incoming UD packet
  387. * @dev: the device the packet came in on
  388. * @hdr: the packet header
  389. * @has_grh: true if the packet has a GRH
  390. * @data: the packet data
  391. * @tlen: the packet length
  392. * @qp: the QP the packet came on
  393. *
  394. * This is called from ipath_qp_rcv() to process an incoming UD packet
  395. * for the given QP.
  396. * Called at interrupt level.
  397. */
  398. void ipath_ud_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
  399. int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
  400. {
  401. struct ipath_other_headers *ohdr;
  402. int opcode;
  403. u32 hdrsize;
  404. u32 pad;
  405. unsigned long flags;
  406. struct ib_wc wc;
  407. u32 qkey;
  408. u32 src_qp;
  409. struct ipath_rq *rq;
  410. struct ipath_srq *srq;
  411. struct ipath_rwqe *wqe;
  412. u16 dlid;
  413. int header_in_data;
  414. /* Check for GRH */
  415. if (!has_grh) {
  416. ohdr = &hdr->u.oth;
  417. hdrsize = 8 + 12 + 8; /* LRH + BTH + DETH */
  418. qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
  419. src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
  420. header_in_data = 0;
  421. } else {
  422. ohdr = &hdr->u.l.oth;
  423. hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
  424. /*
  425. * The header with GRH is 68 bytes and the core driver sets
  426. * the eager header buffer size to 56 bytes so the last 12
  427. * bytes of the IB header is in the data buffer.
  428. */
  429. header_in_data =
  430. ipath_layer_get_rcvhdrentsize(dev->dd) == 16;
  431. if (header_in_data) {
  432. qkey = be32_to_cpu(((__be32 *) data)[1]);
  433. src_qp = be32_to_cpu(((__be32 *) data)[2]);
  434. data += 12;
  435. } else {
  436. qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
  437. src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
  438. }
  439. }
  440. src_qp &= IPS_QPN_MASK;
  441. /*
  442. * Check that the permissive LID is only used on QP0
  443. * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
  444. */
  445. if (qp->ibqp.qp_num) {
  446. if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
  447. hdr->lrh[3] == IB_LID_PERMISSIVE)) {
  448. dev->n_pkt_drops++;
  449. goto bail;
  450. }
  451. if (unlikely(qkey != qp->qkey)) {
  452. /* XXX OK to lose a count once in a while. */
  453. dev->qkey_violations++;
  454. dev->n_pkt_drops++;
  455. goto bail;
  456. }
  457. } else if (hdr->lrh[1] == IB_LID_PERMISSIVE ||
  458. hdr->lrh[3] == IB_LID_PERMISSIVE) {
  459. struct ib_smp *smp = (struct ib_smp *) data;
  460. if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  461. dev->n_pkt_drops++;
  462. goto bail;
  463. }
  464. }
  465. /* Get the number of bytes the message was padded by. */
  466. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  467. if (unlikely(tlen < (hdrsize + pad + 4))) {
  468. /* Drop incomplete packets. */
  469. dev->n_pkt_drops++;
  470. goto bail;
  471. }
  472. tlen -= hdrsize + pad + 4;
  473. /* Drop invalid MAD packets (see 13.5.3.1). */
  474. if (unlikely((qp->ibqp.qp_num == 0 &&
  475. (tlen != 256 ||
  476. (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)) ||
  477. (qp->ibqp.qp_num == 1 &&
  478. (tlen != 256 ||
  479. (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))) {
  480. dev->n_pkt_drops++;
  481. goto bail;
  482. }
  483. /*
  484. * A GRH is expected to preceed the data even if not
  485. * present on the wire.
  486. */
  487. wc.byte_len = tlen + sizeof(struct ib_grh);
  488. /*
  489. * The opcode is in the low byte when its in network order
  490. * (top byte when in host order).
  491. */
  492. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  493. if (qp->ibqp.qp_num > 1 &&
  494. opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
  495. if (header_in_data) {
  496. wc.imm_data = *(__be32 *) data;
  497. data += sizeof(__be32);
  498. } else
  499. wc.imm_data = ohdr->u.ud.imm_data;
  500. wc.wc_flags = IB_WC_WITH_IMM;
  501. hdrsize += sizeof(u32);
  502. } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
  503. wc.imm_data = 0;
  504. wc.wc_flags = 0;
  505. } else {
  506. dev->n_pkt_drops++;
  507. goto bail;
  508. }
  509. /*
  510. * Get the next work request entry to find where to put the data.
  511. * Note that it is safe to drop the lock after changing rq->tail
  512. * since ipath_post_receive() won't fill the empty slot.
  513. */
  514. if (qp->ibqp.srq) {
  515. srq = to_isrq(qp->ibqp.srq);
  516. rq = &srq->rq;
  517. } else {
  518. srq = NULL;
  519. rq = &qp->r_rq;
  520. }
  521. spin_lock_irqsave(&rq->lock, flags);
  522. if (rq->tail == rq->head) {
  523. spin_unlock_irqrestore(&rq->lock, flags);
  524. dev->n_pkt_drops++;
  525. goto bail;
  526. }
  527. /* Silently drop packets which are too big. */
  528. wqe = get_rwqe_ptr(rq, rq->tail);
  529. if (wc.byte_len > wqe->length) {
  530. spin_unlock_irqrestore(&rq->lock, flags);
  531. dev->n_pkt_drops++;
  532. goto bail;
  533. }
  534. wc.wr_id = wqe->wr_id;
  535. qp->r_sge.sge = wqe->sg_list[0];
  536. qp->r_sge.sg_list = wqe->sg_list + 1;
  537. qp->r_sge.num_sge = wqe->num_sge;
  538. if (++rq->tail >= rq->size)
  539. rq->tail = 0;
  540. if (srq && srq->ibsrq.event_handler) {
  541. u32 n;
  542. if (rq->head < rq->tail)
  543. n = rq->size + rq->head - rq->tail;
  544. else
  545. n = rq->head - rq->tail;
  546. if (n < srq->limit) {
  547. struct ib_event ev;
  548. srq->limit = 0;
  549. spin_unlock_irqrestore(&rq->lock, flags);
  550. ev.device = qp->ibqp.device;
  551. ev.element.srq = qp->ibqp.srq;
  552. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  553. srq->ibsrq.event_handler(&ev,
  554. srq->ibsrq.srq_context);
  555. } else
  556. spin_unlock_irqrestore(&rq->lock, flags);
  557. } else
  558. spin_unlock_irqrestore(&rq->lock, flags);
  559. if (has_grh) {
  560. ipath_copy_sge(&qp->r_sge, &hdr->u.l.grh,
  561. sizeof(struct ib_grh));
  562. wc.wc_flags |= IB_WC_GRH;
  563. } else
  564. ipath_skip_sge(&qp->r_sge, sizeof(struct ib_grh));
  565. ipath_copy_sge(&qp->r_sge, data,
  566. wc.byte_len - sizeof(struct ib_grh));
  567. wc.status = IB_WC_SUCCESS;
  568. wc.opcode = IB_WC_RECV;
  569. wc.vendor_err = 0;
  570. wc.qp_num = qp->ibqp.qp_num;
  571. wc.src_qp = src_qp;
  572. /* XXX do we know which pkey matched? Only needed for GSI. */
  573. wc.pkey_index = 0;
  574. wc.slid = be16_to_cpu(hdr->lrh[3]);
  575. wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
  576. dlid = be16_to_cpu(hdr->lrh[1]);
  577. /*
  578. * Save the LMC lower bits if the destination LID is a unicast LID.
  579. */
  580. wc.dlid_path_bits = dlid >= IPS_MULTICAST_LID_BASE ? 0 :
  581. dlid & ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  582. /* Signal completion event if the solicited bit is set. */
  583. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  584. (ohdr->bth[0] &
  585. __constant_cpu_to_be32(1 << 23)) != 0);
  586. bail:;
  587. }