]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/IScsiDxe/IScsiDriver.c
MdeModulePkg-MemoryProfile(1): Add SMRAM_PROFILE_COMMAND_GET_PROFILE_DATA_BY_OFFSET...
[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 Status = IScsiCleanDriverData (ExistPrivate);
869 if (EFI_ERROR (Status)) {
870 goto ON_ERROR;
871 }
872 }
873 } else {
874 //
875 // Use the attempt in earlier order as boot selected in single path mode.
876 //
877 if (AttemptConfigOrder[Index] == mPrivate->BootSelectedIndex) {
878 NeedUpdate = FALSE;
879 }
880 }
881
882 }
883
884 if (NeedUpdate) {
885 mPrivate->OneSessionEstablished = TRUE;
886 mPrivate->BootSelectedIndex = BootSelected;
887 }
888
889 //
890 // Duplicate the Session's tcp connection device path. The source port field
891 // will be set to zero as one iSCSI session is comprised of several iSCSI
892 // connections.
893 //
894 Private->DevicePath = IScsiGetTcpConnDevicePath (Private->Session);
895 if (Private->DevicePath == NULL) {
896 Status = EFI_DEVICE_ERROR;
897 goto ON_ERROR;
898 }
899 //
900 // Install the updated device path onto the ExtScsiPassThruHandle.
901 //
902 Status = gBS->InstallProtocolInterface (
903 &Private->ExtScsiPassThruHandle,
904 &gEfiDevicePathProtocolGuid,
905 EFI_NATIVE_INTERFACE,
906 Private->DevicePath
907 );
908 if (EFI_ERROR (Status)) {
909 goto ON_ERROR;
910 }
911
912 //
913 // ISCSI children should share the default Tcp child, just open the default Tcp child via BY_CHILD_CONTROLLER.
914 //
915 Status = gBS->OpenProtocol (
916 Private->ChildHandle, /// Default Tcp child
917 ProtocolGuid,
918 &Interface,
919 Image,
920 Private->ExtScsiPassThruHandle,
921 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
922 );
923 if (EFI_ERROR (Status)) {
924 gBS->UninstallMultipleProtocolInterfaces (
925 Private->ExtScsiPassThruHandle,
926 &gEfiExtScsiPassThruProtocolGuid,
927 &Private->IScsiExtScsiPassThru,
928 &gEfiDevicePathProtocolGuid,
929 Private->DevicePath,
930 NULL
931 );
932
933 goto ON_ERROR;
934 }
935
936 ON_EXIT:
937
938 //
939 // Update/Publish the iSCSI Boot Firmware Table.
940 //
941 if (mPrivate->BootSelectedIndex != 0) {
942 IScsiPublishIbft ();
943 }
944
945 return EFI_SUCCESS;
946
947 ON_ERROR:
948
949 if (Private->Session != NULL) {
950 IScsiSessionAbort (Private->Session);
951 }
952
953 return Status;
954 }
955
956 /**
957 Stops a device controller or a bus controller. This is the worker function for
958 IScsiIp4(6)DriverBindingStop.
959
960 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
961 @param[in] ControllerHandle A handle to the device being stopped. The handle must
962 support a bus specific I/O protocol for the driver
963 to use to stop the device.
964 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
965 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
966 if NumberOfChildren is 0.
967 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
968
969 @retval EFI_SUCCESS The device was stopped.
970 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
971 @retval EFI_INVALID_PARAMETER Child handle is NULL.
972 @retval EFI_ACCESS_DENIED The protocol could not be removed from the Handle
973 because its interfaces are being used.
974
975 **/
976 EFI_STATUS
977 EFIAPI
978 IScsiStop (
979 IN EFI_DRIVER_BINDING_PROTOCOL *This,
980 IN EFI_HANDLE ControllerHandle,
981 IN UINTN NumberOfChildren,
982 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL,
983 IN UINT8 IpVersion
984 )
985 {
986 EFI_HANDLE IScsiController;
987 EFI_STATUS Status;
988 ISCSI_PRIVATE_PROTOCOL *IScsiIdentifier;
989 ISCSI_DRIVER_DATA *Private;
990 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru;
991 ISCSI_CONNECTION *Conn;
992 EFI_GUID *ProtocolGuid;
993 EFI_GUID *TcpServiceBindingGuid;
994 EFI_GUID *TcpProtocolGuid;
995
996
997 if (NumberOfChildren != 0) {
998 //
999 // We should have only one child.
1000 //
1001 Status = gBS->OpenProtocol (
1002 ChildHandleBuffer[0],
1003 &gEfiExtScsiPassThruProtocolGuid,
1004 (VOID **) &PassThru,
1005 This->DriverBindingHandle,
1006 ControllerHandle,
1007 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1008 );
1009 if (EFI_ERROR (Status)) {
1010 return EFI_DEVICE_ERROR;
1011 }
1012
1013 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru);
1014 Conn = NET_LIST_HEAD (&Private->Session->Conns, ISCSI_CONNECTION, Link);
1015
1016 //
1017 // Previously the TCP protocol is opened BY_CHILD_CONTROLLER. Just close
1018 // the protocol here, but do not uninstall the device path protocol and
1019 // EXT SCSI PASS THRU protocol installed on ExtScsiPassThruHandle.
1020 //
1021 if (IpVersion == IP_VERSION_4) {
1022 ProtocolGuid = &gEfiTcp4ProtocolGuid;
1023 } else {
1024 ProtocolGuid = &gEfiTcp6ProtocolGuid;
1025 }
1026
1027 gBS->CloseProtocol (
1028 Private->ChildHandle,
1029 ProtocolGuid,
1030 Private->Image,
1031 Private->ExtScsiPassThruHandle
1032 );
1033
1034 gBS->CloseProtocol (
1035 Conn->TcpIo.Handle,
1036 ProtocolGuid,
1037 Private->Image,
1038 Private->ExtScsiPassThruHandle
1039 );
1040
1041 return EFI_SUCCESS;
1042 }
1043
1044 //
1045 // Get the handle of the controller we are controling.
1046 //
1047 if (IpVersion == IP_VERSION_4) {
1048 ProtocolGuid = &gIScsiV4PrivateGuid;
1049 TcpProtocolGuid = &gEfiTcp4ProtocolGuid;
1050 TcpServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid;
1051 } else {
1052 ProtocolGuid = &gIScsiV6PrivateGuid;
1053 TcpProtocolGuid = &gEfiTcp6ProtocolGuid;
1054 TcpServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid;
1055 }
1056 IScsiController = NetLibGetNicHandle (ControllerHandle, TcpProtocolGuid);
1057 if (IScsiController == NULL) {
1058 return EFI_SUCCESS;
1059 }
1060
1061 Status = gBS->OpenProtocol (
1062 IScsiController,
1063 ProtocolGuid,
1064 (VOID **) &IScsiIdentifier,
1065 This->DriverBindingHandle,
1066 ControllerHandle,
1067 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1068 );
1069 if (EFI_ERROR (Status)) {
1070 return EFI_DEVICE_ERROR;
1071 }
1072
1073 Private = ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier);
1074 ASSERT (Private != NULL);
1075
1076 if (Private->ChildHandle != NULL) {
1077 Status = gBS->CloseProtocol (
1078 Private->ChildHandle,
1079 TcpProtocolGuid,
1080 This->DriverBindingHandle,
1081 IScsiController
1082 );
1083
1084 ASSERT (!EFI_ERROR (Status));
1085
1086 Status = NetLibDestroyServiceChild (
1087 IScsiController,
1088 This->DriverBindingHandle,
1089 TcpServiceBindingGuid,
1090 Private->ChildHandle
1091 );
1092
1093 ASSERT (!EFI_ERROR (Status));
1094 }
1095
1096 gBS->UninstallProtocolInterface (
1097 IScsiController,
1098 ProtocolGuid,
1099 &Private->IScsiIdentifier
1100 );
1101
1102 //
1103 // Remove this NIC.
1104 //
1105 IScsiRemoveNic (IScsiController);
1106
1107 //
1108 // Update the iSCSI Boot Firware Table.
1109 //
1110 IScsiPublishIbft ();
1111
1112 if (Private->Session != NULL) {
1113 IScsiSessionAbort (Private->Session);
1114 }
1115
1116 Status = IScsiCleanDriverData (Private);
1117
1118 if (EFI_ERROR (Status)) {
1119 return Status;
1120 }
1121
1122 return EFI_SUCCESS;
1123 }
1124
1125 /**
1126 Tests to see if this driver supports a given controller. If a child device is provided,
1127 it tests to see if this driver supports creating a handle for the specified child device.
1128
1129 This function checks to see if the driver specified by This supports the device specified by
1130 ControllerHandle. Drivers typically use the device path attached to
1131 ControllerHandle and/or the services from the bus I/O abstraction attached to
1132 ControllerHandle to determine if the driver supports ControllerHandle. This function
1133 may be called many times during platform initialization. In order to reduce boot times, the tests
1134 performed by this function must be very small and take as little time as possible to execute. This
1135 function must not change the state of any hardware devices, and this function must be aware that the
1136 device specified by ControllerHandle may already be managed by the same driver or a
1137 different driver. This function must match its calls to AllocatePages() with FreePages(),
1138 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
1139 Since ControllerHandle may have been previously started by the same driver, if a protocol is
1140 already in the opened state, then it must not be closed with CloseProtocol(). This is required
1141 to guarantee the state of ControllerHandle is not modified by this function.
1142
1143 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1144 @param[in] ControllerHandle The handle of the controller to test. This handle
1145 must support a protocol interface that supplies
1146 an I/O abstraction to the driver.
1147 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
1148 parameter is ignored by device drivers, and is optional for bus
1149 drivers. For bus drivers, if this parameter is not NULL, then
1150 the bus driver must determine if the bus controller specified
1151 by ControllerHandle and the child controller specified
1152 by RemainingDevicePath are both supported by this
1153 bus driver.
1154
1155 @retval EFI_SUCCESS The device specified by ControllerHandle and
1156 RemainingDevicePath is supported by the driver specified by This.
1157 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
1158 RemainingDevicePath is already managed by the driver
1159 specified by This.
1160 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
1161 RemainingDevicePath is already managed by a different
1162 driver or an application that requires exclusive access.
1163 Currently not implemented.
1164 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
1165 RemainingDevicePath is not supported by the driver specified by This.
1166 **/
1167 EFI_STATUS
1168 EFIAPI
1169 IScsiIp4DriverBindingSupported (
1170 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1171 IN EFI_HANDLE ControllerHandle,
1172 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1173 )
1174 {
1175 return IScsiSupported (
1176 This,
1177 ControllerHandle,
1178 RemainingDevicePath,
1179 IP_VERSION_4
1180 );
1181 }
1182
1183 /**
1184 Starts a device controller or a bus controller.
1185
1186 The Start() function is designed to be invoked from the EFI boot service ConnectController().
1187 As a result, much of the error checking on the parameters to Start() has been moved into this
1188 common boot service. It is legal to call Start() from other locations,
1189 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1190 1. ControllerHandle must be a valid EFI_HANDLE.
1191 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
1192 EFI_DEVICE_PATH_PROTOCOL.
1193 3. Prior to calling Start(), the Supported() function for the driver specified by This must
1194 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
1195
1196 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1197 @param[in] ControllerHandle The handle of the controller to start. 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 a bus driver, if this parameter is NULL, then handles
1203 for all the children of Controller are created by this driver.
1204 If this parameter is not NULL and the first Device Path Node is
1205 not the End of Device Path Node, then only the handle for the
1206 child device specified by the first Device Path Node of
1207 RemainingDevicePath is created by this driver.
1208 If the first Device Path Node of RemainingDevicePath is
1209 the End of Device Path Node, no child handle is created by this
1210 driver.
1211
1212 @retval EFI_SUCCESS The device was started.
1213 @retval EFI_DEVICE_ERROR The device could not be started due to a device error. Currently not implemented.
1214 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1215 @retval Others The driver failed to start the device.
1216
1217 **/
1218 EFI_STATUS
1219 EFIAPI
1220 IScsiIp4DriverBindingStart (
1221 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1222 IN EFI_HANDLE ControllerHandle,
1223 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1224 )
1225 {
1226 EFI_STATUS Status;
1227
1228 Status = IScsiStart (This->DriverBindingHandle, ControllerHandle, IP_VERSION_4);
1229 if (Status == EFI_ALREADY_STARTED) {
1230 Status = EFI_SUCCESS;
1231 }
1232
1233 return Status;
1234 }
1235
1236 /**
1237 Stops a device controller or a bus controller.
1238
1239 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
1240 As a result, much of the error checking on the parameters to Stop() has been moved
1241 into this common boot service. It is legal to call Stop() 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 that was used on a previous call to this
1244 same driver's Start() function.
1245 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
1246 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
1247 Start() function, and the Start() function must have called OpenProtocol() on
1248 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
1249
1250 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1251 @param[in] ControllerHandle A handle to the device being stopped. The handle must
1252 support a bus specific I/O protocol for the driver
1253 to use to stop the device.
1254 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1255 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1256 if NumberOfChildren is 0.
1257
1258 @retval EFI_SUCCESS The device was stopped.
1259 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1260
1261 **/
1262 EFI_STATUS
1263 EFIAPI
1264 IScsiIp4DriverBindingStop (
1265 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1266 IN EFI_HANDLE ControllerHandle,
1267 IN UINTN NumberOfChildren,
1268 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
1269 )
1270 {
1271 return IScsiStop (
1272 This,
1273 ControllerHandle,
1274 NumberOfChildren,
1275 ChildHandleBuffer,
1276 IP_VERSION_4
1277 );
1278 }
1279
1280 /**
1281 Tests to see if this driver supports a given controller. If a child device is provided,
1282 it tests to see if this driver supports creating a handle for the specified child device.
1283
1284 This function checks to see if the driver specified by This supports the device specified by
1285 ControllerHandle. Drivers typically use the device path attached to
1286 ControllerHandle and/or the services from the bus I/O abstraction attached to
1287 ControllerHandle to determine if the driver supports ControllerHandle. This function
1288 may be called many times during platform initialization. In order to reduce boot times, the tests
1289 performed by this function must be very small and take as little time as possible to execute. This
1290 function must not change the state of any hardware devices, and this function must be aware that the
1291 device specified by ControllerHandle may already be managed by the same driver or a
1292 different driver. This function must match its calls to AllocatePages() with FreePages(),
1293 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
1294 Since ControllerHandle may have been previously started by the same driver, if a protocol is
1295 already in the opened state, then it must not be closed with CloseProtocol(). This is required
1296 to guarantee the state of ControllerHandle is not modified by this function.
1297
1298 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1299 @param[in] ControllerHandle The handle of the controller to test. This handle
1300 must support a protocol interface that supplies
1301 an I/O abstraction to the driver.
1302 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
1303 parameter is ignored by device drivers, and is optional for bus
1304 drivers. For bus drivers, if this parameter is not NULL, then
1305 the bus driver must determine if the bus controller specified
1306 by ControllerHandle and the child controller specified
1307 by RemainingDevicePath are both supported by this
1308 bus driver.
1309
1310 @retval EFI_SUCCESS The device specified by ControllerHandle and
1311 RemainingDevicePath is supported by the driver specified by This.
1312 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
1313 RemainingDevicePath is already managed by the driver
1314 specified by This.
1315 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
1316 RemainingDevicePath is already managed by a different
1317 driver or an application that requires exclusive access.
1318 Currently not implemented.
1319 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
1320 RemainingDevicePath is not supported by the driver specified by This.
1321 **/
1322 EFI_STATUS
1323 EFIAPI
1324 IScsiIp6DriverBindingSupported (
1325 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1326 IN EFI_HANDLE ControllerHandle,
1327 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1328 )
1329 {
1330 return IScsiSupported (
1331 This,
1332 ControllerHandle,
1333 RemainingDevicePath,
1334 IP_VERSION_6
1335 );
1336 }
1337
1338 /**
1339 Starts a device controller or a bus controller.
1340
1341 The Start() function is designed to be invoked from the EFI boot service ConnectController().
1342 As a result, much of the error checking on the parameters to Start() has been moved into this
1343 common boot service. It is legal to call Start() from other locations,
1344 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1345 1. ControllerHandle must be a valid EFI_HANDLE.
1346 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
1347 EFI_DEVICE_PATH_PROTOCOL.
1348 3. Prior to calling Start(), the Supported() function for the driver specified by This must
1349 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
1350
1351 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1352 @param[in] ControllerHandle The handle of the controller to start. 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 a bus driver, if this parameter is NULL, then handles
1358 for all the children of Controller are created by this driver.
1359 If this parameter is not NULL and the first Device Path Node is
1360 not the End of Device Path Node, then only the handle for the
1361 child device specified by the first Device Path Node of
1362 RemainingDevicePath is created by this driver.
1363 If the first Device Path Node of RemainingDevicePath is
1364 the End of Device Path Node, no child handle is created by this
1365 driver.
1366
1367 @retval EFI_SUCCESS The device was started.
1368 @retval EFI_DEVICE_ERROR The device could not be started due to a device error. Currently not implemented.
1369 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1370 @retval Others The driver failed to start the device.
1371
1372 **/
1373 EFI_STATUS
1374 EFIAPI
1375 IScsiIp6DriverBindingStart (
1376 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1377 IN EFI_HANDLE ControllerHandle,
1378 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1379 )
1380 {
1381 EFI_STATUS Status;
1382
1383 Status = IScsiStart (This->DriverBindingHandle, ControllerHandle, IP_VERSION_6);
1384 if (Status == EFI_ALREADY_STARTED) {
1385 Status = EFI_SUCCESS;
1386 }
1387
1388 return Status;
1389 }
1390
1391 /**
1392 Stops a device controller or a bus controller.
1393
1394 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
1395 As a result, much of the error checking on the parameters to Stop() has been moved
1396 into this common boot service. It is legal to call Stop() 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 that was used on a previous call to this
1399 same driver's Start() function.
1400 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
1401 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
1402 Start() function, and the Start() function must have called OpenProtocol() on
1403 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
1404
1405 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1406 @param[in] ControllerHandle A handle to the device being stopped. The handle must
1407 support a bus specific I/O protocol for the driver
1408 to use to stop the device.
1409 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1410 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1411 if NumberOfChildren is 0.
1412
1413 @retval EFI_SUCCESS The device was stopped.
1414 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1415
1416 **/
1417 EFI_STATUS
1418 EFIAPI
1419 IScsiIp6DriverBindingStop (
1420 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1421 IN EFI_HANDLE ControllerHandle,
1422 IN UINTN NumberOfChildren,
1423 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
1424 )
1425 {
1426 return IScsiStop (
1427 This,
1428 ControllerHandle,
1429 NumberOfChildren,
1430 ChildHandleBuffer,
1431 IP_VERSION_6
1432 );
1433 }
1434
1435 /**
1436 Unload the iSCSI driver.
1437
1438 @param[in] ImageHandle The handle of the driver image.
1439
1440 @retval EFI_SUCCESS The driver is unloaded.
1441 @retval EFI_DEVICE_ERROR An unexpected error occurred.
1442
1443 **/
1444 EFI_STATUS
1445 EFIAPI
1446 IScsiUnload (
1447 IN EFI_HANDLE ImageHandle
1448 )
1449 {
1450 EFI_STATUS Status;
1451 UINTN DeviceHandleCount;
1452 EFI_HANDLE *DeviceHandleBuffer;
1453 UINTN Index;
1454 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
1455 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
1456
1457 //
1458 // Try to disonnect the driver from the devices it's controlling.
1459 //
1460 Status = gBS->LocateHandleBuffer (
1461 AllHandles,
1462 NULL,
1463 NULL,
1464 &DeviceHandleCount,
1465 &DeviceHandleBuffer
1466 );
1467 if (EFI_ERROR (Status)) {
1468 return Status;
1469 }
1470
1471 //
1472 // Disconnect the iSCSI4 driver from the controlled device.
1473 //
1474 for (Index = 0; Index < DeviceHandleCount; Index++) {
1475 Status = IScsiTestManagedDevice (
1476 DeviceHandleBuffer[Index],
1477 gIScsiIp4DriverBinding.DriverBindingHandle,
1478 &gEfiTcp4ProtocolGuid)
1479 ;
1480 if (EFI_ERROR (Status)) {
1481 continue;
1482 }
1483 Status = gBS->DisconnectController (
1484 DeviceHandleBuffer[Index],
1485 gIScsiIp4DriverBinding.DriverBindingHandle,
1486 NULL
1487 );
1488 if (EFI_ERROR (Status)) {
1489 goto ON_EXIT;
1490 }
1491 }
1492
1493 //
1494 // Disconnect the iSCSI6 driver from the controlled device.
1495 //
1496 for (Index = 0; Index < DeviceHandleCount; Index++) {
1497 Status = IScsiTestManagedDevice (
1498 DeviceHandleBuffer[Index],
1499 gIScsiIp6DriverBinding.DriverBindingHandle,
1500 &gEfiTcp6ProtocolGuid
1501 );
1502 if (EFI_ERROR (Status)) {
1503 continue;
1504 }
1505 Status = gBS->DisconnectController (
1506 DeviceHandleBuffer[Index],
1507 gIScsiIp6DriverBinding.DriverBindingHandle,
1508 NULL
1509 );
1510 if (EFI_ERROR (Status)) {
1511 goto ON_EXIT;
1512 }
1513 }
1514
1515 //
1516 // Unload the iSCSI configuration form.
1517 //
1518 Status = IScsiConfigFormUnload (gIScsiIp4DriverBinding.DriverBindingHandle);
1519 if (EFI_ERROR (Status)) {
1520 goto ON_EXIT;
1521 }
1522
1523 //
1524 // Uninstall the protocols installed by iSCSI driver.
1525 //
1526 Status = gBS->UninstallMultipleProtocolInterfaces (
1527 ImageHandle,
1528 &gEfiAuthenticationInfoProtocolGuid,
1529 &gIScsiAuthenticationInfo,
1530 NULL
1531 );
1532 if (EFI_ERROR (Status)) {
1533 goto ON_EXIT;
1534 }
1535
1536 if (gIScsiControllerNameTable!= NULL) {
1537 Status = FreeUnicodeStringTable (gIScsiControllerNameTable);
1538 if (EFI_ERROR (Status)) {
1539 goto ON_EXIT;
1540 }
1541 gIScsiControllerNameTable = NULL;
1542 }
1543
1544 //
1545 // Uninstall the ComponentName and ComponentName2 protocol from iSCSI4 driver binding handle
1546 // if it has been installed.
1547 //
1548 Status = gBS->HandleProtocol (
1549 gIScsiIp4DriverBinding.DriverBindingHandle,
1550 &gEfiComponentNameProtocolGuid,
1551 (VOID **) &ComponentName
1552 );
1553 if (!EFI_ERROR (Status)) {
1554 Status = gBS->UninstallMultipleProtocolInterfaces (
1555 gIScsiIp4DriverBinding.DriverBindingHandle,
1556 &gEfiComponentNameProtocolGuid,
1557 ComponentName,
1558 NULL
1559 );
1560 if (EFI_ERROR (Status)) {
1561 goto ON_EXIT;
1562 }
1563 }
1564
1565 Status = gBS->HandleProtocol (
1566 gIScsiIp4DriverBinding.DriverBindingHandle,
1567 &gEfiComponentName2ProtocolGuid,
1568 (VOID **) &ComponentName2
1569 );
1570 if (!EFI_ERROR (Status)) {
1571 gBS->UninstallMultipleProtocolInterfaces (
1572 gIScsiIp4DriverBinding.DriverBindingHandle,
1573 &gEfiComponentName2ProtocolGuid,
1574 ComponentName2,
1575 NULL
1576 );
1577 if (EFI_ERROR (Status)) {
1578 goto ON_EXIT;
1579 }
1580 }
1581
1582 //
1583 // Uninstall the ComponentName and ComponentName2 protocol from iSCSI6 driver binding handle
1584 // if it has been installed.
1585 //
1586 Status = gBS->HandleProtocol (
1587 gIScsiIp6DriverBinding.DriverBindingHandle,
1588 &gEfiComponentNameProtocolGuid,
1589 (VOID **) &ComponentName
1590 );
1591 if (!EFI_ERROR (Status)) {
1592 Status = gBS->UninstallMultipleProtocolInterfaces (
1593 gIScsiIp6DriverBinding.DriverBindingHandle,
1594 &gEfiComponentNameProtocolGuid,
1595 ComponentName,
1596 NULL
1597 );
1598 if (EFI_ERROR (Status)) {
1599 goto ON_EXIT;
1600 }
1601 }
1602
1603 Status = gBS->HandleProtocol (
1604 gIScsiIp6DriverBinding.DriverBindingHandle,
1605 &gEfiComponentName2ProtocolGuid,
1606 (VOID **) &ComponentName2
1607 );
1608 if (!EFI_ERROR (Status)) {
1609 gBS->UninstallMultipleProtocolInterfaces (
1610 gIScsiIp6DriverBinding.DriverBindingHandle,
1611 &gEfiComponentName2ProtocolGuid,
1612 ComponentName2,
1613 NULL
1614 );
1615 if (EFI_ERROR (Status)) {
1616 goto ON_EXIT;
1617 }
1618 }
1619
1620 //
1621 // Uninstall the IScsiInitiatorNameProtocol and all the driver binding protocols.
1622 //
1623 Status = gBS->UninstallMultipleProtocolInterfaces (
1624 gIScsiIp4DriverBinding.DriverBindingHandle,
1625 &gEfiDriverBindingProtocolGuid,
1626 &gIScsiIp4DriverBinding,
1627 &gEfiIScsiInitiatorNameProtocolGuid,
1628 &gIScsiInitiatorName,
1629 NULL
1630 );
1631 if (EFI_ERROR (Status)) {
1632 goto ON_EXIT;
1633 }
1634
1635 Status = gBS->UninstallMultipleProtocolInterfaces (
1636 gIScsiIp6DriverBinding.DriverBindingHandle,
1637 &gEfiDriverBindingProtocolGuid,
1638 &gIScsiIp6DriverBinding,
1639 NULL
1640 );
1641
1642 ON_EXIT:
1643
1644 if (DeviceHandleBuffer != NULL) {
1645 FreePool (DeviceHandleBuffer);
1646 }
1647
1648 return Status;
1649 }
1650
1651 /**
1652 This is the declaration of an EFI image entry point. This entry point is
1653 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
1654 both device drivers and bus drivers.
1655
1656 The entry point for iSCSI driver which initializes the global variables and
1657 installs the driver binding, component name protocol, iSCSI initiator name
1658 protocol and Authentication Info protocol on its image.
1659
1660 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
1661 @param[in] SystemTable A pointer to the EFI System Table.
1662
1663 @retval EFI_SUCCESS The operation completed successfully.
1664 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1665
1666 **/
1667 EFI_STATUS
1668 EFIAPI
1669 IScsiDriverEntryPoint (
1670 IN EFI_HANDLE ImageHandle,
1671 IN EFI_SYSTEM_TABLE *SystemTable
1672 )
1673 {
1674 EFI_STATUS Status;
1675 EFI_ISCSI_INITIATOR_NAME_PROTOCOL *IScsiInitiatorName;
1676 EFI_AUTHENTICATION_INFO_PROTOCOL *AuthenticationInfo;
1677
1678 //
1679 // There should be only one EFI_ISCSI_INITIATOR_NAME_PROTOCOL.
1680 //
1681 Status = gBS->LocateProtocol (
1682 &gEfiIScsiInitiatorNameProtocolGuid,
1683 NULL,
1684 (VOID **) &IScsiInitiatorName
1685 );
1686 if (!EFI_ERROR (Status)) {
1687 return EFI_ACCESS_DENIED;
1688 }
1689
1690 //
1691 // Initialize the EFI Driver Library.
1692 //
1693 Status = EfiLibInstallDriverBindingComponentName2 (
1694 ImageHandle,
1695 SystemTable,
1696 &gIScsiIp4DriverBinding,
1697 ImageHandle,
1698 &gIScsiComponentName,
1699 &gIScsiComponentName2
1700 );
1701 if (EFI_ERROR (Status)) {
1702 return Status;
1703 }
1704
1705 Status = EfiLibInstallDriverBindingComponentName2 (
1706 ImageHandle,
1707 SystemTable,
1708 &gIScsiIp6DriverBinding,
1709 NULL,
1710 &gIScsiComponentName,
1711 &gIScsiComponentName2
1712 );
1713 if (EFI_ERROR (Status)) {
1714 goto Error1;
1715 }
1716
1717 //
1718 // Install the iSCSI Initiator Name Protocol.
1719 //
1720 Status = gBS->InstallProtocolInterface (
1721 &ImageHandle,
1722 &gEfiIScsiInitiatorNameProtocolGuid,
1723 EFI_NATIVE_INTERFACE,
1724 &gIScsiInitiatorName
1725 );
1726 if (EFI_ERROR (Status)) {
1727 goto Error2;
1728 }
1729
1730 //
1731 // Create the private data structures.
1732 //
1733 mPrivate = AllocateZeroPool (sizeof (ISCSI_PRIVATE_DATA));
1734 if (mPrivate == NULL) {
1735 Status = EFI_OUT_OF_RESOURCES;
1736 goto Error3;
1737 }
1738
1739 InitializeListHead (&mPrivate->NicInfoList);
1740 InitializeListHead (&mPrivate->AttemptConfigs);
1741
1742 //
1743 // Initialize the configuration form of iSCSI.
1744 //
1745 Status = IScsiConfigFormInit (gIScsiIp4DriverBinding.DriverBindingHandle);
1746 if (EFI_ERROR (Status)) {
1747 goto Error4;
1748 }
1749
1750 //
1751 // There should be only one EFI_AUTHENTICATION_INFO_PROTOCOL. If already exists,
1752 // do not produce the protocol instance.
1753 //
1754 Status = gBS->LocateProtocol (
1755 &gEfiAuthenticationInfoProtocolGuid,
1756 NULL,
1757 (VOID **) &AuthenticationInfo
1758 );
1759 if (Status == EFI_NOT_FOUND) {
1760 Status = gBS->InstallProtocolInterface (
1761 &ImageHandle,
1762 &gEfiAuthenticationInfoProtocolGuid,
1763 EFI_NATIVE_INTERFACE,
1764 &gIScsiAuthenticationInfo
1765 );
1766 if (EFI_ERROR (Status)) {
1767 goto Error5;
1768 }
1769 }
1770
1771 return EFI_SUCCESS;
1772
1773 Error5:
1774 IScsiConfigFormUnload (gIScsiIp4DriverBinding.DriverBindingHandle);
1775
1776 Error4:
1777 FreePool (mPrivate);
1778
1779 Error3:
1780 gBS->UninstallMultipleProtocolInterfaces (
1781 ImageHandle,
1782 &gEfiIScsiInitiatorNameProtocolGuid,
1783 &gIScsiInitiatorName,
1784 NULL
1785 );
1786
1787 Error2:
1788 gBS->UninstallMultipleProtocolInterfaces (
1789 gIScsiIp6DriverBinding.DriverBindingHandle,
1790 &gEfiDriverBindingProtocolGuid,
1791 &gIScsiIp6DriverBinding,
1792 &gEfiComponentName2ProtocolGuid,
1793 &gIScsiComponentName2,
1794 &gEfiComponentNameProtocolGuid,
1795 &gIScsiComponentName,
1796 NULL
1797 );
1798
1799 Error1:
1800 gBS->UninstallMultipleProtocolInterfaces (
1801 ImageHandle,
1802 &gEfiDriverBindingProtocolGuid,
1803 &gIScsiIp4DriverBinding,
1804 &gEfiComponentName2ProtocolGuid,
1805 &gIScsiComponentName2,
1806 &gEfiComponentNameProtocolGuid,
1807 &gIScsiComponentName,
1808 NULL
1809 );
1810
1811 return Status;
1812 }
1813