rxe_qp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. qp->sq.queue = NULL;
  210. return err;
  211. }
  212. qp->req.wqe_index = producer_index(qp->sq.queue);
  213. qp->req.state = QP_STATE_RESET;
  214. qp->req.opcode = -1;
  215. qp->comp.opcode = -1;
  216. spin_lock_init(&qp->sq.sq_lock);
  217. skb_queue_head_init(&qp->req_pkts);
  218. rxe_init_task(rxe, &qp->req.task, qp,
  219. rxe_requester, "req");
  220. rxe_init_task(rxe, &qp->comp.task, qp,
  221. rxe_completer, "comp");
  222. qp->qp_timeout_jiffies = 0; /* Can't be set for UD/UC in modify_qp */
  223. if (init->qp_type == IB_QPT_RC) {
  224. timer_setup(&qp->rnr_nak_timer, rnr_nak_timer, 0);
  225. timer_setup(&qp->retrans_timer, retransmit_timer, 0);
  226. }
  227. return 0;
  228. }
  229. static int rxe_qp_init_resp(struct rxe_dev *rxe, struct rxe_qp *qp,
  230. struct ib_qp_init_attr *init,
  231. struct ib_ucontext *context,
  232. struct rxe_create_qp_resp __user *uresp)
  233. {
  234. int err;
  235. int wqe_size;
  236. if (!qp->srq) {
  237. qp->rq.max_wr = init->cap.max_recv_wr;
  238. qp->rq.max_sge = init->cap.max_recv_sge;
  239. wqe_size = rcv_wqe_size(qp->rq.max_sge);
  240. pr_debug("qp#%d max_wr = %d, max_sge = %d, wqe_size = %d\n",
  241. qp_num(qp), qp->rq.max_wr, qp->rq.max_sge, wqe_size);
  242. qp->rq.queue = rxe_queue_init(rxe,
  243. &qp->rq.max_wr,
  244. wqe_size);
  245. if (!qp->rq.queue)
  246. return -ENOMEM;
  247. err = do_mmap_info(rxe, uresp ? &uresp->rq_mi : NULL, context,
  248. qp->rq.queue->buf, qp->rq.queue->buf_size,
  249. &qp->rq.queue->ip);
  250. if (err) {
  251. vfree(qp->rq.queue->buf);
  252. kfree(qp->rq.queue);
  253. qp->rq.queue = NULL;
  254. return err;
  255. }
  256. }
  257. spin_lock_init(&qp->rq.producer_lock);
  258. spin_lock_init(&qp->rq.consumer_lock);
  259. skb_queue_head_init(&qp->resp_pkts);
  260. rxe_init_task(rxe, &qp->resp.task, qp,
  261. rxe_responder, "resp");
  262. qp->resp.opcode = OPCODE_NONE;
  263. qp->resp.msn = 0;
  264. qp->resp.state = QP_STATE_RESET;
  265. return 0;
  266. }
  267. /* called by the create qp verb */
  268. int rxe_qp_from_init(struct rxe_dev *rxe, struct rxe_qp *qp, struct rxe_pd *pd,
  269. struct ib_qp_init_attr *init,
  270. struct rxe_create_qp_resp __user *uresp,
  271. struct ib_pd *ibpd)
  272. {
  273. int err;
  274. struct rxe_cq *rcq = to_rcq(init->recv_cq);
  275. struct rxe_cq *scq = to_rcq(init->send_cq);
  276. struct rxe_srq *srq = init->srq ? to_rsrq(init->srq) : NULL;
  277. struct ib_ucontext *context = ibpd->uobject ? ibpd->uobject->context : NULL;
  278. rxe_add_ref(pd);
  279. rxe_add_ref(rcq);
  280. rxe_add_ref(scq);
  281. if (srq)
  282. rxe_add_ref(srq);
  283. qp->pd = pd;
  284. qp->rcq = rcq;
  285. qp->scq = scq;
  286. qp->srq = srq;
  287. rxe_qp_init_misc(rxe, qp, init);
  288. err = rxe_qp_init_req(rxe, qp, init, context, uresp);
  289. if (err)
  290. goto err1;
  291. err = rxe_qp_init_resp(rxe, qp, init, context, uresp);
  292. if (err)
  293. goto err2;
  294. qp->attr.qp_state = IB_QPS_RESET;
  295. qp->valid = 1;
  296. return 0;
  297. err2:
  298. rxe_queue_cleanup(qp->sq.queue);
  299. err1:
  300. qp->pd = NULL;
  301. qp->rcq = NULL;
  302. qp->scq = NULL;
  303. qp->srq = NULL;
  304. if (srq)
  305. rxe_drop_ref(srq);
  306. rxe_drop_ref(scq);
  307. rxe_drop_ref(rcq);
  308. rxe_drop_ref(pd);
  309. return err;
  310. }
  311. /* called by the query qp verb */
  312. int rxe_qp_to_init(struct rxe_qp *qp, struct ib_qp_init_attr *init)
  313. {
  314. init->event_handler = qp->ibqp.event_handler;
  315. init->qp_context = qp->ibqp.qp_context;
  316. init->send_cq = qp->ibqp.send_cq;
  317. init->recv_cq = qp->ibqp.recv_cq;
  318. init->srq = qp->ibqp.srq;
  319. init->cap.max_send_wr = qp->sq.max_wr;
  320. init->cap.max_send_sge = qp->sq.max_sge;
  321. init->cap.max_inline_data = qp->sq.max_inline;
  322. if (!qp->srq) {
  323. init->cap.max_recv_wr = qp->rq.max_wr;
  324. init->cap.max_recv_sge = qp->rq.max_sge;
  325. }
  326. init->sq_sig_type = qp->sq_sig_type;
  327. init->qp_type = qp->ibqp.qp_type;
  328. init->port_num = 1;
  329. return 0;
  330. }
  331. /* called by the modify qp verb, this routine checks all the parameters before
  332. * making any changes
  333. */
  334. int rxe_qp_chk_attr(struct rxe_dev *rxe, struct rxe_qp *qp,
  335. struct ib_qp_attr *attr, int mask)
  336. {
  337. enum ib_qp_state cur_state = (mask & IB_QP_CUR_STATE) ?
  338. attr->cur_qp_state : qp->attr.qp_state;
  339. enum ib_qp_state new_state = (mask & IB_QP_STATE) ?
  340. attr->qp_state : cur_state;
  341. if (!ib_modify_qp_is_ok(cur_state, new_state, qp_type(qp), mask,
  342. IB_LINK_LAYER_ETHERNET)) {
  343. pr_warn("invalid mask or state for qp\n");
  344. goto err1;
  345. }
  346. if (mask & IB_QP_STATE) {
  347. if (cur_state == IB_QPS_SQD) {
  348. if (qp->req.state == QP_STATE_DRAIN &&
  349. new_state != IB_QPS_ERR)
  350. goto err1;
  351. }
  352. }
  353. if (mask & IB_QP_PORT) {
  354. if (attr->port_num != 1) {
  355. pr_warn("invalid port %d\n", attr->port_num);
  356. goto err1;
  357. }
  358. }
  359. if (mask & IB_QP_CAP && rxe_qp_chk_cap(rxe, &attr->cap, !!qp->srq))
  360. goto err1;
  361. if (mask & IB_QP_AV && rxe_av_chk_attr(rxe, &attr->ah_attr))
  362. goto err1;
  363. if (mask & IB_QP_ALT_PATH) {
  364. if (rxe_av_chk_attr(rxe, &attr->alt_ah_attr))
  365. goto err1;
  366. if (attr->alt_port_num != 1) {
  367. pr_warn("invalid alt port %d\n", attr->alt_port_num);
  368. goto err1;
  369. }
  370. if (attr->alt_timeout > 31) {
  371. pr_warn("invalid QP alt timeout %d > 31\n",
  372. attr->alt_timeout);
  373. goto err1;
  374. }
  375. }
  376. if (mask & IB_QP_PATH_MTU) {
  377. struct rxe_port *port = &rxe->port;
  378. enum ib_mtu max_mtu = port->attr.max_mtu;
  379. enum ib_mtu mtu = attr->path_mtu;
  380. if (mtu > max_mtu) {
  381. pr_debug("invalid mtu (%d) > (%d)\n",
  382. ib_mtu_enum_to_int(mtu),
  383. ib_mtu_enum_to_int(max_mtu));
  384. goto err1;
  385. }
  386. }
  387. if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
  388. if (attr->max_rd_atomic > rxe->attr.max_qp_rd_atom) {
  389. pr_warn("invalid max_rd_atomic %d > %d\n",
  390. attr->max_rd_atomic,
  391. rxe->attr.max_qp_rd_atom);
  392. goto err1;
  393. }
  394. }
  395. if (mask & IB_QP_TIMEOUT) {
  396. if (attr->timeout > 31) {
  397. pr_warn("invalid QP timeout %d > 31\n",
  398. attr->timeout);
  399. goto err1;
  400. }
  401. }
  402. return 0;
  403. err1:
  404. return -EINVAL;
  405. }
  406. /* move the qp to the reset state */
  407. static void rxe_qp_reset(struct rxe_qp *qp)
  408. {
  409. /* stop tasks from running */
  410. rxe_disable_task(&qp->resp.task);
  411. /* stop request/comp */
  412. if (qp->sq.queue) {
  413. if (qp_type(qp) == IB_QPT_RC)
  414. rxe_disable_task(&qp->comp.task);
  415. rxe_disable_task(&qp->req.task);
  416. }
  417. /* move qp to the reset state */
  418. qp->req.state = QP_STATE_RESET;
  419. qp->resp.state = QP_STATE_RESET;
  420. /* let state machines reset themselves drain work and packet queues
  421. * etc.
  422. */
  423. __rxe_do_task(&qp->resp.task);
  424. if (qp->sq.queue) {
  425. __rxe_do_task(&qp->comp.task);
  426. __rxe_do_task(&qp->req.task);
  427. rxe_queue_reset(qp->sq.queue);
  428. }
  429. /* cleanup attributes */
  430. atomic_set(&qp->ssn, 0);
  431. qp->req.opcode = -1;
  432. qp->req.need_retry = 0;
  433. qp->req.noack_pkts = 0;
  434. qp->resp.msn = 0;
  435. qp->resp.opcode = -1;
  436. qp->resp.drop_msg = 0;
  437. qp->resp.goto_error = 0;
  438. qp->resp.sent_psn_nak = 0;
  439. if (qp->resp.mr) {
  440. rxe_drop_ref(qp->resp.mr);
  441. qp->resp.mr = NULL;
  442. }
  443. cleanup_rd_atomic_resources(qp);
  444. /* reenable tasks */
  445. rxe_enable_task(&qp->resp.task);
  446. if (qp->sq.queue) {
  447. if (qp_type(qp) == IB_QPT_RC)
  448. rxe_enable_task(&qp->comp.task);
  449. rxe_enable_task(&qp->req.task);
  450. }
  451. }
  452. /* drain the send queue */
  453. static void rxe_qp_drain(struct rxe_qp *qp)
  454. {
  455. if (qp->sq.queue) {
  456. if (qp->req.state != QP_STATE_DRAINED) {
  457. qp->req.state = QP_STATE_DRAIN;
  458. if (qp_type(qp) == IB_QPT_RC)
  459. rxe_run_task(&qp->comp.task, 1);
  460. else
  461. __rxe_do_task(&qp->comp.task);
  462. rxe_run_task(&qp->req.task, 1);
  463. }
  464. }
  465. }
  466. /* move the qp to the error state */
  467. void rxe_qp_error(struct rxe_qp *qp)
  468. {
  469. qp->req.state = QP_STATE_ERROR;
  470. qp->resp.state = QP_STATE_ERROR;
  471. qp->attr.qp_state = IB_QPS_ERR;
  472. /* drain work and packet queues */
  473. rxe_run_task(&qp->resp.task, 1);
  474. if (qp_type(qp) == IB_QPT_RC)
  475. rxe_run_task(&qp->comp.task, 1);
  476. else
  477. __rxe_do_task(&qp->comp.task);
  478. rxe_run_task(&qp->req.task, 1);
  479. }
  480. /* called by the modify qp verb */
  481. int rxe_qp_from_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask,
  482. struct ib_udata *udata)
  483. {
  484. int err;
  485. if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
  486. int max_rd_atomic = attr->max_rd_atomic ?
  487. roundup_pow_of_two(attr->max_rd_atomic) : 0;
  488. qp->attr.max_rd_atomic = max_rd_atomic;
  489. atomic_set(&qp->req.rd_atomic, max_rd_atomic);
  490. }
  491. if (mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  492. int max_dest_rd_atomic = attr->max_dest_rd_atomic ?
  493. roundup_pow_of_two(attr->max_dest_rd_atomic) : 0;
  494. qp->attr.max_dest_rd_atomic = max_dest_rd_atomic;
  495. free_rd_atomic_resources(qp);
  496. err = alloc_rd_atomic_resources(qp, max_dest_rd_atomic);
  497. if (err)
  498. return err;
  499. }
  500. if (mask & IB_QP_CUR_STATE)
  501. qp->attr.cur_qp_state = attr->qp_state;
  502. if (mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
  503. qp->attr.en_sqd_async_notify = attr->en_sqd_async_notify;
  504. if (mask & IB_QP_ACCESS_FLAGS)
  505. qp->attr.qp_access_flags = attr->qp_access_flags;
  506. if (mask & IB_QP_PKEY_INDEX)
  507. qp->attr.pkey_index = attr->pkey_index;
  508. if (mask & IB_QP_PORT)
  509. qp->attr.port_num = attr->port_num;
  510. if (mask & IB_QP_QKEY)
  511. qp->attr.qkey = attr->qkey;
  512. if (mask & IB_QP_AV) {
  513. rxe_av_from_attr(attr->port_num, &qp->pri_av, &attr->ah_attr);
  514. rxe_av_fill_ip_info(&qp->pri_av, &attr->ah_attr);
  515. }
  516. if (mask & IB_QP_ALT_PATH) {
  517. rxe_av_from_attr(attr->alt_port_num, &qp->alt_av,
  518. &attr->alt_ah_attr);
  519. rxe_av_fill_ip_info(&qp->alt_av, &attr->alt_ah_attr);
  520. qp->attr.alt_port_num = attr->alt_port_num;
  521. qp->attr.alt_pkey_index = attr->alt_pkey_index;
  522. qp->attr.alt_timeout = attr->alt_timeout;
  523. }
  524. if (mask & IB_QP_PATH_MTU) {
  525. qp->attr.path_mtu = attr->path_mtu;
  526. qp->mtu = ib_mtu_enum_to_int(attr->path_mtu);
  527. }
  528. if (mask & IB_QP_TIMEOUT) {
  529. qp->attr.timeout = attr->timeout;
  530. if (attr->timeout == 0) {
  531. qp->qp_timeout_jiffies = 0;
  532. } else {
  533. /* According to the spec, timeout = 4.096 * 2 ^ attr->timeout [us] */
  534. int j = nsecs_to_jiffies(4096ULL << attr->timeout);
  535. qp->qp_timeout_jiffies = j ? j : 1;
  536. }
  537. }
  538. if (mask & IB_QP_RETRY_CNT) {
  539. qp->attr.retry_cnt = attr->retry_cnt;
  540. qp->comp.retry_cnt = attr->retry_cnt;
  541. pr_debug("qp#%d set retry count = %d\n", qp_num(qp),
  542. attr->retry_cnt);
  543. }
  544. if (mask & IB_QP_RNR_RETRY) {
  545. qp->attr.rnr_retry = attr->rnr_retry;
  546. qp->comp.rnr_retry = attr->rnr_retry;
  547. pr_debug("qp#%d set rnr retry count = %d\n", qp_num(qp),
  548. attr->rnr_retry);
  549. }
  550. if (mask & IB_QP_RQ_PSN) {
  551. qp->attr.rq_psn = (attr->rq_psn & BTH_PSN_MASK);
  552. qp->resp.psn = qp->attr.rq_psn;
  553. pr_debug("qp#%d set resp psn = 0x%x\n", qp_num(qp),
  554. qp->resp.psn);
  555. }
  556. if (mask & IB_QP_MIN_RNR_TIMER) {
  557. qp->attr.min_rnr_timer = attr->min_rnr_timer;
  558. pr_debug("qp#%d set min rnr timer = 0x%x\n", qp_num(qp),
  559. attr->min_rnr_timer);
  560. }
  561. if (mask & IB_QP_SQ_PSN) {
  562. qp->attr.sq_psn = (attr->sq_psn & BTH_PSN_MASK);
  563. qp->req.psn = qp->attr.sq_psn;
  564. qp->comp.psn = qp->attr.sq_psn;
  565. pr_debug("qp#%d set req psn = 0x%x\n", qp_num(qp), qp->req.psn);
  566. }
  567. if (mask & IB_QP_PATH_MIG_STATE)
  568. qp->attr.path_mig_state = attr->path_mig_state;
  569. if (mask & IB_QP_DEST_QPN)
  570. qp->attr.dest_qp_num = attr->dest_qp_num;
  571. if (mask & IB_QP_STATE) {
  572. qp->attr.qp_state = attr->qp_state;
  573. switch (attr->qp_state) {
  574. case IB_QPS_RESET:
  575. pr_debug("qp#%d state -> RESET\n", qp_num(qp));
  576. rxe_qp_reset(qp);
  577. break;
  578. case IB_QPS_INIT:
  579. pr_debug("qp#%d state -> INIT\n", qp_num(qp));
  580. qp->req.state = QP_STATE_INIT;
  581. qp->resp.state = QP_STATE_INIT;
  582. break;
  583. case IB_QPS_RTR:
  584. pr_debug("qp#%d state -> RTR\n", qp_num(qp));
  585. qp->resp.state = QP_STATE_READY;
  586. break;
  587. case IB_QPS_RTS:
  588. pr_debug("qp#%d state -> RTS\n", qp_num(qp));
  589. qp->req.state = QP_STATE_READY;
  590. break;
  591. case IB_QPS_SQD:
  592. pr_debug("qp#%d state -> SQD\n", qp_num(qp));
  593. rxe_qp_drain(qp);
  594. break;
  595. case IB_QPS_SQE:
  596. pr_warn("qp#%d state -> SQE !!?\n", qp_num(qp));
  597. /* Not possible from modify_qp. */
  598. break;
  599. case IB_QPS_ERR:
  600. pr_debug("qp#%d state -> ERR\n", qp_num(qp));
  601. rxe_qp_error(qp);
  602. break;
  603. }
  604. }
  605. return 0;
  606. }
  607. /* called by the query qp verb */
  608. int rxe_qp_to_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask)
  609. {
  610. *attr = qp->attr;
  611. attr->rq_psn = qp->resp.psn;
  612. attr->sq_psn = qp->req.psn;
  613. attr->cap.max_send_wr = qp->sq.max_wr;
  614. attr->cap.max_send_sge = qp->sq.max_sge;
  615. attr->cap.max_inline_data = qp->sq.max_inline;
  616. if (!qp->srq) {
  617. attr->cap.max_recv_wr = qp->rq.max_wr;
  618. attr->cap.max_recv_sge = qp->rq.max_sge;
  619. }
  620. rxe_av_to_attr(&qp->pri_av, &attr->ah_attr);
  621. rxe_av_to_attr(&qp->alt_av, &attr->alt_ah_attr);
  622. if (qp->req.state == QP_STATE_DRAIN) {
  623. attr->sq_draining = 1;
  624. /* applications that get this state
  625. * typically spin on it. yield the
  626. * processor
  627. */
  628. cond_resched();
  629. } else {
  630. attr->sq_draining = 0;
  631. }
  632. pr_debug("attr->sq_draining = %d\n", attr->sq_draining);
  633. return 0;
  634. }
  635. /* called by the destroy qp verb */
  636. void rxe_qp_destroy(struct rxe_qp *qp)
  637. {
  638. qp->valid = 0;
  639. qp->qp_timeout_jiffies = 0;
  640. rxe_cleanup_task(&qp->resp.task);
  641. if (qp_type(qp) == IB_QPT_RC) {
  642. del_timer_sync(&qp->retrans_timer);
  643. del_timer_sync(&qp->rnr_nak_timer);
  644. }
  645. rxe_cleanup_task(&qp->req.task);
  646. rxe_cleanup_task(&qp->comp.task);
  647. /* flush out any receive wr's or pending requests */
  648. __rxe_do_task(&qp->req.task);
  649. if (qp->sq.queue) {
  650. __rxe_do_task(&qp->comp.task);
  651. __rxe_do_task(&qp->req.task);
  652. }
  653. }
  654. /* called when the last reference to the qp is dropped */
  655. static void rxe_qp_do_cleanup(struct work_struct *work)
  656. {
  657. struct rxe_qp *qp = container_of(work, typeof(*qp), cleanup_work.work);
  658. rxe_drop_all_mcast_groups(qp);
  659. if (qp->sq.queue)
  660. rxe_queue_cleanup(qp->sq.queue);
  661. if (qp->srq)
  662. rxe_drop_ref(qp->srq);
  663. if (qp->rq.queue)
  664. rxe_queue_cleanup(qp->rq.queue);
  665. if (qp->scq)
  666. rxe_drop_ref(qp->scq);
  667. if (qp->rcq)
  668. rxe_drop_ref(qp->rcq);
  669. if (qp->pd)
  670. rxe_drop_ref(qp->pd);
  671. if (qp->resp.mr) {
  672. rxe_drop_ref(qp->resp.mr);
  673. qp->resp.mr = NULL;
  674. }
  675. if (qp_type(qp) == IB_QPT_RC)
  676. sk_dst_reset(qp->sk->sk);
  677. free_rd_atomic_resources(qp);
  678. kernel_sock_shutdown(qp->sk, SHUT_RDWR);
  679. sock_release(qp->sk);
  680. }
  681. /* called when the last reference to the qp is dropped */
  682. void rxe_qp_cleanup(struct rxe_pool_entry *arg)
  683. {
  684. struct rxe_qp *qp = container_of(arg, typeof(*qp), pelem);
  685. execute_in_process_context(rxe_qp_do_cleanup, &qp->cleanup_work);
  686. }