]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c
MdeModulePkg: ATA performance tuning.
[mirror_edk2.git] / MdeModulePkg / Bus / Scsi / ScsiDiskDxe / ScsiDisk.c
1 /** @file
2 SCSI disk driver that layers on every SCSI IO protocol in the system.
3
4 Copyright (c) 2006 - 2011, 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
16 #include "ScsiDisk.h"
17
18 EFI_DRIVER_BINDING_PROTOCOL gScsiDiskDriverBinding = {
19 ScsiDiskDriverBindingSupported,
20 ScsiDiskDriverBindingStart,
21 ScsiDiskDriverBindingStop,
22 0xa,
23 NULL,
24 NULL
25 };
26
27 EFI_DISK_INFO_PROTOCOL gScsiDiskInfoProtocolTemplate = {
28 EFI_DISK_INFO_SCSI_INTERFACE_GUID,
29 ScsiDiskInfoInquiry,
30 ScsiDiskInfoIdentify,
31 ScsiDiskInfoSenseData,
32 ScsiDiskInfoWhichIde
33 };
34
35 /**
36 The user Entry Point for module ScsiDisk.
37
38 The user code starts with this function.
39
40 @param ImageHandle The firmware allocated handle for the EFI image.
41 @param SystemTable A pointer to the EFI System Table.
42
43 @retval EFI_SUCCESS The entry point is executed successfully.
44 @retval other Some error occurs when executing this entry point.
45
46 **/
47 EFI_STATUS
48 EFIAPI
49 InitializeScsiDisk(
50 IN EFI_HANDLE ImageHandle,
51 IN EFI_SYSTEM_TABLE *SystemTable
52 )
53 {
54 EFI_STATUS Status;
55
56 //
57 // Install driver model protocol(s).
58 //
59 Status = EfiLibInstallDriverBindingComponentName2 (
60 ImageHandle,
61 SystemTable,
62 &gScsiDiskDriverBinding,
63 ImageHandle,
64 &gScsiDiskComponentName,
65 &gScsiDiskComponentName2
66 );
67 ASSERT_EFI_ERROR (Status);
68
69
70 return Status;
71 }
72
73 /**
74 Test to see if this driver supports ControllerHandle.
75
76 This service is called by the EFI boot service ConnectController(). In order
77 to make drivers as small as possible, there are a few calling restrictions for
78 this service. ConnectController() must follow these calling restrictions.
79 If any other agent wishes to call Supported() it must also follow these
80 calling restrictions.
81
82 @param This Protocol instance pointer.
83 @param ControllerHandle Handle of device to test
84 @param RemainingDevicePath Optional parameter use to pick a specific child
85 device to start.
86
87 @retval EFI_SUCCESS This driver supports this device
88 @retval EFI_ALREADY_STARTED This driver is already running on this device
89 @retval other This driver does not support this device
90
91 **/
92 EFI_STATUS
93 EFIAPI
94 ScsiDiskDriverBindingSupported (
95 IN EFI_DRIVER_BINDING_PROTOCOL *This,
96 IN EFI_HANDLE Controller,
97 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
98 )
99 {
100 EFI_STATUS Status;
101 EFI_SCSI_IO_PROTOCOL *ScsiIo;
102 UINT8 DeviceType;
103
104 Status = gBS->OpenProtocol (
105 Controller,
106 &gEfiScsiIoProtocolGuid,
107 (VOID **) &ScsiIo,
108 This->DriverBindingHandle,
109 Controller,
110 EFI_OPEN_PROTOCOL_BY_DRIVER
111 );
112 if (EFI_ERROR (Status)) {
113 return Status;
114 }
115
116 Status = ScsiIo->GetDeviceType (ScsiIo, &DeviceType);
117 if (!EFI_ERROR (Status)) {
118 if ((DeviceType == EFI_SCSI_TYPE_DISK) || (DeviceType == EFI_SCSI_TYPE_CDROM)) {
119 Status = EFI_SUCCESS;
120 } else {
121 Status = EFI_UNSUPPORTED;
122 }
123 }
124
125 gBS->CloseProtocol (
126 Controller,
127 &gEfiScsiIoProtocolGuid,
128 This->DriverBindingHandle,
129 Controller
130 );
131 return Status;
132 }
133
134
135 /**
136 Start this driver on ControllerHandle.
137
138 This service is called by the EFI boot service ConnectController(). In order
139 to make drivers as small as possible, there are a few calling restrictions for
140 this service. ConnectController() must follow these calling restrictions. If
141 any other agent wishes to call Start() it must also follow these calling
142 restrictions.
143
144 @param This Protocol instance pointer.
145 @param ControllerHandle Handle of device to bind driver to
146 @param RemainingDevicePath Optional parameter use to pick a specific child
147 device to start.
148
149 @retval EFI_SUCCESS This driver is added to ControllerHandle
150 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
151 @retval other This driver does not support this device
152
153 **/
154 EFI_STATUS
155 EFIAPI
156 ScsiDiskDriverBindingStart (
157 IN EFI_DRIVER_BINDING_PROTOCOL *This,
158 IN EFI_HANDLE Controller,
159 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
160 )
161 {
162 EFI_STATUS Status;
163 EFI_SCSI_IO_PROTOCOL *ScsiIo;
164 SCSI_DISK_DEV *ScsiDiskDevice;
165 BOOLEAN Temp;
166 UINT8 Index;
167 UINT8 MaxRetry;
168 BOOLEAN NeedRetry;
169 BOOLEAN MustReadCapacity;
170
171 MustReadCapacity = TRUE;
172
173 ScsiDiskDevice = (SCSI_DISK_DEV *) AllocateZeroPool (sizeof (SCSI_DISK_DEV));
174 if (ScsiDiskDevice == NULL) {
175 return EFI_OUT_OF_RESOURCES;
176 }
177
178 Status = gBS->OpenProtocol (
179 Controller,
180 &gEfiScsiIoProtocolGuid,
181 (VOID **) &ScsiIo,
182 This->DriverBindingHandle,
183 Controller,
184 EFI_OPEN_PROTOCOL_BY_DRIVER
185 );
186 if (EFI_ERROR (Status)) {
187 FreePool (ScsiDiskDevice);
188 return Status;
189 }
190
191 ScsiDiskDevice->Signature = SCSI_DISK_DEV_SIGNATURE;
192 ScsiDiskDevice->ScsiIo = ScsiIo;
193 ScsiDiskDevice->BlkIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION3;
194 ScsiDiskDevice->BlkIo.Media = &ScsiDiskDevice->BlkIoMedia;
195 ScsiDiskDevice->BlkIo.Reset = ScsiDiskReset;
196 ScsiDiskDevice->BlkIo.ReadBlocks = ScsiDiskReadBlocks;
197 ScsiDiskDevice->BlkIo.WriteBlocks = ScsiDiskWriteBlocks;
198 ScsiDiskDevice->BlkIo.FlushBlocks = ScsiDiskFlushBlocks;
199 ScsiDiskDevice->Handle = Controller;
200
201 ScsiIo->GetDeviceType (ScsiIo, &(ScsiDiskDevice->DeviceType));
202 switch (ScsiDiskDevice->DeviceType) {
203 case EFI_SCSI_TYPE_DISK:
204 ScsiDiskDevice->BlkIo.Media->BlockSize = 0x200;
205 MustReadCapacity = TRUE;
206 break;
207
208 case EFI_SCSI_TYPE_CDROM:
209 ScsiDiskDevice->BlkIo.Media->BlockSize = 0x800;
210 MustReadCapacity = FALSE;
211 break;
212 }
213 //
214 // The Sense Data Array's initial size is 6
215 //
216 ScsiDiskDevice->SenseDataNumber = 6;
217 ScsiDiskDevice->SenseData = (EFI_SCSI_SENSE_DATA *) AllocateZeroPool (
218 sizeof (EFI_SCSI_SENSE_DATA) * ScsiDiskDevice->SenseDataNumber
219 );
220 if (ScsiDiskDevice->SenseData == NULL) {
221 gBS->CloseProtocol (
222 Controller,
223 &gEfiScsiIoProtocolGuid,
224 This->DriverBindingHandle,
225 Controller
226 );
227 FreePool (ScsiDiskDevice);
228 return EFI_OUT_OF_RESOURCES;
229 }
230
231 //
232 // Retrieve device information
233 //
234 MaxRetry = 2;
235 for (Index = 0; Index < MaxRetry; Index++) {
236 Status = ScsiDiskInquiryDevice (ScsiDiskDevice, &NeedRetry);
237 if (!EFI_ERROR (Status)) {
238 break;
239 }
240
241 if (!NeedRetry) {
242 FreePool (ScsiDiskDevice->SenseData);
243 gBS->CloseProtocol (
244 Controller,
245 &gEfiScsiIoProtocolGuid,
246 This->DriverBindingHandle,
247 Controller
248 );
249 FreePool (ScsiDiskDevice);
250 return EFI_DEVICE_ERROR;
251 }
252 }
253 //
254 // The second parameter "TRUE" means must
255 // retrieve media capacity
256 //
257 Status = ScsiDiskDetectMedia (ScsiDiskDevice, MustReadCapacity, &Temp);
258 if (!EFI_ERROR (Status)) {
259 //
260 // Determine if Block IO should be produced on this controller handle
261 //
262 if (DetermineInstallBlockIo(Controller)) {
263 InitializeInstallDiskInfo(ScsiDiskDevice, Controller);
264 Status = gBS->InstallMultipleProtocolInterfaces (
265 &Controller,
266 &gEfiBlockIoProtocolGuid,
267 &ScsiDiskDevice->BlkIo,
268 &gEfiDiskInfoProtocolGuid,
269 &ScsiDiskDevice->DiskInfo,
270 NULL
271 );
272 if (!EFI_ERROR(Status)) {
273 ScsiDiskDevice->ControllerNameTable = NULL;
274 AddUnicodeString2 (
275 "eng",
276 gScsiDiskComponentName.SupportedLanguages,
277 &ScsiDiskDevice->ControllerNameTable,
278 L"SCSI Disk Device",
279 TRUE
280 );
281 AddUnicodeString2 (
282 "en",
283 gScsiDiskComponentName2.SupportedLanguages,
284 &ScsiDiskDevice->ControllerNameTable,
285 L"SCSI Disk Device",
286 FALSE
287 );
288 return EFI_SUCCESS;
289 }
290 }
291 }
292
293 gBS->FreePool (ScsiDiskDevice->SenseData);
294 gBS->FreePool (ScsiDiskDevice);
295 gBS->CloseProtocol (
296 Controller,
297 &gEfiScsiIoProtocolGuid,
298 This->DriverBindingHandle,
299 Controller
300 );
301 return Status;
302
303 }
304
305
306 /**
307 Stop this driver on ControllerHandle.
308
309 This service is called by the EFI boot service DisconnectController().
310 In order to make drivers as small as possible, there are a few calling
311 restrictions for this service. DisconnectController() must follow these
312 calling restrictions. If any other agent wishes to call Stop() it must
313 also follow these calling restrictions.
314
315 @param This Protocol instance pointer.
316 @param ControllerHandle Handle of device to stop driver on
317 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
318 children is zero stop the entire bus driver.
319 @param ChildHandleBuffer List of Child Handles to Stop.
320
321 @retval EFI_SUCCESS This driver is removed ControllerHandle
322 @retval other This driver was not removed from this device
323
324 **/
325 EFI_STATUS
326 EFIAPI
327 ScsiDiskDriverBindingStop (
328 IN EFI_DRIVER_BINDING_PROTOCOL *This,
329 IN EFI_HANDLE Controller,
330 IN UINTN NumberOfChildren,
331 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
332 )
333 {
334 EFI_BLOCK_IO_PROTOCOL *BlkIo;
335 SCSI_DISK_DEV *ScsiDiskDevice;
336 EFI_STATUS Status;
337
338 Status = gBS->OpenProtocol (
339 Controller,
340 &gEfiBlockIoProtocolGuid,
341 (VOID **) &BlkIo,
342 This->DriverBindingHandle,
343 Controller,
344 EFI_OPEN_PROTOCOL_GET_PROTOCOL
345 );
346 if (EFI_ERROR (Status)) {
347 return Status;
348 }
349
350 ScsiDiskDevice = SCSI_DISK_DEV_FROM_THIS (BlkIo);
351 Status = gBS->UninstallMultipleProtocolInterfaces (
352 Controller,
353 &gEfiBlockIoProtocolGuid,
354 &ScsiDiskDevice->BlkIo,
355 &gEfiDiskInfoProtocolGuid,
356 &ScsiDiskDevice->DiskInfo,
357 NULL
358 );
359 if (!EFI_ERROR (Status)) {
360 gBS->CloseProtocol (
361 Controller,
362 &gEfiScsiIoProtocolGuid,
363 This->DriverBindingHandle,
364 Controller
365 );
366
367 ReleaseScsiDiskDeviceResources (ScsiDiskDevice);
368
369 return EFI_SUCCESS;
370 }
371 //
372 // errors met
373 //
374 return Status;
375 }
376
377 /**
378 Reset SCSI Disk.
379
380
381 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
382 @param ExtendedVerification The flag about if extend verificate
383
384 @retval EFI_SUCCESS The device was reset.
385 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
386 not be reset.
387 @return EFI_STATUS is returned from EFI_SCSI_IO_PROTOCOL.ResetDevice().
388
389 **/
390 EFI_STATUS
391 EFIAPI
392 ScsiDiskReset (
393 IN EFI_BLOCK_IO_PROTOCOL *This,
394 IN BOOLEAN ExtendedVerification
395 )
396 {
397 EFI_TPL OldTpl;
398 SCSI_DISK_DEV *ScsiDiskDevice;
399 EFI_STATUS Status;
400
401 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
402
403 ScsiDiskDevice = SCSI_DISK_DEV_FROM_THIS (This);
404
405 Status = ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
406
407 if (EFI_ERROR (Status)) {
408 Status = EFI_DEVICE_ERROR;
409 goto Done;
410 }
411
412 if (!ExtendedVerification) {
413 goto Done;
414 }
415
416 Status = ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
417
418 if (EFI_ERROR (Status)) {
419 Status = EFI_DEVICE_ERROR;
420 goto Done;
421 }
422
423 Done:
424 gBS->RestoreTPL (OldTpl);
425 return Status;
426 }
427
428 /**
429 The function is to Read Block from SCSI Disk.
430
431 @param This The pointer of EFI_BLOCK_IO_PROTOCOL.
432 @param MediaId The Id of Media detected
433 @param Lba The logic block address
434 @param BufferSize The size of Buffer
435 @param Buffer The buffer to fill the read out data
436
437 @retval EFI_SUCCESS Successfully to read out block.
438 @retval EFI_DEVICE_ERROR Fail to detect media.
439 @retval EFI_NO_MEDIA Media is not present.
440 @retval EFI_MEDIA_CHANGED Media has changed.
441 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
442 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
443
444 **/
445 EFI_STATUS
446 EFIAPI
447 ScsiDiskReadBlocks (
448 IN EFI_BLOCK_IO_PROTOCOL *This,
449 IN UINT32 MediaId,
450 IN EFI_LBA Lba,
451 IN UINTN BufferSize,
452 OUT VOID *Buffer
453 )
454 {
455 SCSI_DISK_DEV *ScsiDiskDevice;
456 EFI_BLOCK_IO_MEDIA *Media;
457 EFI_STATUS Status;
458 UINTN BlockSize;
459 UINTN NumberOfBlocks;
460 BOOLEAN MediaChange;
461 EFI_TPL OldTpl;
462
463 MediaChange = FALSE;
464 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
465 ScsiDiskDevice = SCSI_DISK_DEV_FROM_THIS (This);
466
467 if (!IS_DEVICE_FIXED(ScsiDiskDevice)) {
468
469 Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
470 if (EFI_ERROR (Status)) {
471 Status = EFI_DEVICE_ERROR;
472 goto Done;
473 }
474
475 if (MediaChange) {
476 gBS->ReinstallProtocolInterface (
477 ScsiDiskDevice->Handle,
478 &gEfiBlockIoProtocolGuid,
479 &ScsiDiskDevice->BlkIo,
480 &ScsiDiskDevice->BlkIo
481 );
482 }
483 }
484 //
485 // Get the intrinsic block size
486 //
487 Media = ScsiDiskDevice->BlkIo.Media;
488 BlockSize = Media->BlockSize;
489
490 NumberOfBlocks = BufferSize / BlockSize;
491
492 if (!(Media->MediaPresent)) {
493 Status = EFI_NO_MEDIA;
494 goto Done;
495 }
496
497 if (MediaId != Media->MediaId) {
498 Status = EFI_MEDIA_CHANGED;
499 goto Done;
500 }
501
502 if (Buffer == NULL) {
503 Status = EFI_INVALID_PARAMETER;
504 goto Done;
505 }
506
507 if (BufferSize == 0) {
508 Status = EFI_SUCCESS;
509 goto Done;
510 }
511
512 if (BufferSize % BlockSize != 0) {
513 Status = EFI_BAD_BUFFER_SIZE;
514 goto Done;
515 }
516
517 if (Lba > Media->LastBlock) {
518 Status = EFI_INVALID_PARAMETER;
519 goto Done;
520 }
521
522 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
523 Status = EFI_INVALID_PARAMETER;
524 goto Done;
525 }
526
527 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
528 Status = EFI_INVALID_PARAMETER;
529 goto Done;
530 }
531
532 //
533 // If all the parameters are valid, then perform read sectors command
534 // to transfer data from device to host.
535 //
536 Status = ScsiDiskReadSectors (ScsiDiskDevice, Buffer, Lba, NumberOfBlocks);
537
538 Done:
539 gBS->RestoreTPL (OldTpl);
540 return Status;
541 }
542
543 /**
544 The function is to Write Block to SCSI Disk.
545
546 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
547 @param MediaId The Id of Media detected
548 @param Lba The logic block address
549 @param BufferSize The size of Buffer
550 @param Buffer The buffer to fill the read out data
551
552 @retval EFI_SUCCESS Successfully to read out block.
553 @retval EFI_WRITE_PROTECTED The device can not be written to.
554 @retval EFI_DEVICE_ERROR Fail to detect media.
555 @retval EFI_NO_MEDIA Media is not present.
556 @retval EFI_MEDIA_CHNAGED Media has changed.
557 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
558 @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
559
560 **/
561 EFI_STATUS
562 EFIAPI
563 ScsiDiskWriteBlocks (
564 IN EFI_BLOCK_IO_PROTOCOL *This,
565 IN UINT32 MediaId,
566 IN EFI_LBA Lba,
567 IN UINTN BufferSize,
568 IN VOID *Buffer
569 )
570 {
571 SCSI_DISK_DEV *ScsiDiskDevice;
572 EFI_BLOCK_IO_MEDIA *Media;
573 EFI_STATUS Status;
574 UINTN BlockSize;
575 UINTN NumberOfBlocks;
576 BOOLEAN MediaChange;
577 EFI_TPL OldTpl;
578
579 MediaChange = FALSE;
580 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
581 ScsiDiskDevice = SCSI_DISK_DEV_FROM_THIS (This);
582
583 if (!IS_DEVICE_FIXED(ScsiDiskDevice)) {
584
585 Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
586 if (EFI_ERROR (Status)) {
587 Status = EFI_DEVICE_ERROR;
588 goto Done;
589 }
590
591 if (MediaChange) {
592 gBS->ReinstallProtocolInterface (
593 ScsiDiskDevice->Handle,
594 &gEfiBlockIoProtocolGuid,
595 &ScsiDiskDevice->BlkIo,
596 &ScsiDiskDevice->BlkIo
597 );
598 }
599 }
600 //
601 // Get the intrinsic block size
602 //
603 Media = ScsiDiskDevice->BlkIo.Media;
604 BlockSize = Media->BlockSize;
605
606 NumberOfBlocks = BufferSize / BlockSize;
607
608 if (!(Media->MediaPresent)) {
609 Status = EFI_NO_MEDIA;
610 goto Done;
611 }
612
613 if (MediaId != Media->MediaId) {
614 Status = EFI_MEDIA_CHANGED;
615 goto Done;
616 }
617
618 if (BufferSize == 0) {
619 Status = EFI_SUCCESS;
620 goto Done;
621 }
622
623 if (Buffer == NULL) {
624 Status = EFI_INVALID_PARAMETER;
625 goto Done;
626 }
627
628 if (BufferSize % BlockSize != 0) {
629 Status = EFI_BAD_BUFFER_SIZE;
630 goto Done;
631 }
632
633 if (Lba > Media->LastBlock) {
634 Status = EFI_INVALID_PARAMETER;
635 goto Done;
636 }
637
638 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
639 Status = EFI_INVALID_PARAMETER;
640 goto Done;
641 }
642
643 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
644 Status = EFI_INVALID_PARAMETER;
645 goto Done;
646 }
647 //
648 // if all the parameters are valid, then perform read sectors command
649 // to transfer data from device to host.
650 //
651 Status = ScsiDiskWriteSectors (ScsiDiskDevice, Buffer, Lba, NumberOfBlocks);
652
653 Done:
654 gBS->RestoreTPL (OldTpl);
655 return Status;
656 }
657
658 /**
659 Flush Block to Disk.
660
661 EFI_SUCCESS is returned directly.
662
663 @param This The pointer of EFI_BLOCK_IO_PROTOCOL
664
665 @retval EFI_SUCCESS All outstanding data was written to the device
666
667 **/
668 EFI_STATUS
669 EFIAPI
670 ScsiDiskFlushBlocks (
671 IN EFI_BLOCK_IO_PROTOCOL *This
672 )
673 {
674 //
675 // return directly
676 //
677 return EFI_SUCCESS;
678 }
679
680
681 /**
682 Detect Device and read out capacity ,if error occurs, parse the sense key.
683
684 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
685 @param MustReadCapacity The flag about reading device capacity
686 @param MediaChange The pointer of flag indicates if media has changed
687
688 @retval EFI_DEVICE_ERROR Indicates that error occurs
689 @retval EFI_SUCCESS Successfully to detect media
690
691 **/
692 EFI_STATUS
693 ScsiDiskDetectMedia (
694 IN SCSI_DISK_DEV *ScsiDiskDevice,
695 IN BOOLEAN MustReadCapacity,
696 OUT BOOLEAN *MediaChange
697 )
698 {
699 EFI_STATUS Status;
700 EFI_SCSI_SENSE_DATA *SenseData;
701 UINTN NumberOfSenseKeys;
702 BOOLEAN NeedRetry;
703 BOOLEAN NeedReadCapacity;
704 UINT8 Retry;
705 UINT8 MaxRetry;
706 EFI_BLOCK_IO_MEDIA OldMedia;
707 UINTN Action;
708 EFI_EVENT TimeoutEvt;
709
710 Status = EFI_SUCCESS;
711 SenseData = NULL;
712 NumberOfSenseKeys = 0;
713 Retry = 0;
714 MaxRetry = 3;
715 Action = ACTION_NO_ACTION;
716 NeedReadCapacity = FALSE;
717 *MediaChange = FALSE;
718 TimeoutEvt = NULL;
719
720 CopyMem (&OldMedia, ScsiDiskDevice->BlkIo.Media, sizeof (OldMedia));
721
722 Status = gBS->CreateEvent (
723 EVT_TIMER,
724 TPL_CALLBACK,
725 NULL,
726 NULL,
727 &TimeoutEvt
728 );
729 if (EFI_ERROR (Status)) {
730 return Status;
731 }
732
733 Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS(120));
734 if (EFI_ERROR (Status)) {
735 goto EXIT;
736 }
737
738 //
739 // Sending Test_Unit cmd to poll device status.
740 // If the sense data shows the drive is not ready or reset before, we need poll the device status again.
741 // We limit the upper boundary to 120 seconds.
742 //
743 while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) {
744 Status = ScsiDiskTestUnitReady (
745 ScsiDiskDevice,
746 &NeedRetry,
747 &SenseData,
748 &NumberOfSenseKeys
749 );
750 if (!EFI_ERROR (Status)) {
751 Status = DetectMediaParsingSenseKeys (
752 ScsiDiskDevice,
753 SenseData,
754 NumberOfSenseKeys,
755 &Action
756 );
757 if (EFI_ERROR (Status)) {
758 goto EXIT;
759 } else if (Action == ACTION_RETRY_COMMAND_LATER) {
760 continue;
761 } else {
762 break;
763 }
764 } else {
765 Retry++;
766 if (!NeedRetry || (Retry >= MaxRetry)) {
767 goto EXIT;
768 }
769 }
770 }
771
772 if (EFI_ERROR (Status)) {
773 goto EXIT;
774 }
775
776 //
777 // ACTION_NO_ACTION: need not read capacity
778 // other action code: need read capacity
779 //
780 if (Action == ACTION_READ_CAPACITY) {
781 NeedReadCapacity = TRUE;
782 }
783
784 //
785 // either NeedReadCapacity is TRUE, or MustReadCapacity is TRUE,
786 // retrieve capacity via Read Capacity command
787 //
788 if (NeedReadCapacity || MustReadCapacity) {
789 //
790 // retrieve media information
791 //
792 for (Retry = 0; Retry < MaxRetry; Retry++) {
793 Status = ScsiDiskReadCapacity (
794 ScsiDiskDevice,
795 &NeedRetry,
796 &SenseData,
797 &NumberOfSenseKeys
798 );
799 if (!EFI_ERROR (Status)) {
800 //
801 // analyze sense key to action
802 //
803 Status = DetectMediaParsingSenseKeys (
804 ScsiDiskDevice,
805 SenseData,
806 NumberOfSenseKeys,
807 &Action
808 );
809 if (EFI_ERROR (Status)) {
810 //
811 // if Status is error, it may indicate crisis error,
812 // so return without retry.
813 //
814 goto EXIT;
815 } else if (Action == ACTION_RETRY_COMMAND_LATER) {
816 Retry = 0;
817 continue;
818 } else {
819 break;
820 }
821 } else {
822 Retry++;
823 if (!NeedRetry || (Retry >= MaxRetry)) {
824 goto EXIT;
825 }
826 }
827 }
828
829 if (EFI_ERROR (Status)) {
830 goto EXIT;
831 }
832 }
833
834 if (ScsiDiskDevice->BlkIo.Media->MediaId != OldMedia.MediaId) {
835 //
836 // Media change information got from the device
837 //
838 *MediaChange = TRUE;
839 }
840
841 if (ScsiDiskDevice->BlkIo.Media->ReadOnly != OldMedia.ReadOnly) {
842 *MediaChange = TRUE;
843 ScsiDiskDevice->BlkIo.Media->MediaId += 1;
844 }
845
846 if (ScsiDiskDevice->BlkIo.Media->BlockSize != OldMedia.BlockSize) {
847 *MediaChange = TRUE;
848 ScsiDiskDevice->BlkIo.Media->MediaId += 1;
849 }
850
851 if (ScsiDiskDevice->BlkIo.Media->LastBlock != OldMedia.LastBlock) {
852 *MediaChange = TRUE;
853 ScsiDiskDevice->BlkIo.Media->MediaId += 1;
854 }
855
856 if (ScsiDiskDevice->BlkIo.Media->MediaPresent != OldMedia.MediaPresent) {
857 if (ScsiDiskDevice->BlkIo.Media->MediaPresent) {
858 //
859 // when change from no media to media present, reset the MediaId to 1.
860 //
861 ScsiDiskDevice->BlkIo.Media->MediaId = 1;
862 } else {
863 //
864 // when no media, reset the MediaId to zero.
865 //
866 ScsiDiskDevice->BlkIo.Media->MediaId = 0;
867 }
868
869 *MediaChange = TRUE;
870 }
871
872 EXIT:
873 if (TimeoutEvt != NULL) {
874 gBS->CloseEvent (TimeoutEvt);
875 }
876 return Status;
877 }
878
879
880 /**
881 Send out Inquiry command to Device.
882
883 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
884 @param NeedRetry Indicates if needs try again when error happens
885
886 @retval EFI_DEVICE_ERROR Indicates that error occurs
887 @retval EFI_SUCCESS Successfully to detect media
888
889 **/
890 EFI_STATUS
891 ScsiDiskInquiryDevice (
892 IN OUT SCSI_DISK_DEV *ScsiDiskDevice,
893 OUT BOOLEAN *NeedRetry
894 )
895 {
896 UINT32 InquiryDataLength;
897 UINT8 SenseDataLength;
898 UINT8 HostAdapterStatus;
899 UINT8 TargetStatus;
900 EFI_SCSI_SENSE_DATA *SenseDataArray;
901 UINTN NumberOfSenseKeys;
902 EFI_STATUS Status;
903 UINT8 MaxRetry;
904 UINT8 Index;
905 EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE SupportedVpdPages;
906 EFI_SCSI_BLOCK_LIMITS_VPD_PAGE BlockLimits;
907 UINTN PageLength;
908
909 InquiryDataLength = sizeof (EFI_SCSI_INQUIRY_DATA);
910 SenseDataLength = 0;
911
912 Status = ScsiInquiryCommand (
913 ScsiDiskDevice->ScsiIo,
914 EFI_TIMER_PERIOD_SECONDS (1),
915 NULL,
916 &SenseDataLength,
917 &HostAdapterStatus,
918 &TargetStatus,
919 (VOID *) &(ScsiDiskDevice->InquiryData),
920 &InquiryDataLength,
921 FALSE
922 );
923 //
924 // no need to check HostAdapterStatus and TargetStatus
925 //
926 if ((Status == EFI_SUCCESS) || (Status == EFI_WARN_BUFFER_TOO_SMALL)) {
927 ParseInquiryData (ScsiDiskDevice);
928
929 if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_DISK) {
930 //
931 // Check whether the device supports Block Limits VPD page (0xB0)
932 //
933 ZeroMem (&SupportedVpdPages, sizeof (SupportedVpdPages));
934 InquiryDataLength = sizeof (SupportedVpdPages);
935 SenseDataLength = 0;
936 Status = ScsiInquiryCommandEx (
937 ScsiDiskDevice->ScsiIo,
938 EFI_TIMER_PERIOD_SECONDS (1),
939 NULL,
940 &SenseDataLength,
941 &HostAdapterStatus,
942 &TargetStatus,
943 (VOID *) &SupportedVpdPages,
944 &InquiryDataLength,
945 TRUE,
946 EFI_SCSI_PAGE_CODE_SUPPORTED_VPD
947 );
948 if (!EFI_ERROR (Status)) {
949 PageLength = (SupportedVpdPages.PageLength2 << 8)
950 | SupportedVpdPages.PageLength1;
951 for (Index = 0; Index < PageLength; Index++) {
952 if (SupportedVpdPages.SupportedVpdPageList[Index] == EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD) {
953 break;
954 }
955 }
956
957 //
958 // Query the Block Limits VPD page
959 //
960 if (Index < PageLength) {
961 ZeroMem (&BlockLimits, sizeof (BlockLimits));
962 InquiryDataLength = sizeof (BlockLimits);
963 SenseDataLength = 0;
964 Status = ScsiInquiryCommandEx (
965 ScsiDiskDevice->ScsiIo,
966 EFI_TIMER_PERIOD_SECONDS (1),
967 NULL,
968 &SenseDataLength,
969 &HostAdapterStatus,
970 &TargetStatus,
971 (VOID *) &BlockLimits,
972 &InquiryDataLength,
973 TRUE,
974 EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD
975 );
976 if (!EFI_ERROR (Status)) {
977 ScsiDiskDevice->BlkIo.Media->OptimalTransferLengthGranularity =
978 (BlockLimits.OptimalTransferLengthGranularity2 << 8) |
979 BlockLimits.OptimalTransferLengthGranularity1;
980 }
981 }
982 }
983 }
984 }
985
986 if (!EFI_ERROR (Status)) {
987 return EFI_SUCCESS;
988
989 } else if (Status == EFI_NOT_READY) {
990 *NeedRetry = TRUE;
991 return EFI_DEVICE_ERROR;
992
993 } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) {
994 *NeedRetry = FALSE;
995 return EFI_DEVICE_ERROR;
996 }
997 //
998 // go ahead to check HostAdapterStatus and TargetStatus
999 // (EFI_TIMEOUT, EFI_DEVICE_ERROR)
1000 //
1001
1002 Status = CheckHostAdapterStatus (HostAdapterStatus);
1003 if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
1004 *NeedRetry = TRUE;
1005 return EFI_DEVICE_ERROR;
1006 } else if (Status == EFI_DEVICE_ERROR) {
1007 //
1008 // reset the scsi channel
1009 //
1010 ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
1011 *NeedRetry = FALSE;
1012 return EFI_DEVICE_ERROR;
1013 }
1014
1015 Status = CheckTargetStatus (TargetStatus);
1016 if (Status == EFI_NOT_READY) {
1017 //
1018 // reset the scsi device
1019 //
1020 ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
1021 *NeedRetry = TRUE;
1022 return EFI_DEVICE_ERROR;
1023
1024 } else if (Status == EFI_DEVICE_ERROR) {
1025 *NeedRetry = FALSE;
1026 return EFI_DEVICE_ERROR;
1027 }
1028
1029 //
1030 // if goes here, meant ScsiInquiryCommand() failed.
1031 // if ScsiDiskRequestSenseKeys() succeeds at last,
1032 // better retry ScsiInquiryCommand(). (by setting *NeedRetry = TRUE)
1033 //
1034 MaxRetry = 3;
1035 for (Index = 0; Index < MaxRetry; Index++) {
1036 Status = ScsiDiskRequestSenseKeys (
1037 ScsiDiskDevice,
1038 NeedRetry,
1039 &SenseDataArray,
1040 &NumberOfSenseKeys,
1041 TRUE
1042 );
1043 if (!EFI_ERROR (Status)) {
1044 *NeedRetry = TRUE;
1045 return EFI_DEVICE_ERROR;
1046 }
1047
1048 if (!*NeedRetry) {
1049 return EFI_DEVICE_ERROR;
1050 }
1051 }
1052 //
1053 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1054 // set *NeedRetry = FALSE to avoid the outside caller try again.
1055 //
1056 *NeedRetry = FALSE;
1057 return EFI_DEVICE_ERROR;
1058 }
1059
1060 /**
1061 To test device.
1062
1063 When Test Unit Ready command succeeds, retrieve Sense Keys via Request Sense;
1064 When Test Unit Ready command encounters any error caused by host adapter or
1065 target, return error without retrieving Sense Keys.
1066
1067 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1068 @param NeedRetry The pointer of flag indicates try again
1069 @param SenseDataArray The pointer of an array of sense data
1070 @param NumberOfSenseKeys The pointer of the number of sense data array
1071
1072 @retval EFI_DEVICE_ERROR Indicates that error occurs
1073 @retval EFI_SUCCESS Successfully to test unit
1074
1075 **/
1076 EFI_STATUS
1077 ScsiDiskTestUnitReady (
1078 IN SCSI_DISK_DEV *ScsiDiskDevice,
1079 OUT BOOLEAN *NeedRetry,
1080 OUT EFI_SCSI_SENSE_DATA **SenseDataArray,
1081 OUT UINTN *NumberOfSenseKeys
1082 )
1083 {
1084 EFI_STATUS Status;
1085 UINT8 SenseDataLength;
1086 UINT8 HostAdapterStatus;
1087 UINT8 TargetStatus;
1088 UINT8 Index;
1089 UINT8 MaxRetry;
1090
1091 SenseDataLength = 0;
1092 *NumberOfSenseKeys = 0;
1093
1094 //
1095 // Parameter 3 and 4: do not require sense data, retrieve it when needed.
1096 //
1097 Status = ScsiTestUnitReadyCommand (
1098 ScsiDiskDevice->ScsiIo,
1099 EFI_TIMER_PERIOD_SECONDS (1),
1100 NULL,
1101 &SenseDataLength,
1102 &HostAdapterStatus,
1103 &TargetStatus
1104 );
1105 //
1106 // no need to check HostAdapterStatus and TargetStatus
1107 //
1108 if (Status == EFI_NOT_READY) {
1109 *NeedRetry = TRUE;
1110 return EFI_DEVICE_ERROR;
1111
1112 } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) {
1113 *NeedRetry = FALSE;
1114 return EFI_DEVICE_ERROR;
1115 }
1116 //
1117 // go ahead to check HostAdapterStatus and TargetStatus(in case of EFI_DEVICE_ERROR)
1118 //
1119
1120 Status = CheckHostAdapterStatus (HostAdapterStatus);
1121 if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
1122 *NeedRetry = TRUE;
1123 return EFI_DEVICE_ERROR;
1124
1125 } else if (Status == EFI_DEVICE_ERROR) {
1126 //
1127 // reset the scsi channel
1128 //
1129 ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
1130 *NeedRetry = FALSE;
1131 return EFI_DEVICE_ERROR;
1132 }
1133
1134 Status = CheckTargetStatus (TargetStatus);
1135 if (Status == EFI_NOT_READY) {
1136 //
1137 // reset the scsi device
1138 //
1139 ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
1140 *NeedRetry = TRUE;
1141 return EFI_DEVICE_ERROR;
1142
1143 } else if (Status == EFI_DEVICE_ERROR) {
1144 *NeedRetry = FALSE;
1145 return EFI_DEVICE_ERROR;
1146 }
1147
1148 MaxRetry = 3;
1149 for (Index = 0; Index < MaxRetry; Index++) {
1150 Status = ScsiDiskRequestSenseKeys (
1151 ScsiDiskDevice,
1152 NeedRetry,
1153 SenseDataArray,
1154 NumberOfSenseKeys,
1155 FALSE
1156 );
1157 if (!EFI_ERROR (Status)) {
1158 return EFI_SUCCESS;
1159 }
1160
1161 if (!*NeedRetry) {
1162 return EFI_DEVICE_ERROR;
1163 }
1164 }
1165 //
1166 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1167 // set *NeedRetry = FALSE to avoid the outside caller try again.
1168 //
1169 *NeedRetry = FALSE;
1170 return EFI_DEVICE_ERROR;
1171 }
1172
1173 /**
1174 Parsing Sense Keys which got from request sense command.
1175
1176 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1177 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
1178 @param NumberOfSenseKeys The number of sense key
1179 @param Action The pointer of action which indicates what is need to do next
1180
1181 @retval EFI_DEVICE_ERROR Indicates that error occurs
1182 @retval EFI_SUCCESS Successfully to complete the parsing
1183
1184 **/
1185 EFI_STATUS
1186 DetectMediaParsingSenseKeys (
1187 OUT SCSI_DISK_DEV *ScsiDiskDevice,
1188 IN EFI_SCSI_SENSE_DATA *SenseData,
1189 IN UINTN NumberOfSenseKeys,
1190 OUT UINTN *Action
1191 )
1192 {
1193 BOOLEAN RetryLater;
1194
1195 //
1196 // Default is to read capacity, unless..
1197 //
1198 *Action = ACTION_READ_CAPACITY;
1199
1200 if (NumberOfSenseKeys == 0) {
1201 if (ScsiDiskDevice->BlkIo.Media->MediaPresent == TRUE) {
1202 *Action = ACTION_NO_ACTION;
1203 }
1204 return EFI_SUCCESS;
1205 }
1206
1207 if (!ScsiDiskHaveSenseKey (SenseData, NumberOfSenseKeys)) {
1208 //
1209 // No Sense Key returned from last submitted command
1210 //
1211 if (ScsiDiskDevice->BlkIo.Media->MediaPresent == TRUE) {
1212 *Action = ACTION_NO_ACTION;
1213 }
1214 return EFI_SUCCESS;
1215 }
1216
1217 if (ScsiDiskIsNoMedia (SenseData, NumberOfSenseKeys)) {
1218 ScsiDiskDevice->BlkIo.Media->MediaPresent = FALSE;
1219 ScsiDiskDevice->BlkIo.Media->LastBlock = 0;
1220 *Action = ACTION_NO_ACTION;
1221 return EFI_SUCCESS;
1222 }
1223
1224 if (ScsiDiskIsMediaChange (SenseData, NumberOfSenseKeys)) {
1225 ScsiDiskDevice->BlkIo.Media->MediaId++;
1226 return EFI_SUCCESS;
1227 }
1228
1229 if (ScsiDiskIsResetBefore (SenseData, NumberOfSenseKeys)) {
1230 *Action = ACTION_RETRY_COMMAND_LATER;
1231 return EFI_SUCCESS;
1232 }
1233
1234 if (ScsiDiskIsMediaError (SenseData, NumberOfSenseKeys)) {
1235 ScsiDiskDevice->BlkIo.Media->MediaPresent = FALSE;
1236 ScsiDiskDevice->BlkIo.Media->LastBlock = 0;
1237 *Action = ACTION_NO_ACTION;
1238 return EFI_DEVICE_ERROR;
1239 }
1240
1241 if (ScsiDiskIsHardwareError (SenseData, NumberOfSenseKeys)) {
1242 *Action = ACTION_NO_ACTION;
1243 return EFI_DEVICE_ERROR;
1244 }
1245
1246 if (!ScsiDiskIsDriveReady (SenseData, NumberOfSenseKeys, &RetryLater)) {
1247 if (RetryLater) {
1248 *Action = ACTION_RETRY_COMMAND_LATER;
1249 return EFI_SUCCESS;
1250 }
1251 *Action = ACTION_NO_ACTION;
1252 return EFI_DEVICE_ERROR;
1253 }
1254
1255 return EFI_SUCCESS;
1256 }
1257
1258
1259 /**
1260 Send read capacity command to device and get the device parameter.
1261
1262 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1263 @param NeedRetry The pointer of flag indicates if need a retry
1264 @param SenseDataArray The pointer of an array of sense data
1265 @param NumberOfSenseKeys The number of sense key
1266
1267 @retval EFI_DEVICE_ERROR Indicates that error occurs
1268 @retval EFI_SUCCESS Successfully to read capacity or sense data is received.
1269
1270 **/
1271 EFI_STATUS
1272 ScsiDiskReadCapacity (
1273 IN OUT SCSI_DISK_DEV *ScsiDiskDevice,
1274 OUT BOOLEAN *NeedRetry,
1275 OUT EFI_SCSI_SENSE_DATA **SenseDataArray,
1276 OUT UINTN *NumberOfSenseKeys
1277 )
1278 {
1279 UINT8 HostAdapterStatus;
1280 UINT8 TargetStatus;
1281 EFI_STATUS CommandStatus;
1282 EFI_STATUS Status;
1283 UINT8 Index;
1284 UINT8 MaxRetry;
1285 UINT8 SenseDataLength;
1286 UINT32 DataLength10;
1287 UINT32 DataLength16;
1288 EFI_SCSI_DISK_CAPACITY_DATA CapacityData10;
1289 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData16;
1290
1291
1292 SenseDataLength = 0;
1293 DataLength10 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA);
1294 DataLength16 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA16);
1295 ZeroMem (&CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA));
1296 ZeroMem (&CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16));
1297
1298 *NumberOfSenseKeys = 0;
1299 *NeedRetry = FALSE;
1300
1301 //
1302 // submit Read Capacity(10) Command. If it returns capacity of FFFFFFFFh,
1303 // 16 byte command should be used to access large hard disk >2TB
1304 //
1305 CommandStatus = ScsiReadCapacityCommand (
1306 ScsiDiskDevice->ScsiIo,
1307 EFI_TIMER_PERIOD_SECONDS(1),
1308 NULL,
1309 &SenseDataLength,
1310 &HostAdapterStatus,
1311 &TargetStatus,
1312 (VOID *) &CapacityData10,
1313 &DataLength10,
1314 FALSE
1315 );
1316
1317 ScsiDiskDevice->Cdb16Byte = FALSE;
1318 if ((!EFI_ERROR (CommandStatus)) && (CapacityData10.LastLba3 == 0xff) && (CapacityData10.LastLba2 == 0xff) &&
1319 (CapacityData10.LastLba1 == 0xff) && (CapacityData10.LastLba0 == 0xff)) {
1320 //
1321 // use Read Capacity (16), Read (16) and Write (16) next when hard disk size > 2TB
1322 //
1323 ScsiDiskDevice->Cdb16Byte = TRUE;
1324 //
1325 // submit Read Capacity(16) Command to get parameter LogicalBlocksPerPhysicalBlock
1326 // and LowestAlignedLba
1327 //
1328 CommandStatus = ScsiReadCapacity16Command (
1329 ScsiDiskDevice->ScsiIo,
1330 EFI_TIMER_PERIOD_SECONDS (1),
1331 NULL,
1332 &SenseDataLength,
1333 &HostAdapterStatus,
1334 &TargetStatus,
1335 (VOID *) &CapacityData16,
1336 &DataLength16,
1337 FALSE
1338 );
1339 }
1340
1341 //
1342 // no need to check HostAdapterStatus and TargetStatus
1343 //
1344 if (CommandStatus == EFI_SUCCESS) {
1345 GetMediaInfo (ScsiDiskDevice, &CapacityData10,&CapacityData16);
1346 return EFI_SUCCESS;
1347
1348 } else if (CommandStatus == EFI_NOT_READY) {
1349 *NeedRetry = TRUE;
1350 return EFI_DEVICE_ERROR;
1351
1352 } else if ((CommandStatus == EFI_INVALID_PARAMETER) || (CommandStatus == EFI_UNSUPPORTED)) {
1353 *NeedRetry = FALSE;
1354 return EFI_DEVICE_ERROR;
1355 }
1356 //
1357 // go ahead to check HostAdapterStatus and TargetStatus
1358 // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
1359 //
1360
1361 Status = CheckHostAdapterStatus (HostAdapterStatus);
1362 if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
1363 *NeedRetry = TRUE;
1364 return EFI_DEVICE_ERROR;
1365
1366 } else if (Status == EFI_DEVICE_ERROR) {
1367 //
1368 // reset the scsi channel
1369 //
1370 ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
1371 *NeedRetry = FALSE;
1372 return EFI_DEVICE_ERROR;
1373 }
1374
1375 Status = CheckTargetStatus (TargetStatus);
1376 if (Status == EFI_NOT_READY) {
1377 //
1378 // reset the scsi device
1379 //
1380 ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
1381 *NeedRetry = TRUE;
1382 return EFI_DEVICE_ERROR;
1383
1384 } else if (Status == EFI_DEVICE_ERROR) {
1385 *NeedRetry = FALSE;
1386 return EFI_DEVICE_ERROR;
1387 }
1388
1389 //
1390 // if goes here, meant ScsiReadCapacityCommand() failed.
1391 // if ScsiDiskRequestSenseKeys() succeeds at last,
1392 // better retry ScsiReadCapacityCommand(). (by setting *NeedRetry = TRUE)
1393 //
1394 MaxRetry = 3;
1395 for (Index = 0; Index < MaxRetry; Index++) {
1396
1397 Status = ScsiDiskRequestSenseKeys (
1398 ScsiDiskDevice,
1399 NeedRetry,
1400 SenseDataArray,
1401 NumberOfSenseKeys,
1402 TRUE
1403 );
1404 if (!EFI_ERROR (Status)) {
1405 return EFI_SUCCESS;
1406 }
1407
1408 if (!*NeedRetry) {
1409 return EFI_DEVICE_ERROR;
1410 }
1411 }
1412 //
1413 // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
1414 // set *NeedRetry = FALSE to avoid the outside caller try again.
1415 //
1416 *NeedRetry = FALSE;
1417 return EFI_DEVICE_ERROR;
1418 }
1419
1420 /**
1421 Check the HostAdapter status and re-interpret it in EFI_STATUS.
1422
1423 @param HostAdapterStatus Host Adapter status
1424
1425 @retval EFI_SUCCESS Host adapter is OK.
1426 @retval EFI_TIMEOUT Timeout.
1427 @retval EFI_NOT_READY Adapter NOT ready.
1428 @retval EFI_DEVICE_ERROR Adapter device error.
1429
1430 **/
1431 EFI_STATUS
1432 CheckHostAdapterStatus (
1433 IN UINT8 HostAdapterStatus
1434 )
1435 {
1436 switch (HostAdapterStatus) {
1437 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK:
1438 return EFI_SUCCESS;
1439
1440 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_SELECTION_TIMEOUT:
1441 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT:
1442 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND:
1443 return EFI_TIMEOUT;
1444
1445 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_MESSAGE_REJECT:
1446 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PARITY_ERROR:
1447 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_REQUEST_SENSE_FAILED:
1448 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN:
1449 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_RESET:
1450 return EFI_NOT_READY;
1451
1452 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_FREE:
1453 case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR:
1454 return EFI_DEVICE_ERROR;
1455
1456 default:
1457 return EFI_SUCCESS;
1458 }
1459 }
1460
1461
1462 /**
1463 Check the target status and re-interpret it in EFI_STATUS.
1464
1465 @param TargetStatus Target status
1466
1467 @retval EFI_NOT_READY Device is NOT ready.
1468 @retval EFI_DEVICE_ERROR
1469 @retval EFI_SUCCESS
1470
1471 **/
1472 EFI_STATUS
1473 CheckTargetStatus (
1474 IN UINT8 TargetStatus
1475 )
1476 {
1477 switch (TargetStatus) {
1478 case EFI_EXT_SCSI_STATUS_TARGET_GOOD:
1479 case EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION:
1480 case EFI_EXT_SCSI_STATUS_TARGET_CONDITION_MET:
1481 return EFI_SUCCESS;
1482
1483 case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE:
1484 case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE_CONDITION_MET:
1485 case EFI_EXT_SCSI_STATUS_TARGET_BUSY:
1486 case EFI_EXT_SCSI_STATUS_TARGET_TASK_SET_FULL:
1487 return EFI_NOT_READY;
1488
1489 case EFI_EXT_SCSI_STATUS_TARGET_RESERVATION_CONFLICT:
1490 return EFI_DEVICE_ERROR;
1491 break;
1492
1493 default:
1494 return EFI_SUCCESS;
1495 }
1496 }
1497
1498
1499 /**
1500 Retrieve all sense keys from the device.
1501
1502 When encountering error during the process, if retrieve sense keys before
1503 error encountered, it returns the sense keys with return status set to EFI_SUCCESS,
1504 and NeedRetry set to FALSE; otherwize, return the proper return status.
1505
1506 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1507 @param NeedRetry The pointer of flag indicates if need a retry
1508 @param SenseDataArray The pointer of an array of sense data
1509 @param NumberOfSenseKeys The number of sense key
1510 @param AskResetIfError The flag indicates if need reset when error occurs
1511
1512 @retval EFI_DEVICE_ERROR Indicates that error occurs
1513 @retval EFI_SUCCESS Successfully to request sense key
1514
1515 **/
1516 EFI_STATUS
1517 ScsiDiskRequestSenseKeys (
1518 IN OUT SCSI_DISK_DEV *ScsiDiskDevice,
1519 OUT BOOLEAN *NeedRetry,
1520 OUT EFI_SCSI_SENSE_DATA **SenseDataArray,
1521 OUT UINTN *NumberOfSenseKeys,
1522 IN BOOLEAN AskResetIfError
1523 )
1524 {
1525 EFI_SCSI_SENSE_DATA *PtrSenseData;
1526 UINT8 SenseDataLength;
1527 BOOLEAN SenseReq;
1528 EFI_STATUS Status;
1529 EFI_STATUS FallStatus;
1530 UINT8 HostAdapterStatus;
1531 UINT8 TargetStatus;
1532
1533 FallStatus = EFI_SUCCESS;
1534 SenseDataLength = (UINT8) sizeof (EFI_SCSI_SENSE_DATA);
1535
1536 ZeroMem (
1537 ScsiDiskDevice->SenseData,
1538 sizeof (EFI_SCSI_SENSE_DATA) * (ScsiDiskDevice->SenseDataNumber)
1539 );
1540
1541 *NumberOfSenseKeys = 0;
1542 *SenseDataArray = ScsiDiskDevice->SenseData;
1543 PtrSenseData = ScsiDiskDevice->SenseData;
1544
1545 for (SenseReq = TRUE; SenseReq;) {
1546 Status = ScsiRequestSenseCommand (
1547 ScsiDiskDevice->ScsiIo,
1548 EFI_TIMER_PERIOD_SECONDS (2),
1549 PtrSenseData,
1550 &SenseDataLength,
1551 &HostAdapterStatus,
1552 &TargetStatus
1553 );
1554 if ((Status == EFI_SUCCESS) || (Status == EFI_WARN_BUFFER_TOO_SMALL)) {
1555 FallStatus = EFI_SUCCESS;
1556
1557 } else if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
1558 *NeedRetry = TRUE;
1559 FallStatus = EFI_DEVICE_ERROR;
1560
1561 } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) {
1562 *NeedRetry = FALSE;
1563 FallStatus = EFI_DEVICE_ERROR;
1564
1565 } else if (Status == EFI_DEVICE_ERROR) {
1566 if (AskResetIfError) {
1567 ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
1568 }
1569
1570 FallStatus = EFI_DEVICE_ERROR;
1571 }
1572
1573 if (EFI_ERROR (FallStatus)) {
1574 if (*NumberOfSenseKeys != 0) {
1575 *NeedRetry = FALSE;
1576 return EFI_SUCCESS;
1577 } else {
1578 return EFI_DEVICE_ERROR;
1579 }
1580 }
1581
1582 (*NumberOfSenseKeys) += 1;
1583
1584 //
1585 // no more sense key or number of sense keys exceeds predefined,
1586 // skip the loop.
1587 //
1588 if ((PtrSenseData->Sense_Key == EFI_SCSI_SK_NO_SENSE) ||
1589 (*NumberOfSenseKeys == ScsiDiskDevice->SenseDataNumber)) {
1590 SenseReq = FALSE;
1591 }
1592 PtrSenseData += 1;
1593 }
1594 return EFI_SUCCESS;
1595 }
1596
1597
1598 /**
1599 Get information from media read capacity command.
1600
1601 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1602 @param Capacity10 The pointer of EFI_SCSI_DISK_CAPACITY_DATA
1603 @param Capacity16 The pointer of EFI_SCSI_DISK_CAPACITY_DATA16
1604
1605 **/
1606 VOID
1607 GetMediaInfo (
1608 IN OUT SCSI_DISK_DEV *ScsiDiskDevice,
1609 IN EFI_SCSI_DISK_CAPACITY_DATA *Capacity10,
1610 IN EFI_SCSI_DISK_CAPACITY_DATA16 *Capacity16
1611 )
1612 {
1613 UINT8 *Ptr;
1614
1615 if (!ScsiDiskDevice->Cdb16Byte) {
1616 ScsiDiskDevice->BlkIo.Media->LastBlock = (Capacity10->LastLba3 << 24) |
1617 (Capacity10->LastLba2 << 16) |
1618 (Capacity10->LastLba1 << 8) |
1619 Capacity10->LastLba0;
1620
1621 ScsiDiskDevice->BlkIo.Media->BlockSize = (Capacity10->BlockSize3 << 24) |
1622 (Capacity10->BlockSize2 << 16) |
1623 (Capacity10->BlockSize1 << 8) |
1624 Capacity10->BlockSize0;
1625 ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = 0;
1626 ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock = 0;
1627 } else {
1628 Ptr = (UINT8*)&ScsiDiskDevice->BlkIo.Media->LastBlock;
1629 *Ptr++ = Capacity16->LastLba0;
1630 *Ptr++ = Capacity16->LastLba1;
1631 *Ptr++ = Capacity16->LastLba2;
1632 *Ptr++ = Capacity16->LastLba3;
1633 *Ptr++ = Capacity16->LastLba4;
1634 *Ptr++ = Capacity16->LastLba5;
1635 *Ptr++ = Capacity16->LastLba6;
1636 *Ptr = Capacity16->LastLba7;
1637
1638 ScsiDiskDevice->BlkIo.Media->BlockSize = (Capacity16->BlockSize3 << 24) |
1639 (Capacity16->BlockSize2 << 16) |
1640 (Capacity16->BlockSize1 << 8) |
1641 Capacity16->BlockSize0;
1642
1643 ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = (Capacity16->LowestAlignLogic2 << 8) |
1644 Capacity16->LowestAlignLogic1;
1645 ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock = (1 << Capacity16->LogicPerPhysical);
1646 }
1647
1648 ScsiDiskDevice->BlkIo.Media->MediaPresent = TRUE;
1649
1650 if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_DISK) {
1651 ScsiDiskDevice->BlkIo.Media->BlockSize = 0x200;
1652 }
1653
1654 if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_CDROM) {
1655 ScsiDiskDevice->BlkIo.Media->BlockSize = 0x800;
1656 }
1657 }
1658
1659 /**
1660 Parse Inquiry data.
1661
1662 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1663
1664 **/
1665 VOID
1666 ParseInquiryData (
1667 IN OUT SCSI_DISK_DEV *ScsiDiskDevice
1668 )
1669 {
1670 ScsiDiskDevice->FixedDevice = (BOOLEAN) ((ScsiDiskDevice->InquiryData.Rmb == 1) ? 0 : 1);
1671 ScsiDiskDevice->BlkIoMedia.RemovableMedia = (BOOLEAN) (!ScsiDiskDevice->FixedDevice);
1672 }
1673
1674 /**
1675 Read sector from SCSI Disk.
1676
1677 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1678 @param Buffer The buffer to fill in the read out data
1679 @param Lba Logic block address
1680 @param NumberOfBlocks The number of blocks to read
1681
1682 @retval EFI_DEVICE_ERROR Indicates a device error.
1683 @retval EFI_SUCCESS Operation is successful.
1684
1685 **/
1686 EFI_STATUS
1687 ScsiDiskReadSectors (
1688 IN SCSI_DISK_DEV *ScsiDiskDevice,
1689 OUT VOID *Buffer,
1690 IN EFI_LBA Lba,
1691 IN UINTN NumberOfBlocks
1692 )
1693 {
1694 UINTN BlocksRemaining;
1695 UINT8 *PtrBuffer;
1696 UINT32 BlockSize;
1697 UINT32 ByteCount;
1698 UINT32 MaxBlock;
1699 UINT32 SectorCount;
1700 UINT64 Timeout;
1701 EFI_STATUS Status;
1702 UINT8 Index;
1703 UINT8 MaxRetry;
1704 BOOLEAN NeedRetry;
1705 EFI_SCSI_SENSE_DATA *SenseData;
1706 UINTN NumberOfSenseKeys;
1707
1708 SenseData = NULL;
1709 NumberOfSenseKeys = 0;
1710
1711 Status = EFI_SUCCESS;
1712
1713 BlocksRemaining = NumberOfBlocks;
1714 BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize;
1715
1716 //
1717 // limit the data bytes that can be transferred by one Read(10) or Read(16) Command
1718 //
1719 if (!ScsiDiskDevice->Cdb16Byte) {
1720 MaxBlock = 0xFFFF;
1721 } else {
1722 MaxBlock = 0xFFFFFFFF;
1723 }
1724
1725 PtrBuffer = Buffer;
1726
1727 while (BlocksRemaining > 0) {
1728
1729 if (BlocksRemaining <= MaxBlock) {
1730 if (!ScsiDiskDevice->Cdb16Byte) {
1731 SectorCount = (UINT16) BlocksRemaining;
1732 } else {
1733 SectorCount = (UINT32) BlocksRemaining;
1734 }
1735 } else {
1736 SectorCount = MaxBlock;
1737 }
1738
1739 ByteCount = SectorCount * BlockSize;
1740 Timeout = EFI_TIMER_PERIOD_SECONDS (2);
1741
1742 MaxRetry = 2;
1743 for (Index = 0; Index < MaxRetry; Index++) {
1744 if (!ScsiDiskDevice->Cdb16Byte) {
1745 Status = ScsiDiskRead10 (
1746 ScsiDiskDevice,
1747 &NeedRetry,
1748 &SenseData,
1749 &NumberOfSenseKeys,
1750 Timeout,
1751 PtrBuffer,
1752 &ByteCount,
1753 (UINT32) Lba,
1754 SectorCount
1755 );
1756 } else {
1757 Status = ScsiDiskRead16 (
1758 ScsiDiskDevice,
1759 &NeedRetry,
1760 &SenseData,
1761 &NumberOfSenseKeys,
1762 Timeout,
1763 PtrBuffer,
1764 &ByteCount,
1765 Lba,
1766 SectorCount
1767 );
1768 }
1769 if (!EFI_ERROR (Status)) {
1770 break;
1771 }
1772
1773 if (!NeedRetry) {
1774 return EFI_DEVICE_ERROR;
1775 }
1776
1777 }
1778
1779 if ((Index == MaxRetry) && (Status != EFI_SUCCESS)) {
1780 return EFI_DEVICE_ERROR;
1781 }
1782
1783 //
1784 // actual transferred sectors
1785 //
1786 SectorCount = ByteCount / BlockSize;
1787
1788 Lba += SectorCount;
1789 PtrBuffer = PtrBuffer + SectorCount * BlockSize;
1790 BlocksRemaining -= SectorCount;
1791 }
1792
1793 return EFI_SUCCESS;
1794 }
1795
1796 /**
1797 Write sector to SCSI Disk.
1798
1799 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
1800 @param Buffer The buffer of data to be written into SCSI Disk
1801 @param Lba Logic block address
1802 @param NumberOfBlocks The number of blocks to read
1803
1804 @retval EFI_DEVICE_ERROR Indicates a device error.
1805 @retval EFI_SUCCESS Operation is successful.
1806
1807 **/
1808 EFI_STATUS
1809 ScsiDiskWriteSectors (
1810 IN SCSI_DISK_DEV *ScsiDiskDevice,
1811 IN VOID *Buffer,
1812 IN EFI_LBA Lba,
1813 IN UINTN NumberOfBlocks
1814 )
1815 {
1816 UINTN BlocksRemaining;
1817 UINT8 *PtrBuffer;
1818 UINT32 BlockSize;
1819 UINT32 ByteCount;
1820 UINT32 MaxBlock;
1821 UINT32 SectorCount;
1822 UINT64 Timeout;
1823 EFI_STATUS Status;
1824 UINT8 Index;
1825 UINT8 MaxRetry;
1826 BOOLEAN NeedRetry;
1827 EFI_SCSI_SENSE_DATA *SenseData;
1828 UINTN NumberOfSenseKeys;
1829
1830 SenseData = NULL;
1831 NumberOfSenseKeys = 0;
1832
1833 Status = EFI_SUCCESS;
1834
1835 BlocksRemaining = NumberOfBlocks;
1836 BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize;
1837
1838 //
1839 // limit the data bytes that can be transferred by one Read(10) or Read(16) Command
1840 //
1841 if (!ScsiDiskDevice->Cdb16Byte) {
1842 MaxBlock = 0xFFFF;
1843 } else {
1844 MaxBlock = 0xFFFFFFFF;
1845 }
1846
1847 PtrBuffer = Buffer;
1848
1849 while (BlocksRemaining > 0) {
1850
1851 if (BlocksRemaining <= MaxBlock) {
1852 if (!ScsiDiskDevice->Cdb16Byte) {
1853 SectorCount = (UINT16) BlocksRemaining;
1854 } else {
1855 SectorCount = (UINT32) BlocksRemaining;
1856 }
1857 } else {
1858 SectorCount = MaxBlock;
1859 }
1860
1861 ByteCount = SectorCount * BlockSize;
1862 Timeout = EFI_TIMER_PERIOD_SECONDS (2);
1863 MaxRetry = 2;
1864 for (Index = 0; Index < MaxRetry; Index++) {
1865 if (!ScsiDiskDevice->Cdb16Byte) {
1866 Status = ScsiDiskWrite10 (
1867 ScsiDiskDevice,
1868 &NeedRetry,
1869 &SenseData,
1870 &NumberOfSenseKeys,
1871 Timeout,
1872 PtrBuffer,
1873 &ByteCount,
1874 (UINT32) Lba,
1875 SectorCount
1876 );
1877 } else {
1878 Status = ScsiDiskWrite16 (
1879 ScsiDiskDevice,
1880 &NeedRetry,
1881 &SenseData,
1882 &NumberOfSenseKeys,
1883 Timeout,
1884 PtrBuffer,
1885 &ByteCount,
1886 Lba,
1887 SectorCount
1888 );
1889 }
1890 if (!EFI_ERROR (Status)) {
1891 break;
1892 }
1893
1894 if (!NeedRetry) {
1895 return EFI_DEVICE_ERROR;
1896 }
1897 }
1898
1899 if ((Index == MaxRetry) && (Status != EFI_SUCCESS)) {
1900 return EFI_DEVICE_ERROR;
1901 }
1902 //
1903 // actual transferred sectors
1904 //
1905 SectorCount = ByteCount / BlockSize;
1906
1907 Lba += SectorCount;
1908 PtrBuffer = PtrBuffer + SectorCount * BlockSize;
1909 BlocksRemaining -= SectorCount;
1910 }
1911
1912 return EFI_SUCCESS;
1913 }
1914
1915
1916 /**
1917 Submit Read(10) command.
1918
1919 @param ScsiDiskDevice The pointer of ScsiDiskDevice
1920 @param NeedRetry The pointer of flag indicates if needs retry if error happens
1921 @param SenseDataArray NOT used yet in this function
1922 @param NumberOfSenseKeys The number of sense key
1923 @param Timeout The time to complete the command
1924 @param DataBuffer The buffer to fill with the read out data
1925 @param DataLength The length of buffer
1926 @param StartLba The start logic block address
1927 @param SectorSize The size of sector
1928
1929 @return EFI_STATUS is returned by calling ScsiRead10Command().
1930 **/
1931 EFI_STATUS
1932 ScsiDiskRead10 (
1933 IN SCSI_DISK_DEV *ScsiDiskDevice,
1934 OUT BOOLEAN *NeedRetry,
1935 OUT EFI_SCSI_SENSE_DATA **SenseDataArray, OPTIONAL
1936 OUT UINTN *NumberOfSenseKeys,
1937 IN UINT64 Timeout,
1938 OUT UINT8 *DataBuffer,
1939 IN OUT UINT32 *DataLength,
1940 IN UINT32 StartLba,
1941 IN UINT32 SectorSize
1942 )
1943 {
1944 UINT8 SenseDataLength;
1945 EFI_STATUS Status;
1946 UINT8 HostAdapterStatus;
1947 UINT8 TargetStatus;
1948
1949 *NeedRetry = FALSE;
1950 *NumberOfSenseKeys = 0;
1951 SenseDataLength = 0;
1952 Status = ScsiRead10Command (
1953 ScsiDiskDevice->ScsiIo,
1954 Timeout,
1955 NULL,
1956 &SenseDataLength,
1957 &HostAdapterStatus,
1958 &TargetStatus,
1959 DataBuffer,
1960 DataLength,
1961 StartLba,
1962 SectorSize
1963 );
1964 return Status;
1965 }
1966
1967
1968 /**
1969 Submit Write(10) Command.
1970
1971 @param ScsiDiskDevice The pointer of ScsiDiskDevice
1972 @param NeedRetry The pointer of flag indicates if needs retry if error happens
1973 @param SenseDataArray NOT used yet in this function
1974 @param NumberOfSenseKeys The number of sense key
1975 @param Timeout The time to complete the command
1976 @param DataBuffer The buffer to fill with the read out data
1977 @param DataLength The length of buffer
1978 @param StartLba The start logic block address
1979 @param SectorSize The size of sector
1980
1981 @return EFI_STATUS is returned by calling ScsiWrite10Command().
1982
1983 **/
1984 EFI_STATUS
1985 ScsiDiskWrite10 (
1986 IN SCSI_DISK_DEV *ScsiDiskDevice,
1987 OUT BOOLEAN *NeedRetry,
1988 OUT EFI_SCSI_SENSE_DATA **SenseDataArray, OPTIONAL
1989 OUT UINTN *NumberOfSenseKeys,
1990 IN UINT64 Timeout,
1991 IN UINT8 *DataBuffer,
1992 IN OUT UINT32 *DataLength,
1993 IN UINT32 StartLba,
1994 IN UINT32 SectorSize
1995 )
1996 {
1997 EFI_STATUS Status;
1998 UINT8 SenseDataLength;
1999 UINT8 HostAdapterStatus;
2000 UINT8 TargetStatus;
2001
2002 *NeedRetry = FALSE;
2003 *NumberOfSenseKeys = 0;
2004 SenseDataLength = 0;
2005 Status = ScsiWrite10Command (
2006 ScsiDiskDevice->ScsiIo,
2007 Timeout,
2008 NULL,
2009 &SenseDataLength,
2010 &HostAdapterStatus,
2011 &TargetStatus,
2012 DataBuffer,
2013 DataLength,
2014 StartLba,
2015 SectorSize
2016 );
2017 return Status;
2018 }
2019
2020
2021 /**
2022 Submit Read(16) command.
2023
2024 @param ScsiDiskDevice The pointer of ScsiDiskDevice
2025 @param NeedRetry The pointer of flag indicates if needs retry if error happens
2026 @param SenseDataArray NOT used yet in this function
2027 @param NumberOfSenseKeys The number of sense key
2028 @param Timeout The time to complete the command
2029 @param DataBuffer The buffer to fill with the read out data
2030 @param DataLength The length of buffer
2031 @param StartLba The start logic block address
2032 @param SectorSize The size of sector
2033
2034 @return EFI_STATUS is returned by calling ScsiRead10Command().
2035 **/
2036 EFI_STATUS
2037 ScsiDiskRead16 (
2038 IN SCSI_DISK_DEV *ScsiDiskDevice,
2039 OUT BOOLEAN *NeedRetry,
2040 OUT EFI_SCSI_SENSE_DATA **SenseDataArray, OPTIONAL
2041 OUT UINTN *NumberOfSenseKeys,
2042 IN UINT64 Timeout,
2043 OUT UINT8 *DataBuffer,
2044 IN OUT UINT32 *DataLength,
2045 IN UINT64 StartLba,
2046 IN UINT32 SectorSize
2047 )
2048 {
2049 UINT8 SenseDataLength;
2050 EFI_STATUS Status;
2051 UINT8 HostAdapterStatus;
2052 UINT8 TargetStatus;
2053
2054 *NeedRetry = FALSE;
2055 *NumberOfSenseKeys = 0;
2056 SenseDataLength = 0;
2057 Status = ScsiRead16Command (
2058 ScsiDiskDevice->ScsiIo,
2059 Timeout,
2060 NULL,
2061 &SenseDataLength,
2062 &HostAdapterStatus,
2063 &TargetStatus,
2064 DataBuffer,
2065 DataLength,
2066 StartLba,
2067 SectorSize
2068 );
2069 return Status;
2070 }
2071
2072
2073 /**
2074 Submit Write(16) Command.
2075
2076 @param ScsiDiskDevice The pointer of ScsiDiskDevice
2077 @param NeedRetry The pointer of flag indicates if needs retry if error happens
2078 @param SenseDataArray NOT used yet in this function
2079 @param NumberOfSenseKeys The number of sense key
2080 @param Timeout The time to complete the command
2081 @param DataBuffer The buffer to fill with the read out data
2082 @param DataLength The length of buffer
2083 @param StartLba The start logic block address
2084 @param SectorSize The size of sector
2085
2086 @return EFI_STATUS is returned by calling ScsiWrite10Command().
2087
2088 **/
2089 EFI_STATUS
2090 ScsiDiskWrite16 (
2091 IN SCSI_DISK_DEV *ScsiDiskDevice,
2092 OUT BOOLEAN *NeedRetry,
2093 OUT EFI_SCSI_SENSE_DATA **SenseDataArray, OPTIONAL
2094 OUT UINTN *NumberOfSenseKeys,
2095 IN UINT64 Timeout,
2096 IN UINT8 *DataBuffer,
2097 IN OUT UINT32 *DataLength,
2098 IN UINT64 StartLba,
2099 IN UINT32 SectorSize
2100 )
2101 {
2102 EFI_STATUS Status;
2103 UINT8 SenseDataLength;
2104 UINT8 HostAdapterStatus;
2105 UINT8 TargetStatus;
2106
2107 *NeedRetry = FALSE;
2108 *NumberOfSenseKeys = 0;
2109 SenseDataLength = 0;
2110 Status = ScsiWrite16Command (
2111 ScsiDiskDevice->ScsiIo,
2112 Timeout,
2113 NULL,
2114 &SenseDataLength,
2115 &HostAdapterStatus,
2116 &TargetStatus,
2117 DataBuffer,
2118 DataLength,
2119 StartLba,
2120 SectorSize
2121 );
2122 return Status;
2123 }
2124
2125
2126 /**
2127 Check sense key to find if media presents.
2128
2129 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2130 @param SenseCounts The number of sense key
2131
2132 @retval TRUE NOT any media
2133 @retval FALSE Media presents
2134 **/
2135 BOOLEAN
2136 ScsiDiskIsNoMedia (
2137 IN EFI_SCSI_SENSE_DATA *SenseData,
2138 IN UINTN SenseCounts
2139 )
2140 {
2141 EFI_SCSI_SENSE_DATA *SensePtr;
2142 UINTN Index;
2143 BOOLEAN IsNoMedia;
2144
2145 IsNoMedia = FALSE;
2146 SensePtr = SenseData;
2147
2148 for (Index = 0; Index < SenseCounts; Index++) {
2149 //
2150 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2),
2151 // Additional Sense Code is ASC_NO_MEDIA (0x3A)
2152 //
2153 if ((SensePtr->Sense_Key == EFI_SCSI_SK_NOT_READY) &&
2154 (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_NO_MEDIA)) {
2155 IsNoMedia = TRUE;
2156 }
2157 SensePtr++;
2158 }
2159
2160 return IsNoMedia;
2161 }
2162
2163
2164 /**
2165 Parse sense key.
2166
2167 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2168 @param SenseCounts The number of sense key
2169
2170 @retval TRUE Error
2171 @retval FALSE NOT error
2172
2173 **/
2174 BOOLEAN
2175 ScsiDiskIsMediaError (
2176 IN EFI_SCSI_SENSE_DATA *SenseData,
2177 IN UINTN SenseCounts
2178 )
2179 {
2180 EFI_SCSI_SENSE_DATA *SensePtr;
2181 UINTN Index;
2182 BOOLEAN IsError;
2183
2184 IsError = FALSE;
2185 SensePtr = SenseData;
2186
2187 for (Index = 0; Index < SenseCounts; Index++) {
2188
2189 switch (SensePtr->Sense_Key) {
2190
2191 case EFI_SCSI_SK_MEDIUM_ERROR:
2192 //
2193 // Sense Key is EFI_SCSI_SK_MEDIUM_ERROR (0x3)
2194 //
2195 switch (SensePtr->Addnl_Sense_Code) {
2196
2197 //
2198 // fall through
2199 //
2200 case EFI_SCSI_ASC_MEDIA_ERR1:
2201
2202 //
2203 // fall through
2204 //
2205 case EFI_SCSI_ASC_MEDIA_ERR2:
2206
2207 //
2208 // fall through
2209 //
2210 case EFI_SCSI_ASC_MEDIA_ERR3:
2211 case EFI_SCSI_ASC_MEDIA_ERR4:
2212 IsError = TRUE;
2213 break;
2214
2215 default:
2216 break;
2217 }
2218
2219 break;
2220
2221 case EFI_SCSI_SK_NOT_READY:
2222 //
2223 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
2224 //
2225 switch (SensePtr->Addnl_Sense_Code) {
2226 //
2227 // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6)
2228 //
2229 case EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN:
2230 IsError = TRUE;
2231 break;
2232
2233 default:
2234 break;
2235 }
2236 break;
2237
2238 default:
2239 break;
2240 }
2241
2242 SensePtr++;
2243 }
2244
2245 return IsError;
2246 }
2247
2248
2249 /**
2250 Check sense key to find if hardware error happens.
2251
2252 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2253 @param SenseCounts The number of sense key
2254
2255 @retval TRUE Hardware error exits.
2256 @retval FALSE NO error.
2257
2258 **/
2259 BOOLEAN
2260 ScsiDiskIsHardwareError (
2261 IN EFI_SCSI_SENSE_DATA *SenseData,
2262 IN UINTN SenseCounts
2263 )
2264 {
2265 EFI_SCSI_SENSE_DATA *SensePtr;
2266 UINTN Index;
2267 BOOLEAN IsError;
2268
2269 IsError = FALSE;
2270 SensePtr = SenseData;
2271
2272 for (Index = 0; Index < SenseCounts; Index++) {
2273
2274 //
2275 // Sense Key is EFI_SCSI_SK_HARDWARE_ERROR (0x4)
2276 //
2277 if (SensePtr->Sense_Key == EFI_SCSI_SK_HARDWARE_ERROR) {
2278 IsError = TRUE;
2279 }
2280
2281 SensePtr++;
2282 }
2283
2284 return IsError;
2285 }
2286
2287
2288 /**
2289 Check sense key to find if media has changed.
2290
2291 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2292 @param SenseCounts The number of sense key
2293
2294 @retval TRUE Media is changed.
2295 @retval FALSE Media is NOT changed.
2296 **/
2297 BOOLEAN
2298 ScsiDiskIsMediaChange (
2299 IN EFI_SCSI_SENSE_DATA *SenseData,
2300 IN UINTN SenseCounts
2301 )
2302 {
2303 EFI_SCSI_SENSE_DATA *SensePtr;
2304 UINTN Index;
2305 BOOLEAN IsMediaChanged;
2306
2307 IsMediaChanged = FALSE;
2308 SensePtr = SenseData;
2309
2310 for (Index = 0; Index < SenseCounts; Index++) {
2311 //
2312 // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6),
2313 // Additional sense code is EFI_SCSI_ASC_MEDIA_CHANGE (0x28)
2314 //
2315 if ((SensePtr->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) &&
2316 (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_MEDIA_CHANGE)) {
2317 IsMediaChanged = TRUE;
2318 }
2319
2320 SensePtr++;
2321 }
2322
2323 return IsMediaChanged;
2324 }
2325
2326 /**
2327 Check sense key to find if reset happens.
2328
2329 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2330 @param SenseCounts The number of sense key
2331
2332 @retval TRUE It is reset before.
2333 @retval FALSE It is NOT reset before.
2334
2335 **/
2336 BOOLEAN
2337 ScsiDiskIsResetBefore (
2338 IN EFI_SCSI_SENSE_DATA *SenseData,
2339 IN UINTN SenseCounts
2340 )
2341 {
2342 EFI_SCSI_SENSE_DATA *SensePtr;
2343 UINTN Index;
2344 BOOLEAN IsResetBefore;
2345
2346 IsResetBefore = FALSE;
2347 SensePtr = SenseData;
2348
2349 for (Index = 0; Index < SenseCounts; Index++) {
2350
2351 //
2352 // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6)
2353 // Additional Sense Code is EFI_SCSI_ASC_RESET (0x29)
2354 //
2355 if ((SensePtr->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) &&
2356 (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_RESET)) {
2357 IsResetBefore = TRUE;
2358 }
2359
2360 SensePtr++;
2361 }
2362
2363 return IsResetBefore;
2364 }
2365
2366 /**
2367 Check sense key to find if the drive is ready.
2368
2369 @param SenseData The pointer of EFI_SCSI_SENSE_DATA
2370 @param SenseCounts The number of sense key
2371 @param RetryLater The flag means if need a retry
2372
2373 @retval TRUE Drive is ready.
2374 @retval FALSE Drive is NOT ready.
2375
2376 **/
2377 BOOLEAN
2378 ScsiDiskIsDriveReady (
2379 IN EFI_SCSI_SENSE_DATA *SenseData,
2380 IN UINTN SenseCounts,
2381 OUT BOOLEAN *RetryLater
2382 )
2383 {
2384 EFI_SCSI_SENSE_DATA *SensePtr;
2385 UINTN Index;
2386 BOOLEAN IsReady;
2387
2388 IsReady = TRUE;
2389 *RetryLater = FALSE;
2390 SensePtr = SenseData;
2391
2392 for (Index = 0; Index < SenseCounts; Index++) {
2393
2394 switch (SensePtr->Sense_Key) {
2395
2396 case EFI_SCSI_SK_NOT_READY:
2397 //
2398 // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
2399 //
2400 switch (SensePtr->Addnl_Sense_Code) {
2401 case EFI_SCSI_ASC_NOT_READY:
2402 //
2403 // Additional Sense Code is EFI_SCSI_ASC_NOT_READY (0x4)
2404 //
2405 switch (SensePtr->Addnl_Sense_Code_Qualifier) {
2406 case EFI_SCSI_ASCQ_IN_PROGRESS:
2407 //
2408 // Additional Sense Code Qualifier is
2409 // EFI_SCSI_ASCQ_IN_PROGRESS (0x1)
2410 //
2411 IsReady = FALSE;
2412 *RetryLater = TRUE;
2413 break;
2414
2415 default:
2416 IsReady = FALSE;
2417 *RetryLater = FALSE;
2418 break;
2419 }
2420 break;
2421
2422 default:
2423 break;
2424 }
2425 break;
2426
2427 default:
2428 break;
2429 }
2430
2431 SensePtr++;
2432 }
2433
2434 return IsReady;
2435 }
2436
2437 /**
2438 Check sense key to find if it has sense key.
2439
2440 @param SenseData - The pointer of EFI_SCSI_SENSE_DATA
2441 @param SenseCounts - The number of sense key
2442
2443 @retval TRUE It has sense key.
2444 @retval FALSE It has NOT any sense key.
2445
2446 **/
2447 BOOLEAN
2448 ScsiDiskHaveSenseKey (
2449 IN EFI_SCSI_SENSE_DATA *SenseData,
2450 IN UINTN SenseCounts
2451 )
2452 {
2453 EFI_SCSI_SENSE_DATA *SensePtr;
2454 UINTN Index;
2455 BOOLEAN HaveSenseKey;
2456
2457 if (SenseCounts == 0) {
2458 HaveSenseKey = FALSE;
2459 } else {
2460 HaveSenseKey = TRUE;
2461 }
2462
2463 SensePtr = SenseData;
2464
2465 for (Index = 0; Index < SenseCounts; Index++) {
2466
2467 //
2468 // Sense Key is SK_NO_SENSE (0x0)
2469 //
2470 if ((SensePtr->Sense_Key == EFI_SCSI_SK_NO_SENSE) &&
2471 (Index == 0)) {
2472 HaveSenseKey = FALSE;
2473 }
2474
2475 SensePtr++;
2476 }
2477
2478 return HaveSenseKey;
2479 }
2480
2481 /**
2482 Release resource about disk device.
2483
2484 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
2485
2486 **/
2487 VOID
2488 ReleaseScsiDiskDeviceResources (
2489 IN SCSI_DISK_DEV *ScsiDiskDevice
2490 )
2491 {
2492 if (ScsiDiskDevice == NULL) {
2493 return ;
2494 }
2495
2496 if (ScsiDiskDevice->SenseData != NULL) {
2497 FreePool (ScsiDiskDevice->SenseData);
2498 ScsiDiskDevice->SenseData = NULL;
2499 }
2500
2501 if (ScsiDiskDevice->ControllerNameTable != NULL) {
2502 FreeUnicodeStringTable (ScsiDiskDevice->ControllerNameTable);
2503 ScsiDiskDevice->ControllerNameTable = NULL;
2504 }
2505
2506 FreePool (ScsiDiskDevice);
2507
2508 ScsiDiskDevice = NULL;
2509 }
2510
2511 /**
2512 Determine if Block Io should be produced.
2513
2514
2515 @param ChildHandle Child Handle to retrieve Parent information.
2516
2517 @retval TRUE Should produce Block Io.
2518 @retval FALSE Should not produce Block Io.
2519
2520 **/
2521 BOOLEAN
2522 DetermineInstallBlockIo (
2523 IN EFI_HANDLE ChildHandle
2524 )
2525 {
2526 EFI_SCSI_PASS_THRU_PROTOCOL *ScsiPassThru;
2527 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru;
2528
2529 //
2530 // Firstly, check if ExtScsiPassThru Protocol parent handle exists. If existence,
2531 // check its attribute, logic or physical.
2532 //
2533 ExtScsiPassThru = (EFI_EXT_SCSI_PASS_THRU_PROTOCOL *)GetParentProtocol (&gEfiExtScsiPassThruProtocolGuid, ChildHandle);
2534 if (ExtScsiPassThru != NULL) {
2535 if ((ExtScsiPassThru->Mode->Attributes & EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL) != 0) {
2536 return TRUE;
2537 }
2538 }
2539
2540 //
2541 // Secondly, check if ScsiPassThru Protocol parent handle exists. If existence,
2542 // check its attribute, logic or physical.
2543 //
2544 ScsiPassThru = (EFI_SCSI_PASS_THRU_PROTOCOL *)GetParentProtocol (&gEfiScsiPassThruProtocolGuid, ChildHandle);
2545 if (ScsiPassThru != NULL) {
2546 if ((ScsiPassThru->Mode->Attributes & EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL) != 0) {
2547 return TRUE;
2548 }
2549 }
2550
2551 return FALSE;
2552 }
2553
2554 /**
2555 Search protocol database and check to see if the protocol
2556 specified by ProtocolGuid is present on a ControllerHandle and opened by
2557 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
2558 If the ControllerHandle is found, then the protocol specified by ProtocolGuid
2559 will be opened on it.
2560
2561
2562 @param ProtocolGuid ProtocolGuid pointer.
2563 @param ChildHandle Child Handle to retrieve Parent information.
2564
2565 **/
2566 VOID *
2567 EFIAPI
2568 GetParentProtocol (
2569 IN EFI_GUID *ProtocolGuid,
2570 IN EFI_HANDLE ChildHandle
2571 )
2572 {
2573 UINTN Index;
2574 UINTN HandleCount;
2575 VOID *Interface;
2576 EFI_STATUS Status;
2577 EFI_HANDLE *HandleBuffer;
2578
2579 //
2580 // Retrieve the list of all handles from the handle database
2581 //
2582 Status = gBS->LocateHandleBuffer (
2583 ByProtocol,
2584 ProtocolGuid,
2585 NULL,
2586 &HandleCount,
2587 &HandleBuffer
2588 );
2589
2590 if (EFI_ERROR (Status)) {
2591 return NULL;
2592 }
2593
2594 //
2595 // Iterate to find who is parent handle that is opened with ProtocolGuid by ChildHandle
2596 //
2597 for (Index = 0; Index < HandleCount; Index++) {
2598 Status = EfiTestChildHandle (HandleBuffer[Index], ChildHandle, ProtocolGuid);
2599 if (!EFI_ERROR (Status)) {
2600 Status = gBS->HandleProtocol (HandleBuffer[Index], ProtocolGuid, (VOID **)&Interface);
2601 if (!EFI_ERROR (Status)) {
2602 gBS->FreePool (HandleBuffer);
2603 return Interface;
2604 }
2605 }
2606 }
2607
2608 gBS->FreePool (HandleBuffer);
2609 return NULL;
2610 }
2611
2612 /**
2613 Provides inquiry information for the controller type.
2614
2615 This function is used by the IDE bus driver to get inquiry data. Data format
2616 of Identify data is defined by the Interface GUID.
2617
2618 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2619 @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
2620 @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
2621
2622 @retval EFI_SUCCESS The command was accepted without any errors.
2623 @retval EFI_NOT_FOUND Device does not support this data class
2624 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
2625 @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
2626
2627 **/
2628 EFI_STATUS
2629 EFIAPI
2630 ScsiDiskInfoInquiry (
2631 IN EFI_DISK_INFO_PROTOCOL *This,
2632 IN OUT VOID *InquiryData,
2633 IN OUT UINT32 *InquiryDataSize
2634 )
2635 {
2636 EFI_STATUS Status;
2637 SCSI_DISK_DEV *ScsiDiskDevice;
2638
2639 ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This);
2640
2641 Status = EFI_BUFFER_TOO_SMALL;
2642 if (*InquiryDataSize >= sizeof (ScsiDiskDevice->InquiryData)) {
2643 Status = EFI_SUCCESS;
2644 CopyMem (InquiryData, &ScsiDiskDevice->InquiryData, sizeof (ScsiDiskDevice->InquiryData));
2645 }
2646 *InquiryDataSize = sizeof (ScsiDiskDevice->InquiryData);
2647 return Status;
2648 }
2649
2650
2651 /**
2652 Provides identify information for the controller type.
2653
2654 This function is used by the IDE bus driver to get identify data. Data format
2655 of Identify data is defined by the Interface GUID.
2656
2657 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
2658 instance.
2659 @param[in, out] IdentifyData Pointer to a buffer for the identify data.
2660 @param[in, out] IdentifyDataSize Pointer to the value for the identify data
2661 size.
2662
2663 @retval EFI_SUCCESS The command was accepted without any errors.
2664 @retval EFI_NOT_FOUND Device does not support this data class
2665 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
2666 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
2667
2668 **/
2669 EFI_STATUS
2670 EFIAPI
2671 ScsiDiskInfoIdentify (
2672 IN EFI_DISK_INFO_PROTOCOL *This,
2673 IN OUT VOID *IdentifyData,
2674 IN OUT UINT32 *IdentifyDataSize
2675 )
2676 {
2677 EFI_STATUS Status;
2678 SCSI_DISK_DEV *ScsiDiskDevice;
2679
2680 if (CompareGuid (&This->Interface, &gEfiDiskInfoScsiInterfaceGuid)) {
2681 //
2682 // Physical SCSI bus does not support this data class.
2683 //
2684 return EFI_NOT_FOUND;
2685 }
2686
2687 ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This);
2688
2689 Status = EFI_BUFFER_TOO_SMALL;
2690 if (*IdentifyDataSize >= sizeof (ScsiDiskDevice->IdentifyData)) {
2691 Status = EFI_SUCCESS;
2692 CopyMem (IdentifyData, &ScsiDiskDevice->IdentifyData, sizeof (ScsiDiskDevice->IdentifyData));
2693 }
2694 *IdentifyDataSize = sizeof (ScsiDiskDevice->IdentifyData);
2695 return Status;
2696 }
2697
2698 /**
2699 Provides sense data information for the controller type.
2700
2701 This function is used by the IDE bus driver to get sense data.
2702 Data format of Sense data is defined by the Interface GUID.
2703
2704 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2705 @param[in, out] SenseData Pointer to the SenseData.
2706 @param[in, out] SenseDataSize Size of SenseData in bytes.
2707 @param[out] SenseDataNumber Pointer to the value for the sense data size.
2708
2709 @retval EFI_SUCCESS The command was accepted without any errors.
2710 @retval EFI_NOT_FOUND Device does not support this data class.
2711 @retval EFI_DEVICE_ERROR Error reading SenseData from device.
2712 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
2713
2714 **/
2715 EFI_STATUS
2716 EFIAPI
2717 ScsiDiskInfoSenseData (
2718 IN EFI_DISK_INFO_PROTOCOL *This,
2719 IN OUT VOID *SenseData,
2720 IN OUT UINT32 *SenseDataSize,
2721 OUT UINT8 *SenseDataNumber
2722 )
2723 {
2724 return EFI_NOT_FOUND;
2725 }
2726
2727
2728 /**
2729 This function is used by the IDE bus driver to get controller information.
2730
2731 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
2732 @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
2733 @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
2734
2735 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
2736 @retval EFI_UNSUPPORTED This is not an IDE device.
2737
2738 **/
2739 EFI_STATUS
2740 EFIAPI
2741 ScsiDiskInfoWhichIde (
2742 IN EFI_DISK_INFO_PROTOCOL *This,
2743 OUT UINT32 *IdeChannel,
2744 OUT UINT32 *IdeDevice
2745 )
2746 {
2747 SCSI_DISK_DEV *ScsiDiskDevice;
2748
2749 if (CompareGuid (&This->Interface, &gEfiDiskInfoScsiInterfaceGuid)) {
2750 //
2751 // This is not an IDE physical device.
2752 //
2753 return EFI_UNSUPPORTED;
2754 }
2755
2756 ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This);
2757 *IdeChannel = ScsiDiskDevice->Channel;
2758 *IdeDevice = ScsiDiskDevice->Device;
2759
2760 return EFI_SUCCESS;
2761 }
2762
2763
2764 /**
2765 Issues ATA IDENTIFY DEVICE command to identify ATAPI device.
2766
2767 This function tries to fill 512-byte ATAPI_IDENTIFY_DATA for ATAPI device to
2768 implement Identify() interface for DiskInfo protocol. The ATA command is sent
2769 via SCSI Request Packet.
2770
2771 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
2772
2773 @retval EFI_SUCCESS The ATAPI device identify data were retrieved successfully.
2774 @retval others Some error occurred during the identification that ATAPI device.
2775
2776 **/
2777 EFI_STATUS
2778 AtapiIdentifyDevice (
2779 IN OUT SCSI_DISK_DEV *ScsiDiskDevice
2780 )
2781 {
2782 EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
2783 UINT8 Cdb[6];
2784
2785 //
2786 // Initialize SCSI REQUEST_PACKET and 6-byte Cdb
2787 //
2788 ZeroMem (&CommandPacket, sizeof (CommandPacket));
2789 ZeroMem (Cdb, sizeof (Cdb));
2790
2791 Cdb[0] = ATA_CMD_IDENTIFY_DEVICE;
2792 CommandPacket.Timeout = EFI_TIMER_PERIOD_SECONDS (1);
2793 CommandPacket.Cdb = Cdb;
2794 CommandPacket.CdbLength = (UINT8) sizeof (Cdb);
2795 CommandPacket.InDataBuffer = &ScsiDiskDevice->IdentifyData;
2796 CommandPacket.InTransferLength = sizeof (ScsiDiskDevice->IdentifyData);
2797
2798 return ScsiDiskDevice->ScsiIo->ExecuteScsiCommand (ScsiDiskDevice->ScsiIo, &CommandPacket, NULL);
2799 }
2800
2801
2802 /**
2803 Initialize the installation of DiskInfo protocol.
2804
2805 This function prepares for the installation of DiskInfo protocol on the child handle.
2806 By default, it installs DiskInfo protocol with SCSI interface GUID. If it further
2807 detects that the physical device is an ATAPI/AHCI device, it then updates interface GUID
2808 to be IDE/AHCI interface GUID.
2809
2810 @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
2811 @param ChildHandle Child handle to install DiskInfo protocol.
2812
2813 **/
2814 VOID
2815 InitializeInstallDiskInfo (
2816 IN SCSI_DISK_DEV *ScsiDiskDevice,
2817 IN EFI_HANDLE ChildHandle
2818 )
2819 {
2820 EFI_STATUS Status;
2821 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
2822 EFI_DEVICE_PATH_PROTOCOL *ChildDevicePathNode;
2823 ATAPI_DEVICE_PATH *AtapiDevicePath;
2824 SATA_DEVICE_PATH *SataDevicePath;
2825 UINTN IdentifyRetry;
2826
2827 Status = gBS->HandleProtocol (ChildHandle, &gEfiDevicePathProtocolGuid, (VOID **) &DevicePathNode);
2828 //
2829 // Device Path protocol must be installed on the device handle.
2830 //
2831 ASSERT_EFI_ERROR (Status);
2832 //
2833 // Copy the DiskInfo protocol template.
2834 //
2835 CopyMem (&ScsiDiskDevice->DiskInfo, &gScsiDiskInfoProtocolTemplate, sizeof (gScsiDiskInfoProtocolTemplate));
2836
2837 while (!IsDevicePathEnd (DevicePathNode)) {
2838 ChildDevicePathNode = NextDevicePathNode (DevicePathNode);
2839 if ((DevicePathType (DevicePathNode) == HARDWARE_DEVICE_PATH) &&
2840 (DevicePathSubType (DevicePathNode) == HW_PCI_DP) &&
2841 (DevicePathType (ChildDevicePathNode) == MESSAGING_DEVICE_PATH) &&
2842 ((DevicePathSubType (ChildDevicePathNode) == MSG_ATAPI_DP) ||
2843 (DevicePathSubType (ChildDevicePathNode) == MSG_SATA_DP))) {
2844
2845 IdentifyRetry = 3;
2846 do {
2847 //
2848 // Issue ATA Identify Device Command via SCSI command, which is required to publish DiskInfo protocol
2849 // with IDE/AHCI interface GUID.
2850 //
2851 Status = AtapiIdentifyDevice (ScsiDiskDevice);
2852 if (!EFI_ERROR (Status)) {
2853 if (DevicePathSubType(ChildDevicePathNode) == MSG_ATAPI_DP) {
2854 //
2855 // We find the valid ATAPI device path
2856 //
2857 AtapiDevicePath = (ATAPI_DEVICE_PATH *) ChildDevicePathNode;
2858 ScsiDiskDevice->Channel = AtapiDevicePath->PrimarySecondary;
2859 ScsiDiskDevice->Device = AtapiDevicePath->SlaveMaster;
2860 //
2861 // Update the DiskInfo.Interface to IDE interface GUID for the physical ATAPI device.
2862 //
2863 CopyGuid (&ScsiDiskDevice->DiskInfo.Interface, &gEfiDiskInfoIdeInterfaceGuid);
2864 } else {
2865 //
2866 // We find the valid SATA device path
2867 //
2868 SataDevicePath = (SATA_DEVICE_PATH *) ChildDevicePathNode;
2869 ScsiDiskDevice->Channel = SataDevicePath->HBAPortNumber;
2870 ScsiDiskDevice->Device = SataDevicePath->PortMultiplierPortNumber;
2871 //
2872 // Update the DiskInfo.Interface to AHCI interface GUID for the physical AHCI device.
2873 //
2874 CopyGuid (&ScsiDiskDevice->DiskInfo.Interface, &gEfiDiskInfoAhciInterfaceGuid);
2875 }
2876 return;
2877 }
2878 } while (--IdentifyRetry > 0);
2879 }
2880 DevicePathNode = ChildDevicePathNode;
2881 }
2882
2883 return;
2884 }