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