]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/IScsiDxe/IScsiDriver.c
update file header
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiDriver.c
1 /** @file
2 The entry point of IScsi driver.
3
4 Copyright (c) 2004 - 2008, Intel Corporation.<BR>
5 All rights reserved. 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
9
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.
12
13 **/
14
15 #include "IScsiImpl.h"
16
17 EFI_DRIVER_BINDING_PROTOCOL gIScsiDriverBinding = {
18 IScsiDriverBindingSupported,
19 IScsiDriverBindingStart,
20 IScsiDriverBindingStop,
21 0xa,
22 NULL,
23 NULL
24 };
25
26 EFI_GUID mIScsiPrivateGuid = ISCSI_PRIVATE_GUID;
27
28
29 /**
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.
32
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.
39
40
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
45 specified by This.
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.
52 **/
53 EFI_STATUS
54 EFIAPI
55 IScsiDriverBindingSupported (
56 IN EFI_DRIVER_BINDING_PROTOCOL *This,
57 IN EFI_HANDLE ControllerHandle,
58 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
59 )
60 {
61 EFI_STATUS Status;
62 EFI_DEVICE_PATH_PROTOCOL *CurrentDevicePath;
63
64 Status = gBS->OpenProtocol (
65 ControllerHandle,
66 &mIScsiPrivateGuid,
67 NULL,
68 This->DriverBindingHandle,
69 ControllerHandle,
70 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
71 );
72 if (!EFI_ERROR (Status)) {
73 return EFI_ALREADY_STARTED;
74 }
75
76 Status = gBS->OpenProtocol (
77 ControllerHandle,
78 &gEfiTcp4ServiceBindingProtocolGuid,
79 NULL,
80 This->DriverBindingHandle,
81 ControllerHandle,
82 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
83 );
84 if (EFI_ERROR (Status)) {
85 return EFI_UNSUPPORTED;
86 }
87
88 CurrentDevicePath = RemainingDevicePath;
89 if (CurrentDevicePath != NULL) {
90 while (!IsDevicePathEnd (CurrentDevicePath)) {
91 if ((CurrentDevicePath->Type == MESSAGING_DEVICE_PATH) && (CurrentDevicePath->SubType == MSG_ISCSI_DP)) {
92 return EFI_SUCCESS;
93 }
94
95 CurrentDevicePath = NextDevicePathNode (CurrentDevicePath);
96 }
97
98 return EFI_UNSUPPORTED;
99 }
100
101 return EFI_SUCCESS;
102 }
103
104 /**
105 Start this driver on ControllerHandle. The Start() function is designed to be
106 invoked from the EFI boot service ConnectController(). As a result, much of
107 the error checking on the parameters to Start() has been moved into this
108 common boot service. It is legal to call Start() from other locations,
109 but the following calling restrictions must be followed or the system behavior will not be deterministic.
110 1. ControllerHandle must be a valid EFI_HANDLE.
111 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
112 EFI_DEVICE_PATH_PROTOCOL.
113 3. Prior to calling Start(), the Supported() function for the driver specified by This must
114 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
115
116 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
117 @param[in] ControllerHandle The handle of the controller to start. This handle
118 must support a protocol interface that supplies
119 an I/O abstraction to the driver.
120 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
121 This parameter is ignored by device drivers, and is optional for bus drivers.
122
123 @retval EFI_SUCCESS The device was started.
124 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
125 Currently not implemented.
126 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
127 @retval Others The driver failded to start the device.
128 **/
129 EFI_STATUS
130 EFIAPI
131 IScsiDriverBindingStart (
132 IN EFI_DRIVER_BINDING_PROTOCOL *This,
133 IN EFI_HANDLE ControllerHandle,
134 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
135 )
136 {
137 EFI_STATUS Status;
138 ISCSI_DRIVER_DATA *Private;
139
140 //
141 // Try to add a port configuration page for this controller.
142 //
143 IScsiConfigUpdateForm (This->DriverBindingHandle, ControllerHandle, TRUE);
144
145 Private = IScsiCreateDriverData (This->DriverBindingHandle, ControllerHandle);
146 if (Private == NULL) {
147 return EFI_OUT_OF_RESOURCES;
148 }
149 //
150 // Get the iSCSI configuration data of this controller.
151 //
152 Status = IScsiGetConfigData (Private);
153 if (EFI_ERROR (Status)) {
154 goto ON_ERROR;
155 }
156 //
157 // Try to login and create an iSCSI session according to the configuration.
158 //
159 Status = IScsiSessionLogin (Private);
160 if (Status == EFI_MEDIA_CHANGED) {
161 //
162 // The specified target is not available and the redirection information is
163 // got, login the session again with the updated target address.
164 //
165 Status = IScsiSessionLogin (Private);
166 }
167
168 if (EFI_ERROR (Status)) {
169 goto ON_ERROR;
170 }
171 //
172 // Duplicate the Session's tcp connection device path. The source port field
173 // will be set to zero as one iSCSI session is comprised of several iSCSI
174 // connections.
175 //
176 Private->DevicePath = IScsiGetTcpConnDevicePath (Private);
177 if (Private->DevicePath == NULL) {
178 goto ON_ERROR;
179 }
180 //
181 // Install the updated device path onto the ExtScsiPassThruHandle.
182 //
183 Status = gBS->InstallProtocolInterface (
184 &Private->ExtScsiPassThruHandle,
185 &gEfiDevicePathProtocolGuid,
186 EFI_NATIVE_INTERFACE,
187 Private->DevicePath
188 );
189 if (EFI_ERROR (Status)) {
190 goto ON_ERROR;
191 }
192 //
193 // Install the iSCSI private stuff as a flag to indicate this controller
194 // is already controlled by iSCSI driver.
195 //
196 Status = gBS->InstallProtocolInterface (
197 &ControllerHandle,
198 &mIScsiPrivateGuid,
199 EFI_NATIVE_INTERFACE,
200 &Private->IScsiIdentifier
201 );
202 if (EFI_ERROR (Status)) {
203 goto ON_ERROR;
204 }
205 //
206 // Update/Publish the iSCSI Boot Firmware Table.
207 //
208 IScsiPublishIbft ();
209
210 return EFI_SUCCESS;
211
212 ON_ERROR:
213
214 IScsiSessionAbort (&Private->Session);
215 IScsiCleanDriverData (Private);
216
217 return Status;
218 }
219
220 /**
221 Stop this driver on ControllerHandle.
222
223 Release the control of this controller and remove the IScsi functions. The Stop()
224 function is designed to be invoked from the EFI boot service DisconnectController().
225 As a result, much of the error checking on the parameters to Stop() has been moved
226 into this common boot service. It is legal to call Stop() from other locations,
227 but the following calling restrictions must be followed or the system behavior will not be deterministic.
228 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
229 same driver's Start() function.
230 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
231 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
232 Start() function, and the Start() function must have called OpenProtocol() on
233 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
234
235 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
236 @param[in] ControllerHandle A handle to the device being stopped. The handle must
237 support a bus specific I/O protocol for the driver
238 to use to stop the device.
239 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.Not used.
240 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
241 if NumberOfChildren is 0.Not used.
242
243 @retval EFI_SUCCESS The device was stopped.
244 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
245 **/
246 EFI_STATUS
247 EFIAPI
248 IScsiDriverBindingStop (
249 IN EFI_DRIVER_BINDING_PROTOCOL *This,
250 IN EFI_HANDLE ControllerHandle,
251 IN UINTN NumberOfChildren,
252 IN EFI_HANDLE *ChildHandleBuffer
253 )
254 {
255 EFI_HANDLE IScsiController;
256 EFI_STATUS Status;
257 ISCSI_PRIVATE_PROTOCOL *IScsiIdentifier;
258 ISCSI_DRIVER_DATA *Private;
259 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru;
260 ISCSI_CONNECTION *Conn;
261
262 if (NumberOfChildren != 0) {
263 //
264 // We should have only one child.
265 //
266 Status = gBS->OpenProtocol (
267 ChildHandleBuffer[0],
268 &gEfiExtScsiPassThruProtocolGuid,
269 (VOID **) &PassThru,
270 This->DriverBindingHandle,
271 ControllerHandle,
272 EFI_OPEN_PROTOCOL_GET_PROTOCOL
273 );
274 if (EFI_ERROR (Status)) {
275 return EFI_DEVICE_ERROR;
276 }
277
278 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru);
279 Conn = NET_LIST_HEAD (&Private->Session.Conns, ISCSI_CONNECTION, Link);
280
281 //
282 // Previously the TCP4 protocol is opened BY_CHILD_CONTROLLER. Just close
283 // the protocol here but not uninstall the device path protocol and
284 // EXT SCSI PASS THRU protocol installed on ExtScsiPassThruHandle.
285 //
286 gBS->CloseProtocol (
287 Conn->Tcp4Io.Handle,
288 &gEfiTcp4ProtocolGuid,
289 Private->Image,
290 Private->ExtScsiPassThruHandle
291 );
292
293 return EFI_SUCCESS;
294 }
295 //
296 // Get the handle of the controller we are controling.
297 //
298 IScsiController = NetLibGetNicHandle (ControllerHandle, &gEfiTcp4ProtocolGuid);
299
300 Status = gBS->OpenProtocol (
301 IScsiController,
302 &mIScsiPrivateGuid,
303 (VOID **)&IScsiIdentifier,
304 This->DriverBindingHandle,
305 ControllerHandle,
306 EFI_OPEN_PROTOCOL_GET_PROTOCOL
307 );
308 if (EFI_ERROR (Status)) {
309 return EFI_DEVICE_ERROR;
310 }
311
312 Private = ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier);
313
314 //
315 // Uninstall the private protocol.
316 //
317 gBS->UninstallProtocolInterface (
318 IScsiController,
319 &mIScsiPrivateGuid,
320 &Private->IScsiIdentifier
321 );
322
323 //
324 // Update the iSCSI Boot Firware Table.
325 //
326 IScsiPublishIbft ();
327
328 IScsiSessionAbort (&Private->Session);
329 IScsiCleanDriverData (Private);
330
331 return EFI_SUCCESS;
332 }
333
334 /**
335 Unloads an image(the iSCSI driver).
336
337 @param[in] ImageHandle Handle that identifies the image to be unloaded.
338
339 @retval EFI_SUCCESS The image has been unloaded.
340 @retval Others Some unexpected errors happened.
341 **/
342 EFI_STATUS
343 EFIAPI
344 EfiIScsiUnload (
345 IN EFI_HANDLE ImageHandle
346 )
347 {
348 EFI_STATUS Status;
349 UINTN DeviceHandleCount;
350 EFI_HANDLE *DeviceHandleBuffer;
351 UINTN Index;
352
353 //
354 // Try to disonnect the driver from the devices it's controlling.
355 //
356 Status = gBS->LocateHandleBuffer (
357 AllHandles,
358 NULL,
359 NULL,
360 &DeviceHandleCount,
361 &DeviceHandleBuffer
362 );
363 if (!EFI_ERROR (Status)) {
364 for (Index = 0; Index < DeviceHandleCount; Index++) {
365 Status = gBS->DisconnectController (
366 DeviceHandleBuffer[Index],
367 ImageHandle,
368 NULL
369 );
370 }
371
372 if (DeviceHandleBuffer != NULL) {
373 gBS->FreePool (DeviceHandleBuffer);
374 }
375 }
376 //
377 // Unload the iSCSI configuration form.
378 //
379 IScsiConfigFormUnload (gIScsiDriverBinding.DriverBindingHandle);
380
381 //
382 // Uninstall the protocols installed by iSCSI driver.
383 //
384 Status = gBS->UninstallMultipleProtocolInterfaces (
385 ImageHandle,
386 &gEfiDriverBindingProtocolGuid,
387 &gIScsiDriverBinding,
388 &gEfiComponentName2ProtocolGuid,
389 &gIScsiComponentName2,
390 &gEfiComponentNameProtocolGuid,
391 &gIScsiComponentName,
392 &gEfiIScsiInitiatorNameProtocolGuid,
393 &gIScsiInitiatorName,
394 NULL
395 );
396
397 return Status;
398 }
399
400 /**
401 This is the declaration of an EFI image entry point. This entry point is
402 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
403 both device drivers and bus drivers. It initialize the global variables and
404 publish the driver binding protocol.
405
406 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
407 @param[in] SystemTable A pointer to the EFI System Table.
408
409 @retval EFI_SUCCESS The operation completed successfully.
410 @retval EFI_ACCESS_DENIED EFI_ISCSI_INITIATOR_NAME_PROTOCOL was installed unexpectedly.
411 @retval Others Some unexpected error happened.
412 **/
413 EFI_STATUS
414 EFIAPI
415 IScsiDriverEntryPoint (
416 IN EFI_HANDLE ImageHandle,
417 IN EFI_SYSTEM_TABLE *SystemTable
418 )
419 {
420 EFI_STATUS Status;
421 EFI_ISCSI_INITIATOR_NAME_PROTOCOL *IScsiInitiatorName;
422
423 //
424 // There should be only one EFI_ISCSI_INITIATOR_NAME_PROTOCOL.
425 //
426 Status = gBS->LocateProtocol (
427 &gEfiIScsiInitiatorNameProtocolGuid,
428 NULL,
429 (VOID**) &IScsiInitiatorName
430 );
431
432 if (!EFI_ERROR (Status)) {
433 return EFI_ACCESS_DENIED;
434 }
435
436 //
437 // Initialize the EFI Driver Library
438 //
439 Status = EfiLibInstallDriverBindingComponentName2 (
440 ImageHandle,
441 SystemTable,
442 &gIScsiDriverBinding,
443 ImageHandle,
444 &gIScsiComponentName,
445 &gIScsiComponentName2
446 );
447
448 if (!EFI_ERROR (Status)) {
449 //
450 // Install the iSCSI Initiator Name Protocol.
451 //
452 Status = gBS->InstallProtocolInterface (
453 &ImageHandle,
454 &gEfiIScsiInitiatorNameProtocolGuid,
455 EFI_NATIVE_INTERFACE,
456 &gIScsiInitiatorName
457 );
458 if (EFI_ERROR (Status)) {
459 gBS->UninstallMultipleProtocolInterfaces (
460 ImageHandle,
461 &gEfiDriverBindingProtocolGuid,
462 &gIScsiDriverBinding,
463 &gEfiComponentName2ProtocolGuid,
464 &gIScsiComponentName2,
465 &gEfiComponentNameProtocolGuid,
466 &gIScsiComponentName,
467 NULL
468 );
469 return Status;
470 }
471
472 //
473 // Initialize the configuration form of iSCSI.
474 //
475 Status = IScsiConfigFormInit (gIScsiDriverBinding.DriverBindingHandle);
476 if (EFI_ERROR (Status)) {
477 gBS->UninstallMultipleProtocolInterfaces (
478 ImageHandle,
479 &gEfiDriverBindingProtocolGuid,
480 &gIScsiDriverBinding,
481 &gEfiComponentName2ProtocolGuid,
482 &gIScsiComponentName2,
483 &gEfiComponentNameProtocolGuid,
484 &gIScsiComponentName,
485 &gEfiIScsiInitiatorNameProtocolGuid,
486 &gIScsiInitiatorName,
487 NULL
488 );
489 }
490 }
491 return Status;
492 }
493