ipath_ud.c 18 KB

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