qp.c 51 KB

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