Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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