qp.c 61 KB

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