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