rxe_qp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  5. */
  6. #include <linux/skbuff.h>
  7. #include <linux/delay.h>
  8. #include <linux/sched.h>
  9. #include <linux/vmalloc.h>
  10. #include <rdma/uverbs_ioctl.h>
  11. #include "rxe.h"
  12. #include "rxe_loc.h"
  13. #include "rxe_queue.h"
  14. #include "rxe_task.h"
  15. static int rxe_qp_chk_cap(struct rxe_dev *rxe, struct ib_qp_cap *cap,
  16. int has_srq)
  17. {
  18. if (cap->max_send_wr > rxe->attr.max_qp_wr) {
  19. pr_debug("invalid send wr = %u > %d\n",
  20. cap->max_send_wr, rxe->attr.max_qp_wr);
  21. goto err1;
  22. }
  23. if (cap->max_send_sge > rxe->attr.max_send_sge) {
  24. pr_debug("invalid send sge = %u > %d\n",
  25. cap->max_send_sge, rxe->attr.max_send_sge);
  26. goto err1;
  27. }
  28. if (!has_srq) {
  29. if (cap->max_recv_wr > rxe->attr.max_qp_wr) {
  30. pr_debug("invalid recv wr = %u > %d\n",
  31. cap->max_recv_wr, rxe->attr.max_qp_wr);
  32. goto err1;
  33. }
  34. if (cap->max_recv_sge > rxe->attr.max_recv_sge) {
  35. pr_debug("invalid recv sge = %u > %d\n",
  36. cap->max_recv_sge, rxe->attr.max_recv_sge);
  37. goto err1;
  38. }
  39. }
  40. if (cap->max_inline_data > rxe->max_inline_data) {
  41. pr_debug("invalid max inline data = %u > %d\n",
  42. cap->max_inline_data, rxe->max_inline_data);
  43. goto err1;
  44. }
  45. return 0;
  46. err1:
  47. return -EINVAL;
  48. }
  49. int rxe_qp_chk_init(struct rxe_dev *rxe, struct ib_qp_init_attr *init)
  50. {
  51. struct ib_qp_cap *cap = &init->cap;
  52. struct rxe_port *port;
  53. int port_num = init->port_num;
  54. switch (init->qp_type) {
  55. case IB_QPT_GSI:
  56. case IB_QPT_RC:
  57. case IB_QPT_UC:
  58. case IB_QPT_UD:
  59. break;
  60. default:
  61. return -EOPNOTSUPP;
  62. }
  63. if (!init->recv_cq || !init->send_cq) {
  64. pr_debug("missing cq\n");
  65. goto err1;
  66. }
  67. if (rxe_qp_chk_cap(rxe, cap, !!init->srq))
  68. goto err1;
  69. if (init->qp_type == IB_QPT_GSI) {
  70. if (!rdma_is_port_valid(&rxe->ib_dev, port_num)) {
  71. pr_debug("invalid port = %d\n", port_num);
  72. goto err1;
  73. }
  74. port = &rxe->port;
  75. if (init->qp_type == IB_QPT_GSI && port->qp_gsi_index) {
  76. pr_debug("GSI QP exists for port %d\n", port_num);
  77. goto err1;
  78. }
  79. }
  80. return 0;
  81. err1:
  82. return -EINVAL;
  83. }
  84. static int alloc_rd_atomic_resources(struct rxe_qp *qp, unsigned int n)
  85. {
  86. qp->resp.res_head = 0;
  87. qp->resp.res_tail = 0;
  88. qp->resp.resources = kcalloc(n, sizeof(struct resp_res), GFP_KERNEL);
  89. if (!qp->resp.resources)
  90. return -ENOMEM;
  91. return 0;
  92. }
  93. static void free_rd_atomic_resources(struct rxe_qp *qp)
  94. {
  95. if (qp->resp.resources) {
  96. int i;
  97. for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
  98. struct resp_res *res = &qp->resp.resources[i];
  99. free_rd_atomic_resource(res);
  100. }
  101. kfree(qp->resp.resources);
  102. qp->resp.resources = NULL;
  103. }
  104. }
  105. void free_rd_atomic_resource(struct resp_res *res)
  106. {
  107. res->type = 0;
  108. }
  109. static void cleanup_rd_atomic_resources(struct rxe_qp *qp)
  110. {
  111. int i;
  112. struct resp_res *res;
  113. if (qp->resp.resources) {
  114. for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
  115. res = &qp->resp.resources[i];
  116. free_rd_atomic_resource(res);
  117. }
  118. }
  119. }
  120. static void rxe_qp_init_misc(struct rxe_dev *rxe, struct rxe_qp *qp,
  121. struct ib_qp_init_attr *init)
  122. {
  123. struct rxe_port *port;
  124. u32 qpn;
  125. qp->sq_sig_type = init->sq_sig_type;
  126. qp->attr.path_mtu = 1;
  127. qp->mtu = ib_mtu_enum_to_int(qp->attr.path_mtu);
  128. qpn = qp->elem.index;
  129. port = &rxe->port;
  130. switch (init->qp_type) {
  131. case IB_QPT_GSI:
  132. qp->ibqp.qp_num = 1;
  133. port->qp_gsi_index = qpn;
  134. qp->attr.port_num = init->port_num;
  135. break;
  136. default:
  137. qp->ibqp.qp_num = qpn;
  138. break;
  139. }
  140. spin_lock_init(&qp->state_lock);
  141. spin_lock_init(&qp->req.task.state_lock);
  142. spin_lock_init(&qp->resp.task.state_lock);
  143. spin_lock_init(&qp->comp.task.state_lock);
  144. spin_lock_init(&qp->sq.sq_lock);
  145. spin_lock_init(&qp->rq.producer_lock);
  146. spin_lock_init(&qp->rq.consumer_lock);
  147. skb_queue_head_init(&qp->req_pkts);
  148. skb_queue_head_init(&qp->resp_pkts);
  149. atomic_set(&qp->ssn, 0);
  150. atomic_set(&qp->skb_out, 0);
  151. }
  152. static int rxe_qp_init_req(struct rxe_dev *rxe, struct rxe_qp *qp,
  153. struct ib_qp_init_attr *init, struct ib_udata *udata,
  154. struct rxe_create_qp_resp __user *uresp)
  155. {
  156. int err;
  157. int wqe_size;
  158. enum queue_type type;
  159. err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0, &qp->sk);
  160. if (err < 0)
  161. return err;
  162. qp->sk->sk->sk_user_data = qp;
  163. /* pick a source UDP port number for this QP based on
  164. * the source QPN. this spreads traffic for different QPs
  165. * across different NIC RX queues (while using a single
  166. * flow for a given QP to maintain packet order).
  167. * the port number must be in the Dynamic Ports range
  168. * (0xc000 - 0xffff).
  169. */
  170. qp->src_port = RXE_ROCE_V2_SPORT + (hash_32(qp_num(qp), 14) & 0x3fff);
  171. qp->sq.max_wr = init->cap.max_send_wr;
  172. /* These caps are limited by rxe_qp_chk_cap() done by the caller */
  173. wqe_size = max_t(int, init->cap.max_send_sge * sizeof(struct ib_sge),
  174. init->cap.max_inline_data);
  175. qp->sq.max_sge = init->cap.max_send_sge =
  176. wqe_size / sizeof(struct ib_sge);
  177. qp->sq.max_inline = init->cap.max_inline_data = wqe_size;
  178. wqe_size += sizeof(struct rxe_send_wqe);
  179. type = QUEUE_TYPE_FROM_CLIENT;
  180. qp->sq.queue = rxe_queue_init(rxe, &qp->sq.max_wr,
  181. wqe_size, type);
  182. if (!qp->sq.queue)
  183. return -ENOMEM;
  184. err = do_mmap_info(rxe, uresp ? &uresp->sq_mi : NULL, udata,
  185. qp->sq.queue->buf, qp->sq.queue->buf_size,
  186. &qp->sq.queue->ip);
  187. if (err) {
  188. vfree(qp->sq.queue->buf);
  189. kfree(qp->sq.queue);
  190. qp->sq.queue = NULL;
  191. return err;
  192. }
  193. qp->req.wqe_index = queue_get_producer(qp->sq.queue,
  194. QUEUE_TYPE_FROM_CLIENT);
  195. qp->req.state = QP_STATE_RESET;
  196. qp->comp.state = QP_STATE_RESET;
  197. qp->req.opcode = -1;
  198. qp->comp.opcode = -1;
  199. rxe_init_task(&qp->req.task, qp, rxe_requester);
  200. rxe_init_task(&qp->comp.task, qp, rxe_completer);
  201. qp->qp_timeout_jiffies = 0; /* Can't be set for UD/UC in modify_qp */
  202. if (init->qp_type == IB_QPT_RC) {
  203. timer_setup(&qp->rnr_nak_timer, rnr_nak_timer, 0);
  204. timer_setup(&qp->retrans_timer, retransmit_timer, 0);
  205. }
  206. return 0;
  207. }
  208. static int rxe_qp_init_resp(struct rxe_dev *rxe, struct rxe_qp *qp,
  209. struct ib_qp_init_attr *init,
  210. struct ib_udata *udata,
  211. struct rxe_create_qp_resp __user *uresp)
  212. {
  213. int err;
  214. int wqe_size;
  215. enum queue_type type;
  216. if (!qp->srq) {
  217. qp->rq.max_wr = init->cap.max_recv_wr;
  218. qp->rq.max_sge = init->cap.max_recv_sge;
  219. wqe_size = rcv_wqe_size(qp->rq.max_sge);
  220. pr_debug("qp#%d max_wr = %d, max_sge = %d, wqe_size = %d\n",
  221. qp_num(qp), qp->rq.max_wr, qp->rq.max_sge, wqe_size);
  222. type = QUEUE_TYPE_FROM_CLIENT;
  223. qp->rq.queue = rxe_queue_init(rxe, &qp->rq.max_wr,
  224. wqe_size, type);
  225. if (!qp->rq.queue)
  226. return -ENOMEM;
  227. err = do_mmap_info(rxe, uresp ? &uresp->rq_mi : NULL, udata,
  228. qp->rq.queue->buf, qp->rq.queue->buf_size,
  229. &qp->rq.queue->ip);
  230. if (err) {
  231. vfree(qp->rq.queue->buf);
  232. kfree(qp->rq.queue);
  233. qp->rq.queue = NULL;
  234. return err;
  235. }
  236. }
  237. rxe_init_task(&qp->resp.task, qp, rxe_responder);
  238. qp->resp.opcode = OPCODE_NONE;
  239. qp->resp.msn = 0;
  240. qp->resp.state = QP_STATE_RESET;
  241. return 0;
  242. }
  243. /* called by the create qp verb */
  244. int rxe_qp_from_init(struct rxe_dev *rxe, struct rxe_qp *qp, struct rxe_pd *pd,
  245. struct ib_qp_init_attr *init,
  246. struct rxe_create_qp_resp __user *uresp,
  247. struct ib_pd *ibpd,
  248. struct ib_udata *udata)
  249. {
  250. int err;
  251. struct rxe_cq *rcq = to_rcq(init->recv_cq);
  252. struct rxe_cq *scq = to_rcq(init->send_cq);
  253. struct rxe_srq *srq = init->srq ? to_rsrq(init->srq) : NULL;
  254. rxe_get(pd);
  255. rxe_get(rcq);
  256. rxe_get(scq);
  257. if (srq)
  258. rxe_get(srq);
  259. qp->pd = pd;
  260. qp->rcq = rcq;
  261. qp->scq = scq;
  262. qp->srq = srq;
  263. atomic_inc(&rcq->num_wq);
  264. atomic_inc(&scq->num_wq);
  265. rxe_qp_init_misc(rxe, qp, init);
  266. err = rxe_qp_init_req(rxe, qp, init, udata, uresp);
  267. if (err)
  268. goto err1;
  269. err = rxe_qp_init_resp(rxe, qp, init, udata, uresp);
  270. if (err)
  271. goto err2;
  272. qp->attr.qp_state = IB_QPS_RESET;
  273. qp->valid = 1;
  274. return 0;
  275. err2:
  276. rxe_queue_cleanup(qp->sq.queue);
  277. qp->sq.queue = NULL;
  278. err1:
  279. atomic_dec(&rcq->num_wq);
  280. atomic_dec(&scq->num_wq);
  281. qp->pd = NULL;
  282. qp->rcq = NULL;
  283. qp->scq = NULL;
  284. qp->srq = NULL;
  285. if (srq)
  286. rxe_put(srq);
  287. rxe_put(scq);
  288. rxe_put(rcq);
  289. rxe_put(pd);
  290. return err;
  291. }
  292. /* called by the query qp verb */
  293. int rxe_qp_to_init(struct rxe_qp *qp, struct ib_qp_init_attr *init)
  294. {
  295. init->event_handler = qp->ibqp.event_handler;
  296. init->qp_context = qp->ibqp.qp_context;
  297. init->send_cq = qp->ibqp.send_cq;
  298. init->recv_cq = qp->ibqp.recv_cq;
  299. init->srq = qp->ibqp.srq;
  300. init->cap.max_send_wr = qp->sq.max_wr;
  301. init->cap.max_send_sge = qp->sq.max_sge;
  302. init->cap.max_inline_data = qp->sq.max_inline;
  303. if (!qp->srq) {
  304. init->cap.max_recv_wr = qp->rq.max_wr;
  305. init->cap.max_recv_sge = qp->rq.max_sge;
  306. }
  307. init->sq_sig_type = qp->sq_sig_type;
  308. init->qp_type = qp->ibqp.qp_type;
  309. init->port_num = 1;
  310. return 0;
  311. }
  312. /* called by the modify qp verb, this routine checks all the parameters before
  313. * making any changes
  314. */
  315. int rxe_qp_chk_attr(struct rxe_dev *rxe, struct rxe_qp *qp,
  316. struct ib_qp_attr *attr, int mask)
  317. {
  318. enum ib_qp_state cur_state = (mask & IB_QP_CUR_STATE) ?
  319. attr->cur_qp_state : qp->attr.qp_state;
  320. enum ib_qp_state new_state = (mask & IB_QP_STATE) ?
  321. attr->qp_state : cur_state;
  322. if (!ib_modify_qp_is_ok(cur_state, new_state, qp_type(qp), mask)) {
  323. pr_debug("invalid mask or state for qp\n");
  324. goto err1;
  325. }
  326. if (mask & IB_QP_STATE) {
  327. if (cur_state == IB_QPS_SQD) {
  328. if (qp->req.state == QP_STATE_DRAIN &&
  329. new_state != IB_QPS_ERR)
  330. goto err1;
  331. }
  332. }
  333. if (mask & IB_QP_PORT) {
  334. if (!rdma_is_port_valid(&rxe->ib_dev, attr->port_num)) {
  335. pr_debug("invalid port %d\n", attr->port_num);
  336. goto err1;
  337. }
  338. }
  339. if (mask & IB_QP_CAP && rxe_qp_chk_cap(rxe, &attr->cap, !!qp->srq))
  340. goto err1;
  341. if (mask & IB_QP_AV && rxe_av_chk_attr(rxe, &attr->ah_attr))
  342. goto err1;
  343. if (mask & IB_QP_ALT_PATH) {
  344. if (rxe_av_chk_attr(rxe, &attr->alt_ah_attr))
  345. goto err1;
  346. if (!rdma_is_port_valid(&rxe->ib_dev, attr->alt_port_num)) {
  347. pr_debug("invalid alt port %d\n", attr->alt_port_num);
  348. goto err1;
  349. }
  350. if (attr->alt_timeout > 31) {
  351. pr_debug("invalid QP alt timeout %d > 31\n",
  352. attr->alt_timeout);
  353. goto err1;
  354. }
  355. }
  356. if (mask & IB_QP_PATH_MTU) {
  357. struct rxe_port *port = &rxe->port;
  358. enum ib_mtu max_mtu = port->attr.max_mtu;
  359. enum ib_mtu mtu = attr->path_mtu;
  360. if (mtu > max_mtu) {
  361. pr_debug("invalid mtu (%d) > (%d)\n",
  362. ib_mtu_enum_to_int(mtu),
  363. ib_mtu_enum_to_int(max_mtu));
  364. goto err1;
  365. }
  366. }
  367. if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
  368. if (attr->max_rd_atomic > rxe->attr.max_qp_rd_atom) {
  369. pr_debug("invalid max_rd_atomic %d > %d\n",
  370. attr->max_rd_atomic,
  371. rxe->attr.max_qp_rd_atom);
  372. goto err1;
  373. }
  374. }
  375. if (mask & IB_QP_TIMEOUT) {
  376. if (attr->timeout > 31) {
  377. pr_debug("invalid QP timeout %d > 31\n", attr->timeout);
  378. goto err1;
  379. }
  380. }
  381. return 0;
  382. err1:
  383. return -EINVAL;
  384. }
  385. /* move the qp to the reset state */
  386. static void rxe_qp_reset(struct rxe_qp *qp)
  387. {
  388. /* stop tasks from running */
  389. rxe_disable_task(&qp->resp.task);
  390. /* stop request/comp */
  391. if (qp->sq.queue) {
  392. if (qp_type(qp) == IB_QPT_RC)
  393. rxe_disable_task(&qp->comp.task);
  394. rxe_disable_task(&qp->req.task);
  395. }
  396. /* move qp to the reset state */
  397. qp->req.state = QP_STATE_RESET;
  398. qp->comp.state = QP_STATE_RESET;
  399. qp->resp.state = QP_STATE_RESET;
  400. /* let state machines reset themselves drain work and packet queues
  401. * etc.
  402. */
  403. __rxe_do_task(&qp->resp.task);
  404. if (qp->sq.queue) {
  405. __rxe_do_task(&qp->comp.task);
  406. __rxe_do_task(&qp->req.task);
  407. rxe_queue_reset(qp->sq.queue);
  408. }
  409. /* cleanup attributes */
  410. atomic_set(&qp->ssn, 0);
  411. qp->req.opcode = -1;
  412. qp->req.need_retry = 0;
  413. qp->req.wait_for_rnr_timer = 0;
  414. qp->req.noack_pkts = 0;
  415. qp->resp.msn = 0;
  416. qp->resp.opcode = -1;
  417. qp->resp.drop_msg = 0;
  418. qp->resp.goto_error = 0;
  419. qp->resp.sent_psn_nak = 0;
  420. if (qp->resp.mr) {
  421. rxe_put(qp->resp.mr);
  422. qp->resp.mr = NULL;
  423. }
  424. cleanup_rd_atomic_resources(qp);
  425. /* reenable tasks */
  426. rxe_enable_task(&qp->resp.task);
  427. if (qp->sq.queue) {
  428. if (qp_type(qp) == IB_QPT_RC)
  429. rxe_enable_task(&qp->comp.task);
  430. rxe_enable_task(&qp->req.task);
  431. }
  432. }
  433. /* drain the send queue */
  434. static void rxe_qp_drain(struct rxe_qp *qp)
  435. {
  436. if (qp->sq.queue) {
  437. if (qp->req.state != QP_STATE_DRAINED) {
  438. qp->req.state = QP_STATE_DRAIN;
  439. if (qp_type(qp) == IB_QPT_RC)
  440. rxe_sched_task(&qp->comp.task);
  441. else
  442. __rxe_do_task(&qp->comp.task);
  443. rxe_sched_task(&qp->req.task);
  444. }
  445. }
  446. }
  447. /* move the qp to the error state */
  448. void rxe_qp_error(struct rxe_qp *qp)
  449. {
  450. qp->req.state = QP_STATE_ERROR;
  451. qp->resp.state = QP_STATE_ERROR;
  452. qp->comp.state = QP_STATE_ERROR;
  453. qp->attr.qp_state = IB_QPS_ERR;
  454. /* drain work and packet queues */
  455. rxe_sched_task(&qp->resp.task);
  456. if (qp_type(qp) == IB_QPT_RC)
  457. rxe_sched_task(&qp->comp.task);
  458. else
  459. __rxe_do_task(&qp->comp.task);
  460. rxe_sched_task(&qp->req.task);
  461. }
  462. /* called by the modify qp verb */
  463. int rxe_qp_from_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask,
  464. struct ib_udata *udata)
  465. {
  466. int err;
  467. if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
  468. int max_rd_atomic = attr->max_rd_atomic ?
  469. roundup_pow_of_two(attr->max_rd_atomic) : 0;
  470. qp->attr.max_rd_atomic = max_rd_atomic;
  471. atomic_set(&qp->req.rd_atomic, max_rd_atomic);
  472. }
  473. if (mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  474. int max_dest_rd_atomic = attr->max_dest_rd_atomic ?
  475. roundup_pow_of_two(attr->max_dest_rd_atomic) : 0;
  476. qp->attr.max_dest_rd_atomic = max_dest_rd_atomic;
  477. free_rd_atomic_resources(qp);
  478. err = alloc_rd_atomic_resources(qp, max_dest_rd_atomic);
  479. if (err)
  480. return err;
  481. }
  482. if (mask & IB_QP_CUR_STATE)
  483. qp->attr.cur_qp_state = attr->qp_state;
  484. if (mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
  485. qp->attr.en_sqd_async_notify = attr->en_sqd_async_notify;
  486. if (mask & IB_QP_ACCESS_FLAGS)
  487. qp->attr.qp_access_flags = attr->qp_access_flags;
  488. if (mask & IB_QP_PKEY_INDEX)
  489. qp->attr.pkey_index = attr->pkey_index;
  490. if (mask & IB_QP_PORT)
  491. qp->attr.port_num = attr->port_num;
  492. if (mask & IB_QP_QKEY)
  493. qp->attr.qkey = attr->qkey;
  494. if (mask & IB_QP_AV)
  495. rxe_init_av(&attr->ah_attr, &qp->pri_av);
  496. if (mask & IB_QP_ALT_PATH) {
  497. rxe_init_av(&attr->alt_ah_attr, &qp->alt_av);
  498. qp->attr.alt_port_num = attr->alt_port_num;
  499. qp->attr.alt_pkey_index = attr->alt_pkey_index;
  500. qp->attr.alt_timeout = attr->alt_timeout;
  501. }
  502. if (mask & IB_QP_PATH_MTU) {
  503. qp->attr.path_mtu = attr->path_mtu;
  504. qp->mtu = ib_mtu_enum_to_int(attr->path_mtu);
  505. }
  506. if (mask & IB_QP_TIMEOUT) {
  507. qp->attr.timeout = attr->timeout;
  508. if (attr->timeout == 0) {
  509. qp->qp_timeout_jiffies = 0;
  510. } else {
  511. /* According to the spec, timeout = 4.096 * 2 ^ attr->timeout [us] */
  512. int j = nsecs_to_jiffies(4096ULL << attr->timeout);
  513. qp->qp_timeout_jiffies = j ? j : 1;
  514. }
  515. }
  516. if (mask & IB_QP_RETRY_CNT) {
  517. qp->attr.retry_cnt = attr->retry_cnt;
  518. qp->comp.retry_cnt = attr->retry_cnt;
  519. pr_debug("qp#%d set retry count = %d\n", qp_num(qp),
  520. attr->retry_cnt);
  521. }
  522. if (mask & IB_QP_RNR_RETRY) {
  523. qp->attr.rnr_retry = attr->rnr_retry;
  524. qp->comp.rnr_retry = attr->rnr_retry;
  525. pr_debug("qp#%d set rnr retry count = %d\n", qp_num(qp),
  526. attr->rnr_retry);
  527. }
  528. if (mask & IB_QP_RQ_PSN) {
  529. qp->attr.rq_psn = (attr->rq_psn & BTH_PSN_MASK);
  530. qp->resp.psn = qp->attr.rq_psn;
  531. pr_debug("qp#%d set resp psn = 0x%x\n", qp_num(qp),
  532. qp->resp.psn);
  533. }
  534. if (mask & IB_QP_MIN_RNR_TIMER) {
  535. qp->attr.min_rnr_timer = attr->min_rnr_timer;
  536. pr_debug("qp#%d set min rnr timer = 0x%x\n", qp_num(qp),
  537. attr->min_rnr_timer);
  538. }
  539. if (mask & IB_QP_SQ_PSN) {
  540. qp->attr.sq_psn = (attr->sq_psn & BTH_PSN_MASK);
  541. qp->req.psn = qp->attr.sq_psn;
  542. qp->comp.psn = qp->attr.sq_psn;
  543. pr_debug("qp#%d set req psn = 0x%x\n", qp_num(qp), qp->req.psn);
  544. }
  545. if (mask & IB_QP_PATH_MIG_STATE)
  546. qp->attr.path_mig_state = attr->path_mig_state;
  547. if (mask & IB_QP_DEST_QPN)
  548. qp->attr.dest_qp_num = attr->dest_qp_num;
  549. if (mask & IB_QP_STATE) {
  550. qp->attr.qp_state = attr->qp_state;
  551. switch (attr->qp_state) {
  552. case IB_QPS_RESET:
  553. pr_debug("qp#%d state -> RESET\n", qp_num(qp));
  554. rxe_qp_reset(qp);
  555. break;
  556. case IB_QPS_INIT:
  557. pr_debug("qp#%d state -> INIT\n", qp_num(qp));
  558. qp->req.state = QP_STATE_INIT;
  559. qp->resp.state = QP_STATE_INIT;
  560. qp->comp.state = QP_STATE_INIT;
  561. break;
  562. case IB_QPS_RTR:
  563. pr_debug("qp#%d state -> RTR\n", qp_num(qp));
  564. qp->resp.state = QP_STATE_READY;
  565. break;
  566. case IB_QPS_RTS:
  567. pr_debug("qp#%d state -> RTS\n", qp_num(qp));
  568. qp->req.state = QP_STATE_READY;
  569. qp->comp.state = QP_STATE_READY;
  570. break;
  571. case IB_QPS_SQD:
  572. pr_debug("qp#%d state -> SQD\n", qp_num(qp));
  573. rxe_qp_drain(qp);
  574. break;
  575. case IB_QPS_SQE:
  576. pr_warn("qp#%d state -> SQE !!?\n", qp_num(qp));
  577. /* Not possible from modify_qp. */
  578. break;
  579. case IB_QPS_ERR:
  580. pr_debug("qp#%d state -> ERR\n", qp_num(qp));
  581. rxe_qp_error(qp);
  582. break;
  583. }
  584. }
  585. return 0;
  586. }
  587. /* called by the query qp verb */
  588. int rxe_qp_to_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask)
  589. {
  590. *attr = qp->attr;
  591. attr->rq_psn = qp->resp.psn;
  592. attr->sq_psn = qp->req.psn;
  593. attr->cap.max_send_wr = qp->sq.max_wr;
  594. attr->cap.max_send_sge = qp->sq.max_sge;
  595. attr->cap.max_inline_data = qp->sq.max_inline;
  596. if (!qp->srq) {
  597. attr->cap.max_recv_wr = qp->rq.max_wr;
  598. attr->cap.max_recv_sge = qp->rq.max_sge;
  599. }
  600. rxe_av_to_attr(&qp->pri_av, &attr->ah_attr);
  601. rxe_av_to_attr(&qp->alt_av, &attr->alt_ah_attr);
  602. if (qp->req.state == QP_STATE_DRAIN) {
  603. attr->sq_draining = 1;
  604. /* applications that get this state
  605. * typically spin on it. yield the
  606. * processor
  607. */
  608. cond_resched();
  609. } else {
  610. attr->sq_draining = 0;
  611. }
  612. pr_debug("attr->sq_draining = %d\n", attr->sq_draining);
  613. return 0;
  614. }
  615. int rxe_qp_chk_destroy(struct rxe_qp *qp)
  616. {
  617. /* See IBA o10-2.2.3
  618. * An attempt to destroy a QP while attached to a mcast group
  619. * will fail immediately.
  620. */
  621. if (atomic_read(&qp->mcg_num)) {
  622. pr_debug("Attempt to destroy QP while attached to multicast group\n");
  623. return -EBUSY;
  624. }
  625. return 0;
  626. }
  627. /* called when the last reference to the qp is dropped */
  628. static void rxe_qp_do_cleanup(struct work_struct *work)
  629. {
  630. struct rxe_qp *qp = container_of(work, typeof(*qp), cleanup_work.work);
  631. qp->valid = 0;
  632. qp->qp_timeout_jiffies = 0;
  633. rxe_cleanup_task(&qp->resp.task);
  634. if (qp_type(qp) == IB_QPT_RC) {
  635. del_timer_sync(&qp->retrans_timer);
  636. del_timer_sync(&qp->rnr_nak_timer);
  637. }
  638. if (qp->req.task.func)
  639. rxe_cleanup_task(&qp->req.task);
  640. if (qp->comp.task.func)
  641. rxe_cleanup_task(&qp->comp.task);
  642. /* flush out any receive wr's or pending requests */
  643. if (qp->req.task.func)
  644. __rxe_do_task(&qp->req.task);
  645. if (qp->sq.queue) {
  646. __rxe_do_task(&qp->comp.task);
  647. __rxe_do_task(&qp->req.task);
  648. }
  649. if (qp->sq.queue)
  650. rxe_queue_cleanup(qp->sq.queue);
  651. if (qp->srq)
  652. rxe_put(qp->srq);
  653. if (qp->rq.queue)
  654. rxe_queue_cleanup(qp->rq.queue);
  655. if (qp->scq) {
  656. atomic_dec(&qp->scq->num_wq);
  657. rxe_put(qp->scq);
  658. }
  659. if (qp->rcq) {
  660. atomic_dec(&qp->rcq->num_wq);
  661. rxe_put(qp->rcq);
  662. }
  663. if (qp->pd)
  664. rxe_put(qp->pd);
  665. if (qp->resp.mr)
  666. rxe_put(qp->resp.mr);
  667. free_rd_atomic_resources(qp);
  668. if (qp->sk) {
  669. if (qp_type(qp) == IB_QPT_RC)
  670. sk_dst_reset(qp->sk->sk);
  671. kernel_sock_shutdown(qp->sk, SHUT_RDWR);
  672. sock_release(qp->sk);
  673. }
  674. }
  675. /* called when the last reference to the qp is dropped */
  676. void rxe_qp_cleanup(struct rxe_pool_elem *elem)
  677. {
  678. struct rxe_qp *qp = container_of(elem, typeof(*qp), elem);
  679. execute_in_process_context(rxe_qp_do_cleanup, &qp->cleanup_work);
  680. }