qp.c 44 KB

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