]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/IScsiDxe/IScsiDriver.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiDriver.c
1 /** @file
2 The entry point of IScsi driver.
3
4 Copyright (c) 2004 - 2013, 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
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 /**
27 Tests to see if this driver supports the RemainingDevicePath.
28
29 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
30 parameter is ignored by device drivers, and is optional for bus
31 drivers. For bus drivers, if this parameter is not NULL, then
32 the bus driver must determine if the bus controller specified
33 by ControllerHandle and the child controller specified
34 by RemainingDevicePath are both supported by this
35 bus driver.
36
37 @retval EFI_SUCCESS The RemainingDevicePath is supported or NULL.
38 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
39 RemainingDevicePath is not supported by the driver specified by This.
40 **/
41 EFI_STATUS
42 IScsiIsDevicePathSupported (
43 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
44 )
45 {
46 EFI_DEVICE_PATH_PROTOCOL *CurrentDevicePath;
47
48 CurrentDevicePath = RemainingDevicePath;
49 if (CurrentDevicePath != NULL) {
50 while (!IsDevicePathEnd (CurrentDevicePath)) {
51 if ((CurrentDevicePath->Type == MESSAGING_DEVICE_PATH) && (CurrentDevicePath->SubType == MSG_ISCSI_DP)) {
52 return EFI_SUCCESS;
53 }
54
55 CurrentDevicePath = NextDevicePathNode (CurrentDevicePath);
56 }
57
58 return EFI_UNSUPPORTED;
59 }
60
61 return EFI_SUCCESS;
62 }
63
64 /**
65 Tests to see if this driver supports a given controller. If a child device is provided,
66 it further tests to see if this driver supports creating a handle for the specified child device.
67
68 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
69 @param[in] ControllerHandle The handle of the controller to test. This handle
70 must support a protocol interface that supplies
71 an I/O abstraction to the driver.
72 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
73 This parameter is ignored by device drivers, and is optional for bus drivers.
74
75
76 @retval EFI_SUCCESS The device specified by ControllerHandle and
77 RemainingDevicePath is supported by the driver specified by This.
78 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
79 RemainingDevicePath is already being managed by the driver
80 specified by This.
81 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
82 RemainingDevicePath is already being managed by a different
83 driver or an application that requires exclusive acces.
84 Currently not implemented.
85 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
86 RemainingDevicePath is not supported by the driver specified by This.
87 **/
88 EFI_STATUS
89 EFIAPI
90 IScsiDriverBindingSupported (
91 IN EFI_DRIVER_BINDING_PROTOCOL *This,
92 IN EFI_HANDLE ControllerHandle,
93 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
94 )
95 {
96 EFI_STATUS Status;
97
98 Status = gBS->OpenProtocol (
99 ControllerHandle,
100 &gEfiCallerIdGuid,
101 NULL,
102 This->DriverBindingHandle,
103 ControllerHandle,
104 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
105 );
106 if (!EFI_ERROR (Status)) {
107 return EFI_ALREADY_STARTED;
108 }
109
110 Status = gBS->OpenProtocol (
111 ControllerHandle,
112 &gEfiTcp4ServiceBindingProtocolGuid,
113 NULL,
114 This->DriverBindingHandle,
115 ControllerHandle,
116 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
117 );
118 if (EFI_ERROR (Status)) {
119 return EFI_UNSUPPORTED;
120 }
121
122 Status = IScsiIsDevicePathSupported (RemainingDevicePath);
123 if (EFI_ERROR (Status)) {
124 return EFI_UNSUPPORTED;
125 }
126
127 if (IScsiDhcpIsConfigured (ControllerHandle)) {
128 Status = gBS->OpenProtocol (
129 ControllerHandle,
130 &gEfiDhcp4ServiceBindingProtocolGuid,
131 NULL,
132 This->DriverBindingHandle,
133 ControllerHandle,
134 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
135 );
136 if (EFI_ERROR (Status)) {
137 return EFI_UNSUPPORTED;
138 }
139 }
140
141 return EFI_SUCCESS;
142 }
143
144 /**
145 Start this driver on ControllerHandle.
146
147 The Start() function is designed to be invoked from the EFI boot service ConnectController().
148 As a result, much of the error checking on the parameters to Start() has been moved into this
149 common boot service. It is legal to call Start() from other locations, but the following calling
150 restrictions must be followed or the system behavior will not be deterministic.
151 1. ControllerHandle must be a valid EFI_HANDLE.
152 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
153 EFI_DEVICE_PATH_PROTOCOL.
154 3. Prior to calling Start(), the Supported() function for the driver specified by This must
155 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
156
157 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
158 @param[in] ControllerHandle The handle of the controller to start. This handle
159 must support a protocol interface that supplies
160 an I/O abstraction to the driver.
161 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
162 This parameter is ignored by device drivers, and is optional for bus drivers.
163
164 @retval EFI_SUCCESS The device was started.
165 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
166 Currently not implemented.
167 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
168 @retval Others The driver failded to start the device.
169 **/
170 EFI_STATUS
171 EFIAPI
172 IScsiDriverBindingStart (
173 IN EFI_DRIVER_BINDING_PROTOCOL *This,
174 IN EFI_HANDLE ControllerHandle,
175 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
176 )
177 {
178 EFI_STATUS Status;
179 ISCSI_DRIVER_DATA *Private;
180 VOID *Interface;
181
182 Private = IScsiCreateDriverData (This->DriverBindingHandle, ControllerHandle);
183 if (Private == NULL) {
184 return EFI_OUT_OF_RESOURCES;
185 }
186
187 //
188 // Create a underlayer child instance, but not need to configure it. Just open ChildHandle
189 // via BY_DRIVER. That is, establishing the relationship between ControllerHandle and ChildHandle.
190 // Therefore, when DisconnectController(), especially VLAN virtual controller handle,
191 // IScsiDriverBindingStop() will be called.
192 //
193 Status = NetLibCreateServiceChild (
194 ControllerHandle,
195 This->DriverBindingHandle,
196 &gEfiTcp4ServiceBindingProtocolGuid,
197 &Private->ChildHandle
198 );
199
200 if (EFI_ERROR (Status)) {
201 goto ON_ERROR;
202 }
203
204 Status = gBS->OpenProtocol (
205 Private->ChildHandle,
206 &gEfiTcp4ProtocolGuid,
207 &Interface,
208 This->DriverBindingHandle,
209 ControllerHandle,
210 EFI_OPEN_PROTOCOL_BY_DRIVER
211 );
212 if (EFI_ERROR (Status)) {
213 goto ON_ERROR;
214 }
215
216 //
217 // Always install private protocol no matter what happens later. We need to
218 // keep the relationship between ControllerHandle and ChildHandle.
219 //
220 Status = gBS->InstallProtocolInterface (
221 &ControllerHandle,
222 &gEfiCallerIdGuid,
223 EFI_NATIVE_INTERFACE,
224 &Private->IScsiIdentifier
225 );
226 if (EFI_ERROR (Status)) {
227 goto ON_ERROR;
228 }
229
230 //
231 // Try to add a port configuration page for this controller.
232 //
233 IScsiConfigUpdateForm (This->DriverBindingHandle, ControllerHandle, TRUE);
234
235 //
236 // Get the iSCSI configuration data of this controller.
237 //
238 Status = IScsiGetConfigData (Private);
239 if (EFI_ERROR (Status)) {
240 goto ON_ERROR;
241 }
242 //
243 // Try to login and create an iSCSI session according to the configuration.
244 //
245 Status = IScsiSessionLogin (Private);
246 if (Status == EFI_MEDIA_CHANGED) {
247 //
248 // The specified target is not available and the redirection information is
249 // got, login the session again with the updated target address.
250 //
251 Status = IScsiSessionLogin (Private);
252 }
253
254 if (EFI_ERROR (Status)) {
255 goto ON_ERROR;
256 }
257 //
258 // Duplicate the Session's tcp connection device path. The source port field
259 // will be set to zero as one iSCSI session is comprised of several iSCSI
260 // connections.
261 //
262 Private->DevicePath = IScsiGetTcpConnDevicePath (Private);
263 if (Private->DevicePath == NULL) {
264 goto ON_ERROR;
265 }
266 //
267 // Install the updated device path onto the ExtScsiPassThruHandle.
268 //
269 Status = gBS->InstallProtocolInterface (
270 &Private->ExtScsiPassThruHandle,
271 &gEfiDevicePathProtocolGuid,
272 EFI_NATIVE_INTERFACE,
273 Private->DevicePath
274 );
275 if (EFI_ERROR (Status)) {
276 goto ON_ERROR;
277 }
278
279 //
280 // Update/Publish the iSCSI Boot Firmware Table.
281 //
282 IScsiPublishIbft ();
283
284 return EFI_SUCCESS;
285
286 ON_ERROR:
287
288 IScsiSessionAbort (&Private->Session);
289
290 return Status;
291 }
292
293 /**
294 Stop this driver on ControllerHandle.
295
296 Release the control of this controller and remove the IScsi functions. The Stop()
297 function is designed to be invoked from the EFI boot service DisconnectController().
298 As a result, much of the error checking on the parameters to Stop() has been moved
299 into this common boot service. It is legal to call Stop() from other locations,
300 but the following calling restrictions must be followed or the system behavior will not be deterministic.
301 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
302 same driver's Start() function.
303 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
304 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
305 Start() function, and the Start() function must have called OpenProtocol() on
306 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
307
308 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
309 @param[in] ControllerHandle A handle to the device being stopped. The handle must
310 support a bus specific I/O protocol for the driver
311 to use to stop the device.
312 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.Not used.
313 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
314 if NumberOfChildren is 0.Not used.
315
316 @retval EFI_SUCCESS The device was stopped.
317 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
318 **/
319 EFI_STATUS
320 EFIAPI
321 IScsiDriverBindingStop (
322 IN EFI_DRIVER_BINDING_PROTOCOL *This,
323 IN EFI_HANDLE ControllerHandle,
324 IN UINTN NumberOfChildren,
325 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
326 )
327 {
328 EFI_HANDLE IScsiController;
329 EFI_STATUS Status;
330 ISCSI_PRIVATE_PROTOCOL *IScsiIdentifier;
331 ISCSI_DRIVER_DATA *Private;
332 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru;
333 ISCSI_CONNECTION *Conn;
334
335 if (NumberOfChildren != 0) {
336 //
337 // We should have only one child.
338 //
339 Status = gBS->OpenProtocol (
340 ChildHandleBuffer[0],
341 &gEfiExtScsiPassThruProtocolGuid,
342 (VOID **) &PassThru,
343 This->DriverBindingHandle,
344 ControllerHandle,
345 EFI_OPEN_PROTOCOL_GET_PROTOCOL
346 );
347 if (EFI_ERROR (Status)) {
348 return EFI_DEVICE_ERROR;
349 }
350
351 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru);
352 Conn = NET_LIST_HEAD (&Private->Session.Conns, ISCSI_CONNECTION, Link);
353
354 //
355 // Previously the TCP4 protocol is opened BY_CHILD_CONTROLLER. Just close
356 // the protocol here but not uninstall the device path protocol and
357 // EXT SCSI PASS THRU protocol installed on ExtScsiPassThruHandle.
358 //
359 gBS->CloseProtocol (
360 Conn->Tcp4Io.Handle,
361 &gEfiTcp4ProtocolGuid,
362 Private->Image,
363 Private->ExtScsiPassThruHandle
364 );
365
366 return EFI_SUCCESS;
367 }
368 //
369 // Get the handle of the controller we are controling.
370 //
371 IScsiController = NetLibGetNicHandle (ControllerHandle, &gEfiTcp4ProtocolGuid);
372
373 Status = gBS->OpenProtocol (
374 IScsiController,
375 &gEfiCallerIdGuid,
376 (VOID **)&IScsiIdentifier,
377 This->DriverBindingHandle,
378 ControllerHandle,
379 EFI_OPEN_PROTOCOL_GET_PROTOCOL
380 );
381 if (EFI_ERROR (Status)) {
382 return EFI_DEVICE_ERROR;
383 }
384
385 Private = ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier);
386
387 if (Private->ChildHandle != NULL) {
388 Status = gBS->CloseProtocol (
389 Private->ChildHandle,
390 &gEfiTcp4ProtocolGuid,
391 This->DriverBindingHandle,
392 IScsiController
393 );
394
395 ASSERT (!EFI_ERROR (Status));
396
397 Status = NetLibDestroyServiceChild (
398 IScsiController,
399 This->DriverBindingHandle,
400 &gEfiTcp4ServiceBindingProtocolGuid,
401 Private->ChildHandle
402 );
403 ASSERT (!EFI_ERROR (Status));
404 }
405
406 IScsiConfigUpdateForm (This->DriverBindingHandle, IScsiController, FALSE);
407
408 //
409 // Uninstall the private protocol.
410 //
411 gBS->UninstallProtocolInterface (
412 IScsiController,
413 &gEfiCallerIdGuid,
414 &Private->IScsiIdentifier
415 );
416
417 //
418 // Update the iSCSI Boot Firware Table.
419 //
420 IScsiPublishIbft ();
421
422 IScsiSessionAbort (&Private->Session);
423 IScsiCleanDriverData (Private);
424
425 return EFI_SUCCESS;
426 }
427
428 /**
429 Unloads an image(the iSCSI driver).
430
431 @param[in] ImageHandle Handle that identifies the image to be unloaded.
432
433 @retval EFI_SUCCESS The image has been unloaded.
434 @retval Others Other errors as indicated.
435 **/
436 EFI_STATUS
437 EFIAPI
438 EfiIScsiUnload (
439 IN EFI_HANDLE ImageHandle
440 )
441 {
442 EFI_STATUS Status;
443 UINTN DeviceHandleCount;
444 EFI_HANDLE *DeviceHandleBuffer;
445 UINTN Index;
446
447 //
448 // Try to disonnect the driver from the devices it's controlling.
449 //
450 Status = gBS->LocateHandleBuffer (
451 AllHandles,
452 NULL,
453 NULL,
454 &DeviceHandleCount,
455 &DeviceHandleBuffer
456 );
457 if (!EFI_ERROR (Status)) {
458 for (Index = 0; Index < DeviceHandleCount; Index++) {
459 Status = gBS->DisconnectController (
460 DeviceHandleBuffer[Index],
461 ImageHandle,
462 NULL
463 );
464 }
465
466 if (DeviceHandleBuffer != NULL) {
467 FreePool (DeviceHandleBuffer);
468 }
469 }
470 //
471 // Unload the iSCSI configuration form.
472 //
473 IScsiConfigFormUnload (gIScsiDriverBinding.DriverBindingHandle);
474
475 //
476 // Uninstall the protocols installed by iSCSI driver.
477 //
478 Status = gBS->UninstallMultipleProtocolInterfaces (
479 ImageHandle,
480 &gEfiDriverBindingProtocolGuid,
481 &gIScsiDriverBinding,
482 &gEfiComponentName2ProtocolGuid,
483 &gIScsiComponentName2,
484 &gEfiComponentNameProtocolGuid,
485 &gIScsiComponentName,
486 &gEfiIScsiInitiatorNameProtocolGuid,
487 &gIScsiInitiatorName,
488 NULL
489 );
490
491 return Status;
492 }
493
494 /**
495 This is the declaration of an EFI image entry point. This entry point is
496 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
497 both device drivers and bus drivers. It initialize the global variables and
498 publish the driver binding protocol.
499
500 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
501 @param[in] SystemTable A pointer to the EFI System Table.
502
503 @retval EFI_SUCCESS The operation completed successfully.
504 @retval EFI_ACCESS_DENIED EFI_ISCSI_INITIATOR_NAME_PROTOCOL was installed unexpectedly.
505 @retval Others Other errors as indicated.
506 **/
507 EFI_STATUS
508 EFIAPI
509 IScsiDriverEntryPoint (
510 IN EFI_HANDLE ImageHandle,
511 IN EFI_SYSTEM_TABLE *SystemTable
512 )
513 {
514 EFI_STATUS Status;
515 EFI_ISCSI_INITIATOR_NAME_PROTOCOL *IScsiInitiatorName;
516
517 //
518 // There should be only one EFI_ISCSI_INITIATOR_NAME_PROTOCOL.
519 //
520 Status = gBS->LocateProtocol (
521 &gEfiIScsiInitiatorNameProtocolGuid,
522 NULL,
523 (VOID**) &IScsiInitiatorName
524 );
525
526 if (!EFI_ERROR (Status)) {
527 return EFI_ACCESS_DENIED;
528 }
529
530 //
531 // Initialize the EFI Driver Library
532 //
533 Status = EfiLibInstallDriverBindingComponentName2 (
534 ImageHandle,
535 SystemTable,
536 &gIScsiDriverBinding,
537 ImageHandle,
538 &gIScsiComponentName,
539 &gIScsiComponentName2
540 );
541
542 if (!EFI_ERROR (Status)) {
543 //
544 // Install the iSCSI Initiator Name Protocol.
545 //
546 Status = gBS->InstallProtocolInterface (
547 &ImageHandle,
548 &gEfiIScsiInitiatorNameProtocolGuid,
549 EFI_NATIVE_INTERFACE,
550 &gIScsiInitiatorName
551 );
552 if (EFI_ERROR (Status)) {
553 gBS->UninstallMultipleProtocolInterfaces (
554 ImageHandle,
555 &gEfiDriverBindingProtocolGuid,
556 &gIScsiDriverBinding,
557 &gEfiComponentName2ProtocolGuid,
558 &gIScsiComponentName2,
559 &gEfiComponentNameProtocolGuid,
560 &gIScsiComponentName,
561 NULL
562 );
563 return Status;
564 }
565
566 //
567 // Initialize the configuration form of iSCSI.
568 //
569 Status = IScsiConfigFormInit ();
570 if (EFI_ERROR (Status)) {
571 gBS->UninstallMultipleProtocolInterfaces (
572 ImageHandle,
573 &gEfiDriverBindingProtocolGuid,
574 &gIScsiDriverBinding,
575 &gEfiComponentName2ProtocolGuid,
576 &gIScsiComponentName2,
577 &gEfiComponentNameProtocolGuid,
578 &gIScsiComponentName,
579 &gEfiIScsiInitiatorNameProtocolGuid,
580 &gIScsiInitiatorName,
581 NULL
582 );
583 }
584 }
585 return Status;
586 }
587