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 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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 of any type
  84. *
  85. * @v hv Hyper-V hypervisor
  86. * @ret rc Return status code
  87. */
  88. static int vmbus_wait_for_any_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. * Wait for received message of a specified type, ignoring any others
  107. *
  108. * @v hv Hyper-V hypervisor
  109. * @v type Message type
  110. * @ret rc Return status code
  111. */
  112. static int vmbus_wait_for_message ( struct hv_hypervisor *hv,
  113. unsigned int type ) {
  114. struct vmbus *vmbus = hv->vmbus;
  115. const struct vmbus_message_header *header = &vmbus->message->header;
  116. int rc;
  117. /* Loop until specified message arrives, or until an error occurs */
  118. while ( 1 ) {
  119. /* Wait for message */
  120. if ( ( rc = vmbus_wait_for_any_message ( hv ) ) != 0 )
  121. return rc;
  122. /* Check for requested message type */
  123. if ( header->type == cpu_to_le32 ( type ) )
  124. return 0;
  125. /* Ignore any other messages (e.g. due to additional
  126. * channels being offered at runtime).
  127. */
  128. DBGC ( vmbus, "VMBUS %p ignoring message type %d (expecting "
  129. "%d)\n", vmbus, le32_to_cpu ( header->type ), type );
  130. }
  131. }
  132. /**
  133. * Initiate contact
  134. *
  135. * @v hv Hyper-V hypervisor
  136. * @v raw VMBus protocol (raw) version
  137. * @ret rc Return status code
  138. */
  139. static int vmbus_initiate_contact ( struct hv_hypervisor *hv,
  140. unsigned int raw ) {
  141. struct vmbus *vmbus = hv->vmbus;
  142. const struct vmbus_version_response *version = &vmbus->message->version;
  143. struct vmbus_initiate_contact initiate;
  144. int rc;
  145. /* Construct message */
  146. memset ( &initiate, 0, sizeof ( initiate ) );
  147. initiate.header.type = cpu_to_le32 ( VMBUS_INITIATE_CONTACT );
  148. initiate.version.raw = cpu_to_le32 ( raw );
  149. initiate.intr = virt_to_phys ( vmbus->intr );
  150. initiate.monitor_in = virt_to_phys ( vmbus->monitor_in );
  151. initiate.monitor_out = virt_to_phys ( vmbus->monitor_out );
  152. /* Post message */
  153. if ( ( rc = vmbus_post_message ( hv, &initiate.header,
  154. sizeof ( initiate ) ) ) != 0 )
  155. return rc;
  156. /* Wait for response */
  157. if ( ( rc = vmbus_wait_for_message ( hv, VMBUS_VERSION_RESPONSE ) ) !=0)
  158. return rc;
  159. /* Check response */
  160. if ( ! version->supported ) {
  161. DBGC ( vmbus, "VMBUS %p requested version not supported\n",
  162. vmbus );
  163. return -ENOTSUP;
  164. }
  165. if ( version->version.raw != cpu_to_le32 ( raw ) ) {
  166. DBGC ( vmbus, "VMBUS %p unexpected version %d.%d\n",
  167. vmbus, le16_to_cpu ( version->version.major ),
  168. le16_to_cpu ( version->version.minor ) );
  169. return -EPROTO;
  170. }
  171. DBGC ( vmbus, "VMBUS %p initiated contact using version %d.%d\n",
  172. vmbus, le16_to_cpu ( version->version.major ),
  173. le16_to_cpu ( version->version.minor ) );
  174. return 0;
  175. }
  176. /**
  177. * Terminate contact
  178. *
  179. * @v hv Hyper-V hypervisor
  180. * @ret rc Return status code
  181. */
  182. static int vmbus_unload ( struct hv_hypervisor *hv ) {
  183. int rc;
  184. /* Post message */
  185. if ( ( rc = vmbus_post_empty_message ( hv, VMBUS_UNLOAD ) ) != 0 )
  186. return rc;
  187. /* Wait for response */
  188. if ( ( rc = vmbus_wait_for_message ( hv, VMBUS_UNLOAD_RESPONSE ) ) != 0)
  189. return rc;
  190. return 0;
  191. }
  192. /**
  193. * Negotiate protocol version
  194. *
  195. * @v hv Hyper-V hypervisor
  196. * @ret rc Return status code
  197. */
  198. static int vmbus_negotiate_version ( struct hv_hypervisor *hv ) {
  199. int rc;
  200. /* We require the ability to disconnect from and reconnect to
  201. * VMBus; if we don't have this then there is no (viable) way
  202. * for a loaded operating system to continue to use any VMBus
  203. * devices. (There is also a small but non-zero risk that the
  204. * host will continue to write to our interrupt and monitor
  205. * pages, since the VMBUS_UNLOAD message in earlier versions
  206. * is essentially a no-op.)
  207. *
  208. * This requires us to ensure that the host supports protocol
  209. * version 3.0 (VMBUS_VERSION_WIN8_1). However, we can't
  210. * actually _use_ protocol version 3.0, since doing so causes
  211. * an iSCSI-booted Windows Server 2012 R2 VM to crash due to a
  212. * NULL pointer dereference in vmbus.sys.
  213. *
  214. * To work around this problem, we first ensure that we can
  215. * connect using protocol v3.0, then disconnect and reconnect
  216. * using the oldest known protocol.
  217. */
  218. /* Initiate contact to check for required protocol support */
  219. if ( ( rc = vmbus_initiate_contact ( hv, VMBUS_VERSION_WIN8_1 ) ) != 0 )
  220. return rc;
  221. /* Terminate contact */
  222. if ( ( rc = vmbus_unload ( hv ) ) != 0 )
  223. return rc;
  224. /* Reinitiate contact using the oldest known protocol version */
  225. if ( ( rc = vmbus_initiate_contact ( hv, VMBUS_VERSION_WS2008 ) ) != 0 )
  226. return rc;
  227. return 0;
  228. }
  229. /**
  230. * Establish GPA descriptor list
  231. *
  232. * @v vmdev VMBus device
  233. * @v data Data buffer
  234. * @v len Length of data buffer
  235. * @ret gpadl GPADL ID, or negative error
  236. */
  237. int vmbus_establish_gpadl ( struct vmbus_device *vmdev, userptr_t data,
  238. size_t len ) {
  239. struct hv_hypervisor *hv = vmdev->hv;
  240. struct vmbus *vmbus = hv->vmbus;
  241. physaddr_t addr = user_to_phys ( data, 0 );
  242. unsigned int pfn_count = hv_pfn_count ( addr, len );
  243. struct {
  244. struct vmbus_gpadl_header gpadlhdr;
  245. struct vmbus_gpa_range range;
  246. uint64_t pfn[pfn_count];
  247. } __attribute__ (( packed )) gpadlhdr;
  248. const struct vmbus_gpadl_created *created = &vmbus->message->created;
  249. static unsigned int gpadl = VMBUS_GPADL_MAGIC;
  250. unsigned int i;
  251. int rc;
  252. /* Allocate GPADL ID */
  253. gpadl++;
  254. /* Construct message */
  255. memset ( &gpadlhdr, 0, sizeof ( gpadlhdr ) );
  256. gpadlhdr.gpadlhdr.header.type = cpu_to_le32 ( VMBUS_GPADL_HEADER );
  257. gpadlhdr.gpadlhdr.channel = cpu_to_le32 ( vmdev->channel );
  258. gpadlhdr.gpadlhdr.gpadl = cpu_to_le32 ( gpadl );
  259. gpadlhdr.gpadlhdr.range_len =
  260. cpu_to_le16 ( ( sizeof ( gpadlhdr.range ) +
  261. sizeof ( gpadlhdr.pfn ) ) );
  262. gpadlhdr.gpadlhdr.range_count = cpu_to_le16 ( 1 );
  263. gpadlhdr.range.len = cpu_to_le32 ( len );
  264. gpadlhdr.range.offset = cpu_to_le32 ( addr & ( PAGE_SIZE - 1 ) );
  265. for ( i = 0 ; i < pfn_count ; i++ )
  266. gpadlhdr.pfn[i] = ( ( addr / PAGE_SIZE ) + i );
  267. /* Post message */
  268. if ( ( rc = vmbus_post_message ( hv, &gpadlhdr.gpadlhdr.header,
  269. sizeof ( gpadlhdr ) ) ) != 0 )
  270. return rc;
  271. /* Wait for response */
  272. if ( ( rc = vmbus_wait_for_message ( hv, VMBUS_GPADL_CREATED ) ) != 0 )
  273. return rc;
  274. /* Check response */
  275. if ( created->channel != cpu_to_le32 ( vmdev->channel ) ) {
  276. DBGC ( vmdev, "VMBUS %s unexpected GPADL channel %d\n",
  277. vmdev->dev.name, le32_to_cpu ( created->channel ) );
  278. return -EPROTO;
  279. }
  280. if ( created->gpadl != cpu_to_le32 ( gpadl ) ) {
  281. DBGC ( vmdev, "VMBUS %s unexpected GPADL ID %#08x\n",
  282. vmdev->dev.name, le32_to_cpu ( created->gpadl ) );
  283. return -EPROTO;
  284. }
  285. if ( created->status != 0 ) {
  286. DBGC ( vmdev, "VMBUS %s GPADL creation failed: %#08x\n",
  287. vmdev->dev.name, le32_to_cpu ( created->status ) );
  288. return -EPROTO;
  289. }
  290. DBGC ( vmdev, "VMBUS %s GPADL %#08x is [%08lx,%08lx)\n",
  291. vmdev->dev.name, gpadl, addr, ( addr + len ) );
  292. return gpadl;
  293. }
  294. /**
  295. * Tear down GPA descriptor list
  296. *
  297. * @v vmdev VMBus device
  298. * @v gpadl GPADL ID
  299. * @ret rc Return status code
  300. */
  301. int vmbus_gpadl_teardown ( struct vmbus_device *vmdev, unsigned int gpadl ) {
  302. struct hv_hypervisor *hv = vmdev->hv;
  303. struct vmbus *vmbus = hv->vmbus;
  304. struct vmbus_gpadl_teardown teardown;
  305. const struct vmbus_gpadl_torndown *torndown = &vmbus->message->torndown;
  306. int rc;
  307. /* Construct message */
  308. memset ( &teardown, 0, sizeof ( teardown ) );
  309. teardown.header.type = cpu_to_le32 ( VMBUS_GPADL_TEARDOWN );
  310. teardown.channel = cpu_to_le32 ( vmdev->channel );
  311. teardown.gpadl = cpu_to_le32 ( gpadl );
  312. /* Post message */
  313. if ( ( rc = vmbus_post_message ( hv, &teardown.header,
  314. sizeof ( teardown ) ) ) != 0 )
  315. return rc;
  316. /* Wait for response */
  317. if ( ( rc = vmbus_wait_for_message ( hv, VMBUS_GPADL_TORNDOWN ) ) != 0 )
  318. return rc;
  319. /* Check response */
  320. if ( torndown->gpadl != cpu_to_le32 ( gpadl ) ) {
  321. DBGC ( vmdev, "VMBUS %s unexpected GPADL ID %#08x\n",
  322. vmdev->dev.name, le32_to_cpu ( torndown->gpadl ) );
  323. return -EPROTO;
  324. }
  325. return 0;
  326. }
  327. /**
  328. * Open VMBus channel
  329. *
  330. * @v vmdev VMBus device
  331. * @v op Channel operations
  332. * @v out_len Outbound ring buffer length
  333. * @v in_len Inbound ring buffer length
  334. * @v mtu Maximum expected data packet length (including headers)
  335. * @ret rc Return status code
  336. *
  337. * Both outbound and inbound ring buffer lengths must be a power of
  338. * two and a multiple of PAGE_SIZE. The requirement to be a power of
  339. * two is a policy decision taken to simplify the ring buffer indexing
  340. * logic.
  341. */
  342. int vmbus_open ( struct vmbus_device *vmdev,
  343. struct vmbus_channel_operations *op,
  344. size_t out_len, size_t in_len, size_t mtu ) {
  345. struct hv_hypervisor *hv = vmdev->hv;
  346. struct vmbus *vmbus = hv->vmbus;
  347. struct vmbus_open_channel open;
  348. const struct vmbus_open_channel_result *opened =
  349. &vmbus->message->opened;
  350. size_t len;
  351. void *ring;
  352. void *packet;
  353. int gpadl;
  354. uint32_t open_id;
  355. int rc;
  356. /* Sanity checks */
  357. assert ( ( out_len % PAGE_SIZE ) == 0 );
  358. assert ( ( out_len & ( out_len - 1 ) ) == 0 );
  359. assert ( ( in_len % PAGE_SIZE ) == 0 );
  360. assert ( ( in_len & ( in_len - 1 ) ) == 0 );
  361. assert ( mtu >= ( sizeof ( struct vmbus_packet_header ) +
  362. sizeof ( struct vmbus_packet_footer ) ) );
  363. /* Allocate packet buffer */
  364. packet = malloc ( mtu );
  365. if ( ! packet ) {
  366. rc = -ENOMEM;
  367. goto err_alloc_packet;
  368. }
  369. /* Allocate ring buffer */
  370. len = ( sizeof ( *vmdev->out ) + out_len +
  371. sizeof ( *vmdev->in ) + in_len );
  372. assert ( ( len % PAGE_SIZE ) == 0 );
  373. ring = malloc_dma ( len, PAGE_SIZE );
  374. if ( ! ring ) {
  375. rc = -ENOMEM;
  376. goto err_alloc_ring;
  377. }
  378. memset ( ring, 0, len );
  379. /* Establish GPADL for ring buffer */
  380. gpadl = vmbus_establish_gpadl ( vmdev, virt_to_user ( ring ), len );
  381. if ( gpadl < 0 ) {
  382. rc = gpadl;
  383. goto err_establish;
  384. }
  385. /* Construct message */
  386. memset ( &open, 0, sizeof ( open ) );
  387. open.header.type = cpu_to_le32 ( VMBUS_OPEN_CHANNEL );
  388. open.channel = cpu_to_le32 ( vmdev->channel );
  389. open_id = random();
  390. open.id = open_id; /* Opaque random value: endianness irrelevant */
  391. open.gpadl = cpu_to_le32 ( gpadl );
  392. open.out_pages = ( ( sizeof ( *vmdev->out ) / PAGE_SIZE ) +
  393. ( out_len / PAGE_SIZE ) );
  394. /* Post message */
  395. if ( ( rc = vmbus_post_message ( hv, &open.header,
  396. sizeof ( open ) ) ) != 0 )
  397. return rc;
  398. /* Wait for response */
  399. if ( ( rc = vmbus_wait_for_message ( hv,
  400. VMBUS_OPEN_CHANNEL_RESULT ) ) != 0)
  401. return rc;
  402. /* Check response */
  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_any_message ( hv ) ) != 0 )
  1004. goto err_wait_for_any_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_any_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. }