qp.c 48 KB

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