ipath_ud.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 = &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. 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 = &qp->ibqp;
  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. if (wr->wr.ud.ah->pd != qp->ibqp.pd) {
  291. ret = -EPERM;
  292. goto bail;
  293. }
  294. /* IB spec says that num_sge == 0 is OK. */
  295. if (wr->num_sge > qp->s_max_sge) {
  296. ret = -EINVAL;
  297. goto bail;
  298. }
  299. if (wr->num_sge > 1) {
  300. sg_list = kmalloc((qp->s_max_sge - 1) * sizeof(*sg_list),
  301. GFP_ATOMIC);
  302. if (!sg_list) {
  303. ret = -ENOMEM;
  304. goto bail;
  305. }
  306. } else
  307. sg_list = NULL;
  308. /* Check the buffer to send. */
  309. ss.sg_list = sg_list;
  310. ss.sge.mr = NULL;
  311. ss.sge.vaddr = NULL;
  312. ss.sge.length = 0;
  313. ss.sge.sge_length = 0;
  314. ss.num_sge = 0;
  315. len = 0;
  316. for (i = 0; i < wr->num_sge; i++) {
  317. /* Check LKEY */
  318. if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
  319. ret = -EINVAL;
  320. goto bail;
  321. }
  322. if (wr->sg_list[i].length == 0)
  323. continue;
  324. if (!ipath_lkey_ok(qp, ss.num_sge ?
  325. sg_list + ss.num_sge - 1 : &ss.sge,
  326. &wr->sg_list[i], 0)) {
  327. ret = -EINVAL;
  328. goto bail;
  329. }
  330. len += wr->sg_list[i].length;
  331. ss.num_sge++;
  332. }
  333. /* Check for invalid packet size. */
  334. if (len > dev->dd->ipath_ibmtu) {
  335. ret = -EINVAL;
  336. goto bail;
  337. }
  338. extra_bytes = (4 - len) & 3;
  339. nwords = (len + extra_bytes) >> 2;
  340. /* Construct the header. */
  341. ah_attr = &to_iah(wr->wr.ud.ah)->attr;
  342. if (ah_attr->dlid == 0) {
  343. ret = -EINVAL;
  344. goto bail;
  345. }
  346. if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE) {
  347. if (ah_attr->dlid != IPATH_PERMISSIVE_LID)
  348. dev->n_multicast_xmit++;
  349. else
  350. dev->n_unicast_xmit++;
  351. } else {
  352. dev->n_unicast_xmit++;
  353. lid = ah_attr->dlid &
  354. ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  355. if (unlikely(lid == dev->dd->ipath_lid)) {
  356. /*
  357. * Pass in an uninitialized ib_wc to save stack
  358. * space.
  359. */
  360. ipath_ud_loopback(qp, &ss, len, wr, &wc);
  361. goto done;
  362. }
  363. }
  364. if (ah_attr->ah_flags & IB_AH_GRH) {
  365. /* Header size in 32-bit words. */
  366. hwords = 17;
  367. lrh0 = IPATH_LRH_GRH;
  368. ohdr = &qp->s_hdr.u.l.oth;
  369. qp->s_hdr.u.l.grh.version_tclass_flow =
  370. cpu_to_be32((6 << 28) |
  371. (ah_attr->grh.traffic_class << 20) |
  372. ah_attr->grh.flow_label);
  373. qp->s_hdr.u.l.grh.paylen =
  374. cpu_to_be16(((wr->opcode ==
  375. IB_WR_SEND_WITH_IMM ? 6 : 5) +
  376. nwords + SIZE_OF_CRC) << 2);
  377. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  378. qp->s_hdr.u.l.grh.next_hdr = 0x1B;
  379. qp->s_hdr.u.l.grh.hop_limit = ah_attr->grh.hop_limit;
  380. /* The SGID is 32-bit aligned. */
  381. qp->s_hdr.u.l.grh.sgid.global.subnet_prefix =
  382. dev->gid_prefix;
  383. qp->s_hdr.u.l.grh.sgid.global.interface_id =
  384. dev->dd->ipath_guid;
  385. qp->s_hdr.u.l.grh.dgid = ah_attr->grh.dgid;
  386. /*
  387. * Don't worry about sending to locally attached multicast
  388. * QPs. It is unspecified by the spec. what happens.
  389. */
  390. } else {
  391. /* Header size in 32-bit words. */
  392. hwords = 7;
  393. lrh0 = IPATH_LRH_BTH;
  394. ohdr = &qp->s_hdr.u.oth;
  395. }
  396. if (wr->opcode == IB_WR_SEND_WITH_IMM) {
  397. ohdr->u.ud.imm_data = wr->imm_data;
  398. wc.imm_data = wr->imm_data;
  399. hwords += 1;
  400. bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
  401. } else if (wr->opcode == IB_WR_SEND) {
  402. wc.imm_data = 0;
  403. bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
  404. } else {
  405. ret = -EINVAL;
  406. goto bail;
  407. }
  408. lrh0 |= ah_attr->sl << 4;
  409. if (qp->ibqp.qp_type == IB_QPT_SMI)
  410. lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
  411. qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
  412. qp->s_hdr.lrh[1] = cpu_to_be16(ah_attr->dlid); /* DEST LID */
  413. qp->s_hdr.lrh[2] = cpu_to_be16(hwords + nwords + SIZE_OF_CRC);
  414. lid = dev->dd->ipath_lid;
  415. if (lid) {
  416. lid |= ah_attr->src_path_bits &
  417. ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  418. qp->s_hdr.lrh[3] = cpu_to_be16(lid);
  419. } else
  420. qp->s_hdr.lrh[3] = IB_LID_PERMISSIVE;
  421. if (wr->send_flags & IB_SEND_SOLICITED)
  422. bth0 |= 1 << 23;
  423. bth0 |= extra_bytes << 20;
  424. bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? IPATH_DEFAULT_P_KEY :
  425. ipath_get_pkey(dev->dd, qp->s_pkey_index);
  426. ohdr->bth[0] = cpu_to_be32(bth0);
  427. /*
  428. * Use the multicast QP if the destination LID is a multicast LID.
  429. */
  430. ohdr->bth[1] = ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
  431. ah_attr->dlid != IPATH_PERMISSIVE_LID ?
  432. __constant_cpu_to_be32(IPATH_MULTICAST_QPN) :
  433. cpu_to_be32(wr->wr.ud.remote_qpn);
  434. /* XXX Could lose a PSN count but not worth locking */
  435. ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & IPATH_PSN_MASK);
  436. /*
  437. * Qkeys with the high order bit set mean use the
  438. * qkey from the QP context instead of the WR (see 10.2.5).
  439. */
  440. ohdr->u.ud.deth[0] = cpu_to_be32((int)wr->wr.ud.remote_qkey < 0 ?
  441. qp->qkey : wr->wr.ud.remote_qkey);
  442. ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
  443. if (ipath_verbs_send(dev->dd, hwords, (u32 *) &qp->s_hdr,
  444. len, &ss))
  445. dev->n_no_piobuf++;
  446. done:
  447. /* Queue the completion status entry. */
  448. if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
  449. (wr->send_flags & IB_SEND_SIGNALED)) {
  450. wc.wr_id = wr->wr_id;
  451. wc.status = IB_WC_SUCCESS;
  452. wc.vendor_err = 0;
  453. wc.opcode = IB_WC_SEND;
  454. wc.byte_len = len;
  455. wc.qp = &qp->ibqp;
  456. wc.src_qp = 0;
  457. wc.wc_flags = 0;
  458. /* XXX initialize other fields? */
  459. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
  460. }
  461. kfree(sg_list);
  462. ret = 0;
  463. bail:
  464. return ret;
  465. }
  466. /**
  467. * ipath_ud_rcv - receive an incoming UD packet
  468. * @dev: the device the packet came in on
  469. * @hdr: the packet header
  470. * @has_grh: true if the packet has a GRH
  471. * @data: the packet data
  472. * @tlen: the packet length
  473. * @qp: the QP the packet came on
  474. *
  475. * This is called from ipath_qp_rcv() to process an incoming UD packet
  476. * for the given QP.
  477. * Called at interrupt level.
  478. */
  479. void ipath_ud_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
  480. int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
  481. {
  482. struct ipath_other_headers *ohdr;
  483. int opcode;
  484. u32 hdrsize;
  485. u32 pad;
  486. struct ib_wc wc;
  487. u32 qkey;
  488. u32 src_qp;
  489. u16 dlid;
  490. int header_in_data;
  491. /* Check for GRH */
  492. if (!has_grh) {
  493. ohdr = &hdr->u.oth;
  494. hdrsize = 8 + 12 + 8; /* LRH + BTH + DETH */
  495. qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
  496. src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
  497. header_in_data = 0;
  498. } else {
  499. ohdr = &hdr->u.l.oth;
  500. hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
  501. /*
  502. * The header with GRH is 68 bytes and the core driver sets
  503. * the eager header buffer size to 56 bytes so the last 12
  504. * bytes of the IB header is in the data buffer.
  505. */
  506. header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
  507. if (header_in_data) {
  508. qkey = be32_to_cpu(((__be32 *) data)[1]);
  509. src_qp = be32_to_cpu(((__be32 *) data)[2]);
  510. data += 12;
  511. } else {
  512. qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
  513. src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
  514. }
  515. }
  516. src_qp &= IPATH_QPN_MASK;
  517. /*
  518. * Check that the permissive LID is only used on QP0
  519. * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
  520. */
  521. if (qp->ibqp.qp_num) {
  522. if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
  523. hdr->lrh[3] == IB_LID_PERMISSIVE)) {
  524. dev->n_pkt_drops++;
  525. goto bail;
  526. }
  527. if (unlikely(qkey != qp->qkey)) {
  528. /* XXX OK to lose a count once in a while. */
  529. dev->qkey_violations++;
  530. dev->n_pkt_drops++;
  531. goto bail;
  532. }
  533. } else if (hdr->lrh[1] == IB_LID_PERMISSIVE ||
  534. hdr->lrh[3] == IB_LID_PERMISSIVE) {
  535. struct ib_smp *smp = (struct ib_smp *) data;
  536. if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  537. dev->n_pkt_drops++;
  538. goto bail;
  539. }
  540. }
  541. /* Get the number of bytes the message was padded by. */
  542. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  543. if (unlikely(tlen < (hdrsize + pad + 4))) {
  544. /* Drop incomplete packets. */
  545. dev->n_pkt_drops++;
  546. goto bail;
  547. }
  548. tlen -= hdrsize + pad + 4;
  549. /* Drop invalid MAD packets (see 13.5.3.1). */
  550. if (unlikely((qp->ibqp.qp_num == 0 &&
  551. (tlen != 256 ||
  552. (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)) ||
  553. (qp->ibqp.qp_num == 1 &&
  554. (tlen != 256 ||
  555. (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))) {
  556. dev->n_pkt_drops++;
  557. goto bail;
  558. }
  559. /*
  560. * A GRH is expected to preceed the data even if not
  561. * present on the wire.
  562. */
  563. wc.byte_len = tlen + sizeof(struct ib_grh);
  564. /*
  565. * The opcode is in the low byte when its in network order
  566. * (top byte when in host order).
  567. */
  568. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  569. if (qp->ibqp.qp_num > 1 &&
  570. opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
  571. if (header_in_data) {
  572. wc.imm_data = *(__be32 *) data;
  573. data += sizeof(__be32);
  574. } else
  575. wc.imm_data = ohdr->u.ud.imm_data;
  576. wc.wc_flags = IB_WC_WITH_IMM;
  577. hdrsize += sizeof(u32);
  578. } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
  579. wc.imm_data = 0;
  580. wc.wc_flags = 0;
  581. } else {
  582. dev->n_pkt_drops++;
  583. goto bail;
  584. }
  585. /*
  586. * Get the next work request entry to find where to put the data.
  587. */
  588. if (qp->r_reuse_sge)
  589. qp->r_reuse_sge = 0;
  590. else if (!ipath_get_rwqe(qp, 0)) {
  591. /*
  592. * Count VL15 packets dropped due to no receive buffer.
  593. * Otherwise, count them as buffer overruns since usually,
  594. * the HW will be able to receive packets even if there are
  595. * no QPs with posted receive buffers.
  596. */
  597. if (qp->ibqp.qp_num == 0)
  598. dev->n_vl15_dropped++;
  599. else
  600. dev->rcv_errors++;
  601. goto bail;
  602. }
  603. /* Silently drop packets which are too big. */
  604. if (wc.byte_len > qp->r_len) {
  605. qp->r_reuse_sge = 1;
  606. dev->n_pkt_drops++;
  607. goto bail;
  608. }
  609. if (has_grh) {
  610. ipath_copy_sge(&qp->r_sge, &hdr->u.l.grh,
  611. sizeof(struct ib_grh));
  612. wc.wc_flags |= IB_WC_GRH;
  613. } else
  614. ipath_skip_sge(&qp->r_sge, sizeof(struct ib_grh));
  615. ipath_copy_sge(&qp->r_sge, data,
  616. wc.byte_len - sizeof(struct ib_grh));
  617. qp->r_wrid_valid = 0;
  618. wc.wr_id = qp->r_wr_id;
  619. wc.status = IB_WC_SUCCESS;
  620. wc.opcode = IB_WC_RECV;
  621. wc.vendor_err = 0;
  622. wc.qp = &qp->ibqp;
  623. wc.src_qp = src_qp;
  624. /* XXX do we know which pkey matched? Only needed for GSI. */
  625. wc.pkey_index = 0;
  626. wc.slid = be16_to_cpu(hdr->lrh[3]);
  627. wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
  628. dlid = be16_to_cpu(hdr->lrh[1]);
  629. /*
  630. * Save the LMC lower bits if the destination LID is a unicast LID.
  631. */
  632. wc.dlid_path_bits = dlid >= IPATH_MULTICAST_LID_BASE ? 0 :
  633. dlid & ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
  634. /* Signal completion event if the solicited bit is set. */
  635. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  636. (ohdr->bth[0] &
  637. __constant_cpu_to_be32(1 << 23)) != 0);
  638. bail:;
  639. }