qp.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. /*
  2. * Copyright (c) 2009-2010 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 "iw_cxgb4.h"
  33. static int ocqp_support = 1;
  34. module_param(ocqp_support, int, 0644);
  35. MODULE_PARM_DESC(ocqp_support, "Support on-chip SQs (default=1)");
  36. static void set_state(struct c4iw_qp *qhp, enum c4iw_qp_state state)
  37. {
  38. unsigned long flag;
  39. spin_lock_irqsave(&qhp->lock, flag);
  40. qhp->attr.state = state;
  41. spin_unlock_irqrestore(&qhp->lock, flag);
  42. }
  43. static void dealloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  44. {
  45. c4iw_ocqp_pool_free(rdev, sq->dma_addr, sq->memsize);
  46. }
  47. static void dealloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  48. {
  49. dma_free_coherent(&(rdev->lldi.pdev->dev), sq->memsize, sq->queue,
  50. pci_unmap_addr(sq, mapping));
  51. }
  52. static void dealloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  53. {
  54. if (t4_sq_onchip(sq))
  55. dealloc_oc_sq(rdev, sq);
  56. else
  57. dealloc_host_sq(rdev, sq);
  58. }
  59. static int alloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  60. {
  61. if (!ocqp_support || !t4_ocqp_supported())
  62. return -ENOSYS;
  63. sq->dma_addr = c4iw_ocqp_pool_alloc(rdev, sq->memsize);
  64. if (!sq->dma_addr)
  65. return -ENOMEM;
  66. sq->phys_addr = rdev->oc_mw_pa + sq->dma_addr -
  67. rdev->lldi.vr->ocq.start;
  68. sq->queue = (__force union t4_wr *)(rdev->oc_mw_kva + sq->dma_addr -
  69. rdev->lldi.vr->ocq.start);
  70. sq->flags |= T4_SQ_ONCHIP;
  71. return 0;
  72. }
  73. static int alloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  74. {
  75. sq->queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev), sq->memsize,
  76. &(sq->dma_addr), GFP_KERNEL);
  77. if (!sq->queue)
  78. return -ENOMEM;
  79. sq->phys_addr = virt_to_phys(sq->queue);
  80. pci_unmap_addr_set(sq, mapping, sq->dma_addr);
  81. return 0;
  82. }
  83. static int destroy_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
  84. struct c4iw_dev_ucontext *uctx)
  85. {
  86. /*
  87. * uP clears EQ contexts when the connection exits rdma mode,
  88. * so no need to post a RESET WR for these EQs.
  89. */
  90. dma_free_coherent(&(rdev->lldi.pdev->dev),
  91. wq->rq.memsize, wq->rq.queue,
  92. dma_unmap_addr(&wq->rq, mapping));
  93. dealloc_sq(rdev, &wq->sq);
  94. c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
  95. kfree(wq->rq.sw_rq);
  96. kfree(wq->sq.sw_sq);
  97. c4iw_put_qpid(rdev, wq->rq.qid, uctx);
  98. c4iw_put_qpid(rdev, wq->sq.qid, uctx);
  99. return 0;
  100. }
  101. static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
  102. struct t4_cq *rcq, struct t4_cq *scq,
  103. struct c4iw_dev_ucontext *uctx)
  104. {
  105. int user = (uctx != &rdev->uctx);
  106. struct fw_ri_res_wr *res_wr;
  107. struct fw_ri_res *res;
  108. int wr_len;
  109. struct c4iw_wr_wait wr_wait;
  110. struct sk_buff *skb;
  111. int ret;
  112. int eqsize;
  113. wq->sq.qid = c4iw_get_qpid(rdev, uctx);
  114. if (!wq->sq.qid)
  115. return -ENOMEM;
  116. wq->rq.qid = c4iw_get_qpid(rdev, uctx);
  117. if (!wq->rq.qid)
  118. goto err1;
  119. if (!user) {
  120. wq->sq.sw_sq = kzalloc(wq->sq.size * sizeof *wq->sq.sw_sq,
  121. GFP_KERNEL);
  122. if (!wq->sq.sw_sq)
  123. goto err2;
  124. wq->rq.sw_rq = kzalloc(wq->rq.size * sizeof *wq->rq.sw_rq,
  125. GFP_KERNEL);
  126. if (!wq->rq.sw_rq)
  127. goto err3;
  128. }
  129. /*
  130. * RQT must be a power of 2.
  131. */
  132. wq->rq.rqt_size = roundup_pow_of_two(wq->rq.size);
  133. wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
  134. if (!wq->rq.rqt_hwaddr)
  135. goto err4;
  136. if (user) {
  137. if (alloc_oc_sq(rdev, &wq->sq) && alloc_host_sq(rdev, &wq->sq))
  138. goto err5;
  139. } else
  140. if (alloc_host_sq(rdev, &wq->sq))
  141. goto err5;
  142. memset(wq->sq.queue, 0, wq->sq.memsize);
  143. dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
  144. wq->rq.queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev),
  145. wq->rq.memsize, &(wq->rq.dma_addr),
  146. GFP_KERNEL);
  147. if (!wq->rq.queue)
  148. goto err6;
  149. PDBG("%s sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx\n",
  150. __func__, wq->sq.queue,
  151. (unsigned long long)virt_to_phys(wq->sq.queue),
  152. wq->rq.queue,
  153. (unsigned long long)virt_to_phys(wq->rq.queue));
  154. memset(wq->rq.queue, 0, wq->rq.memsize);
  155. dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
  156. wq->db = rdev->lldi.db_reg;
  157. wq->gts = rdev->lldi.gts_reg;
  158. if (user) {
  159. wq->sq.udb = (u64)pci_resource_start(rdev->lldi.pdev, 2) +
  160. (wq->sq.qid << rdev->qpshift);
  161. wq->sq.udb &= PAGE_MASK;
  162. wq->rq.udb = (u64)pci_resource_start(rdev->lldi.pdev, 2) +
  163. (wq->rq.qid << rdev->qpshift);
  164. wq->rq.udb &= PAGE_MASK;
  165. }
  166. wq->rdev = rdev;
  167. wq->rq.msn = 1;
  168. /* build fw_ri_res_wr */
  169. wr_len = sizeof *res_wr + 2 * sizeof *res;
  170. skb = alloc_skb(wr_len, GFP_KERNEL);
  171. if (!skb) {
  172. ret = -ENOMEM;
  173. goto err7;
  174. }
  175. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  176. res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
  177. memset(res_wr, 0, wr_len);
  178. res_wr->op_nres = cpu_to_be32(
  179. FW_WR_OP(FW_RI_RES_WR) |
  180. V_FW_RI_RES_WR_NRES(2) |
  181. FW_WR_COMPL(1));
  182. res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
  183. res_wr->cookie = (unsigned long) &wr_wait;
  184. res = res_wr->res;
  185. res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
  186. res->u.sqrq.op = FW_RI_RES_OP_WRITE;
  187. /*
  188. * eqsize is the number of 64B entries plus the status page size.
  189. */
  190. eqsize = wq->sq.size * T4_SQ_NUM_SLOTS + T4_EQ_STATUS_ENTRIES;
  191. res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
  192. V_FW_RI_RES_WR_HOSTFCMODE(0) | /* no host cidx updates */
  193. V_FW_RI_RES_WR_CPRIO(0) | /* don't keep in chip cache */
  194. V_FW_RI_RES_WR_PCIECHN(0) | /* set by uP at ri_init time */
  195. (t4_sq_onchip(&wq->sq) ? F_FW_RI_RES_WR_ONCHIP : 0) |
  196. V_FW_RI_RES_WR_IQID(scq->cqid));
  197. res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
  198. V_FW_RI_RES_WR_DCAEN(0) |
  199. V_FW_RI_RES_WR_DCACPU(0) |
  200. V_FW_RI_RES_WR_FBMIN(2) |
  201. V_FW_RI_RES_WR_FBMAX(2) |
  202. V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
  203. V_FW_RI_RES_WR_CIDXFTHRESH(0) |
  204. V_FW_RI_RES_WR_EQSIZE(eqsize));
  205. res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
  206. res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
  207. res++;
  208. res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
  209. res->u.sqrq.op = FW_RI_RES_OP_WRITE;
  210. /*
  211. * eqsize is the number of 64B entries plus the status page size.
  212. */
  213. eqsize = wq->rq.size * T4_RQ_NUM_SLOTS + T4_EQ_STATUS_ENTRIES;
  214. res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
  215. V_FW_RI_RES_WR_HOSTFCMODE(0) | /* no host cidx updates */
  216. V_FW_RI_RES_WR_CPRIO(0) | /* don't keep in chip cache */
  217. V_FW_RI_RES_WR_PCIECHN(0) | /* set by uP at ri_init time */
  218. V_FW_RI_RES_WR_IQID(rcq->cqid));
  219. res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
  220. V_FW_RI_RES_WR_DCAEN(0) |
  221. V_FW_RI_RES_WR_DCACPU(0) |
  222. V_FW_RI_RES_WR_FBMIN(2) |
  223. V_FW_RI_RES_WR_FBMAX(2) |
  224. V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
  225. V_FW_RI_RES_WR_CIDXFTHRESH(0) |
  226. V_FW_RI_RES_WR_EQSIZE(eqsize));
  227. res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
  228. res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
  229. c4iw_init_wr_wait(&wr_wait);
  230. ret = c4iw_ofld_send(rdev, skb);
  231. if (ret)
  232. goto err7;
  233. ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, wq->sq.qid, __func__);
  234. if (ret)
  235. goto err7;
  236. PDBG("%s sqid 0x%x rqid 0x%x kdb 0x%p squdb 0x%llx rqudb 0x%llx\n",
  237. __func__, wq->sq.qid, wq->rq.qid, wq->db,
  238. (unsigned long long)wq->sq.udb, (unsigned long long)wq->rq.udb);
  239. return 0;
  240. err7:
  241. dma_free_coherent(&(rdev->lldi.pdev->dev),
  242. wq->rq.memsize, wq->rq.queue,
  243. dma_unmap_addr(&wq->rq, mapping));
  244. err6:
  245. dealloc_sq(rdev, &wq->sq);
  246. err5:
  247. c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
  248. err4:
  249. kfree(wq->rq.sw_rq);
  250. err3:
  251. kfree(wq->sq.sw_sq);
  252. err2:
  253. c4iw_put_qpid(rdev, wq->rq.qid, uctx);
  254. err1:
  255. c4iw_put_qpid(rdev, wq->sq.qid, uctx);
  256. return -ENOMEM;
  257. }
  258. static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
  259. struct ib_send_wr *wr, int max, u32 *plenp)
  260. {
  261. u8 *dstp, *srcp;
  262. u32 plen = 0;
  263. int i;
  264. int rem, len;
  265. dstp = (u8 *)immdp->data;
  266. for (i = 0; i < wr->num_sge; i++) {
  267. if ((plen + wr->sg_list[i].length) > max)
  268. return -EMSGSIZE;
  269. srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
  270. plen += wr->sg_list[i].length;
  271. rem = wr->sg_list[i].length;
  272. while (rem) {
  273. if (dstp == (u8 *)&sq->queue[sq->size])
  274. dstp = (u8 *)sq->queue;
  275. if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
  276. len = rem;
  277. else
  278. len = (u8 *)&sq->queue[sq->size] - dstp;
  279. memcpy(dstp, srcp, len);
  280. dstp += len;
  281. srcp += len;
  282. rem -= len;
  283. }
  284. }
  285. len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
  286. if (len)
  287. memset(dstp, 0, len);
  288. immdp->op = FW_RI_DATA_IMMD;
  289. immdp->r1 = 0;
  290. immdp->r2 = 0;
  291. immdp->immdlen = cpu_to_be32(plen);
  292. *plenp = plen;
  293. return 0;
  294. }
  295. static int build_isgl(__be64 *queue_start, __be64 *queue_end,
  296. struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
  297. int num_sge, u32 *plenp)
  298. {
  299. int i;
  300. u32 plen = 0;
  301. __be64 *flitp = (__be64 *)isglp->sge;
  302. for (i = 0; i < num_sge; i++) {
  303. if ((plen + sg_list[i].length) < plen)
  304. return -EMSGSIZE;
  305. plen += sg_list[i].length;
  306. *flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
  307. sg_list[i].length);
  308. if (++flitp == queue_end)
  309. flitp = queue_start;
  310. *flitp = cpu_to_be64(sg_list[i].addr);
  311. if (++flitp == queue_end)
  312. flitp = queue_start;
  313. }
  314. *flitp = (__force __be64)0;
  315. isglp->op = FW_RI_DATA_ISGL;
  316. isglp->r1 = 0;
  317. isglp->nsge = cpu_to_be16(num_sge);
  318. isglp->r2 = 0;
  319. if (plenp)
  320. *plenp = plen;
  321. return 0;
  322. }
  323. static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
  324. struct ib_send_wr *wr, u8 *len16)
  325. {
  326. u32 plen;
  327. int size;
  328. int ret;
  329. if (wr->num_sge > T4_MAX_SEND_SGE)
  330. return -EINVAL;
  331. switch (wr->opcode) {
  332. case IB_WR_SEND:
  333. if (wr->send_flags & IB_SEND_SOLICITED)
  334. wqe->send.sendop_pkd = cpu_to_be32(
  335. V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE));
  336. else
  337. wqe->send.sendop_pkd = cpu_to_be32(
  338. V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND));
  339. wqe->send.stag_inv = 0;
  340. break;
  341. case IB_WR_SEND_WITH_INV:
  342. if (wr->send_flags & IB_SEND_SOLICITED)
  343. wqe->send.sendop_pkd = cpu_to_be32(
  344. V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE_INV));
  345. else
  346. wqe->send.sendop_pkd = cpu_to_be32(
  347. V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_INV));
  348. wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
  349. break;
  350. default:
  351. return -EINVAL;
  352. }
  353. plen = 0;
  354. if (wr->num_sge) {
  355. if (wr->send_flags & IB_SEND_INLINE) {
  356. ret = build_immd(sq, wqe->send.u.immd_src, wr,
  357. T4_MAX_SEND_INLINE, &plen);
  358. if (ret)
  359. return ret;
  360. size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
  361. plen;
  362. } else {
  363. ret = build_isgl((__be64 *)sq->queue,
  364. (__be64 *)&sq->queue[sq->size],
  365. wqe->send.u.isgl_src,
  366. wr->sg_list, wr->num_sge, &plen);
  367. if (ret)
  368. return ret;
  369. size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
  370. wr->num_sge * sizeof(struct fw_ri_sge);
  371. }
  372. } else {
  373. wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
  374. wqe->send.u.immd_src[0].r1 = 0;
  375. wqe->send.u.immd_src[0].r2 = 0;
  376. wqe->send.u.immd_src[0].immdlen = 0;
  377. size = sizeof wqe->send + sizeof(struct fw_ri_immd);
  378. plen = 0;
  379. }
  380. *len16 = DIV_ROUND_UP(size, 16);
  381. wqe->send.plen = cpu_to_be32(plen);
  382. return 0;
  383. }
  384. static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
  385. struct ib_send_wr *wr, u8 *len16)
  386. {
  387. u32 plen;
  388. int size;
  389. int ret;
  390. if (wr->num_sge > T4_MAX_SEND_SGE)
  391. return -EINVAL;
  392. wqe->write.r2 = 0;
  393. wqe->write.stag_sink = cpu_to_be32(wr->wr.rdma.rkey);
  394. wqe->write.to_sink = cpu_to_be64(wr->wr.rdma.remote_addr);
  395. if (wr->num_sge) {
  396. if (wr->send_flags & IB_SEND_INLINE) {
  397. ret = build_immd(sq, wqe->write.u.immd_src, wr,
  398. T4_MAX_WRITE_INLINE, &plen);
  399. if (ret)
  400. return ret;
  401. size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
  402. plen;
  403. } else {
  404. ret = build_isgl((__be64 *)sq->queue,
  405. (__be64 *)&sq->queue[sq->size],
  406. wqe->write.u.isgl_src,
  407. wr->sg_list, wr->num_sge, &plen);
  408. if (ret)
  409. return ret;
  410. size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
  411. wr->num_sge * sizeof(struct fw_ri_sge);
  412. }
  413. } else {
  414. wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
  415. wqe->write.u.immd_src[0].r1 = 0;
  416. wqe->write.u.immd_src[0].r2 = 0;
  417. wqe->write.u.immd_src[0].immdlen = 0;
  418. size = sizeof wqe->write + sizeof(struct fw_ri_immd);
  419. plen = 0;
  420. }
  421. *len16 = DIV_ROUND_UP(size, 16);
  422. wqe->write.plen = cpu_to_be32(plen);
  423. return 0;
  424. }
  425. static int build_rdma_read(union t4_wr *wqe, struct ib_send_wr *wr, u8 *len16)
  426. {
  427. if (wr->num_sge > 1)
  428. return -EINVAL;
  429. if (wr->num_sge) {
  430. wqe->read.stag_src = cpu_to_be32(wr->wr.rdma.rkey);
  431. wqe->read.to_src_hi = cpu_to_be32((u32)(wr->wr.rdma.remote_addr
  432. >> 32));
  433. wqe->read.to_src_lo = cpu_to_be32((u32)wr->wr.rdma.remote_addr);
  434. wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
  435. wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
  436. wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
  437. >> 32));
  438. wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
  439. } else {
  440. wqe->read.stag_src = cpu_to_be32(2);
  441. wqe->read.to_src_hi = 0;
  442. wqe->read.to_src_lo = 0;
  443. wqe->read.stag_sink = cpu_to_be32(2);
  444. wqe->read.plen = 0;
  445. wqe->read.to_sink_hi = 0;
  446. wqe->read.to_sink_lo = 0;
  447. }
  448. wqe->read.r2 = 0;
  449. wqe->read.r5 = 0;
  450. *len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
  451. return 0;
  452. }
  453. static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
  454. struct ib_recv_wr *wr, u8 *len16)
  455. {
  456. int ret;
  457. ret = build_isgl((__be64 *)qhp->wq.rq.queue,
  458. (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
  459. &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
  460. if (ret)
  461. return ret;
  462. *len16 = DIV_ROUND_UP(sizeof wqe->recv +
  463. wr->num_sge * sizeof(struct fw_ri_sge), 16);
  464. return 0;
  465. }
  466. static int build_fastreg(struct t4_sq *sq, union t4_wr *wqe,
  467. struct ib_send_wr *wr, u8 *len16)
  468. {
  469. struct fw_ri_immd *imdp;
  470. __be64 *p;
  471. int i;
  472. int pbllen = roundup(wr->wr.fast_reg.page_list_len * sizeof(u64), 32);
  473. int rem;
  474. if (wr->wr.fast_reg.page_list_len > T4_MAX_FR_DEPTH)
  475. return -EINVAL;
  476. wqe->fr.qpbinde_to_dcacpu = 0;
  477. wqe->fr.pgsz_shift = wr->wr.fast_reg.page_shift - 12;
  478. wqe->fr.addr_type = FW_RI_VA_BASED_TO;
  479. wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->wr.fast_reg.access_flags);
  480. wqe->fr.len_hi = 0;
  481. wqe->fr.len_lo = cpu_to_be32(wr->wr.fast_reg.length);
  482. wqe->fr.stag = cpu_to_be32(wr->wr.fast_reg.rkey);
  483. wqe->fr.va_hi = cpu_to_be32(wr->wr.fast_reg.iova_start >> 32);
  484. wqe->fr.va_lo_fbo = cpu_to_be32(wr->wr.fast_reg.iova_start &
  485. 0xffffffff);
  486. WARN_ON(pbllen > T4_MAX_FR_IMMD);
  487. imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
  488. imdp->op = FW_RI_DATA_IMMD;
  489. imdp->r1 = 0;
  490. imdp->r2 = 0;
  491. imdp->immdlen = cpu_to_be32(pbllen);
  492. p = (__be64 *)(imdp + 1);
  493. rem = pbllen;
  494. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
  495. *p = cpu_to_be64((u64)wr->wr.fast_reg.page_list->page_list[i]);
  496. rem -= sizeof *p;
  497. if (++p == (__be64 *)&sq->queue[sq->size])
  498. p = (__be64 *)sq->queue;
  499. }
  500. BUG_ON(rem < 0);
  501. while (rem) {
  502. *p = 0;
  503. rem -= sizeof *p;
  504. if (++p == (__be64 *)&sq->queue[sq->size])
  505. p = (__be64 *)sq->queue;
  506. }
  507. *len16 = DIV_ROUND_UP(sizeof wqe->fr + sizeof *imdp + pbllen, 16);
  508. return 0;
  509. }
  510. static int build_inv_stag(union t4_wr *wqe, struct ib_send_wr *wr,
  511. u8 *len16)
  512. {
  513. wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
  514. wqe->inv.r2 = 0;
  515. *len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
  516. return 0;
  517. }
  518. void c4iw_qp_add_ref(struct ib_qp *qp)
  519. {
  520. PDBG("%s ib_qp %p\n", __func__, qp);
  521. atomic_inc(&(to_c4iw_qp(qp)->refcnt));
  522. }
  523. void c4iw_qp_rem_ref(struct ib_qp *qp)
  524. {
  525. PDBG("%s ib_qp %p\n", __func__, qp);
  526. if (atomic_dec_and_test(&(to_c4iw_qp(qp)->refcnt)))
  527. wake_up(&(to_c4iw_qp(qp)->wait));
  528. }
  529. int c4iw_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  530. struct ib_send_wr **bad_wr)
  531. {
  532. int err = 0;
  533. u8 len16 = 0;
  534. enum fw_wr_opcodes fw_opcode = 0;
  535. enum fw_ri_wr_flags fw_flags;
  536. struct c4iw_qp *qhp;
  537. union t4_wr *wqe;
  538. u32 num_wrs;
  539. struct t4_swsqe *swsqe;
  540. unsigned long flag;
  541. u16 idx = 0;
  542. qhp = to_c4iw_qp(ibqp);
  543. spin_lock_irqsave(&qhp->lock, flag);
  544. if (t4_wq_in_error(&qhp->wq)) {
  545. spin_unlock_irqrestore(&qhp->lock, flag);
  546. return -EINVAL;
  547. }
  548. num_wrs = t4_sq_avail(&qhp->wq);
  549. if (num_wrs == 0) {
  550. spin_unlock_irqrestore(&qhp->lock, flag);
  551. return -ENOMEM;
  552. }
  553. while (wr) {
  554. if (num_wrs == 0) {
  555. err = -ENOMEM;
  556. *bad_wr = wr;
  557. break;
  558. }
  559. wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
  560. qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
  561. fw_flags = 0;
  562. if (wr->send_flags & IB_SEND_SOLICITED)
  563. fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
  564. if (wr->send_flags & IB_SEND_SIGNALED)
  565. fw_flags |= FW_RI_COMPLETION_FLAG;
  566. swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
  567. switch (wr->opcode) {
  568. case IB_WR_SEND_WITH_INV:
  569. case IB_WR_SEND:
  570. if (wr->send_flags & IB_SEND_FENCE)
  571. fw_flags |= FW_RI_READ_FENCE_FLAG;
  572. fw_opcode = FW_RI_SEND_WR;
  573. if (wr->opcode == IB_WR_SEND)
  574. swsqe->opcode = FW_RI_SEND;
  575. else
  576. swsqe->opcode = FW_RI_SEND_WITH_INV;
  577. err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
  578. break;
  579. case IB_WR_RDMA_WRITE:
  580. fw_opcode = FW_RI_RDMA_WRITE_WR;
  581. swsqe->opcode = FW_RI_RDMA_WRITE;
  582. err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
  583. break;
  584. case IB_WR_RDMA_READ:
  585. case IB_WR_RDMA_READ_WITH_INV:
  586. fw_opcode = FW_RI_RDMA_READ_WR;
  587. swsqe->opcode = FW_RI_READ_REQ;
  588. if (wr->opcode == IB_WR_RDMA_READ_WITH_INV)
  589. fw_flags = FW_RI_RDMA_READ_INVALIDATE;
  590. else
  591. fw_flags = 0;
  592. err = build_rdma_read(wqe, wr, &len16);
  593. if (err)
  594. break;
  595. swsqe->read_len = wr->sg_list[0].length;
  596. if (!qhp->wq.sq.oldest_read)
  597. qhp->wq.sq.oldest_read = swsqe;
  598. break;
  599. case IB_WR_FAST_REG_MR:
  600. fw_opcode = FW_RI_FR_NSMR_WR;
  601. swsqe->opcode = FW_RI_FAST_REGISTER;
  602. err = build_fastreg(&qhp->wq.sq, wqe, wr, &len16);
  603. break;
  604. case IB_WR_LOCAL_INV:
  605. if (wr->send_flags & IB_SEND_FENCE)
  606. fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
  607. fw_opcode = FW_RI_INV_LSTAG_WR;
  608. swsqe->opcode = FW_RI_LOCAL_INV;
  609. err = build_inv_stag(wqe, wr, &len16);
  610. break;
  611. default:
  612. PDBG("%s post of type=%d TBD!\n", __func__,
  613. wr->opcode);
  614. err = -EINVAL;
  615. }
  616. if (err) {
  617. *bad_wr = wr;
  618. break;
  619. }
  620. swsqe->idx = qhp->wq.sq.pidx;
  621. swsqe->complete = 0;
  622. swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED);
  623. swsqe->wr_id = wr->wr_id;
  624. init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
  625. PDBG("%s cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
  626. __func__, (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
  627. swsqe->opcode, swsqe->read_len);
  628. wr = wr->next;
  629. num_wrs--;
  630. t4_sq_produce(&qhp->wq, len16);
  631. idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
  632. }
  633. if (t4_wq_db_enabled(&qhp->wq))
  634. t4_ring_sq_db(&qhp->wq, idx);
  635. spin_unlock_irqrestore(&qhp->lock, flag);
  636. return err;
  637. }
  638. int c4iw_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  639. struct ib_recv_wr **bad_wr)
  640. {
  641. int err = 0;
  642. struct c4iw_qp *qhp;
  643. union t4_recv_wr *wqe;
  644. u32 num_wrs;
  645. u8 len16 = 0;
  646. unsigned long flag;
  647. u16 idx = 0;
  648. qhp = to_c4iw_qp(ibqp);
  649. spin_lock_irqsave(&qhp->lock, flag);
  650. if (t4_wq_in_error(&qhp->wq)) {
  651. spin_unlock_irqrestore(&qhp->lock, flag);
  652. return -EINVAL;
  653. }
  654. num_wrs = t4_rq_avail(&qhp->wq);
  655. if (num_wrs == 0) {
  656. spin_unlock_irqrestore(&qhp->lock, flag);
  657. return -ENOMEM;
  658. }
  659. while (wr) {
  660. if (wr->num_sge > T4_MAX_RECV_SGE) {
  661. err = -EINVAL;
  662. *bad_wr = wr;
  663. break;
  664. }
  665. wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
  666. qhp->wq.rq.wq_pidx *
  667. T4_EQ_ENTRY_SIZE);
  668. if (num_wrs)
  669. err = build_rdma_recv(qhp, wqe, wr, &len16);
  670. else
  671. err = -ENOMEM;
  672. if (err) {
  673. *bad_wr = wr;
  674. break;
  675. }
  676. qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
  677. wqe->recv.opcode = FW_RI_RECV_WR;
  678. wqe->recv.r1 = 0;
  679. wqe->recv.wrid = qhp->wq.rq.pidx;
  680. wqe->recv.r2[0] = 0;
  681. wqe->recv.r2[1] = 0;
  682. wqe->recv.r2[2] = 0;
  683. wqe->recv.len16 = len16;
  684. PDBG("%s cookie 0x%llx pidx %u\n", __func__,
  685. (unsigned long long) wr->wr_id, qhp->wq.rq.pidx);
  686. t4_rq_produce(&qhp->wq, len16);
  687. idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
  688. wr = wr->next;
  689. num_wrs--;
  690. }
  691. if (t4_wq_db_enabled(&qhp->wq))
  692. t4_ring_rq_db(&qhp->wq, idx);
  693. spin_unlock_irqrestore(&qhp->lock, flag);
  694. return err;
  695. }
  696. int c4iw_bind_mw(struct ib_qp *qp, struct ib_mw *mw, struct ib_mw_bind *mw_bind)
  697. {
  698. return -ENOSYS;
  699. }
  700. static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
  701. u8 *ecode)
  702. {
  703. int status;
  704. int tagged;
  705. int opcode;
  706. int rqtype;
  707. int send_inv;
  708. if (!err_cqe) {
  709. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  710. *ecode = 0;
  711. return;
  712. }
  713. status = CQE_STATUS(err_cqe);
  714. opcode = CQE_OPCODE(err_cqe);
  715. rqtype = RQ_TYPE(err_cqe);
  716. send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
  717. (opcode == FW_RI_SEND_WITH_SE_INV);
  718. tagged = (opcode == FW_RI_RDMA_WRITE) ||
  719. (rqtype && (opcode == FW_RI_READ_RESP));
  720. switch (status) {
  721. case T4_ERR_STAG:
  722. if (send_inv) {
  723. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  724. *ecode = RDMAP_CANT_INV_STAG;
  725. } else {
  726. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  727. *ecode = RDMAP_INV_STAG;
  728. }
  729. break;
  730. case T4_ERR_PDID:
  731. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  732. if ((opcode == FW_RI_SEND_WITH_INV) ||
  733. (opcode == FW_RI_SEND_WITH_SE_INV))
  734. *ecode = RDMAP_CANT_INV_STAG;
  735. else
  736. *ecode = RDMAP_STAG_NOT_ASSOC;
  737. break;
  738. case T4_ERR_QPID:
  739. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  740. *ecode = RDMAP_STAG_NOT_ASSOC;
  741. break;
  742. case T4_ERR_ACCESS:
  743. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  744. *ecode = RDMAP_ACC_VIOL;
  745. break;
  746. case T4_ERR_WRAP:
  747. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  748. *ecode = RDMAP_TO_WRAP;
  749. break;
  750. case T4_ERR_BOUND:
  751. if (tagged) {
  752. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  753. *ecode = DDPT_BASE_BOUNDS;
  754. } else {
  755. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  756. *ecode = RDMAP_BASE_BOUNDS;
  757. }
  758. break;
  759. case T4_ERR_INVALIDATE_SHARED_MR:
  760. case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  761. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  762. *ecode = RDMAP_CANT_INV_STAG;
  763. break;
  764. case T4_ERR_ECC:
  765. case T4_ERR_ECC_PSTAG:
  766. case T4_ERR_INTERNAL_ERR:
  767. *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
  768. *ecode = 0;
  769. break;
  770. case T4_ERR_OUT_OF_RQE:
  771. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  772. *ecode = DDPU_INV_MSN_NOBUF;
  773. break;
  774. case T4_ERR_PBL_ADDR_BOUND:
  775. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  776. *ecode = DDPT_BASE_BOUNDS;
  777. break;
  778. case T4_ERR_CRC:
  779. *layer_type = LAYER_MPA|DDP_LLP;
  780. *ecode = MPA_CRC_ERR;
  781. break;
  782. case T4_ERR_MARKER:
  783. *layer_type = LAYER_MPA|DDP_LLP;
  784. *ecode = MPA_MARKER_ERR;
  785. break;
  786. case T4_ERR_PDU_LEN_ERR:
  787. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  788. *ecode = DDPU_MSG_TOOBIG;
  789. break;
  790. case T4_ERR_DDP_VERSION:
  791. if (tagged) {
  792. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  793. *ecode = DDPT_INV_VERS;
  794. } else {
  795. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  796. *ecode = DDPU_INV_VERS;
  797. }
  798. break;
  799. case T4_ERR_RDMA_VERSION:
  800. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  801. *ecode = RDMAP_INV_VERS;
  802. break;
  803. case T4_ERR_OPCODE:
  804. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  805. *ecode = RDMAP_INV_OPCODE;
  806. break;
  807. case T4_ERR_DDP_QUEUE_NUM:
  808. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  809. *ecode = DDPU_INV_QN;
  810. break;
  811. case T4_ERR_MSN:
  812. case T4_ERR_MSN_GAP:
  813. case T4_ERR_MSN_RANGE:
  814. case T4_ERR_IRD_OVERFLOW:
  815. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  816. *ecode = DDPU_INV_MSN_RANGE;
  817. break;
  818. case T4_ERR_TBIT:
  819. *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
  820. *ecode = 0;
  821. break;
  822. case T4_ERR_MO:
  823. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  824. *ecode = DDPU_INV_MO;
  825. break;
  826. default:
  827. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  828. *ecode = 0;
  829. break;
  830. }
  831. }
  832. static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
  833. gfp_t gfp)
  834. {
  835. struct fw_ri_wr *wqe;
  836. struct sk_buff *skb;
  837. struct terminate_message *term;
  838. PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
  839. qhp->ep->hwtid);
  840. skb = alloc_skb(sizeof *wqe, gfp);
  841. if (!skb)
  842. return;
  843. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  844. wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
  845. memset(wqe, 0, sizeof *wqe);
  846. wqe->op_compl = cpu_to_be32(FW_WR_OP(FW_RI_INIT_WR));
  847. wqe->flowid_len16 = cpu_to_be32(
  848. FW_WR_FLOWID(qhp->ep->hwtid) |
  849. FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
  850. wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
  851. wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
  852. term = (struct terminate_message *)wqe->u.terminate.termmsg;
  853. if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
  854. term->layer_etype = qhp->attr.layer_etype;
  855. term->ecode = qhp->attr.ecode;
  856. } else
  857. build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
  858. c4iw_ofld_send(&qhp->rhp->rdev, skb);
  859. }
  860. /*
  861. * Assumes qhp lock is held.
  862. */
  863. static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
  864. struct c4iw_cq *schp)
  865. {
  866. int count;
  867. int flushed;
  868. unsigned long flag;
  869. PDBG("%s qhp %p rchp %p schp %p\n", __func__, qhp, rchp, schp);
  870. /* locking hierarchy: cq lock first, then qp lock. */
  871. spin_lock_irqsave(&rchp->lock, flag);
  872. spin_lock(&qhp->lock);
  873. c4iw_flush_hw_cq(&rchp->cq);
  874. c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
  875. flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
  876. spin_unlock(&qhp->lock);
  877. spin_unlock_irqrestore(&rchp->lock, flag);
  878. if (flushed) {
  879. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  880. (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
  881. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  882. }
  883. /* locking hierarchy: cq lock first, then qp lock. */
  884. spin_lock_irqsave(&schp->lock, flag);
  885. spin_lock(&qhp->lock);
  886. c4iw_flush_hw_cq(&schp->cq);
  887. c4iw_count_scqes(&schp->cq, &qhp->wq, &count);
  888. flushed = c4iw_flush_sq(&qhp->wq, &schp->cq, count);
  889. spin_unlock(&qhp->lock);
  890. spin_unlock_irqrestore(&schp->lock, flag);
  891. if (flushed) {
  892. spin_lock_irqsave(&schp->comp_handler_lock, flag);
  893. (*schp->ibcq.comp_handler)(&schp->ibcq, schp->ibcq.cq_context);
  894. spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
  895. }
  896. }
  897. static void flush_qp(struct c4iw_qp *qhp)
  898. {
  899. struct c4iw_cq *rchp, *schp;
  900. unsigned long flag;
  901. rchp = get_chp(qhp->rhp, qhp->attr.rcq);
  902. schp = get_chp(qhp->rhp, qhp->attr.scq);
  903. if (qhp->ibqp.uobject) {
  904. t4_set_wq_in_error(&qhp->wq);
  905. t4_set_cq_in_error(&rchp->cq);
  906. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  907. (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
  908. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  909. if (schp != rchp) {
  910. t4_set_cq_in_error(&schp->cq);
  911. spin_lock_irqsave(&schp->comp_handler_lock, flag);
  912. (*schp->ibcq.comp_handler)(&schp->ibcq,
  913. schp->ibcq.cq_context);
  914. spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
  915. }
  916. return;
  917. }
  918. __flush_qp(qhp, rchp, schp);
  919. }
  920. static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
  921. struct c4iw_ep *ep)
  922. {
  923. struct fw_ri_wr *wqe;
  924. int ret;
  925. struct sk_buff *skb;
  926. PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
  927. ep->hwtid);
  928. skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
  929. if (!skb)
  930. return -ENOMEM;
  931. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  932. wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
  933. memset(wqe, 0, sizeof *wqe);
  934. wqe->op_compl = cpu_to_be32(
  935. FW_WR_OP(FW_RI_INIT_WR) |
  936. FW_WR_COMPL(1));
  937. wqe->flowid_len16 = cpu_to_be32(
  938. FW_WR_FLOWID(ep->hwtid) |
  939. FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
  940. wqe->cookie = (unsigned long) &ep->com.wr_wait;
  941. wqe->u.fini.type = FW_RI_TYPE_FINI;
  942. ret = c4iw_ofld_send(&rhp->rdev, skb);
  943. if (ret)
  944. goto out;
  945. ret = c4iw_wait_for_reply(&rhp->rdev, &ep->com.wr_wait, qhp->ep->hwtid,
  946. qhp->wq.sq.qid, __func__);
  947. out:
  948. PDBG("%s ret %d\n", __func__, ret);
  949. return ret;
  950. }
  951. static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
  952. {
  953. PDBG("%s p2p_type = %d\n", __func__, p2p_type);
  954. memset(&init->u, 0, sizeof init->u);
  955. switch (p2p_type) {
  956. case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
  957. init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
  958. init->u.write.stag_sink = cpu_to_be32(1);
  959. init->u.write.to_sink = cpu_to_be64(1);
  960. init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
  961. init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
  962. sizeof(struct fw_ri_immd),
  963. 16);
  964. break;
  965. case FW_RI_INIT_P2PTYPE_READ_REQ:
  966. init->u.write.opcode = FW_RI_RDMA_READ_WR;
  967. init->u.read.stag_src = cpu_to_be32(1);
  968. init->u.read.to_src_lo = cpu_to_be32(1);
  969. init->u.read.stag_sink = cpu_to_be32(1);
  970. init->u.read.to_sink_lo = cpu_to_be32(1);
  971. init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
  972. break;
  973. }
  974. }
  975. static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
  976. {
  977. struct fw_ri_wr *wqe;
  978. int ret;
  979. struct sk_buff *skb;
  980. PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
  981. qhp->ep->hwtid);
  982. skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
  983. if (!skb)
  984. return -ENOMEM;
  985. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  986. wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
  987. memset(wqe, 0, sizeof *wqe);
  988. wqe->op_compl = cpu_to_be32(
  989. FW_WR_OP(FW_RI_INIT_WR) |
  990. FW_WR_COMPL(1));
  991. wqe->flowid_len16 = cpu_to_be32(
  992. FW_WR_FLOWID(qhp->ep->hwtid) |
  993. FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
  994. wqe->cookie = (unsigned long) &qhp->ep->com.wr_wait;
  995. wqe->u.init.type = FW_RI_TYPE_INIT;
  996. wqe->u.init.mpareqbit_p2ptype =
  997. V_FW_RI_WR_MPAREQBIT(qhp->attr.mpa_attr.initiator) |
  998. V_FW_RI_WR_P2PTYPE(qhp->attr.mpa_attr.p2p_type);
  999. wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
  1000. if (qhp->attr.mpa_attr.recv_marker_enabled)
  1001. wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
  1002. if (qhp->attr.mpa_attr.xmit_marker_enabled)
  1003. wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
  1004. if (qhp->attr.mpa_attr.crc_enabled)
  1005. wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
  1006. wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
  1007. FW_RI_QP_RDMA_WRITE_ENABLE |
  1008. FW_RI_QP_BIND_ENABLE;
  1009. if (!qhp->ibqp.uobject)
  1010. wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
  1011. FW_RI_QP_STAG0_ENABLE;
  1012. wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
  1013. wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
  1014. wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
  1015. wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
  1016. wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
  1017. wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
  1018. wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
  1019. wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
  1020. wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
  1021. wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
  1022. wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
  1023. wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
  1024. wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
  1025. rhp->rdev.lldi.vr->rq.start);
  1026. if (qhp->attr.mpa_attr.initiator)
  1027. build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
  1028. ret = c4iw_ofld_send(&rhp->rdev, skb);
  1029. if (ret)
  1030. goto out;
  1031. ret = c4iw_wait_for_reply(&rhp->rdev, &qhp->ep->com.wr_wait,
  1032. qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
  1033. out:
  1034. PDBG("%s ret %d\n", __func__, ret);
  1035. return ret;
  1036. }
  1037. int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
  1038. enum c4iw_qp_attr_mask mask,
  1039. struct c4iw_qp_attributes *attrs,
  1040. int internal)
  1041. {
  1042. int ret = 0;
  1043. struct c4iw_qp_attributes newattr = qhp->attr;
  1044. int disconnect = 0;
  1045. int terminate = 0;
  1046. int abort = 0;
  1047. int free = 0;
  1048. struct c4iw_ep *ep = NULL;
  1049. PDBG("%s qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n", __func__,
  1050. qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
  1051. (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
  1052. mutex_lock(&qhp->mutex);
  1053. /* Process attr changes if in IDLE */
  1054. if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
  1055. if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
  1056. ret = -EIO;
  1057. goto out;
  1058. }
  1059. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
  1060. newattr.enable_rdma_read = attrs->enable_rdma_read;
  1061. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
  1062. newattr.enable_rdma_write = attrs->enable_rdma_write;
  1063. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
  1064. newattr.enable_bind = attrs->enable_bind;
  1065. if (mask & C4IW_QP_ATTR_MAX_ORD) {
  1066. if (attrs->max_ord > c4iw_max_read_depth) {
  1067. ret = -EINVAL;
  1068. goto out;
  1069. }
  1070. newattr.max_ord = attrs->max_ord;
  1071. }
  1072. if (mask & C4IW_QP_ATTR_MAX_IRD) {
  1073. if (attrs->max_ird > c4iw_max_read_depth) {
  1074. ret = -EINVAL;
  1075. goto out;
  1076. }
  1077. newattr.max_ird = attrs->max_ird;
  1078. }
  1079. qhp->attr = newattr;
  1080. }
  1081. if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
  1082. goto out;
  1083. if (qhp->attr.state == attrs->next_state)
  1084. goto out;
  1085. switch (qhp->attr.state) {
  1086. case C4IW_QP_STATE_IDLE:
  1087. switch (attrs->next_state) {
  1088. case C4IW_QP_STATE_RTS:
  1089. if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
  1090. ret = -EINVAL;
  1091. goto out;
  1092. }
  1093. if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
  1094. ret = -EINVAL;
  1095. goto out;
  1096. }
  1097. qhp->attr.mpa_attr = attrs->mpa_attr;
  1098. qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
  1099. qhp->ep = qhp->attr.llp_stream_handle;
  1100. set_state(qhp, C4IW_QP_STATE_RTS);
  1101. /*
  1102. * Ref the endpoint here and deref when we
  1103. * disassociate the endpoint from the QP. This
  1104. * happens in CLOSING->IDLE transition or *->ERROR
  1105. * transition.
  1106. */
  1107. c4iw_get_ep(&qhp->ep->com);
  1108. ret = rdma_init(rhp, qhp);
  1109. if (ret)
  1110. goto err;
  1111. break;
  1112. case C4IW_QP_STATE_ERROR:
  1113. set_state(qhp, C4IW_QP_STATE_ERROR);
  1114. flush_qp(qhp);
  1115. break;
  1116. default:
  1117. ret = -EINVAL;
  1118. goto out;
  1119. }
  1120. break;
  1121. case C4IW_QP_STATE_RTS:
  1122. switch (attrs->next_state) {
  1123. case C4IW_QP_STATE_CLOSING:
  1124. BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
  1125. set_state(qhp, C4IW_QP_STATE_CLOSING);
  1126. ep = qhp->ep;
  1127. if (!internal) {
  1128. abort = 0;
  1129. disconnect = 1;
  1130. c4iw_get_ep(&qhp->ep->com);
  1131. }
  1132. if (qhp->ibqp.uobject)
  1133. t4_set_wq_in_error(&qhp->wq);
  1134. ret = rdma_fini(rhp, qhp, ep);
  1135. if (ret)
  1136. goto err;
  1137. break;
  1138. case C4IW_QP_STATE_TERMINATE:
  1139. set_state(qhp, C4IW_QP_STATE_TERMINATE);
  1140. qhp->attr.layer_etype = attrs->layer_etype;
  1141. qhp->attr.ecode = attrs->ecode;
  1142. if (qhp->ibqp.uobject)
  1143. t4_set_wq_in_error(&qhp->wq);
  1144. ep = qhp->ep;
  1145. if (!internal)
  1146. terminate = 1;
  1147. disconnect = 1;
  1148. c4iw_get_ep(&qhp->ep->com);
  1149. break;
  1150. case C4IW_QP_STATE_ERROR:
  1151. set_state(qhp, C4IW_QP_STATE_ERROR);
  1152. if (qhp->ibqp.uobject)
  1153. t4_set_wq_in_error(&qhp->wq);
  1154. if (!internal) {
  1155. abort = 1;
  1156. disconnect = 1;
  1157. ep = qhp->ep;
  1158. c4iw_get_ep(&qhp->ep->com);
  1159. }
  1160. goto err;
  1161. break;
  1162. default:
  1163. ret = -EINVAL;
  1164. goto out;
  1165. }
  1166. break;
  1167. case C4IW_QP_STATE_CLOSING:
  1168. if (!internal) {
  1169. ret = -EINVAL;
  1170. goto out;
  1171. }
  1172. switch (attrs->next_state) {
  1173. case C4IW_QP_STATE_IDLE:
  1174. flush_qp(qhp);
  1175. set_state(qhp, C4IW_QP_STATE_IDLE);
  1176. qhp->attr.llp_stream_handle = NULL;
  1177. c4iw_put_ep(&qhp->ep->com);
  1178. qhp->ep = NULL;
  1179. wake_up(&qhp->wait);
  1180. break;
  1181. case C4IW_QP_STATE_ERROR:
  1182. goto err;
  1183. default:
  1184. ret = -EINVAL;
  1185. goto err;
  1186. }
  1187. break;
  1188. case C4IW_QP_STATE_ERROR:
  1189. if (attrs->next_state != C4IW_QP_STATE_IDLE) {
  1190. ret = -EINVAL;
  1191. goto out;
  1192. }
  1193. if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
  1194. ret = -EINVAL;
  1195. goto out;
  1196. }
  1197. set_state(qhp, C4IW_QP_STATE_IDLE);
  1198. break;
  1199. case C4IW_QP_STATE_TERMINATE:
  1200. if (!internal) {
  1201. ret = -EINVAL;
  1202. goto out;
  1203. }
  1204. goto err;
  1205. break;
  1206. default:
  1207. printk(KERN_ERR "%s in a bad state %d\n",
  1208. __func__, qhp->attr.state);
  1209. ret = -EINVAL;
  1210. goto err;
  1211. break;
  1212. }
  1213. goto out;
  1214. err:
  1215. PDBG("%s disassociating ep %p qpid 0x%x\n", __func__, qhp->ep,
  1216. qhp->wq.sq.qid);
  1217. /* disassociate the LLP connection */
  1218. qhp->attr.llp_stream_handle = NULL;
  1219. if (!ep)
  1220. ep = qhp->ep;
  1221. qhp->ep = NULL;
  1222. set_state(qhp, C4IW_QP_STATE_ERROR);
  1223. free = 1;
  1224. wake_up(&qhp->wait);
  1225. BUG_ON(!ep);
  1226. flush_qp(qhp);
  1227. out:
  1228. mutex_unlock(&qhp->mutex);
  1229. if (terminate)
  1230. post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
  1231. /*
  1232. * If disconnect is 1, then we need to initiate a disconnect
  1233. * on the EP. This can be a normal close (RTS->CLOSING) or
  1234. * an abnormal close (RTS/CLOSING->ERROR).
  1235. */
  1236. if (disconnect) {
  1237. c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
  1238. GFP_KERNEL);
  1239. c4iw_put_ep(&ep->com);
  1240. }
  1241. /*
  1242. * If free is 1, then we've disassociated the EP from the QP
  1243. * and we need to dereference the EP.
  1244. */
  1245. if (free)
  1246. c4iw_put_ep(&ep->com);
  1247. PDBG("%s exit state %d\n", __func__, qhp->attr.state);
  1248. return ret;
  1249. }
  1250. int c4iw_destroy_qp(struct ib_qp *ib_qp)
  1251. {
  1252. struct c4iw_dev *rhp;
  1253. struct c4iw_qp *qhp;
  1254. struct c4iw_qp_attributes attrs;
  1255. struct c4iw_ucontext *ucontext;
  1256. qhp = to_c4iw_qp(ib_qp);
  1257. rhp = qhp->rhp;
  1258. attrs.next_state = C4IW_QP_STATE_ERROR;
  1259. if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
  1260. c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  1261. else
  1262. c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
  1263. wait_event(qhp->wait, !qhp->ep);
  1264. remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
  1265. atomic_dec(&qhp->refcnt);
  1266. wait_event(qhp->wait, !atomic_read(&qhp->refcnt));
  1267. ucontext = ib_qp->uobject ?
  1268. to_c4iw_ucontext(ib_qp->uobject->context) : NULL;
  1269. destroy_qp(&rhp->rdev, &qhp->wq,
  1270. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  1271. PDBG("%s ib_qp %p qpid 0x%0x\n", __func__, ib_qp, qhp->wq.sq.qid);
  1272. kfree(qhp);
  1273. return 0;
  1274. }
  1275. struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
  1276. struct ib_udata *udata)
  1277. {
  1278. struct c4iw_dev *rhp;
  1279. struct c4iw_qp *qhp;
  1280. struct c4iw_pd *php;
  1281. struct c4iw_cq *schp;
  1282. struct c4iw_cq *rchp;
  1283. struct c4iw_create_qp_resp uresp;
  1284. int sqsize, rqsize;
  1285. struct c4iw_ucontext *ucontext;
  1286. int ret;
  1287. struct c4iw_mm_entry *mm1, *mm2, *mm3, *mm4, *mm5 = NULL;
  1288. PDBG("%s ib_pd %p\n", __func__, pd);
  1289. if (attrs->qp_type != IB_QPT_RC)
  1290. return ERR_PTR(-EINVAL);
  1291. php = to_c4iw_pd(pd);
  1292. rhp = php->rhp;
  1293. schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
  1294. rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
  1295. if (!schp || !rchp)
  1296. return ERR_PTR(-EINVAL);
  1297. if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
  1298. return ERR_PTR(-EINVAL);
  1299. rqsize = roundup(attrs->cap.max_recv_wr + 1, 16);
  1300. if (rqsize > T4_MAX_RQ_SIZE)
  1301. return ERR_PTR(-E2BIG);
  1302. sqsize = roundup(attrs->cap.max_send_wr + 1, 16);
  1303. if (sqsize > T4_MAX_SQ_SIZE)
  1304. return ERR_PTR(-E2BIG);
  1305. ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
  1306. qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
  1307. if (!qhp)
  1308. return ERR_PTR(-ENOMEM);
  1309. qhp->wq.sq.size = sqsize;
  1310. qhp->wq.sq.memsize = (sqsize + 1) * sizeof *qhp->wq.sq.queue;
  1311. qhp->wq.rq.size = rqsize;
  1312. qhp->wq.rq.memsize = (rqsize + 1) * sizeof *qhp->wq.rq.queue;
  1313. if (ucontext) {
  1314. qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
  1315. qhp->wq.rq.memsize = roundup(qhp->wq.rq.memsize, PAGE_SIZE);
  1316. }
  1317. PDBG("%s sqsize %u sqmemsize %zu rqsize %u rqmemsize %zu\n",
  1318. __func__, sqsize, qhp->wq.sq.memsize, rqsize, qhp->wq.rq.memsize);
  1319. ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
  1320. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  1321. if (ret)
  1322. goto err1;
  1323. attrs->cap.max_recv_wr = rqsize - 1;
  1324. attrs->cap.max_send_wr = sqsize - 1;
  1325. attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
  1326. qhp->rhp = rhp;
  1327. qhp->attr.pd = php->pdid;
  1328. qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
  1329. qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
  1330. qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
  1331. qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
  1332. qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
  1333. qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
  1334. qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
  1335. qhp->attr.state = C4IW_QP_STATE_IDLE;
  1336. qhp->attr.next_state = C4IW_QP_STATE_IDLE;
  1337. qhp->attr.enable_rdma_read = 1;
  1338. qhp->attr.enable_rdma_write = 1;
  1339. qhp->attr.enable_bind = 1;
  1340. qhp->attr.max_ord = 1;
  1341. qhp->attr.max_ird = 1;
  1342. spin_lock_init(&qhp->lock);
  1343. mutex_init(&qhp->mutex);
  1344. init_waitqueue_head(&qhp->wait);
  1345. atomic_set(&qhp->refcnt, 1);
  1346. ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
  1347. if (ret)
  1348. goto err2;
  1349. if (udata) {
  1350. mm1 = kmalloc(sizeof *mm1, GFP_KERNEL);
  1351. if (!mm1) {
  1352. ret = -ENOMEM;
  1353. goto err3;
  1354. }
  1355. mm2 = kmalloc(sizeof *mm2, GFP_KERNEL);
  1356. if (!mm2) {
  1357. ret = -ENOMEM;
  1358. goto err4;
  1359. }
  1360. mm3 = kmalloc(sizeof *mm3, GFP_KERNEL);
  1361. if (!mm3) {
  1362. ret = -ENOMEM;
  1363. goto err5;
  1364. }
  1365. mm4 = kmalloc(sizeof *mm4, GFP_KERNEL);
  1366. if (!mm4) {
  1367. ret = -ENOMEM;
  1368. goto err6;
  1369. }
  1370. if (t4_sq_onchip(&qhp->wq.sq)) {
  1371. mm5 = kmalloc(sizeof *mm5, GFP_KERNEL);
  1372. if (!mm5) {
  1373. ret = -ENOMEM;
  1374. goto err7;
  1375. }
  1376. uresp.flags = C4IW_QPF_ONCHIP;
  1377. } else
  1378. uresp.flags = 0;
  1379. uresp.qid_mask = rhp->rdev.qpmask;
  1380. uresp.sqid = qhp->wq.sq.qid;
  1381. uresp.sq_size = qhp->wq.sq.size;
  1382. uresp.sq_memsize = qhp->wq.sq.memsize;
  1383. uresp.rqid = qhp->wq.rq.qid;
  1384. uresp.rq_size = qhp->wq.rq.size;
  1385. uresp.rq_memsize = qhp->wq.rq.memsize;
  1386. spin_lock(&ucontext->mmap_lock);
  1387. if (mm5) {
  1388. uresp.ma_sync_key = ucontext->key;
  1389. ucontext->key += PAGE_SIZE;
  1390. }
  1391. uresp.sq_key = ucontext->key;
  1392. ucontext->key += PAGE_SIZE;
  1393. uresp.rq_key = ucontext->key;
  1394. ucontext->key += PAGE_SIZE;
  1395. uresp.sq_db_gts_key = ucontext->key;
  1396. ucontext->key += PAGE_SIZE;
  1397. uresp.rq_db_gts_key = ucontext->key;
  1398. ucontext->key += PAGE_SIZE;
  1399. spin_unlock(&ucontext->mmap_lock);
  1400. ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
  1401. if (ret)
  1402. goto err8;
  1403. mm1->key = uresp.sq_key;
  1404. mm1->addr = qhp->wq.sq.phys_addr;
  1405. mm1->len = PAGE_ALIGN(qhp->wq.sq.memsize);
  1406. insert_mmap(ucontext, mm1);
  1407. mm2->key = uresp.rq_key;
  1408. mm2->addr = virt_to_phys(qhp->wq.rq.queue);
  1409. mm2->len = PAGE_ALIGN(qhp->wq.rq.memsize);
  1410. insert_mmap(ucontext, mm2);
  1411. mm3->key = uresp.sq_db_gts_key;
  1412. mm3->addr = qhp->wq.sq.udb;
  1413. mm3->len = PAGE_SIZE;
  1414. insert_mmap(ucontext, mm3);
  1415. mm4->key = uresp.rq_db_gts_key;
  1416. mm4->addr = qhp->wq.rq.udb;
  1417. mm4->len = PAGE_SIZE;
  1418. insert_mmap(ucontext, mm4);
  1419. if (mm5) {
  1420. mm5->key = uresp.ma_sync_key;
  1421. mm5->addr = (pci_resource_start(rhp->rdev.lldi.pdev, 0)
  1422. + A_PCIE_MA_SYNC) & PAGE_MASK;
  1423. mm5->len = PAGE_SIZE;
  1424. insert_mmap(ucontext, mm5);
  1425. }
  1426. }
  1427. qhp->ibqp.qp_num = qhp->wq.sq.qid;
  1428. init_timer(&(qhp->timer));
  1429. PDBG("%s qhp %p sq_num_entries %d, rq_num_entries %d qpid 0x%0x\n",
  1430. __func__, qhp, qhp->attr.sq_num_entries, qhp->attr.rq_num_entries,
  1431. qhp->wq.sq.qid);
  1432. return &qhp->ibqp;
  1433. err8:
  1434. kfree(mm5);
  1435. err7:
  1436. kfree(mm4);
  1437. err6:
  1438. kfree(mm3);
  1439. err5:
  1440. kfree(mm2);
  1441. err4:
  1442. kfree(mm1);
  1443. err3:
  1444. remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
  1445. err2:
  1446. destroy_qp(&rhp->rdev, &qhp->wq,
  1447. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  1448. err1:
  1449. kfree(qhp);
  1450. return ERR_PTR(ret);
  1451. }
  1452. int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1453. int attr_mask, struct ib_udata *udata)
  1454. {
  1455. struct c4iw_dev *rhp;
  1456. struct c4iw_qp *qhp;
  1457. enum c4iw_qp_attr_mask mask = 0;
  1458. struct c4iw_qp_attributes attrs;
  1459. PDBG("%s ib_qp %p\n", __func__, ibqp);
  1460. /* iwarp does not support the RTR state */
  1461. if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
  1462. attr_mask &= ~IB_QP_STATE;
  1463. /* Make sure we still have something left to do */
  1464. if (!attr_mask)
  1465. return 0;
  1466. memset(&attrs, 0, sizeof attrs);
  1467. qhp = to_c4iw_qp(ibqp);
  1468. rhp = qhp->rhp;
  1469. attrs.next_state = c4iw_convert_state(attr->qp_state);
  1470. attrs.enable_rdma_read = (attr->qp_access_flags &
  1471. IB_ACCESS_REMOTE_READ) ? 1 : 0;
  1472. attrs.enable_rdma_write = (attr->qp_access_flags &
  1473. IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  1474. attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
  1475. mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
  1476. mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
  1477. (C4IW_QP_ATTR_ENABLE_RDMA_READ |
  1478. C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
  1479. C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
  1480. return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
  1481. }
  1482. struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
  1483. {
  1484. PDBG("%s ib_dev %p qpn 0x%x\n", __func__, dev, qpn);
  1485. return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
  1486. }