ipath_ud.c 17 KB

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