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