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