rxe_qp.c 19 KB

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