]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Pci/IdeBusDxe/Atapi.c
1f989e72f67dd239865f88942ff3a911fe8ed662
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Pci / IdeBusDxe / Atapi.c
1 /** @file
2 Copyright (c) 2006 - 2008, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 **/
12
13 #include "IdeBus.h"
14
15 /**
16 This function is used to get the current status of the media residing
17 in the LS-120 drive or ZIP drive. The media status is returned in the
18 Error Status.
19
20 @param[in] *IdeDev
21 pointer pointing to IDE_BLK_IO_DEV data structure, used
22 to record all the information of the IDE device.
23
24 @retval EFI_SUCCESS
25 The media status is achieved successfully and the media
26 can be read/written.
27
28 @retval EFI_DEVICE_ERROR
29 Get Media Status Command is failed.
30
31 @retval EFI_NO_MEDIA
32 There is no media in the drive.
33
34 @retval EFI_WRITE_PROTECTED
35 The media is writing protected.
36
37 @note
38 This function must be called after the LS120EnableMediaStatus()
39 with second parameter set to TRUE
40 (means enable media status notification) is called.
41
42 **/
43 EFI_STATUS
44 LS120GetMediaStatus (
45 IN IDE_BLK_IO_DEV *IdeDev
46 )
47 {
48 UINT8 DeviceSelect;
49 UINT8 StatusValue;
50 EFI_STATUS EfiStatus;
51 //
52 // Poll Alternate Register for BSY clear within timeout.
53 //
54 EfiStatus = WaitForBSYClear2 (IdeDev, ATATIMEOUT);
55 if (EFI_ERROR (EfiStatus)) {
56 return EFI_DEVICE_ERROR;
57 }
58
59 //
60 // Select device via Device/Head Register.
61 //
62 DeviceSelect = (UINT8) ((IdeDev->Device) << 4 | 0xe0);
63 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, DeviceSelect);
64
65 //
66 // Poll Alternate Register for DRDY set within timeout.
67 // After device is selected, DRDY set indicates the device is ready to
68 // accept command.
69 //
70 EfiStatus = DRDYReady2 (IdeDev, ATATIMEOUT);
71 if (EFI_ERROR (EfiStatus)) {
72 return EFI_DEVICE_ERROR;
73 }
74
75 //
76 // Get Media Status Command is sent
77 //
78 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, 0xDA);
79
80 //
81 // BSY bit will clear after command is complete.
82 //
83 EfiStatus = WaitForBSYClear2 (IdeDev, ATATIMEOUT);
84 if (EFI_ERROR (EfiStatus)) {
85 return EFI_DEVICE_ERROR;
86 }
87
88 //
89 // the media status is returned by the command in the ERROR register
90 //
91 StatusValue = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Error);
92
93 if ((StatusValue & BIT1) != 0) {
94 return EFI_NO_MEDIA;
95 }
96
97 if ((StatusValue & BIT6) != 0) {
98 return EFI_WRITE_PROTECTED;
99 } else {
100 return EFI_SUCCESS;
101 }
102 }
103
104 /**
105 This function is used to send Enable Media Status Notification Command
106 or Disable Media Status Notification Command.
107
108 @param[in] *IdeDev
109 pointer pointing to IDE_BLK_IO_DEV data structure, used
110 to record all the information of the IDE device.
111
112 @param[in] Enable
113 a flag that indicates whether enable or disable media
114 status notification.
115
116 @retval EFI_SUCCESS
117 If command completes successfully.
118
119 @retval EFI_DEVICE_ERROR
120 If command failed.
121
122 **/
123 EFI_STATUS
124 LS120EnableMediaStatus (
125 IN IDE_BLK_IO_DEV *IdeDev,
126 IN BOOLEAN Enable
127 )
128 {
129 UINT8 DeviceSelect;
130 EFI_STATUS Status;
131
132 //
133 // Poll Alternate Register for BSY clear within timeout.
134 //
135 Status = WaitForBSYClear2 (IdeDev, ATATIMEOUT);
136 if (EFI_ERROR (Status)) {
137 return EFI_DEVICE_ERROR;
138 }
139
140 //
141 // Select device via Device/Head Register.
142 //
143 DeviceSelect = (UINT8) ((IdeDev->Device) << 4 | 0xe0);
144 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, DeviceSelect);
145
146 //
147 // Poll Alternate Register for DRDY set within timeout.
148 // After device is selected, DRDY set indicates the device is ready to
149 // accept command.
150 //
151 Status = DRDYReady2 (IdeDev, ATATIMEOUT);
152 if (EFI_ERROR (Status)) {
153 return EFI_DEVICE_ERROR;
154 }
155
156 if (Enable) {
157 //
158 // 0x95: Enable media status notification
159 //
160 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x95);
161 } else {
162 //
163 // 0x31: Disable media status notification
164 //
165 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x31);
166 }
167 //
168 // Set Feature Command is sent
169 //
170 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, 0xEF);
171
172 //
173 // BSY bit will clear after command is complete.
174 //
175 Status = WaitForBSYClear (IdeDev, ATATIMEOUT);
176 if (EFI_ERROR (Status)) {
177 return EFI_DEVICE_ERROR;
178 }
179
180 return EFI_SUCCESS;
181 }
182
183 /**
184 This function is called by DiscoverIdeDevice() during its device
185 identification.
186
187 Its main purpose is to get enough information for the device media
188 to fill in the Media data structure of the Block I/O Protocol interface.
189
190 There are 5 steps to reach such objective:
191
192 1. Sends out the ATAPI Identify Command to the specified device.
193 Only ATAPI device responses to this command. If the command succeeds,
194 it returns the Identify data structure which filled with information
195 about the device. Since the ATAPI device contains removable media,
196 the only meaningful information is the device module name.
197
198 2. Sends out ATAPI Inquiry Packet Command to the specified device.
199 This command will return inquiry data of the device, which contains
200 the device type information.
201
202 3. Allocate sense data space for future use. We don't detect the media
203 presence here to improvement boot performance, especially when CD
204 media is present. The media detection will be performed just before
205 each BLK_IO read/write
206
207 @param[in] *IdeDev
208 pointer pointing to IDE_BLK_IO_DEV data structure, used
209 to record all the information of the IDE device.
210
211 @retval EFI_SUCCESS
212 Identify ATAPI device successfully.
213
214 @retval EFI_DEVICE_ERROR
215 ATAPI Identify Device Command failed or device type
216 is not supported by this IDE driver.
217
218 @note
219 Parameter "IdeDev" will be updated in this function.
220
221 TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
222 TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
223 **/
224 EFI_STATUS
225 ATAPIIdentify (
226 IN IDE_BLK_IO_DEV *IdeDev
227 )
228 {
229 EFI_IDENTIFY_DATA *AtapiIdentifyPointer;
230 UINT8 DeviceSelect;
231 EFI_STATUS Status;
232
233 //
234 // device select bit
235 //
236 DeviceSelect = (UINT8) ((IdeDev->Device) << 4);
237
238 AtapiIdentifyPointer = AllocatePool (sizeof (EFI_IDENTIFY_DATA));
239 if (AtapiIdentifyPointer == NULL) {
240 return EFI_OUT_OF_RESOURCES;
241 }
242 //
243 // Send ATAPI Identify Command to get IDENTIFY data.
244 //
245 Status = AtaPioDataIn (
246 IdeDev,
247 (VOID *) AtapiIdentifyPointer,
248 sizeof (EFI_IDENTIFY_DATA),
249 ATA_CMD_IDENTIFY_DEVICE,
250 DeviceSelect,
251 0,
252 0,
253 0,
254 0
255 );
256
257 if (EFI_ERROR (Status)) {
258 gBS->FreePool (AtapiIdentifyPointer);
259 return EFI_DEVICE_ERROR;
260 }
261
262 IdeDev->pIdData = AtapiIdentifyPointer;
263 PrintAtaModuleName (IdeDev);
264
265 //
266 // Send ATAPI Inquiry Packet Command to get INQUIRY data.
267 //
268 Status = AtapiInquiry (IdeDev);
269 if (EFI_ERROR (Status)) {
270 gBS->FreePool (IdeDev->pIdData);
271 //
272 // Make sure the pIdData will not be freed again.
273 //
274 IdeDev->pIdData = NULL;
275 return EFI_DEVICE_ERROR;
276 }
277 //
278 // Get media removable info from INQUIRY data.
279 //
280 IdeDev->BlkIo.Media->RemovableMedia = (UINT8) ((IdeDev->pInquiryData->RMB & 0x80) == 0x80);
281
282 //
283 // Identify device type via INQUIRY data.
284 //
285 switch (IdeDev->pInquiryData->peripheral_type & 0x1f) {
286
287 //
288 // Magnetic Disk
289 //
290 case 0x00:
291
292 //
293 // device is LS120 or ZIP drive.
294 //
295 IdeDev->Type = IdeMagnetic;
296
297 IdeDev->BlkIo.Media->MediaId = 0;
298 //
299 // Give initial value
300 //
301 IdeDev->BlkIo.Media->MediaPresent = FALSE;
302
303 IdeDev->BlkIo.Media->LastBlock = 0;
304 IdeDev->BlkIo.Media->BlockSize = 0x200;
305 break;
306
307 //
308 // CD-ROM
309 //
310 case 0x05:
311
312 IdeDev->Type = IdeCdRom;
313 IdeDev->BlkIo.Media->MediaId = 0;
314 //
315 // Give initial value
316 //
317 IdeDev->BlkIo.Media->MediaPresent = FALSE;
318
319 IdeDev->BlkIo.Media->LastBlock = 0;
320 IdeDev->BlkIo.Media->BlockSize = 0x800;
321 IdeDev->BlkIo.Media->ReadOnly = TRUE;
322 break;
323
324 //
325 // Tape
326 //
327 case 0x01:
328
329 //
330 // WORM
331 //
332 case 0x04:
333
334 //
335 // Optical
336 //
337 case 0x07:
338
339 default:
340 IdeDev->Type = IdeUnknown;
341 gBS->FreePool (IdeDev->pIdData);
342 gBS->FreePool (IdeDev->pInquiryData);
343 //
344 // Make sure the pIdData and pInquiryData will not be freed again.
345 //
346 IdeDev->pIdData = NULL;
347 IdeDev->pInquiryData = NULL;
348 return EFI_DEVICE_ERROR;
349 }
350
351 //
352 // original sense data numbers
353 //
354 IdeDev->SenseDataNumber = 20;
355
356 IdeDev->SenseData = AllocatePool (IdeDev->SenseDataNumber * sizeof (ATAPI_REQUEST_SENSE_DATA));
357 if (IdeDev->SenseData == NULL) {
358 gBS->FreePool (IdeDev->pIdData);
359 gBS->FreePool (IdeDev->pInquiryData);
360 //
361 // Make sure the pIdData and pInquiryData will not be freed again.
362 //
363 IdeDev->pIdData = NULL;
364 IdeDev->pInquiryData = NULL;
365 return EFI_OUT_OF_RESOURCES;
366 }
367
368 return EFI_SUCCESS;
369 }
370
371 /**
372 Sends out ATAPI Inquiry Packet Command to the specified device.
373 This command will return INQUIRY data of the device.
374
375 @param[in] *IdeDev
376 pointer pointing to IDE_BLK_IO_DEV data structure, used
377 to record all the information of the IDE device.
378
379 @retval EFI_SUCCESS
380 Inquiry command completes successfully.
381
382 @retval EFI_DEVICE_ERROR
383 Inquiry command failed.
384
385 @note
386 Parameter "IdeDev" will be updated in this function.
387
388 **/
389 EFI_STATUS
390 AtapiInquiry (
391 IN IDE_BLK_IO_DEV *IdeDev
392 )
393 {
394 ATAPI_PACKET_COMMAND Packet;
395 EFI_STATUS Status;
396 ATAPI_INQUIRY_DATA *InquiryData;
397
398 //
399 // prepare command packet for the ATAPI Inquiry Packet Command.
400 //
401 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
402 Packet.Inquiry.opcode = ATA_CMD_INQUIRY;
403 Packet.Inquiry.page_code = 0;
404 Packet.Inquiry.allocation_length = sizeof (ATAPI_INQUIRY_DATA);
405
406 InquiryData = AllocatePool (sizeof (ATAPI_INQUIRY_DATA));
407 if (InquiryData == NULL) {
408 return EFI_DEVICE_ERROR;
409 }
410
411 //
412 // Send command packet and get requested Inquiry data.
413 //
414 Status = AtapiPacketCommandIn (
415 IdeDev,
416 &Packet,
417 (UINT16 *) InquiryData,
418 sizeof (ATAPI_INQUIRY_DATA),
419 ATAPITIMEOUT
420 );
421 if (EFI_ERROR (Status)) {
422 gBS->FreePool (InquiryData);
423 return EFI_DEVICE_ERROR;
424 }
425
426 IdeDev->pInquiryData = InquiryData;
427
428 return EFI_SUCCESS;
429 }
430
431 /**
432 This function is used to send out ATAPI commands conforms to the
433 Packet Command with PIO Data In Protocol.
434
435 @param[in] *IdeDev
436 pointer pointing to IDE_BLK_IO_DEV data structure, used
437 to record all the information of the IDE device.
438
439 @param[in] *Packet
440 pointer pointing to ATAPI_PACKET_COMMAND data structure
441 which contains the contents of the command.
442
443 @param[in] *Buffer
444 buffer contained data transferred from device to host.
445
446 @param[in] ByteCount
447 data size in byte unit of the buffer.
448
449 @param[in] TimeOut
450 this parameter is used to specify the timeout
451 value for the PioReadWriteData() function.
452
453 @retval EFI_SUCCESS
454 send out the ATAPI packet command successfully
455 and device sends data successfully.
456
457 @retval EFI_DEVICE_ERROR
458 the device failed to send data.
459
460 **/
461 EFI_STATUS
462 AtapiPacketCommandIn (
463 IN IDE_BLK_IO_DEV *IdeDev,
464 IN ATAPI_PACKET_COMMAND *Packet,
465 IN UINT16 *Buffer,
466 IN UINT32 ByteCount,
467 IN UINTN TimeOut
468 )
469 {
470 UINT16 *CommandIndex;
471 EFI_STATUS Status;
472 UINT32 Count;
473
474 //
475 // Set all the command parameters by fill related registers.
476 // Before write to all the following registers, BSY and DRQ must be 0.
477 //
478 Status = DRQClear2 (IdeDev, ATAPITIMEOUT);
479 if (EFI_ERROR (Status)) {
480 return Status;
481 }
482
483 //
484 // Select device via Device/Head Register.
485 //
486 IDEWritePortB (
487 IdeDev->PciIo,
488 IdeDev->IoPort->Head,
489 (UINT8) ((IdeDev->Device << 4) | ATA_DEFAULT_CMD) // DEFAULT_CMD: 0xa0 (1010,0000)
490 );
491
492 //
493 // No OVL; No DMA
494 //
495 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x00);
496
497 //
498 // set the transfersize to ATAPI_MAX_BYTE_COUNT to let the device
499 // determine how many data should be transferred.
500 //
501 IDEWritePortB (
502 IdeDev->PciIo,
503 IdeDev->IoPort->CylinderLsb,
504 (UINT8) (ATAPI_MAX_BYTE_COUNT & 0x00ff)
505 );
506 IDEWritePortB (
507 IdeDev->PciIo,
508 IdeDev->IoPort->CylinderMsb,
509 (UINT8) (ATAPI_MAX_BYTE_COUNT >> 8)
510 );
511
512 //
513 // ATA_DEFAULT_CTL:0x0a (0000,1010)
514 // Disable interrupt
515 //
516 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, ATA_DEFAULT_CTL);
517
518 //
519 // Send Packet command to inform device
520 // that the following data bytes are command packet.
521 //
522 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, ATA_CMD_PACKET);
523
524 Status = DRQReady (IdeDev, ATAPITIMEOUT);
525 if (EFI_ERROR (Status)) {
526 return Status;
527 }
528
529 //
530 // Send out command packet
531 //
532 CommandIndex = Packet->Data16;
533 for (Count = 0; Count < 6; Count++, CommandIndex++) {
534
535 IDEWritePortW (IdeDev->PciIo, IdeDev->IoPort->Data, *CommandIndex);
536 gBS->Stall (10);
537 }
538
539 //
540 // call PioReadWriteData() function to get
541 // requested transfer data form device.
542 //
543 return PioReadWriteData (IdeDev, Buffer, ByteCount, 1, TimeOut);
544 }
545
546 /**
547 This function is used to send out ATAPI commands conforms to the
548 Packet Command with PIO Data Out Protocol.
549
550 @param[in] *IdeDev
551 pointer pointing to IDE_BLK_IO_DEV data structure, used
552 to record all the information of the IDE device.
553
554 @param[in] *Packet
555 pointer pointing to ATAPI_PACKET_COMMAND data structure
556 which contains the contents of the command.
557
558 @param[in] *Buffer
559 buffer contained data transferred from host to device.
560
561 @param[in] ByteCount
562 data size in byte unit of the buffer.
563
564 @param[in] TimeOut
565 this parameter is used to specify the timeout
566 value for the PioReadWriteData() function.
567
568 @retval EFI_SUCCESS
569 send out the ATAPI packet command successfully
570 and device received data successfully.
571
572 @retval EFI_DEVICE_ERROR
573 the device failed to send data.
574
575 **/
576 EFI_STATUS
577 AtapiPacketCommandOut (
578 IN IDE_BLK_IO_DEV *IdeDev,
579 IN ATAPI_PACKET_COMMAND *Packet,
580 IN UINT16 *Buffer,
581 IN UINT32 ByteCount,
582 IN UINTN TimeOut
583 )
584 {
585 UINT16 *CommandIndex;
586 EFI_STATUS Status;
587 UINT32 Count;
588
589 //
590 // set all the command parameters
591 // Before write to all the following registers, BSY and DRQ must be 0.
592 //
593 Status = DRQClear2 (IdeDev, ATAPITIMEOUT);
594 if (EFI_ERROR (Status)) {
595 return Status;
596 }
597
598 //
599 // Select device via Device/Head Register.
600 //
601 IDEWritePortB (
602 IdeDev->PciIo,
603 IdeDev->IoPort->Head,
604 (UINT8) ((IdeDev->Device << 4) | ATA_DEFAULT_CMD) // ATA_DEFAULT_CMD: 0xa0 (1010,0000)
605 );
606
607 //
608 // No OVL; No DMA
609 //
610 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg1.Feature, 0x00);
611
612 //
613 // set the transfersize to ATAPI_MAX_BYTE_COUNT to
614 // let the device determine how many data should be transferred.
615 //
616 IDEWritePortB (
617 IdeDev->PciIo,
618 IdeDev->IoPort->CylinderLsb,
619 (UINT8) (ATAPI_MAX_BYTE_COUNT & 0x00ff)
620 );
621 IDEWritePortB (
622 IdeDev->PciIo,
623 IdeDev->IoPort->CylinderMsb,
624 (UINT8) (ATAPI_MAX_BYTE_COUNT >> 8)
625 );
626
627 //
628 // DEFAULT_CTL:0x0a (0000,1010)
629 // Disable interrupt
630 //
631 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Alt.DeviceControl, ATA_DEFAULT_CTL);
632
633 //
634 // Send Packet command to inform device
635 // that the following data bytes are command packet.
636 //
637 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, ATA_CMD_PACKET);
638
639 Status = DRQReady2 (IdeDev, ATAPITIMEOUT);
640 if (EFI_ERROR (Status)) {
641 return Status;
642 }
643
644 //
645 // Send out command packet
646 //
647 CommandIndex = Packet->Data16;
648 for (Count = 0; Count < 6; Count++, CommandIndex++) {
649 IDEWritePortW (IdeDev->PciIo, IdeDev->IoPort->Data, *CommandIndex);
650 gBS->Stall (10);
651 }
652
653 //
654 // call PioReadWriteData() function to send requested transfer data to device.
655 //
656 return PioReadWriteData (IdeDev, Buffer, ByteCount, 0, TimeOut);
657 }
658
659 /**
660 This function is called by either AtapiPacketCommandIn() or
661 AtapiPacketCommandOut(). It is used to transfer data between
662 host and device. The data direction is specified by the fourth
663 parameter.
664
665 @param[in] *IdeDev
666 pointer pointing to IDE_BLK_IO_DEV data structure, used
667 to record all the information of the IDE device.
668
669 @param[in] *Buffer
670 buffer contained data transferred between host and device.
671
672 @param[in] ByteCount
673 data size in byte unit of the buffer.
674
675 @param[in] Read
676 flag used to determine the data transfer direction.
677 Read equals 1, means data transferred from device to host;
678 Read equals 0, means data transferred from host to device.
679
680 @param[in] TimeOut
681 timeout value for wait DRQ ready before each data
682 stream's transfer.
683
684 @retval EFI_SUCCESS
685 data is transferred successfully.
686
687 @retval EFI_DEVICE_ERROR
688 the device failed to transfer data.
689
690 **/
691 EFI_STATUS
692 PioReadWriteData (
693 IN IDE_BLK_IO_DEV *IdeDev,
694 IN UINT16 *Buffer,
695 IN UINT32 ByteCount,
696 IN BOOLEAN Read,
697 IN UINTN TimeOut
698 )
699 {
700 //
701 // required transfer data in word unit.
702 //
703 UINT32 RequiredWordCount;
704
705 //
706 // actual transfer data in word unit.
707 //
708 UINT32 ActualWordCount;
709 UINT32 WordCount;
710 EFI_STATUS Status;
711 UINT16 *PtrBuffer;
712
713 //
714 // No data transfer is premitted.
715 //
716 if (ByteCount == 0) {
717 return EFI_SUCCESS;
718 }
719 //
720 // for performance, we assert the ByteCount is an even number
721 // which is actually a resonable assumption
722 ASSERT((ByteCount%2) == 0);
723
724 PtrBuffer = Buffer;
725 RequiredWordCount = ByteCount / 2;
726 //
727 // ActuralWordCount means the word count of data really transferred.
728 //
729 ActualWordCount = 0;
730
731 while (ActualWordCount < RequiredWordCount) {
732
733 //
734 // before each data transfer stream, the host should poll DRQ bit ready,
735 // to see whether indicates device is ready to transfer data.
736 //
737 Status = DRQReady2 (IdeDev, TimeOut);
738 if (EFI_ERROR (Status)) {
739 return CheckErrorStatus (IdeDev);
740 }
741
742 //
743 // read Status Register will clear interrupt
744 //
745 IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Status);
746
747 //
748 // get current data transfer size from Cylinder Registers.
749 //
750 WordCount = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderMsb) << 8;
751 WordCount = WordCount | IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->CylinderLsb);
752 WordCount = WordCount & 0xffff;
753 WordCount /= 2;
754
755 WordCount = MIN (WordCount, (RequiredWordCount - ActualWordCount));
756
757 if (Read) {
758 IDEReadPortWMultiple (
759 IdeDev->PciIo,
760 IdeDev->IoPort->Data,
761 WordCount,
762 PtrBuffer
763 );
764 } else {
765 IDEWritePortWMultiple (
766 IdeDev->PciIo,
767 IdeDev->IoPort->Data,
768 WordCount,
769 PtrBuffer
770 );
771 }
772
773 PtrBuffer += WordCount;
774 ActualWordCount += WordCount;
775 }
776
777 if (Read) {
778 //
779 // In the case where the drive wants to send more data than we need to read,
780 // the DRQ bit will be set and cause delays from DRQClear2().
781 // We need to read data from the drive until it clears DRQ so we can move on.
782 //
783 AtapiReadPendingData (IdeDev);
784 }
785
786 //
787 // After data transfer is completed, normally, DRQ bit should clear.
788 //
789 Status = DRQClear2 (IdeDev, ATAPITIMEOUT);
790 if (EFI_ERROR (Status)) {
791 return EFI_DEVICE_ERROR;
792 }
793
794 //
795 // read status register to check whether error happens.
796 //
797 return CheckErrorStatus (IdeDev);
798 }
799
800 /**
801 Sends out ATAPI Test Unit Ready Packet Command to the specified device
802 to find out whether device is accessible.
803
804 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
805 to record all the information of the IDE device.
806 @param[out] *SResult Sense result for this packet command.
807
808 @retval EFI_SUCCESS Device is accessible.
809 @retval EFI_DEVICE_ERROR Device is not accessible.
810
811 **/
812 EFI_STATUS
813 AtapiTestUnitReady (
814 IN IDE_BLK_IO_DEV *IdeDev,
815 OUT SENSE_RESULT *SResult
816 )
817 {
818 ATAPI_PACKET_COMMAND Packet;
819 EFI_STATUS Status;
820 UINTN SenseCount;
821
822 //
823 // fill command packet
824 //
825 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
826 Packet.TestUnitReady.opcode = ATA_CMD_TEST_UNIT_READY;
827
828 //
829 // send command packet
830 //
831 Status = AtapiPacketCommandIn (IdeDev, &Packet, NULL, 0, ATAPITIMEOUT);
832 if (EFI_ERROR (Status)) {
833 return Status;
834 }
835
836 Status = AtapiRequestSense (IdeDev, &SenseCount);
837 if (EFI_ERROR (Status)) {
838 return Status;
839 }
840
841 ParseSenseData (IdeDev, SenseCount, SResult);
842 return EFI_SUCCESS;
843 }
844
845 /**
846 Sends out ATAPI Request Sense Packet Command to the specified device.
847 This command will return all the current Sense data in the device.
848 This function will pack all the Sense data in one single buffer.
849
850 @param[in] *IdeDev
851 pointer pointing to IDE_BLK_IO_DEV data structure, used
852 to record all the information of the IDE device.
853
854 @param[out] **SenseCounts
855 allocated in this function, and freed by the calling function.
856 This buffer is used to accommodate all the sense data returned
857 by the device.
858
859 @retval EFI_SUCCESS
860 Request Sense command completes successfully.
861
862 @retval EFI_DEVICE_ERROR
863 Request Sense command failed.
864
865 **/
866 EFI_STATUS
867 AtapiRequestSense (
868 IN IDE_BLK_IO_DEV *IdeDev,
869 OUT UINTN *SenseCounts
870 )
871 {
872 EFI_STATUS Status;
873 ATAPI_REQUEST_SENSE_DATA *Sense;
874 UINT16 *Ptr;
875 BOOLEAN FetchSenseData;
876 ATAPI_PACKET_COMMAND Packet;
877
878 *SenseCounts = 0;
879
880 ZeroMem (IdeDev->SenseData, sizeof (ATAPI_REQUEST_SENSE_DATA) * (IdeDev->SenseDataNumber));
881 //
882 // fill command packet for Request Sense Packet Command
883 //
884 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
885 Packet.RequestSence.opcode = ATA_CMD_REQUEST_SENSE;
886 Packet.RequestSence.allocation_length = sizeof (ATAPI_REQUEST_SENSE_DATA);
887
888 //
889 // initialize pointer
890 //
891 Ptr = (UINT16 *) IdeDev->SenseData;
892 //
893 // request sense data from device continuously until no sense data
894 // exists in the device.
895 //
896 for (FetchSenseData = TRUE; FetchSenseData;) {
897
898 Sense = (ATAPI_REQUEST_SENSE_DATA *) Ptr;
899
900 //
901 // send out Request Sense Packet Command and get one Sense data form device
902 //
903 Status = AtapiPacketCommandIn (
904 IdeDev,
905 &Packet,
906 Ptr,
907 sizeof (ATAPI_REQUEST_SENSE_DATA),
908 ATAPITIMEOUT
909 );
910 //
911 // failed to get Sense data
912 //
913 if (EFI_ERROR (Status)) {
914 if (*SenseCounts == 0) {
915 return EFI_DEVICE_ERROR;
916 } else {
917 return EFI_SUCCESS;
918 }
919 }
920
921 (*SenseCounts)++;
922 //
923 // We limit MAX sense data count to 20 in order to avoid dead loop. Some
924 // incompatible ATAPI devices don't retrive NO_SENSE when there is no media.
925 // In this case, dead loop occurs if we don't have a gatekeeper. 20 is
926 // supposed to be large enough for any ATAPI device.
927 //
928 if ((Sense->sense_key != ATA_SK_NO_SENSE) && ((*SenseCounts) < 20)) {
929 //
930 // Ptr is word-based pointer
931 //
932 Ptr += (sizeof (ATAPI_REQUEST_SENSE_DATA) + 1) >> 1;
933
934 } else {
935 //
936 // when no sense key, skip out the loop
937 //
938 FetchSenseData = FALSE;
939 }
940 }
941
942 return EFI_SUCCESS;
943 }
944
945 /**
946 Sends out ATAPI Read Capacity Packet Command to the specified device.
947 This command will return the information regarding the capacity of the
948 media in the device.
949
950 Current device status will impact device's response to the Read Capacity
951 Command. For example, if the device once reset, the Read Capacity
952 Command will fail. The Sense data record the current device status, so
953 if the Read Capacity Command failed, the Sense data must be requested
954 and be analyzed to determine if the Read Capacity Command should retry.
955
956 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
957 to record all the information of the IDE device.
958 @param[out] SResult Sense result for this packet command
959
960 @retval EFI_SUCCESS Read Capacity Command finally completes successfully.
961 @retval EFI_DEVICE_ERROR Read Capacity Command failed because of device error.
962
963 @note Parameter "IdeDev" will be updated in this function.
964
965 TODO: EFI_NOT_READY - add return value to function comment
966 **/
967 EFI_STATUS
968 AtapiReadCapacity (
969 IN IDE_BLK_IO_DEV *IdeDev,
970 OUT SENSE_RESULT *SResult
971 )
972 {
973 //
974 // status returned by Read Capacity Packet Command
975 //
976 EFI_STATUS Status;
977 EFI_STATUS SenseStatus;
978 ATAPI_PACKET_COMMAND Packet;
979 UINTN SenseCount;
980
981 //
982 // used for capacity data returned from ATAPI device
983 //
984 ATAPI_READ_CAPACITY_DATA Data;
985 ATAPI_READ_FORMAT_CAPACITY_DATA FormatData;
986
987 ZeroMem (&Data, sizeof (Data));
988 ZeroMem (&FormatData, sizeof (FormatData));
989
990 if (IdeDev->Type == IdeCdRom) {
991
992 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
993 Packet.Inquiry.opcode = ATA_CMD_READ_CAPACITY;
994 Status = AtapiPacketCommandIn (
995 IdeDev,
996 &Packet,
997 (UINT16 *) &Data,
998 sizeof (ATAPI_READ_CAPACITY_DATA),
999 ATAPITIMEOUT
1000 );
1001
1002 } else {
1003 //
1004 // Type == IdeMagnetic
1005 //
1006 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
1007 Packet.ReadFormatCapacity.opcode = ATA_CMD_READ_FORMAT_CAPACITY;
1008 Packet.ReadFormatCapacity.allocation_length_lo = 12;
1009 Status = AtapiPacketCommandIn (
1010 IdeDev,
1011 &Packet,
1012 (UINT16 *) &FormatData,
1013 sizeof (ATAPI_READ_FORMAT_CAPACITY_DATA),
1014 ATAPITIMEOUT
1015 );
1016 }
1017
1018 if (Status == EFI_TIMEOUT) {
1019 return Status;
1020 }
1021
1022 SenseStatus = AtapiRequestSense (IdeDev, &SenseCount);
1023
1024 if (!EFI_ERROR (SenseStatus)) {
1025 ParseSenseData (IdeDev, SenseCount, SResult);
1026
1027 if (!EFI_ERROR (Status) && *SResult == SenseNoSenseKey) {
1028 if (IdeDev->Type == IdeCdRom) {
1029
1030 IdeDev->BlkIo.Media->LastBlock = (Data.LastLba3 << 24) |
1031 (Data.LastLba2 << 16) |
1032 (Data.LastLba1 << 8) |
1033 Data.LastLba0;
1034
1035 IdeDev->BlkIo.Media->MediaPresent = TRUE;
1036
1037 IdeDev->BlkIo.Media->ReadOnly = TRUE;
1038
1039 //
1040 // Because the user data portion in the sector of the Data CD supported
1041 // is always 0x800
1042 //
1043 IdeDev->BlkIo.Media->BlockSize = 0x800;
1044 }
1045
1046 if (IdeDev->Type == IdeMagnetic) {
1047
1048 if (FormatData.DesCode == 3) {
1049 IdeDev->BlkIo.Media->MediaPresent = FALSE;
1050 IdeDev->BlkIo.Media->LastBlock = 0;
1051 } else {
1052
1053 IdeDev->BlkIo.Media->LastBlock = (FormatData.LastLba3 << 24) |
1054 (FormatData.LastLba2 << 16) |
1055 (FormatData.LastLba1 << 8) |
1056 FormatData.LastLba0;
1057 if (IdeDev->BlkIo.Media->LastBlock != 0) {
1058 IdeDev->BlkIo.Media->LastBlock--;
1059
1060 IdeDev->BlkIo.Media->BlockSize = (FormatData.BlockSize2 << 16) |
1061 (FormatData.BlockSize1 << 8) |
1062 FormatData.BlockSize0;
1063
1064 IdeDev->BlkIo.Media->MediaPresent = TRUE;
1065 } else {
1066 IdeDev->BlkIo.Media->MediaPresent = FALSE;
1067 //
1068 // Return EFI_NOT_READY operation succeeds but returned capacity is 0
1069 //
1070 return EFI_NOT_READY;
1071 }
1072
1073 IdeDev->BlkIo.Media->BlockSize = 0x200;
1074
1075 }
1076 }
1077 }
1078
1079 return EFI_SUCCESS;
1080
1081 } else {
1082 return EFI_DEVICE_ERROR;
1083 }
1084 }
1085
1086 /**
1087 Used before read/write blocks from/to ATAPI device media.
1088 Since ATAPI device media is removable, it is necessary to detect
1089 whether media is present and get current present media's
1090 information, and if media has been changed, Block I/O Protocol
1091 need to be reinstalled.
1092
1093 @param[in] *IdeDev
1094 pointer pointing to IDE_BLK_IO_DEV data structure, used
1095 to record all the information of the IDE device.
1096
1097 @param[out] *MediaChange
1098 return value that indicates if the media of the device has been
1099 changed.
1100
1101 @retval EFI_SUCCESS
1102 media found successfully.
1103
1104 @retval EFI_DEVICE_ERROR
1105 any error encounters during media detection.
1106
1107 @retval EFI_NO_MEDIA
1108 media not found.
1109
1110 @note
1111 parameter IdeDev may be updated in this function.
1112
1113 **/
1114 EFI_STATUS
1115 AtapiDetectMedia (
1116 IN IDE_BLK_IO_DEV *IdeDev,
1117 OUT BOOLEAN *MediaChange
1118 )
1119 {
1120 EFI_STATUS Status;
1121 EFI_STATUS CleanStateStatus;
1122 EFI_BLOCK_IO_MEDIA OldMediaInfo;
1123 UINTN RetryTimes;
1124 UINTN RetryNotReady;
1125 SENSE_RESULT SResult;
1126 BOOLEAN WriteProtected;
1127
1128 CopyMem (&OldMediaInfo, IdeDev->BlkIo.Media, sizeof (EFI_BLOCK_IO_MEDIA));
1129 *MediaChange = FALSE;
1130 //
1131 // Retry for SenseDeviceNotReadyNeedRetry.
1132 // Each retry takes 1s and we limit the upper boundary to
1133 // 120 times about 2 min.
1134 //
1135 RetryNotReady = 120;
1136
1137 //
1138 // Do Test Unit Ready
1139 //
1140 DoTUR:
1141 //
1142 // Retry 5 times
1143 //
1144 RetryTimes = 5;
1145 while (RetryTimes != 0) {
1146
1147 Status = AtapiTestUnitReady (IdeDev, &SResult);
1148
1149 if (EFI_ERROR (Status)) {
1150 //
1151 // Test Unit Ready error without sense data.
1152 // For some devices, this means there's extra data
1153 // that has not been read, so we read these extra
1154 // data out before going on.
1155 //
1156 CleanStateStatus = AtapiReadPendingData (IdeDev);
1157 if (EFI_ERROR (CleanStateStatus)) {
1158 //
1159 // Busy wait failed, try again
1160 //
1161 RetryTimes--;
1162 }
1163 //
1164 // Try again without counting down RetryTimes
1165 //
1166 continue;
1167 } else {
1168 switch (SResult) {
1169 case SenseNoSenseKey:
1170 if (IdeDev->BlkIo.Media->MediaPresent) {
1171 goto Done;
1172 } else {
1173 //
1174 // Media present but the internal structure need refreshed.
1175 // Try Read Capacity
1176 //
1177 goto DoRC;
1178 }
1179 break;
1180
1181 case SenseDeviceNotReadyNeedRetry:
1182 if (--RetryNotReady == 0) {
1183 return EFI_DEVICE_ERROR;
1184 }
1185 gBS->Stall (1000 * STALL_1_MILLI_SECOND);
1186 continue;
1187 break;
1188
1189 case SenseNoMedia:
1190 IdeDev->BlkIo.Media->MediaPresent = FALSE;
1191 IdeDev->BlkIo.Media->LastBlock = 0;
1192 goto Done;
1193 break;
1194
1195 case SenseDeviceNotReadyNoRetry:
1196 case SenseMediaError:
1197 return EFI_DEVICE_ERROR;
1198
1199 case SenseMediaChange:
1200 IdeDev->BlkIo.Media->MediaId++;
1201 goto DoRC;
1202 break;
1203
1204 default:
1205 RetryTimes--;
1206 break;
1207 }
1208 }
1209 }
1210
1211 return EFI_DEVICE_ERROR;
1212
1213 //
1214 // Do Read Capacity
1215 //
1216 DoRC:
1217 RetryTimes = 5;
1218
1219 while (RetryTimes != 0) {
1220
1221 Status = AtapiReadCapacity (IdeDev, &SResult);
1222
1223 if (EFI_ERROR (Status)) {
1224 RetryTimes--;
1225 continue;
1226 } else {
1227 switch (SResult) {
1228 case SenseNoSenseKey:
1229 goto Done;
1230 break;
1231
1232 case SenseDeviceNotReadyNeedRetry:
1233 //
1234 // We use Test Unit Ready to retry which
1235 // is faster.
1236 //
1237 goto DoTUR;
1238 break;
1239
1240 case SenseNoMedia:
1241 IdeDev->BlkIo.Media->MediaPresent = FALSE;
1242 IdeDev->BlkIo.Media->LastBlock = 0;
1243 goto Done;
1244 break;
1245
1246 case SenseDeviceNotReadyNoRetry:
1247 case SenseMediaError:
1248 return EFI_DEVICE_ERROR;
1249
1250 case SenseMediaChange:
1251 IdeDev->BlkIo.Media->MediaId++;
1252 continue;
1253 break;
1254
1255 default:
1256 RetryTimes--;
1257 break;
1258 }
1259 }
1260 }
1261
1262 return EFI_DEVICE_ERROR;
1263
1264 Done:
1265 //
1266 // the following code is to check the write-protected for LS120 media
1267 //
1268 if ((IdeDev->BlkIo.Media->MediaPresent) && (IdeDev->Type == IdeMagnetic)) {
1269
1270 Status = IsLS120orZipWriteProtected (IdeDev, &WriteProtected);
1271 if (!EFI_ERROR (Status)) {
1272
1273 if (WriteProtected) {
1274
1275 IdeDev->BlkIo.Media->ReadOnly = TRUE;
1276 } else {
1277
1278 IdeDev->BlkIo.Media->ReadOnly = FALSE;
1279 }
1280
1281 }
1282 }
1283
1284 if (IdeDev->BlkIo.Media->MediaId != OldMediaInfo.MediaId) {
1285 //
1286 // Media change information got from the device
1287 //
1288 *MediaChange = TRUE;
1289 }
1290
1291 if (IdeDev->BlkIo.Media->ReadOnly != OldMediaInfo.ReadOnly) {
1292 *MediaChange = TRUE;
1293 IdeDev->BlkIo.Media->MediaId += 1;
1294 }
1295
1296 if (IdeDev->BlkIo.Media->BlockSize != OldMediaInfo.BlockSize) {
1297 *MediaChange = TRUE;
1298 IdeDev->BlkIo.Media->MediaId += 1;
1299 }
1300
1301 if (IdeDev->BlkIo.Media->LastBlock != OldMediaInfo.LastBlock) {
1302 *MediaChange = TRUE;
1303 IdeDev->BlkIo.Media->MediaId += 1;
1304 }
1305
1306 if (IdeDev->BlkIo.Media->MediaPresent != OldMediaInfo.MediaPresent) {
1307 if (IdeDev->BlkIo.Media->MediaPresent) {
1308 //
1309 // when change from no media to media present, reset the MediaId to 1.
1310 //
1311 IdeDev->BlkIo.Media->MediaId = 1;
1312 } else {
1313 //
1314 // when no media, reset the MediaId to zero.
1315 //
1316 IdeDev->BlkIo.Media->MediaId = 0;
1317 }
1318
1319 *MediaChange = TRUE;
1320 }
1321
1322 //
1323 // if any change on current existing media,
1324 // the Block I/O protocol need to be reinstalled.
1325 //
1326 if (*MediaChange) {
1327 gBS->ReinstallProtocolInterface (
1328 IdeDev->Handle,
1329 &gEfiBlockIoProtocolGuid,
1330 &IdeDev->BlkIo,
1331 &IdeDev->BlkIo
1332 );
1333 }
1334
1335 if (IdeDev->BlkIo.Media->MediaPresent) {
1336 return EFI_SUCCESS;
1337 } else {
1338 return EFI_NO_MEDIA;
1339 }
1340 }
1341
1342 /**
1343 This function is called by the AtapiBlkIoReadBlocks() to perform
1344 read from media in block unit.
1345
1346 The main command used to access media here is READ(10) Command.
1347 READ(10) Command requests that the ATAPI device media transfer
1348 specified data to the host. Data is transferred in block(sector)
1349 unit. The maximum number of blocks that can be transferred once is
1350 65536. This is the main difference between READ(10) and READ(12)
1351 Command. The maximum number of blocks in READ(12) is 2 power 32.
1352
1353 @param[in] *IdeDev
1354 pointer pointing to IDE_BLK_IO_DEV data structure, used
1355 to record all the information of the IDE device.
1356
1357 @param[in] *Buffer
1358 A pointer to the destination buffer for the data.
1359
1360 @param[in] Lba
1361 The starting logical block address to read from
1362 on the device media.
1363
1364 @param[in] NumberOfBlocks
1365 The number of transfer data blocks.
1366
1367 @return status is fully dependent on the return status
1368 of AtapiPacketCommandIn() function.
1369
1370 **/
1371 EFI_STATUS
1372 AtapiReadSectors (
1373 IN IDE_BLK_IO_DEV *IdeDev,
1374 IN VOID *Buffer,
1375 IN EFI_LBA Lba,
1376 IN UINTN NumberOfBlocks
1377 )
1378 {
1379
1380 ATAPI_PACKET_COMMAND Packet;
1381 ATAPI_READ10_CMD *Read10Packet;
1382 EFI_STATUS Status;
1383 UINTN BlocksRemaining;
1384 UINT32 Lba32;
1385 UINT32 BlockSize;
1386 UINT32 ByteCount;
1387 UINT16 SectorCount;
1388 VOID *PtrBuffer;
1389 UINT16 MaxBlock;
1390 UINTN TimeOut;
1391
1392 //
1393 // fill command packet for Read(10) command
1394 //
1395 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
1396 Read10Packet = &Packet.Read10;
1397 Lba32 = (UINT32) Lba;
1398 PtrBuffer = Buffer;
1399
1400 BlockSize = IdeDev->BlkIo.Media->BlockSize;
1401
1402 //
1403 // limit the data bytes that can be transferred by one Read(10) Command
1404 //
1405 MaxBlock = 65535;
1406
1407 BlocksRemaining = NumberOfBlocks;
1408
1409 Status = EFI_SUCCESS;
1410 while (BlocksRemaining > 0) {
1411
1412 if (BlocksRemaining <= MaxBlock) {
1413
1414 SectorCount = (UINT16) BlocksRemaining;
1415 } else {
1416
1417 SectorCount = MaxBlock;
1418 }
1419
1420 //
1421 // fill the Packet data structure
1422 //
1423
1424 Read10Packet->opcode = ATA_CMD_READ_10;
1425
1426 //
1427 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1428 // Lba0 is MSB, Lba3 is LSB
1429 //
1430 Read10Packet->Lba3 = (UINT8) (Lba32 & 0xff);
1431 Read10Packet->Lba2 = (UINT8) (Lba32 >> 8);
1432 Read10Packet->Lba1 = (UINT8) (Lba32 >> 16);
1433 Read10Packet->Lba0 = (UINT8) (Lba32 >> 24);
1434
1435 //
1436 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1437 // TranLen0 is MSB, TranLen is LSB
1438 //
1439 Read10Packet->TranLen1 = (UINT8) (SectorCount & 0xff);
1440 Read10Packet->TranLen0 = (UINT8) (SectorCount >> 8);
1441
1442 ByteCount = SectorCount * BlockSize;
1443
1444 if (IdeDev->Type == IdeCdRom) {
1445 TimeOut = CDROMLONGTIMEOUT;
1446 } else {
1447 TimeOut = ATAPILONGTIMEOUT;
1448 }
1449
1450 Status = AtapiPacketCommandIn (
1451 IdeDev,
1452 &Packet,
1453 (UINT16 *) PtrBuffer,
1454 ByteCount,
1455 TimeOut
1456 );
1457 if (EFI_ERROR (Status)) {
1458 return Status;
1459 }
1460
1461 Lba32 += SectorCount;
1462 PtrBuffer = (UINT8 *) PtrBuffer + SectorCount * BlockSize;
1463 BlocksRemaining -= SectorCount;
1464 }
1465
1466 return Status;
1467 }
1468
1469 /**
1470 This function is called by the AtapiBlkIoWriteBlocks() to perform
1471 write onto media in block unit.
1472 The main command used to access media here is Write(10) Command.
1473 Write(10) Command requests that the ATAPI device media transfer
1474 specified data to the host. Data is transferred in block (sector)
1475 unit. The maximum number of blocks that can be transferred once is
1476 65536.
1477
1478 @param[in] *IdeDev
1479 pointer pointing to IDE_BLK_IO_DEV data structure, used
1480 to record all the information of the IDE device.
1481
1482 @param[in] *Buffer
1483 A pointer to the source buffer for the data.
1484
1485 @param[in] Lba
1486 The starting logical block address to write onto
1487 the device media.
1488
1489 @param[in] NumberOfBlocks
1490 The number of transfer data blocks.
1491
1492 @return status is fully dependent on the return status
1493 of AtapiPacketCommandOut() function.
1494
1495 **/
1496 EFI_STATUS
1497 AtapiWriteSectors (
1498 IN IDE_BLK_IO_DEV *IdeDev,
1499 IN VOID *Buffer,
1500 IN EFI_LBA Lba,
1501 IN UINTN NumberOfBlocks
1502 )
1503 {
1504
1505 ATAPI_PACKET_COMMAND Packet;
1506 ATAPI_READ10_CMD *Read10Packet;
1507
1508 EFI_STATUS Status;
1509 UINTN BlocksRemaining;
1510 UINT32 Lba32;
1511 UINT32 BlockSize;
1512 UINT32 ByteCount;
1513 UINT16 SectorCount;
1514 VOID *PtrBuffer;
1515 UINT16 MaxBlock;
1516
1517 //
1518 // fill command packet for Write(10) command
1519 // Write(10) command packet has the same data structure as
1520 // Read(10) command packet,
1521 // so here use the Read10Packet data structure
1522 // for the Write(10) command packet.
1523 //
1524 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
1525 Read10Packet = &Packet.Read10;
1526
1527 Lba32 = (UINT32) Lba;
1528 PtrBuffer = Buffer;
1529
1530 BlockSize = IdeDev->BlkIo.Media->BlockSize;
1531
1532 //
1533 // limit the data bytes that can be transferred by one Read(10) Command
1534 //
1535 MaxBlock = (UINT16) (65536 / BlockSize);
1536
1537 BlocksRemaining = NumberOfBlocks;
1538
1539 Status = EFI_SUCCESS;
1540 while (BlocksRemaining > 0) {
1541
1542 if (BlocksRemaining >= MaxBlock) {
1543 SectorCount = MaxBlock;
1544 } else {
1545 SectorCount = (UINT16) BlocksRemaining;
1546 }
1547
1548 //
1549 // Command code is WRITE_10.
1550 //
1551 Read10Packet->opcode = ATA_CMD_WRITE_10;
1552
1553 //
1554 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1555 // Lba0 is MSB, Lba3 is LSB
1556 //
1557 Read10Packet->Lba3 = (UINT8) (Lba32 & 0xff);
1558 Read10Packet->Lba2 = (UINT8) (Lba32 >> 8);
1559 Read10Packet->Lba1 = (UINT8) (Lba32 >> 16);
1560 Read10Packet->Lba0 = (UINT8) (Lba32 >> 24);
1561
1562 //
1563 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1564 // TranLen0 is MSB, TranLen is LSB
1565 //
1566 Read10Packet->TranLen1 = (UINT8) (SectorCount & 0xff);
1567 Read10Packet->TranLen0 = (UINT8) (SectorCount >> 8);
1568
1569 ByteCount = SectorCount * BlockSize;
1570
1571 Status = AtapiPacketCommandOut (
1572 IdeDev,
1573 &Packet,
1574 (UINT16 *) PtrBuffer,
1575 ByteCount,
1576 ATAPILONGTIMEOUT
1577 );
1578 if (EFI_ERROR (Status)) {
1579 return Status;
1580 }
1581
1582 Lba32 += SectorCount;
1583 PtrBuffer = ((UINT8 *) PtrBuffer + SectorCount * BlockSize);
1584 BlocksRemaining -= SectorCount;
1585 }
1586
1587 return Status;
1588 }
1589
1590 /**
1591 This function is used to implement the Soft Reset on the specified
1592 ATAPI device. Different from the AtaSoftReset(), here reset is a ATA
1593 Soft Reset Command special for ATAPI device, and it only take effects
1594 on the specified ATAPI device, not on the whole IDE bus.
1595 Since the ATAPI soft reset is needed when device is in exceptional
1596 condition (such as BSY bit is always set ), I think the Soft Reset
1597 command should be sent without waiting for the BSY clear and DRDY
1598 set.
1599 This function is called by IdeBlkIoReset(),
1600 a interface function of Block I/O protocol.
1601
1602 @param[in] *IdeDev
1603 pointer pointing to IDE_BLK_IO_DEV data structure, used
1604 to record all the information of the IDE device.
1605
1606 @retval EFI_SUCCESS
1607 Soft reset completes successfully.
1608
1609 @retval EFI_DEVICE_ERROR
1610 Any step during the reset process is failed.
1611
1612 **/
1613 EFI_STATUS
1614 AtapiSoftReset (
1615 IN IDE_BLK_IO_DEV *IdeDev
1616 )
1617 {
1618 UINT8 Command;
1619 UINT8 DeviceSelect;
1620 EFI_STATUS Status;
1621
1622 //
1623 // for ATAPI device, no need to wait DRDY ready after device selecting.
1624 // (bit7 and bit5 are both set to 1 for backward compatibility)
1625 //
1626 DeviceSelect = (UINT8) (((BIT7 | BIT5) | (IdeDev->Device << 4)));
1627 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, DeviceSelect);
1628
1629 Command = ATA_CMD_SOFT_RESET;
1630 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, Command);
1631
1632 //
1633 // BSY cleared is the only status return to the host by the device
1634 // when reset is completed.
1635 // slave device needs at most 31s to clear BSY
1636 //
1637 Status = WaitForBSYClear (IdeDev, 31000);
1638 if (EFI_ERROR (Status)) {
1639 return EFI_DEVICE_ERROR;
1640 }
1641
1642 //
1643 // stall 5 seconds to make the device status stable
1644 //
1645 gBS->Stall (5000000);
1646
1647 return EFI_SUCCESS;
1648 }
1649
1650 /**
1651 This function is the ATAPI implementation for ReadBlocks in the
1652 Block I/O Protocol interface.
1653
1654 @param[in] *IdeBlkIoDevice
1655 Indicates the calling context.
1656
1657 @param[in] MediaId
1658 The media id that the read request is for.
1659
1660 @param[in] LBA
1661 The starting logical block address to read from
1662 on the device.
1663
1664 @param[in] BufferSize
1665 The size of the Buffer in bytes. This must be a
1666 multiple of the intrinsic block size of the device.
1667
1668 @param[out] *Buffer
1669 A pointer to the destination buffer for the data.
1670 The caller is responsible for either having implicit
1671 or explicit ownership of the memory that data is read into.
1672
1673 @retval EFI_SUCCESS
1674 Read Blocks successfully.
1675
1676 @retval EFI_DEVICE_ERROR
1677 Read Blocks failed.
1678
1679 @retval EFI_NO_MEDIA
1680 There is no media in the device.
1681
1682 @retval EFI_MEDIA_CHANGED
1683 The MediaId is not for the current media.
1684
1685 @retval EFI_BAD_BUFFER_SIZE
1686 The BufferSize parameter is not a multiple of the
1687 intrinsic block size of the device.
1688
1689 @retval EFI_INVALID_PARAMETER
1690 The read request contains LBAs that are not valid,
1691 or the data buffer is not valid.
1692
1693 **/
1694 EFI_STATUS
1695 AtapiBlkIoReadBlocks (
1696 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1697 IN UINT32 MediaId,
1698 IN EFI_LBA LBA,
1699 IN UINTN BufferSize,
1700 OUT VOID *Buffer
1701 )
1702 {
1703 EFI_BLOCK_IO_MEDIA *Media;
1704 UINTN BlockSize;
1705 UINTN NumberOfBlocks;
1706 EFI_STATUS Status;
1707
1708 BOOLEAN MediaChange;
1709
1710 if (Buffer == NULL) {
1711 return EFI_INVALID_PARAMETER;
1712 }
1713
1714 if (BufferSize == 0) {
1715 return EFI_SUCCESS;
1716 }
1717
1718 //
1719 // ATAPI device media is removable, so it is a must
1720 // to detect media first before read operation
1721 //
1722 MediaChange = FALSE;
1723 Status = AtapiDetectMedia (IdeBlkIoDevice, &MediaChange);
1724 if (EFI_ERROR (Status)) {
1725
1726 if (IdeBlkIoDevice->Cache != NULL) {
1727 gBS->FreePool (IdeBlkIoDevice->Cache);
1728 IdeBlkIoDevice->Cache = NULL;
1729 }
1730
1731 return Status;
1732 }
1733 //
1734 // Get the intrinsic block size
1735 //
1736 Media = IdeBlkIoDevice->BlkIo.Media;
1737 BlockSize = Media->BlockSize;
1738
1739 NumberOfBlocks = BufferSize / BlockSize;
1740
1741 if (!(Media->MediaPresent)) {
1742
1743 if (IdeBlkIoDevice->Cache != NULL) {
1744 gBS->FreePool (IdeBlkIoDevice->Cache);
1745 IdeBlkIoDevice->Cache = NULL;
1746 }
1747 return EFI_NO_MEDIA;
1748
1749 }
1750
1751 if ((MediaId != Media->MediaId) || MediaChange) {
1752
1753 if (IdeBlkIoDevice->Cache != NULL) {
1754 gBS->FreePool (IdeBlkIoDevice->Cache);
1755 IdeBlkIoDevice->Cache = NULL;
1756 }
1757 return EFI_MEDIA_CHANGED;
1758 }
1759
1760 if (BufferSize % BlockSize != 0) {
1761 return EFI_BAD_BUFFER_SIZE;
1762 }
1763
1764 if (LBA > Media->LastBlock) {
1765 return EFI_INVALID_PARAMETER;
1766 }
1767
1768 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1769 return EFI_INVALID_PARAMETER;
1770 }
1771
1772 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1773 return EFI_INVALID_PARAMETER;
1774 }
1775
1776 //
1777 // if all the parameters are valid, then perform read sectors command
1778 // to transfer data from device to host.
1779 //
1780 Status = AtapiReadSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1781 if (EFI_ERROR (Status)) {
1782 return EFI_DEVICE_ERROR;
1783 }
1784
1785 //
1786 // Read blocks succeeded
1787 //
1788
1789 //
1790 // save the first block to the cache for performance
1791 //
1792 if (LBA == 0 && (IdeBlkIoDevice->Cache == NULL)) {
1793 IdeBlkIoDevice->Cache = AllocatePool (BlockSize);
1794 if (IdeBlkIoDevice->Cache!= NULL) {
1795 CopyMem ((UINT8 *) IdeBlkIoDevice->Cache, (UINT8 *) Buffer, BlockSize);
1796 }
1797 }
1798
1799 return EFI_SUCCESS;
1800
1801 }
1802
1803 /**
1804 This function is the ATAPI implementation for WriteBlocks in the
1805 Block I/O Protocol interface.
1806
1807 @param[in] *IdeBlkIoDevice
1808 Indicates the calling context.
1809
1810 @param[in] MediaId
1811 The media id that the write request is for.
1812
1813 @param[in] LBA
1814 The starting logical block address to write onto
1815 the device.
1816
1817 @param[in] BufferSize
1818 The size of the Buffer in bytes. This must be a
1819 multiple of the intrinsic block size of the device.
1820
1821 @param[out] *Buffer
1822 A pointer to the source buffer for the data.
1823 The caller is responsible for either having implicit
1824 or explicit ownership of the memory that data is
1825 written from.
1826
1827 @retval EFI_SUCCESS
1828 Write Blocks successfully.
1829
1830 @retval EFI_DEVICE_ERROR
1831 Write Blocks failed.
1832
1833 @retval EFI_NO_MEDIA
1834 There is no media in the device.
1835
1836 @retval EFI_MEDIA_CHANGE
1837 The MediaId is not for the current media.
1838
1839 @retval EFI_BAD_BUFFER_SIZE
1840 The BufferSize parameter is not a multiple of the
1841 intrinsic block size of the device.
1842
1843 @retval EFI_INVALID_PARAMETER
1844 The write request contains LBAs that are not valid,
1845 or the data buffer is not valid.
1846
1847 TODO: EFI_MEDIA_CHANGED - add return value to function comment
1848 TODO: EFI_WRITE_PROTECTED - add return value to function comment
1849 **/
1850 EFI_STATUS
1851 AtapiBlkIoWriteBlocks (
1852 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1853 IN UINT32 MediaId,
1854 IN EFI_LBA LBA,
1855 IN UINTN BufferSize,
1856 OUT VOID *Buffer
1857 )
1858 {
1859
1860 EFI_BLOCK_IO_MEDIA *Media;
1861 UINTN BlockSize;
1862 UINTN NumberOfBlocks;
1863 EFI_STATUS Status;
1864 BOOLEAN MediaChange;
1865
1866 if (LBA == 0 && IdeBlkIoDevice->Cache != NULL) {
1867 gBS->FreePool (IdeBlkIoDevice->Cache);
1868 IdeBlkIoDevice->Cache = NULL;
1869 }
1870
1871 if (Buffer == NULL) {
1872 return EFI_INVALID_PARAMETER;
1873 }
1874
1875 if (BufferSize == 0) {
1876 return EFI_SUCCESS;
1877 }
1878
1879 //
1880 // ATAPI device media is removable,
1881 // so it is a must to detect media first before write operation
1882 //
1883 MediaChange = FALSE;
1884 Status = AtapiDetectMedia (IdeBlkIoDevice, &MediaChange);
1885 if (EFI_ERROR (Status)) {
1886
1887 if (LBA == 0 && IdeBlkIoDevice->Cache != NULL) {
1888 gBS->FreePool (IdeBlkIoDevice->Cache);
1889 IdeBlkIoDevice->Cache = NULL;
1890 }
1891 return Status;
1892 }
1893
1894 //
1895 // Get the intrinsic block size
1896 //
1897 Media = IdeBlkIoDevice->BlkIo.Media;
1898 BlockSize = Media->BlockSize;
1899 NumberOfBlocks = BufferSize / BlockSize;
1900
1901 if (!(Media->MediaPresent)) {
1902
1903 if (LBA == 0 && IdeBlkIoDevice->Cache != NULL) {
1904 gBS->FreePool (IdeBlkIoDevice->Cache);
1905 IdeBlkIoDevice->Cache = NULL;
1906 }
1907 return EFI_NO_MEDIA;
1908 }
1909
1910 if ((MediaId != Media->MediaId) || MediaChange) {
1911
1912 if (LBA == 0 && IdeBlkIoDevice->Cache != NULL) {
1913 gBS->FreePool (IdeBlkIoDevice->Cache);
1914 IdeBlkIoDevice->Cache = NULL;
1915 }
1916 return EFI_MEDIA_CHANGED;
1917 }
1918
1919 if (Media->ReadOnly) {
1920 return EFI_WRITE_PROTECTED;
1921 }
1922
1923 if (BufferSize % BlockSize != 0) {
1924 return EFI_BAD_BUFFER_SIZE;
1925 }
1926
1927 if (LBA > Media->LastBlock) {
1928 return EFI_INVALID_PARAMETER;
1929 }
1930
1931 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1932 return EFI_INVALID_PARAMETER;
1933 }
1934
1935 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1936 return EFI_INVALID_PARAMETER;
1937 }
1938
1939 //
1940 // if all the parameters are valid,
1941 // then perform write sectors command to transfer data from host to device.
1942 //
1943 Status = AtapiWriteSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1944 if (EFI_ERROR (Status)) {
1945 return EFI_DEVICE_ERROR;
1946 }
1947
1948 return EFI_SUCCESS;
1949
1950 }
1951
1952 /**
1953 This function is used to parse sense data. Only the first
1954 sense data is honoured.
1955
1956 @param[in] IdeDev Indicates the calling context.
1957 @param[in] SenseCount Count of sense data.
1958 @param[out] Result The parsed result.
1959
1960 @retval EFI_SUCCESS Successfully parsed.
1961 @retval EFI_INVALID_PARAMETER Count of sense data is zero.
1962
1963 **/
1964 EFI_STATUS
1965 ParseSenseData (
1966 IN IDE_BLK_IO_DEV *IdeDev,
1967 IN UINTN SenseCount,
1968 OUT SENSE_RESULT *Result
1969 )
1970 {
1971 ATAPI_REQUEST_SENSE_DATA *SenseData;
1972
1973 if (SenseCount == 0) {
1974 return EFI_INVALID_PARAMETER;
1975 }
1976
1977 //
1978 // Only use the first sense data
1979 //
1980 SenseData = IdeDev->SenseData;
1981 *Result = SenseOtherSense;
1982
1983 switch (SenseData->sense_key) {
1984 case ATA_SK_NO_SENSE:
1985 *Result = SenseNoSenseKey;
1986 break;
1987 case ATA_SK_NOT_READY:
1988 switch (SenseData->addnl_sense_code) {
1989 case ATA_ASC_NO_MEDIA:
1990 *Result = SenseNoMedia;
1991 break;
1992 case ATA_ASC_MEDIA_UPSIDE_DOWN:
1993 *Result = SenseMediaError;
1994 break;
1995 case ATA_ASC_NOT_READY:
1996 if (SenseData->addnl_sense_code_qualifier == ATA_ASCQ_IN_PROGRESS) {
1997 *Result = SenseDeviceNotReadyNeedRetry;
1998 } else {
1999 *Result = SenseDeviceNotReadyNoRetry;
2000 }
2001 break;
2002 }
2003 break;
2004 case ATA_SK_UNIT_ATTENTION:
2005 if (SenseData->addnl_sense_code == ATA_ASC_MEDIA_CHANGE) {
2006 *Result = SenseMediaChange;
2007 }
2008 break;
2009 case ATA_SK_MEDIUM_ERROR:
2010 switch (SenseData->addnl_sense_code) {
2011 case ATA_ASC_MEDIA_ERR1:
2012 case ATA_ASC_MEDIA_ERR2:
2013 case ATA_ASC_MEDIA_ERR3:
2014 case ATA_ASC_MEDIA_ERR4:
2015 *Result = SenseMediaError;
2016 break;
2017 }
2018 break;
2019 default:
2020 break;
2021 }
2022
2023 return EFI_SUCCESS;
2024 }
2025
2026 /**
2027 This function reads the pending data in the device.
2028
2029 @param[in] IdeDev Indicates the calling context.
2030
2031 @retval EFI_SUCCESS Successfully read.
2032 @retval EFI_NOT_READY The BSY is set avoiding reading.
2033
2034 **/
2035 EFI_STATUS
2036 AtapiReadPendingData (
2037 IN IDE_BLK_IO_DEV *IdeDev
2038 )
2039 {
2040 UINT8 AltRegister;
2041 UINT16 TempWordBuffer;
2042
2043 AltRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.AltStatus);
2044 if ((AltRegister & ATA_STSREG_BSY) == ATA_STSREG_BSY) {
2045 return EFI_NOT_READY;
2046 }
2047 if ((AltRegister & (ATA_STSREG_BSY | ATA_STSREG_DRQ)) == ATA_STSREG_DRQ) {
2048 TempWordBuffer = IDEReadPortB (IdeDev->PciIo,IdeDev->IoPort->Alt.AltStatus);
2049 while ((TempWordBuffer & (ATA_STSREG_BSY | ATA_STSREG_DRQ)) == ATA_STSREG_DRQ) {
2050 IDEReadPortWMultiple (
2051 IdeDev->PciIo,
2052 IdeDev->IoPort->Data,
2053 1,
2054 &TempWordBuffer
2055 );
2056 TempWordBuffer = IDEReadPortB (IdeDev->PciIo,IdeDev->IoPort->Alt.AltStatus);
2057 }
2058 }
2059 return EFI_SUCCESS;
2060 }
2061
2062 /**
2063 TODO: Add function description
2064
2065 @param IdeDev TODO: add argument description
2066 @param WriteProtected TODO: add argument description
2067
2068 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2069 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2070 @retval EFI_SUCCESS TODO: Add description for return value.
2071
2072 **/
2073 EFI_STATUS
2074 IsLS120orZipWriteProtected (
2075 IN IDE_BLK_IO_DEV *IdeDev,
2076 OUT BOOLEAN *WriteProtected
2077 )
2078 {
2079 EFI_STATUS Status;
2080
2081 *WriteProtected = FALSE;
2082
2083 Status = LS120EnableMediaStatus (IdeDev, TRUE);
2084 if (EFI_ERROR (Status)) {
2085 return EFI_DEVICE_ERROR;
2086 }
2087
2088 //
2089 // the Get Media Status Command is only valid
2090 // if a Set Features/Enable Media Status Command has been priviously issued.
2091 //
2092 if (LS120GetMediaStatus (IdeDev) == EFI_WRITE_PROTECTED) {
2093
2094 *WriteProtected = TRUE;
2095 } else {
2096
2097 *WriteProtected = FALSE;
2098 }
2099
2100 //
2101 // After Get Media Status Command completes,
2102 // Set Features/Disable Media Command should be sent.
2103 //
2104 Status = LS120EnableMediaStatus (IdeDev, FALSE);
2105 if (EFI_ERROR (Status)) {
2106 return EFI_DEVICE_ERROR;
2107 }
2108
2109 return EFI_SUCCESS;
2110 }