qp.c 70 KB

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