rxe_qp.c 20 KB

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