iwch_qp.c 26 KB

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