qp.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110
  1. /*
  2. * Copyright(c) 2016, 2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/hash.h>
  48. #include <linux/bitops.h>
  49. #include <linux/lockdep.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/slab.h>
  52. #include <rdma/ib_verbs.h>
  53. #include <rdma/ib_hdrs.h>
  54. #include <rdma/opa_addr.h>
  55. #include "qp.h"
  56. #include "vt.h"
  57. #include "trace.h"
  58. static void rvt_rc_timeout(struct timer_list *t);
  59. /*
  60. * Convert the AETH RNR timeout code into the number of microseconds.
  61. */
  62. static const u32 ib_rvt_rnr_table[32] = {
  63. 655360, /* 00: 655.36 */
  64. 10, /* 01: .01 */
  65. 20, /* 02 .02 */
  66. 30, /* 03: .03 */
  67. 40, /* 04: .04 */
  68. 60, /* 05: .06 */
  69. 80, /* 06: .08 */
  70. 120, /* 07: .12 */
  71. 160, /* 08: .16 */
  72. 240, /* 09: .24 */
  73. 320, /* 0A: .32 */
  74. 480, /* 0B: .48 */
  75. 640, /* 0C: .64 */
  76. 960, /* 0D: .96 */
  77. 1280, /* 0E: 1.28 */
  78. 1920, /* 0F: 1.92 */
  79. 2560, /* 10: 2.56 */
  80. 3840, /* 11: 3.84 */
  81. 5120, /* 12: 5.12 */
  82. 7680, /* 13: 7.68 */
  83. 10240, /* 14: 10.24 */
  84. 15360, /* 15: 15.36 */
  85. 20480, /* 16: 20.48 */
  86. 30720, /* 17: 30.72 */
  87. 40960, /* 18: 40.96 */
  88. 61440, /* 19: 61.44 */
  89. 81920, /* 1A: 81.92 */
  90. 122880, /* 1B: 122.88 */
  91. 163840, /* 1C: 163.84 */
  92. 245760, /* 1D: 245.76 */
  93. 327680, /* 1E: 327.68 */
  94. 491520 /* 1F: 491.52 */
  95. };
  96. /*
  97. * Note that it is OK to post send work requests in the SQE and ERR
  98. * states; rvt_do_send() will process them and generate error
  99. * completions as per IB 1.2 C10-96.
  100. */
  101. const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
  102. [IB_QPS_RESET] = 0,
  103. [IB_QPS_INIT] = RVT_POST_RECV_OK,
  104. [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
  105. [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  106. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
  107. RVT_PROCESS_NEXT_SEND_OK,
  108. [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  109. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
  110. [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  111. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  112. [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
  113. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  114. };
  115. EXPORT_SYMBOL(ib_rvt_state_ops);
  116. /* platform specific: return the last level cache (llc) size, in KiB */
  117. static int rvt_wss_llc_size(void)
  118. {
  119. /* assume that the boot CPU value is universal for all CPUs */
  120. return boot_cpu_data.x86_cache_size;
  121. }
  122. /* platform specific: cacheless copy */
  123. static void cacheless_memcpy(void *dst, void *src, size_t n)
  124. {
  125. /*
  126. * Use the only available X64 cacheless copy. Add a __user cast
  127. * to quiet sparse. The src agument is already in the kernel so
  128. * there are no security issues. The extra fault recovery machinery
  129. * is not invoked.
  130. */
  131. __copy_user_nocache(dst, (void __user *)src, n, 0);
  132. }
  133. void rvt_wss_exit(struct rvt_dev_info *rdi)
  134. {
  135. struct rvt_wss *wss = rdi->wss;
  136. if (!wss)
  137. return;
  138. /* coded to handle partially initialized and repeat callers */
  139. kfree(wss->entries);
  140. wss->entries = NULL;
  141. kfree(rdi->wss);
  142. rdi->wss = NULL;
  143. }
  144. /**
  145. * rvt_wss_init - Init wss data structures
  146. *
  147. * Return: 0 on success
  148. */
  149. int rvt_wss_init(struct rvt_dev_info *rdi)
  150. {
  151. unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode;
  152. unsigned int wss_threshold = rdi->dparms.wss_threshold;
  153. unsigned int wss_clean_period = rdi->dparms.wss_clean_period;
  154. long llc_size;
  155. long llc_bits;
  156. long table_size;
  157. long table_bits;
  158. struct rvt_wss *wss;
  159. int node = rdi->dparms.node;
  160. if (sge_copy_mode != RVT_SGE_COPY_ADAPTIVE) {
  161. rdi->wss = NULL;
  162. return 0;
  163. }
  164. rdi->wss = kzalloc_node(sizeof(*rdi->wss), GFP_KERNEL, node);
  165. if (!rdi->wss)
  166. return -ENOMEM;
  167. wss = rdi->wss;
  168. /* check for a valid percent range - default to 80 if none or invalid */
  169. if (wss_threshold < 1 || wss_threshold > 100)
  170. wss_threshold = 80;
  171. /* reject a wildly large period */
  172. if (wss_clean_period > 1000000)
  173. wss_clean_period = 256;
  174. /* reject a zero period */
  175. if (wss_clean_period == 0)
  176. wss_clean_period = 1;
  177. /*
  178. * Calculate the table size - the next power of 2 larger than the
  179. * LLC size. LLC size is in KiB.
  180. */
  181. llc_size = rvt_wss_llc_size() * 1024;
  182. table_size = roundup_pow_of_two(llc_size);
  183. /* one bit per page in rounded up table */
  184. llc_bits = llc_size / PAGE_SIZE;
  185. table_bits = table_size / PAGE_SIZE;
  186. wss->pages_mask = table_bits - 1;
  187. wss->num_entries = table_bits / BITS_PER_LONG;
  188. wss->threshold = (llc_bits * wss_threshold) / 100;
  189. if (wss->threshold == 0)
  190. wss->threshold = 1;
  191. wss->clean_period = wss_clean_period;
  192. atomic_set(&wss->clean_counter, wss_clean_period);
  193. wss->entries = kcalloc_node(wss->num_entries, sizeof(*wss->entries),
  194. GFP_KERNEL, node);
  195. if (!wss->entries) {
  196. rvt_wss_exit(rdi);
  197. return -ENOMEM;
  198. }
  199. return 0;
  200. }
  201. /*
  202. * Advance the clean counter. When the clean period has expired,
  203. * clean an entry.
  204. *
  205. * This is implemented in atomics to avoid locking. Because multiple
  206. * variables are involved, it can be racy which can lead to slightly
  207. * inaccurate information. Since this is only a heuristic, this is
  208. * OK. Any innaccuracies will clean themselves out as the counter
  209. * advances. That said, it is unlikely the entry clean operation will
  210. * race - the next possible racer will not start until the next clean
  211. * period.
  212. *
  213. * The clean counter is implemented as a decrement to zero. When zero
  214. * is reached an entry is cleaned.
  215. */
  216. static void wss_advance_clean_counter(struct rvt_wss *wss)
  217. {
  218. int entry;
  219. int weight;
  220. unsigned long bits;
  221. /* become the cleaner if we decrement the counter to zero */
  222. if (atomic_dec_and_test(&wss->clean_counter)) {
  223. /*
  224. * Set, not add, the clean period. This avoids an issue
  225. * where the counter could decrement below the clean period.
  226. * Doing a set can result in lost decrements, slowing the
  227. * clean advance. Since this a heuristic, this possible
  228. * slowdown is OK.
  229. *
  230. * An alternative is to loop, advancing the counter by a
  231. * clean period until the result is > 0. However, this could
  232. * lead to several threads keeping another in the clean loop.
  233. * This could be mitigated by limiting the number of times
  234. * we stay in the loop.
  235. */
  236. atomic_set(&wss->clean_counter, wss->clean_period);
  237. /*
  238. * Uniquely grab the entry to clean and move to next.
  239. * The current entry is always the lower bits of
  240. * wss.clean_entry. The table size, wss.num_entries,
  241. * is always a power-of-2.
  242. */
  243. entry = (atomic_inc_return(&wss->clean_entry) - 1)
  244. & (wss->num_entries - 1);
  245. /* clear the entry and count the bits */
  246. bits = xchg(&wss->entries[entry], 0);
  247. weight = hweight64((u64)bits);
  248. /* only adjust the contended total count if needed */
  249. if (weight)
  250. atomic_sub(weight, &wss->total_count);
  251. }
  252. }
  253. /*
  254. * Insert the given address into the working set array.
  255. */
  256. static void wss_insert(struct rvt_wss *wss, void *address)
  257. {
  258. u32 page = ((unsigned long)address >> PAGE_SHIFT) & wss->pages_mask;
  259. u32 entry = page / BITS_PER_LONG; /* assumes this ends up a shift */
  260. u32 nr = page & (BITS_PER_LONG - 1);
  261. if (!test_and_set_bit(nr, &wss->entries[entry]))
  262. atomic_inc(&wss->total_count);
  263. wss_advance_clean_counter(wss);
  264. }
  265. /*
  266. * Is the working set larger than the threshold?
  267. */
  268. static inline bool wss_exceeds_threshold(struct rvt_wss *wss)
  269. {
  270. return atomic_read(&wss->total_count) >= wss->threshold;
  271. }
  272. static void get_map_page(struct rvt_qpn_table *qpt,
  273. struct rvt_qpn_map *map)
  274. {
  275. unsigned long page = get_zeroed_page(GFP_KERNEL);
  276. /*
  277. * Free the page if someone raced with us installing it.
  278. */
  279. spin_lock(&qpt->lock);
  280. if (map->page)
  281. free_page(page);
  282. else
  283. map->page = (void *)page;
  284. spin_unlock(&qpt->lock);
  285. }
  286. /**
  287. * init_qpn_table - initialize the QP number table for a device
  288. * @qpt: the QPN table
  289. */
  290. static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
  291. {
  292. u32 offset, i;
  293. struct rvt_qpn_map *map;
  294. int ret = 0;
  295. if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
  296. return -EINVAL;
  297. spin_lock_init(&qpt->lock);
  298. qpt->last = rdi->dparms.qpn_start;
  299. qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
  300. /*
  301. * Drivers may want some QPs beyond what we need for verbs let them use
  302. * our qpn table. No need for two. Lets go ahead and mark the bitmaps
  303. * for those. The reserved range must be *after* the range which verbs
  304. * will pick from.
  305. */
  306. /* Figure out number of bit maps needed before reserved range */
  307. qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
  308. /* This should always be zero */
  309. offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
  310. /* Starting with the first reserved bit map */
  311. map = &qpt->map[qpt->nmaps];
  312. rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
  313. rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
  314. for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
  315. if (!map->page) {
  316. get_map_page(qpt, map);
  317. if (!map->page) {
  318. ret = -ENOMEM;
  319. break;
  320. }
  321. }
  322. set_bit(offset, map->page);
  323. offset++;
  324. if (offset == RVT_BITS_PER_PAGE) {
  325. /* next page */
  326. qpt->nmaps++;
  327. map++;
  328. offset = 0;
  329. }
  330. }
  331. return ret;
  332. }
  333. /**
  334. * free_qpn_table - free the QP number table for a device
  335. * @qpt: the QPN table
  336. */
  337. static void free_qpn_table(struct rvt_qpn_table *qpt)
  338. {
  339. int i;
  340. for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
  341. free_page((unsigned long)qpt->map[i].page);
  342. }
  343. /**
  344. * rvt_driver_qp_init - Init driver qp resources
  345. * @rdi: rvt dev strucutre
  346. *
  347. * Return: 0 on success
  348. */
  349. int rvt_driver_qp_init(struct rvt_dev_info *rdi)
  350. {
  351. int i;
  352. int ret = -ENOMEM;
  353. if (!rdi->dparms.qp_table_size)
  354. return -EINVAL;
  355. /*
  356. * If driver is not doing any QP allocation then make sure it is
  357. * providing the necessary QP functions.
  358. */
  359. if (!rdi->driver_f.free_all_qps ||
  360. !rdi->driver_f.qp_priv_alloc ||
  361. !rdi->driver_f.qp_priv_free ||
  362. !rdi->driver_f.notify_qp_reset ||
  363. !rdi->driver_f.notify_restart_rc)
  364. return -EINVAL;
  365. /* allocate parent object */
  366. rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
  367. rdi->dparms.node);
  368. if (!rdi->qp_dev)
  369. return -ENOMEM;
  370. /* allocate hash table */
  371. rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
  372. rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
  373. rdi->qp_dev->qp_table =
  374. kmalloc_array_node(rdi->qp_dev->qp_table_size,
  375. sizeof(*rdi->qp_dev->qp_table),
  376. GFP_KERNEL, rdi->dparms.node);
  377. if (!rdi->qp_dev->qp_table)
  378. goto no_qp_table;
  379. for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
  380. RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
  381. spin_lock_init(&rdi->qp_dev->qpt_lock);
  382. /* initialize qpn map */
  383. if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
  384. goto fail_table;
  385. spin_lock_init(&rdi->n_qps_lock);
  386. return 0;
  387. fail_table:
  388. kfree(rdi->qp_dev->qp_table);
  389. free_qpn_table(&rdi->qp_dev->qpn_table);
  390. no_qp_table:
  391. kfree(rdi->qp_dev);
  392. return ret;
  393. }
  394. /**
  395. * free_all_qps - check for QPs still in use
  396. * @rdi: rvt device info structure
  397. *
  398. * There should not be any QPs still in use.
  399. * Free memory for table.
  400. */
  401. static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
  402. {
  403. unsigned long flags;
  404. struct rvt_qp *qp;
  405. unsigned n, qp_inuse = 0;
  406. spinlock_t *ql; /* work around too long line below */
  407. if (rdi->driver_f.free_all_qps)
  408. qp_inuse = rdi->driver_f.free_all_qps(rdi);
  409. qp_inuse += rvt_mcast_tree_empty(rdi);
  410. if (!rdi->qp_dev)
  411. return qp_inuse;
  412. ql = &rdi->qp_dev->qpt_lock;
  413. spin_lock_irqsave(ql, flags);
  414. for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
  415. qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
  416. lockdep_is_held(ql));
  417. RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
  418. for (; qp; qp = rcu_dereference_protected(qp->next,
  419. lockdep_is_held(ql)))
  420. qp_inuse++;
  421. }
  422. spin_unlock_irqrestore(ql, flags);
  423. synchronize_rcu();
  424. return qp_inuse;
  425. }
  426. /**
  427. * rvt_qp_exit - clean up qps on device exit
  428. * @rdi: rvt dev structure
  429. *
  430. * Check for qp leaks and free resources.
  431. */
  432. void rvt_qp_exit(struct rvt_dev_info *rdi)
  433. {
  434. u32 qps_inuse = rvt_free_all_qps(rdi);
  435. if (qps_inuse)
  436. rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
  437. qps_inuse);
  438. if (!rdi->qp_dev)
  439. return;
  440. kfree(rdi->qp_dev->qp_table);
  441. free_qpn_table(&rdi->qp_dev->qpn_table);
  442. kfree(rdi->qp_dev);
  443. }
  444. static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
  445. struct rvt_qpn_map *map, unsigned off)
  446. {
  447. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  448. }
  449. /**
  450. * alloc_qpn - Allocate the next available qpn or zero/one for QP type
  451. * IB_QPT_SMI/IB_QPT_GSI
  452. * @rdi: rvt device info structure
  453. * @qpt: queue pair number table pointer
  454. * @port_num: IB port number, 1 based, comes from core
  455. *
  456. * Return: The queue pair number
  457. */
  458. static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
  459. enum ib_qp_type type, u8 port_num)
  460. {
  461. u32 i, offset, max_scan, qpn;
  462. struct rvt_qpn_map *map;
  463. u32 ret;
  464. if (rdi->driver_f.alloc_qpn)
  465. return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
  466. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  467. unsigned n;
  468. ret = type == IB_QPT_GSI;
  469. n = 1 << (ret + 2 * (port_num - 1));
  470. spin_lock(&qpt->lock);
  471. if (qpt->flags & n)
  472. ret = -EINVAL;
  473. else
  474. qpt->flags |= n;
  475. spin_unlock(&qpt->lock);
  476. goto bail;
  477. }
  478. qpn = qpt->last + qpt->incr;
  479. if (qpn >= RVT_QPN_MAX)
  480. qpn = qpt->incr | ((qpt->last & 1) ^ 1);
  481. /* offset carries bit 0 */
  482. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  483. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  484. max_scan = qpt->nmaps - !offset;
  485. for (i = 0;;) {
  486. if (unlikely(!map->page)) {
  487. get_map_page(qpt, map);
  488. if (unlikely(!map->page))
  489. break;
  490. }
  491. do {
  492. if (!test_and_set_bit(offset, map->page)) {
  493. qpt->last = qpn;
  494. ret = qpn;
  495. goto bail;
  496. }
  497. offset += qpt->incr;
  498. /*
  499. * This qpn might be bogus if offset >= BITS_PER_PAGE.
  500. * That is OK. It gets re-assigned below
  501. */
  502. qpn = mk_qpn(qpt, map, offset);
  503. } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
  504. /*
  505. * In order to keep the number of pages allocated to a
  506. * minimum, we scan the all existing pages before increasing
  507. * the size of the bitmap table.
  508. */
  509. if (++i > max_scan) {
  510. if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
  511. break;
  512. map = &qpt->map[qpt->nmaps++];
  513. /* start at incr with current bit 0 */
  514. offset = qpt->incr | (offset & 1);
  515. } else if (map < &qpt->map[qpt->nmaps]) {
  516. ++map;
  517. /* start at incr with current bit 0 */
  518. offset = qpt->incr | (offset & 1);
  519. } else {
  520. map = &qpt->map[0];
  521. /* wrap to first map page, invert bit 0 */
  522. offset = qpt->incr | ((offset & 1) ^ 1);
  523. }
  524. /* there can be no set bits in low-order QoS bits */
  525. WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
  526. qpn = mk_qpn(qpt, map, offset);
  527. }
  528. ret = -ENOMEM;
  529. bail:
  530. return ret;
  531. }
  532. /**
  533. * rvt_clear_mr_refs - Drop help mr refs
  534. * @qp: rvt qp data structure
  535. * @clr_sends: If shoudl clear send side or not
  536. */
  537. static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
  538. {
  539. unsigned n;
  540. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  541. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  542. rvt_put_ss(&qp->s_rdma_read_sge);
  543. rvt_put_ss(&qp->r_sge);
  544. if (clr_sends) {
  545. while (qp->s_last != qp->s_head) {
  546. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  547. rvt_put_swqe(wqe);
  548. if (qp->ibqp.qp_type == IB_QPT_UD ||
  549. qp->ibqp.qp_type == IB_QPT_SMI ||
  550. qp->ibqp.qp_type == IB_QPT_GSI)
  551. atomic_dec(&ibah_to_rvtah(
  552. wqe->ud_wr.ah)->refcount);
  553. if (++qp->s_last >= qp->s_size)
  554. qp->s_last = 0;
  555. smp_wmb(); /* see qp_set_savail */
  556. }
  557. if (qp->s_rdma_mr) {
  558. rvt_put_mr(qp->s_rdma_mr);
  559. qp->s_rdma_mr = NULL;
  560. }
  561. }
  562. for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) {
  563. struct rvt_ack_entry *e = &qp->s_ack_queue[n];
  564. if (e->rdma_sge.mr) {
  565. rvt_put_mr(e->rdma_sge.mr);
  566. e->rdma_sge.mr = NULL;
  567. }
  568. }
  569. }
  570. /**
  571. * rvt_swqe_has_lkey - return true if lkey is used by swqe
  572. * @wqe - the send wqe
  573. * @lkey - the lkey
  574. *
  575. * Test the swqe for using lkey
  576. */
  577. static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey)
  578. {
  579. int i;
  580. for (i = 0; i < wqe->wr.num_sge; i++) {
  581. struct rvt_sge *sge = &wqe->sg_list[i];
  582. if (rvt_mr_has_lkey(sge->mr, lkey))
  583. return true;
  584. }
  585. return false;
  586. }
  587. /**
  588. * rvt_qp_sends_has_lkey - return true is qp sends use lkey
  589. * @qp - the rvt_qp
  590. * @lkey - the lkey
  591. */
  592. static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey)
  593. {
  594. u32 s_last = qp->s_last;
  595. while (s_last != qp->s_head) {
  596. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last);
  597. if (rvt_swqe_has_lkey(wqe, lkey))
  598. return true;
  599. if (++s_last >= qp->s_size)
  600. s_last = 0;
  601. }
  602. if (qp->s_rdma_mr)
  603. if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey))
  604. return true;
  605. return false;
  606. }
  607. /**
  608. * rvt_qp_acks_has_lkey - return true if acks have lkey
  609. * @qp - the qp
  610. * @lkey - the lkey
  611. */
  612. static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey)
  613. {
  614. int i;
  615. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  616. for (i = 0; qp->s_ack_queue && i < rvt_max_atomic(rdi); i++) {
  617. struct rvt_ack_entry *e = &qp->s_ack_queue[i];
  618. if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey))
  619. return true;
  620. }
  621. return false;
  622. }
  623. /*
  624. * rvt_qp_mr_clean - clean up remote ops for lkey
  625. * @qp - the qp
  626. * @lkey - the lkey that is being de-registered
  627. *
  628. * This routine checks if the lkey is being used by
  629. * the qp.
  630. *
  631. * If so, the qp is put into an error state to elminate
  632. * any references from the qp.
  633. */
  634. void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey)
  635. {
  636. bool lastwqe = false;
  637. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  638. qp->ibqp.qp_type == IB_QPT_GSI)
  639. /* avoid special QPs */
  640. return;
  641. spin_lock_irq(&qp->r_lock);
  642. spin_lock(&qp->s_hlock);
  643. spin_lock(&qp->s_lock);
  644. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  645. goto check_lwqe;
  646. if (rvt_ss_has_lkey(&qp->r_sge, lkey) ||
  647. rvt_qp_sends_has_lkey(qp, lkey) ||
  648. rvt_qp_acks_has_lkey(qp, lkey))
  649. lastwqe = rvt_error_qp(qp, IB_WC_LOC_PROT_ERR);
  650. check_lwqe:
  651. spin_unlock(&qp->s_lock);
  652. spin_unlock(&qp->s_hlock);
  653. spin_unlock_irq(&qp->r_lock);
  654. if (lastwqe) {
  655. struct ib_event ev;
  656. ev.device = qp->ibqp.device;
  657. ev.element.qp = &qp->ibqp;
  658. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  659. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  660. }
  661. }
  662. /**
  663. * rvt_remove_qp - remove qp form table
  664. * @rdi: rvt dev struct
  665. * @qp: qp to remove
  666. *
  667. * Remove the QP from the table so it can't be found asynchronously by
  668. * the receive routine.
  669. */
  670. static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  671. {
  672. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  673. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  674. unsigned long flags;
  675. int removed = 1;
  676. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  677. if (rcu_dereference_protected(rvp->qp[0],
  678. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  679. RCU_INIT_POINTER(rvp->qp[0], NULL);
  680. } else if (rcu_dereference_protected(rvp->qp[1],
  681. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  682. RCU_INIT_POINTER(rvp->qp[1], NULL);
  683. } else {
  684. struct rvt_qp *q;
  685. struct rvt_qp __rcu **qpp;
  686. removed = 0;
  687. qpp = &rdi->qp_dev->qp_table[n];
  688. for (; (q = rcu_dereference_protected(*qpp,
  689. lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
  690. qpp = &q->next) {
  691. if (q == qp) {
  692. RCU_INIT_POINTER(*qpp,
  693. rcu_dereference_protected(qp->next,
  694. lockdep_is_held(&rdi->qp_dev->qpt_lock)));
  695. removed = 1;
  696. trace_rvt_qpremove(qp, n);
  697. break;
  698. }
  699. }
  700. }
  701. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  702. if (removed) {
  703. synchronize_rcu();
  704. rvt_put_qp(qp);
  705. }
  706. }
  707. /**
  708. * rvt_init_qp - initialize the QP state to the reset state
  709. * @qp: the QP to init or reinit
  710. * @type: the QP type
  711. *
  712. * This function is called from both rvt_create_qp() and
  713. * rvt_reset_qp(). The difference is that the reset
  714. * patch the necessary locks to protect against concurent
  715. * access.
  716. */
  717. static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  718. enum ib_qp_type type)
  719. {
  720. qp->remote_qpn = 0;
  721. qp->qkey = 0;
  722. qp->qp_access_flags = 0;
  723. qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
  724. qp->s_hdrwords = 0;
  725. qp->s_wqe = NULL;
  726. qp->s_draining = 0;
  727. qp->s_next_psn = 0;
  728. qp->s_last_psn = 0;
  729. qp->s_sending_psn = 0;
  730. qp->s_sending_hpsn = 0;
  731. qp->s_psn = 0;
  732. qp->r_psn = 0;
  733. qp->r_msn = 0;
  734. if (type == IB_QPT_RC) {
  735. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  736. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  737. } else {
  738. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  739. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  740. }
  741. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  742. qp->r_nak_state = 0;
  743. qp->r_aflags = 0;
  744. qp->r_flags = 0;
  745. qp->s_head = 0;
  746. qp->s_tail = 0;
  747. qp->s_cur = 0;
  748. qp->s_acked = 0;
  749. qp->s_last = 0;
  750. qp->s_ssn = 1;
  751. qp->s_lsn = 0;
  752. qp->s_mig_state = IB_MIG_MIGRATED;
  753. qp->r_head_ack_queue = 0;
  754. qp->s_tail_ack_queue = 0;
  755. qp->s_num_rd_atomic = 0;
  756. if (qp->r_rq.wq) {
  757. qp->r_rq.wq->head = 0;
  758. qp->r_rq.wq->tail = 0;
  759. }
  760. qp->r_sge.num_sge = 0;
  761. atomic_set(&qp->s_reserved_used, 0);
  762. }
  763. /**
  764. * rvt_reset_qp - initialize the QP state to the reset state
  765. * @qp: the QP to reset
  766. * @type: the QP type
  767. *
  768. * r_lock, s_hlock, and s_lock are required to be held by the caller
  769. */
  770. static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  771. enum ib_qp_type type)
  772. __must_hold(&qp->s_lock)
  773. __must_hold(&qp->s_hlock)
  774. __must_hold(&qp->r_lock)
  775. {
  776. lockdep_assert_held(&qp->r_lock);
  777. lockdep_assert_held(&qp->s_hlock);
  778. lockdep_assert_held(&qp->s_lock);
  779. if (qp->state != IB_QPS_RESET) {
  780. qp->state = IB_QPS_RESET;
  781. /* Let drivers flush their waitlist */
  782. rdi->driver_f.flush_qp_waiters(qp);
  783. rvt_stop_rc_timers(qp);
  784. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
  785. spin_unlock(&qp->s_lock);
  786. spin_unlock(&qp->s_hlock);
  787. spin_unlock_irq(&qp->r_lock);
  788. /* Stop the send queue and the retry timer */
  789. rdi->driver_f.stop_send_queue(qp);
  790. rvt_del_timers_sync(qp);
  791. /* Wait for things to stop */
  792. rdi->driver_f.quiesce_qp(qp);
  793. /* take qp out the hash and wait for it to be unused */
  794. rvt_remove_qp(rdi, qp);
  795. /* grab the lock b/c it was locked at call time */
  796. spin_lock_irq(&qp->r_lock);
  797. spin_lock(&qp->s_hlock);
  798. spin_lock(&qp->s_lock);
  799. rvt_clear_mr_refs(qp, 1);
  800. /*
  801. * Let the driver do any tear down or re-init it needs to for
  802. * a qp that has been reset
  803. */
  804. rdi->driver_f.notify_qp_reset(qp);
  805. }
  806. rvt_init_qp(rdi, qp, type);
  807. lockdep_assert_held(&qp->r_lock);
  808. lockdep_assert_held(&qp->s_hlock);
  809. lockdep_assert_held(&qp->s_lock);
  810. }
  811. /** rvt_free_qpn - Free a qpn from the bit map
  812. * @qpt: QP table
  813. * @qpn: queue pair number to free
  814. */
  815. static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  816. {
  817. struct rvt_qpn_map *map;
  818. map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
  819. if (map->page)
  820. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  821. }
  822. /**
  823. * rvt_create_qp - create a queue pair for a device
  824. * @ibpd: the protection domain who's device we create the queue pair for
  825. * @init_attr: the attributes of the queue pair
  826. * @udata: user data for libibverbs.so
  827. *
  828. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  829. * unique idea of what queue pair numbers mean. For instance there is a reserved
  830. * range for PSM.
  831. *
  832. * Return: the queue pair on success, otherwise returns an errno.
  833. *
  834. * Called by the ib_create_qp() core verbs function.
  835. */
  836. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  837. struct ib_qp_init_attr *init_attr,
  838. struct ib_udata *udata)
  839. {
  840. struct rvt_qp *qp;
  841. int err;
  842. struct rvt_swqe *swq = NULL;
  843. size_t sz;
  844. size_t sg_list_sz;
  845. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  846. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  847. void *priv = NULL;
  848. size_t sqsize;
  849. if (!rdi)
  850. return ERR_PTR(-EINVAL);
  851. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_send_sge ||
  852. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  853. init_attr->create_flags)
  854. return ERR_PTR(-EINVAL);
  855. /* Check receive queue parameters if no SRQ is specified. */
  856. if (!init_attr->srq) {
  857. if (init_attr->cap.max_recv_sge >
  858. rdi->dparms.props.max_recv_sge ||
  859. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  860. return ERR_PTR(-EINVAL);
  861. if (init_attr->cap.max_send_sge +
  862. init_attr->cap.max_send_wr +
  863. init_attr->cap.max_recv_sge +
  864. init_attr->cap.max_recv_wr == 0)
  865. return ERR_PTR(-EINVAL);
  866. }
  867. sqsize =
  868. init_attr->cap.max_send_wr + 1 +
  869. rdi->dparms.reserved_operations;
  870. switch (init_attr->qp_type) {
  871. case IB_QPT_SMI:
  872. case IB_QPT_GSI:
  873. if (init_attr->port_num == 0 ||
  874. init_attr->port_num > ibpd->device->phys_port_cnt)
  875. return ERR_PTR(-EINVAL);
  876. /* fall through */
  877. case IB_QPT_UC:
  878. case IB_QPT_RC:
  879. case IB_QPT_UD:
  880. sz = sizeof(struct rvt_sge) *
  881. init_attr->cap.max_send_sge +
  882. sizeof(struct rvt_swqe);
  883. swq = vzalloc_node(array_size(sz, sqsize), rdi->dparms.node);
  884. if (!swq)
  885. return ERR_PTR(-ENOMEM);
  886. sz = sizeof(*qp);
  887. sg_list_sz = 0;
  888. if (init_attr->srq) {
  889. struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
  890. if (srq->rq.max_sge > 1)
  891. sg_list_sz = sizeof(*qp->r_sg_list) *
  892. (srq->rq.max_sge - 1);
  893. } else if (init_attr->cap.max_recv_sge > 1)
  894. sg_list_sz = sizeof(*qp->r_sg_list) *
  895. (init_attr->cap.max_recv_sge - 1);
  896. qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
  897. rdi->dparms.node);
  898. if (!qp)
  899. goto bail_swq;
  900. RCU_INIT_POINTER(qp->next, NULL);
  901. if (init_attr->qp_type == IB_QPT_RC) {
  902. qp->s_ack_queue =
  903. kcalloc_node(rvt_max_atomic(rdi),
  904. sizeof(*qp->s_ack_queue),
  905. GFP_KERNEL,
  906. rdi->dparms.node);
  907. if (!qp->s_ack_queue)
  908. goto bail_qp;
  909. }
  910. /* initialize timers needed for rc qp */
  911. timer_setup(&qp->s_timer, rvt_rc_timeout, 0);
  912. hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
  913. HRTIMER_MODE_REL);
  914. qp->s_rnr_timer.function = rvt_rc_rnr_retry;
  915. /*
  916. * Driver needs to set up it's private QP structure and do any
  917. * initialization that is needed.
  918. */
  919. priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
  920. if (IS_ERR(priv)) {
  921. ret = priv;
  922. goto bail_qp;
  923. }
  924. qp->priv = priv;
  925. qp->timeout_jiffies =
  926. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  927. 1000UL);
  928. if (init_attr->srq) {
  929. sz = 0;
  930. } else {
  931. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  932. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  933. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  934. sizeof(struct rvt_rwqe);
  935. if (udata)
  936. qp->r_rq.wq = vmalloc_user(
  937. sizeof(struct rvt_rwq) +
  938. qp->r_rq.size * sz);
  939. else
  940. qp->r_rq.wq = vzalloc_node(
  941. sizeof(struct rvt_rwq) +
  942. qp->r_rq.size * sz,
  943. rdi->dparms.node);
  944. if (!qp->r_rq.wq)
  945. goto bail_driver_priv;
  946. }
  947. /*
  948. * ib_create_qp() will initialize qp->ibqp
  949. * except for qp->ibqp.qp_num.
  950. */
  951. spin_lock_init(&qp->r_lock);
  952. spin_lock_init(&qp->s_hlock);
  953. spin_lock_init(&qp->s_lock);
  954. spin_lock_init(&qp->r_rq.lock);
  955. atomic_set(&qp->refcount, 0);
  956. atomic_set(&qp->local_ops_pending, 0);
  957. init_waitqueue_head(&qp->wait);
  958. INIT_LIST_HEAD(&qp->rspwait);
  959. qp->state = IB_QPS_RESET;
  960. qp->s_wq = swq;
  961. qp->s_size = sqsize;
  962. qp->s_avail = init_attr->cap.max_send_wr;
  963. qp->s_max_sge = init_attr->cap.max_send_sge;
  964. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  965. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  966. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  967. init_attr->qp_type,
  968. init_attr->port_num);
  969. if (err < 0) {
  970. ret = ERR_PTR(err);
  971. goto bail_rq_wq;
  972. }
  973. qp->ibqp.qp_num = err;
  974. qp->port_num = init_attr->port_num;
  975. rvt_init_qp(rdi, qp, init_attr->qp_type);
  976. break;
  977. default:
  978. /* Don't support raw QPs */
  979. return ERR_PTR(-EINVAL);
  980. }
  981. init_attr->cap.max_inline_data = 0;
  982. /*
  983. * Return the address of the RWQ as the offset to mmap.
  984. * See rvt_mmap() for details.
  985. */
  986. if (udata && udata->outlen >= sizeof(__u64)) {
  987. if (!qp->r_rq.wq) {
  988. __u64 offset = 0;
  989. err = ib_copy_to_udata(udata, &offset,
  990. sizeof(offset));
  991. if (err) {
  992. ret = ERR_PTR(err);
  993. goto bail_qpn;
  994. }
  995. } else {
  996. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  997. qp->ip = rvt_create_mmap_info(rdi, s,
  998. ibpd->uobject->context,
  999. qp->r_rq.wq);
  1000. if (!qp->ip) {
  1001. ret = ERR_PTR(-ENOMEM);
  1002. goto bail_qpn;
  1003. }
  1004. err = ib_copy_to_udata(udata, &qp->ip->offset,
  1005. sizeof(qp->ip->offset));
  1006. if (err) {
  1007. ret = ERR_PTR(err);
  1008. goto bail_ip;
  1009. }
  1010. }
  1011. qp->pid = current->pid;
  1012. }
  1013. spin_lock(&rdi->n_qps_lock);
  1014. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  1015. spin_unlock(&rdi->n_qps_lock);
  1016. ret = ERR_PTR(-ENOMEM);
  1017. goto bail_ip;
  1018. }
  1019. rdi->n_qps_allocated++;
  1020. /*
  1021. * Maintain a busy_jiffies variable that will be added to the timeout
  1022. * period in mod_retry_timer and add_retry_timer. This busy jiffies
  1023. * is scaled by the number of rc qps created for the device to reduce
  1024. * the number of timeouts occurring when there is a large number of
  1025. * qps. busy_jiffies is incremented every rc qp scaling interval.
  1026. * The scaling interval is selected based on extensive performance
  1027. * evaluation of targeted workloads.
  1028. */
  1029. if (init_attr->qp_type == IB_QPT_RC) {
  1030. rdi->n_rc_qps++;
  1031. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1032. }
  1033. spin_unlock(&rdi->n_qps_lock);
  1034. if (qp->ip) {
  1035. spin_lock_irq(&rdi->pending_lock);
  1036. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  1037. spin_unlock_irq(&rdi->pending_lock);
  1038. }
  1039. ret = &qp->ibqp;
  1040. /*
  1041. * We have our QP and its good, now keep track of what types of opcodes
  1042. * can be processed on this QP. We do this by keeping track of what the
  1043. * 3 high order bits of the opcode are.
  1044. */
  1045. switch (init_attr->qp_type) {
  1046. case IB_QPT_SMI:
  1047. case IB_QPT_GSI:
  1048. case IB_QPT_UD:
  1049. qp->allowed_ops = IB_OPCODE_UD;
  1050. break;
  1051. case IB_QPT_RC:
  1052. qp->allowed_ops = IB_OPCODE_RC;
  1053. break;
  1054. case IB_QPT_UC:
  1055. qp->allowed_ops = IB_OPCODE_UC;
  1056. break;
  1057. default:
  1058. ret = ERR_PTR(-EINVAL);
  1059. goto bail_ip;
  1060. }
  1061. return ret;
  1062. bail_ip:
  1063. if (qp->ip)
  1064. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1065. bail_qpn:
  1066. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1067. bail_rq_wq:
  1068. if (!qp->ip)
  1069. vfree(qp->r_rq.wq);
  1070. bail_driver_priv:
  1071. rdi->driver_f.qp_priv_free(rdi, qp);
  1072. bail_qp:
  1073. kfree(qp->s_ack_queue);
  1074. kfree(qp);
  1075. bail_swq:
  1076. vfree(swq);
  1077. return ret;
  1078. }
  1079. /**
  1080. * rvt_error_qp - put a QP into the error state
  1081. * @qp: the QP to put into the error state
  1082. * @err: the receive completion error to signal if a RWQE is active
  1083. *
  1084. * Flushes both send and receive work queues.
  1085. *
  1086. * Return: true if last WQE event should be generated.
  1087. * The QP r_lock and s_lock should be held and interrupts disabled.
  1088. * If we are already in error state, just return.
  1089. */
  1090. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  1091. {
  1092. struct ib_wc wc;
  1093. int ret = 0;
  1094. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1095. lockdep_assert_held(&qp->r_lock);
  1096. lockdep_assert_held(&qp->s_lock);
  1097. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  1098. goto bail;
  1099. qp->state = IB_QPS_ERR;
  1100. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  1101. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  1102. del_timer(&qp->s_timer);
  1103. }
  1104. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  1105. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  1106. rdi->driver_f.notify_error_qp(qp);
  1107. /* Schedule the sending tasklet to drain the send work queue. */
  1108. if (READ_ONCE(qp->s_last) != qp->s_head)
  1109. rdi->driver_f.schedule_send(qp);
  1110. rvt_clear_mr_refs(qp, 0);
  1111. memset(&wc, 0, sizeof(wc));
  1112. wc.qp = &qp->ibqp;
  1113. wc.opcode = IB_WC_RECV;
  1114. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  1115. wc.wr_id = qp->r_wr_id;
  1116. wc.status = err;
  1117. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1118. }
  1119. wc.status = IB_WC_WR_FLUSH_ERR;
  1120. if (qp->r_rq.wq) {
  1121. struct rvt_rwq *wq;
  1122. u32 head;
  1123. u32 tail;
  1124. spin_lock(&qp->r_rq.lock);
  1125. /* sanity check pointers before trusting them */
  1126. wq = qp->r_rq.wq;
  1127. head = wq->head;
  1128. if (head >= qp->r_rq.size)
  1129. head = 0;
  1130. tail = wq->tail;
  1131. if (tail >= qp->r_rq.size)
  1132. tail = 0;
  1133. while (tail != head) {
  1134. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  1135. if (++tail >= qp->r_rq.size)
  1136. tail = 0;
  1137. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1138. }
  1139. wq->tail = tail;
  1140. spin_unlock(&qp->r_rq.lock);
  1141. } else if (qp->ibqp.event_handler) {
  1142. ret = 1;
  1143. }
  1144. bail:
  1145. return ret;
  1146. }
  1147. EXPORT_SYMBOL(rvt_error_qp);
  1148. /*
  1149. * Put the QP into the hash table.
  1150. * The hash table holds a reference to the QP.
  1151. */
  1152. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  1153. {
  1154. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  1155. unsigned long flags;
  1156. rvt_get_qp(qp);
  1157. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  1158. if (qp->ibqp.qp_num <= 1) {
  1159. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  1160. } else {
  1161. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  1162. qp->next = rdi->qp_dev->qp_table[n];
  1163. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  1164. trace_rvt_qpinsert(qp, n);
  1165. }
  1166. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  1167. }
  1168. /**
  1169. * rvt_modify_qp - modify the attributes of a queue pair
  1170. * @ibqp: the queue pair who's attributes we're modifying
  1171. * @attr: the new attributes
  1172. * @attr_mask: the mask of attributes to modify
  1173. * @udata: user data for libibverbs.so
  1174. *
  1175. * Return: 0 on success, otherwise returns an errno.
  1176. */
  1177. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1178. int attr_mask, struct ib_udata *udata)
  1179. {
  1180. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1181. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1182. enum ib_qp_state cur_state, new_state;
  1183. struct ib_event ev;
  1184. int lastwqe = 0;
  1185. int mig = 0;
  1186. int pmtu = 0; /* for gcc warning only */
  1187. int opa_ah;
  1188. spin_lock_irq(&qp->r_lock);
  1189. spin_lock(&qp->s_hlock);
  1190. spin_lock(&qp->s_lock);
  1191. cur_state = attr_mask & IB_QP_CUR_STATE ?
  1192. attr->cur_qp_state : qp->state;
  1193. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1194. opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
  1195. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  1196. attr_mask))
  1197. goto inval;
  1198. if (rdi->driver_f.check_modify_qp &&
  1199. rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
  1200. goto inval;
  1201. if (attr_mask & IB_QP_AV) {
  1202. if (opa_ah) {
  1203. if (rdma_ah_get_dlid(&attr->ah_attr) >=
  1204. opa_get_mcast_base(OPA_MCAST_NR))
  1205. goto inval;
  1206. } else {
  1207. if (rdma_ah_get_dlid(&attr->ah_attr) >=
  1208. be16_to_cpu(IB_MULTICAST_LID_BASE))
  1209. goto inval;
  1210. }
  1211. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  1212. goto inval;
  1213. }
  1214. if (attr_mask & IB_QP_ALT_PATH) {
  1215. if (opa_ah) {
  1216. if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
  1217. opa_get_mcast_base(OPA_MCAST_NR))
  1218. goto inval;
  1219. } else {
  1220. if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
  1221. be16_to_cpu(IB_MULTICAST_LID_BASE))
  1222. goto inval;
  1223. }
  1224. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  1225. goto inval;
  1226. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  1227. goto inval;
  1228. }
  1229. if (attr_mask & IB_QP_PKEY_INDEX)
  1230. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  1231. goto inval;
  1232. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1233. if (attr->min_rnr_timer > 31)
  1234. goto inval;
  1235. if (attr_mask & IB_QP_PORT)
  1236. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  1237. qp->ibqp.qp_type == IB_QPT_GSI ||
  1238. attr->port_num == 0 ||
  1239. attr->port_num > ibqp->device->phys_port_cnt)
  1240. goto inval;
  1241. if (attr_mask & IB_QP_DEST_QPN)
  1242. if (attr->dest_qp_num > RVT_QPN_MASK)
  1243. goto inval;
  1244. if (attr_mask & IB_QP_RETRY_CNT)
  1245. if (attr->retry_cnt > 7)
  1246. goto inval;
  1247. if (attr_mask & IB_QP_RNR_RETRY)
  1248. if (attr->rnr_retry > 7)
  1249. goto inval;
  1250. /*
  1251. * Don't allow invalid path_mtu values. OK to set greater
  1252. * than the active mtu (or even the max_cap, if we have tuned
  1253. * that to a small mtu. We'll set qp->path_mtu
  1254. * to the lesser of requested attribute mtu and active,
  1255. * for packetizing messages.
  1256. * Note that the QP port has to be set in INIT and MTU in RTR.
  1257. */
  1258. if (attr_mask & IB_QP_PATH_MTU) {
  1259. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  1260. if (pmtu < 0)
  1261. goto inval;
  1262. }
  1263. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1264. if (attr->path_mig_state == IB_MIG_REARM) {
  1265. if (qp->s_mig_state == IB_MIG_ARMED)
  1266. goto inval;
  1267. if (new_state != IB_QPS_RTS)
  1268. goto inval;
  1269. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  1270. if (qp->s_mig_state == IB_MIG_REARM)
  1271. goto inval;
  1272. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  1273. goto inval;
  1274. if (qp->s_mig_state == IB_MIG_ARMED)
  1275. mig = 1;
  1276. } else {
  1277. goto inval;
  1278. }
  1279. }
  1280. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1281. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  1282. goto inval;
  1283. switch (new_state) {
  1284. case IB_QPS_RESET:
  1285. if (qp->state != IB_QPS_RESET)
  1286. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1287. break;
  1288. case IB_QPS_RTR:
  1289. /* Allow event to re-trigger if QP set to RTR more than once */
  1290. qp->r_flags &= ~RVT_R_COMM_EST;
  1291. qp->state = new_state;
  1292. break;
  1293. case IB_QPS_SQD:
  1294. qp->s_draining = qp->s_last != qp->s_cur;
  1295. qp->state = new_state;
  1296. break;
  1297. case IB_QPS_SQE:
  1298. if (qp->ibqp.qp_type == IB_QPT_RC)
  1299. goto inval;
  1300. qp->state = new_state;
  1301. break;
  1302. case IB_QPS_ERR:
  1303. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  1304. break;
  1305. default:
  1306. qp->state = new_state;
  1307. break;
  1308. }
  1309. if (attr_mask & IB_QP_PKEY_INDEX)
  1310. qp->s_pkey_index = attr->pkey_index;
  1311. if (attr_mask & IB_QP_PORT)
  1312. qp->port_num = attr->port_num;
  1313. if (attr_mask & IB_QP_DEST_QPN)
  1314. qp->remote_qpn = attr->dest_qp_num;
  1315. if (attr_mask & IB_QP_SQ_PSN) {
  1316. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  1317. qp->s_psn = qp->s_next_psn;
  1318. qp->s_sending_psn = qp->s_next_psn;
  1319. qp->s_last_psn = qp->s_next_psn - 1;
  1320. qp->s_sending_hpsn = qp->s_last_psn;
  1321. }
  1322. if (attr_mask & IB_QP_RQ_PSN)
  1323. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  1324. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1325. qp->qp_access_flags = attr->qp_access_flags;
  1326. if (attr_mask & IB_QP_AV) {
  1327. rdma_replace_ah_attr(&qp->remote_ah_attr, &attr->ah_attr);
  1328. qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
  1329. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  1330. }
  1331. if (attr_mask & IB_QP_ALT_PATH) {
  1332. rdma_replace_ah_attr(&qp->alt_ah_attr, &attr->alt_ah_attr);
  1333. qp->s_alt_pkey_index = attr->alt_pkey_index;
  1334. }
  1335. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1336. qp->s_mig_state = attr->path_mig_state;
  1337. if (mig) {
  1338. qp->remote_ah_attr = qp->alt_ah_attr;
  1339. qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
  1340. qp->s_pkey_index = qp->s_alt_pkey_index;
  1341. }
  1342. }
  1343. if (attr_mask & IB_QP_PATH_MTU) {
  1344. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  1345. qp->log_pmtu = ilog2(qp->pmtu);
  1346. }
  1347. if (attr_mask & IB_QP_RETRY_CNT) {
  1348. qp->s_retry_cnt = attr->retry_cnt;
  1349. qp->s_retry = attr->retry_cnt;
  1350. }
  1351. if (attr_mask & IB_QP_RNR_RETRY) {
  1352. qp->s_rnr_retry_cnt = attr->rnr_retry;
  1353. qp->s_rnr_retry = attr->rnr_retry;
  1354. }
  1355. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1356. qp->r_min_rnr_timer = attr->min_rnr_timer;
  1357. if (attr_mask & IB_QP_TIMEOUT) {
  1358. qp->timeout = attr->timeout;
  1359. qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
  1360. }
  1361. if (attr_mask & IB_QP_QKEY)
  1362. qp->qkey = attr->qkey;
  1363. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1364. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  1365. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1366. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1367. if (rdi->driver_f.modify_qp)
  1368. rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
  1369. spin_unlock(&qp->s_lock);
  1370. spin_unlock(&qp->s_hlock);
  1371. spin_unlock_irq(&qp->r_lock);
  1372. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1373. rvt_insert_qp(rdi, qp);
  1374. if (lastwqe) {
  1375. ev.device = qp->ibqp.device;
  1376. ev.element.qp = &qp->ibqp;
  1377. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1378. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1379. }
  1380. if (mig) {
  1381. ev.device = qp->ibqp.device;
  1382. ev.element.qp = &qp->ibqp;
  1383. ev.event = IB_EVENT_PATH_MIG;
  1384. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1385. }
  1386. return 0;
  1387. inval:
  1388. spin_unlock(&qp->s_lock);
  1389. spin_unlock(&qp->s_hlock);
  1390. spin_unlock_irq(&qp->r_lock);
  1391. return -EINVAL;
  1392. }
  1393. /**
  1394. * rvt_destroy_qp - destroy a queue pair
  1395. * @ibqp: the queue pair to destroy
  1396. *
  1397. * Note that this can be called while the QP is actively sending or
  1398. * receiving!
  1399. *
  1400. * Return: 0 on success.
  1401. */
  1402. int rvt_destroy_qp(struct ib_qp *ibqp)
  1403. {
  1404. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1405. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1406. spin_lock_irq(&qp->r_lock);
  1407. spin_lock(&qp->s_hlock);
  1408. spin_lock(&qp->s_lock);
  1409. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1410. spin_unlock(&qp->s_lock);
  1411. spin_unlock(&qp->s_hlock);
  1412. spin_unlock_irq(&qp->r_lock);
  1413. wait_event(qp->wait, !atomic_read(&qp->refcount));
  1414. /* qpn is now available for use again */
  1415. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1416. spin_lock(&rdi->n_qps_lock);
  1417. rdi->n_qps_allocated--;
  1418. if (qp->ibqp.qp_type == IB_QPT_RC) {
  1419. rdi->n_rc_qps--;
  1420. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1421. }
  1422. spin_unlock(&rdi->n_qps_lock);
  1423. if (qp->ip)
  1424. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1425. else
  1426. vfree(qp->r_rq.wq);
  1427. vfree(qp->s_wq);
  1428. rdi->driver_f.qp_priv_free(rdi, qp);
  1429. kfree(qp->s_ack_queue);
  1430. rdma_destroy_ah_attr(&qp->remote_ah_attr);
  1431. rdma_destroy_ah_attr(&qp->alt_ah_attr);
  1432. kfree(qp);
  1433. return 0;
  1434. }
  1435. /**
  1436. * rvt_query_qp - query an ipbq
  1437. * @ibqp: IB qp to query
  1438. * @attr: attr struct to fill in
  1439. * @attr_mask: attr mask ignored
  1440. * @init_attr: struct to fill in
  1441. *
  1442. * Return: always 0
  1443. */
  1444. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1445. int attr_mask, struct ib_qp_init_attr *init_attr)
  1446. {
  1447. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1448. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1449. attr->qp_state = qp->state;
  1450. attr->cur_qp_state = attr->qp_state;
  1451. attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  1452. attr->path_mig_state = qp->s_mig_state;
  1453. attr->qkey = qp->qkey;
  1454. attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
  1455. attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
  1456. attr->dest_qp_num = qp->remote_qpn;
  1457. attr->qp_access_flags = qp->qp_access_flags;
  1458. attr->cap.max_send_wr = qp->s_size - 1 -
  1459. rdi->dparms.reserved_operations;
  1460. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  1461. attr->cap.max_send_sge = qp->s_max_sge;
  1462. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  1463. attr->cap.max_inline_data = 0;
  1464. attr->ah_attr = qp->remote_ah_attr;
  1465. attr->alt_ah_attr = qp->alt_ah_attr;
  1466. attr->pkey_index = qp->s_pkey_index;
  1467. attr->alt_pkey_index = qp->s_alt_pkey_index;
  1468. attr->en_sqd_async_notify = 0;
  1469. attr->sq_draining = qp->s_draining;
  1470. attr->max_rd_atomic = qp->s_max_rd_atomic;
  1471. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  1472. attr->min_rnr_timer = qp->r_min_rnr_timer;
  1473. attr->port_num = qp->port_num;
  1474. attr->timeout = qp->timeout;
  1475. attr->retry_cnt = qp->s_retry_cnt;
  1476. attr->rnr_retry = qp->s_rnr_retry_cnt;
  1477. attr->alt_port_num =
  1478. rdma_ah_get_port_num(&qp->alt_ah_attr);
  1479. attr->alt_timeout = qp->alt_timeout;
  1480. init_attr->event_handler = qp->ibqp.event_handler;
  1481. init_attr->qp_context = qp->ibqp.qp_context;
  1482. init_attr->send_cq = qp->ibqp.send_cq;
  1483. init_attr->recv_cq = qp->ibqp.recv_cq;
  1484. init_attr->srq = qp->ibqp.srq;
  1485. init_attr->cap = attr->cap;
  1486. if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
  1487. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  1488. else
  1489. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  1490. init_attr->qp_type = qp->ibqp.qp_type;
  1491. init_attr->port_num = qp->port_num;
  1492. return 0;
  1493. }
  1494. /**
  1495. * rvt_post_receive - post a receive on a QP
  1496. * @ibqp: the QP to post the receive on
  1497. * @wr: the WR to post
  1498. * @bad_wr: the first bad WR is put here
  1499. *
  1500. * This may be called from interrupt context.
  1501. *
  1502. * Return: 0 on success otherwise errno
  1503. */
  1504. int rvt_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  1505. const struct ib_recv_wr **bad_wr)
  1506. {
  1507. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1508. struct rvt_rwq *wq = qp->r_rq.wq;
  1509. unsigned long flags;
  1510. int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
  1511. !qp->ibqp.srq;
  1512. /* Check that state is OK to post receive. */
  1513. if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
  1514. *bad_wr = wr;
  1515. return -EINVAL;
  1516. }
  1517. for (; wr; wr = wr->next) {
  1518. struct rvt_rwqe *wqe;
  1519. u32 next;
  1520. int i;
  1521. if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
  1522. *bad_wr = wr;
  1523. return -EINVAL;
  1524. }
  1525. spin_lock_irqsave(&qp->r_rq.lock, flags);
  1526. next = wq->head + 1;
  1527. if (next >= qp->r_rq.size)
  1528. next = 0;
  1529. if (next == wq->tail) {
  1530. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1531. *bad_wr = wr;
  1532. return -ENOMEM;
  1533. }
  1534. if (unlikely(qp_err_flush)) {
  1535. struct ib_wc wc;
  1536. memset(&wc, 0, sizeof(wc));
  1537. wc.qp = &qp->ibqp;
  1538. wc.opcode = IB_WC_RECV;
  1539. wc.wr_id = wr->wr_id;
  1540. wc.status = IB_WC_WR_FLUSH_ERR;
  1541. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1542. } else {
  1543. wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
  1544. wqe->wr_id = wr->wr_id;
  1545. wqe->num_sge = wr->num_sge;
  1546. for (i = 0; i < wr->num_sge; i++)
  1547. wqe->sg_list[i] = wr->sg_list[i];
  1548. /*
  1549. * Make sure queue entry is written
  1550. * before the head index.
  1551. */
  1552. smp_wmb();
  1553. wq->head = next;
  1554. }
  1555. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1556. }
  1557. return 0;
  1558. }
  1559. /**
  1560. * rvt_qp_valid_operation - validate post send wr request
  1561. * @qp - the qp
  1562. * @post-parms - the post send table for the driver
  1563. * @wr - the work request
  1564. *
  1565. * The routine validates the operation based on the
  1566. * validation table an returns the length of the operation
  1567. * which can extend beyond the ib_send_bw. Operation
  1568. * dependent flags key atomic operation validation.
  1569. *
  1570. * There is an exception for UD qps that validates the pd and
  1571. * overrides the length to include the additional UD specific
  1572. * length.
  1573. *
  1574. * Returns a negative error or the length of the work request
  1575. * for building the swqe.
  1576. */
  1577. static inline int rvt_qp_valid_operation(
  1578. struct rvt_qp *qp,
  1579. const struct rvt_operation_params *post_parms,
  1580. const struct ib_send_wr *wr)
  1581. {
  1582. int len;
  1583. if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
  1584. return -EINVAL;
  1585. if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
  1586. return -EINVAL;
  1587. if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
  1588. ibpd_to_rvtpd(qp->ibqp.pd)->user)
  1589. return -EINVAL;
  1590. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
  1591. (wr->num_sge == 0 ||
  1592. wr->sg_list[0].length < sizeof(u64) ||
  1593. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  1594. return -EINVAL;
  1595. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
  1596. !qp->s_max_rd_atomic)
  1597. return -EINVAL;
  1598. len = post_parms[wr->opcode].length;
  1599. /* UD specific */
  1600. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1601. qp->ibqp.qp_type != IB_QPT_RC) {
  1602. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1603. return -EINVAL;
  1604. len = sizeof(struct ib_ud_wr);
  1605. }
  1606. return len;
  1607. }
  1608. /**
  1609. * rvt_qp_is_avail - determine queue capacity
  1610. * @qp: the qp
  1611. * @rdi: the rdmavt device
  1612. * @reserved_op: is reserved operation
  1613. *
  1614. * This assumes the s_hlock is held but the s_last
  1615. * qp variable is uncontrolled.
  1616. *
  1617. * For non reserved operations, the qp->s_avail
  1618. * may be changed.
  1619. *
  1620. * The return value is zero or a -ENOMEM.
  1621. */
  1622. static inline int rvt_qp_is_avail(
  1623. struct rvt_qp *qp,
  1624. struct rvt_dev_info *rdi,
  1625. bool reserved_op)
  1626. {
  1627. u32 slast;
  1628. u32 avail;
  1629. u32 reserved_used;
  1630. /* see rvt_qp_wqe_unreserve() */
  1631. smp_mb__before_atomic();
  1632. reserved_used = atomic_read(&qp->s_reserved_used);
  1633. if (unlikely(reserved_op)) {
  1634. /* see rvt_qp_wqe_unreserve() */
  1635. smp_mb__before_atomic();
  1636. if (reserved_used >= rdi->dparms.reserved_operations)
  1637. return -ENOMEM;
  1638. return 0;
  1639. }
  1640. /* non-reserved operations */
  1641. if (likely(qp->s_avail))
  1642. return 0;
  1643. slast = READ_ONCE(qp->s_last);
  1644. if (qp->s_head >= slast)
  1645. avail = qp->s_size - (qp->s_head - slast);
  1646. else
  1647. avail = slast - qp->s_head;
  1648. /* see rvt_qp_wqe_unreserve() */
  1649. smp_mb__before_atomic();
  1650. reserved_used = atomic_read(&qp->s_reserved_used);
  1651. avail = avail - 1 -
  1652. (rdi->dparms.reserved_operations - reserved_used);
  1653. /* insure we don't assign a negative s_avail */
  1654. if ((s32)avail <= 0)
  1655. return -ENOMEM;
  1656. qp->s_avail = avail;
  1657. if (WARN_ON(qp->s_avail >
  1658. (qp->s_size - 1 - rdi->dparms.reserved_operations)))
  1659. rvt_pr_err(rdi,
  1660. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1661. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1662. qp->s_head, qp->s_tail, qp->s_cur,
  1663. qp->s_acked, qp->s_last);
  1664. return 0;
  1665. }
  1666. /**
  1667. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1668. * @qp: the QP to post on
  1669. * @wr: the work request to send
  1670. */
  1671. static int rvt_post_one_wr(struct rvt_qp *qp,
  1672. const struct ib_send_wr *wr,
  1673. bool *call_send)
  1674. {
  1675. struct rvt_swqe *wqe;
  1676. u32 next;
  1677. int i;
  1678. int j;
  1679. int acc;
  1680. struct rvt_lkey_table *rkt;
  1681. struct rvt_pd *pd;
  1682. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1683. u8 log_pmtu;
  1684. int ret;
  1685. size_t cplen;
  1686. bool reserved_op;
  1687. int local_ops_delayed = 0;
  1688. BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
  1689. /* IB spec says that num_sge == 0 is OK. */
  1690. if (unlikely(wr->num_sge > qp->s_max_sge))
  1691. return -EINVAL;
  1692. ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
  1693. if (ret < 0)
  1694. return ret;
  1695. cplen = ret;
  1696. /*
  1697. * Local operations include fast register and local invalidate.
  1698. * Fast register needs to be processed immediately because the
  1699. * registered lkey may be used by following work requests and the
  1700. * lkey needs to be valid at the time those requests are posted.
  1701. * Local invalidate can be processed immediately if fencing is
  1702. * not required and no previous local invalidate ops are pending.
  1703. * Signaled local operations that have been processed immediately
  1704. * need to have requests with "completion only" flags set posted
  1705. * to the send queue in order to generate completions.
  1706. */
  1707. if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
  1708. switch (wr->opcode) {
  1709. case IB_WR_REG_MR:
  1710. ret = rvt_fast_reg_mr(qp,
  1711. reg_wr(wr)->mr,
  1712. reg_wr(wr)->key,
  1713. reg_wr(wr)->access);
  1714. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1715. return ret;
  1716. break;
  1717. case IB_WR_LOCAL_INV:
  1718. if ((wr->send_flags & IB_SEND_FENCE) ||
  1719. atomic_read(&qp->local_ops_pending)) {
  1720. local_ops_delayed = 1;
  1721. } else {
  1722. ret = rvt_invalidate_rkey(
  1723. qp, wr->ex.invalidate_rkey);
  1724. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1725. return ret;
  1726. }
  1727. break;
  1728. default:
  1729. return -EINVAL;
  1730. }
  1731. }
  1732. reserved_op = rdi->post_parms[wr->opcode].flags &
  1733. RVT_OPERATION_USE_RESERVE;
  1734. /* check for avail */
  1735. ret = rvt_qp_is_avail(qp, rdi, reserved_op);
  1736. if (ret)
  1737. return ret;
  1738. next = qp->s_head + 1;
  1739. if (next >= qp->s_size)
  1740. next = 0;
  1741. rkt = &rdi->lkey_table;
  1742. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1743. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1744. /* cplen has length from above */
  1745. memcpy(&wqe->wr, wr, cplen);
  1746. wqe->length = 0;
  1747. j = 0;
  1748. if (wr->num_sge) {
  1749. struct rvt_sge *last_sge = NULL;
  1750. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1751. IB_ACCESS_LOCAL_WRITE : 0;
  1752. for (i = 0; i < wr->num_sge; i++) {
  1753. u32 length = wr->sg_list[i].length;
  1754. if (length == 0)
  1755. continue;
  1756. ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
  1757. &wr->sg_list[i], acc);
  1758. if (unlikely(ret < 0))
  1759. goto bail_inval_free;
  1760. wqe->length += length;
  1761. if (ret)
  1762. last_sge = &wqe->sg_list[j];
  1763. j += ret;
  1764. }
  1765. wqe->wr.num_sge = j;
  1766. }
  1767. /*
  1768. * Calculate and set SWQE PSN values prior to handing it off
  1769. * to the driver's check routine. This give the driver the
  1770. * opportunity to adjust PSN values based on internal checks.
  1771. */
  1772. log_pmtu = qp->log_pmtu;
  1773. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1774. qp->ibqp.qp_type != IB_QPT_RC) {
  1775. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1776. log_pmtu = ah->log_pmtu;
  1777. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1778. }
  1779. if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
  1780. if (local_ops_delayed)
  1781. atomic_inc(&qp->local_ops_pending);
  1782. else
  1783. wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
  1784. wqe->ssn = 0;
  1785. wqe->psn = 0;
  1786. wqe->lpsn = 0;
  1787. } else {
  1788. wqe->ssn = qp->s_ssn++;
  1789. wqe->psn = qp->s_next_psn;
  1790. wqe->lpsn = wqe->psn +
  1791. (wqe->length ?
  1792. ((wqe->length - 1) >> log_pmtu) :
  1793. 0);
  1794. }
  1795. /* general part of wqe valid - allow for driver checks */
  1796. if (rdi->driver_f.setup_wqe) {
  1797. ret = rdi->driver_f.setup_wqe(qp, wqe, call_send);
  1798. if (ret < 0)
  1799. goto bail_inval_free_ref;
  1800. }
  1801. if (!(rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL))
  1802. qp->s_next_psn = wqe->lpsn + 1;
  1803. if (unlikely(reserved_op)) {
  1804. wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
  1805. rvt_qp_wqe_reserve(qp, wqe);
  1806. } else {
  1807. wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
  1808. qp->s_avail--;
  1809. }
  1810. trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
  1811. smp_wmb(); /* see request builders */
  1812. qp->s_head = next;
  1813. return 0;
  1814. bail_inval_free_ref:
  1815. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1816. qp->ibqp.qp_type != IB_QPT_RC)
  1817. atomic_dec(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1818. bail_inval_free:
  1819. /* release mr holds */
  1820. while (j) {
  1821. struct rvt_sge *sge = &wqe->sg_list[--j];
  1822. rvt_put_mr(sge->mr);
  1823. }
  1824. return ret;
  1825. }
  1826. /**
  1827. * rvt_post_send - post a send on a QP
  1828. * @ibqp: the QP to post the send on
  1829. * @wr: the list of work requests to post
  1830. * @bad_wr: the first bad WR is put here
  1831. *
  1832. * This may be called from interrupt context.
  1833. *
  1834. * Return: 0 on success else errno
  1835. */
  1836. int rvt_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  1837. const struct ib_send_wr **bad_wr)
  1838. {
  1839. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1840. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1841. unsigned long flags = 0;
  1842. bool call_send;
  1843. unsigned nreq = 0;
  1844. int err = 0;
  1845. spin_lock_irqsave(&qp->s_hlock, flags);
  1846. /*
  1847. * Ensure QP state is such that we can send. If not bail out early,
  1848. * there is no need to do this every time we post a send.
  1849. */
  1850. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1851. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1852. return -EINVAL;
  1853. }
  1854. /*
  1855. * If the send queue is empty, and we only have a single WR then just go
  1856. * ahead and kick the send engine into gear. Otherwise we will always
  1857. * just schedule the send to happen later.
  1858. */
  1859. call_send = qp->s_head == READ_ONCE(qp->s_last) && !wr->next;
  1860. for (; wr; wr = wr->next) {
  1861. err = rvt_post_one_wr(qp, wr, &call_send);
  1862. if (unlikely(err)) {
  1863. *bad_wr = wr;
  1864. goto bail;
  1865. }
  1866. nreq++;
  1867. }
  1868. bail:
  1869. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1870. if (nreq) {
  1871. /*
  1872. * Only call do_send if there is exactly one packet, and the
  1873. * driver said it was ok.
  1874. */
  1875. if (nreq == 1 && call_send)
  1876. rdi->driver_f.do_send(qp);
  1877. else
  1878. rdi->driver_f.schedule_send_no_lock(qp);
  1879. }
  1880. return err;
  1881. }
  1882. /**
  1883. * rvt_post_srq_receive - post a receive on a shared receive queue
  1884. * @ibsrq: the SRQ to post the receive on
  1885. * @wr: the list of work requests to post
  1886. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1887. *
  1888. * This may be called from interrupt context.
  1889. *
  1890. * Return: 0 on success else errno
  1891. */
  1892. int rvt_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
  1893. const struct ib_recv_wr **bad_wr)
  1894. {
  1895. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1896. struct rvt_rwq *wq;
  1897. unsigned long flags;
  1898. for (; wr; wr = wr->next) {
  1899. struct rvt_rwqe *wqe;
  1900. u32 next;
  1901. int i;
  1902. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1903. *bad_wr = wr;
  1904. return -EINVAL;
  1905. }
  1906. spin_lock_irqsave(&srq->rq.lock, flags);
  1907. wq = srq->rq.wq;
  1908. next = wq->head + 1;
  1909. if (next >= srq->rq.size)
  1910. next = 0;
  1911. if (next == wq->tail) {
  1912. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1913. *bad_wr = wr;
  1914. return -ENOMEM;
  1915. }
  1916. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1917. wqe->wr_id = wr->wr_id;
  1918. wqe->num_sge = wr->num_sge;
  1919. for (i = 0; i < wr->num_sge; i++)
  1920. wqe->sg_list[i] = wr->sg_list[i];
  1921. /* Make sure queue entry is written before the head index. */
  1922. smp_wmb();
  1923. wq->head = next;
  1924. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1925. }
  1926. return 0;
  1927. }
  1928. /*
  1929. * Validate a RWQE and fill in the SGE state.
  1930. * Return 1 if OK.
  1931. */
  1932. static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
  1933. {
  1934. int i, j, ret;
  1935. struct ib_wc wc;
  1936. struct rvt_lkey_table *rkt;
  1937. struct rvt_pd *pd;
  1938. struct rvt_sge_state *ss;
  1939. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1940. rkt = &rdi->lkey_table;
  1941. pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
  1942. ss = &qp->r_sge;
  1943. ss->sg_list = qp->r_sg_list;
  1944. qp->r_len = 0;
  1945. for (i = j = 0; i < wqe->num_sge; i++) {
  1946. if (wqe->sg_list[i].length == 0)
  1947. continue;
  1948. /* Check LKEY */
  1949. ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
  1950. NULL, &wqe->sg_list[i],
  1951. IB_ACCESS_LOCAL_WRITE);
  1952. if (unlikely(ret <= 0))
  1953. goto bad_lkey;
  1954. qp->r_len += wqe->sg_list[i].length;
  1955. j++;
  1956. }
  1957. ss->num_sge = j;
  1958. ss->total_len = qp->r_len;
  1959. return 1;
  1960. bad_lkey:
  1961. while (j) {
  1962. struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
  1963. rvt_put_mr(sge->mr);
  1964. }
  1965. ss->num_sge = 0;
  1966. memset(&wc, 0, sizeof(wc));
  1967. wc.wr_id = wqe->wr_id;
  1968. wc.status = IB_WC_LOC_PROT_ERR;
  1969. wc.opcode = IB_WC_RECV;
  1970. wc.qp = &qp->ibqp;
  1971. /* Signal solicited completion event. */
  1972. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1973. return 0;
  1974. }
  1975. /**
  1976. * rvt_get_rwqe - copy the next RWQE into the QP's RWQE
  1977. * @qp: the QP
  1978. * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
  1979. *
  1980. * Return -1 if there is a local error, 0 if no RWQE is available,
  1981. * otherwise return 1.
  1982. *
  1983. * Can be called from interrupt level.
  1984. */
  1985. int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only)
  1986. {
  1987. unsigned long flags;
  1988. struct rvt_rq *rq;
  1989. struct rvt_rwq *wq;
  1990. struct rvt_srq *srq;
  1991. struct rvt_rwqe *wqe;
  1992. void (*handler)(struct ib_event *, void *);
  1993. u32 tail;
  1994. int ret;
  1995. if (qp->ibqp.srq) {
  1996. srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
  1997. handler = srq->ibsrq.event_handler;
  1998. rq = &srq->rq;
  1999. } else {
  2000. srq = NULL;
  2001. handler = NULL;
  2002. rq = &qp->r_rq;
  2003. }
  2004. spin_lock_irqsave(&rq->lock, flags);
  2005. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
  2006. ret = 0;
  2007. goto unlock;
  2008. }
  2009. wq = rq->wq;
  2010. tail = wq->tail;
  2011. /* Validate tail before using it since it is user writable. */
  2012. if (tail >= rq->size)
  2013. tail = 0;
  2014. if (unlikely(tail == wq->head)) {
  2015. ret = 0;
  2016. goto unlock;
  2017. }
  2018. /* Make sure entry is read after head index is read. */
  2019. smp_rmb();
  2020. wqe = rvt_get_rwqe_ptr(rq, tail);
  2021. /*
  2022. * Even though we update the tail index in memory, the verbs
  2023. * consumer is not supposed to post more entries until a
  2024. * completion is generated.
  2025. */
  2026. if (++tail >= rq->size)
  2027. tail = 0;
  2028. wq->tail = tail;
  2029. if (!wr_id_only && !init_sge(qp, wqe)) {
  2030. ret = -1;
  2031. goto unlock;
  2032. }
  2033. qp->r_wr_id = wqe->wr_id;
  2034. ret = 1;
  2035. set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
  2036. if (handler) {
  2037. u32 n;
  2038. /*
  2039. * Validate head pointer value and compute
  2040. * the number of remaining WQEs.
  2041. */
  2042. n = wq->head;
  2043. if (n >= rq->size)
  2044. n = 0;
  2045. if (n < tail)
  2046. n += rq->size - tail;
  2047. else
  2048. n -= tail;
  2049. if (n < srq->limit) {
  2050. struct ib_event ev;
  2051. srq->limit = 0;
  2052. spin_unlock_irqrestore(&rq->lock, flags);
  2053. ev.device = qp->ibqp.device;
  2054. ev.element.srq = qp->ibqp.srq;
  2055. ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
  2056. handler(&ev, srq->ibsrq.srq_context);
  2057. goto bail;
  2058. }
  2059. }
  2060. unlock:
  2061. spin_unlock_irqrestore(&rq->lock, flags);
  2062. bail:
  2063. return ret;
  2064. }
  2065. EXPORT_SYMBOL(rvt_get_rwqe);
  2066. /**
  2067. * qp_comm_est - handle trap with QP established
  2068. * @qp: the QP
  2069. */
  2070. void rvt_comm_est(struct rvt_qp *qp)
  2071. {
  2072. qp->r_flags |= RVT_R_COMM_EST;
  2073. if (qp->ibqp.event_handler) {
  2074. struct ib_event ev;
  2075. ev.device = qp->ibqp.device;
  2076. ev.element.qp = &qp->ibqp;
  2077. ev.event = IB_EVENT_COMM_EST;
  2078. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  2079. }
  2080. }
  2081. EXPORT_SYMBOL(rvt_comm_est);
  2082. void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
  2083. {
  2084. unsigned long flags;
  2085. int lastwqe;
  2086. spin_lock_irqsave(&qp->s_lock, flags);
  2087. lastwqe = rvt_error_qp(qp, err);
  2088. spin_unlock_irqrestore(&qp->s_lock, flags);
  2089. if (lastwqe) {
  2090. struct ib_event ev;
  2091. ev.device = qp->ibqp.device;
  2092. ev.element.qp = &qp->ibqp;
  2093. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  2094. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  2095. }
  2096. }
  2097. EXPORT_SYMBOL(rvt_rc_error);
  2098. /*
  2099. * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
  2100. * @index - the index
  2101. * return usec from an index into ib_rvt_rnr_table
  2102. */
  2103. unsigned long rvt_rnr_tbl_to_usec(u32 index)
  2104. {
  2105. return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
  2106. }
  2107. EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
  2108. static inline unsigned long rvt_aeth_to_usec(u32 aeth)
  2109. {
  2110. return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
  2111. IB_AETH_CREDIT_MASK];
  2112. }
  2113. /*
  2114. * rvt_add_retry_timer - add/start a retry timer
  2115. * @qp - the QP
  2116. * add a retry timer on the QP
  2117. */
  2118. void rvt_add_retry_timer(struct rvt_qp *qp)
  2119. {
  2120. struct ib_qp *ibqp = &qp->ibqp;
  2121. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  2122. lockdep_assert_held(&qp->s_lock);
  2123. qp->s_flags |= RVT_S_TIMER;
  2124. /* 4.096 usec. * (1 << qp->timeout) */
  2125. qp->s_timer.expires = jiffies + qp->timeout_jiffies +
  2126. rdi->busy_jiffies;
  2127. add_timer(&qp->s_timer);
  2128. }
  2129. EXPORT_SYMBOL(rvt_add_retry_timer);
  2130. /**
  2131. * rvt_add_rnr_timer - add/start an rnr timer
  2132. * @qp - the QP
  2133. * @aeth - aeth of RNR timeout, simulated aeth for loopback
  2134. * add an rnr timer on the QP
  2135. */
  2136. void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
  2137. {
  2138. u32 to;
  2139. lockdep_assert_held(&qp->s_lock);
  2140. qp->s_flags |= RVT_S_WAIT_RNR;
  2141. to = rvt_aeth_to_usec(aeth);
  2142. trace_rvt_rnrnak_add(qp, to);
  2143. hrtimer_start(&qp->s_rnr_timer,
  2144. ns_to_ktime(1000 * to), HRTIMER_MODE_REL_PINNED);
  2145. }
  2146. EXPORT_SYMBOL(rvt_add_rnr_timer);
  2147. /**
  2148. * rvt_stop_rc_timers - stop all timers
  2149. * @qp - the QP
  2150. * stop any pending timers
  2151. */
  2152. void rvt_stop_rc_timers(struct rvt_qp *qp)
  2153. {
  2154. lockdep_assert_held(&qp->s_lock);
  2155. /* Remove QP from all timers */
  2156. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  2157. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  2158. del_timer(&qp->s_timer);
  2159. hrtimer_try_to_cancel(&qp->s_rnr_timer);
  2160. }
  2161. }
  2162. EXPORT_SYMBOL(rvt_stop_rc_timers);
  2163. /**
  2164. * rvt_stop_rnr_timer - stop an rnr timer
  2165. * @qp - the QP
  2166. *
  2167. * stop an rnr timer and return if the timer
  2168. * had been pending.
  2169. */
  2170. static void rvt_stop_rnr_timer(struct rvt_qp *qp)
  2171. {
  2172. lockdep_assert_held(&qp->s_lock);
  2173. /* Remove QP from rnr timer */
  2174. if (qp->s_flags & RVT_S_WAIT_RNR) {
  2175. qp->s_flags &= ~RVT_S_WAIT_RNR;
  2176. trace_rvt_rnrnak_stop(qp, 0);
  2177. }
  2178. }
  2179. /**
  2180. * rvt_del_timers_sync - wait for any timeout routines to exit
  2181. * @qp - the QP
  2182. */
  2183. void rvt_del_timers_sync(struct rvt_qp *qp)
  2184. {
  2185. del_timer_sync(&qp->s_timer);
  2186. hrtimer_cancel(&qp->s_rnr_timer);
  2187. }
  2188. EXPORT_SYMBOL(rvt_del_timers_sync);
  2189. /**
  2190. * This is called from s_timer for missing responses.
  2191. */
  2192. static void rvt_rc_timeout(struct timer_list *t)
  2193. {
  2194. struct rvt_qp *qp = from_timer(qp, t, s_timer);
  2195. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  2196. unsigned long flags;
  2197. spin_lock_irqsave(&qp->r_lock, flags);
  2198. spin_lock(&qp->s_lock);
  2199. if (qp->s_flags & RVT_S_TIMER) {
  2200. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  2201. qp->s_flags &= ~RVT_S_TIMER;
  2202. rvp->n_rc_timeouts++;
  2203. del_timer(&qp->s_timer);
  2204. trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
  2205. if (rdi->driver_f.notify_restart_rc)
  2206. rdi->driver_f.notify_restart_rc(qp,
  2207. qp->s_last_psn + 1,
  2208. 1);
  2209. rdi->driver_f.schedule_send(qp);
  2210. }
  2211. spin_unlock(&qp->s_lock);
  2212. spin_unlock_irqrestore(&qp->r_lock, flags);
  2213. }
  2214. /*
  2215. * This is called from s_timer for RNR timeouts.
  2216. */
  2217. enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
  2218. {
  2219. struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
  2220. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  2221. unsigned long flags;
  2222. spin_lock_irqsave(&qp->s_lock, flags);
  2223. rvt_stop_rnr_timer(qp);
  2224. trace_rvt_rnrnak_timeout(qp, 0);
  2225. rdi->driver_f.schedule_send(qp);
  2226. spin_unlock_irqrestore(&qp->s_lock, flags);
  2227. return HRTIMER_NORESTART;
  2228. }
  2229. EXPORT_SYMBOL(rvt_rc_rnr_retry);
  2230. /**
  2231. * rvt_qp_iter_init - initial for QP iteration
  2232. * @rdi: rvt devinfo
  2233. * @v: u64 value
  2234. *
  2235. * This returns an iterator suitable for iterating QPs
  2236. * in the system.
  2237. *
  2238. * The @cb is a user defined callback and @v is a 64
  2239. * bit value passed to and relevant for processing in the
  2240. * @cb. An example use case would be to alter QP processing
  2241. * based on criteria not part of the rvt_qp.
  2242. *
  2243. * Use cases that require memory allocation to succeed
  2244. * must preallocate appropriately.
  2245. *
  2246. * Return: a pointer to an rvt_qp_iter or NULL
  2247. */
  2248. struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
  2249. u64 v,
  2250. void (*cb)(struct rvt_qp *qp, u64 v))
  2251. {
  2252. struct rvt_qp_iter *i;
  2253. i = kzalloc(sizeof(*i), GFP_KERNEL);
  2254. if (!i)
  2255. return NULL;
  2256. i->rdi = rdi;
  2257. /* number of special QPs (SMI/GSI) for device */
  2258. i->specials = rdi->ibdev.phys_port_cnt * 2;
  2259. i->v = v;
  2260. i->cb = cb;
  2261. return i;
  2262. }
  2263. EXPORT_SYMBOL(rvt_qp_iter_init);
  2264. /**
  2265. * rvt_qp_iter_next - return the next QP in iter
  2266. * @iter - the iterator
  2267. *
  2268. * Fine grained QP iterator suitable for use
  2269. * with debugfs seq_file mechanisms.
  2270. *
  2271. * Updates iter->qp with the current QP when the return
  2272. * value is 0.
  2273. *
  2274. * Return: 0 - iter->qp is valid 1 - no more QPs
  2275. */
  2276. int rvt_qp_iter_next(struct rvt_qp_iter *iter)
  2277. __must_hold(RCU)
  2278. {
  2279. int n = iter->n;
  2280. int ret = 1;
  2281. struct rvt_qp *pqp = iter->qp;
  2282. struct rvt_qp *qp;
  2283. struct rvt_dev_info *rdi = iter->rdi;
  2284. /*
  2285. * The approach is to consider the special qps
  2286. * as additional table entries before the
  2287. * real hash table. Since the qp code sets
  2288. * the qp->next hash link to NULL, this works just fine.
  2289. *
  2290. * iter->specials is 2 * # ports
  2291. *
  2292. * n = 0..iter->specials is the special qp indices
  2293. *
  2294. * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
  2295. * the potential hash bucket entries
  2296. *
  2297. */
  2298. for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
  2299. if (pqp) {
  2300. qp = rcu_dereference(pqp->next);
  2301. } else {
  2302. if (n < iter->specials) {
  2303. struct rvt_ibport *rvp;
  2304. int pidx;
  2305. pidx = n % rdi->ibdev.phys_port_cnt;
  2306. rvp = rdi->ports[pidx];
  2307. qp = rcu_dereference(rvp->qp[n & 1]);
  2308. } else {
  2309. qp = rcu_dereference(
  2310. rdi->qp_dev->qp_table[
  2311. (n - iter->specials)]);
  2312. }
  2313. }
  2314. pqp = qp;
  2315. if (qp) {
  2316. iter->qp = qp;
  2317. iter->n = n;
  2318. return 0;
  2319. }
  2320. }
  2321. return ret;
  2322. }
  2323. EXPORT_SYMBOL(rvt_qp_iter_next);
  2324. /**
  2325. * rvt_qp_iter - iterate all QPs
  2326. * @rdi - rvt devinfo
  2327. * @v - a 64 bit value
  2328. * @cb - a callback
  2329. *
  2330. * This provides a way for iterating all QPs.
  2331. *
  2332. * The @cb is a user defined callback and @v is a 64
  2333. * bit value passed to and relevant for processing in the
  2334. * cb. An example use case would be to alter QP processing
  2335. * based on criteria not part of the rvt_qp.
  2336. *
  2337. * The code has an internal iterator to simplify
  2338. * non seq_file use cases.
  2339. */
  2340. void rvt_qp_iter(struct rvt_dev_info *rdi,
  2341. u64 v,
  2342. void (*cb)(struct rvt_qp *qp, u64 v))
  2343. {
  2344. int ret;
  2345. struct rvt_qp_iter i = {
  2346. .rdi = rdi,
  2347. .specials = rdi->ibdev.phys_port_cnt * 2,
  2348. .v = v,
  2349. .cb = cb
  2350. };
  2351. rcu_read_lock();
  2352. do {
  2353. ret = rvt_qp_iter_next(&i);
  2354. if (!ret) {
  2355. rvt_get_qp(i.qp);
  2356. rcu_read_unlock();
  2357. i.cb(i.qp, i.v);
  2358. rcu_read_lock();
  2359. rvt_put_qp(i.qp);
  2360. }
  2361. } while (!ret);
  2362. rcu_read_unlock();
  2363. }
  2364. EXPORT_SYMBOL(rvt_qp_iter);
  2365. /*
  2366. * This should be called with s_lock held.
  2367. */
  2368. void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
  2369. enum ib_wc_status status)
  2370. {
  2371. u32 old_last, last;
  2372. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  2373. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  2374. return;
  2375. last = qp->s_last;
  2376. old_last = last;
  2377. trace_rvt_qp_send_completion(qp, wqe, last);
  2378. if (++last >= qp->s_size)
  2379. last = 0;
  2380. trace_rvt_qp_send_completion(qp, wqe, last);
  2381. qp->s_last = last;
  2382. /* See post_send() */
  2383. barrier();
  2384. rvt_put_swqe(wqe);
  2385. if (qp->ibqp.qp_type == IB_QPT_UD ||
  2386. qp->ibqp.qp_type == IB_QPT_SMI ||
  2387. qp->ibqp.qp_type == IB_QPT_GSI)
  2388. atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
  2389. rvt_qp_swqe_complete(qp,
  2390. wqe,
  2391. rdi->wc_opcode[wqe->wr.opcode],
  2392. status);
  2393. if (qp->s_acked == old_last)
  2394. qp->s_acked = last;
  2395. if (qp->s_cur == old_last)
  2396. qp->s_cur = last;
  2397. if (qp->s_tail == old_last)
  2398. qp->s_tail = last;
  2399. if (qp->state == IB_QPS_SQD && last == qp->s_cur)
  2400. qp->s_draining = 0;
  2401. }
  2402. EXPORT_SYMBOL(rvt_send_complete);
  2403. /**
  2404. * rvt_copy_sge - copy data to SGE memory
  2405. * @qp: associated QP
  2406. * @ss: the SGE state
  2407. * @data: the data to copy
  2408. * @length: the length of the data
  2409. * @release: boolean to release MR
  2410. * @copy_last: do a separate copy of the last 8 bytes
  2411. */
  2412. void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,
  2413. void *data, u32 length,
  2414. bool release, bool copy_last)
  2415. {
  2416. struct rvt_sge *sge = &ss->sge;
  2417. int i;
  2418. bool in_last = false;
  2419. bool cacheless_copy = false;
  2420. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  2421. struct rvt_wss *wss = rdi->wss;
  2422. unsigned int sge_copy_mode = rdi->dparms.sge_copy_mode;
  2423. if (sge_copy_mode == RVT_SGE_COPY_CACHELESS) {
  2424. cacheless_copy = length >= PAGE_SIZE;
  2425. } else if (sge_copy_mode == RVT_SGE_COPY_ADAPTIVE) {
  2426. if (length >= PAGE_SIZE) {
  2427. /*
  2428. * NOTE: this *assumes*:
  2429. * o The first vaddr is the dest.
  2430. * o If multiple pages, then vaddr is sequential.
  2431. */
  2432. wss_insert(wss, sge->vaddr);
  2433. if (length >= (2 * PAGE_SIZE))
  2434. wss_insert(wss, (sge->vaddr + PAGE_SIZE));
  2435. cacheless_copy = wss_exceeds_threshold(wss);
  2436. } else {
  2437. wss_advance_clean_counter(wss);
  2438. }
  2439. }
  2440. if (copy_last) {
  2441. if (length > 8) {
  2442. length -= 8;
  2443. } else {
  2444. copy_last = false;
  2445. in_last = true;
  2446. }
  2447. }
  2448. again:
  2449. while (length) {
  2450. u32 len = rvt_get_sge_length(sge, length);
  2451. WARN_ON_ONCE(len == 0);
  2452. if (unlikely(in_last)) {
  2453. /* enforce byte transfer ordering */
  2454. for (i = 0; i < len; i++)
  2455. ((u8 *)sge->vaddr)[i] = ((u8 *)data)[i];
  2456. } else if (cacheless_copy) {
  2457. cacheless_memcpy(sge->vaddr, data, len);
  2458. } else {
  2459. memcpy(sge->vaddr, data, len);
  2460. }
  2461. rvt_update_sge(ss, len, release);
  2462. data += len;
  2463. length -= len;
  2464. }
  2465. if (copy_last) {
  2466. copy_last = false;
  2467. in_last = true;
  2468. length = 8;
  2469. goto again;
  2470. }
  2471. }
  2472. EXPORT_SYMBOL(rvt_copy_sge);
  2473. /**
  2474. * ruc_loopback - handle UC and RC loopback requests
  2475. * @sqp: the sending QP
  2476. *
  2477. * This is called from rvt_do_send() to forward a WQE addressed to the same HFI
  2478. * Note that although we are single threaded due to the send engine, we still
  2479. * have to protect against post_send(). We don't have to worry about
  2480. * receive interrupts since this is a connected protocol and all packets
  2481. * will pass through here.
  2482. */
  2483. void rvt_ruc_loopback(struct rvt_qp *sqp)
  2484. {
  2485. struct rvt_ibport *rvp = NULL;
  2486. struct rvt_dev_info *rdi = ib_to_rvt(sqp->ibqp.device);
  2487. struct rvt_qp *qp;
  2488. struct rvt_swqe *wqe;
  2489. struct rvt_sge *sge;
  2490. unsigned long flags;
  2491. struct ib_wc wc;
  2492. u64 sdata;
  2493. atomic64_t *maddr;
  2494. enum ib_wc_status send_status;
  2495. bool release;
  2496. int ret;
  2497. bool copy_last = false;
  2498. int local_ops = 0;
  2499. rcu_read_lock();
  2500. rvp = rdi->ports[sqp->port_num - 1];
  2501. /*
  2502. * Note that we check the responder QP state after
  2503. * checking the requester's state.
  2504. */
  2505. qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), rvp,
  2506. sqp->remote_qpn);
  2507. spin_lock_irqsave(&sqp->s_lock, flags);
  2508. /* Return if we are already busy processing a work request. */
  2509. if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
  2510. !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
  2511. goto unlock;
  2512. sqp->s_flags |= RVT_S_BUSY;
  2513. again:
  2514. if (sqp->s_last == READ_ONCE(sqp->s_head))
  2515. goto clr_busy;
  2516. wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
  2517. /* Return if it is not OK to start a new work request. */
  2518. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
  2519. if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
  2520. goto clr_busy;
  2521. /* We are in the error state, flush the work request. */
  2522. send_status = IB_WC_WR_FLUSH_ERR;
  2523. goto flush_send;
  2524. }
  2525. /*
  2526. * We can rely on the entry not changing without the s_lock
  2527. * being held until we update s_last.
  2528. * We increment s_cur to indicate s_last is in progress.
  2529. */
  2530. if (sqp->s_last == sqp->s_cur) {
  2531. if (++sqp->s_cur >= sqp->s_size)
  2532. sqp->s_cur = 0;
  2533. }
  2534. spin_unlock_irqrestore(&sqp->s_lock, flags);
  2535. if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
  2536. qp->ibqp.qp_type != sqp->ibqp.qp_type) {
  2537. rvp->n_pkt_drops++;
  2538. /*
  2539. * For RC, the requester would timeout and retry so
  2540. * shortcut the timeouts and just signal too many retries.
  2541. */
  2542. if (sqp->ibqp.qp_type == IB_QPT_RC)
  2543. send_status = IB_WC_RETRY_EXC_ERR;
  2544. else
  2545. send_status = IB_WC_SUCCESS;
  2546. goto serr;
  2547. }
  2548. memset(&wc, 0, sizeof(wc));
  2549. send_status = IB_WC_SUCCESS;
  2550. release = true;
  2551. sqp->s_sge.sge = wqe->sg_list[0];
  2552. sqp->s_sge.sg_list = wqe->sg_list + 1;
  2553. sqp->s_sge.num_sge = wqe->wr.num_sge;
  2554. sqp->s_len = wqe->length;
  2555. switch (wqe->wr.opcode) {
  2556. case IB_WR_REG_MR:
  2557. goto send_comp;
  2558. case IB_WR_LOCAL_INV:
  2559. if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
  2560. if (rvt_invalidate_rkey(sqp,
  2561. wqe->wr.ex.invalidate_rkey))
  2562. send_status = IB_WC_LOC_PROT_ERR;
  2563. local_ops = 1;
  2564. }
  2565. goto send_comp;
  2566. case IB_WR_SEND_WITH_INV:
  2567. if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
  2568. wc.wc_flags = IB_WC_WITH_INVALIDATE;
  2569. wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
  2570. }
  2571. goto send;
  2572. case IB_WR_SEND_WITH_IMM:
  2573. wc.wc_flags = IB_WC_WITH_IMM;
  2574. wc.ex.imm_data = wqe->wr.ex.imm_data;
  2575. /* FALLTHROUGH */
  2576. case IB_WR_SEND:
  2577. send:
  2578. ret = rvt_get_rwqe(qp, false);
  2579. if (ret < 0)
  2580. goto op_err;
  2581. if (!ret)
  2582. goto rnr_nak;
  2583. break;
  2584. case IB_WR_RDMA_WRITE_WITH_IMM:
  2585. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  2586. goto inv_err;
  2587. wc.wc_flags = IB_WC_WITH_IMM;
  2588. wc.ex.imm_data = wqe->wr.ex.imm_data;
  2589. ret = rvt_get_rwqe(qp, true);
  2590. if (ret < 0)
  2591. goto op_err;
  2592. if (!ret)
  2593. goto rnr_nak;
  2594. /* skip copy_last set and qp_access_flags recheck */
  2595. goto do_write;
  2596. case IB_WR_RDMA_WRITE:
  2597. copy_last = rvt_is_user_qp(qp);
  2598. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  2599. goto inv_err;
  2600. do_write:
  2601. if (wqe->length == 0)
  2602. break;
  2603. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
  2604. wqe->rdma_wr.remote_addr,
  2605. wqe->rdma_wr.rkey,
  2606. IB_ACCESS_REMOTE_WRITE)))
  2607. goto acc_err;
  2608. qp->r_sge.sg_list = NULL;
  2609. qp->r_sge.num_sge = 1;
  2610. qp->r_sge.total_len = wqe->length;
  2611. break;
  2612. case IB_WR_RDMA_READ:
  2613. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
  2614. goto inv_err;
  2615. if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
  2616. wqe->rdma_wr.remote_addr,
  2617. wqe->rdma_wr.rkey,
  2618. IB_ACCESS_REMOTE_READ)))
  2619. goto acc_err;
  2620. release = false;
  2621. sqp->s_sge.sg_list = NULL;
  2622. sqp->s_sge.num_sge = 1;
  2623. qp->r_sge.sge = wqe->sg_list[0];
  2624. qp->r_sge.sg_list = wqe->sg_list + 1;
  2625. qp->r_sge.num_sge = wqe->wr.num_sge;
  2626. qp->r_sge.total_len = wqe->length;
  2627. break;
  2628. case IB_WR_ATOMIC_CMP_AND_SWP:
  2629. case IB_WR_ATOMIC_FETCH_AND_ADD:
  2630. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
  2631. goto inv_err;
  2632. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
  2633. wqe->atomic_wr.remote_addr,
  2634. wqe->atomic_wr.rkey,
  2635. IB_ACCESS_REMOTE_ATOMIC)))
  2636. goto acc_err;
  2637. /* Perform atomic OP and save result. */
  2638. maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
  2639. sdata = wqe->atomic_wr.compare_add;
  2640. *(u64 *)sqp->s_sge.sge.vaddr =
  2641. (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
  2642. (u64)atomic64_add_return(sdata, maddr) - sdata :
  2643. (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
  2644. sdata, wqe->atomic_wr.swap);
  2645. rvt_put_mr(qp->r_sge.sge.mr);
  2646. qp->r_sge.num_sge = 0;
  2647. goto send_comp;
  2648. default:
  2649. send_status = IB_WC_LOC_QP_OP_ERR;
  2650. goto serr;
  2651. }
  2652. sge = &sqp->s_sge.sge;
  2653. while (sqp->s_len) {
  2654. u32 len = sqp->s_len;
  2655. if (len > sge->length)
  2656. len = sge->length;
  2657. if (len > sge->sge_length)
  2658. len = sge->sge_length;
  2659. WARN_ON_ONCE(len == 0);
  2660. rvt_copy_sge(qp, &qp->r_sge, sge->vaddr,
  2661. len, release, copy_last);
  2662. sge->vaddr += len;
  2663. sge->length -= len;
  2664. sge->sge_length -= len;
  2665. if (sge->sge_length == 0) {
  2666. if (!release)
  2667. rvt_put_mr(sge->mr);
  2668. if (--sqp->s_sge.num_sge)
  2669. *sge = *sqp->s_sge.sg_list++;
  2670. } else if (sge->length == 0 && sge->mr->lkey) {
  2671. if (++sge->n >= RVT_SEGSZ) {
  2672. if (++sge->m >= sge->mr->mapsz)
  2673. break;
  2674. sge->n = 0;
  2675. }
  2676. sge->vaddr =
  2677. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  2678. sge->length =
  2679. sge->mr->map[sge->m]->segs[sge->n].length;
  2680. }
  2681. sqp->s_len -= len;
  2682. }
  2683. if (release)
  2684. rvt_put_ss(&qp->r_sge);
  2685. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  2686. goto send_comp;
  2687. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  2688. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  2689. else
  2690. wc.opcode = IB_WC_RECV;
  2691. wc.wr_id = qp->r_wr_id;
  2692. wc.status = IB_WC_SUCCESS;
  2693. wc.byte_len = wqe->length;
  2694. wc.qp = &qp->ibqp;
  2695. wc.src_qp = qp->remote_qpn;
  2696. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
  2697. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  2698. wc.port_num = 1;
  2699. /* Signal completion event if the solicited bit is set. */
  2700. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  2701. wqe->wr.send_flags & IB_SEND_SOLICITED);
  2702. send_comp:
  2703. spin_lock_irqsave(&sqp->s_lock, flags);
  2704. rvp->n_loop_pkts++;
  2705. flush_send:
  2706. sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
  2707. rvt_send_complete(sqp, wqe, send_status);
  2708. if (local_ops) {
  2709. atomic_dec(&sqp->local_ops_pending);
  2710. local_ops = 0;
  2711. }
  2712. goto again;
  2713. rnr_nak:
  2714. /* Handle RNR NAK */
  2715. if (qp->ibqp.qp_type == IB_QPT_UC)
  2716. goto send_comp;
  2717. rvp->n_rnr_naks++;
  2718. /*
  2719. * Note: we don't need the s_lock held since the BUSY flag
  2720. * makes this single threaded.
  2721. */
  2722. if (sqp->s_rnr_retry == 0) {
  2723. send_status = IB_WC_RNR_RETRY_EXC_ERR;
  2724. goto serr;
  2725. }
  2726. if (sqp->s_rnr_retry_cnt < 7)
  2727. sqp->s_rnr_retry--;
  2728. spin_lock_irqsave(&sqp->s_lock, flags);
  2729. if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
  2730. goto clr_busy;
  2731. rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
  2732. IB_AETH_CREDIT_SHIFT);
  2733. goto clr_busy;
  2734. op_err:
  2735. send_status = IB_WC_REM_OP_ERR;
  2736. wc.status = IB_WC_LOC_QP_OP_ERR;
  2737. goto err;
  2738. inv_err:
  2739. send_status = IB_WC_REM_INV_REQ_ERR;
  2740. wc.status = IB_WC_LOC_QP_OP_ERR;
  2741. goto err;
  2742. acc_err:
  2743. send_status = IB_WC_REM_ACCESS_ERR;
  2744. wc.status = IB_WC_LOC_PROT_ERR;
  2745. err:
  2746. /* responder goes to error state */
  2747. rvt_rc_error(qp, wc.status);
  2748. serr:
  2749. spin_lock_irqsave(&sqp->s_lock, flags);
  2750. rvt_send_complete(sqp, wqe, send_status);
  2751. if (sqp->ibqp.qp_type == IB_QPT_RC) {
  2752. int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
  2753. sqp->s_flags &= ~RVT_S_BUSY;
  2754. spin_unlock_irqrestore(&sqp->s_lock, flags);
  2755. if (lastwqe) {
  2756. struct ib_event ev;
  2757. ev.device = sqp->ibqp.device;
  2758. ev.element.qp = &sqp->ibqp;
  2759. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  2760. sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
  2761. }
  2762. goto done;
  2763. }
  2764. clr_busy:
  2765. sqp->s_flags &= ~RVT_S_BUSY;
  2766. unlock:
  2767. spin_unlock_irqrestore(&sqp->s_lock, flags);
  2768. done:
  2769. rcu_read_unlock();
  2770. }
  2771. EXPORT_SYMBOL(rvt_ruc_loopback);