1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
|
/*
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** @file
*
* Hyper-V network virtual service client
*
* The network virtual service client (NetVSC) connects to the network
* virtual service provider (NetVSP) via the Hyper-V virtual machine
* bus (VMBus). It provides a transport layer for RNDIS packets.
*/
#include <errno.h>
#include <unistd.h>
#include <byteswap.h>
#include <ipxe/umalloc.h>
#include <ipxe/rndis.h>
#include <ipxe/vmbus.h>
#include "netvsc.h"
/**
* Send control message and wait for completion
*
* @v netvsc NetVSC device
* @v xrid Relative transaction ID
* @v data Data
* @v len Length of data
* @ret rc Return status code
*/
static int netvsc_control ( struct netvsc_device *netvsc, unsigned int xrid,
const void *data, size_t len ) {
uint64_t xid = ( NETVSC_BASE_XID + xrid );
unsigned int i;
int rc;
/* Send control message */
if ( ( rc = vmbus_send_control ( netvsc->vmdev, xid, data, len ) ) !=0){
DBGC ( netvsc, "NETVSC %s could not send control message: %s\n",
netvsc->name, strerror ( rc ) );
return rc;
}
/* Record transaction ID */
netvsc->wait_xrid = xrid;
/* Wait for operation to complete */
for ( i = 0 ; i < NETVSC_MAX_WAIT_MS ; i++ ) {
/* Check for completion */
if ( ! netvsc->wait_xrid )
return netvsc->wait_rc;
/* Poll VMBus device */
vmbus_poll ( netvsc->vmdev );
/* Delay for 1ms */
mdelay ( 1 );
}
DBGC ( netvsc, "NETVSC %s timed out waiting for XRID %d\n",
netvsc->name, xrid );
vmbus_dump_channel ( netvsc->vmdev );
return -ETIMEDOUT;
}
/**
* Handle generic completion
*
* @v netvsc NetVSC device
* @v data Data
* @v len Length of data
* @ret rc Return status code
*/
static int netvsc_completed ( struct netvsc_device *netvsc __unused,
const void *data __unused, size_t len __unused ) {
return 0;
}
/**
* Initialise communication
*
* @v netvsc NetVSC device
* @ret rc Return status code
*/
static int netvsc_initialise ( struct netvsc_device *netvsc ) {
struct netvsc_init_message msg;
int rc;
/* Construct message */
memset ( &msg, 0, sizeof ( msg ) );
msg.header.type = cpu_to_le32 ( NETVSC_INIT_MSG );
msg.min = cpu_to_le32 ( NETVSC_VERSION_1 );
msg.max = cpu_to_le32 ( NETVSC_VERSION_1 );
/* Send message and wait for completion */
if ( ( rc = netvsc_control ( netvsc, NETVSC_INIT_XRID, &msg,
sizeof ( msg ) ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not initialise: %s\n",
netvsc->name, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Handle initialisation completion
*
* @v netvsc NetVSC device
* @v data Data
* @v len Length of data
* @ret rc Return status code
*/
static int
netvsc_initialised ( struct netvsc_device *netvsc, const void *data,
size_t len ) {
const struct netvsc_init_completion *cmplt = data;
/* Check completion */
if ( len < sizeof ( *cmplt ) ) {
DBGC ( netvsc, "NETVSC %s underlength initialisation "
"completion (%zd bytes)\n", netvsc->name, len );
return -EINVAL;
}
if ( cmplt->header.type != cpu_to_le32 ( NETVSC_INIT_CMPLT ) ) {
DBGC ( netvsc, "NETVSC %s unexpected initialisation completion "
"type %d\n", netvsc->name,
le32_to_cpu ( cmplt->header.type ) );
return -EPROTO;
}
if ( cmplt->status != cpu_to_le32 ( NETVSC_OK ) ) {
DBGC ( netvsc, "NETVSC %s initialisation failure status %d\n",
netvsc->name, le32_to_cpu ( cmplt->status ) );
return -EPROTO;
}
return 0;
}
/**
* Set NDIS version
*
* @v netvsc NetVSC device
* @ret rc Return status code
*/
static int netvsc_ndis_version ( struct netvsc_device *netvsc ) {
struct netvsc_ndis_version_message msg;
int rc;
/* Construct message */
memset ( &msg, 0, sizeof ( msg ) );
msg.header.type = cpu_to_le32 ( NETVSC_NDIS_VERSION_MSG );
msg.major = cpu_to_le32 ( NETVSC_NDIS_MAJOR );
msg.minor = cpu_to_le32 ( NETVSC_NDIS_MINOR );
/* Send message and wait for completion */
if ( ( rc = netvsc_control ( netvsc, NETVSC_NDIS_VERSION_XRID,
&msg, sizeof ( msg ) ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not set NDIS version: %s\n",
netvsc->name, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Establish data buffer
*
* @v netvsc NetVSC device
* @v buffer Data buffer
* @ret rc Return status code
*/
static int netvsc_establish_buffer ( struct netvsc_device *netvsc,
struct netvsc_buffer *buffer ) {
struct netvsc_establish_buffer_message msg;
int rc;
/* Construct message */
memset ( &msg, 0, sizeof ( msg ) );
msg.header.type = cpu_to_le32 ( buffer->establish_type );
msg.gpadl = cpu_to_le32 ( buffer->gpadl );
msg.pageset = buffer->pages.pageset; /* Already protocol-endian */
/* Send message and wait for completion */
if ( ( rc = netvsc_control ( netvsc, buffer->establish_xrid, &msg,
sizeof ( msg ) ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not establish buffer: %s\n",
netvsc->name, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Handle establish receive data buffer completion
*
* @v netvsc NetVSC device
* @v data Data
* @v len Length of data
* @ret rc Return status code
*/
static int netvsc_rx_established_buffer ( struct netvsc_device *netvsc,
const void *data, size_t len ) {
const struct netvsc_rx_establish_buffer_completion *cmplt = data;
/* Check completion */
if ( len < sizeof ( *cmplt ) ) {
DBGC ( netvsc, "NETVSC %s underlength buffer completion (%zd "
"bytes)\n", netvsc->name, len );
return -EINVAL;
}
if ( cmplt->header.type != cpu_to_le32 ( NETVSC_RX_ESTABLISH_CMPLT ) ) {
DBGC ( netvsc, "NETVSC %s unexpected buffer completion type "
"%d\n", netvsc->name, le32_to_cpu ( cmplt->header.type));
return -EPROTO;
}
if ( cmplt->status != cpu_to_le32 ( NETVSC_OK ) ) {
DBGC ( netvsc, "NETVSC %s buffer failure status %d\n",
netvsc->name, le32_to_cpu ( cmplt->status ) );
return -EPROTO;
}
return 0;
}
/**
* Revoke data buffer
*
* @v netvsc NetVSC device
* @v buffer Data buffer
* @ret rc Return status code
*/
static int netvsc_revoke_buffer ( struct netvsc_device *netvsc,
struct netvsc_buffer *buffer ) {
struct netvsc_revoke_buffer_message msg;
int rc;
/* Construct message */
memset ( &msg, 0, sizeof ( msg ) );
msg.header.type = cpu_to_le32 ( buffer->revoke_type );
msg.pageset = buffer->pages.pageset; /* Already protocol-endian */
/* Send message and wait for completion */
if ( ( rc = netvsc_control ( netvsc, buffer->revoke_xrid,
&msg, sizeof ( msg ) ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not revoke buffer: %s\n",
netvsc->name, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Handle received control packet
*
* @v vmdev VMBus device
* @v xid Transaction ID
* @v data Data
* @v len Length of data
* @ret rc Return status code
*/
static int netvsc_recv_control ( struct vmbus_device *vmdev, uint64_t xid,
const void *data, size_t len ) {
struct rndis_device *rndis = vmbus_get_drvdata ( vmdev );
struct netvsc_device *netvsc = rndis->priv;
DBGC ( netvsc, "NETVSC %s received unsupported control packet "
"(%08llx):\n", netvsc->name, xid );
DBGC_HDA ( netvsc, 0, data, len );
return -ENOTSUP;
}
/**
* Handle received data packet
*
* @v vmdev VMBus device
* @v xid Transaction ID
* @v data Data
* @v len Length of data
* @v list List of I/O buffers
* @ret rc Return status code
*/
static int netvsc_recv_data ( struct vmbus_device *vmdev, uint64_t xid,
const void *data, size_t len,
struct list_head *list ) {
struct rndis_device *rndis = vmbus_get_drvdata ( vmdev );
struct netvsc_device *netvsc = rndis->priv;
const struct netvsc_rndis_message *msg = data;
struct io_buffer *iobuf;
struct io_buffer *tmp;
int rc;
/* Sanity check */
if ( len < sizeof ( *msg ) ) {
DBGC ( netvsc, "NETVSC %s received underlength RNDIS packet "
"(%zd bytes)\n", netvsc->name, len );
rc = -EINVAL;
goto err_sanity;
}
if ( msg->header.type != cpu_to_le32 ( NETVSC_RNDIS_MSG ) ) {
DBGC ( netvsc, "NETVSC %s received unexpected RNDIS packet "
"type %d\n", netvsc->name,
le32_to_cpu ( msg->header.type ) );
rc = -EINVAL;
goto err_sanity;
}
/* Send completion back to host */
if ( ( rc = vmbus_send_completion ( vmdev, xid, NULL, 0 ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not send completion: %s\n",
netvsc->name, strerror ( rc ) );
goto err_completion;
}
/* Hand off to RNDIS */
list_for_each_entry_safe ( iobuf, tmp, list, list ) {
list_del ( &iobuf->list );
rndis_rx ( rndis, iob_disown ( iobuf ) );
}
return 0;
err_completion:
err_sanity:
list_for_each_entry_safe ( iobuf, tmp, list, list ) {
list_del ( &iobuf->list );
free_iob ( iobuf );
}
return rc;
}
/**
* Handle received completion packet
*
* @v vmdev VMBus device
* @v xid Transaction ID
* @v data Data
* @v len Length of data
* @ret rc Return status code
*/
static int netvsc_recv_completion ( struct vmbus_device *vmdev, uint64_t xid,
const void *data, size_t len ) {
struct rndis_device *rndis = vmbus_get_drvdata ( vmdev );
struct netvsc_device *netvsc = rndis->priv;
struct io_buffer *iobuf;
int ( * completion ) ( struct netvsc_device *netvsc,
const void *data, size_t len );
unsigned int xrid = ( xid - NETVSC_BASE_XID );
unsigned int tx_id;
int rc;
/* Handle transmit completion, if applicable */
tx_id = ( xrid - NETVSC_TX_BASE_XRID );
if ( ( tx_id < NETVSC_TX_NUM_DESC ) &&
( ( iobuf = netvsc->tx.iobufs[tx_id] ) != NULL ) ) {
/* Free buffer ID */
netvsc->tx.iobufs[tx_id] = NULL;
netvsc->tx.ids[ ( netvsc->tx.id_cons++ ) &
( netvsc->tx.count - 1 ) ] = tx_id;
/* Hand back to RNDIS */
rndis_tx_complete ( rndis, iobuf );
return 0;
}
/* Otherwise determine completion handler */
if ( xrid == NETVSC_INIT_XRID ) {
completion = netvsc_initialised;
} else if ( xrid == NETVSC_RX_ESTABLISH_XRID ) {
completion = netvsc_rx_established_buffer;
} else if ( ( netvsc->wait_xrid != 0 ) &&
( xrid == netvsc->wait_xrid ) ) {
completion = netvsc_completed;
} else {
DBGC ( netvsc, "NETVSC %s received unexpected completion "
"(%08llx)\n", netvsc->name, xid );
return -EPIPE;
}
/* Hand off to completion handler */
rc = completion ( netvsc, data, len );
/* Record completion handler result if applicable */
if ( xrid == netvsc->wait_xrid ) {
netvsc->wait_xrid = 0;
netvsc->wait_rc = rc;
}
return rc;
}
/**
* Handle received cancellation packet
*
* @v vmdev VMBus device
* @v xid Transaction ID
* @ret rc Return status code
*/
static int netvsc_recv_cancellation ( struct vmbus_device *vmdev,
uint64_t xid ) {
struct rndis_device *rndis = vmbus_get_drvdata ( vmdev );
struct netvsc_device *netvsc = rndis->priv;
DBGC ( netvsc, "NETVSC %s received unsupported cancellation packet "
"(%08llx):\n", netvsc->name, xid );
return -ENOTSUP;
}
/** VMBus channel operations */
static struct vmbus_channel_operations netvsc_channel_operations = {
.recv_control = netvsc_recv_control,
.recv_data = netvsc_recv_data,
.recv_completion = netvsc_recv_completion,
.recv_cancellation = netvsc_recv_cancellation,
};
/**
* Poll for completed and received packets
*
* @v rndis RNDIS device
*/
static void netvsc_poll ( struct rndis_device *rndis ) {
struct netvsc_device *netvsc = rndis->priv;
struct vmbus_device *vmdev = netvsc->vmdev;
/* Poll VMBus device */
while ( vmbus_has_data ( vmdev ) )
vmbus_poll ( vmdev );
}
/**
* Transmit packet
*
* @v rndis RNDIS device
* @v iobuf I/O buffer
* @ret rc Return status code
*
* If this method returns success then the RNDIS device must
* eventually report completion via rndis_tx_complete().
*/
static int netvsc_transmit ( struct rndis_device *rndis,
struct io_buffer *iobuf ) {
struct netvsc_device *netvsc = rndis->priv;
struct rndis_header *header = iobuf->data;
struct netvsc_rndis_message msg;
unsigned int tx_id;
unsigned int xrid;
uint64_t xid;
int rc;
/* Sanity check */
assert ( iob_len ( iobuf ) >= sizeof ( *header ) );
assert ( iob_len ( iobuf ) == le32_to_cpu ( header->len ) );
/* Check that we have space in the transmit ring */
if ( netvsc_ring_is_full ( &netvsc->tx ) )
return rndis_tx_defer ( rndis, iobuf );
/* Allocate buffer ID and calculate transaction ID */
tx_id = netvsc->tx.ids[ netvsc->tx.id_prod & ( netvsc->tx.count - 1 ) ];
assert ( netvsc->tx.iobufs[tx_id] == NULL );
xrid = ( NETVSC_TX_BASE_XRID + tx_id );
xid = ( NETVSC_BASE_XID + xrid );
/* Construct message */
memset ( &msg, 0, sizeof ( msg ) );
msg.header.type = cpu_to_le32 ( NETVSC_RNDIS_MSG );
msg.channel = ( ( header->type == cpu_to_le32 ( RNDIS_PACKET_MSG ) ) ?
NETVSC_RNDIS_DATA : NETVSC_RNDIS_CONTROL );
msg.buffer = cpu_to_le32 ( NETVSC_RNDIS_NO_BUFFER );
/* Send message */
if ( ( rc = vmbus_send_data ( netvsc->vmdev, xid, &msg, sizeof ( msg ),
iobuf ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not send RNDIS message: %s\n",
netvsc->name, strerror ( rc ) );
return rc;
}
/* Store I/O buffer and consume buffer ID */
netvsc->tx.iobufs[tx_id] = iobuf;
netvsc->tx.id_prod++;
return 0;
}
/**
* Cancel transmission
*
* @v netvsc NetVSC device
* @v iobuf I/O buffer
* @v tx_id Transmission ID
*/
static void netvsc_cancel_transmit ( struct netvsc_device *netvsc,
struct io_buffer *iobuf,
unsigned int tx_id ) {
unsigned int xrid;
uint64_t xid;
/* Send cancellation */
xrid = ( NETVSC_TX_BASE_XRID + tx_id );
xid = ( NETVSC_BASE_XID + xrid );
DBGC ( netvsc, "NETVSC %s cancelling transmission %#x\n",
netvsc->name, tx_id );
vmbus_send_cancellation ( netvsc->vmdev, xid );
/* Report back to RNDIS */
rndis_tx_complete_err ( netvsc->rndis, iobuf, -ECANCELED );
}
/**
* Create descriptor ring
*
* @v netvsc NetVSC device
* @v ring Descriptor ring
* @ret rc Return status code
*/
static int netvsc_create_ring ( struct netvsc_device *netvsc __unused,
struct netvsc_ring *ring ) {
unsigned int i;
/* Initialise buffer ID ring */
for ( i = 0 ; i < ring->count ; i++ ) {
ring->ids[i] = i;
assert ( ring->iobufs[i] == NULL );
}
ring->id_prod = 0;
ring->id_cons = 0;
return 0;
}
/**
* Destroy descriptor ring
*
* @v netvsc NetVSC device
* @v ring Descriptor ring
* @v discard Method used to discard outstanding buffer, or NULL
*/
static void netvsc_destroy_ring ( struct netvsc_device *netvsc,
struct netvsc_ring *ring,
void ( * discard ) ( struct netvsc_device *,
struct io_buffer *,
unsigned int ) ) {
struct io_buffer *iobuf;
unsigned int i;
/* Flush any outstanding buffers */
for ( i = 0 ; i < ring->count ; i++ ) {
iobuf = ring->iobufs[i];
if ( ! iobuf )
continue;
ring->iobufs[i] = NULL;
ring->ids[ ( ring->id_cons++ ) & ( ring->count - 1 ) ] = i;
if ( discard )
discard ( netvsc, iobuf, i );
}
/* Sanity check */
assert ( netvsc_ring_is_empty ( ring ) );
}
/**
* Copy data from data buffer
*
* @v pages Transfer page set
* @v data Data buffer
* @v offset Offset within page set
* @v len Length within page set
* @ret rc Return status code
*/
static int netvsc_buffer_copy ( struct vmbus_xfer_pages *pages, void *data,
size_t offset, size_t len ) {
struct netvsc_buffer *buffer =
container_of ( pages, struct netvsc_buffer, pages );
/* Sanity check */
if ( ( offset > buffer->len ) || ( len > ( buffer->len - offset ) ) )
return -ERANGE;
/* Copy data from buffer */
copy_from_user ( data, buffer->data, offset, len );
return 0;
}
/** Transfer page set operations */
static struct vmbus_xfer_pages_operations netvsc_xfer_pages_operations = {
.copy = netvsc_buffer_copy,
};
/**
* Create data buffer
*
* @v netvsc NetVSC device
* @v buffer Data buffer
* @ret rc Return status code
*/
static int netvsc_create_buffer ( struct netvsc_device *netvsc,
struct netvsc_buffer *buffer ) {
struct vmbus_device *vmdev = netvsc->vmdev;
int gpadl;
int rc;
/* Allocate receive buffer */
buffer->data = umalloc ( buffer->len );
if ( ! buffer->data ) {
DBGC ( netvsc, "NETVSC %s could not allocate %zd-byte buffer\n",
netvsc->name, buffer->len );
rc = -ENOMEM;
goto err_alloc;
}
/* Establish GPA descriptor list */
gpadl = vmbus_establish_gpadl ( vmdev, buffer->data, buffer->len );
if ( gpadl < 0 ) {
rc = gpadl;
DBGC ( netvsc, "NETVSC %s could not establish GPADL: %s\n",
netvsc->name, strerror ( rc ) );
goto err_establish_gpadl;
}
buffer->gpadl = gpadl;
/* Register transfer page set */
if ( ( rc = vmbus_register_pages ( vmdev, &buffer->pages ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not register transfer pages: "
"%s\n", netvsc->name, strerror ( rc ) );
goto err_register_pages;
}
return 0;
vmbus_unregister_pages ( vmdev, &buffer->pages );
err_register_pages:
vmbus_gpadl_teardown ( vmdev, gpadl );
err_establish_gpadl:
ufree ( buffer->data );
err_alloc:
return rc;
}
/**
* Destroy data buffer
*
* @v netvsc NetVSC device
* @v buffer Data buffer
*/
static void netvsc_destroy_buffer ( struct netvsc_device *netvsc,
struct netvsc_buffer *buffer ) {
struct vmbus_device *vmdev = netvsc->vmdev;
int rc;
/* Unregister transfer pages */
vmbus_unregister_pages ( vmdev, &buffer->pages );
/* Tear down GPA descriptor list */
if ( ( rc = vmbus_gpadl_teardown ( vmdev, buffer->gpadl ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not tear down GPADL: %s\n",
netvsc->name, strerror ( rc ) );
/* Death is imminent. The host may well continue to
* write to the data buffer. The best we can do is
* leak memory for now and hope that the host doesn't
* write to this region after we load an OS.
*/
return;
}
/* Free buffer */
ufree ( buffer->data );
}
/**
* Open device
*
* @v rndis RNDIS device
* @ret rc Return status code
*/
static int netvsc_open ( struct rndis_device *rndis ) {
struct netvsc_device *netvsc = rndis->priv;
int rc;
/* Initialise receive buffer */
if ( ( rc = netvsc_create_buffer ( netvsc, &netvsc->rx ) ) != 0 )
goto err_create_rx;
/* Open channel */
if ( ( rc = vmbus_open ( netvsc->vmdev, &netvsc_channel_operations,
PAGE_SIZE, PAGE_SIZE, NETVSC_MTU ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not open VMBus: %s\n",
netvsc->name, strerror ( rc ) );
goto err_vmbus_open;
}
/* Initialise communication with NetVSP */
if ( ( rc = netvsc_initialise ( netvsc ) ) != 0 )
goto err_initialise;
if ( ( rc = netvsc_ndis_version ( netvsc ) ) != 0 )
goto err_ndis_version;
/* Initialise transmit ring */
if ( ( rc = netvsc_create_ring ( netvsc, &netvsc->tx ) ) != 0 )
goto err_create_tx;
/* Establish receive buffer */
if ( ( rc = netvsc_establish_buffer ( netvsc, &netvsc->rx ) ) != 0 )
goto err_establish_rx;
return 0;
netvsc_revoke_buffer ( netvsc, &netvsc->rx );
err_establish_rx:
netvsc_destroy_ring ( netvsc, &netvsc->tx, NULL );
err_create_tx:
err_ndis_version:
err_initialise:
vmbus_close ( netvsc->vmdev );
err_vmbus_open:
netvsc_destroy_buffer ( netvsc, &netvsc->rx );
err_create_rx:
return rc;
}
/**
* Close device
*
* @v rndis RNDIS device
*/
static void netvsc_close ( struct rndis_device *rndis ) {
struct netvsc_device *netvsc = rndis->priv;
/* Revoke receive buffer */
netvsc_revoke_buffer ( netvsc, &netvsc->rx );
/* Destroy transmit ring */
netvsc_destroy_ring ( netvsc, &netvsc->tx, netvsc_cancel_transmit );
/* Close channel */
vmbus_close ( netvsc->vmdev );
/* Destroy receive buffer */
netvsc_destroy_buffer ( netvsc, &netvsc->rx );
}
/** RNDIS operations */
static struct rndis_operations netvsc_operations = {
.open = netvsc_open,
.close = netvsc_close,
.transmit = netvsc_transmit,
.poll = netvsc_poll,
};
/**
* Probe device
*
* @v vmdev VMBus device
* @ret rc Return status code
*/
static int netvsc_probe ( struct vmbus_device *vmdev ) {
struct netvsc_device *netvsc;
struct rndis_device *rndis;
int rc;
/* Allocate and initialise structure */
rndis = alloc_rndis ( sizeof ( *netvsc ) );
if ( ! rndis ) {
rc = -ENOMEM;
goto err_alloc;
}
rndis_init ( rndis, &netvsc_operations );
rndis->netdev->dev = &vmdev->dev;
netvsc = rndis->priv;
netvsc->vmdev = vmdev;
netvsc->rndis = rndis;
netvsc->name = vmdev->dev.name;
netvsc_init_ring ( &netvsc->tx, NETVSC_TX_NUM_DESC,
netvsc->tx_iobufs, netvsc->tx_ids );
netvsc_init_buffer ( &netvsc->rx, NETVSC_RX_BUF_PAGESET,
&netvsc_xfer_pages_operations,
NETVSC_RX_ESTABLISH_MSG, NETVSC_RX_ESTABLISH_XRID,
NETVSC_RX_REVOKE_MSG, NETVSC_RX_REVOKE_XRID,
NETVSC_RX_BUF_LEN );
vmbus_set_drvdata ( vmdev, rndis );
/* Register RNDIS device */
if ( ( rc = register_rndis ( rndis ) ) != 0 ) {
DBGC ( netvsc, "NETVSC %s could not register: %s\n",
netvsc->name, strerror ( rc ) );
goto err_register;
}
return 0;
unregister_rndis ( rndis );
err_register:
free_rndis ( rndis );
err_alloc:
return rc;
}
/**
* Remove device
*
* @v vmdev VMBus device
*/
static void netvsc_remove ( struct vmbus_device *vmdev ) {
struct rndis_device *rndis = vmbus_get_drvdata ( vmdev );
/* Unregister RNDIS device */
unregister_rndis ( rndis );
/* Free RNDIS device */
free_rndis ( rndis );
}
/** NetVSC driver */
struct vmbus_driver netvsc_driver __vmbus_driver = {
.name = "netvsc",
.type = VMBUS_TYPE ( 0xf8615163, 0xdf3e, 0x46c5, 0x913f,
0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e ),
.probe = netvsc_probe,
.remove = netvsc_remove,
};
|