ipath_ud.c 17 KB

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