virtio_ring.c 102 KB

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