qp.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  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. * @rdi: rvt device info structure
  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. slast = READ_ONCE(qp->s_last);
  1488. if (qp->s_head >= slast)
  1489. avail = qp->s_size - (qp->s_head - slast);
  1490. else
  1491. avail = slast - qp->s_head;
  1492. /* see rvt_qp_wqe_unreserve() */
  1493. smp_mb__before_atomic();
  1494. reserved_used = atomic_read(&qp->s_reserved_used);
  1495. avail = avail - 1 -
  1496. (rdi->dparms.reserved_operations - reserved_used);
  1497. /* insure we don't assign a negative s_avail */
  1498. if ((s32)avail <= 0)
  1499. return -ENOMEM;
  1500. qp->s_avail = avail;
  1501. if (WARN_ON(qp->s_avail >
  1502. (qp->s_size - 1 - rdi->dparms.reserved_operations)))
  1503. rvt_pr_err(rdi,
  1504. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1505. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1506. qp->s_head, qp->s_tail, qp->s_cur,
  1507. qp->s_acked, qp->s_last);
  1508. return 0;
  1509. }
  1510. /**
  1511. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1512. * @qp: the QP to post on
  1513. * @wr: the work request to send
  1514. */
  1515. static int rvt_post_one_wr(struct rvt_qp *qp,
  1516. struct ib_send_wr *wr,
  1517. int *call_send)
  1518. {
  1519. struct rvt_swqe *wqe;
  1520. u32 next;
  1521. int i;
  1522. int j;
  1523. int acc;
  1524. struct rvt_lkey_table *rkt;
  1525. struct rvt_pd *pd;
  1526. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1527. u8 log_pmtu;
  1528. int ret;
  1529. size_t cplen;
  1530. bool reserved_op;
  1531. int local_ops_delayed = 0;
  1532. BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
  1533. /* IB spec says that num_sge == 0 is OK. */
  1534. if (unlikely(wr->num_sge > qp->s_max_sge))
  1535. return -EINVAL;
  1536. ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
  1537. if (ret < 0)
  1538. return ret;
  1539. cplen = ret;
  1540. /*
  1541. * Local operations include fast register and local invalidate.
  1542. * Fast register needs to be processed immediately because the
  1543. * registered lkey may be used by following work requests and the
  1544. * lkey needs to be valid at the time those requests are posted.
  1545. * Local invalidate can be processed immediately if fencing is
  1546. * not required and no previous local invalidate ops are pending.
  1547. * Signaled local operations that have been processed immediately
  1548. * need to have requests with "completion only" flags set posted
  1549. * to the send queue in order to generate completions.
  1550. */
  1551. if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
  1552. switch (wr->opcode) {
  1553. case IB_WR_REG_MR:
  1554. ret = rvt_fast_reg_mr(qp,
  1555. reg_wr(wr)->mr,
  1556. reg_wr(wr)->key,
  1557. reg_wr(wr)->access);
  1558. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1559. return ret;
  1560. break;
  1561. case IB_WR_LOCAL_INV:
  1562. if ((wr->send_flags & IB_SEND_FENCE) ||
  1563. atomic_read(&qp->local_ops_pending)) {
  1564. local_ops_delayed = 1;
  1565. } else {
  1566. ret = rvt_invalidate_rkey(
  1567. qp, wr->ex.invalidate_rkey);
  1568. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1569. return ret;
  1570. }
  1571. break;
  1572. default:
  1573. return -EINVAL;
  1574. }
  1575. }
  1576. reserved_op = rdi->post_parms[wr->opcode].flags &
  1577. RVT_OPERATION_USE_RESERVE;
  1578. /* check for avail */
  1579. ret = rvt_qp_is_avail(qp, rdi, reserved_op);
  1580. if (ret)
  1581. return ret;
  1582. next = qp->s_head + 1;
  1583. if (next >= qp->s_size)
  1584. next = 0;
  1585. rkt = &rdi->lkey_table;
  1586. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1587. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1588. /* cplen has length from above */
  1589. memcpy(&wqe->wr, wr, cplen);
  1590. wqe->length = 0;
  1591. j = 0;
  1592. if (wr->num_sge) {
  1593. struct rvt_sge *last_sge = NULL;
  1594. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1595. IB_ACCESS_LOCAL_WRITE : 0;
  1596. for (i = 0; i < wr->num_sge; i++) {
  1597. u32 length = wr->sg_list[i].length;
  1598. if (length == 0)
  1599. continue;
  1600. ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
  1601. &wr->sg_list[i], acc);
  1602. if (unlikely(ret < 0))
  1603. goto bail_inval_free;
  1604. wqe->length += length;
  1605. if (ret)
  1606. last_sge = &wqe->sg_list[j];
  1607. j += ret;
  1608. }
  1609. wqe->wr.num_sge = j;
  1610. }
  1611. /* general part of wqe valid - allow for driver checks */
  1612. if (rdi->driver_f.check_send_wqe) {
  1613. ret = rdi->driver_f.check_send_wqe(qp, wqe);
  1614. if (ret < 0)
  1615. goto bail_inval_free;
  1616. if (ret)
  1617. *call_send = ret;
  1618. }
  1619. log_pmtu = qp->log_pmtu;
  1620. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1621. qp->ibqp.qp_type != IB_QPT_RC) {
  1622. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1623. log_pmtu = ah->log_pmtu;
  1624. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1625. }
  1626. if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
  1627. if (local_ops_delayed)
  1628. atomic_inc(&qp->local_ops_pending);
  1629. else
  1630. wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
  1631. wqe->ssn = 0;
  1632. wqe->psn = 0;
  1633. wqe->lpsn = 0;
  1634. } else {
  1635. wqe->ssn = qp->s_ssn++;
  1636. wqe->psn = qp->s_next_psn;
  1637. wqe->lpsn = wqe->psn +
  1638. (wqe->length ?
  1639. ((wqe->length - 1) >> log_pmtu) :
  1640. 0);
  1641. qp->s_next_psn = wqe->lpsn + 1;
  1642. }
  1643. if (unlikely(reserved_op)) {
  1644. wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
  1645. rvt_qp_wqe_reserve(qp, wqe);
  1646. } else {
  1647. wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
  1648. qp->s_avail--;
  1649. }
  1650. trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
  1651. smp_wmb(); /* see request builders */
  1652. qp->s_head = next;
  1653. return 0;
  1654. bail_inval_free:
  1655. /* release mr holds */
  1656. while (j) {
  1657. struct rvt_sge *sge = &wqe->sg_list[--j];
  1658. rvt_put_mr(sge->mr);
  1659. }
  1660. return ret;
  1661. }
  1662. /**
  1663. * rvt_post_send - post a send on a QP
  1664. * @ibqp: the QP to post the send on
  1665. * @wr: the list of work requests to post
  1666. * @bad_wr: the first bad WR is put here
  1667. *
  1668. * This may be called from interrupt context.
  1669. *
  1670. * Return: 0 on success else errno
  1671. */
  1672. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1673. struct ib_send_wr **bad_wr)
  1674. {
  1675. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1676. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1677. unsigned long flags = 0;
  1678. int call_send;
  1679. unsigned nreq = 0;
  1680. int err = 0;
  1681. spin_lock_irqsave(&qp->s_hlock, flags);
  1682. /*
  1683. * Ensure QP state is such that we can send. If not bail out early,
  1684. * there is no need to do this every time we post a send.
  1685. */
  1686. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1687. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1688. return -EINVAL;
  1689. }
  1690. /*
  1691. * If the send queue is empty, and we only have a single WR then just go
  1692. * ahead and kick the send engine into gear. Otherwise we will always
  1693. * just schedule the send to happen later.
  1694. */
  1695. call_send = qp->s_head == READ_ONCE(qp->s_last) && !wr->next;
  1696. for (; wr; wr = wr->next) {
  1697. err = rvt_post_one_wr(qp, wr, &call_send);
  1698. if (unlikely(err)) {
  1699. *bad_wr = wr;
  1700. goto bail;
  1701. }
  1702. nreq++;
  1703. }
  1704. bail:
  1705. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1706. if (nreq) {
  1707. if (call_send)
  1708. rdi->driver_f.do_send(qp);
  1709. else
  1710. rdi->driver_f.schedule_send_no_lock(qp);
  1711. }
  1712. return err;
  1713. }
  1714. /**
  1715. * rvt_post_srq_receive - post a receive on a shared receive queue
  1716. * @ibsrq: the SRQ to post the receive on
  1717. * @wr: the list of work requests to post
  1718. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1719. *
  1720. * This may be called from interrupt context.
  1721. *
  1722. * Return: 0 on success else errno
  1723. */
  1724. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1725. struct ib_recv_wr **bad_wr)
  1726. {
  1727. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1728. struct rvt_rwq *wq;
  1729. unsigned long flags;
  1730. for (; wr; wr = wr->next) {
  1731. struct rvt_rwqe *wqe;
  1732. u32 next;
  1733. int i;
  1734. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1735. *bad_wr = wr;
  1736. return -EINVAL;
  1737. }
  1738. spin_lock_irqsave(&srq->rq.lock, flags);
  1739. wq = srq->rq.wq;
  1740. next = wq->head + 1;
  1741. if (next >= srq->rq.size)
  1742. next = 0;
  1743. if (next == wq->tail) {
  1744. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1745. *bad_wr = wr;
  1746. return -ENOMEM;
  1747. }
  1748. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1749. wqe->wr_id = wr->wr_id;
  1750. wqe->num_sge = wr->num_sge;
  1751. for (i = 0; i < wr->num_sge; i++)
  1752. wqe->sg_list[i] = wr->sg_list[i];
  1753. /* Make sure queue entry is written before the head index. */
  1754. smp_wmb();
  1755. wq->head = next;
  1756. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1757. }
  1758. return 0;
  1759. }
  1760. /**
  1761. * qp_comm_est - handle trap with QP established
  1762. * @qp: the QP
  1763. */
  1764. void rvt_comm_est(struct rvt_qp *qp)
  1765. {
  1766. qp->r_flags |= RVT_R_COMM_EST;
  1767. if (qp->ibqp.event_handler) {
  1768. struct ib_event ev;
  1769. ev.device = qp->ibqp.device;
  1770. ev.element.qp = &qp->ibqp;
  1771. ev.event = IB_EVENT_COMM_EST;
  1772. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1773. }
  1774. }
  1775. EXPORT_SYMBOL(rvt_comm_est);
  1776. void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
  1777. {
  1778. unsigned long flags;
  1779. int lastwqe;
  1780. spin_lock_irqsave(&qp->s_lock, flags);
  1781. lastwqe = rvt_error_qp(qp, err);
  1782. spin_unlock_irqrestore(&qp->s_lock, flags);
  1783. if (lastwqe) {
  1784. struct ib_event ev;
  1785. ev.device = qp->ibqp.device;
  1786. ev.element.qp = &qp->ibqp;
  1787. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1788. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1789. }
  1790. }
  1791. EXPORT_SYMBOL(rvt_rc_error);
  1792. /*
  1793. * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
  1794. * @index - the index
  1795. * return usec from an index into ib_rvt_rnr_table
  1796. */
  1797. unsigned long rvt_rnr_tbl_to_usec(u32 index)
  1798. {
  1799. return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
  1800. }
  1801. EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
  1802. static inline unsigned long rvt_aeth_to_usec(u32 aeth)
  1803. {
  1804. return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
  1805. IB_AETH_CREDIT_MASK];
  1806. }
  1807. /*
  1808. * rvt_add_retry_timer - add/start a retry timer
  1809. * @qp - the QP
  1810. * add a retry timer on the QP
  1811. */
  1812. void rvt_add_retry_timer(struct rvt_qp *qp)
  1813. {
  1814. struct ib_qp *ibqp = &qp->ibqp;
  1815. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1816. lockdep_assert_held(&qp->s_lock);
  1817. qp->s_flags |= RVT_S_TIMER;
  1818. /* 4.096 usec. * (1 << qp->timeout) */
  1819. qp->s_timer.expires = jiffies + qp->timeout_jiffies +
  1820. rdi->busy_jiffies;
  1821. add_timer(&qp->s_timer);
  1822. }
  1823. EXPORT_SYMBOL(rvt_add_retry_timer);
  1824. /**
  1825. * rvt_add_rnr_timer - add/start an rnr timer
  1826. * @qp - the QP
  1827. * @aeth - aeth of RNR timeout, simulated aeth for loopback
  1828. * add an rnr timer on the QP
  1829. */
  1830. void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
  1831. {
  1832. u32 to;
  1833. lockdep_assert_held(&qp->s_lock);
  1834. qp->s_flags |= RVT_S_WAIT_RNR;
  1835. to = rvt_aeth_to_usec(aeth);
  1836. trace_rvt_rnrnak_add(qp, to);
  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 void rvt_stop_rnr_timer(struct rvt_qp *qp)
  1865. {
  1866. lockdep_assert_held(&qp->s_lock);
  1867. /* Remove QP from rnr timer */
  1868. if (qp->s_flags & RVT_S_WAIT_RNR) {
  1869. qp->s_flags &= ~RVT_S_WAIT_RNR;
  1870. trace_rvt_rnrnak_stop(qp, 0);
  1871. }
  1872. }
  1873. /**
  1874. * rvt_del_timers_sync - wait for any timeout routines to exit
  1875. * @qp - the QP
  1876. */
  1877. void rvt_del_timers_sync(struct rvt_qp *qp)
  1878. {
  1879. del_timer_sync(&qp->s_timer);
  1880. hrtimer_cancel(&qp->s_rnr_timer);
  1881. }
  1882. EXPORT_SYMBOL(rvt_del_timers_sync);
  1883. /**
  1884. * This is called from s_timer for missing responses.
  1885. */
  1886. static void rvt_rc_timeout(struct timer_list *t)
  1887. {
  1888. struct rvt_qp *qp = from_timer(qp, t, s_timer);
  1889. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1890. unsigned long flags;
  1891. spin_lock_irqsave(&qp->r_lock, flags);
  1892. spin_lock(&qp->s_lock);
  1893. if (qp->s_flags & RVT_S_TIMER) {
  1894. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  1895. qp->s_flags &= ~RVT_S_TIMER;
  1896. rvp->n_rc_timeouts++;
  1897. del_timer(&qp->s_timer);
  1898. trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
  1899. if (rdi->driver_f.notify_restart_rc)
  1900. rdi->driver_f.notify_restart_rc(qp,
  1901. qp->s_last_psn + 1,
  1902. 1);
  1903. rdi->driver_f.schedule_send(qp);
  1904. }
  1905. spin_unlock(&qp->s_lock);
  1906. spin_unlock_irqrestore(&qp->r_lock, flags);
  1907. }
  1908. /*
  1909. * This is called from s_timer for RNR timeouts.
  1910. */
  1911. enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
  1912. {
  1913. struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
  1914. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1915. unsigned long flags;
  1916. spin_lock_irqsave(&qp->s_lock, flags);
  1917. rvt_stop_rnr_timer(qp);
  1918. trace_rvt_rnrnak_timeout(qp, 0);
  1919. rdi->driver_f.schedule_send(qp);
  1920. spin_unlock_irqrestore(&qp->s_lock, flags);
  1921. return HRTIMER_NORESTART;
  1922. }
  1923. EXPORT_SYMBOL(rvt_rc_rnr_retry);
  1924. /**
  1925. * rvt_qp_iter_init - initial for QP iteration
  1926. * @rdi: rvt devinfo
  1927. * @v: u64 value
  1928. *
  1929. * This returns an iterator suitable for iterating QPs
  1930. * in the system.
  1931. *
  1932. * The @cb is a user defined callback and @v is a 64
  1933. * bit value passed to and relevant for processing in the
  1934. * @cb. An example use case would be to alter QP processing
  1935. * based on criteria not part of the rvt_qp.
  1936. *
  1937. * Use cases that require memory allocation to succeed
  1938. * must preallocate appropriately.
  1939. *
  1940. * Return: a pointer to an rvt_qp_iter or NULL
  1941. */
  1942. struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
  1943. u64 v,
  1944. void (*cb)(struct rvt_qp *qp, u64 v))
  1945. {
  1946. struct rvt_qp_iter *i;
  1947. i = kzalloc(sizeof(*i), GFP_KERNEL);
  1948. if (!i)
  1949. return NULL;
  1950. i->rdi = rdi;
  1951. /* number of special QPs (SMI/GSI) for device */
  1952. i->specials = rdi->ibdev.phys_port_cnt * 2;
  1953. i->v = v;
  1954. i->cb = cb;
  1955. return i;
  1956. }
  1957. EXPORT_SYMBOL(rvt_qp_iter_init);
  1958. /**
  1959. * rvt_qp_iter_next - return the next QP in iter
  1960. * @iter - the iterator
  1961. *
  1962. * Fine grained QP iterator suitable for use
  1963. * with debugfs seq_file mechanisms.
  1964. *
  1965. * Updates iter->qp with the current QP when the return
  1966. * value is 0.
  1967. *
  1968. * Return: 0 - iter->qp is valid 1 - no more QPs
  1969. */
  1970. int rvt_qp_iter_next(struct rvt_qp_iter *iter)
  1971. __must_hold(RCU)
  1972. {
  1973. int n = iter->n;
  1974. int ret = 1;
  1975. struct rvt_qp *pqp = iter->qp;
  1976. struct rvt_qp *qp;
  1977. struct rvt_dev_info *rdi = iter->rdi;
  1978. /*
  1979. * The approach is to consider the special qps
  1980. * as additional table entries before the
  1981. * real hash table. Since the qp code sets
  1982. * the qp->next hash link to NULL, this works just fine.
  1983. *
  1984. * iter->specials is 2 * # ports
  1985. *
  1986. * n = 0..iter->specials is the special qp indices
  1987. *
  1988. * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
  1989. * the potential hash bucket entries
  1990. *
  1991. */
  1992. for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
  1993. if (pqp) {
  1994. qp = rcu_dereference(pqp->next);
  1995. } else {
  1996. if (n < iter->specials) {
  1997. struct rvt_ibport *rvp;
  1998. int pidx;
  1999. pidx = n % rdi->ibdev.phys_port_cnt;
  2000. rvp = rdi->ports[pidx];
  2001. qp = rcu_dereference(rvp->qp[n & 1]);
  2002. } else {
  2003. qp = rcu_dereference(
  2004. rdi->qp_dev->qp_table[
  2005. (n - iter->specials)]);
  2006. }
  2007. }
  2008. pqp = qp;
  2009. if (qp) {
  2010. iter->qp = qp;
  2011. iter->n = n;
  2012. return 0;
  2013. }
  2014. }
  2015. return ret;
  2016. }
  2017. EXPORT_SYMBOL(rvt_qp_iter_next);
  2018. /**
  2019. * rvt_qp_iter - iterate all QPs
  2020. * @rdi - rvt devinfo
  2021. * @v - a 64 bit value
  2022. * @cb - a callback
  2023. *
  2024. * This provides a way for iterating all QPs.
  2025. *
  2026. * The @cb is a user defined callback and @v is a 64
  2027. * bit value passed to and relevant for processing in the
  2028. * cb. An example use case would be to alter QP processing
  2029. * based on criteria not part of the rvt_qp.
  2030. *
  2031. * The code has an internal iterator to simplify
  2032. * non seq_file use cases.
  2033. */
  2034. void rvt_qp_iter(struct rvt_dev_info *rdi,
  2035. u64 v,
  2036. void (*cb)(struct rvt_qp *qp, u64 v))
  2037. {
  2038. int ret;
  2039. struct rvt_qp_iter i = {
  2040. .rdi = rdi,
  2041. .specials = rdi->ibdev.phys_port_cnt * 2,
  2042. .v = v,
  2043. .cb = cb
  2044. };
  2045. rcu_read_lock();
  2046. do {
  2047. ret = rvt_qp_iter_next(&i);
  2048. if (!ret) {
  2049. rvt_get_qp(i.qp);
  2050. rcu_read_unlock();
  2051. i.cb(i.qp, i.v);
  2052. rcu_read_lock();
  2053. rvt_put_qp(i.qp);
  2054. }
  2055. } while (!ret);
  2056. rcu_read_unlock();
  2057. }
  2058. EXPORT_SYMBOL(rvt_qp_iter);