iwch_qp.c 32 KB

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