Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

vmbus.c 37KB

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