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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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. goto err_post_message;
  398. /* Wait for response */
  399. if ( ( rc = vmbus_wait_for_message ( hv,
  400. VMBUS_OPEN_CHANNEL_RESULT ) ) != 0)
  401. goto err_wait_for_message;
  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. rc = -EPROTO;
  407. goto err_check_response;
  408. }
  409. if ( opened->id != open_id /* Non-endian */ ) {
  410. DBGC ( vmdev, "VMBUS %s unexpected open ID %#08x\n",
  411. vmdev->dev.name, le32_to_cpu ( opened->id ) );
  412. rc = -EPROTO;
  413. goto err_check_response;
  414. }
  415. if ( opened->status != 0 ) {
  416. DBGC ( vmdev, "VMBUS %s open failed: %#08x\n",
  417. vmdev->dev.name, le32_to_cpu ( opened->status ) );
  418. rc = -EPROTO;
  419. goto err_check_response;
  420. }
  421. /* Store channel parameters */
  422. vmdev->out_len = out_len;
  423. vmdev->in_len = in_len;
  424. vmdev->out = ring;
  425. vmdev->in = ( ring + sizeof ( *vmdev->out ) + out_len );
  426. vmdev->gpadl = gpadl;
  427. vmdev->op = op;
  428. vmdev->mtu = mtu;
  429. vmdev->packet = packet;
  430. DBGC ( vmdev, "VMBUS %s channel GPADL %#08x ring "
  431. "[%#08lx,%#08lx,%#08lx)\n", vmdev->dev.name, vmdev->gpadl,
  432. virt_to_phys ( vmdev->out ), virt_to_phys ( vmdev->in ),
  433. ( virt_to_phys ( vmdev->out ) + len ) );
  434. return 0;
  435. err_check_response:
  436. err_wait_for_message:
  437. err_post_message:
  438. vmbus_gpadl_teardown ( vmdev, vmdev->gpadl );
  439. err_establish:
  440. free_dma ( ring, len );
  441. err_alloc_ring:
  442. free ( packet );
  443. err_alloc_packet:
  444. return rc;
  445. }
  446. /**
  447. * Close VMBus channel
  448. *
  449. * @v vmdev VMBus device
  450. */
  451. void vmbus_close ( struct vmbus_device *vmdev ) {
  452. struct hv_hypervisor *hv = vmdev->hv;
  453. struct vmbus_close_channel close;
  454. size_t len;
  455. int rc;
  456. /* Construct message */
  457. memset ( &close, 0, sizeof ( close ) );
  458. close.header.type = cpu_to_le32 ( VMBUS_CLOSE_CHANNEL );
  459. close.channel = cpu_to_le32 ( vmdev->channel );
  460. /* Post message */
  461. if ( ( rc = vmbus_post_message ( hv, &close.header,
  462. sizeof ( close ) ) ) != 0 ) {
  463. DBGC ( vmdev, "VMBUS %s failed to close: %s\n",
  464. vmdev->dev.name, strerror ( rc ) );
  465. /* Continue to attempt to tear down GPADL, so that our
  466. * memory is no longer accessible by the remote VM.
  467. */
  468. }
  469. /* Tear down GPADL */
  470. if ( ( rc = vmbus_gpadl_teardown ( vmdev,
  471. vmdev->gpadl ) ) != 0 ) {
  472. DBGC ( vmdev, "VMBUS %s failed to tear down channel GPADL: "
  473. "%s\n", vmdev->dev.name, strerror ( rc ) );
  474. /* We can't prevent the remote VM from continuing to
  475. * access this memory, so leak it.
  476. */
  477. return;
  478. }
  479. /* Free ring buffer */
  480. len = ( sizeof ( *vmdev->out ) + vmdev->out_len +
  481. sizeof ( *vmdev->in ) + vmdev->in_len );
  482. free_dma ( vmdev->out, len );
  483. vmdev->out = NULL;
  484. vmdev->in = NULL;
  485. /* Free packet buffer */
  486. free ( vmdev->packet );
  487. vmdev->packet = NULL;
  488. DBGC ( vmdev, "VMBUS %s closed\n", vmdev->dev.name );
  489. }
  490. /**
  491. * Signal channel via monitor page
  492. *
  493. * @v vmdev VMBus device
  494. */
  495. static void vmbus_signal_monitor ( struct vmbus_device *vmdev ) {
  496. struct hv_hypervisor *hv = vmdev->hv;
  497. struct vmbus *vmbus = hv->vmbus;
  498. struct hv_monitor_trigger *trigger;
  499. unsigned int group;
  500. unsigned int bit;
  501. /* Set bit in monitor trigger group */
  502. group = ( vmdev->monitor / ( 8 * sizeof ( trigger->pending ) ));
  503. bit = ( vmdev->monitor % ( 8 * sizeof ( trigger->pending ) ) );
  504. trigger = &vmbus->monitor_out->trigger[group];
  505. set_bit ( bit, trigger );
  506. }
  507. /**
  508. * Signal channel via hypervisor event
  509. *
  510. * @v vmdev VMBus device
  511. */
  512. static void vmbus_signal_event ( struct vmbus_device *vmdev ) {
  513. struct hv_hypervisor *hv = vmdev->hv;
  514. int rc;
  515. /* Signal hypervisor event */
  516. if ( ( rc = hv_signal_event ( hv, VMBUS_EVENT_ID, 0 ) ) != 0 ) {
  517. DBGC ( vmdev, "VMBUS %s could not signal event: %s\n",
  518. vmdev->dev.name, strerror ( rc ) );
  519. return;
  520. }
  521. }
  522. /**
  523. * Fill outbound ring buffer
  524. *
  525. * @v vmdev VMBus device
  526. * @v prod Producer index
  527. * @v data Data
  528. * @v len Length
  529. * @ret prod New producer index
  530. *
  531. * The caller must ensure that there is sufficient space in the ring
  532. * buffer.
  533. */
  534. static size_t vmbus_produce ( struct vmbus_device *vmdev, size_t prod,
  535. const void *data, size_t len ) {
  536. size_t first;
  537. size_t second;
  538. /* Determine fragment lengths */
  539. first = ( vmdev->out_len - prod );
  540. if ( first > len )
  541. first = len;
  542. second = ( len - first );
  543. /* Copy fragment(s) */
  544. memcpy ( &vmdev->out->data[prod], data, first );
  545. if ( second )
  546. memcpy ( &vmdev->out->data[0], ( data + first ), second );
  547. return ( ( prod + len ) & ( vmdev->out_len - 1 ) );
  548. }
  549. /**
  550. * Consume inbound ring buffer
  551. *
  552. * @v vmdev VMBus device
  553. * @v cons Consumer index
  554. * @v data Data buffer, or NULL
  555. * @v len Length to consume
  556. * @ret cons New consumer index
  557. */
  558. static size_t vmbus_consume ( struct vmbus_device *vmdev, size_t cons,
  559. void *data, size_t len ) {
  560. size_t first;
  561. size_t second;
  562. /* Determine fragment lengths */
  563. first = ( vmdev->in_len - cons );
  564. if ( first > len )
  565. first = len;
  566. second = ( len - first );
  567. /* Copy fragment(s) */
  568. memcpy ( data, &vmdev->in->data[cons], first );
  569. if ( second )
  570. memcpy ( ( data + first ), &vmdev->in->data[0], second );
  571. return ( ( cons + len ) & ( vmdev->in_len - 1 ) );
  572. }
  573. /**
  574. * Send packet via ring buffer
  575. *
  576. * @v vmdev VMBus device
  577. * @v header Packet header
  578. * @v data Data
  579. * @v len Length of data
  580. * @ret rc Return status code
  581. *
  582. * Send a packet via the outbound ring buffer. All fields in the
  583. * packet header must be filled in, with the exception of the total
  584. * packet length.
  585. */
  586. static int vmbus_send ( struct vmbus_device *vmdev,
  587. struct vmbus_packet_header *header,
  588. const void *data, size_t len ) {
  589. struct hv_hypervisor *hv = vmdev->hv;
  590. struct vmbus *vmbus = hv->vmbus;
  591. static uint8_t padding[ 8 - 1 ];
  592. struct vmbus_packet_footer footer;
  593. size_t header_len;
  594. size_t pad_len;
  595. size_t footer_len;
  596. size_t ring_len;
  597. size_t cons;
  598. size_t prod;
  599. size_t old_prod;
  600. size_t fill;
  601. /* Sanity check */
  602. assert ( vmdev->out != NULL );
  603. /* Calculate lengths */
  604. header_len = ( le16_to_cpu ( header->hdr_qlen ) * 8 );
  605. pad_len = ( ( -len ) & ( 8 - 1 ) );
  606. footer_len = sizeof ( footer );
  607. ring_len = ( header_len + len + pad_len + footer_len );
  608. /* Check that we have enough room in the outbound ring buffer */
  609. cons = le32_to_cpu ( vmdev->out->cons );
  610. prod = le32_to_cpu ( vmdev->out->prod );
  611. old_prod = prod;
  612. fill = ( ( prod - cons ) & ( vmdev->out_len - 1 ) );
  613. if ( ( fill + ring_len ) >= vmdev->out_len ) {
  614. DBGC ( vmdev, "VMBUS %s ring buffer full\n", vmdev->dev.name );
  615. return -ENOBUFS;
  616. }
  617. /* Complete header */
  618. header->qlen = cpu_to_le16 ( ( ring_len - footer_len ) / 8 );
  619. /* Construct footer */
  620. footer.reserved = 0;
  621. footer.prod = vmdev->out->prod;
  622. /* Copy packet to buffer */
  623. DBGC2 ( vmdev, "VMBUS %s sending:\n", vmdev->dev.name );
  624. DBGC2_HDA ( vmdev, prod, header, header_len );
  625. prod = vmbus_produce ( vmdev, prod, header, header_len );
  626. DBGC2_HDA ( vmdev, prod, data, len );
  627. prod = vmbus_produce ( vmdev, prod, data, len );
  628. prod = vmbus_produce ( vmdev, prod, padding, pad_len );
  629. DBGC2_HDA ( vmdev, prod, &footer, sizeof ( footer ) );
  630. prod = vmbus_produce ( vmdev, prod, &footer, sizeof ( footer ) );
  631. assert ( ( ( prod - old_prod ) & ( vmdev->out_len - 1 ) ) == ring_len );
  632. /* Update producer index */
  633. wmb();
  634. vmdev->out->prod = cpu_to_le32 ( prod );
  635. /* Return if we do not need to signal the host. This follows
  636. * the logic of hv_need_to_signal() in the Linux driver.
  637. */
  638. mb();
  639. if ( vmdev->out->intr_mask )
  640. return 0;
  641. rmb();
  642. cons = le32_to_cpu ( vmdev->out->cons );
  643. if ( cons != old_prod )
  644. return 0;
  645. /* Set channel bit in interrupt page */
  646. set_bit ( vmdev->channel, vmbus->intr->out );
  647. /* Signal the host */
  648. vmdev->signal ( vmdev );
  649. return 0;
  650. }
  651. /**
  652. * Send control packet via ring buffer
  653. *
  654. * @v vmdev VMBus device
  655. * @v xid Transaction ID (or zero to not request completion)
  656. * @v data Data
  657. * @v len Length of data
  658. * @ret rc Return status code
  659. *
  660. * Send data using a VMBUS_DATA_INBAND packet.
  661. */
  662. int vmbus_send_control ( struct vmbus_device *vmdev, uint64_t xid,
  663. const void *data, size_t len ) {
  664. struct vmbus_packet_header *header = vmdev->packet;
  665. /* Construct header in packet buffer */
  666. assert ( header != NULL );
  667. header->type = cpu_to_le16 ( VMBUS_DATA_INBAND );
  668. header->hdr_qlen = cpu_to_le16 ( sizeof ( *header ) / 8 );
  669. header->flags = ( xid ?
  670. cpu_to_le16 ( VMBUS_COMPLETION_REQUESTED ) : 0 );
  671. header->xid = xid; /* Non-endian */
  672. return vmbus_send ( vmdev, header, data, len );
  673. }
  674. /**
  675. * Send data packet via ring buffer
  676. *
  677. * @v vmdev VMBus device
  678. * @v xid Transaction ID
  679. * @v data Data
  680. * @v len Length of data
  681. * @v iobuf I/O buffer
  682. * @ret rc Return status code
  683. *
  684. * Send data using a VMBUS_DATA_GPA_DIRECT packet. The caller is
  685. * responsible for ensuring that the I/O buffer remains untouched
  686. * until the corresponding completion has been received.
  687. */
  688. int vmbus_send_data ( struct vmbus_device *vmdev, uint64_t xid,
  689. const void *data, size_t len, struct io_buffer *iobuf ) {
  690. physaddr_t addr = virt_to_phys ( iobuf->data );
  691. unsigned int pfn_count = hv_pfn_count ( addr, iob_len ( iobuf ) );
  692. struct {
  693. struct vmbus_gpa_direct_header gpa;
  694. struct vmbus_gpa_range range;
  695. uint64_t pfn[pfn_count];
  696. } __attribute__ (( packed )) *header = vmdev->packet;
  697. unsigned int i;
  698. /* Sanity check */
  699. assert ( header != NULL );
  700. assert ( sizeof ( *header ) <= vmdev->mtu );
  701. /* Construct header in packet buffer */
  702. header->gpa.header.type = cpu_to_le16 ( VMBUS_DATA_GPA_DIRECT );
  703. header->gpa.header.hdr_qlen = cpu_to_le16 ( sizeof ( *header ) / 8 );
  704. header->gpa.header.flags = cpu_to_le16 ( VMBUS_COMPLETION_REQUESTED );
  705. header->gpa.header.xid = xid; /* Non-endian */
  706. header->gpa.range_count = 1;
  707. header->range.len = cpu_to_le32 ( iob_len ( iobuf ) );
  708. header->range.offset = cpu_to_le32 ( addr & ( PAGE_SIZE - 1 ) );
  709. for ( i = 0 ; i < pfn_count ; i++ )
  710. header->pfn[i] = ( ( addr / PAGE_SIZE ) + i );
  711. return vmbus_send ( vmdev, &header->gpa.header, data, len );
  712. }
  713. /**
  714. * Send completion packet via ring buffer
  715. *
  716. * @v vmdev VMBus device
  717. * @v xid Transaction ID
  718. * @v data Data
  719. * @v len Length of data
  720. * @ret rc Return status code
  721. *
  722. * Send data using a VMBUS_COMPLETION packet.
  723. */
  724. int vmbus_send_completion ( struct vmbus_device *vmdev, uint64_t xid,
  725. const void *data, size_t len ) {
  726. struct vmbus_packet_header *header = vmdev->packet;
  727. /* Construct header in packet buffer */
  728. assert ( header != NULL );
  729. header->type = cpu_to_le16 ( VMBUS_COMPLETION );
  730. header->hdr_qlen = cpu_to_le16 ( sizeof ( *header ) / 8 );
  731. header->flags = 0;
  732. header->xid = xid; /* Non-endian */
  733. return vmbus_send ( vmdev, header, data, len );
  734. }
  735. /**
  736. * Send cancellation packet via ring buffer
  737. *
  738. * @v vmdev VMBus device
  739. * @v xid Transaction ID
  740. * @ret rc Return status code
  741. *
  742. * Send data using a VMBUS_CANCELLATION packet.
  743. */
  744. int vmbus_send_cancellation ( struct vmbus_device *vmdev, uint64_t xid ) {
  745. struct vmbus_packet_header *header = vmdev->packet;
  746. /* Construct header in packet buffer */
  747. assert ( header != NULL );
  748. header->type = cpu_to_le16 ( VMBUS_CANCELLATION );
  749. header->hdr_qlen = cpu_to_le16 ( sizeof ( *header ) / 8 );
  750. header->flags = 0;
  751. header->xid = xid; /* Non-endian */
  752. return vmbus_send ( vmdev, header, NULL, 0 );
  753. }
  754. /**
  755. * Get transfer page set from pageset ID
  756. *
  757. * @v vmdev VMBus device
  758. * @v pageset Page set ID (in protocol byte order)
  759. * @ret pages Page set, or NULL if not found
  760. */
  761. static struct vmbus_xfer_pages * vmbus_xfer_pages ( struct vmbus_device *vmdev,
  762. uint16_t pageset ) {
  763. struct vmbus_xfer_pages *pages;
  764. /* Locate page set */
  765. list_for_each_entry ( pages, &vmdev->pages, list ) {
  766. if ( pages->pageset == pageset )
  767. return pages;
  768. }
  769. DBGC ( vmdev, "VMBUS %s unrecognised page set ID %#04x\n",
  770. vmdev->dev.name, le16_to_cpu ( pageset ) );
  771. return NULL;
  772. }
  773. /**
  774. * Construct I/O buffer list from transfer pages
  775. *
  776. * @v vmdev VMBus device
  777. * @v header Transfer page header
  778. * @v list I/O buffer list to populate
  779. * @ret rc Return status code
  780. */
  781. static int vmbus_xfer_page_iobufs ( struct vmbus_device *vmdev,
  782. struct vmbus_packet_header *header,
  783. struct list_head *list ) {
  784. struct vmbus_xfer_page_header *page_header =
  785. container_of ( header, struct vmbus_xfer_page_header, header );
  786. struct vmbus_xfer_pages *pages;
  787. struct io_buffer *iobuf;
  788. struct io_buffer *tmp;
  789. size_t len;
  790. size_t offset;
  791. unsigned int range_count;
  792. unsigned int i;
  793. int rc;
  794. /* Sanity check */
  795. assert ( header->type == cpu_to_le16 ( VMBUS_DATA_XFER_PAGES ) );
  796. /* Locate page set */
  797. pages = vmbus_xfer_pages ( vmdev, page_header->pageset );
  798. if ( ! pages ) {
  799. rc = -ENOENT;
  800. goto err_pages;
  801. }
  802. /* Allocate and populate I/O buffers */
  803. range_count = le32_to_cpu ( page_header->range_count );
  804. for ( i = 0 ; i < range_count ; i++ ) {
  805. /* Parse header */
  806. len = le32_to_cpu ( page_header->range[i].len );
  807. offset = le32_to_cpu ( page_header->range[i].offset );
  808. /* Allocate I/O buffer */
  809. iobuf = alloc_iob ( len );
  810. if ( ! iobuf ) {
  811. DBGC ( vmdev, "VMBUS %s could not allocate %zd-byte "
  812. "I/O buffer\n", vmdev->dev.name, len );
  813. rc = -ENOMEM;
  814. goto err_alloc;
  815. }
  816. /* Add I/O buffer to list */
  817. list_add ( &iobuf->list, list );
  818. /* Populate I/O buffer */
  819. if ( ( rc = pages->op->copy ( pages, iob_put ( iobuf, len ),
  820. offset, len ) ) != 0 ) {
  821. DBGC ( vmdev, "VMBUS %s could not populate I/O buffer "
  822. "range [%zd,%zd): %s\n",
  823. vmdev->dev.name, offset, len, strerror ( rc ) );
  824. goto err_copy;
  825. }
  826. }
  827. return 0;
  828. err_copy:
  829. err_alloc:
  830. list_for_each_entry_safe ( iobuf, tmp, list, list ) {
  831. list_del ( &iobuf->list );
  832. free_iob ( iobuf );
  833. }
  834. err_pages:
  835. return rc;
  836. }
  837. /**
  838. * Poll ring buffer
  839. *
  840. * @v vmdev VMBus device
  841. * @ret rc Return status code
  842. */
  843. int vmbus_poll ( struct vmbus_device *vmdev ) {
  844. struct vmbus_packet_header *header = vmdev->packet;
  845. struct list_head list;
  846. void *data;
  847. size_t header_len;
  848. size_t len;
  849. size_t footer_len;
  850. size_t ring_len;
  851. size_t cons;
  852. size_t old_cons;
  853. uint64_t xid;
  854. int rc;
  855. /* Sanity checks */
  856. assert ( vmdev->packet != NULL );
  857. assert ( vmdev->in != NULL );
  858. /* Return immediately if buffer is empty */
  859. if ( ! vmbus_has_data ( vmdev ) )
  860. return 0;
  861. cons = le32_to_cpu ( vmdev->in->cons );
  862. old_cons = cons;
  863. /* Consume (start of) header */
  864. cons = vmbus_consume ( vmdev, cons, header, sizeof ( *header ) );
  865. /* Parse and sanity check header */
  866. header_len = ( le16_to_cpu ( header->hdr_qlen ) * 8 );
  867. if ( header_len < sizeof ( *header ) ) {
  868. DBGC ( vmdev, "VMBUS %s received underlength header (%zd "
  869. "bytes)\n", vmdev->dev.name, header_len );
  870. return -EINVAL;
  871. }
  872. len = ( ( le16_to_cpu ( header->qlen ) * 8 ) - header_len );
  873. footer_len = sizeof ( struct vmbus_packet_footer );
  874. ring_len = ( header_len + len + footer_len );
  875. if ( ring_len > vmdev->mtu ) {
  876. DBGC ( vmdev, "VMBUS %s received overlength packet (%zd "
  877. "bytes)\n", vmdev->dev.name, ring_len );
  878. return -ERANGE;
  879. }
  880. xid = le64_to_cpu ( header->xid );
  881. /* Consume remainder of packet */
  882. cons = vmbus_consume ( vmdev, cons,
  883. ( ( ( void * ) header ) + sizeof ( *header ) ),
  884. ( ring_len - sizeof ( *header ) ) );
  885. DBGC2 ( vmdev, "VMBUS %s received:\n", vmdev->dev.name );
  886. DBGC2_HDA ( vmdev, old_cons, header, ring_len );
  887. assert ( ( ( cons - old_cons ) & ( vmdev->in_len - 1 ) ) == ring_len );
  888. /* Allocate I/O buffers, if applicable */
  889. INIT_LIST_HEAD ( &list );
  890. if ( header->type == cpu_to_le16 ( VMBUS_DATA_XFER_PAGES ) ) {
  891. if ( ( rc = vmbus_xfer_page_iobufs ( vmdev, header,
  892. &list ) ) != 0 )
  893. return rc;
  894. }
  895. /* Update producer index */
  896. rmb();
  897. vmdev->in->cons = cpu_to_le32 ( cons );
  898. /* Handle packet */
  899. data = ( ( ( void * ) header ) + header_len );
  900. switch ( header->type ) {
  901. case cpu_to_le16 ( VMBUS_DATA_INBAND ) :
  902. if ( ( rc = vmdev->op->recv_control ( vmdev, xid, data,
  903. len ) ) != 0 ) {
  904. DBGC ( vmdev, "VMBUS %s could not handle control "
  905. "packet: %s\n",
  906. vmdev->dev.name, strerror ( rc ) );
  907. return rc;
  908. }
  909. break;
  910. case cpu_to_le16 ( VMBUS_DATA_XFER_PAGES ) :
  911. if ( ( rc = vmdev->op->recv_data ( vmdev, xid, data, len,
  912. &list ) ) != 0 ) {
  913. DBGC ( vmdev, "VMBUS %s could not handle data packet: "
  914. "%s\n", vmdev->dev.name, strerror ( rc ) );
  915. return rc;
  916. }
  917. break;
  918. case cpu_to_le16 ( VMBUS_COMPLETION ) :
  919. if ( ( rc = vmdev->op->recv_completion ( vmdev, xid, data,
  920. len ) ) != 0 ) {
  921. DBGC ( vmdev, "VMBUS %s could not handle completion: "
  922. "%s\n", vmdev->dev.name, strerror ( rc ) );
  923. return rc;
  924. }
  925. break;
  926. case cpu_to_le16 ( VMBUS_CANCELLATION ) :
  927. if ( ( rc = vmdev->op->recv_cancellation ( vmdev, xid ) ) != 0){
  928. DBGC ( vmdev, "VMBUS %s could not handle cancellation: "
  929. "%s\n", vmdev->dev.name, strerror ( rc ) );
  930. return rc;
  931. }
  932. break;
  933. default:
  934. DBGC ( vmdev, "VMBUS %s unknown packet type %d\n",
  935. vmdev->dev.name, le16_to_cpu ( header->type ) );
  936. return -ENOTSUP;
  937. }
  938. return 0;
  939. }
  940. /**
  941. * Dump channel status (for debugging)
  942. *
  943. * @v vmdev VMBus device
  944. */
  945. void vmbus_dump_channel ( struct vmbus_device *vmdev ) {
  946. size_t out_prod = le32_to_cpu ( vmdev->out->prod );
  947. size_t out_cons = le32_to_cpu ( vmdev->out->cons );
  948. size_t in_prod = le32_to_cpu ( vmdev->in->prod );
  949. size_t in_cons = le32_to_cpu ( vmdev->in->cons );
  950. size_t in_len;
  951. size_t first;
  952. size_t second;
  953. /* Dump ring status */
  954. DBGC ( vmdev, "VMBUS %s out %03zx:%03zx%s in %03zx:%03zx%s\n",
  955. vmdev->dev.name, out_prod, out_cons,
  956. ( vmdev->out->intr_mask ? "(m)" : "" ), in_prod, in_cons,
  957. ( vmdev->in->intr_mask ? "(m)" : "" ) );
  958. /* Dump inbound ring contents, if any */
  959. if ( in_prod != in_cons ) {
  960. in_len = ( ( in_prod - in_cons ) &
  961. ( vmdev->in_len - 1 ) );
  962. first = ( vmdev->in_len - in_cons );
  963. if ( first > in_len )
  964. first = in_len;
  965. second = ( in_len - first );
  966. DBGC_HDA ( vmdev, in_cons, &vmdev->in->data[in_cons], first );
  967. DBGC_HDA ( vmdev, 0, &vmdev->in->data[0], second );
  968. }
  969. }
  970. /**
  971. * Find driver for VMBus device
  972. *
  973. * @v vmdev VMBus device
  974. * @ret driver Driver, or NULL
  975. */
  976. static struct vmbus_driver * vmbus_find_driver ( const union uuid *type ) {
  977. struct vmbus_driver *vmdrv;
  978. for_each_table_entry ( vmdrv, VMBUS_DRIVERS ) {
  979. if ( memcmp ( &vmdrv->type, type, sizeof ( *type ) ) == 0 )
  980. return vmdrv;
  981. }
  982. return NULL;
  983. }
  984. /**
  985. * Probe channels
  986. *
  987. * @v hv Hyper-V hypervisor
  988. * @v parent Parent device
  989. * @ret rc Return status code
  990. */
  991. static int vmbus_probe_channels ( struct hv_hypervisor *hv,
  992. struct device *parent ) {
  993. struct vmbus *vmbus = hv->vmbus;
  994. const struct vmbus_message_header *header = &vmbus->message->header;
  995. const struct vmbus_offer_channel *offer = &vmbus->message->offer;
  996. const union uuid *type;
  997. union uuid instance;
  998. struct vmbus_driver *driver;
  999. struct vmbus_device *vmdev;
  1000. struct vmbus_device *tmp;
  1001. unsigned int channel;
  1002. int rc;
  1003. /* Post message */
  1004. if ( ( rc = vmbus_post_empty_message ( hv, VMBUS_REQUEST_OFFERS ) ) !=0)
  1005. goto err_post_message;
  1006. /* Collect responses */
  1007. while ( 1 ) {
  1008. /* Wait for response */
  1009. if ( ( rc = vmbus_wait_for_any_message ( hv ) ) != 0 )
  1010. goto err_wait_for_any_message;
  1011. /* Handle response */
  1012. if ( header->type == cpu_to_le32 ( VMBUS_OFFER_CHANNEL ) ) {
  1013. /* Parse offer */
  1014. type = &offer->type;
  1015. channel = le32_to_cpu ( offer->channel );
  1016. DBGC2 ( vmbus, "VMBUS %p offer %d type %s",
  1017. vmbus, channel, uuid_ntoa ( type ) );
  1018. if ( offer->monitored )
  1019. DBGC2 ( vmbus, " monitor %d", offer->monitor );
  1020. DBGC2 ( vmbus, "\n" );
  1021. /* Look for a driver */
  1022. driver = vmbus_find_driver ( type );
  1023. if ( ! driver ) {
  1024. DBGC2 ( vmbus, "VMBUS %p has no driver for "
  1025. "type %s\n", vmbus, uuid_ntoa ( type ));
  1026. /* Not a fatal error */
  1027. continue;
  1028. }
  1029. /* Allocate and initialise device */
  1030. vmdev = zalloc ( sizeof ( *vmdev ) );
  1031. if ( ! vmdev ) {
  1032. rc = -ENOMEM;
  1033. goto err_alloc_vmdev;
  1034. }
  1035. memcpy ( &instance, &offer->instance,
  1036. sizeof ( instance ) );
  1037. uuid_mangle ( &instance );
  1038. snprintf ( vmdev->dev.name, sizeof ( vmdev->dev.name ),
  1039. "{%s}", uuid_ntoa ( &instance ) );
  1040. vmdev->dev.desc.bus_type = BUS_TYPE_HV;
  1041. INIT_LIST_HEAD ( &vmdev->dev.children );
  1042. list_add_tail ( &vmdev->dev.siblings,
  1043. &parent->children );
  1044. vmdev->dev.parent = parent;
  1045. vmdev->hv = hv;
  1046. vmdev->channel = channel;
  1047. vmdev->monitor = offer->monitor;
  1048. vmdev->signal = ( offer->monitored ?
  1049. vmbus_signal_monitor :
  1050. vmbus_signal_event );
  1051. INIT_LIST_HEAD ( &vmdev->pages );
  1052. vmdev->driver = driver;
  1053. vmdev->dev.driver_name = driver->name;
  1054. DBGC ( vmdev, "VMBUS %s has driver \"%s\"\n",
  1055. vmdev->dev.name, vmdev->driver->name );
  1056. } else if ( header->type ==
  1057. cpu_to_le32 ( VMBUS_ALL_OFFERS_DELIVERED ) ) {
  1058. break;
  1059. } else {
  1060. DBGC ( vmbus, "VMBUS %p unexpected offer response type "
  1061. "%d\n", vmbus, le32_to_cpu ( header->type ) );
  1062. rc = -EPROTO;
  1063. goto err_unexpected_offer;
  1064. }
  1065. }
  1066. /* Probe all devices. We do this only after completing
  1067. * enumeration since devices will need to send and receive
  1068. * VMBus messages.
  1069. */
  1070. list_for_each_entry ( vmdev, &parent->children, dev.siblings ) {
  1071. if ( ( rc = vmdev->driver->probe ( vmdev ) ) != 0 ) {
  1072. DBGC ( vmdev, "VMBUS %s could not probe: %s\n",
  1073. vmdev->dev.name, strerror ( rc ) );
  1074. goto err_probe;
  1075. }
  1076. }
  1077. return 0;
  1078. err_probe:
  1079. /* Remove driver from each device that was already probed */
  1080. list_for_each_entry_continue_reverse ( vmdev, &parent->children,
  1081. dev.siblings ) {
  1082. vmdev->driver->remove ( vmdev );
  1083. }
  1084. err_unexpected_offer:
  1085. err_alloc_vmdev:
  1086. err_wait_for_any_message:
  1087. /* Free any devices allocated (but potentially not yet probed) */
  1088. list_for_each_entry_safe ( vmdev, tmp, &parent->children,
  1089. dev.siblings ) {
  1090. list_del ( &vmdev->dev.siblings );
  1091. free ( vmdev );
  1092. }
  1093. err_post_message:
  1094. return rc;
  1095. }
  1096. /**
  1097. * Remove channels
  1098. *
  1099. * @v hv Hyper-V hypervisor
  1100. * @v parent Parent device
  1101. */
  1102. static void vmbus_remove_channels ( struct hv_hypervisor *hv __unused,
  1103. struct device *parent ) {
  1104. struct vmbus_device *vmdev;
  1105. struct vmbus_device *tmp;
  1106. /* Remove devices */
  1107. list_for_each_entry_safe ( vmdev, tmp, &parent->children,
  1108. dev.siblings ) {
  1109. vmdev->driver->remove ( vmdev );
  1110. assert ( list_empty ( &vmdev->dev.children ) );
  1111. assert ( vmdev->out == NULL );
  1112. assert ( vmdev->in == NULL );
  1113. assert ( vmdev->packet == NULL );
  1114. assert ( list_empty ( &vmdev->pages ) );
  1115. list_del ( &vmdev->dev.siblings );
  1116. free ( vmdev );
  1117. }
  1118. }
  1119. /**
  1120. * Probe Hyper-V virtual machine bus
  1121. *
  1122. * @v hv Hyper-V hypervisor
  1123. * @v parent Parent device
  1124. * @ret rc Return status code
  1125. */
  1126. int vmbus_probe ( struct hv_hypervisor *hv, struct device *parent ) {
  1127. struct vmbus *vmbus;
  1128. int rc;
  1129. /* Allocate and initialise structure */
  1130. vmbus = zalloc ( sizeof ( *vmbus ) );
  1131. if ( ! vmbus ) {
  1132. rc = -ENOMEM;
  1133. goto err_alloc;
  1134. }
  1135. hv->vmbus = vmbus;
  1136. /* Initialise message buffer pointer
  1137. *
  1138. * We use a pointer to the fixed-size Hyper-V received message
  1139. * buffer. This allows us to access fields within received
  1140. * messages without first checking the message size: any
  1141. * fields beyond the end of the message will read as zero.
  1142. */
  1143. vmbus->message = ( ( void * ) hv->message->received.data );
  1144. assert ( sizeof ( *vmbus->message ) <=
  1145. sizeof ( hv->message->received.data ) );
  1146. /* Allocate interrupt and monitor pages */
  1147. if ( ( rc = hv_alloc_pages ( hv, &vmbus->intr, &vmbus->monitor_in,
  1148. &vmbus->monitor_out, NULL ) ) != 0 )
  1149. goto err_alloc_pages;
  1150. /* Enable message interrupt */
  1151. hv_enable_sint ( hv, VMBUS_MESSAGE_SINT );
  1152. /* Negotiate protocol version */
  1153. if ( ( rc = vmbus_negotiate_version ( hv ) ) != 0 )
  1154. goto err_negotiate_version;
  1155. /* Enumerate channels */
  1156. if ( ( rc = vmbus_probe_channels ( hv, parent ) ) != 0 )
  1157. goto err_probe_channels;
  1158. return 0;
  1159. vmbus_remove_channels ( hv, parent );
  1160. err_probe_channels:
  1161. vmbus_unload ( hv );
  1162. err_negotiate_version:
  1163. hv_disable_sint ( hv, VMBUS_MESSAGE_SINT );
  1164. hv_free_pages ( hv, vmbus->intr, vmbus->monitor_in, vmbus->monitor_out,
  1165. NULL );
  1166. err_alloc_pages:
  1167. free ( vmbus );
  1168. err_alloc:
  1169. return rc;
  1170. }
  1171. /**
  1172. * Remove Hyper-V virtual machine bus
  1173. *
  1174. * @v hv Hyper-V hypervisor
  1175. * @v parent Parent device
  1176. */
  1177. void vmbus_remove ( struct hv_hypervisor *hv, struct device *parent ) {
  1178. struct vmbus *vmbus = hv->vmbus;
  1179. vmbus_remove_channels ( hv, parent );
  1180. vmbus_unload ( hv );
  1181. hv_disable_sint ( hv, VMBUS_MESSAGE_SINT );
  1182. hv_free_pages ( hv, vmbus->intr, vmbus->monitor_in, vmbus->monitor_out,
  1183. NULL );
  1184. free ( vmbus );
  1185. }