2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Andrew Schultz
33 * Device model implementation for an IDE disk
41 #include "arch/isa_traits.hh"
42 #include "base/chunk_generator.hh"
43 #include "base/cprintf.hh" // csprintf
44 #include "base/trace.hh"
45 #include "config/the_isa.hh"
46 #include "debug/IdeDisk.hh"
47 #include "dev/disk_image.hh"
48 #include "dev/ide_ctrl.hh"
49 #include "dev/ide_disk.hh"
50 #include "sim/core.hh"
51 #include "sim/sim_object.hh"
54 using namespace TheISA
;
56 IdeDisk::IdeDisk(const Params
*p
)
57 : SimObject(p
), ctrl(NULL
), image(p
->image
), diskDelay(p
->delay
),
58 dmaTransferEvent(this), dmaReadCG(NULL
), dmaReadWaitEvent(this),
59 dmaWriteCG(NULL
), dmaWriteWaitEvent(this), dmaPrdReadEvent(this),
60 dmaReadEvent(this), dmaWriteEvent(this)
62 // Reset the device state
65 // fill out the drive ID structure
66 memset(&driveID
, 0, sizeof(struct ataparams
));
68 // Calculate LBA and C/H/S values
73 uint32_t lba_size
= image
->size();
74 if (lba_size
>= 16383*16*63) {
84 if ((lba_size
/ sectors
) >= 16)
87 heads
= (lba_size
/ sectors
);
89 cylinders
= lba_size
/ (heads
* sectors
);
92 // Setup the model name
93 strncpy((char *)driveID
.atap_model
, "5MI EDD si k",
94 sizeof(driveID
.atap_model
));
95 // Set the maximum multisector transfer size
96 driveID
.atap_multi
= MAX_MULTSECT
;
97 // IORDY supported, IORDY disabled, LBA enabled, DMA enabled
98 driveID
.atap_capabilities1
= 0x7;
99 // UDMA support, EIDE support
100 driveID
.atap_extensions
= 0x6;
101 // Setup default C/H/S settings
102 driveID
.atap_cylinders
= cylinders
;
103 driveID
.atap_sectors
= sectors
;
104 driveID
.atap_heads
= heads
;
105 // Setup the current multisector transfer size
106 driveID
.atap_curmulti
= MAX_MULTSECT
;
107 driveID
.atap_curmulti_valid
= 0x1;
108 // Number of sectors on disk
109 driveID
.atap_capacity
= lba_size
;
110 // Multiword DMA mode 2 and below supported
111 driveID
.atap_dmamode_supp
= 0x4;
112 // Set PIO mode 4 and 3 supported
113 driveID
.atap_piomode_supp
= 0x3;
114 // Set DMA mode 4 and below supported
115 driveID
.atap_udmamode_supp
= 0x1f;
116 // Statically set hardware config word
117 driveID
.atap_hwreset_res
= 0x4001;
119 //arbitrary for now...
120 driveID
.atap_ata_major
= WDC_VER_ATA7
;
125 // destroy the data buffer
126 delete [] dataBuffer
;
130 IdeDisk::reset(int id
)
132 // initialize the data buffer and shadow registers
133 dataBuffer
= new uint8_t[MAX_DMA_SIZE
];
135 memset(dataBuffer
, 0, MAX_DMA_SIZE
);
136 memset(&cmdReg
, 0, sizeof(CommandReg_t
));
137 memset(&curPrd
.entry
, 0, sizeof(PrdEntry_t
));
147 // set the device state to idle
151 devState
= Device_Idle_S
;
153 } else if (id
== DEV1
) {
154 devState
= Device_Idle_NS
;
157 panic("Invalid device ID: %#x\n", id
);
160 // set the device ready bit
161 status
= STATUS_DRDY_BIT
;
163 /* The error register must be set to 0x1 on start-up to
164 indicate that no diagnostic error was detected */
173 IdeDisk::isDEVSelect()
175 return ctrl
->isDiskSelected(this);
179 IdeDisk::pciToDma(Addr pciAddr
)
182 return ctrl
->pciToDma(pciAddr
);
184 panic("Access to unset controller!\n");
188 // Device registers read/write
192 IdeDisk::readCommand(const Addr offset
, int size
, uint8_t *data
)
194 if (offset
== DATA_OFFSET
) {
195 if (size
== sizeof(uint16_t)) {
196 *(uint16_t *)data
= cmdReg
.data
;
197 } else if (size
== sizeof(uint32_t)) {
198 *(uint16_t *)data
= cmdReg
.data
;
199 updateState(ACT_DATA_READ_SHORT
);
200 *((uint16_t *)data
+ 1) = cmdReg
.data
;
202 panic("Data read of unsupported size %d.\n", size
);
204 updateState(ACT_DATA_READ_SHORT
);
207 assert(size
== sizeof(uint8_t));
210 *data
= cmdReg
.error
;
213 *data
= cmdReg
.sec_count
;
216 *data
= cmdReg
.sec_num
;
219 *data
= cmdReg
.cyl_low
;
222 *data
= cmdReg
.cyl_high
;
225 *data
= cmdReg
.drive
;
229 updateState(ACT_STAT_READ
);
232 panic("Invalid IDE command register offset: %#x\n", offset
);
234 DPRINTF(IdeDisk
, "Read to disk at offset: %#x data %#x\n", offset
, *data
);
238 IdeDisk::readControl(const Addr offset
, int size
, uint8_t *data
)
240 assert(size
== sizeof(uint8_t));
242 if (offset
!= ALTSTAT_OFFSET
)
243 panic("Invalid IDE control register offset: %#x\n", offset
);
244 DPRINTF(IdeDisk
, "Read to disk at offset: %#x data %#x\n", offset
, *data
);
248 IdeDisk::writeCommand(const Addr offset
, int size
, const uint8_t *data
)
250 if (offset
== DATA_OFFSET
) {
251 if (size
== sizeof(uint16_t)) {
252 cmdReg
.data
= *(const uint16_t *)data
;
253 } else if (size
== sizeof(uint32_t)) {
254 cmdReg
.data
= *(const uint16_t *)data
;
255 updateState(ACT_DATA_WRITE_SHORT
);
256 cmdReg
.data
= *((const uint16_t *)data
+ 1);
258 panic("Data write of unsupported size %d.\n", size
);
260 updateState(ACT_DATA_WRITE_SHORT
);
264 assert(size
== sizeof(uint8_t));
266 case FEATURES_OFFSET
:
269 cmdReg
.sec_count
= *data
;
272 cmdReg
.sec_num
= *data
;
275 cmdReg
.cyl_low
= *data
;
278 cmdReg
.cyl_high
= *data
;
281 cmdReg
.drive
= *data
;
282 updateState(ACT_SELECT_WRITE
);
285 cmdReg
.command
= *data
;
286 updateState(ACT_CMD_WRITE
);
289 panic("Invalid IDE command register offset: %#x\n", offset
);
291 DPRINTF(IdeDisk
, "Write to disk at offset: %#x data %#x\n", offset
,
296 IdeDisk::writeControl(const Addr offset
, int size
, const uint8_t *data
)
298 if (offset
!= CONTROL_OFFSET
)
299 panic("Invalid IDE control register offset: %#x\n", offset
);
301 if (*data
& CONTROL_RST_BIT
) {
302 // force the device into the reset state
303 devState
= Device_Srst
;
304 updateState(ACT_SRST_SET
);
305 } else if (devState
== Device_Srst
&& !(*data
& CONTROL_RST_BIT
)) {
306 updateState(ACT_SRST_CLEAR
);
309 nIENBit
= *data
& CONTROL_IEN_BIT
;
311 DPRINTF(IdeDisk
, "Write to disk at offset: %#x data %#x\n", offset
,
316 // Perform DMA transactions
320 IdeDisk::doDmaTransfer()
322 if (dmaState
!= Dma_Transfer
|| devState
!= Transfer_Data_Dma
)
323 panic("Inconsistent DMA transfer state: dmaState = %d devState = %d\n",
326 if (ctrl
->dmaPending() || ctrl
->getState() != SimObject::Running
) {
327 schedule(dmaTransferEvent
, curTick() + DMA_BACKOFF_PERIOD
);
330 ctrl
->dmaRead(curPrdAddr
, sizeof(PrdEntry_t
), &dmaPrdReadEvent
,
331 (uint8_t*)&curPrd
.entry
);
335 IdeDisk::dmaPrdReadDone()
338 "PRD: baseAddr:%#x (%#x) byteCount:%d (%d) eot:%#x sector:%d\n",
339 curPrd
.getBaseAddr(), pciToDma(curPrd
.getBaseAddr()),
340 curPrd
.getByteCount(), (cmdBytesLeft
/SectorSize
),
341 curPrd
.getEOT(), curSector
);
343 // the prd pointer has already been translated, so just do an increment
344 curPrdAddr
= curPrdAddr
+ sizeof(PrdEntry_t
);
353 IdeDisk::doDmaDataRead()
355 /** @todo we need to figure out what the delay actually will be */
356 Tick totalDiskDelay
= diskDelay
+ (curPrd
.getByteCount() / SectorSize
);
358 DPRINTF(IdeDisk
, "doDmaRead, diskDelay: %d totalDiskDelay: %d\n",
359 diskDelay
, totalDiskDelay
);
361 schedule(dmaReadWaitEvent
, curTick() + totalDiskDelay
);
367 using namespace Stats
;
369 .name(name() + ".dma_read_full_pages")
370 .desc("Number of full page size DMA reads (not PRD).")
373 .name(name() + ".dma_read_bytes")
374 .desc("Number of bytes transfered via DMA reads (not PRD).")
377 .name(name() + ".dma_read_txs")
378 .desc("Number of DMA read transactions (not PRD).")
382 .name(name() + ".dma_write_full_pages")
383 .desc("Number of full page size DMA writes.")
386 .name(name() + ".dma_write_bytes")
387 .desc("Number of bytes transfered via DMA writes.")
390 .name(name() + ".dma_write_txs")
391 .desc("Number of DMA write transactions.")
400 // clear out the data buffer
401 memset(dataBuffer
, 0, MAX_DMA_SIZE
);
402 dmaReadCG
= new ChunkGenerator(curPrd
.getBaseAddr(),
403 curPrd
.getByteCount(), TheISA::PageBytes
);
406 if (ctrl
->dmaPending() || ctrl
->getState() != SimObject::Running
) {
407 schedule(dmaReadWaitEvent
, curTick() + DMA_BACKOFF_PERIOD
);
409 } else if (!dmaReadCG
->done()) {
410 assert(dmaReadCG
->complete() < MAX_DMA_SIZE
);
411 ctrl
->dmaRead(pciToDma(dmaReadCG
->addr()), dmaReadCG
->size(),
412 &dmaReadWaitEvent
, dataBuffer
+ dmaReadCG
->complete());
413 dmaReadBytes
+= dmaReadCG
->size();
415 if (dmaReadCG
->size() == TheISA::PageBytes
)
419 assert(dmaReadCG
->done());
427 IdeDisk::dmaReadDone()
430 uint32_t bytesWritten
= 0;
433 // write the data to the disk image
434 for (bytesWritten
= 0; bytesWritten
< curPrd
.getByteCount();
435 bytesWritten
+= SectorSize
) {
437 cmdBytesLeft
-= SectorSize
;
438 writeDisk(curSector
++, (uint8_t *)(dataBuffer
+ bytesWritten
));
442 if (curPrd
.getEOT()) {
443 assert(cmdBytesLeft
== 0);
445 updateState(ACT_DMA_DONE
);
452 IdeDisk::doDmaDataWrite()
454 /** @todo we need to figure out what the delay actually will be */
455 Tick totalDiskDelay
= diskDelay
+ (curPrd
.getByteCount() / SectorSize
);
456 uint32_t bytesRead
= 0;
458 DPRINTF(IdeDisk
, "doDmaWrite, diskDelay: %d totalDiskDelay: %d\n",
459 diskDelay
, totalDiskDelay
);
461 memset(dataBuffer
, 0, MAX_DMA_SIZE
);
462 assert(cmdBytesLeft
<= MAX_DMA_SIZE
);
463 while (bytesRead
< curPrd
.getByteCount()) {
464 readDisk(curSector
++, (uint8_t *)(dataBuffer
+ bytesRead
));
465 bytesRead
+= SectorSize
;
466 cmdBytesLeft
-= SectorSize
;
469 schedule(dmaWriteWaitEvent
, curTick() + totalDiskDelay
);
473 IdeDisk::doDmaWrite()
477 // clear out the data buffer
478 dmaWriteCG
= new ChunkGenerator(curPrd
.getBaseAddr(),
479 curPrd
.getByteCount(), TheISA::PageBytes
);
481 if (ctrl
->dmaPending() || ctrl
->getState() != SimObject::Running
) {
482 schedule(dmaWriteWaitEvent
, curTick() + DMA_BACKOFF_PERIOD
);
484 } else if (!dmaWriteCG
->done()) {
485 assert(dmaWriteCG
->complete() < MAX_DMA_SIZE
);
486 ctrl
->dmaWrite(pciToDma(dmaWriteCG
->addr()), dmaWriteCG
->size(),
487 &dmaWriteWaitEvent
, dataBuffer
+ dmaWriteCG
->complete());
488 dmaWriteBytes
+= dmaWriteCG
->size();
490 if (dmaWriteCG
->size() == TheISA::PageBytes
)
494 assert(dmaWriteCG
->done());
502 IdeDisk::dmaWriteDone()
505 if (curPrd
.getEOT()) {
506 assert(cmdBytesLeft
== 0);
508 updateState(ACT_DMA_DONE
);
515 // Disk utility routines
519 IdeDisk::readDisk(uint32_t sector
, uint8_t *data
)
521 uint32_t bytesRead
= image
->read(data
, sector
);
523 if (bytesRead
!= SectorSize
)
524 panic("Can't read from %s. Only %d of %d read. errno=%d\n",
525 name(), bytesRead
, SectorSize
, errno
);
529 IdeDisk::writeDisk(uint32_t sector
, uint8_t *data
)
531 uint32_t bytesWritten
= image
->write(data
, sector
);
533 if (bytesWritten
!= SectorSize
)
534 panic("Can't write to %s. Only %d of %d written. errno=%d\n",
535 name(), bytesWritten
, SectorSize
, errno
);
539 // Setup and handle commands
543 IdeDisk::startDma(const uint32_t &prdTableBase
)
545 if (dmaState
!= Dma_Start
)
546 panic("Inconsistent DMA state, should be in Dma_Start!\n");
548 if (devState
!= Transfer_Data_Dma
)
549 panic("Inconsistent device state for DMA start!\n");
551 // PRD base address is given by bits 31:2
552 curPrdAddr
= pciToDma((Addr
)(prdTableBase
& ~ULL(0x3)));
554 dmaState
= Dma_Transfer
;
556 // schedule dma transfer (doDmaTransfer)
557 schedule(dmaTransferEvent
, curTick() + 1);
563 if (dmaState
== Dma_Idle
)
564 panic("Inconsistent DMA state, should be Start or Transfer!");
566 if (devState
!= Transfer_Data_Dma
&& devState
!= Prepare_Data_Dma
)
567 panic("Inconsistent device state, should be Transfer or Prepare!\n");
569 updateState(ACT_CMD_ERROR
);
573 IdeDisk::startCommand()
575 DevAction_t action
= ACT_NONE
;
580 switch (cmdReg
.command
) {
581 // Supported non-data commands
582 case WDSF_READ_NATIVE_MAX
:
583 size
= image
->size() - 1;
584 cmdReg
.sec_num
= (size
& 0xff);
585 cmdReg
.cyl_low
= ((size
& 0xff00) >> 8);
586 cmdReg
.cyl_high
= ((size
& 0xff0000) >> 16);
587 cmdReg
.head
= ((size
& 0xf000000) >> 24);
589 devState
= Command_Execution
;
590 action
= ACT_CMD_COMPLETE
;
595 case WDCC_STANDBY_IMMED
:
596 case WDCC_FLUSHCACHE
:
601 devState
= Command_Execution
;
602 action
= ACT_CMD_COMPLETE
;
605 // Supported PIO data-in commands
607 cmdBytes
= cmdBytesLeft
= sizeof(struct ataparams
);
608 devState
= Prepare_Data_In
;
609 action
= ACT_DATA_READY
;
614 if (!(cmdReg
.drive
& DRIVE_LBA_BIT
))
615 panic("Attempt to perform CHS access, only supports LBA\n");
617 if (cmdReg
.sec_count
== 0)
618 cmdBytes
= cmdBytesLeft
= (256 * SectorSize
);
620 cmdBytes
= cmdBytesLeft
= (cmdReg
.sec_count
* SectorSize
);
622 curSector
= getLBABase();
624 /** @todo make this a scheduled event to simulate disk delay */
625 devState
= Prepare_Data_In
;
626 action
= ACT_DATA_READY
;
629 // Supported PIO data-out commands
630 case WDCC_WRITEMULTI
:
632 if (!(cmdReg
.drive
& DRIVE_LBA_BIT
))
633 panic("Attempt to perform CHS access, only supports LBA\n");
635 if (cmdReg
.sec_count
== 0)
636 cmdBytes
= cmdBytesLeft
= (256 * SectorSize
);
638 cmdBytes
= cmdBytesLeft
= (cmdReg
.sec_count
* SectorSize
);
640 curSector
= getLBABase();
642 devState
= Prepare_Data_Out
;
643 action
= ACT_DATA_READY
;
646 // Supported DMA commands
648 dmaRead
= true; // a write to the disk is a DMA read from memory
650 if (!(cmdReg
.drive
& DRIVE_LBA_BIT
))
651 panic("Attempt to perform CHS access, only supports LBA\n");
653 if (cmdReg
.sec_count
== 0)
654 cmdBytes
= cmdBytesLeft
= (256 * SectorSize
);
656 cmdBytes
= cmdBytesLeft
= (cmdReg
.sec_count
* SectorSize
);
658 curSector
= getLBABase();
660 devState
= Prepare_Data_Dma
;
661 action
= ACT_DMA_READY
;
665 panic("Unsupported ATA command: %#x\n", cmdReg
.command
);
668 if (action
!= ACT_NONE
) {
670 status
|= STATUS_BSY_BIT
;
672 status
&= ~STATUS_DRQ_BIT
;
674 status
&= ~STATUS_DF_BIT
;
681 // Handle setting and clearing interrupts
687 DPRINTF(IdeDisk
, "Posting Interrupt\n");
689 panic("Attempt to post an interrupt with one pending\n");
693 // talk to controller to set interrupt
702 DPRINTF(IdeDisk
, "Clearing Interrupt\n");
704 panic("Attempt to clear a non-pending interrupt\n");
708 // talk to controller to clear interrupt
714 // Manage the device internal state machine
718 IdeDisk::updateState(DevAction_t action
)
722 if (action
== ACT_SRST_SET
) {
724 status
|= STATUS_BSY_BIT
;
725 } else if (action
== ACT_SRST_CLEAR
) {
727 status
&= ~STATUS_BSY_BIT
;
729 // reset the device state
735 if (action
== ACT_SELECT_WRITE
&& !isDEVSelect()) {
736 devState
= Device_Idle_NS
;
737 } else if (action
== ACT_CMD_WRITE
) {
744 if (action
== ACT_SELECT_WRITE
&& !isDEVSelect()) {
745 devState
= Device_Idle_NS
;
747 } else if (action
== ACT_STAT_READ
|| isIENSet()) {
748 devState
= Device_Idle_S
;
750 } else if (action
== ACT_CMD_WRITE
) {
758 if (action
== ACT_SELECT_WRITE
&& isDEVSelect()) {
759 if (!isIENSet() && intrPending
) {
760 devState
= Device_Idle_SI
;
763 if (isIENSet() || !intrPending
) {
764 devState
= Device_Idle_S
;
769 case Command_Execution
:
770 if (action
== ACT_CMD_COMPLETE
) {
775 devState
= Device_Idle_SI
;
778 devState
= Device_Idle_S
;
783 case Prepare_Data_In
:
784 if (action
== ACT_CMD_ERROR
) {
789 devState
= Device_Idle_SI
;
792 devState
= Device_Idle_S
;
794 } else if (action
== ACT_DATA_READY
) {
796 status
&= ~STATUS_BSY_BIT
;
798 status
|= STATUS_DRQ_BIT
;
800 // copy the data into the data buffer
801 if (cmdReg
.command
== WDCC_IDENTIFY
) {
802 // Reset the drqBytes for this block
803 drqBytesLeft
= sizeof(struct ataparams
);
805 memcpy((void *)dataBuffer
, (void *)&driveID
,
806 sizeof(struct ataparams
));
808 // Reset the drqBytes for this block
809 drqBytesLeft
= SectorSize
;
811 readDisk(curSector
++, dataBuffer
);
814 // put the first two bytes into the data register
815 memcpy((void *)&cmdReg
.data
, (void *)dataBuffer
,
819 devState
= Data_Ready_INTRQ_In
;
822 devState
= Transfer_Data_In
;
827 case Data_Ready_INTRQ_In
:
828 if (action
== ACT_STAT_READ
) {
829 devState
= Transfer_Data_In
;
834 case Transfer_Data_In
:
835 if (action
== ACT_DATA_READ_BYTE
|| action
== ACT_DATA_READ_SHORT
) {
836 if (action
== ACT_DATA_READ_BYTE
) {
837 panic("DEBUG: READING DATA ONE BYTE AT A TIME!\n");
842 // copy next short into data registers
844 memcpy((void *)&cmdReg
.data
,
845 (void *)&dataBuffer
[SectorSize
- drqBytesLeft
],
849 if (drqBytesLeft
== 0) {
850 if (cmdBytesLeft
== 0) {
853 devState
= Device_Idle_S
;
855 devState
= Prepare_Data_In
;
857 status
|= STATUS_BSY_BIT
;
859 status
&= ~STATUS_DRQ_BIT
;
861 /** @todo change this to a scheduled event to simulate
863 updateState(ACT_DATA_READY
);
869 case Prepare_Data_Out
:
870 if (action
== ACT_CMD_ERROR
|| cmdBytesLeft
== 0) {
875 devState
= Device_Idle_SI
;
878 devState
= Device_Idle_S
;
880 } else if (action
== ACT_DATA_READY
&& cmdBytesLeft
!= 0) {
882 status
&= ~STATUS_BSY_BIT
;
884 status
|= STATUS_DRQ_BIT
;
886 // clear the data buffer to get it ready for writes
887 memset(dataBuffer
, 0, MAX_DMA_SIZE
);
889 // reset the drqBytes for this block
890 drqBytesLeft
= SectorSize
;
892 if (cmdBytesLeft
== cmdBytes
|| isIENSet()) {
893 devState
= Transfer_Data_Out
;
895 devState
= Data_Ready_INTRQ_Out
;
901 case Data_Ready_INTRQ_Out
:
902 if (action
== ACT_STAT_READ
) {
903 devState
= Transfer_Data_Out
;
908 case Transfer_Data_Out
:
909 if (action
== ACT_DATA_WRITE_BYTE
||
910 action
== ACT_DATA_WRITE_SHORT
) {
912 if (action
== ACT_DATA_READ_BYTE
) {
913 panic("DEBUG: WRITING DATA ONE BYTE AT A TIME!\n");
915 // copy the latest short into the data buffer
916 memcpy((void *)&dataBuffer
[SectorSize
- drqBytesLeft
],
917 (void *)&cmdReg
.data
,
924 if (drqBytesLeft
== 0) {
925 // copy the block to the disk
926 writeDisk(curSector
++, dataBuffer
);
929 status
|= STATUS_BSY_BIT
;
931 status
|= STATUS_SEEK_BIT
;
933 status
&= ~STATUS_DRQ_BIT
;
935 devState
= Prepare_Data_Out
;
937 /** @todo change this to a scheduled event to simulate
939 updateState(ACT_DATA_READY
);
944 case Prepare_Data_Dma
:
945 if (action
== ACT_CMD_ERROR
) {
950 devState
= Device_Idle_SI
;
953 devState
= Device_Idle_S
;
955 } else if (action
== ACT_DMA_READY
) {
957 status
&= ~STATUS_BSY_BIT
;
959 status
|= STATUS_DRQ_BIT
;
961 devState
= Transfer_Data_Dma
;
963 if (dmaState
!= Dma_Idle
)
964 panic("Inconsistent DMA state, should be Dma_Idle\n");
966 dmaState
= Dma_Start
;
967 // wait for the write to the DMA start bit
971 case Transfer_Data_Dma
:
972 if (action
== ACT_CMD_ERROR
|| action
== ACT_DMA_DONE
) {
976 status
|= STATUS_SEEK_BIT
;
977 // clear the controller state for DMA transfer
978 ctrl
->setDmaComplete(this);
981 devState
= Device_Idle_SI
;
984 devState
= Device_Idle_S
;
990 panic("Unknown IDE device state: %#x\n", devState
);
995 IdeDisk::serialize(ostream
&os
)
997 // Check all outstanding events to see if they are scheduled
998 // these are all mutually exclusive
1000 Events_t event
= None
;
1004 if (dmaTransferEvent
.scheduled()) {
1005 reschedule
= dmaTransferEvent
.when();
1009 if (dmaReadWaitEvent
.scheduled()) {
1010 reschedule
= dmaReadWaitEvent
.when();
1014 if (dmaWriteWaitEvent
.scheduled()) {
1015 reschedule
= dmaWriteWaitEvent
.when();
1019 if (dmaPrdReadEvent
.scheduled()) {
1020 reschedule
= dmaPrdReadEvent
.when();
1024 if (dmaReadEvent
.scheduled()) {
1025 reschedule
= dmaReadEvent
.when();
1029 if (dmaWriteEvent
.scheduled()) {
1030 reschedule
= dmaWriteEvent
.when();
1035 assert(eventCount
<= 1);
1037 SERIALIZE_SCALAR(reschedule
);
1038 SERIALIZE_ENUM(event
);
1040 // Serialize device registers
1041 SERIALIZE_SCALAR(cmdReg
.data
);
1042 SERIALIZE_SCALAR(cmdReg
.sec_count
);
1043 SERIALIZE_SCALAR(cmdReg
.sec_num
);
1044 SERIALIZE_SCALAR(cmdReg
.cyl_low
);
1045 SERIALIZE_SCALAR(cmdReg
.cyl_high
);
1046 SERIALIZE_SCALAR(cmdReg
.drive
);
1047 SERIALIZE_SCALAR(cmdReg
.command
);
1048 SERIALIZE_SCALAR(status
);
1049 SERIALIZE_SCALAR(nIENBit
);
1050 SERIALIZE_SCALAR(devID
);
1052 // Serialize the PRD related information
1053 SERIALIZE_SCALAR(curPrd
.entry
.baseAddr
);
1054 SERIALIZE_SCALAR(curPrd
.entry
.byteCount
);
1055 SERIALIZE_SCALAR(curPrd
.entry
.endOfTable
);
1056 SERIALIZE_SCALAR(curPrdAddr
);
1058 /** @todo need to serialized chunk generator stuff!! */
1059 // Serialize current transfer related information
1060 SERIALIZE_SCALAR(cmdBytesLeft
);
1061 SERIALIZE_SCALAR(cmdBytes
);
1062 SERIALIZE_SCALAR(drqBytesLeft
);
1063 SERIALIZE_SCALAR(curSector
);
1064 SERIALIZE_SCALAR(dmaRead
);
1065 SERIALIZE_SCALAR(intrPending
);
1066 SERIALIZE_ENUM(devState
);
1067 SERIALIZE_ENUM(dmaState
);
1068 SERIALIZE_ARRAY(dataBuffer
, MAX_DMA_SIZE
);
1072 IdeDisk::unserialize(Checkpoint
*cp
, const string
§ion
)
1074 // Reschedule events that were outstanding
1075 // these are all mutually exclusive
1076 Tick reschedule
= 0;
1077 Events_t event
= None
;
1079 UNSERIALIZE_SCALAR(reschedule
);
1080 UNSERIALIZE_ENUM(event
);
1084 case Transfer
: schedule(dmaTransferEvent
, reschedule
); break;
1085 case ReadWait
: schedule(dmaReadWaitEvent
, reschedule
); break;
1086 case WriteWait
: schedule(dmaWriteWaitEvent
, reschedule
); break;
1087 case PrdRead
: schedule(dmaPrdReadEvent
, reschedule
); break;
1088 case DmaRead
: schedule(dmaReadEvent
, reschedule
); break;
1089 case DmaWrite
: schedule(dmaWriteEvent
, reschedule
); break;
1092 // Unserialize device registers
1093 UNSERIALIZE_SCALAR(cmdReg
.data
);
1094 UNSERIALIZE_SCALAR(cmdReg
.sec_count
);
1095 UNSERIALIZE_SCALAR(cmdReg
.sec_num
);
1096 UNSERIALIZE_SCALAR(cmdReg
.cyl_low
);
1097 UNSERIALIZE_SCALAR(cmdReg
.cyl_high
);
1098 UNSERIALIZE_SCALAR(cmdReg
.drive
);
1099 UNSERIALIZE_SCALAR(cmdReg
.command
);
1100 UNSERIALIZE_SCALAR(status
);
1101 UNSERIALIZE_SCALAR(nIENBit
);
1102 UNSERIALIZE_SCALAR(devID
);
1104 // Unserialize the PRD related information
1105 UNSERIALIZE_SCALAR(curPrd
.entry
.baseAddr
);
1106 UNSERIALIZE_SCALAR(curPrd
.entry
.byteCount
);
1107 UNSERIALIZE_SCALAR(curPrd
.entry
.endOfTable
);
1108 UNSERIALIZE_SCALAR(curPrdAddr
);
1110 /** @todo need to serialized chunk generator stuff!! */
1111 // Unserialize current transfer related information
1112 UNSERIALIZE_SCALAR(cmdBytes
);
1113 UNSERIALIZE_SCALAR(cmdBytesLeft
);
1114 UNSERIALIZE_SCALAR(drqBytesLeft
);
1115 UNSERIALIZE_SCALAR(curSector
);
1116 UNSERIALIZE_SCALAR(dmaRead
);
1117 UNSERIALIZE_SCALAR(intrPending
);
1118 UNSERIALIZE_ENUM(devState
);
1119 UNSERIALIZE_ENUM(dmaState
);
1120 UNSERIALIZE_ARRAY(dataBuffer
, MAX_DMA_SIZE
);
1124 IdeDiskParams::create()
1126 return new IdeDisk(this);