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