DriverBinding.c 26 KB

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