2
0

vmbus.c 74 KB

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