2 The entry point of IScsi driver.
4 Copyright (c) 2004 - 2010, 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
,
26 EFI_GUID gIScsiPrivateGuid
= ISCSI_PRIVATE_GUID
;
30 Tests to see if this driver supports a given controller. If a child device is provided,
31 it further tests to see if this driver supports creating a handle for the specified child device.
33 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
34 @param[in] ControllerHandle The handle of the controller to test. This handle
35 must support a protocol interface that supplies
36 an I/O abstraction to the driver.
37 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
38 This parameter is ignored by device drivers, and is optional for bus drivers.
41 @retval EFI_SUCCESS The device specified by ControllerHandle and
42 RemainingDevicePath is supported by the driver specified by This.
43 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
44 RemainingDevicePath is already being managed by the driver
46 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
47 RemainingDevicePath is already being managed by a different
48 driver or an application that requires exclusive acces.
49 Currently not implemented.
50 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
51 RemainingDevicePath is not supported by the driver specified by This.
55 IScsiDriverBindingSupported (
56 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
57 IN EFI_HANDLE ControllerHandle
,
58 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
62 EFI_DEVICE_PATH_PROTOCOL
*CurrentDevicePath
;
64 Status
= gBS
->OpenProtocol (
68 This
->DriverBindingHandle
,
70 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
72 if (!EFI_ERROR (Status
)) {
73 return EFI_ALREADY_STARTED
;
76 Status
= gBS
->OpenProtocol (
78 &gEfiTcp4ServiceBindingProtocolGuid
,
80 This
->DriverBindingHandle
,
82 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
84 if (EFI_ERROR (Status
)) {
85 return EFI_UNSUPPORTED
;
88 CurrentDevicePath
= RemainingDevicePath
;
89 if (CurrentDevicePath
!= NULL
) {
90 while (!IsDevicePathEnd (CurrentDevicePath
)) {
91 if ((CurrentDevicePath
->Type
== MESSAGING_DEVICE_PATH
) && (CurrentDevicePath
->SubType
== MSG_ISCSI_DP
)) {
95 CurrentDevicePath
= NextDevicePathNode (CurrentDevicePath
);
98 return EFI_UNSUPPORTED
;
105 Start this driver on ControllerHandle.
107 The Start() function is designed to be invoked from the EFI boot service ConnectController().
108 As a result, much of the error checking on the parameters to Start() has been moved into this
109 common boot service. It is legal to call Start() from other locations, but the following calling
110 restrictions must be followed or the system behavior will not be deterministic.
111 1. ControllerHandle must be a valid EFI_HANDLE.
112 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
113 EFI_DEVICE_PATH_PROTOCOL.
114 3. Prior to calling Start(), the Supported() function for the driver specified by This must
115 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
117 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
118 @param[in] ControllerHandle The handle of the controller to start. This handle
119 must support a protocol interface that supplies
120 an I/O abstraction to the driver.
121 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
122 This parameter is ignored by device drivers, and is optional for bus drivers.
124 @retval EFI_SUCCESS The device was started.
125 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
126 Currently not implemented.
127 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
128 @retval Others The driver failded to start the device.
132 IScsiDriverBindingStart (
133 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
134 IN EFI_HANDLE ControllerHandle
,
135 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
139 ISCSI_DRIVER_DATA
*Private
;
142 Private
= IScsiCreateDriverData (This
->DriverBindingHandle
, ControllerHandle
);
143 if (Private
== NULL
) {
144 return EFI_OUT_OF_RESOURCES
;
148 // Create a underlayer child instance, but not need to configure it. Just open ChildHandle
149 // via BY_DRIVER. That is, establishing the relationship between ControllerHandle and ChildHandle.
150 // Therefore, when DisconnectController(), especially VLAN virtual controller handle,
151 // IScsiDriverBindingStop() will be called.
153 Status
= NetLibCreateServiceChild (
155 This
->DriverBindingHandle
,
156 &gEfiTcp4ServiceBindingProtocolGuid
,
157 &Private
->ChildHandle
160 if (EFI_ERROR (Status
)) {
164 Status
= gBS
->OpenProtocol (
165 Private
->ChildHandle
,
166 &gEfiTcp4ProtocolGuid
,
168 This
->DriverBindingHandle
,
170 EFI_OPEN_PROTOCOL_BY_DRIVER
172 if (EFI_ERROR (Status
)) {
177 // Always install private protocol no matter what happens later. We need to
178 // keep the relationship between ControllerHandle and ChildHandle.
180 Status
= gBS
->InstallProtocolInterface (
183 EFI_NATIVE_INTERFACE
,
184 &Private
->IScsiIdentifier
186 if (EFI_ERROR (Status
)) {
191 // Try to add a port configuration page for this controller.
193 IScsiConfigUpdateForm (This
->DriverBindingHandle
, ControllerHandle
, TRUE
);
196 // Get the iSCSI configuration data of this controller.
198 Status
= IScsiGetConfigData (Private
);
199 if (EFI_ERROR (Status
)) {
203 // Try to login and create an iSCSI session according to the configuration.
205 Status
= IScsiSessionLogin (Private
);
206 if (Status
== EFI_MEDIA_CHANGED
) {
208 // The specified target is not available and the redirection information is
209 // got, login the session again with the updated target address.
211 Status
= IScsiSessionLogin (Private
);
214 if (EFI_ERROR (Status
)) {
218 // Duplicate the Session's tcp connection device path. The source port field
219 // will be set to zero as one iSCSI session is comprised of several iSCSI
222 Private
->DevicePath
= IScsiGetTcpConnDevicePath (Private
);
223 if (Private
->DevicePath
== NULL
) {
227 // Install the updated device path onto the ExtScsiPassThruHandle.
229 Status
= gBS
->InstallProtocolInterface (
230 &Private
->ExtScsiPassThruHandle
,
231 &gEfiDevicePathProtocolGuid
,
232 EFI_NATIVE_INTERFACE
,
235 if (EFI_ERROR (Status
)) {
240 // Update/Publish the iSCSI Boot Firmware Table.
248 IScsiSessionAbort (&Private
->Session
);
254 Stop this driver on ControllerHandle.
256 Release the control of this controller and remove the IScsi functions. The Stop()
257 function is designed to be invoked from the EFI boot service DisconnectController().
258 As a result, much of the error checking on the parameters to Stop() has been moved
259 into this common boot service. It is legal to call Stop() from other locations,
260 but the following calling restrictions must be followed or the system behavior will not be deterministic.
261 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
262 same driver's Start() function.
263 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
264 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
265 Start() function, and the Start() function must have called OpenProtocol() on
266 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
268 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
269 @param[in] ControllerHandle A handle to the device being stopped. The handle must
270 support a bus specific I/O protocol for the driver
271 to use to stop the device.
272 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.Not used.
273 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
274 if NumberOfChildren is 0.Not used.
276 @retval EFI_SUCCESS The device was stopped.
277 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
281 IScsiDriverBindingStop (
282 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
283 IN EFI_HANDLE ControllerHandle
,
284 IN UINTN NumberOfChildren
,
285 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
288 EFI_HANDLE IScsiController
;
290 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
291 ISCSI_DRIVER_DATA
*Private
;
292 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
;
293 ISCSI_CONNECTION
*Conn
;
295 if (NumberOfChildren
!= 0) {
297 // We should have only one child.
299 Status
= gBS
->OpenProtocol (
300 ChildHandleBuffer
[0],
301 &gEfiExtScsiPassThruProtocolGuid
,
303 This
->DriverBindingHandle
,
305 EFI_OPEN_PROTOCOL_GET_PROTOCOL
307 if (EFI_ERROR (Status
)) {
308 return EFI_DEVICE_ERROR
;
311 Private
= ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru
);
312 Conn
= NET_LIST_HEAD (&Private
->Session
.Conns
, ISCSI_CONNECTION
, Link
);
315 // Previously the TCP4 protocol is opened BY_CHILD_CONTROLLER. Just close
316 // the protocol here but not uninstall the device path protocol and
317 // EXT SCSI PASS THRU protocol installed on ExtScsiPassThruHandle.
321 &gEfiTcp4ProtocolGuid
,
323 Private
->ExtScsiPassThruHandle
329 // Get the handle of the controller we are controling.
331 IScsiController
= NetLibGetNicHandle (ControllerHandle
, &gEfiTcp4ProtocolGuid
);
333 Status
= gBS
->OpenProtocol (
336 (VOID
**)&IScsiIdentifier
,
337 This
->DriverBindingHandle
,
339 EFI_OPEN_PROTOCOL_GET_PROTOCOL
341 if (EFI_ERROR (Status
)) {
342 return EFI_DEVICE_ERROR
;
345 Private
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
347 if (Private
->ChildHandle
!= NULL
) {
348 Status
= gBS
->CloseProtocol (
349 Private
->ChildHandle
,
350 &gEfiTcp4ProtocolGuid
,
351 This
->DriverBindingHandle
,
355 ASSERT (!EFI_ERROR (Status
));
357 Status
= NetLibDestroyServiceChild (
359 This
->DriverBindingHandle
,
360 &gEfiTcp4ServiceBindingProtocolGuid
,
363 ASSERT (!EFI_ERROR (Status
));
366 IScsiConfigUpdateForm (This
->DriverBindingHandle
, IScsiController
, FALSE
);
369 // Uninstall the private protocol.
371 gBS
->UninstallProtocolInterface (
374 &Private
->IScsiIdentifier
378 // Update the iSCSI Boot Firware Table.
382 IScsiSessionAbort (&Private
->Session
);
383 IScsiCleanDriverData (Private
);
389 Unloads an image(the iSCSI driver).
391 @param[in] ImageHandle Handle that identifies the image to be unloaded.
393 @retval EFI_SUCCESS The image has been unloaded.
394 @retval Others Other errors as indicated.
399 IN EFI_HANDLE ImageHandle
403 UINTN DeviceHandleCount
;
404 EFI_HANDLE
*DeviceHandleBuffer
;
408 // Try to disonnect the driver from the devices it's controlling.
410 Status
= gBS
->LocateHandleBuffer (
417 if (!EFI_ERROR (Status
)) {
418 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
419 Status
= gBS
->DisconnectController (
420 DeviceHandleBuffer
[Index
],
426 if (DeviceHandleBuffer
!= NULL
) {
427 FreePool (DeviceHandleBuffer
);
431 // Unload the iSCSI configuration form.
433 IScsiConfigFormUnload (gIScsiDriverBinding
.DriverBindingHandle
);
436 // Uninstall the protocols installed by iSCSI driver.
438 Status
= gBS
->UninstallMultipleProtocolInterfaces (
440 &gEfiDriverBindingProtocolGuid
,
441 &gIScsiDriverBinding
,
442 &gEfiComponentName2ProtocolGuid
,
443 &gIScsiComponentName2
,
444 &gEfiComponentNameProtocolGuid
,
445 &gIScsiComponentName
,
446 &gEfiIScsiInitiatorNameProtocolGuid
,
447 &gIScsiInitiatorName
,
455 This is the declaration of an EFI image entry point. This entry point is
456 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
457 both device drivers and bus drivers. It initialize the global variables and
458 publish the driver binding protocol.
460 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
461 @param[in] SystemTable A pointer to the EFI System Table.
463 @retval EFI_SUCCESS The operation completed successfully.
464 @retval EFI_ACCESS_DENIED EFI_ISCSI_INITIATOR_NAME_PROTOCOL was installed unexpectedly.
465 @retval Others Other errors as indicated.
469 IScsiDriverEntryPoint (
470 IN EFI_HANDLE ImageHandle
,
471 IN EFI_SYSTEM_TABLE
*SystemTable
475 EFI_ISCSI_INITIATOR_NAME_PROTOCOL
*IScsiInitiatorName
;
478 // There should be only one EFI_ISCSI_INITIATOR_NAME_PROTOCOL.
480 Status
= gBS
->LocateProtocol (
481 &gEfiIScsiInitiatorNameProtocolGuid
,
483 (VOID
**) &IScsiInitiatorName
486 if (!EFI_ERROR (Status
)) {
487 return EFI_ACCESS_DENIED
;
491 // Initialize the EFI Driver Library
493 Status
= EfiLibInstallDriverBindingComponentName2 (
496 &gIScsiDriverBinding
,
498 &gIScsiComponentName
,
499 &gIScsiComponentName2
502 if (!EFI_ERROR (Status
)) {
504 // Install the iSCSI Initiator Name Protocol.
506 Status
= gBS
->InstallProtocolInterface (
508 &gEfiIScsiInitiatorNameProtocolGuid
,
509 EFI_NATIVE_INTERFACE
,
512 if (EFI_ERROR (Status
)) {
513 gBS
->UninstallMultipleProtocolInterfaces (
515 &gEfiDriverBindingProtocolGuid
,
516 &gIScsiDriverBinding
,
517 &gEfiComponentName2ProtocolGuid
,
518 &gIScsiComponentName2
,
519 &gEfiComponentNameProtocolGuid
,
520 &gIScsiComponentName
,
527 // Initialize the configuration form of iSCSI.
529 Status
= IScsiConfigFormInit ();
530 if (EFI_ERROR (Status
)) {
531 gBS
->UninstallMultipleProtocolInterfaces (
533 &gEfiDriverBindingProtocolGuid
,
534 &gIScsiDriverBinding
,
535 &gEfiComponentName2ProtocolGuid
,
536 &gIScsiComponentName2
,
537 &gEfiComponentNameProtocolGuid
,
538 &gIScsiComponentName
,
539 &gEfiIScsiInitiatorNameProtocolGuid
,
540 &gIScsiInitiatorName
,