rxe_req.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, 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 <linux/skbuff.h>
  34. #include "rxe.h"
  35. #include "rxe_loc.h"
  36. #include "rxe_queue.h"
  37. static int next_opcode(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
  38. u32 opcode);
  39. static inline void retry_first_write_send(struct rxe_qp *qp,
  40. struct rxe_send_wqe *wqe,
  41. unsigned mask, int npsn)
  42. {
  43. int i;
  44. for (i = 0; i < npsn; i++) {
  45. int to_send = (wqe->dma.resid > qp->mtu) ?
  46. qp->mtu : wqe->dma.resid;
  47. qp->req.opcode = next_opcode(qp, wqe,
  48. wqe->wr.opcode);
  49. if (wqe->wr.send_flags & IB_SEND_INLINE) {
  50. wqe->dma.resid -= to_send;
  51. wqe->dma.sge_offset += to_send;
  52. } else {
  53. advance_dma_data(&wqe->dma, to_send);
  54. }
  55. if (mask & WR_WRITE_MASK)
  56. wqe->iova += qp->mtu;
  57. }
  58. }
  59. static void req_retry(struct rxe_qp *qp)
  60. {
  61. struct rxe_send_wqe *wqe;
  62. unsigned int wqe_index;
  63. unsigned int mask;
  64. int npsn;
  65. int first = 1;
  66. wqe = queue_head(qp->sq.queue);
  67. npsn = (qp->comp.psn - wqe->first_psn) & BTH_PSN_MASK;
  68. qp->req.wqe_index = consumer_index(qp->sq.queue);
  69. qp->req.psn = qp->comp.psn;
  70. qp->req.opcode = -1;
  71. for (wqe_index = consumer_index(qp->sq.queue);
  72. wqe_index != producer_index(qp->sq.queue);
  73. wqe_index = next_index(qp->sq.queue, wqe_index)) {
  74. wqe = addr_from_index(qp->sq.queue, wqe_index);
  75. mask = wr_opcode_mask(wqe->wr.opcode, qp);
  76. if (wqe->state == wqe_state_posted)
  77. break;
  78. if (wqe->state == wqe_state_done)
  79. continue;
  80. wqe->iova = (mask & WR_ATOMIC_MASK) ?
  81. wqe->wr.wr.atomic.remote_addr :
  82. (mask & WR_READ_OR_WRITE_MASK) ?
  83. wqe->wr.wr.rdma.remote_addr :
  84. 0;
  85. if (!first || (mask & WR_READ_MASK) == 0) {
  86. wqe->dma.resid = wqe->dma.length;
  87. wqe->dma.cur_sge = 0;
  88. wqe->dma.sge_offset = 0;
  89. }
  90. if (first) {
  91. first = 0;
  92. if (mask & WR_WRITE_OR_SEND_MASK)
  93. retry_first_write_send(qp, wqe, mask, npsn);
  94. if (mask & WR_READ_MASK)
  95. wqe->iova += npsn * qp->mtu;
  96. }
  97. wqe->state = wqe_state_posted;
  98. }
  99. }
  100. void rnr_nak_timer(unsigned long data)
  101. {
  102. struct rxe_qp *qp = (struct rxe_qp *)data;
  103. pr_debug("qp#%d rnr nak timer fired\n", qp_num(qp));
  104. rxe_run_task(&qp->req.task, 1);
  105. }
  106. static struct rxe_send_wqe *req_next_wqe(struct rxe_qp *qp)
  107. {
  108. struct rxe_send_wqe *wqe = queue_head(qp->sq.queue);
  109. unsigned long flags;
  110. if (unlikely(qp->req.state == QP_STATE_DRAIN)) {
  111. /* check to see if we are drained;
  112. * state_lock used by requester and completer
  113. */
  114. spin_lock_irqsave(&qp->state_lock, flags);
  115. do {
  116. if (qp->req.state != QP_STATE_DRAIN) {
  117. /* comp just finished */
  118. spin_unlock_irqrestore(&qp->state_lock,
  119. flags);
  120. break;
  121. }
  122. if (wqe && ((qp->req.wqe_index !=
  123. consumer_index(qp->sq.queue)) ||
  124. (wqe->state != wqe_state_posted))) {
  125. /* comp not done yet */
  126. spin_unlock_irqrestore(&qp->state_lock,
  127. flags);
  128. break;
  129. }
  130. qp->req.state = QP_STATE_DRAINED;
  131. spin_unlock_irqrestore(&qp->state_lock, flags);
  132. if (qp->ibqp.event_handler) {
  133. struct ib_event ev;
  134. ev.device = qp->ibqp.device;
  135. ev.element.qp = &qp->ibqp;
  136. ev.event = IB_EVENT_SQ_DRAINED;
  137. qp->ibqp.event_handler(&ev,
  138. qp->ibqp.qp_context);
  139. }
  140. } while (0);
  141. }
  142. if (qp->req.wqe_index == producer_index(qp->sq.queue))
  143. return NULL;
  144. wqe = addr_from_index(qp->sq.queue, qp->req.wqe_index);
  145. if (unlikely((qp->req.state == QP_STATE_DRAIN ||
  146. qp->req.state == QP_STATE_DRAINED) &&
  147. (wqe->state != wqe_state_processing)))
  148. return NULL;
  149. if (unlikely((wqe->wr.send_flags & IB_SEND_FENCE) &&
  150. (qp->req.wqe_index != consumer_index(qp->sq.queue)))) {
  151. qp->req.wait_fence = 1;
  152. return NULL;
  153. }
  154. wqe->mask = wr_opcode_mask(wqe->wr.opcode, qp);
  155. return wqe;
  156. }
  157. static int next_opcode_rc(struct rxe_qp *qp, u32 opcode, int fits)
  158. {
  159. switch (opcode) {
  160. case IB_WR_RDMA_WRITE:
  161. if (qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_FIRST ||
  162. qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_MIDDLE)
  163. return fits ?
  164. IB_OPCODE_RC_RDMA_WRITE_LAST :
  165. IB_OPCODE_RC_RDMA_WRITE_MIDDLE;
  166. else
  167. return fits ?
  168. IB_OPCODE_RC_RDMA_WRITE_ONLY :
  169. IB_OPCODE_RC_RDMA_WRITE_FIRST;
  170. case IB_WR_RDMA_WRITE_WITH_IMM:
  171. if (qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_FIRST ||
  172. qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_MIDDLE)
  173. return fits ?
  174. IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE :
  175. IB_OPCODE_RC_RDMA_WRITE_MIDDLE;
  176. else
  177. return fits ?
  178. IB_OPCODE_RC_RDMA_WRITE_ONLY_WITH_IMMEDIATE :
  179. IB_OPCODE_RC_RDMA_WRITE_FIRST;
  180. case IB_WR_SEND:
  181. if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
  182. qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
  183. return fits ?
  184. IB_OPCODE_RC_SEND_LAST :
  185. IB_OPCODE_RC_SEND_MIDDLE;
  186. else
  187. return fits ?
  188. IB_OPCODE_RC_SEND_ONLY :
  189. IB_OPCODE_RC_SEND_FIRST;
  190. case IB_WR_SEND_WITH_IMM:
  191. if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
  192. qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
  193. return fits ?
  194. IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE :
  195. IB_OPCODE_RC_SEND_MIDDLE;
  196. else
  197. return fits ?
  198. IB_OPCODE_RC_SEND_ONLY_WITH_IMMEDIATE :
  199. IB_OPCODE_RC_SEND_FIRST;
  200. case IB_WR_RDMA_READ:
  201. return IB_OPCODE_RC_RDMA_READ_REQUEST;
  202. case IB_WR_ATOMIC_CMP_AND_SWP:
  203. return IB_OPCODE_RC_COMPARE_SWAP;
  204. case IB_WR_ATOMIC_FETCH_AND_ADD:
  205. return IB_OPCODE_RC_FETCH_ADD;
  206. case IB_WR_SEND_WITH_INV:
  207. if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
  208. qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
  209. return fits ? IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE :
  210. IB_OPCODE_RC_SEND_MIDDLE;
  211. else
  212. return fits ? IB_OPCODE_RC_SEND_ONLY_WITH_INVALIDATE :
  213. IB_OPCODE_RC_SEND_FIRST;
  214. case IB_WR_REG_MR:
  215. case IB_WR_LOCAL_INV:
  216. return opcode;
  217. }
  218. return -EINVAL;
  219. }
  220. static int next_opcode_uc(struct rxe_qp *qp, u32 opcode, int fits)
  221. {
  222. switch (opcode) {
  223. case IB_WR_RDMA_WRITE:
  224. if (qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_FIRST ||
  225. qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_MIDDLE)
  226. return fits ?
  227. IB_OPCODE_UC_RDMA_WRITE_LAST :
  228. IB_OPCODE_UC_RDMA_WRITE_MIDDLE;
  229. else
  230. return fits ?
  231. IB_OPCODE_UC_RDMA_WRITE_ONLY :
  232. IB_OPCODE_UC_RDMA_WRITE_FIRST;
  233. case IB_WR_RDMA_WRITE_WITH_IMM:
  234. if (qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_FIRST ||
  235. qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_MIDDLE)
  236. return fits ?
  237. IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE :
  238. IB_OPCODE_UC_RDMA_WRITE_MIDDLE;
  239. else
  240. return fits ?
  241. IB_OPCODE_UC_RDMA_WRITE_ONLY_WITH_IMMEDIATE :
  242. IB_OPCODE_UC_RDMA_WRITE_FIRST;
  243. case IB_WR_SEND:
  244. if (qp->req.opcode == IB_OPCODE_UC_SEND_FIRST ||
  245. qp->req.opcode == IB_OPCODE_UC_SEND_MIDDLE)
  246. return fits ?
  247. IB_OPCODE_UC_SEND_LAST :
  248. IB_OPCODE_UC_SEND_MIDDLE;
  249. else
  250. return fits ?
  251. IB_OPCODE_UC_SEND_ONLY :
  252. IB_OPCODE_UC_SEND_FIRST;
  253. case IB_WR_SEND_WITH_IMM:
  254. if (qp->req.opcode == IB_OPCODE_UC_SEND_FIRST ||
  255. qp->req.opcode == IB_OPCODE_UC_SEND_MIDDLE)
  256. return fits ?
  257. IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE :
  258. IB_OPCODE_UC_SEND_MIDDLE;
  259. else
  260. return fits ?
  261. IB_OPCODE_UC_SEND_ONLY_WITH_IMMEDIATE :
  262. IB_OPCODE_UC_SEND_FIRST;
  263. }
  264. return -EINVAL;
  265. }
  266. static int next_opcode(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
  267. u32 opcode)
  268. {
  269. int fits = (wqe->dma.resid <= qp->mtu);
  270. switch (qp_type(qp)) {
  271. case IB_QPT_RC:
  272. return next_opcode_rc(qp, opcode, fits);
  273. case IB_QPT_UC:
  274. return next_opcode_uc(qp, opcode, fits);
  275. case IB_QPT_SMI:
  276. case IB_QPT_UD:
  277. case IB_QPT_GSI:
  278. switch (opcode) {
  279. case IB_WR_SEND:
  280. return IB_OPCODE_UD_SEND_ONLY;
  281. case IB_WR_SEND_WITH_IMM:
  282. return IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  283. }
  284. break;
  285. default:
  286. break;
  287. }
  288. return -EINVAL;
  289. }
  290. static inline int check_init_depth(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
  291. {
  292. int depth;
  293. if (wqe->has_rd_atomic)
  294. return 0;
  295. qp->req.need_rd_atomic = 1;
  296. depth = atomic_dec_return(&qp->req.rd_atomic);
  297. if (depth >= 0) {
  298. qp->req.need_rd_atomic = 0;
  299. wqe->has_rd_atomic = 1;
  300. return 0;
  301. }
  302. atomic_inc(&qp->req.rd_atomic);
  303. return -EAGAIN;
  304. }
  305. static inline int get_mtu(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
  306. {
  307. struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
  308. struct rxe_port *port;
  309. struct rxe_av *av;
  310. if ((qp_type(qp) == IB_QPT_RC) || (qp_type(qp) == IB_QPT_UC))
  311. return qp->mtu;
  312. av = &wqe->av;
  313. port = &rxe->port;
  314. return port->mtu_cap;
  315. }
  316. static struct sk_buff *init_req_packet(struct rxe_qp *qp,
  317. struct rxe_send_wqe *wqe,
  318. int opcode, int payload,
  319. struct rxe_pkt_info *pkt)
  320. {
  321. struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
  322. struct rxe_port *port = &rxe->port;
  323. struct sk_buff *skb;
  324. struct rxe_send_wr *ibwr = &wqe->wr;
  325. struct rxe_av *av;
  326. int pad = (-payload) & 0x3;
  327. int paylen;
  328. int solicited;
  329. u16 pkey;
  330. u32 qp_num;
  331. int ack_req;
  332. /* length from start of bth to end of icrc */
  333. paylen = rxe_opcode[opcode].length + payload + pad + RXE_ICRC_SIZE;
  334. /* pkt->hdr, rxe, port_num and mask are initialized in ifc
  335. * layer
  336. */
  337. pkt->opcode = opcode;
  338. pkt->qp = qp;
  339. pkt->psn = qp->req.psn;
  340. pkt->mask = rxe_opcode[opcode].mask;
  341. pkt->paylen = paylen;
  342. pkt->offset = 0;
  343. pkt->wqe = wqe;
  344. /* init skb */
  345. av = rxe_get_av(pkt);
  346. skb = rxe->ifc_ops->init_packet(rxe, av, paylen, pkt);
  347. if (unlikely(!skb))
  348. return NULL;
  349. /* init bth */
  350. solicited = (ibwr->send_flags & IB_SEND_SOLICITED) &&
  351. (pkt->mask & RXE_END_MASK) &&
  352. ((pkt->mask & (RXE_SEND_MASK)) ||
  353. (pkt->mask & (RXE_WRITE_MASK | RXE_IMMDT_MASK)) ==
  354. (RXE_WRITE_MASK | RXE_IMMDT_MASK));
  355. pkey = (qp_type(qp) == IB_QPT_GSI) ?
  356. port->pkey_tbl[ibwr->wr.ud.pkey_index] :
  357. port->pkey_tbl[qp->attr.pkey_index];
  358. qp_num = (pkt->mask & RXE_DETH_MASK) ? ibwr->wr.ud.remote_qpn :
  359. qp->attr.dest_qp_num;
  360. ack_req = ((pkt->mask & RXE_END_MASK) ||
  361. (qp->req.noack_pkts++ > RXE_MAX_PKT_PER_ACK));
  362. if (ack_req)
  363. qp->req.noack_pkts = 0;
  364. bth_init(pkt, pkt->opcode, solicited, 0, pad, pkey, qp_num,
  365. ack_req, pkt->psn);
  366. /* init optional headers */
  367. if (pkt->mask & RXE_RETH_MASK) {
  368. reth_set_rkey(pkt, ibwr->wr.rdma.rkey);
  369. reth_set_va(pkt, wqe->iova);
  370. reth_set_len(pkt, wqe->dma.length);
  371. }
  372. if (pkt->mask & RXE_IMMDT_MASK)
  373. immdt_set_imm(pkt, ibwr->ex.imm_data);
  374. if (pkt->mask & RXE_IETH_MASK)
  375. ieth_set_rkey(pkt, ibwr->ex.invalidate_rkey);
  376. if (pkt->mask & RXE_ATMETH_MASK) {
  377. atmeth_set_va(pkt, wqe->iova);
  378. if (opcode == IB_OPCODE_RC_COMPARE_SWAP ||
  379. opcode == IB_OPCODE_RD_COMPARE_SWAP) {
  380. atmeth_set_swap_add(pkt, ibwr->wr.atomic.swap);
  381. atmeth_set_comp(pkt, ibwr->wr.atomic.compare_add);
  382. } else {
  383. atmeth_set_swap_add(pkt, ibwr->wr.atomic.compare_add);
  384. }
  385. atmeth_set_rkey(pkt, ibwr->wr.atomic.rkey);
  386. }
  387. if (pkt->mask & RXE_DETH_MASK) {
  388. if (qp->ibqp.qp_num == 1)
  389. deth_set_qkey(pkt, GSI_QKEY);
  390. else
  391. deth_set_qkey(pkt, ibwr->wr.ud.remote_qkey);
  392. deth_set_sqp(pkt, qp->ibqp.qp_num);
  393. }
  394. return skb;
  395. }
  396. static int fill_packet(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
  397. struct rxe_pkt_info *pkt, struct sk_buff *skb,
  398. int paylen)
  399. {
  400. struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
  401. u32 crc = 0;
  402. u32 *p;
  403. int err;
  404. err = rxe->ifc_ops->prepare(rxe, pkt, skb, &crc);
  405. if (err)
  406. return err;
  407. if (pkt->mask & RXE_WRITE_OR_SEND) {
  408. if (wqe->wr.send_flags & IB_SEND_INLINE) {
  409. u8 *tmp = &wqe->dma.inline_data[wqe->dma.sge_offset];
  410. crc = crc32_le(crc, tmp, paylen);
  411. memcpy(payload_addr(pkt), tmp, paylen);
  412. wqe->dma.resid -= paylen;
  413. wqe->dma.sge_offset += paylen;
  414. } else {
  415. err = copy_data(rxe, qp->pd, 0, &wqe->dma,
  416. payload_addr(pkt), paylen,
  417. from_mem_obj,
  418. &crc);
  419. if (err)
  420. return err;
  421. }
  422. }
  423. p = payload_addr(pkt) + paylen + bth_pad(pkt);
  424. *p = ~crc;
  425. return 0;
  426. }
  427. static void update_wqe_state(struct rxe_qp *qp,
  428. struct rxe_send_wqe *wqe,
  429. struct rxe_pkt_info *pkt)
  430. {
  431. if (pkt->mask & RXE_END_MASK) {
  432. if (qp_type(qp) == IB_QPT_RC)
  433. wqe->state = wqe_state_pending;
  434. } else {
  435. wqe->state = wqe_state_processing;
  436. }
  437. }
  438. static void update_wqe_psn(struct rxe_qp *qp,
  439. struct rxe_send_wqe *wqe,
  440. struct rxe_pkt_info *pkt,
  441. int payload)
  442. {
  443. /* number of packets left to send including current one */
  444. int num_pkt = (wqe->dma.resid + payload + qp->mtu - 1) / qp->mtu;
  445. /* handle zero length packet case */
  446. if (num_pkt == 0)
  447. num_pkt = 1;
  448. if (pkt->mask & RXE_START_MASK) {
  449. wqe->first_psn = qp->req.psn;
  450. wqe->last_psn = (qp->req.psn + num_pkt - 1) & BTH_PSN_MASK;
  451. }
  452. if (pkt->mask & RXE_READ_MASK)
  453. qp->req.psn = (wqe->first_psn + num_pkt) & BTH_PSN_MASK;
  454. else
  455. qp->req.psn = (qp->req.psn + 1) & BTH_PSN_MASK;
  456. }
  457. static void save_state(struct rxe_send_wqe *wqe,
  458. struct rxe_qp *qp,
  459. struct rxe_send_wqe *rollback_wqe,
  460. u32 *rollback_psn)
  461. {
  462. rollback_wqe->state = wqe->state;
  463. rollback_wqe->first_psn = wqe->first_psn;
  464. rollback_wqe->last_psn = wqe->last_psn;
  465. *rollback_psn = qp->req.psn;
  466. }
  467. static void rollback_state(struct rxe_send_wqe *wqe,
  468. struct rxe_qp *qp,
  469. struct rxe_send_wqe *rollback_wqe,
  470. u32 rollback_psn)
  471. {
  472. wqe->state = rollback_wqe->state;
  473. wqe->first_psn = rollback_wqe->first_psn;
  474. wqe->last_psn = rollback_wqe->last_psn;
  475. qp->req.psn = rollback_psn;
  476. }
  477. static void update_state(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
  478. struct rxe_pkt_info *pkt, int payload)
  479. {
  480. qp->req.opcode = pkt->opcode;
  481. if (pkt->mask & RXE_END_MASK)
  482. qp->req.wqe_index = next_index(qp->sq.queue, qp->req.wqe_index);
  483. qp->need_req_skb = 0;
  484. if (qp->qp_timeout_jiffies && !timer_pending(&qp->retrans_timer))
  485. mod_timer(&qp->retrans_timer,
  486. jiffies + qp->qp_timeout_jiffies);
  487. }
  488. int rxe_requester(void *arg)
  489. {
  490. struct rxe_qp *qp = (struct rxe_qp *)arg;
  491. struct rxe_pkt_info pkt;
  492. struct sk_buff *skb;
  493. struct rxe_send_wqe *wqe;
  494. enum rxe_hdr_mask mask;
  495. int payload;
  496. int mtu;
  497. int opcode;
  498. int ret;
  499. struct rxe_send_wqe rollback_wqe;
  500. u32 rollback_psn;
  501. next_wqe:
  502. if (unlikely(!qp->valid || qp->req.state == QP_STATE_ERROR))
  503. goto exit;
  504. if (unlikely(qp->req.state == QP_STATE_RESET)) {
  505. qp->req.wqe_index = consumer_index(qp->sq.queue);
  506. qp->req.opcode = -1;
  507. qp->req.need_rd_atomic = 0;
  508. qp->req.wait_psn = 0;
  509. qp->req.need_retry = 0;
  510. goto exit;
  511. }
  512. if (unlikely(qp->req.need_retry)) {
  513. req_retry(qp);
  514. qp->req.need_retry = 0;
  515. }
  516. wqe = req_next_wqe(qp);
  517. if (unlikely(!wqe))
  518. goto exit;
  519. if (wqe->mask & WR_REG_MASK) {
  520. if (wqe->wr.opcode == IB_WR_LOCAL_INV) {
  521. struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
  522. struct rxe_mem *rmr;
  523. rmr = rxe_pool_get_index(&rxe->mr_pool,
  524. wqe->wr.ex.invalidate_rkey >> 8);
  525. if (!rmr) {
  526. pr_err("No mr for key %#x\n",
  527. wqe->wr.ex.invalidate_rkey);
  528. wqe->state = wqe_state_error;
  529. wqe->status = IB_WC_MW_BIND_ERR;
  530. goto exit;
  531. }
  532. rmr->state = RXE_MEM_STATE_FREE;
  533. rxe_drop_ref(rmr);
  534. wqe->state = wqe_state_done;
  535. wqe->status = IB_WC_SUCCESS;
  536. } else if (wqe->wr.opcode == IB_WR_REG_MR) {
  537. struct rxe_mem *rmr = to_rmr(wqe->wr.wr.reg.mr);
  538. rmr->state = RXE_MEM_STATE_VALID;
  539. rmr->access = wqe->wr.wr.reg.access;
  540. rmr->lkey = wqe->wr.wr.reg.key;
  541. rmr->rkey = wqe->wr.wr.reg.key;
  542. wqe->state = wqe_state_done;
  543. wqe->status = IB_WC_SUCCESS;
  544. } else {
  545. goto exit;
  546. }
  547. qp->req.wqe_index = next_index(qp->sq.queue,
  548. qp->req.wqe_index);
  549. goto next_wqe;
  550. }
  551. if (unlikely(qp_type(qp) == IB_QPT_RC &&
  552. qp->req.psn > (qp->comp.psn + RXE_MAX_UNACKED_PSNS))) {
  553. qp->req.wait_psn = 1;
  554. goto exit;
  555. }
  556. /* Limit the number of inflight SKBs per QP */
  557. if (unlikely(atomic_read(&qp->skb_out) >
  558. RXE_INFLIGHT_SKBS_PER_QP_HIGH)) {
  559. qp->need_req_skb = 1;
  560. goto exit;
  561. }
  562. opcode = next_opcode(qp, wqe, wqe->wr.opcode);
  563. if (unlikely(opcode < 0)) {
  564. wqe->status = IB_WC_LOC_QP_OP_ERR;
  565. goto exit;
  566. }
  567. mask = rxe_opcode[opcode].mask;
  568. if (unlikely(mask & RXE_READ_OR_ATOMIC)) {
  569. if (check_init_depth(qp, wqe))
  570. goto exit;
  571. }
  572. mtu = get_mtu(qp, wqe);
  573. payload = (mask & RXE_WRITE_OR_SEND) ? wqe->dma.resid : 0;
  574. if (payload > mtu) {
  575. if (qp_type(qp) == IB_QPT_UD) {
  576. /* C10-93.1.1: If the total sum of all the buffer lengths specified for a
  577. * UD message exceeds the MTU of the port as returned by QueryHCA, the CI
  578. * shall not emit any packets for this message. Further, the CI shall not
  579. * generate an error due to this condition.
  580. */
  581. /* fake a successful UD send */
  582. wqe->first_psn = qp->req.psn;
  583. wqe->last_psn = qp->req.psn;
  584. qp->req.psn = (qp->req.psn + 1) & BTH_PSN_MASK;
  585. qp->req.opcode = IB_OPCODE_UD_SEND_ONLY;
  586. qp->req.wqe_index = next_index(qp->sq.queue,
  587. qp->req.wqe_index);
  588. wqe->state = wqe_state_done;
  589. wqe->status = IB_WC_SUCCESS;
  590. __rxe_do_task(&qp->comp.task);
  591. return 0;
  592. }
  593. payload = mtu;
  594. }
  595. skb = init_req_packet(qp, wqe, opcode, payload, &pkt);
  596. if (unlikely(!skb)) {
  597. pr_err("qp#%d Failed allocating skb\n", qp_num(qp));
  598. goto err;
  599. }
  600. if (fill_packet(qp, wqe, &pkt, skb, payload)) {
  601. pr_debug("qp#%d Error during fill packet\n", qp_num(qp));
  602. goto err;
  603. }
  604. /*
  605. * To prevent a race on wqe access between requester and completer,
  606. * wqe members state and psn need to be set before calling
  607. * rxe_xmit_packet().
  608. * Otherwise, completer might initiate an unjustified retry flow.
  609. */
  610. save_state(wqe, qp, &rollback_wqe, &rollback_psn);
  611. update_wqe_state(qp, wqe, &pkt);
  612. update_wqe_psn(qp, wqe, &pkt, payload);
  613. ret = rxe_xmit_packet(to_rdev(qp->ibqp.device), qp, &pkt, skb);
  614. if (ret) {
  615. qp->need_req_skb = 1;
  616. rollback_state(wqe, qp, &rollback_wqe, rollback_psn);
  617. if (ret == -EAGAIN) {
  618. kfree_skb(skb);
  619. rxe_run_task(&qp->req.task, 1);
  620. goto exit;
  621. }
  622. goto err;
  623. }
  624. update_state(qp, wqe, &pkt, payload);
  625. goto next_wqe;
  626. err:
  627. kfree_skb(skb);
  628. wqe->status = IB_WC_LOC_PROT_ERR;
  629. wqe->state = wqe_state_error;
  630. /*
  631. * IBA Spec. Section 10.7.3.1 SIGNALED COMPLETIONS
  632. * ---------8<---------8<-------------
  633. * ...Note that if a completion error occurs, a Work Completion
  634. * will always be generated, even if the signaling
  635. * indicator requests an Unsignaled Completion.
  636. * ---------8<---------8<-------------
  637. */
  638. wqe->wr.send_flags |= IB_SEND_SIGNALED;
  639. __rxe_do_task(&qp->comp.task);
  640. return -EAGAIN;
  641. exit:
  642. return -EAGAIN;
  643. }