]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Bus/Pci/IdeBus/Dxe/ata.c
a08c79cadee5d082a4346d253542760942e20175
[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 EFI_STATUS
28 AtaReadSectorsExt (
29 IN IDE_BLK_IO_DEV *IdeDev,
30 IN OUT VOID *DataBuffer,
31 IN EFI_LBA StartLba,
32 IN UINTN NumberOfBlocks
33 );
34
35 EFI_STATUS
36 AtaWriteSectorsExt (
37 IN IDE_BLK_IO_DEV *IdeDev,
38 IN VOID *DataBuffer,
39 IN EFI_LBA StartLba,
40 IN UINTN NumberOfBlocks
41 );
42
43 EFI_STATUS
44 AtaPioDataInExt (
45 IN IDE_BLK_IO_DEV *IdeDev,
46 IN OUT VOID *Buffer,
47 IN UINT32 ByteCount,
48 IN UINT8 AtaCommand,
49 IN EFI_LBA StartLba,
50 IN UINT16 SectorCount
51 );
52
53 EFI_STATUS
54 AtaPioDataOutExt (
55 IN IDE_BLK_IO_DEV *IdeDev,
56 IN VOID *Buffer,
57 IN UINT32 ByteCount,
58 IN UINT8 AtaCommand,
59 IN EFI_LBA StartLba,
60 IN UINT16 SectorCount
61 );
62
63 /**
64 Sends out an ATA Identify Command to the specified device.
65
66 This function is called by DiscoverIdeDevice() during its device
67 identification. It sends out the ATA Identify Command to the
68 specified device. Only ATA device responses to this command. If
69 the command succeeds, it returns the Identify data structure which
70 contains information about the device. This function extracts the
71 information it needs to fill the IDE_BLK_IO_DEV data structure,
72 including device type, media block size, media capacity, and etc.
73
74 @param[in] *IdeDev
75 pointer pointing to IDE_BLK_IO_DEV data structure,used
76 to record all the information of the IDE device.
77
78 @retval EFI_SUCCESS Identify ATA device successfully.
79
80 @retval EFI_DEVICE_ERROR ATA Identify Device Command failed or
81 device is not ATA device.
82
83 @note
84 parameter IdeDev will be updated in this function.
85
86 **/
87 EFI_STATUS
88 ATAIdentify (
89 IN IDE_BLK_IO_DEV *IdeDev
90 )
91 {
92 EFI_STATUS Status;
93 EFI_IDENTIFY_DATA *AtaIdentifyPointer;
94 UINT32 Capacity;
95 UINT8 DeviceSelect;
96
97 //
98 // AtaIdentifyPointer is used for accommodating returned IDENTIFY data of
99 // the ATA Identify command
100 //
101 AtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA));
102
103 //
104 // use ATA PIO Data In protocol to send ATA Identify command
105 // and receive data from device
106 //
107 DeviceSelect = 0;
108 DeviceSelect = (UINT8) ((IdeDev->Device) << 4);
109 Status = AtaPioDataIn (
110 IdeDev,
111 (VOID *) AtaIdentifyPointer,
112 sizeof (EFI_IDENTIFY_DATA),
113 IDENTIFY_DRIVE_CMD,
114 DeviceSelect,
115 0,
116 0,
117 0,
118 0
119 );
120 //
121 // If ATA Identify command succeeds, then according to the received
122 // IDENTIFY data,
123 // identify the device type ( ATA or not ).
124 // If ATA device, fill the information in IdeDev.
125 // If not ATA device, return IDE_DEVICE_ERROR
126 //
127 if (!EFI_ERROR (Status)) {
128
129 IdeDev->pIdData = AtaIdentifyPointer;
130
131 //
132 // Print ATA Module Name
133 //
134 PrintAtaModuleName (IdeDev);
135
136 //
137 // bit 15 of pAtaIdentify->config is used to identify whether device is
138 // ATA device or ATAPI device.
139 // if 0, means ATA device; if 1, means ATAPI device.
140 //
141 if ((AtaIdentifyPointer->AtaData.config & 0x8000) == 0x00) {
142 //
143 // Detect if support S.M.A.R.T. If yes, enable it as default
144 //
145 AtaSMARTSupport (IdeDev);
146
147 //
148 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device)
149 //
150 Status = AtaAtapi6Identify (IdeDev);
151 if (!EFI_ERROR (Status)) {
152 //
153 // It's a disk with >120GB capacity, initialized in AtaAtapi6Identify()
154 //
155 return EFI_SUCCESS;
156 }
157 //
158 // This is a hard disk <= 120GB capacity, treat it as normal hard disk
159 //
160 IdeDev->Type = IdeHardDisk;
161
162 //
163 // Block Media Information:
164 // Media->LogicalPartition , Media->WriteCaching will be filled
165 // in the DiscoverIdeDevcie() function.
166 //
167 IdeDev->BlkIo.Media->IoAlign = 4;
168 IdeDev->BlkIo.Media->MediaId = 1;
169 IdeDev->BlkIo.Media->RemovableMedia = FALSE;
170 IdeDev->BlkIo.Media->MediaPresent = TRUE;
171 IdeDev->BlkIo.Media->ReadOnly = FALSE;
172 IdeDev->BlkIo.Media->BlockSize = 0x200;
173
174 //
175 // Calculate device capacity
176 //
177 Capacity = ((UINT32)AtaIdentifyPointer->AtaData.user_addressable_sectors_hi << 16) |
178 AtaIdentifyPointer->AtaData.user_addressable_sectors_lo ;
179 IdeDev->BlkIo.Media->LastBlock = Capacity - 1;
180
181 return EFI_SUCCESS;
182
183 }
184 }
185
186 gBS->FreePool (AtaIdentifyPointer);
187 //
188 // Make sure the pIdData will not be freed again.
189 //
190 IdeDev->pIdData = NULL;
191
192 return EFI_DEVICE_ERROR;
193 }
194
195
196 /**
197 This function is called by ATAIdentify() to identity whether this disk
198 supports ATA/ATAPI6 48bit addressing, ie support >120G capacity
199
200 @param[in] *IdeDev
201 pointer pointing to IDE_BLK_IO_DEV data structure, used
202 to record all the information of the IDE device.
203
204 @retval EFI_SUCCESS The disk specified by IdeDev is a Atapi6 supported one
205 and 48-bit addressing must be used
206
207 @retval EFI_UNSUPPORTED The disk dosn't not support Atapi6 or it supports but
208 the capacity is below 120G, 48bit addressing is not
209 needed
210
211 @note
212 This function must be called after DEVICE_IDENTITY command has been
213 successfully returned
214
215 **/
216 EFI_STATUS
217 AtaAtapi6Identify (
218 IN IDE_BLK_IO_DEV *IdeDev
219 )
220 {
221 UINT8 Index;
222 EFI_LBA TmpLba;
223 EFI_LBA Capacity;
224 EFI_IDENTIFY_DATA *Atapi6IdentifyStruct;
225
226 if (IdeDev->pIdData == NULL) {
227 return EFI_UNSUPPORTED;
228 }
229
230 Atapi6IdentifyStruct = IdeDev->pIdData;
231
232 if ((Atapi6IdentifyStruct->AtapiData.cmd_set_support_83 & bit10) == 0) {
233 //
234 // The device dosn't support 48 bit addressing
235 //
236 return EFI_UNSUPPORTED;
237 }
238
239 //
240 // 48 bit address feature set is supported, get maximum capacity
241 //
242 Capacity = Atapi6IdentifyStruct->AtapiData.max_user_lba_for_48bit_addr[0];
243 for (Index = 1; Index < 4; Index++) {
244 //
245 // Lower byte goes first: word[100] is the lowest word, word[103] is highest
246 //
247 TmpLba = Atapi6IdentifyStruct->AtapiData.max_user_lba_for_48bit_addr[Index];
248 Capacity |= LShiftU64 (TmpLba, 16 * Index);
249 }
250
251 if (Capacity > MAX_28BIT_ADDRESSING_CAPACITY) {
252 //
253 // Capacity exceeds 120GB. 48-bit addressing is really needed
254 //
255 IdeDev->Type = Ide48bitAddressingHardDisk;
256
257 //
258 // Fill block media information:Media->LogicalPartition ,
259 // Media->WriteCaching will be filledin the DiscoverIdeDevcie() function.
260 //
261 IdeDev->BlkIo.Media->IoAlign = 4;
262 IdeDev->BlkIo.Media->MediaId = 1;
263 IdeDev->BlkIo.Media->RemovableMedia = FALSE;
264 IdeDev->BlkIo.Media->MediaPresent = TRUE;
265 IdeDev->BlkIo.Media->ReadOnly = FALSE;
266 IdeDev->BlkIo.Media->BlockSize = 0x200;
267 IdeDev->BlkIo.Media->LastBlock = Capacity - 1;
268
269 return EFI_SUCCESS;
270 }
271
272 return EFI_UNSUPPORTED;
273 }
274
275 /**
276 This function is called by ATAIdentify() or ATAPIIdentify()
277 to print device's module name.
278
279 @param[in] *IdeDev
280 pointer pointing to IDE_BLK_IO_DEV data structure, used
281 to record all the information of the IDE device.
282
283 **/
284 VOID
285 PrintAtaModuleName (
286 IN IDE_BLK_IO_DEV *IdeDev
287 )
288 {
289 if (IdeDev->pIdData == NULL) {
290 return ;
291 }
292
293 SwapStringChars (IdeDev->ModelName, IdeDev->pIdData->AtaData.ModelName, 40);
294 IdeDev->ModelName[40] = 0x00;
295 }
296
297 /**
298 This function is used to send out ATA commands conforms to the
299 PIO Data In Protocol.
300
301 @param[in] *IdeDev
302 pointer pointing to IDE_BLK_IO_DEV data structure, used
303 to record all the information of the IDE device.
304
305 @param[in] *Buffer
306 buffer contained data transferred from device to host.
307
308 @param[in] ByteCount
309 data size in byte unit of the buffer.
310
311 @param[in] AtaCommand
312 value of the Command Register
313
314 @param[in] Head
315 value of the Head/Device Register
316
317 @param[in] SectorCount
318 value of the Sector Count Register
319
320 @param[in] SectorNumber
321 value of the Sector Number Register
322
323 @param[in] CylinderLsb
324 value of the low byte of the Cylinder Register
325
326 @param[in] CylinderMsb
327 value of the high byte of the Cylinder Register
328
329 @retval EFI_SUCCESS send out the ATA command and device send required
330 data successfully.
331
332 @retval EFI_DEVICE_ERROR command sent failed.
333
334 **/
335 EFI_STATUS
336 AtaPioDataIn (
337 IN IDE_BLK_IO_DEV *IdeDev,
338 IN VOID *Buffer,
339 IN UINT32 ByteCount,
340 IN UINT8 AtaCommand,
341 IN UINT8 Head,
342 IN UINT8 SectorCount,
343 IN UINT8 SectorNumber,
344 IN UINT8 CylinderLsb,
345 IN UINT8 CylinderMsb
346 )
347 {
348 UINTN WordCount;
349 UINTN Increment;
350 UINT16 *Buffer16;
351 EFI_STATUS Status;
352
353 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
354 if (EFI_ERROR (Status)) {
355 return EFI_DEVICE_ERROR;
356 }
357
358 //
359 // e0:1110,0000-- bit7 and bit5 are reserved bits.
360 // bit6 set means LBA mode
361 //
362 IDEWritePortB (
363 IdeDev->PciIo,
364 IdeDev->IoPort->Head,
365 (UINT8) ((IdeDev->Device << 4) | 0xe0 | Head)
366 );
367
368 //
369 // All ATAPI device's ATA commands can be issued regardless of the
370 // state of the DRDY
371 //
372 if (IdeDev->Type == IdeHardDisk) {
373
374 Status = DRDYReady (IdeDev, ATATIMEOUT);
375 if (EFI_ERROR (Status)) {
376 return EFI_DEVICE_ERROR;
377 }
378 }
379 //
380 // set all the command parameters
381 // Before write to all the following registers, BSY and DRQ must be 0.
382 //
383 Status = DRQClear2 (IdeDev, ATATIMEOUT);
384 if (EFI_ERROR (Status)) {
385 return EFI_DEVICE_ERROR;
386 }
387
388 if (AtaCommand == SET_FEATURES_CMD) {
389 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
390 }
391
392 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount);
393 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, SectorNumber);
394 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, CylinderLsb);
395 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, CylinderMsb);
396
397 //
398 // send command via Command Register
399 //
400 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
401
402 Buffer16 = (UINT16 *) Buffer;
403
404 //
405 // According to PIO data in protocol, host can perform a series of reads to
406 // the data register after each time device set DRQ ready;
407 // The data size of "a series of read" is command specific.
408 // For most ATA command, data size received from device will not exceed
409 // 1 sector, hence the data size for "a series of read" can be the whole data
410 // size of one command request.
411 // For ATA command such as Read Sector command, the data size of one ATA
412 // command request is often larger than 1 sector, according to the
413 // Read Sector command, the data size of "a series of read" is exactly 1
414 // sector.
415 // Here for simplification reason, we specify the data size for
416 // "a series of read" to 1 sector (256 words) if data size of one ATA command
417 // request is larger than 256 words.
418 //
419 Increment = 256;
420
421 //
422 // used to record bytes of currently transfered data
423 //
424 WordCount = 0;
425
426 while (WordCount < ByteCount / 2) {
427 //
428 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
429 //
430 Status = DRQReady2 (IdeDev, ATATIMEOUT);
431 if (EFI_ERROR (Status)) {
432 return EFI_DEVICE_ERROR;
433 }
434
435 Status = CheckErrorStatus (IdeDev);
436 if (EFI_ERROR (Status)) {
437 return EFI_DEVICE_ERROR;
438 }
439
440 //
441 // Get the byte count for one series of read
442 //
443 if ((WordCount + Increment) > ByteCount / 2) {
444 Increment = ByteCount / 2 - WordCount;
445 }
446
447 IDEReadPortWMultiple (
448 IdeDev->PciIo,
449 IdeDev->IoPort->Data,
450 Increment,
451 Buffer16
452 );
453
454 WordCount += Increment;
455 Buffer16 += Increment;
456
457 }
458
459 DRQClear (IdeDev, ATATIMEOUT);
460
461 return CheckErrorStatus (IdeDev);
462 }
463
464 /**
465 This function is used to send out ATA commands conforms to the
466 PIO Data Out Protocol.
467
468 @param *IdeDev
469 pointer pointing to IDE_BLK_IO_DEV data structure, used
470 to record all the information of the IDE device.
471
472 @param *Buffer buffer contained data transferred from host to device.
473 @param ByteCount data size in byte unit of the buffer.
474 @param AtaCommand value of the Command Register
475 @param Head value of the Head/Device Register
476 @param SectorCount value of the Sector Count Register
477 @param SectorNumber value of the Sector Number Register
478 @param CylinderLsb value of the low byte of the Cylinder Register
479 @param CylinderMsb value of the high byte of the Cylinder Register
480
481 @retval EFI_SUCCESS send out the ATA command and device received required
482 data successfully.
483
484 @retval EFI_DEVICE_ERROR command sent failed.
485
486 **/
487 EFI_STATUS
488 AtaPioDataOut (
489 IN IDE_BLK_IO_DEV *IdeDev,
490 IN VOID *Buffer,
491 IN UINT32 ByteCount,
492 IN UINT8 AtaCommand,
493 IN UINT8 Head,
494 IN UINT8 SectorCount,
495 IN UINT8 SectorNumber,
496 IN UINT8 CylinderLsb,
497 IN UINT8 CylinderMsb
498 )
499 {
500 UINTN WordCount;
501 UINTN Increment;
502 UINT16 *Buffer16;
503 EFI_STATUS Status;
504
505 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
506 if (EFI_ERROR (Status)) {
507 return EFI_DEVICE_ERROR;
508 }
509
510 //
511 // select device via Head/Device register.
512 // Before write Head/Device register, BSY and DRQ must be 0.
513 //
514 Status = DRQClear2 (IdeDev, ATATIMEOUT);
515 if (EFI_ERROR (Status)) {
516 return EFI_DEVICE_ERROR;
517 }
518
519 //
520 // e0:1110,0000-- bit7 and bit5 are reserved bits.
521 // bit6 set means LBA mode
522 //
523 IDEWritePortB (
524 IdeDev->PciIo,
525 IdeDev->IoPort->Head,
526 (UINT8) ((IdeDev->Device << 4) | 0xe0 | Head)
527 );
528
529 Status = DRDYReady (IdeDev, ATATIMEOUT);
530 if (EFI_ERROR (Status)) {
531 return EFI_DEVICE_ERROR;
532 }
533
534 //
535 // set all the command parameters
536 // Before write to all the following registers, BSY and DRQ must be 0.
537 //
538 Status = DRQClear2 (IdeDev, ATATIMEOUT);
539 if (EFI_ERROR (Status)) {
540 return EFI_DEVICE_ERROR;
541 }
542
543 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount);
544 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, SectorNumber);
545 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, CylinderLsb);
546 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, CylinderMsb);
547
548 //
549 // send command via Command Register
550 //
551 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
552
553 Buffer16 = (UINT16 *) Buffer;
554
555 //
556 // According to PIO data out protocol, host can perform a series of
557 // writes to the data register after each time device set DRQ ready;
558 // The data size of "a series of read" is command specific.
559 // For most ATA command, data size written to device will not exceed 1 sector,
560 // hence the data size for "a series of write" can be the data size of one
561 // command request.
562 // For ATA command such as Write Sector command, the data size of one
563 // ATA command request is often larger than 1 sector, according to the
564 // Write Sector command, the data size of "a series of read" is exactly
565 // 1 sector.
566 // Here for simplification reason, we specify the data size for
567 // "a series of write" to 1 sector (256 words) if data size of one ATA command
568 // request is larger than 256 words.
569 //
570 Increment = 256;
571 WordCount = 0;
572
573 while (WordCount < ByteCount / 2) {
574
575 //
576 // DRQReady2-- read Alternate Status Register to determine the DRQ bit
577 // data transfer can be performed only when DRQ is ready.
578 //
579 Status = DRQReady2 (IdeDev, ATATIMEOUT);
580 if (EFI_ERROR (Status)) {
581 return EFI_DEVICE_ERROR;
582 }
583
584 Status = CheckErrorStatus (IdeDev);
585 if (EFI_ERROR (Status)) {
586 return EFI_DEVICE_ERROR;
587 }
588
589 //
590 // Check the remaining byte count is less than 512 bytes
591 //
592 if ((WordCount + Increment) > ByteCount / 2) {
593 Increment = ByteCount / 2 - WordCount;
594 }
595 //
596 // perform a series of write without check DRQ ready
597 //
598
599 IDEWritePortWMultiple (
600 IdeDev->PciIo,
601 IdeDev->IoPort->Data,
602 Increment,
603 Buffer16
604 );
605 WordCount += Increment;
606 Buffer16 += Increment;
607
608 }
609
610 DRQClear (IdeDev, ATATIMEOUT);
611
612 return CheckErrorStatus (IdeDev);
613 }
614
615 /**
616 This function is used to analyze the Status Register and print out
617 some debug information and if there is ERR bit set in the Status
618 Register, the Error Register's value is also be parsed and print out.
619
620 @param[in] *IdeDev
621 pointer pointing to IDE_BLK_IO_DEV data structure, used
622 to record all the information of the IDE device.
623
624 @retval EFI_SUCCESS No err information in the Status Register.
625 @retval EFI_DEVICE_ERROR Any err information in the Status Register.
626
627 **/
628 EFI_STATUS
629 CheckErrorStatus (
630 IN IDE_BLK_IO_DEV *IdeDev
631 )
632 {
633 UINT8 StatusRegister;
634
635 //#ifdef EFI_DEBUG
636
637 UINT8 ErrorRegister;
638
639 //#endif
640
641 StatusRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status);
642
643 DEBUG_CODE_BEGIN ();
644
645 if (StatusRegister & DWF) {
646 DEBUG (
647 (EFI_D_BLKIO,
648 "CheckErrorStatus()-- %02x : Error : Write Fault\n",
649 StatusRegister)
650 );
651 }
652
653 if (StatusRegister & CORR) {
654 DEBUG (
655 (EFI_D_BLKIO,
656 "CheckErrorStatus()-- %02x : Error : Corrected Data\n",
657 StatusRegister)
658 );
659 }
660
661 if (StatusRegister & ERR) {
662 ErrorRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error);
663
664 if (ErrorRegister & BBK_ERR) {
665 DEBUG (
666 (EFI_D_BLKIO,
667 "CheckErrorStatus()-- %02x : Error : Bad Block Detected\n",
668 ErrorRegister)
669 );
670 }
671
672 if (ErrorRegister & UNC_ERR) {
673 DEBUG (
674 (EFI_D_BLKIO,
675 "CheckErrorStatus()-- %02x : Error : Uncorrectable Data\n",
676 ErrorRegister)
677 );
678 }
679
680 if (ErrorRegister & MC_ERR) {
681 DEBUG (
682 (EFI_D_BLKIO,
683 "CheckErrorStatus()-- %02x : Error : Media Change\n",
684 ErrorRegister)
685 );
686 }
687
688 if (ErrorRegister & ABRT_ERR) {
689 DEBUG (
690 (EFI_D_BLKIO,
691 "CheckErrorStatus()-- %02x : Error : Abort\n",
692 ErrorRegister)
693 );
694 }
695
696 if (ErrorRegister & TK0NF_ERR) {
697 DEBUG (
698 (EFI_D_BLKIO,
699 "CheckErrorStatus()-- %02x : Error : Track 0 Not Found\n",
700 ErrorRegister)
701 );
702 }
703
704 if (ErrorRegister & AMNF_ERR) {
705 DEBUG (
706 (EFI_D_BLKIO,
707 "CheckErrorStatus()-- %02x : Error : Address Mark Not Found\n",
708 ErrorRegister)
709 );
710 }
711 }
712
713 DEBUG_CODE_END ();
714
715 if ((StatusRegister & (ERR | DWF | CORR)) == 0) {
716 return EFI_SUCCESS;
717 }
718
719 return EFI_DEVICE_ERROR;
720
721 }
722
723 /**
724 This function is called by the AtaBlkIoReadBlocks() to perform
725 reading from media in block unit.
726
727 @param[in] *IdeDev
728 pointer pointing to IDE_BLK_IO_DEV data structure, used
729 to record all the information of the IDE device.
730
731 @param[in] *DataBuffer
732 A pointer to the destination buffer for the data.
733
734 @param[in] Lba
735 The starting logical block address to read from
736 on the device media.
737
738 @param[in] NumberOfBlocks
739 The number of transfer data blocks.
740
741 @return return status is fully dependent on the return status
742 of AtaPioDataIn() function.
743
744 **/
745 EFI_STATUS
746 AtaReadSectors (
747 IN IDE_BLK_IO_DEV *IdeDev,
748 IN VOID *DataBuffer,
749 IN EFI_LBA Lba,
750 IN UINTN NumberOfBlocks
751 )
752 {
753 EFI_STATUS Status;
754 UINTN BlocksRemaining;
755 UINT32 Lba32;
756 UINT8 Lba0;
757 UINT8 Lba1;
758 UINT8 Lba2;
759 UINT8 Lba3;
760 UINT8 AtaCommand;
761 UINT8 SectorCount8;
762 UINT16 SectorCount;
763 UINTN ByteCount;
764 VOID *Buffer;
765
766 Buffer = DataBuffer;
767
768 //
769 // Using ATA Read Sector(s) command (opcode=0x20) with PIO DATA IN protocol
770 //
771 AtaCommand = READ_SECTORS_CMD;
772
773
774 BlocksRemaining = NumberOfBlocks;
775
776 Lba32 = (UINT32) Lba;
777
778 Status = EFI_SUCCESS;
779
780 while (BlocksRemaining > 0) {
781
782 //
783 // in ATA-3 spec, LBA is in 28 bit width
784 //
785 Lba0 = (UINT8) Lba32;
786 Lba1 = (UINT8) (Lba32 >> 8);
787 Lba2 = (UINT8) (Lba32 >> 16);
788 //
789 // low 4 bit of Lba3 stands for LBA bit24~bit27.
790 //
791 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);
792
793 if (BlocksRemaining >= 0x100) {
794
795 //
796 // SectorCount8 is sent to Sector Count register, 0x00 means 256
797 // sectors to be read
798 //
799 SectorCount8 = 0x00;
800 //
801 // SectorCount is used to record the number of sectors to be read
802 //
803 SectorCount = 256;
804 } else {
805
806 SectorCount8 = (UINT8) BlocksRemaining;
807 SectorCount = (UINT16) BlocksRemaining;
808 }
809
810 //
811 // ByteCount is the number of bytes that will be read
812 //
813 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
814
815 //
816 // call AtaPioDataIn() to send Read Sector Command and receive data read
817 //
818 Status = AtaPioDataIn (
819 IdeDev,
820 Buffer,
821 (UINT32) ByteCount,
822 AtaCommand,
823 Lba3,
824 SectorCount8,
825 Lba0,
826 Lba1,
827 Lba2
828 );
829 if (EFI_ERROR (Status)) {
830 return Status;
831 }
832
833 Lba32 += SectorCount;
834 Buffer = ((UINT8 *) Buffer + ByteCount);
835 BlocksRemaining -= SectorCount;
836 }
837
838 return Status;
839 }
840
841 /**
842 This function is called by the AtaBlkIoWriteBlocks() to perform
843 writing onto media in block unit.
844
845 @param[in] *IdeDev
846 pointer pointing to IDE_BLK_IO_DEV data structure,used
847 to record all the information of the IDE device.
848
849 @param[in] *BufferData
850 A pointer to the source buffer for the data.
851
852 @param[in] Lba
853 The starting logical block address to write onto
854 the device media.
855
856 @param[in] NumberOfBlocks
857 The number of transfer data blocks.
858
859 @return return status is fully dependent on the return status
860 of AtaPioDataOut() function.
861
862 **/
863 EFI_STATUS
864 AtaWriteSectors (
865 IN IDE_BLK_IO_DEV *IdeDev,
866 IN VOID *BufferData,
867 IN EFI_LBA Lba,
868 IN UINTN NumberOfBlocks
869 )
870 {
871 EFI_STATUS Status;
872 UINTN BlocksRemaining;
873 UINT32 Lba32;
874 UINT8 Lba0;
875 UINT8 Lba1;
876 UINT8 Lba2;
877 UINT8 Lba3;
878 UINT8 AtaCommand;
879 UINT8 SectorCount8;
880 UINT16 SectorCount;
881 UINTN ByteCount;
882 VOID *Buffer;
883
884 Buffer = BufferData;
885
886 //
887 // Using Write Sector(s) command (opcode=0x30) with PIO DATA OUT protocol
888 //
889 AtaCommand = WRITE_SECTORS_CMD;
890
891 BlocksRemaining = NumberOfBlocks;
892
893 Lba32 = (UINT32) Lba;
894
895 Status = EFI_SUCCESS;
896
897 while (BlocksRemaining > 0) {
898
899 Lba0 = (UINT8) Lba32;
900 Lba1 = (UINT8) (Lba32 >> 8);
901 Lba2 = (UINT8) (Lba32 >> 16);
902 Lba3 = (UINT8) ((Lba32 >> 24) & 0x0f);
903
904 if (BlocksRemaining >= 0x100) {
905
906 //
907 // SectorCount8 is sent to Sector Count register, 0x00 means 256 sectors
908 // to be written
909 //
910 SectorCount8 = 0x00;
911 //
912 // SectorCount is used to record the number of sectors to be written
913 //
914 SectorCount = 256;
915 } else {
916
917 SectorCount8 = (UINT8) BlocksRemaining;
918 SectorCount = (UINT16) BlocksRemaining;
919 }
920
921 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
922
923 Status = AtaPioDataOut (
924 IdeDev,
925 Buffer,
926 (UINT32) ByteCount,
927 AtaCommand,
928 Lba3,
929 SectorCount8,
930 Lba0,
931 Lba1,
932 Lba2
933 );
934 if (EFI_ERROR (Status)) {
935 return Status;
936 }
937
938 Lba32 += SectorCount;
939 Buffer = ((UINT8 *) Buffer + ByteCount);
940 BlocksRemaining -= SectorCount;
941 }
942
943 return Status;
944 }
945
946 /**
947 This function is used to implement the Soft Reset on the specified
948 device. But, the ATA Soft Reset mechanism is so strong a reset method
949 that it will force resetting on both devices connected to the
950 same cable.
951
952 It is called by IdeBlkIoReset(), a interface function of Block
953 I/O protocol.
954
955 This function can also be used by the ATAPI device to perform reset when
956 ATAPI Reset command is failed.
957
958 @param[in] *IdeDev
959 pointer pointing to IDE_BLK_IO_DEV data structure, used
960 to record all the information of the IDE device.
961
962 @retval EFI_SUCCESS Soft reset completes successfully.
963 @retval EFI_DEVICE_ERROR Any step during the reset process is failed.
964
965 @note
966 The registers initial values after ATA soft reset are different
967 to the ATA device and ATAPI device.
968
969 **/
970 EFI_STATUS
971 AtaSoftReset (
972 IN IDE_BLK_IO_DEV *IdeDev
973 )
974 {
975
976 UINT8 DeviceControl;
977
978 DeviceControl = 0;
979 //
980 // set SRST bit to initiate soft reset
981 //
982 DeviceControl |= SRST;
983
984 //
985 // disable Interrupt
986 //
987 DeviceControl |= bit1;
988
989 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
990
991 //
992 // SRST should assert for at least 5 us, we use 10 us for
993 // better compatibility
994 //
995 gBS->Stall (10);
996
997 //
998 // Enable interrupt to support UDMA, and clear SRST bit
999 //
1000 DeviceControl = 0;
1001 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
1002
1003 //
1004 // Wait for at least 2 ms to check BSY status, we use 10 ms
1005 // for better compatibility
1006 //
1007 gBS->Stall(10000);
1008 //
1009 // slave device needs at most 31s to clear BSY
1010 //
1011 if (WaitForBSYClear (IdeDev, 31000) == EFI_TIMEOUT) {
1012 return EFI_DEVICE_ERROR;
1013 }
1014
1015 return EFI_SUCCESS;
1016 }
1017
1018 /**
1019 This function is the ATA implementation for ReadBlocks in the
1020 Block I/O Protocol interface.
1021
1022 @param[in] *IdeBlkIoDevice
1023 Indicates the calling context.
1024
1025 @param[in] MediaId
1026 The media id that the read request is for.
1027
1028 @param[in] LBA
1029 The starting logical block address to read from
1030 on the device.
1031
1032 @param[in] BufferSize
1033 The size of the Buffer in bytes. This must be a
1034 multiple of the intrinsic block size of the device.
1035
1036 @param[out] *Buffer
1037 A pointer to the destination buffer for the data.
1038 The caller is responsible for either having implicit
1039 or explicit ownership of the memory that data is read into.
1040
1041 @retval EFI_SUCCESS Read Blocks successfully.
1042 @retval EFI_DEVICE_ERROR Read Blocks failed.
1043 @retval EFI_NO_MEDIA There is no media in the device.
1044 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
1045
1046 @retval EFI_BAD_BUFFER_SIZE
1047 The BufferSize parameter is not a multiple of the
1048 intrinsic block size of the device.
1049
1050 @retval EFI_INVALID_PARAMETER
1051 The read request contains LBAs that are not valid,
1052 or the data buffer is not valid.
1053
1054 @note
1055 If Read Block error because of device error, this function will call
1056 AtaSoftReset() function to reset device.
1057
1058 **/
1059 EFI_STATUS
1060 AtaBlkIoReadBlocks (
1061 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1062 IN UINT32 MediaId,
1063 IN EFI_LBA LBA,
1064 IN UINTN BufferSize,
1065 OUT VOID *Buffer
1066 )
1067 {
1068 EFI_BLOCK_IO_MEDIA *Media;
1069 UINTN BlockSize;
1070 UINTN NumberOfBlocks;
1071 EFI_STATUS Status;
1072
1073 if (Buffer == NULL) {
1074 return EFI_INVALID_PARAMETER;
1075 }
1076
1077 if (BufferSize == 0) {
1078 return EFI_SUCCESS;
1079 }
1080
1081 Status = EFI_SUCCESS;
1082
1083 //
1084 // Get the intrinsic block size
1085 //
1086 Media = IdeBlkIoDevice->BlkIo.Media;
1087 BlockSize = Media->BlockSize;
1088
1089 NumberOfBlocks = BufferSize / BlockSize;
1090
1091 if (MediaId != Media->MediaId) {
1092 return EFI_MEDIA_CHANGED;
1093 }
1094
1095 if (BufferSize % BlockSize != 0) {
1096 return EFI_BAD_BUFFER_SIZE;
1097 }
1098
1099 if (!(Media->MediaPresent)) {
1100 return EFI_NO_MEDIA;
1101 }
1102
1103 if (LBA > Media->LastBlock) {
1104 return EFI_INVALID_PARAMETER;
1105 }
1106
1107 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1108 return EFI_INVALID_PARAMETER;
1109 }
1110
1111 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1112 return EFI_INVALID_PARAMETER;
1113 }
1114
1115 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) {
1116 //
1117 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 read block mechanism
1118 //
1119 Status = AtaUdmaReadExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1120 if (EFI_ERROR (Status)) {
1121 Status = AtaReadSectorsExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1122 }
1123 } else {
1124 //
1125 // For ATA-3 compatible device, use ATA-3 read block mechanism
1126 //
1127 Status = AtaUdmaRead (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1128 if (EFI_ERROR (Status)) {
1129 Status = AtaReadSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1130 }
1131 }
1132
1133 if (EFI_ERROR (Status)) {
1134 AtaSoftReset (IdeBlkIoDevice);
1135 return EFI_DEVICE_ERROR;
1136 }
1137
1138 return EFI_SUCCESS;
1139
1140 }
1141
1142 /**
1143 This function is the ATA implementation for WriteBlocks in the
1144 Block I/O Protocol interface.
1145
1146 @param[in] *IdeBlkIoDevice
1147 Indicates the calling context.
1148
1149 @param[in] MediaId
1150 The media id that the write request is for.
1151
1152 @param[in] LBA
1153 The starting logical block address to write onto
1154 the device.
1155
1156 @param[in] BufferSize
1157 The size of the Buffer in bytes. This must be a
1158 multiple of the intrinsic block size of the device.
1159
1160 @param[out] *Buffer
1161 A pointer to the source buffer for the data.
1162 The caller is responsible for either having implicit
1163 or explicit ownership of the memory that data is
1164 written from.
1165
1166 @retval EFI_SUCCESS Write Blocks successfully.
1167 @retval EFI_DEVICE_ERROR Write Blocks failed.
1168 @retval EFI_NO_MEDIA There is no media in the device.
1169 @retval EFI_MEDIA_CHANGE The MediaId is not for the current media.
1170
1171 @retval EFI_BAD_BUFFER_SIZE
1172 The BufferSize parameter is not a multiple of the
1173 intrinsic block size of the device.
1174
1175 @retval EFI_INVALID_PARAMETER
1176 The write request contains LBAs that are not valid,
1177 or the data buffer is not valid.
1178
1179 @note
1180 If Write Block error because of device error, this function will call
1181 AtaSoftReset() function to reset device.
1182
1183 **/
1184 EFI_STATUS
1185 AtaBlkIoWriteBlocks (
1186 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1187 IN UINT32 MediaId,
1188 IN EFI_LBA LBA,
1189 IN UINTN BufferSize,
1190 OUT VOID *Buffer
1191 )
1192 {
1193
1194 EFI_BLOCK_IO_MEDIA *Media;
1195 UINTN BlockSize;
1196 UINTN NumberOfBlocks;
1197 EFI_STATUS Status;
1198
1199 if (Buffer == NULL) {
1200 return EFI_INVALID_PARAMETER;
1201 }
1202
1203 if (BufferSize == 0) {
1204 return EFI_SUCCESS;
1205 }
1206
1207 Status = EFI_SUCCESS;
1208
1209 //
1210 // Get the intrinsic block size
1211 //
1212 Media = IdeBlkIoDevice->BlkIo.Media;
1213 BlockSize = Media->BlockSize;
1214 NumberOfBlocks = BufferSize / BlockSize;
1215
1216 if (MediaId != Media->MediaId) {
1217 return EFI_MEDIA_CHANGED;
1218 }
1219
1220 if (BufferSize % BlockSize != 0) {
1221 return EFI_BAD_BUFFER_SIZE;
1222 }
1223
1224 if (LBA > Media->LastBlock) {
1225 return EFI_INVALID_PARAMETER;
1226 }
1227
1228 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1229 return EFI_INVALID_PARAMETER;
1230 }
1231
1232 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1233 return EFI_INVALID_PARAMETER;
1234 }
1235
1236 if (IdeBlkIoDevice->Type == Ide48bitAddressingHardDisk) {
1237 //
1238 // For ATA/ATAPI-6 device(capcity > 120GB), use ATA-6 write block mechanism
1239 //
1240 Status = AtaUdmaWriteExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1241 if (EFI_ERROR (Status)) {
1242 Status = AtaWriteSectorsExt (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1243 }
1244 } else {
1245 //
1246 // For ATA-3 compatible device, use ATA-3 write block mechanism
1247 //
1248 Status = AtaUdmaWrite (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1249 if (EFI_ERROR (Status)) {
1250 Status = AtaWriteSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1251 }
1252 }
1253
1254 if (EFI_ERROR (Status)) {
1255 AtaSoftReset (IdeBlkIoDevice);
1256 return EFI_DEVICE_ERROR;
1257 }
1258
1259 return EFI_SUCCESS;
1260 }
1261
1262 /**
1263 This function is called by the AtaBlkIoReadBlocks() to perform
1264 reading from media in block unit. The function has been enhanced to
1265 support >120GB access and transfer at most 65536 blocks per command
1266
1267 @param[in] *IdeDev
1268 pointer pointing to IDE_BLK_IO_DEV data structure, used
1269 to record all the information of the IDE device.
1270
1271 @param[in] *DataBuffer A pointer to the destination buffer for the data.
1272 @param[in] StartLba The starting logical block address to read from
1273 on the device media.
1274 @param[in] NumberOfBlocks The number of transfer data blocks.
1275
1276 @return return status is fully dependent on the return status
1277 of AtaPioDataInExt() function.
1278
1279 **/
1280 EFI_STATUS
1281 AtaReadSectorsExt (
1282 IN IDE_BLK_IO_DEV *IdeDev,
1283 IN VOID *DataBuffer,
1284 IN EFI_LBA StartLba,
1285 IN UINTN NumberOfBlocks
1286 )
1287 {
1288 EFI_STATUS Status;
1289 UINTN BlocksRemaining;
1290 EFI_LBA Lba64;
1291 UINT8 AtaCommand;
1292 UINT16 SectorCount;
1293 UINT32 ByteCount;
1294 VOID *Buffer;
1295
1296 //
1297 // Using ATA "Read Sectors Ext" command(opcode=0x24) with PIO DATA IN protocol
1298 //
1299 AtaCommand = READ_SECTORS_EXT_CMD;
1300 Buffer = DataBuffer;
1301 BlocksRemaining = NumberOfBlocks;
1302 Lba64 = StartLba;
1303 Status = EFI_SUCCESS;
1304
1305 while (BlocksRemaining > 0) {
1306
1307 if (BlocksRemaining >= 0x10000) {
1308 //
1309 // SectorCount is used to record the number of sectors to be read
1310 // Max 65536 sectors can be transfered at a time.
1311 //
1312 SectorCount = 0xffff;
1313 } else {
1314 SectorCount = (UINT16) BlocksRemaining;
1315 }
1316
1317 //
1318 // ByteCount is the number of bytes that will be read
1319 //
1320 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
1321
1322 //
1323 // call AtaPioDataInExt() to send Read Sector Command and receive data read
1324 //
1325 Status = AtaPioDataInExt (
1326 IdeDev,
1327 Buffer,
1328 ByteCount,
1329 AtaCommand,
1330 Lba64,
1331 SectorCount
1332 );
1333 if (EFI_ERROR (Status)) {
1334 return Status;
1335 }
1336
1337 Lba64 += SectorCount;
1338 Buffer = ((UINT8 *) Buffer + ByteCount);
1339 BlocksRemaining -= SectorCount;
1340 }
1341
1342 return Status;
1343 }
1344
1345 /**
1346 This function is called by the AtaBlkIoWriteBlocks() to perform
1347 writing onto media in block unit. The function has been enhanced to
1348 support >120GB access and transfer at most 65536 blocks per command
1349
1350 @param[in] *IdeDev
1351 pointer pointing to IDE_BLK_IO_DEV data structure,used
1352 to record all the information of the IDE device.
1353
1354 @param[in] *DataBuffer
1355 A pointer to the source buffer for the data.
1356
1357 @param[in] Lba
1358 The starting logical block address to write onto
1359 the device media.
1360
1361 @param[in] NumberOfBlocks
1362 The number of transfer data blocks.
1363
1364 @return status is fully dependent on the return status
1365 of AtaPioDataOutExt() function.
1366
1367 **/
1368 EFI_STATUS
1369 AtaWriteSectorsExt (
1370 IN IDE_BLK_IO_DEV *IdeDev,
1371 IN VOID *DataBuffer,
1372 IN EFI_LBA StartLba,
1373 IN UINTN NumberOfBlocks
1374 )
1375 {
1376 EFI_STATUS Status;
1377 EFI_LBA Lba64;
1378 UINTN BlocksRemaining;
1379 UINT8 AtaCommand;
1380 UINT16 SectorCount;
1381 UINT32 ByteCount;
1382 VOID *Buffer;
1383
1384 //
1385 // Using ATA "Write Sectors Ext" cmd(opcode=0x24) with PIO DATA OUT protocol
1386 //
1387 AtaCommand = WRITE_SECTORS_EXT_CMD;
1388 Lba64 = StartLba;
1389 Buffer = DataBuffer;
1390 BlocksRemaining = NumberOfBlocks;
1391
1392 Status = EFI_SUCCESS;
1393
1394 while (BlocksRemaining > 0) {
1395
1396 if (BlocksRemaining >= 0x10000) {
1397 //
1398 // SectorCount is used to record the number of sectors to be written.
1399 // Max 65536 sectors can be transfered at a time.
1400 //
1401 SectorCount = 0xffff;
1402 } else {
1403 SectorCount = (UINT16) BlocksRemaining;
1404 }
1405
1406 //
1407 // ByteCount is the number of bytes that will be written
1408 //
1409 ByteCount = SectorCount * (IdeDev->BlkIo.Media->BlockSize);
1410
1411 //
1412 // Call AtaPioDataOutExt() to send "Write Sectors Ext" Command
1413 //
1414 Status = AtaPioDataOutExt (
1415 IdeDev,
1416 Buffer,
1417 ByteCount,
1418 AtaCommand,
1419 Lba64,
1420 SectorCount
1421 );
1422 if (EFI_ERROR (Status)) {
1423 return Status;
1424 }
1425
1426 Lba64 += SectorCount;
1427 Buffer = ((UINT8 *) Buffer + ByteCount);
1428 BlocksRemaining -= SectorCount;
1429 }
1430
1431 return Status;
1432 }
1433
1434 /**
1435 This function is used to send out ATA commands conforms to the
1436 PIO Data In Protocol, supporting ATA/ATAPI-6 standard
1437
1438 Comparing with ATA-3 data in protocol, we have two differents here:<BR>
1439 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1440 wait will frequently fail... cause writing function return error)
1441
1442 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1443 slow down writing performance by 100 times!)
1444
1445 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
1446 to record all the information of the IDE device.
1447
1448 @param[in,out] *Buffer buffer contained data transferred from device to host.
1449 @param[in] ByteCount data size in byte unit of the buffer.
1450 @param[in] AtaCommand value of the Command Register
1451 @param[in] StartLba the start LBA of this transaction
1452 @param[in] SectorCount the count of sectors to be transfered
1453
1454 @retval EFI_SUCCESS send out the ATA command and device send required
1455 data successfully.
1456
1457 @retval EFI_DEVICE_ERROR command sent failed.
1458
1459 **/
1460 EFI_STATUS
1461 AtaPioDataInExt (
1462 IN IDE_BLK_IO_DEV *IdeDev,
1463 IN OUT VOID *Buffer,
1464 IN UINT32 ByteCount,
1465 IN UINT8 AtaCommand,
1466 IN EFI_LBA StartLba,
1467 IN UINT16 SectorCount
1468 )
1469 {
1470 UINT8 DevSel;
1471 UINT8 SectorCount8;
1472 UINT8 LbaLow;
1473 UINT8 LbaMid;
1474 UINT8 LbaHigh;
1475 UINTN WordCount;
1476 UINTN Increment;
1477 UINT16 *Buffer16;
1478 EFI_STATUS Status;
1479
1480 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1481 if (EFI_ERROR (Status)) {
1482 return EFI_DEVICE_ERROR;
1483 }
1484
1485 //
1486 // Select device, set bit6 as 1 to indicate LBA mode is used
1487 //
1488 DevSel = (UINT8) (IdeDev->Device << 4);
1489 DevSel |= 0x40;
1490 IDEWritePortB (
1491 IdeDev->PciIo,
1492 IdeDev->IoPort->Head,
1493 DevSel
1494 );
1495
1496 //
1497 // Wait for DRDY singnal asserting. ATAPI device needn't wait
1498 //
1499 if ( (IdeDev->Type == IdeHardDisk) ||
1500 (IdeDev->Type == Ide48bitAddressingHardDisk)) {
1501
1502 Status = DRDYReady (IdeDev, ATATIMEOUT);
1503 if (EFI_ERROR (Status)) {
1504 return EFI_DEVICE_ERROR;
1505 }
1506 }
1507
1508 //
1509 // Fill feature register if needed
1510 //
1511 if (AtaCommand == SET_FEATURES_CMD) {
1512 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
1513 }
1514
1515 //
1516 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1517 //
1518 SectorCount8 = (UINT8) (SectorCount >> 8);
1519 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1520
1521 SectorCount8 = (UINT8) SectorCount;
1522 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1523
1524 //
1525 // Fill the start LBA registers, which are also two-byte FIFO
1526 //
1527 LbaLow = (UINT8) RShiftU64 (StartLba, 24);
1528 LbaMid = (UINT8) RShiftU64 (StartLba, 32);
1529 LbaHigh = (UINT8) RShiftU64 (StartLba, 40);
1530 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1531 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1532 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1533
1534 LbaLow = (UINT8) StartLba;
1535 LbaMid = (UINT8) RShiftU64 (StartLba, 8);
1536 LbaHigh = (UINT8) RShiftU64 (StartLba, 16);
1537 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1538 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1539 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1540
1541 //
1542 // Send command via Command Register, invoking the processing of this command
1543 //
1544 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1545
1546 Buffer16 = (UINT16 *) Buffer;
1547
1548 //
1549 // According to PIO data in protocol, host can perform a series of reads to
1550 // the data register after each time device set DRQ ready;
1551 //
1552
1553 //
1554 // 256 words
1555 //
1556 Increment = 256;
1557
1558 //
1559 // used to record bytes of currently transfered data
1560 //
1561 WordCount = 0;
1562
1563 while (WordCount < ByteCount / 2) {
1564 //
1565 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1566 //
1567 Status = DRQReady2 (IdeDev, ATATIMEOUT);
1568 if (EFI_ERROR (Status)) {
1569 return EFI_DEVICE_ERROR;
1570 }
1571
1572 Status = CheckErrorStatus (IdeDev);
1573 if (EFI_ERROR (Status)) {
1574 return EFI_DEVICE_ERROR;
1575 }
1576
1577 //
1578 // Get the byte count for one series of read
1579 //
1580 if ((WordCount + Increment) > ByteCount / 2) {
1581 Increment = ByteCount / 2 - WordCount;
1582 }
1583
1584 IDEReadPortWMultiple (
1585 IdeDev->PciIo,
1586 IdeDev->IoPort->Data,
1587 Increment,
1588 Buffer16
1589 );
1590
1591 WordCount += Increment;
1592 Buffer16 += Increment;
1593
1594 }
1595
1596 return CheckErrorStatus (IdeDev);
1597 }
1598
1599 /**
1600 This function is used to send out ATA commands conforms to the
1601 PIO Data Out Protocol, supporting ATA/ATAPI-6 standard
1602
1603 Comparing with ATA-3 data out protocol, we have two differents here:<BR>
1604 1. Do NOT wait for DRQ clear before sending command into IDE device.(the
1605 wait will frequently fail... cause writing function return error)
1606
1607 2. Do NOT wait for DRQ clear after all data readed.(the wait greatly
1608 slow down writing performance by 100 times!)
1609
1610 @param[in] *IdeDev
1611 pointer pointing to IDE_BLK_IO_DEV data structure, used
1612 to record all the information of the IDE device.
1613
1614 @param[in] *Buffer buffer contained data transferred from host to device.
1615 @param[in] ByteCount data size in byte unit of the buffer.
1616 @param[in] AtaCommand value of the Command Register
1617 @param[in] StartLba the start LBA of this transaction
1618 @param[in] SectorCount the count of sectors to be transfered
1619
1620 @retval EFI_SUCCESS send out the ATA command and device receive required
1621 data successfully.
1622
1623 @retval EFI_DEVICE_ERROR command sent failed.
1624
1625 **/
1626 EFI_STATUS
1627 AtaPioDataOutExt (
1628 IN IDE_BLK_IO_DEV *IdeDev,
1629 IN VOID *Buffer,
1630 IN UINT32 ByteCount,
1631 IN UINT8 AtaCommand,
1632 IN EFI_LBA StartLba,
1633 IN UINT16 SectorCount
1634 )
1635 {
1636 UINT8 DevSel;
1637 UINT8 SectorCount8;
1638 UINT8 LbaLow;
1639 UINT8 LbaMid;
1640 UINT8 LbaHigh;
1641 UINTN WordCount;
1642 UINTN Increment;
1643 UINT16 *Buffer16;
1644 EFI_STATUS Status;
1645
1646 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1647 if (EFI_ERROR (Status)) {
1648 return EFI_DEVICE_ERROR;
1649 }
1650
1651 //
1652 // Select device. Set bit6 as 1 to indicate LBA mode is used
1653 //
1654 DevSel = (UINT8) (IdeDev->Device << 4);
1655 DevSel |= 0x40;
1656 IDEWritePortB (
1657 IdeDev->PciIo,
1658 IdeDev->IoPort->Head,
1659 DevSel
1660 );
1661
1662 //
1663 // Wait for DRDY singnal asserting.
1664 //
1665 Status = DRDYReady (IdeDev, ATATIMEOUT);
1666 if (EFI_ERROR (Status)) {
1667 return EFI_DEVICE_ERROR;
1668 }
1669
1670 //
1671 // Fill feature register if needed
1672 //
1673 if (AtaCommand == SET_FEATURES_CMD) {
1674 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x03);
1675 }
1676
1677 //
1678 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1679 //
1680 SectorCount8 = (UINT8) (SectorCount >> 8);
1681 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1682
1683 SectorCount8 = (UINT8) SectorCount;
1684 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1685
1686 //
1687 // Fill the start LBA registers, which are also two-byte FIFO
1688 //
1689 LbaLow = (UINT8) RShiftU64 (StartLba, 24);
1690 LbaMid = (UINT8) RShiftU64 (StartLba, 32);
1691 LbaHigh = (UINT8) RShiftU64 (StartLba, 40);
1692 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1693 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1694 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1695
1696 LbaLow = (UINT8) StartLba;
1697 LbaMid = (UINT8) RShiftU64 (StartLba, 8);
1698 LbaHigh = (UINT8) RShiftU64 (StartLba, 16);
1699 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1700 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1701 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1702
1703 //
1704 // Send command via Command Register, invoking the processing of this command
1705 //
1706 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
1707
1708 Buffer16 = (UINT16 *) Buffer;
1709
1710 //
1711 // According to PIO Data Out protocol, host can perform a series of writes to
1712 // the data register after each time device set DRQ ready;
1713 //
1714 Increment = 256;
1715
1716 //
1717 // used to record bytes of currently transfered data
1718 //
1719 WordCount = 0;
1720
1721 while (WordCount < ByteCount / 2) {
1722 //
1723 // Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
1724 //
1725 Status = DRQReady2 (IdeDev, ATATIMEOUT);
1726 if (EFI_ERROR (Status)) {
1727 return EFI_DEVICE_ERROR;
1728 }
1729
1730 Status = CheckErrorStatus (IdeDev);
1731 if (EFI_ERROR (Status)) {
1732 return EFI_DEVICE_ERROR;
1733 }
1734
1735 //
1736 // Write data into device by one series of writing to data register
1737 //
1738 if ((WordCount + Increment) > ByteCount / 2) {
1739 Increment = ByteCount / 2 - WordCount;
1740 }
1741
1742 IDEWritePortWMultiple (
1743 IdeDev->PciIo,
1744 IdeDev->IoPort->Data,
1745 Increment,
1746 Buffer16
1747 );
1748
1749 WordCount += Increment;
1750 Buffer16 += Increment;
1751
1752 }
1753 //
1754 // while
1755 //
1756
1757 return CheckErrorStatus (IdeDev);
1758 }
1759
1760
1761 /**
1762 Enable SMART of the disk if supported
1763
1764 @param[in] *IdeDev
1765 pointer pointing to IDE_BLK_IO_DEV data structure,used
1766 to record all the information of the IDE device.
1767
1768 **/
1769 VOID
1770 AtaSMARTSupport (
1771 IN IDE_BLK_IO_DEV *IdeDev
1772 )
1773 {
1774 EFI_STATUS Status;
1775 BOOLEAN SMARTSupported;
1776 UINT8 Device;
1777 EFI_IDENTIFY_DATA *TmpAtaIdentifyPointer;
1778 UINT8 DeviceSelect;
1779 UINT8 LBAMid;
1780 UINT8 LBAHigh;
1781
1782 //
1783 // Detect if the device supports S.M.A.R.T.
1784 //
1785 if ((IdeDev->pIdData->AtaData.command_set_supported_83 & 0xc000) != 0x4000) {
1786 //
1787 // Data in word 82 is not valid (bit15 shall be zero and bit14 shall be to one)
1788 //
1789 return ;
1790 } else {
1791 if ((IdeDev->pIdData->AtaData.command_set_supported_82 & 0x0001) != 0x0001) {
1792 //
1793 // S.M.A.R.T is not supported by the device
1794 //
1795 SMARTSupported = FALSE;
1796 } else {
1797 SMARTSupported = TRUE;
1798 }
1799 }
1800
1801 if (!SMARTSupported) {
1802 //
1803 // Report nonsupport status code
1804 //
1805 REPORT_STATUS_CODE (
1806 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1807 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_NOTSUPPORTED)
1808 );
1809 } else {
1810 //
1811 // Enable this feature
1812 //
1813 REPORT_STATUS_CODE (
1814 EFI_PROGRESS_CODE,
1815 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_ENABLE)
1816 );
1817
1818 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
1819 Status = AtaNonDataCommandIn (
1820 IdeDev,
1821 ATA_SMART_CMD,
1822 Device,
1823 ATA_SMART_ENABLE_OPERATION,
1824 0,
1825 0,
1826 ATA_CONSTANT_4F,
1827 ATA_CONSTANT_C2
1828 );
1829 //
1830 // Detect if this feature is enabled
1831 //
1832 TmpAtaIdentifyPointer = (EFI_IDENTIFY_DATA *) AllocateZeroPool (sizeof (EFI_IDENTIFY_DATA));
1833
1834 DeviceSelect = (UINT8) ((IdeDev->Device) << 4);
1835 Status = AtaPioDataIn (
1836 IdeDev,
1837 (VOID *) TmpAtaIdentifyPointer,
1838 sizeof (EFI_IDENTIFY_DATA),
1839 IDENTIFY_DRIVE_CMD,
1840 DeviceSelect,
1841 0,
1842 0,
1843 0,
1844 0
1845 );
1846 if (EFI_ERROR (Status)) {
1847 gBS->FreePool (TmpAtaIdentifyPointer);
1848 return ;
1849 }
1850
1851 //
1852 // Check if the feature is enabled
1853 //
1854 if ((TmpAtaIdentifyPointer->AtaData.command_set_feature_enb_85 & 0x0001) == 0x0001) {
1855 //
1856 // Read status data
1857 //
1858 AtaNonDataCommandIn (
1859 IdeDev,
1860 ATA_SMART_CMD,
1861 Device,
1862 ATA_SMART_RETURN_STATUS,
1863 0,
1864 0,
1865 ATA_CONSTANT_4F,
1866 ATA_CONSTANT_C2
1867 );
1868 LBAMid = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb);
1869 LBAHigh = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb);
1870
1871 if ((LBAMid == 0x4f) && (LBAHigh == 0xc2)) {
1872 //
1873 // The threshold exceeded condition is not detected by the device
1874 //
1875 REPORT_STATUS_CODE (
1876 EFI_PROGRESS_CODE,
1877 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_UNDERTHRESHOLD)
1878 );
1879
1880 } else if ((LBAMid == 0xf4) && (LBAHigh == 0x2c)) {
1881 //
1882 // The threshold exceeded condition is detected by the device
1883 //
1884 REPORT_STATUS_CODE (
1885 EFI_PROGRESS_CODE,
1886 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_OVERTHRESHOLD)
1887 );
1888 }
1889
1890 } else {
1891 //
1892 // Report disabled status code
1893 //
1894 REPORT_STATUS_CODE (
1895 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1896 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_ATA_BUS_SMART_DISABLED)
1897 );
1898 }
1899
1900 gBS->FreePool (TmpAtaIdentifyPointer);
1901 }
1902
1903 return ;
1904 }
1905
1906 /**
1907 Send ATA Ext command into device with NON_DATA protocol
1908
1909 @param IdeDev Standard IDE device private data structure
1910 @param AtaCommand The ATA command to be sent
1911 @param Device The value in Device register
1912 @param Feature The value in Feature register
1913 @param SectorCount The value in SectorCount register
1914 @param LbaAddress The LBA address in 48-bit mode
1915
1916 @retval EFI_SUCCESS Reading succeed
1917 @retval EFI_DEVICE_ERROR Error executing commands on this device
1918
1919 **/
1920 EFI_STATUS
1921 AtaCommandIssueExt (
1922 IN IDE_BLK_IO_DEV *IdeDev,
1923 IN UINT8 AtaCommand,
1924 IN UINT8 Device,
1925 IN UINT16 Feature,
1926 IN UINT16 SectorCount,
1927 IN EFI_LBA LbaAddress
1928 )
1929 {
1930 EFI_STATUS Status;
1931 UINT8 SectorCount8;
1932 UINT8 Feature8;
1933 UINT8 LbaLow;
1934 UINT8 LbaMid;
1935 UINT8 LbaHigh;
1936
1937 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
1938 if (EFI_ERROR (Status)) {
1939 return EFI_DEVICE_ERROR;
1940 }
1941
1942 //
1943 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
1944 //
1945 IDEWritePortB (
1946 IdeDev->PciIo,
1947 IdeDev->IoPort->Head,
1948 (UINT8) ((IdeDev->Device << 4) | 0xe0)
1949 );
1950
1951 //
1952 // ATA commands for ATA device must be issued when DRDY is set
1953 //
1954 Status = DRDYReady (IdeDev, ATATIMEOUT);
1955 if (EFI_ERROR (Status)) {
1956 return EFI_DEVICE_ERROR;
1957 }
1958
1959 //
1960 // Pass parameter into device register block
1961 //
1962 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
1963
1964 //
1965 // Fill the feature register, which is a two-byte FIFO. Need write twice.
1966 //
1967 Feature8 = (UINT8) (Feature >> 8);
1968 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1969
1970 Feature8 = (UINT8) Feature;
1971 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
1972
1973 //
1974 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
1975 //
1976 SectorCount8 = (UINT8) (SectorCount >> 8);
1977 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1978
1979 SectorCount8 = (UINT8) SectorCount;
1980 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
1981
1982 //
1983 // Fill the start LBA registers, which are also two-byte FIFO
1984 //
1985 LbaLow = (UINT8) RShiftU64 (LbaAddress, 24);
1986 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1987 LbaLow = (UINT8) LbaAddress;
1988 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, LbaLow);
1989
1990 LbaMid = (UINT8) RShiftU64 (LbaAddress, 32);
1991 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1992 LbaMid = (UINT8) RShiftU64 (LbaAddress, 8);
1993 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, LbaMid);
1994
1995 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 40);
1996 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1997 LbaHigh = (UINT8) RShiftU64 (LbaAddress, 16);
1998 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, LbaHigh);
1999
2000 //
2001 // Work around for Segate 160G disk writing
2002 //
2003 gBS->Stall (1800);
2004
2005 //
2006 // Send command via Command Register
2007 //
2008 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
2009
2010 //
2011 // Stall at least 400ns
2012 //
2013 gBS->Stall (100);
2014
2015 return EFI_SUCCESS;
2016 }
2017
2018 /**
2019 Send ATA Ext command into device with NON_DATA protocol
2020
2021 @param IdeDev Standard IDE device private data structure
2022 @param AtaCommand The ATA command to be sent
2023 @param Device The value in Device register
2024 @param Feature The value in Feature register
2025 @param SectorCount The value in SectorCount register
2026 @param LbaAddress The LBA address in 48-bit mode
2027
2028 @retval EFI_SUCCESS Reading succeed
2029 @retval EFI_DEVICE_ERROR Error executing commands on this device
2030
2031 **/
2032 EFI_STATUS
2033 AtaCommandIssue (
2034 IN IDE_BLK_IO_DEV *IdeDev,
2035 IN UINT8 AtaCommand,
2036 IN UINT8 Device,
2037 IN UINT16 Feature,
2038 IN UINT16 SectorCount,
2039 IN EFI_LBA LbaAddress
2040 )
2041 {
2042 EFI_STATUS Status;
2043 UINT8 SectorCount8;
2044 UINT8 Feature8;
2045 UINT8 Lba0;
2046 UINT8 Lba1;
2047 UINT8 Lba2;
2048 UINT8 Lba3;
2049
2050 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
2051 if (EFI_ERROR (Status)) {
2052 return EFI_DEVICE_ERROR;
2053 }
2054
2055 //
2056 // Select device (bit4), set LBA mode(bit6) (use 0xe0 for compatibility)
2057 //
2058 IDEWritePortB (
2059 IdeDev->PciIo,
2060 IdeDev->IoPort->Head,
2061 (UINT8) ((IdeDev->Device << 4) | 0xe0)
2062 );
2063
2064 //
2065 // ATA commands for ATA device must be issued when DRDY is set
2066 //
2067 Status = DRDYReady (IdeDev, ATATIMEOUT);
2068 if (EFI_ERROR (Status)) {
2069 return EFI_DEVICE_ERROR;
2070 }
2071
2072 Lba0 = (UINT8) LbaAddress;
2073 Lba1 = (UINT8) RShiftU64 (LbaAddress, 8);
2074 Lba2 = (UINT8) RShiftU64 (LbaAddress, 16);
2075 Lba3 = (UINT8) RShiftU64 (LbaAddress, 24);
2076 Device |= Lba3;
2077
2078 //
2079 // Pass parameter into device register block
2080 //
2081 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2082
2083 //
2084 // Fill the feature register, which is a two-byte FIFO. Need write twice.
2085 //
2086 Feature8 = (UINT8) Feature;
2087 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, Feature8);
2088
2089 //
2090 // Fill the sector count register, which is a two-byte FIFO. Need write twice.
2091 //
2092 SectorCount8 = (UINT8) SectorCount;
2093 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorCount, SectorCount8);
2094
2095 //
2096 // Fill the start LBA registers, which are also two-byte FIFO
2097 //
2098
2099 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->SectorNumber, Lba0);
2100 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb, Lba1);
2101 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb, Lba2);
2102
2103 //
2104 // Send command via Command Register
2105 //
2106 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, AtaCommand);
2107
2108 //
2109 // Stall at least 400ns
2110 //
2111 gBS->Stall (100);
2112
2113 return EFI_SUCCESS;
2114 }
2115
2116 /**
2117 This function is called by the AtaBlkIoReadBlocks() to perform
2118 reading from media in block unit. The function has been enhanced to
2119 support >120GB access and transfer at most 65536 blocks per command
2120
2121 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2122 to record all the information of the IDE device.
2123
2124 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2125
2126 @param[in] StartLba The starting logical block address to read from
2127 on the device media.
2128
2129 @param[in] NumberOfBlocks The number of transfer data blocks.
2130
2131 @return The device status of UDMA operation. If the operation is
2132 successful, return EFI_SUCCESS.
2133
2134 TODO: EFI_UNSUPPORTED - add return value to function comment
2135 TODO: EFI_DEVICE_ERROR - add return value to function comment
2136 TODO: EFI_DEVICE_ERROR - add return value to function comment
2137 TODO: EFI_DEVICE_ERROR - add return value to function comment
2138 **/
2139 EFI_STATUS
2140 AtaUdmaReadExt (
2141 IN IDE_BLK_IO_DEV *IdeDev,
2142 IN VOID *DataBuffer,
2143 IN EFI_LBA StartLba,
2144 IN UINTN NumberOfBlocks
2145 )
2146 {
2147 IDE_DMA_PRD *PrdAddr;
2148 IDE_DMA_PRD *UsedPrdAddr;
2149 IDE_DMA_PRD *TempPrdAddr;
2150 UINT8 RegisterValue;
2151 UINT8 Device;
2152 UINT64 IoPortForBmic;
2153 UINT64 IoPortForBmis;
2154 UINT64 IoPortForBmid;
2155 EFI_STATUS Status;
2156 UINTN PrdTableNum;
2157 UINTN ByteCount;
2158 UINTN ByteAvailable;
2159 UINT8 *PrdBuffer;
2160 UINTN RemainBlockNum;
2161 UINT8 DeviceControl;
2162 UINT32 Count;
2163 UINTN PageCount;
2164 VOID *Map;
2165 EFI_PHYSICAL_ADDRESS MemPage;
2166 EFI_PHYSICAL_ADDRESS DeviceAddress;
2167
2168 //
2169 // Channel and device differential. Select device.
2170 //
2171 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
2172
2173 //
2174 // Enable interrupt to support UDMA and Select device
2175 //
2176 DeviceControl = 0;
2177 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2178
2179 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2180
2181 if (IdePrimary == IdeDev->Channel) {
2182 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICP_OFFSET;
2183 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISP_OFFSET;
2184 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDP_OFFSET;
2185 } else {
2186 if (IdeSecondary == IdeDev->Channel) {
2187 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICS_OFFSET;
2188 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISS_OFFSET;
2189 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDS_OFFSET;
2190 } else {
2191 return EFI_UNSUPPORTED;
2192 }
2193 }
2194
2195 RemainBlockNum = NumberOfBlocks;
2196 while (RemainBlockNum > 0) {
2197
2198 if (RemainBlockNum >= MAX_DMA_EXT_COMMAND_SECTORS) {
2199 //
2200 // SectorCount is used to record the number of sectors to be read
2201 // Max 65536 sectors can be transfered at a time.
2202 //
2203 NumberOfBlocks = MAX_DMA_EXT_COMMAND_SECTORS;
2204 RemainBlockNum -= MAX_DMA_EXT_COMMAND_SECTORS;
2205 } else {
2206 NumberOfBlocks = (UINT16) RemainBlockNum;
2207 RemainBlockNum = 0;
2208 }
2209
2210 //
2211 // Calculate the number of PRD table to make sure the memory region
2212 // not cross 64K boundary
2213 //
2214 ByteCount = NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2215 PrdTableNum = ((ByteCount >> 16) + 1) + 1;
2216
2217 //
2218 // Build PRD table
2219 //
2220 MemPage = 0xFFFFFFFF;
2221 PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));
2222 Status = gBS->AllocatePages (
2223 AllocateMaxAddress,
2224 EfiBootServicesData,
2225 PageCount,
2226 &MemPage
2227 );
2228 if (EFI_ERROR (Status)) {
2229 return EFI_OUT_OF_RESOURCES;
2230 }
2231 ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));
2232
2233 PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);
2234
2235 //
2236 // To make sure PRD is allocated in one 64K page
2237 //
2238 if (((UINTN) PrdAddr & 0x0FFFF) > (((UINTN) PrdAddr + PrdTableNum * sizeof (IDE_DMA_PRD) - 1) & 0x0FFFF)) {
2239 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x10000) & 0xFFFF0000);
2240 } else {
2241 if ((UINTN) PrdAddr & 0x03) {
2242 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x04) & 0xFFFFFFFC);
2243 } else {
2244 UsedPrdAddr = PrdAddr;
2245 }
2246 }
2247
2248 //
2249 // Build the PRD table
2250 //
2251 Status = IdeDev->PciIo->Map (
2252 IdeDev->PciIo,
2253 EfiPciIoOperationBusMasterWrite,
2254 DataBuffer,
2255 &ByteCount,
2256 &DeviceAddress,
2257 &Map
2258 );
2259 if (EFI_ERROR (Status)) {
2260 gBS->FreePages (MemPage, PageCount);
2261 return EFI_OUT_OF_RESOURCES;
2262 }
2263 PrdBuffer = (VOID *) ((UINTN) DeviceAddress);
2264 TempPrdAddr = UsedPrdAddr;
2265 while (TRUE) {
2266
2267 ByteAvailable = 0x10000 - ((UINTN) PrdBuffer & 0xFFFF);
2268
2269 if (ByteCount <= ByteAvailable) {
2270 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2271 TempPrdAddr->ByteCount = (UINT16) ByteCount;
2272 TempPrdAddr->EndOfTable = 0x8000;
2273 break;
2274 }
2275
2276 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2277 TempPrdAddr->ByteCount = (UINT16) ByteAvailable;
2278
2279 ByteCount -= ByteAvailable;
2280 PrdBuffer += ByteAvailable;
2281 TempPrdAddr++;
2282 }
2283
2284 //
2285 // Set the base address to BMID register
2286 //
2287 IdeDev->PciIo->Io.Write (
2288 IdeDev->PciIo,
2289 EfiPciIoWidthUint32,
2290 EFI_PCI_IO_PASS_THROUGH_BAR,
2291 IoPortForBmid,
2292 1,
2293 &UsedPrdAddr
2294 );
2295
2296 //
2297 // Set BMIC register to identify the operation direction
2298 //
2299 IdeDev->PciIo->Io.Read (
2300 IdeDev->PciIo,
2301 EfiPciIoWidthUint8,
2302 EFI_PCI_IO_PASS_THROUGH_BAR,
2303 IoPortForBmic,
2304 1,
2305 &RegisterValue
2306 );
2307
2308 RegisterValue |= BMIC_nREAD;
2309
2310 IdeDev->PciIo->Io.Write (
2311 IdeDev->PciIo,
2312 EfiPciIoWidthUint8,
2313 EFI_PCI_IO_PASS_THROUGH_BAR,
2314 IoPortForBmic,
2315 1,
2316 &RegisterValue
2317 );
2318
2319 //
2320 // Read BMIS register and clear ERROR and INTR bit
2321 //
2322 IdeDev->PciIo->Io.Read (
2323 IdeDev->PciIo,
2324 EfiPciIoWidthUint8,
2325 EFI_PCI_IO_PASS_THROUGH_BAR,
2326 IoPortForBmis,
2327 1,
2328 &RegisterValue
2329 );
2330
2331 RegisterValue |= BMIS_INTERRUPT | BMIS_ERROR;
2332
2333 IdeDev->PciIo->Io.Write (
2334 IdeDev->PciIo,
2335 EfiPciIoWidthUint8,
2336 EFI_PCI_IO_PASS_THROUGH_BAR,
2337 IoPortForBmis,
2338 1,
2339 &RegisterValue
2340 );
2341
2342 //
2343 // Issue READ DMA EXT command
2344 //
2345 Status = AtaCommandIssueExt (
2346 IdeDev,
2347 READ_DMA_EXT_CMD,
2348 Device,
2349 0,
2350 (UINT16) NumberOfBlocks,
2351 StartLba
2352 );
2353 if (EFI_ERROR (Status)) {
2354 gBS->FreePages (MemPage, PageCount);
2355 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2356 return EFI_DEVICE_ERROR;
2357 }
2358
2359 //
2360 // Set START bit of BMIC register
2361 //
2362 IdeDev->PciIo->Io.Read (
2363 IdeDev->PciIo,
2364 EfiPciIoWidthUint8,
2365 EFI_PCI_IO_PASS_THROUGH_BAR,
2366 IoPortForBmic,
2367 1,
2368 &RegisterValue
2369 );
2370
2371 RegisterValue |= BMIC_START;
2372
2373 IdeDev->PciIo->Io.Write (
2374 IdeDev->PciIo,
2375 EfiPciIoWidthUint8,
2376 EFI_PCI_IO_PASS_THROUGH_BAR,
2377 IoPortForBmic,
2378 1,
2379 &RegisterValue
2380 );
2381
2382 //
2383 // Check the INTERRUPT and ERROR bit of BMIS
2384 // Max transfer number of sectors for one command is 65536(32Mbyte),
2385 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
2386 // So set the variable Count to 2000, for about 2 second timeout time.
2387 //
2388 Count = 2000;
2389 while (TRUE) {
2390
2391 IdeDev->PciIo->Io.Read (
2392 IdeDev->PciIo,
2393 EfiPciIoWidthUint8,
2394 EFI_PCI_IO_PASS_THROUGH_BAR,
2395 IoPortForBmis,
2396 1,
2397 &RegisterValue
2398 );
2399 if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {
2400 if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {
2401 //
2402 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
2403 //
2404 IdeDev->PciIo->Io.Read (
2405 IdeDev->PciIo,
2406 EfiPciIoWidthUint8,
2407 EFI_PCI_IO_PASS_THROUGH_BAR,
2408 IoPortForBmic,
2409 1,
2410 &RegisterValue
2411 );
2412
2413 RegisterValue &= ~((UINT8)BMIC_START);
2414
2415 IdeDev->PciIo->Io.Write (
2416 IdeDev->PciIo,
2417 EfiPciIoWidthUint8,
2418 EFI_PCI_IO_PASS_THROUGH_BAR,
2419 IoPortForBmic,
2420 1,
2421 &RegisterValue
2422 );
2423 gBS->FreePages (MemPage, PageCount);
2424 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2425 return EFI_DEVICE_ERROR;
2426 }
2427 break;
2428 }
2429
2430 gBS->Stall (1000);
2431 Count --;
2432 }
2433
2434 gBS->FreePages (MemPage, PageCount);
2435 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2436 //
2437 // Read Status Register of IDE device to clear interrupt
2438 //
2439 RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);
2440 //
2441 // Clear START bit of BMIC register
2442 //
2443 IdeDev->PciIo->Io.Read (
2444 IdeDev->PciIo,
2445 EfiPciIoWidthUint8,
2446 EFI_PCI_IO_PASS_THROUGH_BAR,
2447 IoPortForBmic,
2448 1,
2449 &RegisterValue
2450 );
2451
2452 RegisterValue &= ~((UINT8) BMIC_START);
2453
2454 IdeDev->PciIo->Io.Write (
2455 IdeDev->PciIo,
2456 EfiPciIoWidthUint8,
2457 EFI_PCI_IO_PASS_THROUGH_BAR,
2458 IoPortForBmic,
2459 1,
2460 &RegisterValue
2461 );
2462
2463 if (RegisterValue & BMIS_ERROR) {
2464 return EFI_DEVICE_ERROR;
2465 }
2466
2467 DataBuffer = (UINT8 *) DataBuffer + NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2468 StartLba += NumberOfBlocks;
2469 }
2470
2471 //
2472 // Disable interrupt of Select device
2473 //
2474 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);
2475 DeviceControl |= IEN_L;
2476 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2477
2478 return EFI_SUCCESS;
2479 }
2480
2481 /**
2482 This function is called by the AtaBlkIoReadBlocks() to perform
2483 reading from media in block unit. The function has been enhanced to
2484 support >120GB access and transfer at most 65536 blocks per command
2485
2486 @param[in] *IdeDev
2487 pointer pointing to IDE_BLK_IO_DEV data structure, used
2488 to record all the information of the IDE device.
2489
2490 @param[in] *DataBuffer A pointer to the destination buffer for the data.
2491 @param[in] StartLba The starting logical block address to read from
2492 on the device media.
2493 @param[in] NumberOfBlocks The number of transfer data blocks.
2494
2495 @return The device status of UDMA operation. If the operation is
2496 successful, return EFI_SUCCESS.
2497
2498 TODO: EFI_UNSUPPORTED - add return value to function comment
2499 TODO: EFI_DEVICE_ERROR - add return value to function comment
2500 TODO: EFI_DEVICE_ERROR - add return value to function comment
2501 TODO: EFI_DEVICE_ERROR - add return value to function comment
2502 **/
2503 EFI_STATUS
2504 AtaUdmaRead (
2505 IN IDE_BLK_IO_DEV *IdeDev,
2506 IN VOID *DataBuffer,
2507 IN EFI_LBA StartLba,
2508 IN UINTN NumberOfBlocks
2509 )
2510 {
2511 IDE_DMA_PRD *PrdAddr;
2512 IDE_DMA_PRD *UsedPrdAddr;
2513 IDE_DMA_PRD *TempPrdAddr;
2514 UINT8 RegisterValue;
2515 UINT8 Device;
2516 UINT64 IoPortForBmic;
2517 UINT64 IoPortForBmis;
2518 UINT64 IoPortForBmid;
2519 EFI_STATUS Status;
2520 UINTN PrdTableNum;
2521 UINTN ByteCount;
2522 UINTN ByteAvailable;
2523 UINT8 *PrdBuffer;
2524 UINTN RemainBlockNum;
2525 UINT8 DeviceControl;
2526 UINT32 Count;
2527 UINTN PageCount;
2528 VOID *Map;
2529 EFI_PHYSICAL_ADDRESS MemPage;
2530 EFI_PHYSICAL_ADDRESS DeviceAddress;
2531
2532 //
2533 // Channel and device differential
2534 //
2535 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
2536
2537 //
2538 // Enable interrupt to support UDMA and Select device
2539 //
2540 DeviceControl = 0;
2541 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2542
2543 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2544
2545 if (IdePrimary == IdeDev->Channel) {
2546 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICP_OFFSET;
2547 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISP_OFFSET;
2548 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDP_OFFSET;
2549 } else {
2550 if (IdeSecondary == IdeDev->Channel) {
2551 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICS_OFFSET;
2552 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISS_OFFSET;
2553 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDS_OFFSET;
2554 } else {
2555 return EFI_UNSUPPORTED;
2556 }
2557 }
2558
2559 RemainBlockNum = NumberOfBlocks;
2560 while (RemainBlockNum > 0) {
2561
2562 if (RemainBlockNum >= MAX_DMA_COMMAND_SECTORS) {
2563 //
2564 // SectorCount is used to record the number of sectors to be read
2565 // Max 256 sectors can be transfered at a time.
2566 //
2567 NumberOfBlocks = MAX_DMA_COMMAND_SECTORS;
2568 RemainBlockNum -= MAX_DMA_COMMAND_SECTORS;
2569 } else {
2570 NumberOfBlocks = (UINT16) RemainBlockNum;
2571 RemainBlockNum = 0;
2572 }
2573
2574 //
2575 // Calculate the number of PRD table to make sure the memory region
2576 // not cross 64K boundary
2577 //
2578 ByteCount = NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2579 PrdTableNum = ((ByteCount >> 16) + 1) + 1;
2580
2581 //
2582 // Build PRD table
2583 //
2584 MemPage = 0xFFFFFFFF;
2585 PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));
2586 Status = gBS->AllocatePages (
2587 AllocateMaxAddress,
2588 EfiBootServicesData,
2589 PageCount,
2590 &MemPage
2591 );
2592 if (EFI_ERROR (Status)) {
2593 return EFI_OUT_OF_RESOURCES;
2594 }
2595 ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));
2596
2597 PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);
2598 //
2599 // To make sure PRD is allocated in one 64K page
2600 //
2601 if (((UINTN) PrdAddr & 0x0FFFF) > (((UINTN) PrdAddr + PrdTableNum * sizeof (IDE_DMA_PRD) - 1) & 0x0FFFF)) {
2602 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x10000) & 0xFFFF0000);
2603 } else {
2604 if ((UINTN) PrdAddr & 0x03) {
2605 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x04) & 0xFFFFFFFC);
2606 } else {
2607 UsedPrdAddr = PrdAddr;
2608 }
2609 }
2610
2611 //
2612 // Build the PRD table
2613 //
2614 Status = IdeDev->PciIo->Map (
2615 IdeDev->PciIo,
2616 EfiPciIoOperationBusMasterWrite,
2617 DataBuffer,
2618 &ByteCount,
2619 &DeviceAddress,
2620 &Map
2621 );
2622 if (EFI_ERROR (Status)) {
2623 gBS->FreePages (MemPage, PageCount);
2624 return EFI_OUT_OF_RESOURCES;
2625 }
2626 PrdBuffer = (UINT8 *) ((UINTN) DeviceAddress);
2627 TempPrdAddr = UsedPrdAddr;
2628 while (TRUE) {
2629
2630 ByteAvailable = 0x10000 - ((UINTN) PrdBuffer & 0xFFFF);
2631
2632 if (ByteCount <= ByteAvailable) {
2633 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2634 TempPrdAddr->ByteCount = (UINT16) ByteCount;
2635 TempPrdAddr->EndOfTable = 0x8000;
2636 break;
2637 }
2638
2639 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2640 TempPrdAddr->ByteCount = (UINT16) ByteAvailable;
2641
2642 ByteCount -= ByteAvailable;
2643 PrdBuffer += ByteAvailable;
2644 TempPrdAddr++;
2645 }
2646
2647 //
2648 // Set the base address to BMID register
2649 //
2650 IdeDev->PciIo->Io.Write (
2651 IdeDev->PciIo,
2652 EfiPciIoWidthUint32,
2653 EFI_PCI_IO_PASS_THROUGH_BAR,
2654 IoPortForBmid,
2655 1,
2656 &UsedPrdAddr
2657 );
2658
2659 //
2660 // Set BMIC register to identify the operation direction
2661 //
2662 IdeDev->PciIo->Io.Read (
2663 IdeDev->PciIo,
2664 EfiPciIoWidthUint8,
2665 EFI_PCI_IO_PASS_THROUGH_BAR,
2666 IoPortForBmic,
2667 1,
2668 &RegisterValue
2669 );
2670
2671 RegisterValue |= BMIC_nREAD;
2672
2673 IdeDev->PciIo->Io.Write (
2674 IdeDev->PciIo,
2675 EfiPciIoWidthUint8,
2676 EFI_PCI_IO_PASS_THROUGH_BAR,
2677 IoPortForBmic,
2678 1,
2679 &RegisterValue
2680 );
2681
2682 //
2683 // Read BMIS register and clear ERROR and INTR bit
2684 //
2685 IdeDev->PciIo->Io.Read (
2686 IdeDev->PciIo,
2687 EfiPciIoWidthUint8,
2688 EFI_PCI_IO_PASS_THROUGH_BAR,
2689 IoPortForBmis,
2690 1,
2691 &RegisterValue
2692 );
2693
2694 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR);
2695
2696 IdeDev->PciIo->Io.Write (
2697 IdeDev->PciIo,
2698 EfiPciIoWidthUint8,
2699 EFI_PCI_IO_PASS_THROUGH_BAR,
2700 IoPortForBmis,
2701 1,
2702 &RegisterValue
2703 );
2704
2705 //
2706 // Issue READ DMA command
2707 //
2708 Status = AtaCommandIssue (
2709 IdeDev,
2710 READ_DMA_CMD,
2711 Device,
2712 0,
2713 (UINT16) NumberOfBlocks,
2714 StartLba
2715 );
2716 if (EFI_ERROR (Status)) {
2717 gBS->FreePages (MemPage, PageCount);
2718 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2719 return EFI_DEVICE_ERROR;
2720 }
2721
2722 //
2723 // Set START bit of BMIC register
2724 //
2725 IdeDev->PciIo->Io.Read (
2726 IdeDev->PciIo,
2727 EfiPciIoWidthUint8,
2728 EFI_PCI_IO_PASS_THROUGH_BAR,
2729 IoPortForBmic,
2730 1,
2731 &RegisterValue
2732 );
2733
2734 RegisterValue |= BMIC_START;
2735
2736 IdeDev->PciIo->Io.Write (
2737 IdeDev->PciIo,
2738 EfiPciIoWidthUint8,
2739 EFI_PCI_IO_PASS_THROUGH_BAR,
2740 IoPortForBmic,
2741 1,
2742 &RegisterValue
2743 );
2744
2745 //
2746 // Check the INTERRUPT and ERROR bit of BMIS
2747 // Max transfer number of sectors for one command is 65536(32Mbyte),
2748 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
2749 // So set the variable Count to 2000, for about 2 second timeout time.
2750 //
2751 Count = 2000;
2752 while (TRUE) {
2753
2754 IdeDev->PciIo->Io.Read (
2755 IdeDev->PciIo,
2756 EfiPciIoWidthUint8,
2757 EFI_PCI_IO_PASS_THROUGH_BAR,
2758 IoPortForBmis,
2759 1,
2760 &RegisterValue
2761 );
2762 if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {
2763 if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {
2764 //
2765 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
2766 //
2767 IdeDev->PciIo->Io.Read (
2768 IdeDev->PciIo,
2769 EfiPciIoWidthUint8,
2770 EFI_PCI_IO_PASS_THROUGH_BAR,
2771 IoPortForBmic,
2772 1,
2773 &RegisterValue
2774 );
2775
2776 RegisterValue &= ~((UINT8)BMIC_START);
2777
2778 IdeDev->PciIo->Io.Write (
2779 IdeDev->PciIo,
2780 EfiPciIoWidthUint8,
2781 EFI_PCI_IO_PASS_THROUGH_BAR,
2782 IoPortForBmic,
2783 1,
2784 &RegisterValue
2785 );
2786 gBS->FreePages (MemPage, PageCount);
2787 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2788 return EFI_DEVICE_ERROR;
2789 }
2790 break;
2791 }
2792
2793 gBS->Stall (1000);
2794 Count --;
2795 }
2796
2797 gBS->FreePages (MemPage, PageCount);
2798 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
2799 //
2800 // Read Status Register of IDE device to clear interrupt
2801 //
2802 RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);
2803 //
2804 // Clear START bit of BMIC register
2805 //
2806 IdeDev->PciIo->Io.Read (
2807 IdeDev->PciIo,
2808 EfiPciIoWidthUint8,
2809 EFI_PCI_IO_PASS_THROUGH_BAR,
2810 IoPortForBmic,
2811 1,
2812 &RegisterValue
2813 );
2814
2815 RegisterValue &= ~((UINT8) BMIC_START);
2816
2817 IdeDev->PciIo->Io.Write (
2818 IdeDev->PciIo,
2819 EfiPciIoWidthUint8,
2820 EFI_PCI_IO_PASS_THROUGH_BAR,
2821 IoPortForBmic,
2822 1,
2823 &RegisterValue
2824 );
2825
2826 if (RegisterValue & BMIS_ERROR) {
2827 return EFI_DEVICE_ERROR;
2828 }
2829
2830 DataBuffer = (UINT8 *) DataBuffer + NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2831 StartLba += NumberOfBlocks;
2832 }
2833
2834 //
2835 // Disable interrupt of Select device
2836 //
2837 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);
2838 DeviceControl |= IEN_L;
2839 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2840
2841 return EFI_SUCCESS;
2842 }
2843
2844 /**
2845 This function is called by the AtaBlkIoWriteBlocks() to perform
2846 writing to media in block unit. The function has been enhanced to
2847 support >120GB access and transfer at most 65536 blocks per command
2848
2849 @param[in] *IdeDev pointer pointing to IDE_BLK_IO_DEV data structure, used
2850 to record all the information of the IDE device.
2851
2852 @param[in] *DataBuffer A pointer to the source buffer for the data.
2853
2854 @param[in] StartLba The starting logical block address to write to
2855 on the device media.
2856
2857 @param[in] NumberOfBlocks The number of transfer data blocks.
2858
2859 @return The device status of UDMA operation. If the operation is
2860 successful, return EFI_SUCCESS.
2861
2862 TODO: EFI_UNSUPPORTED - add return value to function comment
2863 TODO: EFI_DEVICE_ERROR - add return value to function comment
2864 TODO: EFI_DEVICE_ERROR - add return value to function comment
2865 **/
2866 EFI_STATUS
2867 AtaUdmaWriteExt (
2868 IN IDE_BLK_IO_DEV *IdeDev,
2869 IN VOID *DataBuffer,
2870 IN EFI_LBA StartLba,
2871 IN UINTN NumberOfBlocks
2872 )
2873 {
2874 IDE_DMA_PRD *PrdAddr;
2875 IDE_DMA_PRD *UsedPrdAddr;
2876 IDE_DMA_PRD *TempPrdAddr;
2877 UINT8 RegisterValue;
2878 UINT8 Device;
2879 UINT64 IoPortForBmic;
2880 UINT64 IoPortForBmis;
2881 UINT64 IoPortForBmid;
2882 EFI_STATUS Status;
2883 UINTN PrdTableNum;
2884 UINTN ByteCount;
2885 UINTN ByteAvailable;
2886 UINT8 *PrdBuffer;
2887 UINTN RemainBlockNum;
2888 UINT8 DeviceControl;
2889 UINT32 Count;
2890 UINTN PageCount;
2891 VOID *Map;
2892 EFI_PHYSICAL_ADDRESS MemPage;
2893 EFI_PHYSICAL_ADDRESS DeviceAddress;
2894
2895 //
2896 // Channel and device differential
2897 //
2898 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
2899
2900 //
2901 // Enable interrupt to support UDMA and Select device
2902 //
2903 DeviceControl = 0;
2904 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
2905
2906 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
2907
2908 if (IdePrimary == IdeDev->Channel) {
2909 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICP_OFFSET;
2910 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISP_OFFSET;
2911 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDP_OFFSET;
2912 } else {
2913 if (IdeSecondary == IdeDev->Channel) {
2914 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICS_OFFSET;
2915 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISS_OFFSET;
2916 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDS_OFFSET;
2917 } else {
2918 return EFI_UNSUPPORTED;
2919 }
2920 }
2921
2922 RemainBlockNum = NumberOfBlocks;
2923 while (RemainBlockNum > 0) {
2924
2925 if (RemainBlockNum >= MAX_DMA_EXT_COMMAND_SECTORS) {
2926 //
2927 // SectorCount is used to record the number of sectors to be read
2928 // Max 65536 sectors can be transfered at a time.
2929 //
2930 NumberOfBlocks = MAX_DMA_EXT_COMMAND_SECTORS;
2931 RemainBlockNum -= MAX_DMA_EXT_COMMAND_SECTORS;
2932 } else {
2933 NumberOfBlocks = (UINT16) RemainBlockNum;
2934 RemainBlockNum = 0;
2935 }
2936
2937 //
2938 // Calculate the number of PRD table to make sure the memory region
2939 // not cross 64K boundary
2940 //
2941 ByteCount = NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
2942 PrdTableNum = ((ByteCount >> 16) + 1) + 1;
2943
2944 //
2945 // Build PRD table
2946 //
2947 MemPage = 0xFFFFFFFF;
2948 PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));
2949 Status = gBS->AllocatePages (
2950 AllocateMaxAddress,
2951 EfiBootServicesData,
2952 PageCount,
2953 &MemPage
2954 );
2955 if (EFI_ERROR (Status)) {
2956 return EFI_OUT_OF_RESOURCES;
2957 }
2958 ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));
2959
2960 PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);
2961 //
2962 // To make sure PRD is allocated in one 64K page
2963 //
2964 if (((UINTN) PrdAddr & 0x0FFFF) > (((UINTN) PrdAddr + PrdTableNum * sizeof (IDE_DMA_PRD) - 1) & 0x0FFFF)) {
2965 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x10000) & 0xFFFF0000);
2966 } else {
2967 if ((UINTN) PrdAddr & 0x03) {
2968 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x04) & 0xFFFFFFFC);
2969 } else {
2970 UsedPrdAddr = PrdAddr;
2971 }
2972 }
2973
2974 //
2975 // Build the PRD table
2976 //
2977 Status = IdeDev->PciIo->Map (
2978 IdeDev->PciIo,
2979 EfiPciIoOperationBusMasterRead,
2980 DataBuffer,
2981 &ByteCount,
2982 &DeviceAddress,
2983 &Map
2984 );
2985 if (EFI_ERROR (Status)) {
2986 gBS->FreePages (MemPage, PageCount);
2987 return EFI_OUT_OF_RESOURCES;
2988 }
2989 PrdBuffer = (UINT8 *) ((UINTN) DeviceAddress);
2990 TempPrdAddr = UsedPrdAddr;
2991 while (TRUE) {
2992
2993 ByteAvailable = 0x10000 - ((UINTN) PrdBuffer & 0xFFFF);
2994
2995 if (ByteCount <= ByteAvailable) {
2996 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
2997 TempPrdAddr->ByteCount = (UINT16) ByteCount;
2998 TempPrdAddr->EndOfTable = 0x8000;
2999 break;
3000 }
3001
3002 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
3003 TempPrdAddr->ByteCount = (UINT16) ByteAvailable;
3004
3005 ByteCount -= ByteAvailable;
3006 PrdBuffer += ByteAvailable;
3007 TempPrdAddr++;
3008 }
3009
3010 //
3011 // Set the base address to BMID register
3012 //
3013 IdeDev->PciIo->Io.Write (
3014 IdeDev->PciIo,
3015 EfiPciIoWidthUint32,
3016 EFI_PCI_IO_PASS_THROUGH_BAR,
3017 IoPortForBmid,
3018 1,
3019 &UsedPrdAddr
3020 );
3021
3022 //
3023 // Set BMIC register to identify the operation direction
3024 //
3025 IdeDev->PciIo->Io.Read (
3026 IdeDev->PciIo,
3027 EfiPciIoWidthUint8,
3028 EFI_PCI_IO_PASS_THROUGH_BAR,
3029 IoPortForBmic,
3030 1,
3031 &RegisterValue
3032 );
3033 //
3034 // 0000 1000
3035 //
3036 RegisterValue &= ~((UINT8) BMIC_nREAD);
3037
3038 IdeDev->PciIo->Io.Write (
3039 IdeDev->PciIo,
3040 EfiPciIoWidthUint8,
3041 EFI_PCI_IO_PASS_THROUGH_BAR,
3042 IoPortForBmic,
3043 1,
3044 &RegisterValue
3045 );
3046
3047 //
3048 // Read BMIS register and clear ERROR and INTR bit
3049 //
3050 IdeDev->PciIo->Io.Read (
3051 IdeDev->PciIo,
3052 EfiPciIoWidthUint8,
3053 EFI_PCI_IO_PASS_THROUGH_BAR,
3054 IoPortForBmis,
3055 1,
3056 &RegisterValue
3057 );
3058
3059 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR);
3060
3061 IdeDev->PciIo->Io.Write (
3062 IdeDev->PciIo,
3063 EfiPciIoWidthUint8,
3064 EFI_PCI_IO_PASS_THROUGH_BAR,
3065 IoPortForBmis,
3066 1,
3067 &RegisterValue
3068 );
3069
3070 //
3071 // Issue WRITE DMA EXT command
3072 //
3073 Status = AtaCommandIssueExt (
3074 IdeDev,
3075 WRITE_DMA_EXT_CMD,
3076 Device,
3077 0,
3078 (UINT16) NumberOfBlocks,
3079 StartLba
3080 );
3081 if (EFI_ERROR (Status)) {
3082 gBS->FreePages (MemPage, PageCount);
3083 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
3084 return EFI_DEVICE_ERROR;
3085 }
3086
3087 //
3088 // Set START bit of BMIC register
3089 //
3090 IdeDev->PciIo->Io.Read (
3091 IdeDev->PciIo,
3092 EfiPciIoWidthUint8,
3093 EFI_PCI_IO_PASS_THROUGH_BAR,
3094 IoPortForBmic,
3095 1,
3096 &RegisterValue
3097 );
3098
3099 RegisterValue |= BMIC_START;
3100
3101 IdeDev->PciIo->Io.Write (
3102 IdeDev->PciIo,
3103 EfiPciIoWidthUint8,
3104 EFI_PCI_IO_PASS_THROUGH_BAR,
3105 IoPortForBmic,
3106 1,
3107 &RegisterValue
3108 );
3109
3110 //
3111 // Check the INTERRUPT and ERROR bit of BMIS
3112 // Max transfer number of sectors for one command is 65536(32Mbyte),
3113 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
3114 // So set the variable Count to 2000, for about 2 second timeout time.
3115 //
3116 Count = 2000;
3117 while (TRUE) {
3118
3119 IdeDev->PciIo->Io.Read (
3120 IdeDev->PciIo,
3121 EfiPciIoWidthUint8,
3122 EFI_PCI_IO_PASS_THROUGH_BAR,
3123 IoPortForBmis,
3124 1,
3125 &RegisterValue
3126 );
3127 if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {
3128 if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {
3129 //
3130 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
3131 //
3132 IdeDev->PciIo->Io.Read (
3133 IdeDev->PciIo,
3134 EfiPciIoWidthUint8,
3135 EFI_PCI_IO_PASS_THROUGH_BAR,
3136 IoPortForBmic,
3137 1,
3138 &RegisterValue
3139 );
3140
3141 RegisterValue &= ~((UINT8)BMIC_START);
3142
3143 IdeDev->PciIo->Io.Write (
3144 IdeDev->PciIo,
3145 EfiPciIoWidthUint8,
3146 EFI_PCI_IO_PASS_THROUGH_BAR,
3147 IoPortForBmic,
3148 1,
3149 &RegisterValue
3150 );
3151 gBS->FreePages (MemPage, PageCount);
3152 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
3153 return EFI_DEVICE_ERROR;
3154 }
3155 break;
3156 }
3157
3158 gBS->Stall (1000);
3159 Count --;
3160 }
3161
3162 gBS->FreePages (MemPage, PageCount);
3163 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
3164 //
3165 // Read Status Register of IDE device to clear interrupt
3166 //
3167 RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);
3168 //
3169 // Clear START bit of BMIC register
3170 //
3171 IdeDev->PciIo->Io.Read (
3172 IdeDev->PciIo,
3173 EfiPciIoWidthUint8,
3174 EFI_PCI_IO_PASS_THROUGH_BAR,
3175 IoPortForBmic,
3176 1,
3177 &RegisterValue
3178 );
3179
3180 RegisterValue &= ~((UINT8) BMIC_START);
3181
3182 IdeDev->PciIo->Io.Write (
3183 IdeDev->PciIo,
3184 EfiPciIoWidthUint8,
3185 EFI_PCI_IO_PASS_THROUGH_BAR,
3186 IoPortForBmic,
3187 1,
3188 &RegisterValue
3189 );
3190
3191 DataBuffer = (UINT8 *) DataBuffer + NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
3192 StartLba += NumberOfBlocks;
3193 }
3194
3195 //
3196 // Disable interrupt of Select device
3197 //
3198 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);
3199 DeviceControl |= IEN_L;
3200 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
3201
3202 return EFI_SUCCESS;
3203 }
3204
3205 /**
3206 This function is called by the AtaBlkIoWriteBlocks() to perform
3207 writing to media in block unit. The function has been enhanced to
3208 support >120GB access and transfer at most 65536 blocks per command
3209
3210 @param[in] *IdeDev
3211 pointer pointing to IDE_BLK_IO_DEV data structure, used
3212 to record all the information of the IDE device.
3213
3214 @param[in] *DataBuffer
3215 A pointer to the source buffer for the data.
3216
3217 @param[in] StartLba
3218 The starting logical block address to write to
3219 on the device media.
3220
3221 @param[in] NumberOfBlocks
3222 The number of transfer data blocks.
3223
3224 @return The device status of UDMA operation. If the operation is
3225 successful, return EFI_SUCCESS.
3226
3227 TODO: EFI_UNSUPPORTED - add return value to function comment
3228 TODO: EFI_DEVICE_ERROR - add return value to function comment
3229 TODO: EFI_DEVICE_ERROR - add return value to function comment
3230 **/
3231 EFI_STATUS
3232 AtaUdmaWrite (
3233 IN IDE_BLK_IO_DEV *IdeDev,
3234 IN VOID *DataBuffer,
3235 IN EFI_LBA StartLba,
3236 IN UINTN NumberOfBlocks
3237 )
3238 {
3239 IDE_DMA_PRD *PrdAddr;
3240 IDE_DMA_PRD *UsedPrdAddr;
3241 IDE_DMA_PRD *TempPrdAddr;
3242 UINT8 RegisterValue;
3243 UINT8 Device;
3244 UINT64 IoPortForBmic;
3245 UINT64 IoPortForBmis;
3246 UINT64 IoPortForBmid;
3247 EFI_STATUS Status;
3248 UINTN PrdTableNum;
3249 UINTN ByteCount;
3250 UINTN ByteAvailable;
3251 UINT8 *PrdBuffer;
3252 UINTN RemainBlockNum;
3253 UINT8 DeviceControl;
3254 UINT32 Count;
3255 UINTN PageCount;
3256 VOID *Map;
3257 EFI_PHYSICAL_ADDRESS MemPage;
3258 EFI_PHYSICAL_ADDRESS DeviceAddress;
3259
3260 //
3261 // Channel and device differential
3262 //
3263 Device = (UINT8) ((IdeDev->Device << 4) | 0xe0);
3264
3265 //
3266 // Enable interrupt to support UDMA
3267 //
3268 DeviceControl = 0;
3269 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
3270
3271 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, Device);
3272
3273 if (IdePrimary == IdeDev->Channel) {
3274 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICP_OFFSET;
3275 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISP_OFFSET;
3276 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDP_OFFSET;
3277 } else {
3278 if (IdeSecondary == IdeDev->Channel) {
3279 IoPortForBmic = IdeDev->IoPort->BusMasterBaseAddr + BMICS_OFFSET;
3280 IoPortForBmis = IdeDev->IoPort->BusMasterBaseAddr + BMISS_OFFSET;
3281 IoPortForBmid = IdeDev->IoPort->BusMasterBaseAddr + BMIDS_OFFSET;
3282 } else {
3283 return EFI_UNSUPPORTED;
3284 }
3285 }
3286
3287 RemainBlockNum = NumberOfBlocks;
3288 while (RemainBlockNum > 0) {
3289
3290 if (RemainBlockNum >= MAX_DMA_COMMAND_SECTORS) {
3291 //
3292 // SectorCount is used to record the number of sectors to be read
3293 // Max 256 sectors can be transfered at a time.
3294 //
3295 NumberOfBlocks = MAX_DMA_COMMAND_SECTORS;
3296 RemainBlockNum -= MAX_DMA_COMMAND_SECTORS;
3297 } else {
3298 NumberOfBlocks = (UINT16) RemainBlockNum;
3299 RemainBlockNum = 0;
3300 }
3301
3302 //
3303 // Calculate the number of PRD table to make sure the memory region
3304 // not cross 64K boundary
3305 //
3306 ByteCount = NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
3307 PrdTableNum = ((ByteCount >> 16) + 1) + 1;
3308
3309 //
3310 // Build PRD table
3311 //
3312 MemPage = 0xFFFFFFFF;
3313 PageCount = EFI_SIZE_TO_PAGES (2 * PrdTableNum * sizeof (IDE_DMA_PRD));
3314 Status = gBS->AllocatePages (
3315 AllocateMaxAddress,
3316 EfiBootServicesData,
3317 PageCount,
3318 &MemPage
3319 );
3320 if (EFI_ERROR (Status)) {
3321 return EFI_OUT_OF_RESOURCES;
3322 }
3323 ZeroMem ((VOID *) ((UINTN) MemPage), EFI_PAGES_TO_SIZE (PageCount));
3324
3325 PrdAddr = (IDE_DMA_PRD *) ((UINTN) MemPage);
3326
3327 //
3328 // To make sure PRD is allocated in one 64K page
3329 //
3330 if (((UINTN) PrdAddr & 0x0FFFF) > (((UINTN) PrdAddr + PrdTableNum * sizeof (IDE_DMA_PRD) - 1) & 0x0FFFF)) {
3331 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x10000) & 0xFFFF0000);
3332 } else {
3333 if ((UINTN) PrdAddr & 0x03) {
3334 UsedPrdAddr = (IDE_DMA_PRD *) ((UINTN) ((UINT8 *) PrdAddr + 0x04) & 0xFFFFFFFC);
3335 } else {
3336 UsedPrdAddr = PrdAddr;
3337 }
3338 }
3339
3340 //
3341 // Build the PRD table
3342 //
3343 Status = IdeDev->PciIo->Map (
3344 IdeDev->PciIo,
3345 EfiPciIoOperationBusMasterRead,
3346 DataBuffer,
3347 &ByteCount,
3348 &DeviceAddress,
3349 &Map
3350 );
3351 if (EFI_ERROR (Status)) {
3352 gBS->FreePages (MemPage, PageCount);
3353 return EFI_OUT_OF_RESOURCES;
3354 }
3355 PrdBuffer = (UINT8 *) ((UINTN) DeviceAddress);
3356 TempPrdAddr = UsedPrdAddr;
3357 while (TRUE) {
3358
3359 ByteAvailable = 0x10000 - ((UINTN) PrdBuffer & 0xFFFF);
3360
3361 if (ByteCount <= ByteAvailable) {
3362 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
3363 TempPrdAddr->ByteCount = (UINT16) ByteCount;
3364 TempPrdAddr->EndOfTable = 0x8000;
3365 break;
3366 }
3367
3368 TempPrdAddr->RegionBaseAddr = (UINT32) ((UINTN) PrdBuffer);
3369 TempPrdAddr->ByteCount = (UINT16) ByteAvailable;
3370
3371 ByteCount -= ByteAvailable;
3372 PrdBuffer += ByteAvailable;
3373 TempPrdAddr++;
3374 }
3375
3376 //
3377 // Set the base address to BMID register
3378 //
3379 IdeDev->PciIo->Io.Write (
3380 IdeDev->PciIo,
3381 EfiPciIoWidthUint32,
3382 EFI_PCI_IO_PASS_THROUGH_BAR,
3383 IoPortForBmid,
3384 1,
3385 &UsedPrdAddr
3386 );
3387
3388 //
3389 // Set BMIC register to identify the operation direction
3390 //
3391 IdeDev->PciIo->Io.Read (
3392 IdeDev->PciIo,
3393 EfiPciIoWidthUint8,
3394 EFI_PCI_IO_PASS_THROUGH_BAR,
3395 IoPortForBmic,
3396 1,
3397 &RegisterValue
3398 );
3399 //
3400 // 0000 1000
3401 //
3402 RegisterValue &= ~((UINT8) BMIC_nREAD);
3403
3404 IdeDev->PciIo->Io.Write (
3405 IdeDev->PciIo,
3406 EfiPciIoWidthUint8,
3407 EFI_PCI_IO_PASS_THROUGH_BAR,
3408 IoPortForBmic,
3409 1,
3410 &RegisterValue
3411 );
3412
3413 //
3414 // Read BMIS register and clear ERROR and INTR bit
3415 //
3416 IdeDev->PciIo->Io.Read (
3417 IdeDev->PciIo,
3418 EfiPciIoWidthUint8,
3419 EFI_PCI_IO_PASS_THROUGH_BAR,
3420 IoPortForBmis,
3421 1,
3422 &RegisterValue
3423 );
3424
3425 RegisterValue |= (BMIS_INTERRUPT | BMIS_ERROR);
3426
3427 IdeDev->PciIo->Io.Write (
3428 IdeDev->PciIo,
3429 EfiPciIoWidthUint8,
3430 EFI_PCI_IO_PASS_THROUGH_BAR,
3431 IoPortForBmis,
3432 1,
3433 &RegisterValue
3434 );
3435
3436 //
3437 // Issue WRITE DMA command
3438 //
3439 Status = AtaCommandIssue (
3440 IdeDev,
3441 WRITE_DMA_CMD,
3442 Device,
3443 0,
3444 (UINT16) NumberOfBlocks,
3445 StartLba
3446 );
3447 if (EFI_ERROR (Status)) {
3448 gBS->FreePages (MemPage, PageCount);
3449 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
3450 return EFI_DEVICE_ERROR;
3451 }
3452
3453 //
3454 // Set START bit of BMIC register
3455 //
3456 IdeDev->PciIo->Io.Read (
3457 IdeDev->PciIo,
3458 EfiPciIoWidthUint8,
3459 EFI_PCI_IO_PASS_THROUGH_BAR,
3460 IoPortForBmic,
3461 1,
3462 &RegisterValue
3463 );
3464
3465 RegisterValue |= BMIC_START;
3466
3467 IdeDev->PciIo->Io.Write (
3468 IdeDev->PciIo,
3469 EfiPciIoWidthUint8,
3470 EFI_PCI_IO_PASS_THROUGH_BAR,
3471 IoPortForBmic,
3472 1,
3473 &RegisterValue
3474 );
3475
3476 //
3477 // Check the INTERRUPT and ERROR bit of BMIS
3478 // Max transfer number of sectors for one command is 65536(32Mbyte),
3479 // it will cost 1 second to transfer these data in UDMA mode 2(33.3MBps).
3480 // So set the variable Count to 2000, for about 2 second timeout time.
3481 //
3482 Count = 2000;
3483 while (TRUE) {
3484
3485 IdeDev->PciIo->Io.Read (
3486 IdeDev->PciIo,
3487 EfiPciIoWidthUint8,
3488 EFI_PCI_IO_PASS_THROUGH_BAR,
3489 IoPortForBmis,
3490 1,
3491 &RegisterValue
3492 );
3493 if ((RegisterValue & (BMIS_INTERRUPT | BMIS_ERROR)) || (Count == 0)) {
3494 if ((RegisterValue & BMIS_ERROR) || (Count == 0)) {
3495 //
3496 // Clear START bit of BMIC register before return EFI_DEVICE_ERROR
3497 //
3498 IdeDev->PciIo->Io.Read (
3499 IdeDev->PciIo,
3500 EfiPciIoWidthUint8,
3501 EFI_PCI_IO_PASS_THROUGH_BAR,
3502 IoPortForBmic,
3503 1,
3504 &RegisterValue
3505 );
3506
3507 RegisterValue &= ~((UINT8)BMIC_START);
3508
3509 IdeDev->PciIo->Io.Write (
3510 IdeDev->PciIo,
3511 EfiPciIoWidthUint8,
3512 EFI_PCI_IO_PASS_THROUGH_BAR,
3513 IoPortForBmic,
3514 1,
3515 &RegisterValue
3516 );
3517 gBS->FreePages (MemPage, PageCount);
3518 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
3519 return EFI_DEVICE_ERROR;
3520 }
3521 break;
3522 }
3523
3524 gBS->Stall (1000);
3525 Count --;
3526 }
3527
3528 gBS->FreePages (MemPage, PageCount);
3529 IdeDev->PciIo->Unmap (IdeDev->PciIo, Map);
3530
3531 //
3532 // Read Status Register of IDE device to clear interrupt
3533 //
3534 RegisterValue = IDEReadPortB(IdeDev->PciIo,IdeDev->IoPort->Reg.Status);
3535 //
3536 // Clear START bit of BMIC register
3537 //
3538 IdeDev->PciIo->Io.Read (
3539 IdeDev->PciIo,
3540 EfiPciIoWidthUint8,
3541 EFI_PCI_IO_PASS_THROUGH_BAR,
3542 IoPortForBmic,
3543 1,
3544 &RegisterValue
3545 );
3546
3547 RegisterValue &= ~((UINT8) BMIC_START);
3548
3549 IdeDev->PciIo->Io.Write (
3550 IdeDev->PciIo,
3551 EfiPciIoWidthUint8,
3552 EFI_PCI_IO_PASS_THROUGH_BAR,
3553 IoPortForBmic,
3554 1,
3555 &RegisterValue
3556 );
3557
3558 DataBuffer = (UINT8 *) DataBuffer + NumberOfBlocks * IdeDev->BlkIo.Media->BlockSize;
3559 StartLba += NumberOfBlocks;
3560 }
3561
3562 //
3563 // Disable interrupt of Select device
3564 //
3565 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl);
3566 DeviceControl |= IEN_L;
3567 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, DeviceControl);
3568
3569 return EFI_SUCCESS;
3570 }