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