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