iwch_qp.c 26 KB

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