c2_qp.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. static inline void c2_lock_cqs(struct c2_cq *send_cq, struct c2_cq *recv_cq)
  495. {
  496. if (send_cq == recv_cq)
  497. spin_lock_irq(&send_cq->lock);
  498. else if (send_cq > recv_cq) {
  499. spin_lock_irq(&send_cq->lock);
  500. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  501. } else {
  502. spin_lock_irq(&recv_cq->lock);
  503. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  504. }
  505. }
  506. static inline void c2_unlock_cqs(struct c2_cq *send_cq, struct c2_cq *recv_cq)
  507. {
  508. if (send_cq == recv_cq)
  509. spin_unlock_irq(&send_cq->lock);
  510. else if (send_cq > recv_cq) {
  511. spin_unlock(&recv_cq->lock);
  512. spin_unlock_irq(&send_cq->lock);
  513. } else {
  514. spin_unlock(&send_cq->lock);
  515. spin_unlock_irq(&recv_cq->lock);
  516. }
  517. }
  518. void c2_free_qp(struct c2_dev *c2dev, struct c2_qp *qp)
  519. {
  520. struct c2_cq *send_cq;
  521. struct c2_cq *recv_cq;
  522. send_cq = to_c2cq(qp->ibqp.send_cq);
  523. recv_cq = to_c2cq(qp->ibqp.recv_cq);
  524. /*
  525. * Lock CQs here, so that CQ polling code can do QP lookup
  526. * without taking a lock.
  527. */
  528. c2_lock_cqs(send_cq, recv_cq);
  529. c2_free_qpn(c2dev, qp->qpn);
  530. c2_unlock_cqs(send_cq, recv_cq);
  531. /*
  532. * Destory qp in the rnic...
  533. */
  534. destroy_qp(c2dev, qp);
  535. /*
  536. * Mark any unreaped CQEs as null and void.
  537. */
  538. c2_cq_clean(c2dev, qp, send_cq->cqn);
  539. if (send_cq != recv_cq)
  540. c2_cq_clean(c2dev, qp, recv_cq->cqn);
  541. /*
  542. * Unmap the MQs and return the shared pointers
  543. * to the message pool.
  544. */
  545. iounmap(qp->sq_mq.peer);
  546. iounmap(qp->rq_mq.peer);
  547. c2_free_mqsp(qp->sq_mq.shared);
  548. c2_free_mqsp(qp->rq_mq.shared);
  549. atomic_dec(&qp->refcount);
  550. wait_event(qp->wait, !atomic_read(&qp->refcount));
  551. }
  552. /*
  553. * Function: move_sgl
  554. *
  555. * Description:
  556. * Move an SGL from the user's work request struct into a CCIL Work Request
  557. * message, swapping to WR byte order and ensure the total length doesn't
  558. * overflow.
  559. *
  560. * IN:
  561. * dst - ptr to CCIL Work Request message SGL memory.
  562. * src - ptr to the consumers SGL memory.
  563. *
  564. * OUT: none
  565. *
  566. * Return:
  567. * CCIL status codes.
  568. */
  569. static int
  570. move_sgl(struct c2_data_addr * dst, struct ib_sge *src, int count, u32 * p_len,
  571. u8 * actual_count)
  572. {
  573. u32 tot = 0; /* running total */
  574. u8 acount = 0; /* running total non-0 len sge's */
  575. while (count > 0) {
  576. /*
  577. * If the addition of this SGE causes the
  578. * total SGL length to exceed 2^32-1, then
  579. * fail-n-bail.
  580. *
  581. * If the current total plus the next element length
  582. * wraps, then it will go negative and be less than the
  583. * current total...
  584. */
  585. if ((tot + src->length) < tot) {
  586. return -EINVAL;
  587. }
  588. /*
  589. * Bug: 1456 (as well as 1498 & 1643)
  590. * Skip over any sge's supplied with len=0
  591. */
  592. if (src->length) {
  593. tot += src->length;
  594. dst->stag = cpu_to_be32(src->lkey);
  595. dst->to = cpu_to_be64(src->addr);
  596. dst->length = cpu_to_be32(src->length);
  597. dst++;
  598. acount++;
  599. }
  600. src++;
  601. count--;
  602. }
  603. if (acount == 0) {
  604. /*
  605. * Bug: 1476 (as well as 1498, 1456 and 1643)
  606. * Setup the SGL in the WR to make it easier for the RNIC.
  607. * This way, the FW doesn't have to deal with special cases.
  608. * Setting length=0 should be sufficient.
  609. */
  610. dst->stag = 0;
  611. dst->to = 0;
  612. dst->length = 0;
  613. }
  614. *p_len = tot;
  615. *actual_count = acount;
  616. return 0;
  617. }
  618. /*
  619. * Function: c2_activity (private function)
  620. *
  621. * Description:
  622. * Post an mq index to the host->adapter activity fifo.
  623. *
  624. * IN:
  625. * c2dev - ptr to c2dev structure
  626. * mq_index - mq index to post
  627. * shared - value most recently written to shared
  628. *
  629. * OUT:
  630. *
  631. * Return:
  632. * none
  633. */
  634. static inline void c2_activity(struct c2_dev *c2dev, u32 mq_index, u16 shared)
  635. {
  636. /*
  637. * First read the register to see if the FIFO is full, and if so,
  638. * spin until it's not. This isn't perfect -- there is no
  639. * synchronization among the clients of the register, but in
  640. * practice it prevents multiple CPU from hammering the bus
  641. * with PCI RETRY. Note that when this does happen, the card
  642. * cannot get on the bus and the card and system hang in a
  643. * deadlock -- thus the need for this code. [TOT]
  644. */
  645. while (readl(c2dev->regs + PCI_BAR0_ADAPTER_HINT) & 0x80000000)
  646. udelay(10);
  647. __raw_writel(C2_HINT_MAKE(mq_index, shared),
  648. c2dev->regs + PCI_BAR0_ADAPTER_HINT);
  649. }
  650. /*
  651. * Function: qp_wr_post
  652. *
  653. * Description:
  654. * This in-line function allocates a MQ msg, then moves the host-copy of
  655. * the completed WR into msg. Then it posts the message.
  656. *
  657. * IN:
  658. * q - ptr to user MQ.
  659. * wr - ptr to host-copy of the WR.
  660. * qp - ptr to user qp
  661. * size - Number of bytes to post. Assumed to be divisible by 4.
  662. *
  663. * OUT: none
  664. *
  665. * Return:
  666. * CCIL status codes.
  667. */
  668. static int qp_wr_post(struct c2_mq *q, union c2wr * wr, struct c2_qp *qp, u32 size)
  669. {
  670. union c2wr *msg;
  671. msg = c2_mq_alloc(q);
  672. if (msg == NULL) {
  673. return -EINVAL;
  674. }
  675. #ifdef CCMSGMAGIC
  676. ((c2wr_hdr_t *) wr)->magic = cpu_to_be32(CCWR_MAGIC);
  677. #endif
  678. /*
  679. * Since all header fields in the WR are the same as the
  680. * CQE, set the following so the adapter need not.
  681. */
  682. c2_wr_set_result(wr, CCERR_PENDING);
  683. /*
  684. * Copy the wr down to the adapter
  685. */
  686. memcpy((void *) msg, (void *) wr, size);
  687. c2_mq_produce(q);
  688. return 0;
  689. }
  690. int c2_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
  691. struct ib_send_wr **bad_wr)
  692. {
  693. struct c2_dev *c2dev = to_c2dev(ibqp->device);
  694. struct c2_qp *qp = to_c2qp(ibqp);
  695. union c2wr wr;
  696. unsigned long lock_flags;
  697. int err = 0;
  698. u32 flags;
  699. u32 tot_len;
  700. u8 actual_sge_count;
  701. u32 msg_size;
  702. if (qp->state > IB_QPS_RTS)
  703. return -EINVAL;
  704. while (ib_wr) {
  705. flags = 0;
  706. wr.sqwr.sq_hdr.user_hdr.hdr.context = ib_wr->wr_id;
  707. if (ib_wr->send_flags & IB_SEND_SIGNALED) {
  708. flags |= SQ_SIGNALED;
  709. }
  710. switch (ib_wr->opcode) {
  711. case IB_WR_SEND:
  712. if (ib_wr->send_flags & IB_SEND_SOLICITED) {
  713. c2_wr_set_id(&wr, C2_WR_TYPE_SEND_SE);
  714. msg_size = sizeof(struct c2wr_send_req);
  715. } else {
  716. c2_wr_set_id(&wr, C2_WR_TYPE_SEND);
  717. msg_size = sizeof(struct c2wr_send_req);
  718. }
  719. wr.sqwr.send.remote_stag = 0;
  720. msg_size += sizeof(struct c2_data_addr) * ib_wr->num_sge;
  721. if (ib_wr->num_sge > qp->send_sgl_depth) {
  722. err = -EINVAL;
  723. break;
  724. }
  725. if (ib_wr->send_flags & IB_SEND_FENCE) {
  726. flags |= SQ_READ_FENCE;
  727. }
  728. err = move_sgl((struct c2_data_addr *) & (wr.sqwr.send.data),
  729. ib_wr->sg_list,
  730. ib_wr->num_sge,
  731. &tot_len, &actual_sge_count);
  732. wr.sqwr.send.sge_len = cpu_to_be32(tot_len);
  733. c2_wr_set_sge_count(&wr, actual_sge_count);
  734. break;
  735. case IB_WR_RDMA_WRITE:
  736. c2_wr_set_id(&wr, C2_WR_TYPE_RDMA_WRITE);
  737. msg_size = sizeof(struct c2wr_rdma_write_req) +
  738. (sizeof(struct c2_data_addr) * ib_wr->num_sge);
  739. if (ib_wr->num_sge > qp->rdma_write_sgl_depth) {
  740. err = -EINVAL;
  741. break;
  742. }
  743. if (ib_wr->send_flags & IB_SEND_FENCE) {
  744. flags |= SQ_READ_FENCE;
  745. }
  746. wr.sqwr.rdma_write.remote_stag =
  747. cpu_to_be32(ib_wr->wr.rdma.rkey);
  748. wr.sqwr.rdma_write.remote_to =
  749. cpu_to_be64(ib_wr->wr.rdma.remote_addr);
  750. err = move_sgl((struct c2_data_addr *)
  751. & (wr.sqwr.rdma_write.data),
  752. ib_wr->sg_list,
  753. ib_wr->num_sge,
  754. &tot_len, &actual_sge_count);
  755. wr.sqwr.rdma_write.sge_len = cpu_to_be32(tot_len);
  756. c2_wr_set_sge_count(&wr, actual_sge_count);
  757. break;
  758. case IB_WR_RDMA_READ:
  759. c2_wr_set_id(&wr, C2_WR_TYPE_RDMA_READ);
  760. msg_size = sizeof(struct c2wr_rdma_read_req);
  761. /* IWarp only suppots 1 sge for RDMA reads */
  762. if (ib_wr->num_sge > 1) {
  763. err = -EINVAL;
  764. break;
  765. }
  766. /*
  767. * Move the local and remote stag/to/len into the WR.
  768. */
  769. wr.sqwr.rdma_read.local_stag =
  770. cpu_to_be32(ib_wr->sg_list->lkey);
  771. wr.sqwr.rdma_read.local_to =
  772. cpu_to_be64(ib_wr->sg_list->addr);
  773. wr.sqwr.rdma_read.remote_stag =
  774. cpu_to_be32(ib_wr->wr.rdma.rkey);
  775. wr.sqwr.rdma_read.remote_to =
  776. cpu_to_be64(ib_wr->wr.rdma.remote_addr);
  777. wr.sqwr.rdma_read.length =
  778. cpu_to_be32(ib_wr->sg_list->length);
  779. break;
  780. default:
  781. /* error */
  782. msg_size = 0;
  783. err = -EINVAL;
  784. break;
  785. }
  786. /*
  787. * If we had an error on the last wr build, then
  788. * break out. Possible errors include bogus WR
  789. * type, and a bogus SGL length...
  790. */
  791. if (err) {
  792. break;
  793. }
  794. /*
  795. * Store flags
  796. */
  797. c2_wr_set_flags(&wr, flags);
  798. /*
  799. * Post the puppy!
  800. */
  801. spin_lock_irqsave(&qp->lock, lock_flags);
  802. err = qp_wr_post(&qp->sq_mq, &wr, qp, msg_size);
  803. if (err) {
  804. spin_unlock_irqrestore(&qp->lock, lock_flags);
  805. break;
  806. }
  807. /*
  808. * Enqueue mq index to activity FIFO.
  809. */
  810. c2_activity(c2dev, qp->sq_mq.index, qp->sq_mq.hint_count);
  811. spin_unlock_irqrestore(&qp->lock, lock_flags);
  812. ib_wr = ib_wr->next;
  813. }
  814. if (err)
  815. *bad_wr = ib_wr;
  816. return err;
  817. }
  818. int c2_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
  819. struct ib_recv_wr **bad_wr)
  820. {
  821. struct c2_dev *c2dev = to_c2dev(ibqp->device);
  822. struct c2_qp *qp = to_c2qp(ibqp);
  823. union c2wr wr;
  824. unsigned long lock_flags;
  825. int err = 0;
  826. if (qp->state > IB_QPS_RTS)
  827. return -EINVAL;
  828. /*
  829. * Try and post each work request
  830. */
  831. while (ib_wr) {
  832. u32 tot_len;
  833. u8 actual_sge_count;
  834. if (ib_wr->num_sge > qp->recv_sgl_depth) {
  835. err = -EINVAL;
  836. break;
  837. }
  838. /*
  839. * Create local host-copy of the WR
  840. */
  841. wr.rqwr.rq_hdr.user_hdr.hdr.context = ib_wr->wr_id;
  842. c2_wr_set_id(&wr, CCWR_RECV);
  843. c2_wr_set_flags(&wr, 0);
  844. /* sge_count is limited to eight bits. */
  845. BUG_ON(ib_wr->num_sge >= 256);
  846. err = move_sgl((struct c2_data_addr *) & (wr.rqwr.data),
  847. ib_wr->sg_list,
  848. ib_wr->num_sge, &tot_len, &actual_sge_count);
  849. c2_wr_set_sge_count(&wr, actual_sge_count);
  850. /*
  851. * If we had an error on the last wr build, then
  852. * break out. Possible errors include bogus WR
  853. * type, and a bogus SGL length...
  854. */
  855. if (err) {
  856. break;
  857. }
  858. spin_lock_irqsave(&qp->lock, lock_flags);
  859. err = qp_wr_post(&qp->rq_mq, &wr, qp, qp->rq_mq.msg_size);
  860. if (err) {
  861. spin_unlock_irqrestore(&qp->lock, lock_flags);
  862. break;
  863. }
  864. /*
  865. * Enqueue mq index to activity FIFO
  866. */
  867. c2_activity(c2dev, qp->rq_mq.index, qp->rq_mq.hint_count);
  868. spin_unlock_irqrestore(&qp->lock, lock_flags);
  869. ib_wr = ib_wr->next;
  870. }
  871. if (err)
  872. *bad_wr = ib_wr;
  873. return err;
  874. }
  875. void __devinit c2_init_qp_table(struct c2_dev *c2dev)
  876. {
  877. spin_lock_init(&c2dev->qp_table.lock);
  878. idr_init(&c2dev->qp_table.idr);
  879. }
  880. void __devexit c2_cleanup_qp_table(struct c2_dev *c2dev)
  881. {
  882. idr_destroy(&c2dev->qp_table.idr);
  883. }