c2_qp.c 24 KB

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