vmbus.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  1. /*
  2. * QEMU Hyper-V VMBus
  3. *
  4. * Copyright (c) 2017-2018 Virtuozzo International GmbH.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu/error-report.h"
  11. #include "qemu/main-loop.h"
  12. #include "qapi/error.h"
  13. #include "migration/vmstate.h"
  14. #include "hw/qdev-properties.h"
  15. #include "hw/hyperv/hyperv.h"
  16. #include "hw/hyperv/vmbus.h"
  17. #include "hw/hyperv/vmbus-bridge.h"
  18. #include "hw/sysbus.h"
  19. #include "cpu.h"
  20. #include "trace.h"
  21. #define TYPE_VMBUS "vmbus"
  22. #define VMBUS(obj) OBJECT_CHECK(VMBus, (obj), TYPE_VMBUS)
  23. enum {
  24. VMGPADL_INIT,
  25. VMGPADL_ALIVE,
  26. VMGPADL_TEARINGDOWN,
  27. VMGPADL_TORNDOWN,
  28. };
  29. struct VMBusGpadl {
  30. /* GPADL id */
  31. uint32_t id;
  32. /* associated channel id (rudimentary?) */
  33. uint32_t child_relid;
  34. /* number of pages in the GPADL as declared in GPADL_HEADER message */
  35. uint32_t num_gfns;
  36. /*
  37. * Due to limited message size, GPADL may not fit fully in a single
  38. * GPADL_HEADER message, and is further popluated using GPADL_BODY
  39. * messages. @seen_gfns is the number of pages seen so far; once it
  40. * reaches @num_gfns, the GPADL is ready to use.
  41. */
  42. uint32_t seen_gfns;
  43. /* array of GFNs (of size @num_gfns once allocated) */
  44. uint64_t *gfns;
  45. uint8_t state;
  46. QTAILQ_ENTRY(VMBusGpadl) link;
  47. VMBus *vmbus;
  48. unsigned refcount;
  49. };
  50. /*
  51. * Wrap sequential read from / write to GPADL.
  52. */
  53. typedef struct GpadlIter {
  54. VMBusGpadl *gpadl;
  55. AddressSpace *as;
  56. DMADirection dir;
  57. /* offset into GPADL where the next i/o will be performed */
  58. uint32_t off;
  59. /*
  60. * Cached mapping of the currently accessed page, up to page boundary.
  61. * Updated lazily on i/o.
  62. * Note: MemoryRegionCache can not be used here because pages in the GPADL
  63. * are non-contiguous and may belong to different memory regions.
  64. */
  65. void *map;
  66. /* offset after last i/o (i.e. not affected by seek) */
  67. uint32_t last_off;
  68. /*
  69. * Indicator that the iterator is active and may have a cached mapping.
  70. * Allows to enforce bracketing of all i/o (which may create cached
  71. * mappings) and thus exclude mapping leaks.
  72. */
  73. bool active;
  74. } GpadlIter;
  75. /*
  76. * Ring buffer. There are two of them, sitting in the same GPADL, for each
  77. * channel.
  78. * Each ring buffer consists of a set of pages, with the first page containing
  79. * the ring buffer header, and the remaining pages being for data packets.
  80. */
  81. typedef struct VMBusRingBufCommon {
  82. AddressSpace *as;
  83. /* GPA of the ring buffer header */
  84. dma_addr_t rb_addr;
  85. /* start and length of the ring buffer data area within GPADL */
  86. uint32_t base;
  87. uint32_t len;
  88. GpadlIter iter;
  89. } VMBusRingBufCommon;
  90. typedef struct VMBusSendRingBuf {
  91. VMBusRingBufCommon common;
  92. /* current write index, to be committed at the end of send */
  93. uint32_t wr_idx;
  94. /* write index at the start of send */
  95. uint32_t last_wr_idx;
  96. /* space to be requested from the guest */
  97. uint32_t wanted;
  98. /* space reserved for planned sends */
  99. uint32_t reserved;
  100. /* last seen read index */
  101. uint32_t last_seen_rd_idx;
  102. } VMBusSendRingBuf;
  103. typedef struct VMBusRecvRingBuf {
  104. VMBusRingBufCommon common;
  105. /* current read index, to be committed at the end of receive */
  106. uint32_t rd_idx;
  107. /* read index at the start of receive */
  108. uint32_t last_rd_idx;
  109. /* last seen write index */
  110. uint32_t last_seen_wr_idx;
  111. } VMBusRecvRingBuf;
  112. enum {
  113. VMOFFER_INIT,
  114. VMOFFER_SENDING,
  115. VMOFFER_SENT,
  116. };
  117. enum {
  118. VMCHAN_INIT,
  119. VMCHAN_OPENING,
  120. VMCHAN_OPEN,
  121. };
  122. struct VMBusChannel {
  123. VMBusDevice *dev;
  124. /* channel id */
  125. uint32_t id;
  126. /*
  127. * subchannel index within the device; subchannel #0 is "primary" and
  128. * always exists
  129. */
  130. uint16_t subchan_idx;
  131. uint32_t open_id;
  132. /* VP_INDEX of the vCPU to notify with (synthetic) interrupts */
  133. uint32_t target_vp;
  134. /* GPADL id to use for the ring buffers */
  135. uint32_t ringbuf_gpadl;
  136. /* start (in pages) of the send ring buffer within @ringbuf_gpadl */
  137. uint32_t ringbuf_send_offset;
  138. uint8_t offer_state;
  139. uint8_t state;
  140. bool is_open;
  141. /* main device worker; copied from the device class */
  142. VMBusChannelNotifyCb notify_cb;
  143. /*
  144. * guest->host notifications, either sent directly or dispatched via
  145. * interrupt page (older VMBus)
  146. */
  147. EventNotifier notifier;
  148. VMBus *vmbus;
  149. /*
  150. * SINT route to signal with host->guest notifications; may be shared with
  151. * the main VMBus SINT route
  152. */
  153. HvSintRoute *notify_route;
  154. VMBusGpadl *gpadl;
  155. VMBusSendRingBuf send_ringbuf;
  156. VMBusRecvRingBuf recv_ringbuf;
  157. QTAILQ_ENTRY(VMBusChannel) link;
  158. };
  159. /*
  160. * Hyper-V spec mandates that every message port has 16 buffers, which means
  161. * that the guest can post up to this many messages without blocking.
  162. * Therefore a queue for incoming messages has to be provided.
  163. * For outgoing (i.e. host->guest) messages there's no queue; the VMBus just
  164. * doesn't transition to a new state until the message is known to have been
  165. * successfully delivered to the respective SynIC message slot.
  166. */
  167. #define HV_MSG_QUEUE_LEN 16
  168. /* Hyper-V devices never use channel #0. Must be something special. */
  169. #define VMBUS_FIRST_CHANID 1
  170. /* Each channel occupies one bit within a single event page sint slot. */
  171. #define VMBUS_CHANID_COUNT (HV_EVENT_FLAGS_COUNT - VMBUS_FIRST_CHANID)
  172. /* Leave a few connection numbers for other purposes. */
  173. #define VMBUS_CHAN_CONNECTION_OFFSET 16
  174. /*
  175. * Since the success or failure of sending a message is reported
  176. * asynchronously, the VMBus state machine has effectively two entry points:
  177. * vmbus_run and vmbus_msg_cb (the latter is called when the host->guest
  178. * message delivery status becomes known). Both are run as oneshot BHs on the
  179. * main aio context, ensuring serialization.
  180. */
  181. enum {
  182. VMBUS_LISTEN,
  183. VMBUS_HANDSHAKE,
  184. VMBUS_OFFER,
  185. VMBUS_CREATE_GPADL,
  186. VMBUS_TEARDOWN_GPADL,
  187. VMBUS_OPEN_CHANNEL,
  188. VMBUS_UNLOAD,
  189. VMBUS_STATE_MAX
  190. };
  191. struct VMBus {
  192. BusState parent;
  193. uint8_t state;
  194. /* protection against recursive aio_poll (see vmbus_run) */
  195. bool in_progress;
  196. /* whether there's a message being delivered to the guest */
  197. bool msg_in_progress;
  198. uint32_t version;
  199. /* VP_INDEX of the vCPU to send messages and interrupts to */
  200. uint32_t target_vp;
  201. HvSintRoute *sint_route;
  202. /*
  203. * interrupt page for older protocol versions; newer ones use SynIC event
  204. * flags directly
  205. */
  206. hwaddr int_page_gpa;
  207. DECLARE_BITMAP(chanid_bitmap, VMBUS_CHANID_COUNT);
  208. /* incoming message queue */
  209. struct hyperv_post_message_input rx_queue[HV_MSG_QUEUE_LEN];
  210. uint8_t rx_queue_head;
  211. uint8_t rx_queue_size;
  212. QemuMutex rx_queue_lock;
  213. QTAILQ_HEAD(, VMBusGpadl) gpadl_list;
  214. QTAILQ_HEAD(, VMBusChannel) channel_list;
  215. /*
  216. * guest->host notifications for older VMBus, to be dispatched via
  217. * interrupt page
  218. */
  219. EventNotifier notifier;
  220. };
  221. static bool gpadl_full(VMBusGpadl *gpadl)
  222. {
  223. return gpadl->seen_gfns == gpadl->num_gfns;
  224. }
  225. static VMBusGpadl *create_gpadl(VMBus *vmbus, uint32_t id,
  226. uint32_t child_relid, uint32_t num_gfns)
  227. {
  228. VMBusGpadl *gpadl = g_new0(VMBusGpadl, 1);
  229. gpadl->id = id;
  230. gpadl->child_relid = child_relid;
  231. gpadl->num_gfns = num_gfns;
  232. gpadl->gfns = g_new(uint64_t, num_gfns);
  233. QTAILQ_INSERT_HEAD(&vmbus->gpadl_list, gpadl, link);
  234. gpadl->vmbus = vmbus;
  235. gpadl->refcount = 1;
  236. return gpadl;
  237. }
  238. static void free_gpadl(VMBusGpadl *gpadl)
  239. {
  240. QTAILQ_REMOVE(&gpadl->vmbus->gpadl_list, gpadl, link);
  241. g_free(gpadl->gfns);
  242. g_free(gpadl);
  243. }
  244. static VMBusGpadl *find_gpadl(VMBus *vmbus, uint32_t gpadl_id)
  245. {
  246. VMBusGpadl *gpadl;
  247. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  248. if (gpadl->id == gpadl_id) {
  249. return gpadl;
  250. }
  251. }
  252. return NULL;
  253. }
  254. VMBusGpadl *vmbus_get_gpadl(VMBusChannel *chan, uint32_t gpadl_id)
  255. {
  256. VMBusGpadl *gpadl = find_gpadl(chan->vmbus, gpadl_id);
  257. if (!gpadl || !gpadl_full(gpadl)) {
  258. return NULL;
  259. }
  260. gpadl->refcount++;
  261. return gpadl;
  262. }
  263. void vmbus_put_gpadl(VMBusGpadl *gpadl)
  264. {
  265. if (!gpadl) {
  266. return;
  267. }
  268. if (--gpadl->refcount) {
  269. return;
  270. }
  271. free_gpadl(gpadl);
  272. }
  273. uint32_t vmbus_gpadl_len(VMBusGpadl *gpadl)
  274. {
  275. return gpadl->num_gfns * TARGET_PAGE_SIZE;
  276. }
  277. static void gpadl_iter_init(GpadlIter *iter, VMBusGpadl *gpadl,
  278. AddressSpace *as, DMADirection dir)
  279. {
  280. iter->gpadl = gpadl;
  281. iter->as = as;
  282. iter->dir = dir;
  283. iter->active = false;
  284. }
  285. static inline void gpadl_iter_cache_unmap(GpadlIter *iter)
  286. {
  287. uint32_t map_start_in_page = (uintptr_t)iter->map & ~TARGET_PAGE_MASK;
  288. uint32_t io_end_in_page = ((iter->last_off - 1) & ~TARGET_PAGE_MASK) + 1;
  289. /* mapping is only done to do non-zero amount of i/o */
  290. assert(iter->last_off > 0);
  291. assert(map_start_in_page < io_end_in_page);
  292. dma_memory_unmap(iter->as, iter->map, TARGET_PAGE_SIZE - map_start_in_page,
  293. iter->dir, io_end_in_page - map_start_in_page);
  294. }
  295. /*
  296. * Copy exactly @len bytes between the GPADL pointed to by @iter and @buf.
  297. * The direction of the copy is determined by @iter->dir.
  298. * The caller must ensure the operation overflows neither @buf nor the GPADL
  299. * (there's an assert for the latter).
  300. * Reuse the currently mapped page in the GPADL if possible.
  301. */
  302. static ssize_t gpadl_iter_io(GpadlIter *iter, void *buf, uint32_t len)
  303. {
  304. ssize_t ret = len;
  305. assert(iter->active);
  306. while (len) {
  307. uint32_t off_in_page = iter->off & ~TARGET_PAGE_MASK;
  308. uint32_t pgleft = TARGET_PAGE_SIZE - off_in_page;
  309. uint32_t cplen = MIN(pgleft, len);
  310. void *p;
  311. /* try to reuse the cached mapping */
  312. if (iter->map) {
  313. uint32_t map_start_in_page =
  314. (uintptr_t)iter->map & ~TARGET_PAGE_MASK;
  315. uint32_t off_base = iter->off & ~TARGET_PAGE_MASK;
  316. uint32_t mapped_base = (iter->last_off - 1) & ~TARGET_PAGE_MASK;
  317. if (off_base != mapped_base || off_in_page < map_start_in_page) {
  318. gpadl_iter_cache_unmap(iter);
  319. iter->map = NULL;
  320. }
  321. }
  322. if (!iter->map) {
  323. dma_addr_t maddr;
  324. dma_addr_t mlen = pgleft;
  325. uint32_t idx = iter->off >> TARGET_PAGE_BITS;
  326. assert(idx < iter->gpadl->num_gfns);
  327. maddr = (iter->gpadl->gfns[idx] << TARGET_PAGE_BITS) | off_in_page;
  328. iter->map = dma_memory_map(iter->as, maddr, &mlen, iter->dir);
  329. if (mlen != pgleft) {
  330. dma_memory_unmap(iter->as, iter->map, mlen, iter->dir, 0);
  331. iter->map = NULL;
  332. return -EFAULT;
  333. }
  334. }
  335. p = (void *)(((uintptr_t)iter->map & TARGET_PAGE_MASK) | off_in_page);
  336. if (iter->dir == DMA_DIRECTION_FROM_DEVICE) {
  337. memcpy(p, buf, cplen);
  338. } else {
  339. memcpy(buf, p, cplen);
  340. }
  341. buf += cplen;
  342. len -= cplen;
  343. iter->off += cplen;
  344. iter->last_off = iter->off;
  345. }
  346. return ret;
  347. }
  348. /*
  349. * Position the iterator @iter at new offset @new_off.
  350. * If this results in the cached mapping being unusable with the new offset,
  351. * unmap it.
  352. */
  353. static inline void gpadl_iter_seek(GpadlIter *iter, uint32_t new_off)
  354. {
  355. assert(iter->active);
  356. iter->off = new_off;
  357. }
  358. /*
  359. * Start a series of i/o on the GPADL.
  360. * After this i/o and seek operations on @iter become legal.
  361. */
  362. static inline void gpadl_iter_start_io(GpadlIter *iter)
  363. {
  364. assert(!iter->active);
  365. /* mapping is cached lazily on i/o */
  366. iter->map = NULL;
  367. iter->active = true;
  368. }
  369. /*
  370. * End the eariler started series of i/o on the GPADL and release the cached
  371. * mapping if any.
  372. */
  373. static inline void gpadl_iter_end_io(GpadlIter *iter)
  374. {
  375. assert(iter->active);
  376. if (iter->map) {
  377. gpadl_iter_cache_unmap(iter);
  378. }
  379. iter->active = false;
  380. }
  381. static void vmbus_resched(VMBus *vmbus);
  382. static void vmbus_msg_cb(void *data, int status);
  383. ssize_t vmbus_iov_to_gpadl(VMBusChannel *chan, VMBusGpadl *gpadl, uint32_t off,
  384. const struct iovec *iov, size_t iov_cnt)
  385. {
  386. GpadlIter iter;
  387. size_t i;
  388. ssize_t ret = 0;
  389. gpadl_iter_init(&iter, gpadl, chan->dev->dma_as,
  390. DMA_DIRECTION_FROM_DEVICE);
  391. gpadl_iter_start_io(&iter);
  392. gpadl_iter_seek(&iter, off);
  393. for (i = 0; i < iov_cnt; i++) {
  394. ret = gpadl_iter_io(&iter, iov[i].iov_base, iov[i].iov_len);
  395. if (ret < 0) {
  396. goto out;
  397. }
  398. }
  399. out:
  400. gpadl_iter_end_io(&iter);
  401. return ret;
  402. }
  403. int vmbus_map_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov,
  404. unsigned iov_cnt, size_t len, size_t off)
  405. {
  406. int ret_cnt = 0, ret;
  407. unsigned i;
  408. QEMUSGList *sgl = &req->sgl;
  409. ScatterGatherEntry *sg = sgl->sg;
  410. for (i = 0; i < sgl->nsg; i++) {
  411. if (sg[i].len > off) {
  412. break;
  413. }
  414. off -= sg[i].len;
  415. }
  416. for (; len && i < sgl->nsg; i++) {
  417. dma_addr_t mlen = MIN(sg[i].len - off, len);
  418. dma_addr_t addr = sg[i].base + off;
  419. len -= mlen;
  420. off = 0;
  421. for (; mlen; ret_cnt++) {
  422. dma_addr_t l = mlen;
  423. dma_addr_t a = addr;
  424. if (ret_cnt == iov_cnt) {
  425. ret = -ENOBUFS;
  426. goto err;
  427. }
  428. iov[ret_cnt].iov_base = dma_memory_map(sgl->as, a, &l, dir);
  429. if (!l) {
  430. ret = -EFAULT;
  431. goto err;
  432. }
  433. iov[ret_cnt].iov_len = l;
  434. addr += l;
  435. mlen -= l;
  436. }
  437. }
  438. return ret_cnt;
  439. err:
  440. vmbus_unmap_sgl(req, dir, iov, ret_cnt, 0);
  441. return ret;
  442. }
  443. void vmbus_unmap_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov,
  444. unsigned iov_cnt, size_t accessed)
  445. {
  446. QEMUSGList *sgl = &req->sgl;
  447. unsigned i;
  448. for (i = 0; i < iov_cnt; i++) {
  449. size_t acsd = MIN(accessed, iov[i].iov_len);
  450. dma_memory_unmap(sgl->as, iov[i].iov_base, iov[i].iov_len, dir, acsd);
  451. accessed -= acsd;
  452. }
  453. }
  454. static const VMStateDescription vmstate_gpadl = {
  455. .name = "vmbus/gpadl",
  456. .version_id = 0,
  457. .minimum_version_id = 0,
  458. .fields = (VMStateField[]) {
  459. VMSTATE_UINT32(id, VMBusGpadl),
  460. VMSTATE_UINT32(child_relid, VMBusGpadl),
  461. VMSTATE_UINT32(num_gfns, VMBusGpadl),
  462. VMSTATE_UINT32(seen_gfns, VMBusGpadl),
  463. VMSTATE_VARRAY_UINT32_ALLOC(gfns, VMBusGpadl, num_gfns, 0,
  464. vmstate_info_uint64, uint64_t),
  465. VMSTATE_UINT8(state, VMBusGpadl),
  466. VMSTATE_END_OF_LIST()
  467. }
  468. };
  469. /*
  470. * Wrap the index into a ring buffer of @len bytes.
  471. * @idx is assumed not to exceed twice the size of the ringbuffer, so only
  472. * single wraparound is considered.
  473. */
  474. static inline uint32_t rb_idx_wrap(uint32_t idx, uint32_t len)
  475. {
  476. if (idx >= len) {
  477. idx -= len;
  478. }
  479. return idx;
  480. }
  481. /*
  482. * Circular difference between two indices into a ring buffer of @len bytes.
  483. * @allow_catchup - whether @idx1 may catch up @idx2; e.g. read index may catch
  484. * up write index but not vice versa.
  485. */
  486. static inline uint32_t rb_idx_delta(uint32_t idx1, uint32_t idx2, uint32_t len,
  487. bool allow_catchup)
  488. {
  489. return rb_idx_wrap(idx2 + len - idx1 - !allow_catchup, len);
  490. }
  491. static vmbus_ring_buffer *ringbuf_map_hdr(VMBusRingBufCommon *ringbuf)
  492. {
  493. vmbus_ring_buffer *rb;
  494. dma_addr_t mlen = sizeof(*rb);
  495. rb = dma_memory_map(ringbuf->as, ringbuf->rb_addr, &mlen,
  496. DMA_DIRECTION_FROM_DEVICE);
  497. if (mlen != sizeof(*rb)) {
  498. dma_memory_unmap(ringbuf->as, rb, mlen,
  499. DMA_DIRECTION_FROM_DEVICE, 0);
  500. return NULL;
  501. }
  502. return rb;
  503. }
  504. static void ringbuf_unmap_hdr(VMBusRingBufCommon *ringbuf,
  505. vmbus_ring_buffer *rb, bool dirty)
  506. {
  507. assert(rb);
  508. dma_memory_unmap(ringbuf->as, rb, sizeof(*rb), DMA_DIRECTION_FROM_DEVICE,
  509. dirty ? sizeof(*rb) : 0);
  510. }
  511. static void ringbuf_init_common(VMBusRingBufCommon *ringbuf, VMBusGpadl *gpadl,
  512. AddressSpace *as, DMADirection dir,
  513. uint32_t begin, uint32_t end)
  514. {
  515. ringbuf->as = as;
  516. ringbuf->rb_addr = gpadl->gfns[begin] << TARGET_PAGE_BITS;
  517. ringbuf->base = (begin + 1) << TARGET_PAGE_BITS;
  518. ringbuf->len = (end - begin - 1) << TARGET_PAGE_BITS;
  519. gpadl_iter_init(&ringbuf->iter, gpadl, as, dir);
  520. }
  521. static int ringbufs_init(VMBusChannel *chan)
  522. {
  523. vmbus_ring_buffer *rb;
  524. VMBusSendRingBuf *send_ringbuf = &chan->send_ringbuf;
  525. VMBusRecvRingBuf *recv_ringbuf = &chan->recv_ringbuf;
  526. if (chan->ringbuf_send_offset <= 1 ||
  527. chan->gpadl->num_gfns <= chan->ringbuf_send_offset + 1) {
  528. return -EINVAL;
  529. }
  530. ringbuf_init_common(&recv_ringbuf->common, chan->gpadl, chan->dev->dma_as,
  531. DMA_DIRECTION_TO_DEVICE, 0, chan->ringbuf_send_offset);
  532. ringbuf_init_common(&send_ringbuf->common, chan->gpadl, chan->dev->dma_as,
  533. DMA_DIRECTION_FROM_DEVICE, chan->ringbuf_send_offset,
  534. chan->gpadl->num_gfns);
  535. send_ringbuf->wanted = 0;
  536. send_ringbuf->reserved = 0;
  537. rb = ringbuf_map_hdr(&recv_ringbuf->common);
  538. if (!rb) {
  539. return -EFAULT;
  540. }
  541. recv_ringbuf->rd_idx = recv_ringbuf->last_rd_idx = rb->read_index;
  542. ringbuf_unmap_hdr(&recv_ringbuf->common, rb, false);
  543. rb = ringbuf_map_hdr(&send_ringbuf->common);
  544. if (!rb) {
  545. return -EFAULT;
  546. }
  547. send_ringbuf->wr_idx = send_ringbuf->last_wr_idx = rb->write_index;
  548. send_ringbuf->last_seen_rd_idx = rb->read_index;
  549. rb->feature_bits |= VMBUS_RING_BUFFER_FEAT_PENDING_SZ;
  550. ringbuf_unmap_hdr(&send_ringbuf->common, rb, true);
  551. if (recv_ringbuf->rd_idx >= recv_ringbuf->common.len ||
  552. send_ringbuf->wr_idx >= send_ringbuf->common.len) {
  553. return -EOVERFLOW;
  554. }
  555. return 0;
  556. }
  557. /*
  558. * Perform io between the GPADL-backed ringbuffer @ringbuf and @buf, wrapping
  559. * around if needed.
  560. * @len is assumed not to exceed the size of the ringbuffer, so only single
  561. * wraparound is considered.
  562. */
  563. static ssize_t ringbuf_io(VMBusRingBufCommon *ringbuf, void *buf, uint32_t len)
  564. {
  565. ssize_t ret1 = 0, ret2 = 0;
  566. uint32_t remain = ringbuf->len + ringbuf->base - ringbuf->iter.off;
  567. if (len >= remain) {
  568. ret1 = gpadl_iter_io(&ringbuf->iter, buf, remain);
  569. if (ret1 < 0) {
  570. return ret1;
  571. }
  572. gpadl_iter_seek(&ringbuf->iter, ringbuf->base);
  573. buf += remain;
  574. len -= remain;
  575. }
  576. ret2 = gpadl_iter_io(&ringbuf->iter, buf, len);
  577. if (ret2 < 0) {
  578. return ret2;
  579. }
  580. return ret1 + ret2;
  581. }
  582. /*
  583. * Position the circular iterator within @ringbuf to offset @new_off, wrapping
  584. * around if needed.
  585. * @new_off is assumed not to exceed twice the size of the ringbuffer, so only
  586. * single wraparound is considered.
  587. */
  588. static inline void ringbuf_seek(VMBusRingBufCommon *ringbuf, uint32_t new_off)
  589. {
  590. gpadl_iter_seek(&ringbuf->iter,
  591. ringbuf->base + rb_idx_wrap(new_off, ringbuf->len));
  592. }
  593. static inline uint32_t ringbuf_tell(VMBusRingBufCommon *ringbuf)
  594. {
  595. return ringbuf->iter.off - ringbuf->base;
  596. }
  597. static inline void ringbuf_start_io(VMBusRingBufCommon *ringbuf)
  598. {
  599. gpadl_iter_start_io(&ringbuf->iter);
  600. }
  601. static inline void ringbuf_end_io(VMBusRingBufCommon *ringbuf)
  602. {
  603. gpadl_iter_end_io(&ringbuf->iter);
  604. }
  605. VMBusDevice *vmbus_channel_device(VMBusChannel *chan)
  606. {
  607. return chan->dev;
  608. }
  609. VMBusChannel *vmbus_device_channel(VMBusDevice *dev, uint32_t chan_idx)
  610. {
  611. if (chan_idx >= dev->num_channels) {
  612. return NULL;
  613. }
  614. return &dev->channels[chan_idx];
  615. }
  616. uint32_t vmbus_channel_idx(VMBusChannel *chan)
  617. {
  618. return chan - chan->dev->channels;
  619. }
  620. void vmbus_channel_notify_host(VMBusChannel *chan)
  621. {
  622. event_notifier_set(&chan->notifier);
  623. }
  624. bool vmbus_channel_is_open(VMBusChannel *chan)
  625. {
  626. return chan->is_open;
  627. }
  628. /*
  629. * Notify the guest side about the data to work on in the channel ring buffer.
  630. * The notification is done by signaling a dedicated per-channel SynIC event
  631. * flag (more recent guests) or setting a bit in the interrupt page and firing
  632. * the VMBus SINT (older guests).
  633. */
  634. static int vmbus_channel_notify_guest(VMBusChannel *chan)
  635. {
  636. int res = 0;
  637. unsigned long *int_map, mask;
  638. unsigned idx;
  639. hwaddr addr = chan->vmbus->int_page_gpa;
  640. hwaddr len = TARGET_PAGE_SIZE / 2, dirty = 0;
  641. trace_vmbus_channel_notify_guest(chan->id);
  642. if (!addr) {
  643. return hyperv_set_event_flag(chan->notify_route, chan->id);
  644. }
  645. int_map = cpu_physical_memory_map(addr, &len, 1);
  646. if (len != TARGET_PAGE_SIZE / 2) {
  647. res = -ENXIO;
  648. goto unmap;
  649. }
  650. idx = BIT_WORD(chan->id);
  651. mask = BIT_MASK(chan->id);
  652. if ((atomic_fetch_or(&int_map[idx], mask) & mask) != mask) {
  653. res = hyperv_sint_route_set_sint(chan->notify_route);
  654. dirty = len;
  655. }
  656. unmap:
  657. cpu_physical_memory_unmap(int_map, len, 1, dirty);
  658. return res;
  659. }
  660. #define VMBUS_PKT_TRAILER sizeof(uint64_t)
  661. static uint32_t vmbus_pkt_hdr_set_offsets(vmbus_packet_hdr *hdr,
  662. uint32_t desclen, uint32_t msglen)
  663. {
  664. hdr->offset_qwords = sizeof(*hdr) / sizeof(uint64_t) +
  665. DIV_ROUND_UP(desclen, sizeof(uint64_t));
  666. hdr->len_qwords = hdr->offset_qwords +
  667. DIV_ROUND_UP(msglen, sizeof(uint64_t));
  668. return hdr->len_qwords * sizeof(uint64_t) + VMBUS_PKT_TRAILER;
  669. }
  670. /*
  671. * Simplified ring buffer operation with paired barriers annotations in the
  672. * producer and consumer loops:
  673. *
  674. * producer * consumer
  675. * ~~~~~~~~ * ~~~~~~~~
  676. * write pending_send_sz * read write_index
  677. * smp_mb [A] * smp_mb [C]
  678. * read read_index * read packet
  679. * smp_mb [B] * read/write out-of-band data
  680. * read/write out-of-band data * smp_mb [B]
  681. * write packet * write read_index
  682. * smp_mb [C] * smp_mb [A]
  683. * write write_index * read pending_send_sz
  684. * smp_wmb [D] * smp_rmb [D]
  685. * write pending_send_sz * read write_index
  686. * ... * ...
  687. */
  688. static inline uint32_t ringbuf_send_avail(VMBusSendRingBuf *ringbuf)
  689. {
  690. /* don't trust guest data */
  691. if (ringbuf->last_seen_rd_idx >= ringbuf->common.len) {
  692. return 0;
  693. }
  694. return rb_idx_delta(ringbuf->wr_idx, ringbuf->last_seen_rd_idx,
  695. ringbuf->common.len, false);
  696. }
  697. static ssize_t ringbuf_send_update_idx(VMBusChannel *chan)
  698. {
  699. VMBusSendRingBuf *ringbuf = &chan->send_ringbuf;
  700. vmbus_ring_buffer *rb;
  701. uint32_t written;
  702. written = rb_idx_delta(ringbuf->last_wr_idx, ringbuf->wr_idx,
  703. ringbuf->common.len, true);
  704. if (!written) {
  705. return 0;
  706. }
  707. rb = ringbuf_map_hdr(&ringbuf->common);
  708. if (!rb) {
  709. return -EFAULT;
  710. }
  711. ringbuf->reserved -= written;
  712. /* prevent reorder with the data operation and packet write */
  713. smp_mb(); /* barrier pair [C] */
  714. rb->write_index = ringbuf->wr_idx;
  715. /*
  716. * If the producer earlier indicated that it wants to be notified when the
  717. * consumer frees certain amount of space in the ring buffer, that amount
  718. * is reduced by the size of the completed write.
  719. */
  720. if (ringbuf->wanted) {
  721. /* otherwise reservation would fail */
  722. assert(ringbuf->wanted < written);
  723. ringbuf->wanted -= written;
  724. /* prevent reorder with write_index write */
  725. smp_wmb(); /* barrier pair [D] */
  726. rb->pending_send_sz = ringbuf->wanted;
  727. }
  728. /* prevent reorder with write_index or pending_send_sz write */
  729. smp_mb(); /* barrier pair [A] */
  730. ringbuf->last_seen_rd_idx = rb->read_index;
  731. /*
  732. * The consumer may have missed the reduction of pending_send_sz and skip
  733. * notification, so re-check the blocking condition, and, if it's no longer
  734. * true, ensure processing another iteration by simulating consumer's
  735. * notification.
  736. */
  737. if (ringbuf_send_avail(ringbuf) >= ringbuf->wanted) {
  738. vmbus_channel_notify_host(chan);
  739. }
  740. /* skip notification by consumer's request */
  741. if (rb->interrupt_mask) {
  742. goto out;
  743. }
  744. /*
  745. * The consumer hasn't caught up with the producer's previous state so it's
  746. * not blocked.
  747. * (last_seen_rd_idx comes from the guest but it's safe to use w/o
  748. * validation here as it only affects notification.)
  749. */
  750. if (rb_idx_delta(ringbuf->last_seen_rd_idx, ringbuf->wr_idx,
  751. ringbuf->common.len, true) > written) {
  752. goto out;
  753. }
  754. vmbus_channel_notify_guest(chan);
  755. out:
  756. ringbuf_unmap_hdr(&ringbuf->common, rb, true);
  757. ringbuf->last_wr_idx = ringbuf->wr_idx;
  758. return written;
  759. }
  760. int vmbus_channel_reserve(VMBusChannel *chan,
  761. uint32_t desclen, uint32_t msglen)
  762. {
  763. VMBusSendRingBuf *ringbuf = &chan->send_ringbuf;
  764. vmbus_ring_buffer *rb = NULL;
  765. vmbus_packet_hdr hdr;
  766. uint32_t needed = ringbuf->reserved +
  767. vmbus_pkt_hdr_set_offsets(&hdr, desclen, msglen);
  768. /* avoid touching the guest memory if possible */
  769. if (likely(needed <= ringbuf_send_avail(ringbuf))) {
  770. goto success;
  771. }
  772. rb = ringbuf_map_hdr(&ringbuf->common);
  773. if (!rb) {
  774. return -EFAULT;
  775. }
  776. /* fetch read index from guest memory and try again */
  777. ringbuf->last_seen_rd_idx = rb->read_index;
  778. if (likely(needed <= ringbuf_send_avail(ringbuf))) {
  779. goto success;
  780. }
  781. rb->pending_send_sz = needed;
  782. /*
  783. * The consumer may have made progress and freed up some space before
  784. * seeing updated pending_send_sz, so re-read read_index (preventing
  785. * reorder with the pending_send_sz write) and try again.
  786. */
  787. smp_mb(); /* barrier pair [A] */
  788. ringbuf->last_seen_rd_idx = rb->read_index;
  789. if (needed > ringbuf_send_avail(ringbuf)) {
  790. goto out;
  791. }
  792. success:
  793. ringbuf->reserved = needed;
  794. needed = 0;
  795. /* clear pending_send_sz if it was set */
  796. if (ringbuf->wanted) {
  797. if (!rb) {
  798. rb = ringbuf_map_hdr(&ringbuf->common);
  799. if (!rb) {
  800. /* failure to clear pending_send_sz is non-fatal */
  801. goto out;
  802. }
  803. }
  804. rb->pending_send_sz = 0;
  805. }
  806. /* prevent reorder of the following data operation with read_index read */
  807. smp_mb(); /* barrier pair [B] */
  808. out:
  809. if (rb) {
  810. ringbuf_unmap_hdr(&ringbuf->common, rb, ringbuf->wanted == needed);
  811. }
  812. ringbuf->wanted = needed;
  813. return needed ? -ENOSPC : 0;
  814. }
  815. ssize_t vmbus_channel_send(VMBusChannel *chan, uint16_t pkt_type,
  816. void *desc, uint32_t desclen,
  817. void *msg, uint32_t msglen,
  818. bool need_comp, uint64_t transaction_id)
  819. {
  820. ssize_t ret = 0;
  821. vmbus_packet_hdr hdr;
  822. uint32_t totlen;
  823. VMBusSendRingBuf *ringbuf = &chan->send_ringbuf;
  824. if (!vmbus_channel_is_open(chan)) {
  825. return -EINVAL;
  826. }
  827. totlen = vmbus_pkt_hdr_set_offsets(&hdr, desclen, msglen);
  828. hdr.type = pkt_type;
  829. hdr.flags = need_comp ? VMBUS_PACKET_FLAG_REQUEST_COMPLETION : 0;
  830. hdr.transaction_id = transaction_id;
  831. assert(totlen <= ringbuf->reserved);
  832. ringbuf_start_io(&ringbuf->common);
  833. ringbuf_seek(&ringbuf->common, ringbuf->wr_idx);
  834. ret = ringbuf_io(&ringbuf->common, &hdr, sizeof(hdr));
  835. if (ret < 0) {
  836. goto out;
  837. }
  838. if (desclen) {
  839. assert(desc);
  840. ret = ringbuf_io(&ringbuf->common, desc, desclen);
  841. if (ret < 0) {
  842. goto out;
  843. }
  844. ringbuf_seek(&ringbuf->common,
  845. ringbuf->wr_idx + hdr.offset_qwords * sizeof(uint64_t));
  846. }
  847. ret = ringbuf_io(&ringbuf->common, msg, msglen);
  848. if (ret < 0) {
  849. goto out;
  850. }
  851. ringbuf_seek(&ringbuf->common, ringbuf->wr_idx + totlen);
  852. ringbuf->wr_idx = ringbuf_tell(&ringbuf->common);
  853. ret = 0;
  854. out:
  855. ringbuf_end_io(&ringbuf->common);
  856. if (ret) {
  857. return ret;
  858. }
  859. return ringbuf_send_update_idx(chan);
  860. }
  861. ssize_t vmbus_channel_send_completion(VMBusChanReq *req,
  862. void *msg, uint32_t msglen)
  863. {
  864. assert(req->need_comp);
  865. return vmbus_channel_send(req->chan, VMBUS_PACKET_COMP, NULL, 0,
  866. msg, msglen, false, req->transaction_id);
  867. }
  868. static int sgl_from_gpa_ranges(QEMUSGList *sgl, VMBusDevice *dev,
  869. VMBusRingBufCommon *ringbuf, uint32_t len)
  870. {
  871. int ret;
  872. vmbus_pkt_gpa_direct hdr;
  873. hwaddr curaddr = 0;
  874. hwaddr curlen = 0;
  875. int num;
  876. if (len < sizeof(hdr)) {
  877. return -EIO;
  878. }
  879. ret = ringbuf_io(ringbuf, &hdr, sizeof(hdr));
  880. if (ret < 0) {
  881. return ret;
  882. }
  883. len -= sizeof(hdr);
  884. num = (len - hdr.rangecount * sizeof(vmbus_gpa_range)) / sizeof(uint64_t);
  885. if (num < 0) {
  886. return -EIO;
  887. }
  888. qemu_sglist_init(sgl, DEVICE(dev), num, ringbuf->as);
  889. for (; hdr.rangecount; hdr.rangecount--) {
  890. vmbus_gpa_range range;
  891. if (len < sizeof(range)) {
  892. goto eio;
  893. }
  894. ret = ringbuf_io(ringbuf, &range, sizeof(range));
  895. if (ret < 0) {
  896. goto err;
  897. }
  898. len -= sizeof(range);
  899. if (range.byte_offset & TARGET_PAGE_MASK) {
  900. goto eio;
  901. }
  902. for (; range.byte_count; range.byte_offset = 0) {
  903. uint64_t paddr;
  904. uint32_t plen = MIN(range.byte_count,
  905. TARGET_PAGE_SIZE - range.byte_offset);
  906. if (len < sizeof(uint64_t)) {
  907. goto eio;
  908. }
  909. ret = ringbuf_io(ringbuf, &paddr, sizeof(paddr));
  910. if (ret < 0) {
  911. goto err;
  912. }
  913. len -= sizeof(uint64_t);
  914. paddr <<= TARGET_PAGE_BITS;
  915. paddr |= range.byte_offset;
  916. range.byte_count -= plen;
  917. if (curaddr + curlen == paddr) {
  918. /* consecutive fragments - join */
  919. curlen += plen;
  920. } else {
  921. if (curlen) {
  922. qemu_sglist_add(sgl, curaddr, curlen);
  923. }
  924. curaddr = paddr;
  925. curlen = plen;
  926. }
  927. }
  928. }
  929. if (curlen) {
  930. qemu_sglist_add(sgl, curaddr, curlen);
  931. }
  932. return 0;
  933. eio:
  934. ret = -EIO;
  935. err:
  936. qemu_sglist_destroy(sgl);
  937. return ret;
  938. }
  939. static VMBusChanReq *vmbus_alloc_req(VMBusChannel *chan,
  940. uint32_t size, uint16_t pkt_type,
  941. uint32_t msglen, uint64_t transaction_id,
  942. bool need_comp)
  943. {
  944. VMBusChanReq *req;
  945. uint32_t msgoff = QEMU_ALIGN_UP(size, __alignof__(*req->msg));
  946. uint32_t totlen = msgoff + msglen;
  947. req = g_malloc0(totlen);
  948. req->chan = chan;
  949. req->pkt_type = pkt_type;
  950. req->msg = (void *)req + msgoff;
  951. req->msglen = msglen;
  952. req->transaction_id = transaction_id;
  953. req->need_comp = need_comp;
  954. return req;
  955. }
  956. int vmbus_channel_recv_start(VMBusChannel *chan)
  957. {
  958. VMBusRecvRingBuf *ringbuf = &chan->recv_ringbuf;
  959. vmbus_ring_buffer *rb;
  960. rb = ringbuf_map_hdr(&ringbuf->common);
  961. if (!rb) {
  962. return -EFAULT;
  963. }
  964. ringbuf->last_seen_wr_idx = rb->write_index;
  965. ringbuf_unmap_hdr(&ringbuf->common, rb, false);
  966. if (ringbuf->last_seen_wr_idx >= ringbuf->common.len) {
  967. return -EOVERFLOW;
  968. }
  969. /* prevent reorder of the following data operation with write_index read */
  970. smp_mb(); /* barrier pair [C] */
  971. return 0;
  972. }
  973. void *vmbus_channel_recv_peek(VMBusChannel *chan, uint32_t size)
  974. {
  975. VMBusRecvRingBuf *ringbuf = &chan->recv_ringbuf;
  976. vmbus_packet_hdr hdr = {};
  977. VMBusChanReq *req;
  978. uint32_t avail;
  979. uint32_t totlen, pktlen, msglen, msgoff, desclen;
  980. assert(size >= sizeof(*req));
  981. /* safe as last_seen_wr_idx is validated in vmbus_channel_recv_start */
  982. avail = rb_idx_delta(ringbuf->rd_idx, ringbuf->last_seen_wr_idx,
  983. ringbuf->common.len, true);
  984. if (avail < sizeof(hdr)) {
  985. return NULL;
  986. }
  987. ringbuf_seek(&ringbuf->common, ringbuf->rd_idx);
  988. if (ringbuf_io(&ringbuf->common, &hdr, sizeof(hdr)) < 0) {
  989. return NULL;
  990. }
  991. pktlen = hdr.len_qwords * sizeof(uint64_t);
  992. totlen = pktlen + VMBUS_PKT_TRAILER;
  993. if (totlen > avail) {
  994. return NULL;
  995. }
  996. msgoff = hdr.offset_qwords * sizeof(uint64_t);
  997. if (msgoff > pktlen || msgoff < sizeof(hdr)) {
  998. error_report("%s: malformed packet: %u %u", __func__, msgoff, pktlen);
  999. return NULL;
  1000. }
  1001. msglen = pktlen - msgoff;
  1002. req = vmbus_alloc_req(chan, size, hdr.type, msglen, hdr.transaction_id,
  1003. hdr.flags & VMBUS_PACKET_FLAG_REQUEST_COMPLETION);
  1004. switch (hdr.type) {
  1005. case VMBUS_PACKET_DATA_USING_GPA_DIRECT:
  1006. desclen = msgoff - sizeof(hdr);
  1007. if (sgl_from_gpa_ranges(&req->sgl, chan->dev, &ringbuf->common,
  1008. desclen) < 0) {
  1009. error_report("%s: failed to convert GPA ranges to SGL", __func__);
  1010. goto free_req;
  1011. }
  1012. break;
  1013. case VMBUS_PACKET_DATA_INBAND:
  1014. case VMBUS_PACKET_COMP:
  1015. break;
  1016. default:
  1017. error_report("%s: unexpected msg type: %x", __func__, hdr.type);
  1018. goto free_req;
  1019. }
  1020. ringbuf_seek(&ringbuf->common, ringbuf->rd_idx + msgoff);
  1021. if (ringbuf_io(&ringbuf->common, req->msg, msglen) < 0) {
  1022. goto free_req;
  1023. }
  1024. ringbuf_seek(&ringbuf->common, ringbuf->rd_idx + totlen);
  1025. return req;
  1026. free_req:
  1027. vmbus_free_req(req);
  1028. return NULL;
  1029. }
  1030. void vmbus_channel_recv_pop(VMBusChannel *chan)
  1031. {
  1032. VMBusRecvRingBuf *ringbuf = &chan->recv_ringbuf;
  1033. ringbuf->rd_idx = ringbuf_tell(&ringbuf->common);
  1034. }
  1035. ssize_t vmbus_channel_recv_done(VMBusChannel *chan)
  1036. {
  1037. VMBusRecvRingBuf *ringbuf = &chan->recv_ringbuf;
  1038. vmbus_ring_buffer *rb;
  1039. uint32_t read;
  1040. read = rb_idx_delta(ringbuf->last_rd_idx, ringbuf->rd_idx,
  1041. ringbuf->common.len, true);
  1042. if (!read) {
  1043. return 0;
  1044. }
  1045. rb = ringbuf_map_hdr(&ringbuf->common);
  1046. if (!rb) {
  1047. return -EFAULT;
  1048. }
  1049. /* prevent reorder with the data operation and packet read */
  1050. smp_mb(); /* barrier pair [B] */
  1051. rb->read_index = ringbuf->rd_idx;
  1052. /* prevent reorder of the following pending_send_sz read */
  1053. smp_mb(); /* barrier pair [A] */
  1054. if (rb->interrupt_mask) {
  1055. goto out;
  1056. }
  1057. if (rb->feature_bits & VMBUS_RING_BUFFER_FEAT_PENDING_SZ) {
  1058. uint32_t wr_idx, wr_avail;
  1059. uint32_t wanted = rb->pending_send_sz;
  1060. if (!wanted) {
  1061. goto out;
  1062. }
  1063. /* prevent reorder with pending_send_sz read */
  1064. smp_rmb(); /* barrier pair [D] */
  1065. wr_idx = rb->write_index;
  1066. wr_avail = rb_idx_delta(wr_idx, ringbuf->rd_idx, ringbuf->common.len,
  1067. true);
  1068. /* the producer wasn't blocked on the consumer state */
  1069. if (wr_avail >= read + wanted) {
  1070. goto out;
  1071. }
  1072. /* there's not enough space for the producer to make progress */
  1073. if (wr_avail < wanted) {
  1074. goto out;
  1075. }
  1076. }
  1077. vmbus_channel_notify_guest(chan);
  1078. out:
  1079. ringbuf_unmap_hdr(&ringbuf->common, rb, true);
  1080. ringbuf->last_rd_idx = ringbuf->rd_idx;
  1081. return read;
  1082. }
  1083. void vmbus_free_req(void *req)
  1084. {
  1085. VMBusChanReq *r = req;
  1086. if (!req) {
  1087. return;
  1088. }
  1089. if (r->sgl.dev) {
  1090. qemu_sglist_destroy(&r->sgl);
  1091. }
  1092. g_free(req);
  1093. }
  1094. static const VMStateDescription vmstate_sgent = {
  1095. .name = "vmbus/sgentry",
  1096. .version_id = 0,
  1097. .minimum_version_id = 0,
  1098. .fields = (VMStateField[]) {
  1099. VMSTATE_UINT64(base, ScatterGatherEntry),
  1100. VMSTATE_UINT64(len, ScatterGatherEntry),
  1101. VMSTATE_END_OF_LIST()
  1102. }
  1103. };
  1104. typedef struct VMBusChanReqSave {
  1105. uint16_t chan_idx;
  1106. uint16_t pkt_type;
  1107. uint32_t msglen;
  1108. void *msg;
  1109. uint64_t transaction_id;
  1110. bool need_comp;
  1111. uint32_t num;
  1112. ScatterGatherEntry *sgl;
  1113. } VMBusChanReqSave;
  1114. static const VMStateDescription vmstate_vmbus_chan_req = {
  1115. .name = "vmbus/vmbus_chan_req",
  1116. .version_id = 0,
  1117. .minimum_version_id = 0,
  1118. .fields = (VMStateField[]) {
  1119. VMSTATE_UINT16(chan_idx, VMBusChanReqSave),
  1120. VMSTATE_UINT16(pkt_type, VMBusChanReqSave),
  1121. VMSTATE_UINT32(msglen, VMBusChanReqSave),
  1122. VMSTATE_VBUFFER_ALLOC_UINT32(msg, VMBusChanReqSave, 0, NULL, msglen),
  1123. VMSTATE_UINT64(transaction_id, VMBusChanReqSave),
  1124. VMSTATE_BOOL(need_comp, VMBusChanReqSave),
  1125. VMSTATE_UINT32(num, VMBusChanReqSave),
  1126. VMSTATE_STRUCT_VARRAY_POINTER_UINT32(sgl, VMBusChanReqSave, num,
  1127. vmstate_sgent, ScatterGatherEntry),
  1128. VMSTATE_END_OF_LIST()
  1129. }
  1130. };
  1131. void vmbus_save_req(QEMUFile *f, VMBusChanReq *req)
  1132. {
  1133. VMBusChanReqSave req_save;
  1134. req_save.chan_idx = req->chan->subchan_idx;
  1135. req_save.pkt_type = req->pkt_type;
  1136. req_save.msglen = req->msglen;
  1137. req_save.msg = req->msg;
  1138. req_save.transaction_id = req->transaction_id;
  1139. req_save.need_comp = req->need_comp;
  1140. req_save.num = req->sgl.nsg;
  1141. req_save.sgl = g_memdup(req->sgl.sg,
  1142. req_save.num * sizeof(ScatterGatherEntry));
  1143. vmstate_save_state(f, &vmstate_vmbus_chan_req, &req_save, NULL);
  1144. g_free(req_save.sgl);
  1145. }
  1146. void *vmbus_load_req(QEMUFile *f, VMBusDevice *dev, uint32_t size)
  1147. {
  1148. VMBusChanReqSave req_save;
  1149. VMBusChanReq *req = NULL;
  1150. VMBusChannel *chan = NULL;
  1151. uint32_t i;
  1152. vmstate_load_state(f, &vmstate_vmbus_chan_req, &req_save, 0);
  1153. if (req_save.chan_idx >= dev->num_channels) {
  1154. error_report("%s: %u(chan_idx) > %u(num_channels)", __func__,
  1155. req_save.chan_idx, dev->num_channels);
  1156. goto out;
  1157. }
  1158. chan = &dev->channels[req_save.chan_idx];
  1159. if (vmbus_channel_reserve(chan, 0, req_save.msglen)) {
  1160. goto out;
  1161. }
  1162. req = vmbus_alloc_req(chan, size, req_save.pkt_type, req_save.msglen,
  1163. req_save.transaction_id, req_save.need_comp);
  1164. if (req_save.msglen) {
  1165. memcpy(req->msg, req_save.msg, req_save.msglen);
  1166. }
  1167. for (i = 0; i < req_save.num; i++) {
  1168. qemu_sglist_add(&req->sgl, req_save.sgl[i].base, req_save.sgl[i].len);
  1169. }
  1170. out:
  1171. if (req_save.msglen) {
  1172. g_free(req_save.msg);
  1173. }
  1174. if (req_save.num) {
  1175. g_free(req_save.sgl);
  1176. }
  1177. return req;
  1178. }
  1179. static void channel_event_cb(EventNotifier *e)
  1180. {
  1181. VMBusChannel *chan = container_of(e, VMBusChannel, notifier);
  1182. if (event_notifier_test_and_clear(e)) {
  1183. /*
  1184. * All receives are supposed to happen within the device worker, so
  1185. * bracket it with ringbuf_start/end_io on the receive ringbuffer, and
  1186. * potentially reuse the cached mapping throughout the worker.
  1187. * Can't do this for sends as they may happen outside the device
  1188. * worker.
  1189. */
  1190. VMBusRecvRingBuf *ringbuf = &chan->recv_ringbuf;
  1191. ringbuf_start_io(&ringbuf->common);
  1192. chan->notify_cb(chan);
  1193. ringbuf_end_io(&ringbuf->common);
  1194. }
  1195. }
  1196. static int alloc_chan_id(VMBus *vmbus)
  1197. {
  1198. int ret;
  1199. ret = find_next_zero_bit(vmbus->chanid_bitmap, VMBUS_CHANID_COUNT, 0);
  1200. if (ret == VMBUS_CHANID_COUNT) {
  1201. return -ENOMEM;
  1202. }
  1203. return ret + VMBUS_FIRST_CHANID;
  1204. }
  1205. static int register_chan_id(VMBusChannel *chan)
  1206. {
  1207. return test_and_set_bit(chan->id - VMBUS_FIRST_CHANID,
  1208. chan->vmbus->chanid_bitmap) ? -EEXIST : 0;
  1209. }
  1210. static void unregister_chan_id(VMBusChannel *chan)
  1211. {
  1212. clear_bit(chan->id - VMBUS_FIRST_CHANID, chan->vmbus->chanid_bitmap);
  1213. }
  1214. static uint32_t chan_connection_id(VMBusChannel *chan)
  1215. {
  1216. return VMBUS_CHAN_CONNECTION_OFFSET + chan->id;
  1217. }
  1218. static void init_channel(VMBus *vmbus, VMBusDevice *dev, VMBusDeviceClass *vdc,
  1219. VMBusChannel *chan, uint16_t idx, Error **errp)
  1220. {
  1221. int res;
  1222. chan->dev = dev;
  1223. chan->notify_cb = vdc->chan_notify_cb;
  1224. chan->subchan_idx = idx;
  1225. chan->vmbus = vmbus;
  1226. res = alloc_chan_id(vmbus);
  1227. if (res < 0) {
  1228. error_setg(errp, "no spare channel id");
  1229. return;
  1230. }
  1231. chan->id = res;
  1232. register_chan_id(chan);
  1233. /*
  1234. * The guest drivers depend on the device subchannels (idx #1+) to be
  1235. * offered after the primary channel (idx #0) of that device. To ensure
  1236. * that, record the channels on the channel list in the order they appear
  1237. * within the device.
  1238. */
  1239. QTAILQ_INSERT_TAIL(&vmbus->channel_list, chan, link);
  1240. }
  1241. static void deinit_channel(VMBusChannel *chan)
  1242. {
  1243. assert(chan->state == VMCHAN_INIT);
  1244. QTAILQ_REMOVE(&chan->vmbus->channel_list, chan, link);
  1245. unregister_chan_id(chan);
  1246. }
  1247. static void create_channels(VMBus *vmbus, VMBusDevice *dev, Error **errp)
  1248. {
  1249. uint16_t i;
  1250. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(dev);
  1251. Error *err = NULL;
  1252. dev->num_channels = vdc->num_channels ? vdc->num_channels(dev) : 1;
  1253. if (dev->num_channels < 1) {
  1254. error_setg(errp, "invalid #channels: %u", dev->num_channels);
  1255. return;
  1256. }
  1257. dev->channels = g_new0(VMBusChannel, dev->num_channels);
  1258. for (i = 0; i < dev->num_channels; i++) {
  1259. init_channel(vmbus, dev, vdc, &dev->channels[i], i, &err);
  1260. if (err) {
  1261. goto err_init;
  1262. }
  1263. }
  1264. return;
  1265. err_init:
  1266. while (i--) {
  1267. deinit_channel(&dev->channels[i]);
  1268. }
  1269. error_propagate(errp, err);
  1270. }
  1271. static void free_channels(VMBusDevice *dev)
  1272. {
  1273. uint16_t i;
  1274. for (i = 0; i < dev->num_channels; i++) {
  1275. deinit_channel(&dev->channels[i]);
  1276. }
  1277. g_free(dev->channels);
  1278. }
  1279. static HvSintRoute *make_sint_route(VMBus *vmbus, uint32_t vp_index)
  1280. {
  1281. VMBusChannel *chan;
  1282. if (vp_index == vmbus->target_vp) {
  1283. hyperv_sint_route_ref(vmbus->sint_route);
  1284. return vmbus->sint_route;
  1285. }
  1286. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1287. if (chan->target_vp == vp_index && vmbus_channel_is_open(chan)) {
  1288. hyperv_sint_route_ref(chan->notify_route);
  1289. return chan->notify_route;
  1290. }
  1291. }
  1292. return hyperv_sint_route_new(vp_index, VMBUS_SINT, NULL, NULL);
  1293. }
  1294. static void open_channel(VMBusChannel *chan)
  1295. {
  1296. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(chan->dev);
  1297. chan->gpadl = vmbus_get_gpadl(chan, chan->ringbuf_gpadl);
  1298. if (!chan->gpadl) {
  1299. return;
  1300. }
  1301. if (ringbufs_init(chan)) {
  1302. goto put_gpadl;
  1303. }
  1304. if (event_notifier_init(&chan->notifier, 0)) {
  1305. goto put_gpadl;
  1306. }
  1307. event_notifier_set_handler(&chan->notifier, channel_event_cb);
  1308. if (hyperv_set_event_flag_handler(chan_connection_id(chan),
  1309. &chan->notifier)) {
  1310. goto cleanup_notifier;
  1311. }
  1312. chan->notify_route = make_sint_route(chan->vmbus, chan->target_vp);
  1313. if (!chan->notify_route) {
  1314. goto clear_event_flag_handler;
  1315. }
  1316. if (vdc->open_channel && vdc->open_channel(chan)) {
  1317. goto unref_sint_route;
  1318. }
  1319. chan->is_open = true;
  1320. return;
  1321. unref_sint_route:
  1322. hyperv_sint_route_unref(chan->notify_route);
  1323. clear_event_flag_handler:
  1324. hyperv_set_event_flag_handler(chan_connection_id(chan), NULL);
  1325. cleanup_notifier:
  1326. event_notifier_set_handler(&chan->notifier, NULL);
  1327. event_notifier_cleanup(&chan->notifier);
  1328. put_gpadl:
  1329. vmbus_put_gpadl(chan->gpadl);
  1330. }
  1331. static void close_channel(VMBusChannel *chan)
  1332. {
  1333. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(chan->dev);
  1334. if (!chan->is_open) {
  1335. return;
  1336. }
  1337. if (vdc->close_channel) {
  1338. vdc->close_channel(chan);
  1339. }
  1340. hyperv_sint_route_unref(chan->notify_route);
  1341. hyperv_set_event_flag_handler(chan_connection_id(chan), NULL);
  1342. event_notifier_set_handler(&chan->notifier, NULL);
  1343. event_notifier_cleanup(&chan->notifier);
  1344. vmbus_put_gpadl(chan->gpadl);
  1345. chan->is_open = false;
  1346. }
  1347. static int channel_post_load(void *opaque, int version_id)
  1348. {
  1349. VMBusChannel *chan = opaque;
  1350. return register_chan_id(chan);
  1351. }
  1352. static const VMStateDescription vmstate_channel = {
  1353. .name = "vmbus/channel",
  1354. .version_id = 0,
  1355. .minimum_version_id = 0,
  1356. .post_load = channel_post_load,
  1357. .fields = (VMStateField[]) {
  1358. VMSTATE_UINT32(id, VMBusChannel),
  1359. VMSTATE_UINT16(subchan_idx, VMBusChannel),
  1360. VMSTATE_UINT32(open_id, VMBusChannel),
  1361. VMSTATE_UINT32(target_vp, VMBusChannel),
  1362. VMSTATE_UINT32(ringbuf_gpadl, VMBusChannel),
  1363. VMSTATE_UINT32(ringbuf_send_offset, VMBusChannel),
  1364. VMSTATE_UINT8(offer_state, VMBusChannel),
  1365. VMSTATE_UINT8(state, VMBusChannel),
  1366. VMSTATE_END_OF_LIST()
  1367. }
  1368. };
  1369. static VMBusChannel *find_channel(VMBus *vmbus, uint32_t id)
  1370. {
  1371. VMBusChannel *chan;
  1372. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1373. if (chan->id == id) {
  1374. return chan;
  1375. }
  1376. }
  1377. return NULL;
  1378. }
  1379. static int enqueue_incoming_message(VMBus *vmbus,
  1380. const struct hyperv_post_message_input *msg)
  1381. {
  1382. int ret = 0;
  1383. uint8_t idx, prev_size;
  1384. qemu_mutex_lock(&vmbus->rx_queue_lock);
  1385. if (vmbus->rx_queue_size == HV_MSG_QUEUE_LEN) {
  1386. ret = -ENOBUFS;
  1387. goto out;
  1388. }
  1389. prev_size = vmbus->rx_queue_size;
  1390. idx = (vmbus->rx_queue_head + vmbus->rx_queue_size) % HV_MSG_QUEUE_LEN;
  1391. memcpy(&vmbus->rx_queue[idx], msg, sizeof(*msg));
  1392. vmbus->rx_queue_size++;
  1393. /* only need to resched if the queue was empty before */
  1394. if (!prev_size) {
  1395. vmbus_resched(vmbus);
  1396. }
  1397. out:
  1398. qemu_mutex_unlock(&vmbus->rx_queue_lock);
  1399. return ret;
  1400. }
  1401. static uint16_t vmbus_recv_message(const struct hyperv_post_message_input *msg,
  1402. void *data)
  1403. {
  1404. VMBus *vmbus = data;
  1405. struct vmbus_message_header *vmbus_msg;
  1406. if (msg->message_type != HV_MESSAGE_VMBUS) {
  1407. return HV_STATUS_INVALID_HYPERCALL_INPUT;
  1408. }
  1409. if (msg->payload_size < sizeof(struct vmbus_message_header)) {
  1410. return HV_STATUS_INVALID_HYPERCALL_INPUT;
  1411. }
  1412. vmbus_msg = (struct vmbus_message_header *)msg->payload;
  1413. trace_vmbus_recv_message(vmbus_msg->message_type, msg->payload_size);
  1414. if (vmbus_msg->message_type == VMBUS_MSG_INVALID ||
  1415. vmbus_msg->message_type >= VMBUS_MSG_COUNT) {
  1416. error_report("vmbus: unknown message type %#x",
  1417. vmbus_msg->message_type);
  1418. return HV_STATUS_INVALID_HYPERCALL_INPUT;
  1419. }
  1420. if (enqueue_incoming_message(vmbus, msg)) {
  1421. return HV_STATUS_INSUFFICIENT_BUFFERS;
  1422. }
  1423. return HV_STATUS_SUCCESS;
  1424. }
  1425. static bool vmbus_initialized(VMBus *vmbus)
  1426. {
  1427. return vmbus->version > 0 && vmbus->version <= VMBUS_VERSION_CURRENT;
  1428. }
  1429. static void vmbus_reset_all(VMBus *vmbus)
  1430. {
  1431. qbus_reset_all(BUS(vmbus));
  1432. }
  1433. static void post_msg(VMBus *vmbus, void *msgdata, uint32_t msglen)
  1434. {
  1435. int ret;
  1436. struct hyperv_message msg = {
  1437. .header.message_type = HV_MESSAGE_VMBUS,
  1438. };
  1439. assert(!vmbus->msg_in_progress);
  1440. assert(msglen <= sizeof(msg.payload));
  1441. assert(msglen >= sizeof(struct vmbus_message_header));
  1442. vmbus->msg_in_progress = true;
  1443. trace_vmbus_post_msg(((struct vmbus_message_header *)msgdata)->message_type,
  1444. msglen);
  1445. memcpy(msg.payload, msgdata, msglen);
  1446. msg.header.payload_size = ROUND_UP(msglen, VMBUS_MESSAGE_SIZE_ALIGN);
  1447. ret = hyperv_post_msg(vmbus->sint_route, &msg);
  1448. if (ret == 0 || ret == -EAGAIN) {
  1449. return;
  1450. }
  1451. error_report("message delivery fatal failure: %d; aborting vmbus", ret);
  1452. vmbus_reset_all(vmbus);
  1453. }
  1454. static int vmbus_init(VMBus *vmbus)
  1455. {
  1456. if (vmbus->target_vp != (uint32_t)-1) {
  1457. vmbus->sint_route = hyperv_sint_route_new(vmbus->target_vp, VMBUS_SINT,
  1458. vmbus_msg_cb, vmbus);
  1459. if (!vmbus->sint_route) {
  1460. error_report("failed to set up SINT route");
  1461. return -ENOMEM;
  1462. }
  1463. }
  1464. return 0;
  1465. }
  1466. static void vmbus_deinit(VMBus *vmbus)
  1467. {
  1468. VMBusGpadl *gpadl, *tmp_gpadl;
  1469. VMBusChannel *chan;
  1470. QTAILQ_FOREACH_SAFE(gpadl, &vmbus->gpadl_list, link, tmp_gpadl) {
  1471. if (gpadl->state == VMGPADL_TORNDOWN) {
  1472. continue;
  1473. }
  1474. vmbus_put_gpadl(gpadl);
  1475. }
  1476. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1477. chan->offer_state = VMOFFER_INIT;
  1478. }
  1479. hyperv_sint_route_unref(vmbus->sint_route);
  1480. vmbus->sint_route = NULL;
  1481. vmbus->int_page_gpa = 0;
  1482. vmbus->target_vp = (uint32_t)-1;
  1483. vmbus->version = 0;
  1484. vmbus->state = VMBUS_LISTEN;
  1485. vmbus->msg_in_progress = false;
  1486. }
  1487. static void handle_initiate_contact(VMBus *vmbus,
  1488. vmbus_message_initiate_contact *msg,
  1489. uint32_t msglen)
  1490. {
  1491. if (msglen < sizeof(*msg)) {
  1492. return;
  1493. }
  1494. trace_vmbus_initiate_contact(msg->version_requested >> 16,
  1495. msg->version_requested & 0xffff,
  1496. msg->target_vcpu, msg->monitor_page1,
  1497. msg->monitor_page2, msg->interrupt_page);
  1498. /*
  1499. * Reset vmbus on INITIATE_CONTACT regardless of its previous state.
  1500. * Useful, in particular, with vmbus-aware BIOS which can't shut vmbus down
  1501. * before handing over to OS loader.
  1502. */
  1503. vmbus_reset_all(vmbus);
  1504. vmbus->target_vp = msg->target_vcpu;
  1505. vmbus->version = msg->version_requested;
  1506. if (vmbus->version < VMBUS_VERSION_WIN8) {
  1507. /* linux passes interrupt page even when it doesn't need it */
  1508. vmbus->int_page_gpa = msg->interrupt_page;
  1509. }
  1510. vmbus->state = VMBUS_HANDSHAKE;
  1511. if (vmbus_init(vmbus)) {
  1512. error_report("failed to init vmbus; aborting");
  1513. vmbus_deinit(vmbus);
  1514. return;
  1515. }
  1516. }
  1517. static void send_handshake(VMBus *vmbus)
  1518. {
  1519. struct vmbus_message_version_response msg = {
  1520. .header.message_type = VMBUS_MSG_VERSION_RESPONSE,
  1521. .version_supported = vmbus_initialized(vmbus),
  1522. };
  1523. post_msg(vmbus, &msg, sizeof(msg));
  1524. }
  1525. static void handle_request_offers(VMBus *vmbus, void *msgdata, uint32_t msglen)
  1526. {
  1527. VMBusChannel *chan;
  1528. if (!vmbus_initialized(vmbus)) {
  1529. return;
  1530. }
  1531. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1532. if (chan->offer_state == VMOFFER_INIT) {
  1533. chan->offer_state = VMOFFER_SENDING;
  1534. break;
  1535. }
  1536. }
  1537. vmbus->state = VMBUS_OFFER;
  1538. }
  1539. static void send_offer(VMBus *vmbus)
  1540. {
  1541. VMBusChannel *chan;
  1542. struct vmbus_message_header alloffers_msg = {
  1543. .message_type = VMBUS_MSG_ALLOFFERS_DELIVERED,
  1544. };
  1545. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1546. if (chan->offer_state == VMOFFER_SENDING) {
  1547. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(chan->dev);
  1548. /* Hyper-V wants LE GUIDs */
  1549. QemuUUID classid = qemu_uuid_bswap(vdc->classid);
  1550. QemuUUID instanceid = qemu_uuid_bswap(chan->dev->instanceid);
  1551. struct vmbus_message_offer_channel msg = {
  1552. .header.message_type = VMBUS_MSG_OFFERCHANNEL,
  1553. .child_relid = chan->id,
  1554. .connection_id = chan_connection_id(chan),
  1555. .channel_flags = vdc->channel_flags,
  1556. .mmio_size_mb = vdc->mmio_size_mb,
  1557. .sub_channel_index = vmbus_channel_idx(chan),
  1558. .interrupt_flags = VMBUS_OFFER_INTERRUPT_DEDICATED,
  1559. };
  1560. memcpy(msg.type_uuid, &classid, sizeof(classid));
  1561. memcpy(msg.instance_uuid, &instanceid, sizeof(instanceid));
  1562. trace_vmbus_send_offer(chan->id, chan->dev);
  1563. post_msg(vmbus, &msg, sizeof(msg));
  1564. return;
  1565. }
  1566. }
  1567. /* no more offers, send terminator message */
  1568. trace_vmbus_terminate_offers();
  1569. post_msg(vmbus, &alloffers_msg, sizeof(alloffers_msg));
  1570. }
  1571. static bool complete_offer(VMBus *vmbus)
  1572. {
  1573. VMBusChannel *chan;
  1574. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1575. if (chan->offer_state == VMOFFER_SENDING) {
  1576. chan->offer_state = VMOFFER_SENT;
  1577. goto next_offer;
  1578. }
  1579. }
  1580. /*
  1581. * no transitioning channels found so this is completing the terminator
  1582. * message, and vmbus can move to the next state
  1583. */
  1584. return true;
  1585. next_offer:
  1586. /* try to mark another channel for offering */
  1587. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1588. if (chan->offer_state == VMOFFER_INIT) {
  1589. chan->offer_state = VMOFFER_SENDING;
  1590. break;
  1591. }
  1592. }
  1593. /*
  1594. * if an offer has been sent there are more offers or the terminator yet to
  1595. * send, so no state transition for vmbus
  1596. */
  1597. return false;
  1598. }
  1599. static void handle_gpadl_header(VMBus *vmbus, vmbus_message_gpadl_header *msg,
  1600. uint32_t msglen)
  1601. {
  1602. VMBusGpadl *gpadl;
  1603. uint32_t num_gfns, i;
  1604. /* must include at least one gpa range */
  1605. if (msglen < sizeof(*msg) + sizeof(msg->range[0]) ||
  1606. !vmbus_initialized(vmbus)) {
  1607. return;
  1608. }
  1609. num_gfns = (msg->range_buflen - msg->rangecount * sizeof(msg->range[0])) /
  1610. sizeof(msg->range[0].pfn_array[0]);
  1611. trace_vmbus_gpadl_header(msg->gpadl_id, num_gfns);
  1612. /*
  1613. * In theory the GPADL_HEADER message can define a GPADL with multiple GPA
  1614. * ranges each with arbitrary size and alignment. However in practice only
  1615. * single-range page-aligned GPADLs have been observed so just ignore
  1616. * anything else and simplify things greatly.
  1617. */
  1618. if (msg->rangecount != 1 || msg->range[0].byte_offset ||
  1619. (msg->range[0].byte_count != (num_gfns << TARGET_PAGE_BITS))) {
  1620. return;
  1621. }
  1622. /* ignore requests to create already existing GPADLs */
  1623. if (find_gpadl(vmbus, msg->gpadl_id)) {
  1624. return;
  1625. }
  1626. gpadl = create_gpadl(vmbus, msg->gpadl_id, msg->child_relid, num_gfns);
  1627. for (i = 0; i < num_gfns &&
  1628. (void *)&msg->range[0].pfn_array[i + 1] <= (void *)msg + msglen;
  1629. i++) {
  1630. gpadl->gfns[gpadl->seen_gfns++] = msg->range[0].pfn_array[i];
  1631. }
  1632. if (gpadl_full(gpadl)) {
  1633. vmbus->state = VMBUS_CREATE_GPADL;
  1634. }
  1635. }
  1636. static void handle_gpadl_body(VMBus *vmbus, vmbus_message_gpadl_body *msg,
  1637. uint32_t msglen)
  1638. {
  1639. VMBusGpadl *gpadl;
  1640. uint32_t num_gfns_left, i;
  1641. if (msglen < sizeof(*msg) || !vmbus_initialized(vmbus)) {
  1642. return;
  1643. }
  1644. trace_vmbus_gpadl_body(msg->gpadl_id);
  1645. gpadl = find_gpadl(vmbus, msg->gpadl_id);
  1646. if (!gpadl) {
  1647. return;
  1648. }
  1649. num_gfns_left = gpadl->num_gfns - gpadl->seen_gfns;
  1650. assert(num_gfns_left);
  1651. for (i = 0; i < num_gfns_left &&
  1652. (void *)&msg->pfn_array[i + 1] <= (void *)msg + msglen; i++) {
  1653. gpadl->gfns[gpadl->seen_gfns++] = msg->pfn_array[i];
  1654. }
  1655. if (gpadl_full(gpadl)) {
  1656. vmbus->state = VMBUS_CREATE_GPADL;
  1657. }
  1658. }
  1659. static void send_create_gpadl(VMBus *vmbus)
  1660. {
  1661. VMBusGpadl *gpadl;
  1662. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  1663. if (gpadl_full(gpadl) && gpadl->state == VMGPADL_INIT) {
  1664. struct vmbus_message_gpadl_created msg = {
  1665. .header.message_type = VMBUS_MSG_GPADL_CREATED,
  1666. .gpadl_id = gpadl->id,
  1667. .child_relid = gpadl->child_relid,
  1668. };
  1669. trace_vmbus_gpadl_created(gpadl->id);
  1670. post_msg(vmbus, &msg, sizeof(msg));
  1671. return;
  1672. }
  1673. }
  1674. assert(false);
  1675. }
  1676. static bool complete_create_gpadl(VMBus *vmbus)
  1677. {
  1678. VMBusGpadl *gpadl;
  1679. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  1680. if (gpadl_full(gpadl) && gpadl->state == VMGPADL_INIT) {
  1681. gpadl->state = VMGPADL_ALIVE;
  1682. return true;
  1683. }
  1684. }
  1685. assert(false);
  1686. return false;
  1687. }
  1688. static void handle_gpadl_teardown(VMBus *vmbus,
  1689. vmbus_message_gpadl_teardown *msg,
  1690. uint32_t msglen)
  1691. {
  1692. VMBusGpadl *gpadl;
  1693. if (msglen < sizeof(*msg) || !vmbus_initialized(vmbus)) {
  1694. return;
  1695. }
  1696. trace_vmbus_gpadl_teardown(msg->gpadl_id);
  1697. gpadl = find_gpadl(vmbus, msg->gpadl_id);
  1698. if (!gpadl || gpadl->state == VMGPADL_TORNDOWN) {
  1699. return;
  1700. }
  1701. gpadl->state = VMGPADL_TEARINGDOWN;
  1702. vmbus->state = VMBUS_TEARDOWN_GPADL;
  1703. }
  1704. static void send_teardown_gpadl(VMBus *vmbus)
  1705. {
  1706. VMBusGpadl *gpadl;
  1707. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  1708. if (gpadl->state == VMGPADL_TEARINGDOWN) {
  1709. struct vmbus_message_gpadl_torndown msg = {
  1710. .header.message_type = VMBUS_MSG_GPADL_TORNDOWN,
  1711. .gpadl_id = gpadl->id,
  1712. };
  1713. trace_vmbus_gpadl_torndown(gpadl->id);
  1714. post_msg(vmbus, &msg, sizeof(msg));
  1715. return;
  1716. }
  1717. }
  1718. assert(false);
  1719. }
  1720. static bool complete_teardown_gpadl(VMBus *vmbus)
  1721. {
  1722. VMBusGpadl *gpadl;
  1723. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  1724. if (gpadl->state == VMGPADL_TEARINGDOWN) {
  1725. gpadl->state = VMGPADL_TORNDOWN;
  1726. vmbus_put_gpadl(gpadl);
  1727. return true;
  1728. }
  1729. }
  1730. assert(false);
  1731. return false;
  1732. }
  1733. static void handle_open_channel(VMBus *vmbus, vmbus_message_open_channel *msg,
  1734. uint32_t msglen)
  1735. {
  1736. VMBusChannel *chan;
  1737. if (msglen < sizeof(*msg) || !vmbus_initialized(vmbus)) {
  1738. return;
  1739. }
  1740. trace_vmbus_open_channel(msg->child_relid, msg->ring_buffer_gpadl_id,
  1741. msg->target_vp);
  1742. chan = find_channel(vmbus, msg->child_relid);
  1743. if (!chan || chan->state != VMCHAN_INIT) {
  1744. return;
  1745. }
  1746. chan->ringbuf_gpadl = msg->ring_buffer_gpadl_id;
  1747. chan->ringbuf_send_offset = msg->ring_buffer_offset;
  1748. chan->target_vp = msg->target_vp;
  1749. chan->open_id = msg->open_id;
  1750. open_channel(chan);
  1751. chan->state = VMCHAN_OPENING;
  1752. vmbus->state = VMBUS_OPEN_CHANNEL;
  1753. }
  1754. static void send_open_channel(VMBus *vmbus)
  1755. {
  1756. VMBusChannel *chan;
  1757. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1758. if (chan->state == VMCHAN_OPENING) {
  1759. struct vmbus_message_open_result msg = {
  1760. .header.message_type = VMBUS_MSG_OPENCHANNEL_RESULT,
  1761. .child_relid = chan->id,
  1762. .open_id = chan->open_id,
  1763. .status = !vmbus_channel_is_open(chan),
  1764. };
  1765. trace_vmbus_channel_open(chan->id, msg.status);
  1766. post_msg(vmbus, &msg, sizeof(msg));
  1767. return;
  1768. }
  1769. }
  1770. assert(false);
  1771. }
  1772. static bool complete_open_channel(VMBus *vmbus)
  1773. {
  1774. VMBusChannel *chan;
  1775. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1776. if (chan->state == VMCHAN_OPENING) {
  1777. if (vmbus_channel_is_open(chan)) {
  1778. chan->state = VMCHAN_OPEN;
  1779. /*
  1780. * simulate guest notification of ringbuffer space made
  1781. * available, for the channel protocols where the host
  1782. * initiates the communication
  1783. */
  1784. vmbus_channel_notify_host(chan);
  1785. } else {
  1786. chan->state = VMCHAN_INIT;
  1787. }
  1788. return true;
  1789. }
  1790. }
  1791. assert(false);
  1792. return false;
  1793. }
  1794. static void vdev_reset_on_close(VMBusDevice *vdev)
  1795. {
  1796. uint16_t i;
  1797. for (i = 0; i < vdev->num_channels; i++) {
  1798. if (vmbus_channel_is_open(&vdev->channels[i])) {
  1799. return;
  1800. }
  1801. }
  1802. /* all channels closed -- reset device */
  1803. qdev_reset_all(DEVICE(vdev));
  1804. }
  1805. static void handle_close_channel(VMBus *vmbus, vmbus_message_close_channel *msg,
  1806. uint32_t msglen)
  1807. {
  1808. VMBusChannel *chan;
  1809. if (msglen < sizeof(*msg) || !vmbus_initialized(vmbus)) {
  1810. return;
  1811. }
  1812. trace_vmbus_close_channel(msg->child_relid);
  1813. chan = find_channel(vmbus, msg->child_relid);
  1814. if (!chan) {
  1815. return;
  1816. }
  1817. close_channel(chan);
  1818. chan->state = VMCHAN_INIT;
  1819. vdev_reset_on_close(chan->dev);
  1820. }
  1821. static void handle_unload(VMBus *vmbus, void *msg, uint32_t msglen)
  1822. {
  1823. vmbus->state = VMBUS_UNLOAD;
  1824. }
  1825. static void send_unload(VMBus *vmbus)
  1826. {
  1827. vmbus_message_header msg = {
  1828. .message_type = VMBUS_MSG_UNLOAD_RESPONSE,
  1829. };
  1830. qemu_mutex_lock(&vmbus->rx_queue_lock);
  1831. vmbus->rx_queue_size = 0;
  1832. qemu_mutex_unlock(&vmbus->rx_queue_lock);
  1833. post_msg(vmbus, &msg, sizeof(msg));
  1834. return;
  1835. }
  1836. static bool complete_unload(VMBus *vmbus)
  1837. {
  1838. vmbus_reset_all(vmbus);
  1839. return true;
  1840. }
  1841. static void process_message(VMBus *vmbus)
  1842. {
  1843. struct hyperv_post_message_input *hv_msg;
  1844. struct vmbus_message_header *msg;
  1845. void *msgdata;
  1846. uint32_t msglen;
  1847. qemu_mutex_lock(&vmbus->rx_queue_lock);
  1848. if (!vmbus->rx_queue_size) {
  1849. goto unlock;
  1850. }
  1851. hv_msg = &vmbus->rx_queue[vmbus->rx_queue_head];
  1852. msglen = hv_msg->payload_size;
  1853. if (msglen < sizeof(*msg)) {
  1854. goto out;
  1855. }
  1856. msgdata = hv_msg->payload;
  1857. msg = (struct vmbus_message_header *)msgdata;
  1858. trace_vmbus_process_incoming_message(msg->message_type);
  1859. switch (msg->message_type) {
  1860. case VMBUS_MSG_INITIATE_CONTACT:
  1861. handle_initiate_contact(vmbus, msgdata, msglen);
  1862. break;
  1863. case VMBUS_MSG_REQUESTOFFERS:
  1864. handle_request_offers(vmbus, msgdata, msglen);
  1865. break;
  1866. case VMBUS_MSG_GPADL_HEADER:
  1867. handle_gpadl_header(vmbus, msgdata, msglen);
  1868. break;
  1869. case VMBUS_MSG_GPADL_BODY:
  1870. handle_gpadl_body(vmbus, msgdata, msglen);
  1871. break;
  1872. case VMBUS_MSG_GPADL_TEARDOWN:
  1873. handle_gpadl_teardown(vmbus, msgdata, msglen);
  1874. break;
  1875. case VMBUS_MSG_OPENCHANNEL:
  1876. handle_open_channel(vmbus, msgdata, msglen);
  1877. break;
  1878. case VMBUS_MSG_CLOSECHANNEL:
  1879. handle_close_channel(vmbus, msgdata, msglen);
  1880. break;
  1881. case VMBUS_MSG_UNLOAD:
  1882. handle_unload(vmbus, msgdata, msglen);
  1883. break;
  1884. default:
  1885. error_report("unknown message type %#x", msg->message_type);
  1886. break;
  1887. }
  1888. out:
  1889. vmbus->rx_queue_size--;
  1890. vmbus->rx_queue_head++;
  1891. vmbus->rx_queue_head %= HV_MSG_QUEUE_LEN;
  1892. vmbus_resched(vmbus);
  1893. unlock:
  1894. qemu_mutex_unlock(&vmbus->rx_queue_lock);
  1895. }
  1896. static const struct {
  1897. void (*run)(VMBus *vmbus);
  1898. bool (*complete)(VMBus *vmbus);
  1899. } state_runner[] = {
  1900. [VMBUS_LISTEN] = {process_message, NULL},
  1901. [VMBUS_HANDSHAKE] = {send_handshake, NULL},
  1902. [VMBUS_OFFER] = {send_offer, complete_offer},
  1903. [VMBUS_CREATE_GPADL] = {send_create_gpadl, complete_create_gpadl},
  1904. [VMBUS_TEARDOWN_GPADL] = {send_teardown_gpadl, complete_teardown_gpadl},
  1905. [VMBUS_OPEN_CHANNEL] = {send_open_channel, complete_open_channel},
  1906. [VMBUS_UNLOAD] = {send_unload, complete_unload},
  1907. };
  1908. static void vmbus_do_run(VMBus *vmbus)
  1909. {
  1910. if (vmbus->msg_in_progress) {
  1911. return;
  1912. }
  1913. assert(vmbus->state < VMBUS_STATE_MAX);
  1914. assert(state_runner[vmbus->state].run);
  1915. state_runner[vmbus->state].run(vmbus);
  1916. }
  1917. static void vmbus_run(void *opaque)
  1918. {
  1919. VMBus *vmbus = opaque;
  1920. /* make sure no recursion happens (e.g. due to recursive aio_poll()) */
  1921. if (vmbus->in_progress) {
  1922. return;
  1923. }
  1924. vmbus->in_progress = true;
  1925. /*
  1926. * FIXME: if vmbus_resched() is called from within vmbus_do_run(), it
  1927. * should go *after* the code that can result in aio_poll; otherwise
  1928. * reschedules can be missed. No idea how to enforce that.
  1929. */
  1930. vmbus_do_run(vmbus);
  1931. vmbus->in_progress = false;
  1932. }
  1933. static void vmbus_msg_cb(void *data, int status)
  1934. {
  1935. VMBus *vmbus = data;
  1936. bool (*complete)(VMBus *vmbus);
  1937. assert(vmbus->msg_in_progress);
  1938. trace_vmbus_msg_cb(status);
  1939. if (status == -EAGAIN) {
  1940. goto out;
  1941. }
  1942. if (status) {
  1943. error_report("message delivery fatal failure: %d; aborting vmbus",
  1944. status);
  1945. vmbus_reset_all(vmbus);
  1946. return;
  1947. }
  1948. assert(vmbus->state < VMBUS_STATE_MAX);
  1949. complete = state_runner[vmbus->state].complete;
  1950. if (!complete || complete(vmbus)) {
  1951. vmbus->state = VMBUS_LISTEN;
  1952. }
  1953. out:
  1954. vmbus->msg_in_progress = false;
  1955. vmbus_resched(vmbus);
  1956. }
  1957. static void vmbus_resched(VMBus *vmbus)
  1958. {
  1959. aio_bh_schedule_oneshot(qemu_get_aio_context(), vmbus_run, vmbus);
  1960. }
  1961. static void vmbus_signal_event(EventNotifier *e)
  1962. {
  1963. VMBusChannel *chan;
  1964. VMBus *vmbus = container_of(e, VMBus, notifier);
  1965. unsigned long *int_map;
  1966. hwaddr addr, len;
  1967. bool is_dirty = false;
  1968. if (!event_notifier_test_and_clear(e)) {
  1969. return;
  1970. }
  1971. trace_vmbus_signal_event();
  1972. if (!vmbus->int_page_gpa) {
  1973. return;
  1974. }
  1975. addr = vmbus->int_page_gpa + TARGET_PAGE_SIZE / 2;
  1976. len = TARGET_PAGE_SIZE / 2;
  1977. int_map = cpu_physical_memory_map(addr, &len, 1);
  1978. if (len != TARGET_PAGE_SIZE / 2) {
  1979. goto unmap;
  1980. }
  1981. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1982. if (bitmap_test_and_clear_atomic(int_map, chan->id, 1)) {
  1983. if (!vmbus_channel_is_open(chan)) {
  1984. continue;
  1985. }
  1986. vmbus_channel_notify_host(chan);
  1987. is_dirty = true;
  1988. }
  1989. }
  1990. unmap:
  1991. cpu_physical_memory_unmap(int_map, len, 1, is_dirty);
  1992. }
  1993. static void vmbus_dev_realize(DeviceState *dev, Error **errp)
  1994. {
  1995. VMBusDevice *vdev = VMBUS_DEVICE(dev);
  1996. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(vdev);
  1997. VMBus *vmbus = VMBUS(qdev_get_parent_bus(dev));
  1998. BusChild *child;
  1999. Error *err = NULL;
  2000. char idstr[UUID_FMT_LEN + 1];
  2001. assert(!qemu_uuid_is_null(&vdev->instanceid));
  2002. /* Check for instance id collision for this class id */
  2003. QTAILQ_FOREACH(child, &BUS(vmbus)->children, sibling) {
  2004. VMBusDevice *child_dev = VMBUS_DEVICE(child->child);
  2005. if (child_dev == vdev) {
  2006. continue;
  2007. }
  2008. if (qemu_uuid_is_equal(&child_dev->instanceid, &vdev->instanceid)) {
  2009. qemu_uuid_unparse(&vdev->instanceid, idstr);
  2010. error_setg(&err, "duplicate vmbus device instance id %s", idstr);
  2011. goto error_out;
  2012. }
  2013. }
  2014. vdev->dma_as = &address_space_memory;
  2015. create_channels(vmbus, vdev, &err);
  2016. if (err) {
  2017. goto error_out;
  2018. }
  2019. if (vdc->vmdev_realize) {
  2020. vdc->vmdev_realize(vdev, &err);
  2021. if (err) {
  2022. goto err_vdc_realize;
  2023. }
  2024. }
  2025. return;
  2026. err_vdc_realize:
  2027. free_channels(vdev);
  2028. error_out:
  2029. error_propagate(errp, err);
  2030. }
  2031. static void vmbus_dev_reset(DeviceState *dev)
  2032. {
  2033. uint16_t i;
  2034. VMBusDevice *vdev = VMBUS_DEVICE(dev);
  2035. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(vdev);
  2036. if (vdev->channels) {
  2037. for (i = 0; i < vdev->num_channels; i++) {
  2038. VMBusChannel *chan = &vdev->channels[i];
  2039. close_channel(chan);
  2040. chan->state = VMCHAN_INIT;
  2041. }
  2042. }
  2043. if (vdc->vmdev_reset) {
  2044. vdc->vmdev_reset(vdev);
  2045. }
  2046. }
  2047. static void vmbus_dev_unrealize(DeviceState *dev)
  2048. {
  2049. VMBusDevice *vdev = VMBUS_DEVICE(dev);
  2050. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(vdev);
  2051. if (vdc->vmdev_unrealize) {
  2052. vdc->vmdev_unrealize(vdev);
  2053. }
  2054. free_channels(vdev);
  2055. }
  2056. static void vmbus_dev_class_init(ObjectClass *klass, void *data)
  2057. {
  2058. DeviceClass *kdev = DEVICE_CLASS(klass);
  2059. kdev->bus_type = TYPE_VMBUS;
  2060. kdev->realize = vmbus_dev_realize;
  2061. kdev->unrealize = vmbus_dev_unrealize;
  2062. kdev->reset = vmbus_dev_reset;
  2063. }
  2064. static Property vmbus_dev_instanceid =
  2065. DEFINE_PROP_UUID("instanceid", VMBusDevice, instanceid);
  2066. static void vmbus_dev_instance_init(Object *obj)
  2067. {
  2068. VMBusDevice *vdev = VMBUS_DEVICE(obj);
  2069. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(vdev);
  2070. if (!qemu_uuid_is_null(&vdc->instanceid)) {
  2071. /* Class wants to only have a single instance with a fixed UUID */
  2072. vdev->instanceid = vdc->instanceid;
  2073. } else {
  2074. qdev_property_add_static(DEVICE(vdev), &vmbus_dev_instanceid);
  2075. }
  2076. }
  2077. const VMStateDescription vmstate_vmbus_dev = {
  2078. .name = TYPE_VMBUS_DEVICE,
  2079. .version_id = 0,
  2080. .minimum_version_id = 0,
  2081. .fields = (VMStateField[]) {
  2082. VMSTATE_UINT8_ARRAY(instanceid.data, VMBusDevice, 16),
  2083. VMSTATE_UINT16(num_channels, VMBusDevice),
  2084. VMSTATE_STRUCT_VARRAY_POINTER_UINT16(channels, VMBusDevice,
  2085. num_channels, vmstate_channel,
  2086. VMBusChannel),
  2087. VMSTATE_END_OF_LIST()
  2088. }
  2089. };
  2090. /* vmbus generic device base */
  2091. static const TypeInfo vmbus_dev_type_info = {
  2092. .name = TYPE_VMBUS_DEVICE,
  2093. .parent = TYPE_DEVICE,
  2094. .abstract = true,
  2095. .instance_size = sizeof(VMBusDevice),
  2096. .class_size = sizeof(VMBusDeviceClass),
  2097. .class_init = vmbus_dev_class_init,
  2098. .instance_init = vmbus_dev_instance_init,
  2099. };
  2100. static void vmbus_realize(BusState *bus, Error **errp)
  2101. {
  2102. int ret = 0;
  2103. Error *local_err = NULL;
  2104. VMBus *vmbus = VMBUS(bus);
  2105. qemu_mutex_init(&vmbus->rx_queue_lock);
  2106. QTAILQ_INIT(&vmbus->gpadl_list);
  2107. QTAILQ_INIT(&vmbus->channel_list);
  2108. ret = hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID,
  2109. vmbus_recv_message, vmbus);
  2110. if (ret != 0) {
  2111. error_setg(&local_err, "hyperv set message handler failed: %d", ret);
  2112. goto error_out;
  2113. }
  2114. ret = event_notifier_init(&vmbus->notifier, 0);
  2115. if (ret != 0) {
  2116. error_setg(&local_err, "event notifier failed to init with %d", ret);
  2117. goto remove_msg_handler;
  2118. }
  2119. event_notifier_set_handler(&vmbus->notifier, vmbus_signal_event);
  2120. ret = hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID,
  2121. &vmbus->notifier);
  2122. if (ret != 0) {
  2123. error_setg(&local_err, "hyperv set event handler failed with %d", ret);
  2124. goto clear_event_notifier;
  2125. }
  2126. return;
  2127. clear_event_notifier:
  2128. event_notifier_cleanup(&vmbus->notifier);
  2129. remove_msg_handler:
  2130. hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID, NULL, NULL);
  2131. error_out:
  2132. qemu_mutex_destroy(&vmbus->rx_queue_lock);
  2133. error_propagate(errp, local_err);
  2134. }
  2135. static void vmbus_unrealize(BusState *bus)
  2136. {
  2137. VMBus *vmbus = VMBUS(bus);
  2138. hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID, NULL, NULL);
  2139. hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID, NULL);
  2140. event_notifier_cleanup(&vmbus->notifier);
  2141. qemu_mutex_destroy(&vmbus->rx_queue_lock);
  2142. }
  2143. static void vmbus_reset(BusState *bus)
  2144. {
  2145. vmbus_deinit(VMBUS(bus));
  2146. }
  2147. static char *vmbus_get_dev_path(DeviceState *dev)
  2148. {
  2149. BusState *bus = qdev_get_parent_bus(dev);
  2150. return qdev_get_dev_path(bus->parent);
  2151. }
  2152. static char *vmbus_get_fw_dev_path(DeviceState *dev)
  2153. {
  2154. VMBusDevice *vdev = VMBUS_DEVICE(dev);
  2155. char uuid[UUID_FMT_LEN + 1];
  2156. qemu_uuid_unparse(&vdev->instanceid, uuid);
  2157. return g_strdup_printf("%s@%s", qdev_fw_name(dev), uuid);
  2158. }
  2159. static void vmbus_class_init(ObjectClass *klass, void *data)
  2160. {
  2161. BusClass *k = BUS_CLASS(klass);
  2162. k->get_dev_path = vmbus_get_dev_path;
  2163. k->get_fw_dev_path = vmbus_get_fw_dev_path;
  2164. k->realize = vmbus_realize;
  2165. k->unrealize = vmbus_unrealize;
  2166. k->reset = vmbus_reset;
  2167. }
  2168. static int vmbus_pre_load(void *opaque)
  2169. {
  2170. VMBusChannel *chan;
  2171. VMBus *vmbus = VMBUS(opaque);
  2172. /*
  2173. * channel IDs allocated by the source will come in the migration stream
  2174. * for each channel, so clean up the ones allocated at realize
  2175. */
  2176. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  2177. unregister_chan_id(chan);
  2178. }
  2179. return 0;
  2180. }
  2181. static int vmbus_post_load(void *opaque, int version_id)
  2182. {
  2183. int ret;
  2184. VMBus *vmbus = VMBUS(opaque);
  2185. VMBusGpadl *gpadl;
  2186. VMBusChannel *chan;
  2187. ret = vmbus_init(vmbus);
  2188. if (ret) {
  2189. return ret;
  2190. }
  2191. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  2192. gpadl->vmbus = vmbus;
  2193. gpadl->refcount = 1;
  2194. }
  2195. /*
  2196. * reopening channels depends on initialized vmbus so it's done here
  2197. * instead of channel_post_load()
  2198. */
  2199. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  2200. if (chan->state == VMCHAN_OPENING || chan->state == VMCHAN_OPEN) {
  2201. open_channel(chan);
  2202. }
  2203. if (chan->state != VMCHAN_OPEN) {
  2204. continue;
  2205. }
  2206. if (!vmbus_channel_is_open(chan)) {
  2207. /* reopen failed, abort loading */
  2208. return -1;
  2209. }
  2210. /* resume processing on the guest side if it missed the notification */
  2211. hyperv_sint_route_set_sint(chan->notify_route);
  2212. /* ditto on the host side */
  2213. vmbus_channel_notify_host(chan);
  2214. }
  2215. vmbus_resched(vmbus);
  2216. return 0;
  2217. }
  2218. static const VMStateDescription vmstate_post_message_input = {
  2219. .name = "vmbus/hyperv_post_message_input",
  2220. .version_id = 0,
  2221. .minimum_version_id = 0,
  2222. .fields = (VMStateField[]) {
  2223. /*
  2224. * skip connection_id and message_type as they are validated before
  2225. * queueing and ignored on dequeueing
  2226. */
  2227. VMSTATE_UINT32(payload_size, struct hyperv_post_message_input),
  2228. VMSTATE_UINT8_ARRAY(payload, struct hyperv_post_message_input,
  2229. HV_MESSAGE_PAYLOAD_SIZE),
  2230. VMSTATE_END_OF_LIST()
  2231. }
  2232. };
  2233. static bool vmbus_rx_queue_needed(void *opaque)
  2234. {
  2235. VMBus *vmbus = VMBUS(opaque);
  2236. return vmbus->rx_queue_size;
  2237. }
  2238. static const VMStateDescription vmstate_rx_queue = {
  2239. .name = "vmbus/rx_queue",
  2240. .version_id = 0,
  2241. .minimum_version_id = 0,
  2242. .needed = vmbus_rx_queue_needed,
  2243. .fields = (VMStateField[]) {
  2244. VMSTATE_UINT8(rx_queue_head, VMBus),
  2245. VMSTATE_UINT8(rx_queue_size, VMBus),
  2246. VMSTATE_STRUCT_ARRAY(rx_queue, VMBus,
  2247. HV_MSG_QUEUE_LEN, 0,
  2248. vmstate_post_message_input,
  2249. struct hyperv_post_message_input),
  2250. VMSTATE_END_OF_LIST()
  2251. }
  2252. };
  2253. static const VMStateDescription vmstate_vmbus = {
  2254. .name = TYPE_VMBUS,
  2255. .version_id = 0,
  2256. .minimum_version_id = 0,
  2257. .pre_load = vmbus_pre_load,
  2258. .post_load = vmbus_post_load,
  2259. .fields = (VMStateField[]) {
  2260. VMSTATE_UINT8(state, VMBus),
  2261. VMSTATE_UINT32(version, VMBus),
  2262. VMSTATE_UINT32(target_vp, VMBus),
  2263. VMSTATE_UINT64(int_page_gpa, VMBus),
  2264. VMSTATE_QTAILQ_V(gpadl_list, VMBus, 0,
  2265. vmstate_gpadl, VMBusGpadl, link),
  2266. VMSTATE_END_OF_LIST()
  2267. },
  2268. .subsections = (const VMStateDescription * []) {
  2269. &vmstate_rx_queue,
  2270. NULL
  2271. }
  2272. };
  2273. static const TypeInfo vmbus_type_info = {
  2274. .name = TYPE_VMBUS,
  2275. .parent = TYPE_BUS,
  2276. .instance_size = sizeof(VMBus),
  2277. .class_init = vmbus_class_init,
  2278. };
  2279. static void vmbus_bridge_realize(DeviceState *dev, Error **errp)
  2280. {
  2281. VMBusBridge *bridge = VMBUS_BRIDGE(dev);
  2282. /*
  2283. * here there's at least one vmbus bridge that is being realized, so
  2284. * vmbus_bridge_find can only return NULL if it's not unique
  2285. */
  2286. if (!vmbus_bridge_find()) {
  2287. error_setg(errp, "there can be at most one %s in the system",
  2288. TYPE_VMBUS_BRIDGE);
  2289. return;
  2290. }
  2291. if (!hyperv_is_synic_enabled()) {
  2292. error_report("VMBus requires usable Hyper-V SynIC and VP_INDEX");
  2293. return;
  2294. }
  2295. bridge->bus = VMBUS(qbus_create(TYPE_VMBUS, dev, "vmbus"));
  2296. }
  2297. static char *vmbus_bridge_ofw_unit_address(const SysBusDevice *dev)
  2298. {
  2299. /* there can be only one VMBus */
  2300. return g_strdup("0");
  2301. }
  2302. static const VMStateDescription vmstate_vmbus_bridge = {
  2303. .name = TYPE_VMBUS_BRIDGE,
  2304. .version_id = 0,
  2305. .minimum_version_id = 0,
  2306. .fields = (VMStateField[]) {
  2307. VMSTATE_STRUCT_POINTER(bus, VMBusBridge, vmstate_vmbus, VMBus),
  2308. VMSTATE_END_OF_LIST()
  2309. },
  2310. };
  2311. static Property vmbus_bridge_props[] = {
  2312. DEFINE_PROP_UINT8("irq", VMBusBridge, irq, 7),
  2313. DEFINE_PROP_END_OF_LIST()
  2314. };
  2315. static void vmbus_bridge_class_init(ObjectClass *klass, void *data)
  2316. {
  2317. DeviceClass *k = DEVICE_CLASS(klass);
  2318. SysBusDeviceClass *sk = SYS_BUS_DEVICE_CLASS(klass);
  2319. k->realize = vmbus_bridge_realize;
  2320. k->fw_name = "vmbus";
  2321. sk->explicit_ofw_unit_address = vmbus_bridge_ofw_unit_address;
  2322. set_bit(DEVICE_CATEGORY_BRIDGE, k->categories);
  2323. k->vmsd = &vmstate_vmbus_bridge;
  2324. device_class_set_props(k, vmbus_bridge_props);
  2325. /* override SysBusDevice's default */
  2326. k->user_creatable = true;
  2327. }
  2328. static const TypeInfo vmbus_bridge_type_info = {
  2329. .name = TYPE_VMBUS_BRIDGE,
  2330. .parent = TYPE_SYS_BUS_DEVICE,
  2331. .instance_size = sizeof(VMBusBridge),
  2332. .class_init = vmbus_bridge_class_init,
  2333. };
  2334. static void vmbus_register_types(void)
  2335. {
  2336. type_register_static(&vmbus_bridge_type_info);
  2337. type_register_static(&vmbus_dev_type_info);
  2338. type_register_static(&vmbus_type_info);
  2339. }
  2340. type_init(vmbus_register_types)