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