qp.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849
  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, int has_rq)
  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. dealloc_sq(rdev, &wq->sq);
  137. kfree(wq->sq.sw_sq);
  138. c4iw_put_qpid(rdev, wq->sq.qid, uctx);
  139. if (has_rq) {
  140. dma_free_coherent(&rdev->lldi.pdev->dev,
  141. wq->rq.memsize, wq->rq.queue,
  142. dma_unmap_addr(&wq->rq, mapping));
  143. c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
  144. kfree(wq->rq.sw_rq);
  145. c4iw_put_qpid(rdev, wq->rq.qid, uctx);
  146. }
  147. return 0;
  148. }
  149. /*
  150. * Determine the BAR2 virtual address and qid. If pbar2_pa is not NULL,
  151. * then this is a user mapping so compute the page-aligned physical address
  152. * for mapping.
  153. */
  154. void __iomem *c4iw_bar2_addrs(struct c4iw_rdev *rdev, unsigned int qid,
  155. enum cxgb4_bar2_qtype qtype,
  156. unsigned int *pbar2_qid, u64 *pbar2_pa)
  157. {
  158. u64 bar2_qoffset;
  159. int ret;
  160. ret = cxgb4_bar2_sge_qregs(rdev->lldi.ports[0], qid, qtype,
  161. pbar2_pa ? 1 : 0,
  162. &bar2_qoffset, pbar2_qid);
  163. if (ret)
  164. return NULL;
  165. if (pbar2_pa)
  166. *pbar2_pa = (rdev->bar2_pa + bar2_qoffset) & PAGE_MASK;
  167. if (is_t4(rdev->lldi.adapter_type))
  168. return NULL;
  169. return rdev->bar2_kva + bar2_qoffset;
  170. }
  171. static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
  172. struct t4_cq *rcq, struct t4_cq *scq,
  173. struct c4iw_dev_ucontext *uctx,
  174. struct c4iw_wr_wait *wr_waitp,
  175. int need_rq)
  176. {
  177. int user = (uctx != &rdev->uctx);
  178. struct fw_ri_res_wr *res_wr;
  179. struct fw_ri_res *res;
  180. int wr_len;
  181. struct sk_buff *skb;
  182. int ret = 0;
  183. int eqsize;
  184. wq->sq.qid = c4iw_get_qpid(rdev, uctx);
  185. if (!wq->sq.qid)
  186. return -ENOMEM;
  187. if (need_rq) {
  188. wq->rq.qid = c4iw_get_qpid(rdev, uctx);
  189. if (!wq->rq.qid) {
  190. ret = -ENOMEM;
  191. goto free_sq_qid;
  192. }
  193. }
  194. if (!user) {
  195. wq->sq.sw_sq = kcalloc(wq->sq.size, sizeof(*wq->sq.sw_sq),
  196. GFP_KERNEL);
  197. if (!wq->sq.sw_sq) {
  198. ret = -ENOMEM;
  199. goto free_rq_qid;//FIXME
  200. }
  201. if (need_rq) {
  202. wq->rq.sw_rq = kcalloc(wq->rq.size,
  203. sizeof(*wq->rq.sw_rq),
  204. GFP_KERNEL);
  205. if (!wq->rq.sw_rq) {
  206. ret = -ENOMEM;
  207. goto free_sw_sq;
  208. }
  209. }
  210. }
  211. if (need_rq) {
  212. /*
  213. * RQT must be a power of 2 and at least 16 deep.
  214. */
  215. wq->rq.rqt_size =
  216. roundup_pow_of_two(max_t(u16, wq->rq.size, 16));
  217. wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
  218. if (!wq->rq.rqt_hwaddr) {
  219. ret = -ENOMEM;
  220. goto free_sw_rq;
  221. }
  222. }
  223. ret = alloc_sq(rdev, &wq->sq, user);
  224. if (ret)
  225. goto free_hwaddr;
  226. memset(wq->sq.queue, 0, wq->sq.memsize);
  227. dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
  228. if (need_rq) {
  229. wq->rq.queue = dma_alloc_coherent(&rdev->lldi.pdev->dev,
  230. wq->rq.memsize,
  231. &wq->rq.dma_addr,
  232. GFP_KERNEL);
  233. if (!wq->rq.queue) {
  234. ret = -ENOMEM;
  235. goto free_sq;
  236. }
  237. pr_debug("sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx\n",
  238. wq->sq.queue,
  239. (unsigned long long)virt_to_phys(wq->sq.queue),
  240. wq->rq.queue,
  241. (unsigned long long)virt_to_phys(wq->rq.queue));
  242. memset(wq->rq.queue, 0, wq->rq.memsize);
  243. dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
  244. }
  245. wq->db = rdev->lldi.db_reg;
  246. wq->sq.bar2_va = c4iw_bar2_addrs(rdev, wq->sq.qid,
  247. CXGB4_BAR2_QTYPE_EGRESS,
  248. &wq->sq.bar2_qid,
  249. user ? &wq->sq.bar2_pa : NULL);
  250. if (need_rq)
  251. wq->rq.bar2_va = c4iw_bar2_addrs(rdev, wq->rq.qid,
  252. CXGB4_BAR2_QTYPE_EGRESS,
  253. &wq->rq.bar2_qid,
  254. user ? &wq->rq.bar2_pa : NULL);
  255. /*
  256. * User mode must have bar2 access.
  257. */
  258. if (user && (!wq->sq.bar2_pa || (need_rq && !wq->rq.bar2_pa))) {
  259. pr_warn("%s: sqid %u or rqid %u not in BAR2 range\n",
  260. pci_name(rdev->lldi.pdev), wq->sq.qid, wq->rq.qid);
  261. goto free_dma;
  262. }
  263. wq->rdev = rdev;
  264. wq->rq.msn = 1;
  265. /* build fw_ri_res_wr */
  266. wr_len = sizeof *res_wr + 2 * sizeof *res;
  267. if (need_rq)
  268. wr_len += sizeof(*res);
  269. skb = alloc_skb(wr_len, GFP_KERNEL);
  270. if (!skb) {
  271. ret = -ENOMEM;
  272. goto free_dma;
  273. }
  274. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  275. res_wr = __skb_put_zero(skb, wr_len);
  276. res_wr->op_nres = cpu_to_be32(
  277. FW_WR_OP_V(FW_RI_RES_WR) |
  278. FW_RI_RES_WR_NRES_V(need_rq ? 2 : 1) |
  279. FW_WR_COMPL_F);
  280. res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
  281. res_wr->cookie = (uintptr_t)wr_waitp;
  282. res = res_wr->res;
  283. res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
  284. res->u.sqrq.op = FW_RI_RES_OP_WRITE;
  285. /*
  286. * eqsize is the number of 64B entries plus the status page size.
  287. */
  288. eqsize = wq->sq.size * T4_SQ_NUM_SLOTS +
  289. rdev->hw_queue.t4_eq_status_entries;
  290. res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
  291. FW_RI_RES_WR_HOSTFCMODE_V(0) | /* no host cidx updates */
  292. FW_RI_RES_WR_CPRIO_V(0) | /* don't keep in chip cache */
  293. FW_RI_RES_WR_PCIECHN_V(0) | /* set by uP at ri_init time */
  294. (t4_sq_onchip(&wq->sq) ? FW_RI_RES_WR_ONCHIP_F : 0) |
  295. FW_RI_RES_WR_IQID_V(scq->cqid));
  296. res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
  297. FW_RI_RES_WR_DCAEN_V(0) |
  298. FW_RI_RES_WR_DCACPU_V(0) |
  299. FW_RI_RES_WR_FBMIN_V(2) |
  300. (t4_sq_onchip(&wq->sq) ? FW_RI_RES_WR_FBMAX_V(2) :
  301. FW_RI_RES_WR_FBMAX_V(3)) |
  302. FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
  303. FW_RI_RES_WR_CIDXFTHRESH_V(0) |
  304. FW_RI_RES_WR_EQSIZE_V(eqsize));
  305. res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
  306. res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
  307. if (need_rq) {
  308. res++;
  309. res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
  310. res->u.sqrq.op = FW_RI_RES_OP_WRITE;
  311. /*
  312. * eqsize is the number of 64B entries plus the status page size
  313. */
  314. eqsize = wq->rq.size * T4_RQ_NUM_SLOTS +
  315. rdev->hw_queue.t4_eq_status_entries;
  316. res->u.sqrq.fetchszm_to_iqid =
  317. /* no host cidx updates */
  318. cpu_to_be32(FW_RI_RES_WR_HOSTFCMODE_V(0) |
  319. /* don't keep in chip cache */
  320. FW_RI_RES_WR_CPRIO_V(0) |
  321. /* set by uP at ri_init time */
  322. FW_RI_RES_WR_PCIECHN_V(0) |
  323. FW_RI_RES_WR_IQID_V(rcq->cqid));
  324. res->u.sqrq.dcaen_to_eqsize =
  325. cpu_to_be32(FW_RI_RES_WR_DCAEN_V(0) |
  326. FW_RI_RES_WR_DCACPU_V(0) |
  327. FW_RI_RES_WR_FBMIN_V(2) |
  328. FW_RI_RES_WR_FBMAX_V(3) |
  329. FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
  330. FW_RI_RES_WR_CIDXFTHRESH_V(0) |
  331. FW_RI_RES_WR_EQSIZE_V(eqsize));
  332. res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
  333. res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
  334. }
  335. c4iw_init_wr_wait(wr_waitp);
  336. ret = c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, wq->sq.qid, __func__);
  337. if (ret)
  338. goto free_dma;
  339. pr_debug("sqid 0x%x rqid 0x%x kdb 0x%p sq_bar2_addr %p rq_bar2_addr %p\n",
  340. wq->sq.qid, wq->rq.qid, wq->db,
  341. wq->sq.bar2_va, wq->rq.bar2_va);
  342. return 0;
  343. free_dma:
  344. if (need_rq)
  345. dma_free_coherent(&rdev->lldi.pdev->dev,
  346. wq->rq.memsize, wq->rq.queue,
  347. dma_unmap_addr(&wq->rq, mapping));
  348. free_sq:
  349. dealloc_sq(rdev, &wq->sq);
  350. free_hwaddr:
  351. if (need_rq)
  352. c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
  353. free_sw_rq:
  354. if (need_rq)
  355. kfree(wq->rq.sw_rq);
  356. free_sw_sq:
  357. kfree(wq->sq.sw_sq);
  358. free_rq_qid:
  359. if (need_rq)
  360. c4iw_put_qpid(rdev, wq->rq.qid, uctx);
  361. free_sq_qid:
  362. c4iw_put_qpid(rdev, wq->sq.qid, uctx);
  363. return ret;
  364. }
  365. static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
  366. const struct ib_send_wr *wr, int max, u32 *plenp)
  367. {
  368. u8 *dstp, *srcp;
  369. u32 plen = 0;
  370. int i;
  371. int rem, len;
  372. dstp = (u8 *)immdp->data;
  373. for (i = 0; i < wr->num_sge; i++) {
  374. if ((plen + wr->sg_list[i].length) > max)
  375. return -EMSGSIZE;
  376. srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
  377. plen += wr->sg_list[i].length;
  378. rem = wr->sg_list[i].length;
  379. while (rem) {
  380. if (dstp == (u8 *)&sq->queue[sq->size])
  381. dstp = (u8 *)sq->queue;
  382. if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
  383. len = rem;
  384. else
  385. len = (u8 *)&sq->queue[sq->size] - dstp;
  386. memcpy(dstp, srcp, len);
  387. dstp += len;
  388. srcp += len;
  389. rem -= len;
  390. }
  391. }
  392. len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
  393. if (len)
  394. memset(dstp, 0, len);
  395. immdp->op = FW_RI_DATA_IMMD;
  396. immdp->r1 = 0;
  397. immdp->r2 = 0;
  398. immdp->immdlen = cpu_to_be32(plen);
  399. *plenp = plen;
  400. return 0;
  401. }
  402. static int build_isgl(__be64 *queue_start, __be64 *queue_end,
  403. struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
  404. int num_sge, u32 *plenp)
  405. {
  406. int i;
  407. u32 plen = 0;
  408. __be64 *flitp;
  409. if ((__be64 *)isglp == queue_end)
  410. isglp = (struct fw_ri_isgl *)queue_start;
  411. flitp = (__be64 *)isglp->sge;
  412. for (i = 0; i < num_sge; i++) {
  413. if ((plen + sg_list[i].length) < plen)
  414. return -EMSGSIZE;
  415. plen += sg_list[i].length;
  416. *flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
  417. sg_list[i].length);
  418. if (++flitp == queue_end)
  419. flitp = queue_start;
  420. *flitp = cpu_to_be64(sg_list[i].addr);
  421. if (++flitp == queue_end)
  422. flitp = queue_start;
  423. }
  424. *flitp = (__force __be64)0;
  425. isglp->op = FW_RI_DATA_ISGL;
  426. isglp->r1 = 0;
  427. isglp->nsge = cpu_to_be16(num_sge);
  428. isglp->r2 = 0;
  429. if (plenp)
  430. *plenp = plen;
  431. return 0;
  432. }
  433. static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
  434. const struct ib_send_wr *wr, u8 *len16)
  435. {
  436. u32 plen;
  437. int size;
  438. int ret;
  439. if (wr->num_sge > T4_MAX_SEND_SGE)
  440. return -EINVAL;
  441. switch (wr->opcode) {
  442. case IB_WR_SEND:
  443. if (wr->send_flags & IB_SEND_SOLICITED)
  444. wqe->send.sendop_pkd = cpu_to_be32(
  445. FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_SE));
  446. else
  447. wqe->send.sendop_pkd = cpu_to_be32(
  448. FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND));
  449. wqe->send.stag_inv = 0;
  450. break;
  451. case IB_WR_SEND_WITH_INV:
  452. if (wr->send_flags & IB_SEND_SOLICITED)
  453. wqe->send.sendop_pkd = cpu_to_be32(
  454. FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_SE_INV));
  455. else
  456. wqe->send.sendop_pkd = cpu_to_be32(
  457. FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_INV));
  458. wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
  459. break;
  460. default:
  461. return -EINVAL;
  462. }
  463. wqe->send.r3 = 0;
  464. wqe->send.r4 = 0;
  465. plen = 0;
  466. if (wr->num_sge) {
  467. if (wr->send_flags & IB_SEND_INLINE) {
  468. ret = build_immd(sq, wqe->send.u.immd_src, wr,
  469. T4_MAX_SEND_INLINE, &plen);
  470. if (ret)
  471. return ret;
  472. size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
  473. plen;
  474. } else {
  475. ret = build_isgl((__be64 *)sq->queue,
  476. (__be64 *)&sq->queue[sq->size],
  477. wqe->send.u.isgl_src,
  478. wr->sg_list, wr->num_sge, &plen);
  479. if (ret)
  480. return ret;
  481. size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
  482. wr->num_sge * sizeof(struct fw_ri_sge);
  483. }
  484. } else {
  485. wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
  486. wqe->send.u.immd_src[0].r1 = 0;
  487. wqe->send.u.immd_src[0].r2 = 0;
  488. wqe->send.u.immd_src[0].immdlen = 0;
  489. size = sizeof wqe->send + sizeof(struct fw_ri_immd);
  490. plen = 0;
  491. }
  492. *len16 = DIV_ROUND_UP(size, 16);
  493. wqe->send.plen = cpu_to_be32(plen);
  494. return 0;
  495. }
  496. static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
  497. const struct ib_send_wr *wr, u8 *len16)
  498. {
  499. u32 plen;
  500. int size;
  501. int ret;
  502. if (wr->num_sge > T4_MAX_SEND_SGE)
  503. return -EINVAL;
  504. /*
  505. * iWARP protocol supports 64 bit immediate data but rdma api
  506. * limits it to 32bit.
  507. */
  508. if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  509. wqe->write.iw_imm_data.ib_imm_data.imm_data32 = wr->ex.imm_data;
  510. else
  511. wqe->write.iw_imm_data.ib_imm_data.imm_data32 = 0;
  512. wqe->write.stag_sink = cpu_to_be32(rdma_wr(wr)->rkey);
  513. wqe->write.to_sink = cpu_to_be64(rdma_wr(wr)->remote_addr);
  514. if (wr->num_sge) {
  515. if (wr->send_flags & IB_SEND_INLINE) {
  516. ret = build_immd(sq, wqe->write.u.immd_src, wr,
  517. T4_MAX_WRITE_INLINE, &plen);
  518. if (ret)
  519. return ret;
  520. size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
  521. plen;
  522. } else {
  523. ret = build_isgl((__be64 *)sq->queue,
  524. (__be64 *)&sq->queue[sq->size],
  525. wqe->write.u.isgl_src,
  526. wr->sg_list, wr->num_sge, &plen);
  527. if (ret)
  528. return ret;
  529. size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
  530. wr->num_sge * sizeof(struct fw_ri_sge);
  531. }
  532. } else {
  533. wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
  534. wqe->write.u.immd_src[0].r1 = 0;
  535. wqe->write.u.immd_src[0].r2 = 0;
  536. wqe->write.u.immd_src[0].immdlen = 0;
  537. size = sizeof wqe->write + sizeof(struct fw_ri_immd);
  538. plen = 0;
  539. }
  540. *len16 = DIV_ROUND_UP(size, 16);
  541. wqe->write.plen = cpu_to_be32(plen);
  542. return 0;
  543. }
  544. static void build_immd_cmpl(struct t4_sq *sq, struct fw_ri_immd_cmpl *immdp,
  545. struct ib_send_wr *wr)
  546. {
  547. memcpy((u8 *)immdp->data, (u8 *)(uintptr_t)wr->sg_list->addr, 16);
  548. memset(immdp->r1, 0, 6);
  549. immdp->op = FW_RI_DATA_IMMD;
  550. immdp->immdlen = 16;
  551. }
  552. static void build_rdma_write_cmpl(struct t4_sq *sq,
  553. struct fw_ri_rdma_write_cmpl_wr *wcwr,
  554. const struct ib_send_wr *wr, u8 *len16)
  555. {
  556. u32 plen;
  557. int size;
  558. /*
  559. * This code assumes the struct fields preceding the write isgl
  560. * fit in one 64B WR slot. This is because the WQE is built
  561. * directly in the dma queue, and wrapping is only handled
  562. * by the code buildling sgls. IE the "fixed part" of the wr
  563. * structs must all fit in 64B. The WQE build code should probably be
  564. * redesigned to avoid this restriction, but for now just add
  565. * the BUILD_BUG_ON() to catch if this WQE struct gets too big.
  566. */
  567. BUILD_BUG_ON(offsetof(struct fw_ri_rdma_write_cmpl_wr, u) > 64);
  568. wcwr->stag_sink = cpu_to_be32(rdma_wr(wr)->rkey);
  569. wcwr->to_sink = cpu_to_be64(rdma_wr(wr)->remote_addr);
  570. wcwr->stag_inv = cpu_to_be32(wr->next->ex.invalidate_rkey);
  571. wcwr->r2 = 0;
  572. wcwr->r3 = 0;
  573. /* SEND_INV SGL */
  574. if (wr->next->send_flags & IB_SEND_INLINE)
  575. build_immd_cmpl(sq, &wcwr->u_cmpl.immd_src, wr->next);
  576. else
  577. build_isgl((__be64 *)sq->queue, (__be64 *)&sq->queue[sq->size],
  578. &wcwr->u_cmpl.isgl_src, wr->next->sg_list, 1, NULL);
  579. /* WRITE SGL */
  580. build_isgl((__be64 *)sq->queue, (__be64 *)&sq->queue[sq->size],
  581. wcwr->u.isgl_src, wr->sg_list, wr->num_sge, &plen);
  582. size = sizeof(*wcwr) + sizeof(struct fw_ri_isgl) +
  583. wr->num_sge * sizeof(struct fw_ri_sge);
  584. wcwr->plen = cpu_to_be32(plen);
  585. *len16 = DIV_ROUND_UP(size, 16);
  586. }
  587. static int build_rdma_read(union t4_wr *wqe, const struct ib_send_wr *wr,
  588. u8 *len16)
  589. {
  590. if (wr->num_sge > 1)
  591. return -EINVAL;
  592. if (wr->num_sge && wr->sg_list[0].length) {
  593. wqe->read.stag_src = cpu_to_be32(rdma_wr(wr)->rkey);
  594. wqe->read.to_src_hi = cpu_to_be32((u32)(rdma_wr(wr)->remote_addr
  595. >> 32));
  596. wqe->read.to_src_lo = cpu_to_be32((u32)rdma_wr(wr)->remote_addr);
  597. wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
  598. wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
  599. wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
  600. >> 32));
  601. wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
  602. } else {
  603. wqe->read.stag_src = cpu_to_be32(2);
  604. wqe->read.to_src_hi = 0;
  605. wqe->read.to_src_lo = 0;
  606. wqe->read.stag_sink = cpu_to_be32(2);
  607. wqe->read.plen = 0;
  608. wqe->read.to_sink_hi = 0;
  609. wqe->read.to_sink_lo = 0;
  610. }
  611. wqe->read.r2 = 0;
  612. wqe->read.r5 = 0;
  613. *len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
  614. return 0;
  615. }
  616. static void post_write_cmpl(struct c4iw_qp *qhp, const struct ib_send_wr *wr)
  617. {
  618. bool send_signaled = (wr->next->send_flags & IB_SEND_SIGNALED) ||
  619. qhp->sq_sig_all;
  620. bool write_signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
  621. qhp->sq_sig_all;
  622. struct t4_swsqe *swsqe;
  623. union t4_wr *wqe;
  624. u16 write_wrid;
  625. u8 len16;
  626. u16 idx;
  627. /*
  628. * The sw_sq entries still look like a WRITE and a SEND and consume
  629. * 2 slots. The FW WR, however, will be a single uber-WR.
  630. */
  631. wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
  632. qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
  633. build_rdma_write_cmpl(&qhp->wq.sq, &wqe->write_cmpl, wr, &len16);
  634. /* WRITE swsqe */
  635. swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
  636. swsqe->opcode = FW_RI_RDMA_WRITE;
  637. swsqe->idx = qhp->wq.sq.pidx;
  638. swsqe->complete = 0;
  639. swsqe->signaled = write_signaled;
  640. swsqe->flushed = 0;
  641. swsqe->wr_id = wr->wr_id;
  642. if (c4iw_wr_log) {
  643. swsqe->sge_ts =
  644. cxgb4_read_sge_timestamp(qhp->rhp->rdev.lldi.ports[0]);
  645. swsqe->host_time = ktime_get();
  646. }
  647. write_wrid = qhp->wq.sq.pidx;
  648. /* just bump the sw_sq */
  649. qhp->wq.sq.in_use++;
  650. if (++qhp->wq.sq.pidx == qhp->wq.sq.size)
  651. qhp->wq.sq.pidx = 0;
  652. /* SEND_WITH_INV swsqe */
  653. swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
  654. swsqe->opcode = FW_RI_SEND_WITH_INV;
  655. swsqe->idx = qhp->wq.sq.pidx;
  656. swsqe->complete = 0;
  657. swsqe->signaled = send_signaled;
  658. swsqe->flushed = 0;
  659. swsqe->wr_id = wr->next->wr_id;
  660. if (c4iw_wr_log) {
  661. swsqe->sge_ts =
  662. cxgb4_read_sge_timestamp(qhp->rhp->rdev.lldi.ports[0]);
  663. swsqe->host_time = ktime_get();
  664. }
  665. wqe->write_cmpl.flags_send = send_signaled ? FW_RI_COMPLETION_FLAG : 0;
  666. wqe->write_cmpl.wrid_send = qhp->wq.sq.pidx;
  667. init_wr_hdr(wqe, write_wrid, FW_RI_RDMA_WRITE_CMPL_WR,
  668. write_signaled ? FW_RI_COMPLETION_FLAG : 0, len16);
  669. t4_sq_produce(&qhp->wq, len16);
  670. idx = DIV_ROUND_UP(len16 * 16, T4_EQ_ENTRY_SIZE);
  671. t4_ring_sq_db(&qhp->wq, idx, wqe);
  672. }
  673. static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
  674. const struct ib_recv_wr *wr, u8 *len16)
  675. {
  676. int ret;
  677. ret = build_isgl((__be64 *)qhp->wq.rq.queue,
  678. (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
  679. &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
  680. if (ret)
  681. return ret;
  682. *len16 = DIV_ROUND_UP(sizeof wqe->recv +
  683. wr->num_sge * sizeof(struct fw_ri_sge), 16);
  684. return 0;
  685. }
  686. static int build_srq_recv(union t4_recv_wr *wqe, const struct ib_recv_wr *wr,
  687. u8 *len16)
  688. {
  689. int ret;
  690. ret = build_isgl((__be64 *)wqe, (__be64 *)(wqe + 1),
  691. &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
  692. if (ret)
  693. return ret;
  694. *len16 = DIV_ROUND_UP(sizeof(wqe->recv) +
  695. wr->num_sge * sizeof(struct fw_ri_sge), 16);
  696. return 0;
  697. }
  698. static void build_tpte_memreg(struct fw_ri_fr_nsmr_tpte_wr *fr,
  699. const struct ib_reg_wr *wr, struct c4iw_mr *mhp,
  700. u8 *len16)
  701. {
  702. __be64 *p = (__be64 *)fr->pbl;
  703. fr->r2 = cpu_to_be32(0);
  704. fr->stag = cpu_to_be32(mhp->ibmr.rkey);
  705. fr->tpte.valid_to_pdid = cpu_to_be32(FW_RI_TPTE_VALID_F |
  706. FW_RI_TPTE_STAGKEY_V((mhp->ibmr.rkey & FW_RI_TPTE_STAGKEY_M)) |
  707. FW_RI_TPTE_STAGSTATE_V(1) |
  708. FW_RI_TPTE_STAGTYPE_V(FW_RI_STAG_NSMR) |
  709. FW_RI_TPTE_PDID_V(mhp->attr.pdid));
  710. fr->tpte.locread_to_qpid = cpu_to_be32(
  711. FW_RI_TPTE_PERM_V(c4iw_ib_to_tpt_access(wr->access)) |
  712. FW_RI_TPTE_ADDRTYPE_V(FW_RI_VA_BASED_TO) |
  713. FW_RI_TPTE_PS_V(ilog2(wr->mr->page_size) - 12));
  714. fr->tpte.nosnoop_pbladdr = cpu_to_be32(FW_RI_TPTE_PBLADDR_V(
  715. PBL_OFF(&mhp->rhp->rdev, mhp->attr.pbl_addr)>>3));
  716. fr->tpte.dca_mwbcnt_pstag = cpu_to_be32(0);
  717. fr->tpte.len_hi = cpu_to_be32(0);
  718. fr->tpte.len_lo = cpu_to_be32(mhp->ibmr.length);
  719. fr->tpte.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
  720. fr->tpte.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova & 0xffffffff);
  721. p[0] = cpu_to_be64((u64)mhp->mpl[0]);
  722. p[1] = cpu_to_be64((u64)mhp->mpl[1]);
  723. *len16 = DIV_ROUND_UP(sizeof(*fr), 16);
  724. }
  725. static int build_memreg(struct t4_sq *sq, union t4_wr *wqe,
  726. const struct ib_reg_wr *wr, struct c4iw_mr *mhp,
  727. u8 *len16, bool dsgl_supported)
  728. {
  729. struct fw_ri_immd *imdp;
  730. __be64 *p;
  731. int i;
  732. int pbllen = roundup(mhp->mpl_len * sizeof(u64), 32);
  733. int rem;
  734. if (mhp->mpl_len > t4_max_fr_depth(dsgl_supported && use_dsgl))
  735. return -EINVAL;
  736. wqe->fr.qpbinde_to_dcacpu = 0;
  737. wqe->fr.pgsz_shift = ilog2(wr->mr->page_size) - 12;
  738. wqe->fr.addr_type = FW_RI_VA_BASED_TO;
  739. wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->access);
  740. wqe->fr.len_hi = 0;
  741. wqe->fr.len_lo = cpu_to_be32(mhp->ibmr.length);
  742. wqe->fr.stag = cpu_to_be32(wr->key);
  743. wqe->fr.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
  744. wqe->fr.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova &
  745. 0xffffffff);
  746. if (dsgl_supported && use_dsgl && (pbllen > max_fr_immd)) {
  747. struct fw_ri_dsgl *sglp;
  748. for (i = 0; i < mhp->mpl_len; i++)
  749. mhp->mpl[i] = (__force u64)cpu_to_be64((u64)mhp->mpl[i]);
  750. sglp = (struct fw_ri_dsgl *)(&wqe->fr + 1);
  751. sglp->op = FW_RI_DATA_DSGL;
  752. sglp->r1 = 0;
  753. sglp->nsge = cpu_to_be16(1);
  754. sglp->addr0 = cpu_to_be64(mhp->mpl_addr);
  755. sglp->len0 = cpu_to_be32(pbllen);
  756. *len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*sglp), 16);
  757. } else {
  758. imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
  759. imdp->op = FW_RI_DATA_IMMD;
  760. imdp->r1 = 0;
  761. imdp->r2 = 0;
  762. imdp->immdlen = cpu_to_be32(pbllen);
  763. p = (__be64 *)(imdp + 1);
  764. rem = pbllen;
  765. for (i = 0; i < mhp->mpl_len; i++) {
  766. *p = cpu_to_be64((u64)mhp->mpl[i]);
  767. rem -= sizeof(*p);
  768. if (++p == (__be64 *)&sq->queue[sq->size])
  769. p = (__be64 *)sq->queue;
  770. }
  771. while (rem) {
  772. *p = 0;
  773. rem -= sizeof(*p);
  774. if (++p == (__be64 *)&sq->queue[sq->size])
  775. p = (__be64 *)sq->queue;
  776. }
  777. *len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*imdp)
  778. + pbllen, 16);
  779. }
  780. return 0;
  781. }
  782. static int build_inv_stag(union t4_wr *wqe, const struct ib_send_wr *wr,
  783. u8 *len16)
  784. {
  785. wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
  786. wqe->inv.r2 = 0;
  787. *len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
  788. return 0;
  789. }
  790. static void free_qp_work(struct work_struct *work)
  791. {
  792. struct c4iw_ucontext *ucontext;
  793. struct c4iw_qp *qhp;
  794. struct c4iw_dev *rhp;
  795. qhp = container_of(work, struct c4iw_qp, free_work);
  796. ucontext = qhp->ucontext;
  797. rhp = qhp->rhp;
  798. pr_debug("qhp %p ucontext %p\n", qhp, ucontext);
  799. destroy_qp(&rhp->rdev, &qhp->wq,
  800. ucontext ? &ucontext->uctx : &rhp->rdev.uctx, !qhp->srq);
  801. if (ucontext)
  802. c4iw_put_ucontext(ucontext);
  803. c4iw_put_wr_wait(qhp->wr_waitp);
  804. kfree(qhp);
  805. }
  806. static void queue_qp_free(struct kref *kref)
  807. {
  808. struct c4iw_qp *qhp;
  809. qhp = container_of(kref, struct c4iw_qp, kref);
  810. pr_debug("qhp %p\n", qhp);
  811. queue_work(qhp->rhp->rdev.free_workq, &qhp->free_work);
  812. }
  813. void c4iw_qp_add_ref(struct ib_qp *qp)
  814. {
  815. pr_debug("ib_qp %p\n", qp);
  816. kref_get(&to_c4iw_qp(qp)->kref);
  817. }
  818. void c4iw_qp_rem_ref(struct ib_qp *qp)
  819. {
  820. pr_debug("ib_qp %p\n", qp);
  821. kref_put(&to_c4iw_qp(qp)->kref, queue_qp_free);
  822. }
  823. static void add_to_fc_list(struct list_head *head, struct list_head *entry)
  824. {
  825. if (list_empty(entry))
  826. list_add_tail(entry, head);
  827. }
  828. static int ring_kernel_sq_db(struct c4iw_qp *qhp, u16 inc)
  829. {
  830. unsigned long flags;
  831. spin_lock_irqsave(&qhp->rhp->lock, flags);
  832. spin_lock(&qhp->lock);
  833. if (qhp->rhp->db_state == NORMAL)
  834. t4_ring_sq_db(&qhp->wq, inc, NULL);
  835. else {
  836. add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
  837. qhp->wq.sq.wq_pidx_inc += inc;
  838. }
  839. spin_unlock(&qhp->lock);
  840. spin_unlock_irqrestore(&qhp->rhp->lock, flags);
  841. return 0;
  842. }
  843. static int ring_kernel_rq_db(struct c4iw_qp *qhp, u16 inc)
  844. {
  845. unsigned long flags;
  846. spin_lock_irqsave(&qhp->rhp->lock, flags);
  847. spin_lock(&qhp->lock);
  848. if (qhp->rhp->db_state == NORMAL)
  849. t4_ring_rq_db(&qhp->wq, inc, NULL);
  850. else {
  851. add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
  852. qhp->wq.rq.wq_pidx_inc += inc;
  853. }
  854. spin_unlock(&qhp->lock);
  855. spin_unlock_irqrestore(&qhp->rhp->lock, flags);
  856. return 0;
  857. }
  858. static int ib_to_fw_opcode(int ib_opcode)
  859. {
  860. int opcode;
  861. switch (ib_opcode) {
  862. case IB_WR_SEND_WITH_INV:
  863. opcode = FW_RI_SEND_WITH_INV;
  864. break;
  865. case IB_WR_SEND:
  866. opcode = FW_RI_SEND;
  867. break;
  868. case IB_WR_RDMA_WRITE:
  869. opcode = FW_RI_RDMA_WRITE;
  870. break;
  871. case IB_WR_RDMA_WRITE_WITH_IMM:
  872. opcode = FW_RI_WRITE_IMMEDIATE;
  873. break;
  874. case IB_WR_RDMA_READ:
  875. case IB_WR_RDMA_READ_WITH_INV:
  876. opcode = FW_RI_READ_REQ;
  877. break;
  878. case IB_WR_REG_MR:
  879. opcode = FW_RI_FAST_REGISTER;
  880. break;
  881. case IB_WR_LOCAL_INV:
  882. opcode = FW_RI_LOCAL_INV;
  883. break;
  884. default:
  885. opcode = -EINVAL;
  886. }
  887. return opcode;
  888. }
  889. static int complete_sq_drain_wr(struct c4iw_qp *qhp,
  890. const struct ib_send_wr *wr)
  891. {
  892. struct t4_cqe cqe = {};
  893. struct c4iw_cq *schp;
  894. unsigned long flag;
  895. struct t4_cq *cq;
  896. int opcode;
  897. schp = to_c4iw_cq(qhp->ibqp.send_cq);
  898. cq = &schp->cq;
  899. opcode = ib_to_fw_opcode(wr->opcode);
  900. if (opcode < 0)
  901. return opcode;
  902. cqe.u.drain_cookie = wr->wr_id;
  903. cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
  904. CQE_OPCODE_V(opcode) |
  905. CQE_TYPE_V(1) |
  906. CQE_SWCQE_V(1) |
  907. CQE_DRAIN_V(1) |
  908. CQE_QPID_V(qhp->wq.sq.qid));
  909. spin_lock_irqsave(&schp->lock, flag);
  910. cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
  911. cq->sw_queue[cq->sw_pidx] = cqe;
  912. t4_swcq_produce(cq);
  913. spin_unlock_irqrestore(&schp->lock, flag);
  914. if (t4_clear_cq_armed(&schp->cq)) {
  915. spin_lock_irqsave(&schp->comp_handler_lock, flag);
  916. (*schp->ibcq.comp_handler)(&schp->ibcq,
  917. schp->ibcq.cq_context);
  918. spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
  919. }
  920. return 0;
  921. }
  922. static int complete_sq_drain_wrs(struct c4iw_qp *qhp,
  923. const struct ib_send_wr *wr,
  924. const struct ib_send_wr **bad_wr)
  925. {
  926. int ret = 0;
  927. while (wr) {
  928. ret = complete_sq_drain_wr(qhp, wr);
  929. if (ret) {
  930. *bad_wr = wr;
  931. break;
  932. }
  933. wr = wr->next;
  934. }
  935. return ret;
  936. }
  937. static void complete_rq_drain_wr(struct c4iw_qp *qhp,
  938. const struct ib_recv_wr *wr)
  939. {
  940. struct t4_cqe cqe = {};
  941. struct c4iw_cq *rchp;
  942. unsigned long flag;
  943. struct t4_cq *cq;
  944. rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
  945. cq = &rchp->cq;
  946. cqe.u.drain_cookie = wr->wr_id;
  947. cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
  948. CQE_OPCODE_V(FW_RI_SEND) |
  949. CQE_TYPE_V(0) |
  950. CQE_SWCQE_V(1) |
  951. CQE_DRAIN_V(1) |
  952. CQE_QPID_V(qhp->wq.sq.qid));
  953. spin_lock_irqsave(&rchp->lock, flag);
  954. cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
  955. cq->sw_queue[cq->sw_pidx] = cqe;
  956. t4_swcq_produce(cq);
  957. spin_unlock_irqrestore(&rchp->lock, flag);
  958. if (t4_clear_cq_armed(&rchp->cq)) {
  959. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  960. (*rchp->ibcq.comp_handler)(&rchp->ibcq,
  961. rchp->ibcq.cq_context);
  962. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  963. }
  964. }
  965. static void complete_rq_drain_wrs(struct c4iw_qp *qhp,
  966. const struct ib_recv_wr *wr)
  967. {
  968. while (wr) {
  969. complete_rq_drain_wr(qhp, wr);
  970. wr = wr->next;
  971. }
  972. }
  973. int c4iw_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  974. const struct ib_send_wr **bad_wr)
  975. {
  976. int err = 0;
  977. u8 len16 = 0;
  978. enum fw_wr_opcodes fw_opcode = 0;
  979. enum fw_ri_wr_flags fw_flags;
  980. struct c4iw_qp *qhp;
  981. struct c4iw_dev *rhp;
  982. union t4_wr *wqe = NULL;
  983. u32 num_wrs;
  984. struct t4_swsqe *swsqe;
  985. unsigned long flag;
  986. u16 idx = 0;
  987. qhp = to_c4iw_qp(ibqp);
  988. rhp = qhp->rhp;
  989. spin_lock_irqsave(&qhp->lock, flag);
  990. /*
  991. * If the qp has been flushed, then just insert a special
  992. * drain cqe.
  993. */
  994. if (qhp->wq.flushed) {
  995. spin_unlock_irqrestore(&qhp->lock, flag);
  996. err = complete_sq_drain_wrs(qhp, wr, bad_wr);
  997. return err;
  998. }
  999. num_wrs = t4_sq_avail(&qhp->wq);
  1000. if (num_wrs == 0) {
  1001. spin_unlock_irqrestore(&qhp->lock, flag);
  1002. *bad_wr = wr;
  1003. return -ENOMEM;
  1004. }
  1005. /*
  1006. * Fastpath for NVMe-oF target WRITE + SEND_WITH_INV wr chain which is
  1007. * the response for small NVMEe-oF READ requests. If the chain is
  1008. * exactly a WRITE->SEND_WITH_INV and the sgl depths and lengths
  1009. * meet the requirements of the fw_ri_write_cmpl_wr work request,
  1010. * then build and post the write_cmpl WR. If any of the tests
  1011. * below are not true, then we continue on with the tradtional WRITE
  1012. * and SEND WRs.
  1013. */
  1014. if (qhp->rhp->rdev.lldi.write_cmpl_support &&
  1015. CHELSIO_CHIP_VERSION(qhp->rhp->rdev.lldi.adapter_type) >=
  1016. CHELSIO_T5 &&
  1017. wr && wr->next && !wr->next->next &&
  1018. wr->opcode == IB_WR_RDMA_WRITE &&
  1019. wr->sg_list[0].length && wr->num_sge <= T4_WRITE_CMPL_MAX_SGL &&
  1020. wr->next->opcode == IB_WR_SEND_WITH_INV &&
  1021. wr->next->sg_list[0].length == T4_WRITE_CMPL_MAX_CQE &&
  1022. wr->next->num_sge == 1 && num_wrs >= 2) {
  1023. post_write_cmpl(qhp, wr);
  1024. spin_unlock_irqrestore(&qhp->lock, flag);
  1025. return 0;
  1026. }
  1027. while (wr) {
  1028. if (num_wrs == 0) {
  1029. err = -ENOMEM;
  1030. *bad_wr = wr;
  1031. break;
  1032. }
  1033. wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
  1034. qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
  1035. fw_flags = 0;
  1036. if (wr->send_flags & IB_SEND_SOLICITED)
  1037. fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
  1038. if (wr->send_flags & IB_SEND_SIGNALED || qhp->sq_sig_all)
  1039. fw_flags |= FW_RI_COMPLETION_FLAG;
  1040. swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
  1041. switch (wr->opcode) {
  1042. case IB_WR_SEND_WITH_INV:
  1043. case IB_WR_SEND:
  1044. if (wr->send_flags & IB_SEND_FENCE)
  1045. fw_flags |= FW_RI_READ_FENCE_FLAG;
  1046. fw_opcode = FW_RI_SEND_WR;
  1047. if (wr->opcode == IB_WR_SEND)
  1048. swsqe->opcode = FW_RI_SEND;
  1049. else
  1050. swsqe->opcode = FW_RI_SEND_WITH_INV;
  1051. err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
  1052. break;
  1053. case IB_WR_RDMA_WRITE_WITH_IMM:
  1054. if (unlikely(!rhp->rdev.lldi.write_w_imm_support)) {
  1055. err = -EINVAL;
  1056. break;
  1057. }
  1058. fw_flags |= FW_RI_RDMA_WRITE_WITH_IMMEDIATE;
  1059. /*FALLTHROUGH*/
  1060. case IB_WR_RDMA_WRITE:
  1061. fw_opcode = FW_RI_RDMA_WRITE_WR;
  1062. swsqe->opcode = FW_RI_RDMA_WRITE;
  1063. err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
  1064. break;
  1065. case IB_WR_RDMA_READ:
  1066. case IB_WR_RDMA_READ_WITH_INV:
  1067. fw_opcode = FW_RI_RDMA_READ_WR;
  1068. swsqe->opcode = FW_RI_READ_REQ;
  1069. if (wr->opcode == IB_WR_RDMA_READ_WITH_INV) {
  1070. c4iw_invalidate_mr(rhp, wr->sg_list[0].lkey);
  1071. fw_flags = FW_RI_RDMA_READ_INVALIDATE;
  1072. } else {
  1073. fw_flags = 0;
  1074. }
  1075. err = build_rdma_read(wqe, wr, &len16);
  1076. if (err)
  1077. break;
  1078. swsqe->read_len = wr->sg_list[0].length;
  1079. if (!qhp->wq.sq.oldest_read)
  1080. qhp->wq.sq.oldest_read = swsqe;
  1081. break;
  1082. case IB_WR_REG_MR: {
  1083. struct c4iw_mr *mhp = to_c4iw_mr(reg_wr(wr)->mr);
  1084. swsqe->opcode = FW_RI_FAST_REGISTER;
  1085. if (rhp->rdev.lldi.fr_nsmr_tpte_wr_support &&
  1086. !mhp->attr.state && mhp->mpl_len <= 2) {
  1087. fw_opcode = FW_RI_FR_NSMR_TPTE_WR;
  1088. build_tpte_memreg(&wqe->fr_tpte, reg_wr(wr),
  1089. mhp, &len16);
  1090. } else {
  1091. fw_opcode = FW_RI_FR_NSMR_WR;
  1092. err = build_memreg(&qhp->wq.sq, wqe, reg_wr(wr),
  1093. mhp, &len16,
  1094. rhp->rdev.lldi.ulptx_memwrite_dsgl);
  1095. if (err)
  1096. break;
  1097. }
  1098. mhp->attr.state = 1;
  1099. break;
  1100. }
  1101. case IB_WR_LOCAL_INV:
  1102. if (wr->send_flags & IB_SEND_FENCE)
  1103. fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
  1104. fw_opcode = FW_RI_INV_LSTAG_WR;
  1105. swsqe->opcode = FW_RI_LOCAL_INV;
  1106. err = build_inv_stag(wqe, wr, &len16);
  1107. c4iw_invalidate_mr(rhp, wr->ex.invalidate_rkey);
  1108. break;
  1109. default:
  1110. pr_warn("%s post of type=%d TBD!\n", __func__,
  1111. wr->opcode);
  1112. err = -EINVAL;
  1113. }
  1114. if (err) {
  1115. *bad_wr = wr;
  1116. break;
  1117. }
  1118. swsqe->idx = qhp->wq.sq.pidx;
  1119. swsqe->complete = 0;
  1120. swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
  1121. qhp->sq_sig_all;
  1122. swsqe->flushed = 0;
  1123. swsqe->wr_id = wr->wr_id;
  1124. if (c4iw_wr_log) {
  1125. swsqe->sge_ts = cxgb4_read_sge_timestamp(
  1126. rhp->rdev.lldi.ports[0]);
  1127. swsqe->host_time = ktime_get();
  1128. }
  1129. init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
  1130. pr_debug("cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
  1131. (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
  1132. swsqe->opcode, swsqe->read_len);
  1133. wr = wr->next;
  1134. num_wrs--;
  1135. t4_sq_produce(&qhp->wq, len16);
  1136. idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
  1137. }
  1138. if (!rhp->rdev.status_page->db_off) {
  1139. t4_ring_sq_db(&qhp->wq, idx, wqe);
  1140. spin_unlock_irqrestore(&qhp->lock, flag);
  1141. } else {
  1142. spin_unlock_irqrestore(&qhp->lock, flag);
  1143. ring_kernel_sq_db(qhp, idx);
  1144. }
  1145. return err;
  1146. }
  1147. int c4iw_post_receive(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  1148. const struct ib_recv_wr **bad_wr)
  1149. {
  1150. int err = 0;
  1151. struct c4iw_qp *qhp;
  1152. union t4_recv_wr *wqe = NULL;
  1153. u32 num_wrs;
  1154. u8 len16 = 0;
  1155. unsigned long flag;
  1156. u16 idx = 0;
  1157. qhp = to_c4iw_qp(ibqp);
  1158. spin_lock_irqsave(&qhp->lock, flag);
  1159. /*
  1160. * If the qp has been flushed, then just insert a special
  1161. * drain cqe.
  1162. */
  1163. if (qhp->wq.flushed) {
  1164. spin_unlock_irqrestore(&qhp->lock, flag);
  1165. complete_rq_drain_wrs(qhp, wr);
  1166. return err;
  1167. }
  1168. num_wrs = t4_rq_avail(&qhp->wq);
  1169. if (num_wrs == 0) {
  1170. spin_unlock_irqrestore(&qhp->lock, flag);
  1171. *bad_wr = wr;
  1172. return -ENOMEM;
  1173. }
  1174. while (wr) {
  1175. if (wr->num_sge > T4_MAX_RECV_SGE) {
  1176. err = -EINVAL;
  1177. *bad_wr = wr;
  1178. break;
  1179. }
  1180. wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
  1181. qhp->wq.rq.wq_pidx *
  1182. T4_EQ_ENTRY_SIZE);
  1183. if (num_wrs)
  1184. err = build_rdma_recv(qhp, wqe, wr, &len16);
  1185. else
  1186. err = -ENOMEM;
  1187. if (err) {
  1188. *bad_wr = wr;
  1189. break;
  1190. }
  1191. qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
  1192. if (c4iw_wr_log) {
  1193. qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].sge_ts =
  1194. cxgb4_read_sge_timestamp(
  1195. qhp->rhp->rdev.lldi.ports[0]);
  1196. qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].host_time =
  1197. ktime_get();
  1198. }
  1199. wqe->recv.opcode = FW_RI_RECV_WR;
  1200. wqe->recv.r1 = 0;
  1201. wqe->recv.wrid = qhp->wq.rq.pidx;
  1202. wqe->recv.r2[0] = 0;
  1203. wqe->recv.r2[1] = 0;
  1204. wqe->recv.r2[2] = 0;
  1205. wqe->recv.len16 = len16;
  1206. pr_debug("cookie 0x%llx pidx %u\n",
  1207. (unsigned long long)wr->wr_id, qhp->wq.rq.pidx);
  1208. t4_rq_produce(&qhp->wq, len16);
  1209. idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
  1210. wr = wr->next;
  1211. num_wrs--;
  1212. }
  1213. if (!qhp->rhp->rdev.status_page->db_off) {
  1214. t4_ring_rq_db(&qhp->wq, idx, wqe);
  1215. spin_unlock_irqrestore(&qhp->lock, flag);
  1216. } else {
  1217. spin_unlock_irqrestore(&qhp->lock, flag);
  1218. ring_kernel_rq_db(qhp, idx);
  1219. }
  1220. return err;
  1221. }
  1222. static void defer_srq_wr(struct t4_srq *srq, union t4_recv_wr *wqe,
  1223. u64 wr_id, u8 len16)
  1224. {
  1225. struct t4_srq_pending_wr *pwr = &srq->pending_wrs[srq->pending_pidx];
  1226. pr_debug("%s cidx %u pidx %u wq_pidx %u in_use %u ooo_count %u wr_id 0x%llx pending_cidx %u pending_pidx %u pending_in_use %u\n",
  1227. __func__, srq->cidx, srq->pidx, srq->wq_pidx,
  1228. srq->in_use, srq->ooo_count,
  1229. (unsigned long long)wr_id, srq->pending_cidx,
  1230. srq->pending_pidx, srq->pending_in_use);
  1231. pwr->wr_id = wr_id;
  1232. pwr->len16 = len16;
  1233. memcpy(&pwr->wqe, wqe, len16 * 16);
  1234. t4_srq_produce_pending_wr(srq);
  1235. }
  1236. int c4iw_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
  1237. const struct ib_recv_wr **bad_wr)
  1238. {
  1239. union t4_recv_wr *wqe, lwqe;
  1240. struct c4iw_srq *srq;
  1241. unsigned long flag;
  1242. u8 len16 = 0;
  1243. u16 idx = 0;
  1244. int err = 0;
  1245. u32 num_wrs;
  1246. srq = to_c4iw_srq(ibsrq);
  1247. spin_lock_irqsave(&srq->lock, flag);
  1248. num_wrs = t4_srq_avail(&srq->wq);
  1249. if (num_wrs == 0) {
  1250. spin_unlock_irqrestore(&srq->lock, flag);
  1251. return -ENOMEM;
  1252. }
  1253. while (wr) {
  1254. if (wr->num_sge > T4_MAX_RECV_SGE) {
  1255. err = -EINVAL;
  1256. *bad_wr = wr;
  1257. break;
  1258. }
  1259. wqe = &lwqe;
  1260. if (num_wrs)
  1261. err = build_srq_recv(wqe, wr, &len16);
  1262. else
  1263. err = -ENOMEM;
  1264. if (err) {
  1265. *bad_wr = wr;
  1266. break;
  1267. }
  1268. wqe->recv.opcode = FW_RI_RECV_WR;
  1269. wqe->recv.r1 = 0;
  1270. wqe->recv.wrid = srq->wq.pidx;
  1271. wqe->recv.r2[0] = 0;
  1272. wqe->recv.r2[1] = 0;
  1273. wqe->recv.r2[2] = 0;
  1274. wqe->recv.len16 = len16;
  1275. if (srq->wq.ooo_count ||
  1276. srq->wq.pending_in_use ||
  1277. srq->wq.sw_rq[srq->wq.pidx].valid) {
  1278. defer_srq_wr(&srq->wq, wqe, wr->wr_id, len16);
  1279. } else {
  1280. srq->wq.sw_rq[srq->wq.pidx].wr_id = wr->wr_id;
  1281. srq->wq.sw_rq[srq->wq.pidx].valid = 1;
  1282. c4iw_copy_wr_to_srq(&srq->wq, wqe, len16);
  1283. pr_debug("%s cidx %u pidx %u wq_pidx %u in_use %u wr_id 0x%llx\n",
  1284. __func__, srq->wq.cidx,
  1285. srq->wq.pidx, srq->wq.wq_pidx,
  1286. srq->wq.in_use,
  1287. (unsigned long long)wr->wr_id);
  1288. t4_srq_produce(&srq->wq, len16);
  1289. idx += DIV_ROUND_UP(len16 * 16, T4_EQ_ENTRY_SIZE);
  1290. }
  1291. wr = wr->next;
  1292. num_wrs--;
  1293. }
  1294. if (idx)
  1295. t4_ring_srq_db(&srq->wq, idx, len16, wqe);
  1296. spin_unlock_irqrestore(&srq->lock, flag);
  1297. return err;
  1298. }
  1299. static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
  1300. u8 *ecode)
  1301. {
  1302. int status;
  1303. int tagged;
  1304. int opcode;
  1305. int rqtype;
  1306. int send_inv;
  1307. if (!err_cqe) {
  1308. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  1309. *ecode = 0;
  1310. return;
  1311. }
  1312. status = CQE_STATUS(err_cqe);
  1313. opcode = CQE_OPCODE(err_cqe);
  1314. rqtype = RQ_TYPE(err_cqe);
  1315. send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
  1316. (opcode == FW_RI_SEND_WITH_SE_INV);
  1317. tagged = (opcode == FW_RI_RDMA_WRITE) ||
  1318. (rqtype && (opcode == FW_RI_READ_RESP));
  1319. switch (status) {
  1320. case T4_ERR_STAG:
  1321. if (send_inv) {
  1322. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  1323. *ecode = RDMAP_CANT_INV_STAG;
  1324. } else {
  1325. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1326. *ecode = RDMAP_INV_STAG;
  1327. }
  1328. break;
  1329. case T4_ERR_PDID:
  1330. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1331. if ((opcode == FW_RI_SEND_WITH_INV) ||
  1332. (opcode == FW_RI_SEND_WITH_SE_INV))
  1333. *ecode = RDMAP_CANT_INV_STAG;
  1334. else
  1335. *ecode = RDMAP_STAG_NOT_ASSOC;
  1336. break;
  1337. case T4_ERR_QPID:
  1338. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1339. *ecode = RDMAP_STAG_NOT_ASSOC;
  1340. break;
  1341. case T4_ERR_ACCESS:
  1342. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1343. *ecode = RDMAP_ACC_VIOL;
  1344. break;
  1345. case T4_ERR_WRAP:
  1346. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1347. *ecode = RDMAP_TO_WRAP;
  1348. break;
  1349. case T4_ERR_BOUND:
  1350. if (tagged) {
  1351. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  1352. *ecode = DDPT_BASE_BOUNDS;
  1353. } else {
  1354. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  1355. *ecode = RDMAP_BASE_BOUNDS;
  1356. }
  1357. break;
  1358. case T4_ERR_INVALIDATE_SHARED_MR:
  1359. case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  1360. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  1361. *ecode = RDMAP_CANT_INV_STAG;
  1362. break;
  1363. case T4_ERR_ECC:
  1364. case T4_ERR_ECC_PSTAG:
  1365. case T4_ERR_INTERNAL_ERR:
  1366. *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
  1367. *ecode = 0;
  1368. break;
  1369. case T4_ERR_OUT_OF_RQE:
  1370. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1371. *ecode = DDPU_INV_MSN_NOBUF;
  1372. break;
  1373. case T4_ERR_PBL_ADDR_BOUND:
  1374. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  1375. *ecode = DDPT_BASE_BOUNDS;
  1376. break;
  1377. case T4_ERR_CRC:
  1378. *layer_type = LAYER_MPA|DDP_LLP;
  1379. *ecode = MPA_CRC_ERR;
  1380. break;
  1381. case T4_ERR_MARKER:
  1382. *layer_type = LAYER_MPA|DDP_LLP;
  1383. *ecode = MPA_MARKER_ERR;
  1384. break;
  1385. case T4_ERR_PDU_LEN_ERR:
  1386. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1387. *ecode = DDPU_MSG_TOOBIG;
  1388. break;
  1389. case T4_ERR_DDP_VERSION:
  1390. if (tagged) {
  1391. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  1392. *ecode = DDPT_INV_VERS;
  1393. } else {
  1394. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1395. *ecode = DDPU_INV_VERS;
  1396. }
  1397. break;
  1398. case T4_ERR_RDMA_VERSION:
  1399. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  1400. *ecode = RDMAP_INV_VERS;
  1401. break;
  1402. case T4_ERR_OPCODE:
  1403. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  1404. *ecode = RDMAP_INV_OPCODE;
  1405. break;
  1406. case T4_ERR_DDP_QUEUE_NUM:
  1407. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1408. *ecode = DDPU_INV_QN;
  1409. break;
  1410. case T4_ERR_MSN:
  1411. case T4_ERR_MSN_GAP:
  1412. case T4_ERR_MSN_RANGE:
  1413. case T4_ERR_IRD_OVERFLOW:
  1414. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1415. *ecode = DDPU_INV_MSN_RANGE;
  1416. break;
  1417. case T4_ERR_TBIT:
  1418. *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
  1419. *ecode = 0;
  1420. break;
  1421. case T4_ERR_MO:
  1422. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  1423. *ecode = DDPU_INV_MO;
  1424. break;
  1425. default:
  1426. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  1427. *ecode = 0;
  1428. break;
  1429. }
  1430. }
  1431. static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
  1432. gfp_t gfp)
  1433. {
  1434. struct fw_ri_wr *wqe;
  1435. struct sk_buff *skb;
  1436. struct terminate_message *term;
  1437. pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid,
  1438. qhp->ep->hwtid);
  1439. skb = skb_dequeue(&qhp->ep->com.ep_skb_list);
  1440. if (WARN_ON(!skb))
  1441. return;
  1442. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  1443. wqe = __skb_put_zero(skb, sizeof(*wqe));
  1444. wqe->op_compl = cpu_to_be32(FW_WR_OP_V(FW_RI_INIT_WR));
  1445. wqe->flowid_len16 = cpu_to_be32(
  1446. FW_WR_FLOWID_V(qhp->ep->hwtid) |
  1447. FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
  1448. wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
  1449. wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
  1450. term = (struct terminate_message *)wqe->u.terminate.termmsg;
  1451. if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
  1452. term->layer_etype = qhp->attr.layer_etype;
  1453. term->ecode = qhp->attr.ecode;
  1454. } else
  1455. build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
  1456. c4iw_ofld_send(&qhp->rhp->rdev, skb);
  1457. }
  1458. /*
  1459. * Assumes qhp lock is held.
  1460. */
  1461. static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
  1462. struct c4iw_cq *schp)
  1463. {
  1464. int count;
  1465. int rq_flushed = 0, sq_flushed;
  1466. unsigned long flag;
  1467. pr_debug("qhp %p rchp %p schp %p\n", qhp, rchp, schp);
  1468. /* locking hierarchy: cqs lock first, then qp lock. */
  1469. spin_lock_irqsave(&rchp->lock, flag);
  1470. if (schp != rchp)
  1471. spin_lock(&schp->lock);
  1472. spin_lock(&qhp->lock);
  1473. if (qhp->wq.flushed) {
  1474. spin_unlock(&qhp->lock);
  1475. if (schp != rchp)
  1476. spin_unlock(&schp->lock);
  1477. spin_unlock_irqrestore(&rchp->lock, flag);
  1478. return;
  1479. }
  1480. qhp->wq.flushed = 1;
  1481. t4_set_wq_in_error(&qhp->wq, 0);
  1482. c4iw_flush_hw_cq(rchp, qhp);
  1483. if (!qhp->srq) {
  1484. c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
  1485. rq_flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
  1486. }
  1487. if (schp != rchp)
  1488. c4iw_flush_hw_cq(schp, qhp);
  1489. sq_flushed = c4iw_flush_sq(qhp);
  1490. spin_unlock(&qhp->lock);
  1491. if (schp != rchp)
  1492. spin_unlock(&schp->lock);
  1493. spin_unlock_irqrestore(&rchp->lock, flag);
  1494. if (schp == rchp) {
  1495. if ((rq_flushed || sq_flushed) &&
  1496. t4_clear_cq_armed(&rchp->cq)) {
  1497. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  1498. (*rchp->ibcq.comp_handler)(&rchp->ibcq,
  1499. rchp->ibcq.cq_context);
  1500. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  1501. }
  1502. } else {
  1503. if (rq_flushed && t4_clear_cq_armed(&rchp->cq)) {
  1504. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  1505. (*rchp->ibcq.comp_handler)(&rchp->ibcq,
  1506. rchp->ibcq.cq_context);
  1507. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  1508. }
  1509. if (sq_flushed && t4_clear_cq_armed(&schp->cq)) {
  1510. spin_lock_irqsave(&schp->comp_handler_lock, flag);
  1511. (*schp->ibcq.comp_handler)(&schp->ibcq,
  1512. schp->ibcq.cq_context);
  1513. spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
  1514. }
  1515. }
  1516. }
  1517. static void flush_qp(struct c4iw_qp *qhp)
  1518. {
  1519. struct c4iw_cq *rchp, *schp;
  1520. unsigned long flag;
  1521. rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
  1522. schp = to_c4iw_cq(qhp->ibqp.send_cq);
  1523. if (qhp->ibqp.uobject) {
  1524. /* for user qps, qhp->wq.flushed is protected by qhp->mutex */
  1525. if (qhp->wq.flushed)
  1526. return;
  1527. qhp->wq.flushed = 1;
  1528. t4_set_wq_in_error(&qhp->wq, 0);
  1529. t4_set_cq_in_error(&rchp->cq);
  1530. spin_lock_irqsave(&rchp->comp_handler_lock, flag);
  1531. (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
  1532. spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
  1533. if (schp != rchp) {
  1534. t4_set_cq_in_error(&schp->cq);
  1535. spin_lock_irqsave(&schp->comp_handler_lock, flag);
  1536. (*schp->ibcq.comp_handler)(&schp->ibcq,
  1537. schp->ibcq.cq_context);
  1538. spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
  1539. }
  1540. return;
  1541. }
  1542. __flush_qp(qhp, rchp, schp);
  1543. }
  1544. static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
  1545. struct c4iw_ep *ep)
  1546. {
  1547. struct fw_ri_wr *wqe;
  1548. int ret;
  1549. struct sk_buff *skb;
  1550. pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid, ep->hwtid);
  1551. skb = skb_dequeue(&ep->com.ep_skb_list);
  1552. if (WARN_ON(!skb))
  1553. return -ENOMEM;
  1554. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  1555. wqe = __skb_put_zero(skb, sizeof(*wqe));
  1556. wqe->op_compl = cpu_to_be32(
  1557. FW_WR_OP_V(FW_RI_INIT_WR) |
  1558. FW_WR_COMPL_F);
  1559. wqe->flowid_len16 = cpu_to_be32(
  1560. FW_WR_FLOWID_V(ep->hwtid) |
  1561. FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
  1562. wqe->cookie = (uintptr_t)ep->com.wr_waitp;
  1563. wqe->u.fini.type = FW_RI_TYPE_FINI;
  1564. ret = c4iw_ref_send_wait(&rhp->rdev, skb, ep->com.wr_waitp,
  1565. qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
  1566. pr_debug("ret %d\n", ret);
  1567. return ret;
  1568. }
  1569. static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
  1570. {
  1571. pr_debug("p2p_type = %d\n", p2p_type);
  1572. memset(&init->u, 0, sizeof init->u);
  1573. switch (p2p_type) {
  1574. case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
  1575. init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
  1576. init->u.write.stag_sink = cpu_to_be32(1);
  1577. init->u.write.to_sink = cpu_to_be64(1);
  1578. init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
  1579. init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
  1580. sizeof(struct fw_ri_immd),
  1581. 16);
  1582. break;
  1583. case FW_RI_INIT_P2PTYPE_READ_REQ:
  1584. init->u.write.opcode = FW_RI_RDMA_READ_WR;
  1585. init->u.read.stag_src = cpu_to_be32(1);
  1586. init->u.read.to_src_lo = cpu_to_be32(1);
  1587. init->u.read.stag_sink = cpu_to_be32(1);
  1588. init->u.read.to_sink_lo = cpu_to_be32(1);
  1589. init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
  1590. break;
  1591. }
  1592. }
  1593. static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
  1594. {
  1595. struct fw_ri_wr *wqe;
  1596. int ret;
  1597. struct sk_buff *skb;
  1598. pr_debug("qhp %p qid 0x%x tid %u ird %u ord %u\n", qhp,
  1599. qhp->wq.sq.qid, qhp->ep->hwtid, qhp->ep->ird, qhp->ep->ord);
  1600. skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
  1601. if (!skb) {
  1602. ret = -ENOMEM;
  1603. goto out;
  1604. }
  1605. ret = alloc_ird(rhp, qhp->attr.max_ird);
  1606. if (ret) {
  1607. qhp->attr.max_ird = 0;
  1608. kfree_skb(skb);
  1609. goto out;
  1610. }
  1611. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  1612. wqe = __skb_put_zero(skb, sizeof(*wqe));
  1613. wqe->op_compl = cpu_to_be32(
  1614. FW_WR_OP_V(FW_RI_INIT_WR) |
  1615. FW_WR_COMPL_F);
  1616. wqe->flowid_len16 = cpu_to_be32(
  1617. FW_WR_FLOWID_V(qhp->ep->hwtid) |
  1618. FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
  1619. wqe->cookie = (uintptr_t)qhp->ep->com.wr_waitp;
  1620. wqe->u.init.type = FW_RI_TYPE_INIT;
  1621. wqe->u.init.mpareqbit_p2ptype =
  1622. FW_RI_WR_MPAREQBIT_V(qhp->attr.mpa_attr.initiator) |
  1623. FW_RI_WR_P2PTYPE_V(qhp->attr.mpa_attr.p2p_type);
  1624. wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
  1625. if (qhp->attr.mpa_attr.recv_marker_enabled)
  1626. wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
  1627. if (qhp->attr.mpa_attr.xmit_marker_enabled)
  1628. wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
  1629. if (qhp->attr.mpa_attr.crc_enabled)
  1630. wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
  1631. wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
  1632. FW_RI_QP_RDMA_WRITE_ENABLE |
  1633. FW_RI_QP_BIND_ENABLE;
  1634. if (!qhp->ibqp.uobject)
  1635. wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
  1636. FW_RI_QP_STAG0_ENABLE;
  1637. wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
  1638. wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
  1639. wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
  1640. wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
  1641. if (qhp->srq) {
  1642. wqe->u.init.rq_eqid = cpu_to_be32(FW_RI_INIT_RQEQID_SRQ |
  1643. qhp->srq->idx);
  1644. } else {
  1645. wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
  1646. wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
  1647. wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
  1648. rhp->rdev.lldi.vr->rq.start);
  1649. }
  1650. wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
  1651. wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
  1652. wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
  1653. wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
  1654. wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
  1655. wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
  1656. if (qhp->attr.mpa_attr.initiator)
  1657. build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
  1658. ret = c4iw_ref_send_wait(&rhp->rdev, skb, qhp->ep->com.wr_waitp,
  1659. qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
  1660. if (!ret)
  1661. goto out;
  1662. free_ird(rhp, qhp->attr.max_ird);
  1663. out:
  1664. pr_debug("ret %d\n", ret);
  1665. return ret;
  1666. }
  1667. int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
  1668. enum c4iw_qp_attr_mask mask,
  1669. struct c4iw_qp_attributes *attrs,
  1670. int internal)
  1671. {
  1672. int ret = 0;
  1673. struct c4iw_qp_attributes newattr = qhp->attr;
  1674. int disconnect = 0;
  1675. int terminate = 0;
  1676. int abort = 0;
  1677. int free = 0;
  1678. struct c4iw_ep *ep = NULL;
  1679. pr_debug("qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n",
  1680. qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
  1681. (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
  1682. mutex_lock(&qhp->mutex);
  1683. /* Process attr changes if in IDLE */
  1684. if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
  1685. if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
  1686. ret = -EIO;
  1687. goto out;
  1688. }
  1689. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
  1690. newattr.enable_rdma_read = attrs->enable_rdma_read;
  1691. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
  1692. newattr.enable_rdma_write = attrs->enable_rdma_write;
  1693. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
  1694. newattr.enable_bind = attrs->enable_bind;
  1695. if (mask & C4IW_QP_ATTR_MAX_ORD) {
  1696. if (attrs->max_ord > c4iw_max_read_depth) {
  1697. ret = -EINVAL;
  1698. goto out;
  1699. }
  1700. newattr.max_ord = attrs->max_ord;
  1701. }
  1702. if (mask & C4IW_QP_ATTR_MAX_IRD) {
  1703. if (attrs->max_ird > cur_max_read_depth(rhp)) {
  1704. ret = -EINVAL;
  1705. goto out;
  1706. }
  1707. newattr.max_ird = attrs->max_ird;
  1708. }
  1709. qhp->attr = newattr;
  1710. }
  1711. if (mask & C4IW_QP_ATTR_SQ_DB) {
  1712. ret = ring_kernel_sq_db(qhp, attrs->sq_db_inc);
  1713. goto out;
  1714. }
  1715. if (mask & C4IW_QP_ATTR_RQ_DB) {
  1716. ret = ring_kernel_rq_db(qhp, attrs->rq_db_inc);
  1717. goto out;
  1718. }
  1719. if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
  1720. goto out;
  1721. if (qhp->attr.state == attrs->next_state)
  1722. goto out;
  1723. switch (qhp->attr.state) {
  1724. case C4IW_QP_STATE_IDLE:
  1725. switch (attrs->next_state) {
  1726. case C4IW_QP_STATE_RTS:
  1727. if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
  1728. ret = -EINVAL;
  1729. goto out;
  1730. }
  1731. if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
  1732. ret = -EINVAL;
  1733. goto out;
  1734. }
  1735. qhp->attr.mpa_attr = attrs->mpa_attr;
  1736. qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
  1737. qhp->ep = qhp->attr.llp_stream_handle;
  1738. set_state(qhp, C4IW_QP_STATE_RTS);
  1739. /*
  1740. * Ref the endpoint here and deref when we
  1741. * disassociate the endpoint from the QP. This
  1742. * happens in CLOSING->IDLE transition or *->ERROR
  1743. * transition.
  1744. */
  1745. c4iw_get_ep(&qhp->ep->com);
  1746. ret = rdma_init(rhp, qhp);
  1747. if (ret)
  1748. goto err;
  1749. break;
  1750. case C4IW_QP_STATE_ERROR:
  1751. set_state(qhp, C4IW_QP_STATE_ERROR);
  1752. flush_qp(qhp);
  1753. break;
  1754. default:
  1755. ret = -EINVAL;
  1756. goto out;
  1757. }
  1758. break;
  1759. case C4IW_QP_STATE_RTS:
  1760. switch (attrs->next_state) {
  1761. case C4IW_QP_STATE_CLOSING:
  1762. t4_set_wq_in_error(&qhp->wq, 0);
  1763. set_state(qhp, C4IW_QP_STATE_CLOSING);
  1764. ep = qhp->ep;
  1765. if (!internal) {
  1766. abort = 0;
  1767. disconnect = 1;
  1768. c4iw_get_ep(&qhp->ep->com);
  1769. }
  1770. ret = rdma_fini(rhp, qhp, ep);
  1771. if (ret)
  1772. goto err;
  1773. break;
  1774. case C4IW_QP_STATE_TERMINATE:
  1775. t4_set_wq_in_error(&qhp->wq, 0);
  1776. set_state(qhp, C4IW_QP_STATE_TERMINATE);
  1777. qhp->attr.layer_etype = attrs->layer_etype;
  1778. qhp->attr.ecode = attrs->ecode;
  1779. ep = qhp->ep;
  1780. if (!internal) {
  1781. c4iw_get_ep(&qhp->ep->com);
  1782. terminate = 1;
  1783. disconnect = 1;
  1784. } else {
  1785. terminate = qhp->attr.send_term;
  1786. ret = rdma_fini(rhp, qhp, ep);
  1787. if (ret)
  1788. goto err;
  1789. }
  1790. break;
  1791. case C4IW_QP_STATE_ERROR:
  1792. t4_set_wq_in_error(&qhp->wq, 0);
  1793. set_state(qhp, C4IW_QP_STATE_ERROR);
  1794. if (!internal) {
  1795. abort = 1;
  1796. disconnect = 1;
  1797. ep = qhp->ep;
  1798. c4iw_get_ep(&qhp->ep->com);
  1799. }
  1800. goto err;
  1801. break;
  1802. default:
  1803. ret = -EINVAL;
  1804. goto out;
  1805. }
  1806. break;
  1807. case C4IW_QP_STATE_CLOSING:
  1808. /*
  1809. * Allow kernel users to move to ERROR for qp draining.
  1810. */
  1811. if (!internal && (qhp->ibqp.uobject || attrs->next_state !=
  1812. C4IW_QP_STATE_ERROR)) {
  1813. ret = -EINVAL;
  1814. goto out;
  1815. }
  1816. switch (attrs->next_state) {
  1817. case C4IW_QP_STATE_IDLE:
  1818. flush_qp(qhp);
  1819. set_state(qhp, C4IW_QP_STATE_IDLE);
  1820. qhp->attr.llp_stream_handle = NULL;
  1821. c4iw_put_ep(&qhp->ep->com);
  1822. qhp->ep = NULL;
  1823. wake_up(&qhp->wait);
  1824. break;
  1825. case C4IW_QP_STATE_ERROR:
  1826. goto err;
  1827. default:
  1828. ret = -EINVAL;
  1829. goto err;
  1830. }
  1831. break;
  1832. case C4IW_QP_STATE_ERROR:
  1833. if (attrs->next_state != C4IW_QP_STATE_IDLE) {
  1834. ret = -EINVAL;
  1835. goto out;
  1836. }
  1837. if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
  1838. ret = -EINVAL;
  1839. goto out;
  1840. }
  1841. set_state(qhp, C4IW_QP_STATE_IDLE);
  1842. break;
  1843. case C4IW_QP_STATE_TERMINATE:
  1844. if (!internal) {
  1845. ret = -EINVAL;
  1846. goto out;
  1847. }
  1848. goto err;
  1849. break;
  1850. default:
  1851. pr_err("%s in a bad state %d\n", __func__, qhp->attr.state);
  1852. ret = -EINVAL;
  1853. goto err;
  1854. break;
  1855. }
  1856. goto out;
  1857. err:
  1858. pr_debug("disassociating ep %p qpid 0x%x\n", qhp->ep,
  1859. qhp->wq.sq.qid);
  1860. /* disassociate the LLP connection */
  1861. qhp->attr.llp_stream_handle = NULL;
  1862. if (!ep)
  1863. ep = qhp->ep;
  1864. qhp->ep = NULL;
  1865. set_state(qhp, C4IW_QP_STATE_ERROR);
  1866. free = 1;
  1867. abort = 1;
  1868. flush_qp(qhp);
  1869. wake_up(&qhp->wait);
  1870. out:
  1871. mutex_unlock(&qhp->mutex);
  1872. if (terminate)
  1873. post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
  1874. /*
  1875. * If disconnect is 1, then we need to initiate a disconnect
  1876. * on the EP. This can be a normal close (RTS->CLOSING) or
  1877. * an abnormal close (RTS/CLOSING->ERROR).
  1878. */
  1879. if (disconnect) {
  1880. c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
  1881. GFP_KERNEL);
  1882. c4iw_put_ep(&ep->com);
  1883. }
  1884. /*
  1885. * If free is 1, then we've disassociated the EP from the QP
  1886. * and we need to dereference the EP.
  1887. */
  1888. if (free)
  1889. c4iw_put_ep(&ep->com);
  1890. pr_debug("exit state %d\n", qhp->attr.state);
  1891. return ret;
  1892. }
  1893. int c4iw_destroy_qp(struct ib_qp *ib_qp)
  1894. {
  1895. struct c4iw_dev *rhp;
  1896. struct c4iw_qp *qhp;
  1897. struct c4iw_qp_attributes attrs;
  1898. qhp = to_c4iw_qp(ib_qp);
  1899. rhp = qhp->rhp;
  1900. attrs.next_state = C4IW_QP_STATE_ERROR;
  1901. if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
  1902. c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  1903. else
  1904. c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
  1905. wait_event(qhp->wait, !qhp->ep);
  1906. remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
  1907. spin_lock_irq(&rhp->lock);
  1908. if (!list_empty(&qhp->db_fc_entry))
  1909. list_del_init(&qhp->db_fc_entry);
  1910. spin_unlock_irq(&rhp->lock);
  1911. free_ird(rhp, qhp->attr.max_ird);
  1912. c4iw_qp_rem_ref(ib_qp);
  1913. pr_debug("ib_qp %p qpid 0x%0x\n", ib_qp, qhp->wq.sq.qid);
  1914. return 0;
  1915. }
  1916. struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
  1917. struct ib_udata *udata)
  1918. {
  1919. struct c4iw_dev *rhp;
  1920. struct c4iw_qp *qhp;
  1921. struct c4iw_pd *php;
  1922. struct c4iw_cq *schp;
  1923. struct c4iw_cq *rchp;
  1924. struct c4iw_create_qp_resp uresp;
  1925. unsigned int sqsize, rqsize = 0;
  1926. struct c4iw_ucontext *ucontext;
  1927. int ret;
  1928. struct c4iw_mm_entry *sq_key_mm, *rq_key_mm = NULL, *sq_db_key_mm;
  1929. struct c4iw_mm_entry *rq_db_key_mm = NULL, *ma_sync_key_mm = NULL;
  1930. pr_debug("ib_pd %p\n", pd);
  1931. if (attrs->qp_type != IB_QPT_RC)
  1932. return ERR_PTR(-EINVAL);
  1933. php = to_c4iw_pd(pd);
  1934. rhp = php->rhp;
  1935. schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
  1936. rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
  1937. if (!schp || !rchp)
  1938. return ERR_PTR(-EINVAL);
  1939. if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
  1940. return ERR_PTR(-EINVAL);
  1941. if (!attrs->srq) {
  1942. if (attrs->cap.max_recv_wr > rhp->rdev.hw_queue.t4_max_rq_size)
  1943. return ERR_PTR(-E2BIG);
  1944. rqsize = attrs->cap.max_recv_wr + 1;
  1945. if (rqsize < 8)
  1946. rqsize = 8;
  1947. }
  1948. if (attrs->cap.max_send_wr > rhp->rdev.hw_queue.t4_max_sq_size)
  1949. return ERR_PTR(-E2BIG);
  1950. sqsize = attrs->cap.max_send_wr + 1;
  1951. if (sqsize < 8)
  1952. sqsize = 8;
  1953. ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
  1954. qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
  1955. if (!qhp)
  1956. return ERR_PTR(-ENOMEM);
  1957. qhp->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
  1958. if (!qhp->wr_waitp) {
  1959. ret = -ENOMEM;
  1960. goto err_free_qhp;
  1961. }
  1962. qhp->wq.sq.size = sqsize;
  1963. qhp->wq.sq.memsize =
  1964. (sqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
  1965. sizeof(*qhp->wq.sq.queue) + 16 * sizeof(__be64);
  1966. qhp->wq.sq.flush_cidx = -1;
  1967. if (!attrs->srq) {
  1968. qhp->wq.rq.size = rqsize;
  1969. qhp->wq.rq.memsize =
  1970. (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
  1971. sizeof(*qhp->wq.rq.queue);
  1972. }
  1973. if (ucontext) {
  1974. qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
  1975. if (!attrs->srq)
  1976. qhp->wq.rq.memsize =
  1977. roundup(qhp->wq.rq.memsize, PAGE_SIZE);
  1978. }
  1979. ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
  1980. ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
  1981. qhp->wr_waitp, !attrs->srq);
  1982. if (ret)
  1983. goto err_free_wr_wait;
  1984. attrs->cap.max_recv_wr = rqsize - 1;
  1985. attrs->cap.max_send_wr = sqsize - 1;
  1986. attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
  1987. qhp->rhp = rhp;
  1988. qhp->attr.pd = php->pdid;
  1989. qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
  1990. qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
  1991. qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
  1992. qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
  1993. qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
  1994. if (!attrs->srq) {
  1995. qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
  1996. qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
  1997. }
  1998. qhp->attr.state = C4IW_QP_STATE_IDLE;
  1999. qhp->attr.next_state = C4IW_QP_STATE_IDLE;
  2000. qhp->attr.enable_rdma_read = 1;
  2001. qhp->attr.enable_rdma_write = 1;
  2002. qhp->attr.enable_bind = 1;
  2003. qhp->attr.max_ord = 0;
  2004. qhp->attr.max_ird = 0;
  2005. qhp->sq_sig_all = attrs->sq_sig_type == IB_SIGNAL_ALL_WR;
  2006. spin_lock_init(&qhp->lock);
  2007. mutex_init(&qhp->mutex);
  2008. init_waitqueue_head(&qhp->wait);
  2009. kref_init(&qhp->kref);
  2010. INIT_WORK(&qhp->free_work, free_qp_work);
  2011. ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
  2012. if (ret)
  2013. goto err_destroy_qp;
  2014. if (udata && ucontext) {
  2015. sq_key_mm = kmalloc(sizeof(*sq_key_mm), GFP_KERNEL);
  2016. if (!sq_key_mm) {
  2017. ret = -ENOMEM;
  2018. goto err_remove_handle;
  2019. }
  2020. if (!attrs->srq) {
  2021. rq_key_mm = kmalloc(sizeof(*rq_key_mm), GFP_KERNEL);
  2022. if (!rq_key_mm) {
  2023. ret = -ENOMEM;
  2024. goto err_free_sq_key;
  2025. }
  2026. }
  2027. sq_db_key_mm = kmalloc(sizeof(*sq_db_key_mm), GFP_KERNEL);
  2028. if (!sq_db_key_mm) {
  2029. ret = -ENOMEM;
  2030. goto err_free_rq_key;
  2031. }
  2032. if (!attrs->srq) {
  2033. rq_db_key_mm =
  2034. kmalloc(sizeof(*rq_db_key_mm), GFP_KERNEL);
  2035. if (!rq_db_key_mm) {
  2036. ret = -ENOMEM;
  2037. goto err_free_sq_db_key;
  2038. }
  2039. }
  2040. memset(&uresp, 0, sizeof(uresp));
  2041. if (t4_sq_onchip(&qhp->wq.sq)) {
  2042. ma_sync_key_mm = kmalloc(sizeof(*ma_sync_key_mm),
  2043. GFP_KERNEL);
  2044. if (!ma_sync_key_mm) {
  2045. ret = -ENOMEM;
  2046. goto err_free_rq_db_key;
  2047. }
  2048. uresp.flags = C4IW_QPF_ONCHIP;
  2049. }
  2050. if (rhp->rdev.lldi.write_w_imm_support)
  2051. uresp.flags |= C4IW_QPF_WRITE_W_IMM;
  2052. uresp.qid_mask = rhp->rdev.qpmask;
  2053. uresp.sqid = qhp->wq.sq.qid;
  2054. uresp.sq_size = qhp->wq.sq.size;
  2055. uresp.sq_memsize = qhp->wq.sq.memsize;
  2056. if (!attrs->srq) {
  2057. uresp.rqid = qhp->wq.rq.qid;
  2058. uresp.rq_size = qhp->wq.rq.size;
  2059. uresp.rq_memsize = qhp->wq.rq.memsize;
  2060. }
  2061. spin_lock(&ucontext->mmap_lock);
  2062. if (ma_sync_key_mm) {
  2063. uresp.ma_sync_key = ucontext->key;
  2064. ucontext->key += PAGE_SIZE;
  2065. }
  2066. uresp.sq_key = ucontext->key;
  2067. ucontext->key += PAGE_SIZE;
  2068. if (!attrs->srq) {
  2069. uresp.rq_key = ucontext->key;
  2070. ucontext->key += PAGE_SIZE;
  2071. }
  2072. uresp.sq_db_gts_key = ucontext->key;
  2073. ucontext->key += PAGE_SIZE;
  2074. if (!attrs->srq) {
  2075. uresp.rq_db_gts_key = ucontext->key;
  2076. ucontext->key += PAGE_SIZE;
  2077. }
  2078. spin_unlock(&ucontext->mmap_lock);
  2079. ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
  2080. if (ret)
  2081. goto err_free_ma_sync_key;
  2082. sq_key_mm->key = uresp.sq_key;
  2083. sq_key_mm->addr = qhp->wq.sq.phys_addr;
  2084. sq_key_mm->len = PAGE_ALIGN(qhp->wq.sq.memsize);
  2085. insert_mmap(ucontext, sq_key_mm);
  2086. if (!attrs->srq) {
  2087. rq_key_mm->key = uresp.rq_key;
  2088. rq_key_mm->addr = virt_to_phys(qhp->wq.rq.queue);
  2089. rq_key_mm->len = PAGE_ALIGN(qhp->wq.rq.memsize);
  2090. insert_mmap(ucontext, rq_key_mm);
  2091. }
  2092. sq_db_key_mm->key = uresp.sq_db_gts_key;
  2093. sq_db_key_mm->addr = (u64)(unsigned long)qhp->wq.sq.bar2_pa;
  2094. sq_db_key_mm->len = PAGE_SIZE;
  2095. insert_mmap(ucontext, sq_db_key_mm);
  2096. if (!attrs->srq) {
  2097. rq_db_key_mm->key = uresp.rq_db_gts_key;
  2098. rq_db_key_mm->addr =
  2099. (u64)(unsigned long)qhp->wq.rq.bar2_pa;
  2100. rq_db_key_mm->len = PAGE_SIZE;
  2101. insert_mmap(ucontext, rq_db_key_mm);
  2102. }
  2103. if (ma_sync_key_mm) {
  2104. ma_sync_key_mm->key = uresp.ma_sync_key;
  2105. ma_sync_key_mm->addr =
  2106. (pci_resource_start(rhp->rdev.lldi.pdev, 0) +
  2107. PCIE_MA_SYNC_A) & PAGE_MASK;
  2108. ma_sync_key_mm->len = PAGE_SIZE;
  2109. insert_mmap(ucontext, ma_sync_key_mm);
  2110. }
  2111. c4iw_get_ucontext(ucontext);
  2112. qhp->ucontext = ucontext;
  2113. }
  2114. if (!attrs->srq) {
  2115. qhp->wq.qp_errp =
  2116. &qhp->wq.rq.queue[qhp->wq.rq.size].status.qp_err;
  2117. } else {
  2118. qhp->wq.qp_errp =
  2119. &qhp->wq.sq.queue[qhp->wq.sq.size].status.qp_err;
  2120. qhp->wq.srqidxp =
  2121. &qhp->wq.sq.queue[qhp->wq.sq.size].status.srqidx;
  2122. }
  2123. qhp->ibqp.qp_num = qhp->wq.sq.qid;
  2124. if (attrs->srq)
  2125. qhp->srq = to_c4iw_srq(attrs->srq);
  2126. INIT_LIST_HEAD(&qhp->db_fc_entry);
  2127. pr_debug("sq id %u size %u memsize %zu num_entries %u rq id %u size %u memsize %zu num_entries %u\n",
  2128. qhp->wq.sq.qid, qhp->wq.sq.size, qhp->wq.sq.memsize,
  2129. attrs->cap.max_send_wr, qhp->wq.rq.qid, qhp->wq.rq.size,
  2130. qhp->wq.rq.memsize, attrs->cap.max_recv_wr);
  2131. return &qhp->ibqp;
  2132. err_free_ma_sync_key:
  2133. kfree(ma_sync_key_mm);
  2134. err_free_rq_db_key:
  2135. if (!attrs->srq)
  2136. kfree(rq_db_key_mm);
  2137. err_free_sq_db_key:
  2138. kfree(sq_db_key_mm);
  2139. err_free_rq_key:
  2140. if (!attrs->srq)
  2141. kfree(rq_key_mm);
  2142. err_free_sq_key:
  2143. kfree(sq_key_mm);
  2144. err_remove_handle:
  2145. remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
  2146. err_destroy_qp:
  2147. destroy_qp(&rhp->rdev, &qhp->wq,
  2148. ucontext ? &ucontext->uctx : &rhp->rdev.uctx, !attrs->srq);
  2149. err_free_wr_wait:
  2150. c4iw_put_wr_wait(qhp->wr_waitp);
  2151. err_free_qhp:
  2152. kfree(qhp);
  2153. return ERR_PTR(ret);
  2154. }
  2155. int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  2156. int attr_mask, struct ib_udata *udata)
  2157. {
  2158. struct c4iw_dev *rhp;
  2159. struct c4iw_qp *qhp;
  2160. enum c4iw_qp_attr_mask mask = 0;
  2161. struct c4iw_qp_attributes attrs;
  2162. pr_debug("ib_qp %p\n", ibqp);
  2163. /* iwarp does not support the RTR state */
  2164. if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
  2165. attr_mask &= ~IB_QP_STATE;
  2166. /* Make sure we still have something left to do */
  2167. if (!attr_mask)
  2168. return 0;
  2169. memset(&attrs, 0, sizeof attrs);
  2170. qhp = to_c4iw_qp(ibqp);
  2171. rhp = qhp->rhp;
  2172. attrs.next_state = c4iw_convert_state(attr->qp_state);
  2173. attrs.enable_rdma_read = (attr->qp_access_flags &
  2174. IB_ACCESS_REMOTE_READ) ? 1 : 0;
  2175. attrs.enable_rdma_write = (attr->qp_access_flags &
  2176. IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  2177. attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
  2178. mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
  2179. mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
  2180. (C4IW_QP_ATTR_ENABLE_RDMA_READ |
  2181. C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
  2182. C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
  2183. /*
  2184. * Use SQ_PSN and RQ_PSN to pass in IDX_INC values for
  2185. * ringing the queue db when we're in DB_FULL mode.
  2186. * Only allow this on T4 devices.
  2187. */
  2188. attrs.sq_db_inc = attr->sq_psn;
  2189. attrs.rq_db_inc = attr->rq_psn;
  2190. mask |= (attr_mask & IB_QP_SQ_PSN) ? C4IW_QP_ATTR_SQ_DB : 0;
  2191. mask |= (attr_mask & IB_QP_RQ_PSN) ? C4IW_QP_ATTR_RQ_DB : 0;
  2192. if (!is_t4(to_c4iw_qp(ibqp)->rhp->rdev.lldi.adapter_type) &&
  2193. (mask & (C4IW_QP_ATTR_SQ_DB|C4IW_QP_ATTR_RQ_DB)))
  2194. return -EINVAL;
  2195. return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
  2196. }
  2197. struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
  2198. {
  2199. pr_debug("ib_dev %p qpn 0x%x\n", dev, qpn);
  2200. return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
  2201. }
  2202. void c4iw_dispatch_srq_limit_reached_event(struct c4iw_srq *srq)
  2203. {
  2204. struct ib_event event = {};
  2205. event.device = &srq->rhp->ibdev;
  2206. event.element.srq = &srq->ibsrq;
  2207. event.event = IB_EVENT_SRQ_LIMIT_REACHED;
  2208. ib_dispatch_event(&event);
  2209. }
  2210. int c4iw_modify_srq(struct ib_srq *ib_srq, struct ib_srq_attr *attr,
  2211. enum ib_srq_attr_mask srq_attr_mask,
  2212. struct ib_udata *udata)
  2213. {
  2214. struct c4iw_srq *srq = to_c4iw_srq(ib_srq);
  2215. int ret = 0;
  2216. /*
  2217. * XXX 0 mask == a SW interrupt for srq_limit reached...
  2218. */
  2219. if (udata && !srq_attr_mask) {
  2220. c4iw_dispatch_srq_limit_reached_event(srq);
  2221. goto out;
  2222. }
  2223. /* no support for this yet */
  2224. if (srq_attr_mask & IB_SRQ_MAX_WR) {
  2225. ret = -EINVAL;
  2226. goto out;
  2227. }
  2228. if (!udata && (srq_attr_mask & IB_SRQ_LIMIT)) {
  2229. srq->armed = true;
  2230. srq->srq_limit = attr->srq_limit;
  2231. }
  2232. out:
  2233. return ret;
  2234. }
  2235. int c4iw_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  2236. int attr_mask, struct ib_qp_init_attr *init_attr)
  2237. {
  2238. struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
  2239. memset(attr, 0, sizeof *attr);
  2240. memset(init_attr, 0, sizeof *init_attr);
  2241. attr->qp_state = to_ib_qp_state(qhp->attr.state);
  2242. init_attr->cap.max_send_wr = qhp->attr.sq_num_entries;
  2243. init_attr->cap.max_recv_wr = qhp->attr.rq_num_entries;
  2244. init_attr->cap.max_send_sge = qhp->attr.sq_max_sges;
  2245. init_attr->cap.max_recv_sge = qhp->attr.sq_max_sges;
  2246. init_attr->cap.max_inline_data = T4_MAX_SEND_INLINE;
  2247. init_attr->sq_sig_type = qhp->sq_sig_all ? IB_SIGNAL_ALL_WR : 0;
  2248. return 0;
  2249. }
  2250. static void free_srq_queue(struct c4iw_srq *srq, struct c4iw_dev_ucontext *uctx,
  2251. struct c4iw_wr_wait *wr_waitp)
  2252. {
  2253. struct c4iw_rdev *rdev = &srq->rhp->rdev;
  2254. struct sk_buff *skb = srq->destroy_skb;
  2255. struct t4_srq *wq = &srq->wq;
  2256. struct fw_ri_res_wr *res_wr;
  2257. struct fw_ri_res *res;
  2258. int wr_len;
  2259. wr_len = sizeof(*res_wr) + sizeof(*res);
  2260. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  2261. res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
  2262. memset(res_wr, 0, wr_len);
  2263. res_wr->op_nres = cpu_to_be32(FW_WR_OP_V(FW_RI_RES_WR) |
  2264. FW_RI_RES_WR_NRES_V(1) |
  2265. FW_WR_COMPL_F);
  2266. res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
  2267. res_wr->cookie = (uintptr_t)wr_waitp;
  2268. res = res_wr->res;
  2269. res->u.srq.restype = FW_RI_RES_TYPE_SRQ;
  2270. res->u.srq.op = FW_RI_RES_OP_RESET;
  2271. res->u.srq.srqid = cpu_to_be32(srq->idx);
  2272. res->u.srq.eqid = cpu_to_be32(wq->qid);
  2273. c4iw_init_wr_wait(wr_waitp);
  2274. c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, 0, __func__);
  2275. dma_free_coherent(&rdev->lldi.pdev->dev,
  2276. wq->memsize, wq->queue,
  2277. pci_unmap_addr(wq, mapping));
  2278. c4iw_rqtpool_free(rdev, wq->rqt_hwaddr, wq->rqt_size);
  2279. kfree(wq->sw_rq);
  2280. c4iw_put_qpid(rdev, wq->qid, uctx);
  2281. }
  2282. static int alloc_srq_queue(struct c4iw_srq *srq, struct c4iw_dev_ucontext *uctx,
  2283. struct c4iw_wr_wait *wr_waitp)
  2284. {
  2285. struct c4iw_rdev *rdev = &srq->rhp->rdev;
  2286. int user = (uctx != &rdev->uctx);
  2287. struct t4_srq *wq = &srq->wq;
  2288. struct fw_ri_res_wr *res_wr;
  2289. struct fw_ri_res *res;
  2290. struct sk_buff *skb;
  2291. int wr_len;
  2292. int eqsize;
  2293. int ret = -ENOMEM;
  2294. wq->qid = c4iw_get_qpid(rdev, uctx);
  2295. if (!wq->qid)
  2296. goto err;
  2297. if (!user) {
  2298. wq->sw_rq = kcalloc(wq->size, sizeof(*wq->sw_rq),
  2299. GFP_KERNEL);
  2300. if (!wq->sw_rq)
  2301. goto err_put_qpid;
  2302. wq->pending_wrs = kcalloc(srq->wq.size,
  2303. sizeof(*srq->wq.pending_wrs),
  2304. GFP_KERNEL);
  2305. if (!wq->pending_wrs)
  2306. goto err_free_sw_rq;
  2307. }
  2308. wq->rqt_size = wq->size;
  2309. wq->rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rqt_size);
  2310. if (!wq->rqt_hwaddr)
  2311. goto err_free_pending_wrs;
  2312. wq->rqt_abs_idx = (wq->rqt_hwaddr - rdev->lldi.vr->rq.start) >>
  2313. T4_RQT_ENTRY_SHIFT;
  2314. wq->queue = dma_alloc_coherent(&rdev->lldi.pdev->dev,
  2315. wq->memsize, &wq->dma_addr,
  2316. GFP_KERNEL);
  2317. if (!wq->queue)
  2318. goto err_free_rqtpool;
  2319. memset(wq->queue, 0, wq->memsize);
  2320. pci_unmap_addr_set(wq, mapping, wq->dma_addr);
  2321. wq->bar2_va = c4iw_bar2_addrs(rdev, wq->qid, CXGB4_BAR2_QTYPE_EGRESS,
  2322. &wq->bar2_qid,
  2323. user ? &wq->bar2_pa : NULL);
  2324. /*
  2325. * User mode must have bar2 access.
  2326. */
  2327. if (user && !wq->bar2_va) {
  2328. pr_warn(MOD "%s: srqid %u not in BAR2 range.\n",
  2329. pci_name(rdev->lldi.pdev), wq->qid);
  2330. ret = -EINVAL;
  2331. goto err_free_queue;
  2332. }
  2333. /* build fw_ri_res_wr */
  2334. wr_len = sizeof(*res_wr) + sizeof(*res);
  2335. skb = alloc_skb(wr_len, GFP_KERNEL | __GFP_NOFAIL);
  2336. if (!skb)
  2337. goto err_free_queue;
  2338. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  2339. res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
  2340. memset(res_wr, 0, wr_len);
  2341. res_wr->op_nres = cpu_to_be32(FW_WR_OP_V(FW_RI_RES_WR) |
  2342. FW_RI_RES_WR_NRES_V(1) |
  2343. FW_WR_COMPL_F);
  2344. res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
  2345. res_wr->cookie = (uintptr_t)wr_waitp;
  2346. res = res_wr->res;
  2347. res->u.srq.restype = FW_RI_RES_TYPE_SRQ;
  2348. res->u.srq.op = FW_RI_RES_OP_WRITE;
  2349. /*
  2350. * eqsize is the number of 64B entries plus the status page size.
  2351. */
  2352. eqsize = wq->size * T4_RQ_NUM_SLOTS +
  2353. rdev->hw_queue.t4_eq_status_entries;
  2354. res->u.srq.eqid = cpu_to_be32(wq->qid);
  2355. res->u.srq.fetchszm_to_iqid =
  2356. /* no host cidx updates */
  2357. cpu_to_be32(FW_RI_RES_WR_HOSTFCMODE_V(0) |
  2358. FW_RI_RES_WR_CPRIO_V(0) | /* don't keep in chip cache */
  2359. FW_RI_RES_WR_PCIECHN_V(0) | /* set by uP at ri_init time */
  2360. FW_RI_RES_WR_FETCHRO_V(0)); /* relaxed_ordering */
  2361. res->u.srq.dcaen_to_eqsize =
  2362. cpu_to_be32(FW_RI_RES_WR_DCAEN_V(0) |
  2363. FW_RI_RES_WR_DCACPU_V(0) |
  2364. FW_RI_RES_WR_FBMIN_V(2) |
  2365. FW_RI_RES_WR_FBMAX_V(3) |
  2366. FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
  2367. FW_RI_RES_WR_CIDXFTHRESH_V(0) |
  2368. FW_RI_RES_WR_EQSIZE_V(eqsize));
  2369. res->u.srq.eqaddr = cpu_to_be64(wq->dma_addr);
  2370. res->u.srq.srqid = cpu_to_be32(srq->idx);
  2371. res->u.srq.pdid = cpu_to_be32(srq->pdid);
  2372. res->u.srq.hwsrqsize = cpu_to_be32(wq->rqt_size);
  2373. res->u.srq.hwsrqaddr = cpu_to_be32(wq->rqt_hwaddr -
  2374. rdev->lldi.vr->rq.start);
  2375. c4iw_init_wr_wait(wr_waitp);
  2376. ret = c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, wq->qid, __func__);
  2377. if (ret)
  2378. goto err_free_queue;
  2379. pr_debug("%s srq %u eqid %u pdid %u queue va %p pa 0x%llx\n"
  2380. " bar2_addr %p rqt addr 0x%x size %d\n",
  2381. __func__, srq->idx, wq->qid, srq->pdid, wq->queue,
  2382. (u64)virt_to_phys(wq->queue), wq->bar2_va,
  2383. wq->rqt_hwaddr, wq->rqt_size);
  2384. return 0;
  2385. err_free_queue:
  2386. dma_free_coherent(&rdev->lldi.pdev->dev,
  2387. wq->memsize, wq->queue,
  2388. pci_unmap_addr(wq, mapping));
  2389. err_free_rqtpool:
  2390. c4iw_rqtpool_free(rdev, wq->rqt_hwaddr, wq->rqt_size);
  2391. err_free_pending_wrs:
  2392. if (!user)
  2393. kfree(wq->pending_wrs);
  2394. err_free_sw_rq:
  2395. if (!user)
  2396. kfree(wq->sw_rq);
  2397. err_put_qpid:
  2398. c4iw_put_qpid(rdev, wq->qid, uctx);
  2399. err:
  2400. return ret;
  2401. }
  2402. void c4iw_copy_wr_to_srq(struct t4_srq *srq, union t4_recv_wr *wqe, u8 len16)
  2403. {
  2404. u64 *src, *dst;
  2405. src = (u64 *)wqe;
  2406. dst = (u64 *)((u8 *)srq->queue + srq->wq_pidx * T4_EQ_ENTRY_SIZE);
  2407. while (len16) {
  2408. *dst++ = *src++;
  2409. if (dst >= (u64 *)&srq->queue[srq->size])
  2410. dst = (u64 *)srq->queue;
  2411. *dst++ = *src++;
  2412. if (dst >= (u64 *)&srq->queue[srq->size])
  2413. dst = (u64 *)srq->queue;
  2414. len16--;
  2415. }
  2416. }
  2417. struct ib_srq *c4iw_create_srq(struct ib_pd *pd, struct ib_srq_init_attr *attrs,
  2418. struct ib_udata *udata)
  2419. {
  2420. struct c4iw_dev *rhp;
  2421. struct c4iw_srq *srq;
  2422. struct c4iw_pd *php;
  2423. struct c4iw_create_srq_resp uresp;
  2424. struct c4iw_ucontext *ucontext;
  2425. struct c4iw_mm_entry *srq_key_mm, *srq_db_key_mm;
  2426. int rqsize;
  2427. int ret;
  2428. int wr_len;
  2429. pr_debug("%s ib_pd %p\n", __func__, pd);
  2430. php = to_c4iw_pd(pd);
  2431. rhp = php->rhp;
  2432. if (!rhp->rdev.lldi.vr->srq.size)
  2433. return ERR_PTR(-EINVAL);
  2434. if (attrs->attr.max_wr > rhp->rdev.hw_queue.t4_max_rq_size)
  2435. return ERR_PTR(-E2BIG);
  2436. if (attrs->attr.max_sge > T4_MAX_RECV_SGE)
  2437. return ERR_PTR(-E2BIG);
  2438. /*
  2439. * SRQ RQT and RQ must be a power of 2 and at least 16 deep.
  2440. */
  2441. rqsize = attrs->attr.max_wr + 1;
  2442. rqsize = roundup_pow_of_two(max_t(u16, rqsize, 16));
  2443. ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
  2444. srq = kzalloc(sizeof(*srq), GFP_KERNEL);
  2445. if (!srq)
  2446. return ERR_PTR(-ENOMEM);
  2447. srq->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
  2448. if (!srq->wr_waitp) {
  2449. ret = -ENOMEM;
  2450. goto err_free_srq;
  2451. }
  2452. srq->idx = c4iw_alloc_srq_idx(&rhp->rdev);
  2453. if (srq->idx < 0) {
  2454. ret = -ENOMEM;
  2455. goto err_free_wr_wait;
  2456. }
  2457. wr_len = sizeof(struct fw_ri_res_wr) + sizeof(struct fw_ri_res);
  2458. srq->destroy_skb = alloc_skb(wr_len, GFP_KERNEL);
  2459. if (!srq->destroy_skb) {
  2460. ret = -ENOMEM;
  2461. goto err_free_srq_idx;
  2462. }
  2463. srq->rhp = rhp;
  2464. srq->pdid = php->pdid;
  2465. srq->wq.size = rqsize;
  2466. srq->wq.memsize =
  2467. (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
  2468. sizeof(*srq->wq.queue);
  2469. if (ucontext)
  2470. srq->wq.memsize = roundup(srq->wq.memsize, PAGE_SIZE);
  2471. ret = alloc_srq_queue(srq, ucontext ? &ucontext->uctx :
  2472. &rhp->rdev.uctx, srq->wr_waitp);
  2473. if (ret)
  2474. goto err_free_skb;
  2475. attrs->attr.max_wr = rqsize - 1;
  2476. if (CHELSIO_CHIP_VERSION(rhp->rdev.lldi.adapter_type) > CHELSIO_T6)
  2477. srq->flags = T4_SRQ_LIMIT_SUPPORT;
  2478. ret = insert_handle(rhp, &rhp->qpidr, srq, srq->wq.qid);
  2479. if (ret)
  2480. goto err_free_queue;
  2481. if (udata) {
  2482. srq_key_mm = kmalloc(sizeof(*srq_key_mm), GFP_KERNEL);
  2483. if (!srq_key_mm) {
  2484. ret = -ENOMEM;
  2485. goto err_remove_handle;
  2486. }
  2487. srq_db_key_mm = kmalloc(sizeof(*srq_db_key_mm), GFP_KERNEL);
  2488. if (!srq_db_key_mm) {
  2489. ret = -ENOMEM;
  2490. goto err_free_srq_key_mm;
  2491. }
  2492. memset(&uresp, 0, sizeof(uresp));
  2493. uresp.flags = srq->flags;
  2494. uresp.qid_mask = rhp->rdev.qpmask;
  2495. uresp.srqid = srq->wq.qid;
  2496. uresp.srq_size = srq->wq.size;
  2497. uresp.srq_memsize = srq->wq.memsize;
  2498. uresp.rqt_abs_idx = srq->wq.rqt_abs_idx;
  2499. spin_lock(&ucontext->mmap_lock);
  2500. uresp.srq_key = ucontext->key;
  2501. ucontext->key += PAGE_SIZE;
  2502. uresp.srq_db_gts_key = ucontext->key;
  2503. ucontext->key += PAGE_SIZE;
  2504. spin_unlock(&ucontext->mmap_lock);
  2505. ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  2506. if (ret)
  2507. goto err_free_srq_db_key_mm;
  2508. srq_key_mm->key = uresp.srq_key;
  2509. srq_key_mm->addr = virt_to_phys(srq->wq.queue);
  2510. srq_key_mm->len = PAGE_ALIGN(srq->wq.memsize);
  2511. insert_mmap(ucontext, srq_key_mm);
  2512. srq_db_key_mm->key = uresp.srq_db_gts_key;
  2513. srq_db_key_mm->addr = (u64)(unsigned long)srq->wq.bar2_pa;
  2514. srq_db_key_mm->len = PAGE_SIZE;
  2515. insert_mmap(ucontext, srq_db_key_mm);
  2516. }
  2517. pr_debug("%s srq qid %u idx %u size %u memsize %lu num_entries %u\n",
  2518. __func__, srq->wq.qid, srq->idx, srq->wq.size,
  2519. (unsigned long)srq->wq.memsize, attrs->attr.max_wr);
  2520. spin_lock_init(&srq->lock);
  2521. return &srq->ibsrq;
  2522. err_free_srq_db_key_mm:
  2523. kfree(srq_db_key_mm);
  2524. err_free_srq_key_mm:
  2525. kfree(srq_key_mm);
  2526. err_remove_handle:
  2527. remove_handle(rhp, &rhp->qpidr, srq->wq.qid);
  2528. err_free_queue:
  2529. free_srq_queue(srq, ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
  2530. srq->wr_waitp);
  2531. err_free_skb:
  2532. if (srq->destroy_skb)
  2533. kfree_skb(srq->destroy_skb);
  2534. err_free_srq_idx:
  2535. c4iw_free_srq_idx(&rhp->rdev, srq->idx);
  2536. err_free_wr_wait:
  2537. c4iw_put_wr_wait(srq->wr_waitp);
  2538. err_free_srq:
  2539. kfree(srq);
  2540. return ERR_PTR(ret);
  2541. }
  2542. int c4iw_destroy_srq(struct ib_srq *ibsrq)
  2543. {
  2544. struct c4iw_dev *rhp;
  2545. struct c4iw_srq *srq;
  2546. struct c4iw_ucontext *ucontext;
  2547. srq = to_c4iw_srq(ibsrq);
  2548. rhp = srq->rhp;
  2549. pr_debug("%s id %d\n", __func__, srq->wq.qid);
  2550. remove_handle(rhp, &rhp->qpidr, srq->wq.qid);
  2551. ucontext = ibsrq->uobject ?
  2552. to_c4iw_ucontext(ibsrq->uobject->context) : NULL;
  2553. free_srq_queue(srq, ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
  2554. srq->wr_waitp);
  2555. c4iw_free_srq_idx(&rhp->rdev, srq->idx);
  2556. c4iw_put_wr_wait(srq->wr_waitp);
  2557. kfree(srq);
  2558. return 0;
  2559. }