c2_qp.c 23 KB

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