virtio_ring.c 74 KB

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