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

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