c2_qp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  6. * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
  7. *
  8. * This software is available to you under a choice of one of two
  9. * licenses. You may choose to be licensed under the terms of the GNU
  10. * General Public License (GPL) Version 2, available from the file
  11. * COPYING in the main directory of this source tree, or the
  12. * OpenIB.org BSD license below:
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials
  25. * provided with the distribution.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  31. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  32. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  33. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. * SOFTWARE.
  35. *
  36. */
  37. #include <linux/delay.h>
  38. #include <linux/gfp.h>
  39. #include "c2.h"
  40. #include "c2_vq.h"
  41. #include "c2_status.h"
  42. #define C2_MAX_ORD_PER_QP 128
  43. #define C2_MAX_IRD_PER_QP 128
  44. #define C2_HINT_MAKE(q_index, hint_count) (((q_index) << 16) | hint_count)
  45. #define C2_HINT_GET_INDEX(hint) (((hint) & 0x7FFF0000) >> 16)
  46. #define C2_HINT_GET_COUNT(hint) ((hint) & 0x0000FFFF)
  47. #define NO_SUPPORT -1
  48. static const u8 c2_opcode[] = {
  49. [IB_WR_SEND] = C2_WR_TYPE_SEND,
  50. [IB_WR_SEND_WITH_IMM] = NO_SUPPORT,
  51. [IB_WR_RDMA_WRITE] = C2_WR_TYPE_RDMA_WRITE,
  52. [IB_WR_RDMA_WRITE_WITH_IMM] = NO_SUPPORT,
  53. [IB_WR_RDMA_READ] = C2_WR_TYPE_RDMA_READ,
  54. [IB_WR_ATOMIC_CMP_AND_SWP] = NO_SUPPORT,
  55. [IB_WR_ATOMIC_FETCH_AND_ADD] = NO_SUPPORT,
  56. };
  57. static int to_c2_state(enum ib_qp_state ib_state)
  58. {
  59. switch (ib_state) {
  60. case IB_QPS_RESET:
  61. return C2_QP_STATE_IDLE;
  62. case IB_QPS_RTS:
  63. return C2_QP_STATE_RTS;
  64. case IB_QPS_SQD:
  65. return C2_QP_STATE_CLOSING;
  66. case IB_QPS_SQE:
  67. return C2_QP_STATE_CLOSING;
  68. case IB_QPS_ERR:
  69. return C2_QP_STATE_ERROR;
  70. default:
  71. return -1;
  72. }
  73. }
  74. static int to_ib_state(enum c2_qp_state c2_state)
  75. {
  76. switch (c2_state) {
  77. case C2_QP_STATE_IDLE:
  78. return IB_QPS_RESET;
  79. case C2_QP_STATE_CONNECTING:
  80. return IB_QPS_RTR;
  81. case C2_QP_STATE_RTS:
  82. return IB_QPS_RTS;
  83. case C2_QP_STATE_CLOSING:
  84. return IB_QPS_SQD;
  85. case C2_QP_STATE_ERROR:
  86. return IB_QPS_ERR;
  87. case C2_QP_STATE_TERMINATE:
  88. return IB_QPS_SQE;
  89. default:
  90. return -1;
  91. }
  92. }
  93. static const char *to_ib_state_str(int ib_state)
  94. {
  95. static const char *state_str[] = {
  96. "IB_QPS_RESET",
  97. "IB_QPS_INIT",
  98. "IB_QPS_RTR",
  99. "IB_QPS_RTS",
  100. "IB_QPS_SQD",
  101. "IB_QPS_SQE",
  102. "IB_QPS_ERR"
  103. };
  104. if (ib_state < IB_QPS_RESET ||
  105. ib_state > IB_QPS_ERR)
  106. return "<invalid IB QP state>";
  107. ib_state -= IB_QPS_RESET;
  108. return state_str[ib_state];
  109. }
  110. void c2_set_qp_state(struct c2_qp *qp, int c2_state)
  111. {
  112. int new_state = to_ib_state(c2_state);
  113. pr_debug("%s: qp[%p] state modify %s --> %s\n",
  114. __func__,
  115. qp,
  116. to_ib_state_str(qp->state),
  117. to_ib_state_str(new_state));
  118. qp->state = new_state;
  119. }
  120. #define C2_QP_NO_ATTR_CHANGE 0xFFFFFFFF
  121. int c2_qp_modify(struct c2_dev *c2dev, struct c2_qp *qp,
  122. struct ib_qp_attr *attr, int attr_mask)
  123. {
  124. struct c2wr_qp_modify_req wr;
  125. struct c2wr_qp_modify_rep *reply;
  126. struct c2_vq_req *vq_req;
  127. unsigned long flags;
  128. u8 next_state;
  129. int err;
  130. pr_debug("%s:%d qp=%p, %s --> %s\n",
  131. __func__, __LINE__,
  132. qp,
  133. to_ib_state_str(qp->state),
  134. to_ib_state_str(attr->qp_state));
  135. vq_req = vq_req_alloc(c2dev);
  136. if (!vq_req)
  137. return -ENOMEM;
  138. c2_wr_set_id(&wr, CCWR_QP_MODIFY);
  139. wr.hdr.context = (unsigned long) vq_req;
  140. wr.rnic_handle = c2dev->adapter_handle;
  141. wr.qp_handle = qp->adapter_handle;
  142. wr.ord = cpu_to_be32(C2_QP_NO_ATTR_CHANGE);
  143. wr.ird = cpu_to_be32(C2_QP_NO_ATTR_CHANGE);
  144. wr.sq_depth = cpu_to_be32(C2_QP_NO_ATTR_CHANGE);
  145. wr.rq_depth = cpu_to_be32(C2_QP_NO_ATTR_CHANGE);
  146. if (attr_mask & IB_QP_STATE) {
  147. /* Ensure the state is valid */
  148. if (attr->qp_state < 0 || attr->qp_state > IB_QPS_ERR) {
  149. err = -EINVAL;
  150. goto bail0;
  151. }
  152. wr.next_qp_state = cpu_to_be32(to_c2_state(attr->qp_state));
  153. if (attr->qp_state == IB_QPS_ERR) {
  154. spin_lock_irqsave(&qp->lock, flags);
  155. if (qp->cm_id && qp->state == IB_QPS_RTS) {
  156. pr_debug("Generating CLOSE event for QP-->ERR, "
  157. "qp=%p, cm_id=%p\n",qp,qp->cm_id);
  158. /* Generate an CLOSE event */
  159. vq_req->cm_id = qp->cm_id;
  160. vq_req->event = IW_CM_EVENT_CLOSE;
  161. }
  162. spin_unlock_irqrestore(&qp->lock, flags);
  163. }
  164. next_state = attr->qp_state;
  165. } else if (attr_mask & IB_QP_CUR_STATE) {
  166. if (attr->cur_qp_state != IB_QPS_RTR &&
  167. attr->cur_qp_state != IB_QPS_RTS &&
  168. attr->cur_qp_state != IB_QPS_SQD &&
  169. attr->cur_qp_state != IB_QPS_SQE) {
  170. err = -EINVAL;
  171. goto bail0;
  172. } else
  173. wr.next_qp_state =
  174. cpu_to_be32(to_c2_state(attr->cur_qp_state));
  175. next_state = attr->cur_qp_state;
  176. } else {
  177. err = 0;
  178. goto bail0;
  179. }
  180. /* reference the request struct */
  181. vq_req_get(c2dev, vq_req);
  182. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  183. if (err) {
  184. vq_req_put(c2dev, vq_req);
  185. goto bail0;
  186. }
  187. err = vq_wait_for_reply(c2dev, vq_req);
  188. if (err)
  189. goto bail0;
  190. reply = (struct c2wr_qp_modify_rep *) (unsigned long) vq_req->reply_msg;
  191. if (!reply) {
  192. err = -ENOMEM;
  193. goto bail0;
  194. }
  195. err = c2_errno(reply);
  196. if (!err)
  197. qp->state = next_state;
  198. #ifdef DEBUG
  199. else
  200. pr_debug("%s: c2_errno=%d\n", __func__, err);
  201. #endif
  202. /*
  203. * If we're going to error and generating the event here, then
  204. * we need to remove the reference because there will be no
  205. * close event generated by the adapter
  206. */
  207. spin_lock_irqsave(&qp->lock, flags);
  208. if (vq_req->event==IW_CM_EVENT_CLOSE && qp->cm_id) {
  209. qp->cm_id->rem_ref(qp->cm_id);
  210. qp->cm_id = NULL;
  211. }
  212. spin_unlock_irqrestore(&qp->lock, flags);
  213. vq_repbuf_free(c2dev, reply);
  214. bail0:
  215. vq_req_free(c2dev, vq_req);
  216. pr_debug("%s:%d qp=%p, cur_state=%s\n",
  217. __func__, __LINE__,
  218. qp,
  219. to_ib_state_str(qp->state));
  220. return err;
  221. }
  222. int c2_qp_set_read_limits(struct c2_dev *c2dev, struct c2_qp *qp,
  223. int ord, int ird)
  224. {
  225. struct c2wr_qp_modify_req wr;
  226. struct c2wr_qp_modify_rep *reply;
  227. struct c2_vq_req *vq_req;
  228. int err;
  229. vq_req = vq_req_alloc(c2dev);
  230. if (!vq_req)
  231. return -ENOMEM;
  232. c2_wr_set_id(&wr, CCWR_QP_MODIFY);
  233. wr.hdr.context = (unsigned long) vq_req;
  234. wr.rnic_handle = c2dev->adapter_handle;
  235. wr.qp_handle = qp->adapter_handle;
  236. wr.ord = cpu_to_be32(ord);
  237. wr.ird = cpu_to_be32(ird);
  238. wr.sq_depth = cpu_to_be32(C2_QP_NO_ATTR_CHANGE);
  239. wr.rq_depth = cpu_to_be32(C2_QP_NO_ATTR_CHANGE);
  240. wr.next_qp_state = cpu_to_be32(C2_QP_NO_ATTR_CHANGE);
  241. /* reference the request struct */
  242. vq_req_get(c2dev, vq_req);
  243. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  244. if (err) {
  245. vq_req_put(c2dev, vq_req);
  246. goto bail0;
  247. }
  248. err = vq_wait_for_reply(c2dev, vq_req);
  249. if (err)
  250. goto bail0;
  251. reply = (struct c2wr_qp_modify_rep *) (unsigned long)
  252. vq_req->reply_msg;
  253. if (!reply) {
  254. err = -ENOMEM;
  255. goto bail0;
  256. }
  257. err = c2_errno(reply);
  258. vq_repbuf_free(c2dev, reply);
  259. bail0:
  260. vq_req_free(c2dev, vq_req);
  261. return err;
  262. }
  263. static int destroy_qp(struct c2_dev *c2dev, struct c2_qp *qp)
  264. {
  265. struct c2_vq_req *vq_req;
  266. struct c2wr_qp_destroy_req wr;
  267. struct c2wr_qp_destroy_rep *reply;
  268. unsigned long flags;
  269. int err;
  270. /*
  271. * Allocate a verb request message
  272. */
  273. vq_req = vq_req_alloc(c2dev);
  274. if (!vq_req) {
  275. return -ENOMEM;
  276. }
  277. /*
  278. * Initialize the WR
  279. */
  280. c2_wr_set_id(&wr, CCWR_QP_DESTROY);
  281. wr.hdr.context = (unsigned long) vq_req;
  282. wr.rnic_handle = c2dev->adapter_handle;
  283. wr.qp_handle = qp->adapter_handle;
  284. /*
  285. * reference the request struct. dereferenced in the int handler.
  286. */
  287. vq_req_get(c2dev, vq_req);
  288. spin_lock_irqsave(&qp->lock, flags);
  289. if (qp->cm_id && qp->state == IB_QPS_RTS) {
  290. pr_debug("destroy_qp: generating CLOSE event for QP-->ERR, "
  291. "qp=%p, cm_id=%p\n",qp,qp->cm_id);
  292. /* Generate an CLOSE event */
  293. vq_req->qp = qp;
  294. vq_req->cm_id = qp->cm_id;
  295. vq_req->event = IW_CM_EVENT_CLOSE;
  296. }
  297. spin_unlock_irqrestore(&qp->lock, flags);
  298. /*
  299. * Send WR to adapter
  300. */
  301. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  302. if (err) {
  303. vq_req_put(c2dev, vq_req);
  304. goto bail0;
  305. }
  306. /*
  307. * Wait for reply from adapter
  308. */
  309. err = vq_wait_for_reply(c2dev, vq_req);
  310. if (err) {
  311. goto bail0;
  312. }
  313. /*
  314. * Process reply
  315. */
  316. reply = (struct c2wr_qp_destroy_rep *) (unsigned long) (vq_req->reply_msg);
  317. if (!reply) {
  318. err = -ENOMEM;
  319. goto bail0;
  320. }
  321. spin_lock_irqsave(&qp->lock, flags);
  322. if (qp->cm_id) {
  323. qp->cm_id->rem_ref(qp->cm_id);
  324. qp->cm_id = NULL;
  325. }
  326. spin_unlock_irqrestore(&qp->lock, flags);
  327. vq_repbuf_free(c2dev, reply);
  328. bail0:
  329. vq_req_free(c2dev, vq_req);
  330. return err;
  331. }
  332. static int c2_alloc_qpn(struct c2_dev *c2dev, struct c2_qp *qp)
  333. {
  334. int ret;
  335. do {
  336. spin_lock_irq(&c2dev->qp_table.lock);
  337. ret = idr_get_new_above(&c2dev->qp_table.idr, qp,
  338. c2dev->qp_table.last++, &qp->qpn);
  339. spin_unlock_irq(&c2dev->qp_table.lock);
  340. } while ((ret == -EAGAIN) &&
  341. idr_pre_get(&c2dev->qp_table.idr, GFP_KERNEL));
  342. return ret;
  343. }
  344. static void c2_free_qpn(struct c2_dev *c2dev, int qpn)
  345. {
  346. spin_lock_irq(&c2dev->qp_table.lock);
  347. idr_remove(&c2dev->qp_table.idr, qpn);
  348. spin_unlock_irq(&c2dev->qp_table.lock);
  349. }
  350. struct c2_qp *c2_find_qpn(struct c2_dev *c2dev, int qpn)
  351. {
  352. unsigned long flags;
  353. struct c2_qp *qp;
  354. spin_lock_irqsave(&c2dev->qp_table.lock, flags);
  355. qp = idr_find(&c2dev->qp_table.idr, qpn);
  356. spin_unlock_irqrestore(&c2dev->qp_table.lock, flags);
  357. return qp;
  358. }
  359. int c2_alloc_qp(struct c2_dev *c2dev,
  360. struct c2_pd *pd,
  361. struct ib_qp_init_attr *qp_attrs, struct c2_qp *qp)
  362. {
  363. struct c2wr_qp_create_req wr;
  364. struct c2wr_qp_create_rep *reply;
  365. struct c2_vq_req *vq_req;
  366. struct c2_cq *send_cq = to_c2cq(qp_attrs->send_cq);
  367. struct c2_cq *recv_cq = to_c2cq(qp_attrs->recv_cq);
  368. unsigned long peer_pa;
  369. u32 q_size, msg_size, mmap_size;
  370. void __iomem *mmap;
  371. int err;
  372. err = c2_alloc_qpn(c2dev, qp);
  373. if (err)
  374. return err;
  375. qp->ibqp.qp_num = qp->qpn;
  376. qp->ibqp.qp_type = IB_QPT_RC;
  377. /* Allocate the SQ and RQ shared pointers */
  378. qp->sq_mq.shared = c2_alloc_mqsp(c2dev, c2dev->kern_mqsp_pool,
  379. &qp->sq_mq.shared_dma, GFP_KERNEL);
  380. if (!qp->sq_mq.shared) {
  381. err = -ENOMEM;
  382. goto bail0;
  383. }
  384. qp->rq_mq.shared = c2_alloc_mqsp(c2dev, c2dev->kern_mqsp_pool,
  385. &qp->rq_mq.shared_dma, GFP_KERNEL);
  386. if (!qp->rq_mq.shared) {
  387. err = -ENOMEM;
  388. goto bail1;
  389. }
  390. /* Allocate the verbs request */
  391. vq_req = vq_req_alloc(c2dev);
  392. if (vq_req == NULL) {
  393. err = -ENOMEM;
  394. goto bail2;
  395. }
  396. /* Initialize the work request */
  397. memset(&wr, 0, sizeof(wr));
  398. c2_wr_set_id(&wr, CCWR_QP_CREATE);
  399. wr.hdr.context = (unsigned long) vq_req;
  400. wr.rnic_handle = c2dev->adapter_handle;
  401. wr.sq_cq_handle = send_cq->adapter_handle;
  402. wr.rq_cq_handle = recv_cq->adapter_handle;
  403. wr.sq_depth = cpu_to_be32(qp_attrs->cap.max_send_wr + 1);
  404. wr.rq_depth = cpu_to_be32(qp_attrs->cap.max_recv_wr + 1);
  405. wr.srq_handle = 0;
  406. wr.flags = cpu_to_be32(QP_RDMA_READ | QP_RDMA_WRITE | QP_MW_BIND |
  407. QP_ZERO_STAG | QP_RDMA_READ_RESPONSE);
  408. wr.send_sgl_depth = cpu_to_be32(qp_attrs->cap.max_send_sge);
  409. wr.recv_sgl_depth = cpu_to_be32(qp_attrs->cap.max_recv_sge);
  410. wr.rdma_write_sgl_depth = cpu_to_be32(qp_attrs->cap.max_send_sge);
  411. wr.shared_sq_ht = cpu_to_be64(qp->sq_mq.shared_dma);
  412. wr.shared_rq_ht = cpu_to_be64(qp->rq_mq.shared_dma);
  413. wr.ord = cpu_to_be32(C2_MAX_ORD_PER_QP);
  414. wr.ird = cpu_to_be32(C2_MAX_IRD_PER_QP);
  415. wr.pd_id = pd->pd_id;
  416. wr.user_context = (unsigned long) qp;
  417. vq_req_get(c2dev, vq_req);
  418. /* Send the WR to the adapter */
  419. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  420. if (err) {
  421. vq_req_put(c2dev, vq_req);
  422. goto bail3;
  423. }
  424. /* Wait for the verb reply */
  425. err = vq_wait_for_reply(c2dev, vq_req);
  426. if (err) {
  427. goto bail3;
  428. }
  429. /* Process the reply */
  430. reply = (struct c2wr_qp_create_rep *) (unsigned long) (vq_req->reply_msg);
  431. if (!reply) {
  432. err = -ENOMEM;
  433. goto bail3;
  434. }
  435. if ((err = c2_wr_get_result(reply)) != 0) {
  436. goto bail4;
  437. }
  438. /* Fill in the kernel QP struct */
  439. atomic_set(&qp->refcount, 1);
  440. qp->adapter_handle = reply->qp_handle;
  441. qp->state = IB_QPS_RESET;
  442. qp->send_sgl_depth = qp_attrs->cap.max_send_sge;
  443. qp->rdma_write_sgl_depth = qp_attrs->cap.max_send_sge;
  444. qp->recv_sgl_depth = qp_attrs->cap.max_recv_sge;
  445. init_waitqueue_head(&qp->wait);
  446. /* Initialize the SQ MQ */
  447. q_size = be32_to_cpu(reply->sq_depth);
  448. msg_size = be32_to_cpu(reply->sq_msg_size);
  449. peer_pa = c2dev->pa + be32_to_cpu(reply->sq_mq_start);
  450. mmap_size = PAGE_ALIGN(sizeof(struct c2_mq_shared) + msg_size * q_size);
  451. mmap = ioremap_nocache(peer_pa, mmap_size);
  452. if (!mmap) {
  453. err = -ENOMEM;
  454. goto bail5;
  455. }
  456. c2_mq_req_init(&qp->sq_mq,
  457. be32_to_cpu(reply->sq_mq_index),
  458. q_size,
  459. msg_size,
  460. mmap + sizeof(struct c2_mq_shared), /* pool start */
  461. mmap, /* peer */
  462. C2_MQ_ADAPTER_TARGET);
  463. /* Initialize the RQ mq */
  464. q_size = be32_to_cpu(reply->rq_depth);
  465. msg_size = be32_to_cpu(reply->rq_msg_size);
  466. peer_pa = c2dev->pa + be32_to_cpu(reply->rq_mq_start);
  467. mmap_size = PAGE_ALIGN(sizeof(struct c2_mq_shared) + msg_size * q_size);
  468. mmap = ioremap_nocache(peer_pa, mmap_size);
  469. if (!mmap) {
  470. err = -ENOMEM;
  471. goto bail6;
  472. }
  473. c2_mq_req_init(&qp->rq_mq,
  474. be32_to_cpu(reply->rq_mq_index),
  475. q_size,
  476. msg_size,
  477. mmap + sizeof(struct c2_mq_shared), /* pool start */
  478. mmap, /* peer */
  479. C2_MQ_ADAPTER_TARGET);
  480. vq_repbuf_free(c2dev, reply);
  481. vq_req_free(c2dev, vq_req);
  482. return 0;
  483. bail6:
  484. iounmap(qp->sq_mq.peer);
  485. bail5:
  486. destroy_qp(c2dev, qp);
  487. bail4:
  488. vq_repbuf_free(c2dev, reply);
  489. bail3:
  490. vq_req_free(c2dev, vq_req);
  491. bail2:
  492. c2_free_mqsp(qp->rq_mq.shared);
  493. bail1:
  494. c2_free_mqsp(qp->sq_mq.shared);
  495. bail0:
  496. c2_free_qpn(c2dev, qp->qpn);
  497. return err;
  498. }
  499. static inline void c2_lock_cqs(struct c2_cq *send_cq, struct c2_cq *recv_cq)
  500. {
  501. if (send_cq == recv_cq)
  502. spin_lock_irq(&send_cq->lock);
  503. else if (send_cq > recv_cq) {
  504. spin_lock_irq(&send_cq->lock);
  505. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  506. } else {
  507. spin_lock_irq(&recv_cq->lock);
  508. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  509. }
  510. }
  511. static inline void c2_unlock_cqs(struct c2_cq *send_cq, struct c2_cq *recv_cq)
  512. {
  513. if (send_cq == recv_cq)
  514. spin_unlock_irq(&send_cq->lock);
  515. else if (send_cq > recv_cq) {
  516. spin_unlock(&recv_cq->lock);
  517. spin_unlock_irq(&send_cq->lock);
  518. } else {
  519. spin_unlock(&send_cq->lock);
  520. spin_unlock_irq(&recv_cq->lock);
  521. }
  522. }
  523. void c2_free_qp(struct c2_dev *c2dev, struct c2_qp *qp)
  524. {
  525. struct c2_cq *send_cq;
  526. struct c2_cq *recv_cq;
  527. send_cq = to_c2cq(qp->ibqp.send_cq);
  528. recv_cq = to_c2cq(qp->ibqp.recv_cq);
  529. /*
  530. * Lock CQs here, so that CQ polling code can do QP lookup
  531. * without taking a lock.
  532. */
  533. c2_lock_cqs(send_cq, recv_cq);
  534. c2_free_qpn(c2dev, qp->qpn);
  535. c2_unlock_cqs(send_cq, recv_cq);
  536. /*
  537. * Destroy qp in the rnic...
  538. */
  539. destroy_qp(c2dev, qp);
  540. /*
  541. * Mark any unreaped CQEs as null and void.
  542. */
  543. c2_cq_clean(c2dev, qp, send_cq->cqn);
  544. if (send_cq != recv_cq)
  545. c2_cq_clean(c2dev, qp, recv_cq->cqn);
  546. /*
  547. * Unmap the MQs and return the shared pointers
  548. * to the message pool.
  549. */
  550. iounmap(qp->sq_mq.peer);
  551. iounmap(qp->rq_mq.peer);
  552. c2_free_mqsp(qp->sq_mq.shared);
  553. c2_free_mqsp(qp->rq_mq.shared);
  554. atomic_dec(&qp->refcount);
  555. wait_event(qp->wait, !atomic_read(&qp->refcount));
  556. }
  557. /*
  558. * Function: move_sgl
  559. *
  560. * Description:
  561. * Move an SGL from the user's work request struct into a CCIL Work Request
  562. * message, swapping to WR byte order and ensure the total length doesn't
  563. * overflow.
  564. *
  565. * IN:
  566. * dst - ptr to CCIL Work Request message SGL memory.
  567. * src - ptr to the consumers SGL memory.
  568. *
  569. * OUT: none
  570. *
  571. * Return:
  572. * CCIL status codes.
  573. */
  574. static int
  575. move_sgl(struct c2_data_addr * dst, struct ib_sge *src, int count, u32 * p_len,
  576. u8 * actual_count)
  577. {
  578. u32 tot = 0; /* running total */
  579. u8 acount = 0; /* running total non-0 len sge's */
  580. while (count > 0) {
  581. /*
  582. * If the addition of this SGE causes the
  583. * total SGL length to exceed 2^32-1, then
  584. * fail-n-bail.
  585. *
  586. * If the current total plus the next element length
  587. * wraps, then it will go negative and be less than the
  588. * current total...
  589. */
  590. if ((tot + src->length) < tot) {
  591. return -EINVAL;
  592. }
  593. /*
  594. * Bug: 1456 (as well as 1498 & 1643)
  595. * Skip over any sge's supplied with len=0
  596. */
  597. if (src->length) {
  598. tot += src->length;
  599. dst->stag = cpu_to_be32(src->lkey);
  600. dst->to = cpu_to_be64(src->addr);
  601. dst->length = cpu_to_be32(src->length);
  602. dst++;
  603. acount++;
  604. }
  605. src++;
  606. count--;
  607. }
  608. if (acount == 0) {
  609. /*
  610. * Bug: 1476 (as well as 1498, 1456 and 1643)
  611. * Setup the SGL in the WR to make it easier for the RNIC.
  612. * This way, the FW doesn't have to deal with special cases.
  613. * Setting length=0 should be sufficient.
  614. */
  615. dst->stag = 0;
  616. dst->to = 0;
  617. dst->length = 0;
  618. }
  619. *p_len = tot;
  620. *actual_count = acount;
  621. return 0;
  622. }
  623. /*
  624. * Function: c2_activity (private function)
  625. *
  626. * Description:
  627. * Post an mq index to the host->adapter activity fifo.
  628. *
  629. * IN:
  630. * c2dev - ptr to c2dev structure
  631. * mq_index - mq index to post
  632. * shared - value most recently written to shared
  633. *
  634. * OUT:
  635. *
  636. * Return:
  637. * none
  638. */
  639. static inline void c2_activity(struct c2_dev *c2dev, u32 mq_index, u16 shared)
  640. {
  641. /*
  642. * First read the register to see if the FIFO is full, and if so,
  643. * spin until it's not. This isn't perfect -- there is no
  644. * synchronization among the clients of the register, but in
  645. * practice it prevents multiple CPU from hammering the bus
  646. * with PCI RETRY. Note that when this does happen, the card
  647. * cannot get on the bus and the card and system hang in a
  648. * deadlock -- thus the need for this code. [TOT]
  649. */
  650. while (readl(c2dev->regs + PCI_BAR0_ADAPTER_HINT) & 0x80000000)
  651. udelay(10);
  652. __raw_writel(C2_HINT_MAKE(mq_index, shared),
  653. c2dev->regs + PCI_BAR0_ADAPTER_HINT);
  654. }
  655. /*
  656. * Function: qp_wr_post
  657. *
  658. * Description:
  659. * This in-line function allocates a MQ msg, then moves the host-copy of
  660. * the completed WR into msg. Then it posts the message.
  661. *
  662. * IN:
  663. * q - ptr to user MQ.
  664. * wr - ptr to host-copy of the WR.
  665. * qp - ptr to user qp
  666. * size - Number of bytes to post. Assumed to be divisible by 4.
  667. *
  668. * OUT: none
  669. *
  670. * Return:
  671. * CCIL status codes.
  672. */
  673. static int qp_wr_post(struct c2_mq *q, union c2wr * wr, struct c2_qp *qp, u32 size)
  674. {
  675. union c2wr *msg;
  676. msg = c2_mq_alloc(q);
  677. if (msg == NULL) {
  678. return -EINVAL;
  679. }
  680. #ifdef CCMSGMAGIC
  681. ((c2wr_hdr_t *) wr)->magic = cpu_to_be32(CCWR_MAGIC);
  682. #endif
  683. /*
  684. * Since all header fields in the WR are the same as the
  685. * CQE, set the following so the adapter need not.
  686. */
  687. c2_wr_set_result(wr, CCERR_PENDING);
  688. /*
  689. * Copy the wr down to the adapter
  690. */
  691. memcpy((void *) msg, (void *) wr, size);
  692. c2_mq_produce(q);
  693. return 0;
  694. }
  695. int c2_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
  696. struct ib_send_wr **bad_wr)
  697. {
  698. struct c2_dev *c2dev = to_c2dev(ibqp->device);
  699. struct c2_qp *qp = to_c2qp(ibqp);
  700. union c2wr wr;
  701. unsigned long lock_flags;
  702. int err = 0;
  703. u32 flags;
  704. u32 tot_len;
  705. u8 actual_sge_count;
  706. u32 msg_size;
  707. if (qp->state > IB_QPS_RTS) {
  708. err = -EINVAL;
  709. goto out;
  710. }
  711. while (ib_wr) {
  712. flags = 0;
  713. wr.sqwr.sq_hdr.user_hdr.hdr.context = ib_wr->wr_id;
  714. if (ib_wr->send_flags & IB_SEND_SIGNALED) {
  715. flags |= SQ_SIGNALED;
  716. }
  717. switch (ib_wr->opcode) {
  718. case IB_WR_SEND:
  719. case IB_WR_SEND_WITH_INV:
  720. if (ib_wr->opcode == IB_WR_SEND) {
  721. if (ib_wr->send_flags & IB_SEND_SOLICITED)
  722. c2_wr_set_id(&wr, C2_WR_TYPE_SEND_SE);
  723. else
  724. c2_wr_set_id(&wr, C2_WR_TYPE_SEND);
  725. wr.sqwr.send.remote_stag = 0;
  726. } else {
  727. if (ib_wr->send_flags & IB_SEND_SOLICITED)
  728. c2_wr_set_id(&wr, C2_WR_TYPE_SEND_SE_INV);
  729. else
  730. c2_wr_set_id(&wr, C2_WR_TYPE_SEND_INV);
  731. wr.sqwr.send.remote_stag =
  732. cpu_to_be32(ib_wr->ex.invalidate_rkey);
  733. }
  734. msg_size = sizeof(struct c2wr_send_req) +
  735. sizeof(struct c2_data_addr) * ib_wr->num_sge;
  736. if (ib_wr->num_sge > qp->send_sgl_depth) {
  737. err = -EINVAL;
  738. break;
  739. }
  740. if (ib_wr->send_flags & IB_SEND_FENCE) {
  741. flags |= SQ_READ_FENCE;
  742. }
  743. err = move_sgl((struct c2_data_addr *) & (wr.sqwr.send.data),
  744. ib_wr->sg_list,
  745. ib_wr->num_sge,
  746. &tot_len, &actual_sge_count);
  747. wr.sqwr.send.sge_len = cpu_to_be32(tot_len);
  748. c2_wr_set_sge_count(&wr, actual_sge_count);
  749. break;
  750. case IB_WR_RDMA_WRITE:
  751. c2_wr_set_id(&wr, C2_WR_TYPE_RDMA_WRITE);
  752. msg_size = sizeof(struct c2wr_rdma_write_req) +
  753. (sizeof(struct c2_data_addr) * ib_wr->num_sge);
  754. if (ib_wr->num_sge > qp->rdma_write_sgl_depth) {
  755. err = -EINVAL;
  756. break;
  757. }
  758. if (ib_wr->send_flags & IB_SEND_FENCE) {
  759. flags |= SQ_READ_FENCE;
  760. }
  761. wr.sqwr.rdma_write.remote_stag =
  762. cpu_to_be32(ib_wr->wr.rdma.rkey);
  763. wr.sqwr.rdma_write.remote_to =
  764. cpu_to_be64(ib_wr->wr.rdma.remote_addr);
  765. err = move_sgl((struct c2_data_addr *)
  766. & (wr.sqwr.rdma_write.data),
  767. ib_wr->sg_list,
  768. ib_wr->num_sge,
  769. &tot_len, &actual_sge_count);
  770. wr.sqwr.rdma_write.sge_len = cpu_to_be32(tot_len);
  771. c2_wr_set_sge_count(&wr, actual_sge_count);
  772. break;
  773. case IB_WR_RDMA_READ:
  774. c2_wr_set_id(&wr, C2_WR_TYPE_RDMA_READ);
  775. msg_size = sizeof(struct c2wr_rdma_read_req);
  776. /* IWarp only suppots 1 sge for RDMA reads */
  777. if (ib_wr->num_sge > 1) {
  778. err = -EINVAL;
  779. break;
  780. }
  781. /*
  782. * Move the local and remote stag/to/len into the WR.
  783. */
  784. wr.sqwr.rdma_read.local_stag =
  785. cpu_to_be32(ib_wr->sg_list->lkey);
  786. wr.sqwr.rdma_read.local_to =
  787. cpu_to_be64(ib_wr->sg_list->addr);
  788. wr.sqwr.rdma_read.remote_stag =
  789. cpu_to_be32(ib_wr->wr.rdma.rkey);
  790. wr.sqwr.rdma_read.remote_to =
  791. cpu_to_be64(ib_wr->wr.rdma.remote_addr);
  792. wr.sqwr.rdma_read.length =
  793. cpu_to_be32(ib_wr->sg_list->length);
  794. break;
  795. default:
  796. /* error */
  797. msg_size = 0;
  798. err = -EINVAL;
  799. break;
  800. }
  801. /*
  802. * If we had an error on the last wr build, then
  803. * break out. Possible errors include bogus WR
  804. * type, and a bogus SGL length...
  805. */
  806. if (err) {
  807. break;
  808. }
  809. /*
  810. * Store flags
  811. */
  812. c2_wr_set_flags(&wr, flags);
  813. /*
  814. * Post the puppy!
  815. */
  816. spin_lock_irqsave(&qp->lock, lock_flags);
  817. err = qp_wr_post(&qp->sq_mq, &wr, qp, msg_size);
  818. if (err) {
  819. spin_unlock_irqrestore(&qp->lock, lock_flags);
  820. break;
  821. }
  822. /*
  823. * Enqueue mq index to activity FIFO.
  824. */
  825. c2_activity(c2dev, qp->sq_mq.index, qp->sq_mq.hint_count);
  826. spin_unlock_irqrestore(&qp->lock, lock_flags);
  827. ib_wr = ib_wr->next;
  828. }
  829. out:
  830. if (err)
  831. *bad_wr = ib_wr;
  832. return err;
  833. }
  834. int c2_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
  835. struct ib_recv_wr **bad_wr)
  836. {
  837. struct c2_dev *c2dev = to_c2dev(ibqp->device);
  838. struct c2_qp *qp = to_c2qp(ibqp);
  839. union c2wr wr;
  840. unsigned long lock_flags;
  841. int err = 0;
  842. if (qp->state > IB_QPS_RTS) {
  843. err = -EINVAL;
  844. goto out;
  845. }
  846. /*
  847. * Try and post each work request
  848. */
  849. while (ib_wr) {
  850. u32 tot_len;
  851. u8 actual_sge_count;
  852. if (ib_wr->num_sge > qp->recv_sgl_depth) {
  853. err = -EINVAL;
  854. break;
  855. }
  856. /*
  857. * Create local host-copy of the WR
  858. */
  859. wr.rqwr.rq_hdr.user_hdr.hdr.context = ib_wr->wr_id;
  860. c2_wr_set_id(&wr, CCWR_RECV);
  861. c2_wr_set_flags(&wr, 0);
  862. /* sge_count is limited to eight bits. */
  863. BUG_ON(ib_wr->num_sge >= 256);
  864. err = move_sgl((struct c2_data_addr *) & (wr.rqwr.data),
  865. ib_wr->sg_list,
  866. ib_wr->num_sge, &tot_len, &actual_sge_count);
  867. c2_wr_set_sge_count(&wr, actual_sge_count);
  868. /*
  869. * If we had an error on the last wr build, then
  870. * break out. Possible errors include bogus WR
  871. * type, and a bogus SGL length...
  872. */
  873. if (err) {
  874. break;
  875. }
  876. spin_lock_irqsave(&qp->lock, lock_flags);
  877. err = qp_wr_post(&qp->rq_mq, &wr, qp, qp->rq_mq.msg_size);
  878. if (err) {
  879. spin_unlock_irqrestore(&qp->lock, lock_flags);
  880. break;
  881. }
  882. /*
  883. * Enqueue mq index to activity FIFO
  884. */
  885. c2_activity(c2dev, qp->rq_mq.index, qp->rq_mq.hint_count);
  886. spin_unlock_irqrestore(&qp->lock, lock_flags);
  887. ib_wr = ib_wr->next;
  888. }
  889. out:
  890. if (err)
  891. *bad_wr = ib_wr;
  892. return err;
  893. }
  894. void __devinit c2_init_qp_table(struct c2_dev *c2dev)
  895. {
  896. spin_lock_init(&c2dev->qp_table.lock);
  897. idr_init(&c2dev->qp_table.idr);
  898. }
  899. void __devexit c2_cleanup_qp_table(struct c2_dev *c2dev)
  900. {
  901. idr_destroy(&c2dev->qp_table.idr);
  902. }