qp.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  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. static void get_map_page(struct rvt_qpn_table *qpt,
  117. struct rvt_qpn_map *map)
  118. {
  119. unsigned long page = get_zeroed_page(GFP_KERNEL);
  120. /*
  121. * Free the page if someone raced with us installing it.
  122. */
  123. spin_lock(&qpt->lock);
  124. if (map->page)
  125. free_page(page);
  126. else
  127. map->page = (void *)page;
  128. spin_unlock(&qpt->lock);
  129. }
  130. /**
  131. * init_qpn_table - initialize the QP number table for a device
  132. * @qpt: the QPN table
  133. */
  134. static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
  135. {
  136. u32 offset, i;
  137. struct rvt_qpn_map *map;
  138. int ret = 0;
  139. if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
  140. return -EINVAL;
  141. spin_lock_init(&qpt->lock);
  142. qpt->last = rdi->dparms.qpn_start;
  143. qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
  144. /*
  145. * Drivers may want some QPs beyond what we need for verbs let them use
  146. * our qpn table. No need for two. Lets go ahead and mark the bitmaps
  147. * for those. The reserved range must be *after* the range which verbs
  148. * will pick from.
  149. */
  150. /* Figure out number of bit maps needed before reserved range */
  151. qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
  152. /* This should always be zero */
  153. offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
  154. /* Starting with the first reserved bit map */
  155. map = &qpt->map[qpt->nmaps];
  156. rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
  157. rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
  158. for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
  159. if (!map->page) {
  160. get_map_page(qpt, map);
  161. if (!map->page) {
  162. ret = -ENOMEM;
  163. break;
  164. }
  165. }
  166. set_bit(offset, map->page);
  167. offset++;
  168. if (offset == RVT_BITS_PER_PAGE) {
  169. /* next page */
  170. qpt->nmaps++;
  171. map++;
  172. offset = 0;
  173. }
  174. }
  175. return ret;
  176. }
  177. /**
  178. * free_qpn_table - free the QP number table for a device
  179. * @qpt: the QPN table
  180. */
  181. static void free_qpn_table(struct rvt_qpn_table *qpt)
  182. {
  183. int i;
  184. for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
  185. free_page((unsigned long)qpt->map[i].page);
  186. }
  187. /**
  188. * rvt_driver_qp_init - Init driver qp resources
  189. * @rdi: rvt dev strucutre
  190. *
  191. * Return: 0 on success
  192. */
  193. int rvt_driver_qp_init(struct rvt_dev_info *rdi)
  194. {
  195. int i;
  196. int ret = -ENOMEM;
  197. if (!rdi->dparms.qp_table_size)
  198. return -EINVAL;
  199. /*
  200. * If driver is not doing any QP allocation then make sure it is
  201. * providing the necessary QP functions.
  202. */
  203. if (!rdi->driver_f.free_all_qps ||
  204. !rdi->driver_f.qp_priv_alloc ||
  205. !rdi->driver_f.qp_priv_free ||
  206. !rdi->driver_f.notify_qp_reset ||
  207. !rdi->driver_f.notify_restart_rc)
  208. return -EINVAL;
  209. /* allocate parent object */
  210. rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
  211. rdi->dparms.node);
  212. if (!rdi->qp_dev)
  213. return -ENOMEM;
  214. /* allocate hash table */
  215. rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
  216. rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
  217. rdi->qp_dev->qp_table =
  218. kmalloc_array_node(rdi->qp_dev->qp_table_size,
  219. sizeof(*rdi->qp_dev->qp_table),
  220. GFP_KERNEL, rdi->dparms.node);
  221. if (!rdi->qp_dev->qp_table)
  222. goto no_qp_table;
  223. for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
  224. RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
  225. spin_lock_init(&rdi->qp_dev->qpt_lock);
  226. /* initialize qpn map */
  227. if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
  228. goto fail_table;
  229. spin_lock_init(&rdi->n_qps_lock);
  230. return 0;
  231. fail_table:
  232. kfree(rdi->qp_dev->qp_table);
  233. free_qpn_table(&rdi->qp_dev->qpn_table);
  234. no_qp_table:
  235. kfree(rdi->qp_dev);
  236. return ret;
  237. }
  238. /**
  239. * free_all_qps - check for QPs still in use
  240. * @qpt: the QP table to empty
  241. *
  242. * There should not be any QPs still in use.
  243. * Free memory for table.
  244. */
  245. static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
  246. {
  247. unsigned long flags;
  248. struct rvt_qp *qp;
  249. unsigned n, qp_inuse = 0;
  250. spinlock_t *ql; /* work around too long line below */
  251. if (rdi->driver_f.free_all_qps)
  252. qp_inuse = rdi->driver_f.free_all_qps(rdi);
  253. qp_inuse += rvt_mcast_tree_empty(rdi);
  254. if (!rdi->qp_dev)
  255. return qp_inuse;
  256. ql = &rdi->qp_dev->qpt_lock;
  257. spin_lock_irqsave(ql, flags);
  258. for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
  259. qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
  260. lockdep_is_held(ql));
  261. RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
  262. for (; qp; qp = rcu_dereference_protected(qp->next,
  263. lockdep_is_held(ql)))
  264. qp_inuse++;
  265. }
  266. spin_unlock_irqrestore(ql, flags);
  267. synchronize_rcu();
  268. return qp_inuse;
  269. }
  270. /**
  271. * rvt_qp_exit - clean up qps on device exit
  272. * @rdi: rvt dev structure
  273. *
  274. * Check for qp leaks and free resources.
  275. */
  276. void rvt_qp_exit(struct rvt_dev_info *rdi)
  277. {
  278. u32 qps_inuse = rvt_free_all_qps(rdi);
  279. if (qps_inuse)
  280. rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
  281. qps_inuse);
  282. if (!rdi->qp_dev)
  283. return;
  284. kfree(rdi->qp_dev->qp_table);
  285. free_qpn_table(&rdi->qp_dev->qpn_table);
  286. kfree(rdi->qp_dev);
  287. }
  288. static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
  289. struct rvt_qpn_map *map, unsigned off)
  290. {
  291. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  292. }
  293. /**
  294. * alloc_qpn - Allocate the next available qpn or zero/one for QP type
  295. * IB_QPT_SMI/IB_QPT_GSI
  296. *@rdi: rvt device info structure
  297. *@qpt: queue pair number table pointer
  298. *@port_num: IB port number, 1 based, comes from core
  299. *
  300. * Return: The queue pair number
  301. */
  302. static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
  303. enum ib_qp_type type, u8 port_num)
  304. {
  305. u32 i, offset, max_scan, qpn;
  306. struct rvt_qpn_map *map;
  307. u32 ret;
  308. if (rdi->driver_f.alloc_qpn)
  309. return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
  310. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  311. unsigned n;
  312. ret = type == IB_QPT_GSI;
  313. n = 1 << (ret + 2 * (port_num - 1));
  314. spin_lock(&qpt->lock);
  315. if (qpt->flags & n)
  316. ret = -EINVAL;
  317. else
  318. qpt->flags |= n;
  319. spin_unlock(&qpt->lock);
  320. goto bail;
  321. }
  322. qpn = qpt->last + qpt->incr;
  323. if (qpn >= RVT_QPN_MAX)
  324. qpn = qpt->incr | ((qpt->last & 1) ^ 1);
  325. /* offset carries bit 0 */
  326. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  327. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  328. max_scan = qpt->nmaps - !offset;
  329. for (i = 0;;) {
  330. if (unlikely(!map->page)) {
  331. get_map_page(qpt, map);
  332. if (unlikely(!map->page))
  333. break;
  334. }
  335. do {
  336. if (!test_and_set_bit(offset, map->page)) {
  337. qpt->last = qpn;
  338. ret = qpn;
  339. goto bail;
  340. }
  341. offset += qpt->incr;
  342. /*
  343. * This qpn might be bogus if offset >= BITS_PER_PAGE.
  344. * That is OK. It gets re-assigned below
  345. */
  346. qpn = mk_qpn(qpt, map, offset);
  347. } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
  348. /*
  349. * In order to keep the number of pages allocated to a
  350. * minimum, we scan the all existing pages before increasing
  351. * the size of the bitmap table.
  352. */
  353. if (++i > max_scan) {
  354. if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
  355. break;
  356. map = &qpt->map[qpt->nmaps++];
  357. /* start at incr with current bit 0 */
  358. offset = qpt->incr | (offset & 1);
  359. } else if (map < &qpt->map[qpt->nmaps]) {
  360. ++map;
  361. /* start at incr with current bit 0 */
  362. offset = qpt->incr | (offset & 1);
  363. } else {
  364. map = &qpt->map[0];
  365. /* wrap to first map page, invert bit 0 */
  366. offset = qpt->incr | ((offset & 1) ^ 1);
  367. }
  368. /* there can be no set bits in low-order QoS bits */
  369. WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
  370. qpn = mk_qpn(qpt, map, offset);
  371. }
  372. ret = -ENOMEM;
  373. bail:
  374. return ret;
  375. }
  376. /**
  377. * rvt_clear_mr_refs - Drop help mr refs
  378. * @qp: rvt qp data structure
  379. * @clr_sends: If shoudl clear send side or not
  380. */
  381. static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
  382. {
  383. unsigned n;
  384. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  385. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  386. rvt_put_ss(&qp->s_rdma_read_sge);
  387. rvt_put_ss(&qp->r_sge);
  388. if (clr_sends) {
  389. while (qp->s_last != qp->s_head) {
  390. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  391. rvt_put_swqe(wqe);
  392. if (qp->ibqp.qp_type == IB_QPT_UD ||
  393. qp->ibqp.qp_type == IB_QPT_SMI ||
  394. qp->ibqp.qp_type == IB_QPT_GSI)
  395. atomic_dec(&ibah_to_rvtah(
  396. wqe->ud_wr.ah)->refcount);
  397. if (++qp->s_last >= qp->s_size)
  398. qp->s_last = 0;
  399. smp_wmb(); /* see qp_set_savail */
  400. }
  401. if (qp->s_rdma_mr) {
  402. rvt_put_mr(qp->s_rdma_mr);
  403. qp->s_rdma_mr = NULL;
  404. }
  405. }
  406. for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) {
  407. struct rvt_ack_entry *e = &qp->s_ack_queue[n];
  408. if (e->rdma_sge.mr) {
  409. rvt_put_mr(e->rdma_sge.mr);
  410. e->rdma_sge.mr = NULL;
  411. }
  412. }
  413. }
  414. /**
  415. * rvt_swqe_has_lkey - return true if lkey is used by swqe
  416. * @wqe - the send wqe
  417. * @lkey - the lkey
  418. *
  419. * Test the swqe for using lkey
  420. */
  421. static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey)
  422. {
  423. int i;
  424. for (i = 0; i < wqe->wr.num_sge; i++) {
  425. struct rvt_sge *sge = &wqe->sg_list[i];
  426. if (rvt_mr_has_lkey(sge->mr, lkey))
  427. return true;
  428. }
  429. return false;
  430. }
  431. /**
  432. * rvt_qp_sends_has_lkey - return true is qp sends use lkey
  433. * @qp - the rvt_qp
  434. * @lkey - the lkey
  435. */
  436. static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey)
  437. {
  438. u32 s_last = qp->s_last;
  439. while (s_last != qp->s_head) {
  440. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last);
  441. if (rvt_swqe_has_lkey(wqe, lkey))
  442. return true;
  443. if (++s_last >= qp->s_size)
  444. s_last = 0;
  445. }
  446. if (qp->s_rdma_mr)
  447. if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey))
  448. return true;
  449. return false;
  450. }
  451. /**
  452. * rvt_qp_acks_has_lkey - return true if acks have lkey
  453. * @qp - the qp
  454. * @lkey - the lkey
  455. */
  456. static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey)
  457. {
  458. int i;
  459. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  460. for (i = 0; qp->s_ack_queue && i < rvt_max_atomic(rdi); i++) {
  461. struct rvt_ack_entry *e = &qp->s_ack_queue[i];
  462. if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey))
  463. return true;
  464. }
  465. return false;
  466. }
  467. /*
  468. * rvt_qp_mr_clean - clean up remote ops for lkey
  469. * @qp - the qp
  470. * @lkey - the lkey that is being de-registered
  471. *
  472. * This routine checks if the lkey is being used by
  473. * the qp.
  474. *
  475. * If so, the qp is put into an error state to elminate
  476. * any references from the qp.
  477. */
  478. void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey)
  479. {
  480. bool lastwqe = false;
  481. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  482. qp->ibqp.qp_type == IB_QPT_GSI)
  483. /* avoid special QPs */
  484. return;
  485. spin_lock_irq(&qp->r_lock);
  486. spin_lock(&qp->s_hlock);
  487. spin_lock(&qp->s_lock);
  488. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  489. goto check_lwqe;
  490. if (rvt_ss_has_lkey(&qp->r_sge, lkey) ||
  491. rvt_qp_sends_has_lkey(qp, lkey) ||
  492. rvt_qp_acks_has_lkey(qp, lkey))
  493. lastwqe = rvt_error_qp(qp, IB_WC_LOC_PROT_ERR);
  494. check_lwqe:
  495. spin_unlock(&qp->s_lock);
  496. spin_unlock(&qp->s_hlock);
  497. spin_unlock_irq(&qp->r_lock);
  498. if (lastwqe) {
  499. struct ib_event ev;
  500. ev.device = qp->ibqp.device;
  501. ev.element.qp = &qp->ibqp;
  502. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  503. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  504. }
  505. }
  506. /**
  507. * rvt_remove_qp - remove qp form table
  508. * @rdi: rvt dev struct
  509. * @qp: qp to remove
  510. *
  511. * Remove the QP from the table so it can't be found asynchronously by
  512. * the receive routine.
  513. */
  514. static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  515. {
  516. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  517. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  518. unsigned long flags;
  519. int removed = 1;
  520. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  521. if (rcu_dereference_protected(rvp->qp[0],
  522. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  523. RCU_INIT_POINTER(rvp->qp[0], NULL);
  524. } else if (rcu_dereference_protected(rvp->qp[1],
  525. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  526. RCU_INIT_POINTER(rvp->qp[1], NULL);
  527. } else {
  528. struct rvt_qp *q;
  529. struct rvt_qp __rcu **qpp;
  530. removed = 0;
  531. qpp = &rdi->qp_dev->qp_table[n];
  532. for (; (q = rcu_dereference_protected(*qpp,
  533. lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
  534. qpp = &q->next) {
  535. if (q == qp) {
  536. RCU_INIT_POINTER(*qpp,
  537. rcu_dereference_protected(qp->next,
  538. lockdep_is_held(&rdi->qp_dev->qpt_lock)));
  539. removed = 1;
  540. trace_rvt_qpremove(qp, n);
  541. break;
  542. }
  543. }
  544. }
  545. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  546. if (removed) {
  547. synchronize_rcu();
  548. rvt_put_qp(qp);
  549. }
  550. }
  551. /**
  552. * rvt_init_qp - initialize the QP state to the reset state
  553. * @qp: the QP to init or reinit
  554. * @type: the QP type
  555. *
  556. * This function is called from both rvt_create_qp() and
  557. * rvt_reset_qp(). The difference is that the reset
  558. * patch the necessary locks to protect against concurent
  559. * access.
  560. */
  561. static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  562. enum ib_qp_type type)
  563. {
  564. qp->remote_qpn = 0;
  565. qp->qkey = 0;
  566. qp->qp_access_flags = 0;
  567. qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
  568. qp->s_hdrwords = 0;
  569. qp->s_wqe = NULL;
  570. qp->s_draining = 0;
  571. qp->s_next_psn = 0;
  572. qp->s_last_psn = 0;
  573. qp->s_sending_psn = 0;
  574. qp->s_sending_hpsn = 0;
  575. qp->s_psn = 0;
  576. qp->r_psn = 0;
  577. qp->r_msn = 0;
  578. if (type == IB_QPT_RC) {
  579. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  580. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  581. } else {
  582. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  583. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  584. }
  585. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  586. qp->r_nak_state = 0;
  587. qp->r_aflags = 0;
  588. qp->r_flags = 0;
  589. qp->s_head = 0;
  590. qp->s_tail = 0;
  591. qp->s_cur = 0;
  592. qp->s_acked = 0;
  593. qp->s_last = 0;
  594. qp->s_ssn = 1;
  595. qp->s_lsn = 0;
  596. qp->s_mig_state = IB_MIG_MIGRATED;
  597. qp->r_head_ack_queue = 0;
  598. qp->s_tail_ack_queue = 0;
  599. qp->s_num_rd_atomic = 0;
  600. if (qp->r_rq.wq) {
  601. qp->r_rq.wq->head = 0;
  602. qp->r_rq.wq->tail = 0;
  603. }
  604. qp->r_sge.num_sge = 0;
  605. atomic_set(&qp->s_reserved_used, 0);
  606. }
  607. /**
  608. * rvt_reset_qp - initialize the QP state to the reset state
  609. * @qp: the QP to reset
  610. * @type: the QP type
  611. *
  612. * r_lock, s_hlock, and s_lock are required to be held by the caller
  613. */
  614. static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  615. enum ib_qp_type type)
  616. __must_hold(&qp->s_lock)
  617. __must_hold(&qp->s_hlock)
  618. __must_hold(&qp->r_lock)
  619. {
  620. lockdep_assert_held(&qp->r_lock);
  621. lockdep_assert_held(&qp->s_hlock);
  622. lockdep_assert_held(&qp->s_lock);
  623. if (qp->state != IB_QPS_RESET) {
  624. qp->state = IB_QPS_RESET;
  625. /* Let drivers flush their waitlist */
  626. rdi->driver_f.flush_qp_waiters(qp);
  627. rvt_stop_rc_timers(qp);
  628. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
  629. spin_unlock(&qp->s_lock);
  630. spin_unlock(&qp->s_hlock);
  631. spin_unlock_irq(&qp->r_lock);
  632. /* Stop the send queue and the retry timer */
  633. rdi->driver_f.stop_send_queue(qp);
  634. rvt_del_timers_sync(qp);
  635. /* Wait for things to stop */
  636. rdi->driver_f.quiesce_qp(qp);
  637. /* take qp out the hash and wait for it to be unused */
  638. rvt_remove_qp(rdi, qp);
  639. /* grab the lock b/c it was locked at call time */
  640. spin_lock_irq(&qp->r_lock);
  641. spin_lock(&qp->s_hlock);
  642. spin_lock(&qp->s_lock);
  643. rvt_clear_mr_refs(qp, 1);
  644. /*
  645. * Let the driver do any tear down or re-init it needs to for
  646. * a qp that has been reset
  647. */
  648. rdi->driver_f.notify_qp_reset(qp);
  649. }
  650. rvt_init_qp(rdi, qp, type);
  651. lockdep_assert_held(&qp->r_lock);
  652. lockdep_assert_held(&qp->s_hlock);
  653. lockdep_assert_held(&qp->s_lock);
  654. }
  655. /** rvt_free_qpn - Free a qpn from the bit map
  656. * @qpt: QP table
  657. * @qpn: queue pair number to free
  658. */
  659. static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  660. {
  661. struct rvt_qpn_map *map;
  662. map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
  663. if (map->page)
  664. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  665. }
  666. /**
  667. * rvt_create_qp - create a queue pair for a device
  668. * @ibpd: the protection domain who's device we create the queue pair for
  669. * @init_attr: the attributes of the queue pair
  670. * @udata: user data for libibverbs.so
  671. *
  672. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  673. * unique idea of what queue pair numbers mean. For instance there is a reserved
  674. * range for PSM.
  675. *
  676. * Return: the queue pair on success, otherwise returns an errno.
  677. *
  678. * Called by the ib_create_qp() core verbs function.
  679. */
  680. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  681. struct ib_qp_init_attr *init_attr,
  682. struct ib_udata *udata)
  683. {
  684. struct rvt_qp *qp;
  685. int err;
  686. struct rvt_swqe *swq = NULL;
  687. size_t sz;
  688. size_t sg_list_sz;
  689. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  690. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  691. void *priv = NULL;
  692. size_t sqsize;
  693. if (!rdi)
  694. return ERR_PTR(-EINVAL);
  695. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
  696. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  697. init_attr->create_flags)
  698. return ERR_PTR(-EINVAL);
  699. /* Check receive queue parameters if no SRQ is specified. */
  700. if (!init_attr->srq) {
  701. if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
  702. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  703. return ERR_PTR(-EINVAL);
  704. if (init_attr->cap.max_send_sge +
  705. init_attr->cap.max_send_wr +
  706. init_attr->cap.max_recv_sge +
  707. init_attr->cap.max_recv_wr == 0)
  708. return ERR_PTR(-EINVAL);
  709. }
  710. sqsize =
  711. init_attr->cap.max_send_wr + 1 +
  712. rdi->dparms.reserved_operations;
  713. switch (init_attr->qp_type) {
  714. case IB_QPT_SMI:
  715. case IB_QPT_GSI:
  716. if (init_attr->port_num == 0 ||
  717. init_attr->port_num > ibpd->device->phys_port_cnt)
  718. return ERR_PTR(-EINVAL);
  719. /* fall through */
  720. case IB_QPT_UC:
  721. case IB_QPT_RC:
  722. case IB_QPT_UD:
  723. sz = sizeof(struct rvt_sge) *
  724. init_attr->cap.max_send_sge +
  725. sizeof(struct rvt_swqe);
  726. swq = vzalloc_node(sqsize * sz, rdi->dparms.node);
  727. if (!swq)
  728. return ERR_PTR(-ENOMEM);
  729. sz = sizeof(*qp);
  730. sg_list_sz = 0;
  731. if (init_attr->srq) {
  732. struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
  733. if (srq->rq.max_sge > 1)
  734. sg_list_sz = sizeof(*qp->r_sg_list) *
  735. (srq->rq.max_sge - 1);
  736. } else if (init_attr->cap.max_recv_sge > 1)
  737. sg_list_sz = sizeof(*qp->r_sg_list) *
  738. (init_attr->cap.max_recv_sge - 1);
  739. qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
  740. rdi->dparms.node);
  741. if (!qp)
  742. goto bail_swq;
  743. RCU_INIT_POINTER(qp->next, NULL);
  744. if (init_attr->qp_type == IB_QPT_RC) {
  745. qp->s_ack_queue =
  746. kzalloc_node(
  747. sizeof(*qp->s_ack_queue) *
  748. rvt_max_atomic(rdi),
  749. GFP_KERNEL,
  750. rdi->dparms.node);
  751. if (!qp->s_ack_queue)
  752. goto bail_qp;
  753. }
  754. /* initialize timers needed for rc qp */
  755. timer_setup(&qp->s_timer, rvt_rc_timeout, 0);
  756. hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
  757. HRTIMER_MODE_REL);
  758. qp->s_rnr_timer.function = rvt_rc_rnr_retry;
  759. /*
  760. * Driver needs to set up it's private QP structure and do any
  761. * initialization that is needed.
  762. */
  763. priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
  764. if (IS_ERR(priv)) {
  765. ret = priv;
  766. goto bail_qp;
  767. }
  768. qp->priv = priv;
  769. qp->timeout_jiffies =
  770. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  771. 1000UL);
  772. if (init_attr->srq) {
  773. sz = 0;
  774. } else {
  775. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  776. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  777. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  778. sizeof(struct rvt_rwqe);
  779. if (udata)
  780. qp->r_rq.wq = vmalloc_user(
  781. sizeof(struct rvt_rwq) +
  782. qp->r_rq.size * sz);
  783. else
  784. qp->r_rq.wq = vzalloc_node(
  785. sizeof(struct rvt_rwq) +
  786. qp->r_rq.size * sz,
  787. rdi->dparms.node);
  788. if (!qp->r_rq.wq)
  789. goto bail_driver_priv;
  790. }
  791. /*
  792. * ib_create_qp() will initialize qp->ibqp
  793. * except for qp->ibqp.qp_num.
  794. */
  795. spin_lock_init(&qp->r_lock);
  796. spin_lock_init(&qp->s_hlock);
  797. spin_lock_init(&qp->s_lock);
  798. spin_lock_init(&qp->r_rq.lock);
  799. atomic_set(&qp->refcount, 0);
  800. atomic_set(&qp->local_ops_pending, 0);
  801. init_waitqueue_head(&qp->wait);
  802. INIT_LIST_HEAD(&qp->rspwait);
  803. qp->state = IB_QPS_RESET;
  804. qp->s_wq = swq;
  805. qp->s_size = sqsize;
  806. qp->s_avail = init_attr->cap.max_send_wr;
  807. qp->s_max_sge = init_attr->cap.max_send_sge;
  808. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  809. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  810. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  811. init_attr->qp_type,
  812. init_attr->port_num);
  813. if (err < 0) {
  814. ret = ERR_PTR(err);
  815. goto bail_rq_wq;
  816. }
  817. qp->ibqp.qp_num = err;
  818. qp->port_num = init_attr->port_num;
  819. rvt_init_qp(rdi, qp, init_attr->qp_type);
  820. break;
  821. default:
  822. /* Don't support raw QPs */
  823. return ERR_PTR(-EINVAL);
  824. }
  825. init_attr->cap.max_inline_data = 0;
  826. /*
  827. * Return the address of the RWQ as the offset to mmap.
  828. * See rvt_mmap() for details.
  829. */
  830. if (udata && udata->outlen >= sizeof(__u64)) {
  831. if (!qp->r_rq.wq) {
  832. __u64 offset = 0;
  833. err = ib_copy_to_udata(udata, &offset,
  834. sizeof(offset));
  835. if (err) {
  836. ret = ERR_PTR(err);
  837. goto bail_qpn;
  838. }
  839. } else {
  840. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  841. qp->ip = rvt_create_mmap_info(rdi, s,
  842. ibpd->uobject->context,
  843. qp->r_rq.wq);
  844. if (!qp->ip) {
  845. ret = ERR_PTR(-ENOMEM);
  846. goto bail_qpn;
  847. }
  848. err = ib_copy_to_udata(udata, &qp->ip->offset,
  849. sizeof(qp->ip->offset));
  850. if (err) {
  851. ret = ERR_PTR(err);
  852. goto bail_ip;
  853. }
  854. }
  855. qp->pid = current->pid;
  856. }
  857. spin_lock(&rdi->n_qps_lock);
  858. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  859. spin_unlock(&rdi->n_qps_lock);
  860. ret = ERR_PTR(-ENOMEM);
  861. goto bail_ip;
  862. }
  863. rdi->n_qps_allocated++;
  864. /*
  865. * Maintain a busy_jiffies variable that will be added to the timeout
  866. * period in mod_retry_timer and add_retry_timer. This busy jiffies
  867. * is scaled by the number of rc qps created for the device to reduce
  868. * the number of timeouts occurring when there is a large number of
  869. * qps. busy_jiffies is incremented every rc qp scaling interval.
  870. * The scaling interval is selected based on extensive performance
  871. * evaluation of targeted workloads.
  872. */
  873. if (init_attr->qp_type == IB_QPT_RC) {
  874. rdi->n_rc_qps++;
  875. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  876. }
  877. spin_unlock(&rdi->n_qps_lock);
  878. if (qp->ip) {
  879. spin_lock_irq(&rdi->pending_lock);
  880. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  881. spin_unlock_irq(&rdi->pending_lock);
  882. }
  883. ret = &qp->ibqp;
  884. /*
  885. * We have our QP and its good, now keep track of what types of opcodes
  886. * can be processed on this QP. We do this by keeping track of what the
  887. * 3 high order bits of the opcode are.
  888. */
  889. switch (init_attr->qp_type) {
  890. case IB_QPT_SMI:
  891. case IB_QPT_GSI:
  892. case IB_QPT_UD:
  893. qp->allowed_ops = IB_OPCODE_UD;
  894. break;
  895. case IB_QPT_RC:
  896. qp->allowed_ops = IB_OPCODE_RC;
  897. break;
  898. case IB_QPT_UC:
  899. qp->allowed_ops = IB_OPCODE_UC;
  900. break;
  901. default:
  902. ret = ERR_PTR(-EINVAL);
  903. goto bail_ip;
  904. }
  905. return ret;
  906. bail_ip:
  907. if (qp->ip)
  908. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  909. bail_qpn:
  910. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  911. bail_rq_wq:
  912. if (!qp->ip)
  913. vfree(qp->r_rq.wq);
  914. bail_driver_priv:
  915. rdi->driver_f.qp_priv_free(rdi, qp);
  916. bail_qp:
  917. kfree(qp->s_ack_queue);
  918. kfree(qp);
  919. bail_swq:
  920. vfree(swq);
  921. return ret;
  922. }
  923. /**
  924. * rvt_error_qp - put a QP into the error state
  925. * @qp: the QP to put into the error state
  926. * @err: the receive completion error to signal if a RWQE is active
  927. *
  928. * Flushes both send and receive work queues.
  929. *
  930. * Return: true if last WQE event should be generated.
  931. * The QP r_lock and s_lock should be held and interrupts disabled.
  932. * If we are already in error state, just return.
  933. */
  934. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  935. {
  936. struct ib_wc wc;
  937. int ret = 0;
  938. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  939. lockdep_assert_held(&qp->r_lock);
  940. lockdep_assert_held(&qp->s_lock);
  941. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  942. goto bail;
  943. qp->state = IB_QPS_ERR;
  944. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  945. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  946. del_timer(&qp->s_timer);
  947. }
  948. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  949. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  950. rdi->driver_f.notify_error_qp(qp);
  951. /* Schedule the sending tasklet to drain the send work queue. */
  952. if (READ_ONCE(qp->s_last) != qp->s_head)
  953. rdi->driver_f.schedule_send(qp);
  954. rvt_clear_mr_refs(qp, 0);
  955. memset(&wc, 0, sizeof(wc));
  956. wc.qp = &qp->ibqp;
  957. wc.opcode = IB_WC_RECV;
  958. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  959. wc.wr_id = qp->r_wr_id;
  960. wc.status = err;
  961. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  962. }
  963. wc.status = IB_WC_WR_FLUSH_ERR;
  964. if (qp->r_rq.wq) {
  965. struct rvt_rwq *wq;
  966. u32 head;
  967. u32 tail;
  968. spin_lock(&qp->r_rq.lock);
  969. /* sanity check pointers before trusting them */
  970. wq = qp->r_rq.wq;
  971. head = wq->head;
  972. if (head >= qp->r_rq.size)
  973. head = 0;
  974. tail = wq->tail;
  975. if (tail >= qp->r_rq.size)
  976. tail = 0;
  977. while (tail != head) {
  978. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  979. if (++tail >= qp->r_rq.size)
  980. tail = 0;
  981. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  982. }
  983. wq->tail = tail;
  984. spin_unlock(&qp->r_rq.lock);
  985. } else if (qp->ibqp.event_handler) {
  986. ret = 1;
  987. }
  988. bail:
  989. return ret;
  990. }
  991. EXPORT_SYMBOL(rvt_error_qp);
  992. /*
  993. * Put the QP into the hash table.
  994. * The hash table holds a reference to the QP.
  995. */
  996. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  997. {
  998. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  999. unsigned long flags;
  1000. rvt_get_qp(qp);
  1001. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  1002. if (qp->ibqp.qp_num <= 1) {
  1003. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  1004. } else {
  1005. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  1006. qp->next = rdi->qp_dev->qp_table[n];
  1007. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  1008. trace_rvt_qpinsert(qp, n);
  1009. }
  1010. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  1011. }
  1012. /**
  1013. * rvt_modify_qp - modify the attributes of a queue pair
  1014. * @ibqp: the queue pair who's attributes we're modifying
  1015. * @attr: the new attributes
  1016. * @attr_mask: the mask of attributes to modify
  1017. * @udata: user data for libibverbs.so
  1018. *
  1019. * Return: 0 on success, otherwise returns an errno.
  1020. */
  1021. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1022. int attr_mask, struct ib_udata *udata)
  1023. {
  1024. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1025. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1026. enum ib_qp_state cur_state, new_state;
  1027. struct ib_event ev;
  1028. int lastwqe = 0;
  1029. int mig = 0;
  1030. int pmtu = 0; /* for gcc warning only */
  1031. enum rdma_link_layer link;
  1032. int opa_ah;
  1033. link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
  1034. spin_lock_irq(&qp->r_lock);
  1035. spin_lock(&qp->s_hlock);
  1036. spin_lock(&qp->s_lock);
  1037. cur_state = attr_mask & IB_QP_CUR_STATE ?
  1038. attr->cur_qp_state : qp->state;
  1039. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1040. opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
  1041. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  1042. attr_mask, link))
  1043. goto inval;
  1044. if (rdi->driver_f.check_modify_qp &&
  1045. rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
  1046. goto inval;
  1047. if (attr_mask & IB_QP_AV) {
  1048. if (opa_ah) {
  1049. if (rdma_ah_get_dlid(&attr->ah_attr) >=
  1050. opa_get_mcast_base(OPA_MCAST_NR))
  1051. goto inval;
  1052. } else {
  1053. if (rdma_ah_get_dlid(&attr->ah_attr) >=
  1054. be16_to_cpu(IB_MULTICAST_LID_BASE))
  1055. goto inval;
  1056. }
  1057. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  1058. goto inval;
  1059. }
  1060. if (attr_mask & IB_QP_ALT_PATH) {
  1061. if (opa_ah) {
  1062. if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
  1063. opa_get_mcast_base(OPA_MCAST_NR))
  1064. goto inval;
  1065. } else {
  1066. if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
  1067. be16_to_cpu(IB_MULTICAST_LID_BASE))
  1068. goto inval;
  1069. }
  1070. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  1071. goto inval;
  1072. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  1073. goto inval;
  1074. }
  1075. if (attr_mask & IB_QP_PKEY_INDEX)
  1076. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  1077. goto inval;
  1078. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1079. if (attr->min_rnr_timer > 31)
  1080. goto inval;
  1081. if (attr_mask & IB_QP_PORT)
  1082. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  1083. qp->ibqp.qp_type == IB_QPT_GSI ||
  1084. attr->port_num == 0 ||
  1085. attr->port_num > ibqp->device->phys_port_cnt)
  1086. goto inval;
  1087. if (attr_mask & IB_QP_DEST_QPN)
  1088. if (attr->dest_qp_num > RVT_QPN_MASK)
  1089. goto inval;
  1090. if (attr_mask & IB_QP_RETRY_CNT)
  1091. if (attr->retry_cnt > 7)
  1092. goto inval;
  1093. if (attr_mask & IB_QP_RNR_RETRY)
  1094. if (attr->rnr_retry > 7)
  1095. goto inval;
  1096. /*
  1097. * Don't allow invalid path_mtu values. OK to set greater
  1098. * than the active mtu (or even the max_cap, if we have tuned
  1099. * that to a small mtu. We'll set qp->path_mtu
  1100. * to the lesser of requested attribute mtu and active,
  1101. * for packetizing messages.
  1102. * Note that the QP port has to be set in INIT and MTU in RTR.
  1103. */
  1104. if (attr_mask & IB_QP_PATH_MTU) {
  1105. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  1106. if (pmtu < 0)
  1107. goto inval;
  1108. }
  1109. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1110. if (attr->path_mig_state == IB_MIG_REARM) {
  1111. if (qp->s_mig_state == IB_MIG_ARMED)
  1112. goto inval;
  1113. if (new_state != IB_QPS_RTS)
  1114. goto inval;
  1115. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  1116. if (qp->s_mig_state == IB_MIG_REARM)
  1117. goto inval;
  1118. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  1119. goto inval;
  1120. if (qp->s_mig_state == IB_MIG_ARMED)
  1121. mig = 1;
  1122. } else {
  1123. goto inval;
  1124. }
  1125. }
  1126. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1127. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  1128. goto inval;
  1129. switch (new_state) {
  1130. case IB_QPS_RESET:
  1131. if (qp->state != IB_QPS_RESET)
  1132. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1133. break;
  1134. case IB_QPS_RTR:
  1135. /* Allow event to re-trigger if QP set to RTR more than once */
  1136. qp->r_flags &= ~RVT_R_COMM_EST;
  1137. qp->state = new_state;
  1138. break;
  1139. case IB_QPS_SQD:
  1140. qp->s_draining = qp->s_last != qp->s_cur;
  1141. qp->state = new_state;
  1142. break;
  1143. case IB_QPS_SQE:
  1144. if (qp->ibqp.qp_type == IB_QPT_RC)
  1145. goto inval;
  1146. qp->state = new_state;
  1147. break;
  1148. case IB_QPS_ERR:
  1149. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  1150. break;
  1151. default:
  1152. qp->state = new_state;
  1153. break;
  1154. }
  1155. if (attr_mask & IB_QP_PKEY_INDEX)
  1156. qp->s_pkey_index = attr->pkey_index;
  1157. if (attr_mask & IB_QP_PORT)
  1158. qp->port_num = attr->port_num;
  1159. if (attr_mask & IB_QP_DEST_QPN)
  1160. qp->remote_qpn = attr->dest_qp_num;
  1161. if (attr_mask & IB_QP_SQ_PSN) {
  1162. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  1163. qp->s_psn = qp->s_next_psn;
  1164. qp->s_sending_psn = qp->s_next_psn;
  1165. qp->s_last_psn = qp->s_next_psn - 1;
  1166. qp->s_sending_hpsn = qp->s_last_psn;
  1167. }
  1168. if (attr_mask & IB_QP_RQ_PSN)
  1169. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  1170. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1171. qp->qp_access_flags = attr->qp_access_flags;
  1172. if (attr_mask & IB_QP_AV) {
  1173. qp->remote_ah_attr = attr->ah_attr;
  1174. qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
  1175. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  1176. }
  1177. if (attr_mask & IB_QP_ALT_PATH) {
  1178. qp->alt_ah_attr = attr->alt_ah_attr;
  1179. qp->s_alt_pkey_index = attr->alt_pkey_index;
  1180. }
  1181. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1182. qp->s_mig_state = attr->path_mig_state;
  1183. if (mig) {
  1184. qp->remote_ah_attr = qp->alt_ah_attr;
  1185. qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
  1186. qp->s_pkey_index = qp->s_alt_pkey_index;
  1187. }
  1188. }
  1189. if (attr_mask & IB_QP_PATH_MTU) {
  1190. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  1191. qp->log_pmtu = ilog2(qp->pmtu);
  1192. }
  1193. if (attr_mask & IB_QP_RETRY_CNT) {
  1194. qp->s_retry_cnt = attr->retry_cnt;
  1195. qp->s_retry = attr->retry_cnt;
  1196. }
  1197. if (attr_mask & IB_QP_RNR_RETRY) {
  1198. qp->s_rnr_retry_cnt = attr->rnr_retry;
  1199. qp->s_rnr_retry = attr->rnr_retry;
  1200. }
  1201. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1202. qp->r_min_rnr_timer = attr->min_rnr_timer;
  1203. if (attr_mask & IB_QP_TIMEOUT) {
  1204. qp->timeout = attr->timeout;
  1205. qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
  1206. }
  1207. if (attr_mask & IB_QP_QKEY)
  1208. qp->qkey = attr->qkey;
  1209. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1210. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  1211. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1212. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1213. if (rdi->driver_f.modify_qp)
  1214. rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
  1215. spin_unlock(&qp->s_lock);
  1216. spin_unlock(&qp->s_hlock);
  1217. spin_unlock_irq(&qp->r_lock);
  1218. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1219. rvt_insert_qp(rdi, qp);
  1220. if (lastwqe) {
  1221. ev.device = qp->ibqp.device;
  1222. ev.element.qp = &qp->ibqp;
  1223. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1224. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1225. }
  1226. if (mig) {
  1227. ev.device = qp->ibqp.device;
  1228. ev.element.qp = &qp->ibqp;
  1229. ev.event = IB_EVENT_PATH_MIG;
  1230. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1231. }
  1232. return 0;
  1233. inval:
  1234. spin_unlock(&qp->s_lock);
  1235. spin_unlock(&qp->s_hlock);
  1236. spin_unlock_irq(&qp->r_lock);
  1237. return -EINVAL;
  1238. }
  1239. /**
  1240. * rvt_destroy_qp - destroy a queue pair
  1241. * @ibqp: the queue pair to destroy
  1242. *
  1243. * Note that this can be called while the QP is actively sending or
  1244. * receiving!
  1245. *
  1246. * Return: 0 on success.
  1247. */
  1248. int rvt_destroy_qp(struct ib_qp *ibqp)
  1249. {
  1250. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1251. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1252. spin_lock_irq(&qp->r_lock);
  1253. spin_lock(&qp->s_hlock);
  1254. spin_lock(&qp->s_lock);
  1255. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1256. spin_unlock(&qp->s_lock);
  1257. spin_unlock(&qp->s_hlock);
  1258. spin_unlock_irq(&qp->r_lock);
  1259. wait_event(qp->wait, !atomic_read(&qp->refcount));
  1260. /* qpn is now available for use again */
  1261. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1262. spin_lock(&rdi->n_qps_lock);
  1263. rdi->n_qps_allocated--;
  1264. if (qp->ibqp.qp_type == IB_QPT_RC) {
  1265. rdi->n_rc_qps--;
  1266. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1267. }
  1268. spin_unlock(&rdi->n_qps_lock);
  1269. if (qp->ip)
  1270. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1271. else
  1272. vfree(qp->r_rq.wq);
  1273. vfree(qp->s_wq);
  1274. rdi->driver_f.qp_priv_free(rdi, qp);
  1275. kfree(qp->s_ack_queue);
  1276. kfree(qp);
  1277. return 0;
  1278. }
  1279. /**
  1280. * rvt_query_qp - query an ipbq
  1281. * @ibqp: IB qp to query
  1282. * @attr: attr struct to fill in
  1283. * @attr_mask: attr mask ignored
  1284. * @init_attr: struct to fill in
  1285. *
  1286. * Return: always 0
  1287. */
  1288. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1289. int attr_mask, struct ib_qp_init_attr *init_attr)
  1290. {
  1291. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1292. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1293. attr->qp_state = qp->state;
  1294. attr->cur_qp_state = attr->qp_state;
  1295. attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  1296. attr->path_mig_state = qp->s_mig_state;
  1297. attr->qkey = qp->qkey;
  1298. attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
  1299. attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
  1300. attr->dest_qp_num = qp->remote_qpn;
  1301. attr->qp_access_flags = qp->qp_access_flags;
  1302. attr->cap.max_send_wr = qp->s_size - 1 -
  1303. rdi->dparms.reserved_operations;
  1304. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  1305. attr->cap.max_send_sge = qp->s_max_sge;
  1306. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  1307. attr->cap.max_inline_data = 0;
  1308. attr->ah_attr = qp->remote_ah_attr;
  1309. attr->alt_ah_attr = qp->alt_ah_attr;
  1310. attr->pkey_index = qp->s_pkey_index;
  1311. attr->alt_pkey_index = qp->s_alt_pkey_index;
  1312. attr->en_sqd_async_notify = 0;
  1313. attr->sq_draining = qp->s_draining;
  1314. attr->max_rd_atomic = qp->s_max_rd_atomic;
  1315. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  1316. attr->min_rnr_timer = qp->r_min_rnr_timer;
  1317. attr->port_num = qp->port_num;
  1318. attr->timeout = qp->timeout;
  1319. attr->retry_cnt = qp->s_retry_cnt;
  1320. attr->rnr_retry = qp->s_rnr_retry_cnt;
  1321. attr->alt_port_num =
  1322. rdma_ah_get_port_num(&qp->alt_ah_attr);
  1323. attr->alt_timeout = qp->alt_timeout;
  1324. init_attr->event_handler = qp->ibqp.event_handler;
  1325. init_attr->qp_context = qp->ibqp.qp_context;
  1326. init_attr->send_cq = qp->ibqp.send_cq;
  1327. init_attr->recv_cq = qp->ibqp.recv_cq;
  1328. init_attr->srq = qp->ibqp.srq;
  1329. init_attr->cap = attr->cap;
  1330. if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
  1331. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  1332. else
  1333. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  1334. init_attr->qp_type = qp->ibqp.qp_type;
  1335. init_attr->port_num = qp->port_num;
  1336. return 0;
  1337. }
  1338. /**
  1339. * rvt_post_receive - post a receive on a QP
  1340. * @ibqp: the QP to post the receive on
  1341. * @wr: the WR to post
  1342. * @bad_wr: the first bad WR is put here
  1343. *
  1344. * This may be called from interrupt context.
  1345. *
  1346. * Return: 0 on success otherwise errno
  1347. */
  1348. int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1349. struct ib_recv_wr **bad_wr)
  1350. {
  1351. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1352. struct rvt_rwq *wq = qp->r_rq.wq;
  1353. unsigned long flags;
  1354. int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
  1355. !qp->ibqp.srq;
  1356. /* Check that state is OK to post receive. */
  1357. if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
  1358. *bad_wr = wr;
  1359. return -EINVAL;
  1360. }
  1361. for (; wr; wr = wr->next) {
  1362. struct rvt_rwqe *wqe;
  1363. u32 next;
  1364. int i;
  1365. if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
  1366. *bad_wr = wr;
  1367. return -EINVAL;
  1368. }
  1369. spin_lock_irqsave(&qp->r_rq.lock, flags);
  1370. next = wq->head + 1;
  1371. if (next >= qp->r_rq.size)
  1372. next = 0;
  1373. if (next == wq->tail) {
  1374. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1375. *bad_wr = wr;
  1376. return -ENOMEM;
  1377. }
  1378. if (unlikely(qp_err_flush)) {
  1379. struct ib_wc wc;
  1380. memset(&wc, 0, sizeof(wc));
  1381. wc.qp = &qp->ibqp;
  1382. wc.opcode = IB_WC_RECV;
  1383. wc.wr_id = wr->wr_id;
  1384. wc.status = IB_WC_WR_FLUSH_ERR;
  1385. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1386. } else {
  1387. wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
  1388. wqe->wr_id = wr->wr_id;
  1389. wqe->num_sge = wr->num_sge;
  1390. for (i = 0; i < wr->num_sge; i++)
  1391. wqe->sg_list[i] = wr->sg_list[i];
  1392. /*
  1393. * Make sure queue entry is written
  1394. * before the head index.
  1395. */
  1396. smp_wmb();
  1397. wq->head = next;
  1398. }
  1399. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1400. }
  1401. return 0;
  1402. }
  1403. /**
  1404. * rvt_qp_valid_operation - validate post send wr request
  1405. * @qp - the qp
  1406. * @post-parms - the post send table for the driver
  1407. * @wr - the work request
  1408. *
  1409. * The routine validates the operation based on the
  1410. * validation table an returns the length of the operation
  1411. * which can extend beyond the ib_send_bw. Operation
  1412. * dependent flags key atomic operation validation.
  1413. *
  1414. * There is an exception for UD qps that validates the pd and
  1415. * overrides the length to include the additional UD specific
  1416. * length.
  1417. *
  1418. * Returns a negative error or the length of the work request
  1419. * for building the swqe.
  1420. */
  1421. static inline int rvt_qp_valid_operation(
  1422. struct rvt_qp *qp,
  1423. const struct rvt_operation_params *post_parms,
  1424. struct ib_send_wr *wr)
  1425. {
  1426. int len;
  1427. if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
  1428. return -EINVAL;
  1429. if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
  1430. return -EINVAL;
  1431. if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
  1432. ibpd_to_rvtpd(qp->ibqp.pd)->user)
  1433. return -EINVAL;
  1434. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
  1435. (wr->num_sge == 0 ||
  1436. wr->sg_list[0].length < sizeof(u64) ||
  1437. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  1438. return -EINVAL;
  1439. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
  1440. !qp->s_max_rd_atomic)
  1441. return -EINVAL;
  1442. len = post_parms[wr->opcode].length;
  1443. /* UD specific */
  1444. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1445. qp->ibqp.qp_type != IB_QPT_RC) {
  1446. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1447. return -EINVAL;
  1448. len = sizeof(struct ib_ud_wr);
  1449. }
  1450. return len;
  1451. }
  1452. /**
  1453. * rvt_qp_is_avail - determine queue capacity
  1454. * @qp - the qp
  1455. * @rdi - the rdmavt device
  1456. * @reserved_op - is reserved operation
  1457. *
  1458. * This assumes the s_hlock is held but the s_last
  1459. * qp variable is uncontrolled.
  1460. *
  1461. * For non reserved operations, the qp->s_avail
  1462. * may be changed.
  1463. *
  1464. * The return value is zero or a -ENOMEM.
  1465. */
  1466. static inline int rvt_qp_is_avail(
  1467. struct rvt_qp *qp,
  1468. struct rvt_dev_info *rdi,
  1469. bool reserved_op)
  1470. {
  1471. u32 slast;
  1472. u32 avail;
  1473. u32 reserved_used;
  1474. /* see rvt_qp_wqe_unreserve() */
  1475. smp_mb__before_atomic();
  1476. reserved_used = atomic_read(&qp->s_reserved_used);
  1477. if (unlikely(reserved_op)) {
  1478. /* see rvt_qp_wqe_unreserve() */
  1479. smp_mb__before_atomic();
  1480. if (reserved_used >= rdi->dparms.reserved_operations)
  1481. return -ENOMEM;
  1482. return 0;
  1483. }
  1484. /* non-reserved operations */
  1485. if (likely(qp->s_avail))
  1486. return 0;
  1487. smp_read_barrier_depends(); /* see rc.c */
  1488. slast = READ_ONCE(qp->s_last);
  1489. if (qp->s_head >= slast)
  1490. avail = qp->s_size - (qp->s_head - slast);
  1491. else
  1492. avail = slast - qp->s_head;
  1493. /* see rvt_qp_wqe_unreserve() */
  1494. smp_mb__before_atomic();
  1495. reserved_used = atomic_read(&qp->s_reserved_used);
  1496. avail = avail - 1 -
  1497. (rdi->dparms.reserved_operations - reserved_used);
  1498. /* insure we don't assign a negative s_avail */
  1499. if ((s32)avail <= 0)
  1500. return -ENOMEM;
  1501. qp->s_avail = avail;
  1502. if (WARN_ON(qp->s_avail >
  1503. (qp->s_size - 1 - rdi->dparms.reserved_operations)))
  1504. rvt_pr_err(rdi,
  1505. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1506. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1507. qp->s_head, qp->s_tail, qp->s_cur,
  1508. qp->s_acked, qp->s_last);
  1509. return 0;
  1510. }
  1511. /**
  1512. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1513. * @qp: the QP to post on
  1514. * @wr: the work request to send
  1515. */
  1516. static int rvt_post_one_wr(struct rvt_qp *qp,
  1517. struct ib_send_wr *wr,
  1518. int *call_send)
  1519. {
  1520. struct rvt_swqe *wqe;
  1521. u32 next;
  1522. int i;
  1523. int j;
  1524. int acc;
  1525. struct rvt_lkey_table *rkt;
  1526. struct rvt_pd *pd;
  1527. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1528. u8 log_pmtu;
  1529. int ret;
  1530. size_t cplen;
  1531. bool reserved_op;
  1532. int local_ops_delayed = 0;
  1533. BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
  1534. /* IB spec says that num_sge == 0 is OK. */
  1535. if (unlikely(wr->num_sge > qp->s_max_sge))
  1536. return -EINVAL;
  1537. ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
  1538. if (ret < 0)
  1539. return ret;
  1540. cplen = ret;
  1541. /*
  1542. * Local operations include fast register and local invalidate.
  1543. * Fast register needs to be processed immediately because the
  1544. * registered lkey may be used by following work requests and the
  1545. * lkey needs to be valid at the time those requests are posted.
  1546. * Local invalidate can be processed immediately if fencing is
  1547. * not required and no previous local invalidate ops are pending.
  1548. * Signaled local operations that have been processed immediately
  1549. * need to have requests with "completion only" flags set posted
  1550. * to the send queue in order to generate completions.
  1551. */
  1552. if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
  1553. switch (wr->opcode) {
  1554. case IB_WR_REG_MR:
  1555. ret = rvt_fast_reg_mr(qp,
  1556. reg_wr(wr)->mr,
  1557. reg_wr(wr)->key,
  1558. reg_wr(wr)->access);
  1559. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1560. return ret;
  1561. break;
  1562. case IB_WR_LOCAL_INV:
  1563. if ((wr->send_flags & IB_SEND_FENCE) ||
  1564. atomic_read(&qp->local_ops_pending)) {
  1565. local_ops_delayed = 1;
  1566. } else {
  1567. ret = rvt_invalidate_rkey(
  1568. qp, wr->ex.invalidate_rkey);
  1569. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1570. return ret;
  1571. }
  1572. break;
  1573. default:
  1574. return -EINVAL;
  1575. }
  1576. }
  1577. reserved_op = rdi->post_parms[wr->opcode].flags &
  1578. RVT_OPERATION_USE_RESERVE;
  1579. /* check for avail */
  1580. ret = rvt_qp_is_avail(qp, rdi, reserved_op);
  1581. if (ret)
  1582. return ret;
  1583. next = qp->s_head + 1;
  1584. if (next >= qp->s_size)
  1585. next = 0;
  1586. rkt = &rdi->lkey_table;
  1587. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1588. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1589. /* cplen has length from above */
  1590. memcpy(&wqe->wr, wr, cplen);
  1591. wqe->length = 0;
  1592. j = 0;
  1593. if (wr->num_sge) {
  1594. struct rvt_sge *last_sge = NULL;
  1595. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1596. IB_ACCESS_LOCAL_WRITE : 0;
  1597. for (i = 0; i < wr->num_sge; i++) {
  1598. u32 length = wr->sg_list[i].length;
  1599. if (length == 0)
  1600. continue;
  1601. ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
  1602. &wr->sg_list[i], acc);
  1603. if (unlikely(ret < 0))
  1604. goto bail_inval_free;
  1605. wqe->length += length;
  1606. if (ret)
  1607. last_sge = &wqe->sg_list[j];
  1608. j += ret;
  1609. }
  1610. wqe->wr.num_sge = j;
  1611. }
  1612. /* general part of wqe valid - allow for driver checks */
  1613. if (rdi->driver_f.check_send_wqe) {
  1614. ret = rdi->driver_f.check_send_wqe(qp, wqe);
  1615. if (ret < 0)
  1616. goto bail_inval_free;
  1617. if (ret)
  1618. *call_send = ret;
  1619. }
  1620. log_pmtu = qp->log_pmtu;
  1621. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1622. qp->ibqp.qp_type != IB_QPT_RC) {
  1623. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1624. log_pmtu = ah->log_pmtu;
  1625. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1626. }
  1627. if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
  1628. if (local_ops_delayed)
  1629. atomic_inc(&qp->local_ops_pending);
  1630. else
  1631. wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
  1632. wqe->ssn = 0;
  1633. wqe->psn = 0;
  1634. wqe->lpsn = 0;
  1635. } else {
  1636. wqe->ssn = qp->s_ssn++;
  1637. wqe->psn = qp->s_next_psn;
  1638. wqe->lpsn = wqe->psn +
  1639. (wqe->length ?
  1640. ((wqe->length - 1) >> log_pmtu) :
  1641. 0);
  1642. qp->s_next_psn = wqe->lpsn + 1;
  1643. }
  1644. if (unlikely(reserved_op)) {
  1645. wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
  1646. rvt_qp_wqe_reserve(qp, wqe);
  1647. } else {
  1648. wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
  1649. qp->s_avail--;
  1650. }
  1651. trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
  1652. smp_wmb(); /* see request builders */
  1653. qp->s_head = next;
  1654. return 0;
  1655. bail_inval_free:
  1656. /* release mr holds */
  1657. while (j) {
  1658. struct rvt_sge *sge = &wqe->sg_list[--j];
  1659. rvt_put_mr(sge->mr);
  1660. }
  1661. return ret;
  1662. }
  1663. /**
  1664. * rvt_post_send - post a send on a QP
  1665. * @ibqp: the QP to post the send on
  1666. * @wr: the list of work requests to post
  1667. * @bad_wr: the first bad WR is put here
  1668. *
  1669. * This may be called from interrupt context.
  1670. *
  1671. * Return: 0 on success else errno
  1672. */
  1673. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1674. struct ib_send_wr **bad_wr)
  1675. {
  1676. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1677. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1678. unsigned long flags = 0;
  1679. int call_send;
  1680. unsigned nreq = 0;
  1681. int err = 0;
  1682. spin_lock_irqsave(&qp->s_hlock, flags);
  1683. /*
  1684. * Ensure QP state is such that we can send. If not bail out early,
  1685. * there is no need to do this every time we post a send.
  1686. */
  1687. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1688. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1689. return -EINVAL;
  1690. }
  1691. /*
  1692. * If the send queue is empty, and we only have a single WR then just go
  1693. * ahead and kick the send engine into gear. Otherwise we will always
  1694. * just schedule the send to happen later.
  1695. */
  1696. call_send = qp->s_head == READ_ONCE(qp->s_last) && !wr->next;
  1697. for (; wr; wr = wr->next) {
  1698. err = rvt_post_one_wr(qp, wr, &call_send);
  1699. if (unlikely(err)) {
  1700. *bad_wr = wr;
  1701. goto bail;
  1702. }
  1703. nreq++;
  1704. }
  1705. bail:
  1706. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1707. if (nreq) {
  1708. if (call_send)
  1709. rdi->driver_f.do_send(qp);
  1710. else
  1711. rdi->driver_f.schedule_send_no_lock(qp);
  1712. }
  1713. return err;
  1714. }
  1715. /**
  1716. * rvt_post_srq_receive - post a receive on a shared receive queue
  1717. * @ibsrq: the SRQ to post the receive on
  1718. * @wr: the list of work requests to post
  1719. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1720. *
  1721. * This may be called from interrupt context.
  1722. *
  1723. * Return: 0 on success else errno
  1724. */
  1725. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1726. struct ib_recv_wr **bad_wr)
  1727. {
  1728. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1729. struct rvt_rwq *wq;
  1730. unsigned long flags;
  1731. for (; wr; wr = wr->next) {
  1732. struct rvt_rwqe *wqe;
  1733. u32 next;
  1734. int i;
  1735. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1736. *bad_wr = wr;
  1737. return -EINVAL;
  1738. }
  1739. spin_lock_irqsave(&srq->rq.lock, flags);
  1740. wq = srq->rq.wq;
  1741. next = wq->head + 1;
  1742. if (next >= srq->rq.size)
  1743. next = 0;
  1744. if (next == wq->tail) {
  1745. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1746. *bad_wr = wr;
  1747. return -ENOMEM;
  1748. }
  1749. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1750. wqe->wr_id = wr->wr_id;
  1751. wqe->num_sge = wr->num_sge;
  1752. for (i = 0; i < wr->num_sge; i++)
  1753. wqe->sg_list[i] = wr->sg_list[i];
  1754. /* Make sure queue entry is written before the head index. */
  1755. smp_wmb();
  1756. wq->head = next;
  1757. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1758. }
  1759. return 0;
  1760. }
  1761. /**
  1762. * qp_comm_est - handle trap with QP established
  1763. * @qp: the QP
  1764. */
  1765. void rvt_comm_est(struct rvt_qp *qp)
  1766. {
  1767. qp->r_flags |= RVT_R_COMM_EST;
  1768. if (qp->ibqp.event_handler) {
  1769. struct ib_event ev;
  1770. ev.device = qp->ibqp.device;
  1771. ev.element.qp = &qp->ibqp;
  1772. ev.event = IB_EVENT_COMM_EST;
  1773. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1774. }
  1775. }
  1776. EXPORT_SYMBOL(rvt_comm_est);
  1777. void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
  1778. {
  1779. unsigned long flags;
  1780. int lastwqe;
  1781. spin_lock_irqsave(&qp->s_lock, flags);
  1782. lastwqe = rvt_error_qp(qp, err);
  1783. spin_unlock_irqrestore(&qp->s_lock, flags);
  1784. if (lastwqe) {
  1785. struct ib_event ev;
  1786. ev.device = qp->ibqp.device;
  1787. ev.element.qp = &qp->ibqp;
  1788. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1789. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1790. }
  1791. }
  1792. EXPORT_SYMBOL(rvt_rc_error);
  1793. /*
  1794. * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
  1795. * @index - the index
  1796. * return usec from an index into ib_rvt_rnr_table
  1797. */
  1798. unsigned long rvt_rnr_tbl_to_usec(u32 index)
  1799. {
  1800. return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
  1801. }
  1802. EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
  1803. static inline unsigned long rvt_aeth_to_usec(u32 aeth)
  1804. {
  1805. return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
  1806. IB_AETH_CREDIT_MASK];
  1807. }
  1808. /*
  1809. * rvt_add_retry_timer - add/start a retry timer
  1810. * @qp - the QP
  1811. * add a retry timer on the QP
  1812. */
  1813. void rvt_add_retry_timer(struct rvt_qp *qp)
  1814. {
  1815. struct ib_qp *ibqp = &qp->ibqp;
  1816. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1817. lockdep_assert_held(&qp->s_lock);
  1818. qp->s_flags |= RVT_S_TIMER;
  1819. /* 4.096 usec. * (1 << qp->timeout) */
  1820. qp->s_timer.expires = jiffies + qp->timeout_jiffies +
  1821. rdi->busy_jiffies;
  1822. add_timer(&qp->s_timer);
  1823. }
  1824. EXPORT_SYMBOL(rvt_add_retry_timer);
  1825. /**
  1826. * rvt_add_rnr_timer - add/start an rnr timer
  1827. * @qp - the QP
  1828. * @aeth - aeth of RNR timeout, simulated aeth for loopback
  1829. * add an rnr timer on the QP
  1830. */
  1831. void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
  1832. {
  1833. u32 to;
  1834. lockdep_assert_held(&qp->s_lock);
  1835. qp->s_flags |= RVT_S_WAIT_RNR;
  1836. to = rvt_aeth_to_usec(aeth);
  1837. hrtimer_start(&qp->s_rnr_timer,
  1838. ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
  1839. }
  1840. EXPORT_SYMBOL(rvt_add_rnr_timer);
  1841. /**
  1842. * rvt_stop_rc_timers - stop all timers
  1843. * @qp - the QP
  1844. * stop any pending timers
  1845. */
  1846. void rvt_stop_rc_timers(struct rvt_qp *qp)
  1847. {
  1848. lockdep_assert_held(&qp->s_lock);
  1849. /* Remove QP from all timers */
  1850. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  1851. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  1852. del_timer(&qp->s_timer);
  1853. hrtimer_try_to_cancel(&qp->s_rnr_timer);
  1854. }
  1855. }
  1856. EXPORT_SYMBOL(rvt_stop_rc_timers);
  1857. /**
  1858. * rvt_stop_rnr_timer - stop an rnr timer
  1859. * @qp - the QP
  1860. *
  1861. * stop an rnr timer and return if the timer
  1862. * had been pending.
  1863. */
  1864. static int rvt_stop_rnr_timer(struct rvt_qp *qp)
  1865. {
  1866. int rval = 0;
  1867. lockdep_assert_held(&qp->s_lock);
  1868. /* Remove QP from rnr timer */
  1869. if (qp->s_flags & RVT_S_WAIT_RNR) {
  1870. qp->s_flags &= ~RVT_S_WAIT_RNR;
  1871. rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
  1872. }
  1873. return rval;
  1874. }
  1875. /**
  1876. * rvt_del_timers_sync - wait for any timeout routines to exit
  1877. * @qp - the QP
  1878. */
  1879. void rvt_del_timers_sync(struct rvt_qp *qp)
  1880. {
  1881. del_timer_sync(&qp->s_timer);
  1882. hrtimer_cancel(&qp->s_rnr_timer);
  1883. }
  1884. EXPORT_SYMBOL(rvt_del_timers_sync);
  1885. /**
  1886. * This is called from s_timer for missing responses.
  1887. */
  1888. static void rvt_rc_timeout(struct timer_list *t)
  1889. {
  1890. struct rvt_qp *qp = from_timer(qp, t, s_timer);
  1891. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1892. unsigned long flags;
  1893. spin_lock_irqsave(&qp->r_lock, flags);
  1894. spin_lock(&qp->s_lock);
  1895. if (qp->s_flags & RVT_S_TIMER) {
  1896. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  1897. qp->s_flags &= ~RVT_S_TIMER;
  1898. rvp->n_rc_timeouts++;
  1899. del_timer(&qp->s_timer);
  1900. trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
  1901. if (rdi->driver_f.notify_restart_rc)
  1902. rdi->driver_f.notify_restart_rc(qp,
  1903. qp->s_last_psn + 1,
  1904. 1);
  1905. rdi->driver_f.schedule_send(qp);
  1906. }
  1907. spin_unlock(&qp->s_lock);
  1908. spin_unlock_irqrestore(&qp->r_lock, flags);
  1909. }
  1910. /*
  1911. * This is called from s_timer for RNR timeouts.
  1912. */
  1913. enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
  1914. {
  1915. struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
  1916. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1917. unsigned long flags;
  1918. spin_lock_irqsave(&qp->s_lock, flags);
  1919. rvt_stop_rnr_timer(qp);
  1920. rdi->driver_f.schedule_send(qp);
  1921. spin_unlock_irqrestore(&qp->s_lock, flags);
  1922. return HRTIMER_NORESTART;
  1923. }
  1924. EXPORT_SYMBOL(rvt_rc_rnr_retry);
  1925. /**
  1926. * rvt_qp_iter_init - initial for QP iteration
  1927. * @rdi - rvt devinfo
  1928. * @v - u64 value
  1929. *
  1930. * This returns an iterator suitable for iterating QPs
  1931. * in the system.
  1932. *
  1933. * The @cb is a user defined callback and @v is a 64
  1934. * bit value passed to and relevant for processing in the
  1935. * @cb. An example use case would be to alter QP processing
  1936. * based on criteria not part of the rvt_qp.
  1937. *
  1938. * Use cases that require memory allocation to succeed
  1939. * must preallocate appropriately.
  1940. *
  1941. * Return: a pointer to an rvt_qp_iter or NULL
  1942. */
  1943. struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
  1944. u64 v,
  1945. void (*cb)(struct rvt_qp *qp, u64 v))
  1946. {
  1947. struct rvt_qp_iter *i;
  1948. i = kzalloc(sizeof(*i), GFP_KERNEL);
  1949. if (!i)
  1950. return NULL;
  1951. i->rdi = rdi;
  1952. /* number of special QPs (SMI/GSI) for device */
  1953. i->specials = rdi->ibdev.phys_port_cnt * 2;
  1954. i->v = v;
  1955. i->cb = cb;
  1956. return i;
  1957. }
  1958. EXPORT_SYMBOL(rvt_qp_iter_init);
  1959. /**
  1960. * rvt_qp_iter_next - return the next QP in iter
  1961. * @iter - the iterator
  1962. *
  1963. * Fine grained QP iterator suitable for use
  1964. * with debugfs seq_file mechanisms.
  1965. *
  1966. * Updates iter->qp with the current QP when the return
  1967. * value is 0.
  1968. *
  1969. * Return: 0 - iter->qp is valid 1 - no more QPs
  1970. */
  1971. int rvt_qp_iter_next(struct rvt_qp_iter *iter)
  1972. __must_hold(RCU)
  1973. {
  1974. int n = iter->n;
  1975. int ret = 1;
  1976. struct rvt_qp *pqp = iter->qp;
  1977. struct rvt_qp *qp;
  1978. struct rvt_dev_info *rdi = iter->rdi;
  1979. /*
  1980. * The approach is to consider the special qps
  1981. * as additional table entries before the
  1982. * real hash table. Since the qp code sets
  1983. * the qp->next hash link to NULL, this works just fine.
  1984. *
  1985. * iter->specials is 2 * # ports
  1986. *
  1987. * n = 0..iter->specials is the special qp indices
  1988. *
  1989. * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
  1990. * the potential hash bucket entries
  1991. *
  1992. */
  1993. for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
  1994. if (pqp) {
  1995. qp = rcu_dereference(pqp->next);
  1996. } else {
  1997. if (n < iter->specials) {
  1998. struct rvt_ibport *rvp;
  1999. int pidx;
  2000. pidx = n % rdi->ibdev.phys_port_cnt;
  2001. rvp = rdi->ports[pidx];
  2002. qp = rcu_dereference(rvp->qp[n & 1]);
  2003. } else {
  2004. qp = rcu_dereference(
  2005. rdi->qp_dev->qp_table[
  2006. (n - iter->specials)]);
  2007. }
  2008. }
  2009. pqp = qp;
  2010. if (qp) {
  2011. iter->qp = qp;
  2012. iter->n = n;
  2013. return 0;
  2014. }
  2015. }
  2016. return ret;
  2017. }
  2018. EXPORT_SYMBOL(rvt_qp_iter_next);
  2019. /**
  2020. * rvt_qp_iter - iterate all QPs
  2021. * @rdi - rvt devinfo
  2022. * @v - a 64 bit value
  2023. * @cb - a callback
  2024. *
  2025. * This provides a way for iterating all QPs.
  2026. *
  2027. * The @cb is a user defined callback and @v is a 64
  2028. * bit value passed to and relevant for processing in the
  2029. * cb. An example use case would be to alter QP processing
  2030. * based on criteria not part of the rvt_qp.
  2031. *
  2032. * The code has an internal iterator to simplify
  2033. * non seq_file use cases.
  2034. */
  2035. void rvt_qp_iter(struct rvt_dev_info *rdi,
  2036. u64 v,
  2037. void (*cb)(struct rvt_qp *qp, u64 v))
  2038. {
  2039. int ret;
  2040. struct rvt_qp_iter i = {
  2041. .rdi = rdi,
  2042. .specials = rdi->ibdev.phys_port_cnt * 2,
  2043. .v = v,
  2044. .cb = cb
  2045. };
  2046. rcu_read_lock();
  2047. do {
  2048. ret = rvt_qp_iter_next(&i);
  2049. if (!ret) {
  2050. rvt_get_qp(i.qp);
  2051. rcu_read_unlock();
  2052. i.cb(i.qp, i.v);
  2053. rcu_read_lock();
  2054. rvt_put_qp(i.qp);
  2055. }
  2056. } while (!ret);
  2057. rcu_read_unlock();
  2058. }
  2059. EXPORT_SYMBOL(rvt_qp_iter);