iwch_qp.c 32 KB

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