]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Pci/IdeBusDxe/ata.c
fixed the following problems:
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Pci / IdeBusDxe / ata.c
1 /** @file
2 Copyright (c) 2006 - 2007 Intel Corporation. <BR>
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 @par Revision Reference:
12 2002-6: Add Atapi6 enhancement, support >120GB hard disk, including
13 update - ATAIdentity() func
14 update - AtaBlockIoReadBlocks() func
15 update - AtaBlockIoWriteBlocks() func
16 add - AtaAtapi6Identify() func
17 add - AtaReadSectorsExt() func
18 add - AtaWriteSectorsExt() func
19 add - AtaPioDataInExt() func
20 add - AtaPioDataOutExt() func
21
22 **/
23
24 #include "idebus.h"
25
26 /**
27 Sends out an ATA Identify Command to the specified device.
28
29 This function is called by DiscoverIdeDevice() during its device
30 identification. It sends out the ATA Identify Command to the
31 specified device. Only ATA device responses to this command. If
32 the command succeeds, it returns the Identify data structure which
33 contains information about the device. This function extracts the
34 information it needs to fill the IDE_BLK_IO_DEV data structure,
35 including device type, media block size, media capacity, and etc.
36
37 @param[in] *IdeDev
38 pointer pointing to IDE_BLK_IO_DEV data structure,used
39 to record all the information of the IDE device.
40
41 @retval EFI_SUCCESS Identify ATA device successfully.
42
43 @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or
44 device is not ATA device.
45
46 @note
47 parameter IdeDev will be updated in this function.
48
49 **/
50 EFI_STATUS
51 ATAIdentify (
52 IN IDE_BLK_IO_DEV *IdeDev
53 )
54 {
55 EFI_STATUS Status;
56 EFI_IDENTIFY_DATA *AtaIdentifyPointer;
57 UINT32 Capacity;
58 UINT8 DeviceSelect;
59 UINTN Retry;
60
61 //
62 // AtaIdentifyPointer is used for accommodating returned IDENTIFY data of
63 // the ATA Identify command
64 //
65 AtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA));
66
67 //
68 // use ATA PIO Data In protocol to send ATA Identify command
69 // and receive data from device
70 //
71 DeviceSelect = (UINT8) ((IdeDev->Device) << 4);
72
73
74 Retry = 3;
75 while (Retry > 0) {
76 Status = AtaPioDataIn (
77 IdeDev,
78 (VOID *) AtaIdentifyPointer,
79 sizeof (EFI_IDENTIFY_DATA),
80 ATA_CMD_IDENTIFY_DRIVE,
81 DeviceSelect,
82 0,
83 0,
84 0,
85 0
86 );
87 //
88 // If ATA Identify command succeeds, then according to the received
89 // IDENTIFY data,
90 // identify the device type ( ATA or not ).
91 // If ATA device, fill the information in IdeDev.
92 // If not ATA device, return IDE_DEVICE_ERROR
93 //
94 if (!EFI_ERROR (Status)) {
95
96 IdeDev->pIdData = AtaIdentifyPointer;
97
98 //
99 // Print ATA Module Name
100 //
101 PrintAtaModuleName (IdeDev);
102
103 //
104 // bit 15 of pAtaIdentify->config is used to identify whether device is
105 // ATA device or ATAPI device.
106 // if 0, means ATA device; if 1, means ATAPI device.
107 //
108 if ((AtaIdentifyPointer->AtaData.config & 0x8000) == 0x00) {
109 //
110 // Detect if support S.M.A.R.T. If yes, enable it as default
111 //
112 AtaSMARTSupport (IdeDev);
113
114 //
115 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device)
116 //
117 Status = AtaAtapi6Identify (IdeDev);
118 if (!EFI_ERROR (Status)) {
119 //
120 // It's a disk with >120GB capacity, initialized in AtaAtapi6Identify()
121 //
122 return EFI_SUCCESS;
123 } else if (Status == EFI_DEVICE_ERROR) {
124 //
125 // Some disk with big capacity (>200GB) is slow when being identified
126 // and will return all zero for word83.
127 // We try twice at first. If it fails, we do a SoftRest and try again.
128 //
129 Retry--;
130 if (Retry == 1) {
131 //
132 // Do a SoftRest before the third attempt.
133 //
134 AtaSoftReset (IdeDev);
135 }
136 continue;
137 }
138 //
139 // This is a hard disk <= 120GB capacity, treat it as normal hard disk
140 //
141 IdeDev->Type = IdeHardDisk;
142
143 //
144 // Block Media Information:
145 // Media->LogicalPartition , Media->WriteCaching will be filled
146 // in the DiscoverIdeDevcie() function.
147 //
148 IdeDev->BlkIo.Media->IoAlign = 4;
149 IdeDev->BlkIo.Media->MediaId = 1;
150 IdeDev->BlkIo.Media->RemovableMedia = FALSE;
151 IdeDev->BlkIo.Media->MediaPresent = TRUE;
152 IdeDev->BlkIo.Media->ReadOnly = FALSE;
153 IdeDev->BlkIo.Media->BlockSize = 0x200;
154
155 //
156 // Calculate device capacity
157 //
158 Capacity = ((UINT32)AtaIdentifyPointer->AtaData.user_addressable_sectors_hi << 16) |
159 AtaIdentifyPointer->AtaData.user_addressable_sectors_lo ;
160 IdeDev->BlkIo.Media->LastBlock = Capacity - 1;
161
162 return EFI_SUCCESS;
163 }
164
165 }
166 break;
167 }
168
169 gBS->FreePool (AtaIdentifyPointer);
170 //
171 // Make sure the pIdData will not be freed again.
172 //
173 IdeDev->pIdData = NULL;
174
175 return EFI_DEVICE_ERROR;
176 }
177
178
179 /**
180 This function is called by ATAIdentify() to identity whether this disk
181 supports ATA/ATAPI6 48bit addressing, ie support >120G capacity
182
183 @param[in] *IdeDev
184 pointer pointing to IDE_BLK_IO_DEV data structure, used
185 to record all the information of the IDE device.
186
187 @retval EFI_SUCCESS The disk specified by IdeDev is a Atapi6 supported one
188 and 48-bit addressing must be used
189
190 @retval EFI_UNSUPPORTED The disk dosn't not support Atapi6 or it supports but
191 the capacity is below 120G, 48bit addressing is not needed
192
193 @retval EFI_DEVICE_ERROR The identify data in IdeDev is incorrect
194
195 @note
196 This function must be called after DEVICE_IDENTITY command has been
197 successfully returned
198
199 **/
200 EFI_STATUS
201 AtaAtapi6Identify (
202 IN IDE_BLK_IO_DEV *IdeDev
203 )
204 {
205 UINT8 Index;
206 EFI_LBA TmpLba;
207 EFI_LBA Capacity;
208 EFI_IDENTIFY_DATA *Atapi6IdentifyStruct;
209
210 if (IdeDev->pIdData == NULL) {
211 return EFI_UNSUPPORTED;
212 }
213
214 Atapi6IdentifyStruct = IdeDev->pIdData;
215
216 if ((Atapi6IdentifyStruct->AtapiData.cmd_set_support_83 & (BIT15 | BIT14)) != 0x4000) {
217 //
218 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one
219 //
220 return EFI_DEVICE_ERROR;
221 }
222
223 if ((Atapi6IdentifyStruct->AtapiData.cmd_set_support_83 & BIT10) == 0) {
224 //
225 // The device dosn't support 48 bit addressing
226 //
227 return EFI_UNSUPPORTED;
228 }
229
230 //
231 // 48 bit address feature set is supported, get maximum capacity
232 //
233 Capacity = Atapi6IdentifyStruct->AtapiData.max_user_lba_for_48bit_addr[0];
234 for (Index = 1; Index < 4; Index++) {
235 //
236 // Lower byte goes first: word[100] is the lowest word, word[103] is highest
237 //
238 TmpLba = Atapi6IdentifyStruct->AtapiData.max_user_lba_for_48bit_addr[Index];
239 Capacity |= LShiftU64 (TmpLba, 16 * Index);
240 }
241
242 if (Capacity > MAX_28BIT_ADDRESSING_CAPACITY) {
243 //
244 // Capacity exceeds 120GB. 48-bit addressing is really needed
245 //
246 IdeDev->Type = Ide48bitAddressingHardDisk;
247
248 //
249 // Fill block media information:Media->LogicalPartition ,
250 // Media->WriteCaching will be filledin the DiscoverIdeDevcie() function.
251 //
252 IdeDev->BlkIo.Media->IoAlign = 4;
253 IdeDev->BlkIo.Media->MediaId = 1;
254 IdeDev->BlkIo.Media->RemovableMedia = FALSE;
255 IdeDev->BlkIo.Media->MediaPresent = TRUE;
256 IdeDev->BlkIo.Media->ReadOnly = FALSE;
257 IdeDev->BlkIo.Media->BlockSize = 0x200;
258 IdeDev->BlkIo.Media->LastBlock = Capacity - 1;
259
260 return EFI_SUCCESS;
261 }
262
263 return EFI_UNSUPPORTED;
264 }
265
266 /**
267 This function is called by ATAIdentify() or ATAPIIdentify()
268 to print device's module name.
269
270 @param[in] *IdeDev
271 pointer pointing to IDE_BLK_IO_DEV data structure, used
272 to record all the information of the IDE device.
273
274 **/
275 VOID
276 PrintAtaModuleName (
277 IN IDE_BLK_IO_DEV *IdeDev
278 )
279 {
280 if (IdeDev->pIdData == NULL) {
281 return ;
282 }
283
284 SwapStringChars (IdeDev->ModelName, IdeDev->pIdData->AtaData.ModelName, 40);
285 IdeDev->ModelName[40] = 0x00;
286 }
287
288 /**
289 This function is used to send out ATA commands conforms to the
290 PIO Data In Protocol.
291
292 @param[in] *IdeDev
293 pointer pointing to IDE_BLK_IO_DEV data structure, used
294 to record all the information of the IDE device.
295
296 @param[in] *Buffer
297 buffer contained data transferred from device to host.
298
299 @param[in] ByteCount
300 data size in byte unit of the buffer.
301
302 @param[in] AtaCommand
303 value of the Command Register
304
305 @param[in] Head
306 value of the Head/Device Register
307
308 @param[in] SectorCount
309 value of the Sector Count Register
310
311 @param[in] SectorNumber
312 value of the Sector Number Register
313
314 @param[in] CylinderLsb
315 value of the low byte of the Cylinder Register
316
317 @param[in] CylinderMsb
318 value of the high byte of the Cylinder Register
319
320 @retval EFI_SUCCESS send out the ATA command and device send required
321 data successfully.
322
323 @retval EFI_DEVICE_ERROR command sent failed.
324
325 **/
326 EFI_STATUS
327 AtaPioDataIn (
328 IN IDE_BLK_IO_DEV *IdeDev,
329 IN VOID *Buffer,
330 IN UINT32 ByteCount,
331 IN UINT8 AtaCommand,
332 IN UINT8 Head,
333 IN UINT8 SectorCount,
334 IN UINT8 SectorNumber,
335 IN UINT8 CylinderLsb,
336 IN UINT8 CylinderMsb
337 )
338 {
339 UINTN WordCount;
340 UINTN Increment;
341 UINT16 *Buffer16;
342 EFI_STATUS Status;
343
344 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
345 if (EFI_ERROR (Status)) {
346 return EFI_DEVICE_ERROR;
347 }
348
349 //
350 // e0:1110,0000-- bit7 and bit5 are reserved bits.
351 // bit6 set means LBA mode
352 //
353 IDEWritePortB (
354 IdeDev->PciIo,
355 IdeDev->IoPort->Head,
356 (UINT8) ((IdeDev->Device << 4) | 0xe0 | Head)
357 );
358
359 //
360 // All ATAPI device's ATA commands can be issued regardless of the
361 // state of the DRDY
362 //
363 if (IdeDev->Type == IdeHardDisk) {
364
365 Status = DRDYReady (IdeDev, ATATIMEOUT);
366 if (EFI_ERROR (Status)) {
367 return EFI_DEVICE_ERROR;
368 }
369 }
370 //
371 // set all the command parameters
372 // Before write to all the following registers, BSY and DRQ must be 0.
373 //
374 Status = DRQClear2 (IdeDev, ATATIMEOUT);
375 if (EFI_ERROR (Status)) {
376 return EFI_DEVICE_ERROR;
377 }
378
379 if (AtaCommand == ATA_CMD_SET_FEATURES) {
380 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
381 }
382
383 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount);
384 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, SectorNumber);
385 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, CylinderLsb);
386 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, CylinderMsb);
387
388 //
389 // send command via Command Register
390 //
391 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
392
393 Buffer16 = (UINT16 *) Buffer;
394
395 //
396 // According to PIO data in protocol, host can perform a series of reads to
397 // the data register after each time device set DRQ ready;
398 // The data size of "a series of read" is command specific.
399 // For most ATA command, data size received from device will not exceed
400 // 1 sector, hence the data size for "a series of read" can be the whole data
401 // size of one command request.
402 // For ATA command such as Read Sector command, the data size of one ATA
403 // command request is often larger than 1 sector, according to the
404 // Read Sector command, the data size of "a series of read" is exactly 1
405 // sector.
406 // Here for simplification reason, we specify the data size for
407 // "a series of read" to 1 sector (256 words) if data size of one ATA command
408 // request is larger than 256 words.
409 //
410 Increment = 256;
411
412 //
413 // used to record bytes of currently transfered data
414 //
415 WordCount = 0;
416
417 while (WordCount < ByteCount / 2) {
418 //
419 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
420 //
421 Status = DRQReady2 (IdeDev, ATATIMEOUT);
422 if (EFI_ERROR (Status)) {
423 return EFI_DEVICE_ERROR;
424 }
425
426 Status = CheckErrorStatus (IdeDev);
427 if (EFI_ERROR (Status)) {
428 return EFI_DEVICE_ERROR;
429 }
430
431 //
432 // Get the byte count for one series of read
433 //
434 if ((WordCount + Increment) > ByteCount / 2) {
435 Increment = ByteCount / 2 - WordCount;
436 }
437
438 IDEReadPortWMultiple (
439 IdeDev->PciIo,
440 IdeDev->IoPort->Data,
441 Increment,
442 Buffer16
443 );
444
445 WordCount += Increment;
446 Buffer16 += Increment;
447
448 }
449
450 DRQClear (IdeDev, ATATIMEOUT);
451
452 return CheckErrorStatus (IdeDev);
453 }
454
455 /**
456 This function is used to send out ATA commands conforms to the
457 PIO Data Out Protocol.
458
459 @param *IdeDev
460 pointer pointing to IDE_BLK_IO_DEV data structure, used
461 to record all the information of the IDE device.
462
463 @param *Buffer buffer contained data transferred from host to device.
464 @param ByteCount data size in byte unit of the buffer.
465 @param AtaCommand value of the Command Register
466 @param Head value of the Head/Device Register
467 @param SectorCount value of the Sector Count Register
468 @param SectorNumber value of the Sector Number Register
469 @param CylinderLsb value of the low byte of the Cylinder Register
470 @param CylinderMsb value of the high byte of the Cylinder Register
471
472 @retval EFI_SUCCESS send out the ATA command and device received required
473 data successfully.
474
475 @retval EFI_DEVICE_ERROR command sent failed.
476
477 **/
478 EFI_STATUS
479 AtaPioDataOut (
480 IN IDE_BLK_IO_DEV *IdeDev,
481 IN VOID *Buffer,
482 IN UINT32 ByteCount,
483 IN UINT8 AtaCommand,
484 IN UINT8 Head,
485 IN UINT8 SectorCount,
486 IN UINT8 SectorNumber,
487 IN UINT8 CylinderLsb,
488 IN UINT8 CylinderMsb
489 )
490 {
491 UINTN WordCount;
492 UINTN Increment;
493 UINT16 *Buffer16;
494 EFI_STATUS Status;
495
496 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
497 if (EFI_ERROR (Status)) {
498 return EFI_DEVICE_ERROR;
499 }
500
501 //
502 // select device via Head/Device register.
503 // Before write Head/Device register, BSY and DRQ must be 0.
504 //
505 Status = DRQClear2 (IdeDev, ATATIMEOUT);
506 if (EFI_ERROR (Status)) {
507 return EFI_DEVICE_ERROR;
508 }
509
510 //
511 // e0:1110,0000-- bit7 and bit5 are reserved bits.
512 // bit6 set means LBA mode
513 //
514 IDEWritePortB (
515 IdeDev->PciIo,
516 IdeDev->IoPort->Head,
517 (UINT8) ((IdeDev->Device << 4) | 0xe0 | Head)
518 );
519
520 Status = DRDYReady (IdeDev, ATATIMEOUT);
521 if (EFI_ERROR (Status)) {
522 return EFI_DEVICE_ERROR;
523 }
524
525 //
526 // set all the command parameters
527 // Before write to all the following registers, BSY and DRQ must be 0.
528 //
529 Status = DRQClear2 (IdeDev, ATATIMEOUT);
530 if (EFI_ERROR (Status)) {
531 return EFI_DEVICE_ERROR;
532 }
533
534 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount);
535 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, SectorNumber);
536 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, CylinderLsb);
537 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, CylinderMsb);
538
539 //
540 // send command via Command Register
541 //
542 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
543
544 Buffer16 = (UINT16 *) Buffer;
545
546 //
547 // According to PIO data out protocol, host can perform a series of
548 // writes to the data register after each time device set DRQ ready;
549 // The data size of "a series of read" is command specific.
550 // For most ATA command, data size written to device will not exceed 1 sector,
551 // hence the data size for "a series of write" can be the data size of one
552 // command request.
553 // For ATA command such as Write Sector command, the data size of one
554 // ATA command request is often larger than 1 sector, according to the
555 // Write Sector command, the data size of "a series of read" is exactly
556 // 1 sector.
557 // Here for simplification reason, we specify the data size for
558 // "a series of write" to 1 sector (256 words) if data size of one ATA command
559 // request is larger than 256 words.
560 //
561 Increment = 256;
562 WordCount = 0;
563
564 while (WordCount < ByteCount / 2) {
565
566 //
567 // DRQReady2-- read Alternate Status Register to determine the DRQ bit
568 // data transfer can be performed only when DRQ is ready.
569 //
570 Status = DRQReady2 (IdeDev, ATATIMEOUT);
571 if (EFI_ERROR (Status)) {
572 return EFI_DEVICE_ERROR;
573 }
574
575 Status = CheckErrorStatus (IdeDev);
576 if (EFI_ERROR (Status)) {
577 return EFI_DEVICE_ERROR;
578 }
579
580 //
581 // Check the remaining byte count is less than 512 bytes
582 //
583 if ((WordCount + Increment) > ByteCount / 2) {
584 Increment = ByteCount / 2 - WordCount;
585 }
586 //
587 // perform a series of write without check DRQ ready
588 //
589
590 IDEWritePortWMultiple (
591 IdeDev->PciIo,
592 IdeDev->IoPort->Data,
593 Increment,
594 Buffer16
595 );
596 WordCount += Increment;
597 Buffer16 += Increment;
598
599 }
600
601 DRQClear (IdeDev, ATATIMEOUT);
602
603 return CheckErrorStatus (IdeDev);
604 }
605
606 /**
607 This function is used to analyze the Status Register and print out
608 some debug information and if there is ERR bit set in the Status
609 Register, the Error Register's value is also be parsed and print out.
610
611 @param[in] *IdeDev
612 pointer pointing to IDE_BLK_IO_DEV data structure, used
613 to record all the information of the IDE device.
614
615 @retval EFI_SUCCESS No err information in the Status Register.
616 @retval EFI_DEVICE_ERROR Any err information in the Status Register.
617
618 **/
619 EFI_STATUS
620 CheckErrorStatus (
621 IN IDE_BLK_IO_DEV *IdeDev
622 )
623 {
624 UINT8 StatusRegister;
625 UINT8 ErrorRegister;
626
627 StatusRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status);
628
629 DEBUG_CODE_BEGIN ();
630
631 if (StatusRegister & ATA_STSREG_DWF) {
632 DEBUG (
633 (EFI_D_BLKIO,
634 "CheckErrorStatus()-- %02x : Error : Write Fault\n",
635 StatusRegister)
636 );
637 }
638
639 if (StatusRegister & ATA_STSREG_CORR) {
640 DEBUG (
641 (EFI_D_BLKIO,
642 "CheckErrorStatus()-- %02x : Error : Corrected Data\n",
643 StatusRegister)
644 );
645 }
646
647 if (StatusRegister & ATA_STSREG_ERR) {
648 ErrorRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error);
649
650 if (ErrorRegister & ATA_ERRREG_BBK) {
651 DEBUG (
652 (EFI_D_BLKIO,
653 "CheckErrorStatus()-- %02x : Error : Bad Block Detected\n",
654 ErrorRegister)
655 );
656 }
657
658 if (ErrorRegister & ATA_ERRREG_UNC) {
659 DEBUG (
660 (EFI_D_BLKIO,
661 "CheckErrorStatus()-- %02x : Error : Uncorrectable Data\n",
662 ErrorRegister)
663 );
664 }
665
666 if (ErrorRegister & ATA_ERRREG_MC) {
667 DEBUG (
668 (EFI_D_BLKIO,
669 "CheckErrorStatus()-- %02x : Error : Media Change\n",
670 ErrorRegister)
671 );
672 }
673
674 if (ErrorRegister & ATA_ERRREG_ABRT) {
675 DEBUG (
676 (EFI_D_BLKIO,
677 "CheckErrorStatus()-- %02x : Error : Abort\n",
678 ErrorRegister)
679 );
680 }
681
682 if (ErrorRegister & ATA_ERRREG_TK0NF) {
683 DEBUG (
684 (EFI_D_BLKIO,
685 "CheckErrorStatus()-- %02x : Error : Track 0 Not Found\n",
686 ErrorRegister)
687 );
688 }
689
690 if (ErrorRegister & ATA_ERRREG_AMNF) {
691 DEBUG (
692 (EFI_D_BLKIO,
693 "CheckErrorStatus()-- %02x : Error : Address Mark Not Found\n",
694 ErrorRegister)
695 );
696 }
697 }
698
699 DEBUG_CODE_END ();
700
701 if ((StatusRegister & (ATA_STSREG_ERR | ATA_STSREG_DWF | ATA_STSREG_CORR)) == 0) {
702 return EFI_SUCCESS;
703 }
704
705 return EFI_DEVICE_ERROR;
706
707 }
708
709 /**
710 This function is called by the AtaBlkIoReadBlocks() to perform
711 reading from media in block unit.
712
713 @param[in] *IdeDev
714 pointer pointing to IDE_BLK_IO_DEV data structure, used
715 to record all the information of the IDE device.
716
717 @param[in] *DataBuffer
718 A pointer to the destination buffer for the data.
719
720 @param[in] Lba
721 The starting logical block address to read from
722 on the device media.
723
724 @param[in] NumberOfBlocks
725 The number of transfer data blocks.
726
727 @return return status is fully dependent on the return status
728 of AtaPioDataIn() function.
729
730 **/
731 EFI_STATUS
732 AtaReadSectors (
733 IN IDE_BLK_IO_DEV *IdeDev,
734 IN VOID *DataBuffer,
735 IN EFI_LBA Lba,
736 IN UINTN NumberOfBlocks
737 )
738 {
739 EFI_STATUS Status;
740 UINTN BlocksRemaining;
741 UINT32 Lba32;
742 UINT8 Lba0;
743 UINT8 Lba1;
744 UINT8 Lba2;
745 UINT8 Lba3;
746 UINT8 AtaCommand;
747 UINT8 SectorCount8;
748 UINT16 SectorCount;
749 UINTN ByteCount;
750 VOID *Buffer;
751
752 Buffer = DataBuffer;
753
754 //
755 // Using ATA Read Sector(s) command (opcode=0x20) with PIO DATA IN protocol
756 //
757 AtaCommand = ATA_CMD_READ_SECTORS;
758
759
760 BlocksRemaining = NumberOfBlocks;
761
762 Lba32 = (UINT32) Lba;
763
764 Status = EFI_SUCCESS;
765
766 while (BlocksRemaining > 0) {
767
768 //
769 // in ATA-3 spec, LBA is in 28 bit width
770 //
771 Lba0 = (UINT8) Lba32;
772 Lba1 = (UINT8) (Lba32 >> 8);
773 Lba2 = (UINT8) (Lba32 >> 16);
774 //
775 // low 4 bit of Lba3 stands for LBA bit24~bit27.
776 //
777 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);
778
779 if (BlocksRemaining >= 0x100) {
780
781 //
782 // SectorCount8 is sent to Sector Count register, 0x00 means 256
783 // sectors to be read
784 //
785 SectorCount8 = 0x00;
786 //
787 // SectorCount is used to record the number of sectors to be read
788 //
789 SectorCount = 256;
790 } else {
791
792 SectorCount8 = (UINT8) BlocksRemaining;
793 SectorCount = (UINT16) BlocksRemaining;
794 }
795
796 //
797 // ByteCount is the number of bytes that will be read
798 //
799 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
800
801 //
802 // call AtaPioDataIn() to send Read Sector Command and receive data read
803 //
804 Status = AtaPioDataIn (
805 IdeDev,
806 Buffer,
807 (UINT32) ByteCount,
808 AtaCommand,
809 Lba3,
810 SectorCount8,
811 Lba0,
812 Lba1,
813 Lba2
814 );
815 if (EFI_ERROR (Status)) {
816 return Status;
817 }
818
819 Lba32 += SectorCount;
820 Buffer = ((UINT8 *) Buffer + ByteCount);
821 BlocksRemaining -= SectorCount;
822 }
823
824 return Status;
825 }
826
827 /**
828 This function is called by the AtaBlkIoWriteBlocks() to perform
829 writing onto media in block unit.
830
831 @param[in] *IdeDev
832 pointer pointing to IDE_BLK_IO_DEV data structure,used
833 to record all the information of the IDE device.
834
835 @param[in] *BufferData
836 A pointer to the source buffer for the data.
837
838 @param[in] Lba
839 The starting logical block address to write onto
840 the device media.
841
842 @param[in] NumberOfBlocks
843 The number of transfer data blocks.
844
845 @return return status is fully dependent on the return status
846 of AtaPioDataOut() function.
847
848 **/
849 EFI_STATUS
850 AtaWriteSectors (
851 IN IDE_BLK_IO_DEV *IdeDev,
852 IN VOID *BufferData,
853 IN EFI_LBA Lba,
854 IN UINTN NumberOfBlocks
855 )
856 {
857 EFI_STATUS Status;
858 UINTN BlocksRemaining;
859 UINT32 Lba32;
860 UINT8 Lba0;
861 UINT8 Lba1;
862 UINT8 Lba2;
863 UINT8 Lba3;
864 UINT8 AtaCommand;
865 UINT8 SectorCount8;
866 UINT16 SectorCount;
867 UINTN ByteCount;
868 VOID *Buffer;
869
870 Buffer = BufferData;
871
872 //
873 // Using Write Sector(s) command (opcode=0x30) with PIO DATA OUT protocol
874 //
875 AtaCommand = ATA_CMD_WRITE_SECTORS;
876
877 BlocksRemaining = NumberOfBlocks;
878
879 Lba32 = (UINT32) Lba;
880
881 Status = EFI_SUCCESS;
882
883 while (BlocksRemaining > 0) {
884
885 Lba0 = (UINT8) Lba32;
886 Lba1 = (UINT8) (Lba32 >> 8);
887 Lba2 = (UINT8) (Lba32 >> 16);
888 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);
889
890 if (BlocksRemaining >= 0x100) {
891
892 //
893 // SectorCount8 is sent to Sector Count register, 0x00 means 256 sectors
894 // to be written
895 //
896 SectorCount8 = 0x00;
897 //
898 // SectorCount is used to record the number of sectors to be written
899 //
900 SectorCount = 256;
901 } else {
902
903 SectorCount8 = (UINT8) BlocksRemaining;
904 SectorCount = (UINT16) BlocksRemaining;
905 }
906
907 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
908
909 Status = AtaPioDataOut (
910 IdeDev,
911 Buffer,
912 (UINT32) ByteCount,
913 AtaCommand,
914 Lba3,
915 SectorCount8,
916 Lba0,
917 Lba1,
918 Lba2
919 );
920 if (EFI_ERROR (Status)) {
921 return Status;
922 }
923
924 Lba32 += SectorCount;
925 Buffer = ((UINT8 *) Buffer + ByteCount);
926 BlocksRemaining -= SectorCount;
927 }
928
929 return Status;
930 }
931
932 /**
933 This function is used to implement the Soft Reset on the specified
934 device. But, the ATA Soft Reset mechanism is so strong a reset method
935 that it will force resetting on both devices connected to the
936 same cable.
937
938 It is called by IdeBlkIoReset(), a interface function of Block
939 I/O protocol.
940
941 This function can also be used by the ATAPI device to perform reset when
942 ATAPI Reset command is failed.
943
944 @param[in] *IdeDev
945 pointer pointing to IDE_BLK_IO_DEV data structure, used
946 to record all the information of the IDE device.
947
948 @retval EFI_SUCCESS Soft reset completes successfully.
949 @retval EFI_DEVICE_ERROR Any step during the reset process is failed.
950
951 @note
952 The registers initial values after ATA soft reset are different
953 to the ATA device and ATAPI device.
954
955 **/
956 EFI_STATUS
957 AtaSoftReset (
958 IN IDE_BLK_IO_DEV *IdeDev
959 )
960 {
961
962 UINT8 DeviceControl;
963
964 DeviceControl = 0;
965 //
966 // set SRST bit to initiate soft reset
967 //
968 DeviceControl |= ATA_CTLREG_SRST;
969
970 //
971 // disable Interrupt
972 //
973 DeviceControl |= BIT1;
974
975 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
976
977 //
978 // SRST should assert for at least 5 us, we use 10 us for
979 // better compatibility
980 //
981 gBS->Stall (10);
982
983 //
984 // Enable interrupt to support UDMA, and clear SRST bit
985 //
986 DeviceControl = 0;
987 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
988
989 //
990 // Wait for at least 2 ms to check BSY status, we use 10 ms
991 // for better compatibility
992 //
993 gBS->Stall(10000);
994 //
995 // slave device needs at most 31s to clear BSY
996 //
997 if (WaitForBSYClear (IdeDev, 31000) == EFI_TIMEOUT) {
998 return EFI_DEVICE_ERROR;
999 }
1000
1001 return EFI_SUCCESS;
1002 }
1003
1004 /**
1005 This function is the ATA implementation for ReadBlocks in the
1006 Block I/O Protocol interface.
1007
1008 @param[in] *IdeBlkIoDevice
1009 Indicates the calling context.
1010
1011 @param[in] MediaId
1012 The media id that the read request is for.
1013
1014 @param[in] LBA
1015 The starting logical block address to read from
1016 on the device.
1017
1018 @param[in] BufferSize
1019 The size of the Buffer in bytes. This must be a
1020 multiple of the intrinsic block size of the device.
1021
1022 @param[out] *Buffer
1023 A pointer to the destination buffer for the data.
1024 The caller is responsible for either having implicit
1025 or explicit ownership of the memory that data is read into.
1026
1027 @retval EFI_SUCCESS Read Blocks successfully.
1028 @retval EFI_DEVICE_ERROR Read Blocks failed.
1029 @retval EFI_NO_MEDIA There is no media in the device.
1030 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
1031
1032 @retval EFI_BAD_BUFFER_SIZE
1033 The BufferSize parameter is not a multiple of the
1034 intrinsic block size of the device.
1035
1036 @retval EFI_INVALID_PARAMETER
1037 The read request contains LBAs that are not valid,
1038 or the data buffer is not valid.
1039
1040 @note
1041 If Read Block error because of device error, this function will call
1042 AtaSoftReset() function to reset device.
1043
1044 **/
1045 EFI_STATUS
1046 AtaBlkIoReadBlocks (
1047 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1048 IN UINT32 MediaId,
1049 IN EFI_LBA LBA,
1050 IN UINTN BufferSize,
1051 OUT VOID *Buffer
1052 )
1053 {
1054 EFI_BLOCK_IO_MEDIA *Media;
1055 UINTN BlockSize;
1056 UINTN NumberOfBlocks;
1057 EFI_STATUS Status;
1058
1059 if (Buffer == NULL) {
1060 return EFI_INVALID_PARAMETER;
1061 }
1062
1063 if (BufferSize == 0) {
1064 return EFI_SUCCESS;
1065 }
1066
1067 Status = EFI_SUCCESS;
1068
1069 //
1070 // Get the intrinsic block size
1071 //
1072 Media = IdeBlkIoDevice->BlkIo.Media;
1073 BlockSize = Media->BlockSize;
1074
1075 NumberOfBlocks = BufferSize / BlockSize;
1076
1077 if (MediaId != Media->MediaId) {
1078 return EFI_MEDIA_CHANGED;
1079 }
1080
1081 if (BufferSize % BlockSize != 0) {
1082 return EFI_BAD_BUFFER_SIZE;
1083 }
1084
1085 if (!(Media->MediaPresent)) {
1086 return EFI_NO_MEDIA;
1087 }
1088
1089 if (LBA > Media->LastBlock) {
1090 return EFI_INVALID_PARAMETER;
1091 }
1092
1093 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1094 return EFI_INVALID_PARAMETER;
1095 }
1096
1097 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1098 return EFI_INVALID_PARAMETER;
1099 }
1100
1101 Status = EFI_SUCCESS;
1102 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) {
1103 //
1104 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 read block mechanism
1105 //
1106 if (IdeBlkIoDevice->UdmaMode.Valid) {
1107 Status = AtaUdmaReadExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1108 } else {
1109 Status = AtaReadSectorsExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1110 }
1111 } else {
1112 //
1113 // For ATA-3 compatible device, use ATA-3 read block mechanism
1114 //
1115 if (IdeBlkIoDevice->UdmaMode.Valid) {
1116 Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1117 } else {
1118 Status = AtaReadSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1119 }
1120 }
1121
1122 if (EFI_ERROR (Status)) {
1123 AtaSoftReset (IdeBlkIoDevice);
1124 return EFI_DEVICE_ERROR;
1125 }
1126
1127 return EFI_SUCCESS;
1128
1129 }
1130
1131 /**
1132 This function is the ATA implementation for WriteBlocks in the
1133 Block I/O Protocol interface.
1134
1135 @param[in] *IdeBlkIoDevice
1136 Indicates the calling context.
1137
1138 @param[in] MediaId
1139 The media id that the write request is for.
1140
1141 @param[in] LBA
1142 The starting logical block address to write onto
1143 the device.
1144
1145 @param[in] BufferSize
1146 The size of the Buffer in bytes. This must be a
1147 multiple of the intrinsic block size of the device.
1148
1149 @param[out] *Buffer
1150 A pointer to the source buffer for the data.
1151 The caller is responsible for either having implicit
1152 or explicit ownership of the memory that data is
1153 written from.
1154
1155 @retval EFI_SUCCESS Write Blocks successfully.
1156 @retval EFI_DEVICE_ERROR Write Blocks failed.
1157 @retval EFI_NO_MEDIA There is no media in the device.
1158 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
1159
1160 @retval EFI_BAD_BUFFER_SIZE
1161 The BufferSize parameter is not a multiple of the
1162 intrinsic block size of the device.
1163
1164 @retval EFI_INVALID_PARAMETER
1165 The write request contains LBAs that are not valid,
1166 or the data buffer is not valid.
1167
1168 @note
1169 If Write Block error because of device error, this function will call
1170 AtaSoftReset() function to reset device.
1171
1172 **/
1173 EFI_STATUS
1174 AtaBlkIoWriteBlocks (
1175 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1176 IN UINT32 MediaId,
1177 IN EFI_LBA LBA,
1178 IN UINTN BufferSize,
1179 OUT VOID *Buffer
1180 )
1181 {
1182
1183 EFI_BLOCK_IO_MEDIA *Media;
1184 UINTN BlockSize;
1185 UINTN NumberOfBlocks;
1186 EFI_STATUS Status;
1187
1188 if (Buffer == NULL) {
1189 return EFI_INVALID_PARAMETER;
1190 }
1191
1192 if (BufferSize == 0) {
1193 return EFI_SUCCESS;
1194 }
1195
1196 Status = EFI_SUCCESS;
1197
1198 //
1199 // Get the intrinsic block size
1200 //
1201 Media = IdeBlkIoDevice->BlkIo.Media;
1202 BlockSize = Media->BlockSize;
1203 NumberOfBlocks = BufferSize / BlockSize;
1204
1205 if (MediaId != Media->MediaId) {
1206 return EFI_MEDIA_CHANGED;
1207 }
1208
1209 if (BufferSize % BlockSize != 0) {
1210 return EFI_BAD_BUFFER_SIZE;
1211 }
1212
1213 if (LBA > Media->LastBlock) {
1214 return EFI_INVALID_PARAMETER;
1215 }
1216
1217 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1218 return EFI_INVALID_PARAMETER;
1219 }
1220
1221 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1222 return EFI_INVALID_PARAMETER;
1223 }
1224
1225 Status = EFI_SUCCESS;
1226 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) {
1227 //
1228 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 write block mechanism
1229 //
1230 if (IdeBlkIoDevice->UdmaMode.Valid) {
1231 Status = AtaUdmaWriteExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1232 } else {
1233 Status = AtaWriteSectorsExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1234 }
1235 } else {
1236 //
1237 // For ATA-3 compatible device, use ATA-3 write block mechanism
1238 //
1239 if (IdeBlkIoDevice->UdmaMode.Valid) {
1240 Status = AtaUdmaWrite (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1241 } else {
1242 Status = AtaWriteSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1243 }
1244 }
1245
1246 if (EFI_ERROR (Status)) {
1247 AtaSoftReset (IdeBlkIoDevice);
1248 return EFI_DEVICE_ERROR;
1249 }
1250
1251 return EFI_SUCCESS;
1252 }
1253
1254 /**
1255 This function is called by the AtaBlkIoReadBlocks() to perform
1256 reading from media in block unit. The function has been enhanced to
1257 support >120GB access and transfer at most 65536 blocks per command
1258
1259 @param[in] *IdeDev
1260 pointer pointing to IDE_BLK_IO_DEV data structure, used
1261 to record all the information of the IDE device.
1262
1263 @param[in] *DataBuffer A pointer to the destination buffer for the data.
1264 @param[in] StartLba The starting logical block address to read from
1265 on the device media.
1266 @param[in] NumberOfBlocks The number of transfer data blocks.
1267
1268 @return return status is fully dependent on the return status
1269 of AtaPioDataInExt() function.
1270
1271 **/
1272 EFI_STATUS
1273 AtaReadSectorsExt (
1274 IN IDE_BLK_IO_DEV *IdeDev,
1275 IN VOID *DataBuffer,
1276 IN EFI_LBA StartLba,
1277 IN UINTN NumberOfBlocks
1278 )
1279 {
1280 EFI_STATUS Status;
1281 UINTN BlocksRemaining;
1282 EFI_LBA Lba64;
1283 UINT8 AtaCommand;
1284 UINT16 SectorCount;
1285 UINT32 ByteCount;
1286 VOID *Buffer;
1287
1288 //
1289 // Using ATA "Read Sectors Ext" command(opcode=0x24) with PIO DATA IN protocol
1290 //
1291 AtaCommand = ATA_CMD_READ_SECTORS_EXT;
1292 Buffer = DataBuffer;
1293 BlocksRemaining = NumberOfBlocks;
1294 Lba64 = StartLba;
1295 Status = EFI_SUCCESS;
1296
1297 while (BlocksRemaining > 0) {
1298
1299 if (BlocksRemaining >= 0x10000) {
1300 //
1301 // SectorCount is used to record the number of sectors to be read
1302 // Max 65536 sectors can be transfered at a time.
1303 //
1304 SectorCount = 0xffff;
1305 } else {
1306 SectorCount = (UINT16) BlocksRemaining;
1307 }
1308
1309 //
1310 // ByteCount is the number of bytes that will be read
1311 //
1312 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
1313
1314 //
1315 // call AtaPioDataInExt() to send Read Sector Command and receive data read
1316 //
1317 Status = AtaPioDataInExt (
1318 IdeDev,
1319 Buffer,
1320 ByteCount,
1321 AtaCommand,
1322 Lba64,
1323 SectorCount
1324 );
1325 if (EFI_ERROR (Status)) {
1326 return Status;
1327 }
1328
1329 Lba64 += SectorCount;
1330 Buffer = ((UINT8 *) Buffer + ByteCount);
1331 BlocksRemaining -= SectorCount;
1332 }
1333
1334 return Status;
1335 }
1336
1337 /**
1338 This function is called by the AtaBlkIoWriteBlocks() to perform
1339 writing onto media in block unit. The function has been enhanced to
1340 support >120GB access and transfer at most 65536 blocks per command
1341
1342 @param[in] *IdeDev
1343 pointer pointing to IDE_BLK_IO_DEV data structure,used
1344 to record all the information of the IDE device.
1345
1346 @param[in] *DataBuffer
1347 A pointer to the source buffer for the data.
1348
1349 @param[in] Lba
1350 The starting logical block address to write onto
1351 the device media.
1352
1353 @param[in] NumberOfBlocks
1354 The number of transfer data blocks.
1355
1356 @return status is fully dependent on the return status
1357 of AtaPioDataOutExt() function.
1358
1359 **/
1360 EFI_STATUS
1361 AtaWriteSectorsExt (
1362 IN IDE_BLK_IO_DEV *IdeDev,
1363 IN VOID *DataBuffer,
1364 IN EFI_LBA StartLba,
1365 IN UINTN NumberOfBlocks
1366 )
1367 {
1368 EFI_STATUS Status;
1369 EFI_LBA Lba64;
1370 UINTN BlocksRemaining;
1371 UINT8 AtaCommand;
1372 UINT16 SectorCount;
1373 UINT32 ByteCount;
1374 VOID *Buffer;
1375
1376 //
1377 // Using ATA "Write Sectors Ext" cmd(opcode=0x24) with PIO DATA OUT protocol
1378 //
1379 AtaCommand = ATA_CMD_WRITE_SECTORS_EXT;
1380 Lba64 = StartLba;
1381 Buffer = DataBuffer;
1382 BlocksRemaining = NumberOfBlocks;
1383
1384 Status = EFI_SUCCESS;
1385
1386 while (BlocksRemaining > 0) {
1387
1388 if (BlocksRemaining >= 0x10000) {
1389 //
1390 // SectorCount is used to record the number of sectors to be written.
1391 // Max 65536 sectors can be transfered at a time.
1392 //
1393 SectorCount = 0xffff;
1394 } else {
1395 SectorCount = (UINT16) BlocksRemaining;
1396 }
1397
1398 //
1399 // ByteCount is the number of bytes that will be written
1400 //
1401 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
1402
1403 //
1404 // Call AtaPioDataOutExt() to send "Write Sectors Ext" Command
1405 //
1406 Status = AtaPioDataOutExt (
1407 IdeDev,
1408 Buffer,
1409 ByteCount,
1410 AtaCommand,
1411 Lba64,
1412 SectorCount
1413 );
1414 if (EFI_ERROR (Status)) {
1415 return Status;
1416 }
1417
1418 Lba64 += SectorCount;
1419 Buffer = ((UINT8 *) Buffer + ByteCount);
1420 BlocksRemaining -= SectorCount;
1421 }
1422
1423 return Status;
1424 }
1425
1426 /**
1427 This function is used to send out ATA commands conforms to the
1428 PIO Data In Protocol, supporting ATA/ATAPI-6 standard
1429
1430 Comparing with ATA-3 data in protocol, we have two differents here:<BR>
1431 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1432 wait will frequently fail... cause writing function return error)
1433
1434 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1435 slow down writing performance by 100 times!)
1436
1437 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
1438 to record all the information of the IDE device.
1439
1440 @param[in,out] *Buffer buffer contained data transferred from device to host.
1441 @param[in] ByteCount data size in byte unit of the buffer.
1442 @param[in] AtaCommand value of the Command Register
1443 @param[in] StartLba the start LBA of this transaction
1444 @param[in] SectorCount the count of sectors to be transfered
1445
1446 @retval EFI_SUCCESS send out the ATA command and device send required
1447 data successfully.
1448
1449 @retval EFI_DEVICE_ERROR command sent failed.
1450
1451 **/
1452 EFI_STATUS
1453 AtaPioDataInExt (
1454 IN IDE_BLK_IO_DEV *IdeDev,
1455 IN OUT VOID *Buffer,
1456 IN UINT32 ByteCount,
1457 IN UINT8 AtaCommand,
1458 IN EFI_LBA StartLba,
1459 IN UINT16 SectorCount
1460 )
1461 {
1462 UINT8 DevSel;
1463 UINT8 SectorCount8;
1464 UINT8 LbaLow;
1465 UINT8 LbaMid;
1466 UINT8 LbaHigh;
1467 UINTN WordCount;
1468 UINTN Increment;
1469 UINT16 *Buffer16;
1470 EFI_STATUS Status;
1471
1472 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1473 if (EFI_ERROR (Status)) {
1474 return EFI_DEVICE_ERROR;
1475 }
1476
1477 //
1478 // Select device, set bit6 as 1 to indicate LBA mode is used
1479 //
1480 DevSel = (UINT8) (IdeDev->Device << 4);
1481 DevSel |= 0x40;
1482 IDEWritePortB (
1483 IdeDev->PciIo,
1484 IdeDev->IoPort->Head,
1485 DevSel
1486 );
1487
1488 //
1489 // Wait for DRDY singnal asserting. ATAPI device needn't wait
1490 //
1491 if ( (IdeDev->Type == IdeHardDisk) ||
1492 (IdeDev->Type == Ide48bitAddressingHardDisk)) {
1493
1494 Status = DRDYReady (IdeDev, ATATIMEOUT);
1495 if (EFI_ERROR (Status)) {
1496 return EFI_DEVICE_ERROR;
1497 }
1498 }
1499
1500 //
1501 // Fill feature register if needed
1502 //
1503 if (AtaCommand == ATA_CMD_SET_FEATURES) {
1504 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
1505 }
1506
1507 //
1508 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1509 //
1510 SectorCount8 = (UINT8) (SectorCount >> 8);
1511 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1512
1513 SectorCount8 = (UINT8) SectorCount;
1514 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1515
1516 //
1517 // Fill the start LBA registers, which are also two-byte FIFO
1518 //
1519 LbaLow = (UINT8) RShiftU64 (StartLba, 24);
1520 LbaMid = (UINT8) RShiftU64 (StartLba, 32);
1521 LbaHigh = (UINT8) RShiftU64 (StartLba, 40);
1522 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1523 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1524 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1525
1526 LbaLow = (UINT8) StartLba;
1527 LbaMid = (UINT8) RShiftU64 (StartLba, 8);
1528 LbaHigh = (UINT8) RShiftU64 (StartLba, 16);
1529 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1530 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1531 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1532
1533 //
1534 // Send command via Command Register, invoking the processing of this command
1535 //
1536 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1537
1538 Buffer16 = (UINT16 *) Buffer;
1539
1540 //
1541 // According to PIO data in protocol, host can perform a series of reads to
1542 // the data register after each time device set DRQ ready;
1543 //
1544
1545 //
1546 // 256 words
1547 //
1548 Increment = 256;
1549
1550 //
1551 // used to record bytes of currently transfered data
1552 //
1553 WordCount = 0;
1554
1555 while (WordCount < ByteCount / 2) {
1556 //
1557 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1558 //
1559 Status = DRQReady2 (IdeDev, ATATIMEOUT);
1560 if (EFI_ERROR (Status)) {
1561 return EFI_DEVICE_ERROR;
1562 }
1563
1564 Status = CheckErrorStatus (IdeDev);
1565 if (EFI_ERROR (Status)) {
1566 return EFI_DEVICE_ERROR;
1567 }
1568
1569 //
1570 // Get the byte count for one series of read
1571 //
1572 if ((WordCount + Increment) > ByteCount / 2) {
1573 Increment = ByteCount / 2 - WordCount;
1574 }
1575
1576 IDEReadPortWMultiple (
1577 IdeDev->PciIo,
1578 IdeDev->IoPort->Data,
1579 Increment,
1580 Buffer16
1581 );
1582
1583 WordCount += Increment;
1584 Buffer16 += Increment;
1585
1586 }
1587
1588 return CheckErrorStatus (IdeDev);
1589 }
1590
1591 /**
1592 This function is used to send out ATA commands conforms to the
1593 PIO Data Out Protocol, supporting ATA/ATAPI-6 standard
1594
1595 Comparing with ATA-3 data out protocol, we have two differents here:<BR>
1596 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1597 wait will frequently fail... cause writing function return error)
1598
1599 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1600 slow down writing performance by 100 times!)
1601
1602 @param[in] *IdeDev
1603 pointer pointing to IDE_BLK_IO_DEV data structure, used
1604 to record all the information of the IDE device.
1605
1606 @param[in] *Buffer buffer contained data transferred from host to device.
1607 @param[in] ByteCount data size in byte unit of the buffer.
1608 @param[in] AtaCommand value of the Command Register
1609 @param[in] StartLba the start LBA of this transaction
1610 @param[in] SectorCount the count of sectors to be transfered
1611
1612 @retval EFI_SUCCESS send out the ATA command and device receive required
1613 data successfully.
1614
1615 @retval EFI_DEVICE_ERROR command sent failed.
1616
1617 **/
1618 EFI_STATUS
1619 AtaPioDataOutExt (
1620 IN IDE_BLK_IO_DEV *IdeDev,
1621 IN VOID *Buffer,
1622 IN UINT32 ByteCount,
1623 IN UINT8 AtaCommand,
1624 IN EFI_LBA StartLba,
1625 IN UINT16 SectorCount
1626 )
1627 {
1628 UINT8 DevSel;
1629 UINT8 SectorCount8;
1630 UINT8 LbaLow;
1631 UINT8 LbaMid;
1632 UINT8 LbaHigh;
1633 UINTN WordCount;
1634 UINTN Increment;
1635 UINT16 *Buffer16;
1636 EFI_STATUS Status;
1637
1638 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1639 if (EFI_ERROR (Status)) {
1640 return EFI_DEVICE_ERROR;
1641 }
1642
1643 //
1644 // Select device. Set bit6 as 1 to indicate LBA mode is used
1645 //
1646 DevSel = (UINT8) (IdeDev->Device << 4);
1647 DevSel |= 0x40;
1648 IDEWritePortB (
1649 IdeDev->PciIo,
1650 IdeDev->IoPort->Head,
1651 DevSel
1652 );
1653
1654 //
1655 // Wait for DRDY singnal asserting.
1656 //
1657 Status = DRDYReady (IdeDev, ATATIMEOUT);
1658 if (EFI_ERROR (Status)) {
1659 return EFI_DEVICE_ERROR;
1660 }
1661
1662 //
1663 // Fill feature register if needed
1664 //
1665 if (AtaCommand == ATA_CMD_SET_FEATURES) {
1666 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
1667 }
1668
1669 //
1670 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1671 //
1672 SectorCount8 = (UINT8) (SectorCount >> 8);
1673 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1674
1675 SectorCount8 = (UINT8) SectorCount;
1676 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1677
1678 //
1679 // Fill the start LBA registers, which are also two-byte FIFO
1680 //
1681 LbaLow = (UINT8) RShiftU64 (StartLba, 24);
1682 LbaMid = (UINT8) RShiftU64 (StartLba, 32);
1683 LbaHigh = (UINT8) RShiftU64 (StartLba, 40);
1684 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1685 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1686 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1687
1688 LbaLow = (UINT8) StartLba;
1689 LbaMid = (UINT8) RShiftU64 (StartLba, 8);
1690 LbaHigh = (UINT8) RShiftU64 (StartLba, 16);
1691 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1692 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1693 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1694
1695 //
1696 // Send command via Command Register, invoking the processing of this command
1697 //
1698 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1699
1700 Buffer16 = (UINT16 *) Buffer;
1701
1702 //
1703 // According to PIO Data Out protocol, host can perform a series of writes to
1704 // the data register after each time device set DRQ ready;
1705 //
1706 Increment = 256;
1707
1708 //
1709 // used to record bytes of currently transfered data
1710 //
1711 WordCount = 0;
1712
1713 while (WordCount < ByteCount / 2) {
1714 //
1715 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1716 //
1717 Status = DRQReady2 (IdeDev, ATATIMEOUT);
1718 if (EFI_ERROR (Status)) {
1719 return EFI_DEVICE_ERROR;
1720 }
1721
1722 Status = CheckErrorStatus (IdeDev);
1723 if (EFI_ERROR (Status)) {
1724 return EFI_DEVICE_ERROR;
1725 }
1726
1727 //
1728 // Write data into device by one series of writing to data register
1729 //
1730 if ((WordCount + Increment) > ByteCount / 2) {
1731 Increment = ByteCount / 2 - WordCount;
1732 }
1733
1734 IDEWritePortWMultiple (
1735 IdeDev->PciIo,
1736 IdeDev->IoPort->Data,
1737 Increment,
1738 Buffer16
1739 );
1740
1741 WordCount += Increment;
1742 Buffer16 += Increment;
1743
1744 }
1745 //
1746 // while
1747 //
1748
1749 return CheckErrorStatus (IdeDev);
1750 }
1751
1752
1753 /**
1754 Enable SMART of the disk if supported
1755
1756 @param[in] *IdeDev
1757 pointer pointing to IDE_BLK_IO_DEV data structure,used
1758 to record all the information of the IDE device.
1759
1760 **/
1761 VOID
1762 AtaSMARTSupport (
1763 IN IDE_BLK_IO_DEV *IdeDev
1764 )
1765 {
1766 EFI_STATUS Status;
1767 BOOLEAN SMARTSupported;
1768 UINT8 Device;
1769 EFI_IDENTIFY_DATA *TmpAtaIdentifyPointer;
1770 UINT8 DeviceSelect;
1771 UINT8 LBAMid;
1772 UINT8 LBAHigh;
1773
1774 //
1775 // Detect if the device supports S.M.A.R.T.
1776 //
1777 if ((IdeDev->pIdData->AtaData.command_set_supported_83 & 0xc000) != 0x4000) {
1778 //
1779 // Data in word 82 is not valid (bit15 shall be zero and bit14 shall be to one)
1780 //
1781 return ;
1782 } else {
1783 if ((IdeDev->pIdData->AtaData.command_set_supported_82 & 0x0001) != 0x0001) {
1784 //
1785 // S.M.A.R.T is not supported by the device
1786 //
1787 SMARTSupported = FALSE;
1788 } else {
1789 SMARTSupported = TRUE;
1790 }
1791 }
1792
1793 if (!SMARTSupported) {
1794 //
1795 // Report nonsupport status code
1796 //
1797 REPORT_STATUS_CODE (
1798 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1799 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_NOTSUPPORTED)
1800 );
1801 } else {
1802 //
1803 // Enable this feature
1804 //
1805 REPORT_STATUS_CODE (
1806 EFI_PROGRESS_CODE,
1807 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_ENABLE)
1808 );
1809
1810 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
1811 Status = AtaNonDataCommandIn (
1812 IdeDev,
1813 ATA_CMD_SMART,
1814 Device,
1815 ATA_SMART_ENABLE_OPERATION,
1816 0,
1817 0,
1818 ATA_CONSTANT_4F,
1819 ATA_CONSTANT_C2
1820 );
1821 //
1822 // Detect if this feature is enabled
1823 //
1824 TmpAtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA));
1825
1826 DeviceSelect = (UINT8) ((IdeDev->Device) << 4);
1827 Status = AtaPioDataIn (
1828 IdeDev,
1829 (VOID *) TmpAtaIdentifyPointer,
1830 sizeof (EFI_IDENTIFY_DATA),
1831 ATA_CMD_IDENTIFY_DRIVE,
1832 DeviceSelect,
1833 0,
1834 0,
1835 0,
1836 0
1837 );
1838 if (EFI_ERROR (Status)) {
1839 gBS->FreePool (TmpAtaIdentifyPointer);
1840 return ;
1841 }
1842
1843 //
1844 // Check if the feature is enabled
1845 //
1846 if ((TmpAtaIdentifyPointer->AtaData.command_set_feature_enb_85 & 0x0001) == 0x0001) {
1847 //
1848 // Read status data
1849 //
1850 AtaNonDataCommandIn (
1851 IdeDev,
1852 ATA_CMD_SMART,
1853 Device,
1854 ATA_SMART_RETURN_STATUS,
1855 0,
1856 0,
1857 ATA_CONSTANT_4F,
1858 ATA_CONSTANT_C2
1859 );
1860 LBAMid = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb);
1861 LBAHigh = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb);
1862
1863 if ((LBAMid == 0x4f) && (LBAHigh == 0xc2)) {
1864 //
1865 // The threshold exceeded condition is not detected by the device
1866 //
1867 REPORT_STATUS_CODE (
1868 EFI_PROGRESS_CODE,
1869 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_UNDERTHRESHOLD)
1870 );
1871
1872 } else if ((LBAMid == 0xf4) && (LBAHigh == 0x2c)) {
1873 //
1874 // The threshold exceeded condition is detected by the device
1875 //
1876 REPORT_STATUS_CODE (
1877 EFI_PROGRESS_CODE,
1878 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_OVERTHRESHOLD)
1879 );
1880 }
1881
1882 } else {
1883 //
1884 // Report disabled status code
1885 //
1886 REPORT_STATUS_CODE (
1887 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1888 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_DISABLED)
1889 );
1890 }
1891
1892 gBS->FreePool (TmpAtaIdentifyPointer);
1893 }
1894
1895 return ;
1896 }
1897
1898 /**
1899 Send ATA Ext command into device with NON_DATA protocol
1900
1901 @param IdeDev Standard IDE device private data structure
1902 @param AtaCommand The ATA command to be sent
1903 @param Device The value in Device register
1904 @param Feature The value in Feature register
1905 @param SectorCount The value in SectorCount register
1906 @param LbaAddress The LBA address in 48-bit mode
1907
1908 @retval EFI_SUCCESS Reading succeed
1909 @retval EFI_DEVICE_ERROR Error executing commands on this device
1910
1911 **/
1912 EFI_STATUS
1913 AtaCommandIssueExt (
1914 IN IDE_BLK_IO_DEV *IdeDev,
1915 IN UINT8 AtaCommand,
1916 IN UINT8 Device,
1917 IN UINT16 Feature,
1918 IN UINT16 SectorCount,
1919 IN EFI_LBA LbaAddress
1920 )
1921 {
1922 EFI_STATUS Status;
1923 UINT8 SectorCount8;
1924 UINT8 Feature8;
1925 UINT8 LbaLow;
1926 UINT8 LbaMid;
1927 UINT8 LbaHigh;
1928
1929 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1930 if (EFI_ERROR (Status)) {
1931 return EFI_DEVICE_ERROR;
1932 }
1933
1934 //
1935 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1936 //
1937 IDEWritePortB (
1938 IdeDev->PciIo,
1939 IdeDev->IoPort->Head,
1940 (UINT8) ((IdeDev->Device << 4) | 0xe0)
1941 );
1942
1943 //
1944 // ATA commands for ATA device must be issued when DRDY is set
1945 //
1946 Status = DRDYReady (IdeDev, ATATIMEOUT);
1947 if (EFI_ERROR (Status)) {
1948 return EFI_DEVICE_ERROR;
1949 }
1950
1951 //
1952 // Pass parameter into device register block
1953 //
1954 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
1955
1956 //
1957 // Fill the feature register, which is a two-byte FIFO. Need write twice.
1958 //
1959 Feature8 = (UINT8) (Feature >> 8);
1960 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1961
1962 Feature8 = (UINT8) Feature;
1963 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1964
1965 //
1966 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1967 //
1968 SectorCount8 = (UINT8) (SectorCount >> 8);
1969 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1970
1971 SectorCount8 = (UINT8) SectorCount;
1972 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1973
1974 //
1975 // Fill the start LBA registers, which are also two-byte FIFO
1976 //
1977 LbaLow = (UINT8) RShiftU64 (LbaAddress, 24);
1978 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1979 LbaLow = (UINT8) LbaAddress;
1980 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1981
1982 LbaMid = (UINT8) RShiftU64 (LbaAddress, 32);
1983 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1984 LbaMid = (UINT8) RShiftU64 (LbaAddress, 8);
1985 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1986
1987 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 40);
1988 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1989 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 16);
1990 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1991
1992 //
1993 // Work around for Segate 160G disk writing
1994 //
1995 gBS->Stall (1800);
1996
1997 //
1998 // Send command via Command Register
1999 //
2000 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
2001
2002 //
2003 // Stall at least 400ns
2004 //
2005 gBS->Stall (100);
2006
2007 return EFI_SUCCESS;
2008 }
2009
2010 /**
2011 Send ATA Ext command into device with NON_DATA protocol
2012
2013 @param IdeDev Standard IDE device private data structure
2014 @param AtaCommand The ATA command to be sent
2015 @param Device The value in Device register
2016 @param Feature The value in Feature register
2017 @param SectorCount The value in SectorCount register
2018 @param LbaAddress The LBA address in 48-bit mode
2019
2020 @retval EFI_SUCCESS Reading succeed
2021 @retval EFI_DEVICE_ERROR Error executing commands on this device
2022
2023 **/
2024 EFI_STATUS
2025 AtaCommandIssue (
2026 IN IDE_BLK_IO_DEV *IdeDev,
2027 IN UINT8 AtaCommand,
2028 IN UINT8 Device,
2029 IN UINT16 Feature,
2030 IN UINT16 SectorCount,
2031 IN EFI_LBA LbaAddress
2032 )
2033 {
2034 EFI_STATUS Status;
2035 UINT8 SectorCount8;
2036 UINT8 Feature8;
2037 UINT8 Lba0;
2038 UINT8 Lba1;
2039 UINT8 Lba2;
2040 UINT8 Lba3;
2041
2042 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
2043 if (EFI_ERROR (Status)) {
2044 return EFI_DEVICE_ERROR;
2045 }
2046
2047 //
2048 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
2049 //
2050 IDEWritePortB (
2051 IdeDev->PciIo,
2052 IdeDev->IoPort->Head,
2053 (UINT8) ((IdeDev->Device << 4) | 0xe0)
2054 );
2055
2056 //
2057 // ATA commands for ATA device must be issued when DRDY is set
2058 //
2059 Status = DRDYReady (IdeDev, ATATIMEOUT);
2060 if (EFI_ERROR (Status)) {
2061 return EFI_DEVICE_ERROR;
2062 }
2063
2064 Lba0 = (UINT8) LbaAddress;
2065 Lba1 = (UINT8) RShiftU64 (LbaAddress, 8);
2066 Lba2 = (UINT8) RShiftU64 (LbaAddress, 16);
2067 Lba3 = (UINT8) RShiftU64 (LbaAddress, 24);
2068 Device = (UINT8) (Device | Lba3);
2069
2070 //
2071 // Pass parameter into device register block
2072 //
2073 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2074
2075 //
2076 // Fill the feature register, which is a two-byte FIFO. Need write twice.
2077 //
2078 Feature8 = (UINT8) Feature;
2079 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
2080
2081 //
2082 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
2083 //
2084 SectorCount8 = (UINT8) SectorCount;
2085 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
2086
2087 //
2088 // Fill the start LBA registers, which are also two-byte FIFO
2089 //
2090
2091 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, Lba0);
2092 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, Lba1);
2093 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, Lba2);
2094
2095 //
2096 // Send command via Command Register
2097 //
2098 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
2099
2100 //
2101 // Stall at least 400ns
2102 //
2103 gBS->Stall (100);
2104
2105 return EFI_SUCCESS;
2106 }
2107
2108 /**
2109 This function is called by the AtaBlkIoReadBlocks() to perform
2110 reading from media in block unit. The function has been enhanced to
2111 support >120GB access and transfer at most 65536 blocks per command
2112
2113 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2114 to record all the information of the IDE device.
2115
2116 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2117
2118 @param[in] StartLba The starting logical block address to read from
2119 on the device media.
2120
2121 @param[in] NumberOfBlocks The number of transfer data blocks.
2122
2123 @return The device status of UDMA operation. If the operation is
2124 successful, return EFI_SUCCESS.
2125
2126 TODO: EFI_UNSUPPORTED - add return value to function comment
2127 TODO: EFI_DEVICE_ERROR - add return value to function comment
2128 TODO: EFI_DEVICE_ERROR - add return value to function comment
2129 TODO: EFI_DEVICE_ERROR - add return value to function comment
2130 **/
2131 EFI_STATUS
2132 AtaUdmaReadExt (
2133 IN IDE_BLK_IO_DEV *IdeDev,
2134 IN VOID *DataBuffer,
2135 IN EFI_LBA StartLba,
2136 IN UINTN NumberOfBlocks
2137 )
2138 {
2139 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaReadExtOp);
2140 }
2141
2142 /**
2143 This function is called by the AtaBlkIoReadBlocks() to perform
2144 reading from media in block unit. The function has been enhanced to
2145 support >120GB access and transfer at most 65536 blocks per command
2146
2147 @param[in] *IdeDev
2148 pointer pointing to IDE_BLK_IO_DEV data structure, used
2149 to record all the information of the IDE device.
2150
2151 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2152 @param[in] StartLba The starting logical block address to read from
2153 on the device media.
2154 @param[in] NumberOfBlocks The number of transfer data blocks.
2155
2156 @return The device status of UDMA operation. If the operation is
2157 successful, return EFI_SUCCESS.
2158
2159 TODO: EFI_UNSUPPORTED - add return value to function comment
2160 TODO: EFI_DEVICE_ERROR - add return value to function comment
2161 TODO: EFI_DEVICE_ERROR - add return value to function comment
2162 TODO: EFI_DEVICE_ERROR - add return value to function comment
2163 **/
2164 EFI_STATUS
2165 AtaUdmaRead (
2166 IN IDE_BLK_IO_DEV *IdeDev,
2167 IN VOID *DataBuffer,
2168 IN EFI_LBA StartLba,
2169 IN UINTN NumberOfBlocks
2170 )
2171 {
2172 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaReadOp);
2173 }
2174
2175 /**
2176 This function is called by the AtaBlkIoWriteBlocks() to perform
2177 writing to media in block unit. The function has been enhanced to
2178 support >120GB access and transfer at most 65536 blocks per command
2179
2180 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2181 to record all the information of the IDE device.
2182
2183 @param[in] *DataBuffer A pointer to the source buffer for the data.
2184
2185 @param[in] StartLba The starting logical block address to write to
2186 on the device media.
2187
2188 @param[in] NumberOfBlocks The number of transfer data blocks.
2189
2190 @return The device status of UDMA operation. If the operation is
2191 successful, return EFI_SUCCESS.
2192
2193 TODO: EFI_UNSUPPORTED - add return value to function comment
2194 TODO: EFI_DEVICE_ERROR - add return value to function comment
2195 TODO: EFI_DEVICE_ERROR - add return value to function comment
2196 **/
2197 EFI_STATUS
2198 AtaUdmaWriteExt (
2199 IN IDE_BLK_IO_DEV *IdeDev,
2200 IN VOID *DataBuffer,
2201 IN EFI_LBA StartLba,
2202 IN UINTN NumberOfBlocks
2203 )
2204 {
2205 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaWriteExtOp);
2206 }
2207
2208 /**
2209 This function is called by the AtaBlkIoWriteBlocks() to perform
2210 writing to media in block unit. The function has been enhanced to
2211 support >120GB access and transfer at most 65536 blocks per command
2212
2213 @param[in] *IdeDev
2214 pointer pointing to IDE_BLK_IO_DEV data structure, used
2215 to record all the information of the IDE device.
2216
2217 @param[in] *DataBuffer
2218 A pointer to the source buffer for the data.
2219
2220 @param[in] StartLba
2221 The starting logical block address to write to
2222 on the device media.
2223
2224 @param[in] NumberOfBlocks
2225 The number of transfer data blocks.
2226
2227 @return The device status of UDMA operation. If the operation is
2228 successful, return EFI_SUCCESS.
2229
2230 TODO: EFI_UNSUPPORTED - add return value to function comment
2231 TODO: EFI_DEVICE_ERROR - add return value to function comment
2232 TODO: EFI_DEVICE_ERROR - add return value to function comment
2233 **/
2234 EFI_STATUS
2235 AtaUdmaWrite (
2236 IN IDE_BLK_IO_DEV *IdeDev,
2237 IN VOID *DataBuffer,
2238 IN EFI_LBA StartLba,
2239 IN UINTN NumberOfBlocks
2240 )
2241 {
2242 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaWriteOp);
2243 }
2244
2245 /**
2246 Perform an ATA Udma operation (Read, ReadExt, Write, WriteExt).
2247
2248 @param[in] *IdeDev
2249 pointer pointing to IDE_BLK_IO_DEV data structure, used
2250 to record all the information of the IDE device.
2251
2252 @param[in] *DataBuffer
2253 A pointer to the source buffer for the data.
2254
2255 @param[in] StartLba
2256 The starting logical block address to write to
2257 on the device media.
2258
2259 @param[in] NumberOfBlocks
2260 The number of transfer data blocks.
2261
2262 @param[in] UdmaOp
2263 The perform operations could be AtaUdmaReadOp, AtaUdmaReadExOp,
2264 AtaUdmaWriteOp, AtaUdmaWriteExOp
2265
2266 @return The device status of UDMA operation. If the operation is
2267 successful, return EFI_SUCCESS.
2268
2269 **/
2270 EFI_STATUS
2271 DoAtaUdma (
2272 IN IDE_BLK_IO_DEV *IdeDev,
2273 IN VOID *DataBuffer,
2274 IN EFI_LBA StartLba,
2275 IN UINTN NumberOfBlocks,
2276 IN ATA_UDMA_OPERATION UdmaOp
2277 )
2278 {
2279 IDE_DMA_PRD *PrdAddr;
2280 IDE_DMA_PRD *UsedPrdAddr;
2281 IDE_DMA_PRD *TempPrdAddr;
2282 UINT8 RegisterValue;
2283 UINT8 Device;
2284 UINT64 IoPortForBmic;
2285 UINT64 IoPortForBmis;
2286 UINT64 IoPortForBmid;
2287 EFI_STATUS Status;
2288 UINTN PrdTableNum;
2289 UINTN ByteCount;
2290 UINTN ByteAvailable;
2291 UINT8 *PrdBuffer;
2292 UINTN RemainBlockNum;
2293 UINT8 DeviceControl;
2294 UINT32 Count;
2295 UINTN PageCount;
2296 VOID *Map;
2297 VOID *MemPage;
2298 EFI_PHYSICAL_ADDRESS DeviceAddress;
2299 UINTN MaxDmaCommandSectors;
2300 EFI_PCI_IO_PROTOCOL_OPERATION PciIoProtocolOp;
2301 UINT8 AtaCommand;
2302
2303 switch (UdmaOp) {
2304 case AtaUdmaReadOp:
2305 MaxDmaCommandSectors = ATAPI_MAX_DMA_CMD_SECTORS;
2306 PciIoProtocolOp = EfiPciIoOperationBusMasterWrite;
2307 AtaCommand = ATA_CMD_READ_DMA;
2308 break;
2309 case AtaUdmaReadExtOp:
2310 MaxDmaCommandSectors = ATAPI_MAX_DMA_EXT_CMD_SECTORS;
2311 PciIoProtocolOp = EfiPciIoOperationBusMasterWrite;
2312 AtaCommand = ATA_CMD_READ_DMA_EXT;
2313 break;
2314 case AtaUdmaWriteOp:
2315 MaxDmaCommandSectors = ATAPI_MAX_DMA_CMD_SECTORS;
2316 PciIoProtocolOp = EfiPciIoOperationBusMasterRead;
2317 AtaCommand = ATA_CMD_WRITE_DMA;
2318 break;
2319 case AtaUdmaWriteExtOp:
2320 MaxDmaCommandSectors = ATAPI_MAX_DMA_EXT_CMD_SECTORS;
2321 PciIoProtocolOp = EfiPciIoOperationBusMasterRead;
2322 AtaCommand = ATA_CMD_WRITE_DMA_EXT;
2323 break;
2324 default:
2325 return EFI_UNSUPPORTED;
2326 break;
2327 }
2328
2329 //
2330 // Select device
2331 //
2332 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
2333 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2334
2335 //
2336 // Enable interrupt to support UDMA
2337 //
2338 DeviceControl = 0;
2339 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2340
2341 if (IdePrimary == IdeDev->Channel) {
2342 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICP_OFFSET;
2343 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISP_OFFSET;
2344 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDP_OFFSET;
2345 } else {
2346 if (IdeSecondary == IdeDev->Channel) {
2347 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICS_OFFSET;
2348 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISS_OFFSET;
2349 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDS_OFFSET;
2350 } else {
2351 return EFI_UNSUPPORTED;
2352 }
2353 }
2354
2355 //
2356 // Read BMIS register and clear ERROR and INTR bit
2357 //
2358 IdeDev->PciIo->Io.Read (
2359 IdeDev->PciIo,
2360 EfiPciIoWidthUint8,
2361 EFI_PCI_IO_PASS_THROUGH_BAR,
2362 IoPortForBmis,
2363 1,
2364 &RegisterValue
2365 );
2366
2367 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR);
2368
2369 IdeDev->PciIo->Io.Write (
2370 IdeDev->PciIo,
2371 EfiPciIoWidthUint8,
2372 EFI_PCI_IO_PASS_THROUGH_BAR,
2373 IoPortForBmis,
2374 1,
2375 &RegisterValue
2376 );
2377
2378 Status = EFI_SUCCESS;
2379
2380 RemainBlockNum = NumberOfBlocks;
2381 while (RemainBlockNum > 0) {
2382
2383 if (RemainBlockNum >= MaxDmaCommandSectors) {
2384 //
2385 // SectorCount is used to record the number of sectors to be read
2386 // Max 65536 sectors can be transfered at a time.
2387 //
2388 NumberOfBlocks = MaxDmaCommandSectors;
2389 RemainBlockNum -= MaxDmaCommandSectors;
2390 } else {
2391 NumberOfBlocks = (UINT16) RemainBlockNum;
2392 RemainBlockNum = 0;
2393 }
2394
2395 //
2396 // Calculate the number of PRD table to make sure the memory region
2397 // not cross 64K boundary
2398 //
2399 ByteCount = NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2400 PrdTableNum = ((ByteCount >> 16) + 1) + 1;
2401
2402 //
2403 // Build PRD table
2404 //
2405 PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));
2406 Status = IdeDev->PciIo->AllocateBuffer (
2407 IdeDev->PciIo,
2408 AllocateAnyPages,
2409 EfiBootServicesData,
2410 PageCount,
2411 &MemPage,
2412 0
2413 );
2414 if (EFI_ERROR (Status)) {
2415 return EFI_OUT_OF_RESOURCES;
2416 }
2417 ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));
2418
2419 PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);
2420 //
2421 // To make sure PRD is allocated in one 64K page
2422 //
2423 if (((UINTN) PrdAddr & 0x0FFFF) > (((UINTN) PrdAddr + PrdTableNum * sizeof (IDE_DMA_PRD) - 1) & 0x0FFFF)) {
2424 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x10000) & 0xFFFF0000);
2425 } else {
2426 if ((UINTN) PrdAddr & 0x03) {
2427 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x04) & 0xFFFFFFFC);
2428 } else {
2429 UsedPrdAddr = PrdAddr;
2430 }
2431 }
2432
2433 //
2434 // Build the PRD table
2435 //
2436 Status = IdeDev->PciIo->Map (
2437 IdeDev->PciIo,
2438 PciIoProtocolOp,
2439 DataBuffer,
2440 &ByteCount,
2441 &DeviceAddress,
2442 &Map
2443 );
2444 if (EFI_ERROR (Status)) {
2445 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
2446 return EFI_OUT_OF_RESOURCES;
2447 }
2448 PrdBuffer = (VOID *) ((UINTN) DeviceAddress);
2449 TempPrdAddr = UsedPrdAddr;
2450 while (TRUE) {
2451
2452 ByteAvailable = 0x10000 - ((UINTN) PrdBuffer & 0xFFFF);
2453
2454 if (ByteCount <= ByteAvailable) {
2455 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2456 TempPrdAddr->ByteCount = (UINT16) ByteCount;
2457 TempPrdAddr->EndOfTable = 0x8000;
2458 break;
2459 }
2460
2461 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2462 TempPrdAddr->ByteCount = (UINT16) ByteAvailable;
2463
2464 ByteCount -= ByteAvailable;
2465 PrdBuffer += ByteAvailable;
2466 TempPrdAddr++;
2467 }
2468
2469 //
2470 // Set the base address to BMID register
2471 //
2472 IdeDev->PciIo->Io.Write (
2473 IdeDev->PciIo,
2474 EfiPciIoWidthUint32,
2475 EFI_PCI_IO_PASS_THROUGH_BAR,
2476 IoPortForBmid,
2477 1,
2478 &UsedPrdAddr
2479 );
2480
2481 //
2482 // Set BMIC register to identify the operation direction
2483 //
2484 IdeDev->PciIo->Io.Read (
2485 IdeDev->PciIo,
2486 EfiPciIoWidthUint8,
2487 EFI_PCI_IO_PASS_THROUGH_BAR,
2488 IoPortForBmic,
2489 1,
2490 &RegisterValue
2491 );
2492
2493 if (UdmaOp == AtaUdmaReadExtOp || UdmaOp == AtaUdmaReadOp) {
2494 RegisterValue |= BMIC_nREAD;
2495 } else {
2496 RegisterValue &= ~((UINT8) BMIC_nREAD);
2497 }
2498
2499 IdeDev->PciIo->Io.Write (
2500 IdeDev->PciIo,
2501 EfiPciIoWidthUint8,
2502 EFI_PCI_IO_PASS_THROUGH_BAR,
2503 IoPortForBmic,
2504 1,
2505 &RegisterValue
2506 );
2507
2508 if (UdmaOp == AtaUdmaWriteExtOp || UdmaOp == AtaUdmaReadExtOp) {
2509 Status = AtaCommandIssueExt (
2510 IdeDev,
2511 AtaCommand,
2512 Device,
2513 0,
2514 (UINT16) NumberOfBlocks,
2515 StartLba
2516 );
2517 } else {
2518 Status = AtaCommandIssue (
2519 IdeDev,
2520 AtaCommand,
2521 Device,
2522 0,
2523 (UINT16) NumberOfBlocks,
2524 StartLba
2525 );
2526 }
2527
2528 if (EFI_ERROR (Status)) {
2529 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
2530 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2531 return EFI_DEVICE_ERROR;
2532 }
2533
2534 //
2535 // Set START bit of BMIC register
2536 //
2537 IdeDev->PciIo->Io.Read (
2538 IdeDev->PciIo,
2539 EfiPciIoWidthUint8,
2540 EFI_PCI_IO_PASS_THROUGH_BAR,
2541 IoPortForBmic,
2542 1,
2543 &RegisterValue
2544 );
2545
2546 RegisterValue |= BMIC_START;
2547
2548 IdeDev->PciIo->Io.Write (
2549 IdeDev->PciIo,
2550 EfiPciIoWidthUint8,
2551 EFI_PCI_IO_PASS_THROUGH_BAR,
2552 IoPortForBmic,
2553 1,
2554 &RegisterValue
2555 );
2556
2557 //
2558 // Check the INTERRUPT and ERROR bit of BMIS
2559 // Max transfer number of sectors for one command is 65536(32Mbyte),
2560 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
2561 // So set the variable Count to 2000, for about 2 second timeout time.
2562 //
2563 Status = EFI_SUCCESS;
2564 Count = 2000;
2565 while (TRUE) {
2566
2567 IdeDev->PciIo->Io.Read (
2568 IdeDev->PciIo,
2569 EfiPciIoWidthUint8,
2570 EFI_PCI_IO_PASS_THROUGH_BAR,
2571 IoPortForBmis,
2572 1,
2573 &RegisterValue
2574 );
2575 if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {
2576 if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {
2577 Status = EFI_DEVICE_ERROR;
2578 break;
2579 }
2580 break;
2581 }
2582
2583 gBS->Stall (1000);
2584 Count --;
2585 }
2586
2587 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
2588 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2589 //
2590 // Read BMIS register and clear ERROR and INTR bit
2591 //
2592 IdeDev->PciIo->Io.Read (
2593 IdeDev->PciIo,
2594 EfiPciIoWidthUint8,
2595 EFI_PCI_IO_PASS_THROUGH_BAR,
2596 IoPortForBmis,
2597 1,
2598 &RegisterValue
2599 );
2600
2601 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR);
2602
2603 IdeDev->PciIo->Io.Write (
2604 IdeDev->PciIo,
2605 EfiPciIoWidthUint8,
2606 EFI_PCI_IO_PASS_THROUGH_BAR,
2607 IoPortForBmis,
2608 1,
2609 &RegisterValue
2610 );
2611 //
2612 // Read Status Register of IDE device to clear interrupt
2613 //
2614 RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);
2615 //
2616 // Clear START bit of BMIC register
2617 //
2618 IdeDev->PciIo->Io.Read (
2619 IdeDev->PciIo,
2620 EfiPciIoWidthUint8,
2621 EFI_PCI_IO_PASS_THROUGH_BAR,
2622 IoPortForBmic,
2623 1,
2624 &RegisterValue
2625 );
2626
2627 RegisterValue &= ~((UINT8) BMIC_START);
2628
2629 IdeDev->PciIo->Io.Write (
2630 IdeDev->PciIo,
2631 EfiPciIoWidthUint8,
2632 EFI_PCI_IO_PASS_THROUGH_BAR,
2633 IoPortForBmic,
2634 1,
2635 &RegisterValue
2636 );
2637
2638 if (RegisterValue & BMIS_ERROR) {
2639 return EFI_DEVICE_ERROR;
2640 }
2641
2642 if (EFI_ERROR (Status)) {
2643 break;
2644 }
2645 DataBuffer = (UINT8 *) DataBuffer + NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2646 StartLba += NumberOfBlocks;
2647 }
2648
2649 //
2650 // Disable interrupt of Select device
2651 //
2652 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);
2653 DeviceControl |= ATA_CTLREG_IEN_L;
2654 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2655
2656 return Status;
2657 }
2658