ipath_ud.c 18 KB

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