qp.c 48 KB

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