qp.c 52 KB

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