iwch_qp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
  3. * Copyright (c) 2006 Open Grid Computing, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include "iwch_provider.h"
  34. #include "iwch.h"
  35. #include "iwch_cm.h"
  36. #include "cxio_hal.h"
  37. #define NO_SUPPORT -1
  38. static inline int iwch_build_rdma_send(union t3_wr *wqe, struct ib_send_wr *wr,
  39. u8 * flit_cnt)
  40. {
  41. int i;
  42. u32 plen;
  43. switch (wr->opcode) {
  44. case IB_WR_SEND:
  45. case IB_WR_SEND_WITH_IMM:
  46. if (wr->send_flags & IB_SEND_SOLICITED)
  47. wqe->send.rdmaop = T3_SEND_WITH_SE;
  48. else
  49. wqe->send.rdmaop = T3_SEND;
  50. wqe->send.rem_stag = 0;
  51. break;
  52. #if 0 /* Not currently supported */
  53. case TYPE_SEND_INVALIDATE:
  54. case TYPE_SEND_INVALIDATE_IMMEDIATE:
  55. wqe->send.rdmaop = T3_SEND_WITH_INV;
  56. wqe->send.rem_stag = cpu_to_be32(wr->wr.rdma.rkey);
  57. break;
  58. case TYPE_SEND_SE_INVALIDATE:
  59. wqe->send.rdmaop = T3_SEND_WITH_SE_INV;
  60. wqe->send.rem_stag = cpu_to_be32(wr->wr.rdma.rkey);
  61. break;
  62. #endif
  63. default:
  64. break;
  65. }
  66. if (wr->num_sge > T3_MAX_SGE)
  67. return -EINVAL;
  68. wqe->send.reserved[0] = 0;
  69. wqe->send.reserved[1] = 0;
  70. wqe->send.reserved[2] = 0;
  71. if (wr->opcode == IB_WR_SEND_WITH_IMM) {
  72. plen = 4;
  73. wqe->send.sgl[0].stag = wr->imm_data;
  74. wqe->send.sgl[0].len = __constant_cpu_to_be32(0);
  75. wqe->send.num_sgle = __constant_cpu_to_be32(0);
  76. *flit_cnt = 5;
  77. } else {
  78. plen = 0;
  79. for (i = 0; i < wr->num_sge; i++) {
  80. if ((plen + wr->sg_list[i].length) < plen) {
  81. return -EMSGSIZE;
  82. }
  83. plen += wr->sg_list[i].length;
  84. wqe->send.sgl[i].stag =
  85. cpu_to_be32(wr->sg_list[i].lkey);
  86. wqe->send.sgl[i].len =
  87. cpu_to_be32(wr->sg_list[i].length);
  88. wqe->send.sgl[i].to = cpu_to_be64(wr->sg_list[i].addr);
  89. }
  90. wqe->send.num_sgle = cpu_to_be32(wr->num_sge);
  91. *flit_cnt = 4 + ((wr->num_sge) << 1);
  92. }
  93. wqe->send.plen = cpu_to_be32(plen);
  94. return 0;
  95. }
  96. static inline int iwch_build_rdma_write(union t3_wr *wqe, struct ib_send_wr *wr,
  97. u8 *flit_cnt)
  98. {
  99. int i;
  100. u32 plen;
  101. if (wr->num_sge > T3_MAX_SGE)
  102. return -EINVAL;
  103. wqe->write.rdmaop = T3_RDMA_WRITE;
  104. wqe->write.reserved[0] = 0;
  105. wqe->write.reserved[1] = 0;
  106. wqe->write.reserved[2] = 0;
  107. wqe->write.stag_sink = cpu_to_be32(wr->wr.rdma.rkey);
  108. wqe->write.to_sink = cpu_to_be64(wr->wr.rdma.remote_addr);
  109. if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  110. plen = 4;
  111. wqe->write.sgl[0].stag = wr->imm_data;
  112. wqe->write.sgl[0].len = __constant_cpu_to_be32(0);
  113. wqe->write.num_sgle = __constant_cpu_to_be32(0);
  114. *flit_cnt = 6;
  115. } else {
  116. plen = 0;
  117. for (i = 0; i < wr->num_sge; i++) {
  118. if ((plen + wr->sg_list[i].length) < plen) {
  119. return -EMSGSIZE;
  120. }
  121. plen += wr->sg_list[i].length;
  122. wqe->write.sgl[i].stag =
  123. cpu_to_be32(wr->sg_list[i].lkey);
  124. wqe->write.sgl[i].len =
  125. cpu_to_be32(wr->sg_list[i].length);
  126. wqe->write.sgl[i].to =
  127. cpu_to_be64(wr->sg_list[i].addr);
  128. }
  129. wqe->write.num_sgle = cpu_to_be32(wr->num_sge);
  130. *flit_cnt = 5 + ((wr->num_sge) << 1);
  131. }
  132. wqe->write.plen = cpu_to_be32(plen);
  133. return 0;
  134. }
  135. static inline int iwch_build_rdma_read(union t3_wr *wqe, struct ib_send_wr *wr,
  136. u8 *flit_cnt)
  137. {
  138. if (wr->num_sge > 1)
  139. return -EINVAL;
  140. wqe->read.rdmaop = T3_READ_REQ;
  141. wqe->read.reserved[0] = 0;
  142. wqe->read.reserved[1] = 0;
  143. wqe->read.reserved[2] = 0;
  144. wqe->read.rem_stag = cpu_to_be32(wr->wr.rdma.rkey);
  145. wqe->read.rem_to = cpu_to_be64(wr->wr.rdma.remote_addr);
  146. wqe->read.local_stag = cpu_to_be32(wr->sg_list[0].lkey);
  147. wqe->read.local_len = cpu_to_be32(wr->sg_list[0].length);
  148. wqe->read.local_to = cpu_to_be64(wr->sg_list[0].addr);
  149. *flit_cnt = sizeof(struct t3_rdma_read_wr) >> 3;
  150. return 0;
  151. }
  152. /*
  153. * TBD: this is going to be moved to firmware. Missing pdid/qpid check for now.
  154. */
  155. static inline int iwch_sgl2pbl_map(struct iwch_dev *rhp,
  156. struct ib_sge *sg_list, u32 num_sgle,
  157. u32 * pbl_addr, u8 * page_size)
  158. {
  159. int i;
  160. struct iwch_mr *mhp;
  161. u32 offset;
  162. for (i = 0; i < num_sgle; i++) {
  163. mhp = get_mhp(rhp, (sg_list[i].lkey) >> 8);
  164. if (!mhp) {
  165. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  166. return -EIO;
  167. }
  168. if (!mhp->attr.state) {
  169. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  170. return -EIO;
  171. }
  172. if (mhp->attr.zbva) {
  173. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  174. return -EIO;
  175. }
  176. if (sg_list[i].addr < mhp->attr.va_fbo) {
  177. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  178. return -EINVAL;
  179. }
  180. if (sg_list[i].addr + ((u64) sg_list[i].length) <
  181. sg_list[i].addr) {
  182. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  183. return -EINVAL;
  184. }
  185. if (sg_list[i].addr + ((u64) sg_list[i].length) >
  186. mhp->attr.va_fbo + ((u64) mhp->attr.len)) {
  187. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  188. return -EINVAL;
  189. }
  190. offset = sg_list[i].addr - mhp->attr.va_fbo;
  191. offset += ((u32) mhp->attr.va_fbo) %
  192. (1UL << (12 + mhp->attr.page_size));
  193. pbl_addr[i] = ((mhp->attr.pbl_addr -
  194. rhp->rdev.rnic_info.pbl_base) >> 3) +
  195. (offset >> (12 + mhp->attr.page_size));
  196. page_size[i] = mhp->attr.page_size;
  197. }
  198. return 0;
  199. }
  200. static inline int iwch_build_rdma_recv(struct iwch_dev *rhp,
  201. union t3_wr *wqe,
  202. struct ib_recv_wr *wr)
  203. {
  204. int i, err = 0;
  205. u32 pbl_addr[4];
  206. u8 page_size[4];
  207. if (wr->num_sge > T3_MAX_SGE)
  208. return -EINVAL;
  209. err = iwch_sgl2pbl_map(rhp, wr->sg_list, wr->num_sge, pbl_addr,
  210. page_size);
  211. if (err)
  212. return err;
  213. wqe->recv.pagesz[0] = page_size[0];
  214. wqe->recv.pagesz[1] = page_size[1];
  215. wqe->recv.pagesz[2] = page_size[2];
  216. wqe->recv.pagesz[3] = page_size[3];
  217. wqe->recv.num_sgle = cpu_to_be32(wr->num_sge);
  218. for (i = 0; i < wr->num_sge; i++) {
  219. wqe->recv.sgl[i].stag = cpu_to_be32(wr->sg_list[i].lkey);
  220. wqe->recv.sgl[i].len = cpu_to_be32(wr->sg_list[i].length);
  221. /* to in the WQE == the offset into the page */
  222. wqe->recv.sgl[i].to = cpu_to_be64(((u32) wr->sg_list[i].addr) %
  223. (1UL << (12 + page_size[i])));
  224. /* pbl_addr is the adapters address in the PBL */
  225. wqe->recv.pbl_addr[i] = cpu_to_be32(pbl_addr[i]);
  226. }
  227. for (; i < T3_MAX_SGE; i++) {
  228. wqe->recv.sgl[i].stag = 0;
  229. wqe->recv.sgl[i].len = 0;
  230. wqe->recv.sgl[i].to = 0;
  231. wqe->recv.pbl_addr[i] = 0;
  232. }
  233. return 0;
  234. }
  235. int iwch_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  236. struct ib_send_wr **bad_wr)
  237. {
  238. int err = 0;
  239. u8 t3_wr_flit_cnt;
  240. enum t3_wr_opcode t3_wr_opcode = 0;
  241. enum t3_wr_flags t3_wr_flags;
  242. struct iwch_qp *qhp;
  243. u32 idx;
  244. union t3_wr *wqe;
  245. u32 num_wrs;
  246. unsigned long flag;
  247. struct t3_swsq *sqp;
  248. qhp = to_iwch_qp(ibqp);
  249. spin_lock_irqsave(&qhp->lock, flag);
  250. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  251. spin_unlock_irqrestore(&qhp->lock, flag);
  252. return -EINVAL;
  253. }
  254. num_wrs = Q_FREECNT(qhp->wq.sq_rptr, qhp->wq.sq_wptr,
  255. qhp->wq.sq_size_log2);
  256. if (num_wrs <= 0) {
  257. spin_unlock_irqrestore(&qhp->lock, flag);
  258. return -ENOMEM;
  259. }
  260. while (wr) {
  261. if (num_wrs == 0) {
  262. err = -ENOMEM;
  263. *bad_wr = wr;
  264. break;
  265. }
  266. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  267. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  268. t3_wr_flags = 0;
  269. if (wr->send_flags & IB_SEND_SOLICITED)
  270. t3_wr_flags |= T3_SOLICITED_EVENT_FLAG;
  271. if (wr->send_flags & IB_SEND_FENCE)
  272. t3_wr_flags |= T3_READ_FENCE_FLAG;
  273. if (wr->send_flags & IB_SEND_SIGNALED)
  274. t3_wr_flags |= T3_COMPLETION_FLAG;
  275. sqp = qhp->wq.sq +
  276. Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2);
  277. switch (wr->opcode) {
  278. case IB_WR_SEND:
  279. case IB_WR_SEND_WITH_IMM:
  280. t3_wr_opcode = T3_WR_SEND;
  281. err = iwch_build_rdma_send(wqe, wr, &t3_wr_flit_cnt);
  282. break;
  283. case IB_WR_RDMA_WRITE:
  284. case IB_WR_RDMA_WRITE_WITH_IMM:
  285. t3_wr_opcode = T3_WR_WRITE;
  286. err = iwch_build_rdma_write(wqe, wr, &t3_wr_flit_cnt);
  287. break;
  288. case IB_WR_RDMA_READ:
  289. t3_wr_opcode = T3_WR_READ;
  290. t3_wr_flags = 0; /* T3 reads are always signaled */
  291. err = iwch_build_rdma_read(wqe, wr, &t3_wr_flit_cnt);
  292. if (err)
  293. break;
  294. sqp->read_len = wqe->read.local_len;
  295. if (!qhp->wq.oldest_read)
  296. qhp->wq.oldest_read = sqp;
  297. break;
  298. default:
  299. PDBG("%s post of type=%d TBD!\n", __FUNCTION__,
  300. wr->opcode);
  301. err = -EINVAL;
  302. }
  303. if (err) {
  304. *bad_wr = wr;
  305. break;
  306. }
  307. wqe->send.wrid.id0.hi = qhp->wq.sq_wptr;
  308. sqp->wr_id = wr->wr_id;
  309. sqp->opcode = wr2opcode(t3_wr_opcode);
  310. sqp->sq_wptr = qhp->wq.sq_wptr;
  311. sqp->complete = 0;
  312. sqp->signaled = (wr->send_flags & IB_SEND_SIGNALED);
  313. build_fw_riwrh((void *) wqe, t3_wr_opcode, t3_wr_flags,
  314. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2),
  315. 0, t3_wr_flit_cnt);
  316. PDBG("%s cookie 0x%llx wq idx 0x%x swsq idx %ld opcode %d\n",
  317. __FUNCTION__, (unsigned long long) wr->wr_id, idx,
  318. Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2),
  319. sqp->opcode);
  320. wr = wr->next;
  321. num_wrs--;
  322. ++(qhp->wq.wptr);
  323. ++(qhp->wq.sq_wptr);
  324. }
  325. spin_unlock_irqrestore(&qhp->lock, flag);
  326. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  327. return err;
  328. }
  329. int iwch_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  330. struct ib_recv_wr **bad_wr)
  331. {
  332. int err = 0;
  333. struct iwch_qp *qhp;
  334. u32 idx;
  335. union t3_wr *wqe;
  336. u32 num_wrs;
  337. unsigned long flag;
  338. qhp = to_iwch_qp(ibqp);
  339. spin_lock_irqsave(&qhp->lock, flag);
  340. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  341. spin_unlock_irqrestore(&qhp->lock, flag);
  342. return -EINVAL;
  343. }
  344. num_wrs = Q_FREECNT(qhp->wq.rq_rptr, qhp->wq.rq_wptr,
  345. qhp->wq.rq_size_log2) - 1;
  346. if (!wr) {
  347. spin_unlock_irqrestore(&qhp->lock, flag);
  348. return -EINVAL;
  349. }
  350. while (wr) {
  351. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  352. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  353. if (num_wrs)
  354. err = iwch_build_rdma_recv(qhp->rhp, wqe, wr);
  355. else
  356. err = -ENOMEM;
  357. if (err) {
  358. *bad_wr = wr;
  359. break;
  360. }
  361. qhp->wq.rq[Q_PTR2IDX(qhp->wq.rq_wptr, qhp->wq.rq_size_log2)] =
  362. wr->wr_id;
  363. build_fw_riwrh((void *) wqe, T3_WR_RCV, T3_COMPLETION_FLAG,
  364. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2),
  365. 0, sizeof(struct t3_receive_wr) >> 3);
  366. PDBG("%s cookie 0x%llx idx 0x%x rq_wptr 0x%x rw_rptr 0x%x "
  367. "wqe %p \n", __FUNCTION__, (unsigned long long) wr->wr_id,
  368. idx, qhp->wq.rq_wptr, qhp->wq.rq_rptr, wqe);
  369. ++(qhp->wq.rq_wptr);
  370. ++(qhp->wq.wptr);
  371. wr = wr->next;
  372. num_wrs--;
  373. }
  374. spin_unlock_irqrestore(&qhp->lock, flag);
  375. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  376. return err;
  377. }
  378. int iwch_bind_mw(struct ib_qp *qp,
  379. struct ib_mw *mw,
  380. struct ib_mw_bind *mw_bind)
  381. {
  382. struct iwch_dev *rhp;
  383. struct iwch_mw *mhp;
  384. struct iwch_qp *qhp;
  385. union t3_wr *wqe;
  386. u32 pbl_addr;
  387. u8 page_size;
  388. u32 num_wrs;
  389. unsigned long flag;
  390. struct ib_sge sgl;
  391. int err=0;
  392. enum t3_wr_flags t3_wr_flags;
  393. u32 idx;
  394. struct t3_swsq *sqp;
  395. qhp = to_iwch_qp(qp);
  396. mhp = to_iwch_mw(mw);
  397. rhp = qhp->rhp;
  398. spin_lock_irqsave(&qhp->lock, flag);
  399. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  400. spin_unlock_irqrestore(&qhp->lock, flag);
  401. return -EINVAL;
  402. }
  403. num_wrs = Q_FREECNT(qhp->wq.sq_rptr, qhp->wq.sq_wptr,
  404. qhp->wq.sq_size_log2);
  405. if ((num_wrs) <= 0) {
  406. spin_unlock_irqrestore(&qhp->lock, flag);
  407. return -ENOMEM;
  408. }
  409. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  410. PDBG("%s: idx 0x%0x, mw 0x%p, mw_bind 0x%p\n", __FUNCTION__, idx,
  411. mw, mw_bind);
  412. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  413. t3_wr_flags = 0;
  414. if (mw_bind->send_flags & IB_SEND_SIGNALED)
  415. t3_wr_flags = T3_COMPLETION_FLAG;
  416. sgl.addr = mw_bind->addr;
  417. sgl.lkey = mw_bind->mr->lkey;
  418. sgl.length = mw_bind->length;
  419. wqe->bind.reserved = 0;
  420. wqe->bind.type = T3_VA_BASED_TO;
  421. /* TBD: check perms */
  422. wqe->bind.perms = iwch_convert_access(mw_bind->mw_access_flags);
  423. wqe->bind.mr_stag = cpu_to_be32(mw_bind->mr->lkey);
  424. wqe->bind.mw_stag = cpu_to_be32(mw->rkey);
  425. wqe->bind.mw_len = cpu_to_be32(mw_bind->length);
  426. wqe->bind.mw_va = cpu_to_be64(mw_bind->addr);
  427. err = iwch_sgl2pbl_map(rhp, &sgl, 1, &pbl_addr, &page_size);
  428. if (err) {
  429. spin_unlock_irqrestore(&qhp->lock, flag);
  430. return err;
  431. }
  432. wqe->send.wrid.id0.hi = qhp->wq.sq_wptr;
  433. sqp = qhp->wq.sq + Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2);
  434. sqp->wr_id = mw_bind->wr_id;
  435. sqp->opcode = T3_BIND_MW;
  436. sqp->sq_wptr = qhp->wq.sq_wptr;
  437. sqp->complete = 0;
  438. sqp->signaled = (mw_bind->send_flags & IB_SEND_SIGNALED);
  439. wqe->bind.mr_pbl_addr = cpu_to_be32(pbl_addr);
  440. wqe->bind.mr_pagesz = page_size;
  441. wqe->flit[T3_SQ_COOKIE_FLIT] = mw_bind->wr_id;
  442. build_fw_riwrh((void *)wqe, T3_WR_BIND, t3_wr_flags,
  443. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2), 0,
  444. sizeof(struct t3_bind_mw_wr) >> 3);
  445. ++(qhp->wq.wptr);
  446. ++(qhp->wq.sq_wptr);
  447. spin_unlock_irqrestore(&qhp->lock, flag);
  448. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  449. return err;
  450. }
  451. static inline void build_term_codes(int t3err, u8 *layer_type, u8 *ecode,
  452. int tagged)
  453. {
  454. switch (t3err) {
  455. case TPT_ERR_STAG:
  456. if (tagged == 1) {
  457. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  458. *ecode = DDPT_INV_STAG;
  459. } else if (tagged == 2) {
  460. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  461. *ecode = RDMAP_INV_STAG;
  462. }
  463. break;
  464. case TPT_ERR_PDID:
  465. case TPT_ERR_QPID:
  466. case TPT_ERR_ACCESS:
  467. if (tagged == 1) {
  468. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  469. *ecode = DDPT_STAG_NOT_ASSOC;
  470. } else if (tagged == 2) {
  471. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  472. *ecode = RDMAP_STAG_NOT_ASSOC;
  473. }
  474. break;
  475. case TPT_ERR_WRAP:
  476. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  477. *ecode = RDMAP_TO_WRAP;
  478. break;
  479. case TPT_ERR_BOUND:
  480. if (tagged == 1) {
  481. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  482. *ecode = DDPT_BASE_BOUNDS;
  483. } else if (tagged == 2) {
  484. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  485. *ecode = RDMAP_BASE_BOUNDS;
  486. } else {
  487. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  488. *ecode = DDPU_MSG_TOOBIG;
  489. }
  490. break;
  491. case TPT_ERR_INVALIDATE_SHARED_MR:
  492. case TPT_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  493. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  494. *ecode = RDMAP_CANT_INV_STAG;
  495. break;
  496. case TPT_ERR_ECC:
  497. case TPT_ERR_ECC_PSTAG:
  498. case TPT_ERR_INTERNAL_ERR:
  499. *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
  500. *ecode = 0;
  501. break;
  502. case TPT_ERR_OUT_OF_RQE:
  503. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  504. *ecode = DDPU_INV_MSN_NOBUF;
  505. break;
  506. case TPT_ERR_PBL_ADDR_BOUND:
  507. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  508. *ecode = DDPT_BASE_BOUNDS;
  509. break;
  510. case TPT_ERR_CRC:
  511. *layer_type = LAYER_MPA|DDP_LLP;
  512. *ecode = MPA_CRC_ERR;
  513. break;
  514. case TPT_ERR_MARKER:
  515. *layer_type = LAYER_MPA|DDP_LLP;
  516. *ecode = MPA_MARKER_ERR;
  517. break;
  518. case TPT_ERR_PDU_LEN_ERR:
  519. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  520. *ecode = DDPU_MSG_TOOBIG;
  521. break;
  522. case TPT_ERR_DDP_VERSION:
  523. if (tagged) {
  524. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  525. *ecode = DDPT_INV_VERS;
  526. } else {
  527. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  528. *ecode = DDPU_INV_VERS;
  529. }
  530. break;
  531. case TPT_ERR_RDMA_VERSION:
  532. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  533. *ecode = RDMAP_INV_VERS;
  534. break;
  535. case TPT_ERR_OPCODE:
  536. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  537. *ecode = RDMAP_INV_OPCODE;
  538. break;
  539. case TPT_ERR_DDP_QUEUE_NUM:
  540. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  541. *ecode = DDPU_INV_QN;
  542. break;
  543. case TPT_ERR_MSN:
  544. case TPT_ERR_MSN_GAP:
  545. case TPT_ERR_MSN_RANGE:
  546. case TPT_ERR_IRD_OVERFLOW:
  547. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  548. *ecode = DDPU_INV_MSN_RANGE;
  549. break;
  550. case TPT_ERR_TBIT:
  551. *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
  552. *ecode = 0;
  553. break;
  554. case TPT_ERR_MO:
  555. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  556. *ecode = DDPU_INV_MO;
  557. break;
  558. default:
  559. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  560. *ecode = 0;
  561. break;
  562. }
  563. }
  564. /*
  565. * This posts a TERMINATE with layer=RDMA, type=catastrophic.
  566. */
  567. int iwch_post_terminate(struct iwch_qp *qhp, struct respQ_msg_t *rsp_msg)
  568. {
  569. union t3_wr *wqe;
  570. struct terminate_message *term;
  571. int status;
  572. int tagged = 0;
  573. struct sk_buff *skb;
  574. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  575. skb = alloc_skb(40, GFP_ATOMIC);
  576. if (!skb) {
  577. printk(KERN_ERR "%s cannot send TERMINATE!\n", __FUNCTION__);
  578. return -ENOMEM;
  579. }
  580. wqe = (union t3_wr *)skb_put(skb, 40);
  581. memset(wqe, 0, 40);
  582. wqe->send.rdmaop = T3_TERMINATE;
  583. /* immediate data length */
  584. wqe->send.plen = htonl(4);
  585. /* immediate data starts here. */
  586. term = (struct terminate_message *)wqe->send.sgl;
  587. if (rsp_msg) {
  588. status = CQE_STATUS(rsp_msg->cqe);
  589. if (CQE_OPCODE(rsp_msg->cqe) == T3_RDMA_WRITE)
  590. tagged = 1;
  591. if ((CQE_OPCODE(rsp_msg->cqe) == T3_READ_REQ) ||
  592. (CQE_OPCODE(rsp_msg->cqe) == T3_READ_RESP))
  593. tagged = 2;
  594. } else {
  595. status = TPT_ERR_INTERNAL_ERR;
  596. }
  597. build_term_codes(status, &term->layer_etype, &term->ecode, tagged);
  598. build_fw_riwrh((void *)wqe, T3_WR_SEND,
  599. T3_COMPLETION_FLAG | T3_NOTIFY_FLAG, 1,
  600. qhp->ep->hwtid, 5);
  601. skb->priority = CPL_PRIORITY_DATA;
  602. return cxgb3_ofld_send(qhp->rhp->rdev.t3cdev_p, skb);
  603. }
  604. /*
  605. * Assumes qhp lock is held.
  606. */
  607. static void __flush_qp(struct iwch_qp *qhp, unsigned long *flag)
  608. {
  609. struct iwch_cq *rchp, *schp;
  610. int count;
  611. rchp = get_chp(qhp->rhp, qhp->attr.rcq);
  612. schp = get_chp(qhp->rhp, qhp->attr.scq);
  613. PDBG("%s qhp %p rchp %p schp %p\n", __FUNCTION__, qhp, rchp, schp);
  614. /* take a ref on the qhp since we must release the lock */
  615. atomic_inc(&qhp->refcnt);
  616. spin_unlock_irqrestore(&qhp->lock, *flag);
  617. /* locking heirarchy: cq lock first, then qp lock. */
  618. spin_lock_irqsave(&rchp->lock, *flag);
  619. spin_lock(&qhp->lock);
  620. cxio_flush_hw_cq(&rchp->cq);
  621. cxio_count_rcqes(&rchp->cq, &qhp->wq, &count);
  622. cxio_flush_rq(&qhp->wq, &rchp->cq, count);
  623. spin_unlock(&qhp->lock);
  624. spin_unlock_irqrestore(&rchp->lock, *flag);
  625. /* locking heirarchy: cq lock first, then qp lock. */
  626. spin_lock_irqsave(&schp->lock, *flag);
  627. spin_lock(&qhp->lock);
  628. cxio_flush_hw_cq(&schp->cq);
  629. cxio_count_scqes(&schp->cq, &qhp->wq, &count);
  630. cxio_flush_sq(&qhp->wq, &schp->cq, count);
  631. spin_unlock(&qhp->lock);
  632. spin_unlock_irqrestore(&schp->lock, *flag);
  633. /* deref */
  634. if (atomic_dec_and_test(&qhp->refcnt))
  635. wake_up(&qhp->wait);
  636. spin_lock_irqsave(&qhp->lock, *flag);
  637. }
  638. static inline void flush_qp(struct iwch_qp *qhp, unsigned long *flag)
  639. {
  640. if (t3b_device(qhp->rhp))
  641. cxio_set_wq_in_error(&qhp->wq);
  642. else
  643. __flush_qp(qhp, flag);
  644. }
  645. /*
  646. * Return non zero if at least one RECV was pre-posted.
  647. */
  648. static inline int rqes_posted(struct iwch_qp *qhp)
  649. {
  650. return fw_riwrh_opcode((struct fw_riwrh *)qhp->wq.queue) == T3_WR_RCV;
  651. }
  652. static int rdma_init(struct iwch_dev *rhp, struct iwch_qp *qhp,
  653. enum iwch_qp_attr_mask mask,
  654. struct iwch_qp_attributes *attrs)
  655. {
  656. struct t3_rdma_init_attr init_attr;
  657. int ret;
  658. init_attr.tid = qhp->ep->hwtid;
  659. init_attr.qpid = qhp->wq.qpid;
  660. init_attr.pdid = qhp->attr.pd;
  661. init_attr.scqid = qhp->attr.scq;
  662. init_attr.rcqid = qhp->attr.rcq;
  663. init_attr.rq_addr = qhp->wq.rq_addr;
  664. init_attr.rq_size = 1 << qhp->wq.rq_size_log2;
  665. init_attr.mpaattrs = uP_RI_MPA_IETF_ENABLE |
  666. qhp->attr.mpa_attr.recv_marker_enabled |
  667. (qhp->attr.mpa_attr.xmit_marker_enabled << 1) |
  668. (qhp->attr.mpa_attr.crc_enabled << 2);
  669. /*
  670. * XXX - The IWCM doesn't quite handle getting these
  671. * attrs set before going into RTS. For now, just turn
  672. * them on always...
  673. */
  674. #if 0
  675. init_attr.qpcaps = qhp->attr.enableRdmaRead |
  676. (qhp->attr.enableRdmaWrite << 1) |
  677. (qhp->attr.enableBind << 2) |
  678. (qhp->attr.enable_stag0_fastreg << 3) |
  679. (qhp->attr.enable_stag0_fastreg << 4);
  680. #else
  681. init_attr.qpcaps = 0x1f;
  682. #endif
  683. init_attr.tcp_emss = qhp->ep->emss;
  684. init_attr.ord = qhp->attr.max_ord;
  685. init_attr.ird = qhp->attr.max_ird;
  686. init_attr.qp_dma_addr = qhp->wq.dma_addr;
  687. init_attr.qp_dma_size = (1UL << qhp->wq.size_log2);
  688. init_attr.flags = rqes_posted(qhp) ? RECVS_POSTED : 0;
  689. PDBG("%s init_attr.rq_addr 0x%x init_attr.rq_size = %d "
  690. "flags 0x%x qpcaps 0x%x\n", __FUNCTION__,
  691. init_attr.rq_addr, init_attr.rq_size,
  692. init_attr.flags, init_attr.qpcaps);
  693. ret = cxio_rdma_init(&rhp->rdev, &init_attr);
  694. PDBG("%s ret %d\n", __FUNCTION__, ret);
  695. return ret;
  696. }
  697. int iwch_modify_qp(struct iwch_dev *rhp, struct iwch_qp *qhp,
  698. enum iwch_qp_attr_mask mask,
  699. struct iwch_qp_attributes *attrs,
  700. int internal)
  701. {
  702. int ret = 0;
  703. struct iwch_qp_attributes newattr = qhp->attr;
  704. unsigned long flag;
  705. int disconnect = 0;
  706. int terminate = 0;
  707. int abort = 0;
  708. int free = 0;
  709. struct iwch_ep *ep = NULL;
  710. PDBG("%s qhp %p qpid 0x%x ep %p state %d -> %d\n", __FUNCTION__,
  711. qhp, qhp->wq.qpid, qhp->ep, qhp->attr.state,
  712. (mask & IWCH_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
  713. spin_lock_irqsave(&qhp->lock, flag);
  714. /* Process attr changes if in IDLE */
  715. if (mask & IWCH_QP_ATTR_VALID_MODIFY) {
  716. if (qhp->attr.state != IWCH_QP_STATE_IDLE) {
  717. ret = -EIO;
  718. goto out;
  719. }
  720. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_READ)
  721. newattr.enable_rdma_read = attrs->enable_rdma_read;
  722. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_WRITE)
  723. newattr.enable_rdma_write = attrs->enable_rdma_write;
  724. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_BIND)
  725. newattr.enable_bind = attrs->enable_bind;
  726. if (mask & IWCH_QP_ATTR_MAX_ORD) {
  727. if (attrs->max_ord >
  728. rhp->attr.max_rdma_read_qp_depth) {
  729. ret = -EINVAL;
  730. goto out;
  731. }
  732. newattr.max_ord = attrs->max_ord;
  733. }
  734. if (mask & IWCH_QP_ATTR_MAX_IRD) {
  735. if (attrs->max_ird >
  736. rhp->attr.max_rdma_reads_per_qp) {
  737. ret = -EINVAL;
  738. goto out;
  739. }
  740. newattr.max_ird = attrs->max_ird;
  741. }
  742. qhp->attr = newattr;
  743. }
  744. if (!(mask & IWCH_QP_ATTR_NEXT_STATE))
  745. goto out;
  746. if (qhp->attr.state == attrs->next_state)
  747. goto out;
  748. switch (qhp->attr.state) {
  749. case IWCH_QP_STATE_IDLE:
  750. switch (attrs->next_state) {
  751. case IWCH_QP_STATE_RTS:
  752. if (!(mask & IWCH_QP_ATTR_LLP_STREAM_HANDLE)) {
  753. ret = -EINVAL;
  754. goto out;
  755. }
  756. if (!(mask & IWCH_QP_ATTR_MPA_ATTR)) {
  757. ret = -EINVAL;
  758. goto out;
  759. }
  760. qhp->attr.mpa_attr = attrs->mpa_attr;
  761. qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
  762. qhp->ep = qhp->attr.llp_stream_handle;
  763. qhp->attr.state = IWCH_QP_STATE_RTS;
  764. /*
  765. * Ref the endpoint here and deref when we
  766. * disassociate the endpoint from the QP. This
  767. * happens in CLOSING->IDLE transition or *->ERROR
  768. * transition.
  769. */
  770. get_ep(&qhp->ep->com);
  771. spin_unlock_irqrestore(&qhp->lock, flag);
  772. ret = rdma_init(rhp, qhp, mask, attrs);
  773. spin_lock_irqsave(&qhp->lock, flag);
  774. if (ret)
  775. goto err;
  776. break;
  777. case IWCH_QP_STATE_ERROR:
  778. qhp->attr.state = IWCH_QP_STATE_ERROR;
  779. flush_qp(qhp, &flag);
  780. break;
  781. default:
  782. ret = -EINVAL;
  783. goto out;
  784. }
  785. break;
  786. case IWCH_QP_STATE_RTS:
  787. switch (attrs->next_state) {
  788. case IWCH_QP_STATE_CLOSING:
  789. BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
  790. qhp->attr.state = IWCH_QP_STATE_CLOSING;
  791. if (!internal) {
  792. abort=0;
  793. disconnect = 1;
  794. ep = qhp->ep;
  795. }
  796. break;
  797. case IWCH_QP_STATE_TERMINATE:
  798. qhp->attr.state = IWCH_QP_STATE_TERMINATE;
  799. if (t3b_device(qhp->rhp))
  800. cxio_set_wq_in_error(&qhp->wq);
  801. if (!internal)
  802. terminate = 1;
  803. break;
  804. case IWCH_QP_STATE_ERROR:
  805. qhp->attr.state = IWCH_QP_STATE_ERROR;
  806. if (!internal) {
  807. abort=1;
  808. disconnect = 1;
  809. ep = qhp->ep;
  810. }
  811. goto err;
  812. break;
  813. default:
  814. ret = -EINVAL;
  815. goto out;
  816. }
  817. break;
  818. case IWCH_QP_STATE_CLOSING:
  819. if (!internal) {
  820. ret = -EINVAL;
  821. goto out;
  822. }
  823. switch (attrs->next_state) {
  824. case IWCH_QP_STATE_IDLE:
  825. qhp->attr.state = IWCH_QP_STATE_IDLE;
  826. qhp->attr.llp_stream_handle = NULL;
  827. put_ep(&qhp->ep->com);
  828. qhp->ep = NULL;
  829. wake_up(&qhp->wait);
  830. break;
  831. case IWCH_QP_STATE_ERROR:
  832. goto err;
  833. default:
  834. ret = -EINVAL;
  835. goto err;
  836. }
  837. break;
  838. case IWCH_QP_STATE_ERROR:
  839. if (attrs->next_state != IWCH_QP_STATE_IDLE) {
  840. ret = -EINVAL;
  841. goto out;
  842. }
  843. if (!Q_EMPTY(qhp->wq.sq_rptr, qhp->wq.sq_wptr) ||
  844. !Q_EMPTY(qhp->wq.rq_rptr, qhp->wq.rq_wptr)) {
  845. ret = -EINVAL;
  846. goto out;
  847. }
  848. qhp->attr.state = IWCH_QP_STATE_IDLE;
  849. memset(&qhp->attr, 0, sizeof(qhp->attr));
  850. break;
  851. case IWCH_QP_STATE_TERMINATE:
  852. if (!internal) {
  853. ret = -EINVAL;
  854. goto out;
  855. }
  856. goto err;
  857. break;
  858. default:
  859. printk(KERN_ERR "%s in a bad state %d\n",
  860. __FUNCTION__, qhp->attr.state);
  861. ret = -EINVAL;
  862. goto err;
  863. break;
  864. }
  865. goto out;
  866. err:
  867. PDBG("%s disassociating ep %p qpid 0x%x\n", __FUNCTION__, qhp->ep,
  868. qhp->wq.qpid);
  869. /* disassociate the LLP connection */
  870. qhp->attr.llp_stream_handle = NULL;
  871. ep = qhp->ep;
  872. qhp->ep = NULL;
  873. qhp->attr.state = IWCH_QP_STATE_ERROR;
  874. free=1;
  875. wake_up(&qhp->wait);
  876. BUG_ON(!ep);
  877. flush_qp(qhp, &flag);
  878. out:
  879. spin_unlock_irqrestore(&qhp->lock, flag);
  880. if (terminate)
  881. iwch_post_terminate(qhp, NULL);
  882. /*
  883. * If disconnect is 1, then we need to initiate a disconnect
  884. * on the EP. This can be a normal close (RTS->CLOSING) or
  885. * an abnormal close (RTS/CLOSING->ERROR).
  886. */
  887. if (disconnect)
  888. iwch_ep_disconnect(ep, abort, GFP_KERNEL);
  889. /*
  890. * If free is 1, then we've disassociated the EP from the QP
  891. * and we need to dereference the EP.
  892. */
  893. if (free)
  894. put_ep(&ep->com);
  895. PDBG("%s exit state %d\n", __FUNCTION__, qhp->attr.state);
  896. return ret;
  897. }
  898. static int quiesce_qp(struct iwch_qp *qhp)
  899. {
  900. spin_lock_irq(&qhp->lock);
  901. iwch_quiesce_tid(qhp->ep);
  902. qhp->flags |= QP_QUIESCED;
  903. spin_unlock_irq(&qhp->lock);
  904. return 0;
  905. }
  906. static int resume_qp(struct iwch_qp *qhp)
  907. {
  908. spin_lock_irq(&qhp->lock);
  909. iwch_resume_tid(qhp->ep);
  910. qhp->flags &= ~QP_QUIESCED;
  911. spin_unlock_irq(&qhp->lock);
  912. return 0;
  913. }
  914. int iwch_quiesce_qps(struct iwch_cq *chp)
  915. {
  916. int i;
  917. struct iwch_qp *qhp;
  918. for (i=0; i < T3_MAX_NUM_QP; i++) {
  919. qhp = get_qhp(chp->rhp, i);
  920. if (!qhp)
  921. continue;
  922. if ((qhp->attr.rcq == chp->cq.cqid) && !qp_quiesced(qhp)) {
  923. quiesce_qp(qhp);
  924. continue;
  925. }
  926. if ((qhp->attr.scq == chp->cq.cqid) && !qp_quiesced(qhp))
  927. quiesce_qp(qhp);
  928. }
  929. return 0;
  930. }
  931. int iwch_resume_qps(struct iwch_cq *chp)
  932. {
  933. int i;
  934. struct iwch_qp *qhp;
  935. for (i=0; i < T3_MAX_NUM_QP; i++) {
  936. qhp = get_qhp(chp->rhp, i);
  937. if (!qhp)
  938. continue;
  939. if ((qhp->attr.rcq == chp->cq.cqid) && qp_quiesced(qhp)) {
  940. resume_qp(qhp);
  941. continue;
  942. }
  943. if ((qhp->attr.scq == chp->cq.cqid) && qp_quiesced(qhp))
  944. resume_qp(qhp);
  945. }
  946. return 0;
  947. }