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