]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c
1d01ba798f38203afe0fc0d19c0e4c9cf3eb0484
[mirror_edk2.git] / EdkModulePkg / Bus / Pci / IdeBus / Dxe / ata.c
1 /** @file
2 Copyright (c) 2006 - 2007 Intel Corporation. <BR>
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 @par Revision Reference:
12 2002-6: Add Atapi6 enhancement, support >120GB hard disk, including
13 update - ATAIdentity() func
14 update - AtaBlockIoReadBlocks() func
15 update - AtaBlockIoWriteBlocks() func
16 add - AtaAtapi6Identify() func
17 add - AtaReadSectorsExt() func
18 add - AtaWriteSectorsExt() func
19 add - AtaPioDataInExt() func
20 add - AtaPioDataOutExt() func
21
22 **/
23
24 #include "idebus.h"
25
26 /**
27 Sends out an ATA Identify Command to the specified device.
28
29 This function is called by DiscoverIdeDevice() during its device
30 identification. It sends out the ATA Identify Command to the
31 specified device. Only ATA device responses to this command. If
32 the command succeeds, it returns the Identify data structure which
33 contains information about the device. This function extracts the
34 information it needs to fill the IDE_BLK_IO_DEV data structure,
35 including device type, media block size, media capacity, and etc.
36
37 @param[in] *IdeDev
38 pointer pointing to IDE_BLK_IO_DEV data structure,used
39 to record all the information of the IDE device.
40
41 @retval EFI_SUCCESS Identify ATA device successfully.
42
43 @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or
44 device is not ATA device.
45
46 @note
47 parameter IdeDev will be updated in this function.
48
49 **/
50 EFI_STATUS
51 ATAIdentify (
52 IN IDE_BLK_IO_DEV *IdeDev
53 )
54 {
55 EFI_STATUS Status;
56 EFI_IDENTIFY_DATA *AtaIdentifyPointer;
57 UINT32 Capacity;
58 UINT8 DeviceSelect;
59
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 UINT8 ErrorRegister;
598
599 StatusRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status);
600
601 DEBUG_CODE_BEGIN ();
602
603 if (StatusRegister & DWF) {
604 DEBUG (
605 (EFI_D_BLKIO,
606 "CheckErrorStatus()-- %02x : Error : Write Fault\n",
607 StatusRegister)
608 );
609 }
610
611 if (StatusRegister & CORR) {
612 DEBUG (
613 (EFI_D_BLKIO,
614 "CheckErrorStatus()-- %02x : Error : Corrected Data\n",
615 StatusRegister)
616 );
617 }
618
619 if (StatusRegister & ERR) {
620 ErrorRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error);
621
622 if (ErrorRegister & BBK_ERR) {
623 DEBUG (
624 (EFI_D_BLKIO,
625 "CheckErrorStatus()-- %02x : Error : Bad Block Detected\n",
626 ErrorRegister)
627 );
628 }
629
630 if (ErrorRegister & UNC_ERR) {
631 DEBUG (
632 (EFI_D_BLKIO,
633 "CheckErrorStatus()-- %02x : Error : Uncorrectable Data\n",
634 ErrorRegister)
635 );
636 }
637
638 if (ErrorRegister & MC_ERR) {
639 DEBUG (
640 (EFI_D_BLKIO,
641 "CheckErrorStatus()-- %02x : Error : Media Change\n",
642 ErrorRegister)
643 );
644 }
645
646 if (ErrorRegister & ABRT_ERR) {
647 DEBUG (
648 (EFI_D_BLKIO,
649 "CheckErrorStatus()-- %02x : Error : Abort\n",
650 ErrorRegister)
651 );
652 }
653
654 if (ErrorRegister & TK0NF_ERR) {
655 DEBUG (
656 (EFI_D_BLKIO,
657 "CheckErrorStatus()-- %02x : Error : Track 0 Not Found\n",
658 ErrorRegister)
659 );
660 }
661
662 if (ErrorRegister & AMNF_ERR) {
663 DEBUG (
664 (EFI_D_BLKIO,
665 "CheckErrorStatus()-- %02x : Error : Address Mark Not Found\n",
666 ErrorRegister)
667 );
668 }
669 }
670
671 DEBUG_CODE_END ();
672
673 if ((StatusRegister & (ERR | DWF | CORR)) == 0) {
674 return EFI_SUCCESS;
675 }
676
677 return EFI_DEVICE_ERROR;
678
679 }
680
681 /**
682 This function is called by the AtaBlkIoReadBlocks() to perform
683 reading from media in block unit.
684
685 @param[in] *IdeDev
686 pointer pointing to IDE_BLK_IO_DEV data structure, used
687 to record all the information of the IDE device.
688
689 @param[in] *DataBuffer
690 A pointer to the destination buffer for the data.
691
692 @param[in] Lba
693 The starting logical block address to read from
694 on the device media.
695
696 @param[in] NumberOfBlocks
697 The number of transfer data blocks.
698
699 @return return status is fully dependent on the return status
700 of AtaPioDataIn() function.
701
702 **/
703 EFI_STATUS
704 AtaReadSectors (
705 IN IDE_BLK_IO_DEV *IdeDev,
706 IN VOID *DataBuffer,
707 IN EFI_LBA Lba,
708 IN UINTN NumberOfBlocks
709 )
710 {
711 EFI_STATUS Status;
712 UINTN BlocksRemaining;
713 UINT32 Lba32;
714 UINT8 Lba0;
715 UINT8 Lba1;
716 UINT8 Lba2;
717 UINT8 Lba3;
718 UINT8 AtaCommand;
719 UINT8 SectorCount8;
720 UINT16 SectorCount;
721 UINTN ByteCount;
722 VOID *Buffer;
723
724 Buffer = DataBuffer;
725
726 //
727 // Using ATA Read Sector(s) command (opcode=0x20) with PIO DATA IN protocol
728 //
729 AtaCommand = READ_SECTORS_CMD;
730
731
732 BlocksRemaining = NumberOfBlocks;
733
734 Lba32 = (UINT32) Lba;
735
736 Status = EFI_SUCCESS;
737
738 while (BlocksRemaining > 0) {
739
740 //
741 // in ATA-3 spec, LBA is in 28 bit width
742 //
743 Lba0 = (UINT8) Lba32;
744 Lba1 = (UINT8) (Lba32 >> 8);
745 Lba2 = (UINT8) (Lba32 >> 16);
746 //
747 // low 4 bit of Lba3 stands for LBA bit24~bit27.
748 //
749 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);
750
751 if (BlocksRemaining >= 0x100) {
752
753 //
754 // SectorCount8 is sent to Sector Count register, 0x00 means 256
755 // sectors to be read
756 //
757 SectorCount8 = 0x00;
758 //
759 // SectorCount is used to record the number of sectors to be read
760 //
761 SectorCount = 256;
762 } else {
763
764 SectorCount8 = (UINT8) BlocksRemaining;
765 SectorCount = (UINT16) BlocksRemaining;
766 }
767
768 //
769 // ByteCount is the number of bytes that will be read
770 //
771 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
772
773 //
774 // call AtaPioDataIn() to send Read Sector Command and receive data read
775 //
776 Status = AtaPioDataIn (
777 IdeDev,
778 Buffer,
779 (UINT32) ByteCount,
780 AtaCommand,
781 Lba3,
782 SectorCount8,
783 Lba0,
784 Lba1,
785 Lba2
786 );
787 if (EFI_ERROR (Status)) {
788 return Status;
789 }
790
791 Lba32 += SectorCount;
792 Buffer = ((UINT8 *) Buffer + ByteCount);
793 BlocksRemaining -= SectorCount;
794 }
795
796 return Status;
797 }
798
799 /**
800 This function is called by the AtaBlkIoWriteBlocks() to perform
801 writing onto media in block unit.
802
803 @param[in] *IdeDev
804 pointer pointing to IDE_BLK_IO_DEV data structure,used
805 to record all the information of the IDE device.
806
807 @param[in] *BufferData
808 A pointer to the source buffer for the data.
809
810 @param[in] Lba
811 The starting logical block address to write onto
812 the device media.
813
814 @param[in] NumberOfBlocks
815 The number of transfer data blocks.
816
817 @return return status is fully dependent on the return status
818 of AtaPioDataOut() function.
819
820 **/
821 EFI_STATUS
822 AtaWriteSectors (
823 IN IDE_BLK_IO_DEV *IdeDev,
824 IN VOID *BufferData,
825 IN EFI_LBA Lba,
826 IN UINTN NumberOfBlocks
827 )
828 {
829 EFI_STATUS Status;
830 UINTN BlocksRemaining;
831 UINT32 Lba32;
832 UINT8 Lba0;
833 UINT8 Lba1;
834 UINT8 Lba2;
835 UINT8 Lba3;
836 UINT8 AtaCommand;
837 UINT8 SectorCount8;
838 UINT16 SectorCount;
839 UINTN ByteCount;
840 VOID *Buffer;
841
842 Buffer = BufferData;
843
844 //
845 // Using Write Sector(s) command (opcode=0x30) with PIO DATA OUT protocol
846 //
847 AtaCommand = WRITE_SECTORS_CMD;
848
849 BlocksRemaining = NumberOfBlocks;
850
851 Lba32 = (UINT32) Lba;
852
853 Status = EFI_SUCCESS;
854
855 while (BlocksRemaining > 0) {
856
857 Lba0 = (UINT8) Lba32;
858 Lba1 = (UINT8) (Lba32 >> 8);
859 Lba2 = (UINT8) (Lba32 >> 16);
860 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);
861
862 if (BlocksRemaining >= 0x100) {
863
864 //
865 // SectorCount8 is sent to Sector Count register, 0x00 means 256 sectors
866 // to be written
867 //
868 SectorCount8 = 0x00;
869 //
870 // SectorCount is used to record the number of sectors to be written
871 //
872 SectorCount = 256;
873 } else {
874
875 SectorCount8 = (UINT8) BlocksRemaining;
876 SectorCount = (UINT16) BlocksRemaining;
877 }
878
879 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
880
881 Status = AtaPioDataOut (
882 IdeDev,
883 Buffer,
884 (UINT32) ByteCount,
885 AtaCommand,
886 Lba3,
887 SectorCount8,
888 Lba0,
889 Lba1,
890 Lba2
891 );
892 if (EFI_ERROR (Status)) {
893 return Status;
894 }
895
896 Lba32 += SectorCount;
897 Buffer = ((UINT8 *) Buffer + ByteCount);
898 BlocksRemaining -= SectorCount;
899 }
900
901 return Status;
902 }
903
904 /**
905 This function is used to implement the Soft Reset on the specified
906 device. But, the ATA Soft Reset mechanism is so strong a reset method
907 that it will force resetting on both devices connected to the
908 same cable.
909
910 It is called by IdeBlkIoReset(), a interface function of Block
911 I/O protocol.
912
913 This function can also be used by the ATAPI device to perform reset when
914 ATAPI Reset command is failed.
915
916 @param[in] *IdeDev
917 pointer pointing to IDE_BLK_IO_DEV data structure, used
918 to record all the information of the IDE device.
919
920 @retval EFI_SUCCESS Soft reset completes successfully.
921 @retval EFI_DEVICE_ERROR Any step during the reset process is failed.
922
923 @note
924 The registers initial values after ATA soft reset are different
925 to the ATA device and ATAPI device.
926
927 **/
928 EFI_STATUS
929 AtaSoftReset (
930 IN IDE_BLK_IO_DEV *IdeDev
931 )
932 {
933
934 UINT8 DeviceControl;
935
936 DeviceControl = 0;
937 //
938 // set SRST bit to initiate soft reset
939 //
940 DeviceControl |= SRST;
941
942 //
943 // disable Interrupt
944 //
945 DeviceControl |= bit1;
946
947 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
948
949 //
950 // SRST should assert for at least 5 us, we use 10 us for
951 // better compatibility
952 //
953 gBS->Stall (10);
954
955 //
956 // Enable interrupt to support UDMA, and clear SRST bit
957 //
958 DeviceControl = 0;
959 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
960
961 //
962 // Wait for at least 2 ms to check BSY status, we use 10 ms
963 // for better compatibility
964 //
965 gBS->Stall(10000);
966 //
967 // slave device needs at most 31s to clear BSY
968 //
969 if (WaitForBSYClear (IdeDev, 31000) == EFI_TIMEOUT) {
970 return EFI_DEVICE_ERROR;
971 }
972
973 return EFI_SUCCESS;
974 }
975
976 /**
977 This function is the ATA implementation for ReadBlocks in the
978 Block I/O Protocol interface.
979
980 @param[in] *IdeBlkIoDevice
981 Indicates the calling context.
982
983 @param[in] MediaId
984 The media id that the read request is for.
985
986 @param[in] LBA
987 The starting logical block address to read from
988 on the device.
989
990 @param[in] BufferSize
991 The size of the Buffer in bytes. This must be a
992 multiple of the intrinsic block size of the device.
993
994 @param[out] *Buffer
995 A pointer to the destination buffer for the data.
996 The caller is responsible for either having implicit
997 or explicit ownership of the memory that data is read into.
998
999 @retval EFI_SUCCESS Read Blocks successfully.
1000 @retval EFI_DEVICE_ERROR Read Blocks failed.
1001 @retval EFI_NO_MEDIA There is no media in the device.
1002 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
1003
1004 @retval EFI_BAD_BUFFER_SIZE
1005 The BufferSize parameter is not a multiple of the
1006 intrinsic block size of the device.
1007
1008 @retval EFI_INVALID_PARAMETER
1009 The read request contains LBAs that are not valid,
1010 or the data buffer is not valid.
1011
1012 @note
1013 If Read Block error because of device error, this function will call
1014 AtaSoftReset() function to reset device.
1015
1016 **/
1017 EFI_STATUS
1018 AtaBlkIoReadBlocks (
1019 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1020 IN UINT32 MediaId,
1021 IN EFI_LBA LBA,
1022 IN UINTN BufferSize,
1023 OUT VOID *Buffer
1024 )
1025 {
1026 EFI_BLOCK_IO_MEDIA *Media;
1027 UINTN BlockSize;
1028 UINTN NumberOfBlocks;
1029 EFI_STATUS Status;
1030
1031 if (Buffer == NULL) {
1032 return EFI_INVALID_PARAMETER;
1033 }
1034
1035 if (BufferSize == 0) {
1036 return EFI_SUCCESS;
1037 }
1038
1039 Status = EFI_SUCCESS;
1040
1041 //
1042 // Get the intrinsic block size
1043 //
1044 Media = IdeBlkIoDevice->BlkIo.Media;
1045 BlockSize = Media->BlockSize;
1046
1047 NumberOfBlocks = BufferSize / BlockSize;
1048
1049 if (MediaId != Media->MediaId) {
1050 return EFI_MEDIA_CHANGED;
1051 }
1052
1053 if (BufferSize % BlockSize != 0) {
1054 return EFI_BAD_BUFFER_SIZE;
1055 }
1056
1057 if (!(Media->MediaPresent)) {
1058 return EFI_NO_MEDIA;
1059 }
1060
1061 if (LBA > Media->LastBlock) {
1062 return EFI_INVALID_PARAMETER;
1063 }
1064
1065 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1066 return EFI_INVALID_PARAMETER;
1067 }
1068
1069 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1070 return EFI_INVALID_PARAMETER;
1071 }
1072
1073 Status = EFI_SUCCESS;
1074 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) {
1075 //
1076 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 read block mechanism
1077 //
1078 if (IdeBlkIoDevice->UdmaMode.Valid) {
1079 Status = AtaUdmaReadExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1080 } else {
1081 Status = AtaReadSectorsExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1082 }
1083 } else {
1084 //
1085 // For ATA-3 compatible device, use ATA-3 read block mechanism
1086 //
1087 if (IdeBlkIoDevice->UdmaMode.Valid) {
1088 Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1089 } else {
1090 Status = AtaReadSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1091 }
1092 }
1093
1094 if (EFI_ERROR (Status)) {
1095 AtaSoftReset (IdeBlkIoDevice);
1096 return EFI_DEVICE_ERROR;
1097 }
1098
1099 return EFI_SUCCESS;
1100
1101 }
1102
1103 /**
1104 This function is the ATA implementation for WriteBlocks in the
1105 Block I/O Protocol interface.
1106
1107 @param[in] *IdeBlkIoDevice
1108 Indicates the calling context.
1109
1110 @param[in] MediaId
1111 The media id that the write request is for.
1112
1113 @param[in] LBA
1114 The starting logical block address to write onto
1115 the device.
1116
1117 @param[in] BufferSize
1118 The size of the Buffer in bytes. This must be a
1119 multiple of the intrinsic block size of the device.
1120
1121 @param[out] *Buffer
1122 A pointer to the source buffer for the data.
1123 The caller is responsible for either having implicit
1124 or explicit ownership of the memory that data is
1125 written from.
1126
1127 @retval EFI_SUCCESS Write Blocks successfully.
1128 @retval EFI_DEVICE_ERROR Write Blocks failed.
1129 @retval EFI_NO_MEDIA There is no media in the device.
1130 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
1131
1132 @retval EFI_BAD_BUFFER_SIZE
1133 The BufferSize parameter is not a multiple of the
1134 intrinsic block size of the device.
1135
1136 @retval EFI_INVALID_PARAMETER
1137 The write request contains LBAs that are not valid,
1138 or the data buffer is not valid.
1139
1140 @note
1141 If Write Block error because of device error, this function will call
1142 AtaSoftReset() function to reset device.
1143
1144 **/
1145 EFI_STATUS
1146 AtaBlkIoWriteBlocks (
1147 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1148 IN UINT32 MediaId,
1149 IN EFI_LBA LBA,
1150 IN UINTN BufferSize,
1151 OUT VOID *Buffer
1152 )
1153 {
1154
1155 EFI_BLOCK_IO_MEDIA *Media;
1156 UINTN BlockSize;
1157 UINTN NumberOfBlocks;
1158 EFI_STATUS Status;
1159
1160 if (Buffer == NULL) {
1161 return EFI_INVALID_PARAMETER;
1162 }
1163
1164 if (BufferSize == 0) {
1165 return EFI_SUCCESS;
1166 }
1167
1168 Status = EFI_SUCCESS;
1169
1170 //
1171 // Get the intrinsic block size
1172 //
1173 Media = IdeBlkIoDevice->BlkIo.Media;
1174 BlockSize = Media->BlockSize;
1175 NumberOfBlocks = BufferSize / BlockSize;
1176
1177 if (MediaId != Media->MediaId) {
1178 return EFI_MEDIA_CHANGED;
1179 }
1180
1181 if (BufferSize % BlockSize != 0) {
1182 return EFI_BAD_BUFFER_SIZE;
1183 }
1184
1185 if (LBA > Media->LastBlock) {
1186 return EFI_INVALID_PARAMETER;
1187 }
1188
1189 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1190 return EFI_INVALID_PARAMETER;
1191 }
1192
1193 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1194 return EFI_INVALID_PARAMETER;
1195 }
1196
1197 Status = EFI_SUCCESS;
1198 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) {
1199 //
1200 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 write block mechanism
1201 //
1202 if (IdeBlkIoDevice->UdmaMode.Valid) {
1203 Status = AtaUdmaWriteExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1204 } else {
1205 Status = AtaWriteSectorsExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1206 }
1207 } else {
1208 //
1209 // For ATA-3 compatible device, use ATA-3 write block mechanism
1210 //
1211 if (IdeBlkIoDevice->UdmaMode.Valid) {
1212 Status = AtaUdmaWrite (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1213 } else {
1214 Status = AtaWriteSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1215 }
1216 }
1217
1218 if (EFI_ERROR (Status)) {
1219 AtaSoftReset (IdeBlkIoDevice);
1220 return EFI_DEVICE_ERROR;
1221 }
1222
1223 return EFI_SUCCESS;
1224 }
1225
1226 /**
1227 This function is called by the AtaBlkIoReadBlocks() to perform
1228 reading from media in block unit. The function has been enhanced to
1229 support >120GB access and transfer at most 65536 blocks per command
1230
1231 @param[in] *IdeDev
1232 pointer pointing to IDE_BLK_IO_DEV data structure, used
1233 to record all the information of the IDE device.
1234
1235 @param[in] *DataBuffer A pointer to the destination buffer for the data.
1236 @param[in] StartLba The starting logical block address to read from
1237 on the device media.
1238 @param[in] NumberOfBlocks The number of transfer data blocks.
1239
1240 @return return status is fully dependent on the return status
1241 of AtaPioDataInExt() function.
1242
1243 **/
1244 EFI_STATUS
1245 AtaReadSectorsExt (
1246 IN IDE_BLK_IO_DEV *IdeDev,
1247 IN VOID *DataBuffer,
1248 IN EFI_LBA StartLba,
1249 IN UINTN NumberOfBlocks
1250 )
1251 {
1252 EFI_STATUS Status;
1253 UINTN BlocksRemaining;
1254 EFI_LBA Lba64;
1255 UINT8 AtaCommand;
1256 UINT16 SectorCount;
1257 UINT32 ByteCount;
1258 VOID *Buffer;
1259
1260 //
1261 // Using ATA "Read Sectors Ext" command(opcode=0x24) with PIO DATA IN protocol
1262 //
1263 AtaCommand = READ_SECTORS_EXT_CMD;
1264 Buffer = DataBuffer;
1265 BlocksRemaining = NumberOfBlocks;
1266 Lba64 = StartLba;
1267 Status = EFI_SUCCESS;
1268
1269 while (BlocksRemaining > 0) {
1270
1271 if (BlocksRemaining >= 0x10000) {
1272 //
1273 // SectorCount is used to record the number of sectors to be read
1274 // Max 65536 sectors can be transfered at a time.
1275 //
1276 SectorCount = 0xffff;
1277 } else {
1278 SectorCount = (UINT16) BlocksRemaining;
1279 }
1280
1281 //
1282 // ByteCount is the number of bytes that will be read
1283 //
1284 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
1285
1286 //
1287 // call AtaPioDataInExt() to send Read Sector Command and receive data read
1288 //
1289 Status = AtaPioDataInExt (
1290 IdeDev,
1291 Buffer,
1292 ByteCount,
1293 AtaCommand,
1294 Lba64,
1295 SectorCount
1296 );
1297 if (EFI_ERROR (Status)) {
1298 return Status;
1299 }
1300
1301 Lba64 += SectorCount;
1302 Buffer = ((UINT8 *) Buffer + ByteCount);
1303 BlocksRemaining -= SectorCount;
1304 }
1305
1306 return Status;
1307 }
1308
1309 /**
1310 This function is called by the AtaBlkIoWriteBlocks() to perform
1311 writing onto media in block unit. The function has been enhanced to
1312 support >120GB access and transfer at most 65536 blocks per command
1313
1314 @param[in] *IdeDev
1315 pointer pointing to IDE_BLK_IO_DEV data structure,used
1316 to record all the information of the IDE device.
1317
1318 @param[in] *DataBuffer
1319 A pointer to the source buffer for the data.
1320
1321 @param[in] Lba
1322 The starting logical block address to write onto
1323 the device media.
1324
1325 @param[in] NumberOfBlocks
1326 The number of transfer data blocks.
1327
1328 @return status is fully dependent on the return status
1329 of AtaPioDataOutExt() function.
1330
1331 **/
1332 EFI_STATUS
1333 AtaWriteSectorsExt (
1334 IN IDE_BLK_IO_DEV *IdeDev,
1335 IN VOID *DataBuffer,
1336 IN EFI_LBA StartLba,
1337 IN UINTN NumberOfBlocks
1338 )
1339 {
1340 EFI_STATUS Status;
1341 EFI_LBA Lba64;
1342 UINTN BlocksRemaining;
1343 UINT8 AtaCommand;
1344 UINT16 SectorCount;
1345 UINT32 ByteCount;
1346 VOID *Buffer;
1347
1348 //
1349 // Using ATA "Write Sectors Ext" cmd(opcode=0x24) with PIO DATA OUT protocol
1350 //
1351 AtaCommand = WRITE_SECTORS_EXT_CMD;
1352 Lba64 = StartLba;
1353 Buffer = DataBuffer;
1354 BlocksRemaining = NumberOfBlocks;
1355
1356 Status = EFI_SUCCESS;
1357
1358 while (BlocksRemaining > 0) {
1359
1360 if (BlocksRemaining >= 0x10000) {
1361 //
1362 // SectorCount is used to record the number of sectors to be written.
1363 // Max 65536 sectors can be transfered at a time.
1364 //
1365 SectorCount = 0xffff;
1366 } else {
1367 SectorCount = (UINT16) BlocksRemaining;
1368 }
1369
1370 //
1371 // ByteCount is the number of bytes that will be written
1372 //
1373 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
1374
1375 //
1376 // Call AtaPioDataOutExt() to send "Write Sectors Ext" Command
1377 //
1378 Status = AtaPioDataOutExt (
1379 IdeDev,
1380 Buffer,
1381 ByteCount,
1382 AtaCommand,
1383 Lba64,
1384 SectorCount
1385 );
1386 if (EFI_ERROR (Status)) {
1387 return Status;
1388 }
1389
1390 Lba64 += SectorCount;
1391 Buffer = ((UINT8 *) Buffer + ByteCount);
1392 BlocksRemaining -= SectorCount;
1393 }
1394
1395 return Status;
1396 }
1397
1398 /**
1399 This function is used to send out ATA commands conforms to the
1400 PIO Data In Protocol, supporting ATA/ATAPI-6 standard
1401
1402 Comparing with ATA-3 data in protocol, we have two differents here:<BR>
1403 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1404 wait will frequently fail... cause writing function return error)
1405
1406 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1407 slow down writing performance by 100 times!)
1408
1409 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
1410 to record all the information of the IDE device.
1411
1412 @param[in,out] *Buffer buffer contained data transferred from device to host.
1413 @param[in] ByteCount data size in byte unit of the buffer.
1414 @param[in] AtaCommand value of the Command Register
1415 @param[in] StartLba the start LBA of this transaction
1416 @param[in] SectorCount the count of sectors to be transfered
1417
1418 @retval EFI_SUCCESS send out the ATA command and device send required
1419 data successfully.
1420
1421 @retval EFI_DEVICE_ERROR command sent failed.
1422
1423 **/
1424 EFI_STATUS
1425 AtaPioDataInExt (
1426 IN IDE_BLK_IO_DEV *IdeDev,
1427 IN OUT VOID *Buffer,
1428 IN UINT32 ByteCount,
1429 IN UINT8 AtaCommand,
1430 IN EFI_LBA StartLba,
1431 IN UINT16 SectorCount
1432 )
1433 {
1434 UINT8 DevSel;
1435 UINT8 SectorCount8;
1436 UINT8 LbaLow;
1437 UINT8 LbaMid;
1438 UINT8 LbaHigh;
1439 UINTN WordCount;
1440 UINTN Increment;
1441 UINT16 *Buffer16;
1442 EFI_STATUS Status;
1443
1444 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1445 if (EFI_ERROR (Status)) {
1446 return EFI_DEVICE_ERROR;
1447 }
1448
1449 //
1450 // Select device, set bit6 as 1 to indicate LBA mode is used
1451 //
1452 DevSel = (UINT8) (IdeDev->Device << 4);
1453 DevSel |= 0x40;
1454 IDEWritePortB (
1455 IdeDev->PciIo,
1456 IdeDev->IoPort->Head,
1457 DevSel
1458 );
1459
1460 //
1461 // Wait for DRDY singnal asserting. ATAPI device needn't wait
1462 //
1463 if ( (IdeDev->Type == IdeHardDisk) ||
1464 (IdeDev->Type == Ide48bitAddressingHardDisk)) {
1465
1466 Status = DRDYReady (IdeDev, ATATIMEOUT);
1467 if (EFI_ERROR (Status)) {
1468 return EFI_DEVICE_ERROR;
1469 }
1470 }
1471
1472 //
1473 // Fill feature register if needed
1474 //
1475 if (AtaCommand == SET_FEATURES_CMD) {
1476 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
1477 }
1478
1479 //
1480 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1481 //
1482 SectorCount8 = (UINT8) (SectorCount >> 8);
1483 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1484
1485 SectorCount8 = (UINT8) SectorCount;
1486 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1487
1488 //
1489 // Fill the start LBA registers, which are also two-byte FIFO
1490 //
1491 LbaLow = (UINT8) RShiftU64 (StartLba, 24);
1492 LbaMid = (UINT8) RShiftU64 (StartLba, 32);
1493 LbaHigh = (UINT8) RShiftU64 (StartLba, 40);
1494 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1495 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1496 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1497
1498 LbaLow = (UINT8) StartLba;
1499 LbaMid = (UINT8) RShiftU64 (StartLba, 8);
1500 LbaHigh = (UINT8) RShiftU64 (StartLba, 16);
1501 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1502 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1503 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1504
1505 //
1506 // Send command via Command Register, invoking the processing of this command
1507 //
1508 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1509
1510 Buffer16 = (UINT16 *) Buffer;
1511
1512 //
1513 // According to PIO data in protocol, host can perform a series of reads to
1514 // the data register after each time device set DRQ ready;
1515 //
1516
1517 //
1518 // 256 words
1519 //
1520 Increment = 256;
1521
1522 //
1523 // used to record bytes of currently transfered data
1524 //
1525 WordCount = 0;
1526
1527 while (WordCount < ByteCount / 2) {
1528 //
1529 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1530 //
1531 Status = DRQReady2 (IdeDev, ATATIMEOUT);
1532 if (EFI_ERROR (Status)) {
1533 return EFI_DEVICE_ERROR;
1534 }
1535
1536 Status = CheckErrorStatus (IdeDev);
1537 if (EFI_ERROR (Status)) {
1538 return EFI_DEVICE_ERROR;
1539 }
1540
1541 //
1542 // Get the byte count for one series of read
1543 //
1544 if ((WordCount + Increment) > ByteCount / 2) {
1545 Increment = ByteCount / 2 - WordCount;
1546 }
1547
1548 IDEReadPortWMultiple (
1549 IdeDev->PciIo,
1550 IdeDev->IoPort->Data,
1551 Increment,
1552 Buffer16
1553 );
1554
1555 WordCount += Increment;
1556 Buffer16 += Increment;
1557
1558 }
1559
1560 return CheckErrorStatus (IdeDev);
1561 }
1562
1563 /**
1564 This function is used to send out ATA commands conforms to the
1565 PIO Data Out Protocol, supporting ATA/ATAPI-6 standard
1566
1567 Comparing with ATA-3 data out protocol, we have two differents here:<BR>
1568 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1569 wait will frequently fail... cause writing function return error)
1570
1571 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1572 slow down writing performance by 100 times!)
1573
1574 @param[in] *IdeDev
1575 pointer pointing to IDE_BLK_IO_DEV data structure, used
1576 to record all the information of the IDE device.
1577
1578 @param[in] *Buffer buffer contained data transferred from host to device.
1579 @param[in] ByteCount data size in byte unit of the buffer.
1580 @param[in] AtaCommand value of the Command Register
1581 @param[in] StartLba the start LBA of this transaction
1582 @param[in] SectorCount the count of sectors to be transfered
1583
1584 @retval EFI_SUCCESS send out the ATA command and device receive required
1585 data successfully.
1586
1587 @retval EFI_DEVICE_ERROR command sent failed.
1588
1589 **/
1590 EFI_STATUS
1591 AtaPioDataOutExt (
1592 IN IDE_BLK_IO_DEV *IdeDev,
1593 IN VOID *Buffer,
1594 IN UINT32 ByteCount,
1595 IN UINT8 AtaCommand,
1596 IN EFI_LBA StartLba,
1597 IN UINT16 SectorCount
1598 )
1599 {
1600 UINT8 DevSel;
1601 UINT8 SectorCount8;
1602 UINT8 LbaLow;
1603 UINT8 LbaMid;
1604 UINT8 LbaHigh;
1605 UINTN WordCount;
1606 UINTN Increment;
1607 UINT16 *Buffer16;
1608 EFI_STATUS Status;
1609
1610 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1611 if (EFI_ERROR (Status)) {
1612 return EFI_DEVICE_ERROR;
1613 }
1614
1615 //
1616 // Select device. Set bit6 as 1 to indicate LBA mode is used
1617 //
1618 DevSel = (UINT8) (IdeDev->Device << 4);
1619 DevSel |= 0x40;
1620 IDEWritePortB (
1621 IdeDev->PciIo,
1622 IdeDev->IoPort->Head,
1623 DevSel
1624 );
1625
1626 //
1627 // Wait for DRDY singnal asserting.
1628 //
1629 Status = DRDYReady (IdeDev, ATATIMEOUT);
1630 if (EFI_ERROR (Status)) {
1631 return EFI_DEVICE_ERROR;
1632 }
1633
1634 //
1635 // Fill feature register if needed
1636 //
1637 if (AtaCommand == SET_FEATURES_CMD) {
1638 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
1639 }
1640
1641 //
1642 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1643 //
1644 SectorCount8 = (UINT8) (SectorCount >> 8);
1645 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1646
1647 SectorCount8 = (UINT8) SectorCount;
1648 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1649
1650 //
1651 // Fill the start LBA registers, which are also two-byte FIFO
1652 //
1653 LbaLow = (UINT8) RShiftU64 (StartLba, 24);
1654 LbaMid = (UINT8) RShiftU64 (StartLba, 32);
1655 LbaHigh = (UINT8) RShiftU64 (StartLba, 40);
1656 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1657 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1658 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1659
1660 LbaLow = (UINT8) StartLba;
1661 LbaMid = (UINT8) RShiftU64 (StartLba, 8);
1662 LbaHigh = (UINT8) RShiftU64 (StartLba, 16);
1663 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1664 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1665 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1666
1667 //
1668 // Send command via Command Register, invoking the processing of this command
1669 //
1670 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1671
1672 Buffer16 = (UINT16 *) Buffer;
1673
1674 //
1675 // According to PIO Data Out protocol, host can perform a series of writes to
1676 // the data register after each time device set DRQ ready;
1677 //
1678 Increment = 256;
1679
1680 //
1681 // used to record bytes of currently transfered data
1682 //
1683 WordCount = 0;
1684
1685 while (WordCount < ByteCount / 2) {
1686 //
1687 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1688 //
1689 Status = DRQReady2 (IdeDev, ATATIMEOUT);
1690 if (EFI_ERROR (Status)) {
1691 return EFI_DEVICE_ERROR;
1692 }
1693
1694 Status = CheckErrorStatus (IdeDev);
1695 if (EFI_ERROR (Status)) {
1696 return EFI_DEVICE_ERROR;
1697 }
1698
1699 //
1700 // Write data into device by one series of writing to data register
1701 //
1702 if ((WordCount + Increment) > ByteCount / 2) {
1703 Increment = ByteCount / 2 - WordCount;
1704 }
1705
1706 IDEWritePortWMultiple (
1707 IdeDev->PciIo,
1708 IdeDev->IoPort->Data,
1709 Increment,
1710 Buffer16
1711 );
1712
1713 WordCount += Increment;
1714 Buffer16 += Increment;
1715
1716 }
1717 //
1718 // while
1719 //
1720
1721 return CheckErrorStatus (IdeDev);
1722 }
1723
1724
1725 /**
1726 Enable SMART of the disk if supported
1727
1728 @param[in] *IdeDev
1729 pointer pointing to IDE_BLK_IO_DEV data structure,used
1730 to record all the information of the IDE device.
1731
1732 **/
1733 VOID
1734 AtaSMARTSupport (
1735 IN IDE_BLK_IO_DEV *IdeDev
1736 )
1737 {
1738 EFI_STATUS Status;
1739 BOOLEAN SMARTSupported;
1740 UINT8 Device;
1741 EFI_IDENTIFY_DATA *TmpAtaIdentifyPointer;
1742 UINT8 DeviceSelect;
1743 UINT8 LBAMid;
1744 UINT8 LBAHigh;
1745
1746 //
1747 // Detect if the device supports S.M.A.R.T.
1748 //
1749 if ((IdeDev->pIdData->AtaData.command_set_supported_83 & 0xc000) != 0x4000) {
1750 //
1751 // Data in word 82 is not valid (bit15 shall be zero and bit14 shall be to one)
1752 //
1753 return ;
1754 } else {
1755 if ((IdeDev->pIdData->AtaData.command_set_supported_82 & 0x0001) != 0x0001) {
1756 //
1757 // S.M.A.R.T is not supported by the device
1758 //
1759 SMARTSupported = FALSE;
1760 } else {
1761 SMARTSupported = TRUE;
1762 }
1763 }
1764
1765 if (!SMARTSupported) {
1766 //
1767 // Report nonsupport status code
1768 //
1769 REPORT_STATUS_CODE (
1770 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1771 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_NOTSUPPORTED)
1772 );
1773 } else {
1774 //
1775 // Enable this feature
1776 //
1777 REPORT_STATUS_CODE (
1778 EFI_PROGRESS_CODE,
1779 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_ENABLE)
1780 );
1781
1782 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
1783 Status = AtaNonDataCommandIn (
1784 IdeDev,
1785 ATA_SMART_CMD,
1786 Device,
1787 ATA_SMART_ENABLE_OPERATION,
1788 0,
1789 0,
1790 ATA_CONSTANT_4F,
1791 ATA_CONSTANT_C2
1792 );
1793 //
1794 // Detect if this feature is enabled
1795 //
1796 TmpAtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA));
1797
1798 DeviceSelect = (UINT8) ((IdeDev->Device) << 4);
1799 Status = AtaPioDataIn (
1800 IdeDev,
1801 (VOID *) TmpAtaIdentifyPointer,
1802 sizeof (EFI_IDENTIFY_DATA),
1803 IDENTIFY_DRIVE_CMD,
1804 DeviceSelect,
1805 0,
1806 0,
1807 0,
1808 0
1809 );
1810 if (EFI_ERROR (Status)) {
1811 gBS->FreePool (TmpAtaIdentifyPointer);
1812 return ;
1813 }
1814
1815 //
1816 // Check if the feature is enabled
1817 //
1818 if ((TmpAtaIdentifyPointer->AtaData.command_set_feature_enb_85 & 0x0001) == 0x0001) {
1819 //
1820 // Read status data
1821 //
1822 AtaNonDataCommandIn (
1823 IdeDev,
1824 ATA_SMART_CMD,
1825 Device,
1826 ATA_SMART_RETURN_STATUS,
1827 0,
1828 0,
1829 ATA_CONSTANT_4F,
1830 ATA_CONSTANT_C2
1831 );
1832 LBAMid = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb);
1833 LBAHigh = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb);
1834
1835 if ((LBAMid == 0x4f) && (LBAHigh == 0xc2)) {
1836 //
1837 // The threshold exceeded condition is not detected by the device
1838 //
1839 REPORT_STATUS_CODE (
1840 EFI_PROGRESS_CODE,
1841 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_UNDERTHRESHOLD)
1842 );
1843
1844 } else if ((LBAMid == 0xf4) && (LBAHigh == 0x2c)) {
1845 //
1846 // The threshold exceeded condition is detected by the device
1847 //
1848 REPORT_STATUS_CODE (
1849 EFI_PROGRESS_CODE,
1850 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_OVERTHRESHOLD)
1851 );
1852 }
1853
1854 } else {
1855 //
1856 // Report disabled status code
1857 //
1858 REPORT_STATUS_CODE (
1859 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1860 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_DISABLED)
1861 );
1862 }
1863
1864 gBS->FreePool (TmpAtaIdentifyPointer);
1865 }
1866
1867 return ;
1868 }
1869
1870 /**
1871 Send ATA Ext command into device with NON_DATA protocol
1872
1873 @param IdeDev Standard IDE device private data structure
1874 @param AtaCommand The ATA command to be sent
1875 @param Device The value in Device register
1876 @param Feature The value in Feature register
1877 @param SectorCount The value in SectorCount register
1878 @param LbaAddress The LBA address in 48-bit mode
1879
1880 @retval EFI_SUCCESS Reading succeed
1881 @retval EFI_DEVICE_ERROR Error executing commands on this device
1882
1883 **/
1884 EFI_STATUS
1885 AtaCommandIssueExt (
1886 IN IDE_BLK_IO_DEV *IdeDev,
1887 IN UINT8 AtaCommand,
1888 IN UINT8 Device,
1889 IN UINT16 Feature,
1890 IN UINT16 SectorCount,
1891 IN EFI_LBA LbaAddress
1892 )
1893 {
1894 EFI_STATUS Status;
1895 UINT8 SectorCount8;
1896 UINT8 Feature8;
1897 UINT8 LbaLow;
1898 UINT8 LbaMid;
1899 UINT8 LbaHigh;
1900
1901 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1902 if (EFI_ERROR (Status)) {
1903 return EFI_DEVICE_ERROR;
1904 }
1905
1906 //
1907 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1908 //
1909 IDEWritePortB (
1910 IdeDev->PciIo,
1911 IdeDev->IoPort->Head,
1912 (UINT8) ((IdeDev->Device << 4) | 0xe0)
1913 );
1914
1915 //
1916 // ATA commands for ATA device must be issued when DRDY is set
1917 //
1918 Status = DRDYReady (IdeDev, ATATIMEOUT);
1919 if (EFI_ERROR (Status)) {
1920 return EFI_DEVICE_ERROR;
1921 }
1922
1923 //
1924 // Pass parameter into device register block
1925 //
1926 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
1927
1928 //
1929 // Fill the feature register, which is a two-byte FIFO. Need write twice.
1930 //
1931 Feature8 = (UINT8) (Feature >> 8);
1932 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1933
1934 Feature8 = (UINT8) Feature;
1935 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1936
1937 //
1938 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1939 //
1940 SectorCount8 = (UINT8) (SectorCount >> 8);
1941 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1942
1943 SectorCount8 = (UINT8) SectorCount;
1944 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1945
1946 //
1947 // Fill the start LBA registers, which are also two-byte FIFO
1948 //
1949 LbaLow = (UINT8) RShiftU64 (LbaAddress, 24);
1950 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1951 LbaLow = (UINT8) LbaAddress;
1952 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1953
1954 LbaMid = (UINT8) RShiftU64 (LbaAddress, 32);
1955 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1956 LbaMid = (UINT8) RShiftU64 (LbaAddress, 8);
1957 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1958
1959 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 40);
1960 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1961 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 16);
1962 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1963
1964 //
1965 // Work around for Segate 160G disk writing
1966 //
1967 gBS->Stall (1800);
1968
1969 //
1970 // Send command via Command Register
1971 //
1972 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1973
1974 //
1975 // Stall at least 400ns
1976 //
1977 gBS->Stall (100);
1978
1979 return EFI_SUCCESS;
1980 }
1981
1982 /**
1983 Send ATA Ext command into device with NON_DATA protocol
1984
1985 @param IdeDev Standard IDE device private data structure
1986 @param AtaCommand The ATA command to be sent
1987 @param Device The value in Device register
1988 @param Feature The value in Feature register
1989 @param SectorCount The value in SectorCount register
1990 @param LbaAddress The LBA address in 48-bit mode
1991
1992 @retval EFI_SUCCESS Reading succeed
1993 @retval EFI_DEVICE_ERROR Error executing commands on this device
1994
1995 **/
1996 EFI_STATUS
1997 AtaCommandIssue (
1998 IN IDE_BLK_IO_DEV *IdeDev,
1999 IN UINT8 AtaCommand,
2000 IN UINT8 Device,
2001 IN UINT16 Feature,
2002 IN UINT16 SectorCount,
2003 IN EFI_LBA LbaAddress
2004 )
2005 {
2006 EFI_STATUS Status;
2007 UINT8 SectorCount8;
2008 UINT8 Feature8;
2009 UINT8 Lba0;
2010 UINT8 Lba1;
2011 UINT8 Lba2;
2012 UINT8 Lba3;
2013
2014 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
2015 if (EFI_ERROR (Status)) {
2016 return EFI_DEVICE_ERROR;
2017 }
2018
2019 //
2020 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
2021 //
2022 IDEWritePortB (
2023 IdeDev->PciIo,
2024 IdeDev->IoPort->Head,
2025 (UINT8) ((IdeDev->Device << 4) | 0xe0)
2026 );
2027
2028 //
2029 // ATA commands for ATA device must be issued when DRDY is set
2030 //
2031 Status = DRDYReady (IdeDev, ATATIMEOUT);
2032 if (EFI_ERROR (Status)) {
2033 return EFI_DEVICE_ERROR;
2034 }
2035
2036 Lba0 = (UINT8) LbaAddress;
2037 Lba1 = (UINT8) RShiftU64 (LbaAddress, 8);
2038 Lba2 = (UINT8) RShiftU64 (LbaAddress, 16);
2039 Lba3 = (UINT8) RShiftU64 (LbaAddress, 24);
2040 Device = (UINT8) (Device | Lba3);
2041
2042 //
2043 // Pass parameter into device register block
2044 //
2045 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2046
2047 //
2048 // Fill the feature register, which is a two-byte FIFO. Need write twice.
2049 //
2050 Feature8 = (UINT8) Feature;
2051 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
2052
2053 //
2054 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
2055 //
2056 SectorCount8 = (UINT8) SectorCount;
2057 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
2058
2059 //
2060 // Fill the start LBA registers, which are also two-byte FIFO
2061 //
2062
2063 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, Lba0);
2064 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, Lba1);
2065 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, Lba2);
2066
2067 //
2068 // Send command via Command Register
2069 //
2070 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
2071
2072 //
2073 // Stall at least 400ns
2074 //
2075 gBS->Stall (100);
2076
2077 return EFI_SUCCESS;
2078 }
2079
2080 /**
2081 This function is called by the AtaBlkIoReadBlocks() to perform
2082 reading from media in block unit. The function has been enhanced to
2083 support >120GB access and transfer at most 65536 blocks per command
2084
2085 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2086 to record all the information of the IDE device.
2087
2088 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2089
2090 @param[in] StartLba The starting logical block address to read from
2091 on the device media.
2092
2093 @param[in] NumberOfBlocks The number of transfer data blocks.
2094
2095 @return The device status of UDMA operation. If the operation is
2096 successful, return EFI_SUCCESS.
2097
2098 TODO: EFI_UNSUPPORTED - add return value to function comment
2099 TODO: EFI_DEVICE_ERROR - add return value to function comment
2100 TODO: EFI_DEVICE_ERROR - add return value to function comment
2101 TODO: EFI_DEVICE_ERROR - add return value to function comment
2102 **/
2103 EFI_STATUS
2104 AtaUdmaReadExt (
2105 IN IDE_BLK_IO_DEV *IdeDev,
2106 IN VOID *DataBuffer,
2107 IN EFI_LBA StartLba,
2108 IN UINTN NumberOfBlocks
2109 )
2110 {
2111 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaReadExtOp);
2112 }
2113
2114 /**
2115 This function is called by the AtaBlkIoReadBlocks() to perform
2116 reading from media in block unit. The function has been enhanced to
2117 support >120GB access and transfer at most 65536 blocks per command
2118
2119 @param[in] *IdeDev
2120 pointer pointing to IDE_BLK_IO_DEV data structure, used
2121 to record all the information of the IDE device.
2122
2123 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2124 @param[in] StartLba The starting logical block address to read from
2125 on the device media.
2126 @param[in] NumberOfBlocks The number of transfer data blocks.
2127
2128 @return The device status of UDMA operation. If the operation is
2129 successful, return EFI_SUCCESS.
2130
2131 TODO: EFI_UNSUPPORTED - add return value to function comment
2132 TODO: EFI_DEVICE_ERROR - add return value to function comment
2133 TODO: EFI_DEVICE_ERROR - add return value to function comment
2134 TODO: EFI_DEVICE_ERROR - add return value to function comment
2135 **/
2136 EFI_STATUS
2137 AtaUdmaRead (
2138 IN IDE_BLK_IO_DEV *IdeDev,
2139 IN VOID *DataBuffer,
2140 IN EFI_LBA StartLba,
2141 IN UINTN NumberOfBlocks
2142 )
2143 {
2144 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaReadOp);
2145 }
2146
2147 /**
2148 This function is called by the AtaBlkIoWriteBlocks() to perform
2149 writing to media in block unit. The function has been enhanced to
2150 support >120GB access and transfer at most 65536 blocks per command
2151
2152 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2153 to record all the information of the IDE device.
2154
2155 @param[in] *DataBuffer A pointer to the source buffer for the data.
2156
2157 @param[in] StartLba The starting logical block address to write to
2158 on the device media.
2159
2160 @param[in] NumberOfBlocks The number of transfer data blocks.
2161
2162 @return The device status of UDMA operation. If the operation is
2163 successful, return EFI_SUCCESS.
2164
2165 TODO: EFI_UNSUPPORTED - add return value to function comment
2166 TODO: EFI_DEVICE_ERROR - add return value to function comment
2167 TODO: EFI_DEVICE_ERROR - add return value to function comment
2168 **/
2169 EFI_STATUS
2170 AtaUdmaWriteExt (
2171 IN IDE_BLK_IO_DEV *IdeDev,
2172 IN VOID *DataBuffer,
2173 IN EFI_LBA StartLba,
2174 IN UINTN NumberOfBlocks
2175 )
2176 {
2177 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaWriteExtOp);
2178 }
2179
2180 /**
2181 This function is called by the AtaBlkIoWriteBlocks() to perform
2182 writing to media in block unit. The function has been enhanced to
2183 support >120GB access and transfer at most 65536 blocks per command
2184
2185 @param[in] *IdeDev
2186 pointer pointing to IDE_BLK_IO_DEV data structure, used
2187 to record all the information of the IDE device.
2188
2189 @param[in] *DataBuffer
2190 A pointer to the source buffer for the data.
2191
2192 @param[in] StartLba
2193 The starting logical block address to write to
2194 on the device media.
2195
2196 @param[in] NumberOfBlocks
2197 The number of transfer data blocks.
2198
2199 @return The device status of UDMA operation. If the operation is
2200 successful, return EFI_SUCCESS.
2201
2202 TODO: EFI_UNSUPPORTED - add return value to function comment
2203 TODO: EFI_DEVICE_ERROR - add return value to function comment
2204 TODO: EFI_DEVICE_ERROR - add return value to function comment
2205 **/
2206 EFI_STATUS
2207 AtaUdmaWrite (
2208 IN IDE_BLK_IO_DEV *IdeDev,
2209 IN VOID *DataBuffer,
2210 IN EFI_LBA StartLba,
2211 IN UINTN NumberOfBlocks
2212 )
2213 {
2214 return DoAtaUdma (IdeDev, DataBuffer, StartLba, NumberOfBlocks, AtaUdmaWriteOp);
2215 }
2216
2217 /**
2218 Perform an ATA Udma operation (Read, ReadExt, Write, WriteExt).
2219
2220 @param[in] *IdeDev
2221 pointer pointing to IDE_BLK_IO_DEV data structure, used
2222 to record all the information of the IDE device.
2223
2224 @param[in] *DataBuffer
2225 A pointer to the source buffer for the data.
2226
2227 @param[in] StartLba
2228 The starting logical block address to write to
2229 on the device media.
2230
2231 @param[in] NumberOfBlocks
2232 The number of transfer data blocks.
2233
2234 @param[in] UdmaOp
2235 The perform operations could be AtaUdmaReadOp, AtaUdmaReadExOp,
2236 AtaUdmaWriteOp, AtaUdmaWriteExOp
2237
2238 @return The device status of UDMA operation. If the operation is
2239 successful, return EFI_SUCCESS.
2240
2241 **/
2242 EFI_STATUS
2243 DoAtaUdma (
2244 IN IDE_BLK_IO_DEV *IdeDev,
2245 IN VOID *DataBuffer,
2246 IN EFI_LBA StartLba,
2247 IN UINTN NumberOfBlocks,
2248 IN ATA_UDMA_OPERATION UdmaOp
2249 )
2250 {
2251 IDE_DMA_PRD *PrdAddr;
2252 IDE_DMA_PRD *UsedPrdAddr;
2253 IDE_DMA_PRD *TempPrdAddr;
2254 UINT8 RegisterValue;
2255 UINT8 Device;
2256 UINT64 IoPortForBmic;
2257 UINT64 IoPortForBmis;
2258 UINT64 IoPortForBmid;
2259 EFI_STATUS Status;
2260 UINTN PrdTableNum;
2261 UINTN ByteCount;
2262 UINTN ByteAvailable;
2263 UINT8 *PrdBuffer;
2264 UINTN RemainBlockNum;
2265 UINT8 DeviceControl;
2266 UINT32 Count;
2267 UINTN PageCount;
2268 VOID *Map;
2269 VOID *MemPage;
2270 EFI_PHYSICAL_ADDRESS DeviceAddress;
2271 UINTN MaxDmaCommandSectors;
2272 EFI_PCI_IO_PROTOCOL_OPERATION PciIoProtocolOp;
2273 UINT8 AtaCommand;
2274
2275 switch (UdmaOp) {
2276 case AtaUdmaReadOp:
2277 MaxDmaCommandSectors = MAX_DMA_COMMAND_SECTORS;
2278 PciIoProtocolOp = EfiPciIoOperationBusMasterWrite;
2279 AtaCommand = READ_DMA_CMD;
2280 break;
2281 case AtaUdmaReadExtOp:
2282 MaxDmaCommandSectors = MAX_DMA_EXT_COMMAND_SECTORS;
2283 PciIoProtocolOp = EfiPciIoOperationBusMasterWrite;
2284 AtaCommand = READ_DMA_EXT_CMD;
2285 break;
2286 case AtaUdmaWriteOp:
2287 MaxDmaCommandSectors = MAX_DMA_COMMAND_SECTORS;
2288 PciIoProtocolOp = EfiPciIoOperationBusMasterRead;
2289 AtaCommand = WRITE_DMA_CMD;
2290 break;
2291 case AtaUdmaWriteExtOp:
2292 MaxDmaCommandSectors = MAX_DMA_EXT_COMMAND_SECTORS;
2293 PciIoProtocolOp = EfiPciIoOperationBusMasterRead;
2294 AtaCommand = WRITE_DMA_EXT_CMD;
2295 break;
2296 default:
2297 return EFI_UNSUPPORTED;
2298 break;
2299 }
2300
2301 //
2302 // Channel and device differential
2303 //
2304 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
2305
2306 //
2307 // Enable interrupt to support UDMA and Select device
2308 //
2309 DeviceControl = 0;
2310 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2311
2312 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2313
2314 if (IdePrimary == IdeDev->Channel) {
2315 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICP_OFFSET;
2316 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISP_OFFSET;
2317 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDP_OFFSET;
2318 } else {
2319 if (IdeSecondary == IdeDev->Channel) {
2320 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICS_OFFSET;
2321 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISS_OFFSET;
2322 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDS_OFFSET;
2323 } else {
2324 return EFI_UNSUPPORTED;
2325 }
2326 }
2327
2328 RemainBlockNum = NumberOfBlocks;
2329 while (RemainBlockNum > 0) {
2330
2331 if (RemainBlockNum >= MaxDmaCommandSectors) {
2332 //
2333 // SectorCount is used to record the number of sectors to be read
2334 // Max 65536 sectors can be transfered at a time.
2335 //
2336 NumberOfBlocks = MaxDmaCommandSectors;
2337 RemainBlockNum -= MaxDmaCommandSectors;
2338 } else {
2339 NumberOfBlocks = (UINT16) RemainBlockNum;
2340 RemainBlockNum = 0;
2341 }
2342
2343 //
2344 // Calculate the number of PRD table to make sure the memory region
2345 // not cross 64K boundary
2346 //
2347 ByteCount = NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2348 PrdTableNum = ((ByteCount >> 16) + 1) + 1;
2349
2350 //
2351 // Build PRD table
2352 //
2353 PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));
2354 Status = IdeDev->PciIo->AllocateBuffer (
2355 IdeDev->PciIo,
2356 AllocateAnyPages,
2357 EfiBootServicesData,
2358 PageCount,
2359 &MemPage,
2360 0
2361 );
2362 if (EFI_ERROR (Status)) {
2363 return EFI_OUT_OF_RESOURCES;
2364 }
2365 ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));
2366
2367 PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);
2368 //
2369 // To make sure PRD is allocated in one 64K page
2370 //
2371 if (((UINTN) PrdAddr & 0x0FFFF) > (((UINTN) PrdAddr + PrdTableNum * sizeof (IDE_DMA_PRD) - 1) & 0x0FFFF)) {
2372 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x10000) & 0xFFFF0000);
2373 } else {
2374 if ((UINTN) PrdAddr & 0x03) {
2375 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x04) & 0xFFFFFFFC);
2376 } else {
2377 UsedPrdAddr = PrdAddr;
2378 }
2379 }
2380
2381 //
2382 // Build the PRD table
2383 //
2384 Status = IdeDev->PciIo->Map (
2385 IdeDev->PciIo,
2386 PciIoProtocolOp,
2387 DataBuffer,
2388 &ByteCount,
2389 &DeviceAddress,
2390 &Map
2391 );
2392 if (EFI_ERROR (Status)) {
2393 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
2394 return EFI_OUT_OF_RESOURCES;
2395 }
2396 PrdBuffer = (VOID *) ((UINTN) DeviceAddress);
2397 TempPrdAddr = UsedPrdAddr;
2398 while (TRUE) {
2399
2400 ByteAvailable = 0x10000 - ((UINTN) PrdBuffer & 0xFFFF);
2401
2402 if (ByteCount <= ByteAvailable) {
2403 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2404 TempPrdAddr->ByteCount = (UINT16) ByteCount;
2405 TempPrdAddr->EndOfTable = 0x8000;
2406 break;
2407 }
2408
2409 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2410 TempPrdAddr->ByteCount = (UINT16) ByteAvailable;
2411
2412 ByteCount -= ByteAvailable;
2413 PrdBuffer += ByteAvailable;
2414 TempPrdAddr++;
2415 }
2416
2417 //
2418 // Set the base address to BMID register
2419 //
2420 IdeDev->PciIo->Io.Write (
2421 IdeDev->PciIo,
2422 EfiPciIoWidthUint32,
2423 EFI_PCI_IO_PASS_THROUGH_BAR,
2424 IoPortForBmid,
2425 1,
2426 &UsedPrdAddr
2427 );
2428
2429 //
2430 // Set BMIC register to identify the operation direction
2431 //
2432 IdeDev->PciIo->Io.Read (
2433 IdeDev->PciIo,
2434 EfiPciIoWidthUint8,
2435 EFI_PCI_IO_PASS_THROUGH_BAR,
2436 IoPortForBmic,
2437 1,
2438 &RegisterValue
2439 );
2440
2441 if (UdmaOp == AtaUdmaReadExtOp || UdmaOp == AtaUdmaReadOp) {
2442 RegisterValue |= BMIC_nREAD;
2443 } else {
2444 RegisterValue &= ~((UINT8) BMIC_nREAD);
2445 }
2446
2447 IdeDev->PciIo->Io.Write (
2448 IdeDev->PciIo,
2449 EfiPciIoWidthUint8,
2450 EFI_PCI_IO_PASS_THROUGH_BAR,
2451 IoPortForBmic,
2452 1,
2453 &RegisterValue
2454 );
2455
2456 //
2457 // Read BMIS register and clear ERROR and INTR bit
2458 //
2459 IdeDev->PciIo->Io.Read (
2460 IdeDev->PciIo,
2461 EfiPciIoWidthUint8,
2462 EFI_PCI_IO_PASS_THROUGH_BAR,
2463 IoPortForBmis,
2464 1,
2465 &RegisterValue
2466 );
2467
2468 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR);
2469
2470 IdeDev->PciIo->Io.Write (
2471 IdeDev->PciIo,
2472 EfiPciIoWidthUint8,
2473 EFI_PCI_IO_PASS_THROUGH_BAR,
2474 IoPortForBmis,
2475 1,
2476 &RegisterValue
2477 );
2478
2479 if (UdmaOp == AtaUdmaWriteExtOp || UdmaOp == AtaUdmaReadExtOp) {
2480 Status = AtaCommandIssueExt (
2481 IdeDev,
2482 AtaCommand,
2483 Device,
2484 0,
2485 (UINT16) NumberOfBlocks,
2486 StartLba
2487 );
2488 } else {
2489 Status = AtaCommandIssue (
2490 IdeDev,
2491 AtaCommand,
2492 Device,
2493 0,
2494 (UINT16) NumberOfBlocks,
2495 StartLba
2496 );
2497 }
2498
2499 if (EFI_ERROR (Status)) {
2500 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
2501 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2502 return EFI_DEVICE_ERROR;
2503 }
2504
2505 //
2506 // Set START bit of BMIC register
2507 //
2508 IdeDev->PciIo->Io.Read (
2509 IdeDev->PciIo,
2510 EfiPciIoWidthUint8,
2511 EFI_PCI_IO_PASS_THROUGH_BAR,
2512 IoPortForBmic,
2513 1,
2514 &RegisterValue
2515 );
2516
2517 RegisterValue |= BMIC_START;
2518
2519 IdeDev->PciIo->Io.Write (
2520 IdeDev->PciIo,
2521 EfiPciIoWidthUint8,
2522 EFI_PCI_IO_PASS_THROUGH_BAR,
2523 IoPortForBmic,
2524 1,
2525 &RegisterValue
2526 );
2527
2528 //
2529 // Check the INTERRUPT and ERROR bit of BMIS
2530 // Max transfer number of sectors for one command is 65536(32Mbyte),
2531 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
2532 // So set the variable Count to 2000, for about 2 second timeout time.
2533 //
2534 Count = 2000;
2535 while (TRUE) {
2536
2537 IdeDev->PciIo->Io.Read (
2538 IdeDev->PciIo,
2539 EfiPciIoWidthUint8,
2540 EFI_PCI_IO_PASS_THROUGH_BAR,
2541 IoPortForBmis,
2542 1,
2543 &RegisterValue
2544 );
2545 if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {
2546 if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {
2547 //
2548 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
2549 //
2550 IdeDev->PciIo->Io.Read (
2551 IdeDev->PciIo,
2552 EfiPciIoWidthUint8,
2553 EFI_PCI_IO_PASS_THROUGH_BAR,
2554 IoPortForBmic,
2555 1,
2556 &RegisterValue
2557 );
2558
2559 RegisterValue &= ~((UINT8)BMIC_START);
2560
2561 IdeDev->PciIo->Io.Write (
2562 IdeDev->PciIo,
2563 EfiPciIoWidthUint8,
2564 EFI_PCI_IO_PASS_THROUGH_BAR,
2565 IoPortForBmic,
2566 1,
2567 &RegisterValue
2568 );
2569 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
2570 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2571 return EFI_DEVICE_ERROR;
2572 }
2573 break;
2574 }
2575
2576 gBS->Stall (1000);
2577 Count --;
2578 }
2579
2580 IdeDev->PciIo->FreeBuffer (IdeDev->PciIo, PageCount, MemPage);
2581 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2582 //
2583 // Read Status Register of IDE device to clear interrupt
2584 //
2585 RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);
2586 //
2587 // Clear START bit of BMIC register
2588 //
2589 IdeDev->PciIo->Io.Read (
2590 IdeDev->PciIo,
2591 EfiPciIoWidthUint8,
2592 EFI_PCI_IO_PASS_THROUGH_BAR,
2593 IoPortForBmic,
2594 1,
2595 &RegisterValue
2596 );
2597
2598 RegisterValue &= ~((UINT8) BMIC_START);
2599
2600 IdeDev->PciIo->Io.Write (
2601 IdeDev->PciIo,
2602 EfiPciIoWidthUint8,
2603 EFI_PCI_IO_PASS_THROUGH_BAR,
2604 IoPortForBmic,
2605 1,
2606 &RegisterValue
2607 );
2608
2609 if (RegisterValue & BMIS_ERROR) {
2610 return EFI_DEVICE_ERROR;
2611 }
2612
2613 DataBuffer = (UINT8 *) DataBuffer + NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2614 StartLba += NumberOfBlocks;
2615 }
2616
2617 //
2618 // Disable interrupt of Select device
2619 //
2620 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);
2621 DeviceControl |= IEN_L;
2622 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2623
2624 return EFI_SUCCESS;
2625 }