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