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