rxe_qp.c 19 KB

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