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