vmbus.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  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 = (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 = (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. assert(false);
  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. assert(false);
  1603. return false;
  1604. }
  1605. static void handle_gpadl_teardown(VMBus *vmbus,
  1606. vmbus_message_gpadl_teardown *msg,
  1607. uint32_t msglen)
  1608. {
  1609. VMBusGpadl *gpadl;
  1610. if (msglen < sizeof(*msg) || !vmbus_initialized(vmbus)) {
  1611. return;
  1612. }
  1613. trace_vmbus_gpadl_teardown(msg->gpadl_id);
  1614. gpadl = find_gpadl(vmbus, msg->gpadl_id);
  1615. if (!gpadl || gpadl->state == VMGPADL_TORNDOWN) {
  1616. return;
  1617. }
  1618. gpadl->state = VMGPADL_TEARINGDOWN;
  1619. vmbus->state = VMBUS_TEARDOWN_GPADL;
  1620. }
  1621. static void send_teardown_gpadl(VMBus *vmbus)
  1622. {
  1623. VMBusGpadl *gpadl;
  1624. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  1625. if (gpadl->state == VMGPADL_TEARINGDOWN) {
  1626. struct vmbus_message_gpadl_torndown msg = {
  1627. .header.message_type = VMBUS_MSG_GPADL_TORNDOWN,
  1628. .gpadl_id = gpadl->id,
  1629. };
  1630. trace_vmbus_gpadl_torndown(gpadl->id);
  1631. post_msg(vmbus, &msg, sizeof(msg));
  1632. return;
  1633. }
  1634. }
  1635. assert(false);
  1636. }
  1637. static bool complete_teardown_gpadl(VMBus *vmbus)
  1638. {
  1639. VMBusGpadl *gpadl;
  1640. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  1641. if (gpadl->state == VMGPADL_TEARINGDOWN) {
  1642. gpadl->state = VMGPADL_TORNDOWN;
  1643. vmbus_put_gpadl(gpadl);
  1644. return true;
  1645. }
  1646. }
  1647. assert(false);
  1648. return false;
  1649. }
  1650. static void handle_open_channel(VMBus *vmbus, vmbus_message_open_channel *msg,
  1651. uint32_t msglen)
  1652. {
  1653. VMBusChannel *chan;
  1654. if (msglen < sizeof(*msg) || !vmbus_initialized(vmbus)) {
  1655. return;
  1656. }
  1657. trace_vmbus_open_channel(msg->child_relid, msg->ring_buffer_gpadl_id,
  1658. msg->target_vp);
  1659. chan = find_channel(vmbus, msg->child_relid);
  1660. if (!chan || chan->state != VMCHAN_INIT) {
  1661. return;
  1662. }
  1663. chan->ringbuf_gpadl = msg->ring_buffer_gpadl_id;
  1664. chan->ringbuf_send_offset = msg->ring_buffer_offset;
  1665. chan->target_vp = msg->target_vp;
  1666. chan->open_id = msg->open_id;
  1667. open_channel(chan);
  1668. chan->state = VMCHAN_OPENING;
  1669. vmbus->state = VMBUS_OPEN_CHANNEL;
  1670. }
  1671. static void send_open_channel(VMBus *vmbus)
  1672. {
  1673. VMBusChannel *chan;
  1674. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1675. if (chan->state == VMCHAN_OPENING) {
  1676. struct vmbus_message_open_result msg = {
  1677. .header.message_type = VMBUS_MSG_OPENCHANNEL_RESULT,
  1678. .child_relid = chan->id,
  1679. .open_id = chan->open_id,
  1680. .status = !vmbus_channel_is_open(chan),
  1681. };
  1682. trace_vmbus_channel_open(chan->id, msg.status);
  1683. post_msg(vmbus, &msg, sizeof(msg));
  1684. return;
  1685. }
  1686. }
  1687. assert(false);
  1688. }
  1689. static bool complete_open_channel(VMBus *vmbus)
  1690. {
  1691. VMBusChannel *chan;
  1692. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1693. if (chan->state == VMCHAN_OPENING) {
  1694. if (vmbus_channel_is_open(chan)) {
  1695. chan->state = VMCHAN_OPEN;
  1696. /*
  1697. * simulate guest notification of ringbuffer space made
  1698. * available, for the channel protocols where the host
  1699. * initiates the communication
  1700. */
  1701. vmbus_channel_notify_host(chan);
  1702. } else {
  1703. chan->state = VMCHAN_INIT;
  1704. }
  1705. return true;
  1706. }
  1707. }
  1708. assert(false);
  1709. return false;
  1710. }
  1711. static void vdev_reset_on_close(VMBusDevice *vdev)
  1712. {
  1713. uint16_t i;
  1714. for (i = 0; i < vdev->num_channels; i++) {
  1715. if (vmbus_channel_is_open(&vdev->channels[i])) {
  1716. return;
  1717. }
  1718. }
  1719. /* all channels closed -- reset device */
  1720. device_cold_reset(DEVICE(vdev));
  1721. }
  1722. static void handle_close_channel(VMBus *vmbus, vmbus_message_close_channel *msg,
  1723. uint32_t msglen)
  1724. {
  1725. VMBusChannel *chan;
  1726. if (msglen < sizeof(*msg) || !vmbus_initialized(vmbus)) {
  1727. return;
  1728. }
  1729. trace_vmbus_close_channel(msg->child_relid);
  1730. chan = find_channel(vmbus, msg->child_relid);
  1731. if (!chan) {
  1732. return;
  1733. }
  1734. close_channel(chan);
  1735. chan->state = VMCHAN_INIT;
  1736. vdev_reset_on_close(chan->dev);
  1737. }
  1738. static void handle_unload(VMBus *vmbus, void *msg, uint32_t msglen)
  1739. {
  1740. vmbus->state = VMBUS_UNLOAD;
  1741. }
  1742. static void send_unload(VMBus *vmbus)
  1743. {
  1744. vmbus_message_header msg = {
  1745. .message_type = VMBUS_MSG_UNLOAD_RESPONSE,
  1746. };
  1747. qemu_mutex_lock(&vmbus->rx_queue_lock);
  1748. vmbus->rx_queue_size = 0;
  1749. qemu_mutex_unlock(&vmbus->rx_queue_lock);
  1750. post_msg(vmbus, &msg, sizeof(msg));
  1751. return;
  1752. }
  1753. static bool complete_unload(VMBus *vmbus)
  1754. {
  1755. vmbus_reset_all(vmbus);
  1756. return true;
  1757. }
  1758. static void process_message(VMBus *vmbus)
  1759. {
  1760. struct hyperv_post_message_input *hv_msg;
  1761. struct vmbus_message_header *msg;
  1762. void *msgdata;
  1763. uint32_t msglen;
  1764. qemu_mutex_lock(&vmbus->rx_queue_lock);
  1765. if (!vmbus->rx_queue_size) {
  1766. goto unlock;
  1767. }
  1768. hv_msg = &vmbus->rx_queue[vmbus->rx_queue_head];
  1769. msglen = hv_msg->payload_size;
  1770. if (msglen < sizeof(*msg)) {
  1771. goto out;
  1772. }
  1773. msgdata = hv_msg->payload;
  1774. msg = msgdata;
  1775. trace_vmbus_process_incoming_message(msg->message_type);
  1776. switch (msg->message_type) {
  1777. case VMBUS_MSG_INITIATE_CONTACT:
  1778. handle_initiate_contact(vmbus, msgdata, msglen);
  1779. break;
  1780. case VMBUS_MSG_REQUESTOFFERS:
  1781. handle_request_offers(vmbus, msgdata, msglen);
  1782. break;
  1783. case VMBUS_MSG_GPADL_HEADER:
  1784. handle_gpadl_header(vmbus, msgdata, msglen);
  1785. break;
  1786. case VMBUS_MSG_GPADL_BODY:
  1787. handle_gpadl_body(vmbus, msgdata, msglen);
  1788. break;
  1789. case VMBUS_MSG_GPADL_TEARDOWN:
  1790. handle_gpadl_teardown(vmbus, msgdata, msglen);
  1791. break;
  1792. case VMBUS_MSG_OPENCHANNEL:
  1793. handle_open_channel(vmbus, msgdata, msglen);
  1794. break;
  1795. case VMBUS_MSG_CLOSECHANNEL:
  1796. handle_close_channel(vmbus, msgdata, msglen);
  1797. break;
  1798. case VMBUS_MSG_UNLOAD:
  1799. handle_unload(vmbus, msgdata, msglen);
  1800. break;
  1801. default:
  1802. error_report("unknown message type %#x", msg->message_type);
  1803. break;
  1804. }
  1805. out:
  1806. vmbus->rx_queue_size--;
  1807. vmbus->rx_queue_head++;
  1808. vmbus->rx_queue_head %= HV_MSG_QUEUE_LEN;
  1809. vmbus_resched(vmbus);
  1810. unlock:
  1811. qemu_mutex_unlock(&vmbus->rx_queue_lock);
  1812. }
  1813. static const struct {
  1814. void (*run)(VMBus *vmbus);
  1815. bool (*complete)(VMBus *vmbus);
  1816. } state_runner[] = {
  1817. [VMBUS_LISTEN] = {process_message, NULL},
  1818. [VMBUS_HANDSHAKE] = {send_handshake, NULL},
  1819. [VMBUS_OFFER] = {send_offer, complete_offer},
  1820. [VMBUS_CREATE_GPADL] = {send_create_gpadl, complete_create_gpadl},
  1821. [VMBUS_TEARDOWN_GPADL] = {send_teardown_gpadl, complete_teardown_gpadl},
  1822. [VMBUS_OPEN_CHANNEL] = {send_open_channel, complete_open_channel},
  1823. [VMBUS_UNLOAD] = {send_unload, complete_unload},
  1824. };
  1825. static void vmbus_do_run(VMBus *vmbus)
  1826. {
  1827. if (vmbus->msg_in_progress) {
  1828. return;
  1829. }
  1830. assert(vmbus->state < VMBUS_STATE_MAX);
  1831. assert(state_runner[vmbus->state].run);
  1832. state_runner[vmbus->state].run(vmbus);
  1833. }
  1834. static void vmbus_run(void *opaque)
  1835. {
  1836. VMBus *vmbus = opaque;
  1837. /* make sure no recursion happens (e.g. due to recursive aio_poll()) */
  1838. if (vmbus->in_progress) {
  1839. return;
  1840. }
  1841. vmbus->in_progress = true;
  1842. /*
  1843. * FIXME: if vmbus_resched() is called from within vmbus_do_run(), it
  1844. * should go *after* the code that can result in aio_poll; otherwise
  1845. * reschedules can be missed. No idea how to enforce that.
  1846. */
  1847. vmbus_do_run(vmbus);
  1848. vmbus->in_progress = false;
  1849. }
  1850. static void vmbus_msg_cb(void *data, int status)
  1851. {
  1852. VMBus *vmbus = data;
  1853. bool (*complete)(VMBus *vmbus);
  1854. assert(vmbus->msg_in_progress);
  1855. trace_vmbus_msg_cb(status);
  1856. if (status == -EAGAIN) {
  1857. goto out;
  1858. }
  1859. if (status) {
  1860. error_report("message delivery fatal failure: %d; aborting vmbus",
  1861. status);
  1862. vmbus_reset_all(vmbus);
  1863. return;
  1864. }
  1865. assert(vmbus->state < VMBUS_STATE_MAX);
  1866. complete = state_runner[vmbus->state].complete;
  1867. if (!complete || complete(vmbus)) {
  1868. vmbus->state = VMBUS_LISTEN;
  1869. }
  1870. out:
  1871. vmbus->msg_in_progress = false;
  1872. vmbus_resched(vmbus);
  1873. }
  1874. static void vmbus_resched(VMBus *vmbus)
  1875. {
  1876. aio_bh_schedule_oneshot(qemu_get_aio_context(), vmbus_run, vmbus);
  1877. }
  1878. static void vmbus_signal_event(EventNotifier *e)
  1879. {
  1880. VMBusChannel *chan;
  1881. VMBus *vmbus = container_of(e, VMBus, notifier);
  1882. unsigned long *int_map;
  1883. hwaddr addr, len;
  1884. bool is_dirty = false;
  1885. if (!event_notifier_test_and_clear(e)) {
  1886. return;
  1887. }
  1888. trace_vmbus_signal_event();
  1889. if (!vmbus->int_page_gpa) {
  1890. return;
  1891. }
  1892. addr = vmbus->int_page_gpa + TARGET_PAGE_SIZE / 2;
  1893. len = TARGET_PAGE_SIZE / 2;
  1894. int_map = cpu_physical_memory_map(addr, &len, 1);
  1895. if (len != TARGET_PAGE_SIZE / 2) {
  1896. goto unmap;
  1897. }
  1898. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  1899. if (bitmap_test_and_clear_atomic(int_map, chan->id, 1)) {
  1900. if (!vmbus_channel_is_open(chan)) {
  1901. continue;
  1902. }
  1903. vmbus_channel_notify_host(chan);
  1904. is_dirty = true;
  1905. }
  1906. }
  1907. unmap:
  1908. cpu_physical_memory_unmap(int_map, len, 1, is_dirty);
  1909. }
  1910. static void vmbus_dev_realize(DeviceState *dev, Error **errp)
  1911. {
  1912. VMBusDevice *vdev = VMBUS_DEVICE(dev);
  1913. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(vdev);
  1914. VMBus *vmbus = VMBUS(qdev_get_parent_bus(dev));
  1915. BusChild *child;
  1916. Error *err = NULL;
  1917. char idstr[UUID_FMT_LEN + 1];
  1918. assert(!qemu_uuid_is_null(&vdev->instanceid));
  1919. if (!qemu_uuid_is_null(&vdc->instanceid)) {
  1920. /* Class wants to only have a single instance with a fixed UUID */
  1921. if (!qemu_uuid_is_equal(&vdev->instanceid, &vdc->instanceid)) {
  1922. error_setg(&err, "instance id can't be changed");
  1923. goto error_out;
  1924. }
  1925. }
  1926. /* Check for instance id collision for this class id */
  1927. QTAILQ_FOREACH(child, &BUS(vmbus)->children, sibling) {
  1928. VMBusDevice *child_dev = VMBUS_DEVICE(child->child);
  1929. if (child_dev == vdev) {
  1930. continue;
  1931. }
  1932. if (qemu_uuid_is_equal(&child_dev->instanceid, &vdev->instanceid)) {
  1933. qemu_uuid_unparse(&vdev->instanceid, idstr);
  1934. error_setg(&err, "duplicate vmbus device instance id %s", idstr);
  1935. goto error_out;
  1936. }
  1937. }
  1938. vdev->dma_as = &address_space_memory;
  1939. create_channels(vmbus, vdev, &err);
  1940. if (err) {
  1941. goto error_out;
  1942. }
  1943. if (vdc->vmdev_realize) {
  1944. vdc->vmdev_realize(vdev, &err);
  1945. if (err) {
  1946. goto err_vdc_realize;
  1947. }
  1948. }
  1949. return;
  1950. err_vdc_realize:
  1951. free_channels(vdev);
  1952. error_out:
  1953. error_propagate(errp, err);
  1954. }
  1955. static void vmbus_dev_reset(DeviceState *dev)
  1956. {
  1957. uint16_t i;
  1958. VMBusDevice *vdev = VMBUS_DEVICE(dev);
  1959. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(vdev);
  1960. if (vdev->channels) {
  1961. for (i = 0; i < vdev->num_channels; i++) {
  1962. VMBusChannel *chan = &vdev->channels[i];
  1963. close_channel(chan);
  1964. chan->state = VMCHAN_INIT;
  1965. }
  1966. }
  1967. if (vdc->vmdev_reset) {
  1968. vdc->vmdev_reset(vdev);
  1969. }
  1970. }
  1971. static void vmbus_dev_unrealize(DeviceState *dev)
  1972. {
  1973. VMBusDevice *vdev = VMBUS_DEVICE(dev);
  1974. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(vdev);
  1975. if (vdc->vmdev_unrealize) {
  1976. vdc->vmdev_unrealize(vdev);
  1977. }
  1978. free_channels(vdev);
  1979. }
  1980. static Property vmbus_dev_props[] = {
  1981. DEFINE_PROP_UUID("instanceid", VMBusDevice, instanceid),
  1982. DEFINE_PROP_END_OF_LIST()
  1983. };
  1984. static void vmbus_dev_class_init(ObjectClass *klass, void *data)
  1985. {
  1986. DeviceClass *kdev = DEVICE_CLASS(klass);
  1987. device_class_set_props(kdev, vmbus_dev_props);
  1988. kdev->bus_type = TYPE_VMBUS;
  1989. kdev->realize = vmbus_dev_realize;
  1990. kdev->unrealize = vmbus_dev_unrealize;
  1991. kdev->reset = vmbus_dev_reset;
  1992. }
  1993. static void vmbus_dev_instance_init(Object *obj)
  1994. {
  1995. VMBusDevice *vdev = VMBUS_DEVICE(obj);
  1996. VMBusDeviceClass *vdc = VMBUS_DEVICE_GET_CLASS(vdev);
  1997. if (!qemu_uuid_is_null(&vdc->instanceid)) {
  1998. /* Class wants to only have a single instance with a fixed UUID */
  1999. vdev->instanceid = vdc->instanceid;
  2000. }
  2001. }
  2002. const VMStateDescription vmstate_vmbus_dev = {
  2003. .name = TYPE_VMBUS_DEVICE,
  2004. .version_id = 0,
  2005. .minimum_version_id = 0,
  2006. .fields = (VMStateField[]) {
  2007. VMSTATE_UINT8_ARRAY(instanceid.data, VMBusDevice, 16),
  2008. VMSTATE_UINT16(num_channels, VMBusDevice),
  2009. VMSTATE_STRUCT_VARRAY_POINTER_UINT16(channels, VMBusDevice,
  2010. num_channels, vmstate_channel,
  2011. VMBusChannel),
  2012. VMSTATE_END_OF_LIST()
  2013. }
  2014. };
  2015. /* vmbus generic device base */
  2016. static const TypeInfo vmbus_dev_type_info = {
  2017. .name = TYPE_VMBUS_DEVICE,
  2018. .parent = TYPE_DEVICE,
  2019. .abstract = true,
  2020. .instance_size = sizeof(VMBusDevice),
  2021. .class_size = sizeof(VMBusDeviceClass),
  2022. .class_init = vmbus_dev_class_init,
  2023. .instance_init = vmbus_dev_instance_init,
  2024. };
  2025. static void vmbus_realize(BusState *bus, Error **errp)
  2026. {
  2027. int ret = 0;
  2028. VMBus *vmbus = VMBUS(bus);
  2029. qemu_mutex_init(&vmbus->rx_queue_lock);
  2030. QTAILQ_INIT(&vmbus->gpadl_list);
  2031. QTAILQ_INIT(&vmbus->channel_list);
  2032. ret = hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID,
  2033. vmbus_recv_message, vmbus);
  2034. if (ret != 0) {
  2035. error_setg(errp, "hyperv set message handler failed: %d", ret);
  2036. goto error_out;
  2037. }
  2038. ret = event_notifier_init(&vmbus->notifier, 0);
  2039. if (ret != 0) {
  2040. error_setg(errp, "event notifier failed to init with %d", ret);
  2041. goto remove_msg_handler;
  2042. }
  2043. event_notifier_set_handler(&vmbus->notifier, vmbus_signal_event);
  2044. ret = hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID,
  2045. &vmbus->notifier);
  2046. if (ret != 0) {
  2047. error_setg(errp, "hyperv set event handler failed with %d", ret);
  2048. goto clear_event_notifier;
  2049. }
  2050. return;
  2051. clear_event_notifier:
  2052. event_notifier_cleanup(&vmbus->notifier);
  2053. remove_msg_handler:
  2054. hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID, NULL, NULL);
  2055. error_out:
  2056. qemu_mutex_destroy(&vmbus->rx_queue_lock);
  2057. }
  2058. static void vmbus_unrealize(BusState *bus)
  2059. {
  2060. VMBus *vmbus = VMBUS(bus);
  2061. hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID, NULL, NULL);
  2062. hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID, NULL);
  2063. event_notifier_cleanup(&vmbus->notifier);
  2064. qemu_mutex_destroy(&vmbus->rx_queue_lock);
  2065. }
  2066. static void vmbus_reset(BusState *bus)
  2067. {
  2068. vmbus_deinit(VMBUS(bus));
  2069. }
  2070. static char *vmbus_get_dev_path(DeviceState *dev)
  2071. {
  2072. BusState *bus = qdev_get_parent_bus(dev);
  2073. return qdev_get_dev_path(bus->parent);
  2074. }
  2075. static char *vmbus_get_fw_dev_path(DeviceState *dev)
  2076. {
  2077. VMBusDevice *vdev = VMBUS_DEVICE(dev);
  2078. char uuid[UUID_FMT_LEN + 1];
  2079. qemu_uuid_unparse(&vdev->instanceid, uuid);
  2080. return g_strdup_printf("%s@%s", qdev_fw_name(dev), uuid);
  2081. }
  2082. static void vmbus_class_init(ObjectClass *klass, void *data)
  2083. {
  2084. BusClass *k = BUS_CLASS(klass);
  2085. k->get_dev_path = vmbus_get_dev_path;
  2086. k->get_fw_dev_path = vmbus_get_fw_dev_path;
  2087. k->realize = vmbus_realize;
  2088. k->unrealize = vmbus_unrealize;
  2089. k->reset = vmbus_reset;
  2090. }
  2091. static int vmbus_pre_load(void *opaque)
  2092. {
  2093. VMBusChannel *chan;
  2094. VMBus *vmbus = VMBUS(opaque);
  2095. /*
  2096. * channel IDs allocated by the source will come in the migration stream
  2097. * for each channel, so clean up the ones allocated at realize
  2098. */
  2099. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  2100. unregister_chan_id(chan);
  2101. }
  2102. return 0;
  2103. }
  2104. static int vmbus_post_load(void *opaque, int version_id)
  2105. {
  2106. int ret;
  2107. VMBus *vmbus = VMBUS(opaque);
  2108. VMBusGpadl *gpadl;
  2109. VMBusChannel *chan;
  2110. ret = vmbus_init(vmbus);
  2111. if (ret) {
  2112. return ret;
  2113. }
  2114. QTAILQ_FOREACH(gpadl, &vmbus->gpadl_list, link) {
  2115. gpadl->vmbus = vmbus;
  2116. gpadl->refcount = 1;
  2117. }
  2118. /*
  2119. * reopening channels depends on initialized vmbus so it's done here
  2120. * instead of channel_post_load()
  2121. */
  2122. QTAILQ_FOREACH(chan, &vmbus->channel_list, link) {
  2123. if (chan->state == VMCHAN_OPENING || chan->state == VMCHAN_OPEN) {
  2124. open_channel(chan);
  2125. }
  2126. if (chan->state != VMCHAN_OPEN) {
  2127. continue;
  2128. }
  2129. if (!vmbus_channel_is_open(chan)) {
  2130. /* reopen failed, abort loading */
  2131. return -1;
  2132. }
  2133. /* resume processing on the guest side if it missed the notification */
  2134. hyperv_sint_route_set_sint(chan->notify_route);
  2135. /* ditto on the host side */
  2136. vmbus_channel_notify_host(chan);
  2137. }
  2138. vmbus_resched(vmbus);
  2139. return 0;
  2140. }
  2141. static const VMStateDescription vmstate_post_message_input = {
  2142. .name = "vmbus/hyperv_post_message_input",
  2143. .version_id = 0,
  2144. .minimum_version_id = 0,
  2145. .fields = (VMStateField[]) {
  2146. /*
  2147. * skip connection_id and message_type as they are validated before
  2148. * queueing and ignored on dequeueing
  2149. */
  2150. VMSTATE_UINT32(payload_size, struct hyperv_post_message_input),
  2151. VMSTATE_UINT8_ARRAY(payload, struct hyperv_post_message_input,
  2152. HV_MESSAGE_PAYLOAD_SIZE),
  2153. VMSTATE_END_OF_LIST()
  2154. }
  2155. };
  2156. static bool vmbus_rx_queue_needed(void *opaque)
  2157. {
  2158. VMBus *vmbus = VMBUS(opaque);
  2159. return vmbus->rx_queue_size;
  2160. }
  2161. static const VMStateDescription vmstate_rx_queue = {
  2162. .name = "vmbus/rx_queue",
  2163. .version_id = 0,
  2164. .minimum_version_id = 0,
  2165. .needed = vmbus_rx_queue_needed,
  2166. .fields = (VMStateField[]) {
  2167. VMSTATE_UINT8(rx_queue_head, VMBus),
  2168. VMSTATE_UINT8(rx_queue_size, VMBus),
  2169. VMSTATE_STRUCT_ARRAY(rx_queue, VMBus,
  2170. HV_MSG_QUEUE_LEN, 0,
  2171. vmstate_post_message_input,
  2172. struct hyperv_post_message_input),
  2173. VMSTATE_END_OF_LIST()
  2174. }
  2175. };
  2176. static const VMStateDescription vmstate_vmbus = {
  2177. .name = TYPE_VMBUS,
  2178. .version_id = 0,
  2179. .minimum_version_id = 0,
  2180. .pre_load = vmbus_pre_load,
  2181. .post_load = vmbus_post_load,
  2182. .fields = (VMStateField[]) {
  2183. VMSTATE_UINT8(state, VMBus),
  2184. VMSTATE_UINT32(version, VMBus),
  2185. VMSTATE_UINT32(target_vp, VMBus),
  2186. VMSTATE_UINT64(int_page_gpa, VMBus),
  2187. VMSTATE_QTAILQ_V(gpadl_list, VMBus, 0,
  2188. vmstate_gpadl, VMBusGpadl, link),
  2189. VMSTATE_END_OF_LIST()
  2190. },
  2191. .subsections = (const VMStateDescription * []) {
  2192. &vmstate_rx_queue,
  2193. NULL
  2194. }
  2195. };
  2196. static const TypeInfo vmbus_type_info = {
  2197. .name = TYPE_VMBUS,
  2198. .parent = TYPE_BUS,
  2199. .instance_size = sizeof(VMBus),
  2200. .class_init = vmbus_class_init,
  2201. };
  2202. static void vmbus_bridge_realize(DeviceState *dev, Error **errp)
  2203. {
  2204. VMBusBridge *bridge = VMBUS_BRIDGE(dev);
  2205. /*
  2206. * here there's at least one vmbus bridge that is being realized, so
  2207. * vmbus_bridge_find can only return NULL if it's not unique
  2208. */
  2209. if (!vmbus_bridge_find()) {
  2210. error_setg(errp, "there can be at most one %s in the system",
  2211. TYPE_VMBUS_BRIDGE);
  2212. return;
  2213. }
  2214. if (!hyperv_is_synic_enabled()) {
  2215. error_report("VMBus requires usable Hyper-V SynIC and VP_INDEX");
  2216. return;
  2217. }
  2218. bridge->bus = VMBUS(qbus_new(TYPE_VMBUS, dev, "vmbus"));
  2219. }
  2220. static char *vmbus_bridge_ofw_unit_address(const SysBusDevice *dev)
  2221. {
  2222. /* there can be only one VMBus */
  2223. return g_strdup("0");
  2224. }
  2225. static const VMStateDescription vmstate_vmbus_bridge = {
  2226. .name = TYPE_VMBUS_BRIDGE,
  2227. .version_id = 0,
  2228. .minimum_version_id = 0,
  2229. .fields = (VMStateField[]) {
  2230. VMSTATE_STRUCT_POINTER(bus, VMBusBridge, vmstate_vmbus, VMBus),
  2231. VMSTATE_END_OF_LIST()
  2232. },
  2233. };
  2234. static Property vmbus_bridge_props[] = {
  2235. DEFINE_PROP_UINT8("irq", VMBusBridge, irq, 7),
  2236. DEFINE_PROP_END_OF_LIST()
  2237. };
  2238. static void vmbus_bridge_class_init(ObjectClass *klass, void *data)
  2239. {
  2240. DeviceClass *k = DEVICE_CLASS(klass);
  2241. SysBusDeviceClass *sk = SYS_BUS_DEVICE_CLASS(klass);
  2242. k->realize = vmbus_bridge_realize;
  2243. k->fw_name = "vmbus";
  2244. sk->explicit_ofw_unit_address = vmbus_bridge_ofw_unit_address;
  2245. set_bit(DEVICE_CATEGORY_BRIDGE, k->categories);
  2246. k->vmsd = &vmstate_vmbus_bridge;
  2247. device_class_set_props(k, vmbus_bridge_props);
  2248. /* override SysBusDevice's default */
  2249. k->user_creatable = true;
  2250. }
  2251. static const TypeInfo vmbus_bridge_type_info = {
  2252. .name = TYPE_VMBUS_BRIDGE,
  2253. .parent = TYPE_SYS_BUS_DEVICE,
  2254. .instance_size = sizeof(VMBusBridge),
  2255. .class_init = vmbus_bridge_class_init,
  2256. };
  2257. static void vmbus_register_types(void)
  2258. {
  2259. type_register_static(&vmbus_bridge_type_info);
  2260. type_register_static(&vmbus_dev_type_info);
  2261. type_register_static(&vmbus_type_info);
  2262. }
  2263. type_init(vmbus_register_types)