iwch_qp.c 29 KB

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