qp.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  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 "qp.h"
  55. #include "vt.h"
  56. #include "trace.h"
  57. static void rvt_rc_timeout(unsigned long arg);
  58. /*
  59. * Convert the AETH RNR timeout code into the number of microseconds.
  60. */
  61. static const u32 ib_rvt_rnr_table[32] = {
  62. 655360, /* 00: 655.36 */
  63. 10, /* 01: .01 */
  64. 20, /* 02 .02 */
  65. 30, /* 03: .03 */
  66. 40, /* 04: .04 */
  67. 60, /* 05: .06 */
  68. 80, /* 06: .08 */
  69. 120, /* 07: .12 */
  70. 160, /* 08: .16 */
  71. 240, /* 09: .24 */
  72. 320, /* 0A: .32 */
  73. 480, /* 0B: .48 */
  74. 640, /* 0C: .64 */
  75. 960, /* 0D: .96 */
  76. 1280, /* 0E: 1.28 */
  77. 1920, /* 0F: 1.92 */
  78. 2560, /* 10: 2.56 */
  79. 3840, /* 11: 3.84 */
  80. 5120, /* 12: 5.12 */
  81. 7680, /* 13: 7.68 */
  82. 10240, /* 14: 10.24 */
  83. 15360, /* 15: 15.36 */
  84. 20480, /* 16: 20.48 */
  85. 30720, /* 17: 30.72 */
  86. 40960, /* 18: 40.96 */
  87. 61440, /* 19: 61.44 */
  88. 81920, /* 1A: 81.92 */
  89. 122880, /* 1B: 122.88 */
  90. 163840, /* 1C: 163.84 */
  91. 245760, /* 1D: 245.76 */
  92. 327680, /* 1E: 327.68 */
  93. 491520 /* 1F: 491.52 */
  94. };
  95. /*
  96. * Note that it is OK to post send work requests in the SQE and ERR
  97. * states; rvt_do_send() will process them and generate error
  98. * completions as per IB 1.2 C10-96.
  99. */
  100. const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
  101. [IB_QPS_RESET] = 0,
  102. [IB_QPS_INIT] = RVT_POST_RECV_OK,
  103. [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
  104. [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  105. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
  106. RVT_PROCESS_NEXT_SEND_OK,
  107. [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  108. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
  109. [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  110. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  111. [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
  112. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  113. };
  114. EXPORT_SYMBOL(ib_rvt_state_ops);
  115. static void get_map_page(struct rvt_qpn_table *qpt,
  116. struct rvt_qpn_map *map,
  117. gfp_t gfp)
  118. {
  119. unsigned long page = get_zeroed_page(gfp);
  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, GFP_KERNEL);
  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, gfp_t gfp)
  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, gfp);
  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, gfp);
  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. static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  377. {
  378. struct rvt_qpn_map *map;
  379. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  380. if (map->page)
  381. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  382. }
  383. /**
  384. * rvt_clear_mr_refs - Drop help mr refs
  385. * @qp: rvt qp data structure
  386. * @clr_sends: If shoudl clear send side or not
  387. */
  388. static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
  389. {
  390. unsigned n;
  391. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  392. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  393. rvt_put_ss(&qp->s_rdma_read_sge);
  394. rvt_put_ss(&qp->r_sge);
  395. if (clr_sends) {
  396. while (qp->s_last != qp->s_head) {
  397. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  398. unsigned i;
  399. for (i = 0; i < wqe->wr.num_sge; i++) {
  400. struct rvt_sge *sge = &wqe->sg_list[i];
  401. rvt_put_mr(sge->mr);
  402. }
  403. if (qp->ibqp.qp_type == IB_QPT_UD ||
  404. qp->ibqp.qp_type == IB_QPT_SMI ||
  405. qp->ibqp.qp_type == IB_QPT_GSI)
  406. atomic_dec(&ibah_to_rvtah(
  407. wqe->ud_wr.ah)->refcount);
  408. if (++qp->s_last >= qp->s_size)
  409. qp->s_last = 0;
  410. smp_wmb(); /* see qp_set_savail */
  411. }
  412. if (qp->s_rdma_mr) {
  413. rvt_put_mr(qp->s_rdma_mr);
  414. qp->s_rdma_mr = NULL;
  415. }
  416. }
  417. if (qp->ibqp.qp_type != IB_QPT_RC)
  418. return;
  419. for (n = 0; n < rvt_max_atomic(rdi); n++) {
  420. struct rvt_ack_entry *e = &qp->s_ack_queue[n];
  421. if (e->rdma_sge.mr) {
  422. rvt_put_mr(e->rdma_sge.mr);
  423. e->rdma_sge.mr = NULL;
  424. }
  425. }
  426. }
  427. /**
  428. * rvt_remove_qp - remove qp form table
  429. * @rdi: rvt dev struct
  430. * @qp: qp to remove
  431. *
  432. * Remove the QP from the table so it can't be found asynchronously by
  433. * the receive routine.
  434. */
  435. static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  436. {
  437. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  438. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  439. unsigned long flags;
  440. int removed = 1;
  441. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  442. if (rcu_dereference_protected(rvp->qp[0],
  443. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  444. RCU_INIT_POINTER(rvp->qp[0], NULL);
  445. } else if (rcu_dereference_protected(rvp->qp[1],
  446. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  447. RCU_INIT_POINTER(rvp->qp[1], NULL);
  448. } else {
  449. struct rvt_qp *q;
  450. struct rvt_qp __rcu **qpp;
  451. removed = 0;
  452. qpp = &rdi->qp_dev->qp_table[n];
  453. for (; (q = rcu_dereference_protected(*qpp,
  454. lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
  455. qpp = &q->next) {
  456. if (q == qp) {
  457. RCU_INIT_POINTER(*qpp,
  458. rcu_dereference_protected(qp->next,
  459. lockdep_is_held(&rdi->qp_dev->qpt_lock)));
  460. removed = 1;
  461. trace_rvt_qpremove(qp, n);
  462. break;
  463. }
  464. }
  465. }
  466. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  467. if (removed) {
  468. synchronize_rcu();
  469. rvt_put_qp(qp);
  470. }
  471. }
  472. /**
  473. * rvt_init_qp - initialize the QP state to the reset state
  474. * @qp: the QP to init or reinit
  475. * @type: the QP type
  476. *
  477. * This function is called from both rvt_create_qp() and
  478. * rvt_reset_qp(). The difference is that the reset
  479. * patch the necessary locks to protect against concurent
  480. * access.
  481. */
  482. static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  483. enum ib_qp_type type)
  484. {
  485. qp->remote_qpn = 0;
  486. qp->qkey = 0;
  487. qp->qp_access_flags = 0;
  488. qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
  489. qp->s_hdrwords = 0;
  490. qp->s_wqe = NULL;
  491. qp->s_draining = 0;
  492. qp->s_next_psn = 0;
  493. qp->s_last_psn = 0;
  494. qp->s_sending_psn = 0;
  495. qp->s_sending_hpsn = 0;
  496. qp->s_psn = 0;
  497. qp->r_psn = 0;
  498. qp->r_msn = 0;
  499. if (type == IB_QPT_RC) {
  500. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  501. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  502. } else {
  503. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  504. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  505. }
  506. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  507. qp->r_nak_state = 0;
  508. qp->r_aflags = 0;
  509. qp->r_flags = 0;
  510. qp->s_head = 0;
  511. qp->s_tail = 0;
  512. qp->s_cur = 0;
  513. qp->s_acked = 0;
  514. qp->s_last = 0;
  515. qp->s_ssn = 1;
  516. qp->s_lsn = 0;
  517. qp->s_mig_state = IB_MIG_MIGRATED;
  518. qp->r_head_ack_queue = 0;
  519. qp->s_tail_ack_queue = 0;
  520. qp->s_num_rd_atomic = 0;
  521. if (qp->r_rq.wq) {
  522. qp->r_rq.wq->head = 0;
  523. qp->r_rq.wq->tail = 0;
  524. }
  525. qp->r_sge.num_sge = 0;
  526. atomic_set(&qp->s_reserved_used, 0);
  527. }
  528. /**
  529. * rvt_reset_qp - initialize the QP state to the reset state
  530. * @qp: the QP to reset
  531. * @type: the QP type
  532. *
  533. * r_lock, s_hlock, and s_lock are required to be held by the caller
  534. */
  535. static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  536. enum ib_qp_type type)
  537. __must_hold(&qp->s_lock)
  538. __must_hold(&qp->s_hlock)
  539. __must_hold(&qp->r_lock)
  540. {
  541. lockdep_assert_held(&qp->r_lock);
  542. lockdep_assert_held(&qp->s_hlock);
  543. lockdep_assert_held(&qp->s_lock);
  544. if (qp->state != IB_QPS_RESET) {
  545. qp->state = IB_QPS_RESET;
  546. /* Let drivers flush their waitlist */
  547. rdi->driver_f.flush_qp_waiters(qp);
  548. rvt_stop_rc_timers(qp);
  549. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
  550. spin_unlock(&qp->s_lock);
  551. spin_unlock(&qp->s_hlock);
  552. spin_unlock_irq(&qp->r_lock);
  553. /* Stop the send queue and the retry timer */
  554. rdi->driver_f.stop_send_queue(qp);
  555. rvt_del_timers_sync(qp);
  556. /* Wait for things to stop */
  557. rdi->driver_f.quiesce_qp(qp);
  558. /* take qp out the hash and wait for it to be unused */
  559. rvt_remove_qp(rdi, qp);
  560. wait_event(qp->wait, !atomic_read(&qp->refcount));
  561. /* grab the lock b/c it was locked at call time */
  562. spin_lock_irq(&qp->r_lock);
  563. spin_lock(&qp->s_hlock);
  564. spin_lock(&qp->s_lock);
  565. rvt_clear_mr_refs(qp, 1);
  566. /*
  567. * Let the driver do any tear down or re-init it needs to for
  568. * a qp that has been reset
  569. */
  570. rdi->driver_f.notify_qp_reset(qp);
  571. }
  572. rvt_init_qp(rdi, qp, type);
  573. lockdep_assert_held(&qp->r_lock);
  574. lockdep_assert_held(&qp->s_hlock);
  575. lockdep_assert_held(&qp->s_lock);
  576. }
  577. /**
  578. * rvt_create_qp - create a queue pair for a device
  579. * @ibpd: the protection domain who's device we create the queue pair for
  580. * @init_attr: the attributes of the queue pair
  581. * @udata: user data for libibverbs.so
  582. *
  583. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  584. * unique idea of what queue pair numbers mean. For instance there is a reserved
  585. * range for PSM.
  586. *
  587. * Return: the queue pair on success, otherwise returns an errno.
  588. *
  589. * Called by the ib_create_qp() core verbs function.
  590. */
  591. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  592. struct ib_qp_init_attr *init_attr,
  593. struct ib_udata *udata)
  594. {
  595. struct rvt_qp *qp;
  596. int err;
  597. struct rvt_swqe *swq = NULL;
  598. size_t sz;
  599. size_t sg_list_sz;
  600. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  601. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  602. void *priv = NULL;
  603. gfp_t gfp;
  604. size_t sqsize;
  605. if (!rdi)
  606. return ERR_PTR(-EINVAL);
  607. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
  608. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  609. init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
  610. return ERR_PTR(-EINVAL);
  611. /* GFP_NOIO is applicable to RC QP's only */
  612. if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
  613. init_attr->qp_type != IB_QPT_RC)
  614. return ERR_PTR(-EINVAL);
  615. gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
  616. GFP_NOIO : GFP_KERNEL;
  617. /* Check receive queue parameters if no SRQ is specified. */
  618. if (!init_attr->srq) {
  619. if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
  620. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  621. return ERR_PTR(-EINVAL);
  622. if (init_attr->cap.max_send_sge +
  623. init_attr->cap.max_send_wr +
  624. init_attr->cap.max_recv_sge +
  625. init_attr->cap.max_recv_wr == 0)
  626. return ERR_PTR(-EINVAL);
  627. }
  628. sqsize =
  629. init_attr->cap.max_send_wr + 1 +
  630. rdi->dparms.reserved_operations;
  631. switch (init_attr->qp_type) {
  632. case IB_QPT_SMI:
  633. case IB_QPT_GSI:
  634. if (init_attr->port_num == 0 ||
  635. init_attr->port_num > ibpd->device->phys_port_cnt)
  636. return ERR_PTR(-EINVAL);
  637. case IB_QPT_UC:
  638. case IB_QPT_RC:
  639. case IB_QPT_UD:
  640. sz = sizeof(struct rvt_sge) *
  641. init_attr->cap.max_send_sge +
  642. sizeof(struct rvt_swqe);
  643. if (gfp == GFP_NOIO)
  644. swq = __vmalloc(
  645. sqsize * sz,
  646. gfp | __GFP_ZERO, PAGE_KERNEL);
  647. else
  648. swq = vzalloc_node(
  649. sqsize * sz,
  650. rdi->dparms.node);
  651. if (!swq)
  652. return ERR_PTR(-ENOMEM);
  653. sz = sizeof(*qp);
  654. sg_list_sz = 0;
  655. if (init_attr->srq) {
  656. struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
  657. if (srq->rq.max_sge > 1)
  658. sg_list_sz = sizeof(*qp->r_sg_list) *
  659. (srq->rq.max_sge - 1);
  660. } else if (init_attr->cap.max_recv_sge > 1)
  661. sg_list_sz = sizeof(*qp->r_sg_list) *
  662. (init_attr->cap.max_recv_sge - 1);
  663. qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
  664. if (!qp)
  665. goto bail_swq;
  666. RCU_INIT_POINTER(qp->next, NULL);
  667. if (init_attr->qp_type == IB_QPT_RC) {
  668. qp->s_ack_queue =
  669. kzalloc_node(
  670. sizeof(*qp->s_ack_queue) *
  671. rvt_max_atomic(rdi),
  672. gfp,
  673. rdi->dparms.node);
  674. if (!qp->s_ack_queue)
  675. goto bail_qp;
  676. }
  677. /* initialize timers needed for rc qp */
  678. setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
  679. hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
  680. HRTIMER_MODE_REL);
  681. qp->s_rnr_timer.function = rvt_rc_rnr_retry;
  682. /*
  683. * Driver needs to set up it's private QP structure and do any
  684. * initialization that is needed.
  685. */
  686. priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
  687. if (IS_ERR(priv)) {
  688. ret = priv;
  689. goto bail_qp;
  690. }
  691. qp->priv = priv;
  692. qp->timeout_jiffies =
  693. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  694. 1000UL);
  695. if (init_attr->srq) {
  696. sz = 0;
  697. } else {
  698. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  699. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  700. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  701. sizeof(struct rvt_rwqe);
  702. if (udata)
  703. qp->r_rq.wq = vmalloc_user(
  704. sizeof(struct rvt_rwq) +
  705. qp->r_rq.size * sz);
  706. else if (gfp == GFP_NOIO)
  707. qp->r_rq.wq = __vmalloc(
  708. sizeof(struct rvt_rwq) +
  709. qp->r_rq.size * sz,
  710. gfp | __GFP_ZERO, PAGE_KERNEL);
  711. else
  712. qp->r_rq.wq = vzalloc_node(
  713. sizeof(struct rvt_rwq) +
  714. qp->r_rq.size * sz,
  715. rdi->dparms.node);
  716. if (!qp->r_rq.wq)
  717. goto bail_driver_priv;
  718. }
  719. /*
  720. * ib_create_qp() will initialize qp->ibqp
  721. * except for qp->ibqp.qp_num.
  722. */
  723. spin_lock_init(&qp->r_lock);
  724. spin_lock_init(&qp->s_hlock);
  725. spin_lock_init(&qp->s_lock);
  726. spin_lock_init(&qp->r_rq.lock);
  727. atomic_set(&qp->refcount, 0);
  728. atomic_set(&qp->local_ops_pending, 0);
  729. init_waitqueue_head(&qp->wait);
  730. init_timer(&qp->s_timer);
  731. qp->s_timer.data = (unsigned long)qp;
  732. INIT_LIST_HEAD(&qp->rspwait);
  733. qp->state = IB_QPS_RESET;
  734. qp->s_wq = swq;
  735. qp->s_size = sqsize;
  736. qp->s_avail = init_attr->cap.max_send_wr;
  737. qp->s_max_sge = init_attr->cap.max_send_sge;
  738. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  739. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  740. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  741. init_attr->qp_type,
  742. init_attr->port_num, gfp);
  743. if (err < 0) {
  744. ret = ERR_PTR(err);
  745. goto bail_rq_wq;
  746. }
  747. qp->ibqp.qp_num = err;
  748. qp->port_num = init_attr->port_num;
  749. rvt_init_qp(rdi, qp, init_attr->qp_type);
  750. break;
  751. default:
  752. /* Don't support raw QPs */
  753. return ERR_PTR(-EINVAL);
  754. }
  755. init_attr->cap.max_inline_data = 0;
  756. /*
  757. * Return the address of the RWQ as the offset to mmap.
  758. * See rvt_mmap() for details.
  759. */
  760. if (udata && udata->outlen >= sizeof(__u64)) {
  761. if (!qp->r_rq.wq) {
  762. __u64 offset = 0;
  763. err = ib_copy_to_udata(udata, &offset,
  764. sizeof(offset));
  765. if (err) {
  766. ret = ERR_PTR(err);
  767. goto bail_qpn;
  768. }
  769. } else {
  770. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  771. qp->ip = rvt_create_mmap_info(rdi, s,
  772. ibpd->uobject->context,
  773. qp->r_rq.wq);
  774. if (!qp->ip) {
  775. ret = ERR_PTR(-ENOMEM);
  776. goto bail_qpn;
  777. }
  778. err = ib_copy_to_udata(udata, &qp->ip->offset,
  779. sizeof(qp->ip->offset));
  780. if (err) {
  781. ret = ERR_PTR(err);
  782. goto bail_ip;
  783. }
  784. }
  785. qp->pid = current->pid;
  786. }
  787. spin_lock(&rdi->n_qps_lock);
  788. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  789. spin_unlock(&rdi->n_qps_lock);
  790. ret = ERR_PTR(-ENOMEM);
  791. goto bail_ip;
  792. }
  793. rdi->n_qps_allocated++;
  794. /*
  795. * Maintain a busy_jiffies variable that will be added to the timeout
  796. * period in mod_retry_timer and add_retry_timer. This busy jiffies
  797. * is scaled by the number of rc qps created for the device to reduce
  798. * the number of timeouts occurring when there is a large number of
  799. * qps. busy_jiffies is incremented every rc qp scaling interval.
  800. * The scaling interval is selected based on extensive performance
  801. * evaluation of targeted workloads.
  802. */
  803. if (init_attr->qp_type == IB_QPT_RC) {
  804. rdi->n_rc_qps++;
  805. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  806. }
  807. spin_unlock(&rdi->n_qps_lock);
  808. if (qp->ip) {
  809. spin_lock_irq(&rdi->pending_lock);
  810. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  811. spin_unlock_irq(&rdi->pending_lock);
  812. }
  813. ret = &qp->ibqp;
  814. /*
  815. * We have our QP and its good, now keep track of what types of opcodes
  816. * can be processed on this QP. We do this by keeping track of what the
  817. * 3 high order bits of the opcode are.
  818. */
  819. switch (init_attr->qp_type) {
  820. case IB_QPT_SMI:
  821. case IB_QPT_GSI:
  822. case IB_QPT_UD:
  823. qp->allowed_ops = IB_OPCODE_UD;
  824. break;
  825. case IB_QPT_RC:
  826. qp->allowed_ops = IB_OPCODE_RC;
  827. break;
  828. case IB_QPT_UC:
  829. qp->allowed_ops = IB_OPCODE_UC;
  830. break;
  831. default:
  832. ret = ERR_PTR(-EINVAL);
  833. goto bail_ip;
  834. }
  835. return ret;
  836. bail_ip:
  837. if (qp->ip)
  838. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  839. bail_qpn:
  840. free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  841. bail_rq_wq:
  842. if (!qp->ip)
  843. vfree(qp->r_rq.wq);
  844. bail_driver_priv:
  845. rdi->driver_f.qp_priv_free(rdi, qp);
  846. bail_qp:
  847. kfree(qp->s_ack_queue);
  848. kfree(qp);
  849. bail_swq:
  850. vfree(swq);
  851. return ret;
  852. }
  853. /**
  854. * rvt_error_qp - put a QP into the error state
  855. * @qp: the QP to put into the error state
  856. * @err: the receive completion error to signal if a RWQE is active
  857. *
  858. * Flushes both send and receive work queues.
  859. *
  860. * Return: true if last WQE event should be generated.
  861. * The QP r_lock and s_lock should be held and interrupts disabled.
  862. * If we are already in error state, just return.
  863. */
  864. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  865. {
  866. struct ib_wc wc;
  867. int ret = 0;
  868. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  869. lockdep_assert_held(&qp->r_lock);
  870. lockdep_assert_held(&qp->s_lock);
  871. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  872. goto bail;
  873. qp->state = IB_QPS_ERR;
  874. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  875. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  876. del_timer(&qp->s_timer);
  877. }
  878. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  879. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  880. rdi->driver_f.notify_error_qp(qp);
  881. /* Schedule the sending tasklet to drain the send work queue. */
  882. if (ACCESS_ONCE(qp->s_last) != qp->s_head)
  883. rdi->driver_f.schedule_send(qp);
  884. rvt_clear_mr_refs(qp, 0);
  885. memset(&wc, 0, sizeof(wc));
  886. wc.qp = &qp->ibqp;
  887. wc.opcode = IB_WC_RECV;
  888. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  889. wc.wr_id = qp->r_wr_id;
  890. wc.status = err;
  891. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  892. }
  893. wc.status = IB_WC_WR_FLUSH_ERR;
  894. if (qp->r_rq.wq) {
  895. struct rvt_rwq *wq;
  896. u32 head;
  897. u32 tail;
  898. spin_lock(&qp->r_rq.lock);
  899. /* sanity check pointers before trusting them */
  900. wq = qp->r_rq.wq;
  901. head = wq->head;
  902. if (head >= qp->r_rq.size)
  903. head = 0;
  904. tail = wq->tail;
  905. if (tail >= qp->r_rq.size)
  906. tail = 0;
  907. while (tail != head) {
  908. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  909. if (++tail >= qp->r_rq.size)
  910. tail = 0;
  911. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  912. }
  913. wq->tail = tail;
  914. spin_unlock(&qp->r_rq.lock);
  915. } else if (qp->ibqp.event_handler) {
  916. ret = 1;
  917. }
  918. bail:
  919. return ret;
  920. }
  921. EXPORT_SYMBOL(rvt_error_qp);
  922. /*
  923. * Put the QP into the hash table.
  924. * The hash table holds a reference to the QP.
  925. */
  926. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  927. {
  928. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  929. unsigned long flags;
  930. rvt_get_qp(qp);
  931. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  932. if (qp->ibqp.qp_num <= 1) {
  933. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  934. } else {
  935. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  936. qp->next = rdi->qp_dev->qp_table[n];
  937. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  938. trace_rvt_qpinsert(qp, n);
  939. }
  940. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  941. }
  942. /**
  943. * rvt_modify_qp - modify the attributes of a queue pair
  944. * @ibqp: the queue pair who's attributes we're modifying
  945. * @attr: the new attributes
  946. * @attr_mask: the mask of attributes to modify
  947. * @udata: user data for libibverbs.so
  948. *
  949. * Return: 0 on success, otherwise returns an errno.
  950. */
  951. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  952. int attr_mask, struct ib_udata *udata)
  953. {
  954. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  955. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  956. enum ib_qp_state cur_state, new_state;
  957. struct ib_event ev;
  958. int lastwqe = 0;
  959. int mig = 0;
  960. int pmtu = 0; /* for gcc warning only */
  961. enum rdma_link_layer link;
  962. link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
  963. spin_lock_irq(&qp->r_lock);
  964. spin_lock(&qp->s_hlock);
  965. spin_lock(&qp->s_lock);
  966. cur_state = attr_mask & IB_QP_CUR_STATE ?
  967. attr->cur_qp_state : qp->state;
  968. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  969. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  970. attr_mask, link))
  971. goto inval;
  972. if (rdi->driver_f.check_modify_qp &&
  973. rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
  974. goto inval;
  975. if (attr_mask & IB_QP_AV) {
  976. if (rdma_ah_get_dlid(&attr->ah_attr) >=
  977. be16_to_cpu(IB_MULTICAST_LID_BASE))
  978. goto inval;
  979. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  980. goto inval;
  981. }
  982. if (attr_mask & IB_QP_ALT_PATH) {
  983. if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
  984. be16_to_cpu(IB_MULTICAST_LID_BASE))
  985. goto inval;
  986. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  987. goto inval;
  988. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  989. goto inval;
  990. }
  991. if (attr_mask & IB_QP_PKEY_INDEX)
  992. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  993. goto inval;
  994. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  995. if (attr->min_rnr_timer > 31)
  996. goto inval;
  997. if (attr_mask & IB_QP_PORT)
  998. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  999. qp->ibqp.qp_type == IB_QPT_GSI ||
  1000. attr->port_num == 0 ||
  1001. attr->port_num > ibqp->device->phys_port_cnt)
  1002. goto inval;
  1003. if (attr_mask & IB_QP_DEST_QPN)
  1004. if (attr->dest_qp_num > RVT_QPN_MASK)
  1005. goto inval;
  1006. if (attr_mask & IB_QP_RETRY_CNT)
  1007. if (attr->retry_cnt > 7)
  1008. goto inval;
  1009. if (attr_mask & IB_QP_RNR_RETRY)
  1010. if (attr->rnr_retry > 7)
  1011. goto inval;
  1012. /*
  1013. * Don't allow invalid path_mtu values. OK to set greater
  1014. * than the active mtu (or even the max_cap, if we have tuned
  1015. * that to a small mtu. We'll set qp->path_mtu
  1016. * to the lesser of requested attribute mtu and active,
  1017. * for packetizing messages.
  1018. * Note that the QP port has to be set in INIT and MTU in RTR.
  1019. */
  1020. if (attr_mask & IB_QP_PATH_MTU) {
  1021. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  1022. if (pmtu < 0)
  1023. goto inval;
  1024. }
  1025. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1026. if (attr->path_mig_state == IB_MIG_REARM) {
  1027. if (qp->s_mig_state == IB_MIG_ARMED)
  1028. goto inval;
  1029. if (new_state != IB_QPS_RTS)
  1030. goto inval;
  1031. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  1032. if (qp->s_mig_state == IB_MIG_REARM)
  1033. goto inval;
  1034. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  1035. goto inval;
  1036. if (qp->s_mig_state == IB_MIG_ARMED)
  1037. mig = 1;
  1038. } else {
  1039. goto inval;
  1040. }
  1041. }
  1042. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1043. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  1044. goto inval;
  1045. switch (new_state) {
  1046. case IB_QPS_RESET:
  1047. if (qp->state != IB_QPS_RESET)
  1048. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1049. break;
  1050. case IB_QPS_RTR:
  1051. /* Allow event to re-trigger if QP set to RTR more than once */
  1052. qp->r_flags &= ~RVT_R_COMM_EST;
  1053. qp->state = new_state;
  1054. break;
  1055. case IB_QPS_SQD:
  1056. qp->s_draining = qp->s_last != qp->s_cur;
  1057. qp->state = new_state;
  1058. break;
  1059. case IB_QPS_SQE:
  1060. if (qp->ibqp.qp_type == IB_QPT_RC)
  1061. goto inval;
  1062. qp->state = new_state;
  1063. break;
  1064. case IB_QPS_ERR:
  1065. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  1066. break;
  1067. default:
  1068. qp->state = new_state;
  1069. break;
  1070. }
  1071. if (attr_mask & IB_QP_PKEY_INDEX)
  1072. qp->s_pkey_index = attr->pkey_index;
  1073. if (attr_mask & IB_QP_PORT)
  1074. qp->port_num = attr->port_num;
  1075. if (attr_mask & IB_QP_DEST_QPN)
  1076. qp->remote_qpn = attr->dest_qp_num;
  1077. if (attr_mask & IB_QP_SQ_PSN) {
  1078. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  1079. qp->s_psn = qp->s_next_psn;
  1080. qp->s_sending_psn = qp->s_next_psn;
  1081. qp->s_last_psn = qp->s_next_psn - 1;
  1082. qp->s_sending_hpsn = qp->s_last_psn;
  1083. }
  1084. if (attr_mask & IB_QP_RQ_PSN)
  1085. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  1086. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1087. qp->qp_access_flags = attr->qp_access_flags;
  1088. if (attr_mask & IB_QP_AV) {
  1089. qp->remote_ah_attr = attr->ah_attr;
  1090. qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
  1091. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  1092. }
  1093. if (attr_mask & IB_QP_ALT_PATH) {
  1094. qp->alt_ah_attr = attr->alt_ah_attr;
  1095. qp->s_alt_pkey_index = attr->alt_pkey_index;
  1096. }
  1097. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1098. qp->s_mig_state = attr->path_mig_state;
  1099. if (mig) {
  1100. qp->remote_ah_attr = qp->alt_ah_attr;
  1101. qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
  1102. qp->s_pkey_index = qp->s_alt_pkey_index;
  1103. }
  1104. }
  1105. if (attr_mask & IB_QP_PATH_MTU) {
  1106. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  1107. qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  1108. qp->log_pmtu = ilog2(qp->pmtu);
  1109. }
  1110. if (attr_mask & IB_QP_RETRY_CNT) {
  1111. qp->s_retry_cnt = attr->retry_cnt;
  1112. qp->s_retry = attr->retry_cnt;
  1113. }
  1114. if (attr_mask & IB_QP_RNR_RETRY) {
  1115. qp->s_rnr_retry_cnt = attr->rnr_retry;
  1116. qp->s_rnr_retry = attr->rnr_retry;
  1117. }
  1118. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1119. qp->r_min_rnr_timer = attr->min_rnr_timer;
  1120. if (attr_mask & IB_QP_TIMEOUT) {
  1121. qp->timeout = attr->timeout;
  1122. qp->timeout_jiffies =
  1123. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  1124. 1000UL);
  1125. }
  1126. if (attr_mask & IB_QP_QKEY)
  1127. qp->qkey = attr->qkey;
  1128. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1129. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  1130. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1131. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1132. if (rdi->driver_f.modify_qp)
  1133. rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
  1134. spin_unlock(&qp->s_lock);
  1135. spin_unlock(&qp->s_hlock);
  1136. spin_unlock_irq(&qp->r_lock);
  1137. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1138. rvt_insert_qp(rdi, qp);
  1139. if (lastwqe) {
  1140. ev.device = qp->ibqp.device;
  1141. ev.element.qp = &qp->ibqp;
  1142. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1143. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1144. }
  1145. if (mig) {
  1146. ev.device = qp->ibqp.device;
  1147. ev.element.qp = &qp->ibqp;
  1148. ev.event = IB_EVENT_PATH_MIG;
  1149. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1150. }
  1151. return 0;
  1152. inval:
  1153. spin_unlock(&qp->s_lock);
  1154. spin_unlock(&qp->s_hlock);
  1155. spin_unlock_irq(&qp->r_lock);
  1156. return -EINVAL;
  1157. }
  1158. /** rvt_free_qpn - Free a qpn from the bit map
  1159. * @qpt: QP table
  1160. * @qpn: queue pair number to free
  1161. */
  1162. static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  1163. {
  1164. struct rvt_qpn_map *map;
  1165. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  1166. if (map->page)
  1167. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  1168. }
  1169. /**
  1170. * rvt_destroy_qp - destroy a queue pair
  1171. * @ibqp: the queue pair to destroy
  1172. *
  1173. * Note that this can be called while the QP is actively sending or
  1174. * receiving!
  1175. *
  1176. * Return: 0 on success.
  1177. */
  1178. int rvt_destroy_qp(struct ib_qp *ibqp)
  1179. {
  1180. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1181. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1182. spin_lock_irq(&qp->r_lock);
  1183. spin_lock(&qp->s_hlock);
  1184. spin_lock(&qp->s_lock);
  1185. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1186. spin_unlock(&qp->s_lock);
  1187. spin_unlock(&qp->s_hlock);
  1188. spin_unlock_irq(&qp->r_lock);
  1189. /* qpn is now available for use again */
  1190. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1191. spin_lock(&rdi->n_qps_lock);
  1192. rdi->n_qps_allocated--;
  1193. if (qp->ibqp.qp_type == IB_QPT_RC) {
  1194. rdi->n_rc_qps--;
  1195. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1196. }
  1197. spin_unlock(&rdi->n_qps_lock);
  1198. if (qp->ip)
  1199. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1200. else
  1201. vfree(qp->r_rq.wq);
  1202. vfree(qp->s_wq);
  1203. rdi->driver_f.qp_priv_free(rdi, qp);
  1204. kfree(qp->s_ack_queue);
  1205. kfree(qp);
  1206. return 0;
  1207. }
  1208. /**
  1209. * rvt_query_qp - query an ipbq
  1210. * @ibqp: IB qp to query
  1211. * @attr: attr struct to fill in
  1212. * @attr_mask: attr mask ignored
  1213. * @init_attr: struct to fill in
  1214. *
  1215. * Return: always 0
  1216. */
  1217. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1218. int attr_mask, struct ib_qp_init_attr *init_attr)
  1219. {
  1220. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1221. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1222. attr->qp_state = qp->state;
  1223. attr->cur_qp_state = attr->qp_state;
  1224. attr->path_mtu = qp->path_mtu;
  1225. attr->path_mig_state = qp->s_mig_state;
  1226. attr->qkey = qp->qkey;
  1227. attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
  1228. attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
  1229. attr->dest_qp_num = qp->remote_qpn;
  1230. attr->qp_access_flags = qp->qp_access_flags;
  1231. attr->cap.max_send_wr = qp->s_size - 1 -
  1232. rdi->dparms.reserved_operations;
  1233. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  1234. attr->cap.max_send_sge = qp->s_max_sge;
  1235. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  1236. attr->cap.max_inline_data = 0;
  1237. attr->ah_attr = qp->remote_ah_attr;
  1238. attr->alt_ah_attr = qp->alt_ah_attr;
  1239. attr->pkey_index = qp->s_pkey_index;
  1240. attr->alt_pkey_index = qp->s_alt_pkey_index;
  1241. attr->en_sqd_async_notify = 0;
  1242. attr->sq_draining = qp->s_draining;
  1243. attr->max_rd_atomic = qp->s_max_rd_atomic;
  1244. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  1245. attr->min_rnr_timer = qp->r_min_rnr_timer;
  1246. attr->port_num = qp->port_num;
  1247. attr->timeout = qp->timeout;
  1248. attr->retry_cnt = qp->s_retry_cnt;
  1249. attr->rnr_retry = qp->s_rnr_retry_cnt;
  1250. attr->alt_port_num =
  1251. rdma_ah_get_port_num(&qp->alt_ah_attr);
  1252. attr->alt_timeout = qp->alt_timeout;
  1253. init_attr->event_handler = qp->ibqp.event_handler;
  1254. init_attr->qp_context = qp->ibqp.qp_context;
  1255. init_attr->send_cq = qp->ibqp.send_cq;
  1256. init_attr->recv_cq = qp->ibqp.recv_cq;
  1257. init_attr->srq = qp->ibqp.srq;
  1258. init_attr->cap = attr->cap;
  1259. if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
  1260. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  1261. else
  1262. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  1263. init_attr->qp_type = qp->ibqp.qp_type;
  1264. init_attr->port_num = qp->port_num;
  1265. return 0;
  1266. }
  1267. /**
  1268. * rvt_post_receive - post a receive on a QP
  1269. * @ibqp: the QP to post the receive on
  1270. * @wr: the WR to post
  1271. * @bad_wr: the first bad WR is put here
  1272. *
  1273. * This may be called from interrupt context.
  1274. *
  1275. * Return: 0 on success otherwise errno
  1276. */
  1277. int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1278. struct ib_recv_wr **bad_wr)
  1279. {
  1280. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1281. struct rvt_rwq *wq = qp->r_rq.wq;
  1282. unsigned long flags;
  1283. int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
  1284. !qp->ibqp.srq;
  1285. /* Check that state is OK to post receive. */
  1286. if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
  1287. *bad_wr = wr;
  1288. return -EINVAL;
  1289. }
  1290. for (; wr; wr = wr->next) {
  1291. struct rvt_rwqe *wqe;
  1292. u32 next;
  1293. int i;
  1294. if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
  1295. *bad_wr = wr;
  1296. return -EINVAL;
  1297. }
  1298. spin_lock_irqsave(&qp->r_rq.lock, flags);
  1299. next = wq->head + 1;
  1300. if (next >= qp->r_rq.size)
  1301. next = 0;
  1302. if (next == wq->tail) {
  1303. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1304. *bad_wr = wr;
  1305. return -ENOMEM;
  1306. }
  1307. if (unlikely(qp_err_flush)) {
  1308. struct ib_wc wc;
  1309. memset(&wc, 0, sizeof(wc));
  1310. wc.qp = &qp->ibqp;
  1311. wc.opcode = IB_WC_RECV;
  1312. wc.wr_id = wr->wr_id;
  1313. wc.status = IB_WC_WR_FLUSH_ERR;
  1314. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1315. } else {
  1316. wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
  1317. wqe->wr_id = wr->wr_id;
  1318. wqe->num_sge = wr->num_sge;
  1319. for (i = 0; i < wr->num_sge; i++)
  1320. wqe->sg_list[i] = wr->sg_list[i];
  1321. /*
  1322. * Make sure queue entry is written
  1323. * before the head index.
  1324. */
  1325. smp_wmb();
  1326. wq->head = next;
  1327. }
  1328. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1329. }
  1330. return 0;
  1331. }
  1332. /**
  1333. * rvt_qp_valid_operation - validate post send wr request
  1334. * @qp - the qp
  1335. * @post-parms - the post send table for the driver
  1336. * @wr - the work request
  1337. *
  1338. * The routine validates the operation based on the
  1339. * validation table an returns the length of the operation
  1340. * which can extend beyond the ib_send_bw. Operation
  1341. * dependent flags key atomic operation validation.
  1342. *
  1343. * There is an exception for UD qps that validates the pd and
  1344. * overrides the length to include the additional UD specific
  1345. * length.
  1346. *
  1347. * Returns a negative error or the length of the work request
  1348. * for building the swqe.
  1349. */
  1350. static inline int rvt_qp_valid_operation(
  1351. struct rvt_qp *qp,
  1352. const struct rvt_operation_params *post_parms,
  1353. struct ib_send_wr *wr)
  1354. {
  1355. int len;
  1356. if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
  1357. return -EINVAL;
  1358. if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
  1359. return -EINVAL;
  1360. if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
  1361. ibpd_to_rvtpd(qp->ibqp.pd)->user)
  1362. return -EINVAL;
  1363. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
  1364. (wr->num_sge == 0 ||
  1365. wr->sg_list[0].length < sizeof(u64) ||
  1366. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  1367. return -EINVAL;
  1368. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
  1369. !qp->s_max_rd_atomic)
  1370. return -EINVAL;
  1371. len = post_parms[wr->opcode].length;
  1372. /* UD specific */
  1373. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1374. qp->ibqp.qp_type != IB_QPT_RC) {
  1375. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1376. return -EINVAL;
  1377. len = sizeof(struct ib_ud_wr);
  1378. }
  1379. return len;
  1380. }
  1381. /**
  1382. * rvt_qp_is_avail - determine queue capacity
  1383. * @qp - the qp
  1384. * @rdi - the rdmavt device
  1385. * @reserved_op - is reserved operation
  1386. *
  1387. * This assumes the s_hlock is held but the s_last
  1388. * qp variable is uncontrolled.
  1389. *
  1390. * For non reserved operations, the qp->s_avail
  1391. * may be changed.
  1392. *
  1393. * The return value is zero or a -ENOMEM.
  1394. */
  1395. static inline int rvt_qp_is_avail(
  1396. struct rvt_qp *qp,
  1397. struct rvt_dev_info *rdi,
  1398. bool reserved_op)
  1399. {
  1400. u32 slast;
  1401. u32 avail;
  1402. u32 reserved_used;
  1403. /* see rvt_qp_wqe_unreserve() */
  1404. smp_mb__before_atomic();
  1405. reserved_used = atomic_read(&qp->s_reserved_used);
  1406. if (unlikely(reserved_op)) {
  1407. /* see rvt_qp_wqe_unreserve() */
  1408. smp_mb__before_atomic();
  1409. if (reserved_used >= rdi->dparms.reserved_operations)
  1410. return -ENOMEM;
  1411. return 0;
  1412. }
  1413. /* non-reserved operations */
  1414. if (likely(qp->s_avail))
  1415. return 0;
  1416. smp_read_barrier_depends(); /* see rc.c */
  1417. slast = ACCESS_ONCE(qp->s_last);
  1418. if (qp->s_head >= slast)
  1419. avail = qp->s_size - (qp->s_head - slast);
  1420. else
  1421. avail = slast - qp->s_head;
  1422. /* see rvt_qp_wqe_unreserve() */
  1423. smp_mb__before_atomic();
  1424. reserved_used = atomic_read(&qp->s_reserved_used);
  1425. avail = avail - 1 -
  1426. (rdi->dparms.reserved_operations - reserved_used);
  1427. /* insure we don't assign a negative s_avail */
  1428. if ((s32)avail <= 0)
  1429. return -ENOMEM;
  1430. qp->s_avail = avail;
  1431. if (WARN_ON(qp->s_avail >
  1432. (qp->s_size - 1 - rdi->dparms.reserved_operations)))
  1433. rvt_pr_err(rdi,
  1434. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1435. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1436. qp->s_head, qp->s_tail, qp->s_cur,
  1437. qp->s_acked, qp->s_last);
  1438. return 0;
  1439. }
  1440. /**
  1441. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1442. * @qp: the QP to post on
  1443. * @wr: the work request to send
  1444. */
  1445. static int rvt_post_one_wr(struct rvt_qp *qp,
  1446. struct ib_send_wr *wr,
  1447. int *call_send)
  1448. {
  1449. struct rvt_swqe *wqe;
  1450. u32 next;
  1451. int i;
  1452. int j;
  1453. int acc;
  1454. struct rvt_lkey_table *rkt;
  1455. struct rvt_pd *pd;
  1456. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1457. u8 log_pmtu;
  1458. int ret;
  1459. size_t cplen;
  1460. bool reserved_op;
  1461. int local_ops_delayed = 0;
  1462. BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
  1463. /* IB spec says that num_sge == 0 is OK. */
  1464. if (unlikely(wr->num_sge > qp->s_max_sge))
  1465. return -EINVAL;
  1466. ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
  1467. if (ret < 0)
  1468. return ret;
  1469. cplen = ret;
  1470. /*
  1471. * Local operations include fast register and local invalidate.
  1472. * Fast register needs to be processed immediately because the
  1473. * registered lkey may be used by following work requests and the
  1474. * lkey needs to be valid at the time those requests are posted.
  1475. * Local invalidate can be processed immediately if fencing is
  1476. * not required and no previous local invalidate ops are pending.
  1477. * Signaled local operations that have been processed immediately
  1478. * need to have requests with "completion only" flags set posted
  1479. * to the send queue in order to generate completions.
  1480. */
  1481. if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
  1482. switch (wr->opcode) {
  1483. case IB_WR_REG_MR:
  1484. ret = rvt_fast_reg_mr(qp,
  1485. reg_wr(wr)->mr,
  1486. reg_wr(wr)->key,
  1487. reg_wr(wr)->access);
  1488. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1489. return ret;
  1490. break;
  1491. case IB_WR_LOCAL_INV:
  1492. if ((wr->send_flags & IB_SEND_FENCE) ||
  1493. atomic_read(&qp->local_ops_pending)) {
  1494. local_ops_delayed = 1;
  1495. } else {
  1496. ret = rvt_invalidate_rkey(
  1497. qp, wr->ex.invalidate_rkey);
  1498. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1499. return ret;
  1500. }
  1501. break;
  1502. default:
  1503. return -EINVAL;
  1504. }
  1505. }
  1506. reserved_op = rdi->post_parms[wr->opcode].flags &
  1507. RVT_OPERATION_USE_RESERVE;
  1508. /* check for avail */
  1509. ret = rvt_qp_is_avail(qp, rdi, reserved_op);
  1510. if (ret)
  1511. return ret;
  1512. next = qp->s_head + 1;
  1513. if (next >= qp->s_size)
  1514. next = 0;
  1515. rkt = &rdi->lkey_table;
  1516. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1517. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1518. /* cplen has length from above */
  1519. memcpy(&wqe->wr, wr, cplen);
  1520. wqe->length = 0;
  1521. j = 0;
  1522. if (wr->num_sge) {
  1523. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1524. IB_ACCESS_LOCAL_WRITE : 0;
  1525. for (i = 0; i < wr->num_sge; i++) {
  1526. u32 length = wr->sg_list[i].length;
  1527. int ok;
  1528. if (length == 0)
  1529. continue;
  1530. ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
  1531. &wr->sg_list[i], acc);
  1532. if (!ok) {
  1533. ret = -EINVAL;
  1534. goto bail_inval_free;
  1535. }
  1536. wqe->length += length;
  1537. j++;
  1538. }
  1539. wqe->wr.num_sge = j;
  1540. }
  1541. /* general part of wqe valid - allow for driver checks */
  1542. if (rdi->driver_f.check_send_wqe) {
  1543. ret = rdi->driver_f.check_send_wqe(qp, wqe);
  1544. if (ret < 0)
  1545. goto bail_inval_free;
  1546. if (ret)
  1547. *call_send = ret;
  1548. }
  1549. log_pmtu = qp->log_pmtu;
  1550. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1551. qp->ibqp.qp_type != IB_QPT_RC) {
  1552. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1553. log_pmtu = ah->log_pmtu;
  1554. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1555. }
  1556. if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
  1557. if (local_ops_delayed)
  1558. atomic_inc(&qp->local_ops_pending);
  1559. else
  1560. wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
  1561. wqe->ssn = 0;
  1562. wqe->psn = 0;
  1563. wqe->lpsn = 0;
  1564. } else {
  1565. wqe->ssn = qp->s_ssn++;
  1566. wqe->psn = qp->s_next_psn;
  1567. wqe->lpsn = wqe->psn +
  1568. (wqe->length ?
  1569. ((wqe->length - 1) >> log_pmtu) :
  1570. 0);
  1571. qp->s_next_psn = wqe->lpsn + 1;
  1572. }
  1573. if (unlikely(reserved_op)) {
  1574. wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
  1575. rvt_qp_wqe_reserve(qp, wqe);
  1576. } else {
  1577. wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
  1578. qp->s_avail--;
  1579. }
  1580. trace_rvt_post_one_wr(qp, wqe);
  1581. smp_wmb(); /* see request builders */
  1582. qp->s_head = next;
  1583. return 0;
  1584. bail_inval_free:
  1585. /* release mr holds */
  1586. while (j) {
  1587. struct rvt_sge *sge = &wqe->sg_list[--j];
  1588. rvt_put_mr(sge->mr);
  1589. }
  1590. return ret;
  1591. }
  1592. /**
  1593. * rvt_post_send - post a send on a QP
  1594. * @ibqp: the QP to post the send on
  1595. * @wr: the list of work requests to post
  1596. * @bad_wr: the first bad WR is put here
  1597. *
  1598. * This may be called from interrupt context.
  1599. *
  1600. * Return: 0 on success else errno
  1601. */
  1602. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1603. struct ib_send_wr **bad_wr)
  1604. {
  1605. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1606. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1607. unsigned long flags = 0;
  1608. int call_send;
  1609. unsigned nreq = 0;
  1610. int err = 0;
  1611. spin_lock_irqsave(&qp->s_hlock, flags);
  1612. /*
  1613. * Ensure QP state is such that we can send. If not bail out early,
  1614. * there is no need to do this every time we post a send.
  1615. */
  1616. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1617. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1618. return -EINVAL;
  1619. }
  1620. /*
  1621. * If the send queue is empty, and we only have a single WR then just go
  1622. * ahead and kick the send engine into gear. Otherwise we will always
  1623. * just schedule the send to happen later.
  1624. */
  1625. call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
  1626. for (; wr; wr = wr->next) {
  1627. err = rvt_post_one_wr(qp, wr, &call_send);
  1628. if (unlikely(err)) {
  1629. *bad_wr = wr;
  1630. goto bail;
  1631. }
  1632. nreq++;
  1633. }
  1634. bail:
  1635. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1636. if (nreq) {
  1637. if (call_send)
  1638. rdi->driver_f.do_send(qp);
  1639. else
  1640. rdi->driver_f.schedule_send_no_lock(qp);
  1641. }
  1642. return err;
  1643. }
  1644. /**
  1645. * rvt_post_srq_receive - post a receive on a shared receive queue
  1646. * @ibsrq: the SRQ to post the receive on
  1647. * @wr: the list of work requests to post
  1648. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1649. *
  1650. * This may be called from interrupt context.
  1651. *
  1652. * Return: 0 on success else errno
  1653. */
  1654. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1655. struct ib_recv_wr **bad_wr)
  1656. {
  1657. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1658. struct rvt_rwq *wq;
  1659. unsigned long flags;
  1660. for (; wr; wr = wr->next) {
  1661. struct rvt_rwqe *wqe;
  1662. u32 next;
  1663. int i;
  1664. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1665. *bad_wr = wr;
  1666. return -EINVAL;
  1667. }
  1668. spin_lock_irqsave(&srq->rq.lock, flags);
  1669. wq = srq->rq.wq;
  1670. next = wq->head + 1;
  1671. if (next >= srq->rq.size)
  1672. next = 0;
  1673. if (next == wq->tail) {
  1674. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1675. *bad_wr = wr;
  1676. return -ENOMEM;
  1677. }
  1678. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1679. wqe->wr_id = wr->wr_id;
  1680. wqe->num_sge = wr->num_sge;
  1681. for (i = 0; i < wr->num_sge; i++)
  1682. wqe->sg_list[i] = wr->sg_list[i];
  1683. /* Make sure queue entry is written before the head index. */
  1684. smp_wmb();
  1685. wq->head = next;
  1686. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1687. }
  1688. return 0;
  1689. }
  1690. /**
  1691. * qp_comm_est - handle trap with QP established
  1692. * @qp: the QP
  1693. */
  1694. void rvt_comm_est(struct rvt_qp *qp)
  1695. {
  1696. qp->r_flags |= RVT_R_COMM_EST;
  1697. if (qp->ibqp.event_handler) {
  1698. struct ib_event ev;
  1699. ev.device = qp->ibqp.device;
  1700. ev.element.qp = &qp->ibqp;
  1701. ev.event = IB_EVENT_COMM_EST;
  1702. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1703. }
  1704. }
  1705. EXPORT_SYMBOL(rvt_comm_est);
  1706. void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
  1707. {
  1708. unsigned long flags;
  1709. int lastwqe;
  1710. spin_lock_irqsave(&qp->s_lock, flags);
  1711. lastwqe = rvt_error_qp(qp, err);
  1712. spin_unlock_irqrestore(&qp->s_lock, flags);
  1713. if (lastwqe) {
  1714. struct ib_event ev;
  1715. ev.device = qp->ibqp.device;
  1716. ev.element.qp = &qp->ibqp;
  1717. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1718. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1719. }
  1720. }
  1721. EXPORT_SYMBOL(rvt_rc_error);
  1722. /*
  1723. * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
  1724. * @index - the index
  1725. * return usec from an index into ib_rvt_rnr_table
  1726. */
  1727. unsigned long rvt_rnr_tbl_to_usec(u32 index)
  1728. {
  1729. return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
  1730. }
  1731. EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
  1732. static inline unsigned long rvt_aeth_to_usec(u32 aeth)
  1733. {
  1734. return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
  1735. IB_AETH_CREDIT_MASK];
  1736. }
  1737. /*
  1738. * rvt_add_retry_timer - add/start a retry timer
  1739. * @qp - the QP
  1740. * add a retry timer on the QP
  1741. */
  1742. void rvt_add_retry_timer(struct rvt_qp *qp)
  1743. {
  1744. struct ib_qp *ibqp = &qp->ibqp;
  1745. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1746. lockdep_assert_held(&qp->s_lock);
  1747. qp->s_flags |= RVT_S_TIMER;
  1748. /* 4.096 usec. * (1 << qp->timeout) */
  1749. qp->s_timer.expires = jiffies + qp->timeout_jiffies +
  1750. rdi->busy_jiffies;
  1751. add_timer(&qp->s_timer);
  1752. }
  1753. EXPORT_SYMBOL(rvt_add_retry_timer);
  1754. /**
  1755. * rvt_add_rnr_timer - add/start an rnr timer
  1756. * @qp - the QP
  1757. * @aeth - aeth of RNR timeout, simulated aeth for loopback
  1758. * add an rnr timer on the QP
  1759. */
  1760. void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
  1761. {
  1762. u32 to;
  1763. lockdep_assert_held(&qp->s_lock);
  1764. qp->s_flags |= RVT_S_WAIT_RNR;
  1765. to = rvt_aeth_to_usec(aeth);
  1766. hrtimer_start(&qp->s_rnr_timer,
  1767. ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
  1768. }
  1769. EXPORT_SYMBOL(rvt_add_rnr_timer);
  1770. /**
  1771. * rvt_stop_rc_timers - stop all timers
  1772. * @qp - the QP
  1773. * stop any pending timers
  1774. */
  1775. void rvt_stop_rc_timers(struct rvt_qp *qp)
  1776. {
  1777. lockdep_assert_held(&qp->s_lock);
  1778. /* Remove QP from all timers */
  1779. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  1780. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  1781. del_timer(&qp->s_timer);
  1782. hrtimer_try_to_cancel(&qp->s_rnr_timer);
  1783. }
  1784. }
  1785. EXPORT_SYMBOL(rvt_stop_rc_timers);
  1786. /**
  1787. * rvt_stop_rnr_timer - stop an rnr timer
  1788. * @qp - the QP
  1789. *
  1790. * stop an rnr timer and return if the timer
  1791. * had been pending.
  1792. */
  1793. static int rvt_stop_rnr_timer(struct rvt_qp *qp)
  1794. {
  1795. int rval = 0;
  1796. lockdep_assert_held(&qp->s_lock);
  1797. /* Remove QP from rnr timer */
  1798. if (qp->s_flags & RVT_S_WAIT_RNR) {
  1799. qp->s_flags &= ~RVT_S_WAIT_RNR;
  1800. rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
  1801. }
  1802. return rval;
  1803. }
  1804. /**
  1805. * rvt_del_timers_sync - wait for any timeout routines to exit
  1806. * @qp - the QP
  1807. */
  1808. void rvt_del_timers_sync(struct rvt_qp *qp)
  1809. {
  1810. del_timer_sync(&qp->s_timer);
  1811. hrtimer_cancel(&qp->s_rnr_timer);
  1812. }
  1813. EXPORT_SYMBOL(rvt_del_timers_sync);
  1814. /**
  1815. * This is called from s_timer for missing responses.
  1816. */
  1817. static void rvt_rc_timeout(unsigned long arg)
  1818. {
  1819. struct rvt_qp *qp = (struct rvt_qp *)arg;
  1820. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1821. unsigned long flags;
  1822. spin_lock_irqsave(&qp->r_lock, flags);
  1823. spin_lock(&qp->s_lock);
  1824. if (qp->s_flags & RVT_S_TIMER) {
  1825. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  1826. qp->s_flags &= ~RVT_S_TIMER;
  1827. rvp->n_rc_timeouts++;
  1828. del_timer(&qp->s_timer);
  1829. trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
  1830. if (rdi->driver_f.notify_restart_rc)
  1831. rdi->driver_f.notify_restart_rc(qp,
  1832. qp->s_last_psn + 1,
  1833. 1);
  1834. rdi->driver_f.schedule_send(qp);
  1835. }
  1836. spin_unlock(&qp->s_lock);
  1837. spin_unlock_irqrestore(&qp->r_lock, flags);
  1838. }
  1839. /*
  1840. * This is called from s_timer for RNR timeouts.
  1841. */
  1842. enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
  1843. {
  1844. struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
  1845. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1846. unsigned long flags;
  1847. spin_lock_irqsave(&qp->s_lock, flags);
  1848. rvt_stop_rnr_timer(qp);
  1849. rdi->driver_f.schedule_send(qp);
  1850. spin_unlock_irqrestore(&qp->s_lock, flags);
  1851. return HRTIMER_NORESTART;
  1852. }
  1853. EXPORT_SYMBOL(rvt_rc_rnr_retry);