iwch_qp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 void build_term_codes(int t3err, u8 *layer_type, u8 *ecode, int tagged)
  449. {
  450. switch (t3err) {
  451. case TPT_ERR_STAG:
  452. if (tagged == 1) {
  453. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  454. *ecode = DDPT_INV_STAG;
  455. } else if (tagged == 2) {
  456. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  457. *ecode = RDMAP_INV_STAG;
  458. }
  459. break;
  460. case TPT_ERR_PDID:
  461. case TPT_ERR_QPID:
  462. case TPT_ERR_ACCESS:
  463. if (tagged == 1) {
  464. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  465. *ecode = DDPT_STAG_NOT_ASSOC;
  466. } else if (tagged == 2) {
  467. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  468. *ecode = RDMAP_STAG_NOT_ASSOC;
  469. }
  470. break;
  471. case TPT_ERR_WRAP:
  472. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  473. *ecode = RDMAP_TO_WRAP;
  474. break;
  475. case TPT_ERR_BOUND:
  476. if (tagged == 1) {
  477. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  478. *ecode = DDPT_BASE_BOUNDS;
  479. } else if (tagged == 2) {
  480. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  481. *ecode = RDMAP_BASE_BOUNDS;
  482. } else {
  483. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  484. *ecode = DDPU_MSG_TOOBIG;
  485. }
  486. break;
  487. case TPT_ERR_INVALIDATE_SHARED_MR:
  488. case TPT_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  489. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  490. *ecode = RDMAP_CANT_INV_STAG;
  491. break;
  492. case TPT_ERR_ECC:
  493. case TPT_ERR_ECC_PSTAG:
  494. case TPT_ERR_INTERNAL_ERR:
  495. *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
  496. *ecode = 0;
  497. break;
  498. case TPT_ERR_OUT_OF_RQE:
  499. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  500. *ecode = DDPU_INV_MSN_NOBUF;
  501. break;
  502. case TPT_ERR_PBL_ADDR_BOUND:
  503. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  504. *ecode = DDPT_BASE_BOUNDS;
  505. break;
  506. case TPT_ERR_CRC:
  507. *layer_type = LAYER_MPA|DDP_LLP;
  508. *ecode = MPA_CRC_ERR;
  509. break;
  510. case TPT_ERR_MARKER:
  511. *layer_type = LAYER_MPA|DDP_LLP;
  512. *ecode = MPA_MARKER_ERR;
  513. break;
  514. case TPT_ERR_PDU_LEN_ERR:
  515. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  516. *ecode = DDPU_MSG_TOOBIG;
  517. break;
  518. case TPT_ERR_DDP_VERSION:
  519. if (tagged) {
  520. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  521. *ecode = DDPT_INV_VERS;
  522. } else {
  523. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  524. *ecode = DDPU_INV_VERS;
  525. }
  526. break;
  527. case TPT_ERR_RDMA_VERSION:
  528. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  529. *ecode = RDMAP_INV_VERS;
  530. break;
  531. case TPT_ERR_OPCODE:
  532. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  533. *ecode = RDMAP_INV_OPCODE;
  534. break;
  535. case TPT_ERR_DDP_QUEUE_NUM:
  536. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  537. *ecode = DDPU_INV_QN;
  538. break;
  539. case TPT_ERR_MSN:
  540. case TPT_ERR_MSN_GAP:
  541. case TPT_ERR_MSN_RANGE:
  542. case TPT_ERR_IRD_OVERFLOW:
  543. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  544. *ecode = DDPU_INV_MSN_RANGE;
  545. break;
  546. case TPT_ERR_TBIT:
  547. *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
  548. *ecode = 0;
  549. break;
  550. case TPT_ERR_MO:
  551. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  552. *ecode = DDPU_INV_MO;
  553. break;
  554. default:
  555. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  556. *ecode = 0;
  557. break;
  558. }
  559. }
  560. /*
  561. * This posts a TERMINATE with layer=RDMA, type=catastrophic.
  562. */
  563. int iwch_post_terminate(struct iwch_qp *qhp, struct respQ_msg_t *rsp_msg)
  564. {
  565. union t3_wr *wqe;
  566. struct terminate_message *term;
  567. int status;
  568. int tagged = 0;
  569. struct sk_buff *skb;
  570. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  571. skb = alloc_skb(40, GFP_ATOMIC);
  572. if (!skb) {
  573. printk(KERN_ERR "%s cannot send TERMINATE!\n", __FUNCTION__);
  574. return -ENOMEM;
  575. }
  576. wqe = (union t3_wr *)skb_put(skb, 40);
  577. memset(wqe, 0, 40);
  578. wqe->send.rdmaop = T3_TERMINATE;
  579. /* immediate data length */
  580. wqe->send.plen = htonl(4);
  581. /* immediate data starts here. */
  582. term = (struct terminate_message *)wqe->send.sgl;
  583. if (rsp_msg) {
  584. status = CQE_STATUS(rsp_msg->cqe);
  585. if (CQE_OPCODE(rsp_msg->cqe) == T3_RDMA_WRITE)
  586. tagged = 1;
  587. if ((CQE_OPCODE(rsp_msg->cqe) == T3_READ_REQ) ||
  588. (CQE_OPCODE(rsp_msg->cqe) == T3_READ_RESP))
  589. tagged = 2;
  590. } else {
  591. status = TPT_ERR_INTERNAL_ERR;
  592. }
  593. build_term_codes(status, &term->layer_etype, &term->ecode, tagged);
  594. build_fw_riwrh((void *)wqe, T3_WR_SEND,
  595. T3_COMPLETION_FLAG | T3_NOTIFY_FLAG, 1,
  596. qhp->ep->hwtid, 5);
  597. skb->priority = CPL_PRIORITY_DATA;
  598. return cxgb3_ofld_send(qhp->rhp->rdev.t3cdev_p, skb);
  599. }
  600. /*
  601. * Assumes qhp lock is held.
  602. */
  603. static void __flush_qp(struct iwch_qp *qhp, unsigned long *flag)
  604. {
  605. struct iwch_cq *rchp, *schp;
  606. int count;
  607. rchp = get_chp(qhp->rhp, qhp->attr.rcq);
  608. schp = get_chp(qhp->rhp, qhp->attr.scq);
  609. PDBG("%s qhp %p rchp %p schp %p\n", __FUNCTION__, qhp, rchp, schp);
  610. /* take a ref on the qhp since we must release the lock */
  611. atomic_inc(&qhp->refcnt);
  612. spin_unlock_irqrestore(&qhp->lock, *flag);
  613. /* locking heirarchy: cq lock first, then qp lock. */
  614. spin_lock_irqsave(&rchp->lock, *flag);
  615. spin_lock(&qhp->lock);
  616. cxio_flush_hw_cq(&rchp->cq);
  617. cxio_count_rcqes(&rchp->cq, &qhp->wq, &count);
  618. cxio_flush_rq(&qhp->wq, &rchp->cq, count);
  619. spin_unlock(&qhp->lock);
  620. spin_unlock_irqrestore(&rchp->lock, *flag);
  621. /* locking heirarchy: cq lock first, then qp lock. */
  622. spin_lock_irqsave(&schp->lock, *flag);
  623. spin_lock(&qhp->lock);
  624. cxio_flush_hw_cq(&schp->cq);
  625. cxio_count_scqes(&schp->cq, &qhp->wq, &count);
  626. cxio_flush_sq(&qhp->wq, &schp->cq, count);
  627. spin_unlock(&qhp->lock);
  628. spin_unlock_irqrestore(&schp->lock, *flag);
  629. /* deref */
  630. if (atomic_dec_and_test(&qhp->refcnt))
  631. wake_up(&qhp->wait);
  632. spin_lock_irqsave(&qhp->lock, *flag);
  633. }
  634. static void flush_qp(struct iwch_qp *qhp, unsigned long *flag)
  635. {
  636. if (t3b_device(qhp->rhp))
  637. cxio_set_wq_in_error(&qhp->wq);
  638. else
  639. __flush_qp(qhp, flag);
  640. }
  641. /*
  642. * Return non zero if at least one RECV was pre-posted.
  643. */
  644. static int rqes_posted(struct iwch_qp *qhp)
  645. {
  646. return fw_riwrh_opcode((struct fw_riwrh *)qhp->wq.queue) == T3_WR_RCV;
  647. }
  648. static int rdma_init(struct iwch_dev *rhp, struct iwch_qp *qhp,
  649. enum iwch_qp_attr_mask mask,
  650. struct iwch_qp_attributes *attrs)
  651. {
  652. struct t3_rdma_init_attr init_attr;
  653. int ret;
  654. init_attr.tid = qhp->ep->hwtid;
  655. init_attr.qpid = qhp->wq.qpid;
  656. init_attr.pdid = qhp->attr.pd;
  657. init_attr.scqid = qhp->attr.scq;
  658. init_attr.rcqid = qhp->attr.rcq;
  659. init_attr.rq_addr = qhp->wq.rq_addr;
  660. init_attr.rq_size = 1 << qhp->wq.rq_size_log2;
  661. init_attr.mpaattrs = uP_RI_MPA_IETF_ENABLE |
  662. qhp->attr.mpa_attr.recv_marker_enabled |
  663. (qhp->attr.mpa_attr.xmit_marker_enabled << 1) |
  664. (qhp->attr.mpa_attr.crc_enabled << 2);
  665. /*
  666. * XXX - The IWCM doesn't quite handle getting these
  667. * attrs set before going into RTS. For now, just turn
  668. * them on always...
  669. */
  670. #if 0
  671. init_attr.qpcaps = qhp->attr.enableRdmaRead |
  672. (qhp->attr.enableRdmaWrite << 1) |
  673. (qhp->attr.enableBind << 2) |
  674. (qhp->attr.enable_stag0_fastreg << 3) |
  675. (qhp->attr.enable_stag0_fastreg << 4);
  676. #else
  677. init_attr.qpcaps = 0x1f;
  678. #endif
  679. init_attr.tcp_emss = qhp->ep->emss;
  680. init_attr.ord = qhp->attr.max_ord;
  681. init_attr.ird = qhp->attr.max_ird;
  682. init_attr.qp_dma_addr = qhp->wq.dma_addr;
  683. init_attr.qp_dma_size = (1UL << qhp->wq.size_log2);
  684. init_attr.flags = rqes_posted(qhp) ? RECVS_POSTED : 0;
  685. PDBG("%s init_attr.rq_addr 0x%x init_attr.rq_size = %d "
  686. "flags 0x%x qpcaps 0x%x\n", __FUNCTION__,
  687. init_attr.rq_addr, init_attr.rq_size,
  688. init_attr.flags, init_attr.qpcaps);
  689. ret = cxio_rdma_init(&rhp->rdev, &init_attr);
  690. PDBG("%s ret %d\n", __FUNCTION__, ret);
  691. return ret;
  692. }
  693. int iwch_modify_qp(struct iwch_dev *rhp, struct iwch_qp *qhp,
  694. enum iwch_qp_attr_mask mask,
  695. struct iwch_qp_attributes *attrs,
  696. int internal)
  697. {
  698. int ret = 0;
  699. struct iwch_qp_attributes newattr = qhp->attr;
  700. unsigned long flag;
  701. int disconnect = 0;
  702. int terminate = 0;
  703. int abort = 0;
  704. int free = 0;
  705. struct iwch_ep *ep = NULL;
  706. PDBG("%s qhp %p qpid 0x%x ep %p state %d -> %d\n", __FUNCTION__,
  707. qhp, qhp->wq.qpid, qhp->ep, qhp->attr.state,
  708. (mask & IWCH_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
  709. spin_lock_irqsave(&qhp->lock, flag);
  710. /* Process attr changes if in IDLE */
  711. if (mask & IWCH_QP_ATTR_VALID_MODIFY) {
  712. if (qhp->attr.state != IWCH_QP_STATE_IDLE) {
  713. ret = -EIO;
  714. goto out;
  715. }
  716. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_READ)
  717. newattr.enable_rdma_read = attrs->enable_rdma_read;
  718. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_WRITE)
  719. newattr.enable_rdma_write = attrs->enable_rdma_write;
  720. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_BIND)
  721. newattr.enable_bind = attrs->enable_bind;
  722. if (mask & IWCH_QP_ATTR_MAX_ORD) {
  723. if (attrs->max_ord >
  724. rhp->attr.max_rdma_read_qp_depth) {
  725. ret = -EINVAL;
  726. goto out;
  727. }
  728. newattr.max_ord = attrs->max_ord;
  729. }
  730. if (mask & IWCH_QP_ATTR_MAX_IRD) {
  731. if (attrs->max_ird >
  732. rhp->attr.max_rdma_reads_per_qp) {
  733. ret = -EINVAL;
  734. goto out;
  735. }
  736. newattr.max_ird = attrs->max_ird;
  737. }
  738. qhp->attr = newattr;
  739. }
  740. if (!(mask & IWCH_QP_ATTR_NEXT_STATE))
  741. goto out;
  742. if (qhp->attr.state == attrs->next_state)
  743. goto out;
  744. switch (qhp->attr.state) {
  745. case IWCH_QP_STATE_IDLE:
  746. switch (attrs->next_state) {
  747. case IWCH_QP_STATE_RTS:
  748. if (!(mask & IWCH_QP_ATTR_LLP_STREAM_HANDLE)) {
  749. ret = -EINVAL;
  750. goto out;
  751. }
  752. if (!(mask & IWCH_QP_ATTR_MPA_ATTR)) {
  753. ret = -EINVAL;
  754. goto out;
  755. }
  756. qhp->attr.mpa_attr = attrs->mpa_attr;
  757. qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
  758. qhp->ep = qhp->attr.llp_stream_handle;
  759. qhp->attr.state = IWCH_QP_STATE_RTS;
  760. /*
  761. * Ref the endpoint here and deref when we
  762. * disassociate the endpoint from the QP. This
  763. * happens in CLOSING->IDLE transition or *->ERROR
  764. * transition.
  765. */
  766. get_ep(&qhp->ep->com);
  767. spin_unlock_irqrestore(&qhp->lock, flag);
  768. ret = rdma_init(rhp, qhp, mask, attrs);
  769. spin_lock_irqsave(&qhp->lock, flag);
  770. if (ret)
  771. goto err;
  772. break;
  773. case IWCH_QP_STATE_ERROR:
  774. qhp->attr.state = IWCH_QP_STATE_ERROR;
  775. flush_qp(qhp, &flag);
  776. break;
  777. default:
  778. ret = -EINVAL;
  779. goto out;
  780. }
  781. break;
  782. case IWCH_QP_STATE_RTS:
  783. switch (attrs->next_state) {
  784. case IWCH_QP_STATE_CLOSING:
  785. BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
  786. qhp->attr.state = IWCH_QP_STATE_CLOSING;
  787. if (!internal) {
  788. abort=0;
  789. disconnect = 1;
  790. ep = qhp->ep;
  791. }
  792. break;
  793. case IWCH_QP_STATE_TERMINATE:
  794. qhp->attr.state = IWCH_QP_STATE_TERMINATE;
  795. if (t3b_device(qhp->rhp))
  796. cxio_set_wq_in_error(&qhp->wq);
  797. if (!internal)
  798. terminate = 1;
  799. break;
  800. case IWCH_QP_STATE_ERROR:
  801. qhp->attr.state = IWCH_QP_STATE_ERROR;
  802. if (!internal) {
  803. abort=1;
  804. disconnect = 1;
  805. ep = qhp->ep;
  806. }
  807. goto err;
  808. break;
  809. default:
  810. ret = -EINVAL;
  811. goto out;
  812. }
  813. break;
  814. case IWCH_QP_STATE_CLOSING:
  815. if (!internal) {
  816. ret = -EINVAL;
  817. goto out;
  818. }
  819. switch (attrs->next_state) {
  820. case IWCH_QP_STATE_IDLE:
  821. qhp->attr.state = IWCH_QP_STATE_IDLE;
  822. qhp->attr.llp_stream_handle = NULL;
  823. put_ep(&qhp->ep->com);
  824. qhp->ep = NULL;
  825. wake_up(&qhp->wait);
  826. break;
  827. case IWCH_QP_STATE_ERROR:
  828. goto err;
  829. default:
  830. ret = -EINVAL;
  831. goto err;
  832. }
  833. break;
  834. case IWCH_QP_STATE_ERROR:
  835. if (attrs->next_state != IWCH_QP_STATE_IDLE) {
  836. ret = -EINVAL;
  837. goto out;
  838. }
  839. if (!Q_EMPTY(qhp->wq.sq_rptr, qhp->wq.sq_wptr) ||
  840. !Q_EMPTY(qhp->wq.rq_rptr, qhp->wq.rq_wptr)) {
  841. ret = -EINVAL;
  842. goto out;
  843. }
  844. qhp->attr.state = IWCH_QP_STATE_IDLE;
  845. memset(&qhp->attr, 0, sizeof(qhp->attr));
  846. break;
  847. case IWCH_QP_STATE_TERMINATE:
  848. if (!internal) {
  849. ret = -EINVAL;
  850. goto out;
  851. }
  852. goto err;
  853. break;
  854. default:
  855. printk(KERN_ERR "%s in a bad state %d\n",
  856. __FUNCTION__, qhp->attr.state);
  857. ret = -EINVAL;
  858. goto err;
  859. break;
  860. }
  861. goto out;
  862. err:
  863. PDBG("%s disassociating ep %p qpid 0x%x\n", __FUNCTION__, qhp->ep,
  864. qhp->wq.qpid);
  865. /* disassociate the LLP connection */
  866. qhp->attr.llp_stream_handle = NULL;
  867. ep = qhp->ep;
  868. qhp->ep = NULL;
  869. qhp->attr.state = IWCH_QP_STATE_ERROR;
  870. free=1;
  871. wake_up(&qhp->wait);
  872. BUG_ON(!ep);
  873. flush_qp(qhp, &flag);
  874. out:
  875. spin_unlock_irqrestore(&qhp->lock, flag);
  876. if (terminate)
  877. iwch_post_terminate(qhp, NULL);
  878. /*
  879. * If disconnect is 1, then we need to initiate a disconnect
  880. * on the EP. This can be a normal close (RTS->CLOSING) or
  881. * an abnormal close (RTS/CLOSING->ERROR).
  882. */
  883. if (disconnect)
  884. iwch_ep_disconnect(ep, abort, GFP_KERNEL);
  885. /*
  886. * If free is 1, then we've disassociated the EP from the QP
  887. * and we need to dereference the EP.
  888. */
  889. if (free)
  890. put_ep(&ep->com);
  891. PDBG("%s exit state %d\n", __FUNCTION__, qhp->attr.state);
  892. return ret;
  893. }
  894. static int quiesce_qp(struct iwch_qp *qhp)
  895. {
  896. spin_lock_irq(&qhp->lock);
  897. iwch_quiesce_tid(qhp->ep);
  898. qhp->flags |= QP_QUIESCED;
  899. spin_unlock_irq(&qhp->lock);
  900. return 0;
  901. }
  902. static int resume_qp(struct iwch_qp *qhp)
  903. {
  904. spin_lock_irq(&qhp->lock);
  905. iwch_resume_tid(qhp->ep);
  906. qhp->flags &= ~QP_QUIESCED;
  907. spin_unlock_irq(&qhp->lock);
  908. return 0;
  909. }
  910. int iwch_quiesce_qps(struct iwch_cq *chp)
  911. {
  912. int i;
  913. struct iwch_qp *qhp;
  914. for (i=0; i < T3_MAX_NUM_QP; i++) {
  915. qhp = get_qhp(chp->rhp, i);
  916. if (!qhp)
  917. continue;
  918. if ((qhp->attr.rcq == chp->cq.cqid) && !qp_quiesced(qhp)) {
  919. quiesce_qp(qhp);
  920. continue;
  921. }
  922. if ((qhp->attr.scq == chp->cq.cqid) && !qp_quiesced(qhp))
  923. quiesce_qp(qhp);
  924. }
  925. return 0;
  926. }
  927. int iwch_resume_qps(struct iwch_cq *chp)
  928. {
  929. int i;
  930. struct iwch_qp *qhp;
  931. for (i=0; i < T3_MAX_NUM_QP; i++) {
  932. qhp = get_qhp(chp->rhp, i);
  933. if (!qhp)
  934. continue;
  935. if ((qhp->attr.rcq == chp->cq.cqid) && qp_quiesced(qhp)) {
  936. resume_qp(qhp);
  937. continue;
  938. }
  939. if ((qhp->attr.scq == chp->cq.cqid) && qp_quiesced(qhp))
  940. resume_qp(qhp);
  941. }
  942. return 0;
  943. }