qp.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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 bits at shift and below */
  329. WARN_ON(offset & (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. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  352. rvt_put_ss(&qp->s_rdma_read_sge);
  353. rvt_put_ss(&qp->r_sge);
  354. if (clr_sends) {
  355. while (qp->s_last != qp->s_head) {
  356. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  357. unsigned i;
  358. for (i = 0; i < wqe->wr.num_sge; i++) {
  359. struct rvt_sge *sge = &wqe->sg_list[i];
  360. rvt_put_mr(sge->mr);
  361. }
  362. if (qp->ibqp.qp_type == IB_QPT_UD ||
  363. qp->ibqp.qp_type == IB_QPT_SMI ||
  364. qp->ibqp.qp_type == IB_QPT_GSI)
  365. atomic_dec(&ibah_to_rvtah(
  366. wqe->ud_wr.ah)->refcount);
  367. if (++qp->s_last >= qp->s_size)
  368. qp->s_last = 0;
  369. smp_wmb(); /* see qp_set_savail */
  370. }
  371. if (qp->s_rdma_mr) {
  372. rvt_put_mr(qp->s_rdma_mr);
  373. qp->s_rdma_mr = NULL;
  374. }
  375. }
  376. if (qp->ibqp.qp_type != IB_QPT_RC)
  377. return;
  378. for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
  379. struct rvt_ack_entry *e = &qp->s_ack_queue[n];
  380. if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
  381. 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. {
  442. if (qp->state != IB_QPS_RESET) {
  443. qp->state = IB_QPS_RESET;
  444. /* Let drivers flush their waitlist */
  445. rdi->driver_f.flush_qp_waiters(qp);
  446. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
  447. spin_unlock(&qp->s_lock);
  448. spin_unlock(&qp->s_hlock);
  449. spin_unlock_irq(&qp->r_lock);
  450. /* Stop the send queue and the retry timer */
  451. rdi->driver_f.stop_send_queue(qp);
  452. /* Wait for things to stop */
  453. rdi->driver_f.quiesce_qp(qp);
  454. /* take qp out the hash and wait for it to be unused */
  455. rvt_remove_qp(rdi, qp);
  456. wait_event(qp->wait, !atomic_read(&qp->refcount));
  457. /* grab the lock b/c it was locked at call time */
  458. spin_lock_irq(&qp->r_lock);
  459. spin_lock(&qp->s_hlock);
  460. spin_lock(&qp->s_lock);
  461. rvt_clear_mr_refs(qp, 1);
  462. }
  463. /*
  464. * Let the driver do any tear down it needs to for a qp
  465. * that has been reset
  466. */
  467. rdi->driver_f.notify_qp_reset(qp);
  468. qp->remote_qpn = 0;
  469. qp->qkey = 0;
  470. qp->qp_access_flags = 0;
  471. qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
  472. qp->s_hdrwords = 0;
  473. qp->s_wqe = NULL;
  474. qp->s_draining = 0;
  475. qp->s_next_psn = 0;
  476. qp->s_last_psn = 0;
  477. qp->s_sending_psn = 0;
  478. qp->s_sending_hpsn = 0;
  479. qp->s_psn = 0;
  480. qp->r_psn = 0;
  481. qp->r_msn = 0;
  482. if (type == IB_QPT_RC) {
  483. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  484. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  485. } else {
  486. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  487. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  488. }
  489. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  490. qp->r_nak_state = 0;
  491. qp->r_aflags = 0;
  492. qp->r_flags = 0;
  493. qp->s_head = 0;
  494. qp->s_tail = 0;
  495. qp->s_cur = 0;
  496. qp->s_acked = 0;
  497. qp->s_last = 0;
  498. qp->s_ssn = 1;
  499. qp->s_lsn = 0;
  500. qp->s_mig_state = IB_MIG_MIGRATED;
  501. memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
  502. qp->r_head_ack_queue = 0;
  503. qp->s_tail_ack_queue = 0;
  504. qp->s_num_rd_atomic = 0;
  505. if (qp->r_rq.wq) {
  506. qp->r_rq.wq->head = 0;
  507. qp->r_rq.wq->tail = 0;
  508. }
  509. qp->r_sge.num_sge = 0;
  510. }
  511. /**
  512. * rvt_create_qp - create a queue pair for a device
  513. * @ibpd: the protection domain who's device we create the queue pair for
  514. * @init_attr: the attributes of the queue pair
  515. * @udata: user data for libibverbs.so
  516. *
  517. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  518. * unique idea of what queue pair numbers mean. For instance there is a reserved
  519. * range for PSM.
  520. *
  521. * Return: the queue pair on success, otherwise returns an errno.
  522. *
  523. * Called by the ib_create_qp() core verbs function.
  524. */
  525. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  526. struct ib_qp_init_attr *init_attr,
  527. struct ib_udata *udata)
  528. {
  529. struct rvt_qp *qp;
  530. int err;
  531. struct rvt_swqe *swq = NULL;
  532. size_t sz;
  533. size_t sg_list_sz;
  534. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  535. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  536. void *priv = NULL;
  537. gfp_t gfp;
  538. if (!rdi)
  539. return ERR_PTR(-EINVAL);
  540. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
  541. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  542. init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
  543. return ERR_PTR(-EINVAL);
  544. /* GFP_NOIO is applicable to RC QP's only */
  545. if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
  546. init_attr->qp_type != IB_QPT_RC)
  547. return ERR_PTR(-EINVAL);
  548. gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
  549. GFP_NOIO : GFP_KERNEL;
  550. /* Check receive queue parameters if no SRQ is specified. */
  551. if (!init_attr->srq) {
  552. if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
  553. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  554. return ERR_PTR(-EINVAL);
  555. if (init_attr->cap.max_send_sge +
  556. init_attr->cap.max_send_wr +
  557. init_attr->cap.max_recv_sge +
  558. init_attr->cap.max_recv_wr == 0)
  559. return ERR_PTR(-EINVAL);
  560. }
  561. switch (init_attr->qp_type) {
  562. case IB_QPT_SMI:
  563. case IB_QPT_GSI:
  564. if (init_attr->port_num == 0 ||
  565. init_attr->port_num > ibpd->device->phys_port_cnt)
  566. return ERR_PTR(-EINVAL);
  567. case IB_QPT_UC:
  568. case IB_QPT_RC:
  569. case IB_QPT_UD:
  570. sz = sizeof(struct rvt_sge) *
  571. init_attr->cap.max_send_sge +
  572. sizeof(struct rvt_swqe);
  573. if (gfp == GFP_NOIO)
  574. swq = __vmalloc(
  575. (init_attr->cap.max_send_wr + 1) * sz,
  576. gfp, PAGE_KERNEL);
  577. else
  578. swq = vmalloc_node(
  579. (init_attr->cap.max_send_wr + 1) * sz,
  580. rdi->dparms.node);
  581. if (!swq)
  582. return ERR_PTR(-ENOMEM);
  583. sz = sizeof(*qp);
  584. sg_list_sz = 0;
  585. if (init_attr->srq) {
  586. struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
  587. if (srq->rq.max_sge > 1)
  588. sg_list_sz = sizeof(*qp->r_sg_list) *
  589. (srq->rq.max_sge - 1);
  590. } else if (init_attr->cap.max_recv_sge > 1)
  591. sg_list_sz = sizeof(*qp->r_sg_list) *
  592. (init_attr->cap.max_recv_sge - 1);
  593. qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
  594. if (!qp)
  595. goto bail_swq;
  596. RCU_INIT_POINTER(qp->next, NULL);
  597. /*
  598. * Driver needs to set up it's private QP structure and do any
  599. * initialization that is needed.
  600. */
  601. priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
  602. if (!priv)
  603. goto bail_qp;
  604. qp->priv = priv;
  605. qp->timeout_jiffies =
  606. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  607. 1000UL);
  608. if (init_attr->srq) {
  609. sz = 0;
  610. } else {
  611. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  612. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  613. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  614. sizeof(struct rvt_rwqe);
  615. if (udata)
  616. qp->r_rq.wq = vmalloc_user(
  617. sizeof(struct rvt_rwq) +
  618. qp->r_rq.size * sz);
  619. else if (gfp == GFP_NOIO)
  620. qp->r_rq.wq = __vmalloc(
  621. sizeof(struct rvt_rwq) +
  622. qp->r_rq.size * sz,
  623. gfp, PAGE_KERNEL);
  624. else
  625. qp->r_rq.wq = vmalloc_node(
  626. sizeof(struct rvt_rwq) +
  627. qp->r_rq.size * sz,
  628. rdi->dparms.node);
  629. if (!qp->r_rq.wq)
  630. goto bail_driver_priv;
  631. }
  632. /*
  633. * ib_create_qp() will initialize qp->ibqp
  634. * except for qp->ibqp.qp_num.
  635. */
  636. spin_lock_init(&qp->r_lock);
  637. spin_lock_init(&qp->s_hlock);
  638. spin_lock_init(&qp->s_lock);
  639. spin_lock_init(&qp->r_rq.lock);
  640. atomic_set(&qp->refcount, 0);
  641. init_waitqueue_head(&qp->wait);
  642. init_timer(&qp->s_timer);
  643. qp->s_timer.data = (unsigned long)qp;
  644. INIT_LIST_HEAD(&qp->rspwait);
  645. qp->state = IB_QPS_RESET;
  646. qp->s_wq = swq;
  647. qp->s_size = init_attr->cap.max_send_wr + 1;
  648. qp->s_avail = init_attr->cap.max_send_wr;
  649. qp->s_max_sge = init_attr->cap.max_send_sge;
  650. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  651. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  652. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  653. init_attr->qp_type,
  654. init_attr->port_num, gfp);
  655. if (err < 0) {
  656. ret = ERR_PTR(err);
  657. goto bail_rq_wq;
  658. }
  659. qp->ibqp.qp_num = err;
  660. qp->port_num = init_attr->port_num;
  661. rvt_reset_qp(rdi, qp, init_attr->qp_type);
  662. break;
  663. default:
  664. /* Don't support raw QPs */
  665. return ERR_PTR(-EINVAL);
  666. }
  667. init_attr->cap.max_inline_data = 0;
  668. /*
  669. * Return the address of the RWQ as the offset to mmap.
  670. * See rvt_mmap() for details.
  671. */
  672. if (udata && udata->outlen >= sizeof(__u64)) {
  673. if (!qp->r_rq.wq) {
  674. __u64 offset = 0;
  675. err = ib_copy_to_udata(udata, &offset,
  676. sizeof(offset));
  677. if (err) {
  678. ret = ERR_PTR(err);
  679. goto bail_qpn;
  680. }
  681. } else {
  682. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  683. qp->ip = rvt_create_mmap_info(rdi, s,
  684. ibpd->uobject->context,
  685. qp->r_rq.wq);
  686. if (!qp->ip) {
  687. ret = ERR_PTR(-ENOMEM);
  688. goto bail_qpn;
  689. }
  690. err = ib_copy_to_udata(udata, &qp->ip->offset,
  691. sizeof(qp->ip->offset));
  692. if (err) {
  693. ret = ERR_PTR(err);
  694. goto bail_ip;
  695. }
  696. }
  697. qp->pid = current->pid;
  698. }
  699. spin_lock(&rdi->n_qps_lock);
  700. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  701. spin_unlock(&rdi->n_qps_lock);
  702. ret = ERR_PTR(-ENOMEM);
  703. goto bail_ip;
  704. }
  705. rdi->n_qps_allocated++;
  706. /*
  707. * Maintain a busy_jiffies variable that will be added to the timeout
  708. * period in mod_retry_timer and add_retry_timer. This busy jiffies
  709. * is scaled by the number of rc qps created for the device to reduce
  710. * the number of timeouts occurring when there is a large number of
  711. * qps. busy_jiffies is incremented every rc qp scaling interval.
  712. * The scaling interval is selected based on extensive performance
  713. * evaluation of targeted workloads.
  714. */
  715. if (init_attr->qp_type == IB_QPT_RC) {
  716. rdi->n_rc_qps++;
  717. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  718. }
  719. spin_unlock(&rdi->n_qps_lock);
  720. if (qp->ip) {
  721. spin_lock_irq(&rdi->pending_lock);
  722. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  723. spin_unlock_irq(&rdi->pending_lock);
  724. }
  725. ret = &qp->ibqp;
  726. /*
  727. * We have our QP and its good, now keep track of what types of opcodes
  728. * can be processed on this QP. We do this by keeping track of what the
  729. * 3 high order bits of the opcode are.
  730. */
  731. switch (init_attr->qp_type) {
  732. case IB_QPT_SMI:
  733. case IB_QPT_GSI:
  734. case IB_QPT_UD:
  735. qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & RVT_OPCODE_QP_MASK;
  736. break;
  737. case IB_QPT_RC:
  738. qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & RVT_OPCODE_QP_MASK;
  739. break;
  740. case IB_QPT_UC:
  741. qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & RVT_OPCODE_QP_MASK;
  742. break;
  743. default:
  744. ret = ERR_PTR(-EINVAL);
  745. goto bail_ip;
  746. }
  747. return ret;
  748. bail_ip:
  749. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  750. bail_qpn:
  751. free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  752. bail_rq_wq:
  753. vfree(qp->r_rq.wq);
  754. bail_driver_priv:
  755. rdi->driver_f.qp_priv_free(rdi, qp);
  756. bail_qp:
  757. kfree(qp);
  758. bail_swq:
  759. vfree(swq);
  760. return ret;
  761. }
  762. /**
  763. * rvt_error_qp - put a QP into the error state
  764. * @qp: the QP to put into the error state
  765. * @err: the receive completion error to signal if a RWQE is active
  766. *
  767. * Flushes both send and receive work queues.
  768. *
  769. * Return: true if last WQE event should be generated.
  770. * The QP r_lock and s_lock should be held and interrupts disabled.
  771. * If we are already in error state, just return.
  772. */
  773. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  774. {
  775. struct ib_wc wc;
  776. int ret = 0;
  777. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  778. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  779. goto bail;
  780. qp->state = IB_QPS_ERR;
  781. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  782. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  783. del_timer(&qp->s_timer);
  784. }
  785. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  786. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  787. rdi->driver_f.notify_error_qp(qp);
  788. /* Schedule the sending tasklet to drain the send work queue. */
  789. if (ACCESS_ONCE(qp->s_last) != qp->s_head)
  790. rdi->driver_f.schedule_send(qp);
  791. rvt_clear_mr_refs(qp, 0);
  792. memset(&wc, 0, sizeof(wc));
  793. wc.qp = &qp->ibqp;
  794. wc.opcode = IB_WC_RECV;
  795. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  796. wc.wr_id = qp->r_wr_id;
  797. wc.status = err;
  798. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  799. }
  800. wc.status = IB_WC_WR_FLUSH_ERR;
  801. if (qp->r_rq.wq) {
  802. struct rvt_rwq *wq;
  803. u32 head;
  804. u32 tail;
  805. spin_lock(&qp->r_rq.lock);
  806. /* sanity check pointers before trusting them */
  807. wq = qp->r_rq.wq;
  808. head = wq->head;
  809. if (head >= qp->r_rq.size)
  810. head = 0;
  811. tail = wq->tail;
  812. if (tail >= qp->r_rq.size)
  813. tail = 0;
  814. while (tail != head) {
  815. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  816. if (++tail >= qp->r_rq.size)
  817. tail = 0;
  818. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  819. }
  820. wq->tail = tail;
  821. spin_unlock(&qp->r_rq.lock);
  822. } else if (qp->ibqp.event_handler) {
  823. ret = 1;
  824. }
  825. bail:
  826. return ret;
  827. }
  828. EXPORT_SYMBOL(rvt_error_qp);
  829. /*
  830. * Put the QP into the hash table.
  831. * The hash table holds a reference to the QP.
  832. */
  833. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  834. {
  835. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  836. unsigned long flags;
  837. atomic_inc(&qp->refcount);
  838. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  839. if (qp->ibqp.qp_num <= 1) {
  840. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  841. } else {
  842. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  843. qp->next = rdi->qp_dev->qp_table[n];
  844. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  845. trace_rvt_qpinsert(qp, n);
  846. }
  847. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  848. }
  849. /**
  850. * qib_modify_qp - modify the attributes of a queue pair
  851. * @ibqp: the queue pair who's attributes we're modifying
  852. * @attr: the new attributes
  853. * @attr_mask: the mask of attributes to modify
  854. * @udata: user data for libibverbs.so
  855. *
  856. * Return: 0 on success, otherwise returns an errno.
  857. */
  858. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  859. int attr_mask, struct ib_udata *udata)
  860. {
  861. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  862. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  863. enum ib_qp_state cur_state, new_state;
  864. struct ib_event ev;
  865. int lastwqe = 0;
  866. int mig = 0;
  867. int pmtu = 0; /* for gcc warning only */
  868. enum rdma_link_layer link;
  869. link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
  870. spin_lock_irq(&qp->r_lock);
  871. spin_lock(&qp->s_hlock);
  872. spin_lock(&qp->s_lock);
  873. cur_state = attr_mask & IB_QP_CUR_STATE ?
  874. attr->cur_qp_state : qp->state;
  875. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  876. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  877. attr_mask, link))
  878. goto inval;
  879. if (rdi->driver_f.check_modify_qp &&
  880. rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
  881. goto inval;
  882. if (attr_mask & IB_QP_AV) {
  883. if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
  884. goto inval;
  885. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  886. goto inval;
  887. }
  888. if (attr_mask & IB_QP_ALT_PATH) {
  889. if (attr->alt_ah_attr.dlid >=
  890. be16_to_cpu(IB_MULTICAST_LID_BASE))
  891. goto inval;
  892. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  893. goto inval;
  894. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  895. goto inval;
  896. }
  897. if (attr_mask & IB_QP_PKEY_INDEX)
  898. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  899. goto inval;
  900. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  901. if (attr->min_rnr_timer > 31)
  902. goto inval;
  903. if (attr_mask & IB_QP_PORT)
  904. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  905. qp->ibqp.qp_type == IB_QPT_GSI ||
  906. attr->port_num == 0 ||
  907. attr->port_num > ibqp->device->phys_port_cnt)
  908. goto inval;
  909. if (attr_mask & IB_QP_DEST_QPN)
  910. if (attr->dest_qp_num > RVT_QPN_MASK)
  911. goto inval;
  912. if (attr_mask & IB_QP_RETRY_CNT)
  913. if (attr->retry_cnt > 7)
  914. goto inval;
  915. if (attr_mask & IB_QP_RNR_RETRY)
  916. if (attr->rnr_retry > 7)
  917. goto inval;
  918. /*
  919. * Don't allow invalid path_mtu values. OK to set greater
  920. * than the active mtu (or even the max_cap, if we have tuned
  921. * that to a small mtu. We'll set qp->path_mtu
  922. * to the lesser of requested attribute mtu and active,
  923. * for packetizing messages.
  924. * Note that the QP port has to be set in INIT and MTU in RTR.
  925. */
  926. if (attr_mask & IB_QP_PATH_MTU) {
  927. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  928. if (pmtu < 0)
  929. goto inval;
  930. }
  931. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  932. if (attr->path_mig_state == IB_MIG_REARM) {
  933. if (qp->s_mig_state == IB_MIG_ARMED)
  934. goto inval;
  935. if (new_state != IB_QPS_RTS)
  936. goto inval;
  937. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  938. if (qp->s_mig_state == IB_MIG_REARM)
  939. goto inval;
  940. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  941. goto inval;
  942. if (qp->s_mig_state == IB_MIG_ARMED)
  943. mig = 1;
  944. } else {
  945. goto inval;
  946. }
  947. }
  948. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  949. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  950. goto inval;
  951. switch (new_state) {
  952. case IB_QPS_RESET:
  953. if (qp->state != IB_QPS_RESET)
  954. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  955. break;
  956. case IB_QPS_RTR:
  957. /* Allow event to re-trigger if QP set to RTR more than once */
  958. qp->r_flags &= ~RVT_R_COMM_EST;
  959. qp->state = new_state;
  960. break;
  961. case IB_QPS_SQD:
  962. qp->s_draining = qp->s_last != qp->s_cur;
  963. qp->state = new_state;
  964. break;
  965. case IB_QPS_SQE:
  966. if (qp->ibqp.qp_type == IB_QPT_RC)
  967. goto inval;
  968. qp->state = new_state;
  969. break;
  970. case IB_QPS_ERR:
  971. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  972. break;
  973. default:
  974. qp->state = new_state;
  975. break;
  976. }
  977. if (attr_mask & IB_QP_PKEY_INDEX)
  978. qp->s_pkey_index = attr->pkey_index;
  979. if (attr_mask & IB_QP_PORT)
  980. qp->port_num = attr->port_num;
  981. if (attr_mask & IB_QP_DEST_QPN)
  982. qp->remote_qpn = attr->dest_qp_num;
  983. if (attr_mask & IB_QP_SQ_PSN) {
  984. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  985. qp->s_psn = qp->s_next_psn;
  986. qp->s_sending_psn = qp->s_next_psn;
  987. qp->s_last_psn = qp->s_next_psn - 1;
  988. qp->s_sending_hpsn = qp->s_last_psn;
  989. }
  990. if (attr_mask & IB_QP_RQ_PSN)
  991. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  992. if (attr_mask & IB_QP_ACCESS_FLAGS)
  993. qp->qp_access_flags = attr->qp_access_flags;
  994. if (attr_mask & IB_QP_AV) {
  995. qp->remote_ah_attr = attr->ah_attr;
  996. qp->s_srate = attr->ah_attr.static_rate;
  997. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  998. }
  999. if (attr_mask & IB_QP_ALT_PATH) {
  1000. qp->alt_ah_attr = attr->alt_ah_attr;
  1001. qp->s_alt_pkey_index = attr->alt_pkey_index;
  1002. }
  1003. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1004. qp->s_mig_state = attr->path_mig_state;
  1005. if (mig) {
  1006. qp->remote_ah_attr = qp->alt_ah_attr;
  1007. qp->port_num = qp->alt_ah_attr.port_num;
  1008. qp->s_pkey_index = qp->s_alt_pkey_index;
  1009. }
  1010. }
  1011. if (attr_mask & IB_QP_PATH_MTU) {
  1012. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  1013. qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  1014. qp->log_pmtu = ilog2(qp->pmtu);
  1015. }
  1016. if (attr_mask & IB_QP_RETRY_CNT) {
  1017. qp->s_retry_cnt = attr->retry_cnt;
  1018. qp->s_retry = attr->retry_cnt;
  1019. }
  1020. if (attr_mask & IB_QP_RNR_RETRY) {
  1021. qp->s_rnr_retry_cnt = attr->rnr_retry;
  1022. qp->s_rnr_retry = attr->rnr_retry;
  1023. }
  1024. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1025. qp->r_min_rnr_timer = attr->min_rnr_timer;
  1026. if (attr_mask & IB_QP_TIMEOUT) {
  1027. qp->timeout = attr->timeout;
  1028. qp->timeout_jiffies =
  1029. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  1030. 1000UL);
  1031. }
  1032. if (attr_mask & IB_QP_QKEY)
  1033. qp->qkey = attr->qkey;
  1034. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1035. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  1036. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1037. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1038. if (rdi->driver_f.modify_qp)
  1039. rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
  1040. spin_unlock(&qp->s_lock);
  1041. spin_unlock(&qp->s_hlock);
  1042. spin_unlock_irq(&qp->r_lock);
  1043. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1044. rvt_insert_qp(rdi, qp);
  1045. if (lastwqe) {
  1046. ev.device = qp->ibqp.device;
  1047. ev.element.qp = &qp->ibqp;
  1048. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1049. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1050. }
  1051. if (mig) {
  1052. ev.device = qp->ibqp.device;
  1053. ev.element.qp = &qp->ibqp;
  1054. ev.event = IB_EVENT_PATH_MIG;
  1055. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1056. }
  1057. return 0;
  1058. inval:
  1059. spin_unlock(&qp->s_lock);
  1060. spin_unlock(&qp->s_hlock);
  1061. spin_unlock_irq(&qp->r_lock);
  1062. return -EINVAL;
  1063. }
  1064. /** rvt_free_qpn - Free a qpn from the bit map
  1065. * @qpt: QP table
  1066. * @qpn: queue pair number to free
  1067. */
  1068. static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  1069. {
  1070. struct rvt_qpn_map *map;
  1071. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  1072. if (map->page)
  1073. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  1074. }
  1075. /**
  1076. * rvt_destroy_qp - destroy a queue pair
  1077. * @ibqp: the queue pair to destroy
  1078. *
  1079. * Note that this can be called while the QP is actively sending or
  1080. * receiving!
  1081. *
  1082. * Return: 0 on success.
  1083. */
  1084. int rvt_destroy_qp(struct ib_qp *ibqp)
  1085. {
  1086. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1087. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1088. spin_lock_irq(&qp->r_lock);
  1089. spin_lock(&qp->s_hlock);
  1090. spin_lock(&qp->s_lock);
  1091. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1092. spin_unlock(&qp->s_lock);
  1093. spin_unlock(&qp->s_hlock);
  1094. spin_unlock_irq(&qp->r_lock);
  1095. /* qpn is now available for use again */
  1096. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1097. spin_lock(&rdi->n_qps_lock);
  1098. rdi->n_qps_allocated--;
  1099. if (qp->ibqp.qp_type == IB_QPT_RC) {
  1100. rdi->n_rc_qps--;
  1101. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1102. }
  1103. spin_unlock(&rdi->n_qps_lock);
  1104. if (qp->ip)
  1105. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1106. else
  1107. vfree(qp->r_rq.wq);
  1108. vfree(qp->s_wq);
  1109. rdi->driver_f.qp_priv_free(rdi, qp);
  1110. kfree(qp);
  1111. return 0;
  1112. }
  1113. /**
  1114. * rvt_query_qp - query an ipbq
  1115. * @ibqp: IB qp to query
  1116. * @attr: attr struct to fill in
  1117. * @attr_mask: attr mask ignored
  1118. * @init_attr: struct to fill in
  1119. *
  1120. * Return: always 0
  1121. */
  1122. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1123. int attr_mask, struct ib_qp_init_attr *init_attr)
  1124. {
  1125. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1126. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1127. attr->qp_state = qp->state;
  1128. attr->cur_qp_state = attr->qp_state;
  1129. attr->path_mtu = qp->path_mtu;
  1130. attr->path_mig_state = qp->s_mig_state;
  1131. attr->qkey = qp->qkey;
  1132. attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
  1133. attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
  1134. attr->dest_qp_num = qp->remote_qpn;
  1135. attr->qp_access_flags = qp->qp_access_flags;
  1136. attr->cap.max_send_wr = qp->s_size - 1;
  1137. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  1138. attr->cap.max_send_sge = qp->s_max_sge;
  1139. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  1140. attr->cap.max_inline_data = 0;
  1141. attr->ah_attr = qp->remote_ah_attr;
  1142. attr->alt_ah_attr = qp->alt_ah_attr;
  1143. attr->pkey_index = qp->s_pkey_index;
  1144. attr->alt_pkey_index = qp->s_alt_pkey_index;
  1145. attr->en_sqd_async_notify = 0;
  1146. attr->sq_draining = qp->s_draining;
  1147. attr->max_rd_atomic = qp->s_max_rd_atomic;
  1148. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  1149. attr->min_rnr_timer = qp->r_min_rnr_timer;
  1150. attr->port_num = qp->port_num;
  1151. attr->timeout = qp->timeout;
  1152. attr->retry_cnt = qp->s_retry_cnt;
  1153. attr->rnr_retry = qp->s_rnr_retry_cnt;
  1154. attr->alt_port_num = qp->alt_ah_attr.port_num;
  1155. attr->alt_timeout = qp->alt_timeout;
  1156. init_attr->event_handler = qp->ibqp.event_handler;
  1157. init_attr->qp_context = qp->ibqp.qp_context;
  1158. init_attr->send_cq = qp->ibqp.send_cq;
  1159. init_attr->recv_cq = qp->ibqp.recv_cq;
  1160. init_attr->srq = qp->ibqp.srq;
  1161. init_attr->cap = attr->cap;
  1162. if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
  1163. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  1164. else
  1165. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  1166. init_attr->qp_type = qp->ibqp.qp_type;
  1167. init_attr->port_num = qp->port_num;
  1168. return 0;
  1169. }
  1170. /**
  1171. * rvt_post_receive - post a receive on a QP
  1172. * @ibqp: the QP to post the receive on
  1173. * @wr: the WR to post
  1174. * @bad_wr: the first bad WR is put here
  1175. *
  1176. * This may be called from interrupt context.
  1177. *
  1178. * Return: 0 on success otherwise errno
  1179. */
  1180. int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1181. struct ib_recv_wr **bad_wr)
  1182. {
  1183. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1184. struct rvt_rwq *wq = qp->r_rq.wq;
  1185. unsigned long flags;
  1186. int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
  1187. !qp->ibqp.srq;
  1188. /* Check that state is OK to post receive. */
  1189. if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
  1190. *bad_wr = wr;
  1191. return -EINVAL;
  1192. }
  1193. for (; wr; wr = wr->next) {
  1194. struct rvt_rwqe *wqe;
  1195. u32 next;
  1196. int i;
  1197. if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
  1198. *bad_wr = wr;
  1199. return -EINVAL;
  1200. }
  1201. spin_lock_irqsave(&qp->r_rq.lock, flags);
  1202. next = wq->head + 1;
  1203. if (next >= qp->r_rq.size)
  1204. next = 0;
  1205. if (next == wq->tail) {
  1206. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1207. *bad_wr = wr;
  1208. return -ENOMEM;
  1209. }
  1210. if (unlikely(qp_err_flush)) {
  1211. struct ib_wc wc;
  1212. memset(&wc, 0, sizeof(wc));
  1213. wc.qp = &qp->ibqp;
  1214. wc.opcode = IB_WC_RECV;
  1215. wc.wr_id = wr->wr_id;
  1216. wc.status = IB_WC_WR_FLUSH_ERR;
  1217. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1218. } else {
  1219. wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
  1220. wqe->wr_id = wr->wr_id;
  1221. wqe->num_sge = wr->num_sge;
  1222. for (i = 0; i < wr->num_sge; i++)
  1223. wqe->sg_list[i] = wr->sg_list[i];
  1224. /*
  1225. * Make sure queue entry is written
  1226. * before the head index.
  1227. */
  1228. smp_wmb();
  1229. wq->head = next;
  1230. }
  1231. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1232. }
  1233. return 0;
  1234. }
  1235. /**
  1236. * qp_get_savail - return number of avail send entries
  1237. *
  1238. * @qp - the qp
  1239. *
  1240. * This assumes the s_hlock is held but the s_last
  1241. * qp variable is uncontrolled.
  1242. */
  1243. static inline u32 qp_get_savail(struct rvt_qp *qp)
  1244. {
  1245. u32 slast;
  1246. u32 ret;
  1247. smp_read_barrier_depends(); /* see rc.c */
  1248. slast = ACCESS_ONCE(qp->s_last);
  1249. if (qp->s_head >= slast)
  1250. ret = qp->s_size - (qp->s_head - slast);
  1251. else
  1252. ret = slast - qp->s_head;
  1253. return ret - 1;
  1254. }
  1255. /**
  1256. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1257. * @qp: the QP to post on
  1258. * @wr: the work request to send
  1259. */
  1260. static int rvt_post_one_wr(struct rvt_qp *qp,
  1261. struct ib_send_wr *wr,
  1262. int *call_send)
  1263. {
  1264. struct rvt_swqe *wqe;
  1265. u32 next;
  1266. int i;
  1267. int j;
  1268. int acc;
  1269. struct rvt_lkey_table *rkt;
  1270. struct rvt_pd *pd;
  1271. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1272. u8 log_pmtu;
  1273. int ret;
  1274. /* IB spec says that num_sge == 0 is OK. */
  1275. if (unlikely(wr->num_sge > qp->s_max_sge))
  1276. return -EINVAL;
  1277. /*
  1278. * Don't allow RDMA reads or atomic operations on UC or
  1279. * undefined operations.
  1280. * Make sure buffer is large enough to hold the result for atomics.
  1281. */
  1282. if (qp->ibqp.qp_type == IB_QPT_UC) {
  1283. if ((unsigned)wr->opcode >= IB_WR_RDMA_READ)
  1284. return -EINVAL;
  1285. } else if (qp->ibqp.qp_type != IB_QPT_RC) {
  1286. /* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
  1287. if (wr->opcode != IB_WR_SEND &&
  1288. wr->opcode != IB_WR_SEND_WITH_IMM)
  1289. return -EINVAL;
  1290. /* Check UD destination address PD */
  1291. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1292. return -EINVAL;
  1293. } else if ((unsigned)wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
  1294. return -EINVAL;
  1295. } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  1296. (wr->num_sge == 0 ||
  1297. wr->sg_list[0].length < sizeof(u64) ||
  1298. wr->sg_list[0].addr & (sizeof(u64) - 1))) {
  1299. return -EINVAL;
  1300. } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
  1301. return -EINVAL;
  1302. }
  1303. /* check for avail */
  1304. if (unlikely(!qp->s_avail)) {
  1305. qp->s_avail = qp_get_savail(qp);
  1306. if (WARN_ON(qp->s_avail > (qp->s_size - 1)))
  1307. rvt_pr_err(rdi,
  1308. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1309. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1310. qp->s_head, qp->s_tail, qp->s_cur,
  1311. qp->s_acked, qp->s_last);
  1312. if (!qp->s_avail)
  1313. return -ENOMEM;
  1314. }
  1315. next = qp->s_head + 1;
  1316. if (next >= qp->s_size)
  1317. next = 0;
  1318. rkt = &rdi->lkey_table;
  1319. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1320. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1321. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1322. qp->ibqp.qp_type != IB_QPT_RC)
  1323. memcpy(&wqe->ud_wr, ud_wr(wr), sizeof(wqe->ud_wr));
  1324. else if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  1325. wr->opcode == IB_WR_RDMA_WRITE ||
  1326. wr->opcode == IB_WR_RDMA_READ)
  1327. memcpy(&wqe->rdma_wr, rdma_wr(wr), sizeof(wqe->rdma_wr));
  1328. else if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1329. wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  1330. memcpy(&wqe->atomic_wr, atomic_wr(wr), sizeof(wqe->atomic_wr));
  1331. else
  1332. memcpy(&wqe->wr, wr, sizeof(wqe->wr));
  1333. wqe->length = 0;
  1334. j = 0;
  1335. if (wr->num_sge) {
  1336. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1337. IB_ACCESS_LOCAL_WRITE : 0;
  1338. for (i = 0; i < wr->num_sge; i++) {
  1339. u32 length = wr->sg_list[i].length;
  1340. int ok;
  1341. if (length == 0)
  1342. continue;
  1343. ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
  1344. &wr->sg_list[i], acc);
  1345. if (!ok) {
  1346. ret = -EINVAL;
  1347. goto bail_inval_free;
  1348. }
  1349. wqe->length += length;
  1350. j++;
  1351. }
  1352. wqe->wr.num_sge = j;
  1353. }
  1354. /* general part of wqe valid - allow for driver checks */
  1355. if (rdi->driver_f.check_send_wqe) {
  1356. ret = rdi->driver_f.check_send_wqe(qp, wqe);
  1357. if (ret < 0)
  1358. goto bail_inval_free;
  1359. if (ret)
  1360. *call_send = ret;
  1361. }
  1362. log_pmtu = qp->log_pmtu;
  1363. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1364. qp->ibqp.qp_type != IB_QPT_RC) {
  1365. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1366. log_pmtu = ah->log_pmtu;
  1367. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1368. }
  1369. wqe->ssn = qp->s_ssn++;
  1370. wqe->psn = qp->s_next_psn;
  1371. wqe->lpsn = wqe->psn +
  1372. (wqe->length ? ((wqe->length - 1) >> log_pmtu) : 0);
  1373. qp->s_next_psn = wqe->lpsn + 1;
  1374. trace_rvt_post_one_wr(qp, wqe);
  1375. smp_wmb(); /* see request builders */
  1376. qp->s_avail--;
  1377. qp->s_head = next;
  1378. return 0;
  1379. bail_inval_free:
  1380. /* release mr holds */
  1381. while (j) {
  1382. struct rvt_sge *sge = &wqe->sg_list[--j];
  1383. rvt_put_mr(sge->mr);
  1384. }
  1385. return ret;
  1386. }
  1387. /**
  1388. * rvt_post_send - post a send on a QP
  1389. * @ibqp: the QP to post the send on
  1390. * @wr: the list of work requests to post
  1391. * @bad_wr: the first bad WR is put here
  1392. *
  1393. * This may be called from interrupt context.
  1394. *
  1395. * Return: 0 on success else errno
  1396. */
  1397. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1398. struct ib_send_wr **bad_wr)
  1399. {
  1400. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1401. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1402. unsigned long flags = 0;
  1403. int call_send;
  1404. unsigned nreq = 0;
  1405. int err = 0;
  1406. spin_lock_irqsave(&qp->s_hlock, flags);
  1407. /*
  1408. * Ensure QP state is such that we can send. If not bail out early,
  1409. * there is no need to do this every time we post a send.
  1410. */
  1411. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1412. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1413. return -EINVAL;
  1414. }
  1415. /*
  1416. * If the send queue is empty, and we only have a single WR then just go
  1417. * ahead and kick the send engine into gear. Otherwise we will always
  1418. * just schedule the send to happen later.
  1419. */
  1420. call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
  1421. for (; wr; wr = wr->next) {
  1422. err = rvt_post_one_wr(qp, wr, &call_send);
  1423. if (unlikely(err)) {
  1424. *bad_wr = wr;
  1425. goto bail;
  1426. }
  1427. nreq++;
  1428. }
  1429. bail:
  1430. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1431. if (nreq) {
  1432. if (call_send)
  1433. rdi->driver_f.schedule_send_no_lock(qp);
  1434. else
  1435. rdi->driver_f.do_send(qp);
  1436. }
  1437. return err;
  1438. }
  1439. /**
  1440. * rvt_post_srq_receive - post a receive on a shared receive queue
  1441. * @ibsrq: the SRQ to post the receive on
  1442. * @wr: the list of work requests to post
  1443. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1444. *
  1445. * This may be called from interrupt context.
  1446. *
  1447. * Return: 0 on success else errno
  1448. */
  1449. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1450. struct ib_recv_wr **bad_wr)
  1451. {
  1452. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1453. struct rvt_rwq *wq;
  1454. unsigned long flags;
  1455. for (; wr; wr = wr->next) {
  1456. struct rvt_rwqe *wqe;
  1457. u32 next;
  1458. int i;
  1459. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1460. *bad_wr = wr;
  1461. return -EINVAL;
  1462. }
  1463. spin_lock_irqsave(&srq->rq.lock, flags);
  1464. wq = srq->rq.wq;
  1465. next = wq->head + 1;
  1466. if (next >= srq->rq.size)
  1467. next = 0;
  1468. if (next == wq->tail) {
  1469. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1470. *bad_wr = wr;
  1471. return -ENOMEM;
  1472. }
  1473. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1474. wqe->wr_id = wr->wr_id;
  1475. wqe->num_sge = wr->num_sge;
  1476. for (i = 0; i < wr->num_sge; i++)
  1477. wqe->sg_list[i] = wr->sg_list[i];
  1478. /* Make sure queue entry is written before the head index. */
  1479. smp_wmb();
  1480. wq->head = next;
  1481. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1482. }
  1483. return 0;
  1484. }