qp.c 62 KB

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