qp.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  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 int alloc_ird(struct c4iw_dev *dev, u32 ird)
  54. {
  55. int ret = 0;
  56. spin_lock_irq(&dev->lock);
  57. if (ird <= dev->avail_ird)
  58. dev->avail_ird -= ird;
  59. else
  60. ret = -ENOMEM;
  61. spin_unlock_irq(&dev->lock);
  62. if (ret)
  63. dev_warn(&dev->rdev.lldi.pdev->dev,
  64. "device IRD resources exhausted\n");
  65. return ret;
  66. }
  67. static void free_ird(struct c4iw_dev *dev, int ird)
  68. {
  69. spin_lock_irq(&dev->lock);
  70. dev->avail_ird += ird;
  71. spin_unlock_irq(&dev->lock);
  72. }
  73. static void set_state(struct c4iw_qp *qhp, enum c4iw_qp_state state)
  74. {
  75. unsigned long flag;
  76. spin_lock_irqsave(&qhp->lock, flag);
  77. qhp->attr.state = state;
  78. spin_unlock_irqrestore(&qhp->lock, flag);
  79. }
  80. static void dealloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  81. {
  82. c4iw_ocqp_pool_free(rdev, sq->dma_addr, sq->memsize);
  83. }
  84. static void dealloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  85. {
  86. dma_free_coherent(&(rdev->lldi.pdev->dev), sq->memsize, sq->queue,
  87. pci_unmap_addr(sq, mapping));
  88. }
  89. static void dealloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  90. {
  91. if (t4_sq_onchip(sq))
  92. dealloc_oc_sq(rdev, sq);
  93. else
  94. dealloc_host_sq(rdev, sq);
  95. }
  96. static int alloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  97. {
  98. if (!ocqp_support || !ocqp_supported(&rdev->lldi))
  99. return -ENOSYS;
  100. sq->dma_addr = c4iw_ocqp_pool_alloc(rdev, sq->memsize);
  101. if (!sq->dma_addr)
  102. return -ENOMEM;
  103. sq->phys_addr = rdev->oc_mw_pa + sq->dma_addr -
  104. rdev->lldi.vr->ocq.start;
  105. sq->queue = (__force union t4_wr *)(rdev->oc_mw_kva + sq->dma_addr -
  106. rdev->lldi.vr->ocq.start);
  107. sq->flags |= T4_SQ_ONCHIP;
  108. return 0;
  109. }
  110. static int alloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  111. {
  112. sq->queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev), sq->memsize,
  113. &(sq->dma_addr), GFP_KERNEL);
  114. if (!sq->queue)
  115. return -ENOMEM;
  116. sq->phys_addr = virt_to_phys(sq->queue);
  117. pci_unmap_addr_set(sq, mapping, sq->dma_addr);
  118. return 0;
  119. }
  120. static int alloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq, int user)
  121. {
  122. int ret = -ENOSYS;
  123. if (user)
  124. ret = alloc_oc_sq(rdev, sq);
  125. if (ret)
  126. ret = alloc_host_sq(rdev, sq);
  127. return ret;
  128. }
  129. static int destroy_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
  130. struct c4iw_dev_ucontext *uctx)
  131. {
  132. /*
  133. * uP clears EQ contexts when the connection exits rdma mode,
  134. * so no need to post a RESET WR for these EQs.
  135. */
  136. dma_free_coherent(&(rdev->lldi.pdev->dev),
  137. wq->rq.memsize, wq->rq.queue,
  138. dma_unmap_addr(&wq->rq, mapping));
  139. dealloc_sq(rdev, &wq->sq);
  140. c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
  141. kfree(wq->rq.sw_rq);
  142. kfree(wq->sq.sw_sq);
  143. c4iw_put_qpid(rdev, wq->rq.qid, uctx);
  144. c4iw_put_qpid(rdev, wq->sq.qid, uctx);
  145. return 0;
  146. }
  147. /*
  148. * Determine the BAR2 virtual address and qid. If pbar2_pa is not NULL,
  149. * then this is a user mapping so compute the page-aligned physical address
  150. * for mapping.
  151. */
  152. void __iomem *c4iw_bar2_addrs(struct c4iw_rdev *rdev, unsigned int qid,
  153. enum cxgb4_bar2_qtype qtype,
  154. unsigned int *pbar2_qid, u64 *pbar2_pa)
  155. {
  156. u64 bar2_qoffset;
  157. int ret;
  158. ret = cxgb4_bar2_sge_qregs(rdev->lldi.ports[0], qid, qtype,
  159. pbar2_pa ? 1 : 0,
  160. &bar2_qoffset, pbar2_qid);
  161. if (ret)
  162. return NULL;
  163. if (pbar2_pa)
  164. *pbar2_pa = (rdev->bar2_pa + bar2_qoffset) & PAGE_MASK;
  165. if (is_t4(rdev->lldi.adapter_type))
  166. return NULL;
  167. return rdev->bar2_kva + bar2_qoffset;
  168. }
  169. static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
  170. struct t4_cq *rcq, struct t4_cq *scq,
  171. struct c4iw_dev_ucontext *uctx,
  172. struct c4iw_wr_wait *wr_waitp)
  173. {
  174. int user = (uctx != &rdev->uctx);
  175. struct fw_ri_res_wr *res_wr;
  176. struct fw_ri_res *res;
  177. int wr_len;
  178. struct sk_buff *skb;
  179. int ret = 0;
  180. int eqsize;
  181. wq->sq.qid = c4iw_get_qpid(rdev, uctx);
  182. if (!wq->sq.qid)
  183. return -ENOMEM;
  184. wq->rq.qid = c4iw_get_qpid(rdev, uctx);
  185. if (!wq->rq.qid) {
  186. ret = -ENOMEM;
  187. goto free_sq_qid;
  188. }
  189. if (!user) {
  190. wq->sq.sw_sq = kzalloc(wq->sq.size * sizeof *wq->sq.sw_sq,
  191. GFP_KERNEL);
  192. if (!wq->sq.sw_sq) {
  193. ret = -ENOMEM;
  194. goto free_rq_qid;
  195. }
  196. wq->rq.sw_rq = kzalloc(wq->rq.size * sizeof *wq->rq.sw_rq,
  197. GFP_KERNEL);
  198. if (!wq->rq.sw_rq) {
  199. ret = -ENOMEM;
  200. goto free_sw_sq;
  201. }
  202. }
  203. /*
  204. * RQT must be a power of 2 and at least 16 deep.
  205. */
  206. wq->rq.rqt_size = roundup_pow_of_two(max_t(u16, wq->rq.size, 16));
  207. wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
  208. if (!wq->rq.rqt_hwaddr) {
  209. ret = -ENOMEM;
  210. goto free_sw_rq;
  211. }
  212. ret = alloc_sq(rdev, &wq->sq, user);
  213. if (ret)
  214. goto free_hwaddr;
  215. memset(wq->sq.queue, 0, wq->sq.memsize);
  216. dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
  217. wq->rq.queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev),
  218. wq->rq.memsize, &(wq->rq.dma_addr),
  219. GFP_KERNEL);
  220. if (!wq->rq.queue) {
  221. ret = -ENOMEM;
  222. goto free_sq;
  223. }
  224. pr_debug("sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx\n",
  225. wq->sq.queue,
  226. (unsigned long long)virt_to_phys(wq->sq.queue),
  227. wq->rq.queue,
  228. (unsigned long long)virt_to_phys(wq->rq.queue));
  229. memset(wq->rq.queue, 0, wq->rq.memsize);
  230. dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
  231. wq->db = rdev->lldi.db_reg;
  232. wq->sq.bar2_va = c4iw_bar2_addrs(rdev, wq->sq.qid, T4_BAR2_QTYPE_EGRESS,
  233. &wq->sq.bar2_qid,
  234. user ? &wq->sq.bar2_pa : NULL);
  235. wq->rq.bar2_va = c4iw_bar2_addrs(rdev, wq->rq.qid, T4_BAR2_QTYPE_EGRESS,
  236. &wq->rq.bar2_qid,
  237. user ? &wq->rq.bar2_pa : NULL);
  238. /*
  239. * User mode must have bar2 access.
  240. */
  241. if (user && (!wq->sq.bar2_pa || !wq->rq.bar2_pa)) {
  242. pr_warn("%s: sqid %u or rqid %u not in BAR2 range\n",
  243. pci_name(rdev->lldi.pdev), wq->sq.qid, wq->rq.qid);
  244. goto free_dma;
  245. }
  246. wq->rdev = rdev;
  247. wq->rq.msn = 1;
  248. /* build fw_ri_res_wr */
  249. wr_len = sizeof *res_wr + 2 * sizeof *res;
  250. skb = alloc_skb(wr_len, GFP_KERNEL);
  251. if (!skb) {
  252. ret = -ENOMEM;
  253. goto free_dma;
  254. }
  255. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  256. res_wr = __skb_put_zero(skb, wr_len);
  257. res_wr->op_nres = cpu_to_be32(
  258. FW_WR_OP_V(FW_RI_RES_WR) |
  259. FW_RI_RES_WR_NRES_V(2) |
  260. FW_WR_COMPL_F);
  261. res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
  262. res_wr->cookie = (uintptr_t)wr_waitp;
  263. res = res_wr->res;
  264. res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
  265. res->u.sqrq.op = FW_RI_RES_OP_WRITE;
  266. /*
  267. * eqsize is the number of 64B entries plus the status page size.
  268. */
  269. eqsize = wq->sq.size * T4_SQ_NUM_SLOTS +
  270. rdev->hw_queue.t4_eq_status_entries;
  271. res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
  272. FW_RI_RES_WR_HOSTFCMODE_V(0) | /* no host cidx updates */
  273. FW_RI_RES_WR_CPRIO_V(0) | /* don't keep in chip cache */
  274. FW_RI_RES_WR_PCIECHN_V(0) | /* set by uP at ri_init time */
  275. (t4_sq_onchip(&wq->sq) ? FW_RI_RES_WR_ONCHIP_F : 0) |
  276. FW_RI_RES_WR_IQID_V(scq->cqid));
  277. res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
  278. FW_RI_RES_WR_DCAEN_V(0) |
  279. FW_RI_RES_WR_DCACPU_V(0) |
  280. FW_RI_RES_WR_FBMIN_V(2) |
  281. (t4_sq_onchip(&wq->sq) ? FW_RI_RES_WR_FBMAX_V(2) :
  282. FW_RI_RES_WR_FBMAX_V(3)) |
  283. FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
  284. FW_RI_RES_WR_CIDXFTHRESH_V(0) |
  285. FW_RI_RES_WR_EQSIZE_V(eqsize));
  286. res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
  287. res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
  288. res++;
  289. res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
  290. res->u.sqrq.op = FW_RI_RES_OP_WRITE;
  291. /*
  292. * eqsize is the number of 64B entries plus the status page size.
  293. */
  294. eqsize = wq->rq.size * T4_RQ_NUM_SLOTS +
  295. rdev->hw_queue.t4_eq_status_entries;
  296. res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
  297. FW_RI_RES_WR_HOSTFCMODE_V(0) | /* no host cidx updates */
  298. FW_RI_RES_WR_CPRIO_V(0) | /* don't keep in chip cache */
  299. FW_RI_RES_WR_PCIECHN_V(0) | /* set by uP at ri_init time */
  300. FW_RI_RES_WR_IQID_V(rcq->cqid));
  301. res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
  302. FW_RI_RES_WR_DCAEN_V(0) |
  303. FW_RI_RES_WR_DCACPU_V(0) |
  304. FW_RI_RES_WR_FBMIN_V(2) |
  305. FW_RI_RES_WR_FBMAX_V(3) |
  306. FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
  307. FW_RI_RES_WR_CIDXFTHRESH_V(0) |
  308. FW_RI_RES_WR_EQSIZE_V(eqsize));
  309. res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
  310. res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
  311. c4iw_init_wr_wait(wr_waitp);
  312. ret = c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, wq->sq.qid, __func__);
  313. if (ret)
  314. goto free_dma;
  315. pr_debug("sqid 0x%x rqid 0x%x kdb 0x%p sq_bar2_addr %p rq_bar2_addr %p\n",
  316. wq->sq.qid, wq->rq.qid, wq->db,
  317. wq->sq.bar2_va, wq->rq.bar2_va);
  318. return 0;
  319. free_dma:
  320. dma_free_coherent(&(rdev->lldi.pdev->dev),
  321. wq->rq.memsize, wq->rq.queue,
  322. dma_unmap_addr(&wq->rq, mapping));
  323. free_sq:
  324. dealloc_sq(rdev, &wq->sq);
  325. free_hwaddr:
  326. c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
  327. free_sw_rq:
  328. kfree(wq->rq.sw_rq);
  329. free_sw_sq:
  330. kfree(wq->sq.sw_sq);
  331. free_rq_qid:
  332. c4iw_put_qpid(rdev, wq->rq.qid, uctx);
  333. free_sq_qid:
  334. c4iw_put_qpid(rdev, wq->sq.qid, uctx);
  335. return ret;
  336. }
  337. static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
  338. struct ib_send_wr *wr, int max, u32 *plenp)
  339. {
  340. u8 *dstp, *srcp;
  341. u32 plen = 0;
  342. int i;
  343. int rem, len;
  344. dstp = (u8 *)immdp->data;
  345. for (i = 0; i < wr->num_sge; i++) {
  346. if ((plen + wr->sg_list[i].length) > max)
  347. return -EMSGSIZE;
  348. srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
  349. plen += wr->sg_list[i].length;
  350. rem = wr->sg_list[i].length;
  351. while (rem) {
  352. if (dstp == (u8 *)&sq->queue[sq->size])
  353. dstp = (u8 *)sq->queue;
  354. if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
  355. len = rem;
  356. else
  357. len = (u8 *)&sq->queue[sq->size] - dstp;
  358. memcpy(dstp, srcp, len);
  359. dstp += len;
  360. srcp += len;
  361. rem -= len;
  362. }
  363. }
  364. len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
  365. if (len)
  366. memset(dstp, 0, len);
  367. immdp->op = FW_RI_DATA_IMMD;
  368. immdp->r1 = 0;
  369. immdp->r2 = 0;
  370. immdp->immdlen = cpu_to_be32(plen);
  371. *plenp = plen;
  372. return 0;
  373. }
  374. static int build_isgl(__be64 *queue_start, __be64 *queue_end,
  375. struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
  376. int num_sge, u32 *plenp)
  377. {
  378. int i;
  379. u32 plen = 0;
  380. __be64 *flitp = (__be64 *)isglp->sge;
  381. for (i = 0; i < num_sge; i++) {
  382. if ((plen + sg_list[i].length) < plen)
  383. return -EMSGSIZE;
  384. plen += sg_list[i].length;
  385. *flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
  386. sg_list[i].length);
  387. if (++flitp == queue_end)
  388. flitp = queue_start;
  389. *flitp = cpu_to_be64(sg_list[i].addr);
  390. if (++flitp == queue_end)
  391. flitp = queue_start;
  392. }
  393. *flitp = (__force __be64)0;
  394. isglp->op = FW_RI_DATA_ISGL;
  395. isglp->r1 = 0;
  396. isglp->nsge = cpu_to_be16(num_sge);
  397. isglp->r2 = 0;
  398. if (plenp)
  399. *plenp = plen;
  400. return 0;
  401. }
  402. static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
  403. struct ib_send_wr *wr, u8 *len16)
  404. {
  405. u32 plen;
  406. int size;
  407. int ret;
  408. if (wr->num_sge > T4_MAX_SEND_SGE)
  409. return -EINVAL;
  410. switch (wr->opcode) {
  411. case IB_WR_SEND:
  412. if (wr->send_flags & IB_SEND_SOLICITED)
  413. wqe->send.sendop_pkd = cpu_to_be32(
  414. FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_SE));
  415. else
  416. wqe->send.sendop_pkd = cpu_to_be32(
  417. FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND));
  418. wqe->send.stag_inv = 0;
  419. break;
  420. case IB_WR_SEND_WITH_INV:
  421. if (wr->send_flags & IB_SEND_SOLICITED)
  422. wqe->send.sendop_pkd = cpu_to_be32(
  423. FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_SE_INV));
  424. else
  425. wqe->send.sendop_pkd = cpu_to_be32(
  426. FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_INV));
  427. wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
  428. break;
  429. default:
  430. return -EINVAL;
  431. }
  432. wqe->send.r3 = 0;
  433. wqe->send.r4 = 0;
  434. plen = 0;
  435. if (wr->num_sge) {
  436. if (wr->send_flags & IB_SEND_INLINE) {
  437. ret = build_immd(sq, wqe->send.u.immd_src, wr,
  438. T4_MAX_SEND_INLINE, &plen);
  439. if (ret)
  440. return ret;
  441. size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
  442. plen;
  443. } else {
  444. ret = build_isgl((__be64 *)sq->queue,
  445. (__be64 *)&sq->queue[sq->size],
  446. wqe->send.u.isgl_src,
  447. wr->sg_list, wr->num_sge, &plen);
  448. if (ret)
  449. return ret;
  450. size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
  451. wr->num_sge * sizeof(struct fw_ri_sge);
  452. }
  453. } else {
  454. wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
  455. wqe->send.u.immd_src[0].r1 = 0;
  456. wqe->send.u.immd_src[0].r2 = 0;
  457. wqe->send.u.immd_src[0].immdlen = 0;
  458. size = sizeof wqe->send + sizeof(struct fw_ri_immd);
  459. plen = 0;
  460. }
  461. *len16 = DIV_ROUND_UP(size, 16);
  462. wqe->send.plen = cpu_to_be32(plen);
  463. return 0;
  464. }
  465. static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
  466. struct ib_send_wr *wr, u8 *len16)
  467. {
  468. u32 plen;
  469. int size;
  470. int ret;
  471. if (wr->num_sge > T4_MAX_SEND_SGE)
  472. return -EINVAL;
  473. wqe->write.r2 = 0;
  474. wqe->write.stag_sink = cpu_to_be32(rdma_wr(wr)->rkey);
  475. wqe->write.to_sink = cpu_to_be64(rdma_wr(wr)->remote_addr);
  476. if (wr->num_sge) {
  477. if (wr->send_flags & IB_SEND_INLINE) {
  478. ret = build_immd(sq, wqe->write.u.immd_src, wr,
  479. T4_MAX_WRITE_INLINE, &plen);
  480. if (ret)
  481. return ret;
  482. size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
  483. plen;
  484. } else {
  485. ret = build_isgl((__be64 *)sq->queue,
  486. (__be64 *)&sq->queue[sq->size],
  487. wqe->write.u.isgl_src,
  488. wr->sg_list, wr->num_sge, &plen);
  489. if (ret)
  490. return ret;
  491. size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
  492. wr->num_sge * sizeof(struct fw_ri_sge);
  493. }
  494. } else {
  495. wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
  496. wqe->write.u.immd_src[0].r1 = 0;
  497. wqe->write.u.immd_src[0].r2 = 0;
  498. wqe->write.u.immd_src[0].immdlen = 0;
  499. size = sizeof wqe->write + sizeof(struct fw_ri_immd);
  500. plen = 0;
  501. }
  502. *len16 = DIV_ROUND_UP(size, 16);
  503. wqe->write.plen = cpu_to_be32(plen);
  504. return 0;
  505. }
  506. static int build_rdma_read(union t4_wr *wqe, struct ib_send_wr *wr, u8 *len16)
  507. {
  508. if (wr->num_sge > 1)
  509. return -EINVAL;
  510. if (wr->num_sge && wr->sg_list[0].length) {
  511. wqe->read.stag_src = cpu_to_be32(rdma_wr(wr)->rkey);
  512. wqe->read.to_src_hi = cpu_to_be32((u32)(rdma_wr(wr)->remote_addr
  513. >> 32));
  514. wqe->read.to_src_lo = cpu_to_be32((u32)rdma_wr(wr)->remote_addr);
  515. wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
  516. wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
  517. wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
  518. >> 32));
  519. wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
  520. } else {
  521. wqe->read.stag_src = cpu_to_be32(2);
  522. wqe->read.to_src_hi = 0;
  523. wqe->read.to_src_lo = 0;
  524. wqe->read.stag_sink = cpu_to_be32(2);
  525. wqe->read.plen = 0;
  526. wqe->read.to_sink_hi = 0;
  527. wqe->read.to_sink_lo = 0;
  528. }
  529. wqe->read.r2 = 0;
  530. wqe->read.r5 = 0;
  531. *len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
  532. return 0;
  533. }
  534. static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
  535. struct ib_recv_wr *wr, u8 *len16)
  536. {
  537. int ret;
  538. ret = build_isgl((__be64 *)qhp->wq.rq.queue,
  539. (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
  540. &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
  541. if (ret)
  542. return ret;
  543. *len16 = DIV_ROUND_UP(sizeof wqe->recv +
  544. wr->num_sge * sizeof(struct fw_ri_sge), 16);
  545. return 0;
  546. }
  547. static void build_tpte_memreg(struct fw_ri_fr_nsmr_tpte_wr *fr,
  548. struct ib_reg_wr *wr, struct c4iw_mr *mhp,
  549. u8 *len16)
  550. {
  551. __be64 *p = (__be64 *)fr->pbl;
  552. fr->r2 = cpu_to_be32(0);
  553. fr->stag = cpu_to_be32(mhp->ibmr.rkey);
  554. fr->tpte.valid_to_pdid = cpu_to_be32(FW_RI_TPTE_VALID_F |
  555. FW_RI_TPTE_STAGKEY_V((mhp->ibmr.rkey & FW_RI_TPTE_STAGKEY_M)) |
  556. FW_RI_TPTE_STAGSTATE_V(1) |
  557. FW_RI_TPTE_STAGTYPE_V(FW_RI_STAG_NSMR) |
  558. FW_RI_TPTE_PDID_V(mhp->attr.pdid));
  559. fr->tpte.locread_to_qpid = cpu_to_be32(
  560. FW_RI_TPTE_PERM_V(c4iw_ib_to_tpt_access(wr->access)) |
  561. FW_RI_TPTE_ADDRTYPE_V(FW_RI_VA_BASED_TO) |
  562. FW_RI_TPTE_PS_V(ilog2(wr->mr->page_size) - 12));
  563. fr->tpte.nosnoop_pbladdr = cpu_to_be32(FW_RI_TPTE_PBLADDR_V(
  564. PBL_OFF(&mhp->rhp->rdev, mhp->attr.pbl_addr)>>3));
  565. fr->tpte.dca_mwbcnt_pstag = cpu_to_be32(0);
  566. fr->tpte.len_hi = cpu_to_be32(0);
  567. fr->tpte.len_lo = cpu_to_be32(mhp->ibmr.length);
  568. fr->tpte.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
  569. fr->tpte.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova & 0xffffffff);
  570. p[0] = cpu_to_be64((u64)mhp->mpl[0]);
  571. p[1] = cpu_to_be64((u64)mhp->mpl[1]);
  572. *len16 = DIV_ROUND_UP(sizeof(*fr), 16);
  573. }
  574. static int build_memreg(struct t4_sq *sq, union t4_wr *wqe,
  575. struct ib_reg_wr *wr, struct c4iw_mr *mhp, u8 *len16,
  576. bool dsgl_supported)
  577. {
  578. struct fw_ri_immd *imdp;
  579. __be64 *p;
  580. int i;
  581. int pbllen = roundup(mhp->mpl_len * sizeof(u64), 32);
  582. int rem;
  583. if (mhp->mpl_len > t4_max_fr_depth(dsgl_supported && use_dsgl))
  584. return -EINVAL;
  585. wqe->fr.qpbinde_to_dcacpu = 0;
  586. wqe->fr.pgsz_shift = ilog2(wr->mr->page_size) - 12;
  587. wqe->fr.addr_type = FW_RI_VA_BASED_TO;
  588. wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->access);
  589. wqe->fr.len_hi = 0;
  590. wqe->fr.len_lo = cpu_to_be32(mhp->ibmr.length);
  591. wqe->fr.stag = cpu_to_be32(wr->key);
  592. wqe->fr.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
  593. wqe->fr.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova &
  594. 0xffffffff);
  595. if (dsgl_supported && use_dsgl && (pbllen > max_fr_immd)) {
  596. struct fw_ri_dsgl *sglp;
  597. for (i = 0; i < mhp->mpl_len; i++)
  598. mhp->mpl[i] = (__force u64)cpu_to_be64((u64)mhp->mpl[i]);
  599. sglp = (struct fw_ri_dsgl *)(&wqe->fr + 1);
  600. sglp->op = FW_RI_DATA_DSGL;
  601. sglp->r1 = 0;
  602. sglp->nsge = cpu_to_be16(1);
  603. sglp->addr0 = cpu_to_be64(mhp->mpl_addr);
  604. sglp->len0 = cpu_to_be32(pbllen);
  605. *len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*sglp), 16);
  606. } else {
  607. imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
  608. imdp->op = FW_RI_DATA_IMMD;
  609. imdp->r1 = 0;
  610. imdp->r2 = 0;
  611. imdp->immdlen = cpu_to_be32(pbllen);
  612. p = (__be64 *)(imdp + 1);
  613. rem = pbllen;
  614. for (i = 0; i < mhp->mpl_len; i++) {
  615. *p = cpu_to_be64((u64)mhp->mpl[i]);
  616. rem -= sizeof(*p);
  617. if (++p == (__be64 *)&sq->queue[sq->size])
  618. p = (__be64 *)sq->queue;
  619. }
  620. BUG_ON(rem < 0);
  621. while (rem) {
  622. *p = 0;
  623. rem -= sizeof(*p);
  624. if (++p == (__be64 *)&sq->queue[sq->size])
  625. p = (__be64 *)sq->queue;
  626. }
  627. *len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*imdp)
  628. + pbllen, 16);
  629. }
  630. return 0;
  631. }
  632. static int build_inv_stag(union t4_wr *wqe, struct ib_send_wr *wr, u8 *len16)
  633. {
  634. wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
  635. wqe->inv.r2 = 0;
  636. *len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
  637. return 0;
  638. }
  639. static void free_qp_work(struct work_struct *work)
  640. {
  641. struct c4iw_ucontext *ucontext;
  642. struct c4iw_qp *qhp;
  643. struct c4iw_dev *rhp;
  644. qhp = container_of(work, struct c4iw_qp, free_work);
  645. ucontext = qhp->ucontext;
  646. rhp = qhp->rhp;
  647. pr_debug("qhp %p ucontext %p\n", qhp, ucontext);
  648. destroy_qp(&rhp->rdev, &qhp->wq,
  649. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  650. if (ucontext)
  651. c4iw_put_ucontext(ucontext);
  652. c4iw_put_wr_wait(qhp->wr_waitp);
  653. kfree(qhp);
  654. }
  655. static void queue_qp_free(struct kref *kref)
  656. {
  657. struct c4iw_qp *qhp;
  658. qhp = container_of(kref, struct c4iw_qp, kref);
  659. pr_debug("qhp %p\n", qhp);
  660. queue_work(qhp->rhp->rdev.free_workq, &qhp->free_work);
  661. }
  662. void c4iw_qp_add_ref(struct ib_qp *qp)
  663. {
  664. pr_debug("ib_qp %p\n", qp);
  665. kref_get(&to_c4iw_qp(qp)->kref);
  666. }
  667. void c4iw_qp_rem_ref(struct ib_qp *qp)
  668. {
  669. pr_debug("ib_qp %p\n", qp);
  670. kref_put(&to_c4iw_qp(qp)->kref, queue_qp_free);
  671. }
  672. static void add_to_fc_list(struct list_head *head, struct list_head *entry)
  673. {
  674. if (list_empty(entry))
  675. list_add_tail(entry, head);
  676. }
  677. static int ring_kernel_sq_db(struct c4iw_qp *qhp, u16 inc)
  678. {
  679. unsigned long flags;
  680. spin_lock_irqsave(&qhp->rhp->lock, flags);
  681. spin_lock(&qhp->lock);
  682. if (qhp->rhp->db_state == NORMAL)
  683. t4_ring_sq_db(&qhp->wq, inc, NULL);
  684. else {
  685. add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
  686. qhp->wq.sq.wq_pidx_inc += inc;
  687. }
  688. spin_unlock(&qhp->lock);
  689. spin_unlock_irqrestore(&qhp->rhp->lock, flags);
  690. return 0;
  691. }
  692. static int ring_kernel_rq_db(struct c4iw_qp *qhp, u16 inc)
  693. {
  694. unsigned long flags;
  695. spin_lock_irqsave(&qhp->rhp->lock, flags);
  696. spin_lock(&qhp->lock);
  697. if (qhp->rhp->db_state == NORMAL)
  698. t4_ring_rq_db(&qhp->wq, inc, NULL);
  699. else {
  700. add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
  701. qhp->wq.rq.wq_pidx_inc += inc;
  702. }
  703. spin_unlock(&qhp->lock);
  704. spin_unlock_irqrestore(&qhp->rhp->lock, flags);
  705. return 0;
  706. }
  707. static void complete_sq_drain_wr(struct c4iw_qp *qhp, struct ib_send_wr *wr)
  708. {
  709. struct t4_cqe cqe = {};
  710. struct c4iw_cq *schp;
  711. unsigned long flag;
  712. struct t4_cq *cq;
  713. schp = to_c4iw_cq(qhp->ibqp.send_cq);
  714. cq = &schp->cq;
  715. cqe.u.drain_cookie = wr->wr_id;
  716. cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
  717. CQE_OPCODE_V(C4IW_DRAIN_OPCODE) |
  718. CQE_TYPE_V(1) |
  719. CQE_SWCQE_V(1) |
  720. CQE_QPID_V(qhp->wq.sq.qid));
  721. spin_lock_irqsave(&schp->lock, flag);
  722. cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
  723. cq->sw_queue[cq->sw_pidx] = cqe;
  724. t4_swcq_produce(cq);
  725. spin_unlock_irqrestore(&schp->lock, flag);
  726. spin_lock_irqsave(&schp->comp_handler_lock, flag);
  727. (*schp->ibcq.comp_handler)(&schp->ibcq,
  728. schp->ibcq.cq_context);
  729. spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
  730. }
  731. static void complete_rq_drain_wr(struct c4iw_qp *qhp, struct ib_recv_wr *wr)
  732. {
  733. struct t4_cqe cqe = {};
  734. struct c4iw_cq *rchp;
  735. unsigned long flag;
  736. struct t4_cq *cq;
  737. rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
  738. cq = &rchp->cq;
  739. cqe.u.drain_cookie = wr->wr_id;
  740. cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
  741. CQE_OPCODE_V(C4IW_DRAIN_OPCODE) |
  742. CQE_TYPE_V(0) |
  743. CQE_SWCQE_V(1) |
  744. CQE_QPID_V(qhp->wq.sq.qid));
  745. spin_lock_irqsave(&rchp->lock, flag);
  746. cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
  747. cq->sw_queue[cq->sw_pidx] = cqe;
  748. t4_swcq_produce(cq);
  749. spin_unlock_irqrestore(&rchp->lock, flag);
  750. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  751. (*rchp->ibcq.comp_handler)(&rchp->ibcq,
  752. rchp->ibcq.cq_context);
  753. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  754. }
  755. int c4iw_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  756. struct ib_send_wr **bad_wr)
  757. {
  758. int err = 0;
  759. u8 len16 = 0;
  760. enum fw_wr_opcodes fw_opcode = 0;
  761. enum fw_ri_wr_flags fw_flags;
  762. struct c4iw_qp *qhp;
  763. union t4_wr *wqe = NULL;
  764. u32 num_wrs;
  765. struct t4_swsqe *swsqe;
  766. unsigned long flag;
  767. u16 idx = 0;
  768. qhp = to_c4iw_qp(ibqp);
  769. spin_lock_irqsave(&qhp->lock, flag);
  770. if (t4_wq_in_error(&qhp->wq)) {
  771. spin_unlock_irqrestore(&qhp->lock, flag);
  772. complete_sq_drain_wr(qhp, wr);
  773. return err;
  774. }
  775. num_wrs = t4_sq_avail(&qhp->wq);
  776. if (num_wrs == 0) {
  777. spin_unlock_irqrestore(&qhp->lock, flag);
  778. *bad_wr = wr;
  779. return -ENOMEM;
  780. }
  781. while (wr) {
  782. if (num_wrs == 0) {
  783. err = -ENOMEM;
  784. *bad_wr = wr;
  785. break;
  786. }
  787. wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
  788. qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
  789. fw_flags = 0;
  790. if (wr->send_flags & IB_SEND_SOLICITED)
  791. fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
  792. if (wr->send_flags & IB_SEND_SIGNALED || qhp->sq_sig_all)
  793. fw_flags |= FW_RI_COMPLETION_FLAG;
  794. swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
  795. switch (wr->opcode) {
  796. case IB_WR_SEND_WITH_INV:
  797. case IB_WR_SEND:
  798. if (wr->send_flags & IB_SEND_FENCE)
  799. fw_flags |= FW_RI_READ_FENCE_FLAG;
  800. fw_opcode = FW_RI_SEND_WR;
  801. if (wr->opcode == IB_WR_SEND)
  802. swsqe->opcode = FW_RI_SEND;
  803. else
  804. swsqe->opcode = FW_RI_SEND_WITH_INV;
  805. err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
  806. break;
  807. case IB_WR_RDMA_WRITE:
  808. fw_opcode = FW_RI_RDMA_WRITE_WR;
  809. swsqe->opcode = FW_RI_RDMA_WRITE;
  810. err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
  811. break;
  812. case IB_WR_RDMA_READ:
  813. case IB_WR_RDMA_READ_WITH_INV:
  814. fw_opcode = FW_RI_RDMA_READ_WR;
  815. swsqe->opcode = FW_RI_READ_REQ;
  816. if (wr->opcode == IB_WR_RDMA_READ_WITH_INV) {
  817. c4iw_invalidate_mr(qhp->rhp,
  818. wr->sg_list[0].lkey);
  819. fw_flags = FW_RI_RDMA_READ_INVALIDATE;
  820. } else {
  821. fw_flags = 0;
  822. }
  823. err = build_rdma_read(wqe, wr, &len16);
  824. if (err)
  825. break;
  826. swsqe->read_len = wr->sg_list[0].length;
  827. if (!qhp->wq.sq.oldest_read)
  828. qhp->wq.sq.oldest_read = swsqe;
  829. break;
  830. case IB_WR_REG_MR: {
  831. struct c4iw_mr *mhp = to_c4iw_mr(reg_wr(wr)->mr);
  832. swsqe->opcode = FW_RI_FAST_REGISTER;
  833. if (qhp->rhp->rdev.lldi.fr_nsmr_tpte_wr_support &&
  834. !mhp->attr.state && mhp->mpl_len <= 2) {
  835. fw_opcode = FW_RI_FR_NSMR_TPTE_WR;
  836. build_tpte_memreg(&wqe->fr_tpte, reg_wr(wr),
  837. mhp, &len16);
  838. } else {
  839. fw_opcode = FW_RI_FR_NSMR_WR;
  840. err = build_memreg(&qhp->wq.sq, wqe, reg_wr(wr),
  841. mhp, &len16,
  842. qhp->rhp->rdev.lldi.ulptx_memwrite_dsgl);
  843. if (err)
  844. break;
  845. }
  846. mhp->attr.state = 1;
  847. break;
  848. }
  849. case IB_WR_LOCAL_INV:
  850. if (wr->send_flags & IB_SEND_FENCE)
  851. fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
  852. fw_opcode = FW_RI_INV_LSTAG_WR;
  853. swsqe->opcode = FW_RI_LOCAL_INV;
  854. err = build_inv_stag(wqe, wr, &len16);
  855. c4iw_invalidate_mr(qhp->rhp, wr->ex.invalidate_rkey);
  856. break;
  857. default:
  858. pr_warn("%s post of type=%d TBD!\n", __func__,
  859. wr->opcode);
  860. err = -EINVAL;
  861. }
  862. if (err) {
  863. *bad_wr = wr;
  864. break;
  865. }
  866. swsqe->idx = qhp->wq.sq.pidx;
  867. swsqe->complete = 0;
  868. swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
  869. qhp->sq_sig_all;
  870. swsqe->flushed = 0;
  871. swsqe->wr_id = wr->wr_id;
  872. if (c4iw_wr_log) {
  873. swsqe->sge_ts = cxgb4_read_sge_timestamp(
  874. qhp->rhp->rdev.lldi.ports[0]);
  875. getnstimeofday(&swsqe->host_ts);
  876. }
  877. init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
  878. pr_debug("cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
  879. (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
  880. swsqe->opcode, swsqe->read_len);
  881. wr = wr->next;
  882. num_wrs--;
  883. t4_sq_produce(&qhp->wq, len16);
  884. idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
  885. }
  886. if (!qhp->rhp->rdev.status_page->db_off) {
  887. t4_ring_sq_db(&qhp->wq, idx, wqe);
  888. spin_unlock_irqrestore(&qhp->lock, flag);
  889. } else {
  890. spin_unlock_irqrestore(&qhp->lock, flag);
  891. ring_kernel_sq_db(qhp, idx);
  892. }
  893. return err;
  894. }
  895. int c4iw_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  896. struct ib_recv_wr **bad_wr)
  897. {
  898. int err = 0;
  899. struct c4iw_qp *qhp;
  900. union t4_recv_wr *wqe = NULL;
  901. u32 num_wrs;
  902. u8 len16 = 0;
  903. unsigned long flag;
  904. u16 idx = 0;
  905. qhp = to_c4iw_qp(ibqp);
  906. spin_lock_irqsave(&qhp->lock, flag);
  907. if (t4_wq_in_error(&qhp->wq)) {
  908. spin_unlock_irqrestore(&qhp->lock, flag);
  909. complete_rq_drain_wr(qhp, wr);
  910. return err;
  911. }
  912. num_wrs = t4_rq_avail(&qhp->wq);
  913. if (num_wrs == 0) {
  914. spin_unlock_irqrestore(&qhp->lock, flag);
  915. *bad_wr = wr;
  916. return -ENOMEM;
  917. }
  918. while (wr) {
  919. if (wr->num_sge > T4_MAX_RECV_SGE) {
  920. err = -EINVAL;
  921. *bad_wr = wr;
  922. break;
  923. }
  924. wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
  925. qhp->wq.rq.wq_pidx *
  926. T4_EQ_ENTRY_SIZE);
  927. if (num_wrs)
  928. err = build_rdma_recv(qhp, wqe, wr, &len16);
  929. else
  930. err = -ENOMEM;
  931. if (err) {
  932. *bad_wr = wr;
  933. break;
  934. }
  935. qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
  936. if (c4iw_wr_log) {
  937. qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].sge_ts =
  938. cxgb4_read_sge_timestamp(
  939. qhp->rhp->rdev.lldi.ports[0]);
  940. getnstimeofday(
  941. &qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].host_ts);
  942. }
  943. wqe->recv.opcode = FW_RI_RECV_WR;
  944. wqe->recv.r1 = 0;
  945. wqe->recv.wrid = qhp->wq.rq.pidx;
  946. wqe->recv.r2[0] = 0;
  947. wqe->recv.r2[1] = 0;
  948. wqe->recv.r2[2] = 0;
  949. wqe->recv.len16 = len16;
  950. pr_debug("cookie 0x%llx pidx %u\n",
  951. (unsigned long long)wr->wr_id, qhp->wq.rq.pidx);
  952. t4_rq_produce(&qhp->wq, len16);
  953. idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
  954. wr = wr->next;
  955. num_wrs--;
  956. }
  957. if (!qhp->rhp->rdev.status_page->db_off) {
  958. t4_ring_rq_db(&qhp->wq, idx, wqe);
  959. spin_unlock_irqrestore(&qhp->lock, flag);
  960. } else {
  961. spin_unlock_irqrestore(&qhp->lock, flag);
  962. ring_kernel_rq_db(qhp, idx);
  963. }
  964. return err;
  965. }
  966. static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
  967. u8 *ecode)
  968. {
  969. int status;
  970. int tagged;
  971. int opcode;
  972. int rqtype;
  973. int send_inv;
  974. if (!err_cqe) {
  975. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  976. *ecode = 0;
  977. return;
  978. }
  979. status = CQE_STATUS(err_cqe);
  980. opcode = CQE_OPCODE(err_cqe);
  981. rqtype = RQ_TYPE(err_cqe);
  982. send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
  983. (opcode == FW_RI_SEND_WITH_SE_INV);
  984. tagged = (opcode == FW_RI_RDMA_WRITE) ||
  985. (rqtype && (opcode == FW_RI_READ_RESP));
  986. switch (status) {
  987. case T4_ERR_STAG:
  988. if (send_inv) {
  989. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  990. *ecode = RDMAP_CANT_INV_STAG;
  991. } else {
  992. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  993. *ecode = RDMAP_INV_STAG;
  994. }
  995. break;
  996. case T4_ERR_PDID:
  997. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  998. if ((opcode == FW_RI_SEND_WITH_INV) ||
  999. (opcode == FW_RI_SEND_WITH_SE_INV))
  1000. *ecode = RDMAP_CANT_INV_STAG;
  1001. else
  1002. *ecode = RDMAP_STAG_NOT_ASSOC;
  1003. break;
  1004. case T4_ERR_QPID:
  1005. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1006. *ecode = RDMAP_STAG_NOT_ASSOC;
  1007. break;
  1008. case T4_ERR_ACCESS:
  1009. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1010. *ecode = RDMAP_ACC_VIOL;
  1011. break;
  1012. case T4_ERR_WRAP:
  1013. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1014. *ecode = RDMAP_TO_WRAP;
  1015. break;
  1016. case T4_ERR_BOUND:
  1017. if (tagged) {
  1018. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  1019. *ecode = DDPT_BASE_BOUNDS;
  1020. } else {
  1021. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1022. *ecode = RDMAP_BASE_BOUNDS;
  1023. }
  1024. break;
  1025. case T4_ERR_INVALIDATE_SHARED_MR:
  1026. case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  1027. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  1028. *ecode = RDMAP_CANT_INV_STAG;
  1029. break;
  1030. case T4_ERR_ECC:
  1031. case T4_ERR_ECC_PSTAG:
  1032. case T4_ERR_INTERNAL_ERR:
  1033. *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
  1034. *ecode = 0;
  1035. break;
  1036. case T4_ERR_OUT_OF_RQE:
  1037. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1038. *ecode = DDPU_INV_MSN_NOBUF;
  1039. break;
  1040. case T4_ERR_PBL_ADDR_BOUND:
  1041. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  1042. *ecode = DDPT_BASE_BOUNDS;
  1043. break;
  1044. case T4_ERR_CRC:
  1045. *layer_type = LAYER_MPA|DDP_LLP;
  1046. *ecode = MPA_CRC_ERR;
  1047. break;
  1048. case T4_ERR_MARKER:
  1049. *layer_type = LAYER_MPA|DDP_LLP;
  1050. *ecode = MPA_MARKER_ERR;
  1051. break;
  1052. case T4_ERR_PDU_LEN_ERR:
  1053. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1054. *ecode = DDPU_MSG_TOOBIG;
  1055. break;
  1056. case T4_ERR_DDP_VERSION:
  1057. if (tagged) {
  1058. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  1059. *ecode = DDPT_INV_VERS;
  1060. } else {
  1061. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1062. *ecode = DDPU_INV_VERS;
  1063. }
  1064. break;
  1065. case T4_ERR_RDMA_VERSION:
  1066. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  1067. *ecode = RDMAP_INV_VERS;
  1068. break;
  1069. case T4_ERR_OPCODE:
  1070. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  1071. *ecode = RDMAP_INV_OPCODE;
  1072. break;
  1073. case T4_ERR_DDP_QUEUE_NUM:
  1074. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1075. *ecode = DDPU_INV_QN;
  1076. break;
  1077. case T4_ERR_MSN:
  1078. case T4_ERR_MSN_GAP:
  1079. case T4_ERR_MSN_RANGE:
  1080. case T4_ERR_IRD_OVERFLOW:
  1081. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1082. *ecode = DDPU_INV_MSN_RANGE;
  1083. break;
  1084. case T4_ERR_TBIT:
  1085. *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
  1086. *ecode = 0;
  1087. break;
  1088. case T4_ERR_MO:
  1089. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1090. *ecode = DDPU_INV_MO;
  1091. break;
  1092. default:
  1093. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  1094. *ecode = 0;
  1095. break;
  1096. }
  1097. }
  1098. static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
  1099. gfp_t gfp)
  1100. {
  1101. struct fw_ri_wr *wqe;
  1102. struct sk_buff *skb;
  1103. struct terminate_message *term;
  1104. pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid,
  1105. qhp->ep->hwtid);
  1106. skb = skb_dequeue(&qhp->ep->com.ep_skb_list);
  1107. if (WARN_ON(!skb))
  1108. return;
  1109. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  1110. wqe = __skb_put(skb, sizeof(*wqe));
  1111. memset(wqe, 0, sizeof *wqe);
  1112. wqe->op_compl = cpu_to_be32(FW_WR_OP_V(FW_RI_INIT_WR));
  1113. wqe->flowid_len16 = cpu_to_be32(
  1114. FW_WR_FLOWID_V(qhp->ep->hwtid) |
  1115. FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
  1116. wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
  1117. wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
  1118. term = (struct terminate_message *)wqe->u.terminate.termmsg;
  1119. if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
  1120. term->layer_etype = qhp->attr.layer_etype;
  1121. term->ecode = qhp->attr.ecode;
  1122. } else
  1123. build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
  1124. c4iw_ofld_send(&qhp->rhp->rdev, skb);
  1125. }
  1126. /*
  1127. * Assumes qhp lock is held.
  1128. */
  1129. static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
  1130. struct c4iw_cq *schp)
  1131. {
  1132. int count;
  1133. int rq_flushed, sq_flushed;
  1134. unsigned long flag;
  1135. pr_debug("qhp %p rchp %p schp %p\n", qhp, rchp, schp);
  1136. /* locking hierarchy: cq lock first, then qp lock. */
  1137. spin_lock_irqsave(&rchp->lock, flag);
  1138. spin_lock(&qhp->lock);
  1139. if (qhp->wq.flushed) {
  1140. spin_unlock(&qhp->lock);
  1141. spin_unlock_irqrestore(&rchp->lock, flag);
  1142. return;
  1143. }
  1144. qhp->wq.flushed = 1;
  1145. c4iw_flush_hw_cq(rchp);
  1146. c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
  1147. rq_flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
  1148. spin_unlock(&qhp->lock);
  1149. spin_unlock_irqrestore(&rchp->lock, flag);
  1150. /* locking hierarchy: cq lock first, then qp lock. */
  1151. spin_lock_irqsave(&schp->lock, flag);
  1152. spin_lock(&qhp->lock);
  1153. if (schp != rchp)
  1154. c4iw_flush_hw_cq(schp);
  1155. sq_flushed = c4iw_flush_sq(qhp);
  1156. spin_unlock(&qhp->lock);
  1157. spin_unlock_irqrestore(&schp->lock, flag);
  1158. if (schp == rchp) {
  1159. if (t4_clear_cq_armed(&rchp->cq) &&
  1160. (rq_flushed || sq_flushed)) {
  1161. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  1162. (*rchp->ibcq.comp_handler)(&rchp->ibcq,
  1163. rchp->ibcq.cq_context);
  1164. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  1165. }
  1166. } else {
  1167. if (t4_clear_cq_armed(&rchp->cq) && rq_flushed) {
  1168. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  1169. (*rchp->ibcq.comp_handler)(&rchp->ibcq,
  1170. rchp->ibcq.cq_context);
  1171. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  1172. }
  1173. if (t4_clear_cq_armed(&schp->cq) && sq_flushed) {
  1174. spin_lock_irqsave(&schp->comp_handler_lock, flag);
  1175. (*schp->ibcq.comp_handler)(&schp->ibcq,
  1176. schp->ibcq.cq_context);
  1177. spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
  1178. }
  1179. }
  1180. }
  1181. static void flush_qp(struct c4iw_qp *qhp)
  1182. {
  1183. struct c4iw_cq *rchp, *schp;
  1184. unsigned long flag;
  1185. rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
  1186. schp = to_c4iw_cq(qhp->ibqp.send_cq);
  1187. t4_set_wq_in_error(&qhp->wq);
  1188. if (qhp->ibqp.uobject) {
  1189. t4_set_cq_in_error(&rchp->cq);
  1190. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  1191. (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
  1192. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  1193. if (schp != rchp) {
  1194. t4_set_cq_in_error(&schp->cq);
  1195. spin_lock_irqsave(&schp->comp_handler_lock, flag);
  1196. (*schp->ibcq.comp_handler)(&schp->ibcq,
  1197. schp->ibcq.cq_context);
  1198. spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
  1199. }
  1200. return;
  1201. }
  1202. __flush_qp(qhp, rchp, schp);
  1203. }
  1204. static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
  1205. struct c4iw_ep *ep)
  1206. {
  1207. struct fw_ri_wr *wqe;
  1208. int ret;
  1209. struct sk_buff *skb;
  1210. pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid, ep->hwtid);
  1211. skb = skb_dequeue(&ep->com.ep_skb_list);
  1212. if (WARN_ON(!skb))
  1213. return -ENOMEM;
  1214. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  1215. wqe = __skb_put(skb, sizeof(*wqe));
  1216. memset(wqe, 0, sizeof *wqe);
  1217. wqe->op_compl = cpu_to_be32(
  1218. FW_WR_OP_V(FW_RI_INIT_WR) |
  1219. FW_WR_COMPL_F);
  1220. wqe->flowid_len16 = cpu_to_be32(
  1221. FW_WR_FLOWID_V(ep->hwtid) |
  1222. FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
  1223. wqe->cookie = (uintptr_t)ep->com.wr_waitp;
  1224. wqe->u.fini.type = FW_RI_TYPE_FINI;
  1225. ret = c4iw_ref_send_wait(&rhp->rdev, skb, ep->com.wr_waitp,
  1226. qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
  1227. pr_debug("ret %d\n", ret);
  1228. return ret;
  1229. }
  1230. static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
  1231. {
  1232. pr_debug("p2p_type = %d\n", p2p_type);
  1233. memset(&init->u, 0, sizeof init->u);
  1234. switch (p2p_type) {
  1235. case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
  1236. init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
  1237. init->u.write.stag_sink = cpu_to_be32(1);
  1238. init->u.write.to_sink = cpu_to_be64(1);
  1239. init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
  1240. init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
  1241. sizeof(struct fw_ri_immd),
  1242. 16);
  1243. break;
  1244. case FW_RI_INIT_P2PTYPE_READ_REQ:
  1245. init->u.write.opcode = FW_RI_RDMA_READ_WR;
  1246. init->u.read.stag_src = cpu_to_be32(1);
  1247. init->u.read.to_src_lo = cpu_to_be32(1);
  1248. init->u.read.stag_sink = cpu_to_be32(1);
  1249. init->u.read.to_sink_lo = cpu_to_be32(1);
  1250. init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
  1251. break;
  1252. }
  1253. }
  1254. static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
  1255. {
  1256. struct fw_ri_wr *wqe;
  1257. int ret;
  1258. struct sk_buff *skb;
  1259. pr_debug("qhp %p qid 0x%x tid %u ird %u ord %u\n", qhp,
  1260. qhp->wq.sq.qid, qhp->ep->hwtid, qhp->ep->ird, qhp->ep->ord);
  1261. skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
  1262. if (!skb) {
  1263. ret = -ENOMEM;
  1264. goto out;
  1265. }
  1266. ret = alloc_ird(rhp, qhp->attr.max_ird);
  1267. if (ret) {
  1268. qhp->attr.max_ird = 0;
  1269. kfree_skb(skb);
  1270. goto out;
  1271. }
  1272. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  1273. wqe = __skb_put(skb, sizeof(*wqe));
  1274. memset(wqe, 0, sizeof *wqe);
  1275. wqe->op_compl = cpu_to_be32(
  1276. FW_WR_OP_V(FW_RI_INIT_WR) |
  1277. FW_WR_COMPL_F);
  1278. wqe->flowid_len16 = cpu_to_be32(
  1279. FW_WR_FLOWID_V(qhp->ep->hwtid) |
  1280. FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
  1281. wqe->cookie = (uintptr_t)qhp->ep->com.wr_waitp;
  1282. wqe->u.init.type = FW_RI_TYPE_INIT;
  1283. wqe->u.init.mpareqbit_p2ptype =
  1284. FW_RI_WR_MPAREQBIT_V(qhp->attr.mpa_attr.initiator) |
  1285. FW_RI_WR_P2PTYPE_V(qhp->attr.mpa_attr.p2p_type);
  1286. wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
  1287. if (qhp->attr.mpa_attr.recv_marker_enabled)
  1288. wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
  1289. if (qhp->attr.mpa_attr.xmit_marker_enabled)
  1290. wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
  1291. if (qhp->attr.mpa_attr.crc_enabled)
  1292. wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
  1293. wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
  1294. FW_RI_QP_RDMA_WRITE_ENABLE |
  1295. FW_RI_QP_BIND_ENABLE;
  1296. if (!qhp->ibqp.uobject)
  1297. wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
  1298. FW_RI_QP_STAG0_ENABLE;
  1299. wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
  1300. wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
  1301. wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
  1302. wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
  1303. wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
  1304. wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
  1305. wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
  1306. wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
  1307. wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
  1308. wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
  1309. wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
  1310. wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
  1311. wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
  1312. rhp->rdev.lldi.vr->rq.start);
  1313. if (qhp->attr.mpa_attr.initiator)
  1314. build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
  1315. ret = c4iw_ref_send_wait(&rhp->rdev, skb, qhp->ep->com.wr_waitp,
  1316. qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
  1317. if (!ret)
  1318. goto out;
  1319. free_ird(rhp, qhp->attr.max_ird);
  1320. out:
  1321. pr_debug("ret %d\n", ret);
  1322. return ret;
  1323. }
  1324. int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
  1325. enum c4iw_qp_attr_mask mask,
  1326. struct c4iw_qp_attributes *attrs,
  1327. int internal)
  1328. {
  1329. int ret = 0;
  1330. struct c4iw_qp_attributes newattr = qhp->attr;
  1331. int disconnect = 0;
  1332. int terminate = 0;
  1333. int abort = 0;
  1334. int free = 0;
  1335. struct c4iw_ep *ep = NULL;
  1336. pr_debug("qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n",
  1337. qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
  1338. (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
  1339. mutex_lock(&qhp->mutex);
  1340. /* Process attr changes if in IDLE */
  1341. if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
  1342. if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
  1343. ret = -EIO;
  1344. goto out;
  1345. }
  1346. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
  1347. newattr.enable_rdma_read = attrs->enable_rdma_read;
  1348. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
  1349. newattr.enable_rdma_write = attrs->enable_rdma_write;
  1350. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
  1351. newattr.enable_bind = attrs->enable_bind;
  1352. if (mask & C4IW_QP_ATTR_MAX_ORD) {
  1353. if (attrs->max_ord > c4iw_max_read_depth) {
  1354. ret = -EINVAL;
  1355. goto out;
  1356. }
  1357. newattr.max_ord = attrs->max_ord;
  1358. }
  1359. if (mask & C4IW_QP_ATTR_MAX_IRD) {
  1360. if (attrs->max_ird > cur_max_read_depth(rhp)) {
  1361. ret = -EINVAL;
  1362. goto out;
  1363. }
  1364. newattr.max_ird = attrs->max_ird;
  1365. }
  1366. qhp->attr = newattr;
  1367. }
  1368. if (mask & C4IW_QP_ATTR_SQ_DB) {
  1369. ret = ring_kernel_sq_db(qhp, attrs->sq_db_inc);
  1370. goto out;
  1371. }
  1372. if (mask & C4IW_QP_ATTR_RQ_DB) {
  1373. ret = ring_kernel_rq_db(qhp, attrs->rq_db_inc);
  1374. goto out;
  1375. }
  1376. if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
  1377. goto out;
  1378. if (qhp->attr.state == attrs->next_state)
  1379. goto out;
  1380. switch (qhp->attr.state) {
  1381. case C4IW_QP_STATE_IDLE:
  1382. switch (attrs->next_state) {
  1383. case C4IW_QP_STATE_RTS:
  1384. if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
  1385. ret = -EINVAL;
  1386. goto out;
  1387. }
  1388. if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
  1389. ret = -EINVAL;
  1390. goto out;
  1391. }
  1392. qhp->attr.mpa_attr = attrs->mpa_attr;
  1393. qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
  1394. qhp->ep = qhp->attr.llp_stream_handle;
  1395. set_state(qhp, C4IW_QP_STATE_RTS);
  1396. /*
  1397. * Ref the endpoint here and deref when we
  1398. * disassociate the endpoint from the QP. This
  1399. * happens in CLOSING->IDLE transition or *->ERROR
  1400. * transition.
  1401. */
  1402. c4iw_get_ep(&qhp->ep->com);
  1403. ret = rdma_init(rhp, qhp);
  1404. if (ret)
  1405. goto err;
  1406. break;
  1407. case C4IW_QP_STATE_ERROR:
  1408. set_state(qhp, C4IW_QP_STATE_ERROR);
  1409. flush_qp(qhp);
  1410. break;
  1411. default:
  1412. ret = -EINVAL;
  1413. goto out;
  1414. }
  1415. break;
  1416. case C4IW_QP_STATE_RTS:
  1417. switch (attrs->next_state) {
  1418. case C4IW_QP_STATE_CLOSING:
  1419. BUG_ON(kref_read(&qhp->ep->com.kref) < 2);
  1420. t4_set_wq_in_error(&qhp->wq);
  1421. set_state(qhp, C4IW_QP_STATE_CLOSING);
  1422. ep = qhp->ep;
  1423. if (!internal) {
  1424. abort = 0;
  1425. disconnect = 1;
  1426. c4iw_get_ep(&qhp->ep->com);
  1427. }
  1428. ret = rdma_fini(rhp, qhp, ep);
  1429. if (ret)
  1430. goto err;
  1431. break;
  1432. case C4IW_QP_STATE_TERMINATE:
  1433. t4_set_wq_in_error(&qhp->wq);
  1434. set_state(qhp, C4IW_QP_STATE_TERMINATE);
  1435. qhp->attr.layer_etype = attrs->layer_etype;
  1436. qhp->attr.ecode = attrs->ecode;
  1437. ep = qhp->ep;
  1438. if (!internal) {
  1439. c4iw_get_ep(&qhp->ep->com);
  1440. terminate = 1;
  1441. disconnect = 1;
  1442. } else {
  1443. terminate = qhp->attr.send_term;
  1444. ret = rdma_fini(rhp, qhp, ep);
  1445. if (ret)
  1446. goto err;
  1447. }
  1448. break;
  1449. case C4IW_QP_STATE_ERROR:
  1450. t4_set_wq_in_error(&qhp->wq);
  1451. set_state(qhp, C4IW_QP_STATE_ERROR);
  1452. if (!internal) {
  1453. abort = 1;
  1454. disconnect = 1;
  1455. ep = qhp->ep;
  1456. c4iw_get_ep(&qhp->ep->com);
  1457. }
  1458. goto err;
  1459. break;
  1460. default:
  1461. ret = -EINVAL;
  1462. goto out;
  1463. }
  1464. break;
  1465. case C4IW_QP_STATE_CLOSING:
  1466. /*
  1467. * Allow kernel users to move to ERROR for qp draining.
  1468. */
  1469. if (!internal && (qhp->ibqp.uobject || attrs->next_state !=
  1470. C4IW_QP_STATE_ERROR)) {
  1471. ret = -EINVAL;
  1472. goto out;
  1473. }
  1474. switch (attrs->next_state) {
  1475. case C4IW_QP_STATE_IDLE:
  1476. flush_qp(qhp);
  1477. set_state(qhp, C4IW_QP_STATE_IDLE);
  1478. qhp->attr.llp_stream_handle = NULL;
  1479. c4iw_put_ep(&qhp->ep->com);
  1480. qhp->ep = NULL;
  1481. wake_up(&qhp->wait);
  1482. break;
  1483. case C4IW_QP_STATE_ERROR:
  1484. goto err;
  1485. default:
  1486. ret = -EINVAL;
  1487. goto err;
  1488. }
  1489. break;
  1490. case C4IW_QP_STATE_ERROR:
  1491. if (attrs->next_state != C4IW_QP_STATE_IDLE) {
  1492. ret = -EINVAL;
  1493. goto out;
  1494. }
  1495. if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
  1496. ret = -EINVAL;
  1497. goto out;
  1498. }
  1499. set_state(qhp, C4IW_QP_STATE_IDLE);
  1500. break;
  1501. case C4IW_QP_STATE_TERMINATE:
  1502. if (!internal) {
  1503. ret = -EINVAL;
  1504. goto out;
  1505. }
  1506. goto err;
  1507. break;
  1508. default:
  1509. pr_err("%s in a bad state %d\n", __func__, qhp->attr.state);
  1510. ret = -EINVAL;
  1511. goto err;
  1512. break;
  1513. }
  1514. goto out;
  1515. err:
  1516. pr_debug("disassociating ep %p qpid 0x%x\n", qhp->ep,
  1517. qhp->wq.sq.qid);
  1518. /* disassociate the LLP connection */
  1519. qhp->attr.llp_stream_handle = NULL;
  1520. if (!ep)
  1521. ep = qhp->ep;
  1522. qhp->ep = NULL;
  1523. set_state(qhp, C4IW_QP_STATE_ERROR);
  1524. free = 1;
  1525. abort = 1;
  1526. BUG_ON(!ep);
  1527. flush_qp(qhp);
  1528. wake_up(&qhp->wait);
  1529. out:
  1530. mutex_unlock(&qhp->mutex);
  1531. if (terminate)
  1532. post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
  1533. /*
  1534. * If disconnect is 1, then we need to initiate a disconnect
  1535. * on the EP. This can be a normal close (RTS->CLOSING) or
  1536. * an abnormal close (RTS/CLOSING->ERROR).
  1537. */
  1538. if (disconnect) {
  1539. c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
  1540. GFP_KERNEL);
  1541. c4iw_put_ep(&ep->com);
  1542. }
  1543. /*
  1544. * If free is 1, then we've disassociated the EP from the QP
  1545. * and we need to dereference the EP.
  1546. */
  1547. if (free)
  1548. c4iw_put_ep(&ep->com);
  1549. pr_debug("exit state %d\n", qhp->attr.state);
  1550. return ret;
  1551. }
  1552. int c4iw_destroy_qp(struct ib_qp *ib_qp)
  1553. {
  1554. struct c4iw_dev *rhp;
  1555. struct c4iw_qp *qhp;
  1556. struct c4iw_qp_attributes attrs;
  1557. qhp = to_c4iw_qp(ib_qp);
  1558. rhp = qhp->rhp;
  1559. attrs.next_state = C4IW_QP_STATE_ERROR;
  1560. if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
  1561. c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  1562. else
  1563. c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
  1564. wait_event(qhp->wait, !qhp->ep);
  1565. remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
  1566. spin_lock_irq(&rhp->lock);
  1567. if (!list_empty(&qhp->db_fc_entry))
  1568. list_del_init(&qhp->db_fc_entry);
  1569. spin_unlock_irq(&rhp->lock);
  1570. free_ird(rhp, qhp->attr.max_ird);
  1571. c4iw_qp_rem_ref(ib_qp);
  1572. pr_debug("ib_qp %p qpid 0x%0x\n", ib_qp, qhp->wq.sq.qid);
  1573. return 0;
  1574. }
  1575. struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
  1576. struct ib_udata *udata)
  1577. {
  1578. struct c4iw_dev *rhp;
  1579. struct c4iw_qp *qhp;
  1580. struct c4iw_pd *php;
  1581. struct c4iw_cq *schp;
  1582. struct c4iw_cq *rchp;
  1583. struct c4iw_create_qp_resp uresp;
  1584. unsigned int sqsize, rqsize;
  1585. struct c4iw_ucontext *ucontext;
  1586. int ret;
  1587. struct c4iw_mm_entry *sq_key_mm, *rq_key_mm = NULL, *sq_db_key_mm;
  1588. struct c4iw_mm_entry *rq_db_key_mm = NULL, *ma_sync_key_mm = NULL;
  1589. pr_debug("ib_pd %p\n", pd);
  1590. if (attrs->qp_type != IB_QPT_RC)
  1591. return ERR_PTR(-EINVAL);
  1592. php = to_c4iw_pd(pd);
  1593. rhp = php->rhp;
  1594. schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
  1595. rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
  1596. if (!schp || !rchp)
  1597. return ERR_PTR(-EINVAL);
  1598. if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
  1599. return ERR_PTR(-EINVAL);
  1600. if (attrs->cap.max_recv_wr > rhp->rdev.hw_queue.t4_max_rq_size)
  1601. return ERR_PTR(-E2BIG);
  1602. rqsize = attrs->cap.max_recv_wr + 1;
  1603. if (rqsize < 8)
  1604. rqsize = 8;
  1605. if (attrs->cap.max_send_wr > rhp->rdev.hw_queue.t4_max_sq_size)
  1606. return ERR_PTR(-E2BIG);
  1607. sqsize = attrs->cap.max_send_wr + 1;
  1608. if (sqsize < 8)
  1609. sqsize = 8;
  1610. ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
  1611. qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
  1612. if (!qhp)
  1613. return ERR_PTR(-ENOMEM);
  1614. qhp->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
  1615. if (!qhp->wr_waitp) {
  1616. ret = -ENOMEM;
  1617. goto err_free_qhp;
  1618. }
  1619. qhp->wq.sq.size = sqsize;
  1620. qhp->wq.sq.memsize =
  1621. (sqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
  1622. sizeof(*qhp->wq.sq.queue) + 16 * sizeof(__be64);
  1623. qhp->wq.sq.flush_cidx = -1;
  1624. qhp->wq.rq.size = rqsize;
  1625. qhp->wq.rq.memsize =
  1626. (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
  1627. sizeof(*qhp->wq.rq.queue);
  1628. if (ucontext) {
  1629. qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
  1630. qhp->wq.rq.memsize = roundup(qhp->wq.rq.memsize, PAGE_SIZE);
  1631. }
  1632. ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
  1633. ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
  1634. qhp->wr_waitp);
  1635. if (ret)
  1636. goto err_free_wr_wait;
  1637. attrs->cap.max_recv_wr = rqsize - 1;
  1638. attrs->cap.max_send_wr = sqsize - 1;
  1639. attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
  1640. qhp->rhp = rhp;
  1641. qhp->attr.pd = php->pdid;
  1642. qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
  1643. qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
  1644. qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
  1645. qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
  1646. qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
  1647. qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
  1648. qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
  1649. qhp->attr.state = C4IW_QP_STATE_IDLE;
  1650. qhp->attr.next_state = C4IW_QP_STATE_IDLE;
  1651. qhp->attr.enable_rdma_read = 1;
  1652. qhp->attr.enable_rdma_write = 1;
  1653. qhp->attr.enable_bind = 1;
  1654. qhp->attr.max_ord = 0;
  1655. qhp->attr.max_ird = 0;
  1656. qhp->sq_sig_all = attrs->sq_sig_type == IB_SIGNAL_ALL_WR;
  1657. spin_lock_init(&qhp->lock);
  1658. mutex_init(&qhp->mutex);
  1659. init_waitqueue_head(&qhp->wait);
  1660. kref_init(&qhp->kref);
  1661. INIT_WORK(&qhp->free_work, free_qp_work);
  1662. ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
  1663. if (ret)
  1664. goto err_destroy_qp;
  1665. if (udata) {
  1666. sq_key_mm = kmalloc(sizeof(*sq_key_mm), GFP_KERNEL);
  1667. if (!sq_key_mm) {
  1668. ret = -ENOMEM;
  1669. goto err_remove_handle;
  1670. }
  1671. rq_key_mm = kmalloc(sizeof(*rq_key_mm), GFP_KERNEL);
  1672. if (!rq_key_mm) {
  1673. ret = -ENOMEM;
  1674. goto err_free_sq_key;
  1675. }
  1676. sq_db_key_mm = kmalloc(sizeof(*sq_db_key_mm), GFP_KERNEL);
  1677. if (!sq_db_key_mm) {
  1678. ret = -ENOMEM;
  1679. goto err_free_rq_key;
  1680. }
  1681. rq_db_key_mm = kmalloc(sizeof(*rq_db_key_mm), GFP_KERNEL);
  1682. if (!rq_db_key_mm) {
  1683. ret = -ENOMEM;
  1684. goto err_free_sq_db_key;
  1685. }
  1686. if (t4_sq_onchip(&qhp->wq.sq)) {
  1687. ma_sync_key_mm = kmalloc(sizeof(*ma_sync_key_mm),
  1688. GFP_KERNEL);
  1689. if (!ma_sync_key_mm) {
  1690. ret = -ENOMEM;
  1691. goto err_free_rq_db_key;
  1692. }
  1693. uresp.flags = C4IW_QPF_ONCHIP;
  1694. } else
  1695. uresp.flags = 0;
  1696. uresp.qid_mask = rhp->rdev.qpmask;
  1697. uresp.sqid = qhp->wq.sq.qid;
  1698. uresp.sq_size = qhp->wq.sq.size;
  1699. uresp.sq_memsize = qhp->wq.sq.memsize;
  1700. uresp.rqid = qhp->wq.rq.qid;
  1701. uresp.rq_size = qhp->wq.rq.size;
  1702. uresp.rq_memsize = qhp->wq.rq.memsize;
  1703. spin_lock(&ucontext->mmap_lock);
  1704. if (ma_sync_key_mm) {
  1705. uresp.ma_sync_key = ucontext->key;
  1706. ucontext->key += PAGE_SIZE;
  1707. } else {
  1708. uresp.ma_sync_key = 0;
  1709. }
  1710. uresp.sq_key = ucontext->key;
  1711. ucontext->key += PAGE_SIZE;
  1712. uresp.rq_key = ucontext->key;
  1713. ucontext->key += PAGE_SIZE;
  1714. uresp.sq_db_gts_key = ucontext->key;
  1715. ucontext->key += PAGE_SIZE;
  1716. uresp.rq_db_gts_key = ucontext->key;
  1717. ucontext->key += PAGE_SIZE;
  1718. spin_unlock(&ucontext->mmap_lock);
  1719. ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
  1720. if (ret)
  1721. goto err_free_ma_sync_key;
  1722. sq_key_mm->key = uresp.sq_key;
  1723. sq_key_mm->addr = qhp->wq.sq.phys_addr;
  1724. sq_key_mm->len = PAGE_ALIGN(qhp->wq.sq.memsize);
  1725. insert_mmap(ucontext, sq_key_mm);
  1726. rq_key_mm->key = uresp.rq_key;
  1727. rq_key_mm->addr = virt_to_phys(qhp->wq.rq.queue);
  1728. rq_key_mm->len = PAGE_ALIGN(qhp->wq.rq.memsize);
  1729. insert_mmap(ucontext, rq_key_mm);
  1730. sq_db_key_mm->key = uresp.sq_db_gts_key;
  1731. sq_db_key_mm->addr = (u64)(unsigned long)qhp->wq.sq.bar2_pa;
  1732. sq_db_key_mm->len = PAGE_SIZE;
  1733. insert_mmap(ucontext, sq_db_key_mm);
  1734. rq_db_key_mm->key = uresp.rq_db_gts_key;
  1735. rq_db_key_mm->addr = (u64)(unsigned long)qhp->wq.rq.bar2_pa;
  1736. rq_db_key_mm->len = PAGE_SIZE;
  1737. insert_mmap(ucontext, rq_db_key_mm);
  1738. if (ma_sync_key_mm) {
  1739. ma_sync_key_mm->key = uresp.ma_sync_key;
  1740. ma_sync_key_mm->addr =
  1741. (pci_resource_start(rhp->rdev.lldi.pdev, 0) +
  1742. PCIE_MA_SYNC_A) & PAGE_MASK;
  1743. ma_sync_key_mm->len = PAGE_SIZE;
  1744. insert_mmap(ucontext, ma_sync_key_mm);
  1745. }
  1746. c4iw_get_ucontext(ucontext);
  1747. qhp->ucontext = ucontext;
  1748. }
  1749. qhp->ibqp.qp_num = qhp->wq.sq.qid;
  1750. init_timer(&(qhp->timer));
  1751. INIT_LIST_HEAD(&qhp->db_fc_entry);
  1752. pr_debug("sq id %u size %u memsize %zu num_entries %u rq id %u size %u memsize %zu num_entries %u\n",
  1753. qhp->wq.sq.qid, qhp->wq.sq.size, qhp->wq.sq.memsize,
  1754. attrs->cap.max_send_wr, qhp->wq.rq.qid, qhp->wq.rq.size,
  1755. qhp->wq.rq.memsize, attrs->cap.max_recv_wr);
  1756. return &qhp->ibqp;
  1757. err_free_ma_sync_key:
  1758. kfree(ma_sync_key_mm);
  1759. err_free_rq_db_key:
  1760. kfree(rq_db_key_mm);
  1761. err_free_sq_db_key:
  1762. kfree(sq_db_key_mm);
  1763. err_free_rq_key:
  1764. kfree(rq_key_mm);
  1765. err_free_sq_key:
  1766. kfree(sq_key_mm);
  1767. err_remove_handle:
  1768. remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
  1769. err_destroy_qp:
  1770. destroy_qp(&rhp->rdev, &qhp->wq,
  1771. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  1772. err_free_wr_wait:
  1773. c4iw_put_wr_wait(qhp->wr_waitp);
  1774. err_free_qhp:
  1775. kfree(qhp);
  1776. return ERR_PTR(ret);
  1777. }
  1778. int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1779. int attr_mask, struct ib_udata *udata)
  1780. {
  1781. struct c4iw_dev *rhp;
  1782. struct c4iw_qp *qhp;
  1783. enum c4iw_qp_attr_mask mask = 0;
  1784. struct c4iw_qp_attributes attrs;
  1785. pr_debug("ib_qp %p\n", ibqp);
  1786. /* iwarp does not support the RTR state */
  1787. if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
  1788. attr_mask &= ~IB_QP_STATE;
  1789. /* Make sure we still have something left to do */
  1790. if (!attr_mask)
  1791. return 0;
  1792. memset(&attrs, 0, sizeof attrs);
  1793. qhp = to_c4iw_qp(ibqp);
  1794. rhp = qhp->rhp;
  1795. attrs.next_state = c4iw_convert_state(attr->qp_state);
  1796. attrs.enable_rdma_read = (attr->qp_access_flags &
  1797. IB_ACCESS_REMOTE_READ) ? 1 : 0;
  1798. attrs.enable_rdma_write = (attr->qp_access_flags &
  1799. IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  1800. attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
  1801. mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
  1802. mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
  1803. (C4IW_QP_ATTR_ENABLE_RDMA_READ |
  1804. C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
  1805. C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
  1806. /*
  1807. * Use SQ_PSN and RQ_PSN to pass in IDX_INC values for
  1808. * ringing the queue db when we're in DB_FULL mode.
  1809. * Only allow this on T4 devices.
  1810. */
  1811. attrs.sq_db_inc = attr->sq_psn;
  1812. attrs.rq_db_inc = attr->rq_psn;
  1813. mask |= (attr_mask & IB_QP_SQ_PSN) ? C4IW_QP_ATTR_SQ_DB : 0;
  1814. mask |= (attr_mask & IB_QP_RQ_PSN) ? C4IW_QP_ATTR_RQ_DB : 0;
  1815. if (!is_t4(to_c4iw_qp(ibqp)->rhp->rdev.lldi.adapter_type) &&
  1816. (mask & (C4IW_QP_ATTR_SQ_DB|C4IW_QP_ATTR_RQ_DB)))
  1817. return -EINVAL;
  1818. return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
  1819. }
  1820. struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
  1821. {
  1822. pr_debug("ib_dev %p qpn 0x%x\n", dev, qpn);
  1823. return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
  1824. }
  1825. int c4iw_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1826. int attr_mask, struct ib_qp_init_attr *init_attr)
  1827. {
  1828. struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
  1829. memset(attr, 0, sizeof *attr);
  1830. memset(init_attr, 0, sizeof *init_attr);
  1831. attr->qp_state = to_ib_qp_state(qhp->attr.state);
  1832. init_attr->cap.max_send_wr = qhp->attr.sq_num_entries;
  1833. init_attr->cap.max_recv_wr = qhp->attr.rq_num_entries;
  1834. init_attr->cap.max_send_sge = qhp->attr.sq_max_sges;
  1835. init_attr->cap.max_recv_sge = qhp->attr.sq_max_sges;
  1836. init_attr->cap.max_inline_data = T4_MAX_SEND_INLINE;
  1837. init_attr->sq_sig_type = qhp->sq_sig_all ? IB_SIGNAL_ALL_WR : 0;
  1838. return 0;
  1839. }