]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Pci/IdeBusDxe/Atapi.c
e7ddcb980ef1633951ffb79554ea6610520823e2
[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) {
94 return EFI_NO_MEDIA;
95 }
96
97 if (StatusValue & BIT6) {
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[in] *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] **SenseBuffers
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 @param[out] *BufUnit
860 record the unit size of the sense data block in the SenseBuffers,
861
862 @param[out] *BufNumbers
863 record the number of units in the SenseBuffers.
864
865 @retval EFI_SUCCESS
866 Request Sense command completes successfully.
867
868 @retval EFI_DEVICE_ERROR
869 Request Sense command failed.
870
871 **/
872 EFI_STATUS
873 AtapiRequestSense (
874 IN IDE_BLK_IO_DEV *IdeDev,
875 OUT UINTN *SenseCounts
876 )
877 {
878 EFI_STATUS Status;
879 ATAPI_REQUEST_SENSE_DATA *Sense;
880 UINT16 *Ptr;
881 BOOLEAN FetchSenseData;
882 ATAPI_PACKET_COMMAND Packet;
883
884 *SenseCounts = 0;
885
886 ZeroMem (IdeDev->SenseData, sizeof (ATAPI_REQUEST_SENSE_DATA) * (IdeDev->SenseDataNumber));
887 //
888 // fill command packet for Request Sense Packet Command
889 //
890 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
891 Packet.RequestSence.opcode = ATA_CMD_REQUEST_SENSE;
892 Packet.RequestSence.allocation_length = sizeof (ATAPI_REQUEST_SENSE_DATA);
893
894 //
895 // initialize pointer
896 //
897 Ptr = (UINT16 *) IdeDev->SenseData;
898 //
899 // request sense data from device continuously until no sense data
900 // exists in the device.
901 //
902 for (FetchSenseData = TRUE; FetchSenseData;) {
903
904 Sense = (ATAPI_REQUEST_SENSE_DATA *) Ptr;
905
906 //
907 // send out Request Sense Packet Command and get one Sense data form device
908 //
909 Status = AtapiPacketCommandIn (
910 IdeDev,
911 &Packet,
912 Ptr,
913 sizeof (ATAPI_REQUEST_SENSE_DATA),
914 ATAPITIMEOUT
915 );
916 //
917 // failed to get Sense data
918 //
919 if (EFI_ERROR (Status)) {
920 if (*SenseCounts == 0) {
921 return EFI_DEVICE_ERROR;
922 } else {
923 return EFI_SUCCESS;
924 }
925 }
926
927 (*SenseCounts)++;
928 //
929 // We limit MAX sense data count to 20 in order to avoid dead loop. Some
930 // incompatible ATAPI devices don't retrive NO_SENSE when there is no media.
931 // In this case, dead loop occurs if we don't have a gatekeeper. 20 is
932 // supposed to be large enough for any ATAPI device.
933 //
934 if ((Sense->sense_key != ATA_SK_NO_SENSE) && ((*SenseCounts) < 20)) {
935 //
936 // Ptr is word-based pointer
937 //
938 Ptr += (sizeof (ATAPI_REQUEST_SENSE_DATA) + 1) >> 1;
939
940 } else {
941 //
942 // when no sense key, skip out the loop
943 //
944 FetchSenseData = FALSE;
945 }
946 }
947
948 return EFI_SUCCESS;
949 }
950
951 /**
952 Sends out ATAPI Read Capacity Packet Command to the specified device.
953 This command will return the information regarding the capacity of the
954 media in the device.
955
956 Current device status will impact device's response to the Read Capacity
957 Command. For example, if the device once reset, the Read Capacity
958 Command will fail. The Sense data record the current device status, so
959 if the Read Capacity Command failed, the Sense data must be requested
960 and be analyzed to determine if the Read Capacity Command should retry.
961
962 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
963 to record all the information of the IDE device.
964 @param[in] SResult Sense result for this packet command
965
966 @retval EFI_SUCCESS Read Capacity Command finally completes successfully.
967 @retval EFI_DEVICE_ERROR Read Capacity Command failed because of device error.
968
969 @note Parameter "IdeDev" will be updated in this function.
970
971 TODO: EFI_NOT_READY - add return value to function comment
972 **/
973 EFI_STATUS
974 AtapiReadCapacity (
975 IN IDE_BLK_IO_DEV *IdeDev,
976 OUT SENSE_RESULT *SResult
977 )
978 {
979 //
980 // status returned by Read Capacity Packet Command
981 //
982 EFI_STATUS Status;
983 EFI_STATUS SenseStatus;
984 ATAPI_PACKET_COMMAND Packet;
985 UINTN SenseCount;
986
987 //
988 // used for capacity data returned from ATAPI device
989 //
990 ATAPI_READ_CAPACITY_DATA Data;
991 ATAPI_READ_FORMAT_CAPACITY_DATA FormatData;
992
993 ZeroMem (&Data, sizeof (Data));
994 ZeroMem (&FormatData, sizeof (FormatData));
995
996 if (IdeDev->Type == IdeCdRom) {
997
998 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
999 Packet.Inquiry.opcode = ATA_CMD_READ_CAPACITY;
1000 Status = AtapiPacketCommandIn (
1001 IdeDev,
1002 &Packet,
1003 (UINT16 *) &Data,
1004 sizeof (ATAPI_READ_CAPACITY_DATA),
1005 ATAPITIMEOUT
1006 );
1007
1008 } else {
1009 //
1010 // Type == IdeMagnetic
1011 //
1012 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
1013 Packet.ReadFormatCapacity.opcode = ATA_CMD_READ_FORMAT_CAPACITY;
1014 Packet.ReadFormatCapacity.allocation_length_lo = 12;
1015 Status = AtapiPacketCommandIn (
1016 IdeDev,
1017 &Packet,
1018 (UINT16 *) &FormatData,
1019 sizeof (ATAPI_READ_FORMAT_CAPACITY_DATA),
1020 ATAPITIMEOUT
1021 );
1022 }
1023
1024 if (Status == EFI_TIMEOUT) {
1025 return Status;
1026 }
1027
1028 SenseStatus = AtapiRequestSense (IdeDev, &SenseCount);
1029
1030 if (!EFI_ERROR (SenseStatus)) {
1031 ParseSenseData (IdeDev, SenseCount, SResult);
1032
1033 if (!EFI_ERROR (Status) && *SResult == SenseNoSenseKey) {
1034 if (IdeDev->Type == IdeCdRom) {
1035
1036 IdeDev->BlkIo.Media->LastBlock = (Data.LastLba3 << 24) |
1037 (Data.LastLba2 << 16) |
1038 (Data.LastLba1 << 8) |
1039 Data.LastLba0;
1040
1041 IdeDev->BlkIo.Media->MediaPresent = TRUE;
1042
1043 IdeDev->BlkIo.Media->ReadOnly = TRUE;
1044
1045 //
1046 // Because the user data portion in the sector of the Data CD supported
1047 // is always 0x800
1048 //
1049 IdeDev->BlkIo.Media->BlockSize = 0x800;
1050 }
1051
1052 if (IdeDev->Type == IdeMagnetic) {
1053
1054 if (FormatData.DesCode == 3) {
1055 IdeDev->BlkIo.Media->MediaPresent = FALSE;
1056 IdeDev->BlkIo.Media->LastBlock = 0;
1057 } else {
1058
1059 IdeDev->BlkIo.Media->LastBlock = (FormatData.LastLba3 << 24) |
1060 (FormatData.LastLba2 << 16) |
1061 (FormatData.LastLba1 << 8) |
1062 FormatData.LastLba0;
1063 if (IdeDev->BlkIo.Media->LastBlock != 0) {
1064 IdeDev->BlkIo.Media->LastBlock--;
1065
1066 IdeDev->BlkIo.Media->BlockSize = (FormatData.BlockSize2 << 16) |
1067 (FormatData.BlockSize1 << 8) |
1068 FormatData.BlockSize0;
1069
1070 IdeDev->BlkIo.Media->MediaPresent = TRUE;
1071 } else {
1072 IdeDev->BlkIo.Media->MediaPresent = FALSE;
1073 //
1074 // Return EFI_NOT_READY operation succeeds but returned capacity is 0
1075 //
1076 return EFI_NOT_READY;
1077 }
1078
1079 IdeDev->BlkIo.Media->BlockSize = 0x200;
1080
1081 }
1082 }
1083 }
1084
1085 return EFI_SUCCESS;
1086
1087 } else {
1088 return EFI_DEVICE_ERROR;
1089 }
1090 }
1091
1092 /**
1093 Used before read/write blocks from/to ATAPI device media.
1094 Since ATAPI device media is removable, it is necessary to detect
1095 whether media is present and get current present media's
1096 information, and if media has been changed, Block I/O Protocol
1097 need to be reinstalled.
1098
1099 @param[in] *IdeDev
1100 pointer pointing to IDE_BLK_IO_DEV data structure, used
1101 to record all the information of the IDE device.
1102
1103 @param[out] *MediaChange
1104 return value that indicates if the media of the device has been
1105 changed.
1106
1107 @retval EFI_SUCCESS
1108 media found successfully.
1109
1110 @retval EFI_DEVICE_ERROR
1111 any error encounters during media detection.
1112
1113 @retval EFI_NO_MEDIA
1114 media not found.
1115
1116 @note
1117 parameter IdeDev may be updated in this function.
1118
1119 **/
1120 EFI_STATUS
1121 AtapiDetectMedia (
1122 IN IDE_BLK_IO_DEV *IdeDev,
1123 OUT BOOLEAN *MediaChange
1124 )
1125 {
1126 EFI_STATUS Status;
1127 EFI_STATUS CleanStateStatus;
1128 EFI_BLOCK_IO_MEDIA OldMediaInfo;
1129 UINTN RetryTimes;
1130 UINTN RetryNotReady;
1131 SENSE_RESULT SResult;
1132 BOOLEAN WriteProtected;
1133
1134 CopyMem (&OldMediaInfo, IdeDev->BlkIo.Media, sizeof (EFI_BLOCK_IO_MEDIA));
1135 *MediaChange = FALSE;
1136 //
1137 // Retry for SenseDeviceNotReadyNeedRetry.
1138 // Each retry takes 1s and we limit the upper boundary to
1139 // 120 times about 2 min.
1140 //
1141 RetryNotReady = 120;
1142
1143 //
1144 // Do Test Unit Ready
1145 //
1146 DoTUR:
1147 //
1148 // Retry 5 times
1149 //
1150 RetryTimes = 5;
1151 while (RetryTimes != 0) {
1152
1153 Status = AtapiTestUnitReady (IdeDev, &SResult);
1154
1155 if (EFI_ERROR (Status)) {
1156 //
1157 // Test Unit Ready error without sense data.
1158 // For some devices, this means there's extra data
1159 // that has not been read, so we read these extra
1160 // data out before going on.
1161 //
1162 CleanStateStatus = AtapiReadPendingData (IdeDev);
1163 if (EFI_ERROR (CleanStateStatus)) {
1164 //
1165 // Busy wait failed, try again
1166 //
1167 RetryTimes--;
1168 }
1169 //
1170 // Try again without counting down RetryTimes
1171 //
1172 continue;
1173 } else {
1174 switch (SResult) {
1175 case SenseNoSenseKey:
1176 if (IdeDev->BlkIo.Media->MediaPresent) {
1177 goto Done;
1178 } else {
1179 //
1180 // Media present but the internal structure need refreshed.
1181 // Try Read Capacity
1182 //
1183 goto DoRC;
1184 }
1185 break;
1186
1187 case SenseDeviceNotReadyNeedRetry:
1188 if (--RetryNotReady == 0) {
1189 return EFI_DEVICE_ERROR;
1190 }
1191 gBS->Stall (1000 * STALL_1_MILLI_SECOND);
1192 continue;
1193 break;
1194
1195 case SenseNoMedia:
1196 IdeDev->BlkIo.Media->MediaPresent = FALSE;
1197 IdeDev->BlkIo.Media->LastBlock = 0;
1198 goto Done;
1199 break;
1200
1201 case SenseDeviceNotReadyNoRetry:
1202 case SenseMediaError:
1203 return EFI_DEVICE_ERROR;
1204
1205 case SenseMediaChange:
1206 IdeDev->BlkIo.Media->MediaId++;
1207 goto DoRC;
1208 break;
1209
1210 default:
1211 RetryTimes--;
1212 break;
1213 }
1214 }
1215 }
1216
1217 return EFI_DEVICE_ERROR;
1218
1219 //
1220 // Do Read Capacity
1221 //
1222 DoRC:
1223 RetryTimes = 5;
1224
1225 while (RetryTimes != 0) {
1226
1227 Status = AtapiReadCapacity (IdeDev, &SResult);
1228
1229 if (EFI_ERROR (Status)) {
1230 RetryTimes--;
1231 continue;
1232 } else {
1233 switch (SResult) {
1234 case SenseNoSenseKey:
1235 goto Done;
1236 break;
1237
1238 case SenseDeviceNotReadyNeedRetry:
1239 //
1240 // We use Test Unit Ready to retry which
1241 // is faster.
1242 //
1243 goto DoTUR;
1244 break;
1245
1246 case SenseNoMedia:
1247 IdeDev->BlkIo.Media->MediaPresent = FALSE;
1248 IdeDev->BlkIo.Media->LastBlock = 0;
1249 goto Done;
1250 break;
1251
1252 case SenseDeviceNotReadyNoRetry:
1253 case SenseMediaError:
1254 return EFI_DEVICE_ERROR;
1255
1256 case SenseMediaChange:
1257 IdeDev->BlkIo.Media->MediaId++;
1258 continue;
1259 break;
1260
1261 default:
1262 RetryTimes--;
1263 break;
1264 }
1265 }
1266 }
1267
1268 return EFI_DEVICE_ERROR;
1269
1270 Done:
1271 //
1272 // the following code is to check the write-protected for LS120 media
1273 //
1274 if ((IdeDev->BlkIo.Media->MediaPresent) && (IdeDev->Type == IdeMagnetic)) {
1275
1276 Status = IsLS120orZipWriteProtected (IdeDev, &WriteProtected);
1277 if (!EFI_ERROR (Status)) {
1278
1279 if (WriteProtected) {
1280
1281 IdeDev->BlkIo.Media->ReadOnly = TRUE;
1282 } else {
1283
1284 IdeDev->BlkIo.Media->ReadOnly = FALSE;
1285 }
1286
1287 }
1288 }
1289
1290 if (IdeDev->BlkIo.Media->MediaId != OldMediaInfo.MediaId) {
1291 //
1292 // Media change information got from the device
1293 //
1294 *MediaChange = TRUE;
1295 }
1296
1297 if (IdeDev->BlkIo.Media->ReadOnly != OldMediaInfo.ReadOnly) {
1298 *MediaChange = TRUE;
1299 IdeDev->BlkIo.Media->MediaId += 1;
1300 }
1301
1302 if (IdeDev->BlkIo.Media->BlockSize != OldMediaInfo.BlockSize) {
1303 *MediaChange = TRUE;
1304 IdeDev->BlkIo.Media->MediaId += 1;
1305 }
1306
1307 if (IdeDev->BlkIo.Media->LastBlock != OldMediaInfo.LastBlock) {
1308 *MediaChange = TRUE;
1309 IdeDev->BlkIo.Media->MediaId += 1;
1310 }
1311
1312 if (IdeDev->BlkIo.Media->MediaPresent != OldMediaInfo.MediaPresent) {
1313 if (IdeDev->BlkIo.Media->MediaPresent) {
1314 //
1315 // when change from no media to media present, reset the MediaId to 1.
1316 //
1317 IdeDev->BlkIo.Media->MediaId = 1;
1318 } else {
1319 //
1320 // when no media, reset the MediaId to zero.
1321 //
1322 IdeDev->BlkIo.Media->MediaId = 0;
1323 }
1324
1325 *MediaChange = TRUE;
1326 }
1327
1328 //
1329 // if any change on current existing media,
1330 // the Block I/O protocol need to be reinstalled.
1331 //
1332 if (*MediaChange) {
1333 gBS->ReinstallProtocolInterface (
1334 IdeDev->Handle,
1335 &gEfiBlockIoProtocolGuid,
1336 &IdeDev->BlkIo,
1337 &IdeDev->BlkIo
1338 );
1339 }
1340
1341 if (IdeDev->BlkIo.Media->MediaPresent) {
1342 return EFI_SUCCESS;
1343 } else {
1344 return EFI_NO_MEDIA;
1345 }
1346 }
1347
1348 /**
1349 This function is called by the AtapiBlkIoReadBlocks() to perform
1350 read from media in block unit.
1351
1352 The main command used to access media here is READ(10) Command.
1353 READ(10) Command requests that the ATAPI device media transfer
1354 specified data to the host. Data is transferred in block(sector)
1355 unit. The maximum number of blocks that can be transferred once is
1356 65536. This is the main difference between READ(10) and READ(12)
1357 Command. The maximum number of blocks in READ(12) is 2 power 32.
1358
1359 @param[in] *IdeDev
1360 pointer pointing to IDE_BLK_IO_DEV data structure, used
1361 to record all the information of the IDE device.
1362
1363 @param[in] *Buffer
1364 A pointer to the destination buffer for the data.
1365
1366 @param[in] Lba
1367 The starting logical block address to read from
1368 on the device media.
1369
1370 @param[in] NumberOfBlocks
1371 The number of transfer data blocks.
1372
1373 @return status is fully dependent on the return status
1374 of AtapiPacketCommandIn() function.
1375
1376 **/
1377 EFI_STATUS
1378 AtapiReadSectors (
1379 IN IDE_BLK_IO_DEV *IdeDev,
1380 IN VOID *Buffer,
1381 IN EFI_LBA Lba,
1382 IN UINTN NumberOfBlocks
1383 )
1384 {
1385
1386 ATAPI_PACKET_COMMAND Packet;
1387 ATAPI_READ10_CMD *Read10Packet;
1388 EFI_STATUS Status;
1389 UINTN BlocksRemaining;
1390 UINT32 Lba32;
1391 UINT32 BlockSize;
1392 UINT32 ByteCount;
1393 UINT16 SectorCount;
1394 VOID *PtrBuffer;
1395 UINT16 MaxBlock;
1396 UINTN TimeOut;
1397
1398 //
1399 // fill command packet for Read(10) command
1400 //
1401 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
1402 Read10Packet = &Packet.Read10;
1403 Lba32 = (UINT32) Lba;
1404 PtrBuffer = Buffer;
1405
1406 BlockSize = IdeDev->BlkIo.Media->BlockSize;
1407
1408 //
1409 // limit the data bytes that can be transferred by one Read(10) Command
1410 //
1411 MaxBlock = 65535;
1412
1413 BlocksRemaining = NumberOfBlocks;
1414
1415 Status = EFI_SUCCESS;
1416 while (BlocksRemaining > 0) {
1417
1418 if (BlocksRemaining <= MaxBlock) {
1419
1420 SectorCount = (UINT16) BlocksRemaining;
1421 } else {
1422
1423 SectorCount = MaxBlock;
1424 }
1425
1426 //
1427 // fill the Packet data structure
1428 //
1429
1430 Read10Packet->opcode = ATA_CMD_READ_10;
1431
1432 //
1433 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1434 // Lba0 is MSB, Lba3 is LSB
1435 //
1436 Read10Packet->Lba3 = (UINT8) (Lba32 & 0xff);
1437 Read10Packet->Lba2 = (UINT8) (Lba32 >> 8);
1438 Read10Packet->Lba1 = (UINT8) (Lba32 >> 16);
1439 Read10Packet->Lba0 = (UINT8) (Lba32 >> 24);
1440
1441 //
1442 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1443 // TranLen0 is MSB, TranLen is LSB
1444 //
1445 Read10Packet->TranLen1 = (UINT8) (SectorCount & 0xff);
1446 Read10Packet->TranLen0 = (UINT8) (SectorCount >> 8);
1447
1448 ByteCount = SectorCount * BlockSize;
1449
1450 if (IdeDev->Type == IdeCdRom) {
1451 TimeOut = CDROMLONGTIMEOUT;
1452 } else {
1453 TimeOut = ATAPILONGTIMEOUT;
1454 }
1455
1456 Status = AtapiPacketCommandIn (
1457 IdeDev,
1458 &Packet,
1459 (UINT16 *) PtrBuffer,
1460 ByteCount,
1461 TimeOut
1462 );
1463 if (EFI_ERROR (Status)) {
1464 return Status;
1465 }
1466
1467 Lba32 += SectorCount;
1468 PtrBuffer = (UINT8 *) PtrBuffer + SectorCount * BlockSize;
1469 BlocksRemaining -= SectorCount;
1470 }
1471
1472 return Status;
1473 }
1474
1475 /**
1476 This function is called by the AtapiBlkIoWriteBlocks() to perform
1477 write onto media in block unit.
1478 The main command used to access media here is Write(10) Command.
1479 Write(10) Command requests that the ATAPI device media transfer
1480 specified data to the host. Data is transferred in block (sector)
1481 unit. The maximum number of blocks that can be transferred once is
1482 65536.
1483
1484 @param[in] *IdeDev
1485 pointer pointing to IDE_BLK_IO_DEV data structure, used
1486 to record all the information of the IDE device.
1487
1488 @param[in] *Buffer
1489 A pointer to the source buffer for the data.
1490
1491 @param[in] Lba
1492 The starting logical block address to write onto
1493 the device media.
1494
1495 @param[in] NumberOfBlocks
1496 The number of transfer data blocks.
1497
1498 @return status is fully dependent on the return status
1499 of AtapiPacketCommandOut() function.
1500
1501 **/
1502 EFI_STATUS
1503 AtapiWriteSectors (
1504 IN IDE_BLK_IO_DEV *IdeDev,
1505 IN VOID *Buffer,
1506 IN EFI_LBA Lba,
1507 IN UINTN NumberOfBlocks
1508 )
1509 {
1510
1511 ATAPI_PACKET_COMMAND Packet;
1512 ATAPI_READ10_CMD *Read10Packet;
1513
1514 EFI_STATUS Status;
1515 UINTN BlocksRemaining;
1516 UINT32 Lba32;
1517 UINT32 BlockSize;
1518 UINT32 ByteCount;
1519 UINT16 SectorCount;
1520 VOID *PtrBuffer;
1521 UINT16 MaxBlock;
1522
1523 //
1524 // fill command packet for Write(10) command
1525 // Write(10) command packet has the same data structure as
1526 // Read(10) command packet,
1527 // so here use the Read10Packet data structure
1528 // for the Write(10) command packet.
1529 //
1530 ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
1531 Read10Packet = &Packet.Read10;
1532
1533 Lba32 = (UINT32) Lba;
1534 PtrBuffer = Buffer;
1535
1536 BlockSize = IdeDev->BlkIo.Media->BlockSize;
1537
1538 //
1539 // limit the data bytes that can be transferred by one Read(10) Command
1540 //
1541 MaxBlock = (UINT16) (65536 / BlockSize);
1542
1543 BlocksRemaining = NumberOfBlocks;
1544
1545 Status = EFI_SUCCESS;
1546 while (BlocksRemaining > 0) {
1547
1548 if (BlocksRemaining >= MaxBlock) {
1549 SectorCount = MaxBlock;
1550 } else {
1551 SectorCount = (UINT16) BlocksRemaining;
1552 }
1553
1554 //
1555 // Command code is WRITE_10.
1556 //
1557 Read10Packet->opcode = ATA_CMD_WRITE_10;
1558
1559 //
1560 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1561 // Lba0 is MSB, Lba3 is LSB
1562 //
1563 Read10Packet->Lba3 = (UINT8) (Lba32 & 0xff);
1564 Read10Packet->Lba2 = (UINT8) (Lba32 >> 8);
1565 Read10Packet->Lba1 = (UINT8) (Lba32 >> 16);
1566 Read10Packet->Lba0 = (UINT8) (Lba32 >> 24);
1567
1568 //
1569 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1570 // TranLen0 is MSB, TranLen is LSB
1571 //
1572 Read10Packet->TranLen1 = (UINT8) (SectorCount & 0xff);
1573 Read10Packet->TranLen0 = (UINT8) (SectorCount >> 8);
1574
1575 ByteCount = SectorCount * BlockSize;
1576
1577 Status = AtapiPacketCommandOut (
1578 IdeDev,
1579 &Packet,
1580 (UINT16 *) PtrBuffer,
1581 ByteCount,
1582 ATAPILONGTIMEOUT
1583 );
1584 if (EFI_ERROR (Status)) {
1585 return Status;
1586 }
1587
1588 Lba32 += SectorCount;
1589 PtrBuffer = ((UINT8 *) PtrBuffer + SectorCount * BlockSize);
1590 BlocksRemaining -= SectorCount;
1591 }
1592
1593 return Status;
1594 }
1595
1596 /**
1597 This function is used to implement the Soft Reset on the specified
1598 ATAPI device. Different from the AtaSoftReset(), here reset is a ATA
1599 Soft Reset Command special for ATAPI device, and it only take effects
1600 on the specified ATAPI device, not on the whole IDE bus.
1601 Since the ATAPI soft reset is needed when device is in exceptional
1602 condition (such as BSY bit is always set ), I think the Soft Reset
1603 command should be sent without waiting for the BSY clear and DRDY
1604 set.
1605 This function is called by IdeBlkIoReset(),
1606 a interface function of Block I/O protocol.
1607
1608 @param[in] *IdeDev
1609 pointer pointing to IDE_BLK_IO_DEV data structure, used
1610 to record all the information of the IDE device.
1611
1612 @retval EFI_SUCCESS
1613 Soft reset completes successfully.
1614
1615 @retval EFI_DEVICE_ERROR
1616 Any step during the reset process is failed.
1617
1618 **/
1619 EFI_STATUS
1620 AtapiSoftReset (
1621 IN IDE_BLK_IO_DEV *IdeDev
1622 )
1623 {
1624 UINT8 Command;
1625 UINT8 DeviceSelect;
1626 EFI_STATUS Status;
1627
1628 //
1629 // for ATAPI device, no need to wait DRDY ready after device selecting.
1630 // (bit7 and bit5 are both set to 1 for backward compatibility)
1631 //
1632 DeviceSelect = (UINT8) (((BIT7 | BIT5) | (IdeDev->Device << 4)));
1633 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Head, DeviceSelect);
1634
1635 Command = ATA_CMD_SOFT_RESET;
1636 IDEWritePortB (IdeDev->PciIo, IdeDev->IoPort->Reg.Command, Command);
1637
1638 //
1639 // BSY cleared is the only status return to the host by the device
1640 // when reset is completed.
1641 // slave device needs at most 31s to clear BSY
1642 //
1643 Status = WaitForBSYClear (IdeDev, 31000);
1644 if (EFI_ERROR (Status)) {
1645 return EFI_DEVICE_ERROR;
1646 }
1647
1648 //
1649 // stall 5 seconds to make the device status stable
1650 //
1651 gBS->Stall (5000000);
1652
1653 return EFI_SUCCESS;
1654 }
1655
1656 /**
1657 This function is the ATAPI implementation for ReadBlocks in the
1658 Block I/O Protocol interface.
1659
1660 @param[in] *IdeBlkIoDev
1661 Indicates the calling context.
1662
1663 @param[in] MediaId
1664 The media id that the read request is for.
1665
1666 @param[in] LBA
1667 The starting logical block address to read from
1668 on the device.
1669
1670 @param[in] BufferSize
1671 The size of the Buffer in bytes. This must be a
1672 multiple of the intrinsic block size of the device.
1673
1674 @param[out] *Buffer
1675 A pointer to the destination buffer for the data.
1676 The caller is responsible for either having implicit
1677 or explicit ownership of the memory that data is read into.
1678
1679 @retval EFI_SUCCESS
1680 Read Blocks successfully.
1681
1682 @retval EFI_DEVICE_ERROR
1683 Read Blocks failed.
1684
1685 @retval EFI_NO_MEDIA
1686 There is no media in the device.
1687
1688 @retval EFI_MEDIA_CHANGED
1689 The MediaId is not for the current media.
1690
1691 @retval EFI_BAD_BUFFER_SIZE
1692 The BufferSize parameter is not a multiple of the
1693 intrinsic block size of the device.
1694
1695 @retval EFI_INVALID_PARAMETER
1696 The read request contains LBAs that are not valid,
1697 or the data buffer is not valid.
1698
1699 **/
1700 EFI_STATUS
1701 AtapiBlkIoReadBlocks (
1702 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1703 IN UINT32 MediaId,
1704 IN EFI_LBA LBA,
1705 IN UINTN BufferSize,
1706 OUT VOID *Buffer
1707 )
1708 {
1709 EFI_BLOCK_IO_MEDIA *Media;
1710 UINTN BlockSize;
1711 UINTN NumberOfBlocks;
1712 EFI_STATUS Status;
1713
1714 BOOLEAN MediaChange;
1715
1716 if (Buffer == NULL) {
1717 return EFI_INVALID_PARAMETER;
1718 }
1719
1720 if (BufferSize == 0) {
1721 return EFI_SUCCESS;
1722 }
1723
1724 //
1725 // ATAPI device media is removable, so it is a must
1726 // to detect media first before read operation
1727 //
1728 MediaChange = FALSE;
1729 Status = AtapiDetectMedia (IdeBlkIoDevice, &MediaChange);
1730 if (EFI_ERROR (Status)) {
1731
1732 if (IdeBlkIoDevice->Cache != NULL) {
1733 gBS->FreePool (IdeBlkIoDevice->Cache);
1734 IdeBlkIoDevice->Cache = NULL;
1735 }
1736
1737 return Status;
1738 }
1739 //
1740 // Get the intrinsic block size
1741 //
1742 Media = IdeBlkIoDevice->BlkIo.Media;
1743 BlockSize = Media->BlockSize;
1744
1745 NumberOfBlocks = BufferSize / BlockSize;
1746
1747 if (!(Media->MediaPresent)) {
1748
1749 if (IdeBlkIoDevice->Cache != NULL) {
1750 gBS->FreePool (IdeBlkIoDevice->Cache);
1751 IdeBlkIoDevice->Cache = NULL;
1752 }
1753 return EFI_NO_MEDIA;
1754
1755 }
1756
1757 if ((MediaId != Media->MediaId) || MediaChange) {
1758
1759 if (IdeBlkIoDevice->Cache != NULL) {
1760 gBS->FreePool (IdeBlkIoDevice->Cache);
1761 IdeBlkIoDevice->Cache = NULL;
1762 }
1763 return EFI_MEDIA_CHANGED;
1764 }
1765
1766 if (BufferSize % BlockSize != 0) {
1767 return EFI_BAD_BUFFER_SIZE;
1768 }
1769
1770 if (LBA > Media->LastBlock) {
1771 return EFI_INVALID_PARAMETER;
1772 }
1773
1774 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1775 return EFI_INVALID_PARAMETER;
1776 }
1777
1778 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1779 return EFI_INVALID_PARAMETER;
1780 }
1781
1782 //
1783 // if all the parameters are valid, then perform read sectors command
1784 // to transfer data from device to host.
1785 //
1786 Status = AtapiReadSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1787 if (EFI_ERROR (Status)) {
1788 return EFI_DEVICE_ERROR;
1789 }
1790
1791 //
1792 // Read blocks succeeded
1793 //
1794
1795 //
1796 // save the first block to the cache for performance
1797 //
1798 if (LBA == 0 && !IdeBlkIoDevice->Cache) {
1799 IdeBlkIoDevice->Cache = AllocatePool (BlockSize);
1800 if (IdeBlkIoDevice != NULL) {
1801 CopyMem ((UINT8 *) IdeBlkIoDevice->Cache, (UINT8 *) Buffer, BlockSize);
1802 }
1803 }
1804
1805 return EFI_SUCCESS;
1806
1807 }
1808
1809 /**
1810 This function is the ATAPI implementation for WriteBlocks in the
1811 Block I/O Protocol interface.
1812
1813 @param[in] *This
1814 Indicates the calling context.
1815
1816 @param[in] MediaId
1817 The media id that the write request is for.
1818
1819 @param[in] LBA
1820 The starting logical block address to write onto
1821 the device.
1822
1823 @param[in] BufferSize
1824 The size of the Buffer in bytes. This must be a
1825 multiple of the intrinsic block size of the device.
1826
1827 @param[out] *Buffer
1828 A pointer to the source buffer for the data.
1829 The caller is responsible for either having implicit
1830 or explicit ownership of the memory that data is
1831 written from.
1832
1833 @retval EFI_SUCCESS
1834 Write Blocks successfully.
1835
1836 @retval EFI_DEVICE_ERROR
1837 Write Blocks failed.
1838
1839 @retval EFI_NO_MEDIA
1840 There is no media in the device.
1841
1842 @retval EFI_MEDIA_CHANGE
1843 The MediaId is not for the current media.
1844
1845 @retval EFI_BAD_BUFFER_SIZE
1846 The BufferSize parameter is not a multiple of the
1847 intrinsic block size of the device.
1848
1849 @retval EFI_INVALID_PARAMETER
1850 The write request contains LBAs that are not valid,
1851 or the data buffer is not valid.
1852
1853 TODO: EFI_MEDIA_CHANGED - add return value to function comment
1854 TODO: EFI_WRITE_PROTECTED - add return value to function comment
1855 **/
1856 EFI_STATUS
1857 AtapiBlkIoWriteBlocks (
1858 IN IDE_BLK_IO_DEV *IdeBlkIoDevice,
1859 IN UINT32 MediaId,
1860 IN EFI_LBA LBA,
1861 IN UINTN BufferSize,
1862 OUT VOID *Buffer
1863 )
1864 {
1865
1866 EFI_BLOCK_IO_MEDIA *Media;
1867 UINTN BlockSize;
1868 UINTN NumberOfBlocks;
1869 EFI_STATUS Status;
1870 BOOLEAN MediaChange;
1871
1872 if (LBA == 0 && IdeBlkIoDevice->Cache) {
1873 gBS->FreePool (IdeBlkIoDevice->Cache);
1874 IdeBlkIoDevice->Cache = NULL;
1875 }
1876
1877 if (Buffer == NULL) {
1878 return EFI_INVALID_PARAMETER;
1879 }
1880
1881 if (BufferSize == 0) {
1882 return EFI_SUCCESS;
1883 }
1884
1885 //
1886 // ATAPI device media is removable,
1887 // so it is a must to detect media first before write operation
1888 //
1889 MediaChange = FALSE;
1890 Status = AtapiDetectMedia (IdeBlkIoDevice, &MediaChange);
1891 if (EFI_ERROR (Status)) {
1892
1893 if (LBA == 0 && IdeBlkIoDevice->Cache) {
1894 gBS->FreePool (IdeBlkIoDevice->Cache);
1895 IdeBlkIoDevice->Cache = NULL;
1896 }
1897 return Status;
1898 }
1899
1900 //
1901 // Get the intrinsic block size
1902 //
1903 Media = IdeBlkIoDevice->BlkIo.Media;
1904 BlockSize = Media->BlockSize;
1905 NumberOfBlocks = BufferSize / BlockSize;
1906
1907 if (!(Media->MediaPresent)) {
1908
1909 if (LBA == 0 && IdeBlkIoDevice->Cache) {
1910 gBS->FreePool (IdeBlkIoDevice->Cache);
1911 IdeBlkIoDevice->Cache = NULL;
1912 }
1913 return EFI_NO_MEDIA;
1914 }
1915
1916 if ((MediaId != Media->MediaId) || MediaChange) {
1917
1918 if (LBA == 0 && IdeBlkIoDevice->Cache) {
1919 gBS->FreePool (IdeBlkIoDevice->Cache);
1920 IdeBlkIoDevice->Cache = NULL;
1921 }
1922 return EFI_MEDIA_CHANGED;
1923 }
1924
1925 if (Media->ReadOnly) {
1926 return EFI_WRITE_PROTECTED;
1927 }
1928
1929 if (BufferSize % BlockSize != 0) {
1930 return EFI_BAD_BUFFER_SIZE;
1931 }
1932
1933 if (LBA > Media->LastBlock) {
1934 return EFI_INVALID_PARAMETER;
1935 }
1936
1937 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
1938 return EFI_INVALID_PARAMETER;
1939 }
1940
1941 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
1942 return EFI_INVALID_PARAMETER;
1943 }
1944
1945 //
1946 // if all the parameters are valid,
1947 // then perform write sectors command to transfer data from host to device.
1948 //
1949 Status = AtapiWriteSectors (IdeBlkIoDevice, Buffer, LBA, NumberOfBlocks);
1950 if (EFI_ERROR (Status)) {
1951 return EFI_DEVICE_ERROR;
1952 }
1953
1954 return EFI_SUCCESS;
1955
1956 }
1957
1958 /**
1959 This function is used to parse sense data. Only the first
1960 sense data is honoured.
1961
1962 @param[in] IdeDev Indicates the calling context.
1963 @param[in] SenseCount Count of sense data.
1964 @param[out] Result The parsed result.
1965
1966 @retval EFI_SUCCESS Successfully parsed.
1967 @retval EFI_INVALID_PARAMETER Count of sense data is zero.
1968
1969 **/
1970 EFI_STATUS
1971 ParseSenseData (
1972 IN IDE_BLK_IO_DEV *IdeDev,
1973 IN UINTN SenseCount,
1974 OUT SENSE_RESULT *Result
1975 )
1976 {
1977 ATAPI_REQUEST_SENSE_DATA *SenseData;
1978
1979 if (SenseCount == 0) {
1980 return EFI_INVALID_PARAMETER;
1981 }
1982
1983 //
1984 // Only use the first sense data
1985 //
1986 SenseData = IdeDev->SenseData;
1987 *Result = SenseOtherSense;
1988
1989 switch (SenseData->sense_key) {
1990 case ATA_SK_NO_SENSE:
1991 *Result = SenseNoSenseKey;
1992 break;
1993 case ATA_SK_NOT_READY:
1994 switch (SenseData->addnl_sense_code) {
1995 case ATA_ASC_NO_MEDIA:
1996 *Result = SenseNoMedia;
1997 break;
1998 case ATA_ASC_MEDIA_UPSIDE_DOWN:
1999 *Result = SenseMediaError;
2000 break;
2001 case ATA_ASC_NOT_READY:
2002 if (SenseData->addnl_sense_code_qualifier == ATA_ASCQ_IN_PROGRESS) {
2003 *Result = SenseDeviceNotReadyNeedRetry;
2004 } else {
2005 *Result = SenseDeviceNotReadyNoRetry;
2006 }
2007 break;
2008 }
2009 break;
2010 case ATA_SK_UNIT_ATTENTION:
2011 if (SenseData->addnl_sense_code == ATA_ASC_MEDIA_CHANGE) {
2012 *Result = SenseMediaChange;
2013 }
2014 break;
2015 case ATA_SK_MEDIUM_ERROR:
2016 switch (SenseData->addnl_sense_code) {
2017 case ATA_ASC_MEDIA_ERR1:
2018 case ATA_ASC_MEDIA_ERR2:
2019 case ATA_ASC_MEDIA_ERR3:
2020 case ATA_ASC_MEDIA_ERR4:
2021 *Result = SenseMediaError;
2022 break;
2023 }
2024 break;
2025 default:
2026 break;
2027 }
2028
2029 return EFI_SUCCESS;
2030 }
2031
2032 /**
2033 This function reads the pending data in the device.
2034
2035 @param[in] IdeDev Indicates the calling context.
2036
2037 @retval EFI_SUCCESS Successfully read.
2038 @retval EFI_NOT_READY The BSY is set avoiding reading.
2039
2040 **/
2041 EFI_STATUS
2042 AtapiReadPendingData (
2043 IN IDE_BLK_IO_DEV *IdeDev
2044 )
2045 {
2046 UINT8 AltRegister;
2047 UINT16 TempWordBuffer;
2048
2049 AltRegister = IDEReadPortB (IdeDev->PciIo, IdeDev->IoPort->Alt.AltStatus);
2050 if ((AltRegister & ATA_STSREG_BSY) == ATA_STSREG_BSY) {
2051 return EFI_NOT_READY;
2052 }
2053 if ((AltRegister & (ATA_STSREG_BSY | ATA_STSREG_DRQ)) == ATA_STSREG_DRQ) {
2054 TempWordBuffer = IDEReadPortB (IdeDev->PciIo,IdeDev->IoPort->Alt.AltStatus);
2055 while ((TempWordBuffer & (ATA_STSREG_BSY | ATA_STSREG_DRQ)) == ATA_STSREG_DRQ) {
2056 IDEReadPortWMultiple (
2057 IdeDev->PciIo,
2058 IdeDev->IoPort->Data,
2059 1,
2060 &TempWordBuffer
2061 );
2062 TempWordBuffer = IDEReadPortB (IdeDev->PciIo,IdeDev->IoPort->Alt.AltStatus);
2063 }
2064 }
2065 return EFI_SUCCESS;
2066 }
2067
2068 /**
2069 TODO: Add function description
2070
2071 @param IdeDev TODO: add argument description
2072 @param WriteProtected TODO: add argument description
2073
2074 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2075 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2076 @retval EFI_SUCCESS TODO: Add description for return value
2077
2078 **/
2079 EFI_STATUS
2080 IsLS120orZipWriteProtected (
2081 IN IDE_BLK_IO_DEV *IdeDev,
2082 OUT BOOLEAN *WriteProtected
2083 )
2084 {
2085 EFI_STATUS Status;
2086
2087 *WriteProtected = FALSE;
2088
2089 Status = LS120EnableMediaStatus (IdeDev, TRUE);
2090 if (EFI_ERROR (Status)) {
2091 return EFI_DEVICE_ERROR;
2092 }
2093
2094 //
2095 // the Get Media Status Command is only valid
2096 // if a Set Features/Enable Media Status Command has been priviously issued.
2097 //
2098 if (LS120GetMediaStatus (IdeDev) == EFI_WRITE_PROTECTED) {
2099
2100 *WriteProtected = TRUE;
2101 } else {
2102
2103 *WriteProtected = FALSE;
2104 }
2105
2106 //
2107 // After Get Media Status Command completes,
2108 // Set Features/Disable Media Command should be sent.
2109 //
2110 Status = LS120EnableMediaStatus (IdeDev, FALSE);
2111 if (EFI_ERROR (Status)) {
2112 return EFI_DEVICE_ERROR;
2113 }
2114
2115 return EFI_SUCCESS;
2116 }