]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/DriverBinding.c
c9259ab3397b2451d5100e4296bb40255e43afa2
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / DriverBinding.c
1 /** @file
2
3 Driver Binding code and its private helpers for the virtio-net driver.
4
5 Copyright (C) 2013, Red Hat, Inc.
6 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
7
8 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include <IndustryStandard/Pci.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DevicePathLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23
24 #include "VirtioNet.h"
25
26 #define RECEIVE_FILTERS_NO_MCAST ((UINT32) ( \
27 EFI_SIMPLE_NETWORK_RECEIVE_UNICAST | \
28 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST | \
29 EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS \
30 ))
31
32 /*
33 Temporarily enable then reset the virtio-net device in order to retrieve
34 configuration values needed by Simple Network Protocol and Simple Network
35 Mode fields.
36
37 Only VirtioNetSnpPopulate() may call this function.
38
39 If the function fails for any reason, the virtio-net device is moved to
40 VSTAT_FAILED instead of being reset. This serves only informative purposes
41 for the host side.
42
43 param[in,out] Dev The VNET_DEV structure being created for
44 the virtio-net device.
45 param[out] MacAddress MAC address configured by the host.
46 param[out] MediaPresentSupported Link status is made available by the host.
47 param[out] MediaPresent If link status is made available by the
48 host, the current link status is stored in
49 *MediaPresent. Otherwise MediaPresent is
50 unused.
51
52 @retval EFI_UNSUPPORTED The host doesn't supply a MAC address.
53 @return Status codes from Dev->PciIo->Io.Read(),
54 VIRTIO_CFG_READ() and VIRTIO_CFG_WRITE().
55 @retval EFI_SUCCESS Configuration values retrieved.
56 */
57 STATIC
58 EFI_STATUS
59 EFIAPI
60 VirtioNetGetFeatures (
61 IN OUT VNET_DEV *Dev,
62 OUT EFI_MAC_ADDRESS *MacAddress,
63 OUT BOOLEAN *MediaPresentSupported,
64 OUT BOOLEAN *MediaPresent
65 )
66 {
67 EFI_STATUS Status;
68 UINT8 NextDevStat;
69 UINT32 Features;
70 UINT16 LinkStatus;
71
72 //
73 // Interrogate the device for features (virtio-0.9.5, 2.2.1 Device
74 // Initialization Sequence), but don't complete setting it up.
75 //
76 NextDevStat = 0; // step 1 -- reset device
77 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
78 if (EFI_ERROR (Status)) {
79 return Status;
80 }
81
82 NextDevStat |= VSTAT_ACK; // step 2 -- acknowledge device presence
83 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
84 if (EFI_ERROR (Status)) {
85 goto YieldDevice;
86 }
87
88 NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it
89 Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
90 if (EFI_ERROR (Status)) {
91 goto YieldDevice;
92 }
93
94 //
95 // step 4a -- retrieve and validate features
96 //
97 Status = VIRTIO_CFG_READ (Dev, Generic.VhdrDeviceFeatureBits, &Features);
98 if (EFI_ERROR (Status)) {
99 goto YieldDevice;
100 }
101
102 //
103 // get MAC address byte-wise
104 //
105 if ((Features & VIRTIO_NET_F_MAC) == 0) {
106 Status = EFI_UNSUPPORTED;
107 goto YieldDevice;
108 }
109 Status = Dev->PciIo->Io.Read (Dev->PciIo, // PciIo
110 EfiPciIoWidthUint8, // Width
111 PCI_BAR_IDX0, // BarIndex
112 OFFSET_OF_VNET (VhdrMac), // Offset
113 SIZE_OF_VNET (VhdrMac), // Count
114 MacAddress // Buffer
115 );
116
117 if (EFI_ERROR (Status)) {
118 goto YieldDevice;
119 }
120
121 //
122 // check if link status is reported, and if so, what the link status is
123 //
124 if ((Features & VIRTIO_NET_F_STATUS) == 0) {
125 *MediaPresentSupported = FALSE;
126 }
127 else {
128 *MediaPresentSupported = TRUE;
129 Status = VIRTIO_CFG_READ (Dev, VhdrLinkStatus, &LinkStatus);
130 if (EFI_ERROR (Status)) {
131 goto YieldDevice;
132 }
133 *MediaPresent = !!(LinkStatus & VIRTIO_NET_S_LINK_UP);
134 }
135
136 YieldDevice:
137 VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus,
138 EFI_ERROR (Status) ? VSTAT_FAILED : 0);
139
140 return Status;
141 }
142
143
144 /**
145 Set up the Simple Network Protocol fields, the Simple Network Mode fields,
146 and the Exit Boot Services Event of the virtio-net driver instance.
147
148 This function may only be called by VirtioNetDriverBindingStart().
149
150 @param[in,out] Dev The VNET_DEV driver instance being created for the
151 virtio-net device.
152
153 @return Status codes from the CreateEvent() boot service or the
154 VirtioNetGetFeatures() function.
155 @retval EFI_SUCCESS Configuration successful.
156 */
157 STATIC
158 EFI_STATUS
159 EFIAPI
160 VirtioNetSnpPopulate (
161 IN OUT VNET_DEV *Dev
162 )
163 {
164 EFI_STATUS Status;
165
166 //
167 // We set up a function here that is asynchronously callable by an
168 // external application to check if there are any packets available for
169 // reception. The least urgent task priority level we can specify for such a
170 // "software interrupt" is TPL_CALLBACK.
171 //
172 // TPL_CALLBACK is also the maximum TPL an SNP implementation is allowed to
173 // run at (see 6.1 Event, Timer, and Task Priority Services in the UEFI
174 // Specification 2.3.1+errC).
175 //
176 // Since we raise our TPL to TPL_CALLBACK in every single function that
177 // accesses the device, and the external application also queues its interest
178 // for received packets at the same TPL_CALLBACK, in effect the
179 // VirtioNetIsPacketAvailable() function will never interrupt any
180 // device-accessing driver function, it will be scheduled in isolation.
181 //
182 // TPL_CALLBACK (which basically this entire driver runs at) is allowed
183 // for "[l]ong term operations (such as file system operations and disk
184 // I/O)". Because none of our functions block, we'd satisfy an even stronger
185 // requirement.
186 //
187 Status = gBS->CreateEvent (EVT_NOTIFY_WAIT, TPL_CALLBACK,
188 &VirtioNetIsPacketAvailable, Dev, &Dev->Snp.WaitForPacket);
189 if (EFI_ERROR (Status)) {
190 return Status;
191 }
192
193 Dev->Snp.Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
194 Dev->Snp.Start = &VirtioNetStart;
195 Dev->Snp.Stop = &VirtioNetStop;
196 Dev->Snp.Initialize = &VirtioNetInitialize;
197 Dev->Snp.Reset = &VirtioNetReset;
198 Dev->Snp.Shutdown = &VirtioNetShutdown;
199 Dev->Snp.ReceiveFilters = &VirtioNetReceiveFilters;
200 Dev->Snp.StationAddress = &VirtioNetStationAddress;
201 Dev->Snp.Statistics = &VirtioNetStatistics;
202 Dev->Snp.MCastIpToMac = &VirtioNetMcastIpToMac;
203 Dev->Snp.NvData = &VirtioNetNvData;
204 Dev->Snp.GetStatus = &VirtioNetGetStatus;
205 Dev->Snp.Transmit = &VirtioNetTransmit;
206 Dev->Snp.Receive = &VirtioNetReceive;
207 Dev->Snp.Mode = &Dev->Snm;
208
209 Dev->Snm.State = EfiSimpleNetworkStopped;
210 Dev->Snm.HwAddressSize = SIZE_OF_VNET (VhdrMac);
211 Dev->Snm.MediaHeaderSize = SIZE_OF_VNET (VhdrMac) + // dst MAC
212 SIZE_OF_VNET (VhdrMac) + // src MAC
213 2; // Ethertype
214 Dev->Snm.MaxPacketSize = 1500;
215 Dev->Snm.NvRamSize = 0;
216 Dev->Snm.NvRamAccessSize = 0;
217 Dev->Snm.ReceiveFilterMask = RECEIVE_FILTERS_NO_MCAST;
218 Dev->Snm.ReceiveFilterSetting = RECEIVE_FILTERS_NO_MCAST;
219 Dev->Snm.MaxMCastFilterCount = 0;
220 Dev->Snm.MCastFilterCount = 0;
221 Dev->Snm.IfType = 1; // ethernet
222 Dev->Snm.MacAddressChangeable = FALSE;
223 Dev->Snm.MultipleTxSupported = TRUE;
224
225 ASSERT (SIZE_OF_VNET (VhdrMac) <= sizeof (EFI_MAC_ADDRESS));
226
227 Status = VirtioNetGetFeatures (Dev, &Dev->Snm.CurrentAddress,
228 &Dev->Snm.MediaPresentSupported, &Dev->Snm.MediaPresent);
229 if (EFI_ERROR (Status)) {
230 goto CloseWaitForPacket;
231 }
232 CopyMem (&Dev->Snm.PermanentAddress, &Dev->Snm.CurrentAddress,
233 SIZE_OF_VNET (VhdrMac));
234 SetMem (&Dev->Snm.BroadcastAddress, SIZE_OF_VNET (VhdrMac), 0xFF);
235
236 //
237 // VirtioNetExitBoot() is queued by ExitBootServices(); its purpose is to
238 // cancel any pending virtio requests. The TPL_CALLBACK reasoning is
239 // identical to the one above. There's one difference: this kind of
240 // event is "globally visible", which means it can be signalled as soon as
241 // we create it. We haven't raised our TPL here, hence VirtioNetExitBoot()
242 // could be entered immediately. VirtioNetExitBoot() checks Dev->Snm.State,
243 // so we're safe.
244 //
245 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
246 &VirtioNetExitBoot, Dev, &Dev->ExitBoot);
247 if (EFI_ERROR (Status)) {
248 goto CloseWaitForPacket;
249 }
250
251 return EFI_SUCCESS;
252
253 CloseWaitForPacket:
254 gBS->CloseEvent (Dev->Snp.WaitForPacket);
255 return Status;
256 }
257
258
259 /**
260 Release any resources allocated by VirtioNetSnpPopulate().
261
262 This function may only be called by VirtioNetDriverBindingStart(), when
263 rolling back a partial, failed driver instance creation, and by
264 VirtioNetDriverBindingStop(), when disconnecting a virtio-net device from the
265 driver.
266
267 @param[in,out] Dev The VNET_DEV driver instance being destroyed.
268 */
269 STATIC
270 VOID
271 EFIAPI
272 VirtioNetSnpEvacuate (
273 IN OUT VNET_DEV *Dev
274 )
275 {
276 //
277 // This function runs either at TPL_CALLBACK already (from
278 // VirtioNetDriverBindingStop()), or it is part of a teardown following
279 // a partial, failed construction in VirtioNetDriverBindingStart(), when
280 // WaitForPacket was never accessible to the world.
281 //
282 gBS->CloseEvent (Dev->ExitBoot);
283 gBS->CloseEvent (Dev->Snp.WaitForPacket);
284 }
285
286
287 /**
288 Tests to see if this driver supports a given controller. If a child device is
289 provided, it further tests to see if this driver supports creating a handle
290 for the specified child device.
291
292 This function checks to see if the driver specified by This supports the
293 device specified by ControllerHandle. Drivers will typically use the device
294 path attached to ControllerHandle and/or the services from the bus I/O
295 abstraction attached to ControllerHandle to determine if the driver supports
296 ControllerHandle. This function may be called many times during platform
297 initialization. In order to reduce boot times, the tests performed by this
298 function must be very small, and take as little time as possible to execute.
299 This function must not change the state of any hardware devices, and this
300 function must be aware that the device specified by ControllerHandle may
301 already be managed by the same driver or a different driver. This function
302 must match its calls to AllocatePages() with FreePages(), AllocatePool() with
303 FreePool(), and OpenProtocol() with CloseProtocol(). Because ControllerHandle
304 may have been previously started by the same driver, if a protocol is already
305 in the opened state, then it must not be closed with CloseProtocol(). This is
306 required to guarantee the state of ControllerHandle is not modified by this
307 function.
308
309 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL
310 instance.
311 @param[in] ControllerHandle The handle of the controller to test. This
312 handle must support a protocol interface
313 that supplies an I/O abstraction to the
314 driver.
315 @param[in] RemainingDevicePath A pointer to the remaining portion of a
316 device path. This parameter is ignored by
317 device drivers, and is optional for bus
318 drivers. For bus drivers, if this parameter
319 is not NULL, then the bus driver must
320 determine if the bus controller specified by
321 ControllerHandle and the child controller
322 specified by RemainingDevicePath are both
323 supported by this bus driver.
324
325 @retval EFI_SUCCESS The device specified by ControllerHandle and
326 RemainingDevicePath is supported by the
327 driver specified by This.
328 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
329 RemainingDevicePath is already being managed
330 by the driver specified by This.
331 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
332 RemainingDevicePath is already being managed
333 by a different driver or an application that
334 requires exclusive access. Currently not
335 implemented.
336 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
337 RemainingDevicePath is not supported by the
338 driver specified by This.
339 **/
340
341 STATIC
342 EFI_STATUS
343 EFIAPI
344 VirtioNetDriverBindingSupported (
345 IN EFI_DRIVER_BINDING_PROTOCOL *This,
346 IN EFI_HANDLE DeviceHandle,
347 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
348 )
349 {
350 EFI_STATUS Status;
351 EFI_PCI_IO_PROTOCOL *PciIo;
352 PCI_TYPE00 Pci;
353
354 Status = gBS->OpenProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
355 (VOID **)&PciIo, This->DriverBindingHandle, DeviceHandle,
356 EFI_OPEN_PROTOCOL_BY_DRIVER);
357 if (EFI_ERROR (Status)) {
358 return Status;
359 }
360
361 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0,
362 sizeof Pci / sizeof (UINT32), &Pci);
363
364 //
365 // virtio-0.9.5, 2.1 PCI Discovery:
366 // the network device has Subsystem Device ID 1
367 //
368 if (Status == EFI_SUCCESS) {
369 Status = (Pci.Hdr.VendorId == 0x1AF4 &&
370 Pci.Hdr.DeviceId >= 0x1000 && Pci.Hdr.DeviceId <= 0x103F &&
371 Pci.Hdr.RevisionID == 0x00 &&
372 Pci.Device.SubsystemID == VIRTIO_SUBSYSTEM_NETWORK_CARD) ? EFI_SUCCESS : EFI_UNSUPPORTED;
373 }
374
375 gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
376 This->DriverBindingHandle, DeviceHandle);
377 return Status;
378 }
379
380
381 /**
382 Starts a device controller or a bus controller.
383
384 The Start() function is designed to be invoked from the EFI boot service
385 ConnectController(). As a result, much of the error checking on the
386 parameters to Start() has been moved into this common boot service. It is
387 legal to call Start() from other locations, but the following calling
388 restrictions must be followed, or the system behavior will not be
389 deterministic.
390 1. ControllerHandle must be a valid EFI_HANDLE.
391 2. If RemainingDevicePath is not NULL, then it must be a pointer to a
392 naturally aligned EFI_DEVICE_PATH_PROTOCOL.
393 3. Prior to calling Start(), the Supported() function for the driver
394 specified by This must have been called with the same calling parameters,
395 and Supported() must have returned EFI_SUCCESS.
396
397 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL
398 instance.
399 @param[in] ControllerHandle The handle of the controller to start. This
400 handle must support a protocol interface
401 that supplies an I/O abstraction to the
402 driver.
403 @param[in] RemainingDevicePath A pointer to the remaining portion of a
404 device path. This parameter is ignored by
405 device drivers, and is optional for bus
406 drivers. For a bus driver, if this parameter
407 is NULL, then handles for all the children
408 of Controller are created by this driver.
409 If this parameter is not NULL and the first
410 Device Path Node is not the End of Device
411 Path Node, then only the handle for the
412 child device specified by the first Device
413 Path Node of RemainingDevicePath is created
414 by this driver. If the first Device Path
415 Node of RemainingDevicePath is the End of
416 Device Path Node, no child handle is created
417 by this driver.
418
419 @retval EFI_SUCCESS The device was started.
420 @retval EFI_DEVICE_ERROR The device could not be started due to a
421 device error.Currently not implemented.
422 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
423 lack of resources.
424 @retval Others The driver failded to start the device.
425
426 **/
427
428 STATIC
429 EFI_STATUS
430 EFIAPI
431 VirtioNetDriverBindingStart (
432 IN EFI_DRIVER_BINDING_PROTOCOL *This,
433 IN EFI_HANDLE DeviceHandle,
434 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
435 )
436 {
437 EFI_STATUS Status;
438 VNET_DEV *Dev;
439 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
440 MAC_ADDR_DEVICE_PATH MacNode;
441 VOID *ChildPciIo;
442
443 //
444 // allocate space for the driver instance
445 //
446 Dev = (VNET_DEV *) AllocateZeroPool (sizeof *Dev);
447 if (Dev == NULL) {
448 return EFI_OUT_OF_RESOURCES;
449 }
450 Dev->Signature = VNET_SIG;
451
452 //
453 // get PCI access to the device and keep it open
454 //
455 Status = gBS->OpenProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
456 (VOID **)&Dev->PciIo, This->DriverBindingHandle,
457 DeviceHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);
458 if (EFI_ERROR (Status)) {
459 goto FreeVirtioNet;
460 }
461
462 //
463 // save original PCI attributes and enable IO space access
464 //
465 Status = Dev->PciIo->Attributes (Dev->PciIo, EfiPciIoAttributeOperationGet,
466 0, &Dev->OrigPciAttributes);
467 if (EFI_ERROR (Status)) {
468 goto ClosePciIo;
469 }
470
471 Status = Dev->PciIo->Attributes (Dev->PciIo,
472 EfiPciIoAttributeOperationEnable,
473 EFI_PCI_IO_ATTRIBUTE_IO, NULL);
474 if (EFI_ERROR (Status)) {
475 goto ClosePciIo;
476 }
477
478 //
479 // now we can run a basic one-shot virtio-net initialization required to
480 // retrieve the MAC address
481 //
482 Status = VirtioNetSnpPopulate (Dev);
483 if (EFI_ERROR (Status)) {
484 goto RestorePciAttributes;
485 }
486
487 //
488 // get the device path of the virtio-net PCI device -- one-shot open
489 //
490 Status = gBS->OpenProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid,
491 (VOID **)&DevicePath, This->DriverBindingHandle,
492 DeviceHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
493 if (EFI_ERROR (Status)) {
494 goto Evacuate;
495 }
496
497 //
498 // create another device path that has the MAC address appended
499 //
500 MacNode.Header.Type = MESSAGING_DEVICE_PATH;
501 MacNode.Header.SubType = MSG_MAC_ADDR_DP;
502 SetDevicePathNodeLength (&MacNode, sizeof MacNode);
503 CopyMem (&MacNode.MacAddress, &Dev->Snm.CurrentAddress,
504 sizeof (EFI_MAC_ADDRESS));
505 MacNode.IfType = Dev->Snm.IfType;
506
507 Dev->MacDevicePath = AppendDevicePathNode (DevicePath, &MacNode.Header);
508 if (Dev->MacDevicePath == NULL) {
509 Status = EFI_OUT_OF_RESOURCES;
510 goto Evacuate;
511 }
512
513 //
514 // create a child handle with the Simple Network Protocol and the new
515 // device path installed on it
516 //
517 Status = gBS->InstallMultipleProtocolInterfaces (&Dev->MacHandle,
518 &gEfiSimpleNetworkProtocolGuid, &Dev->Snp,
519 &gEfiDevicePathProtocolGuid, Dev->MacDevicePath,
520 NULL);
521 if (EFI_ERROR (Status)) {
522 goto FreeMacDevicePath;
523 }
524
525 //
526 // make a note that we keep this device open with PciIo for the sake of this
527 // child
528 //
529 Status = gBS->OpenProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
530 &ChildPciIo, This->DriverBindingHandle,
531 Dev->MacHandle, EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
532 if (EFI_ERROR (Status)) {
533 goto UninstallMultiple;
534 }
535
536 return EFI_SUCCESS;
537
538 UninstallMultiple:
539 gBS->UninstallMultipleProtocolInterfaces (Dev->MacHandle,
540 &gEfiDevicePathProtocolGuid, Dev->MacDevicePath,
541 &gEfiSimpleNetworkProtocolGuid, &Dev->Snp,
542 NULL);
543
544 FreeMacDevicePath:
545 FreePool (Dev->MacDevicePath);
546
547 Evacuate:
548 VirtioNetSnpEvacuate (Dev);
549
550 RestorePciAttributes:
551 Dev->PciIo->Attributes (Dev->PciIo, EfiPciIoAttributeOperationSet,
552 Dev->OrigPciAttributes, NULL);
553
554 ClosePciIo:
555 gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
556 This->DriverBindingHandle, DeviceHandle);
557
558 FreeVirtioNet:
559 FreePool (Dev);
560
561 return Status;
562 }
563
564
565 /**
566 Stops a device controller or a bus controller.
567
568 The Stop() function is designed to be invoked from the EFI boot service
569 DisconnectController(). As a result, much of the error checking on the
570 parameters to Stop() has been moved into this common boot service. It is
571 legal to call Stop() from other locations, but the following calling
572 restrictions must be followed, or the system behavior will not be
573 deterministic.
574 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous
575 call to this same driver's Start() function.
576 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a
577 valid EFI_HANDLE. In addition, all of these handles must have been created
578 in this driver's Start() function, and the Start() function must have
579 called OpenProtocol() on ControllerHandle with an Attribute of
580 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
581
582 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL
583 instance.
584 @param[in] ControllerHandle A handle to the device being stopped. The
585 handle must support a bus specific I/O
586 protocol for the driver to use to stop the
587 device.
588 @param[in] NumberOfChildren The number of child device handles in
589 ChildHandleBuffer.
590 @param[in] ChildHandleBuffer An array of child handles to be freed. May be
591 NULL if NumberOfChildren is 0.
592
593 @retval EFI_SUCCESS The device was stopped.
594 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device
595 error.
596
597 **/
598 STATIC
599 EFI_STATUS
600 EFIAPI
601 VirtioNetDriverBindingStop (
602 IN EFI_DRIVER_BINDING_PROTOCOL *This,
603 IN EFI_HANDLE DeviceHandle,
604 IN UINTN NumberOfChildren,
605 IN EFI_HANDLE *ChildHandleBuffer
606 )
607 {
608 if (NumberOfChildren > 0) {
609 //
610 // free all resources for whose access we need the child handle, because
611 // the child handle is going away
612 //
613 EFI_STATUS Status;
614 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
615 VNET_DEV *Dev;
616 EFI_TPL OldTpl;
617
618 ASSERT (NumberOfChildren == 1);
619
620 Status = gBS->OpenProtocol (ChildHandleBuffer[0],
621 &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp,
622 This->DriverBindingHandle, DeviceHandle,
623 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
624 ASSERT_EFI_ERROR (Status);
625 Dev = VIRTIO_NET_FROM_SNP (Snp);
626
627 //
628 // prevent any interference with WaitForPacket
629 //
630 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
631
632 ASSERT (Dev->MacHandle == ChildHandleBuffer[0]);
633 if (Dev->Snm.State != EfiSimpleNetworkStopped) {
634 //
635 // device in use, cannot stop driver instance
636 //
637 Status = EFI_DEVICE_ERROR;
638 }
639 else {
640 gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
641 This->DriverBindingHandle, Dev->MacHandle);
642 gBS->UninstallMultipleProtocolInterfaces (Dev->MacHandle,
643 &gEfiDevicePathProtocolGuid, Dev->MacDevicePath,
644 &gEfiSimpleNetworkProtocolGuid, &Dev->Snp,
645 NULL);
646 FreePool (Dev->MacDevicePath);
647 VirtioNetSnpEvacuate (Dev);
648 Dev->PciIo->Attributes (Dev->PciIo, EfiPciIoAttributeOperationSet,
649 Dev->OrigPciAttributes, NULL);
650 FreePool (Dev);
651 }
652
653 gBS->RestoreTPL (OldTpl);
654 return Status;
655 }
656
657 //
658 // release remaining resources, tied directly to the parent handle
659 //
660 gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
661 This->DriverBindingHandle, DeviceHandle);
662
663 return EFI_SUCCESS;
664 }
665
666
667 EFI_DRIVER_BINDING_PROTOCOL gVirtioNetDriverBinding = {
668 &VirtioNetDriverBindingSupported,
669 &VirtioNetDriverBindingStart,
670 &VirtioNetDriverBindingStop,
671 0x10,
672 NULL,
673 NULL
674 };