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