You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vmbus.c 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Hyper-V virtual machine bus
  27. *
  28. */
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <assert.h>
  35. #include <byteswap.h>
  36. #include <ipxe/nap.h>
  37. #include <ipxe/malloc.h>
  38. #include <ipxe/iobuf.h>
  39. #include <ipxe/bitops.h>
  40. #include <ipxe/hyperv.h>
  41. #include <ipxe/vmbus.h>
  42. /** VMBus initial GPADL ID
  43. *
  44. * This is an opaque value with no meaning. The Linux kernel uses
  45. * 0xe1e10.
  46. */
  47. #define VMBUS_GPADL_MAGIC 0x18ae0000
  48. /** Current (i.e. most recently issued) GPADL ID */
  49. static unsigned int vmbus_gpadl = VMBUS_GPADL_MAGIC;
  50. /** Obsolete GPADL ID threshold
  51. *
  52. * When the Hyper-V connection is reset, any previous GPADLs are
  53. * automatically rendered obsolete.
  54. */
  55. unsigned int vmbus_obsolete_gpadl;
  56. /**
  57. * Post message
  58. *
  59. * @v hv Hyper-V hypervisor
  60. * @v header Message header
  61. * @v len Length of message (including header)
  62. * @ret rc Return status code
  63. */
  64. static int vmbus_post_message ( struct hv_hypervisor *hv,
  65. const struct vmbus_message_header *header,
  66. size_t len ) {
  67. struct vmbus *vmbus = hv->vmbus;
  68. int rc;
  69. /* Post message */
  70. if ( ( rc = hv_post_message ( hv, VMBUS_MESSAGE_ID, VMBUS_MESSAGE_TYPE,
  71. header, len ) ) != 0 ) {
  72. DBGC ( vmbus, "VMBUS %p could not post message: %s\n",
  73. vmbus, strerror ( rc ) );
  74. return rc;
  75. }
  76. return 0;
  77. }
  78. /**
  79. * Post empty message
  80. *
  81. * @v hv Hyper-V hypervisor
  82. * @v type Message type
  83. * @ret rc Return status code
  84. */
  85. static int vmbus_post_empty_message ( struct hv_hypervisor *hv,
  86. unsigned int type ) {
  87. struct vmbus_message_header header = { .type = cpu_to_le32 ( type ) };
  88. return vmbus_post_message ( hv, &header, sizeof ( header ) );
  89. }
  90. /**
  91. * Wait for received message of any type
  92. *
  93. * @v hv Hyper-V hypervisor
  94. * @ret rc Return status code
  95. */
  96. static int vmbus_wait_for_any_message ( struct hv_hypervisor *hv ) {
  97. struct vmbus *vmbus = hv->vmbus;
  98. int rc;
  99. /* Wait for message */
  100. if ( ( rc = hv_wait_for_message ( hv, VMBUS_MESSAGE_SINT ) ) != 0 ) {
  101. DBGC ( vmbus, "VMBUS %p failed waiting for message: %s\n",
  102. vmbus, strerror ( rc ) );
  103. return rc;
  104. }
  105. /* Sanity check */
  106. if ( hv->message->received.type != cpu_to_le32 ( VMBUS_MESSAGE_TYPE ) ){
  107. DBGC ( vmbus, "VMBUS %p invalid message type %d\n",
  108. vmbus, le32_to_cpu ( hv->message->received.type ) );
  109. return -EINVAL;
  110. }
  111. return 0;
  112. }
  113. /**
  114. * Wait for received message of a specified type, ignoring any others
  115. *
  116. * @v hv Hyper-V hypervisor
  117. * @v type Message type
  118. * @ret rc Return status code
  119. */
  120. static int vmbus_wait_for_message ( struct hv_hypervisor *hv,
  121. unsigned int type ) {
  122. struct vmbus *vmbus = hv->vmbus;
  123. const struct vmbus_message_header *header = &vmbus->message->header;
  124. int rc;
  125. /* Loop until specified message arrives, or until an error occurs */
  126. while ( 1 ) {
  127. /* Wait for message */
  128. if ( ( rc = vmbus_wait_for_any_message ( hv ) ) != 0 )
  129. return rc;
  130. /* Check for requested message type */
  131. if ( header->type == cpu_to_le32 ( type ) )
  132. return 0;
  133. /* Ignore any other messages (e.g. due to additional
  134. * channels being offered at runtime).
  135. */
  136. DBGC ( vmbus, "VMBUS %p ignoring message type %d (expecting "
  137. "%d)\n", vmbus, le32_to_cpu ( header->type ), type );
  138. }
  139. }
  140. /**
  141. * Initiate contact
  142. *
  143. * @v hv Hyper-V hypervisor
  144. * @v raw VMBus protocol (raw) version
  145. * @ret rc Return status code
  146. */
  147. static int vmbus_initiate_contact ( struct hv_hypervisor *hv,
  148. unsigned int raw ) {
  149. struct vmbus *vmbus = hv->vmbus;
  150. const struct vmbus_version_response *version = &vmbus->message->version;
  151. struct vmbus_initiate_contact initiate;
  152. int rc;
  153. /* Construct message */
  154. memset ( &initiate, 0, sizeof ( initiate ) );
  155. initiate.header.type = cpu_to_le32 ( VMBUS_INITIATE_CONTACT );
  156. initiate.version.raw = cpu_to_le32 ( raw );
  157. initiate.intr = virt_to_phys ( vmbus->intr );
  158. initiate.monitor_in = virt_to_phys ( vmbus->monitor_in );
  159. initiate.monitor_out = virt_to_phys ( vmbus->monitor_out );
  160. /* Post message */
  161. if ( ( rc = vmbus_post_message ( hv, &initiate.header,
  162. sizeof ( initiate ) ) ) != 0 )
  163. return rc;
  164. /* Wait for response */
  165. if ( ( rc = vmbus_wait_for_message ( hv, VMBUS_VERSION_RESPONSE ) ) !=0)
  166. return rc;
  167. /* Check response */
  168. if ( ! version->supported ) {
  169. DBGC ( vmbus, "VMBUS %p requested version not supported\n",
  170. vmbus );
  171. return -ENOTSUP;
  172. }
  173. if ( version->version.raw != cpu_to_le32 ( raw ) ) {
  174. DBGC ( vmbus, "VMBUS %p unexpected version %d.%d\n",
  175. vmbus, le16_to_cpu ( version->version.major ),
  176. le16_to_cpu ( version->version.minor ) );
  177. return -EPROTO;
  178. }
  179. DBGC ( vmbus, "VMBUS %p initiated contact using version %d.%d\n",
  180. vmbus, le16_to_cpu ( version->version.major ),
  181. le16_to_cpu ( version->version.minor ) );
  182. return 0;
  183. }
  184. /**
  185. * Terminate contact
  186. *
  187. * @v hv Hyper-V hypervisor
  188. * @ret rc Return status code
  189. */
  190. static int vmbus_unload ( struct hv_hypervisor *hv ) {
  191. int rc;
  192. /* Post message */
  193. if ( ( rc = vmbus_post_empty_message ( hv, VMBUS_UNLOAD ) ) != 0 )
  194. return rc;
  195. /* Wait for response */
  196. if ( ( rc = vmbus_wait_for_message ( hv, VMBUS_UNLOAD_RESPONSE ) ) != 0)
  197. return rc;
  198. return 0;
  199. }
  200. /**
  201. * Negotiate protocol version
  202. *
  203. * @v hv Hyper-V hypervisor
  204. * @ret rc Return status code
  205. */
  206. static int vmbus_negotiate_version ( struct hv_hypervisor *hv ) {
  207. int rc;
  208. /* We require the ability to disconnect from and reconnect to
  209. * VMBus; if we don't have this then there is no (viable) way
  210. * for a loaded operating system to continue to use any VMBus
  211. * devices. (There is also a small but non-zero risk that the
  212. * host will continue to write to our interrupt and monitor
  213. * pages, since the VMBUS_UNLOAD message in earlier versions
  214. * is essentially a no-op.)
  215. *
  216. * This requires us to ensure that the host supports protocol
  217. * version 3.0 (VMBUS_VERSION_WIN8_1). However, we can't
  218. * actually _use_ protocol version 3.0, since doing so causes
  219. * an iSCSI-booted Windows Server 2012 R2 VM to crash due to a
  220. * NULL pointer dereference in vmbus.sys.
  221. *
  222. * To work around this problem, we first ensure that we can
  223. * connect using protocol v3.0, then disconnect and reconnect
  224. * using the oldest known protocol.
  225. */
  226. /* Initiate contact to check for required protocol support */
  227. if ( ( rc = vmbus_initiate_contact ( hv, VMBUS_VERSION_WIN8_1 ) ) != 0 )
  228. return rc;
  229. /* Terminate contact */
  230. if ( ( rc = vmbus_unload ( hv ) ) != 0 )
  231. return rc;
  232. /* Reinitiate contact using the oldest known protocol version */
  233. if ( ( rc = vmbus_initiate_contact ( hv, VMBUS_VERSION_WS2008 ) ) != 0 )
  234. return rc;
  235. return 0;
  236. }
  237. /**
  238. * Establish GPA descriptor list
  239. *
  240. * @v vmdev VMBus device
  241. * @v data Data buffer
  242. * @v len Length of data buffer
  243. * @ret gpadl GPADL ID, or negative error
  244. */
  245. int vmbus_establish_gpadl ( struct vmbus_device *vmdev, userptr_t data,
  246. size_t len ) {
  247. struct hv_hypervisor *hv = vmdev->hv;
  248. struct vmbus *vmbus = hv->vmbus;
  249. physaddr_t addr = user_to_phys ( data, 0 );
  250. unsigned int pfn_count = hv_pfn_count ( addr, len );
  251. struct {
  252. struct vmbus_gpadl_header gpadlhdr;
  253. struct vmbus_gpa_range range;
  254. uint64_t pfn[pfn_count];
  255. } __attribute__ (( packed )) gpadlhdr;
  256. const struct vmbus_gpadl_created *created = &vmbus->message->created;
  257. unsigned int gpadl;
  258. unsigned int i;
  259. int rc;
  260. /* Allocate GPADL ID */
  261. gpadl = ++vmbus_gpadl;
  262. /* Construct message */
  263. memset ( &gpadlhdr, 0, sizeof ( gpadlhdr ) );
  264. gpadlhdr.gpadlhdr.header.type = cpu_to_le32 ( VMBUS_GPADL_HEADER );
  265. gpadlhdr.gpadlhdr.channel = cpu_to_le32 ( vmdev->channel );
  266. gpadlhdr.gpadlhdr.gpadl = cpu_to_le32 ( gpadl );
  267. gpadlhdr.gpadlhdr.range_len =
  268. cpu_to_le16 ( ( sizeof ( gpadlhdr.range ) +
  269. sizeof ( gpadlhdr.pfn ) ) );
  270. gpadlhdr.gpadlhdr.range_count = cpu_to_le16 ( 1 );
  271. gpadlhdr.range.len = cpu_to_le32 ( len );
  272. gpadlhdr.range.offset = cpu_to_le32 ( addr & ( PAGE_SIZE - 1 ) );
  273. for ( i = 0 ; i < pfn_count ; i++ )
  274. gpadlhdr.pfn[i] = ( ( addr / PAGE_SIZE ) + i );
  275. /* Post message */
  276. if ( ( rc = vmbus_post_message ( hv, &gpadlhdr.gpadlhdr.header,
  277. sizeof ( gpadlhdr ) ) ) != 0 )
  278. return rc;
  279. /* Wait for response */
  280. if ( ( rc = vmbus_wait_for_message ( hv, VMBUS_GPADL_CREATED ) ) != 0 )
  281. return rc;
  282. /* Check response */
  283. if ( created->channel != cpu_to_le32 ( vmdev->channel ) ) {
  284. DBGC ( vmdev, "VMBUS %s unexpected GPADL channel %d\n",
  285. vmdev->dev.name, le32_to_cpu ( created->channel ) );
  286. return -EPROTO;
  287. }
  288. if ( created->gpadl != cpu_to_le32 ( gpadl ) ) {
  289. DBGC ( vmdev, "VMBUS %s unexpected GPADL ID %#08x\n",
  290. vmdev->dev.name, le32_to_cpu ( created->gpadl ) );
  291. return -EPROTO;
  292. }
  293. if ( created->status != 0 ) {
  294. DBGC ( vmdev, "VMBUS %s GPADL creation failed: %#08x\n",
  295. vmdev->dev.name, le32_to_cpu ( created->status ) );
  296. return -EPROTO;
  297. }
  298. DBGC ( vmdev, "VMBUS %s GPADL %#08x is [%08lx,%08lx)\n",
  299. vmdev->dev.name, gpadl, addr, ( addr + len ) );
  300. return gpadl;
  301. }
  302. /**
  303. * Tear down GPA descriptor list
  304. *
  305. * @v vmdev VMBus device
  306. * @v gpadl GPADL ID
  307. * @ret rc Return status code
  308. */
  309. int vmbus_gpadl_teardown ( struct vmbus_device *vmdev, unsigned int gpadl ) {
  310. struct hv_hypervisor *hv = vmdev->hv;
  311. struct vmbus *vmbus = hv->vmbus;
  312. struct vmbus_gpadl_teardown teardown;
  313. const struct vmbus_gpadl_torndown *torndown = &vmbus->message->torndown;
  314. int rc;
  315. /* If GPADL is obsolete (i.e. was created before the most
  316. * recent Hyper-V reset), then we will never receive a
  317. * response to the teardown message. Since the GPADL is
  318. * already destroyed as far as the hypervisor is concerned, no
  319. * further action is required.
  320. */
  321. if ( vmbus_gpadl_is_obsolete ( gpadl ) )
  322. return 0;
  323. /* Construct message */
  324. memset ( &teardown, 0, sizeof ( teardown ) );
  325. teardown.header.type = cpu_to_le32 ( VMBUS_GPADL_TEARDOWN );
  326. teardown.channel = cpu_to_le32 ( vmdev->channel );
  327. teardown.gpadl = cpu_to_le32 ( gpadl );
  328. /* Post message */
  329. if ( ( rc = vmbus_post_message ( hv, &teardown.header,
  330. sizeof ( teardown ) ) ) != 0 )
  331. return rc;
  332. /* Wait for response */
  333. if ( ( rc = vmbus_wait_for_message ( hv, VMBUS_GPADL_TORNDOWN ) ) != 0 )
  334. return rc;
  335. /* Check response */
  336. if ( torndown->gpadl != cpu_to_le32 ( gpadl ) ) {
  337. DBGC ( vmdev, "VMBUS %s unexpected GPADL ID %#08x\n",
  338. vmdev->dev.name, le32_to_cpu ( torndown->gpadl ) );
  339. return -EPROTO;
  340. }
  341. return 0;
  342. }
  343. /**
  344. * Open VMBus channel
  345. *
  346. * @v vmdev VMBus device
  347. * @v op Channel operations
  348. * @v out_len Outbound ring buffer length
  349. * @v in_len Inbound ring buffer length
  350. * @v mtu Maximum expected data packet length (including headers)
  351. * @ret rc Return status code
  352. *
  353. * Both outbound and inbound ring buffer lengths must be a power of
  354. * two and a multiple of PAGE_SIZE. The requirement to be a power of
  355. * two is a policy decision taken to simplify the ring buffer indexing
  356. * logic.
  357. */
  358. int vmbus_open ( struct vmbus_device *vmdev,
  359. struct vmbus_channel_operations *op,
  360. size_t out_len, size_t in_len, size_t mtu ) {
  361. struct hv_hypervisor *hv = vmdev->hv;
  362. struct vmbus *vmbus = hv->vmbus;
  363. struct vmbus_open_channel open;
  364. const struct vmbus_open_channel_result *opened =
  365. &vmbus->message->opened;
  366. size_t len;
  367. void *ring;
  368. void *packet;
  369. int gpadl;
  370. uint32_t open_id;
  371. int rc;
  372. /* Sanity checks */
  373. assert ( ( out_len % PAGE_SIZE ) == 0 );
  374. assert ( ( out_len & ( out_len - 1 ) ) == 0 );
  375. assert ( ( in_len % PAGE_SIZE ) == 0 );
  376. assert ( ( in_len & ( in_len - 1 ) ) == 0 );
  377. assert ( mtu >= ( sizeof ( struct vmbus_packet_header ) +
  378. sizeof ( struct vmbus_packet_footer ) ) );
  379. /* Allocate packet buffer */
  380. packet = malloc ( mtu );
  381. if ( ! packet ) {
  382. rc = -ENOMEM;
  383. goto err_alloc_packet;
  384. }
  385. /* Allocate ring buffer */
  386. len = ( sizeof ( *vmdev->out ) + out_len +
  387. sizeof ( *vmdev->in ) + in_len );
  388. assert ( ( len % PAGE_SIZE ) == 0 );
  389. ring = malloc_dma ( len, PAGE_SIZE );
  390. if ( ! ring ) {
  391. rc = -ENOMEM;
  392. goto err_alloc_ring;
  393. }
  394. memset ( ring, 0, len );
  395. /* Establish GPADL for ring buffer */
  396. gpadl = vmbus_establish_gpadl ( vmdev, virt_to_user ( ring ), len );
  397. if ( gpadl < 0 ) {
  398. rc = gpadl;
  399. goto err_establish;
  400. }
  401. /* Construct message */
  402. memset ( &open, 0, sizeof ( open ) );
  403. open.header.type = cpu_to_le32 ( VMBUS_OPEN_CHANNEL );
  404. open.channel = cpu_to_le32 ( vmdev->channel );
  405. open_id = random();
  406. open.id = open_id; /* Opaque random value: endianness irrelevant */
  407. open.gpadl = cpu_to_le32 ( gpadl );
  408. open.out_pages = ( ( sizeof ( *vmdev->out ) / PAGE_SIZE ) +
  409. ( out_len / PAGE_SIZE ) );
  410. /* Post message */
  411. if ( ( rc = vmbus_post_message ( hv, &open.header,
  412. sizeof ( open ) ) ) != 0 )
  413. goto err_post_message;
  414. /* Wait for response */
  415. if ( ( rc = vmbus_wait_for_message ( hv,
  416. VMBUS_OPEN_CHANNEL_RESULT ) ) != 0)
  417. goto err_wait_for_message;
  418. /* Check response */
  419. if ( opened->channel != cpu_to_le32 ( vmdev->channel ) ) {
  420. DBGC ( vmdev, "VMBUS %s unexpected opened channel %#08x\n",
  421. vmdev->dev.name, le32_to_cpu ( opened->channel ) );
  422. rc = -EPROTO;
  423. goto err_check_response;
  424. }
  425. if ( opened->id != open_id /* Non-endian */ ) {
  426. DBGC ( vmdev, "VMBUS %s unexpected open ID %#08x\n",
  427. vmdev->dev.name, le32_to_cpu ( opened->id ) );
  428. rc = -EPROTO;
  429. goto err_check_response;
  430. }
  431. if ( opened->status != 0 ) {
  432. DBGC ( vmdev, "VMBUS %s open failed: %#08x\n",
  433. vmdev->dev.name, le32_to_cpu ( opened->status ) );
  434. rc = -EPROTO;
  435. goto err_check_response;
  436. }
  437. /* Store channel parameters */
  438. vmdev->out_len = out_len;
  439. vmdev->in_len = in_len;
  440. vmdev->out = ring;
  441. vmdev->in = ( ring + sizeof ( *vmdev->out ) + out_len );
  442. vmdev->gpadl = gpadl;
  443. vmdev->op = op;
  444. vmdev->mtu = mtu;
  445. vmdev->packet = packet;
  446. DBGC ( vmdev, "VMBUS %s channel GPADL %#08x ring "
  447. "[%#08lx,%#08lx,%#08lx)\n", vmdev->dev.name, vmdev->gpadl,
  448. virt_to_phys ( vmdev->out ), virt_to_phys ( vmdev->in ),
  449. ( virt_to_phys ( vmdev->out ) + len ) );
  450. return 0;
  451. err_check_response:
  452. err_wait_for_message:
  453. err_post_message:
  454. vmbus_gpadl_teardown ( vmdev, vmdev->gpadl );
  455. err_establish:
  456. free_dma ( ring, len );
  457. err_alloc_ring:
  458. free ( packet );
  459. err_alloc_packet:
  460. return rc;
  461. }
  462. /**
  463. * Close VMBus channel
  464. *
  465. * @v vmdev VMBus device
  466. */
  467. void vmbus_close ( struct vmbus_device *vmdev ) {
  468. struct hv_hypervisor *hv = vmdev->hv;
  469. struct vmbus_close_channel close;
  470. size_t len;
  471. int rc;
  472. /* Construct message */
  473. memset ( &close, 0, sizeof ( close ) );
  474. close.header.type = cpu_to_le32 ( VMBUS_CLOSE_CHANNEL );
  475. close.channel = cpu_to_le32 ( vmdev->channel );
  476. /* Post message */
  477. if ( ( rc = vmbus_post_message ( hv, &close.header,
  478. sizeof ( close ) ) ) != 0 ) {
  479. DBGC ( vmdev, "VMBUS %s failed to close: %s\n",
  480. vmdev->dev.name, strerror ( rc ) );
  481. /* Continue to attempt to tear down GPADL, so that our
  482. * memory is no longer accessible by the remote VM.
  483. */
  484. }
  485. /* Tear down GPADL */
  486. if ( ( rc = vmbus_gpadl_teardown ( vmdev, vmdev->gpadl ) ) != 0 ) {
  487. DBGC ( vmdev, "VMBUS %s failed to tear down channel GPADL: "
  488. "%s\n", vmdev->dev.name, strerror ( rc ) );
  489. /* We can't prevent the remote VM from continuing to
  490. * access this memory, so leak it.
  491. */
  492. return;
  493. }
  494. /* Free ring buffer */
  495. len = ( sizeof ( *vmdev->out ) + vmdev->out_len +
  496. sizeof ( *vmdev->in ) + vmdev->in_len );
  497. free_dma ( vmdev->out, len );
  498. vmdev->out = NULL;
  499. vmdev->in = NULL;
  500. /* Free packet buffer */
  501. free ( vmdev->packet );
  502. vmdev->packet = NULL;
  503. DBGC ( vmdev, "VMBUS %s closed\n", vmdev->dev.name );
  504. }
  505. /**
  506. * Signal channel via monitor page
  507. *
  508. * @v vmdev VMBus device
  509. */
  510. static void vmbus_signal_monitor ( struct vmbus_device *vmdev ) {
  511. struct hv_hypervisor *hv = vmdev->hv;
  512. struct vmbus *vmbus = hv->vmbus;
  513. struct hv_monitor_trigger *trigger;
  514. unsigned int group;
  515. unsigned int bit;
  516. /* Set bit in monitor trigger group */
  517. group = ( vmdev->monitor / ( 8 * sizeof ( trigger->pending ) ));
  518. bit = ( vmdev->monitor % ( 8 * sizeof ( trigger->pending ) ) );
  519. trigger = &vmbus->monitor_out->trigger[group];
  520. set_bit ( bit, trigger );
  521. }
  522. /**
  523. * Signal channel via hypervisor event
  524. *
  525. * @v vmdev VMBus device
  526. */
  527. static void vmbus_signal_event ( struct vmbus_device *vmdev ) {
  528. struct hv_hypervisor *hv = vmdev->hv;
  529. int rc;
  530. /* Signal hypervisor event */
  531. if ( ( rc = hv_signal_event ( hv, VMBUS_EVENT_ID, 0 ) ) != 0 ) {
  532. DBGC ( vmdev, "VMBUS %s could not signal event: %s\n",
  533. vmdev->dev.name, strerror ( rc ) );
  534. return;
  535. }
  536. }
  537. /**
  538. * Fill outbound ring buffer
  539. *
  540. * @v vmdev VMBus device
  541. * @v prod Producer index
  542. * @v data Data
  543. * @v len Length
  544. * @ret prod New producer index
  545. *
  546. * The caller must ensure that there is sufficient space in the ring
  547. * buffer.
  548. */
  549. static size_t vmbus_produce ( struct vmbus_device *vmdev, size_t prod,
  550. const void *data, size_t len ) {
  551. size_t first;
  552. size_t second;
  553. /* Determine fragment lengths */
  554. first = ( vmdev->out_len - prod );
  555. if ( first > len )
  556. first = len;
  557. second = ( len - first );
  558. /* Copy fragment(s) */
  559. memcpy ( &vmdev->out->data[prod], data, first );
  560. if ( second )
  561. memcpy ( &vmdev->out->data[0], ( data + first ), second );
  562. return ( ( prod + len ) & ( vmdev->out_len - 1 ) );
  563. }
  564. /**
  565. * Consume inbound ring buffer
  566. *
  567. * @v vmdev VMBus device
  568. * @v cons Consumer index
  569. * @v data Data buffer, or NULL
  570. * @v len Length to consume
  571. * @ret cons New consumer index
  572. */
  573. static size_t vmbus_consume ( struct vmbus_device *vmdev, size_t cons,
  574. void *data, size_t len ) {
  575. size_t first;
  576. size_t second;
  577. /* Determine fragment lengths */
  578. first = ( vmdev->in_len - cons );
  579. if ( first > len )
  580. first = len;
  581. second = ( len - first );
  582. /* Copy fragment(s) */
  583. memcpy ( data, &vmdev->in->data[cons], first );
  584. if ( second )
  585. memcpy ( ( data + first ), &vmdev->in->data[0], second );
  586. return ( ( cons + len ) & ( vmdev->in_len - 1 ) );
  587. }
  588. /**
  589. * Send packet via ring buffer
  590. *
  591. * @v vmdev VMBus device
  592. * @v header Packet header
  593. * @v data Data
  594. * @v len Length of data
  595. * @ret rc Return status code
  596. *
  597. * Send a packet via the outbound ring buffer. All fields in the
  598. * packet header must be filled in, with the exception of the total
  599. * packet length.
  600. */
  601. static int vmbus_send ( struct vmbus_device *vmdev,
  602. struct vmbus_packet_header *header,
  603. const void *data, size_t len ) {
  604. struct hv_hypervisor *hv = vmdev->hv;
  605. struct vmbus *vmbus = hv->vmbus;
  606. static uint8_t padding[ 8 - 1 ];
  607. struct vmbus_packet_footer footer;
  608. size_t header_len;
  609. size_t pad_len;
  610. size_t footer_len;
  611. size_t ring_len;
  612. size_t cons;
  613. size_t prod;
  614. size_t old_prod;
  615. size_t fill;
  616. /* Sanity check */
  617. assert ( vmdev->out != NULL );
  618. /* Calculate lengths */
  619. header_len = ( le16_to_cpu ( header->hdr_qlen ) * 8 );
  620. pad_len = ( ( -len ) & ( 8 - 1 ) );
  621. footer_len = sizeof ( footer );
  622. ring_len = ( header_len + len + pad_len + footer_len );
  623. /* Check that we have enough room in the outbound ring buffer */
  624. cons = le32_to_cpu ( vmdev->out->cons );
  625. prod = le32_to_cpu ( vmdev->out->prod );
  626. old_prod = prod;
  627. fill = ( ( prod - cons ) & ( vmdev->out_len - 1 ) );
  628. if ( ( fill + ring_len ) >= vmdev->out_len ) {
  629. DBGC ( vmdev, "VMBUS %s ring buffer full\n", vmdev->dev.name );
  630. return -ENOBUFS;
  631. }
  632. /* Complete header */
  633. header->qlen = cpu_to_le16 ( ( ring_len - footer_len ) / 8 );
  634. /* Construct footer */
  635. footer.reserved = 0;
  636. footer.prod = vmdev->out->prod;
  637. /* Copy packet to buffer */
  638. DBGC2 ( vmdev, "VMBUS %s sending:\n", vmdev->dev.name );
  639. DBGC2_HDA ( vmdev, prod, header, header_len );
  640. prod = vmbus_produce ( vmdev, prod, header, header_len );
  641. DBGC2_HDA ( vmdev, prod, data, len );
  642. prod = vmbus_produce ( vmdev, prod, data, len );
  643. prod = vmbus_produce ( vmdev, prod, padding, pad_len );
  644. DBGC2_HDA ( vmdev, prod, &footer, sizeof ( footer ) );
  645. prod = vmbus_produce ( vmdev, prod, &footer, sizeof ( footer ) );
  646. assert ( ( ( prod - old_prod ) & ( vmdev->out_len - 1 ) ) == ring_len );
  647. /* Update producer index */
  648. wmb();
  649. vmdev->out->prod = cpu_to_le32 ( prod );
  650. /* Return if we do not need to signal the host. This follows
  651. * the logic of hv_need_to_signal() in the Linux driver.
  652. */
  653. mb();
  654. if ( vmdev->out->intr_mask )
  655. return 0;
  656. rmb();
  657. cons = le32_to_cpu ( vmdev->out->cons );
  658. if ( cons != old_prod )
  659. return 0;
  660. /* Set channel bit in interrupt page */
  661. set_bit ( vmdev->channel, vmbus->intr->out );
  662. /* Signal the host */
  663. vmdev->signal ( vmdev );
  664. return 0;
  665. }
  666. /**
  667. * Send control packet via ring buffer
  668. *
  669. * @v vmdev VMBus device
  670. * @v xid Transaction ID (or zero to not request completion)
  671. * @v data Data
  672. * @v len Length of data
  673. * @ret rc Return status code
  674. *
  675. * Send data using a VMBUS_DATA_INBAND packet.
  676. */
  677. int vmbus_send_control ( struct vmbus_device *vmdev, uint64_t xid,
  678. const void *data, size_t len ) {
  679. struct vmbus_packet_header *header = vmdev->packet;
  680. /* Construct header in packet buffer */
  681. assert ( header != NULL );
  682. header->type = cpu_to_le16 ( VMBUS_DATA_INBAND );
  683. header->hdr_qlen = cpu_to_le16 ( sizeof ( *header ) / 8 );
  684. header->flags = ( xid ?
  685. cpu_to_le16 ( VMBUS_COMPLETION_REQUESTED ) : 0 );
  686. header->xid = xid; /* Non-endian */
  687. return vmbus_send ( vmdev, header, data, len );
  688. }
  689. /**
  690. * Send data packet via ring buffer
  691. *
  692. * @v vmdev VMBus device
  693. * @v xid Transaction ID
  694. * @v data Data
  695. * @v len Length of data
  696. * @v iobuf I/O buffer
  697. * @ret rc Return status code
  698. *
  699. * Send data using a VMBUS_DATA_GPA_DIRECT packet. The caller is
  700. * responsible for ensuring that the I/O buffer remains untouched
  701. * until the corresponding completion has been received.
  702. */
  703. int vmbus_send_data ( struct vmbus_device *vmdev, uint64_t xid,
  704. const void *data, size_t len, struct io_buffer *iobuf ) {
  705. physaddr_t addr = virt_to_phys ( iobuf->data );
  706. unsigned int pfn_count = hv_pfn_count ( addr, iob_len ( iobuf ) );
  707. struct {
  708. struct vmbus_gpa_direct_header gpa;
  709. struct vmbus_gpa_range range;
  710. uint64_t pfn[pfn_count];
  711. } __attribute__ (( packed )) *header = vmdev->packet;
  712. unsigned int i;
  713. /* Sanity check */
  714. assert ( header != NULL );
  715. assert ( sizeof ( *header ) <= vmdev->mtu );
  716. /* Construct header in packet buffer */
  717. header->gpa.header.type = cpu_to_le16 ( VMBUS_DATA_GPA_DIRECT );
  718. header->gpa.header.hdr_qlen = cpu_to_le16 ( sizeof ( *header ) / 8 );
  719. header->gpa.header.flags = cpu_to_le16 ( VMBUS_COMPLETION_REQUESTED );
  720. header->gpa.header.xid = xid; /* Non-endian */
  721. header->gpa.range_count = 1;
  722. header->range.len = cpu_to_le32 ( iob_len ( iobuf ) );
  723. header->range.offset = cpu_to_le32 ( addr & ( PAGE_SIZE - 1 ) );
  724. for ( i = 0 ; i < pfn_count ; i++ )
  725. header->pfn[i] = ( ( addr / PAGE_SIZE ) + i );
  726. return vmbus_send ( vmdev, &header->gpa.header, data, len );
  727. }
  728. /**
  729. * Send completion packet via ring buffer
  730. *
  731. * @v vmdev VMBus device
  732. * @v xid Transaction ID
  733. * @v data Data
  734. * @v len Length of data
  735. * @ret rc Return status code
  736. *
  737. * Send data using a VMBUS_COMPLETION packet.
  738. */
  739. int vmbus_send_completion ( struct vmbus_device *vmdev, uint64_t xid,
  740. const void *data, size_t len ) {
  741. struct vmbus_packet_header *header = vmdev->packet;
  742. /* Construct header in packet buffer */
  743. assert ( header != NULL );
  744. header->type = cpu_to_le16 ( VMBUS_COMPLETION );
  745. header->hdr_qlen = cpu_to_le16 ( sizeof ( *header ) / 8 );
  746. header->flags = 0;
  747. header->xid = xid; /* Non-endian */
  748. return vmbus_send ( vmdev, header, data, len );
  749. }
  750. /**
  751. * Send cancellation packet via ring buffer
  752. *
  753. * @v vmdev VMBus device
  754. * @v xid Transaction ID
  755. * @ret rc Return status code
  756. *
  757. * Send data using a VMBUS_CANCELLATION packet.
  758. */
  759. int vmbus_send_cancellation ( struct vmbus_device *vmdev, uint64_t xid ) {
  760. struct vmbus_packet_header *header = vmdev->packet;
  761. /* Construct header in packet buffer */
  762. assert ( header != NULL );
  763. header->type = cpu_to_le16 ( VMBUS_CANCELLATION );
  764. header->hdr_qlen = cpu_to_le16 ( sizeof ( *header ) / 8 );
  765. header->flags = 0;
  766. header->xid = xid; /* Non-endian */
  767. return vmbus_send ( vmdev, header, NULL, 0 );
  768. }
  769. /**
  770. * Get transfer page set from pageset ID
  771. *
  772. * @v vmdev VMBus device
  773. * @v pageset Page set ID (in protocol byte order)
  774. * @ret pages Page set, or NULL if not found
  775. */
  776. static struct vmbus_xfer_pages * vmbus_xfer_pages ( struct vmbus_device *vmdev,
  777. uint16_t pageset ) {
  778. struct vmbus_xfer_pages *pages;
  779. /* Locate page set */
  780. list_for_each_entry ( pages, &vmdev->pages, list ) {
  781. if ( pages->pageset == pageset )
  782. return pages;
  783. }
  784. DBGC ( vmdev, "VMBUS %s unrecognised page set ID %#04x\n",
  785. vmdev->dev.name, le16_to_cpu ( pageset ) );
  786. return NULL;
  787. }
  788. /**
  789. * Construct I/O buffer list from transfer pages
  790. *
  791. * @v vmdev VMBus device
  792. * @v header Transfer page header
  793. * @v list I/O buffer list to populate
  794. * @ret rc Return status code
  795. */
  796. static int vmbus_xfer_page_iobufs ( struct vmbus_device *vmdev,
  797. struct vmbus_packet_header *header,
  798. struct list_head *list ) {
  799. struct vmbus_xfer_page_header *page_header =
  800. container_of ( header, struct vmbus_xfer_page_header, header );
  801. struct vmbus_xfer_pages *pages;
  802. struct io_buffer *iobuf;
  803. struct io_buffer *tmp;
  804. size_t len;
  805. size_t offset;
  806. unsigned int range_count;
  807. unsigned int i;
  808. int rc;
  809. /* Sanity check */
  810. assert ( header->type == cpu_to_le16 ( VMBUS_DATA_XFER_PAGES ) );
  811. /* Locate page set */
  812. pages = vmbus_xfer_pages ( vmdev, page_header->pageset );
  813. if ( ! pages ) {
  814. rc = -ENOENT;
  815. goto err_pages;
  816. }
  817. /* Allocate and populate I/O buffers */
  818. range_count = le32_to_cpu ( page_header->range_count );
  819. for ( i = 0 ; i < range_count ; i++ ) {
  820. /* Parse header */
  821. len = le32_to_cpu ( page_header->range[i].len );
  822. offset = le32_to_cpu ( page_header->range[i].offset );
  823. /* Allocate I/O buffer */
  824. iobuf = alloc_iob ( len );
  825. if ( ! iobuf ) {
  826. DBGC ( vmdev, "VMBUS %s could not allocate %zd-byte "
  827. "I/O buffer\n", vmdev->dev.name, len );
  828. rc = -ENOMEM;
  829. goto err_alloc;
  830. }
  831. /* Add I/O buffer to list */
  832. list_add ( &iobuf->list, list );
  833. /* Populate I/O buffer */
  834. if ( ( rc = pages->op->copy ( pages, iob_put ( iobuf, len ),
  835. offset, len ) ) != 0 ) {
  836. DBGC ( vmdev, "VMBUS %s could not populate I/O buffer "
  837. "range [%zd,%zd): %s\n",
  838. vmdev->dev.name, offset, len, strerror ( rc ) );
  839. goto err_copy;
  840. }
  841. }
  842. return 0;
  843. err_copy:
  844. err_alloc:
  845. list_for_each_entry_safe ( iobuf, tmp, list, list ) {
  846. list_del ( &iobuf->list );
  847. free_iob ( iobuf );
  848. }
  849. err_pages:
  850. return rc;
  851. }
  852. /**
  853. * Poll ring buffer
  854. *
  855. * @v vmdev VMBus device
  856. * @ret rc Return status code
  857. */
  858. int vmbus_poll ( struct vmbus_device *vmdev ) {
  859. struct vmbus_packet_header *header = vmdev->packet;
  860. struct list_head list;
  861. void *data;
  862. size_t header_len;
  863. size_t len;
  864. size_t footer_len;
  865. size_t ring_len;
  866. size_t cons;
  867. size_t old_cons;
  868. uint64_t xid;
  869. int rc;
  870. /* Sanity checks */
  871. assert ( vmdev->packet != NULL );
  872. assert ( vmdev->in != NULL );
  873. /* Return immediately if buffer is empty */
  874. if ( ! vmbus_has_data ( vmdev ) )
  875. return 0;
  876. cons = le32_to_cpu ( vmdev->in->cons );
  877. old_cons = cons;
  878. /* Consume (start of) header */
  879. cons = vmbus_consume ( vmdev, cons, header, sizeof ( *header ) );
  880. /* Parse and sanity check header */
  881. header_len = ( le16_to_cpu ( header->hdr_qlen ) * 8 );
  882. if ( header_len < sizeof ( *header ) ) {
  883. DBGC ( vmdev, "VMBUS %s received underlength header (%zd "
  884. "bytes)\n", vmdev->dev.name, header_len );
  885. return -EINVAL;
  886. }
  887. len = ( ( le16_to_cpu ( header->qlen ) * 8 ) - header_len );
  888. footer_len = sizeof ( struct vmbus_packet_footer );
  889. ring_len = ( header_len + len + footer_len );
  890. if ( ring_len > vmdev->mtu ) {
  891. DBGC ( vmdev, "VMBUS %s received overlength packet (%zd "
  892. "bytes)\n", vmdev->dev.name, ring_len );
  893. return -ERANGE;
  894. }
  895. xid = le64_to_cpu ( header->xid );
  896. /* Consume remainder of packet */
  897. cons = vmbus_consume ( vmdev, cons,
  898. ( ( ( void * ) header ) + sizeof ( *header ) ),
  899. ( ring_len - sizeof ( *header ) ) );
  900. DBGC2 ( vmdev, "VMBUS %s received:\n", vmdev->dev.name );
  901. DBGC2_HDA ( vmdev, old_cons, header, ring_len );
  902. assert ( ( ( cons - old_cons ) & ( vmdev->in_len - 1 ) ) == ring_len );
  903. /* Allocate I/O buffers, if applicable */
  904. INIT_LIST_HEAD ( &list );
  905. if ( header->type == cpu_to_le16 ( VMBUS_DATA_XFER_PAGES ) ) {
  906. if ( ( rc = vmbus_xfer_page_iobufs ( vmdev, header,
  907. &list ) ) != 0 )
  908. return rc;
  909. }
  910. /* Update producer index */
  911. rmb();
  912. vmdev->in->cons = cpu_to_le32 ( cons );
  913. /* Handle packet */
  914. data = ( ( ( void * ) header ) + header_len );
  915. switch ( header->type ) {
  916. case cpu_to_le16 ( VMBUS_DATA_INBAND ) :
  917. if ( ( rc = vmdev->op->recv_control ( vmdev, xid, data,
  918. len ) ) != 0 ) {
  919. DBGC ( vmdev, "VMBUS %s could not handle control "
  920. "packet: %s\n",
  921. vmdev->dev.name, strerror ( rc ) );
  922. return rc;
  923. }
  924. break;
  925. case cpu_to_le16 ( VMBUS_DATA_XFER_PAGES ) :
  926. if ( ( rc = vmdev->op->recv_data ( vmdev, xid, data, len,
  927. &list ) ) != 0 ) {
  928. DBGC ( vmdev, "VMBUS %s could not handle data packet: "
  929. "%s\n", vmdev->dev.name, strerror ( rc ) );
  930. return rc;
  931. }
  932. break;
  933. case cpu_to_le16 ( VMBUS_COMPLETION ) :
  934. if ( ( rc = vmdev->op->recv_completion ( vmdev, xid, data,
  935. len ) ) != 0 ) {
  936. DBGC ( vmdev, "VMBUS %s could not handle completion: "
  937. "%s\n", vmdev->dev.name, strerror ( rc ) );
  938. return rc;
  939. }
  940. break;
  941. case cpu_to_le16 ( VMBUS_CANCELLATION ) :
  942. if ( ( rc = vmdev->op->recv_cancellation ( vmdev, xid ) ) != 0){
  943. DBGC ( vmdev, "VMBUS %s could not handle cancellation: "
  944. "%s\n", vmdev->dev.name, strerror ( rc ) );
  945. return rc;
  946. }
  947. break;
  948. default:
  949. DBGC ( vmdev, "VMBUS %s unknown packet type %d\n",
  950. vmdev->dev.name, le16_to_cpu ( header->type ) );
  951. return -ENOTSUP;
  952. }
  953. return 0;
  954. }
  955. /**
  956. * Dump channel status (for debugging)
  957. *
  958. * @v vmdev VMBus device
  959. */
  960. void vmbus_dump_channel ( struct vmbus_device *vmdev ) {
  961. size_t out_prod = le32_to_cpu ( vmdev->out->prod );
  962. size_t out_cons = le32_to_cpu ( vmdev->out->cons );
  963. size_t in_prod = le32_to_cpu ( vmdev->in->prod );
  964. size_t in_cons = le32_to_cpu ( vmdev->in->cons );
  965. size_t in_len;
  966. size_t first;
  967. size_t second;
  968. /* Dump ring status */
  969. DBGC ( vmdev, "VMBUS %s out %03zx:%03zx%s in %03zx:%03zx%s\n",
  970. vmdev->dev.name, out_prod, out_cons,
  971. ( vmdev->out->intr_mask ? "(m)" : "" ), in_prod, in_cons,
  972. ( vmdev->in->intr_mask ? "(m)" : "" ) );
  973. /* Dump inbound ring contents, if any */
  974. if ( in_prod != in_cons ) {
  975. in_len = ( ( in_prod - in_cons ) &
  976. ( vmdev->in_len - 1 ) );
  977. first = ( vmdev->in_len - in_cons );
  978. if ( first > in_len )
  979. first = in_len;
  980. second = ( in_len - first );
  981. DBGC_HDA ( vmdev, in_cons, &vmdev->in->data[in_cons], first );
  982. DBGC_HDA ( vmdev, 0, &vmdev->in->data[0], second );
  983. }
  984. }
  985. /**
  986. * Find driver for VMBus device
  987. *
  988. * @v vmdev VMBus device
  989. * @ret driver Driver, or NULL
  990. */
  991. static struct vmbus_driver * vmbus_find_driver ( const union uuid *type ) {
  992. struct vmbus_driver *vmdrv;
  993. for_each_table_entry ( vmdrv, VMBUS_DRIVERS ) {
  994. if ( memcmp ( &vmdrv->type, type, sizeof ( *type ) ) == 0 )
  995. return vmdrv;
  996. }
  997. return NULL;
  998. }
  999. /**
  1000. * Probe channels
  1001. *
  1002. * @v hv Hyper-V hypervisor
  1003. * @v parent Parent device
  1004. * @ret rc Return status code
  1005. */
  1006. static int vmbus_probe_channels ( struct hv_hypervisor *hv,
  1007. struct device *parent ) {
  1008. struct vmbus *vmbus = hv->vmbus;
  1009. const struct vmbus_message_header *header = &vmbus->message->header;
  1010. const struct vmbus_offer_channel *offer = &vmbus->message->offer;
  1011. const union uuid *type;
  1012. union uuid instance;
  1013. struct vmbus_driver *driver;
  1014. struct vmbus_device *vmdev;
  1015. struct vmbus_device *tmp;
  1016. unsigned int channel;
  1017. int rc;
  1018. /* Post message */
  1019. if ( ( rc = vmbus_post_empty_message ( hv, VMBUS_REQUEST_OFFERS ) ) !=0)
  1020. goto err_post_message;
  1021. /* Collect responses */
  1022. while ( 1 ) {
  1023. /* Wait for response */
  1024. if ( ( rc = vmbus_wait_for_any_message ( hv ) ) != 0 )
  1025. goto err_wait_for_any_message;
  1026. /* Handle response */
  1027. if ( header->type == cpu_to_le32 ( VMBUS_OFFER_CHANNEL ) ) {
  1028. /* Parse offer */
  1029. type = &offer->type;
  1030. channel = le32_to_cpu ( offer->channel );
  1031. DBGC2 ( vmbus, "VMBUS %p offer %d type %s",
  1032. vmbus, channel, uuid_ntoa ( type ) );
  1033. if ( offer->monitored )
  1034. DBGC2 ( vmbus, " monitor %d", offer->monitor );
  1035. DBGC2 ( vmbus, "\n" );
  1036. /* Look for a driver */
  1037. driver = vmbus_find_driver ( type );
  1038. if ( ! driver ) {
  1039. DBGC2 ( vmbus, "VMBUS %p has no driver for "
  1040. "type %s\n", vmbus, uuid_ntoa ( type ));
  1041. /* Not a fatal error */
  1042. continue;
  1043. }
  1044. /* Allocate and initialise device */
  1045. vmdev = zalloc ( sizeof ( *vmdev ) );
  1046. if ( ! vmdev ) {
  1047. rc = -ENOMEM;
  1048. goto err_alloc_vmdev;
  1049. }
  1050. memcpy ( &instance, &offer->instance,
  1051. sizeof ( instance ) );
  1052. uuid_mangle ( &instance );
  1053. snprintf ( vmdev->dev.name, sizeof ( vmdev->dev.name ),
  1054. "{%s}", uuid_ntoa ( &instance ) );
  1055. vmdev->dev.desc.bus_type = BUS_TYPE_HV;
  1056. INIT_LIST_HEAD ( &vmdev->dev.children );
  1057. list_add_tail ( &vmdev->dev.siblings,
  1058. &parent->children );
  1059. vmdev->dev.parent = parent;
  1060. vmdev->hv = hv;
  1061. memcpy ( &vmdev->instance, &offer->instance,
  1062. sizeof ( vmdev->instance ) );
  1063. vmdev->channel = channel;
  1064. vmdev->monitor = offer->monitor;
  1065. vmdev->signal = ( offer->monitored ?
  1066. vmbus_signal_monitor :
  1067. vmbus_signal_event );
  1068. INIT_LIST_HEAD ( &vmdev->pages );
  1069. vmdev->driver = driver;
  1070. vmdev->dev.driver_name = driver->name;
  1071. DBGC ( vmdev, "VMBUS %s has driver \"%s\"\n",
  1072. vmdev->dev.name, vmdev->driver->name );
  1073. } else if ( header->type ==
  1074. cpu_to_le32 ( VMBUS_ALL_OFFERS_DELIVERED ) ) {
  1075. /* End of offer list */
  1076. break;
  1077. } else {
  1078. DBGC ( vmbus, "VMBUS %p unexpected offer response type "
  1079. "%d\n", vmbus, le32_to_cpu ( header->type ) );
  1080. rc = -EPROTO;
  1081. goto err_unexpected_offer;
  1082. }
  1083. }
  1084. /* Probe all devices. We do this only after completing
  1085. * enumeration since devices will need to send and receive
  1086. * VMBus messages.
  1087. */
  1088. list_for_each_entry ( vmdev, &parent->children, dev.siblings ) {
  1089. if ( ( rc = vmdev->driver->probe ( vmdev ) ) != 0 ) {
  1090. DBGC ( vmdev, "VMBUS %s could not probe: %s\n",
  1091. vmdev->dev.name, strerror ( rc ) );
  1092. goto err_probe;
  1093. }
  1094. }
  1095. return 0;
  1096. err_probe:
  1097. /* Remove driver from each device that was already probed */
  1098. list_for_each_entry_continue_reverse ( vmdev, &parent->children,
  1099. dev.siblings ) {
  1100. vmdev->driver->remove ( vmdev );
  1101. }
  1102. err_unexpected_offer:
  1103. err_alloc_vmdev:
  1104. err_wait_for_any_message:
  1105. /* Free any devices allocated (but potentially not yet probed) */
  1106. list_for_each_entry_safe ( vmdev, tmp, &parent->children,
  1107. dev.siblings ) {
  1108. list_del ( &vmdev->dev.siblings );
  1109. free ( vmdev );
  1110. }
  1111. err_post_message:
  1112. return rc;
  1113. }
  1114. /**
  1115. * Reset channels
  1116. *
  1117. * @v hv Hyper-V hypervisor
  1118. * @v parent Parent device
  1119. * @ret rc Return status code
  1120. */
  1121. static int vmbus_reset_channels ( struct hv_hypervisor *hv,
  1122. struct device *parent ) {
  1123. struct vmbus *vmbus = hv->vmbus;
  1124. const struct vmbus_message_header *header = &vmbus->message->header;
  1125. const struct vmbus_offer_channel *offer = &vmbus->message->offer;
  1126. const union uuid *type;
  1127. struct vmbus_device *vmdev;
  1128. unsigned int channel;
  1129. int rc;
  1130. /* Post message */
  1131. if ( ( rc = vmbus_post_empty_message ( hv, VMBUS_REQUEST_OFFERS ) ) !=0)
  1132. return rc;
  1133. /* Collect responses */
  1134. while ( 1 ) {
  1135. /* Wait for response */
  1136. if ( ( rc = vmbus_wait_for_any_message ( hv ) ) != 0 )
  1137. return rc;
  1138. /* Handle response */
  1139. if ( header->type == cpu_to_le32 ( VMBUS_OFFER_CHANNEL ) ) {
  1140. /* Parse offer */
  1141. type = &offer->type;
  1142. channel = le32_to_cpu ( offer->channel );
  1143. DBGC2 ( vmbus, "VMBUS %p offer %d type %s",
  1144. vmbus, channel, uuid_ntoa ( type ) );
  1145. if ( offer->monitored )
  1146. DBGC2 ( vmbus, " monitor %d", offer->monitor );
  1147. DBGC2 ( vmbus, "\n" );
  1148. /* Do nothing with the offer; we already have all
  1149. * of the relevant state from the initial probe.
  1150. */
  1151. } else if ( header->type ==
  1152. cpu_to_le32 ( VMBUS_ALL_OFFERS_DELIVERED ) ) {
  1153. /* End of offer list */
  1154. break;
  1155. } else {
  1156. DBGC ( vmbus, "VMBUS %p unexpected offer response type "
  1157. "%d\n", vmbus, le32_to_cpu ( header->type ) );
  1158. return -EPROTO;
  1159. }
  1160. }
  1161. /* Reset all devices */
  1162. list_for_each_entry ( vmdev, &parent->children, dev.siblings ) {
  1163. if ( ( rc = vmdev->driver->reset ( vmdev ) ) != 0 ) {
  1164. DBGC ( vmdev, "VMBUS %s could not reset: %s\n",
  1165. vmdev->dev.name, strerror ( rc ) );
  1166. /* Continue attempting to reset other devices */
  1167. continue;
  1168. }
  1169. }
  1170. return 0;
  1171. }
  1172. /**
  1173. * Remove channels
  1174. *
  1175. * @v hv Hyper-V hypervisor
  1176. * @v parent Parent device
  1177. */
  1178. static void vmbus_remove_channels ( struct hv_hypervisor *hv __unused,
  1179. struct device *parent ) {
  1180. struct vmbus_device *vmdev;
  1181. struct vmbus_device *tmp;
  1182. /* Remove devices */
  1183. list_for_each_entry_safe ( vmdev, tmp, &parent->children,
  1184. dev.siblings ) {
  1185. vmdev->driver->remove ( vmdev );
  1186. assert ( list_empty ( &vmdev->dev.children ) );
  1187. assert ( vmdev->out == NULL );
  1188. assert ( vmdev->in == NULL );
  1189. assert ( vmdev->packet == NULL );
  1190. assert ( list_empty ( &vmdev->pages ) );
  1191. list_del ( &vmdev->dev.siblings );
  1192. free ( vmdev );
  1193. }
  1194. }
  1195. /**
  1196. * Probe Hyper-V virtual machine bus
  1197. *
  1198. * @v hv Hyper-V hypervisor
  1199. * @v parent Parent device
  1200. * @ret rc Return status code
  1201. */
  1202. int vmbus_probe ( struct hv_hypervisor *hv, struct device *parent ) {
  1203. struct vmbus *vmbus;
  1204. int rc;
  1205. /* Allocate and initialise structure */
  1206. vmbus = zalloc ( sizeof ( *vmbus ) );
  1207. if ( ! vmbus ) {
  1208. rc = -ENOMEM;
  1209. goto err_alloc;
  1210. }
  1211. hv->vmbus = vmbus;
  1212. /* Initialise message buffer pointer
  1213. *
  1214. * We use a pointer to the fixed-size Hyper-V received message
  1215. * buffer. This allows us to access fields within received
  1216. * messages without first checking the message size: any
  1217. * fields beyond the end of the message will read as zero.
  1218. */
  1219. vmbus->message = ( ( void * ) hv->message->received.data );
  1220. assert ( sizeof ( *vmbus->message ) <=
  1221. sizeof ( hv->message->received.data ) );
  1222. /* Allocate interrupt and monitor pages */
  1223. if ( ( rc = hv_alloc_pages ( hv, &vmbus->intr, &vmbus->monitor_in,
  1224. &vmbus->monitor_out, NULL ) ) != 0 )
  1225. goto err_alloc_pages;
  1226. /* Enable message interrupt */
  1227. hv_enable_sint ( hv, VMBUS_MESSAGE_SINT );
  1228. /* Negotiate protocol version */
  1229. if ( ( rc = vmbus_negotiate_version ( hv ) ) != 0 )
  1230. goto err_negotiate_version;
  1231. /* Enumerate channels */
  1232. if ( ( rc = vmbus_probe_channels ( hv, parent ) ) != 0 )
  1233. goto err_probe_channels;
  1234. return 0;
  1235. vmbus_remove_channels ( hv, parent );
  1236. err_probe_channels:
  1237. vmbus_unload ( hv );
  1238. err_negotiate_version:
  1239. hv_disable_sint ( hv, VMBUS_MESSAGE_SINT );
  1240. hv_free_pages ( hv, vmbus->intr, vmbus->monitor_in, vmbus->monitor_out,
  1241. NULL );
  1242. err_alloc_pages:
  1243. free ( vmbus );
  1244. err_alloc:
  1245. return rc;
  1246. }
  1247. /**
  1248. * Reset Hyper-V virtual machine bus
  1249. *
  1250. * @v hv Hyper-V hypervisor
  1251. * @v parent Parent device
  1252. * @ret rc Return status code
  1253. */
  1254. int vmbus_reset ( struct hv_hypervisor *hv, struct device *parent ) {
  1255. struct vmbus *vmbus = hv->vmbus;
  1256. int rc;
  1257. /* Mark all existent GPADLs as obsolete */
  1258. vmbus_obsolete_gpadl = vmbus_gpadl;
  1259. /* Clear interrupt and monitor pages */
  1260. memset ( vmbus->intr, 0, PAGE_SIZE );
  1261. memset ( vmbus->monitor_in, 0, PAGE_SIZE );
  1262. memset ( vmbus->monitor_out, 0, PAGE_SIZE );
  1263. /* Enable message interrupt */
  1264. hv_enable_sint ( hv, VMBUS_MESSAGE_SINT );
  1265. /* Renegotiate protocol version */
  1266. if ( ( rc = vmbus_negotiate_version ( hv ) ) != 0 )
  1267. return rc;
  1268. /* Reenumerate channels */
  1269. if ( ( rc = vmbus_reset_channels ( hv, parent ) ) != 0 )
  1270. return rc;
  1271. return 0;
  1272. }
  1273. /**
  1274. * Remove Hyper-V virtual machine bus
  1275. *
  1276. * @v hv Hyper-V hypervisor
  1277. * @v parent Parent device
  1278. */
  1279. void vmbus_remove ( struct hv_hypervisor *hv, struct device *parent ) {
  1280. struct vmbus *vmbus = hv->vmbus;
  1281. vmbus_remove_channels ( hv, parent );
  1282. vmbus_unload ( hv );
  1283. hv_disable_sint ( hv, VMBUS_MESSAGE_SINT );
  1284. hv_free_pages ( hv, vmbus->intr, vmbus->monitor_in, vmbus->monitor_out,
  1285. NULL );
  1286. free ( vmbus );
  1287. }