virtio_ring.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Virtio ring implementation.
  3. *
  4. * Copyright 2007 Rusty Russell IBM Corporation
  5. */
  6. #include <linux/virtio.h>
  7. #include <linux/virtio_ring.h>
  8. #include <linux/virtio_config.h>
  9. #include <linux/device.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/hrtimer.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/kmsan.h>
  15. #include <linux/spinlock.h>
  16. #include <xen/xen.h>
  17. #ifdef DEBUG
  18. /* For development, we want to crash whenever the ring is screwed. */
  19. #define BAD_RING(_vq, fmt, args...) \
  20. do { \
  21. dev_err(&(_vq)->vq.vdev->dev, \
  22. "%s:"fmt, (_vq)->vq.name, ##args); \
  23. BUG(); \
  24. } while (0)
  25. /* Caller is supposed to guarantee no reentry. */
  26. #define START_USE(_vq) \
  27. do { \
  28. if ((_vq)->in_use) \
  29. panic("%s:in_use = %i\n", \
  30. (_vq)->vq.name, (_vq)->in_use); \
  31. (_vq)->in_use = __LINE__; \
  32. } while (0)
  33. #define END_USE(_vq) \
  34. do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
  35. #define LAST_ADD_TIME_UPDATE(_vq) \
  36. do { \
  37. ktime_t now = ktime_get(); \
  38. \
  39. /* No kick or get, with .1 second between? Warn. */ \
  40. if ((_vq)->last_add_time_valid) \
  41. WARN_ON(ktime_to_ms(ktime_sub(now, \
  42. (_vq)->last_add_time)) > 100); \
  43. (_vq)->last_add_time = now; \
  44. (_vq)->last_add_time_valid = true; \
  45. } while (0)
  46. #define LAST_ADD_TIME_CHECK(_vq) \
  47. do { \
  48. if ((_vq)->last_add_time_valid) { \
  49. WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \
  50. (_vq)->last_add_time)) > 100); \
  51. } \
  52. } while (0)
  53. #define LAST_ADD_TIME_INVALID(_vq) \
  54. ((_vq)->last_add_time_valid = false)
  55. #else
  56. #define BAD_RING(_vq, fmt, args...) \
  57. do { \
  58. dev_err(&_vq->vq.vdev->dev, \
  59. "%s:"fmt, (_vq)->vq.name, ##args); \
  60. (_vq)->broken = true; \
  61. } while (0)
  62. #define START_USE(vq)
  63. #define END_USE(vq)
  64. #define LAST_ADD_TIME_UPDATE(vq)
  65. #define LAST_ADD_TIME_CHECK(vq)
  66. #define LAST_ADD_TIME_INVALID(vq)
  67. #endif
  68. struct vring_desc_state_split {
  69. void *data; /* Data for callback. */
  70. struct vring_desc *indir_desc; /* Indirect descriptor, if any. */
  71. };
  72. struct vring_desc_state_packed {
  73. void *data; /* Data for callback. */
  74. struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
  75. u16 num; /* Descriptor list length. */
  76. u16 last; /* The last desc state in a list. */
  77. };
  78. struct vring_desc_extra {
  79. dma_addr_t addr; /* Descriptor DMA addr. */
  80. u32 len; /* Descriptor length. */
  81. u16 flags; /* Descriptor flags. */
  82. u16 next; /* The next desc state in a list. */
  83. };
  84. struct vring_virtqueue_split {
  85. /* Actual memory layout for this queue. */
  86. struct vring vring;
  87. /* Last written value to avail->flags */
  88. u16 avail_flags_shadow;
  89. /*
  90. * Last written value to avail->idx in
  91. * guest byte order.
  92. */
  93. u16 avail_idx_shadow;
  94. /* Per-descriptor state. */
  95. struct vring_desc_state_split *desc_state;
  96. struct vring_desc_extra *desc_extra;
  97. /* DMA address and size information */
  98. dma_addr_t queue_dma_addr;
  99. size_t queue_size_in_bytes;
  100. /*
  101. * The parameters for creating vrings are reserved for creating new
  102. * vring.
  103. */
  104. u32 vring_align;
  105. bool may_reduce_num;
  106. };
  107. struct vring_virtqueue_packed {
  108. /* Actual memory layout for this queue. */
  109. struct {
  110. unsigned int num;
  111. struct vring_packed_desc *desc;
  112. struct vring_packed_desc_event *driver;
  113. struct vring_packed_desc_event *device;
  114. } vring;
  115. /* Driver ring wrap counter. */
  116. bool avail_wrap_counter;
  117. /* Avail used flags. */
  118. u16 avail_used_flags;
  119. /* Index of the next avail descriptor. */
  120. u16 next_avail_idx;
  121. /*
  122. * Last written value to driver->flags in
  123. * guest byte order.
  124. */
  125. u16 event_flags_shadow;
  126. /* Per-descriptor state. */
  127. struct vring_desc_state_packed *desc_state;
  128. struct vring_desc_extra *desc_extra;
  129. /* DMA address and size information */
  130. dma_addr_t ring_dma_addr;
  131. dma_addr_t driver_event_dma_addr;
  132. dma_addr_t device_event_dma_addr;
  133. size_t ring_size_in_bytes;
  134. size_t event_size_in_bytes;
  135. };
  136. struct vring_virtqueue {
  137. struct virtqueue vq;
  138. /* Is this a packed ring? */
  139. bool packed_ring;
  140. /* Is DMA API used? */
  141. bool use_dma_api;
  142. /* Can we use weak barriers? */
  143. bool weak_barriers;
  144. /* Other side has made a mess, don't try any more. */
  145. bool broken;
  146. /* Host supports indirect buffers */
  147. bool indirect;
  148. /* Host publishes avail event idx */
  149. bool event;
  150. /* Do DMA mapping by driver */
  151. bool premapped;
  152. /* Do unmap or not for desc. Just when premapped is False and
  153. * use_dma_api is true, this is true.
  154. */
  155. bool do_unmap;
  156. /* Head of free buffer list. */
  157. unsigned int free_head;
  158. /* Number we've added since last sync. */
  159. unsigned int num_added;
  160. /* Last used index we've seen.
  161. * for split ring, it just contains last used index
  162. * for packed ring:
  163. * bits up to VRING_PACKED_EVENT_F_WRAP_CTR include the last used index.
  164. * bits from VRING_PACKED_EVENT_F_WRAP_CTR include the used wrap counter.
  165. */
  166. u16 last_used_idx;
  167. /* Hint for event idx: already triggered no need to disable. */
  168. bool event_triggered;
  169. union {
  170. /* Available for split ring */
  171. struct vring_virtqueue_split split;
  172. /* Available for packed ring */
  173. struct vring_virtqueue_packed packed;
  174. };
  175. /* How to notify other side. FIXME: commonalize hcalls! */
  176. bool (*notify)(struct virtqueue *vq);
  177. /* DMA, allocation, and size information */
  178. bool we_own_ring;
  179. /* Device used for doing DMA */
  180. struct device *dma_dev;
  181. #ifdef DEBUG
  182. /* They're supposed to lock for us. */
  183. unsigned int in_use;
  184. /* Figure out if their kicks are too delayed. */
  185. bool last_add_time_valid;
  186. ktime_t last_add_time;
  187. #endif
  188. };
  189. static struct virtqueue *__vring_new_virtqueue(unsigned int index,
  190. struct vring_virtqueue_split *vring_split,
  191. struct virtio_device *vdev,
  192. bool weak_barriers,
  193. bool context,
  194. bool (*notify)(struct virtqueue *),
  195. void (*callback)(struct virtqueue *),
  196. const char *name,
  197. struct device *dma_dev);
  198. static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num);
  199. static void vring_free(struct virtqueue *_vq);
  200. /*
  201. * Helpers.
  202. */
  203. #define to_vvq(_vq) container_of_const(_vq, struct vring_virtqueue, vq)
  204. static bool virtqueue_use_indirect(const struct vring_virtqueue *vq,
  205. unsigned int total_sg)
  206. {
  207. /*
  208. * If the host supports indirect descriptor tables, and we have multiple
  209. * buffers, then go indirect. FIXME: tune this threshold
  210. */
  211. return (vq->indirect && total_sg > 1 && vq->vq.num_free);
  212. }
  213. /*
  214. * Modern virtio devices have feature bits to specify whether they need a
  215. * quirk and bypass the IOMMU. If not there, just use the DMA API.
  216. *
  217. * If there, the interaction between virtio and DMA API is messy.
  218. *
  219. * On most systems with virtio, physical addresses match bus addresses,
  220. * and it doesn't particularly matter whether we use the DMA API.
  221. *
  222. * On some systems, including Xen and any system with a physical device
  223. * that speaks virtio behind a physical IOMMU, we must use the DMA API
  224. * for virtio DMA to work at all.
  225. *
  226. * On other systems, including SPARC and PPC64, virtio-pci devices are
  227. * enumerated as though they are behind an IOMMU, but the virtio host
  228. * ignores the IOMMU, so we must either pretend that the IOMMU isn't
  229. * there or somehow map everything as the identity.
  230. *
  231. * For the time being, we preserve historic behavior and bypass the DMA
  232. * API.
  233. *
  234. * TODO: install a per-device DMA ops structure that does the right thing
  235. * taking into account all the above quirks, and use the DMA API
  236. * unconditionally on data path.
  237. */
  238. static bool vring_use_dma_api(const struct virtio_device *vdev)
  239. {
  240. if (!virtio_has_dma_quirk(vdev))
  241. return true;
  242. /* Otherwise, we are left to guess. */
  243. /*
  244. * In theory, it's possible to have a buggy QEMU-supposed
  245. * emulated Q35 IOMMU and Xen enabled at the same time. On
  246. * such a configuration, virtio has never worked and will
  247. * not work without an even larger kludge. Instead, enable
  248. * the DMA API if we're a Xen guest, which at least allows
  249. * all of the sensible Xen configurations to work correctly.
  250. */
  251. if (xen_domain())
  252. return true;
  253. return false;
  254. }
  255. size_t virtio_max_dma_size(const struct virtio_device *vdev)
  256. {
  257. size_t max_segment_size = SIZE_MAX;
  258. if (vring_use_dma_api(vdev))
  259. max_segment_size = dma_max_mapping_size(vdev->dev.parent);
  260. return max_segment_size;
  261. }
  262. EXPORT_SYMBOL_GPL(virtio_max_dma_size);
  263. static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
  264. dma_addr_t *dma_handle, gfp_t flag,
  265. struct device *dma_dev)
  266. {
  267. if (vring_use_dma_api(vdev)) {
  268. return dma_alloc_coherent(dma_dev, size,
  269. dma_handle, flag);
  270. } else {
  271. void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
  272. if (queue) {
  273. phys_addr_t phys_addr = virt_to_phys(queue);
  274. *dma_handle = (dma_addr_t)phys_addr;
  275. /*
  276. * Sanity check: make sure we dind't truncate
  277. * the address. The only arches I can find that
  278. * have 64-bit phys_addr_t but 32-bit dma_addr_t
  279. * are certain non-highmem MIPS and x86
  280. * configurations, but these configurations
  281. * should never allocate physical pages above 32
  282. * bits, so this is fine. Just in case, throw a
  283. * warning and abort if we end up with an
  284. * unrepresentable address.
  285. */
  286. if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
  287. free_pages_exact(queue, PAGE_ALIGN(size));
  288. return NULL;
  289. }
  290. }
  291. return queue;
  292. }
  293. }
  294. static void vring_free_queue(struct virtio_device *vdev, size_t size,
  295. void *queue, dma_addr_t dma_handle,
  296. struct device *dma_dev)
  297. {
  298. if (vring_use_dma_api(vdev))
  299. dma_free_coherent(dma_dev, size, queue, dma_handle);
  300. else
  301. free_pages_exact(queue, PAGE_ALIGN(size));
  302. }
  303. /*
  304. * The DMA ops on various arches are rather gnarly right now, and
  305. * making all of the arch DMA ops work on the vring device itself
  306. * is a mess.
  307. */
  308. static struct device *vring_dma_dev(const struct vring_virtqueue *vq)
  309. {
  310. return vq->dma_dev;
  311. }
  312. /* Map one sg entry. */
  313. static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg,
  314. enum dma_data_direction direction, dma_addr_t *addr)
  315. {
  316. if (vq->premapped) {
  317. *addr = sg_dma_address(sg);
  318. return 0;
  319. }
  320. if (!vq->use_dma_api) {
  321. /*
  322. * If DMA is not used, KMSAN doesn't know that the scatterlist
  323. * is initialized by the hardware. Explicitly check/unpoison it
  324. * depending on the direction.
  325. */
  326. kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction);
  327. *addr = (dma_addr_t)sg_phys(sg);
  328. return 0;
  329. }
  330. /*
  331. * We can't use dma_map_sg, because we don't use scatterlists in
  332. * the way it expects (we don't guarantee that the scatterlist
  333. * will exist for the lifetime of the mapping).
  334. */
  335. *addr = dma_map_page(vring_dma_dev(vq),
  336. sg_page(sg), sg->offset, sg->length,
  337. direction);
  338. if (dma_mapping_error(vring_dma_dev(vq), *addr))
  339. return -ENOMEM;
  340. return 0;
  341. }
  342. static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
  343. void *cpu_addr, size_t size,
  344. enum dma_data_direction direction)
  345. {
  346. if (!vq->use_dma_api)
  347. return (dma_addr_t)virt_to_phys(cpu_addr);
  348. return dma_map_single(vring_dma_dev(vq),
  349. cpu_addr, size, direction);
  350. }
  351. static int vring_mapping_error(const struct vring_virtqueue *vq,
  352. dma_addr_t addr)
  353. {
  354. if (!vq->use_dma_api)
  355. return 0;
  356. return dma_mapping_error(vring_dma_dev(vq), addr);
  357. }
  358. static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
  359. {
  360. vq->vq.num_free = num;
  361. if (vq->packed_ring)
  362. vq->last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR);
  363. else
  364. vq->last_used_idx = 0;
  365. vq->event_triggered = false;
  366. vq->num_added = 0;
  367. #ifdef DEBUG
  368. vq->in_use = false;
  369. vq->last_add_time_valid = false;
  370. #endif
  371. }
  372. /*
  373. * Split ring specific functions - *_split().
  374. */
  375. static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
  376. const struct vring_desc *desc)
  377. {
  378. u16 flags;
  379. if (!vq->do_unmap)
  380. return;
  381. flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
  382. dma_unmap_page(vring_dma_dev(vq),
  383. virtio64_to_cpu(vq->vq.vdev, desc->addr),
  384. virtio32_to_cpu(vq->vq.vdev, desc->len),
  385. (flags & VRING_DESC_F_WRITE) ?
  386. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  387. }
  388. static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
  389. unsigned int i)
  390. {
  391. struct vring_desc_extra *extra = vq->split.desc_extra;
  392. u16 flags;
  393. flags = extra[i].flags;
  394. if (flags & VRING_DESC_F_INDIRECT) {
  395. if (!vq->use_dma_api)
  396. goto out;
  397. dma_unmap_single(vring_dma_dev(vq),
  398. extra[i].addr,
  399. extra[i].len,
  400. (flags & VRING_DESC_F_WRITE) ?
  401. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  402. } else {
  403. if (!vq->do_unmap)
  404. goto out;
  405. dma_unmap_page(vring_dma_dev(vq),
  406. extra[i].addr,
  407. extra[i].len,
  408. (flags & VRING_DESC_F_WRITE) ?
  409. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  410. }
  411. out:
  412. return extra[i].next;
  413. }
  414. static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
  415. unsigned int total_sg,
  416. gfp_t gfp)
  417. {
  418. struct vring_desc *desc;
  419. unsigned int i;
  420. /*
  421. * We require lowmem mappings for the descriptors because
  422. * otherwise virt_to_phys will give us bogus addresses in the
  423. * virtqueue.
  424. */
  425. gfp &= ~__GFP_HIGHMEM;
  426. desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
  427. if (!desc)
  428. return NULL;
  429. for (i = 0; i < total_sg; i++)
  430. desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
  431. return desc;
  432. }
  433. static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
  434. struct vring_desc *desc,
  435. unsigned int i,
  436. dma_addr_t addr,
  437. unsigned int len,
  438. u16 flags,
  439. bool indirect)
  440. {
  441. struct vring_virtqueue *vring = to_vvq(vq);
  442. struct vring_desc_extra *extra = vring->split.desc_extra;
  443. u16 next;
  444. desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
  445. desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
  446. desc[i].len = cpu_to_virtio32(vq->vdev, len);
  447. if (!indirect) {
  448. next = extra[i].next;
  449. desc[i].next = cpu_to_virtio16(vq->vdev, next);
  450. extra[i].addr = addr;
  451. extra[i].len = len;
  452. extra[i].flags = flags;
  453. } else
  454. next = virtio16_to_cpu(vq->vdev, desc[i].next);
  455. return next;
  456. }
  457. static inline int virtqueue_add_split(struct virtqueue *_vq,
  458. struct scatterlist *sgs[],
  459. unsigned int total_sg,
  460. unsigned int out_sgs,
  461. unsigned int in_sgs,
  462. void *data,
  463. void *ctx,
  464. gfp_t gfp)
  465. {
  466. struct vring_virtqueue *vq = to_vvq(_vq);
  467. struct scatterlist *sg;
  468. struct vring_desc *desc;
  469. unsigned int i, n, avail, descs_used, prev, err_idx;
  470. int head;
  471. bool indirect;
  472. START_USE(vq);
  473. BUG_ON(data == NULL);
  474. BUG_ON(ctx && vq->indirect);
  475. if (unlikely(vq->broken)) {
  476. END_USE(vq);
  477. return -EIO;
  478. }
  479. LAST_ADD_TIME_UPDATE(vq);
  480. BUG_ON(total_sg == 0);
  481. head = vq->free_head;
  482. if (virtqueue_use_indirect(vq, total_sg))
  483. desc = alloc_indirect_split(_vq, total_sg, gfp);
  484. else {
  485. desc = NULL;
  486. WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
  487. }
  488. if (desc) {
  489. /* Use a single buffer which doesn't continue */
  490. indirect = true;
  491. /* Set up rest to use this indirect table. */
  492. i = 0;
  493. descs_used = 1;
  494. } else {
  495. indirect = false;
  496. desc = vq->split.vring.desc;
  497. i = head;
  498. descs_used = total_sg;
  499. }
  500. if (unlikely(vq->vq.num_free < descs_used)) {
  501. pr_debug("Can't add buf len %i - avail = %i\n",
  502. descs_used, vq->vq.num_free);
  503. /* FIXME: for historical reasons, we force a notify here if
  504. * there are outgoing parts to the buffer. Presumably the
  505. * host should service the ring ASAP. */
  506. if (out_sgs)
  507. vq->notify(&vq->vq);
  508. if (indirect)
  509. kfree(desc);
  510. END_USE(vq);
  511. return -ENOSPC;
  512. }
  513. for (n = 0; n < out_sgs; n++) {
  514. for (sg = sgs[n]; sg; sg = sg_next(sg)) {
  515. dma_addr_t addr;
  516. if (vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr))
  517. goto unmap_release;
  518. prev = i;
  519. /* Note that we trust indirect descriptor
  520. * table since it use stream DMA mapping.
  521. */
  522. i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length,
  523. VRING_DESC_F_NEXT,
  524. indirect);
  525. }
  526. }
  527. for (; n < (out_sgs + in_sgs); n++) {
  528. for (sg = sgs[n]; sg; sg = sg_next(sg)) {
  529. dma_addr_t addr;
  530. if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr))
  531. goto unmap_release;
  532. prev = i;
  533. /* Note that we trust indirect descriptor
  534. * table since it use stream DMA mapping.
  535. */
  536. i = virtqueue_add_desc_split(_vq, desc, i, addr,
  537. sg->length,
  538. VRING_DESC_F_NEXT |
  539. VRING_DESC_F_WRITE,
  540. indirect);
  541. }
  542. }
  543. /* Last one doesn't continue. */
  544. desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
  545. if (!indirect && vq->do_unmap)
  546. vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
  547. ~VRING_DESC_F_NEXT;
  548. if (indirect) {
  549. /* Now that the indirect table is filled in, map it. */
  550. dma_addr_t addr = vring_map_single(
  551. vq, desc, total_sg * sizeof(struct vring_desc),
  552. DMA_TO_DEVICE);
  553. if (vring_mapping_error(vq, addr)) {
  554. if (vq->premapped)
  555. goto free_indirect;
  556. goto unmap_release;
  557. }
  558. virtqueue_add_desc_split(_vq, vq->split.vring.desc,
  559. head, addr,
  560. total_sg * sizeof(struct vring_desc),
  561. VRING_DESC_F_INDIRECT,
  562. false);
  563. }
  564. /* We're using some buffers from the free list. */
  565. vq->vq.num_free -= descs_used;
  566. /* Update free pointer */
  567. if (indirect)
  568. vq->free_head = vq->split.desc_extra[head].next;
  569. else
  570. vq->free_head = i;
  571. /* Store token and indirect buffer state. */
  572. vq->split.desc_state[head].data = data;
  573. if (indirect)
  574. vq->split.desc_state[head].indir_desc = desc;
  575. else
  576. vq->split.desc_state[head].indir_desc = ctx;
  577. /* Put entry in available array (but don't update avail->idx until they
  578. * do sync). */
  579. avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
  580. vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
  581. /* Descriptors and available array need to be set before we expose the
  582. * new available array entries. */
  583. virtio_wmb(vq->weak_barriers);
  584. vq->split.avail_idx_shadow++;
  585. vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
  586. vq->split.avail_idx_shadow);
  587. vq->num_added++;
  588. pr_debug("Added buffer head %i to %p\n", head, vq);
  589. END_USE(vq);
  590. /* This is very unlikely, but theoretically possible. Kick
  591. * just in case. */
  592. if (unlikely(vq->num_added == (1 << 16) - 1))
  593. virtqueue_kick(_vq);
  594. return 0;
  595. unmap_release:
  596. err_idx = i;
  597. if (indirect)
  598. i = 0;
  599. else
  600. i = head;
  601. for (n = 0; n < total_sg; n++) {
  602. if (i == err_idx)
  603. break;
  604. if (indirect) {
  605. vring_unmap_one_split_indirect(vq, &desc[i]);
  606. i = virtio16_to_cpu(_vq->vdev, desc[i].next);
  607. } else
  608. i = vring_unmap_one_split(vq, i);
  609. }
  610. free_indirect:
  611. if (indirect)
  612. kfree(desc);
  613. END_USE(vq);
  614. return -ENOMEM;
  615. }
  616. static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
  617. {
  618. struct vring_virtqueue *vq = to_vvq(_vq);
  619. u16 new, old;
  620. bool needs_kick;
  621. START_USE(vq);
  622. /* We need to expose available array entries before checking avail
  623. * event. */
  624. virtio_mb(vq->weak_barriers);
  625. old = vq->split.avail_idx_shadow - vq->num_added;
  626. new = vq->split.avail_idx_shadow;
  627. vq->num_added = 0;
  628. LAST_ADD_TIME_CHECK(vq);
  629. LAST_ADD_TIME_INVALID(vq);
  630. if (vq->event) {
  631. needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
  632. vring_avail_event(&vq->split.vring)),
  633. new, old);
  634. } else {
  635. needs_kick = !(vq->split.vring.used->flags &
  636. cpu_to_virtio16(_vq->vdev,
  637. VRING_USED_F_NO_NOTIFY));
  638. }
  639. END_USE(vq);
  640. return needs_kick;
  641. }
  642. static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
  643. void **ctx)
  644. {
  645. unsigned int i, j;
  646. __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
  647. /* Clear data ptr. */
  648. vq->split.desc_state[head].data = NULL;
  649. /* Put back on free list: unmap first-level descriptors and find end */
  650. i = head;
  651. while (vq->split.vring.desc[i].flags & nextflag) {
  652. vring_unmap_one_split(vq, i);
  653. i = vq->split.desc_extra[i].next;
  654. vq->vq.num_free++;
  655. }
  656. vring_unmap_one_split(vq, i);
  657. vq->split.desc_extra[i].next = vq->free_head;
  658. vq->free_head = head;
  659. /* Plus final descriptor */
  660. vq->vq.num_free++;
  661. if (vq->indirect) {
  662. struct vring_desc *indir_desc =
  663. vq->split.desc_state[head].indir_desc;
  664. u32 len;
  665. /* Free the indirect table, if any, now that it's unmapped. */
  666. if (!indir_desc)
  667. return;
  668. len = vq->split.desc_extra[head].len;
  669. BUG_ON(!(vq->split.desc_extra[head].flags &
  670. VRING_DESC_F_INDIRECT));
  671. BUG_ON(len == 0 || len % sizeof(struct vring_desc));
  672. if (vq->do_unmap) {
  673. for (j = 0; j < len / sizeof(struct vring_desc); j++)
  674. vring_unmap_one_split_indirect(vq, &indir_desc[j]);
  675. }
  676. kfree(indir_desc);
  677. vq->split.desc_state[head].indir_desc = NULL;
  678. } else if (ctx) {
  679. *ctx = vq->split.desc_state[head].indir_desc;
  680. }
  681. }
  682. static bool more_used_split(const struct vring_virtqueue *vq)
  683. {
  684. return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
  685. vq->split.vring.used->idx);
  686. }
  687. static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
  688. unsigned int *len,
  689. void **ctx)
  690. {
  691. struct vring_virtqueue *vq = to_vvq(_vq);
  692. void *ret;
  693. unsigned int i;
  694. u16 last_used;
  695. START_USE(vq);
  696. if (unlikely(vq->broken)) {
  697. END_USE(vq);
  698. return NULL;
  699. }
  700. if (!more_used_split(vq)) {
  701. pr_debug("No more buffers in queue\n");
  702. END_USE(vq);
  703. return NULL;
  704. }
  705. /* Only get used array entries after they have been exposed by host. */
  706. virtio_rmb(vq->weak_barriers);
  707. last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
  708. i = virtio32_to_cpu(_vq->vdev,
  709. vq->split.vring.used->ring[last_used].id);
  710. *len = virtio32_to_cpu(_vq->vdev,
  711. vq->split.vring.used->ring[last_used].len);
  712. if (unlikely(i >= vq->split.vring.num)) {
  713. BAD_RING(vq, "id %u out of range\n", i);
  714. return NULL;
  715. }
  716. if (unlikely(!vq->split.desc_state[i].data)) {
  717. BAD_RING(vq, "id %u is not a head!\n", i);
  718. return NULL;
  719. }
  720. /* detach_buf_split clears data, so grab it now. */
  721. ret = vq->split.desc_state[i].data;
  722. detach_buf_split(vq, i, ctx);
  723. vq->last_used_idx++;
  724. /* If we expect an interrupt for the next entry, tell host
  725. * by writing event index and flush out the write before
  726. * the read in the next get_buf call. */
  727. if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
  728. virtio_store_mb(vq->weak_barriers,
  729. &vring_used_event(&vq->split.vring),
  730. cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
  731. LAST_ADD_TIME_INVALID(vq);
  732. END_USE(vq);
  733. return ret;
  734. }
  735. static void virtqueue_disable_cb_split(struct virtqueue *_vq)
  736. {
  737. struct vring_virtqueue *vq = to_vvq(_vq);
  738. if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
  739. vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
  740. /*
  741. * If device triggered an event already it won't trigger one again:
  742. * no need to disable.
  743. */
  744. if (vq->event_triggered)
  745. return;
  746. if (vq->event)
  747. /* TODO: this is a hack. Figure out a cleaner value to write. */
  748. vring_used_event(&vq->split.vring) = 0x0;
  749. else
  750. vq->split.vring.avail->flags =
  751. cpu_to_virtio16(_vq->vdev,
  752. vq->split.avail_flags_shadow);
  753. }
  754. }
  755. static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
  756. {
  757. struct vring_virtqueue *vq = to_vvq(_vq);
  758. u16 last_used_idx;
  759. START_USE(vq);
  760. /* We optimistically turn back on interrupts, then check if there was
  761. * more to do. */
  762. /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
  763. * either clear the flags bit or point the event index at the next
  764. * entry. Always do both to keep code simple. */
  765. if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
  766. vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
  767. if (!vq->event)
  768. vq->split.vring.avail->flags =
  769. cpu_to_virtio16(_vq->vdev,
  770. vq->split.avail_flags_shadow);
  771. }
  772. vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
  773. last_used_idx = vq->last_used_idx);
  774. END_USE(vq);
  775. return last_used_idx;
  776. }
  777. static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_used_idx)
  778. {
  779. struct vring_virtqueue *vq = to_vvq(_vq);
  780. return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
  781. vq->split.vring.used->idx);
  782. }
  783. static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
  784. {
  785. struct vring_virtqueue *vq = to_vvq(_vq);
  786. u16 bufs;
  787. START_USE(vq);
  788. /* We optimistically turn back on interrupts, then check if there was
  789. * more to do. */
  790. /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
  791. * either clear the flags bit or point the event index at the next
  792. * entry. Always update the event index to keep code simple. */
  793. if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
  794. vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
  795. if (!vq->event)
  796. vq->split.vring.avail->flags =
  797. cpu_to_virtio16(_vq->vdev,
  798. vq->split.avail_flags_shadow);
  799. }
  800. /* TODO: tune this threshold */
  801. bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
  802. virtio_store_mb(vq->weak_barriers,
  803. &vring_used_event(&vq->split.vring),
  804. cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
  805. if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
  806. - vq->last_used_idx) > bufs)) {
  807. END_USE(vq);
  808. return false;
  809. }
  810. END_USE(vq);
  811. return true;
  812. }
  813. static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
  814. {
  815. struct vring_virtqueue *vq = to_vvq(_vq);
  816. unsigned int i;
  817. void *buf;
  818. START_USE(vq);
  819. for (i = 0; i < vq->split.vring.num; i++) {
  820. if (!vq->split.desc_state[i].data)
  821. continue;
  822. /* detach_buf_split clears data, so grab it now. */
  823. buf = vq->split.desc_state[i].data;
  824. detach_buf_split(vq, i, NULL);
  825. vq->split.avail_idx_shadow--;
  826. vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
  827. vq->split.avail_idx_shadow);
  828. END_USE(vq);
  829. return buf;
  830. }
  831. /* That should have freed everything. */
  832. BUG_ON(vq->vq.num_free != vq->split.vring.num);
  833. END_USE(vq);
  834. return NULL;
  835. }
  836. static void virtqueue_vring_init_split(struct vring_virtqueue_split *vring_split,
  837. struct vring_virtqueue *vq)
  838. {
  839. struct virtio_device *vdev;
  840. vdev = vq->vq.vdev;
  841. vring_split->avail_flags_shadow = 0;
  842. vring_split->avail_idx_shadow = 0;
  843. /* No callback? Tell other side not to bother us. */
  844. if (!vq->vq.callback) {
  845. vring_split->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
  846. if (!vq->event)
  847. vring_split->vring.avail->flags = cpu_to_virtio16(vdev,
  848. vring_split->avail_flags_shadow);
  849. }
  850. }
  851. static void virtqueue_reinit_split(struct vring_virtqueue *vq)
  852. {
  853. int num;
  854. num = vq->split.vring.num;
  855. vq->split.vring.avail->flags = 0;
  856. vq->split.vring.avail->idx = 0;
  857. /* reset avail event */
  858. vq->split.vring.avail->ring[num] = 0;
  859. vq->split.vring.used->flags = 0;
  860. vq->split.vring.used->idx = 0;
  861. /* reset used event */
  862. *(__virtio16 *)&(vq->split.vring.used->ring[num]) = 0;
  863. virtqueue_init(vq, num);
  864. virtqueue_vring_init_split(&vq->split, vq);
  865. }
  866. static void virtqueue_vring_attach_split(struct vring_virtqueue *vq,
  867. struct vring_virtqueue_split *vring_split)
  868. {
  869. vq->split = *vring_split;
  870. /* Put everything in free lists. */
  871. vq->free_head = 0;
  872. }
  873. static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_split)
  874. {
  875. struct vring_desc_state_split *state;
  876. struct vring_desc_extra *extra;
  877. u32 num = vring_split->vring.num;
  878. state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL);
  879. if (!state)
  880. goto err_state;
  881. extra = vring_alloc_desc_extra(num);
  882. if (!extra)
  883. goto err_extra;
  884. memset(state, 0, num * sizeof(struct vring_desc_state_split));
  885. vring_split->desc_state = state;
  886. vring_split->desc_extra = extra;
  887. return 0;
  888. err_extra:
  889. kfree(state);
  890. err_state:
  891. return -ENOMEM;
  892. }
  893. static void vring_free_split(struct vring_virtqueue_split *vring_split,
  894. struct virtio_device *vdev, struct device *dma_dev)
  895. {
  896. vring_free_queue(vdev, vring_split->queue_size_in_bytes,
  897. vring_split->vring.desc,
  898. vring_split->queue_dma_addr,
  899. dma_dev);
  900. kfree(vring_split->desc_state);
  901. kfree(vring_split->desc_extra);
  902. }
  903. static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
  904. struct virtio_device *vdev,
  905. u32 num,
  906. unsigned int vring_align,
  907. bool may_reduce_num,
  908. struct device *dma_dev)
  909. {
  910. void *queue = NULL;
  911. dma_addr_t dma_addr;
  912. /* We assume num is a power of 2. */
  913. if (!is_power_of_2(num)) {
  914. dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
  915. return -EINVAL;
  916. }
  917. /* TODO: allocate each queue chunk individually */
  918. for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
  919. queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
  920. &dma_addr,
  921. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
  922. dma_dev);
  923. if (queue)
  924. break;
  925. if (!may_reduce_num)
  926. return -ENOMEM;
  927. }
  928. if (!num)
  929. return -ENOMEM;
  930. if (!queue) {
  931. /* Try to get a single page. You are my only hope! */
  932. queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
  933. &dma_addr, GFP_KERNEL | __GFP_ZERO,
  934. dma_dev);
  935. }
  936. if (!queue)
  937. return -ENOMEM;
  938. vring_init(&vring_split->vring, num, queue, vring_align);
  939. vring_split->queue_dma_addr = dma_addr;
  940. vring_split->queue_size_in_bytes = vring_size(num, vring_align);
  941. vring_split->vring_align = vring_align;
  942. vring_split->may_reduce_num = may_reduce_num;
  943. return 0;
  944. }
  945. static struct virtqueue *vring_create_virtqueue_split(
  946. unsigned int index,
  947. unsigned int num,
  948. unsigned int vring_align,
  949. struct virtio_device *vdev,
  950. bool weak_barriers,
  951. bool may_reduce_num,
  952. bool context,
  953. bool (*notify)(struct virtqueue *),
  954. void (*callback)(struct virtqueue *),
  955. const char *name,
  956. struct device *dma_dev)
  957. {
  958. struct vring_virtqueue_split vring_split = {};
  959. struct virtqueue *vq;
  960. int err;
  961. err = vring_alloc_queue_split(&vring_split, vdev, num, vring_align,
  962. may_reduce_num, dma_dev);
  963. if (err)
  964. return NULL;
  965. vq = __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
  966. context, notify, callback, name, dma_dev);
  967. if (!vq) {
  968. vring_free_split(&vring_split, vdev, dma_dev);
  969. return NULL;
  970. }
  971. to_vvq(vq)->we_own_ring = true;
  972. return vq;
  973. }
  974. static int virtqueue_resize_split(struct virtqueue *_vq, u32 num)
  975. {
  976. struct vring_virtqueue_split vring_split = {};
  977. struct vring_virtqueue *vq = to_vvq(_vq);
  978. struct virtio_device *vdev = _vq->vdev;
  979. int err;
  980. err = vring_alloc_queue_split(&vring_split, vdev, num,
  981. vq->split.vring_align,
  982. vq->split.may_reduce_num,
  983. vring_dma_dev(vq));
  984. if (err)
  985. goto err;
  986. err = vring_alloc_state_extra_split(&vring_split);
  987. if (err)
  988. goto err_state_extra;
  989. vring_free(&vq->vq);
  990. virtqueue_vring_init_split(&vring_split, vq);
  991. virtqueue_init(vq, vring_split.vring.num);
  992. virtqueue_vring_attach_split(vq, &vring_split);
  993. return 0;
  994. err_state_extra:
  995. vring_free_split(&vring_split, vdev, vring_dma_dev(vq));
  996. err:
  997. virtqueue_reinit_split(vq);
  998. return -ENOMEM;
  999. }
  1000. /*
  1001. * Packed ring specific functions - *_packed().
  1002. */
  1003. static bool packed_used_wrap_counter(u16 last_used_idx)
  1004. {
  1005. return !!(last_used_idx & (1 << VRING_PACKED_EVENT_F_WRAP_CTR));
  1006. }
  1007. static u16 packed_last_used(u16 last_used_idx)
  1008. {
  1009. return last_used_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR));
  1010. }
  1011. static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
  1012. const struct vring_desc_extra *extra)
  1013. {
  1014. u16 flags;
  1015. flags = extra->flags;
  1016. if (flags & VRING_DESC_F_INDIRECT) {
  1017. if (!vq->use_dma_api)
  1018. return;
  1019. dma_unmap_single(vring_dma_dev(vq),
  1020. extra->addr, extra->len,
  1021. (flags & VRING_DESC_F_WRITE) ?
  1022. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1023. } else {
  1024. if (!vq->do_unmap)
  1025. return;
  1026. dma_unmap_page(vring_dma_dev(vq),
  1027. extra->addr, extra->len,
  1028. (flags & VRING_DESC_F_WRITE) ?
  1029. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1030. }
  1031. }
  1032. static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
  1033. const struct vring_packed_desc *desc)
  1034. {
  1035. u16 flags;
  1036. if (!vq->do_unmap)
  1037. return;
  1038. flags = le16_to_cpu(desc->flags);
  1039. dma_unmap_page(vring_dma_dev(vq),
  1040. le64_to_cpu(desc->addr),
  1041. le32_to_cpu(desc->len),
  1042. (flags & VRING_DESC_F_WRITE) ?
  1043. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1044. }
  1045. static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
  1046. gfp_t gfp)
  1047. {
  1048. struct vring_packed_desc *desc;
  1049. /*
  1050. * We require lowmem mappings for the descriptors because
  1051. * otherwise virt_to_phys will give us bogus addresses in the
  1052. * virtqueue.
  1053. */
  1054. gfp &= ~__GFP_HIGHMEM;
  1055. desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
  1056. return desc;
  1057. }
  1058. static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
  1059. struct scatterlist *sgs[],
  1060. unsigned int total_sg,
  1061. unsigned int out_sgs,
  1062. unsigned int in_sgs,
  1063. void *data,
  1064. gfp_t gfp)
  1065. {
  1066. struct vring_packed_desc *desc;
  1067. struct scatterlist *sg;
  1068. unsigned int i, n, err_idx;
  1069. u16 head, id;
  1070. dma_addr_t addr;
  1071. head = vq->packed.next_avail_idx;
  1072. desc = alloc_indirect_packed(total_sg, gfp);
  1073. if (!desc)
  1074. return -ENOMEM;
  1075. if (unlikely(vq->vq.num_free < 1)) {
  1076. pr_debug("Can't add buf len 1 - avail = 0\n");
  1077. kfree(desc);
  1078. END_USE(vq);
  1079. return -ENOSPC;
  1080. }
  1081. i = 0;
  1082. id = vq->free_head;
  1083. BUG_ON(id == vq->packed.vring.num);
  1084. for (n = 0; n < out_sgs + in_sgs; n++) {
  1085. for (sg = sgs[n]; sg; sg = sg_next(sg)) {
  1086. if (vring_map_one_sg(vq, sg, n < out_sgs ?
  1087. DMA_TO_DEVICE : DMA_FROM_DEVICE, &addr))
  1088. goto unmap_release;
  1089. desc[i].flags = cpu_to_le16(n < out_sgs ?
  1090. 0 : VRING_DESC_F_WRITE);
  1091. desc[i].addr = cpu_to_le64(addr);
  1092. desc[i].len = cpu_to_le32(sg->length);
  1093. i++;
  1094. }
  1095. }
  1096. /* Now that the indirect table is filled in, map it. */
  1097. addr = vring_map_single(vq, desc,
  1098. total_sg * sizeof(struct vring_packed_desc),
  1099. DMA_TO_DEVICE);
  1100. if (vring_mapping_error(vq, addr)) {
  1101. if (vq->premapped)
  1102. goto free_desc;
  1103. goto unmap_release;
  1104. }
  1105. vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
  1106. vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
  1107. sizeof(struct vring_packed_desc));
  1108. vq->packed.vring.desc[head].id = cpu_to_le16(id);
  1109. if (vq->use_dma_api) {
  1110. vq->packed.desc_extra[id].addr = addr;
  1111. vq->packed.desc_extra[id].len = total_sg *
  1112. sizeof(struct vring_packed_desc);
  1113. vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
  1114. vq->packed.avail_used_flags;
  1115. }
  1116. /*
  1117. * A driver MUST NOT make the first descriptor in the list
  1118. * available before all subsequent descriptors comprising
  1119. * the list are made available.
  1120. */
  1121. virtio_wmb(vq->weak_barriers);
  1122. vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
  1123. vq->packed.avail_used_flags);
  1124. /* We're using some buffers from the free list. */
  1125. vq->vq.num_free -= 1;
  1126. /* Update free pointer */
  1127. n = head + 1;
  1128. if (n >= vq->packed.vring.num) {
  1129. n = 0;
  1130. vq->packed.avail_wrap_counter ^= 1;
  1131. vq->packed.avail_used_flags ^=
  1132. 1 << VRING_PACKED_DESC_F_AVAIL |
  1133. 1 << VRING_PACKED_DESC_F_USED;
  1134. }
  1135. vq->packed.next_avail_idx = n;
  1136. vq->free_head = vq->packed.desc_extra[id].next;
  1137. /* Store token and indirect buffer state. */
  1138. vq->packed.desc_state[id].num = 1;
  1139. vq->packed.desc_state[id].data = data;
  1140. vq->packed.desc_state[id].indir_desc = desc;
  1141. vq->packed.desc_state[id].last = id;
  1142. vq->num_added += 1;
  1143. pr_debug("Added buffer head %i to %p\n", head, vq);
  1144. END_USE(vq);
  1145. return 0;
  1146. unmap_release:
  1147. err_idx = i;
  1148. for (i = 0; i < err_idx; i++)
  1149. vring_unmap_desc_packed(vq, &desc[i]);
  1150. free_desc:
  1151. kfree(desc);
  1152. END_USE(vq);
  1153. return -ENOMEM;
  1154. }
  1155. static inline int virtqueue_add_packed(struct virtqueue *_vq,
  1156. struct scatterlist *sgs[],
  1157. unsigned int total_sg,
  1158. unsigned int out_sgs,
  1159. unsigned int in_sgs,
  1160. void *data,
  1161. void *ctx,
  1162. gfp_t gfp)
  1163. {
  1164. struct vring_virtqueue *vq = to_vvq(_vq);
  1165. struct vring_packed_desc *desc;
  1166. struct scatterlist *sg;
  1167. unsigned int i, n, c, descs_used, err_idx;
  1168. __le16 head_flags, flags;
  1169. u16 head, id, prev, curr, avail_used_flags;
  1170. int err;
  1171. START_USE(vq);
  1172. BUG_ON(data == NULL);
  1173. BUG_ON(ctx && vq->indirect);
  1174. if (unlikely(vq->broken)) {
  1175. END_USE(vq);
  1176. return -EIO;
  1177. }
  1178. LAST_ADD_TIME_UPDATE(vq);
  1179. BUG_ON(total_sg == 0);
  1180. if (virtqueue_use_indirect(vq, total_sg)) {
  1181. err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
  1182. in_sgs, data, gfp);
  1183. if (err != -ENOMEM) {
  1184. END_USE(vq);
  1185. return err;
  1186. }
  1187. /* fall back on direct */
  1188. }
  1189. head = vq->packed.next_avail_idx;
  1190. avail_used_flags = vq->packed.avail_used_flags;
  1191. WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
  1192. desc = vq->packed.vring.desc;
  1193. i = head;
  1194. descs_used = total_sg;
  1195. if (unlikely(vq->vq.num_free < descs_used)) {
  1196. pr_debug("Can't add buf len %i - avail = %i\n",
  1197. descs_used, vq->vq.num_free);
  1198. END_USE(vq);
  1199. return -ENOSPC;
  1200. }
  1201. id = vq->free_head;
  1202. BUG_ON(id == vq->packed.vring.num);
  1203. curr = id;
  1204. c = 0;
  1205. for (n = 0; n < out_sgs + in_sgs; n++) {
  1206. for (sg = sgs[n]; sg; sg = sg_next(sg)) {
  1207. dma_addr_t addr;
  1208. if (vring_map_one_sg(vq, sg, n < out_sgs ?
  1209. DMA_TO_DEVICE : DMA_FROM_DEVICE, &addr))
  1210. goto unmap_release;
  1211. flags = cpu_to_le16(vq->packed.avail_used_flags |
  1212. (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
  1213. (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
  1214. if (i == head)
  1215. head_flags = flags;
  1216. else
  1217. desc[i].flags = flags;
  1218. desc[i].addr = cpu_to_le64(addr);
  1219. desc[i].len = cpu_to_le32(sg->length);
  1220. desc[i].id = cpu_to_le16(id);
  1221. if (unlikely(vq->use_dma_api)) {
  1222. vq->packed.desc_extra[curr].addr = addr;
  1223. vq->packed.desc_extra[curr].len = sg->length;
  1224. vq->packed.desc_extra[curr].flags =
  1225. le16_to_cpu(flags);
  1226. }
  1227. prev = curr;
  1228. curr = vq->packed.desc_extra[curr].next;
  1229. if ((unlikely(++i >= vq->packed.vring.num))) {
  1230. i = 0;
  1231. vq->packed.avail_used_flags ^=
  1232. 1 << VRING_PACKED_DESC_F_AVAIL |
  1233. 1 << VRING_PACKED_DESC_F_USED;
  1234. }
  1235. }
  1236. }
  1237. if (i <= head)
  1238. vq->packed.avail_wrap_counter ^= 1;
  1239. /* We're using some buffers from the free list. */
  1240. vq->vq.num_free -= descs_used;
  1241. /* Update free pointer */
  1242. vq->packed.next_avail_idx = i;
  1243. vq->free_head = curr;
  1244. /* Store token. */
  1245. vq->packed.desc_state[id].num = descs_used;
  1246. vq->packed.desc_state[id].data = data;
  1247. vq->packed.desc_state[id].indir_desc = ctx;
  1248. vq->packed.desc_state[id].last = prev;
  1249. /*
  1250. * A driver MUST NOT make the first descriptor in the list
  1251. * available before all subsequent descriptors comprising
  1252. * the list are made available.
  1253. */
  1254. virtio_wmb(vq->weak_barriers);
  1255. vq->packed.vring.desc[head].flags = head_flags;
  1256. vq->num_added += descs_used;
  1257. pr_debug("Added buffer head %i to %p\n", head, vq);
  1258. END_USE(vq);
  1259. return 0;
  1260. unmap_release:
  1261. err_idx = i;
  1262. i = head;
  1263. curr = vq->free_head;
  1264. vq->packed.avail_used_flags = avail_used_flags;
  1265. for (n = 0; n < total_sg; n++) {
  1266. if (i == err_idx)
  1267. break;
  1268. vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]);
  1269. curr = vq->packed.desc_extra[curr].next;
  1270. i++;
  1271. if (i >= vq->packed.vring.num)
  1272. i = 0;
  1273. }
  1274. END_USE(vq);
  1275. return -EIO;
  1276. }
  1277. static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
  1278. {
  1279. struct vring_virtqueue *vq = to_vvq(_vq);
  1280. u16 new, old, off_wrap, flags, wrap_counter, event_idx;
  1281. bool needs_kick;
  1282. union {
  1283. struct {
  1284. __le16 off_wrap;
  1285. __le16 flags;
  1286. };
  1287. u32 u32;
  1288. } snapshot;
  1289. START_USE(vq);
  1290. /*
  1291. * We need to expose the new flags value before checking notification
  1292. * suppressions.
  1293. */
  1294. virtio_mb(vq->weak_barriers);
  1295. old = vq->packed.next_avail_idx - vq->num_added;
  1296. new = vq->packed.next_avail_idx;
  1297. vq->num_added = 0;
  1298. snapshot.u32 = *(u32 *)vq->packed.vring.device;
  1299. flags = le16_to_cpu(snapshot.flags);
  1300. LAST_ADD_TIME_CHECK(vq);
  1301. LAST_ADD_TIME_INVALID(vq);
  1302. if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
  1303. needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
  1304. goto out;
  1305. }
  1306. off_wrap = le16_to_cpu(snapshot.off_wrap);
  1307. wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
  1308. event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
  1309. if (wrap_counter != vq->packed.avail_wrap_counter)
  1310. event_idx -= vq->packed.vring.num;
  1311. needs_kick = vring_need_event(event_idx, new, old);
  1312. out:
  1313. END_USE(vq);
  1314. return needs_kick;
  1315. }
  1316. static void detach_buf_packed(struct vring_virtqueue *vq,
  1317. unsigned int id, void **ctx)
  1318. {
  1319. struct vring_desc_state_packed *state = NULL;
  1320. struct vring_packed_desc *desc;
  1321. unsigned int i, curr;
  1322. state = &vq->packed.desc_state[id];
  1323. /* Clear data ptr. */
  1324. state->data = NULL;
  1325. vq->packed.desc_extra[state->last].next = vq->free_head;
  1326. vq->free_head = id;
  1327. vq->vq.num_free += state->num;
  1328. if (unlikely(vq->use_dma_api)) {
  1329. curr = id;
  1330. for (i = 0; i < state->num; i++) {
  1331. vring_unmap_extra_packed(vq,
  1332. &vq->packed.desc_extra[curr]);
  1333. curr = vq->packed.desc_extra[curr].next;
  1334. }
  1335. }
  1336. if (vq->indirect) {
  1337. u32 len;
  1338. /* Free the indirect table, if any, now that it's unmapped. */
  1339. desc = state->indir_desc;
  1340. if (!desc)
  1341. return;
  1342. if (vq->do_unmap) {
  1343. len = vq->packed.desc_extra[id].len;
  1344. for (i = 0; i < len / sizeof(struct vring_packed_desc);
  1345. i++)
  1346. vring_unmap_desc_packed(vq, &desc[i]);
  1347. }
  1348. kfree(desc);
  1349. state->indir_desc = NULL;
  1350. } else if (ctx) {
  1351. *ctx = state->indir_desc;
  1352. }
  1353. }
  1354. static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
  1355. u16 idx, bool used_wrap_counter)
  1356. {
  1357. bool avail, used;
  1358. u16 flags;
  1359. flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
  1360. avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
  1361. used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
  1362. return avail == used && used == used_wrap_counter;
  1363. }
  1364. static bool more_used_packed(const struct vring_virtqueue *vq)
  1365. {
  1366. u16 last_used;
  1367. u16 last_used_idx;
  1368. bool used_wrap_counter;
  1369. last_used_idx = READ_ONCE(vq->last_used_idx);
  1370. last_used = packed_last_used(last_used_idx);
  1371. used_wrap_counter = packed_used_wrap_counter(last_used_idx);
  1372. return is_used_desc_packed(vq, last_used, used_wrap_counter);
  1373. }
  1374. static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
  1375. unsigned int *len,
  1376. void **ctx)
  1377. {
  1378. struct vring_virtqueue *vq = to_vvq(_vq);
  1379. u16 last_used, id, last_used_idx;
  1380. bool used_wrap_counter;
  1381. void *ret;
  1382. START_USE(vq);
  1383. if (unlikely(vq->broken)) {
  1384. END_USE(vq);
  1385. return NULL;
  1386. }
  1387. if (!more_used_packed(vq)) {
  1388. pr_debug("No more buffers in queue\n");
  1389. END_USE(vq);
  1390. return NULL;
  1391. }
  1392. /* Only get used elements after they have been exposed by host. */
  1393. virtio_rmb(vq->weak_barriers);
  1394. last_used_idx = READ_ONCE(vq->last_used_idx);
  1395. used_wrap_counter = packed_used_wrap_counter(last_used_idx);
  1396. last_used = packed_last_used(last_used_idx);
  1397. id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
  1398. *len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
  1399. if (unlikely(id >= vq->packed.vring.num)) {
  1400. BAD_RING(vq, "id %u out of range\n", id);
  1401. return NULL;
  1402. }
  1403. if (unlikely(!vq->packed.desc_state[id].data)) {
  1404. BAD_RING(vq, "id %u is not a head!\n", id);
  1405. return NULL;
  1406. }
  1407. /* detach_buf_packed clears data, so grab it now. */
  1408. ret = vq->packed.desc_state[id].data;
  1409. detach_buf_packed(vq, id, ctx);
  1410. last_used += vq->packed.desc_state[id].num;
  1411. if (unlikely(last_used >= vq->packed.vring.num)) {
  1412. last_used -= vq->packed.vring.num;
  1413. used_wrap_counter ^= 1;
  1414. }
  1415. last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
  1416. WRITE_ONCE(vq->last_used_idx, last_used);
  1417. /*
  1418. * If we expect an interrupt for the next entry, tell host
  1419. * by writing event index and flush out the write before
  1420. * the read in the next get_buf call.
  1421. */
  1422. if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
  1423. virtio_store_mb(vq->weak_barriers,
  1424. &vq->packed.vring.driver->off_wrap,
  1425. cpu_to_le16(vq->last_used_idx));
  1426. LAST_ADD_TIME_INVALID(vq);
  1427. END_USE(vq);
  1428. return ret;
  1429. }
  1430. static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
  1431. {
  1432. struct vring_virtqueue *vq = to_vvq(_vq);
  1433. if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
  1434. vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
  1435. /*
  1436. * If device triggered an event already it won't trigger one again:
  1437. * no need to disable.
  1438. */
  1439. if (vq->event_triggered)
  1440. return;
  1441. vq->packed.vring.driver->flags =
  1442. cpu_to_le16(vq->packed.event_flags_shadow);
  1443. }
  1444. }
  1445. static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
  1446. {
  1447. struct vring_virtqueue *vq = to_vvq(_vq);
  1448. START_USE(vq);
  1449. /*
  1450. * We optimistically turn back on interrupts, then check if there was
  1451. * more to do.
  1452. */
  1453. if (vq->event) {
  1454. vq->packed.vring.driver->off_wrap =
  1455. cpu_to_le16(vq->last_used_idx);
  1456. /*
  1457. * We need to update event offset and event wrap
  1458. * counter first before updating event flags.
  1459. */
  1460. virtio_wmb(vq->weak_barriers);
  1461. }
  1462. if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
  1463. vq->packed.event_flags_shadow = vq->event ?
  1464. VRING_PACKED_EVENT_FLAG_DESC :
  1465. VRING_PACKED_EVENT_FLAG_ENABLE;
  1466. vq->packed.vring.driver->flags =
  1467. cpu_to_le16(vq->packed.event_flags_shadow);
  1468. }
  1469. END_USE(vq);
  1470. return vq->last_used_idx;
  1471. }
  1472. static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
  1473. {
  1474. struct vring_virtqueue *vq = to_vvq(_vq);
  1475. bool wrap_counter;
  1476. u16 used_idx;
  1477. wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
  1478. used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
  1479. return is_used_desc_packed(vq, used_idx, wrap_counter);
  1480. }
  1481. static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
  1482. {
  1483. struct vring_virtqueue *vq = to_vvq(_vq);
  1484. u16 used_idx, wrap_counter, last_used_idx;
  1485. u16 bufs;
  1486. START_USE(vq);
  1487. /*
  1488. * We optimistically turn back on interrupts, then check if there was
  1489. * more to do.
  1490. */
  1491. if (vq->event) {
  1492. /* TODO: tune this threshold */
  1493. bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
  1494. last_used_idx = READ_ONCE(vq->last_used_idx);
  1495. wrap_counter = packed_used_wrap_counter(last_used_idx);
  1496. used_idx = packed_last_used(last_used_idx) + bufs;
  1497. if (used_idx >= vq->packed.vring.num) {
  1498. used_idx -= vq->packed.vring.num;
  1499. wrap_counter ^= 1;
  1500. }
  1501. vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
  1502. (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
  1503. /*
  1504. * We need to update event offset and event wrap
  1505. * counter first before updating event flags.
  1506. */
  1507. virtio_wmb(vq->weak_barriers);
  1508. }
  1509. if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
  1510. vq->packed.event_flags_shadow = vq->event ?
  1511. VRING_PACKED_EVENT_FLAG_DESC :
  1512. VRING_PACKED_EVENT_FLAG_ENABLE;
  1513. vq->packed.vring.driver->flags =
  1514. cpu_to_le16(vq->packed.event_flags_shadow);
  1515. }
  1516. /*
  1517. * We need to update event suppression structure first
  1518. * before re-checking for more used buffers.
  1519. */
  1520. virtio_mb(vq->weak_barriers);
  1521. last_used_idx = READ_ONCE(vq->last_used_idx);
  1522. wrap_counter = packed_used_wrap_counter(last_used_idx);
  1523. used_idx = packed_last_used(last_used_idx);
  1524. if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
  1525. END_USE(vq);
  1526. return false;
  1527. }
  1528. END_USE(vq);
  1529. return true;
  1530. }
  1531. static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
  1532. {
  1533. struct vring_virtqueue *vq = to_vvq(_vq);
  1534. unsigned int i;
  1535. void *buf;
  1536. START_USE(vq);
  1537. for (i = 0; i < vq->packed.vring.num; i++) {
  1538. if (!vq->packed.desc_state[i].data)
  1539. continue;
  1540. /* detach_buf clears data, so grab it now. */
  1541. buf = vq->packed.desc_state[i].data;
  1542. detach_buf_packed(vq, i, NULL);
  1543. END_USE(vq);
  1544. return buf;
  1545. }
  1546. /* That should have freed everything. */
  1547. BUG_ON(vq->vq.num_free != vq->packed.vring.num);
  1548. END_USE(vq);
  1549. return NULL;
  1550. }
  1551. static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num)
  1552. {
  1553. struct vring_desc_extra *desc_extra;
  1554. unsigned int i;
  1555. desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
  1556. GFP_KERNEL);
  1557. if (!desc_extra)
  1558. return NULL;
  1559. memset(desc_extra, 0, num * sizeof(struct vring_desc_extra));
  1560. for (i = 0; i < num - 1; i++)
  1561. desc_extra[i].next = i + 1;
  1562. return desc_extra;
  1563. }
  1564. static void vring_free_packed(struct vring_virtqueue_packed *vring_packed,
  1565. struct virtio_device *vdev,
  1566. struct device *dma_dev)
  1567. {
  1568. if (vring_packed->vring.desc)
  1569. vring_free_queue(vdev, vring_packed->ring_size_in_bytes,
  1570. vring_packed->vring.desc,
  1571. vring_packed->ring_dma_addr,
  1572. dma_dev);
  1573. if (vring_packed->vring.driver)
  1574. vring_free_queue(vdev, vring_packed->event_size_in_bytes,
  1575. vring_packed->vring.driver,
  1576. vring_packed->driver_event_dma_addr,
  1577. dma_dev);
  1578. if (vring_packed->vring.device)
  1579. vring_free_queue(vdev, vring_packed->event_size_in_bytes,
  1580. vring_packed->vring.device,
  1581. vring_packed->device_event_dma_addr,
  1582. dma_dev);
  1583. kfree(vring_packed->desc_state);
  1584. kfree(vring_packed->desc_extra);
  1585. }
  1586. static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed,
  1587. struct virtio_device *vdev,
  1588. u32 num, struct device *dma_dev)
  1589. {
  1590. struct vring_packed_desc *ring;
  1591. struct vring_packed_desc_event *driver, *device;
  1592. dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
  1593. size_t ring_size_in_bytes, event_size_in_bytes;
  1594. ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
  1595. ring = vring_alloc_queue(vdev, ring_size_in_bytes,
  1596. &ring_dma_addr,
  1597. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
  1598. dma_dev);
  1599. if (!ring)
  1600. goto err;
  1601. vring_packed->vring.desc = ring;
  1602. vring_packed->ring_dma_addr = ring_dma_addr;
  1603. vring_packed->ring_size_in_bytes = ring_size_in_bytes;
  1604. event_size_in_bytes = sizeof(struct vring_packed_desc_event);
  1605. driver = vring_alloc_queue(vdev, event_size_in_bytes,
  1606. &driver_event_dma_addr,
  1607. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
  1608. dma_dev);
  1609. if (!driver)
  1610. goto err;
  1611. vring_packed->vring.driver = driver;
  1612. vring_packed->event_size_in_bytes = event_size_in_bytes;
  1613. vring_packed->driver_event_dma_addr = driver_event_dma_addr;
  1614. device = vring_alloc_queue(vdev, event_size_in_bytes,
  1615. &device_event_dma_addr,
  1616. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
  1617. dma_dev);
  1618. if (!device)
  1619. goto err;
  1620. vring_packed->vring.device = device;
  1621. vring_packed->device_event_dma_addr = device_event_dma_addr;
  1622. vring_packed->vring.num = num;
  1623. return 0;
  1624. err:
  1625. vring_free_packed(vring_packed, vdev, dma_dev);
  1626. return -ENOMEM;
  1627. }
  1628. static int vring_alloc_state_extra_packed(struct vring_virtqueue_packed *vring_packed)
  1629. {
  1630. struct vring_desc_state_packed *state;
  1631. struct vring_desc_extra *extra;
  1632. u32 num = vring_packed->vring.num;
  1633. state = kmalloc_array(num, sizeof(struct vring_desc_state_packed), GFP_KERNEL);
  1634. if (!state)
  1635. goto err_desc_state;
  1636. memset(state, 0, num * sizeof(struct vring_desc_state_packed));
  1637. extra = vring_alloc_desc_extra(num);
  1638. if (!extra)
  1639. goto err_desc_extra;
  1640. vring_packed->desc_state = state;
  1641. vring_packed->desc_extra = extra;
  1642. return 0;
  1643. err_desc_extra:
  1644. kfree(state);
  1645. err_desc_state:
  1646. return -ENOMEM;
  1647. }
  1648. static void virtqueue_vring_init_packed(struct vring_virtqueue_packed *vring_packed,
  1649. bool callback)
  1650. {
  1651. vring_packed->next_avail_idx = 0;
  1652. vring_packed->avail_wrap_counter = 1;
  1653. vring_packed->event_flags_shadow = 0;
  1654. vring_packed->avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
  1655. /* No callback? Tell other side not to bother us. */
  1656. if (!callback) {
  1657. vring_packed->event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
  1658. vring_packed->vring.driver->flags =
  1659. cpu_to_le16(vring_packed->event_flags_shadow);
  1660. }
  1661. }
  1662. static void virtqueue_vring_attach_packed(struct vring_virtqueue *vq,
  1663. struct vring_virtqueue_packed *vring_packed)
  1664. {
  1665. vq->packed = *vring_packed;
  1666. /* Put everything in free lists. */
  1667. vq->free_head = 0;
  1668. }
  1669. static void virtqueue_reinit_packed(struct vring_virtqueue *vq)
  1670. {
  1671. memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes);
  1672. memset(vq->packed.vring.driver, 0, vq->packed.event_size_in_bytes);
  1673. /* we need to reset the desc.flags. For more, see is_used_desc_packed() */
  1674. memset(vq->packed.vring.desc, 0, vq->packed.ring_size_in_bytes);
  1675. virtqueue_init(vq, vq->packed.vring.num);
  1676. virtqueue_vring_init_packed(&vq->packed, !!vq->vq.callback);
  1677. }
  1678. static struct virtqueue *vring_create_virtqueue_packed(
  1679. unsigned int index,
  1680. unsigned int num,
  1681. unsigned int vring_align,
  1682. struct virtio_device *vdev,
  1683. bool weak_barriers,
  1684. bool may_reduce_num,
  1685. bool context,
  1686. bool (*notify)(struct virtqueue *),
  1687. void (*callback)(struct virtqueue *),
  1688. const char *name,
  1689. struct device *dma_dev)
  1690. {
  1691. struct vring_virtqueue_packed vring_packed = {};
  1692. struct vring_virtqueue *vq;
  1693. int err;
  1694. if (vring_alloc_queue_packed(&vring_packed, vdev, num, dma_dev))
  1695. goto err_ring;
  1696. vq = kmalloc(sizeof(*vq), GFP_KERNEL);
  1697. if (!vq)
  1698. goto err_vq;
  1699. vq->vq.callback = callback;
  1700. vq->vq.vdev = vdev;
  1701. vq->vq.name = name;
  1702. vq->vq.index = index;
  1703. vq->vq.reset = false;
  1704. vq->we_own_ring = true;
  1705. vq->notify = notify;
  1706. vq->weak_barriers = weak_barriers;
  1707. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  1708. vq->broken = true;
  1709. #else
  1710. vq->broken = false;
  1711. #endif
  1712. vq->packed_ring = true;
  1713. vq->dma_dev = dma_dev;
  1714. vq->use_dma_api = vring_use_dma_api(vdev);
  1715. vq->premapped = false;
  1716. vq->do_unmap = vq->use_dma_api;
  1717. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
  1718. !context;
  1719. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  1720. if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
  1721. vq->weak_barriers = false;
  1722. err = vring_alloc_state_extra_packed(&vring_packed);
  1723. if (err)
  1724. goto err_state_extra;
  1725. virtqueue_vring_init_packed(&vring_packed, !!callback);
  1726. virtqueue_init(vq, num);
  1727. virtqueue_vring_attach_packed(vq, &vring_packed);
  1728. spin_lock(&vdev->vqs_list_lock);
  1729. list_add_tail(&vq->vq.list, &vdev->vqs);
  1730. spin_unlock(&vdev->vqs_list_lock);
  1731. return &vq->vq;
  1732. err_state_extra:
  1733. kfree(vq);
  1734. err_vq:
  1735. vring_free_packed(&vring_packed, vdev, dma_dev);
  1736. err_ring:
  1737. return NULL;
  1738. }
  1739. static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num)
  1740. {
  1741. struct vring_virtqueue_packed vring_packed = {};
  1742. struct vring_virtqueue *vq = to_vvq(_vq);
  1743. struct virtio_device *vdev = _vq->vdev;
  1744. int err;
  1745. if (vring_alloc_queue_packed(&vring_packed, vdev, num, vring_dma_dev(vq)))
  1746. goto err_ring;
  1747. err = vring_alloc_state_extra_packed(&vring_packed);
  1748. if (err)
  1749. goto err_state_extra;
  1750. vring_free(&vq->vq);
  1751. virtqueue_vring_init_packed(&vring_packed, !!vq->vq.callback);
  1752. virtqueue_init(vq, vring_packed.vring.num);
  1753. virtqueue_vring_attach_packed(vq, &vring_packed);
  1754. return 0;
  1755. err_state_extra:
  1756. vring_free_packed(&vring_packed, vdev, vring_dma_dev(vq));
  1757. err_ring:
  1758. virtqueue_reinit_packed(vq);
  1759. return -ENOMEM;
  1760. }
  1761. static int virtqueue_disable_and_recycle(struct virtqueue *_vq,
  1762. void (*recycle)(struct virtqueue *vq, void *buf))
  1763. {
  1764. struct vring_virtqueue *vq = to_vvq(_vq);
  1765. struct virtio_device *vdev = vq->vq.vdev;
  1766. void *buf;
  1767. int err;
  1768. if (!vq->we_own_ring)
  1769. return -EPERM;
  1770. if (!vdev->config->disable_vq_and_reset)
  1771. return -ENOENT;
  1772. if (!vdev->config->enable_vq_after_reset)
  1773. return -ENOENT;
  1774. err = vdev->config->disable_vq_and_reset(_vq);
  1775. if (err)
  1776. return err;
  1777. while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
  1778. recycle(_vq, buf);
  1779. return 0;
  1780. }
  1781. static int virtqueue_enable_after_reset(struct virtqueue *_vq)
  1782. {
  1783. struct vring_virtqueue *vq = to_vvq(_vq);
  1784. struct virtio_device *vdev = vq->vq.vdev;
  1785. if (vdev->config->enable_vq_after_reset(_vq))
  1786. return -EBUSY;
  1787. return 0;
  1788. }
  1789. /*
  1790. * Generic functions and exported symbols.
  1791. */
  1792. static inline int virtqueue_add(struct virtqueue *_vq,
  1793. struct scatterlist *sgs[],
  1794. unsigned int total_sg,
  1795. unsigned int out_sgs,
  1796. unsigned int in_sgs,
  1797. void *data,
  1798. void *ctx,
  1799. gfp_t gfp)
  1800. {
  1801. struct vring_virtqueue *vq = to_vvq(_vq);
  1802. return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
  1803. out_sgs, in_sgs, data, ctx, gfp) :
  1804. virtqueue_add_split(_vq, sgs, total_sg,
  1805. out_sgs, in_sgs, data, ctx, gfp);
  1806. }
  1807. /**
  1808. * virtqueue_add_sgs - expose buffers to other end
  1809. * @_vq: the struct virtqueue we're talking about.
  1810. * @sgs: array of terminated scatterlists.
  1811. * @out_sgs: the number of scatterlists readable by other side
  1812. * @in_sgs: the number of scatterlists which are writable (after readable ones)
  1813. * @data: the token identifying the buffer.
  1814. * @gfp: how to do memory allocations (if necessary).
  1815. *
  1816. * Caller must ensure we don't call this with other virtqueue operations
  1817. * at the same time (except where noted).
  1818. *
  1819. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  1820. */
  1821. int virtqueue_add_sgs(struct virtqueue *_vq,
  1822. struct scatterlist *sgs[],
  1823. unsigned int out_sgs,
  1824. unsigned int in_sgs,
  1825. void *data,
  1826. gfp_t gfp)
  1827. {
  1828. unsigned int i, total_sg = 0;
  1829. /* Count them first. */
  1830. for (i = 0; i < out_sgs + in_sgs; i++) {
  1831. struct scatterlist *sg;
  1832. for (sg = sgs[i]; sg; sg = sg_next(sg))
  1833. total_sg++;
  1834. }
  1835. return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
  1836. data, NULL, gfp);
  1837. }
  1838. EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
  1839. /**
  1840. * virtqueue_add_outbuf - expose output buffers to other end
  1841. * @vq: the struct virtqueue we're talking about.
  1842. * @sg: scatterlist (must be well-formed and terminated!)
  1843. * @num: the number of entries in @sg readable by other side
  1844. * @data: the token identifying the buffer.
  1845. * @gfp: how to do memory allocations (if necessary).
  1846. *
  1847. * Caller must ensure we don't call this with other virtqueue operations
  1848. * at the same time (except where noted).
  1849. *
  1850. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  1851. */
  1852. int virtqueue_add_outbuf(struct virtqueue *vq,
  1853. struct scatterlist *sg, unsigned int num,
  1854. void *data,
  1855. gfp_t gfp)
  1856. {
  1857. return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
  1858. }
  1859. EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
  1860. /**
  1861. * virtqueue_add_inbuf - expose input buffers to other end
  1862. * @vq: the struct virtqueue we're talking about.
  1863. * @sg: scatterlist (must be well-formed and terminated!)
  1864. * @num: the number of entries in @sg writable by other side
  1865. * @data: the token identifying the buffer.
  1866. * @gfp: how to do memory allocations (if necessary).
  1867. *
  1868. * Caller must ensure we don't call this with other virtqueue operations
  1869. * at the same time (except where noted).
  1870. *
  1871. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  1872. */
  1873. int virtqueue_add_inbuf(struct virtqueue *vq,
  1874. struct scatterlist *sg, unsigned int num,
  1875. void *data,
  1876. gfp_t gfp)
  1877. {
  1878. return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
  1879. }
  1880. EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
  1881. /**
  1882. * virtqueue_add_inbuf_ctx - expose input buffers to other end
  1883. * @vq: the struct virtqueue we're talking about.
  1884. * @sg: scatterlist (must be well-formed and terminated!)
  1885. * @num: the number of entries in @sg writable by other side
  1886. * @data: the token identifying the buffer.
  1887. * @ctx: extra context for the token
  1888. * @gfp: how to do memory allocations (if necessary).
  1889. *
  1890. * Caller must ensure we don't call this with other virtqueue operations
  1891. * at the same time (except where noted).
  1892. *
  1893. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  1894. */
  1895. int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
  1896. struct scatterlist *sg, unsigned int num,
  1897. void *data,
  1898. void *ctx,
  1899. gfp_t gfp)
  1900. {
  1901. return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
  1902. }
  1903. EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
  1904. /**
  1905. * virtqueue_dma_dev - get the dma dev
  1906. * @_vq: the struct virtqueue we're talking about.
  1907. *
  1908. * Returns the dma dev. That can been used for dma api.
  1909. */
  1910. struct device *virtqueue_dma_dev(struct virtqueue *_vq)
  1911. {
  1912. struct vring_virtqueue *vq = to_vvq(_vq);
  1913. if (vq->use_dma_api)
  1914. return vring_dma_dev(vq);
  1915. else
  1916. return NULL;
  1917. }
  1918. EXPORT_SYMBOL_GPL(virtqueue_dma_dev);
  1919. /**
  1920. * virtqueue_kick_prepare - first half of split virtqueue_kick call.
  1921. * @_vq: the struct virtqueue
  1922. *
  1923. * Instead of virtqueue_kick(), you can do:
  1924. * if (virtqueue_kick_prepare(vq))
  1925. * virtqueue_notify(vq);
  1926. *
  1927. * This is sometimes useful because the virtqueue_kick_prepare() needs
  1928. * to be serialized, but the actual virtqueue_notify() call does not.
  1929. */
  1930. bool virtqueue_kick_prepare(struct virtqueue *_vq)
  1931. {
  1932. struct vring_virtqueue *vq = to_vvq(_vq);
  1933. return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
  1934. virtqueue_kick_prepare_split(_vq);
  1935. }
  1936. EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
  1937. /**
  1938. * virtqueue_notify - second half of split virtqueue_kick call.
  1939. * @_vq: the struct virtqueue
  1940. *
  1941. * This does not need to be serialized.
  1942. *
  1943. * Returns false if host notify failed or queue is broken, otherwise true.
  1944. */
  1945. bool virtqueue_notify(struct virtqueue *_vq)
  1946. {
  1947. struct vring_virtqueue *vq = to_vvq(_vq);
  1948. if (unlikely(vq->broken))
  1949. return false;
  1950. /* Prod other side to tell it about changes. */
  1951. if (!vq->notify(_vq)) {
  1952. vq->broken = true;
  1953. return false;
  1954. }
  1955. return true;
  1956. }
  1957. EXPORT_SYMBOL_GPL(virtqueue_notify);
  1958. /**
  1959. * virtqueue_kick - update after add_buf
  1960. * @vq: the struct virtqueue
  1961. *
  1962. * After one or more virtqueue_add_* calls, invoke this to kick
  1963. * the other side.
  1964. *
  1965. * Caller must ensure we don't call this with other virtqueue
  1966. * operations at the same time (except where noted).
  1967. *
  1968. * Returns false if kick failed, otherwise true.
  1969. */
  1970. bool virtqueue_kick(struct virtqueue *vq)
  1971. {
  1972. if (virtqueue_kick_prepare(vq))
  1973. return virtqueue_notify(vq);
  1974. return true;
  1975. }
  1976. EXPORT_SYMBOL_GPL(virtqueue_kick);
  1977. /**
  1978. * virtqueue_get_buf_ctx - get the next used buffer
  1979. * @_vq: the struct virtqueue we're talking about.
  1980. * @len: the length written into the buffer
  1981. * @ctx: extra context for the token
  1982. *
  1983. * If the device wrote data into the buffer, @len will be set to the
  1984. * amount written. This means you don't need to clear the buffer
  1985. * beforehand to ensure there's no data leakage in the case of short
  1986. * writes.
  1987. *
  1988. * Caller must ensure we don't call this with other virtqueue
  1989. * operations at the same time (except where noted).
  1990. *
  1991. * Returns NULL if there are no used buffers, or the "data" token
  1992. * handed to virtqueue_add_*().
  1993. */
  1994. void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
  1995. void **ctx)
  1996. {
  1997. struct vring_virtqueue *vq = to_vvq(_vq);
  1998. return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
  1999. virtqueue_get_buf_ctx_split(_vq, len, ctx);
  2000. }
  2001. EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
  2002. void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
  2003. {
  2004. return virtqueue_get_buf_ctx(_vq, len, NULL);
  2005. }
  2006. EXPORT_SYMBOL_GPL(virtqueue_get_buf);
  2007. /**
  2008. * virtqueue_disable_cb - disable callbacks
  2009. * @_vq: the struct virtqueue we're talking about.
  2010. *
  2011. * Note that this is not necessarily synchronous, hence unreliable and only
  2012. * useful as an optimization.
  2013. *
  2014. * Unlike other operations, this need not be serialized.
  2015. */
  2016. void virtqueue_disable_cb(struct virtqueue *_vq)
  2017. {
  2018. struct vring_virtqueue *vq = to_vvq(_vq);
  2019. if (vq->packed_ring)
  2020. virtqueue_disable_cb_packed(_vq);
  2021. else
  2022. virtqueue_disable_cb_split(_vq);
  2023. }
  2024. EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
  2025. /**
  2026. * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
  2027. * @_vq: the struct virtqueue we're talking about.
  2028. *
  2029. * This re-enables callbacks; it returns current queue state
  2030. * in an opaque unsigned value. This value should be later tested by
  2031. * virtqueue_poll, to detect a possible race between the driver checking for
  2032. * more work, and enabling callbacks.
  2033. *
  2034. * Caller must ensure we don't call this with other virtqueue
  2035. * operations at the same time (except where noted).
  2036. */
  2037. unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq)
  2038. {
  2039. struct vring_virtqueue *vq = to_vvq(_vq);
  2040. if (vq->event_triggered)
  2041. vq->event_triggered = false;
  2042. return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
  2043. virtqueue_enable_cb_prepare_split(_vq);
  2044. }
  2045. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
  2046. /**
  2047. * virtqueue_poll - query pending used buffers
  2048. * @_vq: the struct virtqueue we're talking about.
  2049. * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
  2050. *
  2051. * Returns "true" if there are pending used buffers in the queue.
  2052. *
  2053. * This does not need to be serialized.
  2054. */
  2055. bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx)
  2056. {
  2057. struct vring_virtqueue *vq = to_vvq(_vq);
  2058. if (unlikely(vq->broken))
  2059. return false;
  2060. virtio_mb(vq->weak_barriers);
  2061. return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
  2062. virtqueue_poll_split(_vq, last_used_idx);
  2063. }
  2064. EXPORT_SYMBOL_GPL(virtqueue_poll);
  2065. /**
  2066. * virtqueue_enable_cb - restart callbacks after disable_cb.
  2067. * @_vq: the struct virtqueue we're talking about.
  2068. *
  2069. * This re-enables callbacks; it returns "false" if there are pending
  2070. * buffers in the queue, to detect a possible race between the driver
  2071. * checking for more work, and enabling callbacks.
  2072. *
  2073. * Caller must ensure we don't call this with other virtqueue
  2074. * operations at the same time (except where noted).
  2075. */
  2076. bool virtqueue_enable_cb(struct virtqueue *_vq)
  2077. {
  2078. unsigned int last_used_idx = virtqueue_enable_cb_prepare(_vq);
  2079. return !virtqueue_poll(_vq, last_used_idx);
  2080. }
  2081. EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
  2082. /**
  2083. * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
  2084. * @_vq: the struct virtqueue we're talking about.
  2085. *
  2086. * This re-enables callbacks but hints to the other side to delay
  2087. * interrupts until most of the available buffers have been processed;
  2088. * it returns "false" if there are many pending buffers in the queue,
  2089. * to detect a possible race between the driver checking for more work,
  2090. * and enabling callbacks.
  2091. *
  2092. * Caller must ensure we don't call this with other virtqueue
  2093. * operations at the same time (except where noted).
  2094. */
  2095. bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
  2096. {
  2097. struct vring_virtqueue *vq = to_vvq(_vq);
  2098. if (vq->event_triggered)
  2099. data_race(vq->event_triggered = false);
  2100. return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
  2101. virtqueue_enable_cb_delayed_split(_vq);
  2102. }
  2103. EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
  2104. /**
  2105. * virtqueue_detach_unused_buf - detach first unused buffer
  2106. * @_vq: the struct virtqueue we're talking about.
  2107. *
  2108. * Returns NULL or the "data" token handed to virtqueue_add_*().
  2109. * This is not valid on an active queue; it is useful for device
  2110. * shutdown or the reset queue.
  2111. */
  2112. void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
  2113. {
  2114. struct vring_virtqueue *vq = to_vvq(_vq);
  2115. return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
  2116. virtqueue_detach_unused_buf_split(_vq);
  2117. }
  2118. EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
  2119. static inline bool more_used(const struct vring_virtqueue *vq)
  2120. {
  2121. return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
  2122. }
  2123. /**
  2124. * vring_interrupt - notify a virtqueue on an interrupt
  2125. * @irq: the IRQ number (ignored)
  2126. * @_vq: the struct virtqueue to notify
  2127. *
  2128. * Calls the callback function of @_vq to process the virtqueue
  2129. * notification.
  2130. */
  2131. irqreturn_t vring_interrupt(int irq, void *_vq)
  2132. {
  2133. struct vring_virtqueue *vq = to_vvq(_vq);
  2134. if (!more_used(vq)) {
  2135. pr_debug("virtqueue interrupt with no work for %p\n", vq);
  2136. return IRQ_NONE;
  2137. }
  2138. if (unlikely(vq->broken)) {
  2139. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  2140. dev_warn_once(&vq->vq.vdev->dev,
  2141. "virtio vring IRQ raised before DRIVER_OK");
  2142. return IRQ_NONE;
  2143. #else
  2144. return IRQ_HANDLED;
  2145. #endif
  2146. }
  2147. /* Just a hint for performance: so it's ok that this can be racy! */
  2148. if (vq->event)
  2149. data_race(vq->event_triggered = true);
  2150. pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
  2151. if (vq->vq.callback)
  2152. vq->vq.callback(&vq->vq);
  2153. return IRQ_HANDLED;
  2154. }
  2155. EXPORT_SYMBOL_GPL(vring_interrupt);
  2156. /* Only available for split ring */
  2157. static struct virtqueue *__vring_new_virtqueue(unsigned int index,
  2158. struct vring_virtqueue_split *vring_split,
  2159. struct virtio_device *vdev,
  2160. bool weak_barriers,
  2161. bool context,
  2162. bool (*notify)(struct virtqueue *),
  2163. void (*callback)(struct virtqueue *),
  2164. const char *name,
  2165. struct device *dma_dev)
  2166. {
  2167. struct vring_virtqueue *vq;
  2168. int err;
  2169. if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
  2170. return NULL;
  2171. vq = kmalloc(sizeof(*vq), GFP_KERNEL);
  2172. if (!vq)
  2173. return NULL;
  2174. vq->packed_ring = false;
  2175. vq->vq.callback = callback;
  2176. vq->vq.vdev = vdev;
  2177. vq->vq.name = name;
  2178. vq->vq.index = index;
  2179. vq->vq.reset = false;
  2180. vq->we_own_ring = false;
  2181. vq->notify = notify;
  2182. vq->weak_barriers = weak_barriers;
  2183. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  2184. vq->broken = true;
  2185. #else
  2186. vq->broken = false;
  2187. #endif
  2188. vq->dma_dev = dma_dev;
  2189. vq->use_dma_api = vring_use_dma_api(vdev);
  2190. vq->premapped = false;
  2191. vq->do_unmap = vq->use_dma_api;
  2192. vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
  2193. !context;
  2194. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  2195. if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
  2196. vq->weak_barriers = false;
  2197. err = vring_alloc_state_extra_split(vring_split);
  2198. if (err) {
  2199. kfree(vq);
  2200. return NULL;
  2201. }
  2202. virtqueue_vring_init_split(vring_split, vq);
  2203. virtqueue_init(vq, vring_split->vring.num);
  2204. virtqueue_vring_attach_split(vq, vring_split);
  2205. spin_lock(&vdev->vqs_list_lock);
  2206. list_add_tail(&vq->vq.list, &vdev->vqs);
  2207. spin_unlock(&vdev->vqs_list_lock);
  2208. return &vq->vq;
  2209. }
  2210. struct virtqueue *vring_create_virtqueue(
  2211. unsigned int index,
  2212. unsigned int num,
  2213. unsigned int vring_align,
  2214. struct virtio_device *vdev,
  2215. bool weak_barriers,
  2216. bool may_reduce_num,
  2217. bool context,
  2218. bool (*notify)(struct virtqueue *),
  2219. void (*callback)(struct virtqueue *),
  2220. const char *name)
  2221. {
  2222. if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
  2223. return vring_create_virtqueue_packed(index, num, vring_align,
  2224. vdev, weak_barriers, may_reduce_num,
  2225. context, notify, callback, name, vdev->dev.parent);
  2226. return vring_create_virtqueue_split(index, num, vring_align,
  2227. vdev, weak_barriers, may_reduce_num,
  2228. context, notify, callback, name, vdev->dev.parent);
  2229. }
  2230. EXPORT_SYMBOL_GPL(vring_create_virtqueue);
  2231. struct virtqueue *vring_create_virtqueue_dma(
  2232. unsigned int index,
  2233. unsigned int num,
  2234. unsigned int vring_align,
  2235. struct virtio_device *vdev,
  2236. bool weak_barriers,
  2237. bool may_reduce_num,
  2238. bool context,
  2239. bool (*notify)(struct virtqueue *),
  2240. void (*callback)(struct virtqueue *),
  2241. const char *name,
  2242. struct device *dma_dev)
  2243. {
  2244. if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
  2245. return vring_create_virtqueue_packed(index, num, vring_align,
  2246. vdev, weak_barriers, may_reduce_num,
  2247. context, notify, callback, name, dma_dev);
  2248. return vring_create_virtqueue_split(index, num, vring_align,
  2249. vdev, weak_barriers, may_reduce_num,
  2250. context, notify, callback, name, dma_dev);
  2251. }
  2252. EXPORT_SYMBOL_GPL(vring_create_virtqueue_dma);
  2253. /**
  2254. * virtqueue_resize - resize the vring of vq
  2255. * @_vq: the struct virtqueue we're talking about.
  2256. * @num: new ring num
  2257. * @recycle: callback to recycle unused buffers
  2258. * @recycle_done: callback to be invoked when recycle for all unused buffers done
  2259. *
  2260. * When it is really necessary to create a new vring, it will set the current vq
  2261. * into the reset state. Then call the passed callback to recycle the buffer
  2262. * that is no longer used. Only after the new vring is successfully created, the
  2263. * old vring will be released.
  2264. *
  2265. * Caller must ensure we don't call this with other virtqueue operations
  2266. * at the same time (except where noted).
  2267. *
  2268. * Returns zero or a negative error.
  2269. * 0: success.
  2270. * -ENOMEM: Failed to allocate a new ring, fall back to the original ring size.
  2271. * vq can still work normally
  2272. * -EBUSY: Failed to sync with device, vq may not work properly
  2273. * -ENOENT: Transport or device not supported
  2274. * -E2BIG/-EINVAL: num error
  2275. * -EPERM: Operation not permitted
  2276. *
  2277. */
  2278. int virtqueue_resize(struct virtqueue *_vq, u32 num,
  2279. void (*recycle)(struct virtqueue *vq, void *buf),
  2280. void (*recycle_done)(struct virtqueue *vq))
  2281. {
  2282. struct vring_virtqueue *vq = to_vvq(_vq);
  2283. int err, err_reset;
  2284. if (num > vq->vq.num_max)
  2285. return -E2BIG;
  2286. if (!num)
  2287. return -EINVAL;
  2288. if ((vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num) == num)
  2289. return 0;
  2290. err = virtqueue_disable_and_recycle(_vq, recycle);
  2291. if (err)
  2292. return err;
  2293. if (recycle_done)
  2294. recycle_done(_vq);
  2295. if (vq->packed_ring)
  2296. err = virtqueue_resize_packed(_vq, num);
  2297. else
  2298. err = virtqueue_resize_split(_vq, num);
  2299. err_reset = virtqueue_enable_after_reset(_vq);
  2300. if (err_reset)
  2301. return err_reset;
  2302. return err;
  2303. }
  2304. EXPORT_SYMBOL_GPL(virtqueue_resize);
  2305. /**
  2306. * virtqueue_set_dma_premapped - set the vring premapped mode
  2307. * @_vq: the struct virtqueue we're talking about.
  2308. *
  2309. * Enable the premapped mode of the vq.
  2310. *
  2311. * The vring in premapped mode does not do dma internally, so the driver must
  2312. * do dma mapping in advance. The driver must pass the dma_address through
  2313. * dma_address of scatterlist. When the driver got a used buffer from
  2314. * the vring, it has to unmap the dma address.
  2315. *
  2316. * This function must be called immediately after creating the vq, or after vq
  2317. * reset, and before adding any buffers to it.
  2318. *
  2319. * Caller must ensure we don't call this with other virtqueue operations
  2320. * at the same time (except where noted).
  2321. *
  2322. * Returns zero or a negative error.
  2323. * 0: success.
  2324. * -EINVAL: too late to enable premapped mode, the vq already contains buffers.
  2325. */
  2326. int virtqueue_set_dma_premapped(struct virtqueue *_vq)
  2327. {
  2328. struct vring_virtqueue *vq = to_vvq(_vq);
  2329. u32 num;
  2330. START_USE(vq);
  2331. num = vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
  2332. if (num != vq->vq.num_free) {
  2333. END_USE(vq);
  2334. return -EINVAL;
  2335. }
  2336. vq->premapped = true;
  2337. vq->do_unmap = false;
  2338. END_USE(vq);
  2339. return 0;
  2340. }
  2341. EXPORT_SYMBOL_GPL(virtqueue_set_dma_premapped);
  2342. /**
  2343. * virtqueue_reset - detach and recycle all unused buffers
  2344. * @_vq: the struct virtqueue we're talking about.
  2345. * @recycle: callback to recycle unused buffers
  2346. * @recycle_done: callback to be invoked when recycle for all unused buffers done
  2347. *
  2348. * Caller must ensure we don't call this with other virtqueue operations
  2349. * at the same time (except where noted).
  2350. *
  2351. * Returns zero or a negative error.
  2352. * 0: success.
  2353. * -EBUSY: Failed to sync with device, vq may not work properly
  2354. * -ENOENT: Transport or device not supported
  2355. * -EPERM: Operation not permitted
  2356. */
  2357. int virtqueue_reset(struct virtqueue *_vq,
  2358. void (*recycle)(struct virtqueue *vq, void *buf),
  2359. void (*recycle_done)(struct virtqueue *vq))
  2360. {
  2361. struct vring_virtqueue *vq = to_vvq(_vq);
  2362. int err;
  2363. err = virtqueue_disable_and_recycle(_vq, recycle);
  2364. if (err)
  2365. return err;
  2366. if (recycle_done)
  2367. recycle_done(_vq);
  2368. if (vq->packed_ring)
  2369. virtqueue_reinit_packed(vq);
  2370. else
  2371. virtqueue_reinit_split(vq);
  2372. return virtqueue_enable_after_reset(_vq);
  2373. }
  2374. EXPORT_SYMBOL_GPL(virtqueue_reset);
  2375. /* Only available for split ring */
  2376. struct virtqueue *vring_new_virtqueue(unsigned int index,
  2377. unsigned int num,
  2378. unsigned int vring_align,
  2379. struct virtio_device *vdev,
  2380. bool weak_barriers,
  2381. bool context,
  2382. void *pages,
  2383. bool (*notify)(struct virtqueue *vq),
  2384. void (*callback)(struct virtqueue *vq),
  2385. const char *name)
  2386. {
  2387. struct vring_virtqueue_split vring_split = {};
  2388. if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
  2389. return NULL;
  2390. vring_init(&vring_split.vring, num, pages, vring_align);
  2391. return __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
  2392. context, notify, callback, name,
  2393. vdev->dev.parent);
  2394. }
  2395. EXPORT_SYMBOL_GPL(vring_new_virtqueue);
  2396. static void vring_free(struct virtqueue *_vq)
  2397. {
  2398. struct vring_virtqueue *vq = to_vvq(_vq);
  2399. if (vq->we_own_ring) {
  2400. if (vq->packed_ring) {
  2401. vring_free_queue(vq->vq.vdev,
  2402. vq->packed.ring_size_in_bytes,
  2403. vq->packed.vring.desc,
  2404. vq->packed.ring_dma_addr,
  2405. vring_dma_dev(vq));
  2406. vring_free_queue(vq->vq.vdev,
  2407. vq->packed.event_size_in_bytes,
  2408. vq->packed.vring.driver,
  2409. vq->packed.driver_event_dma_addr,
  2410. vring_dma_dev(vq));
  2411. vring_free_queue(vq->vq.vdev,
  2412. vq->packed.event_size_in_bytes,
  2413. vq->packed.vring.device,
  2414. vq->packed.device_event_dma_addr,
  2415. vring_dma_dev(vq));
  2416. kfree(vq->packed.desc_state);
  2417. kfree(vq->packed.desc_extra);
  2418. } else {
  2419. vring_free_queue(vq->vq.vdev,
  2420. vq->split.queue_size_in_bytes,
  2421. vq->split.vring.desc,
  2422. vq->split.queue_dma_addr,
  2423. vring_dma_dev(vq));
  2424. }
  2425. }
  2426. if (!vq->packed_ring) {
  2427. kfree(vq->split.desc_state);
  2428. kfree(vq->split.desc_extra);
  2429. }
  2430. }
  2431. void vring_del_virtqueue(struct virtqueue *_vq)
  2432. {
  2433. struct vring_virtqueue *vq = to_vvq(_vq);
  2434. spin_lock(&vq->vq.vdev->vqs_list_lock);
  2435. list_del(&_vq->list);
  2436. spin_unlock(&vq->vq.vdev->vqs_list_lock);
  2437. vring_free(_vq);
  2438. kfree(vq);
  2439. }
  2440. EXPORT_SYMBOL_GPL(vring_del_virtqueue);
  2441. u32 vring_notification_data(struct virtqueue *_vq)
  2442. {
  2443. struct vring_virtqueue *vq = to_vvq(_vq);
  2444. u16 next;
  2445. if (vq->packed_ring)
  2446. next = (vq->packed.next_avail_idx &
  2447. ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR))) |
  2448. vq->packed.avail_wrap_counter <<
  2449. VRING_PACKED_EVENT_F_WRAP_CTR;
  2450. else
  2451. next = vq->split.avail_idx_shadow;
  2452. return next << 16 | _vq->index;
  2453. }
  2454. EXPORT_SYMBOL_GPL(vring_notification_data);
  2455. /* Manipulates transport-specific feature bits. */
  2456. void vring_transport_features(struct virtio_device *vdev)
  2457. {
  2458. unsigned int i;
  2459. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
  2460. switch (i) {
  2461. case VIRTIO_RING_F_INDIRECT_DESC:
  2462. break;
  2463. case VIRTIO_RING_F_EVENT_IDX:
  2464. break;
  2465. case VIRTIO_F_VERSION_1:
  2466. break;
  2467. case VIRTIO_F_ACCESS_PLATFORM:
  2468. break;
  2469. case VIRTIO_F_RING_PACKED:
  2470. break;
  2471. case VIRTIO_F_ORDER_PLATFORM:
  2472. break;
  2473. case VIRTIO_F_NOTIFICATION_DATA:
  2474. break;
  2475. default:
  2476. /* We don't understand this bit. */
  2477. __virtio_clear_bit(vdev, i);
  2478. }
  2479. }
  2480. }
  2481. EXPORT_SYMBOL_GPL(vring_transport_features);
  2482. /**
  2483. * virtqueue_get_vring_size - return the size of the virtqueue's vring
  2484. * @_vq: the struct virtqueue containing the vring of interest.
  2485. *
  2486. * Returns the size of the vring. This is mainly used for boasting to
  2487. * userspace. Unlike other operations, this need not be serialized.
  2488. */
  2489. unsigned int virtqueue_get_vring_size(const struct virtqueue *_vq)
  2490. {
  2491. const struct vring_virtqueue *vq = to_vvq(_vq);
  2492. return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
  2493. }
  2494. EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
  2495. /*
  2496. * This function should only be called by the core, not directly by the driver.
  2497. */
  2498. void __virtqueue_break(struct virtqueue *_vq)
  2499. {
  2500. struct vring_virtqueue *vq = to_vvq(_vq);
  2501. /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
  2502. WRITE_ONCE(vq->broken, true);
  2503. }
  2504. EXPORT_SYMBOL_GPL(__virtqueue_break);
  2505. /*
  2506. * This function should only be called by the core, not directly by the driver.
  2507. */
  2508. void __virtqueue_unbreak(struct virtqueue *_vq)
  2509. {
  2510. struct vring_virtqueue *vq = to_vvq(_vq);
  2511. /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
  2512. WRITE_ONCE(vq->broken, false);
  2513. }
  2514. EXPORT_SYMBOL_GPL(__virtqueue_unbreak);
  2515. bool virtqueue_is_broken(const struct virtqueue *_vq)
  2516. {
  2517. const struct vring_virtqueue *vq = to_vvq(_vq);
  2518. return READ_ONCE(vq->broken);
  2519. }
  2520. EXPORT_SYMBOL_GPL(virtqueue_is_broken);
  2521. /*
  2522. * This should prevent the device from being used, allowing drivers to
  2523. * recover. You may need to grab appropriate locks to flush.
  2524. */
  2525. void virtio_break_device(struct virtio_device *dev)
  2526. {
  2527. struct virtqueue *_vq;
  2528. spin_lock(&dev->vqs_list_lock);
  2529. list_for_each_entry(_vq, &dev->vqs, list) {
  2530. struct vring_virtqueue *vq = to_vvq(_vq);
  2531. /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
  2532. WRITE_ONCE(vq->broken, true);
  2533. }
  2534. spin_unlock(&dev->vqs_list_lock);
  2535. }
  2536. EXPORT_SYMBOL_GPL(virtio_break_device);
  2537. /*
  2538. * This should allow the device to be used by the driver. You may
  2539. * need to grab appropriate locks to flush the write to
  2540. * vq->broken. This should only be used in some specific case e.g
  2541. * (probing and restoring). This function should only be called by the
  2542. * core, not directly by the driver.
  2543. */
  2544. void __virtio_unbreak_device(struct virtio_device *dev)
  2545. {
  2546. struct virtqueue *_vq;
  2547. spin_lock(&dev->vqs_list_lock);
  2548. list_for_each_entry(_vq, &dev->vqs, list) {
  2549. struct vring_virtqueue *vq = to_vvq(_vq);
  2550. /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
  2551. WRITE_ONCE(vq->broken, false);
  2552. }
  2553. spin_unlock(&dev->vqs_list_lock);
  2554. }
  2555. EXPORT_SYMBOL_GPL(__virtio_unbreak_device);
  2556. dma_addr_t virtqueue_get_desc_addr(const struct virtqueue *_vq)
  2557. {
  2558. const struct vring_virtqueue *vq = to_vvq(_vq);
  2559. BUG_ON(!vq->we_own_ring);
  2560. if (vq->packed_ring)
  2561. return vq->packed.ring_dma_addr;
  2562. return vq->split.queue_dma_addr;
  2563. }
  2564. EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
  2565. dma_addr_t virtqueue_get_avail_addr(const struct virtqueue *_vq)
  2566. {
  2567. const struct vring_virtqueue *vq = to_vvq(_vq);
  2568. BUG_ON(!vq->we_own_ring);
  2569. if (vq->packed_ring)
  2570. return vq->packed.driver_event_dma_addr;
  2571. return vq->split.queue_dma_addr +
  2572. ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
  2573. }
  2574. EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
  2575. dma_addr_t virtqueue_get_used_addr(const struct virtqueue *_vq)
  2576. {
  2577. const struct vring_virtqueue *vq = to_vvq(_vq);
  2578. BUG_ON(!vq->we_own_ring);
  2579. if (vq->packed_ring)
  2580. return vq->packed.device_event_dma_addr;
  2581. return vq->split.queue_dma_addr +
  2582. ((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
  2583. }
  2584. EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
  2585. /* Only available for split ring */
  2586. const struct vring *virtqueue_get_vring(const struct virtqueue *vq)
  2587. {
  2588. return &to_vvq(vq)->split.vring;
  2589. }
  2590. EXPORT_SYMBOL_GPL(virtqueue_get_vring);
  2591. /**
  2592. * virtqueue_dma_map_single_attrs - map DMA for _vq
  2593. * @_vq: the struct virtqueue we're talking about.
  2594. * @ptr: the pointer of the buffer to do dma
  2595. * @size: the size of the buffer to do dma
  2596. * @dir: DMA direction
  2597. * @attrs: DMA Attrs
  2598. *
  2599. * The caller calls this to do dma mapping in advance. The DMA address can be
  2600. * passed to this _vq when it is in pre-mapped mode.
  2601. *
  2602. * return DMA address. Caller should check that by virtqueue_dma_mapping_error().
  2603. */
  2604. dma_addr_t virtqueue_dma_map_single_attrs(struct virtqueue *_vq, void *ptr,
  2605. size_t size,
  2606. enum dma_data_direction dir,
  2607. unsigned long attrs)
  2608. {
  2609. struct vring_virtqueue *vq = to_vvq(_vq);
  2610. if (!vq->use_dma_api) {
  2611. kmsan_handle_dma(virt_to_page(ptr), offset_in_page(ptr), size, dir);
  2612. return (dma_addr_t)virt_to_phys(ptr);
  2613. }
  2614. return dma_map_single_attrs(vring_dma_dev(vq), ptr, size, dir, attrs);
  2615. }
  2616. EXPORT_SYMBOL_GPL(virtqueue_dma_map_single_attrs);
  2617. /**
  2618. * virtqueue_dma_unmap_single_attrs - unmap DMA for _vq
  2619. * @_vq: the struct virtqueue we're talking about.
  2620. * @addr: the dma address to unmap
  2621. * @size: the size of the buffer
  2622. * @dir: DMA direction
  2623. * @attrs: DMA Attrs
  2624. *
  2625. * Unmap the address that is mapped by the virtqueue_dma_map_* APIs.
  2626. *
  2627. */
  2628. void virtqueue_dma_unmap_single_attrs(struct virtqueue *_vq, dma_addr_t addr,
  2629. size_t size, enum dma_data_direction dir,
  2630. unsigned long attrs)
  2631. {
  2632. struct vring_virtqueue *vq = to_vvq(_vq);
  2633. if (!vq->use_dma_api)
  2634. return;
  2635. dma_unmap_single_attrs(vring_dma_dev(vq), addr, size, dir, attrs);
  2636. }
  2637. EXPORT_SYMBOL_GPL(virtqueue_dma_unmap_single_attrs);
  2638. /**
  2639. * virtqueue_dma_mapping_error - check dma address
  2640. * @_vq: the struct virtqueue we're talking about.
  2641. * @addr: DMA address
  2642. *
  2643. * Returns 0 means dma valid. Other means invalid dma address.
  2644. */
  2645. int virtqueue_dma_mapping_error(struct virtqueue *_vq, dma_addr_t addr)
  2646. {
  2647. struct vring_virtqueue *vq = to_vvq(_vq);
  2648. if (!vq->use_dma_api)
  2649. return 0;
  2650. return dma_mapping_error(vring_dma_dev(vq), addr);
  2651. }
  2652. EXPORT_SYMBOL_GPL(virtqueue_dma_mapping_error);
  2653. /**
  2654. * virtqueue_dma_need_sync - check a dma address needs sync
  2655. * @_vq: the struct virtqueue we're talking about.
  2656. * @addr: DMA address
  2657. *
  2658. * Check if the dma address mapped by the virtqueue_dma_map_* APIs needs to be
  2659. * synchronized
  2660. *
  2661. * return bool
  2662. */
  2663. bool virtqueue_dma_need_sync(struct virtqueue *_vq, dma_addr_t addr)
  2664. {
  2665. struct vring_virtqueue *vq = to_vvq(_vq);
  2666. if (!vq->use_dma_api)
  2667. return false;
  2668. return dma_need_sync(vring_dma_dev(vq), addr);
  2669. }
  2670. EXPORT_SYMBOL_GPL(virtqueue_dma_need_sync);
  2671. /**
  2672. * virtqueue_dma_sync_single_range_for_cpu - dma sync for cpu
  2673. * @_vq: the struct virtqueue we're talking about.
  2674. * @addr: DMA address
  2675. * @offset: DMA address offset
  2676. * @size: buf size for sync
  2677. * @dir: DMA direction
  2678. *
  2679. * Before calling this function, use virtqueue_dma_need_sync() to confirm that
  2680. * the DMA address really needs to be synchronized
  2681. *
  2682. */
  2683. void virtqueue_dma_sync_single_range_for_cpu(struct virtqueue *_vq,
  2684. dma_addr_t addr,
  2685. unsigned long offset, size_t size,
  2686. enum dma_data_direction dir)
  2687. {
  2688. struct vring_virtqueue *vq = to_vvq(_vq);
  2689. struct device *dev = vring_dma_dev(vq);
  2690. if (!vq->use_dma_api)
  2691. return;
  2692. dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
  2693. }
  2694. EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_cpu);
  2695. /**
  2696. * virtqueue_dma_sync_single_range_for_device - dma sync for device
  2697. * @_vq: the struct virtqueue we're talking about.
  2698. * @addr: DMA address
  2699. * @offset: DMA address offset
  2700. * @size: buf size for sync
  2701. * @dir: DMA direction
  2702. *
  2703. * Before calling this function, use virtqueue_dma_need_sync() to confirm that
  2704. * the DMA address really needs to be synchronized
  2705. */
  2706. void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq,
  2707. dma_addr_t addr,
  2708. unsigned long offset, size_t size,
  2709. enum dma_data_direction dir)
  2710. {
  2711. struct vring_virtqueue *vq = to_vvq(_vq);
  2712. struct device *dev = vring_dma_dev(vq);
  2713. if (!vq->use_dma_api)
  2714. return;
  2715. dma_sync_single_range_for_device(dev, addr, offset, size, dir);
  2716. }
  2717. EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_device);
  2718. MODULE_DESCRIPTION("Virtio ring implementation");
  2719. MODULE_LICENSE("GPL");