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