ipath_ud.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 "ips_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. extra_bytes = (4 - len) & 3;
  261. nwords = (len + extra_bytes) >> 2;
  262. /* Construct the header. */
  263. ah_attr = &to_iah(wr->wr.ud.ah)->attr;
  264. if (ah_attr->dlid == 0) {
  265. ret = -EINVAL;
  266. goto bail;
  267. }
  268. if (ah_attr->dlid >= IPS_MULTICAST_LID_BASE) {
  269. if (ah_attr->dlid != IPS_PERMISSIVE_LID)
  270. dev->n_multicast_xmit++;
  271. else
  272. dev->n_unicast_xmit++;
  273. } else {
  274. dev->n_unicast_xmit++;
  275. lid = ah_attr->dlid &
  276. ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  277. if (unlikely(lid == ipath_layer_get_lid(dev->dd))) {
  278. /*
  279. * Pass in an uninitialized ib_wc to save stack
  280. * space.
  281. */
  282. ipath_ud_loopback(qp, &ss, len, wr, &wc);
  283. goto done;
  284. }
  285. }
  286. if (ah_attr->ah_flags & IB_AH_GRH) {
  287. /* Header size in 32-bit words. */
  288. hwords = 17;
  289. lrh0 = IPS_LRH_GRH;
  290. ohdr = &qp->s_hdr.u.l.oth;
  291. qp->s_hdr.u.l.grh.version_tclass_flow =
  292. cpu_to_be32((6 << 28) |
  293. (ah_attr->grh.traffic_class << 20) |
  294. ah_attr->grh.flow_label);
  295. qp->s_hdr.u.l.grh.paylen =
  296. cpu_to_be16(((wr->opcode ==
  297. IB_WR_SEND_WITH_IMM ? 6 : 5) +
  298. nwords + SIZE_OF_CRC) << 2);
  299. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  300. qp->s_hdr.u.l.grh.next_hdr = 0x1B;
  301. qp->s_hdr.u.l.grh.hop_limit = ah_attr->grh.hop_limit;
  302. /* The SGID is 32-bit aligned. */
  303. qp->s_hdr.u.l.grh.sgid.global.subnet_prefix =
  304. dev->gid_prefix;
  305. qp->s_hdr.u.l.grh.sgid.global.interface_id =
  306. ipath_layer_get_guid(dev->dd);
  307. qp->s_hdr.u.l.grh.dgid = ah_attr->grh.dgid;
  308. /*
  309. * Don't worry about sending to locally attached multicast
  310. * QPs. It is unspecified by the spec. what happens.
  311. */
  312. } else {
  313. /* Header size in 32-bit words. */
  314. hwords = 7;
  315. lrh0 = IPS_LRH_BTH;
  316. ohdr = &qp->s_hdr.u.oth;
  317. }
  318. if (wr->opcode == IB_WR_SEND_WITH_IMM) {
  319. ohdr->u.ud.imm_data = wr->imm_data;
  320. wc.imm_data = wr->imm_data;
  321. hwords += 1;
  322. bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
  323. } else if (wr->opcode == IB_WR_SEND) {
  324. wc.imm_data = 0;
  325. bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
  326. } else {
  327. ret = -EINVAL;
  328. goto bail;
  329. }
  330. lrh0 |= ah_attr->sl << 4;
  331. if (qp->ibqp.qp_type == IB_QPT_SMI)
  332. lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
  333. qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
  334. qp->s_hdr.lrh[1] = cpu_to_be16(ah_attr->dlid); /* DEST LID */
  335. qp->s_hdr.lrh[2] = cpu_to_be16(hwords + nwords + SIZE_OF_CRC);
  336. lid = ipath_layer_get_lid(dev->dd);
  337. if (lid) {
  338. lid |= ah_attr->src_path_bits &
  339. ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  340. qp->s_hdr.lrh[3] = cpu_to_be16(lid);
  341. } else
  342. qp->s_hdr.lrh[3] = IB_LID_PERMISSIVE;
  343. if (wr->send_flags & IB_SEND_SOLICITED)
  344. bth0 |= 1 << 23;
  345. bth0 |= extra_bytes << 20;
  346. bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? IPS_DEFAULT_P_KEY :
  347. ipath_layer_get_pkey(dev->dd, qp->s_pkey_index);
  348. ohdr->bth[0] = cpu_to_be32(bth0);
  349. /*
  350. * Use the multicast QP if the destination LID is a multicast LID.
  351. */
  352. ohdr->bth[1] = ah_attr->dlid >= IPS_MULTICAST_LID_BASE &&
  353. ah_attr->dlid != IPS_PERMISSIVE_LID ?
  354. __constant_cpu_to_be32(IPS_MULTICAST_QPN) :
  355. cpu_to_be32(wr->wr.ud.remote_qpn);
  356. /* XXX Could lose a PSN count but not worth locking */
  357. ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & IPS_PSN_MASK);
  358. /*
  359. * Qkeys with the high order bit set mean use the
  360. * qkey from the QP context instead of the WR (see 10.2.5).
  361. */
  362. ohdr->u.ud.deth[0] = cpu_to_be32((int)wr->wr.ud.remote_qkey < 0 ?
  363. qp->qkey : wr->wr.ud.remote_qkey);
  364. ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
  365. if (ipath_verbs_send(dev->dd, hwords, (u32 *) &qp->s_hdr,
  366. len, &ss))
  367. dev->n_no_piobuf++;
  368. done:
  369. /* Queue the completion status entry. */
  370. if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &qp->s_flags) ||
  371. (wr->send_flags & IB_SEND_SIGNALED)) {
  372. wc.wr_id = wr->wr_id;
  373. wc.status = IB_WC_SUCCESS;
  374. wc.vendor_err = 0;
  375. wc.opcode = IB_WC_SEND;
  376. wc.byte_len = len;
  377. wc.qp_num = qp->ibqp.qp_num;
  378. wc.src_qp = 0;
  379. wc.wc_flags = 0;
  380. /* XXX initialize other fields? */
  381. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
  382. }
  383. kfree(sg_list);
  384. ret = 0;
  385. bail:
  386. return ret;
  387. }
  388. /**
  389. * ipath_ud_rcv - receive an incoming UD packet
  390. * @dev: the device the packet came in on
  391. * @hdr: the packet header
  392. * @has_grh: true if the packet has a GRH
  393. * @data: the packet data
  394. * @tlen: the packet length
  395. * @qp: the QP the packet came on
  396. *
  397. * This is called from ipath_qp_rcv() to process an incoming UD packet
  398. * for the given QP.
  399. * Called at interrupt level.
  400. */
  401. void ipath_ud_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
  402. int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
  403. {
  404. struct ipath_other_headers *ohdr;
  405. int opcode;
  406. u32 hdrsize;
  407. u32 pad;
  408. unsigned long flags;
  409. struct ib_wc wc;
  410. u32 qkey;
  411. u32 src_qp;
  412. struct ipath_rq *rq;
  413. struct ipath_srq *srq;
  414. struct ipath_rwqe *wqe;
  415. u16 dlid;
  416. int header_in_data;
  417. /* Check for GRH */
  418. if (!has_grh) {
  419. ohdr = &hdr->u.oth;
  420. hdrsize = 8 + 12 + 8; /* LRH + BTH + DETH */
  421. qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
  422. src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
  423. header_in_data = 0;
  424. } else {
  425. ohdr = &hdr->u.l.oth;
  426. hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
  427. /*
  428. * The header with GRH is 68 bytes and the core driver sets
  429. * the eager header buffer size to 56 bytes so the last 12
  430. * bytes of the IB header is in the data buffer.
  431. */
  432. header_in_data =
  433. ipath_layer_get_rcvhdrentsize(dev->dd) == 16;
  434. if (header_in_data) {
  435. qkey = be32_to_cpu(((__be32 *) data)[1]);
  436. src_qp = be32_to_cpu(((__be32 *) data)[2]);
  437. data += 12;
  438. } else {
  439. qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
  440. src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
  441. }
  442. }
  443. src_qp &= IPS_QPN_MASK;
  444. /*
  445. * Check that the permissive LID is only used on QP0
  446. * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
  447. */
  448. if (qp->ibqp.qp_num) {
  449. if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
  450. hdr->lrh[3] == IB_LID_PERMISSIVE)) {
  451. dev->n_pkt_drops++;
  452. goto bail;
  453. }
  454. if (unlikely(qkey != qp->qkey)) {
  455. /* XXX OK to lose a count once in a while. */
  456. dev->qkey_violations++;
  457. dev->n_pkt_drops++;
  458. goto bail;
  459. }
  460. } else if (hdr->lrh[1] == IB_LID_PERMISSIVE ||
  461. hdr->lrh[3] == IB_LID_PERMISSIVE) {
  462. struct ib_smp *smp = (struct ib_smp *) data;
  463. if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  464. dev->n_pkt_drops++;
  465. goto bail;
  466. }
  467. }
  468. /* Get the number of bytes the message was padded by. */
  469. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  470. if (unlikely(tlen < (hdrsize + pad + 4))) {
  471. /* Drop incomplete packets. */
  472. dev->n_pkt_drops++;
  473. goto bail;
  474. }
  475. tlen -= hdrsize + pad + 4;
  476. /* Drop invalid MAD packets (see 13.5.3.1). */
  477. if (unlikely((qp->ibqp.qp_num == 0 &&
  478. (tlen != 256 ||
  479. (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)) ||
  480. (qp->ibqp.qp_num == 1 &&
  481. (tlen != 256 ||
  482. (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))) {
  483. dev->n_pkt_drops++;
  484. goto bail;
  485. }
  486. /*
  487. * A GRH is expected to preceed the data even if not
  488. * present on the wire.
  489. */
  490. wc.byte_len = tlen + sizeof(struct ib_grh);
  491. /*
  492. * The opcode is in the low byte when its in network order
  493. * (top byte when in host order).
  494. */
  495. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  496. if (qp->ibqp.qp_num > 1 &&
  497. opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
  498. if (header_in_data) {
  499. wc.imm_data = *(__be32 *) data;
  500. data += sizeof(__be32);
  501. } else
  502. wc.imm_data = ohdr->u.ud.imm_data;
  503. wc.wc_flags = IB_WC_WITH_IMM;
  504. hdrsize += sizeof(u32);
  505. } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
  506. wc.imm_data = 0;
  507. wc.wc_flags = 0;
  508. } else {
  509. dev->n_pkt_drops++;
  510. goto bail;
  511. }
  512. /*
  513. * Get the next work request entry to find where to put the data.
  514. * Note that it is safe to drop the lock after changing rq->tail
  515. * since ipath_post_receive() won't fill the empty slot.
  516. */
  517. if (qp->ibqp.srq) {
  518. srq = to_isrq(qp->ibqp.srq);
  519. rq = &srq->rq;
  520. } else {
  521. srq = NULL;
  522. rq = &qp->r_rq;
  523. }
  524. spin_lock_irqsave(&rq->lock, flags);
  525. if (rq->tail == rq->head) {
  526. spin_unlock_irqrestore(&rq->lock, flags);
  527. dev->n_pkt_drops++;
  528. goto bail;
  529. }
  530. /* Silently drop packets which are too big. */
  531. wqe = get_rwqe_ptr(rq, rq->tail);
  532. if (wc.byte_len > wqe->length) {
  533. spin_unlock_irqrestore(&rq->lock, flags);
  534. dev->n_pkt_drops++;
  535. goto bail;
  536. }
  537. wc.wr_id = wqe->wr_id;
  538. qp->r_sge.sge = wqe->sg_list[0];
  539. qp->r_sge.sg_list = wqe->sg_list + 1;
  540. qp->r_sge.num_sge = wqe->num_sge;
  541. if (++rq->tail >= rq->size)
  542. rq->tail = 0;
  543. if (srq && srq->ibsrq.event_handler) {
  544. u32 n;
  545. if (rq->head < rq->tail)
  546. n = rq->size + rq->head - rq->tail;
  547. else
  548. n = rq->head - rq->tail;
  549. if (n < srq->limit) {
  550. struct ib_event ev;
  551. srq->limit = 0;
  552. spin_unlock_irqrestore(&rq->lock, flags);
  553. ev.device = qp->ibqp.device;
  554. ev.element.srq = qp->ibqp.srq;
  555. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  556. srq->ibsrq.event_handler(&ev,
  557. srq->ibsrq.srq_context);
  558. } else
  559. spin_unlock_irqrestore(&rq->lock, flags);
  560. } else
  561. spin_unlock_irqrestore(&rq->lock, flags);
  562. if (has_grh) {
  563. ipath_copy_sge(&qp->r_sge, &hdr->u.l.grh,
  564. sizeof(struct ib_grh));
  565. wc.wc_flags |= IB_WC_GRH;
  566. } else
  567. ipath_skip_sge(&qp->r_sge, sizeof(struct ib_grh));
  568. ipath_copy_sge(&qp->r_sge, data,
  569. wc.byte_len - sizeof(struct ib_grh));
  570. wc.status = IB_WC_SUCCESS;
  571. wc.opcode = IB_WC_RECV;
  572. wc.vendor_err = 0;
  573. wc.qp_num = qp->ibqp.qp_num;
  574. wc.src_qp = src_qp;
  575. /* XXX do we know which pkey matched? Only needed for GSI. */
  576. wc.pkey_index = 0;
  577. wc.slid = be16_to_cpu(hdr->lrh[3]);
  578. wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
  579. dlid = be16_to_cpu(hdr->lrh[1]);
  580. /*
  581. * Save the LMC lower bits if the destination LID is a unicast LID.
  582. */
  583. wc.dlid_path_bits = dlid >= IPS_MULTICAST_LID_BASE ? 0 :
  584. dlid & ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  585. /* Signal completion event if the solicited bit is set. */
  586. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  587. (ohdr->bth[0] &
  588. __constant_cpu_to_be32(1 << 23)) != 0);
  589. bail:;
  590. }