qp.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  1. /*
  2. * Copyright(c) 2016 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 "qp.h"
  54. #include "vt.h"
  55. #include "trace.h"
  56. /*
  57. * Note that it is OK to post send work requests in the SQE and ERR
  58. * states; rvt_do_send() will process them and generate error
  59. * completions as per IB 1.2 C10-96.
  60. */
  61. const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
  62. [IB_QPS_RESET] = 0,
  63. [IB_QPS_INIT] = RVT_POST_RECV_OK,
  64. [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
  65. [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  66. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
  67. RVT_PROCESS_NEXT_SEND_OK,
  68. [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  69. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
  70. [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  71. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  72. [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
  73. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  74. };
  75. EXPORT_SYMBOL(ib_rvt_state_ops);
  76. static void get_map_page(struct rvt_qpn_table *qpt,
  77. struct rvt_qpn_map *map,
  78. gfp_t gfp)
  79. {
  80. unsigned long page = get_zeroed_page(gfp);
  81. /*
  82. * Free the page if someone raced with us installing it.
  83. */
  84. spin_lock(&qpt->lock);
  85. if (map->page)
  86. free_page(page);
  87. else
  88. map->page = (void *)page;
  89. spin_unlock(&qpt->lock);
  90. }
  91. /**
  92. * init_qpn_table - initialize the QP number table for a device
  93. * @qpt: the QPN table
  94. */
  95. static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
  96. {
  97. u32 offset, i;
  98. struct rvt_qpn_map *map;
  99. int ret = 0;
  100. if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
  101. return -EINVAL;
  102. spin_lock_init(&qpt->lock);
  103. qpt->last = rdi->dparms.qpn_start;
  104. qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
  105. /*
  106. * Drivers may want some QPs beyond what we need for verbs let them use
  107. * our qpn table. No need for two. Lets go ahead and mark the bitmaps
  108. * for those. The reserved range must be *after* the range which verbs
  109. * will pick from.
  110. */
  111. /* Figure out number of bit maps needed before reserved range */
  112. qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
  113. /* This should always be zero */
  114. offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
  115. /* Starting with the first reserved bit map */
  116. map = &qpt->map[qpt->nmaps];
  117. rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
  118. rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
  119. for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
  120. if (!map->page) {
  121. get_map_page(qpt, map, GFP_KERNEL);
  122. if (!map->page) {
  123. ret = -ENOMEM;
  124. break;
  125. }
  126. }
  127. set_bit(offset, map->page);
  128. offset++;
  129. if (offset == RVT_BITS_PER_PAGE) {
  130. /* next page */
  131. qpt->nmaps++;
  132. map++;
  133. offset = 0;
  134. }
  135. }
  136. return ret;
  137. }
  138. /**
  139. * free_qpn_table - free the QP number table for a device
  140. * @qpt: the QPN table
  141. */
  142. static void free_qpn_table(struct rvt_qpn_table *qpt)
  143. {
  144. int i;
  145. for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
  146. free_page((unsigned long)qpt->map[i].page);
  147. }
  148. /**
  149. * rvt_driver_qp_init - Init driver qp resources
  150. * @rdi: rvt dev strucutre
  151. *
  152. * Return: 0 on success
  153. */
  154. int rvt_driver_qp_init(struct rvt_dev_info *rdi)
  155. {
  156. int i;
  157. int ret = -ENOMEM;
  158. if (!rdi->dparms.qp_table_size)
  159. return -EINVAL;
  160. /*
  161. * If driver is not doing any QP allocation then make sure it is
  162. * providing the necessary QP functions.
  163. */
  164. if (!rdi->driver_f.free_all_qps ||
  165. !rdi->driver_f.qp_priv_alloc ||
  166. !rdi->driver_f.qp_priv_free ||
  167. !rdi->driver_f.notify_qp_reset)
  168. return -EINVAL;
  169. /* allocate parent object */
  170. rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
  171. rdi->dparms.node);
  172. if (!rdi->qp_dev)
  173. return -ENOMEM;
  174. /* allocate hash table */
  175. rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
  176. rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
  177. rdi->qp_dev->qp_table =
  178. kmalloc_node(rdi->qp_dev->qp_table_size *
  179. sizeof(*rdi->qp_dev->qp_table),
  180. GFP_KERNEL, rdi->dparms.node);
  181. if (!rdi->qp_dev->qp_table)
  182. goto no_qp_table;
  183. for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
  184. RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
  185. spin_lock_init(&rdi->qp_dev->qpt_lock);
  186. /* initialize qpn map */
  187. if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
  188. goto fail_table;
  189. spin_lock_init(&rdi->n_qps_lock);
  190. return 0;
  191. fail_table:
  192. kfree(rdi->qp_dev->qp_table);
  193. free_qpn_table(&rdi->qp_dev->qpn_table);
  194. no_qp_table:
  195. kfree(rdi->qp_dev);
  196. return ret;
  197. }
  198. /**
  199. * free_all_qps - check for QPs still in use
  200. * @qpt: the QP table to empty
  201. *
  202. * There should not be any QPs still in use.
  203. * Free memory for table.
  204. */
  205. static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
  206. {
  207. unsigned long flags;
  208. struct rvt_qp *qp;
  209. unsigned n, qp_inuse = 0;
  210. spinlock_t *ql; /* work around too long line below */
  211. if (rdi->driver_f.free_all_qps)
  212. qp_inuse = rdi->driver_f.free_all_qps(rdi);
  213. qp_inuse += rvt_mcast_tree_empty(rdi);
  214. if (!rdi->qp_dev)
  215. return qp_inuse;
  216. ql = &rdi->qp_dev->qpt_lock;
  217. spin_lock_irqsave(ql, flags);
  218. for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
  219. qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
  220. lockdep_is_held(ql));
  221. RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
  222. for (; qp; qp = rcu_dereference_protected(qp->next,
  223. lockdep_is_held(ql)))
  224. qp_inuse++;
  225. }
  226. spin_unlock_irqrestore(ql, flags);
  227. synchronize_rcu();
  228. return qp_inuse;
  229. }
  230. /**
  231. * rvt_qp_exit - clean up qps on device exit
  232. * @rdi: rvt dev structure
  233. *
  234. * Check for qp leaks and free resources.
  235. */
  236. void rvt_qp_exit(struct rvt_dev_info *rdi)
  237. {
  238. u32 qps_inuse = rvt_free_all_qps(rdi);
  239. if (qps_inuse)
  240. rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
  241. qps_inuse);
  242. if (!rdi->qp_dev)
  243. return;
  244. kfree(rdi->qp_dev->qp_table);
  245. free_qpn_table(&rdi->qp_dev->qpn_table);
  246. kfree(rdi->qp_dev);
  247. }
  248. static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
  249. struct rvt_qpn_map *map, unsigned off)
  250. {
  251. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  252. }
  253. /**
  254. * alloc_qpn - Allocate the next available qpn or zero/one for QP type
  255. * IB_QPT_SMI/IB_QPT_GSI
  256. *@rdi: rvt device info structure
  257. *@qpt: queue pair number table pointer
  258. *@port_num: IB port number, 1 based, comes from core
  259. *
  260. * Return: The queue pair number
  261. */
  262. static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
  263. enum ib_qp_type type, u8 port_num, gfp_t gfp)
  264. {
  265. u32 i, offset, max_scan, qpn;
  266. struct rvt_qpn_map *map;
  267. u32 ret;
  268. if (rdi->driver_f.alloc_qpn)
  269. return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
  270. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  271. unsigned n;
  272. ret = type == IB_QPT_GSI;
  273. n = 1 << (ret + 2 * (port_num - 1));
  274. spin_lock(&qpt->lock);
  275. if (qpt->flags & n)
  276. ret = -EINVAL;
  277. else
  278. qpt->flags |= n;
  279. spin_unlock(&qpt->lock);
  280. goto bail;
  281. }
  282. qpn = qpt->last + qpt->incr;
  283. if (qpn >= RVT_QPN_MAX)
  284. qpn = qpt->incr | ((qpt->last & 1) ^ 1);
  285. /* offset carries bit 0 */
  286. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  287. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  288. max_scan = qpt->nmaps - !offset;
  289. for (i = 0;;) {
  290. if (unlikely(!map->page)) {
  291. get_map_page(qpt, map, gfp);
  292. if (unlikely(!map->page))
  293. break;
  294. }
  295. do {
  296. if (!test_and_set_bit(offset, map->page)) {
  297. qpt->last = qpn;
  298. ret = qpn;
  299. goto bail;
  300. }
  301. offset += qpt->incr;
  302. /*
  303. * This qpn might be bogus if offset >= BITS_PER_PAGE.
  304. * That is OK. It gets re-assigned below
  305. */
  306. qpn = mk_qpn(qpt, map, offset);
  307. } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
  308. /*
  309. * In order to keep the number of pages allocated to a
  310. * minimum, we scan the all existing pages before increasing
  311. * the size of the bitmap table.
  312. */
  313. if (++i > max_scan) {
  314. if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
  315. break;
  316. map = &qpt->map[qpt->nmaps++];
  317. /* start at incr with current bit 0 */
  318. offset = qpt->incr | (offset & 1);
  319. } else if (map < &qpt->map[qpt->nmaps]) {
  320. ++map;
  321. /* start at incr with current bit 0 */
  322. offset = qpt->incr | (offset & 1);
  323. } else {
  324. map = &qpt->map[0];
  325. /* wrap to first map page, invert bit 0 */
  326. offset = qpt->incr | ((offset & 1) ^ 1);
  327. }
  328. /* there can be no set bits in low-order QoS bits */
  329. WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
  330. qpn = mk_qpn(qpt, map, offset);
  331. }
  332. ret = -ENOMEM;
  333. bail:
  334. return ret;
  335. }
  336. static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  337. {
  338. struct rvt_qpn_map *map;
  339. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  340. if (map->page)
  341. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  342. }
  343. /**
  344. * rvt_clear_mr_refs - Drop help mr refs
  345. * @qp: rvt qp data structure
  346. * @clr_sends: If shoudl clear send side or not
  347. */
  348. static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
  349. {
  350. unsigned n;
  351. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  352. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  353. rvt_put_ss(&qp->s_rdma_read_sge);
  354. rvt_put_ss(&qp->r_sge);
  355. if (clr_sends) {
  356. while (qp->s_last != qp->s_head) {
  357. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  358. unsigned i;
  359. for (i = 0; i < wqe->wr.num_sge; i++) {
  360. struct rvt_sge *sge = &wqe->sg_list[i];
  361. rvt_put_mr(sge->mr);
  362. }
  363. if (qp->ibqp.qp_type == IB_QPT_UD ||
  364. qp->ibqp.qp_type == IB_QPT_SMI ||
  365. qp->ibqp.qp_type == IB_QPT_GSI)
  366. atomic_dec(&ibah_to_rvtah(
  367. wqe->ud_wr.ah)->refcount);
  368. if (++qp->s_last >= qp->s_size)
  369. qp->s_last = 0;
  370. smp_wmb(); /* see qp_set_savail */
  371. }
  372. if (qp->s_rdma_mr) {
  373. rvt_put_mr(qp->s_rdma_mr);
  374. qp->s_rdma_mr = NULL;
  375. }
  376. }
  377. if (qp->ibqp.qp_type != IB_QPT_RC)
  378. return;
  379. for (n = 0; n < rvt_max_atomic(rdi); n++) {
  380. struct rvt_ack_entry *e = &qp->s_ack_queue[n];
  381. if (e->rdma_sge.mr) {
  382. rvt_put_mr(e->rdma_sge.mr);
  383. e->rdma_sge.mr = NULL;
  384. }
  385. }
  386. }
  387. /**
  388. * rvt_remove_qp - remove qp form table
  389. * @rdi: rvt dev struct
  390. * @qp: qp to remove
  391. *
  392. * Remove the QP from the table so it can't be found asynchronously by
  393. * the receive routine.
  394. */
  395. static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  396. {
  397. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  398. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  399. unsigned long flags;
  400. int removed = 1;
  401. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  402. if (rcu_dereference_protected(rvp->qp[0],
  403. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  404. RCU_INIT_POINTER(rvp->qp[0], NULL);
  405. } else if (rcu_dereference_protected(rvp->qp[1],
  406. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  407. RCU_INIT_POINTER(rvp->qp[1], NULL);
  408. } else {
  409. struct rvt_qp *q;
  410. struct rvt_qp __rcu **qpp;
  411. removed = 0;
  412. qpp = &rdi->qp_dev->qp_table[n];
  413. for (; (q = rcu_dereference_protected(*qpp,
  414. lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
  415. qpp = &q->next) {
  416. if (q == qp) {
  417. RCU_INIT_POINTER(*qpp,
  418. rcu_dereference_protected(qp->next,
  419. lockdep_is_held(&rdi->qp_dev->qpt_lock)));
  420. removed = 1;
  421. trace_rvt_qpremove(qp, n);
  422. break;
  423. }
  424. }
  425. }
  426. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  427. if (removed) {
  428. synchronize_rcu();
  429. if (atomic_dec_and_test(&qp->refcount))
  430. wake_up(&qp->wait);
  431. }
  432. }
  433. /**
  434. * reset_qp - initialize the QP state to the reset state
  435. * @qp: the QP to reset
  436. * @type: the QP type
  437. * r and s lock are required to be held by the caller
  438. */
  439. static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  440. enum ib_qp_type type)
  441. __releases(&qp->s_lock)
  442. __releases(&qp->s_hlock)
  443. __releases(&qp->r_lock)
  444. __acquires(&qp->r_lock)
  445. __acquires(&qp->s_hlock)
  446. __acquires(&qp->s_lock)
  447. {
  448. if (qp->state != IB_QPS_RESET) {
  449. qp->state = IB_QPS_RESET;
  450. /* Let drivers flush their waitlist */
  451. rdi->driver_f.flush_qp_waiters(qp);
  452. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
  453. spin_unlock(&qp->s_lock);
  454. spin_unlock(&qp->s_hlock);
  455. spin_unlock_irq(&qp->r_lock);
  456. /* Stop the send queue and the retry timer */
  457. rdi->driver_f.stop_send_queue(qp);
  458. /* Wait for things to stop */
  459. rdi->driver_f.quiesce_qp(qp);
  460. /* take qp out the hash and wait for it to be unused */
  461. rvt_remove_qp(rdi, qp);
  462. wait_event(qp->wait, !atomic_read(&qp->refcount));
  463. /* grab the lock b/c it was locked at call time */
  464. spin_lock_irq(&qp->r_lock);
  465. spin_lock(&qp->s_hlock);
  466. spin_lock(&qp->s_lock);
  467. rvt_clear_mr_refs(qp, 1);
  468. }
  469. /*
  470. * Let the driver do any tear down it needs to for a qp
  471. * that has been reset
  472. */
  473. rdi->driver_f.notify_qp_reset(qp);
  474. qp->remote_qpn = 0;
  475. qp->qkey = 0;
  476. qp->qp_access_flags = 0;
  477. qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
  478. qp->s_hdrwords = 0;
  479. qp->s_wqe = NULL;
  480. qp->s_draining = 0;
  481. qp->s_next_psn = 0;
  482. qp->s_last_psn = 0;
  483. qp->s_sending_psn = 0;
  484. qp->s_sending_hpsn = 0;
  485. qp->s_psn = 0;
  486. qp->r_psn = 0;
  487. qp->r_msn = 0;
  488. if (type == IB_QPT_RC) {
  489. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  490. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  491. } else {
  492. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  493. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  494. }
  495. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  496. qp->r_nak_state = 0;
  497. qp->r_aflags = 0;
  498. qp->r_flags = 0;
  499. qp->s_head = 0;
  500. qp->s_tail = 0;
  501. qp->s_cur = 0;
  502. qp->s_acked = 0;
  503. qp->s_last = 0;
  504. qp->s_ssn = 1;
  505. qp->s_lsn = 0;
  506. qp->s_mig_state = IB_MIG_MIGRATED;
  507. qp->r_head_ack_queue = 0;
  508. qp->s_tail_ack_queue = 0;
  509. qp->s_num_rd_atomic = 0;
  510. if (qp->r_rq.wq) {
  511. qp->r_rq.wq->head = 0;
  512. qp->r_rq.wq->tail = 0;
  513. }
  514. qp->r_sge.num_sge = 0;
  515. atomic_set(&qp->s_reserved_used, 0);
  516. }
  517. /**
  518. * rvt_create_qp - create a queue pair for a device
  519. * @ibpd: the protection domain who's device we create the queue pair for
  520. * @init_attr: the attributes of the queue pair
  521. * @udata: user data for libibverbs.so
  522. *
  523. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  524. * unique idea of what queue pair numbers mean. For instance there is a reserved
  525. * range for PSM.
  526. *
  527. * Return: the queue pair on success, otherwise returns an errno.
  528. *
  529. * Called by the ib_create_qp() core verbs function.
  530. */
  531. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  532. struct ib_qp_init_attr *init_attr,
  533. struct ib_udata *udata)
  534. {
  535. struct rvt_qp *qp;
  536. int err;
  537. struct rvt_swqe *swq = NULL;
  538. size_t sz;
  539. size_t sg_list_sz;
  540. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  541. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  542. void *priv = NULL;
  543. gfp_t gfp;
  544. size_t sqsize;
  545. if (!rdi)
  546. return ERR_PTR(-EINVAL);
  547. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
  548. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  549. init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
  550. return ERR_PTR(-EINVAL);
  551. /* GFP_NOIO is applicable to RC QP's only */
  552. if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
  553. init_attr->qp_type != IB_QPT_RC)
  554. return ERR_PTR(-EINVAL);
  555. gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
  556. GFP_NOIO : GFP_KERNEL;
  557. /* Check receive queue parameters if no SRQ is specified. */
  558. if (!init_attr->srq) {
  559. if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
  560. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  561. return ERR_PTR(-EINVAL);
  562. if (init_attr->cap.max_send_sge +
  563. init_attr->cap.max_send_wr +
  564. init_attr->cap.max_recv_sge +
  565. init_attr->cap.max_recv_wr == 0)
  566. return ERR_PTR(-EINVAL);
  567. }
  568. sqsize =
  569. init_attr->cap.max_send_wr + 1 +
  570. rdi->dparms.reserved_operations;
  571. switch (init_attr->qp_type) {
  572. case IB_QPT_SMI:
  573. case IB_QPT_GSI:
  574. if (init_attr->port_num == 0 ||
  575. init_attr->port_num > ibpd->device->phys_port_cnt)
  576. return ERR_PTR(-EINVAL);
  577. case IB_QPT_UC:
  578. case IB_QPT_RC:
  579. case IB_QPT_UD:
  580. sz = sizeof(struct rvt_sge) *
  581. init_attr->cap.max_send_sge +
  582. sizeof(struct rvt_swqe);
  583. if (gfp == GFP_NOIO)
  584. swq = __vmalloc(
  585. sqsize * sz,
  586. gfp | __GFP_ZERO, PAGE_KERNEL);
  587. else
  588. swq = vzalloc_node(
  589. sqsize * sz,
  590. rdi->dparms.node);
  591. if (!swq)
  592. return ERR_PTR(-ENOMEM);
  593. sz = sizeof(*qp);
  594. sg_list_sz = 0;
  595. if (init_attr->srq) {
  596. struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
  597. if (srq->rq.max_sge > 1)
  598. sg_list_sz = sizeof(*qp->r_sg_list) *
  599. (srq->rq.max_sge - 1);
  600. } else if (init_attr->cap.max_recv_sge > 1)
  601. sg_list_sz = sizeof(*qp->r_sg_list) *
  602. (init_attr->cap.max_recv_sge - 1);
  603. qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
  604. if (!qp)
  605. goto bail_swq;
  606. RCU_INIT_POINTER(qp->next, NULL);
  607. if (init_attr->qp_type == IB_QPT_RC) {
  608. qp->s_ack_queue =
  609. kzalloc_node(
  610. sizeof(*qp->s_ack_queue) *
  611. rvt_max_atomic(rdi),
  612. gfp,
  613. rdi->dparms.node);
  614. if (!qp->s_ack_queue)
  615. goto bail_qp;
  616. }
  617. /*
  618. * Driver needs to set up it's private QP structure and do any
  619. * initialization that is needed.
  620. */
  621. priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
  622. if (IS_ERR(priv)) {
  623. ret = priv;
  624. goto bail_qp;
  625. }
  626. qp->priv = priv;
  627. qp->timeout_jiffies =
  628. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  629. 1000UL);
  630. if (init_attr->srq) {
  631. sz = 0;
  632. } else {
  633. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  634. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  635. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  636. sizeof(struct rvt_rwqe);
  637. if (udata)
  638. qp->r_rq.wq = vmalloc_user(
  639. sizeof(struct rvt_rwq) +
  640. qp->r_rq.size * sz);
  641. else if (gfp == GFP_NOIO)
  642. qp->r_rq.wq = __vmalloc(
  643. sizeof(struct rvt_rwq) +
  644. qp->r_rq.size * sz,
  645. gfp | __GFP_ZERO, PAGE_KERNEL);
  646. else
  647. qp->r_rq.wq = vzalloc_node(
  648. sizeof(struct rvt_rwq) +
  649. qp->r_rq.size * sz,
  650. rdi->dparms.node);
  651. if (!qp->r_rq.wq)
  652. goto bail_driver_priv;
  653. }
  654. /*
  655. * ib_create_qp() will initialize qp->ibqp
  656. * except for qp->ibqp.qp_num.
  657. */
  658. spin_lock_init(&qp->r_lock);
  659. spin_lock_init(&qp->s_hlock);
  660. spin_lock_init(&qp->s_lock);
  661. spin_lock_init(&qp->r_rq.lock);
  662. atomic_set(&qp->refcount, 0);
  663. atomic_set(&qp->local_ops_pending, 0);
  664. init_waitqueue_head(&qp->wait);
  665. init_timer(&qp->s_timer);
  666. qp->s_timer.data = (unsigned long)qp;
  667. INIT_LIST_HEAD(&qp->rspwait);
  668. qp->state = IB_QPS_RESET;
  669. qp->s_wq = swq;
  670. qp->s_size = sqsize;
  671. qp->s_avail = init_attr->cap.max_send_wr;
  672. qp->s_max_sge = init_attr->cap.max_send_sge;
  673. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  674. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  675. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  676. init_attr->qp_type,
  677. init_attr->port_num, gfp);
  678. if (err < 0) {
  679. ret = ERR_PTR(err);
  680. goto bail_rq_wq;
  681. }
  682. qp->ibqp.qp_num = err;
  683. qp->port_num = init_attr->port_num;
  684. rvt_reset_qp(rdi, qp, init_attr->qp_type);
  685. break;
  686. default:
  687. /* Don't support raw QPs */
  688. return ERR_PTR(-EINVAL);
  689. }
  690. init_attr->cap.max_inline_data = 0;
  691. /*
  692. * Return the address of the RWQ as the offset to mmap.
  693. * See rvt_mmap() for details.
  694. */
  695. if (udata && udata->outlen >= sizeof(__u64)) {
  696. if (!qp->r_rq.wq) {
  697. __u64 offset = 0;
  698. err = ib_copy_to_udata(udata, &offset,
  699. sizeof(offset));
  700. if (err) {
  701. ret = ERR_PTR(err);
  702. goto bail_qpn;
  703. }
  704. } else {
  705. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  706. qp->ip = rvt_create_mmap_info(rdi, s,
  707. ibpd->uobject->context,
  708. qp->r_rq.wq);
  709. if (!qp->ip) {
  710. ret = ERR_PTR(-ENOMEM);
  711. goto bail_qpn;
  712. }
  713. err = ib_copy_to_udata(udata, &qp->ip->offset,
  714. sizeof(qp->ip->offset));
  715. if (err) {
  716. ret = ERR_PTR(err);
  717. goto bail_ip;
  718. }
  719. }
  720. qp->pid = current->pid;
  721. }
  722. spin_lock(&rdi->n_qps_lock);
  723. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  724. spin_unlock(&rdi->n_qps_lock);
  725. ret = ERR_PTR(-ENOMEM);
  726. goto bail_ip;
  727. }
  728. rdi->n_qps_allocated++;
  729. /*
  730. * Maintain a busy_jiffies variable that will be added to the timeout
  731. * period in mod_retry_timer and add_retry_timer. This busy jiffies
  732. * is scaled by the number of rc qps created for the device to reduce
  733. * the number of timeouts occurring when there is a large number of
  734. * qps. busy_jiffies is incremented every rc qp scaling interval.
  735. * The scaling interval is selected based on extensive performance
  736. * evaluation of targeted workloads.
  737. */
  738. if (init_attr->qp_type == IB_QPT_RC) {
  739. rdi->n_rc_qps++;
  740. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  741. }
  742. spin_unlock(&rdi->n_qps_lock);
  743. if (qp->ip) {
  744. spin_lock_irq(&rdi->pending_lock);
  745. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  746. spin_unlock_irq(&rdi->pending_lock);
  747. }
  748. ret = &qp->ibqp;
  749. /*
  750. * We have our QP and its good, now keep track of what types of opcodes
  751. * can be processed on this QP. We do this by keeping track of what the
  752. * 3 high order bits of the opcode are.
  753. */
  754. switch (init_attr->qp_type) {
  755. case IB_QPT_SMI:
  756. case IB_QPT_GSI:
  757. case IB_QPT_UD:
  758. qp->allowed_ops = IB_OPCODE_UD;
  759. break;
  760. case IB_QPT_RC:
  761. qp->allowed_ops = IB_OPCODE_RC;
  762. break;
  763. case IB_QPT_UC:
  764. qp->allowed_ops = IB_OPCODE_UC;
  765. break;
  766. default:
  767. ret = ERR_PTR(-EINVAL);
  768. goto bail_ip;
  769. }
  770. return ret;
  771. bail_ip:
  772. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  773. bail_qpn:
  774. free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  775. bail_rq_wq:
  776. if (!qp->ip)
  777. vfree(qp->r_rq.wq);
  778. bail_driver_priv:
  779. rdi->driver_f.qp_priv_free(rdi, qp);
  780. bail_qp:
  781. kfree(qp->s_ack_queue);
  782. kfree(qp);
  783. bail_swq:
  784. vfree(swq);
  785. return ret;
  786. }
  787. /**
  788. * rvt_error_qp - put a QP into the error state
  789. * @qp: the QP to put into the error state
  790. * @err: the receive completion error to signal if a RWQE is active
  791. *
  792. * Flushes both send and receive work queues.
  793. *
  794. * Return: true if last WQE event should be generated.
  795. * The QP r_lock and s_lock should be held and interrupts disabled.
  796. * If we are already in error state, just return.
  797. */
  798. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  799. {
  800. struct ib_wc wc;
  801. int ret = 0;
  802. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  803. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  804. goto bail;
  805. qp->state = IB_QPS_ERR;
  806. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  807. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  808. del_timer(&qp->s_timer);
  809. }
  810. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  811. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  812. rdi->driver_f.notify_error_qp(qp);
  813. /* Schedule the sending tasklet to drain the send work queue. */
  814. if (ACCESS_ONCE(qp->s_last) != qp->s_head)
  815. rdi->driver_f.schedule_send(qp);
  816. rvt_clear_mr_refs(qp, 0);
  817. memset(&wc, 0, sizeof(wc));
  818. wc.qp = &qp->ibqp;
  819. wc.opcode = IB_WC_RECV;
  820. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  821. wc.wr_id = qp->r_wr_id;
  822. wc.status = err;
  823. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  824. }
  825. wc.status = IB_WC_WR_FLUSH_ERR;
  826. if (qp->r_rq.wq) {
  827. struct rvt_rwq *wq;
  828. u32 head;
  829. u32 tail;
  830. spin_lock(&qp->r_rq.lock);
  831. /* sanity check pointers before trusting them */
  832. wq = qp->r_rq.wq;
  833. head = wq->head;
  834. if (head >= qp->r_rq.size)
  835. head = 0;
  836. tail = wq->tail;
  837. if (tail >= qp->r_rq.size)
  838. tail = 0;
  839. while (tail != head) {
  840. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  841. if (++tail >= qp->r_rq.size)
  842. tail = 0;
  843. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  844. }
  845. wq->tail = tail;
  846. spin_unlock(&qp->r_rq.lock);
  847. } else if (qp->ibqp.event_handler) {
  848. ret = 1;
  849. }
  850. bail:
  851. return ret;
  852. }
  853. EXPORT_SYMBOL(rvt_error_qp);
  854. /*
  855. * Put the QP into the hash table.
  856. * The hash table holds a reference to the QP.
  857. */
  858. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  859. {
  860. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  861. unsigned long flags;
  862. atomic_inc(&qp->refcount);
  863. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  864. if (qp->ibqp.qp_num <= 1) {
  865. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  866. } else {
  867. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  868. qp->next = rdi->qp_dev->qp_table[n];
  869. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  870. trace_rvt_qpinsert(qp, n);
  871. }
  872. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  873. }
  874. /**
  875. * qib_modify_qp - modify the attributes of a queue pair
  876. * @ibqp: the queue pair who's attributes we're modifying
  877. * @attr: the new attributes
  878. * @attr_mask: the mask of attributes to modify
  879. * @udata: user data for libibverbs.so
  880. *
  881. * Return: 0 on success, otherwise returns an errno.
  882. */
  883. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  884. int attr_mask, struct ib_udata *udata)
  885. {
  886. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  887. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  888. enum ib_qp_state cur_state, new_state;
  889. struct ib_event ev;
  890. int lastwqe = 0;
  891. int mig = 0;
  892. int pmtu = 0; /* for gcc warning only */
  893. enum rdma_link_layer link;
  894. link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
  895. spin_lock_irq(&qp->r_lock);
  896. spin_lock(&qp->s_hlock);
  897. spin_lock(&qp->s_lock);
  898. cur_state = attr_mask & IB_QP_CUR_STATE ?
  899. attr->cur_qp_state : qp->state;
  900. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  901. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  902. attr_mask, link))
  903. goto inval;
  904. if (rdi->driver_f.check_modify_qp &&
  905. rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
  906. goto inval;
  907. if (attr_mask & IB_QP_AV) {
  908. if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
  909. goto inval;
  910. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  911. goto inval;
  912. }
  913. if (attr_mask & IB_QP_ALT_PATH) {
  914. if (attr->alt_ah_attr.dlid >=
  915. be16_to_cpu(IB_MULTICAST_LID_BASE))
  916. goto inval;
  917. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  918. goto inval;
  919. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  920. goto inval;
  921. }
  922. if (attr_mask & IB_QP_PKEY_INDEX)
  923. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  924. goto inval;
  925. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  926. if (attr->min_rnr_timer > 31)
  927. goto inval;
  928. if (attr_mask & IB_QP_PORT)
  929. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  930. qp->ibqp.qp_type == IB_QPT_GSI ||
  931. attr->port_num == 0 ||
  932. attr->port_num > ibqp->device->phys_port_cnt)
  933. goto inval;
  934. if (attr_mask & IB_QP_DEST_QPN)
  935. if (attr->dest_qp_num > RVT_QPN_MASK)
  936. goto inval;
  937. if (attr_mask & IB_QP_RETRY_CNT)
  938. if (attr->retry_cnt > 7)
  939. goto inval;
  940. if (attr_mask & IB_QP_RNR_RETRY)
  941. if (attr->rnr_retry > 7)
  942. goto inval;
  943. /*
  944. * Don't allow invalid path_mtu values. OK to set greater
  945. * than the active mtu (or even the max_cap, if we have tuned
  946. * that to a small mtu. We'll set qp->path_mtu
  947. * to the lesser of requested attribute mtu and active,
  948. * for packetizing messages.
  949. * Note that the QP port has to be set in INIT and MTU in RTR.
  950. */
  951. if (attr_mask & IB_QP_PATH_MTU) {
  952. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  953. if (pmtu < 0)
  954. goto inval;
  955. }
  956. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  957. if (attr->path_mig_state == IB_MIG_REARM) {
  958. if (qp->s_mig_state == IB_MIG_ARMED)
  959. goto inval;
  960. if (new_state != IB_QPS_RTS)
  961. goto inval;
  962. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  963. if (qp->s_mig_state == IB_MIG_REARM)
  964. goto inval;
  965. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  966. goto inval;
  967. if (qp->s_mig_state == IB_MIG_ARMED)
  968. mig = 1;
  969. } else {
  970. goto inval;
  971. }
  972. }
  973. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  974. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  975. goto inval;
  976. switch (new_state) {
  977. case IB_QPS_RESET:
  978. if (qp->state != IB_QPS_RESET)
  979. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  980. break;
  981. case IB_QPS_RTR:
  982. /* Allow event to re-trigger if QP set to RTR more than once */
  983. qp->r_flags &= ~RVT_R_COMM_EST;
  984. qp->state = new_state;
  985. break;
  986. case IB_QPS_SQD:
  987. qp->s_draining = qp->s_last != qp->s_cur;
  988. qp->state = new_state;
  989. break;
  990. case IB_QPS_SQE:
  991. if (qp->ibqp.qp_type == IB_QPT_RC)
  992. goto inval;
  993. qp->state = new_state;
  994. break;
  995. case IB_QPS_ERR:
  996. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  997. break;
  998. default:
  999. qp->state = new_state;
  1000. break;
  1001. }
  1002. if (attr_mask & IB_QP_PKEY_INDEX)
  1003. qp->s_pkey_index = attr->pkey_index;
  1004. if (attr_mask & IB_QP_PORT)
  1005. qp->port_num = attr->port_num;
  1006. if (attr_mask & IB_QP_DEST_QPN)
  1007. qp->remote_qpn = attr->dest_qp_num;
  1008. if (attr_mask & IB_QP_SQ_PSN) {
  1009. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  1010. qp->s_psn = qp->s_next_psn;
  1011. qp->s_sending_psn = qp->s_next_psn;
  1012. qp->s_last_psn = qp->s_next_psn - 1;
  1013. qp->s_sending_hpsn = qp->s_last_psn;
  1014. }
  1015. if (attr_mask & IB_QP_RQ_PSN)
  1016. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  1017. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1018. qp->qp_access_flags = attr->qp_access_flags;
  1019. if (attr_mask & IB_QP_AV) {
  1020. qp->remote_ah_attr = attr->ah_attr;
  1021. qp->s_srate = attr->ah_attr.static_rate;
  1022. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  1023. }
  1024. if (attr_mask & IB_QP_ALT_PATH) {
  1025. qp->alt_ah_attr = attr->alt_ah_attr;
  1026. qp->s_alt_pkey_index = attr->alt_pkey_index;
  1027. }
  1028. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1029. qp->s_mig_state = attr->path_mig_state;
  1030. if (mig) {
  1031. qp->remote_ah_attr = qp->alt_ah_attr;
  1032. qp->port_num = qp->alt_ah_attr.port_num;
  1033. qp->s_pkey_index = qp->s_alt_pkey_index;
  1034. }
  1035. }
  1036. if (attr_mask & IB_QP_PATH_MTU) {
  1037. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  1038. qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  1039. qp->log_pmtu = ilog2(qp->pmtu);
  1040. }
  1041. if (attr_mask & IB_QP_RETRY_CNT) {
  1042. qp->s_retry_cnt = attr->retry_cnt;
  1043. qp->s_retry = attr->retry_cnt;
  1044. }
  1045. if (attr_mask & IB_QP_RNR_RETRY) {
  1046. qp->s_rnr_retry_cnt = attr->rnr_retry;
  1047. qp->s_rnr_retry = attr->rnr_retry;
  1048. }
  1049. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1050. qp->r_min_rnr_timer = attr->min_rnr_timer;
  1051. if (attr_mask & IB_QP_TIMEOUT) {
  1052. qp->timeout = attr->timeout;
  1053. qp->timeout_jiffies =
  1054. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  1055. 1000UL);
  1056. }
  1057. if (attr_mask & IB_QP_QKEY)
  1058. qp->qkey = attr->qkey;
  1059. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1060. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  1061. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1062. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1063. if (rdi->driver_f.modify_qp)
  1064. rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
  1065. spin_unlock(&qp->s_lock);
  1066. spin_unlock(&qp->s_hlock);
  1067. spin_unlock_irq(&qp->r_lock);
  1068. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1069. rvt_insert_qp(rdi, qp);
  1070. if (lastwqe) {
  1071. ev.device = qp->ibqp.device;
  1072. ev.element.qp = &qp->ibqp;
  1073. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1074. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1075. }
  1076. if (mig) {
  1077. ev.device = qp->ibqp.device;
  1078. ev.element.qp = &qp->ibqp;
  1079. ev.event = IB_EVENT_PATH_MIG;
  1080. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1081. }
  1082. return 0;
  1083. inval:
  1084. spin_unlock(&qp->s_lock);
  1085. spin_unlock(&qp->s_hlock);
  1086. spin_unlock_irq(&qp->r_lock);
  1087. return -EINVAL;
  1088. }
  1089. /** rvt_free_qpn - Free a qpn from the bit map
  1090. * @qpt: QP table
  1091. * @qpn: queue pair number to free
  1092. */
  1093. static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  1094. {
  1095. struct rvt_qpn_map *map;
  1096. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  1097. if (map->page)
  1098. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  1099. }
  1100. /**
  1101. * rvt_destroy_qp - destroy a queue pair
  1102. * @ibqp: the queue pair to destroy
  1103. *
  1104. * Note that this can be called while the QP is actively sending or
  1105. * receiving!
  1106. *
  1107. * Return: 0 on success.
  1108. */
  1109. int rvt_destroy_qp(struct ib_qp *ibqp)
  1110. {
  1111. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1112. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1113. spin_lock_irq(&qp->r_lock);
  1114. spin_lock(&qp->s_hlock);
  1115. spin_lock(&qp->s_lock);
  1116. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1117. spin_unlock(&qp->s_lock);
  1118. spin_unlock(&qp->s_hlock);
  1119. spin_unlock_irq(&qp->r_lock);
  1120. /* qpn is now available for use again */
  1121. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1122. spin_lock(&rdi->n_qps_lock);
  1123. rdi->n_qps_allocated--;
  1124. if (qp->ibqp.qp_type == IB_QPT_RC) {
  1125. rdi->n_rc_qps--;
  1126. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1127. }
  1128. spin_unlock(&rdi->n_qps_lock);
  1129. if (qp->ip)
  1130. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1131. else
  1132. vfree(qp->r_rq.wq);
  1133. vfree(qp->s_wq);
  1134. rdi->driver_f.qp_priv_free(rdi, qp);
  1135. kfree(qp->s_ack_queue);
  1136. kfree(qp);
  1137. return 0;
  1138. }
  1139. /**
  1140. * rvt_query_qp - query an ipbq
  1141. * @ibqp: IB qp to query
  1142. * @attr: attr struct to fill in
  1143. * @attr_mask: attr mask ignored
  1144. * @init_attr: struct to fill in
  1145. *
  1146. * Return: always 0
  1147. */
  1148. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1149. int attr_mask, struct ib_qp_init_attr *init_attr)
  1150. {
  1151. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1152. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1153. attr->qp_state = qp->state;
  1154. attr->cur_qp_state = attr->qp_state;
  1155. attr->path_mtu = qp->path_mtu;
  1156. attr->path_mig_state = qp->s_mig_state;
  1157. attr->qkey = qp->qkey;
  1158. attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
  1159. attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
  1160. attr->dest_qp_num = qp->remote_qpn;
  1161. attr->qp_access_flags = qp->qp_access_flags;
  1162. attr->cap.max_send_wr = qp->s_size - 1 -
  1163. rdi->dparms.reserved_operations;
  1164. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  1165. attr->cap.max_send_sge = qp->s_max_sge;
  1166. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  1167. attr->cap.max_inline_data = 0;
  1168. attr->ah_attr = qp->remote_ah_attr;
  1169. attr->alt_ah_attr = qp->alt_ah_attr;
  1170. attr->pkey_index = qp->s_pkey_index;
  1171. attr->alt_pkey_index = qp->s_alt_pkey_index;
  1172. attr->en_sqd_async_notify = 0;
  1173. attr->sq_draining = qp->s_draining;
  1174. attr->max_rd_atomic = qp->s_max_rd_atomic;
  1175. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  1176. attr->min_rnr_timer = qp->r_min_rnr_timer;
  1177. attr->port_num = qp->port_num;
  1178. attr->timeout = qp->timeout;
  1179. attr->retry_cnt = qp->s_retry_cnt;
  1180. attr->rnr_retry = qp->s_rnr_retry_cnt;
  1181. attr->alt_port_num = qp->alt_ah_attr.port_num;
  1182. attr->alt_timeout = qp->alt_timeout;
  1183. init_attr->event_handler = qp->ibqp.event_handler;
  1184. init_attr->qp_context = qp->ibqp.qp_context;
  1185. init_attr->send_cq = qp->ibqp.send_cq;
  1186. init_attr->recv_cq = qp->ibqp.recv_cq;
  1187. init_attr->srq = qp->ibqp.srq;
  1188. init_attr->cap = attr->cap;
  1189. if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
  1190. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  1191. else
  1192. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  1193. init_attr->qp_type = qp->ibqp.qp_type;
  1194. init_attr->port_num = qp->port_num;
  1195. return 0;
  1196. }
  1197. /**
  1198. * rvt_post_receive - post a receive on a QP
  1199. * @ibqp: the QP to post the receive on
  1200. * @wr: the WR to post
  1201. * @bad_wr: the first bad WR is put here
  1202. *
  1203. * This may be called from interrupt context.
  1204. *
  1205. * Return: 0 on success otherwise errno
  1206. */
  1207. int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1208. struct ib_recv_wr **bad_wr)
  1209. {
  1210. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1211. struct rvt_rwq *wq = qp->r_rq.wq;
  1212. unsigned long flags;
  1213. int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
  1214. !qp->ibqp.srq;
  1215. /* Check that state is OK to post receive. */
  1216. if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
  1217. *bad_wr = wr;
  1218. return -EINVAL;
  1219. }
  1220. for (; wr; wr = wr->next) {
  1221. struct rvt_rwqe *wqe;
  1222. u32 next;
  1223. int i;
  1224. if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
  1225. *bad_wr = wr;
  1226. return -EINVAL;
  1227. }
  1228. spin_lock_irqsave(&qp->r_rq.lock, flags);
  1229. next = wq->head + 1;
  1230. if (next >= qp->r_rq.size)
  1231. next = 0;
  1232. if (next == wq->tail) {
  1233. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1234. *bad_wr = wr;
  1235. return -ENOMEM;
  1236. }
  1237. if (unlikely(qp_err_flush)) {
  1238. struct ib_wc wc;
  1239. memset(&wc, 0, sizeof(wc));
  1240. wc.qp = &qp->ibqp;
  1241. wc.opcode = IB_WC_RECV;
  1242. wc.wr_id = wr->wr_id;
  1243. wc.status = IB_WC_WR_FLUSH_ERR;
  1244. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1245. } else {
  1246. wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
  1247. wqe->wr_id = wr->wr_id;
  1248. wqe->num_sge = wr->num_sge;
  1249. for (i = 0; i < wr->num_sge; i++)
  1250. wqe->sg_list[i] = wr->sg_list[i];
  1251. /*
  1252. * Make sure queue entry is written
  1253. * before the head index.
  1254. */
  1255. smp_wmb();
  1256. wq->head = next;
  1257. }
  1258. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1259. }
  1260. return 0;
  1261. }
  1262. /**
  1263. * rvt_qp_valid_operation - validate post send wr request
  1264. * @qp - the qp
  1265. * @post-parms - the post send table for the driver
  1266. * @wr - the work request
  1267. *
  1268. * The routine validates the operation based on the
  1269. * validation table an returns the length of the operation
  1270. * which can extend beyond the ib_send_bw. Operation
  1271. * dependent flags key atomic operation validation.
  1272. *
  1273. * There is an exception for UD qps that validates the pd and
  1274. * overrides the length to include the additional UD specific
  1275. * length.
  1276. *
  1277. * Returns a negative error or the length of the work request
  1278. * for building the swqe.
  1279. */
  1280. static inline int rvt_qp_valid_operation(
  1281. struct rvt_qp *qp,
  1282. const struct rvt_operation_params *post_parms,
  1283. struct ib_send_wr *wr)
  1284. {
  1285. int len;
  1286. if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
  1287. return -EINVAL;
  1288. if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
  1289. return -EINVAL;
  1290. if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
  1291. ibpd_to_rvtpd(qp->ibqp.pd)->user)
  1292. return -EINVAL;
  1293. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
  1294. (wr->num_sge == 0 ||
  1295. wr->sg_list[0].length < sizeof(u64) ||
  1296. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  1297. return -EINVAL;
  1298. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
  1299. !qp->s_max_rd_atomic)
  1300. return -EINVAL;
  1301. len = post_parms[wr->opcode].length;
  1302. /* UD specific */
  1303. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1304. qp->ibqp.qp_type != IB_QPT_RC) {
  1305. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1306. return -EINVAL;
  1307. len = sizeof(struct ib_ud_wr);
  1308. }
  1309. return len;
  1310. }
  1311. /**
  1312. * rvt_qp_is_avail - determine queue capacity
  1313. * @qp - the qp
  1314. * @rdi - the rdmavt device
  1315. * @reserved_op - is reserved operation
  1316. *
  1317. * This assumes the s_hlock is held but the s_last
  1318. * qp variable is uncontrolled.
  1319. *
  1320. * For non reserved operations, the qp->s_avail
  1321. * may be changed.
  1322. *
  1323. * The return value is zero or a -ENOMEM.
  1324. */
  1325. static inline int rvt_qp_is_avail(
  1326. struct rvt_qp *qp,
  1327. struct rvt_dev_info *rdi,
  1328. bool reserved_op)
  1329. {
  1330. u32 slast;
  1331. u32 avail;
  1332. u32 reserved_used;
  1333. /* see rvt_qp_wqe_unreserve() */
  1334. smp_mb__before_atomic();
  1335. reserved_used = atomic_read(&qp->s_reserved_used);
  1336. if (unlikely(reserved_op)) {
  1337. /* see rvt_qp_wqe_unreserve() */
  1338. smp_mb__before_atomic();
  1339. if (reserved_used >= rdi->dparms.reserved_operations)
  1340. return -ENOMEM;
  1341. return 0;
  1342. }
  1343. /* non-reserved operations */
  1344. if (likely(qp->s_avail))
  1345. return 0;
  1346. smp_read_barrier_depends(); /* see rc.c */
  1347. slast = ACCESS_ONCE(qp->s_last);
  1348. if (qp->s_head >= slast)
  1349. avail = qp->s_size - (qp->s_head - slast);
  1350. else
  1351. avail = slast - qp->s_head;
  1352. /* see rvt_qp_wqe_unreserve() */
  1353. smp_mb__before_atomic();
  1354. reserved_used = atomic_read(&qp->s_reserved_used);
  1355. avail = avail - 1 -
  1356. (rdi->dparms.reserved_operations - reserved_used);
  1357. /* insure we don't assign a negative s_avail */
  1358. if ((s32)avail <= 0)
  1359. return -ENOMEM;
  1360. qp->s_avail = avail;
  1361. if (WARN_ON(qp->s_avail >
  1362. (qp->s_size - 1 - rdi->dparms.reserved_operations)))
  1363. rvt_pr_err(rdi,
  1364. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1365. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1366. qp->s_head, qp->s_tail, qp->s_cur,
  1367. qp->s_acked, qp->s_last);
  1368. return 0;
  1369. }
  1370. /**
  1371. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1372. * @qp: the QP to post on
  1373. * @wr: the work request to send
  1374. */
  1375. static int rvt_post_one_wr(struct rvt_qp *qp,
  1376. struct ib_send_wr *wr,
  1377. int *call_send)
  1378. {
  1379. struct rvt_swqe *wqe;
  1380. u32 next;
  1381. int i;
  1382. int j;
  1383. int acc;
  1384. struct rvt_lkey_table *rkt;
  1385. struct rvt_pd *pd;
  1386. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1387. u8 log_pmtu;
  1388. int ret;
  1389. size_t cplen;
  1390. bool reserved_op;
  1391. int local_ops_delayed = 0;
  1392. BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
  1393. /* IB spec says that num_sge == 0 is OK. */
  1394. if (unlikely(wr->num_sge > qp->s_max_sge))
  1395. return -EINVAL;
  1396. ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
  1397. if (ret < 0)
  1398. return ret;
  1399. cplen = ret;
  1400. /*
  1401. * Local operations include fast register and local invalidate.
  1402. * Fast register needs to be processed immediately because the
  1403. * registered lkey may be used by following work requests and the
  1404. * lkey needs to be valid at the time those requests are posted.
  1405. * Local invalidate can be processed immediately if fencing is
  1406. * not required and no previous local invalidate ops are pending.
  1407. * Signaled local operations that have been processed immediately
  1408. * need to have requests with "completion only" flags set posted
  1409. * to the send queue in order to generate completions.
  1410. */
  1411. if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
  1412. switch (wr->opcode) {
  1413. case IB_WR_REG_MR:
  1414. ret = rvt_fast_reg_mr(qp,
  1415. reg_wr(wr)->mr,
  1416. reg_wr(wr)->key,
  1417. reg_wr(wr)->access);
  1418. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1419. return ret;
  1420. break;
  1421. case IB_WR_LOCAL_INV:
  1422. if ((wr->send_flags & IB_SEND_FENCE) ||
  1423. atomic_read(&qp->local_ops_pending)) {
  1424. local_ops_delayed = 1;
  1425. } else {
  1426. ret = rvt_invalidate_rkey(
  1427. qp, wr->ex.invalidate_rkey);
  1428. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1429. return ret;
  1430. }
  1431. break;
  1432. default:
  1433. return -EINVAL;
  1434. }
  1435. }
  1436. reserved_op = rdi->post_parms[wr->opcode].flags &
  1437. RVT_OPERATION_USE_RESERVE;
  1438. /* check for avail */
  1439. ret = rvt_qp_is_avail(qp, rdi, reserved_op);
  1440. if (ret)
  1441. return ret;
  1442. next = qp->s_head + 1;
  1443. if (next >= qp->s_size)
  1444. next = 0;
  1445. rkt = &rdi->lkey_table;
  1446. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1447. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1448. /* cplen has length from above */
  1449. memcpy(&wqe->wr, wr, cplen);
  1450. wqe->length = 0;
  1451. j = 0;
  1452. if (wr->num_sge) {
  1453. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1454. IB_ACCESS_LOCAL_WRITE : 0;
  1455. for (i = 0; i < wr->num_sge; i++) {
  1456. u32 length = wr->sg_list[i].length;
  1457. int ok;
  1458. if (length == 0)
  1459. continue;
  1460. ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
  1461. &wr->sg_list[i], acc);
  1462. if (!ok) {
  1463. ret = -EINVAL;
  1464. goto bail_inval_free;
  1465. }
  1466. wqe->length += length;
  1467. j++;
  1468. }
  1469. wqe->wr.num_sge = j;
  1470. }
  1471. /* general part of wqe valid - allow for driver checks */
  1472. if (rdi->driver_f.check_send_wqe) {
  1473. ret = rdi->driver_f.check_send_wqe(qp, wqe);
  1474. if (ret < 0)
  1475. goto bail_inval_free;
  1476. if (ret)
  1477. *call_send = ret;
  1478. }
  1479. log_pmtu = qp->log_pmtu;
  1480. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1481. qp->ibqp.qp_type != IB_QPT_RC) {
  1482. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1483. log_pmtu = ah->log_pmtu;
  1484. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1485. }
  1486. if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
  1487. if (local_ops_delayed)
  1488. atomic_inc(&qp->local_ops_pending);
  1489. else
  1490. wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
  1491. wqe->ssn = 0;
  1492. wqe->psn = 0;
  1493. wqe->lpsn = 0;
  1494. } else {
  1495. wqe->ssn = qp->s_ssn++;
  1496. wqe->psn = qp->s_next_psn;
  1497. wqe->lpsn = wqe->psn +
  1498. (wqe->length ?
  1499. ((wqe->length - 1) >> log_pmtu) :
  1500. 0);
  1501. qp->s_next_psn = wqe->lpsn + 1;
  1502. }
  1503. trace_rvt_post_one_wr(qp, wqe);
  1504. if (unlikely(reserved_op))
  1505. rvt_qp_wqe_reserve(qp, wqe);
  1506. else
  1507. qp->s_avail--;
  1508. smp_wmb(); /* see request builders */
  1509. qp->s_head = next;
  1510. return 0;
  1511. bail_inval_free:
  1512. /* release mr holds */
  1513. while (j) {
  1514. struct rvt_sge *sge = &wqe->sg_list[--j];
  1515. rvt_put_mr(sge->mr);
  1516. }
  1517. return ret;
  1518. }
  1519. /**
  1520. * rvt_post_send - post a send on a QP
  1521. * @ibqp: the QP to post the send on
  1522. * @wr: the list of work requests to post
  1523. * @bad_wr: the first bad WR is put here
  1524. *
  1525. * This may be called from interrupt context.
  1526. *
  1527. * Return: 0 on success else errno
  1528. */
  1529. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1530. struct ib_send_wr **bad_wr)
  1531. {
  1532. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1533. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1534. unsigned long flags = 0;
  1535. int call_send;
  1536. unsigned nreq = 0;
  1537. int err = 0;
  1538. spin_lock_irqsave(&qp->s_hlock, flags);
  1539. /*
  1540. * Ensure QP state is such that we can send. If not bail out early,
  1541. * there is no need to do this every time we post a send.
  1542. */
  1543. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1544. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1545. return -EINVAL;
  1546. }
  1547. /*
  1548. * If the send queue is empty, and we only have a single WR then just go
  1549. * ahead and kick the send engine into gear. Otherwise we will always
  1550. * just schedule the send to happen later.
  1551. */
  1552. call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
  1553. for (; wr; wr = wr->next) {
  1554. err = rvt_post_one_wr(qp, wr, &call_send);
  1555. if (unlikely(err)) {
  1556. *bad_wr = wr;
  1557. goto bail;
  1558. }
  1559. nreq++;
  1560. }
  1561. bail:
  1562. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1563. if (nreq) {
  1564. if (call_send)
  1565. rdi->driver_f.do_send(qp);
  1566. else
  1567. rdi->driver_f.schedule_send_no_lock(qp);
  1568. }
  1569. return err;
  1570. }
  1571. /**
  1572. * rvt_post_srq_receive - post a receive on a shared receive queue
  1573. * @ibsrq: the SRQ to post the receive on
  1574. * @wr: the list of work requests to post
  1575. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1576. *
  1577. * This may be called from interrupt context.
  1578. *
  1579. * Return: 0 on success else errno
  1580. */
  1581. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1582. struct ib_recv_wr **bad_wr)
  1583. {
  1584. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1585. struct rvt_rwq *wq;
  1586. unsigned long flags;
  1587. for (; wr; wr = wr->next) {
  1588. struct rvt_rwqe *wqe;
  1589. u32 next;
  1590. int i;
  1591. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1592. *bad_wr = wr;
  1593. return -EINVAL;
  1594. }
  1595. spin_lock_irqsave(&srq->rq.lock, flags);
  1596. wq = srq->rq.wq;
  1597. next = wq->head + 1;
  1598. if (next >= srq->rq.size)
  1599. next = 0;
  1600. if (next == wq->tail) {
  1601. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1602. *bad_wr = wr;
  1603. return -ENOMEM;
  1604. }
  1605. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1606. wqe->wr_id = wr->wr_id;
  1607. wqe->num_sge = wr->num_sge;
  1608. for (i = 0; i < wr->num_sge; i++)
  1609. wqe->sg_list[i] = wr->sg_list[i];
  1610. /* Make sure queue entry is written before the head index. */
  1611. smp_wmb();
  1612. wq->head = next;
  1613. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1614. }
  1615. return 0;
  1616. }