2 The entry point of IScsi driver.
4 Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "IScsiImpl.h"
17 EFI_DRIVER_BINDING_PROTOCOL gIScsiDriverBinding
= {
18 IScsiDriverBindingSupported
,
19 IScsiDriverBindingStart
,
20 IScsiDriverBindingStop
,
27 Tests to see if this driver supports a given controller. If a child device is provided,
28 it further tests to see if this driver supports creating a handle for the specified child device.
30 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
31 @param[in] ControllerHandle The handle of the controller to test. This handle
32 must support a protocol interface that supplies
33 an I/O abstraction to the driver.
34 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
35 This parameter is ignored by device drivers, and is optional for bus drivers.
38 @retval EFI_SUCCESS The device specified by ControllerHandle and
39 RemainingDevicePath is supported by the driver specified by This.
40 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
41 RemainingDevicePath is already being managed by the driver
43 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
44 RemainingDevicePath is already being managed by a different
45 driver or an application that requires exclusive acces.
46 Currently not implemented.
47 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
48 RemainingDevicePath is not supported by the driver specified by This.
52 IScsiDriverBindingSupported (
53 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
54 IN EFI_HANDLE ControllerHandle
,
55 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
59 EFI_DEVICE_PATH_PROTOCOL
*CurrentDevicePath
;
61 Status
= gBS
->OpenProtocol (
65 This
->DriverBindingHandle
,
67 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
69 if (!EFI_ERROR (Status
)) {
70 return EFI_ALREADY_STARTED
;
73 Status
= gBS
->OpenProtocol (
75 &gEfiTcp4ServiceBindingProtocolGuid
,
77 This
->DriverBindingHandle
,
79 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
81 if (EFI_ERROR (Status
)) {
82 return EFI_UNSUPPORTED
;
85 CurrentDevicePath
= RemainingDevicePath
;
86 if (CurrentDevicePath
!= NULL
) {
87 while (!IsDevicePathEnd (CurrentDevicePath
)) {
88 if ((CurrentDevicePath
->Type
== MESSAGING_DEVICE_PATH
) && (CurrentDevicePath
->SubType
== MSG_ISCSI_DP
)) {
92 CurrentDevicePath
= NextDevicePathNode (CurrentDevicePath
);
95 return EFI_UNSUPPORTED
;
102 Start this driver on ControllerHandle.
104 The Start() function is designed to be invoked from the EFI boot service ConnectController().
105 As a result, much of the error checking on the parameters to Start() has been moved into this
106 common boot service. It is legal to call Start() from other locations, but the following calling
107 restrictions must be followed or the system behavior will not be deterministic.
108 1. ControllerHandle must be a valid EFI_HANDLE.
109 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
110 EFI_DEVICE_PATH_PROTOCOL.
111 3. Prior to calling Start(), the Supported() function for the driver specified by This must
112 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
114 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
115 @param[in] ControllerHandle The handle of the controller to start. This handle
116 must support a protocol interface that supplies
117 an I/O abstraction to the driver.
118 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
119 This parameter is ignored by device drivers, and is optional for bus drivers.
121 @retval EFI_SUCCESS The device was started.
122 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
123 Currently not implemented.
124 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
125 @retval Others The driver failded to start the device.
129 IScsiDriverBindingStart (
130 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
131 IN EFI_HANDLE ControllerHandle
,
132 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
136 ISCSI_DRIVER_DATA
*Private
;
139 Private
= IScsiCreateDriverData (This
->DriverBindingHandle
, ControllerHandle
);
140 if (Private
== NULL
) {
141 return EFI_OUT_OF_RESOURCES
;
145 // Create a underlayer child instance, but not need to configure it. Just open ChildHandle
146 // via BY_DRIVER. That is, establishing the relationship between ControllerHandle and ChildHandle.
147 // Therefore, when DisconnectController(), especially VLAN virtual controller handle,
148 // IScsiDriverBindingStop() will be called.
150 Status
= NetLibCreateServiceChild (
152 This
->DriverBindingHandle
,
153 &gEfiTcp4ServiceBindingProtocolGuid
,
154 &Private
->ChildHandle
157 if (EFI_ERROR (Status
)) {
161 Status
= gBS
->OpenProtocol (
162 Private
->ChildHandle
,
163 &gEfiTcp4ProtocolGuid
,
165 This
->DriverBindingHandle
,
167 EFI_OPEN_PROTOCOL_BY_DRIVER
169 if (EFI_ERROR (Status
)) {
174 // Always install private protocol no matter what happens later. We need to
175 // keep the relationship between ControllerHandle and ChildHandle.
177 Status
= gBS
->InstallProtocolInterface (
180 EFI_NATIVE_INTERFACE
,
181 &Private
->IScsiIdentifier
183 if (EFI_ERROR (Status
)) {
188 // Try to add a port configuration page for this controller.
190 IScsiConfigUpdateForm (This
->DriverBindingHandle
, ControllerHandle
, TRUE
);
193 // Get the iSCSI configuration data of this controller.
195 Status
= IScsiGetConfigData (Private
);
196 if (EFI_ERROR (Status
)) {
200 // Try to login and create an iSCSI session according to the configuration.
202 Status
= IScsiSessionLogin (Private
);
203 if (Status
== EFI_MEDIA_CHANGED
) {
205 // The specified target is not available and the redirection information is
206 // got, login the session again with the updated target address.
208 Status
= IScsiSessionLogin (Private
);
211 if (EFI_ERROR (Status
)) {
215 // Duplicate the Session's tcp connection device path. The source port field
216 // will be set to zero as one iSCSI session is comprised of several iSCSI
219 Private
->DevicePath
= IScsiGetTcpConnDevicePath (Private
);
220 if (Private
->DevicePath
== NULL
) {
224 // Install the updated device path onto the ExtScsiPassThruHandle.
226 Status
= gBS
->InstallProtocolInterface (
227 &Private
->ExtScsiPassThruHandle
,
228 &gEfiDevicePathProtocolGuid
,
229 EFI_NATIVE_INTERFACE
,
232 if (EFI_ERROR (Status
)) {
237 // Update/Publish the iSCSI Boot Firmware Table.
245 IScsiSessionAbort (&Private
->Session
);
251 Stop this driver on ControllerHandle.
253 Release the control of this controller and remove the IScsi functions. The Stop()
254 function is designed to be invoked from the EFI boot service DisconnectController().
255 As a result, much of the error checking on the parameters to Stop() has been moved
256 into this common boot service. It is legal to call Stop() from other locations,
257 but the following calling restrictions must be followed or the system behavior will not be deterministic.
258 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
259 same driver's Start() function.
260 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
261 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
262 Start() function, and the Start() function must have called OpenProtocol() on
263 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
265 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
266 @param[in] ControllerHandle A handle to the device being stopped. The handle must
267 support a bus specific I/O protocol for the driver
268 to use to stop the device.
269 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.Not used.
270 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
271 if NumberOfChildren is 0.Not used.
273 @retval EFI_SUCCESS The device was stopped.
274 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
278 IScsiDriverBindingStop (
279 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
280 IN EFI_HANDLE ControllerHandle
,
281 IN UINTN NumberOfChildren
,
282 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
285 EFI_HANDLE IScsiController
;
287 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
288 ISCSI_DRIVER_DATA
*Private
;
289 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
;
290 ISCSI_CONNECTION
*Conn
;
292 if (NumberOfChildren
!= 0) {
294 // We should have only one child.
296 Status
= gBS
->OpenProtocol (
297 ChildHandleBuffer
[0],
298 &gEfiExtScsiPassThruProtocolGuid
,
300 This
->DriverBindingHandle
,
302 EFI_OPEN_PROTOCOL_GET_PROTOCOL
304 if (EFI_ERROR (Status
)) {
305 return EFI_DEVICE_ERROR
;
308 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
309 Conn
= NET_LIST_HEAD (&Private
->Session
.Conns
, ISCSI_CONNECTION
, Link
);
312 // Previously the TCP4 protocol is opened BY_CHILD_CONTROLLER. Just close
313 // the protocol here but not uninstall the device path protocol and
314 // EXT SCSI PASS THRU protocol installed on ExtScsiPassThruHandle.
318 &gEfiTcp4ProtocolGuid
,
320 Private
->ExtScsiPassThruHandle
326 // Get the handle of the controller we are controling.
328 IScsiController
= NetLibGetNicHandle (ControllerHandle
, &gEfiTcp4ProtocolGuid
);
330 Status
= gBS
->OpenProtocol (
333 (VOID
**)&IScsiIdentifier
,
334 This
->DriverBindingHandle
,
336 EFI_OPEN_PROTOCOL_GET_PROTOCOL
338 if (EFI_ERROR (Status
)) {
339 return EFI_DEVICE_ERROR
;
342 Private
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
344 if (Private
->ChildHandle
!= NULL
) {
345 Status
= gBS
->CloseProtocol (
346 Private
->ChildHandle
,
347 &gEfiTcp4ProtocolGuid
,
348 This
->DriverBindingHandle
,
352 ASSERT (!EFI_ERROR (Status
));
354 Status
= NetLibDestroyServiceChild (
356 This
->DriverBindingHandle
,
357 &gEfiTcp4ServiceBindingProtocolGuid
,
360 ASSERT (!EFI_ERROR (Status
));
363 IScsiConfigUpdateForm (This
->DriverBindingHandle
, IScsiController
, FALSE
);
366 // Uninstall the private protocol.
368 gBS
->UninstallProtocolInterface (
371 &Private
->IScsiIdentifier
375 // Update the iSCSI Boot Firware Table.
379 IScsiSessionAbort (&Private
->Session
);
380 IScsiCleanDriverData (Private
);
386 Unloads an image(the iSCSI driver).
388 @param[in] ImageHandle Handle that identifies the image to be unloaded.
390 @retval EFI_SUCCESS The image has been unloaded.
391 @retval Others Other errors as indicated.
396 IN EFI_HANDLE ImageHandle
400 UINTN DeviceHandleCount
;
401 EFI_HANDLE
*DeviceHandleBuffer
;
405 // Try to disonnect the driver from the devices it's controlling.
407 Status
= gBS
->LocateHandleBuffer (
414 if (!EFI_ERROR (Status
)) {
415 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
416 Status
= gBS
->DisconnectController (
417 DeviceHandleBuffer
[Index
],
423 if (DeviceHandleBuffer
!= NULL
) {
424 FreePool (DeviceHandleBuffer
);
428 // Unload the iSCSI configuration form.
430 IScsiConfigFormUnload (gIScsiDriverBinding
.DriverBindingHandle
);
433 // Uninstall the protocols installed by iSCSI driver.
435 Status
= gBS
->UninstallMultipleProtocolInterfaces (
437 &gEfiDriverBindingProtocolGuid
,
438 &gIScsiDriverBinding
,
439 &gEfiComponentName2ProtocolGuid
,
440 &gIScsiComponentName2
,
441 &gEfiComponentNameProtocolGuid
,
442 &gIScsiComponentName
,
443 &gEfiIScsiInitiatorNameProtocolGuid
,
444 &gIScsiInitiatorName
,
452 This is the declaration of an EFI image entry point. This entry point is
453 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
454 both device drivers and bus drivers. It initialize the global variables and
455 publish the driver binding protocol.
457 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
458 @param[in] SystemTable A pointer to the EFI System Table.
460 @retval EFI_SUCCESS The operation completed successfully.
461 @retval EFI_ACCESS_DENIED EFI_ISCSI_INITIATOR_NAME_PROTOCOL was installed unexpectedly.
462 @retval Others Other errors as indicated.
466 IScsiDriverEntryPoint (
467 IN EFI_HANDLE ImageHandle
,
468 IN EFI_SYSTEM_TABLE
*SystemTable
472 EFI_ISCSI_INITIATOR_NAME_PROTOCOL
*IScsiInitiatorName
;
475 // There should be only one EFI_ISCSI_INITIATOR_NAME_PROTOCOL.
477 Status
= gBS
->LocateProtocol (
478 &gEfiIScsiInitiatorNameProtocolGuid
,
480 (VOID
**) &IScsiInitiatorName
483 if (!EFI_ERROR (Status
)) {
484 return EFI_ACCESS_DENIED
;
488 // Initialize the EFI Driver Library
490 Status
= EfiLibInstallDriverBindingComponentName2 (
493 &gIScsiDriverBinding
,
495 &gIScsiComponentName
,
496 &gIScsiComponentName2
499 if (!EFI_ERROR (Status
)) {
501 // Install the iSCSI Initiator Name Protocol.
503 Status
= gBS
->InstallProtocolInterface (
505 &gEfiIScsiInitiatorNameProtocolGuid
,
506 EFI_NATIVE_INTERFACE
,
509 if (EFI_ERROR (Status
)) {
510 gBS
->UninstallMultipleProtocolInterfaces (
512 &gEfiDriverBindingProtocolGuid
,
513 &gIScsiDriverBinding
,
514 &gEfiComponentName2ProtocolGuid
,
515 &gIScsiComponentName2
,
516 &gEfiComponentNameProtocolGuid
,
517 &gIScsiComponentName
,
524 // Initialize the configuration form of iSCSI.
526 Status
= IScsiConfigFormInit ();
527 if (EFI_ERROR (Status
)) {
528 gBS
->UninstallMultipleProtocolInterfaces (
530 &gEfiDriverBindingProtocolGuid
,
531 &gIScsiDriverBinding
,
532 &gEfiComponentName2ProtocolGuid
,
533 &gIScsiComponentName2
,
534 &gEfiComponentNameProtocolGuid
,
535 &gIScsiComponentName
,
536 &gEfiIScsiInitiatorNameProtocolGuid
,
537 &gIScsiInitiatorName
,