qp.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  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(unsigned long arg);
  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_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. wait_event(qp->wait, !atomic_read(&qp->refcount));
  640. /* grab the lock b/c it was locked at call time */
  641. spin_lock_irq(&qp->r_lock);
  642. spin_lock(&qp->s_hlock);
  643. spin_lock(&qp->s_lock);
  644. rvt_clear_mr_refs(qp, 1);
  645. /*
  646. * Let the driver do any tear down or re-init it needs to for
  647. * a qp that has been reset
  648. */
  649. rdi->driver_f.notify_qp_reset(qp);
  650. }
  651. rvt_init_qp(rdi, qp, type);
  652. lockdep_assert_held(&qp->r_lock);
  653. lockdep_assert_held(&qp->s_hlock);
  654. lockdep_assert_held(&qp->s_lock);
  655. }
  656. /** rvt_free_qpn - Free a qpn from the bit map
  657. * @qpt: QP table
  658. * @qpn: queue pair number to free
  659. */
  660. static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  661. {
  662. struct rvt_qpn_map *map;
  663. map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
  664. if (map->page)
  665. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  666. }
  667. /**
  668. * rvt_create_qp - create a queue pair for a device
  669. * @ibpd: the protection domain who's device we create the queue pair for
  670. * @init_attr: the attributes of the queue pair
  671. * @udata: user data for libibverbs.so
  672. *
  673. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  674. * unique idea of what queue pair numbers mean. For instance there is a reserved
  675. * range for PSM.
  676. *
  677. * Return: the queue pair on success, otherwise returns an errno.
  678. *
  679. * Called by the ib_create_qp() core verbs function.
  680. */
  681. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  682. struct ib_qp_init_attr *init_attr,
  683. struct ib_udata *udata)
  684. {
  685. struct rvt_qp *qp;
  686. int err;
  687. struct rvt_swqe *swq = NULL;
  688. size_t sz;
  689. size_t sg_list_sz;
  690. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  691. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  692. void *priv = NULL;
  693. size_t sqsize;
  694. if (!rdi)
  695. return ERR_PTR(-EINVAL);
  696. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
  697. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  698. init_attr->create_flags)
  699. return ERR_PTR(-EINVAL);
  700. /* Check receive queue parameters if no SRQ is specified. */
  701. if (!init_attr->srq) {
  702. if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
  703. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  704. return ERR_PTR(-EINVAL);
  705. if (init_attr->cap.max_send_sge +
  706. init_attr->cap.max_send_wr +
  707. init_attr->cap.max_recv_sge +
  708. init_attr->cap.max_recv_wr == 0)
  709. return ERR_PTR(-EINVAL);
  710. }
  711. sqsize =
  712. init_attr->cap.max_send_wr + 1 +
  713. rdi->dparms.reserved_operations;
  714. switch (init_attr->qp_type) {
  715. case IB_QPT_SMI:
  716. case IB_QPT_GSI:
  717. if (init_attr->port_num == 0 ||
  718. init_attr->port_num > ibpd->device->phys_port_cnt)
  719. return ERR_PTR(-EINVAL);
  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. setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
  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_timer(&qp->s_timer);
  803. qp->s_timer.data = (unsigned long)qp;
  804. INIT_LIST_HEAD(&qp->rspwait);
  805. qp->state = IB_QPS_RESET;
  806. qp->s_wq = swq;
  807. qp->s_size = sqsize;
  808. qp->s_avail = init_attr->cap.max_send_wr;
  809. qp->s_max_sge = init_attr->cap.max_send_sge;
  810. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  811. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  812. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  813. init_attr->qp_type,
  814. init_attr->port_num);
  815. if (err < 0) {
  816. ret = ERR_PTR(err);
  817. goto bail_rq_wq;
  818. }
  819. qp->ibqp.qp_num = err;
  820. qp->port_num = init_attr->port_num;
  821. rvt_init_qp(rdi, qp, init_attr->qp_type);
  822. break;
  823. default:
  824. /* Don't support raw QPs */
  825. return ERR_PTR(-EINVAL);
  826. }
  827. init_attr->cap.max_inline_data = 0;
  828. /*
  829. * Return the address of the RWQ as the offset to mmap.
  830. * See rvt_mmap() for details.
  831. */
  832. if (udata && udata->outlen >= sizeof(__u64)) {
  833. if (!qp->r_rq.wq) {
  834. __u64 offset = 0;
  835. err = ib_copy_to_udata(udata, &offset,
  836. sizeof(offset));
  837. if (err) {
  838. ret = ERR_PTR(err);
  839. goto bail_qpn;
  840. }
  841. } else {
  842. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  843. qp->ip = rvt_create_mmap_info(rdi, s,
  844. ibpd->uobject->context,
  845. qp->r_rq.wq);
  846. if (!qp->ip) {
  847. ret = ERR_PTR(-ENOMEM);
  848. goto bail_qpn;
  849. }
  850. err = ib_copy_to_udata(udata, &qp->ip->offset,
  851. sizeof(qp->ip->offset));
  852. if (err) {
  853. ret = ERR_PTR(err);
  854. goto bail_ip;
  855. }
  856. }
  857. qp->pid = current->pid;
  858. }
  859. spin_lock(&rdi->n_qps_lock);
  860. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  861. spin_unlock(&rdi->n_qps_lock);
  862. ret = ERR_PTR(-ENOMEM);
  863. goto bail_ip;
  864. }
  865. rdi->n_qps_allocated++;
  866. /*
  867. * Maintain a busy_jiffies variable that will be added to the timeout
  868. * period in mod_retry_timer and add_retry_timer. This busy jiffies
  869. * is scaled by the number of rc qps created for the device to reduce
  870. * the number of timeouts occurring when there is a large number of
  871. * qps. busy_jiffies is incremented every rc qp scaling interval.
  872. * The scaling interval is selected based on extensive performance
  873. * evaluation of targeted workloads.
  874. */
  875. if (init_attr->qp_type == IB_QPT_RC) {
  876. rdi->n_rc_qps++;
  877. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  878. }
  879. spin_unlock(&rdi->n_qps_lock);
  880. if (qp->ip) {
  881. spin_lock_irq(&rdi->pending_lock);
  882. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  883. spin_unlock_irq(&rdi->pending_lock);
  884. }
  885. ret = &qp->ibqp;
  886. /*
  887. * We have our QP and its good, now keep track of what types of opcodes
  888. * can be processed on this QP. We do this by keeping track of what the
  889. * 3 high order bits of the opcode are.
  890. */
  891. switch (init_attr->qp_type) {
  892. case IB_QPT_SMI:
  893. case IB_QPT_GSI:
  894. case IB_QPT_UD:
  895. qp->allowed_ops = IB_OPCODE_UD;
  896. break;
  897. case IB_QPT_RC:
  898. qp->allowed_ops = IB_OPCODE_RC;
  899. break;
  900. case IB_QPT_UC:
  901. qp->allowed_ops = IB_OPCODE_UC;
  902. break;
  903. default:
  904. ret = ERR_PTR(-EINVAL);
  905. goto bail_ip;
  906. }
  907. return ret;
  908. bail_ip:
  909. if (qp->ip)
  910. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  911. bail_qpn:
  912. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  913. bail_rq_wq:
  914. if (!qp->ip)
  915. vfree(qp->r_rq.wq);
  916. bail_driver_priv:
  917. rdi->driver_f.qp_priv_free(rdi, qp);
  918. bail_qp:
  919. kfree(qp->s_ack_queue);
  920. kfree(qp);
  921. bail_swq:
  922. vfree(swq);
  923. return ret;
  924. }
  925. /**
  926. * rvt_error_qp - put a QP into the error state
  927. * @qp: the QP to put into the error state
  928. * @err: the receive completion error to signal if a RWQE is active
  929. *
  930. * Flushes both send and receive work queues.
  931. *
  932. * Return: true if last WQE event should be generated.
  933. * The QP r_lock and s_lock should be held and interrupts disabled.
  934. * If we are already in error state, just return.
  935. */
  936. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  937. {
  938. struct ib_wc wc;
  939. int ret = 0;
  940. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  941. lockdep_assert_held(&qp->r_lock);
  942. lockdep_assert_held(&qp->s_lock);
  943. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  944. goto bail;
  945. qp->state = IB_QPS_ERR;
  946. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  947. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  948. del_timer(&qp->s_timer);
  949. }
  950. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  951. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  952. rdi->driver_f.notify_error_qp(qp);
  953. /* Schedule the sending tasklet to drain the send work queue. */
  954. if (ACCESS_ONCE(qp->s_last) != qp->s_head)
  955. rdi->driver_f.schedule_send(qp);
  956. rvt_clear_mr_refs(qp, 0);
  957. memset(&wc, 0, sizeof(wc));
  958. wc.qp = &qp->ibqp;
  959. wc.opcode = IB_WC_RECV;
  960. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  961. wc.wr_id = qp->r_wr_id;
  962. wc.status = err;
  963. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  964. }
  965. wc.status = IB_WC_WR_FLUSH_ERR;
  966. if (qp->r_rq.wq) {
  967. struct rvt_rwq *wq;
  968. u32 head;
  969. u32 tail;
  970. spin_lock(&qp->r_rq.lock);
  971. /* sanity check pointers before trusting them */
  972. wq = qp->r_rq.wq;
  973. head = wq->head;
  974. if (head >= qp->r_rq.size)
  975. head = 0;
  976. tail = wq->tail;
  977. if (tail >= qp->r_rq.size)
  978. tail = 0;
  979. while (tail != head) {
  980. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  981. if (++tail >= qp->r_rq.size)
  982. tail = 0;
  983. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  984. }
  985. wq->tail = tail;
  986. spin_unlock(&qp->r_rq.lock);
  987. } else if (qp->ibqp.event_handler) {
  988. ret = 1;
  989. }
  990. bail:
  991. return ret;
  992. }
  993. EXPORT_SYMBOL(rvt_error_qp);
  994. /*
  995. * Put the QP into the hash table.
  996. * The hash table holds a reference to the QP.
  997. */
  998. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  999. {
  1000. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  1001. unsigned long flags;
  1002. rvt_get_qp(qp);
  1003. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  1004. if (qp->ibqp.qp_num <= 1) {
  1005. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  1006. } else {
  1007. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  1008. qp->next = rdi->qp_dev->qp_table[n];
  1009. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  1010. trace_rvt_qpinsert(qp, n);
  1011. }
  1012. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  1013. }
  1014. /**
  1015. * rvt_modify_qp - modify the attributes of a queue pair
  1016. * @ibqp: the queue pair who's attributes we're modifying
  1017. * @attr: the new attributes
  1018. * @attr_mask: the mask of attributes to modify
  1019. * @udata: user data for libibverbs.so
  1020. *
  1021. * Return: 0 on success, otherwise returns an errno.
  1022. */
  1023. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1024. int attr_mask, struct ib_udata *udata)
  1025. {
  1026. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1027. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1028. enum ib_qp_state cur_state, new_state;
  1029. struct ib_event ev;
  1030. int lastwqe = 0;
  1031. int mig = 0;
  1032. int pmtu = 0; /* for gcc warning only */
  1033. enum rdma_link_layer link;
  1034. int opa_ah;
  1035. link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
  1036. spin_lock_irq(&qp->r_lock);
  1037. spin_lock(&qp->s_hlock);
  1038. spin_lock(&qp->s_lock);
  1039. cur_state = attr_mask & IB_QP_CUR_STATE ?
  1040. attr->cur_qp_state : qp->state;
  1041. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1042. opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
  1043. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  1044. attr_mask, link))
  1045. goto inval;
  1046. if (rdi->driver_f.check_modify_qp &&
  1047. rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
  1048. goto inval;
  1049. if (attr_mask & IB_QP_AV) {
  1050. if (opa_ah) {
  1051. if (rdma_ah_get_dlid(&attr->ah_attr) >=
  1052. opa_get_mcast_base(OPA_MCAST_NR))
  1053. goto inval;
  1054. } else {
  1055. if (rdma_ah_get_dlid(&attr->ah_attr) >=
  1056. be16_to_cpu(IB_MULTICAST_LID_BASE))
  1057. goto inval;
  1058. }
  1059. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  1060. goto inval;
  1061. }
  1062. if (attr_mask & IB_QP_ALT_PATH) {
  1063. if (opa_ah) {
  1064. if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
  1065. opa_get_mcast_base(OPA_MCAST_NR))
  1066. goto inval;
  1067. } else {
  1068. if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
  1069. be16_to_cpu(IB_MULTICAST_LID_BASE))
  1070. goto inval;
  1071. }
  1072. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  1073. goto inval;
  1074. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  1075. goto inval;
  1076. }
  1077. if (attr_mask & IB_QP_PKEY_INDEX)
  1078. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  1079. goto inval;
  1080. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1081. if (attr->min_rnr_timer > 31)
  1082. goto inval;
  1083. if (attr_mask & IB_QP_PORT)
  1084. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  1085. qp->ibqp.qp_type == IB_QPT_GSI ||
  1086. attr->port_num == 0 ||
  1087. attr->port_num > ibqp->device->phys_port_cnt)
  1088. goto inval;
  1089. if (attr_mask & IB_QP_DEST_QPN)
  1090. if (attr->dest_qp_num > RVT_QPN_MASK)
  1091. goto inval;
  1092. if (attr_mask & IB_QP_RETRY_CNT)
  1093. if (attr->retry_cnt > 7)
  1094. goto inval;
  1095. if (attr_mask & IB_QP_RNR_RETRY)
  1096. if (attr->rnr_retry > 7)
  1097. goto inval;
  1098. /*
  1099. * Don't allow invalid path_mtu values. OK to set greater
  1100. * than the active mtu (or even the max_cap, if we have tuned
  1101. * that to a small mtu. We'll set qp->path_mtu
  1102. * to the lesser of requested attribute mtu and active,
  1103. * for packetizing messages.
  1104. * Note that the QP port has to be set in INIT and MTU in RTR.
  1105. */
  1106. if (attr_mask & IB_QP_PATH_MTU) {
  1107. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  1108. if (pmtu < 0)
  1109. goto inval;
  1110. }
  1111. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1112. if (attr->path_mig_state == IB_MIG_REARM) {
  1113. if (qp->s_mig_state == IB_MIG_ARMED)
  1114. goto inval;
  1115. if (new_state != IB_QPS_RTS)
  1116. goto inval;
  1117. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  1118. if (qp->s_mig_state == IB_MIG_REARM)
  1119. goto inval;
  1120. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  1121. goto inval;
  1122. if (qp->s_mig_state == IB_MIG_ARMED)
  1123. mig = 1;
  1124. } else {
  1125. goto inval;
  1126. }
  1127. }
  1128. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1129. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  1130. goto inval;
  1131. switch (new_state) {
  1132. case IB_QPS_RESET:
  1133. if (qp->state != IB_QPS_RESET)
  1134. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1135. break;
  1136. case IB_QPS_RTR:
  1137. /* Allow event to re-trigger if QP set to RTR more than once */
  1138. qp->r_flags &= ~RVT_R_COMM_EST;
  1139. qp->state = new_state;
  1140. break;
  1141. case IB_QPS_SQD:
  1142. qp->s_draining = qp->s_last != qp->s_cur;
  1143. qp->state = new_state;
  1144. break;
  1145. case IB_QPS_SQE:
  1146. if (qp->ibqp.qp_type == IB_QPT_RC)
  1147. goto inval;
  1148. qp->state = new_state;
  1149. break;
  1150. case IB_QPS_ERR:
  1151. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  1152. break;
  1153. default:
  1154. qp->state = new_state;
  1155. break;
  1156. }
  1157. if (attr_mask & IB_QP_PKEY_INDEX)
  1158. qp->s_pkey_index = attr->pkey_index;
  1159. if (attr_mask & IB_QP_PORT)
  1160. qp->port_num = attr->port_num;
  1161. if (attr_mask & IB_QP_DEST_QPN)
  1162. qp->remote_qpn = attr->dest_qp_num;
  1163. if (attr_mask & IB_QP_SQ_PSN) {
  1164. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  1165. qp->s_psn = qp->s_next_psn;
  1166. qp->s_sending_psn = qp->s_next_psn;
  1167. qp->s_last_psn = qp->s_next_psn - 1;
  1168. qp->s_sending_hpsn = qp->s_last_psn;
  1169. }
  1170. if (attr_mask & IB_QP_RQ_PSN)
  1171. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  1172. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1173. qp->qp_access_flags = attr->qp_access_flags;
  1174. if (attr_mask & IB_QP_AV) {
  1175. qp->remote_ah_attr = attr->ah_attr;
  1176. qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
  1177. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  1178. }
  1179. if (attr_mask & IB_QP_ALT_PATH) {
  1180. qp->alt_ah_attr = attr->alt_ah_attr;
  1181. qp->s_alt_pkey_index = attr->alt_pkey_index;
  1182. }
  1183. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1184. qp->s_mig_state = attr->path_mig_state;
  1185. if (mig) {
  1186. qp->remote_ah_attr = qp->alt_ah_attr;
  1187. qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
  1188. qp->s_pkey_index = qp->s_alt_pkey_index;
  1189. }
  1190. }
  1191. if (attr_mask & IB_QP_PATH_MTU) {
  1192. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  1193. qp->log_pmtu = ilog2(qp->pmtu);
  1194. }
  1195. if (attr_mask & IB_QP_RETRY_CNT) {
  1196. qp->s_retry_cnt = attr->retry_cnt;
  1197. qp->s_retry = attr->retry_cnt;
  1198. }
  1199. if (attr_mask & IB_QP_RNR_RETRY) {
  1200. qp->s_rnr_retry_cnt = attr->rnr_retry;
  1201. qp->s_rnr_retry = attr->rnr_retry;
  1202. }
  1203. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1204. qp->r_min_rnr_timer = attr->min_rnr_timer;
  1205. if (attr_mask & IB_QP_TIMEOUT) {
  1206. qp->timeout = attr->timeout;
  1207. qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
  1208. }
  1209. if (attr_mask & IB_QP_QKEY)
  1210. qp->qkey = attr->qkey;
  1211. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1212. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  1213. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1214. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1215. if (rdi->driver_f.modify_qp)
  1216. rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
  1217. spin_unlock(&qp->s_lock);
  1218. spin_unlock(&qp->s_hlock);
  1219. spin_unlock_irq(&qp->r_lock);
  1220. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1221. rvt_insert_qp(rdi, qp);
  1222. if (lastwqe) {
  1223. ev.device = qp->ibqp.device;
  1224. ev.element.qp = &qp->ibqp;
  1225. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1226. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1227. }
  1228. if (mig) {
  1229. ev.device = qp->ibqp.device;
  1230. ev.element.qp = &qp->ibqp;
  1231. ev.event = IB_EVENT_PATH_MIG;
  1232. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1233. }
  1234. return 0;
  1235. inval:
  1236. spin_unlock(&qp->s_lock);
  1237. spin_unlock(&qp->s_hlock);
  1238. spin_unlock_irq(&qp->r_lock);
  1239. return -EINVAL;
  1240. }
  1241. /**
  1242. * rvt_destroy_qp - destroy a queue pair
  1243. * @ibqp: the queue pair to destroy
  1244. *
  1245. * Note that this can be called while the QP is actively sending or
  1246. * receiving!
  1247. *
  1248. * Return: 0 on success.
  1249. */
  1250. int rvt_destroy_qp(struct ib_qp *ibqp)
  1251. {
  1252. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1253. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1254. spin_lock_irq(&qp->r_lock);
  1255. spin_lock(&qp->s_hlock);
  1256. spin_lock(&qp->s_lock);
  1257. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1258. spin_unlock(&qp->s_lock);
  1259. spin_unlock(&qp->s_hlock);
  1260. spin_unlock_irq(&qp->r_lock);
  1261. /* qpn is now available for use again */
  1262. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1263. spin_lock(&rdi->n_qps_lock);
  1264. rdi->n_qps_allocated--;
  1265. if (qp->ibqp.qp_type == IB_QPT_RC) {
  1266. rdi->n_rc_qps--;
  1267. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1268. }
  1269. spin_unlock(&rdi->n_qps_lock);
  1270. if (qp->ip)
  1271. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1272. else
  1273. vfree(qp->r_rq.wq);
  1274. vfree(qp->s_wq);
  1275. rdi->driver_f.qp_priv_free(rdi, qp);
  1276. kfree(qp->s_ack_queue);
  1277. kfree(qp);
  1278. return 0;
  1279. }
  1280. /**
  1281. * rvt_query_qp - query an ipbq
  1282. * @ibqp: IB qp to query
  1283. * @attr: attr struct to fill in
  1284. * @attr_mask: attr mask ignored
  1285. * @init_attr: struct to fill in
  1286. *
  1287. * Return: always 0
  1288. */
  1289. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1290. int attr_mask, struct ib_qp_init_attr *init_attr)
  1291. {
  1292. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1293. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1294. attr->qp_state = qp->state;
  1295. attr->cur_qp_state = attr->qp_state;
  1296. attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  1297. attr->path_mig_state = qp->s_mig_state;
  1298. attr->qkey = qp->qkey;
  1299. attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
  1300. attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
  1301. attr->dest_qp_num = qp->remote_qpn;
  1302. attr->qp_access_flags = qp->qp_access_flags;
  1303. attr->cap.max_send_wr = qp->s_size - 1 -
  1304. rdi->dparms.reserved_operations;
  1305. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  1306. attr->cap.max_send_sge = qp->s_max_sge;
  1307. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  1308. attr->cap.max_inline_data = 0;
  1309. attr->ah_attr = qp->remote_ah_attr;
  1310. attr->alt_ah_attr = qp->alt_ah_attr;
  1311. attr->pkey_index = qp->s_pkey_index;
  1312. attr->alt_pkey_index = qp->s_alt_pkey_index;
  1313. attr->en_sqd_async_notify = 0;
  1314. attr->sq_draining = qp->s_draining;
  1315. attr->max_rd_atomic = qp->s_max_rd_atomic;
  1316. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  1317. attr->min_rnr_timer = qp->r_min_rnr_timer;
  1318. attr->port_num = qp->port_num;
  1319. attr->timeout = qp->timeout;
  1320. attr->retry_cnt = qp->s_retry_cnt;
  1321. attr->rnr_retry = qp->s_rnr_retry_cnt;
  1322. attr->alt_port_num =
  1323. rdma_ah_get_port_num(&qp->alt_ah_attr);
  1324. attr->alt_timeout = qp->alt_timeout;
  1325. init_attr->event_handler = qp->ibqp.event_handler;
  1326. init_attr->qp_context = qp->ibqp.qp_context;
  1327. init_attr->send_cq = qp->ibqp.send_cq;
  1328. init_attr->recv_cq = qp->ibqp.recv_cq;
  1329. init_attr->srq = qp->ibqp.srq;
  1330. init_attr->cap = attr->cap;
  1331. if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
  1332. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  1333. else
  1334. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  1335. init_attr->qp_type = qp->ibqp.qp_type;
  1336. init_attr->port_num = qp->port_num;
  1337. return 0;
  1338. }
  1339. /**
  1340. * rvt_post_receive - post a receive on a QP
  1341. * @ibqp: the QP to post the receive on
  1342. * @wr: the WR to post
  1343. * @bad_wr: the first bad WR is put here
  1344. *
  1345. * This may be called from interrupt context.
  1346. *
  1347. * Return: 0 on success otherwise errno
  1348. */
  1349. int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1350. struct ib_recv_wr **bad_wr)
  1351. {
  1352. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1353. struct rvt_rwq *wq = qp->r_rq.wq;
  1354. unsigned long flags;
  1355. int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
  1356. !qp->ibqp.srq;
  1357. /* Check that state is OK to post receive. */
  1358. if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
  1359. *bad_wr = wr;
  1360. return -EINVAL;
  1361. }
  1362. for (; wr; wr = wr->next) {
  1363. struct rvt_rwqe *wqe;
  1364. u32 next;
  1365. int i;
  1366. if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
  1367. *bad_wr = wr;
  1368. return -EINVAL;
  1369. }
  1370. spin_lock_irqsave(&qp->r_rq.lock, flags);
  1371. next = wq->head + 1;
  1372. if (next >= qp->r_rq.size)
  1373. next = 0;
  1374. if (next == wq->tail) {
  1375. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1376. *bad_wr = wr;
  1377. return -ENOMEM;
  1378. }
  1379. if (unlikely(qp_err_flush)) {
  1380. struct ib_wc wc;
  1381. memset(&wc, 0, sizeof(wc));
  1382. wc.qp = &qp->ibqp;
  1383. wc.opcode = IB_WC_RECV;
  1384. wc.wr_id = wr->wr_id;
  1385. wc.status = IB_WC_WR_FLUSH_ERR;
  1386. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1387. } else {
  1388. wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
  1389. wqe->wr_id = wr->wr_id;
  1390. wqe->num_sge = wr->num_sge;
  1391. for (i = 0; i < wr->num_sge; i++)
  1392. wqe->sg_list[i] = wr->sg_list[i];
  1393. /*
  1394. * Make sure queue entry is written
  1395. * before the head index.
  1396. */
  1397. smp_wmb();
  1398. wq->head = next;
  1399. }
  1400. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1401. }
  1402. return 0;
  1403. }
  1404. /**
  1405. * rvt_qp_valid_operation - validate post send wr request
  1406. * @qp - the qp
  1407. * @post-parms - the post send table for the driver
  1408. * @wr - the work request
  1409. *
  1410. * The routine validates the operation based on the
  1411. * validation table an returns the length of the operation
  1412. * which can extend beyond the ib_send_bw. Operation
  1413. * dependent flags key atomic operation validation.
  1414. *
  1415. * There is an exception for UD qps that validates the pd and
  1416. * overrides the length to include the additional UD specific
  1417. * length.
  1418. *
  1419. * Returns a negative error or the length of the work request
  1420. * for building the swqe.
  1421. */
  1422. static inline int rvt_qp_valid_operation(
  1423. struct rvt_qp *qp,
  1424. const struct rvt_operation_params *post_parms,
  1425. struct ib_send_wr *wr)
  1426. {
  1427. int len;
  1428. if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
  1429. return -EINVAL;
  1430. if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
  1431. return -EINVAL;
  1432. if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
  1433. ibpd_to_rvtpd(qp->ibqp.pd)->user)
  1434. return -EINVAL;
  1435. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
  1436. (wr->num_sge == 0 ||
  1437. wr->sg_list[0].length < sizeof(u64) ||
  1438. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  1439. return -EINVAL;
  1440. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
  1441. !qp->s_max_rd_atomic)
  1442. return -EINVAL;
  1443. len = post_parms[wr->opcode].length;
  1444. /* UD specific */
  1445. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1446. qp->ibqp.qp_type != IB_QPT_RC) {
  1447. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1448. return -EINVAL;
  1449. len = sizeof(struct ib_ud_wr);
  1450. }
  1451. return len;
  1452. }
  1453. /**
  1454. * rvt_qp_is_avail - determine queue capacity
  1455. * @qp - the qp
  1456. * @rdi - the rdmavt device
  1457. * @reserved_op - is reserved operation
  1458. *
  1459. * This assumes the s_hlock is held but the s_last
  1460. * qp variable is uncontrolled.
  1461. *
  1462. * For non reserved operations, the qp->s_avail
  1463. * may be changed.
  1464. *
  1465. * The return value is zero or a -ENOMEM.
  1466. */
  1467. static inline int rvt_qp_is_avail(
  1468. struct rvt_qp *qp,
  1469. struct rvt_dev_info *rdi,
  1470. bool reserved_op)
  1471. {
  1472. u32 slast;
  1473. u32 avail;
  1474. u32 reserved_used;
  1475. /* see rvt_qp_wqe_unreserve() */
  1476. smp_mb__before_atomic();
  1477. reserved_used = atomic_read(&qp->s_reserved_used);
  1478. if (unlikely(reserved_op)) {
  1479. /* see rvt_qp_wqe_unreserve() */
  1480. smp_mb__before_atomic();
  1481. if (reserved_used >= rdi->dparms.reserved_operations)
  1482. return -ENOMEM;
  1483. return 0;
  1484. }
  1485. /* non-reserved operations */
  1486. if (likely(qp->s_avail))
  1487. return 0;
  1488. smp_read_barrier_depends(); /* see rc.c */
  1489. slast = ACCESS_ONCE(qp->s_last);
  1490. if (qp->s_head >= slast)
  1491. avail = qp->s_size - (qp->s_head - slast);
  1492. else
  1493. avail = slast - qp->s_head;
  1494. /* see rvt_qp_wqe_unreserve() */
  1495. smp_mb__before_atomic();
  1496. reserved_used = atomic_read(&qp->s_reserved_used);
  1497. avail = avail - 1 -
  1498. (rdi->dparms.reserved_operations - reserved_used);
  1499. /* insure we don't assign a negative s_avail */
  1500. if ((s32)avail <= 0)
  1501. return -ENOMEM;
  1502. qp->s_avail = avail;
  1503. if (WARN_ON(qp->s_avail >
  1504. (qp->s_size - 1 - rdi->dparms.reserved_operations)))
  1505. rvt_pr_err(rdi,
  1506. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1507. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1508. qp->s_head, qp->s_tail, qp->s_cur,
  1509. qp->s_acked, qp->s_last);
  1510. return 0;
  1511. }
  1512. /**
  1513. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1514. * @qp: the QP to post on
  1515. * @wr: the work request to send
  1516. */
  1517. static int rvt_post_one_wr(struct rvt_qp *qp,
  1518. struct ib_send_wr *wr,
  1519. int *call_send)
  1520. {
  1521. struct rvt_swqe *wqe;
  1522. u32 next;
  1523. int i;
  1524. int j;
  1525. int acc;
  1526. struct rvt_lkey_table *rkt;
  1527. struct rvt_pd *pd;
  1528. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1529. u8 log_pmtu;
  1530. int ret;
  1531. size_t cplen;
  1532. bool reserved_op;
  1533. int local_ops_delayed = 0;
  1534. BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
  1535. /* IB spec says that num_sge == 0 is OK. */
  1536. if (unlikely(wr->num_sge > qp->s_max_sge))
  1537. return -EINVAL;
  1538. ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
  1539. if (ret < 0)
  1540. return ret;
  1541. cplen = ret;
  1542. /*
  1543. * Local operations include fast register and local invalidate.
  1544. * Fast register needs to be processed immediately because the
  1545. * registered lkey may be used by following work requests and the
  1546. * lkey needs to be valid at the time those requests are posted.
  1547. * Local invalidate can be processed immediately if fencing is
  1548. * not required and no previous local invalidate ops are pending.
  1549. * Signaled local operations that have been processed immediately
  1550. * need to have requests with "completion only" flags set posted
  1551. * to the send queue in order to generate completions.
  1552. */
  1553. if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
  1554. switch (wr->opcode) {
  1555. case IB_WR_REG_MR:
  1556. ret = rvt_fast_reg_mr(qp,
  1557. reg_wr(wr)->mr,
  1558. reg_wr(wr)->key,
  1559. reg_wr(wr)->access);
  1560. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1561. return ret;
  1562. break;
  1563. case IB_WR_LOCAL_INV:
  1564. if ((wr->send_flags & IB_SEND_FENCE) ||
  1565. atomic_read(&qp->local_ops_pending)) {
  1566. local_ops_delayed = 1;
  1567. } else {
  1568. ret = rvt_invalidate_rkey(
  1569. qp, wr->ex.invalidate_rkey);
  1570. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1571. return ret;
  1572. }
  1573. break;
  1574. default:
  1575. return -EINVAL;
  1576. }
  1577. }
  1578. reserved_op = rdi->post_parms[wr->opcode].flags &
  1579. RVT_OPERATION_USE_RESERVE;
  1580. /* check for avail */
  1581. ret = rvt_qp_is_avail(qp, rdi, reserved_op);
  1582. if (ret)
  1583. return ret;
  1584. next = qp->s_head + 1;
  1585. if (next >= qp->s_size)
  1586. next = 0;
  1587. rkt = &rdi->lkey_table;
  1588. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1589. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1590. /* cplen has length from above */
  1591. memcpy(&wqe->wr, wr, cplen);
  1592. wqe->length = 0;
  1593. j = 0;
  1594. if (wr->num_sge) {
  1595. struct rvt_sge *last_sge = NULL;
  1596. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1597. IB_ACCESS_LOCAL_WRITE : 0;
  1598. for (i = 0; i < wr->num_sge; i++) {
  1599. u32 length = wr->sg_list[i].length;
  1600. if (length == 0)
  1601. continue;
  1602. ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
  1603. &wr->sg_list[i], acc);
  1604. if (unlikely(ret < 0))
  1605. goto bail_inval_free;
  1606. wqe->length += length;
  1607. if (ret)
  1608. last_sge = &wqe->sg_list[j];
  1609. j += ret;
  1610. }
  1611. wqe->wr.num_sge = j;
  1612. }
  1613. /* general part of wqe valid - allow for driver checks */
  1614. if (rdi->driver_f.check_send_wqe) {
  1615. ret = rdi->driver_f.check_send_wqe(qp, wqe);
  1616. if (ret < 0)
  1617. goto bail_inval_free;
  1618. if (ret)
  1619. *call_send = ret;
  1620. }
  1621. log_pmtu = qp->log_pmtu;
  1622. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1623. qp->ibqp.qp_type != IB_QPT_RC) {
  1624. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1625. log_pmtu = ah->log_pmtu;
  1626. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1627. }
  1628. if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
  1629. if (local_ops_delayed)
  1630. atomic_inc(&qp->local_ops_pending);
  1631. else
  1632. wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
  1633. wqe->ssn = 0;
  1634. wqe->psn = 0;
  1635. wqe->lpsn = 0;
  1636. } else {
  1637. wqe->ssn = qp->s_ssn++;
  1638. wqe->psn = qp->s_next_psn;
  1639. wqe->lpsn = wqe->psn +
  1640. (wqe->length ?
  1641. ((wqe->length - 1) >> log_pmtu) :
  1642. 0);
  1643. qp->s_next_psn = wqe->lpsn + 1;
  1644. }
  1645. if (unlikely(reserved_op)) {
  1646. wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
  1647. rvt_qp_wqe_reserve(qp, wqe);
  1648. } else {
  1649. wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
  1650. qp->s_avail--;
  1651. }
  1652. trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
  1653. smp_wmb(); /* see request builders */
  1654. qp->s_head = next;
  1655. return 0;
  1656. bail_inval_free:
  1657. /* release mr holds */
  1658. while (j) {
  1659. struct rvt_sge *sge = &wqe->sg_list[--j];
  1660. rvt_put_mr(sge->mr);
  1661. }
  1662. return ret;
  1663. }
  1664. /**
  1665. * rvt_post_send - post a send on a QP
  1666. * @ibqp: the QP to post the send on
  1667. * @wr: the list of work requests to post
  1668. * @bad_wr: the first bad WR is put here
  1669. *
  1670. * This may be called from interrupt context.
  1671. *
  1672. * Return: 0 on success else errno
  1673. */
  1674. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1675. struct ib_send_wr **bad_wr)
  1676. {
  1677. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1678. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1679. unsigned long flags = 0;
  1680. int call_send;
  1681. unsigned nreq = 0;
  1682. int err = 0;
  1683. spin_lock_irqsave(&qp->s_hlock, flags);
  1684. /*
  1685. * Ensure QP state is such that we can send. If not bail out early,
  1686. * there is no need to do this every time we post a send.
  1687. */
  1688. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1689. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1690. return -EINVAL;
  1691. }
  1692. /*
  1693. * If the send queue is empty, and we only have a single WR then just go
  1694. * ahead and kick the send engine into gear. Otherwise we will always
  1695. * just schedule the send to happen later.
  1696. */
  1697. call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
  1698. for (; wr; wr = wr->next) {
  1699. err = rvt_post_one_wr(qp, wr, &call_send);
  1700. if (unlikely(err)) {
  1701. *bad_wr = wr;
  1702. goto bail;
  1703. }
  1704. nreq++;
  1705. }
  1706. bail:
  1707. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1708. if (nreq) {
  1709. if (call_send)
  1710. rdi->driver_f.do_send(qp);
  1711. else
  1712. rdi->driver_f.schedule_send_no_lock(qp);
  1713. }
  1714. return err;
  1715. }
  1716. /**
  1717. * rvt_post_srq_receive - post a receive on a shared receive queue
  1718. * @ibsrq: the SRQ to post the receive on
  1719. * @wr: the list of work requests to post
  1720. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1721. *
  1722. * This may be called from interrupt context.
  1723. *
  1724. * Return: 0 on success else errno
  1725. */
  1726. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1727. struct ib_recv_wr **bad_wr)
  1728. {
  1729. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1730. struct rvt_rwq *wq;
  1731. unsigned long flags;
  1732. for (; wr; wr = wr->next) {
  1733. struct rvt_rwqe *wqe;
  1734. u32 next;
  1735. int i;
  1736. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1737. *bad_wr = wr;
  1738. return -EINVAL;
  1739. }
  1740. spin_lock_irqsave(&srq->rq.lock, flags);
  1741. wq = srq->rq.wq;
  1742. next = wq->head + 1;
  1743. if (next >= srq->rq.size)
  1744. next = 0;
  1745. if (next == wq->tail) {
  1746. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1747. *bad_wr = wr;
  1748. return -ENOMEM;
  1749. }
  1750. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1751. wqe->wr_id = wr->wr_id;
  1752. wqe->num_sge = wr->num_sge;
  1753. for (i = 0; i < wr->num_sge; i++)
  1754. wqe->sg_list[i] = wr->sg_list[i];
  1755. /* Make sure queue entry is written before the head index. */
  1756. smp_wmb();
  1757. wq->head = next;
  1758. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1759. }
  1760. return 0;
  1761. }
  1762. /**
  1763. * qp_comm_est - handle trap with QP established
  1764. * @qp: the QP
  1765. */
  1766. void rvt_comm_est(struct rvt_qp *qp)
  1767. {
  1768. qp->r_flags |= RVT_R_COMM_EST;
  1769. if (qp->ibqp.event_handler) {
  1770. struct ib_event ev;
  1771. ev.device = qp->ibqp.device;
  1772. ev.element.qp = &qp->ibqp;
  1773. ev.event = IB_EVENT_COMM_EST;
  1774. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1775. }
  1776. }
  1777. EXPORT_SYMBOL(rvt_comm_est);
  1778. void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
  1779. {
  1780. unsigned long flags;
  1781. int lastwqe;
  1782. spin_lock_irqsave(&qp->s_lock, flags);
  1783. lastwqe = rvt_error_qp(qp, err);
  1784. spin_unlock_irqrestore(&qp->s_lock, flags);
  1785. if (lastwqe) {
  1786. struct ib_event ev;
  1787. ev.device = qp->ibqp.device;
  1788. ev.element.qp = &qp->ibqp;
  1789. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1790. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1791. }
  1792. }
  1793. EXPORT_SYMBOL(rvt_rc_error);
  1794. /*
  1795. * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
  1796. * @index - the index
  1797. * return usec from an index into ib_rvt_rnr_table
  1798. */
  1799. unsigned long rvt_rnr_tbl_to_usec(u32 index)
  1800. {
  1801. return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
  1802. }
  1803. EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
  1804. static inline unsigned long rvt_aeth_to_usec(u32 aeth)
  1805. {
  1806. return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
  1807. IB_AETH_CREDIT_MASK];
  1808. }
  1809. /*
  1810. * rvt_add_retry_timer - add/start a retry timer
  1811. * @qp - the QP
  1812. * add a retry timer on the QP
  1813. */
  1814. void rvt_add_retry_timer(struct rvt_qp *qp)
  1815. {
  1816. struct ib_qp *ibqp = &qp->ibqp;
  1817. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1818. lockdep_assert_held(&qp->s_lock);
  1819. qp->s_flags |= RVT_S_TIMER;
  1820. /* 4.096 usec. * (1 << qp->timeout) */
  1821. qp->s_timer.expires = jiffies + qp->timeout_jiffies +
  1822. rdi->busy_jiffies;
  1823. add_timer(&qp->s_timer);
  1824. }
  1825. EXPORT_SYMBOL(rvt_add_retry_timer);
  1826. /**
  1827. * rvt_add_rnr_timer - add/start an rnr timer
  1828. * @qp - the QP
  1829. * @aeth - aeth of RNR timeout, simulated aeth for loopback
  1830. * add an rnr timer on the QP
  1831. */
  1832. void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
  1833. {
  1834. u32 to;
  1835. lockdep_assert_held(&qp->s_lock);
  1836. qp->s_flags |= RVT_S_WAIT_RNR;
  1837. to = rvt_aeth_to_usec(aeth);
  1838. hrtimer_start(&qp->s_rnr_timer,
  1839. ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
  1840. }
  1841. EXPORT_SYMBOL(rvt_add_rnr_timer);
  1842. /**
  1843. * rvt_stop_rc_timers - stop all timers
  1844. * @qp - the QP
  1845. * stop any pending timers
  1846. */
  1847. void rvt_stop_rc_timers(struct rvt_qp *qp)
  1848. {
  1849. lockdep_assert_held(&qp->s_lock);
  1850. /* Remove QP from all timers */
  1851. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  1852. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  1853. del_timer(&qp->s_timer);
  1854. hrtimer_try_to_cancel(&qp->s_rnr_timer);
  1855. }
  1856. }
  1857. EXPORT_SYMBOL(rvt_stop_rc_timers);
  1858. /**
  1859. * rvt_stop_rnr_timer - stop an rnr timer
  1860. * @qp - the QP
  1861. *
  1862. * stop an rnr timer and return if the timer
  1863. * had been pending.
  1864. */
  1865. static int rvt_stop_rnr_timer(struct rvt_qp *qp)
  1866. {
  1867. int rval = 0;
  1868. lockdep_assert_held(&qp->s_lock);
  1869. /* Remove QP from rnr timer */
  1870. if (qp->s_flags & RVT_S_WAIT_RNR) {
  1871. qp->s_flags &= ~RVT_S_WAIT_RNR;
  1872. rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
  1873. }
  1874. return rval;
  1875. }
  1876. /**
  1877. * rvt_del_timers_sync - wait for any timeout routines to exit
  1878. * @qp - the QP
  1879. */
  1880. void rvt_del_timers_sync(struct rvt_qp *qp)
  1881. {
  1882. del_timer_sync(&qp->s_timer);
  1883. hrtimer_cancel(&qp->s_rnr_timer);
  1884. }
  1885. EXPORT_SYMBOL(rvt_del_timers_sync);
  1886. /**
  1887. * This is called from s_timer for missing responses.
  1888. */
  1889. static void rvt_rc_timeout(unsigned long arg)
  1890. {
  1891. struct rvt_qp *qp = (struct rvt_qp *)arg;
  1892. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1893. unsigned long flags;
  1894. spin_lock_irqsave(&qp->r_lock, flags);
  1895. spin_lock(&qp->s_lock);
  1896. if (qp->s_flags & RVT_S_TIMER) {
  1897. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  1898. qp->s_flags &= ~RVT_S_TIMER;
  1899. rvp->n_rc_timeouts++;
  1900. del_timer(&qp->s_timer);
  1901. trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
  1902. if (rdi->driver_f.notify_restart_rc)
  1903. rdi->driver_f.notify_restart_rc(qp,
  1904. qp->s_last_psn + 1,
  1905. 1);
  1906. rdi->driver_f.schedule_send(qp);
  1907. }
  1908. spin_unlock(&qp->s_lock);
  1909. spin_unlock_irqrestore(&qp->r_lock, flags);
  1910. }
  1911. /*
  1912. * This is called from s_timer for RNR timeouts.
  1913. */
  1914. enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
  1915. {
  1916. struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
  1917. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1918. unsigned long flags;
  1919. spin_lock_irqsave(&qp->s_lock, flags);
  1920. rvt_stop_rnr_timer(qp);
  1921. rdi->driver_f.schedule_send(qp);
  1922. spin_unlock_irqrestore(&qp->s_lock, flags);
  1923. return HRTIMER_NORESTART;
  1924. }
  1925. EXPORT_SYMBOL(rvt_rc_rnr_retry);
  1926. /**
  1927. * rvt_qp_iter_init - initial for QP iteration
  1928. * @rdi - rvt devinfo
  1929. * @v - u64 value
  1930. *
  1931. * This returns an iterator suitable for iterating QPs
  1932. * in the system.
  1933. *
  1934. * The @cb is a user defined callback and @v is a 64
  1935. * bit value passed to and relevant for processing in the
  1936. * @cb. An example use case would be to alter QP processing
  1937. * based on criteria not part of the rvt_qp.
  1938. *
  1939. * Use cases that require memory allocation to succeed
  1940. * must preallocate appropriately.
  1941. *
  1942. * Return: a pointer to an rvt_qp_iter or NULL
  1943. */
  1944. struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
  1945. u64 v,
  1946. void (*cb)(struct rvt_qp *qp, u64 v))
  1947. {
  1948. struct rvt_qp_iter *i;
  1949. i = kzalloc(sizeof(*i), GFP_KERNEL);
  1950. if (!i)
  1951. return NULL;
  1952. i->rdi = rdi;
  1953. /* number of special QPs (SMI/GSI) for device */
  1954. i->specials = rdi->ibdev.phys_port_cnt * 2;
  1955. i->v = v;
  1956. i->cb = cb;
  1957. return i;
  1958. }
  1959. EXPORT_SYMBOL(rvt_qp_iter_init);
  1960. /**
  1961. * rvt_qp_iter_next - return the next QP in iter
  1962. * @iter - the iterator
  1963. *
  1964. * Fine grained QP iterator suitable for use
  1965. * with debugfs seq_file mechanisms.
  1966. *
  1967. * Updates iter->qp with the current QP when the return
  1968. * value is 0.
  1969. *
  1970. * Return: 0 - iter->qp is valid 1 - no more QPs
  1971. */
  1972. int rvt_qp_iter_next(struct rvt_qp_iter *iter)
  1973. __must_hold(RCU)
  1974. {
  1975. int n = iter->n;
  1976. int ret = 1;
  1977. struct rvt_qp *pqp = iter->qp;
  1978. struct rvt_qp *qp;
  1979. struct rvt_dev_info *rdi = iter->rdi;
  1980. /*
  1981. * The approach is to consider the special qps
  1982. * as additional table entries before the
  1983. * real hash table. Since the qp code sets
  1984. * the qp->next hash link to NULL, this works just fine.
  1985. *
  1986. * iter->specials is 2 * # ports
  1987. *
  1988. * n = 0..iter->specials is the special qp indices
  1989. *
  1990. * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
  1991. * the potential hash bucket entries
  1992. *
  1993. */
  1994. for (; n < rdi->qp_dev->qp_table_size + iter->specials; n++) {
  1995. if (pqp) {
  1996. qp = rcu_dereference(pqp->next);
  1997. } else {
  1998. if (n < iter->specials) {
  1999. struct rvt_ibport *rvp;
  2000. int pidx;
  2001. pidx = n % rdi->ibdev.phys_port_cnt;
  2002. rvp = rdi->ports[pidx];
  2003. qp = rcu_dereference(rvp->qp[n & 1]);
  2004. } else {
  2005. qp = rcu_dereference(
  2006. rdi->qp_dev->qp_table[
  2007. (n - iter->specials)]);
  2008. }
  2009. }
  2010. pqp = qp;
  2011. if (qp) {
  2012. iter->qp = qp;
  2013. iter->n = n;
  2014. return 0;
  2015. }
  2016. }
  2017. return ret;
  2018. }
  2019. EXPORT_SYMBOL(rvt_qp_iter_next);
  2020. /**
  2021. * rvt_qp_iter - iterate all QPs
  2022. * @rdi - rvt devinfo
  2023. * @v - a 64 bit value
  2024. * @cb - a callback
  2025. *
  2026. * This provides a way for iterating all QPs.
  2027. *
  2028. * The @cb is a user defined callback and @v is a 64
  2029. * bit value passed to and relevant for processing in the
  2030. * cb. An example use case would be to alter QP processing
  2031. * based on criteria not part of the rvt_qp.
  2032. *
  2033. * The code has an internal iterator to simplify
  2034. * non seq_file use cases.
  2035. */
  2036. void rvt_qp_iter(struct rvt_dev_info *rdi,
  2037. u64 v,
  2038. void (*cb)(struct rvt_qp *qp, u64 v))
  2039. {
  2040. int ret;
  2041. struct rvt_qp_iter i = {
  2042. .rdi = rdi,
  2043. .specials = rdi->ibdev.phys_port_cnt * 2,
  2044. .v = v,
  2045. .cb = cb
  2046. };
  2047. rcu_read_lock();
  2048. do {
  2049. ret = rvt_qp_iter_next(&i);
  2050. if (!ret) {
  2051. rvt_get_qp(i.qp);
  2052. rcu_read_unlock();
  2053. i.cb(i.qp, i.v);
  2054. rcu_read_lock();
  2055. rvt_put_qp(i.qp);
  2056. }
  2057. } while (!ret);
  2058. rcu_read_unlock();
  2059. }
  2060. EXPORT_SYMBOL(rvt_qp_iter);