ipath_ud.c 18 KB

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