]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/IScsiDxe/IScsiDriver.c
NetworkPkg:Add scriptable configuration to iSCSI driver by leveraging x-UEFI.
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiDriver.c
1 /** @file
2 The entry point of IScsi driver.
3
4 Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2017 Hewlett Packard Enterprise Development LP<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "IScsiImpl.h"
18
19 EFI_DRIVER_BINDING_PROTOCOL gIScsiIp4DriverBinding = {
20 IScsiIp4DriverBindingSupported,
21 IScsiIp4DriverBindingStart,
22 IScsiIp4DriverBindingStop,
23 0xa,
24 NULL,
25 NULL
26 };
27
28 EFI_DRIVER_BINDING_PROTOCOL gIScsiIp6DriverBinding = {
29 IScsiIp6DriverBindingSupported,
30 IScsiIp6DriverBindingStart,
31 IScsiIp6DriverBindingStop,
32 0xa,
33 NULL,
34 NULL
35 };
36
37 EFI_GUID gIScsiV4PrivateGuid = ISCSI_V4_PRIVATE_GUID;
38 EFI_GUID gIScsiV6PrivateGuid = ISCSI_V6_PRIVATE_GUID;
39 ISCSI_PRIVATE_DATA *mPrivate = NULL;
40
41 /**
42 Tests to see if this driver supports the RemainingDevicePath.
43
44 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
45 parameter is ignored by device drivers, and is optional for bus
46 drivers. For bus drivers, if this parameter is not NULL, then
47 the bus driver must determine if the bus controller specified
48 by ControllerHandle and the child controller specified
49 by RemainingDevicePath are both supported by this
50 bus driver.
51
52 @retval EFI_SUCCESS The RemainingDevicePath is supported or NULL.
53 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
54 RemainingDevicePath is not supported by the driver specified by This.
55 **/
56 EFI_STATUS
57 IScsiIsDevicePathSupported (
58 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
59 )
60 {
61 EFI_DEVICE_PATH_PROTOCOL *CurrentDevicePath;
62
63 CurrentDevicePath = RemainingDevicePath;
64 if (CurrentDevicePath != NULL) {
65 while (!IsDevicePathEnd (CurrentDevicePath)) {
66 if ((CurrentDevicePath->Type == MESSAGING_DEVICE_PATH) && (CurrentDevicePath->SubType == MSG_ISCSI_DP)) {
67 return EFI_SUCCESS;
68 }
69
70 CurrentDevicePath = NextDevicePathNode (CurrentDevicePath);
71 }
72
73 return EFI_UNSUPPORTED;
74 }
75
76 return EFI_SUCCESS;
77 }
78
79 /**
80 Check whether an iSCSI HBA adapter already installs an AIP instance with
81 network boot policy matching the value specified in PcdIScsiAIPNetworkBootPolicy.
82 If yes, return EFI_SUCCESS.
83
84 @retval EFI_SUCCESS Found an AIP with matching network boot policy.
85 @retval EFI_NOT_FOUND AIP is unavailable or the network boot policy
86 not matched.
87 **/
88 EFI_STATUS
89 IScsiCheckAip (
90 )
91 {
92 UINTN AipHandleCount;
93 EFI_HANDLE *AipHandleBuffer;
94 UINTN AipIndex;
95 EFI_ADAPTER_INFORMATION_PROTOCOL *Aip;
96 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru;
97 EFI_GUID *InfoTypesBuffer;
98 UINTN InfoTypeBufferCount;
99 UINTN TypeIndex;
100 VOID *InfoBlock;
101 UINTN InfoBlockSize;
102 BOOLEAN Supported;
103 EFI_ADAPTER_INFO_NETWORK_BOOT *NetworkBoot;
104 EFI_STATUS Status;
105 UINT8 NetworkBootPolicy;
106
107 //
108 // Check any AIP instances exist in system.
109 //
110 AipHandleCount = 0;
111 AipHandleBuffer = NULL;
112 Status = gBS->LocateHandleBuffer (
113 ByProtocol,
114 &gEfiAdapterInformationProtocolGuid,
115 NULL,
116 &AipHandleCount,
117 &AipHandleBuffer
118 );
119 if (EFI_ERROR (Status) || AipHandleCount == 0) {
120 return EFI_NOT_FOUND;
121 }
122
123 ASSERT (AipHandleBuffer != NULL);
124
125 InfoBlock = NULL;
126
127 for (AipIndex = 0; AipIndex < AipHandleCount; AipIndex++) {
128 Status = gBS->HandleProtocol (
129 AipHandleBuffer[AipIndex],
130 &gEfiAdapterInformationProtocolGuid,
131 (VOID *) &Aip
132 );
133 ASSERT_EFI_ERROR (Status);
134 ASSERT (Aip != NULL);
135
136 Status = gBS->HandleProtocol (
137 AipHandleBuffer[AipIndex],
138 &gEfiExtScsiPassThruProtocolGuid,
139 (VOID *) &ExtScsiPassThru
140 );
141 if (EFI_ERROR (Status) || ExtScsiPassThru == NULL) {
142 continue;
143 }
144
145 InfoTypesBuffer = NULL;
146 InfoTypeBufferCount = 0;
147 Status = Aip->GetSupportedTypes (Aip, &InfoTypesBuffer, &InfoTypeBufferCount);
148 if (EFI_ERROR (Status) || InfoTypesBuffer == NULL) {
149 continue;
150 }
151 //
152 // Check whether the AIP instance has Network boot information block.
153 //
154 Supported = FALSE;
155 for (TypeIndex = 0; TypeIndex < InfoTypeBufferCount; TypeIndex++) {
156 if (CompareGuid (&InfoTypesBuffer[TypeIndex], &gEfiAdapterInfoNetworkBootGuid)) {
157 Supported = TRUE;
158 break;
159 }
160 }
161
162 FreePool (InfoTypesBuffer);
163 if (!Supported) {
164 continue;
165 }
166
167 //
168 // We now have network boot information block.
169 //
170 InfoBlock = NULL;
171 InfoBlockSize = 0;
172 Status = Aip->GetInformation (Aip, &gEfiAdapterInfoNetworkBootGuid, &InfoBlock, &InfoBlockSize);
173 if (EFI_ERROR (Status) || InfoBlock == NULL) {
174 continue;
175 }
176
177 //
178 // Check whether the network boot policy matches.
179 //
180 NetworkBoot = (EFI_ADAPTER_INFO_NETWORK_BOOT *) InfoBlock;
181 NetworkBootPolicy = PcdGet8 (PcdIScsiAIPNetworkBootPolicy);
182
183 if (NetworkBootPolicy == STOP_UEFI_ISCSI_IF_HBA_INSTALL_AIP) {
184 Status = EFI_SUCCESS;
185 goto Exit;
186 }
187 if (((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_IP4) != 0 &&
188 !NetworkBoot->iScsiIpv4BootCapablity) ||
189 ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_IP6) != 0 &&
190 !NetworkBoot->iScsiIpv6BootCapablity) ||
191 ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_OFFLOAD) != 0 &&
192 !NetworkBoot->OffloadCapability) ||
193 ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_MPIO) != 0 &&
194 !NetworkBoot->iScsiMpioCapability) ||
195 ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_CONFIGURED_IP4) != 0 &&
196 !NetworkBoot->iScsiIpv4Boot) ||
197 ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_CONFIGURED_IP6) != 0 &&
198 !NetworkBoot->iScsiIpv6Boot)) {
199 FreePool (InfoBlock);
200 continue;
201 }
202
203 Status = EFI_SUCCESS;
204 goto Exit;
205 }
206
207 Status = EFI_NOT_FOUND;
208
209 Exit:
210 if (InfoBlock != NULL) {
211 FreePool (InfoBlock);
212 }
213 if (AipHandleBuffer != NULL) {
214 FreePool (AipHandleBuffer);
215 }
216 return Status;
217 }
218
219 /**
220 Tests to see if this driver supports a given controller. This is the worker function for
221 IScsiIp4(6)DriverBindingSupported.
222
223 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
224 @param[in] ControllerHandle The handle of the controller to test. This handle
225 must support a protocol interface that supplies
226 an I/O abstraction to the driver.
227 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
228 parameter is ignored by device drivers, and is optional for bus
229 drivers. For bus drivers, if this parameter is not NULL, then
230 the bus driver must determine if the bus controller specified
231 by ControllerHandle and the child controller specified
232 by RemainingDevicePath are both supported by this
233 bus driver.
234 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
235
236 @retval EFI_SUCCESS The device specified by ControllerHandle and
237 RemainingDevicePath is supported by the driver specified by This.
238 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
239 RemainingDevicePath is already being managed by the driver
240 specified by This.
241 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
242 RemainingDevicePath is not supported by the driver specified by This.
243 **/
244 EFI_STATUS
245 EFIAPI
246 IScsiSupported (
247 IN EFI_DRIVER_BINDING_PROTOCOL *This,
248 IN EFI_HANDLE ControllerHandle,
249 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL,
250 IN UINT8 IpVersion
251 )
252 {
253 EFI_STATUS Status;
254 EFI_GUID *IScsiServiceBindingGuid;
255 EFI_GUID *TcpServiceBindingGuid;
256 EFI_GUID *DhcpServiceBindingGuid;
257 EFI_GUID *DnsServiceBindingGuid;
258
259 if (IpVersion == IP_VERSION_4) {
260 IScsiServiceBindingGuid = &gIScsiV4PrivateGuid;
261 TcpServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid;
262 DhcpServiceBindingGuid = &gEfiDhcp4ServiceBindingProtocolGuid;
263 DnsServiceBindingGuid = &gEfiDns4ServiceBindingProtocolGuid;
264
265 } else {
266 IScsiServiceBindingGuid = &gIScsiV6PrivateGuid;
267 TcpServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid;
268 DhcpServiceBindingGuid = &gEfiDhcp6ServiceBindingProtocolGuid;
269 DnsServiceBindingGuid = &gEfiDns6ServiceBindingProtocolGuid;
270 }
271
272 Status = gBS->OpenProtocol (
273 ControllerHandle,
274 IScsiServiceBindingGuid,
275 NULL,
276 This->DriverBindingHandle,
277 ControllerHandle,
278 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
279 );
280 if (!EFI_ERROR (Status)) {
281 return EFI_ALREADY_STARTED;
282 }
283
284 Status = gBS->OpenProtocol (
285 ControllerHandle,
286 TcpServiceBindingGuid,
287 NULL,
288 This->DriverBindingHandle,
289 ControllerHandle,
290 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
291 );
292 if (EFI_ERROR (Status)) {
293 return EFI_UNSUPPORTED;
294 }
295
296 Status = IScsiIsDevicePathSupported (RemainingDevicePath);
297 if (EFI_ERROR (Status)) {
298 return EFI_UNSUPPORTED;
299 }
300
301 if (IScsiDhcpIsConfigured (ControllerHandle, IpVersion)) {
302 Status = gBS->OpenProtocol (
303 ControllerHandle,
304 DhcpServiceBindingGuid,
305 NULL,
306 This->DriverBindingHandle,
307 ControllerHandle,
308 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
309 );
310 if (EFI_ERROR (Status)) {
311 return EFI_UNSUPPORTED;
312 }
313 }
314
315 if (IScsiDnsIsConfigured (ControllerHandle)) {
316 Status = gBS->OpenProtocol (
317 ControllerHandle,
318 DnsServiceBindingGuid,
319 NULL,
320 This->DriverBindingHandle,
321 ControllerHandle,
322 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
323 );
324 if (EFI_ERROR (Status)) {
325 return EFI_UNSUPPORTED;
326 }
327 }
328
329 return EFI_SUCCESS;
330 }
331
332
333 /**
334 Start to manage the controller. This is the worker function for
335 IScsiIp4(6)DriverBindingStart.
336
337 @param[in] Image Handle of the image.
338 @param[in] ControllerHandle Handle of the controller.
339 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
340
341 @retval EFI_SUCCES This driver was started.
342 @retval EFI_ALREADY_STARTED This driver is already running on this device.
343 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
344 @retval EFI_NOT_FOUND There is no sufficient information to establish
345 the iScsi session.
346 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
347 @retval EFI_DEVICE_ERROR Failed to get TCP connection device path.
348 @retval EFI_ACCESS_DENIED The protocol could not be removed from the Handle
349 because its interfaces are being used.
350
351 **/
352 EFI_STATUS
353 IScsiStart (
354 IN EFI_HANDLE Image,
355 IN EFI_HANDLE ControllerHandle,
356 IN UINT8 IpVersion
357 )
358 {
359 EFI_STATUS Status;
360 ISCSI_DRIVER_DATA *Private;
361 LIST_ENTRY *Entry;
362 LIST_ENTRY *NextEntry;
363 ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
364 ISCSI_SESSION *Session;
365 UINT8 Index;
366 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExistIScsiExtScsiPassThru;
367 ISCSI_DRIVER_DATA *ExistPrivate;
368 UINT8 *AttemptConfigOrder;
369 UINTN AttemptConfigOrderSize;
370 UINT8 BootSelected;
371 EFI_HANDLE *HandleBuffer;
372 UINTN NumberOfHandles;
373 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
374 EFI_GUID *IScsiPrivateGuid;
375 EFI_GUID *TcpServiceBindingGuid;
376 BOOLEAN NeedUpdate;
377 VOID *Interface;
378 EFI_GUID *ProtocolGuid;
379 UINT8 NetworkBootPolicy;
380 ISCSI_SESSION_CONFIG_NVDATA *NvData;
381
382 //
383 // Test to see if iSCSI driver supports the given controller.
384 //
385
386 if (IpVersion == IP_VERSION_4) {
387 IScsiPrivateGuid = &gIScsiV4PrivateGuid;
388 TcpServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid;
389 ProtocolGuid = &gEfiTcp4ProtocolGuid;
390 } else if (IpVersion == IP_VERSION_6) {
391 IScsiPrivateGuid = &gIScsiV6PrivateGuid;
392 TcpServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid;
393 ProtocolGuid = &gEfiTcp6ProtocolGuid;
394 } else {
395 return EFI_INVALID_PARAMETER;
396 }
397
398 Status = gBS->OpenProtocol (
399 ControllerHandle,
400 IScsiPrivateGuid,
401 NULL,
402 Image,
403 ControllerHandle,
404 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
405 );
406 if (!EFI_ERROR (Status)) {
407 return EFI_ALREADY_STARTED;
408 }
409
410 Status = gBS->OpenProtocol (
411 ControllerHandle,
412 TcpServiceBindingGuid,
413 NULL,
414 Image,
415 ControllerHandle,
416 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
417 );
418 if (EFI_ERROR (Status)) {
419 return EFI_UNSUPPORTED;
420 }
421
422 NetworkBootPolicy = PcdGet8 (PcdIScsiAIPNetworkBootPolicy);
423 if (NetworkBootPolicy == ALWAYS_USE_ISCSI_HBA_AND_IGNORE_UEFI_ISCSI) {
424 return EFI_ABORTED;
425 }
426
427 if (NetworkBootPolicy != ALWAYS_USE_UEFI_ISCSI_AND_IGNORE_ISCSI_HBA) {
428 //
429 // Check existing iSCSI AIP.
430 //
431 Status = IScsiCheckAip ();
432 if (!EFI_ERROR (Status)) {
433 //
434 // Find iSCSI AIP with specified network boot policy. return EFI_ABORTED.
435 //
436 return EFI_ABORTED;
437 }
438 }
439
440 //
441 // Record the incoming NIC info.
442 //
443 Status = IScsiAddNic (ControllerHandle);
444 if (EFI_ERROR (Status)) {
445 return Status;
446 }
447
448 //
449 // Create the instance private data.
450 //
451 Private = IScsiCreateDriverData (Image, ControllerHandle);
452 if (Private == NULL) {
453 return EFI_OUT_OF_RESOURCES;
454 }
455
456 //
457 // Create a underlayer child instance, but not need to configure it. Just open ChildHandle
458 // via BY_DRIVER. That is, establishing the relationship between ControllerHandle and ChildHandle.
459 // Therefore, when DisconnectController(), especially VLAN virtual controller handle,
460 // IScsiDriverBindingStop() will be called.
461 //
462 Status = NetLibCreateServiceChild (
463 ControllerHandle,
464 Image,
465 TcpServiceBindingGuid,
466 &Private->ChildHandle
467 );
468
469 if (EFI_ERROR (Status)) {
470 goto ON_ERROR;
471 }
472
473 Status = gBS->OpenProtocol (
474 Private->ChildHandle, /// Default Tcp child
475 ProtocolGuid,
476 &Interface,
477 Image,
478 ControllerHandle,
479 EFI_OPEN_PROTOCOL_BY_DRIVER
480 );
481
482 if (EFI_ERROR (Status)) {
483 goto ON_ERROR;
484 }
485
486 //
487 // Always install private protocol no matter what happens later. We need to
488 // keep the relationship between ControllerHandle and ChildHandle.
489 //
490 Status = gBS->InstallProtocolInterface (
491 &ControllerHandle,
492 IScsiPrivateGuid,
493 EFI_NATIVE_INTERFACE,
494 &Private->IScsiIdentifier
495 );
496 if (EFI_ERROR (Status)) {
497 goto ON_ERROR;
498 }
499
500 if (IpVersion == IP_VERSION_4) {
501 mPrivate->Ipv6Flag = FALSE;
502 } else {
503 mPrivate->Ipv6Flag = TRUE;
504 }
505
506 //
507 // Get the current iSCSI configuration data.
508 //
509 Status = IScsiGetConfigData (Private);
510 if (EFI_ERROR (Status)) {
511 goto ON_ERROR;
512 }
513
514 //
515 // If there is already a successul attempt, check whether this attempt is the
516 // first "enabled for MPIO" attempt. If not, still try the first attempt.
517 // In single path mode, try all attempts.
518 //
519 ExistPrivate = NULL;
520 Status = EFI_NOT_FOUND;
521
522 if (mPrivate->OneSessionEstablished && mPrivate->EnableMpio) {
523 AttemptConfigData = NULL;
524 NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) {
525 AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
526 if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) {
527 break;
528 }
529 }
530
531 if (AttemptConfigData == NULL) {
532 goto ON_ERROR;
533 }
534
535 if (AttemptConfigData->AttemptConfigIndex == mPrivate->BootSelectedIndex) {
536 goto ON_EXIT;
537 }
538
539 //
540 // Uninstall the original ExtScsiPassThru first.
541 //
542
543 //
544 // Locate all ExtScsiPassThru protocol instances.
545 //
546 Status = gBS->LocateHandleBuffer (
547 ByProtocol,
548 &gEfiExtScsiPassThruProtocolGuid,
549 NULL,
550 &NumberOfHandles,
551 &HandleBuffer
552 );
553 if (EFI_ERROR (Status)) {
554 goto ON_ERROR;
555 }
556
557 //
558 // Find ExtScsiPassThru protocol instance produced by this driver.
559 //
560 ExistIScsiExtScsiPassThru = NULL;
561 for (Index = 0; Index < NumberOfHandles && ExistIScsiExtScsiPassThru == NULL; Index++) {
562 Status = gBS->HandleProtocol (
563 HandleBuffer[Index],
564 &gEfiDevicePathProtocolGuid,
565 (VOID **) &DevicePath
566 );
567 if (EFI_ERROR (Status)) {
568 continue;
569 }
570
571 while (!IsDevicePathEnd (DevicePath)) {
572 if ((DevicePath->Type == MESSAGING_DEVICE_PATH) && (DevicePath->SubType == MSG_MAC_ADDR_DP)) {
573 //
574 // Get the ExtScsiPassThru protocol instance.
575 //
576 Status = gBS->HandleProtocol (
577 HandleBuffer[Index],
578 &gEfiExtScsiPassThruProtocolGuid,
579 (VOID **) &ExistIScsiExtScsiPassThru
580 );
581 ASSERT_EFI_ERROR (Status);
582 break;
583 }
584
585 DevicePath = NextDevicePathNode (DevicePath);
586 }
587 }
588
589 FreePool (HandleBuffer);
590
591 if (ExistIScsiExtScsiPassThru == NULL) {
592 Status = EFI_NOT_FOUND;
593 goto ON_ERROR;
594 }
595
596 ExistPrivate = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (ExistIScsiExtScsiPassThru);
597
598 Status = gBS->UninstallProtocolInterface (
599 ExistPrivate->ExtScsiPassThruHandle,
600 &gEfiExtScsiPassThruProtocolGuid,
601 &ExistPrivate->IScsiExtScsiPassThru
602 );
603 if (EFI_ERROR (Status)) {
604 goto ON_ERROR;
605 }
606 }
607
608 //
609 // Install the Ext SCSI PASS THRU protocol.
610 //
611 Status = gBS->InstallProtocolInterface (
612 &Private->ExtScsiPassThruHandle,
613 &gEfiExtScsiPassThruProtocolGuid,
614 EFI_NATIVE_INTERFACE,
615 &Private->IScsiExtScsiPassThru
616 );
617 if (EFI_ERROR (Status)) {
618 goto ON_ERROR;
619 }
620
621 BootSelected = 0;
622
623 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) {
624 AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
625 //
626 // Don't process the attempt that does not associate with the current NIC or
627 // this attempt is disabled or established.
628 //
629 if (AttemptConfigData->NicIndex != mPrivate->CurrentNic ||
630 AttemptConfigData->SessionConfigData.Enabled == ISCSI_DISABLED ||
631 AttemptConfigData->ValidPath) {
632 continue;
633 }
634
635 //
636 // In multipath mode, don't process attempts configured for single path.
637 // In default single path mode, don't process attempts configured for multipath.
638 //
639 if ((mPrivate->EnableMpio &&
640 AttemptConfigData->SessionConfigData.Enabled != ISCSI_ENABLED_FOR_MPIO) ||
641 (!mPrivate->EnableMpio &&
642 AttemptConfigData->SessionConfigData.Enabled != ISCSI_ENABLED)) {
643 continue;
644 }
645
646 //
647 // Don't process the attempt that fails to get the init/target information from DHCP.
648 //
649 if (AttemptConfigData->SessionConfigData.InitiatorInfoFromDhcp &&
650 !AttemptConfigData->DhcpSuccess) {
651 if (!mPrivate->EnableMpio && mPrivate->ValidSinglePathCount > 0) {
652 mPrivate->ValidSinglePathCount--;
653 }
654 continue;
655 }
656
657 //
658 // Don't process the autoconfigure path if it is already established.
659 //
660 if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG &&
661 AttemptConfigData->AutoConfigureSuccess) {
662 continue;
663 }
664
665 //
666 // Don't process the attempt if its IP mode is not in the current IP version.
667 //
668 if (!mPrivate->Ipv6Flag) {
669 if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP6) {
670 continue;
671 }
672 if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG &&
673 AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6) {
674 continue;
675 }
676 } else {
677 if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP4) {
678 continue;
679 }
680 if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG &&
681 AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP4) {
682 continue;
683 }
684 }
685
686 //
687 // Fill in the Session and init it.
688 //
689 Session = (ISCSI_SESSION *) AllocateZeroPool (sizeof (ISCSI_SESSION));
690 if (Session == NULL) {
691 Status = EFI_OUT_OF_RESOURCES;
692 goto ON_ERROR;
693 }
694
695 Session->Private = Private;
696 Session->ConfigData = AttemptConfigData;
697 Session->AuthType = AttemptConfigData->AuthenticationType;
698
699 UnicodeSPrint (
700 mPrivate->PortString,
701 (UINTN) ISCSI_NAME_IFR_MAX_SIZE,
702 L"Attempt %d",
703 (UINTN) AttemptConfigData->AttemptConfigIndex
704 );
705
706 if (Session->AuthType == ISCSI_AUTH_TYPE_CHAP) {
707 Session->AuthData.CHAP.AuthConfig = &AttemptConfigData->AuthConfigData.CHAP;
708 }
709
710 IScsiSessionInit (Session, FALSE);
711
712 //
713 // Try to login and create an iSCSI session according to the configuration.
714 //
715 Status = IScsiSessionLogin (Session);
716 if (Status == EFI_MEDIA_CHANGED) {
717 //
718 // The specified target is not available, and the redirection information is
719 // received. Login the session again with the updated target address.
720 //
721 Status = IScsiSessionLogin (Session);
722 } else if (Status == EFI_NOT_READY) {
723 Status = IScsiSessionReLogin (Session);
724 }
725
726 //
727 // Restore the origial user setting which specifies the proxy/virtual iSCSI target to NV region.
728 //
729 NvData = &AttemptConfigData->SessionConfigData;
730 if (NvData->RedirectFlag) {
731 NvData->TargetPort = NvData->OriginalTargetPort;
732 CopyMem (&NvData->TargetIp, &NvData->OriginalTargetIp, sizeof (EFI_IP_ADDRESS));
733 NvData->RedirectFlag = FALSE;
734
735 gRT->SetVariable (
736 mPrivate->PortString,
737 &gEfiIScsiInitiatorNameProtocolGuid,
738 ISCSI_CONFIG_VAR_ATTR,
739 sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
740 AttemptConfigData
741 );
742 }
743
744 if (EFI_ERROR (Status)) {
745 //
746 // In Single path mode, only the successful attempt will be recorded in iBFT;
747 // in multi-path mode, all the attempt entries in MPIO will be recorded in iBFT.
748 //
749 if (!mPrivate->EnableMpio && mPrivate->ValidSinglePathCount > 0) {
750 mPrivate->ValidSinglePathCount--;
751 }
752
753 FreePool (Session);
754
755 } else {
756 AttemptConfigData->ValidPath = TRUE;
757
758 //
759 // Do not record the attempt in iBFT if it login with KRB5.
760 // TODO: record KRB5 attempt information in the iSCSI device path.
761 //
762 if (Session->AuthType == ISCSI_AUTH_TYPE_KRB) {
763 if (!mPrivate->EnableMpio && mPrivate->ValidSinglePathCount > 0) {
764 mPrivate->ValidSinglePathCount--;
765 }
766
767 AttemptConfigData->ValidiBFTPath = FALSE;
768 } else {
769 AttemptConfigData->ValidiBFTPath = TRUE;
770 }
771
772 //
773 // IScsi session success. Update the attempt state to NVR.
774 //
775 if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) {
776 AttemptConfigData->AutoConfigureSuccess = TRUE;
777 }
778
779 gRT->SetVariable (
780 mPrivate->PortString,
781 &gEfiIScsiInitiatorNameProtocolGuid,
782 ISCSI_CONFIG_VAR_ATTR,
783 sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
784 AttemptConfigData
785 );
786
787 //
788 // Select the first login session. Abort others.
789 //
790 if (Private->Session == NULL) {
791 Private->Session = Session;
792 BootSelected = AttemptConfigData->AttemptConfigIndex;
793 //
794 // Don't validate other attempt in multipath mode if one is success.
795 //
796 if (mPrivate->EnableMpio) {
797 break;
798 }
799 } else {
800 IScsiSessionAbort (Session);
801 FreePool (Session);
802 }
803 }
804 }
805
806 //
807 // All attempts configured for this driver instance are not valid.
808 //
809 if (Private->Session == NULL) {
810 Status = gBS->UninstallProtocolInterface (
811 Private->ExtScsiPassThruHandle,
812 &gEfiExtScsiPassThruProtocolGuid,
813 &Private->IScsiExtScsiPassThru
814 );
815 ASSERT_EFI_ERROR (Status);
816 Private->ExtScsiPassThruHandle = NULL;
817
818 //
819 // Reinstall the original ExtScsiPassThru back.
820 //
821 if (mPrivate->OneSessionEstablished && ExistPrivate != NULL) {
822 Status = gBS->InstallProtocolInterface (
823 &ExistPrivate->ExtScsiPassThruHandle,
824 &gEfiExtScsiPassThruProtocolGuid,
825 EFI_NATIVE_INTERFACE,
826 &ExistPrivate->IScsiExtScsiPassThru
827 );
828 if (EFI_ERROR (Status)) {
829 goto ON_ERROR;
830 }
831
832 goto ON_EXIT;
833 }
834
835 Status = EFI_NOT_FOUND;
836
837 goto ON_ERROR;
838 }
839
840 NeedUpdate = TRUE;
841 //
842 // More than one attempt successes.
843 //
844 if (Private->Session != NULL && mPrivate->OneSessionEstablished) {
845
846 AttemptConfigOrder = IScsiGetVariableAndSize (
847 L"AttemptOrder",
848 &gIScsiConfigGuid,
849 &AttemptConfigOrderSize
850 );
851 if (AttemptConfigOrder == NULL) {
852 goto ON_ERROR;
853 }
854 for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {
855 if (AttemptConfigOrder[Index] == mPrivate->BootSelectedIndex ||
856 AttemptConfigOrder[Index] == BootSelected) {
857 break;
858 }
859 }
860
861 if (mPrivate->EnableMpio) {
862 //
863 // Use the attempt in earlier order. Abort the later one in MPIO.
864 //
865 if (AttemptConfigOrder[Index] == mPrivate->BootSelectedIndex) {
866 IScsiSessionAbort (Private->Session);
867 FreePool (Private->Session);
868 Private->Session = NULL;
869 gBS->UninstallProtocolInterface (
870 Private->ExtScsiPassThruHandle,
871 &gEfiExtScsiPassThruProtocolGuid,
872 &Private->IScsiExtScsiPassThru
873 );
874 Private->ExtScsiPassThruHandle = NULL;
875
876 //
877 // Reinstall the original ExtScsiPassThru back.
878 //
879 Status = gBS->InstallProtocolInterface (
880 &ExistPrivate->ExtScsiPassThruHandle,
881 &gEfiExtScsiPassThruProtocolGuid,
882 EFI_NATIVE_INTERFACE,
883 &ExistPrivate->IScsiExtScsiPassThru
884 );
885 if (EFI_ERROR (Status)) {
886 goto ON_ERROR;
887 }
888
889 goto ON_EXIT;
890 } else {
891 if (AttemptConfigOrder[Index] != BootSelected) {
892 goto ON_ERROR;
893 }
894 mPrivate->BootSelectedIndex = BootSelected;
895 //
896 // Clear the resource in ExistPrivate.
897 //
898 gBS->UninstallProtocolInterface (
899 ExistPrivate->Controller,
900 IScsiPrivateGuid,
901 &ExistPrivate->IScsiIdentifier
902 );
903
904 IScsiRemoveNic (ExistPrivate->Controller);
905 if (ExistPrivate->Session != NULL) {
906 IScsiSessionAbort (ExistPrivate->Session);
907 }
908
909 if (ExistPrivate->DevicePath != NULL) {
910 Status = gBS->UninstallProtocolInterface (
911 ExistPrivate->ExtScsiPassThruHandle,
912 &gEfiDevicePathProtocolGuid,
913 ExistPrivate->DevicePath
914 );
915 if (EFI_ERROR (Status)) {
916 goto ON_ERROR;
917 }
918
919 FreePool (ExistPrivate->DevicePath);
920 }
921
922 gBS->CloseEvent (ExistPrivate->ExitBootServiceEvent);
923 FreePool (ExistPrivate);
924
925 }
926 } else {
927 //
928 // Use the attempt in earlier order as boot selected in single path mode.
929 //
930 if (AttemptConfigOrder[Index] == mPrivate->BootSelectedIndex) {
931 NeedUpdate = FALSE;
932 }
933 }
934
935 }
936
937 if (NeedUpdate) {
938 mPrivate->OneSessionEstablished = TRUE;
939 mPrivate->BootSelectedIndex = BootSelected;
940 }
941
942 //
943 // Duplicate the Session's tcp connection device path. The source port field
944 // will be set to zero as one iSCSI session is comprised of several iSCSI
945 // connections.
946 //
947 Private->DevicePath = IScsiGetTcpConnDevicePath (Private->Session);
948 if (Private->DevicePath == NULL) {
949 Status = EFI_DEVICE_ERROR;
950 goto ON_ERROR;
951 }
952 //
953 // Install the updated device path onto the ExtScsiPassThruHandle.
954 //
955 Status = gBS->InstallProtocolInterface (
956 &Private->ExtScsiPassThruHandle,
957 &gEfiDevicePathProtocolGuid,
958 EFI_NATIVE_INTERFACE,
959 Private->DevicePath
960 );
961 if (EFI_ERROR (Status)) {
962 goto ON_ERROR;
963 }
964
965 //
966 // ISCSI children should share the default Tcp child, just open the default Tcp child via BY_CHILD_CONTROLLER.
967 //
968 Status = gBS->OpenProtocol (
969 Private->ChildHandle, /// Default Tcp child
970 ProtocolGuid,
971 &Interface,
972 Image,
973 Private->ExtScsiPassThruHandle,
974 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
975 );
976 if (EFI_ERROR (Status)) {
977 gBS->UninstallMultipleProtocolInterfaces (
978 Private->ExtScsiPassThruHandle,
979 &gEfiExtScsiPassThruProtocolGuid,
980 &Private->IScsiExtScsiPassThru,
981 &gEfiDevicePathProtocolGuid,
982 Private->DevicePath,
983 NULL
984 );
985
986 goto ON_ERROR;
987 }
988
989 ON_EXIT:
990
991 //
992 // Update/Publish the iSCSI Boot Firmware Table.
993 //
994 if (mPrivate->BootSelectedIndex != 0) {
995 IScsiPublishIbft ();
996 }
997
998 return EFI_SUCCESS;
999
1000 ON_ERROR:
1001
1002 if (Private->Session != NULL) {
1003 IScsiSessionAbort (Private->Session);
1004 }
1005
1006 return Status;
1007 }
1008
1009 /**
1010 Stops a device controller or a bus controller. This is the worker function for
1011 IScsiIp4(6)DriverBindingStop.
1012
1013 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1014 @param[in] ControllerHandle A handle to the device being stopped. The handle must
1015 support a bus specific I/O protocol for the driver
1016 to use to stop the device.
1017 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1018 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1019 if NumberOfChildren is 0.
1020 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
1021
1022 @retval EFI_SUCCESS The device was stopped.
1023 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1024 @retval EFI_INVALID_PARAMETER Child handle is NULL.
1025 @retval EFI_ACCESS_DENIED The protocol could not be removed from the Handle
1026 because its interfaces are being used.
1027
1028 **/
1029 EFI_STATUS
1030 EFIAPI
1031 IScsiStop (
1032 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1033 IN EFI_HANDLE ControllerHandle,
1034 IN UINTN NumberOfChildren,
1035 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL,
1036 IN UINT8 IpVersion
1037 )
1038 {
1039 EFI_HANDLE IScsiController;
1040 EFI_STATUS Status;
1041 ISCSI_PRIVATE_PROTOCOL *IScsiIdentifier;
1042 ISCSI_DRIVER_DATA *Private;
1043 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru;
1044 ISCSI_CONNECTION *Conn;
1045 EFI_GUID *ProtocolGuid;
1046 EFI_GUID *TcpServiceBindingGuid;
1047 EFI_GUID *TcpProtocolGuid;
1048
1049
1050 if (NumberOfChildren != 0) {
1051 //
1052 // We should have only one child.
1053 //
1054 Status = gBS->OpenProtocol (
1055 ChildHandleBuffer[0],
1056 &gEfiExtScsiPassThruProtocolGuid,
1057 (VOID **) &PassThru,
1058 This->DriverBindingHandle,
1059 ControllerHandle,
1060 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1061 );
1062 if (EFI_ERROR (Status)) {
1063 return EFI_DEVICE_ERROR;
1064 }
1065
1066 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru);
1067 Conn = NET_LIST_HEAD (&Private->Session->Conns, ISCSI_CONNECTION, Link);
1068
1069 //
1070 // Previously the TCP protocol is opened BY_CHILD_CONTROLLER. Just close
1071 // the protocol here, but do not uninstall the device path protocol and
1072 // EXT SCSI PASS THRU protocol installed on ExtScsiPassThruHandle.
1073 //
1074 if (IpVersion == IP_VERSION_4) {
1075 ProtocolGuid = &gEfiTcp4ProtocolGuid;
1076 } else {
1077 ProtocolGuid = &gEfiTcp6ProtocolGuid;
1078 }
1079
1080 gBS->CloseProtocol (
1081 Private->ChildHandle,
1082 ProtocolGuid,
1083 Private->Image,
1084 Private->ExtScsiPassThruHandle
1085 );
1086
1087 gBS->CloseProtocol (
1088 Conn->TcpIo.Handle,
1089 ProtocolGuid,
1090 Private->Image,
1091 Private->ExtScsiPassThruHandle
1092 );
1093
1094 return EFI_SUCCESS;
1095 }
1096
1097 //
1098 // Get the handle of the controller we are controling.
1099 //
1100 if (IpVersion == IP_VERSION_4) {
1101 ProtocolGuid = &gIScsiV4PrivateGuid;
1102 TcpProtocolGuid = &gEfiTcp4ProtocolGuid;
1103 TcpServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid;
1104 } else {
1105 ProtocolGuid = &gIScsiV6PrivateGuid;
1106 TcpProtocolGuid = &gEfiTcp6ProtocolGuid;
1107 TcpServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid;
1108 }
1109 IScsiController = NetLibGetNicHandle (ControllerHandle, TcpProtocolGuid);
1110 if (IScsiController == NULL) {
1111 return EFI_SUCCESS;
1112 }
1113
1114 Status = gBS->OpenProtocol (
1115 IScsiController,
1116 ProtocolGuid,
1117 (VOID **) &IScsiIdentifier,
1118 This->DriverBindingHandle,
1119 ControllerHandle,
1120 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1121 );
1122 if (EFI_ERROR (Status)) {
1123 return EFI_DEVICE_ERROR;
1124 }
1125
1126 Private = ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier);
1127 ASSERT (Private != NULL);
1128
1129 if (Private->ChildHandle != NULL) {
1130 Status = gBS->CloseProtocol (
1131 Private->ChildHandle,
1132 TcpProtocolGuid,
1133 This->DriverBindingHandle,
1134 IScsiController
1135 );
1136
1137 ASSERT (!EFI_ERROR (Status));
1138
1139 Status = NetLibDestroyServiceChild (
1140 IScsiController,
1141 This->DriverBindingHandle,
1142 TcpServiceBindingGuid,
1143 Private->ChildHandle
1144 );
1145
1146 ASSERT (!EFI_ERROR (Status));
1147 }
1148
1149 gBS->UninstallProtocolInterface (
1150 IScsiController,
1151 ProtocolGuid,
1152 &Private->IScsiIdentifier
1153 );
1154
1155 //
1156 // Remove this NIC.
1157 //
1158 IScsiRemoveNic (IScsiController);
1159
1160 //
1161 // Update the iSCSI Boot Firware Table.
1162 //
1163 IScsiPublishIbft ();
1164
1165 if (Private->Session != NULL) {
1166 IScsiSessionAbort (Private->Session);
1167 }
1168
1169 Status = IScsiCleanDriverData (Private);
1170
1171 if (EFI_ERROR (Status)) {
1172 return Status;
1173 }
1174
1175 return EFI_SUCCESS;
1176 }
1177
1178 /**
1179 Tests to see if this driver supports a given controller. If a child device is provided,
1180 it tests to see if this driver supports creating a handle for the specified child device.
1181
1182 This function checks to see if the driver specified by This supports the device specified by
1183 ControllerHandle. Drivers typically use the device path attached to
1184 ControllerHandle and/or the services from the bus I/O abstraction attached to
1185 ControllerHandle to determine if the driver supports ControllerHandle. This function
1186 may be called many times during platform initialization. In order to reduce boot times, the tests
1187 performed by this function must be very small and take as little time as possible to execute. This
1188 function must not change the state of any hardware devices, and this function must be aware that the
1189 device specified by ControllerHandle may already be managed by the same driver or a
1190 different driver. This function must match its calls to AllocatePages() with FreePages(),
1191 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
1192 Since ControllerHandle may have been previously started by the same driver, if a protocol is
1193 already in the opened state, then it must not be closed with CloseProtocol(). This is required
1194 to guarantee the state of ControllerHandle is not modified by this function.
1195
1196 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1197 @param[in] ControllerHandle The handle of the controller to test. This handle
1198 must support a protocol interface that supplies
1199 an I/O abstraction to the driver.
1200 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
1201 parameter is ignored by device drivers, and is optional for bus
1202 drivers. For bus drivers, if this parameter is not NULL, then
1203 the bus driver must determine if the bus controller specified
1204 by ControllerHandle and the child controller specified
1205 by RemainingDevicePath are both supported by this
1206 bus driver.
1207
1208 @retval EFI_SUCCESS The device specified by ControllerHandle and
1209 RemainingDevicePath is supported by the driver specified by This.
1210 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
1211 RemainingDevicePath is already managed by the driver
1212 specified by This.
1213 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
1214 RemainingDevicePath is already managed by a different
1215 driver or an application that requires exclusive access.
1216 Currently not implemented.
1217 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
1218 RemainingDevicePath is not supported by the driver specified by This.
1219 **/
1220 EFI_STATUS
1221 EFIAPI
1222 IScsiIp4DriverBindingSupported (
1223 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1224 IN EFI_HANDLE ControllerHandle,
1225 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1226 )
1227 {
1228 return IScsiSupported (
1229 This,
1230 ControllerHandle,
1231 RemainingDevicePath,
1232 IP_VERSION_4
1233 );
1234 }
1235
1236 /**
1237 Starts a device controller or a bus controller.
1238
1239 The Start() function is designed to be invoked from the EFI boot service ConnectController().
1240 As a result, much of the error checking on the parameters to Start() has been moved into this
1241 common boot service. It is legal to call Start() from other locations,
1242 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1243 1. ControllerHandle must be a valid EFI_HANDLE.
1244 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
1245 EFI_DEVICE_PATH_PROTOCOL.
1246 3. Prior to calling Start(), the Supported() function for the driver specified by This must
1247 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
1248
1249 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1250 @param[in] ControllerHandle The handle of the controller to start. This handle
1251 must support a protocol interface that supplies
1252 an I/O abstraction to the driver.
1253 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
1254 parameter is ignored by device drivers, and is optional for bus
1255 drivers. For a bus driver, if this parameter is NULL, then handles
1256 for all the children of Controller are created by this driver.
1257 If this parameter is not NULL and the first Device Path Node is
1258 not the End of Device Path Node, then only the handle for the
1259 child device specified by the first Device Path Node of
1260 RemainingDevicePath is created by this driver.
1261 If the first Device Path Node of RemainingDevicePath is
1262 the End of Device Path Node, no child handle is created by this
1263 driver.
1264
1265 @retval EFI_SUCCESS The device was started.
1266 @retval EFI_DEVICE_ERROR The device could not be started due to a device error. Currently not implemented.
1267 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1268 @retval Others The driver failed to start the device.
1269
1270 **/
1271 EFI_STATUS
1272 EFIAPI
1273 IScsiIp4DriverBindingStart (
1274 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1275 IN EFI_HANDLE ControllerHandle,
1276 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1277 )
1278 {
1279 EFI_STATUS Status;
1280
1281 Status = IScsiStart (This->DriverBindingHandle, ControllerHandle, IP_VERSION_4);
1282 if (Status == EFI_ALREADY_STARTED) {
1283 Status = EFI_SUCCESS;
1284 }
1285
1286 return Status;
1287 }
1288
1289 /**
1290 Stops a device controller or a bus controller.
1291
1292 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
1293 As a result, much of the error checking on the parameters to Stop() has been moved
1294 into this common boot service. It is legal to call Stop() from other locations,
1295 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1296 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
1297 same driver's Start() function.
1298 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
1299 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
1300 Start() function, and the Start() function must have called OpenProtocol() on
1301 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
1302
1303 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1304 @param[in] ControllerHandle A handle to the device being stopped. The handle must
1305 support a bus specific I/O protocol for the driver
1306 to use to stop the device.
1307 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1308 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1309 if NumberOfChildren is 0.
1310
1311 @retval EFI_SUCCESS The device was stopped.
1312 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1313
1314 **/
1315 EFI_STATUS
1316 EFIAPI
1317 IScsiIp4DriverBindingStop (
1318 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1319 IN EFI_HANDLE ControllerHandle,
1320 IN UINTN NumberOfChildren,
1321 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
1322 )
1323 {
1324 return IScsiStop (
1325 This,
1326 ControllerHandle,
1327 NumberOfChildren,
1328 ChildHandleBuffer,
1329 IP_VERSION_4
1330 );
1331 }
1332
1333 /**
1334 Tests to see if this driver supports a given controller. If a child device is provided,
1335 it tests to see if this driver supports creating a handle for the specified child device.
1336
1337 This function checks to see if the driver specified by This supports the device specified by
1338 ControllerHandle. Drivers typically use the device path attached to
1339 ControllerHandle and/or the services from the bus I/O abstraction attached to
1340 ControllerHandle to determine if the driver supports ControllerHandle. This function
1341 may be called many times during platform initialization. In order to reduce boot times, the tests
1342 performed by this function must be very small and take as little time as possible to execute. This
1343 function must not change the state of any hardware devices, and this function must be aware that the
1344 device specified by ControllerHandle may already be managed by the same driver or a
1345 different driver. This function must match its calls to AllocatePages() with FreePages(),
1346 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
1347 Since ControllerHandle may have been previously started by the same driver, if a protocol is
1348 already in the opened state, then it must not be closed with CloseProtocol(). This is required
1349 to guarantee the state of ControllerHandle is not modified by this function.
1350
1351 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1352 @param[in] ControllerHandle The handle of the controller to test. This handle
1353 must support a protocol interface that supplies
1354 an I/O abstraction to the driver.
1355 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
1356 parameter is ignored by device drivers, and is optional for bus
1357 drivers. For bus drivers, if this parameter is not NULL, then
1358 the bus driver must determine if the bus controller specified
1359 by ControllerHandle and the child controller specified
1360 by RemainingDevicePath are both supported by this
1361 bus driver.
1362
1363 @retval EFI_SUCCESS The device specified by ControllerHandle and
1364 RemainingDevicePath is supported by the driver specified by This.
1365 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
1366 RemainingDevicePath is already managed by the driver
1367 specified by This.
1368 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
1369 RemainingDevicePath is already managed by a different
1370 driver or an application that requires exclusive access.
1371 Currently not implemented.
1372 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
1373 RemainingDevicePath is not supported by the driver specified by This.
1374 **/
1375 EFI_STATUS
1376 EFIAPI
1377 IScsiIp6DriverBindingSupported (
1378 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1379 IN EFI_HANDLE ControllerHandle,
1380 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1381 )
1382 {
1383 return IScsiSupported (
1384 This,
1385 ControllerHandle,
1386 RemainingDevicePath,
1387 IP_VERSION_6
1388 );
1389 }
1390
1391 /**
1392 Starts a device controller or a bus controller.
1393
1394 The Start() function is designed to be invoked from the EFI boot service ConnectController().
1395 As a result, much of the error checking on the parameters to Start() has been moved into this
1396 common boot service. It is legal to call Start() from other locations,
1397 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1398 1. ControllerHandle must be a valid EFI_HANDLE.
1399 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
1400 EFI_DEVICE_PATH_PROTOCOL.
1401 3. Prior to calling Start(), the Supported() function for the driver specified by This must
1402 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
1403
1404 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1405 @param[in] ControllerHandle The handle of the controller to start. This handle
1406 must support a protocol interface that supplies
1407 an I/O abstraction to the driver.
1408 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
1409 parameter is ignored by device drivers, and is optional for bus
1410 drivers. For a bus driver, if this parameter is NULL, then handles
1411 for all the children of Controller are created by this driver.
1412 If this parameter is not NULL and the first Device Path Node is
1413 not the End of Device Path Node, then only the handle for the
1414 child device specified by the first Device Path Node of
1415 RemainingDevicePath is created by this driver.
1416 If the first Device Path Node of RemainingDevicePath is
1417 the End of Device Path Node, no child handle is created by this
1418 driver.
1419
1420 @retval EFI_SUCCESS The device was started.
1421 @retval EFI_DEVICE_ERROR The device could not be started due to a device error. Currently not implemented.
1422 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1423 @retval Others The driver failed to start the device.
1424
1425 **/
1426 EFI_STATUS
1427 EFIAPI
1428 IScsiIp6DriverBindingStart (
1429 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1430 IN EFI_HANDLE ControllerHandle,
1431 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1432 )
1433 {
1434 EFI_STATUS Status;
1435
1436 Status = IScsiStart (This->DriverBindingHandle, ControllerHandle, IP_VERSION_6);
1437 if (Status == EFI_ALREADY_STARTED) {
1438 Status = EFI_SUCCESS;
1439 }
1440
1441 return Status;
1442 }
1443
1444 /**
1445 Stops a device controller or a bus controller.
1446
1447 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
1448 As a result, much of the error checking on the parameters to Stop() has been moved
1449 into this common boot service. It is legal to call Stop() from other locations,
1450 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1451 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
1452 same driver's Start() function.
1453 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
1454 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
1455 Start() function, and the Start() function must have called OpenProtocol() on
1456 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
1457
1458 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1459 @param[in] ControllerHandle A handle to the device being stopped. The handle must
1460 support a bus specific I/O protocol for the driver
1461 to use to stop the device.
1462 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1463 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1464 if NumberOfChildren is 0.
1465
1466 @retval EFI_SUCCESS The device was stopped.
1467 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1468
1469 **/
1470 EFI_STATUS
1471 EFIAPI
1472 IScsiIp6DriverBindingStop (
1473 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1474 IN EFI_HANDLE ControllerHandle,
1475 IN UINTN NumberOfChildren,
1476 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
1477 )
1478 {
1479 return IScsiStop (
1480 This,
1481 ControllerHandle,
1482 NumberOfChildren,
1483 ChildHandleBuffer,
1484 IP_VERSION_6
1485 );
1486 }
1487
1488 /**
1489 Unload the iSCSI driver.
1490
1491 @param[in] ImageHandle The handle of the driver image.
1492
1493 @retval EFI_SUCCESS The driver is unloaded.
1494 @retval EFI_DEVICE_ERROR An unexpected error occurred.
1495
1496 **/
1497 EFI_STATUS
1498 EFIAPI
1499 IScsiUnload (
1500 IN EFI_HANDLE ImageHandle
1501 )
1502 {
1503 EFI_STATUS Status;
1504 UINTN DeviceHandleCount;
1505 EFI_HANDLE *DeviceHandleBuffer;
1506 UINTN Index;
1507 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
1508 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
1509
1510 //
1511 // Try to disonnect the driver from the devices it's controlling.
1512 //
1513 Status = gBS->LocateHandleBuffer (
1514 AllHandles,
1515 NULL,
1516 NULL,
1517 &DeviceHandleCount,
1518 &DeviceHandleBuffer
1519 );
1520 if (EFI_ERROR (Status)) {
1521 return Status;
1522 }
1523
1524 //
1525 // Disconnect the iSCSI4 driver from the controlled device.
1526 //
1527 for (Index = 0; Index < DeviceHandleCount; Index++) {
1528 Status = IScsiTestManagedDevice (
1529 DeviceHandleBuffer[Index],
1530 gIScsiIp4DriverBinding.DriverBindingHandle,
1531 &gEfiTcp4ProtocolGuid)
1532 ;
1533 if (EFI_ERROR (Status)) {
1534 continue;
1535 }
1536 Status = gBS->DisconnectController (
1537 DeviceHandleBuffer[Index],
1538 gIScsiIp4DriverBinding.DriverBindingHandle,
1539 NULL
1540 );
1541 if (EFI_ERROR (Status)) {
1542 goto ON_EXIT;
1543 }
1544 }
1545
1546 //
1547 // Disconnect the iSCSI6 driver from the controlled device.
1548 //
1549 for (Index = 0; Index < DeviceHandleCount; Index++) {
1550 Status = IScsiTestManagedDevice (
1551 DeviceHandleBuffer[Index],
1552 gIScsiIp6DriverBinding.DriverBindingHandle,
1553 &gEfiTcp6ProtocolGuid
1554 );
1555 if (EFI_ERROR (Status)) {
1556 continue;
1557 }
1558 Status = gBS->DisconnectController (
1559 DeviceHandleBuffer[Index],
1560 gIScsiIp6DriverBinding.DriverBindingHandle,
1561 NULL
1562 );
1563 if (EFI_ERROR (Status)) {
1564 goto ON_EXIT;
1565 }
1566 }
1567
1568 //
1569 // Unload the iSCSI configuration form.
1570 //
1571 Status = IScsiConfigFormUnload (gIScsiIp4DriverBinding.DriverBindingHandle);
1572 if (EFI_ERROR (Status)) {
1573 goto ON_EXIT;
1574 }
1575
1576 //
1577 // Uninstall the protocols installed by iSCSI driver.
1578 //
1579 Status = gBS->UninstallMultipleProtocolInterfaces (
1580 ImageHandle,
1581 &gEfiAuthenticationInfoProtocolGuid,
1582 &gIScsiAuthenticationInfo,
1583 NULL
1584 );
1585 if (EFI_ERROR (Status)) {
1586 goto ON_EXIT;
1587 }
1588
1589 if (gIScsiControllerNameTable!= NULL) {
1590 Status = FreeUnicodeStringTable (gIScsiControllerNameTable);
1591 if (EFI_ERROR (Status)) {
1592 goto ON_EXIT;
1593 }
1594 gIScsiControllerNameTable = NULL;
1595 }
1596
1597 //
1598 // Uninstall the ComponentName and ComponentName2 protocol from iSCSI4 driver binding handle
1599 // if it has been installed.
1600 //
1601 Status = gBS->HandleProtocol (
1602 gIScsiIp4DriverBinding.DriverBindingHandle,
1603 &gEfiComponentNameProtocolGuid,
1604 (VOID **) &ComponentName
1605 );
1606 if (!EFI_ERROR (Status)) {
1607 Status = gBS->UninstallMultipleProtocolInterfaces (
1608 gIScsiIp4DriverBinding.DriverBindingHandle,
1609 &gEfiComponentNameProtocolGuid,
1610 ComponentName,
1611 NULL
1612 );
1613 if (EFI_ERROR (Status)) {
1614 goto ON_EXIT;
1615 }
1616 }
1617
1618 Status = gBS->HandleProtocol (
1619 gIScsiIp4DriverBinding.DriverBindingHandle,
1620 &gEfiComponentName2ProtocolGuid,
1621 (VOID **) &ComponentName2
1622 );
1623 if (!EFI_ERROR (Status)) {
1624 gBS->UninstallMultipleProtocolInterfaces (
1625 gIScsiIp4DriverBinding.DriverBindingHandle,
1626 &gEfiComponentName2ProtocolGuid,
1627 ComponentName2,
1628 NULL
1629 );
1630 if (EFI_ERROR (Status)) {
1631 goto ON_EXIT;
1632 }
1633 }
1634
1635 //
1636 // Uninstall the ComponentName and ComponentName2 protocol from iSCSI6 driver binding handle
1637 // if it has been installed.
1638 //
1639 Status = gBS->HandleProtocol (
1640 gIScsiIp6DriverBinding.DriverBindingHandle,
1641 &gEfiComponentNameProtocolGuid,
1642 (VOID **) &ComponentName
1643 );
1644 if (!EFI_ERROR (Status)) {
1645 Status = gBS->UninstallMultipleProtocolInterfaces (
1646 gIScsiIp6DriverBinding.DriverBindingHandle,
1647 &gEfiComponentNameProtocolGuid,
1648 ComponentName,
1649 NULL
1650 );
1651 if (EFI_ERROR (Status)) {
1652 goto ON_EXIT;
1653 }
1654 }
1655
1656 Status = gBS->HandleProtocol (
1657 gIScsiIp6DriverBinding.DriverBindingHandle,
1658 &gEfiComponentName2ProtocolGuid,
1659 (VOID **) &ComponentName2
1660 );
1661 if (!EFI_ERROR (Status)) {
1662 gBS->UninstallMultipleProtocolInterfaces (
1663 gIScsiIp6DriverBinding.DriverBindingHandle,
1664 &gEfiComponentName2ProtocolGuid,
1665 ComponentName2,
1666 NULL
1667 );
1668 if (EFI_ERROR (Status)) {
1669 goto ON_EXIT;
1670 }
1671 }
1672
1673 //
1674 // Uninstall the IScsiInitiatorNameProtocol and all the driver binding protocols.
1675 //
1676 Status = gBS->UninstallMultipleProtocolInterfaces (
1677 gIScsiIp4DriverBinding.DriverBindingHandle,
1678 &gEfiDriverBindingProtocolGuid,
1679 &gIScsiIp4DriverBinding,
1680 &gEfiIScsiInitiatorNameProtocolGuid,
1681 &gIScsiInitiatorName,
1682 NULL
1683 );
1684 if (EFI_ERROR (Status)) {
1685 goto ON_EXIT;
1686 }
1687
1688 Status = gBS->UninstallMultipleProtocolInterfaces (
1689 gIScsiIp6DriverBinding.DriverBindingHandle,
1690 &gEfiDriverBindingProtocolGuid,
1691 &gIScsiIp6DriverBinding,
1692 NULL
1693 );
1694
1695 ON_EXIT:
1696
1697 if (DeviceHandleBuffer != NULL) {
1698 FreePool (DeviceHandleBuffer);
1699 }
1700
1701 return Status;
1702 }
1703
1704 /**
1705 This is the declaration of an EFI image entry point. This entry point is
1706 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
1707 both device drivers and bus drivers.
1708
1709 The entry point for iSCSI driver which initializes the global variables and
1710 installs the driver binding, component name protocol, iSCSI initiator name
1711 protocol and Authentication Info protocol on its image.
1712
1713 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
1714 @param[in] SystemTable A pointer to the EFI System Table.
1715
1716 @retval EFI_SUCCESS The operation completed successfully.
1717 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1718
1719 **/
1720 EFI_STATUS
1721 EFIAPI
1722 IScsiDriverEntryPoint (
1723 IN EFI_HANDLE ImageHandle,
1724 IN EFI_SYSTEM_TABLE *SystemTable
1725 )
1726 {
1727 EFI_STATUS Status;
1728 EFI_ISCSI_INITIATOR_NAME_PROTOCOL *IScsiInitiatorName;
1729 EFI_AUTHENTICATION_INFO_PROTOCOL *AuthenticationInfo;
1730
1731 //
1732 // There should be only one EFI_ISCSI_INITIATOR_NAME_PROTOCOL.
1733 //
1734 Status = gBS->LocateProtocol (
1735 &gEfiIScsiInitiatorNameProtocolGuid,
1736 NULL,
1737 (VOID **) &IScsiInitiatorName
1738 );
1739 if (!EFI_ERROR (Status)) {
1740 return EFI_ACCESS_DENIED;
1741 }
1742
1743 //
1744 // Initialize the EFI Driver Library.
1745 //
1746 Status = EfiLibInstallDriverBindingComponentName2 (
1747 ImageHandle,
1748 SystemTable,
1749 &gIScsiIp4DriverBinding,
1750 ImageHandle,
1751 &gIScsiComponentName,
1752 &gIScsiComponentName2
1753 );
1754 if (EFI_ERROR (Status)) {
1755 return Status;
1756 }
1757
1758 Status = EfiLibInstallDriverBindingComponentName2 (
1759 ImageHandle,
1760 SystemTable,
1761 &gIScsiIp6DriverBinding,
1762 NULL,
1763 &gIScsiComponentName,
1764 &gIScsiComponentName2
1765 );
1766 if (EFI_ERROR (Status)) {
1767 goto Error1;
1768 }
1769
1770 //
1771 // Install the iSCSI Initiator Name Protocol.
1772 //
1773 Status = gBS->InstallProtocolInterface (
1774 &ImageHandle,
1775 &gEfiIScsiInitiatorNameProtocolGuid,
1776 EFI_NATIVE_INTERFACE,
1777 &gIScsiInitiatorName
1778 );
1779 if (EFI_ERROR (Status)) {
1780 goto Error2;
1781 }
1782
1783 //
1784 // Create the private data structures.
1785 //
1786 mPrivate = AllocateZeroPool (sizeof (ISCSI_PRIVATE_DATA));
1787 if (mPrivate == NULL) {
1788 Status = EFI_OUT_OF_RESOURCES;
1789 goto Error3;
1790 }
1791
1792 InitializeListHead (&mPrivate->NicInfoList);
1793 InitializeListHead (&mPrivate->AttemptConfigs);
1794
1795 //
1796 // Initialize the configuration form of iSCSI.
1797 //
1798 Status = IScsiConfigFormInit (gIScsiIp4DriverBinding.DriverBindingHandle);
1799 if (EFI_ERROR (Status)) {
1800 goto Error4;
1801 }
1802
1803 //
1804 // Create the Maximum Attempts.
1805 //
1806 Status = IScsiCreateAttempts (PcdGet8 (PcdMaxIScsiAttemptNumber));
1807 if (EFI_ERROR (Status)) {
1808 goto Error5;
1809 }
1810
1811 //
1812 // Create Keywords for all the Attempts.
1813 //
1814 Status = IScsiCreateKeywords (PcdGet8 (PcdMaxIScsiAttemptNumber));
1815 if (EFI_ERROR (Status)) {
1816 goto Error5;
1817 }
1818
1819 //
1820 // There should be only one EFI_AUTHENTICATION_INFO_PROTOCOL. If already exists,
1821 // do not produce the protocol instance.
1822 //
1823 Status = gBS->LocateProtocol (
1824 &gEfiAuthenticationInfoProtocolGuid,
1825 NULL,
1826 (VOID **) &AuthenticationInfo
1827 );
1828 if (Status == EFI_NOT_FOUND) {
1829 Status = gBS->InstallProtocolInterface (
1830 &ImageHandle,
1831 &gEfiAuthenticationInfoProtocolGuid,
1832 EFI_NATIVE_INTERFACE,
1833 &gIScsiAuthenticationInfo
1834 );
1835 if (EFI_ERROR (Status)) {
1836 goto Error6;
1837 }
1838 }
1839
1840 return EFI_SUCCESS;
1841
1842 Error6:
1843 IScsiConfigFormUnload (gIScsiIp4DriverBinding.DriverBindingHandle);
1844
1845 Error5:
1846 IScsiCleanAttemptVariable ();
1847
1848 Error4:
1849 FreePool (mPrivate);
1850
1851 Error3:
1852 gBS->UninstallMultipleProtocolInterfaces (
1853 ImageHandle,
1854 &gEfiIScsiInitiatorNameProtocolGuid,
1855 &gIScsiInitiatorName,
1856 NULL
1857 );
1858
1859 Error2:
1860 gBS->UninstallMultipleProtocolInterfaces (
1861 gIScsiIp6DriverBinding.DriverBindingHandle,
1862 &gEfiDriverBindingProtocolGuid,
1863 &gIScsiIp6DriverBinding,
1864 &gEfiComponentName2ProtocolGuid,
1865 &gIScsiComponentName2,
1866 &gEfiComponentNameProtocolGuid,
1867 &gIScsiComponentName,
1868 NULL
1869 );
1870
1871 Error1:
1872 gBS->UninstallMultipleProtocolInterfaces (
1873 ImageHandle,
1874 &gEfiDriverBindingProtocolGuid,
1875 &gIScsiIp4DriverBinding,
1876 &gEfiComponentName2ProtocolGuid,
1877 &gIScsiComponentName2,
1878 &gEfiComponentNameProtocolGuid,
1879 &gIScsiComponentName,
1880 NULL
1881 );
1882
1883 return Status;
1884 }
1885