c2_qp.c 24 KB

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