c2_qp.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. 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", __FUNCTION__, 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. __FUNCTION__, __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. if (ib_wr->send_flags & IB_SEND_SOLICITED) {
  717. c2_wr_set_id(&wr, C2_WR_TYPE_SEND_SE);
  718. msg_size = sizeof(struct c2wr_send_req);
  719. } else {
  720. c2_wr_set_id(&wr, C2_WR_TYPE_SEND);
  721. msg_size = sizeof(struct c2wr_send_req);
  722. }
  723. wr.sqwr.send.remote_stag = 0;
  724. msg_size += sizeof(struct c2_data_addr) * ib_wr->num_sge;
  725. if (ib_wr->num_sge > qp->send_sgl_depth) {
  726. err = -EINVAL;
  727. break;
  728. }
  729. if (ib_wr->send_flags & IB_SEND_FENCE) {
  730. flags |= SQ_READ_FENCE;
  731. }
  732. err = move_sgl((struct c2_data_addr *) & (wr.sqwr.send.data),
  733. ib_wr->sg_list,
  734. ib_wr->num_sge,
  735. &tot_len, &actual_sge_count);
  736. wr.sqwr.send.sge_len = cpu_to_be32(tot_len);
  737. c2_wr_set_sge_count(&wr, actual_sge_count);
  738. break;
  739. case IB_WR_RDMA_WRITE:
  740. c2_wr_set_id(&wr, C2_WR_TYPE_RDMA_WRITE);
  741. msg_size = sizeof(struct c2wr_rdma_write_req) +
  742. (sizeof(struct c2_data_addr) * ib_wr->num_sge);
  743. if (ib_wr->num_sge > qp->rdma_write_sgl_depth) {
  744. err = -EINVAL;
  745. break;
  746. }
  747. if (ib_wr->send_flags & IB_SEND_FENCE) {
  748. flags |= SQ_READ_FENCE;
  749. }
  750. wr.sqwr.rdma_write.remote_stag =
  751. cpu_to_be32(ib_wr->wr.rdma.rkey);
  752. wr.sqwr.rdma_write.remote_to =
  753. cpu_to_be64(ib_wr->wr.rdma.remote_addr);
  754. err = move_sgl((struct c2_data_addr *)
  755. & (wr.sqwr.rdma_write.data),
  756. ib_wr->sg_list,
  757. ib_wr->num_sge,
  758. &tot_len, &actual_sge_count);
  759. wr.sqwr.rdma_write.sge_len = cpu_to_be32(tot_len);
  760. c2_wr_set_sge_count(&wr, actual_sge_count);
  761. break;
  762. case IB_WR_RDMA_READ:
  763. c2_wr_set_id(&wr, C2_WR_TYPE_RDMA_READ);
  764. msg_size = sizeof(struct c2wr_rdma_read_req);
  765. /* IWarp only suppots 1 sge for RDMA reads */
  766. if (ib_wr->num_sge > 1) {
  767. err = -EINVAL;
  768. break;
  769. }
  770. /*
  771. * Move the local and remote stag/to/len into the WR.
  772. */
  773. wr.sqwr.rdma_read.local_stag =
  774. cpu_to_be32(ib_wr->sg_list->lkey);
  775. wr.sqwr.rdma_read.local_to =
  776. cpu_to_be64(ib_wr->sg_list->addr);
  777. wr.sqwr.rdma_read.remote_stag =
  778. cpu_to_be32(ib_wr->wr.rdma.rkey);
  779. wr.sqwr.rdma_read.remote_to =
  780. cpu_to_be64(ib_wr->wr.rdma.remote_addr);
  781. wr.sqwr.rdma_read.length =
  782. cpu_to_be32(ib_wr->sg_list->length);
  783. break;
  784. default:
  785. /* error */
  786. msg_size = 0;
  787. err = -EINVAL;
  788. break;
  789. }
  790. /*
  791. * If we had an error on the last wr build, then
  792. * break out. Possible errors include bogus WR
  793. * type, and a bogus SGL length...
  794. */
  795. if (err) {
  796. break;
  797. }
  798. /*
  799. * Store flags
  800. */
  801. c2_wr_set_flags(&wr, flags);
  802. /*
  803. * Post the puppy!
  804. */
  805. spin_lock_irqsave(&qp->lock, lock_flags);
  806. err = qp_wr_post(&qp->sq_mq, &wr, qp, msg_size);
  807. if (err) {
  808. spin_unlock_irqrestore(&qp->lock, lock_flags);
  809. break;
  810. }
  811. /*
  812. * Enqueue mq index to activity FIFO.
  813. */
  814. c2_activity(c2dev, qp->sq_mq.index, qp->sq_mq.hint_count);
  815. spin_unlock_irqrestore(&qp->lock, lock_flags);
  816. ib_wr = ib_wr->next;
  817. }
  818. if (err)
  819. *bad_wr = ib_wr;
  820. return err;
  821. }
  822. int c2_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
  823. struct ib_recv_wr **bad_wr)
  824. {
  825. struct c2_dev *c2dev = to_c2dev(ibqp->device);
  826. struct c2_qp *qp = to_c2qp(ibqp);
  827. union c2wr wr;
  828. unsigned long lock_flags;
  829. int err = 0;
  830. if (qp->state > IB_QPS_RTS)
  831. return -EINVAL;
  832. /*
  833. * Try and post each work request
  834. */
  835. while (ib_wr) {
  836. u32 tot_len;
  837. u8 actual_sge_count;
  838. if (ib_wr->num_sge > qp->recv_sgl_depth) {
  839. err = -EINVAL;
  840. break;
  841. }
  842. /*
  843. * Create local host-copy of the WR
  844. */
  845. wr.rqwr.rq_hdr.user_hdr.hdr.context = ib_wr->wr_id;
  846. c2_wr_set_id(&wr, CCWR_RECV);
  847. c2_wr_set_flags(&wr, 0);
  848. /* sge_count is limited to eight bits. */
  849. BUG_ON(ib_wr->num_sge >= 256);
  850. err = move_sgl((struct c2_data_addr *) & (wr.rqwr.data),
  851. ib_wr->sg_list,
  852. ib_wr->num_sge, &tot_len, &actual_sge_count);
  853. c2_wr_set_sge_count(&wr, actual_sge_count);
  854. /*
  855. * If we had an error on the last wr build, then
  856. * break out. Possible errors include bogus WR
  857. * type, and a bogus SGL length...
  858. */
  859. if (err) {
  860. break;
  861. }
  862. spin_lock_irqsave(&qp->lock, lock_flags);
  863. err = qp_wr_post(&qp->rq_mq, &wr, qp, qp->rq_mq.msg_size);
  864. if (err) {
  865. spin_unlock_irqrestore(&qp->lock, lock_flags);
  866. break;
  867. }
  868. /*
  869. * Enqueue mq index to activity FIFO
  870. */
  871. c2_activity(c2dev, qp->rq_mq.index, qp->rq_mq.hint_count);
  872. spin_unlock_irqrestore(&qp->lock, lock_flags);
  873. ib_wr = ib_wr->next;
  874. }
  875. if (err)
  876. *bad_wr = ib_wr;
  877. return err;
  878. }
  879. void __devinit c2_init_qp_table(struct c2_dev *c2dev)
  880. {
  881. spin_lock_init(&c2dev->qp_table.lock);
  882. idr_init(&c2dev->qp_table.idr);
  883. }
  884. void __devexit c2_cleanup_qp_table(struct c2_dev *c2dev)
  885. {
  886. idr_destroy(&c2dev->qp_table.idr);
  887. }