3 * Copyright (C) 2002 MandrakeSoft S.A.
8 * http://www.linux-mandrake.com/
9 * http://www.mandrakesoft.com/
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Major modifications made for the V3VEE project
27 * The V3VEE Project is a joint project between Northwestern University
28 * and the University of New Mexico. You can find out more at
29 * http://www.v3vee.org
31 * Copyright (c) 2008, Zheng Cui <cuizheng@cs.unm.edu>
32 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
33 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
34 * All rights reserved for original changes
39 #include <devices/ramdisk.h>
40 #include <palacios/vmm.h>
41 #include <devices/cdrom.h>
42 #include <devices/ide.h>
43 #include <devices/pci.h>
47 #define PrintTrace(fmt, args...)
53 #define PrintDebug(fmt, args...)
61 * Data type definitions
64 #define INDEX_PULSE_CYCLE 10
69 #define INTR_REASON_BIT_ERR 0x01
70 #define UNABLE_FIND_TAT_CHANNEL_ERR 0x02
72 #define READ_BUF_GT_512 0x04
76 #define PRI_DATA_PORT 0x1f0
77 #define PRI_FEATURES_PORT 0x1f1
78 #define PRI_SECT_CNT_PORT 0x1f2
79 #define PRI_SECT_ADDR1_PORT 0x1f3
80 #define PRI_SECT_ADDR2_PORT 0x1f4
81 #define PRI_SECT_ADDR3_PORT 0x1f5
82 #define PRI_DRV_SEL_PORT 0x1f6
83 #define PRI_CMD_PORT 0x1f7
84 #define PRI_CTRL_PORT 0x3f6
85 #define PRI_ADDR_REG_PORT 0x3f7
87 #define SEC_DATA_PORT 0x170
88 #define SEC_FEATURES_PORT 0x171
89 #define SEC_SECT_CNT_PORT 0x172
90 #define SEC_SECT_ADDR1_PORT 0x173
91 #define SEC_SECT_ADDR2_PORT 0x174
92 #define SEC_SECT_ADDR3_PORT 0x175
93 #define SEC_DRV_SEL_PORT 0x176
94 #define SEC_CMD_PORT 0x177
95 #define SEC_CTRL_PORT 0x376
96 #define SEC_ADDR_REG_PORT 0x377
99 #define PACKET_SIZE 12
103 static const char cdrom_str[] = "CD-ROM";
104 static const char harddisk_str[] = "HARDDISK";
105 static const char none_str[] = "NONE";
108 static inline const char * device_type_to_str(device_type_t type) {
122 static inline void write_features(struct channel_t * channel, uchar_t value) {
123 channel->drives[0].controller.features = value;
124 channel->drives[1].controller.features = value;
128 static inline void write_sector_count(struct channel_t * channel, uchar_t value) {
129 channel->drives[0].controller.sector_count = value;
130 channel->drives[1].controller.sector_count = value;
133 static inline void write_sector_number(struct channel_t * channel, uchar_t value) {
134 channel->drives[0].controller.sector_no = value;
135 channel->drives[1].controller.sector_no = value;
139 static inline void write_cylinder_low(struct channel_t * channel, uchar_t value) {
140 channel->drives[0].controller.cylinder_no &= 0xff00;
141 channel->drives[0].controller.cylinder_no |= value;
142 channel->drives[1].controller.cylinder_no &= 0xff00;
143 channel->drives[1].controller.cylinder_no |= value;
146 static inline void write_cylinder_high(struct channel_t * channel, uchar_t value) {
147 ushort_t val2 = value;
149 channel->drives[0].controller.cylinder_no &= 0x00ff;
150 channel->drives[0].controller.cylinder_no |= (val2 & 0xff00);
152 channel->drives[1].controller.cylinder_no &= 0x00ff;
153 channel->drives[1].controller.cylinder_no |= (val2 & 0xff00);
156 static inline void write_head_no(struct channel_t * channel, uchar_t value) {
157 channel->drives[0].controller.head_no = value;
158 channel->drives[1].controller.head_no = value;
161 static inline void write_lba_mode(struct channel_t * channel, uchar_t value) {
162 channel->drives[0].controller.lba_mode = value;
163 channel->drives[1].controller.lba_mode = value;
167 static inline uint_t get_channel_no(struct ramdisk_t * ramdisk, struct channel_t * channel) {
168 return (((uchar_t *)channel - (uchar_t *)(ramdisk->channels)) / sizeof(struct channel_t));
171 static inline uint_t get_drive_no(struct channel_t * channel, struct drive_t * drive) {
172 return (((uchar_t *)drive - (uchar_t*)(channel->drives)) / sizeof(struct drive_t));
175 static inline struct drive_t * get_selected_drive(struct channel_t * channel) {
176 return &(channel->drives[channel->drive_select]);
180 static inline int is_primary_port(struct ramdisk_t * ramdisk, ushort_t port) {
184 case PRI_FEATURES_PORT:
185 case PRI_SECT_CNT_PORT:
186 case PRI_SECT_ADDR1_PORT:
187 case PRI_SECT_ADDR2_PORT:
188 case PRI_SECT_ADDR3_PORT:
189 case PRI_DRV_SEL_PORT:
200 static inline int is_secondary_port(struct ramdisk_t * ramdisk, ushort_t port) {
204 case SEC_FEATURES_PORT:
205 case SEC_SECT_CNT_PORT:
206 case SEC_SECT_ADDR1_PORT:
207 case SEC_SECT_ADDR2_PORT:
208 case SEC_SECT_ADDR3_PORT:
209 case SEC_DRV_SEL_PORT:
218 static inline int num_drives_on_channel(struct channel_t * channel) {
219 if ((channel->drives[0].device_type == IDE_NONE) &&
220 (channel->drives[1].device_type == IDE_NONE)) {
222 } else if ((channel->drives[0].device_type != IDE_NONE) &&
223 (channel->drives[1].device_type != IDE_NONE)) {
232 static inline uchar_t extract_bits(uchar_t * buf, uint_t buf_offset, uint_t bit_offset, uint_t num_bits) {
233 uchar_t val = buf[buf_offset];
234 val = val >> bit_offset;
235 val &= ((1 << num_bits) -1);
240 static inline uchar_t get_packet_field(struct channel_t * channel, uint_t packet_offset, uint_t bit_offset, uint_t num_bits) {
241 struct drive_t * drive = get_selected_drive(channel);
242 return extract_bits(drive->controller.buffer, packet_offset, bit_offset, num_bits);
246 static inline uchar_t get_packet_byte(struct channel_t * channel, uint_t offset) {
247 struct drive_t * drive = get_selected_drive(channel);
248 return drive->controller.buffer[offset];
251 static inline uint16_t get_packet_word(struct channel_t * channel, uint_t offset) {
252 struct drive_t * drive = get_selected_drive(channel);
253 uint16_t val = drive->controller.buffer[offset];
255 val |= drive->controller.buffer[offset + 1];
260 static inline uint16_t rd_read_16bit(const uint8_t* buf) {
261 return (buf[0] << 8) | buf[1];
266 static inline uint32_t rd_read_32bit(const uint8_t* buf) {
267 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
270 ////////////////////////////////////////////////////////////////////////////
278 static void rd_init_mode_sense_single(struct vm_device * dev, struct channel_t * channel, const void * src, int size);
280 static void rd_command_aborted(struct vm_device * dev, struct channel_t * channel, unsigned value);
285 static int handle_atapi_packet_command(struct vm_device * dev,
286 struct channel_t * channel,
289 static int rd_init_send_atapi_command(struct vm_device * dev,
290 struct channel_t * channel,
291 Bit8u command, int req_length,
292 int alloc_length, bool lazy);
294 static void rd_ready_to_send_atapi(struct vm_device * dev,
295 struct channel_t * channel);
297 static void rd_atapi_cmd_error(struct vm_device * dev,
298 struct channel_t * channel,
299 sense_t sense_key, asc_t asc);
301 static void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel);
302 static void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel);
309 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel);
310 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel);
321 static void rd_print_state(struct ramdisk_t *ramdisk);
325 ////////////////////////////////////////////////////////////////////
331 int v3_ramdisk_register_cdrom(struct vm_device * dev, uint_t busID, uint_t driveID, struct cdrom_ops* cd, void * private_data) {
332 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
333 struct channel_t * channel = &(ramdisk->channels[busID]);
334 struct drive_t * drive = &(channel->drives[driveID]);
335 struct controller_t * controller = &(drive->controller);
339 if (drive->device_type != IDE_NONE) {
340 PrintError("Device already registered at this location\n");
348 strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
350 while (strlen((char *)(drive->model_no)) < 40) {
351 strcat ((char*)(drive->model_no), " ");
354 PrintDebug("CDROM on target %d/%d\n", busID, driveID);
356 drive->device_type = IDE_CDROM;
357 drive->cdrom.locked = 0;
358 drive->sense.sense_key = SENSE_NONE;
359 drive->sense.asc = 0;
360 drive->sense.ascq = 0;
362 drive->private_data = private_data;
364 controller->sector_count = 0;
366 drive->cdrom.cd = cd;
368 PrintDebug("\t\tCD on ata%d-%d: '%s'\n",
372 if(drive->cdrom.cd->insert_cdrom(drive->private_data)) {
373 PrintDebug("\t\tMedia present in CD-ROM drive\n");
374 drive->cdrom.ready = 1;
375 drive->cdrom.capacity = drive->cdrom.cd->capacity(drive->private_data);
376 PrintDebug("\t\tCDROM capacity is %d\n", drive->cdrom.capacity);
378 PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
379 drive->cdrom.ready = 0;
386 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
389 struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
391 PrintDebug("[rd_init_harddrive]\n");
393 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
394 memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
397 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
398 struct channel_t * channel = &(channels[channel_num]);
400 channel->ioaddr1 = 0x0;
401 channel->ioaddr2 = 0x0;
404 for (device = 0; device < 2; device++){
405 struct drive_t * drive = &(channel->drives[device]);
406 struct controller_t * controller = &(drive->controller);
408 controller->status.busy = 0;
409 controller->status.drive_ready = 1;
410 controller->status.write_fault = 0;
411 controller->status.seek_complete = 1;
412 controller->status.drq = 0;
413 controller->status.corrected_data = 0;
414 controller->status.index_pulse = 0;
415 controller->status.index_pulse_count = 0;
416 controller->status.err = 0;
418 controller->error_register = 0x01; // diagnostic code: no error
419 controller->head_no = 0;
420 controller->sector_count = 1;
421 controller->sector_no = 1;
422 controller->cylinder_no = 0;
423 controller->current_command = 0x00;
424 controller->buffer_index = 0;
426 controller->control.reset = 0;
427 controller->control.disable_irq = 0;
428 controller->reset_in_progress = 0;
430 controller->sectors_per_block = 0x80;
431 controller->lba_mode = 0;
434 controller->features = 0;
437 drive->device_type = IDE_NONE;
440 strncpy((char*)(drive->model_no), "", 40);
441 while(strlen((char *)(drive->model_no)) < 40) {
442 strcat ((char*)(drive->model_no), " ");
449 rd_print_state(ramdisk);
456 static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
461 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
466 ////////////////////////////////////////////////////////////////////
470 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
471 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
472 struct channel_t * channel = NULL;
473 struct drive_t * drive = NULL;
474 struct controller_t * controller = NULL;
478 if (is_primary_port(ramdisk, port)) {
479 channel = &(ramdisk->channels[0]);
480 } else if (is_secondary_port(ramdisk, port)) {
481 channel = &(ramdisk->channels[1]);
483 PrintError("Invalid Port: %d\n", port);
487 drive = get_selected_drive(channel);
488 controller = &(drive->controller);
491 PrintTrace("[read_data_handler] IO Read at 0x%x, on drive %d/%d current cmd=0x%x\n",
493 get_channel_no(ramdisk, channel),
494 get_drive_no(channel, drive),
495 controller->current_command);
497 switch (controller->current_command) {
498 case 0xec: // IDENTIFY DEVICE
501 controller->status.busy = 0;
502 controller->status.drive_ready = 1;
503 controller->status.write_fault = 0;
504 controller->status.seek_complete = 1;
505 controller->status.corrected_data = 0;
506 controller->status.err = 0;
509 value32 = controller->buffer[index];
513 value32 |= (controller->buffer[index] << 8);
518 value32 |= (controller->buffer[index] << 16);
519 value32 |= (controller->buffer[index+1] << 24);
523 controller->buffer_index = index;
526 memcpy(dst, controller->buffer + controller->buffer_index, length);
527 controller->buffer_index += length;
529 if (controller->buffer_index >= 512) {
530 controller->status.drq = 0;
535 case 0xa0: //send packet cmd
537 uint_t index = controller->buffer_index;
540 PrintTrace("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n",
541 drive->atapi.command,
543 drive->cdrom.remaining_blocks);
545 // Load block if necessary
549 PrintError("\t\tindex > 2048 : 0x%x\n", index);
553 switch (drive->atapi.command) {
554 case 0x28: // read (10)
555 case 0xa8: // read (12)
558 if (!(drive->cdrom.ready)) {
559 PrintError("\t\tRead with CDROM not ready\n");
563 drive->cdrom.cd->read_block(drive->private_data, controller->buffer,
564 drive->cdrom.next_lba);
565 drive->cdrom.next_lba++;
566 drive->cdrom.remaining_blocks--;
569 if (!(drive->cdrom.remaining_blocks)) {
570 PrintDebug("\t\tLast READ block loaded {CDROM}\n");
572 PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
573 drive->cdrom.remaining_blocks);
576 // one block transfered, start at beginning
580 default: // no need to load a new block
588 value32 = controller->buffer[index + increment];
592 value32 |= (controller->buffer[index + increment] << 8);
597 value32 |= (controller->buffer[index + increment] << 16);
598 value32 |= (controller->buffer[index + increment + 1] << 24);
602 controller->buffer_index = index + increment;
603 controller->drq_index += increment;
606 /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
608 memcpy(dst, controller->buffer + index, length);
610 controller->buffer_index = index + length;
611 controller->drq_index += length;
616 if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
617 controller->status.drq = 0;
618 controller->drq_index = 0;
620 drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
622 if (drive->atapi.total_bytes_remaining > 0) {
623 // one or more blocks remaining (works only for single block commands)
625 PrintDebug("\t\tPACKET drq bytes read\n");
626 controller->interrupt_reason.i_o = 1;
627 controller->status.busy = 0;
628 controller->status.drq = 1;
629 controller->interrupt_reason.c_d = 0;
631 // set new byte count if last block
632 if (drive->atapi.total_bytes_remaining < controller->byte_count) {
633 controller->byte_count = drive->atapi.total_bytes_remaining;
635 drive->atapi.drq_bytes = controller->byte_count;
637 rd_raise_interrupt(dev, channel);
640 PrintDebug("\t\tPACKET all bytes read\n");
642 controller->interrupt_reason.i_o = 1;
643 controller->interrupt_reason.c_d = 1;
644 controller->status.drive_ready = 1;
645 controller->interrupt_reason.rel = 0;
646 controller->status.busy = 0;
647 controller->status.drq = 0;
648 controller->status.err = 0;
650 rd_raise_interrupt(dev, channel);
658 PrintError("\t\tunsupported command: %02x\n", controller->current_command);
668 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
669 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
670 struct channel_t * channel = NULL;
671 struct drive_t * drive = NULL;
672 struct controller_t * controller = NULL;
674 if (is_primary_port(ramdisk, port)) {
675 channel = &(ramdisk->channels[0]);
676 } else if (is_secondary_port(ramdisk, port)) {
677 channel = &(ramdisk->channels[1]);
679 PrintError("Invalid Port: %d\n", port);
683 drive = get_selected_drive(channel);
684 controller = &(drive->controller);
687 PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n",
688 port, controller->current_command);
692 //PrintDebug("[write_data_handler]\n");
693 switch (controller->current_command) {
694 case 0x30: // WRITE SECTORS
695 PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
700 if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
701 PrintError("Error sending atapi packet command in PACKET write to data port\n");
708 PrintError("\t\tIO write(0x%x): current command is %02xh\n",
709 port, controller->current_command);
724 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
725 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
726 struct channel_t * channel = NULL;
727 struct drive_t * drive = NULL;
728 struct controller_t * controller = NULL;
733 if (is_primary_port(ramdisk, port)) {
734 channel = &(ramdisk->channels[0]);
735 } else if (is_secondary_port(ramdisk, port)) {
736 channel = &(ramdisk->channels[1]);
738 PrintError("Invalid Port: %d\n", port);
742 drive = get_selected_drive(channel);
743 controller = &(drive->controller);
746 PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n",
747 port, get_channel_no(ramdisk, channel),
748 channel->drive_select);
751 if (num_drives_on_channel(channel) == 0) {
752 PrintDebug("Setting value to zero because 0 devices on channel\n");
753 // (mch) Just return zero for these registers
754 memset(dst, 0, length);
758 (controller->status.busy << 7) |
759 (controller->status.drive_ready << 6) |
760 (controller->status.write_fault << 5) |
761 (controller->status.seek_complete << 4) |
762 (controller->status.drq << 3) |
763 (controller->status.corrected_data << 2) |
764 (controller->status.index_pulse << 1) |
765 (controller->status.err) );
768 memcpy(dst, &val, length);
770 controller->status.index_pulse_count++;
771 controller->status.index_pulse = 0;
773 if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
774 controller->status.index_pulse = 1;
775 controller->status.index_pulse_count = 0;
779 if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
780 rd_lower_irq(dev, channel);
783 PrintDebug("\t\tRead STATUS = 0x%x\n", *(uchar_t *)dst);
790 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
791 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
792 struct channel_t * channel = NULL;
793 struct drive_t * drive = NULL;
794 struct controller_t * controller = NULL;
795 uchar_t value = *(uchar_t *)src;
798 PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
802 if (is_primary_port(ramdisk, port)) {
803 channel = &(ramdisk->channels[0]);
804 } else if (is_secondary_port(ramdisk, port)) {
805 channel = &(ramdisk->channels[1]);
807 PrintError("Invalid Port: %d\n", port);
811 drive = get_selected_drive(channel);
812 controller = &(drive->controller);
815 PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
816 port, get_channel_no(ramdisk, channel),
817 get_drive_no(channel, drive),
822 case 0xec: // IDENTIFY DEVICE
825 if (drive->device_type == IDE_NONE) {
826 PrintError("\t\tError: disk ata%d-%d not present, aborting\n",
827 get_channel_no(ramdisk, channel),
828 get_drive_no(channel, drive));
829 rd_command_aborted(dev, channel, value);
831 } else if (drive->device_type == IDE_CDROM) {
832 PrintDebug("Identifying CDROM...Going to abort????\n");
833 controller->head_no = 0;
834 controller->sector_count = 1;
835 controller->sector_no = 1;
836 controller->cylinder_no = 0xeb14;
837 rd_command_aborted(dev, channel, 0xec);
839 PrintError("\t\tError: Want to identify HDD!!\n");
841 SELECTED_CONTROLLER(channel).current_command = value;
842 SELECTED_CONTROLLER(channel).error_register = 0;
844 // See ATA/ATAPI-4, 8.12
845 SELECTED_CONTROLLER(channel).status.busy = 0;
846 SELECTED_CONTROLLER(channel).status.drive_ready = 1;
847 SELECTED_CONTROLLER(channel).status.write_fault = 0;
848 SELECTED_CONTROLLER(channel).status.drq = 1;
849 SELECTED_CONTROLLER(channel).status.err = 0;
851 SELECTED_CONTROLLER(channel).status.seek_complete = 1;
852 SELECTED_CONTROLLER(channel).status.corrected_data = 0;
854 SELECTED_CONTROLLER(channel).buffer_index = 0;
855 raise_interrupt(channel);
856 identify_drive(channel);
864 case 0xa1: // IDENTIFY PACKET DEVICE
866 if (drive->device_type == IDE_CDROM) {
867 controller->current_command = value;
868 controller->error_register = 0;
870 controller->status.busy = 0;
871 controller->status.drive_ready = 1;
872 controller->status.write_fault = 0;
873 controller->status.drq = 1;
874 controller->status.err = 0;
876 controller->status.seek_complete = 1;
877 controller->status.corrected_data = 0;
879 controller->buffer_index = 0;
880 rd_raise_interrupt(dev, channel);
881 rd_identify_ATAPI_drive(dev, channel);
883 PrintError("Identifying non cdrom device not supported - ata %d/%d\n",
884 get_channel_no(ramdisk, channel),
885 get_drive_no(channel, drive));
886 rd_command_aborted(dev, channel, 0xa1);
890 case 0xa0: // SEND PACKET (atapi)
892 if (drive->device_type == IDE_CDROM) {
895 if (controller->features & (1 << 0)) {
896 PrintError("\t\tPACKET-DMA not supported");
900 if (controller->features & (1 << 1)) {
901 PrintError("\t\tPACKET-overlapped not supported");
905 // We're already ready!
906 controller->sector_count = 1;
907 controller->status.busy = 0;
908 controller->status.write_fault = 0;
911 controller->status.drq = 1;
912 controller->status.err = 0;
914 // NOTE: no interrupt here
915 controller->current_command = value;
916 controller->buffer_index = 0;
918 PrintError("Sending packet to non cdrom device not supported\n");
919 rd_command_aborted (dev, channel, 0xa0);
924 PrintError("\t\tneed translate command %2x - ata %d\%d\n", value,
925 get_channel_no(ramdisk, channel),
926 get_drive_no(channel, drive));
928 /* JRL THIS NEEDS TO CHANGE */
936 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
937 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
938 struct channel_t * channel = NULL;
939 struct drive_t * master_drive = NULL;
940 struct drive_t * slave_drive = NULL;
941 struct controller_t * controller = NULL;
942 uchar_t value = *(uchar_t *)src;
943 rd_bool prev_control_reset;
946 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
950 if (is_primary_port(ramdisk, port)) {
951 channel = &(ramdisk->channels[0]);
952 } else if (is_secondary_port(ramdisk, port)) {
953 channel = &(ramdisk->channels[1]);
955 PrintError("Invalid Port: %d\n", port);
959 master_drive = &(channel->drives[0]);
960 slave_drive = &(channel->drives[1]);
962 controller = &(get_selected_drive(channel)->controller);
965 PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
966 port, get_channel_no(ramdisk, channel),
967 channel->drive_select,
970 // (mch) Even if device 1 was selected, a write to this register
971 // goes to device 0 (if device 1 is absent)
973 prev_control_reset = controller->control.reset;
977 PrintDebug("RESET Signaled\n");
980 master_drive->controller.control.reset = value & 0x04;
981 slave_drive->controller.control.reset = value & 0x04;
983 // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq = value & 0x02;
984 master_drive->controller.control.disable_irq = value & 0x02;
985 slave_drive->controller.control.disable_irq = value & 0x02;
987 PrintDebug("\t\tadpater control reg: reset controller = %d\n",
988 (unsigned) (controller->control.reset) ? 1 : 0);
989 PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
990 (unsigned) (controller->control.disable_irq) ? 1 : 0);
992 if ((!prev_control_reset) && (controller->control.reset)) {
995 // transition from 0 to 1 causes all drives to reset
996 PrintDebug("\t\thard drive: RESET\n");
998 // (mch) Set BSY, drive not ready
999 for (id = 0; id < 2; id++) {
1000 struct controller_t * ctrl = NULL;
1003 ctrl = &(master_drive->controller);
1004 } else if (id == 1) {
1005 ctrl = &(slave_drive->controller);
1008 ctrl->status.busy = 1;
1009 ctrl->status.drive_ready = 0;
1010 ctrl->reset_in_progress = 1;
1012 ctrl->status.write_fault = 0;
1013 ctrl->status.seek_complete = 1;
1014 ctrl->status.drq = 0;
1015 ctrl->status.corrected_data = 0;
1016 ctrl->status.err = 0;
1018 ctrl->error_register = 0x01; // diagnostic code: no error
1020 ctrl->current_command = 0x00;
1021 ctrl->buffer_index = 0;
1023 ctrl->sectors_per_block = 0x80;
1026 ctrl->control.disable_irq = 0;
1029 rd_lower_irq(dev, channel);
1031 } else if ((controller->reset_in_progress) &&
1032 (!controller->control.reset)) {
1034 // Clear BSY and DRDY
1035 PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
1037 for (id = 0; id < 2; id++) {
1038 struct controller_t * ctrl = NULL;
1039 struct drive_t * drv = NULL;
1042 ctrl = &(master_drive->controller);
1044 } else if (id == 1) {
1045 ctrl = &(slave_drive->controller);
1049 ctrl->status.busy = 0;
1050 ctrl->status.drive_ready = 1;
1051 ctrl->reset_in_progress = 0;
1054 if (drv->device_type == IDE_DISK) {
1055 PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1057 ctrl->sector_count = 1;
1058 ctrl->sector_no = 1;
1059 ctrl->cylinder_no = 0;
1062 ctrl->sector_count = 1;
1063 ctrl->sector_no = 1;
1064 ctrl->cylinder_no = 0xeb14;
1069 PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n",
1070 master_drive->controller.control.disable_irq);
1071 PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n",
1072 slave_drive->controller.control.disable_irq);
1077 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1078 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1079 struct channel_t * channel = NULL;
1080 struct drive_t * drive = NULL;
1081 struct controller_t * controller = NULL;
1085 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1089 if (is_primary_port(ramdisk, port)) {
1090 channel = &(ramdisk->channels[0]);
1091 } else if (is_secondary_port(ramdisk, port)) {
1092 channel = &(ramdisk->channels[1]);
1094 PrintError("Invalid Port: %d\n", port);
1098 drive = get_selected_drive(channel);
1099 controller = &(drive->controller);
1102 PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n",
1103 port, get_channel_no(ramdisk, channel),
1104 channel->drive_select,
1105 controller->current_command);
1109 case PRI_FEATURES_PORT:
1110 case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1112 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1114 controller->status.err = 0;
1116 PrintDebug("\t\tRead FEATURES = 0x%x\n", val);
1118 *(uchar_t *)dst = val;
1124 case PRI_SECT_CNT_PORT:
1125 case SEC_SECT_CNT_PORT: // hard disk sector count / interrupt reason 0x1f2
1127 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1128 PrintDebug("\t\tRead SECTOR COUNT = 0x%x\n", val);
1129 *(uchar_t *)dst = val;
1134 case PRI_SECT_ADDR1_PORT:
1135 case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1137 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1139 PrintDebug("\t\tRead SECTOR ADDR1 = 0x%x\n", val);
1141 *(uchar_t *)dst = val;
1147 case PRI_SECT_ADDR2_PORT:
1148 case SEC_SECT_ADDR2_PORT: // cylinder low 0x1f4
1150 // -- WARNING : On real hardware the controller registers are shared between drives.
1151 // So we must respond even if the select device is not present. Some OS uses this fact
1152 // to detect the disks.... minix2 for example
1153 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no & 0x00ff);
1155 PrintDebug("\t\tRead SECTOR ADDR2 = 0x%x\n", val);
1157 *(uchar_t *)dst = val;
1163 case PRI_SECT_ADDR3_PORT:
1164 case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1166 // -- WARNING : On real hardware the controller registers are shared between drives.
1167 // So we must respond even if the select device is not present. Some OS uses this fact
1168 // to detect the disks.... minix2 for example
1169 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1171 PrintDebug("\t\tRead SECTOR ADDR3 = 0x%x\n", val);
1173 *(uchar_t *)dst = val;
1178 case PRI_DRV_SEL_PORT:
1179 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1181 // b7 Extended data field for ECC
1182 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1183 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1184 // b6 1=LBA mode, 0=CHS mode
1188 uchar_t val = ((1 << 7) |
1189 ((controller->lba_mode > 0) << 6) |
1190 (1 << 5) | // 01b = 512 sector size
1191 (channel->drive_select << 4) |
1192 (controller->head_no << 0));
1194 PrintDebug("\t\tRead DRIVE SELECT = 0x%x\n", val);
1195 *(uchar_t *)dst = val;
1200 case PRI_ADDR_REG_PORT:
1201 case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1203 // Obsolete and unsupported register. Not driven by hard
1204 // disk controller. Report all 1's. If floppy controller
1205 // is handling this address, it will call this function
1206 // set/clear D7 (the only bit it handles), then return
1207 // the combined value
1208 *(uchar_t *)dst = 0xff;
1213 PrintError("Invalid Port: %d\n", port);
1221 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1222 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1223 struct channel_t * channel = NULL;
1224 struct drive_t * drive = NULL;
1225 struct controller_t * controller = NULL;
1226 uchar_t value = *(uchar_t *)src;
1229 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1233 if (is_primary_port(ramdisk, port)) {
1234 channel = &(ramdisk->channels[0]);
1235 } else if (is_secondary_port(ramdisk, port)) {
1236 channel = &(ramdisk->channels[1]);
1238 PrintError("Invalid Port: %d\n", port);
1242 drive = get_selected_drive(channel);
1243 controller = &(drive->controller);
1246 PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n",
1247 port, value, get_channel_no(ramdisk, channel));
1251 case PRI_FEATURES_PORT:
1252 case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1254 write_features(channel, value);
1257 case PRI_SECT_CNT_PORT:
1258 case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1260 write_sector_count(channel, value);
1263 case PRI_SECT_ADDR1_PORT:
1264 case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1266 write_sector_number(channel, value);
1269 case PRI_SECT_ADDR2_PORT:
1270 case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1272 write_cylinder_low(channel, value);
1275 case PRI_SECT_ADDR3_PORT:
1276 case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1278 write_cylinder_high(channel, value);
1281 case PRI_DRV_SEL_PORT:
1282 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1284 // b7 Extended data field for ECC
1285 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1286 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1287 // b6 1=LBA mode, 0=CHS mode
1294 PrintDebug("\tDrive Select value=%x\n", value);
1296 if ((value & 0xa0) != 0xa0) {
1297 PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1300 write_head_no(channel, value & 0xf);
1301 if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1302 PrintDebug("\t\tenabling LBA mode\n");
1305 write_lba_mode(channel, (value >> 6) & 1);
1309 if (drive->cdrom.cd) {
1310 PrintDebug("\t\tSetting LBA on CDROM: %d\n", (value >> 6) & 1);
1311 drive->cdrom.cd->set_LBA(drive->private_data, (value >> 6) & 1);
1315 channel->drive_select = (value >> 4) & 0x01;
1316 drive = get_selected_drive(channel);
1318 if (drive->device_type == IDE_NONE) {
1319 PrintError("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1320 channel->drive_select, get_channel_no(ramdisk, channel));
1322 controller->error_register = 0x04; // aborted
1323 controller->status.err = 1;
1329 PrintError("\t\thard drive: io write to unhandled port 0x%x (value = %c)\n", port, value);
1340 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1341 // struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1342 struct drive_t * drive = get_selected_drive(channel);
1343 struct controller_t * controller = &(drive->controller);
1345 PrintDebug("[raise_interrupt] disable_irq = 0x%02x\n", controller->control.disable_irq);
1347 if (!(controller->control.disable_irq)) {
1349 PrintDebug("\t\tRaising interrupt %d {%s}\n\n", channel->irq, device_type_to_str(drive->device_type));
1351 v3_raise_irq(dev->vm, channel->irq);
1353 PrintDebug("\t\tRaising irq but irq is disabled\n");
1359 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel) {
1360 PrintDebug("[lower_irq] irq = %d\n", channel->irq);
1361 v3_lower_irq(dev->vm, channel->irq);
1370 //////////////////////////////////////////////////////////////////////////
1378 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1379 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1380 struct drive_t * drive = get_selected_drive(channel);
1381 struct controller_t * controller = &(drive->controller);
1383 if (controller->buffer_index >= PACKET_SIZE) {
1384 PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n",
1385 controller->buffer_index);
1389 controller->buffer[controller->buffer_index] = value;
1390 controller->buffer[controller->buffer_index + 1] = (value >> 8);
1391 controller->buffer_index += 2;
1394 /* if packet completely writtten */
1395 if (controller->buffer_index >= PACKET_SIZE) {
1396 // complete command received
1397 Bit8u atapi_command = controller->buffer[0];
1399 PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1401 switch (atapi_command) {
1402 case 0x00: // test unit ready
1404 PrintDebug("Testing unit ready\n");
1405 if (drive->cdrom.ready) {
1406 rd_atapi_cmd_nop(dev, channel);
1408 PrintError("CDROM not ready in test unit ready\n");
1409 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1412 rd_raise_interrupt(dev, channel);
1416 case 0x03: // request sense
1418 int alloc_length = controller->buffer[4];
1420 if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1421 PrintError("Error sending atapi command in Request Sense\n");
1426 controller->buffer[0] = 0x70 | (1 << 7);
1427 controller->buffer[1] = 0;
1428 controller->buffer[2] = drive->sense.sense_key;
1429 controller->buffer[3] = drive->sense.information.arr[0];
1430 controller->buffer[4] = drive->sense.information.arr[1];
1431 controller->buffer[5] = drive->sense.information.arr[2];
1432 controller->buffer[6] = drive->sense.information.arr[3];
1433 controller->buffer[7] = 17 - 7;
1434 controller->buffer[8] = drive->sense.specific_inf.arr[0];
1435 controller->buffer[9] = drive->sense.specific_inf.arr[1];
1436 controller->buffer[10] = drive->sense.specific_inf.arr[2];
1437 controller->buffer[11] = drive->sense.specific_inf.arr[3];
1438 controller->buffer[12] = drive->sense.asc;
1439 controller->buffer[13] = drive->sense.ascq;
1440 controller->buffer[14] = drive->sense.fruc;
1441 controller->buffer[15] = drive->sense.key_spec.arr[0];
1442 controller->buffer[16] = drive->sense.key_spec.arr[1];
1443 controller->buffer[17] = drive->sense.key_spec.arr[2];
1445 rd_ready_to_send_atapi(dev, channel);
1448 case 0x1b: // start stop unit
1450 //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1451 rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1452 rd_bool Start = (controller->buffer[4] >> 0) & 1;
1455 if ((!LoEj) && (!Start)) {
1456 PrintError("FIXME: Stop disc not implemented\n");
1458 rd_atapi_cmd_nop(dev, channel);
1459 rd_raise_interrupt(dev, channel);
1461 } else if (!LoEj && Start) { // start (spin up) the disc
1463 drive->cdrom.cd->start_cdrom(drive->private_data);
1465 PrintError("FIXME: ATAPI start disc not reading TOC\n");
1466 rd_atapi_cmd_nop(dev, channel);
1467 rd_raise_interrupt(dev, channel);
1469 } else if (LoEj && !Start) { // Eject the disc
1470 rd_atapi_cmd_nop(dev, channel);
1471 PrintDebug("Ejecting Disk\n");
1472 if (drive->cdrom.ready) {
1474 drive->cdrom.cd->eject_cdrom(drive->private_data);
1476 drive->cdrom.ready = 0;
1477 //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1478 //bx_gui->update_drive_status_buttons();
1480 rd_raise_interrupt(dev, channel);
1482 } else { // Load the disc
1483 // My guess is that this command only closes the tray, that's a no-op for us
1484 rd_atapi_cmd_nop(dev, channel);
1485 rd_raise_interrupt(dev, channel);
1489 case 0xbd: // mechanism status
1491 uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1493 if (alloc_length == 0) {
1494 PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1498 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1499 PrintError("Error sending atapi command in mechanism status\n");
1503 controller->buffer[0] = 0; // reserved for non changers
1504 controller->buffer[1] = 0; // reserved for non changers
1506 controller->buffer[2] = 0; // Current LBA (TODO!)
1507 controller->buffer[3] = 0; // Current LBA (TODO!)
1508 controller->buffer[4] = 0; // Current LBA (TODO!)
1510 controller->buffer[5] = 1; // one slot
1512 controller->buffer[6] = 0; // slot table length
1513 controller->buffer[7] = 0; // slot table length
1515 rd_ready_to_send_atapi(dev, channel);
1518 case 0x5a: // mode sense
1520 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1522 Bit8u PC = controller->buffer[2] >> 6;
1523 Bit8u PageCode = controller->buffer[2] & 0x3f;
1526 case 0x0: // current values
1529 case 0x01: // error recovery
1532 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1533 PrintError("Error sending atapi command in mode sense error recovery\n");
1537 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1538 sizeof(struct error_recovery_t));
1539 rd_ready_to_send_atapi(dev, channel);
1542 case 0x2a: // CD-ROM capabilities & mech. status
1545 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1546 PrintError("Error sending atapi command in CDROM caps/mech mode-sense\n");
1550 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1552 controller->buffer[8] = 0x2a;
1553 controller->buffer[9] = 0x12;
1554 controller->buffer[10] = 0x00;
1555 controller->buffer[11] = 0x00;
1556 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1557 controller->buffer[12] = 0x70;
1558 controller->buffer[13] = (3 << 5);
1559 controller->buffer[14] = (unsigned char) (1 |
1560 (drive->cdrom.locked ? (1 << 1) : 0) |
1563 controller->buffer[15] = 0x00;
1564 controller->buffer[16] = (706 >> 8) & 0xff;
1565 controller->buffer[17] = 706 & 0xff;
1566 controller->buffer[18] = 0;
1567 controller->buffer[19] = 2;
1568 controller->buffer[20] = (512 >> 8) & 0xff;
1569 controller->buffer[21] = 512 & 0xff;
1570 controller->buffer[22] = (706 >> 8) & 0xff;
1571 controller->buffer[23] = 706 & 0xff;
1572 controller->buffer[24] = 0;
1573 controller->buffer[25] = 0;
1574 controller->buffer[26] = 0;
1575 controller->buffer[27] = 0;
1576 rd_ready_to_send_atapi(dev, channel);
1579 case 0x0d: // CD-ROM
1580 case 0x0e: // CD-ROM audio control
1583 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1585 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1586 ASC_INV_FIELD_IN_CMD_PACKET);
1587 rd_raise_interrupt(dev, channel);
1592 // not implemeted by this device
1593 PrintError("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1595 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1596 ASC_INV_FIELD_IN_CMD_PACKET);
1597 rd_raise_interrupt(dev, channel);
1603 case 0x1: // changeable values
1606 case 0x01: // error recovery
1607 case 0x0d: // CD-ROM
1608 case 0x0e: // CD-ROM audio control
1609 case 0x2a: // CD-ROM capabilities & mech. status
1612 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1614 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1615 ASC_INV_FIELD_IN_CMD_PACKET);
1616 rd_raise_interrupt(dev, channel);
1621 // not implemeted by this device
1622 PrintError("Changeable values of mode sense not supported by cdrom\n");
1623 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1625 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1626 ASC_INV_FIELD_IN_CMD_PACKET);
1627 rd_raise_interrupt(dev, channel);
1633 case 0x2: // default values
1636 case 0x01: // error recovery
1637 case 0x0d: // CD-ROM
1638 case 0x0e: // CD-ROM audio control
1639 case 0x2a: // CD-ROM capabilities & mech. status
1641 PrintError("Default values of mode sense not supported by cdrom\n");
1642 PrintDebug("cdrom: MODE SENSE (dflt), code=%x\n",
1648 PrintError("Default values of mode sense not implemented in cdrom\n");
1649 // not implemeted by this device
1650 PrintDebug("cdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1652 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1653 ASC_INV_FIELD_IN_CMD_PACKET);
1654 rd_raise_interrupt(dev, channel);
1660 case 0x3: // saved values not implemented
1662 PrintError("\t\tSaved values not implemented in mode sense\n");
1663 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1664 rd_raise_interrupt(dev, channel);
1669 PrintError("Unsupported Mode sense value\n");
1676 case 0x12: // inquiry
1678 uint8_t alloc_length = controller->buffer[4];
1680 if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1681 PrintError("Error sending atapi command in inquiry\n");
1685 controller->buffer[0] = 0x05; // CD-ROM
1686 controller->buffer[1] = 0x80; // Removable
1687 controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1688 controller->buffer[3] = 0x21; // ATAPI-2, as specified
1689 controller->buffer[4] = 31; // additional length (total 36)
1690 controller->buffer[5] = 0x00; // reserved
1691 controller->buffer[6] = 0x00; // reserved
1692 controller->buffer[7] = 0x00; // reserved
1695 const char* vendor_id = "VTAB ";
1697 for (i = 0; i < 8; i++) {
1698 controller->buffer[8+i] = vendor_id[i];
1702 const char* product_id = "Turbo CD-ROM ";
1703 for (i = 0; i < 16; i++) {
1704 controller->buffer[16+i] = product_id[i];
1707 // Product Revision level
1708 const char* rev_level = "1.0 ";
1709 for (i = 0; i < 4; i++) {
1710 controller->buffer[32 + i] = rev_level[i];
1713 rd_ready_to_send_atapi(dev, channel);
1716 case 0x25: // read cd-rom capacity
1718 // no allocation length???
1719 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1720 PrintError("Error sending atapi command in read cdrom capacity\n");
1724 if (drive->cdrom.ready) {
1725 uint32_t capacity = drive->cdrom.capacity;
1727 PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1729 controller->buffer[0] = (capacity >> 24) & 0xff;
1730 controller->buffer[1] = (capacity >> 16) & 0xff;
1731 controller->buffer[2] = (capacity >> 8) & 0xff;
1732 controller->buffer[3] = (capacity >> 0) & 0xff;
1733 controller->buffer[4] = (2048 >> 24) & 0xff;
1734 controller->buffer[5] = (2048 >> 16) & 0xff;
1735 controller->buffer[6] = (2048 >> 8) & 0xff;
1736 controller->buffer[7] = (2048 >> 0) & 0xff;
1738 rd_ready_to_send_atapi(dev, channel);
1740 PrintError("CDROM not ready in read cdrom capacity\n");
1741 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1742 rd_raise_interrupt(dev, channel);
1748 case 0xbe: // read cd
1750 if (drive->cdrom.ready) {
1751 PrintError("Read CD with CD present not implemented\n");
1752 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1753 rd_raise_interrupt(dev, channel);
1755 PrintError("Drive not ready in read cd with CD present\n");
1756 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1757 rd_raise_interrupt(dev, channel);
1761 case 0x43: // read toc
1763 if (drive->cdrom.ready) {
1765 bool msf = (controller->buffer[1] >> 1) & 1;
1766 uint8_t starting_track = controller->buffer[6];
1768 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1770 uint8_t format = (controller->buffer[9] >> 6);
1773 PrintDebug("Reading CDROM TOC: Format=%d (byte count=%d) (toc length:%d)\n",
1774 format, controller->byte_count, toc_length);
1778 if (!(drive->cdrom.cd->read_toc(drive->private_data, controller->buffer,
1779 &toc_length, msf, starting_track))) {
1780 PrintError("CDROM: Reading Table of Contents Failed\n");
1781 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1782 ASC_INV_FIELD_IN_CMD_PACKET);
1783 rd_raise_interrupt(dev, channel);
1788 if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1789 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1793 rd_ready_to_send_atapi(dev, channel);
1798 // multi session stuff. we ignore this and emulate a single session only
1800 if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1801 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1805 controller->buffer[0] = 0;
1806 controller->buffer[1] = 0x0a;
1807 controller->buffer[2] = 1;
1808 controller->buffer[3] = 1;
1810 for (i = 0; i < 8; i++) {
1811 controller->buffer[4 + i] = 0;
1814 rd_ready_to_send_atapi(dev, channel);
1819 PrintError("(READ TOC) Format %d not supported\n", format);
1823 PrintError("CDROM not ready in read toc\n");
1824 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1825 rd_raise_interrupt(dev, channel);
1829 case 0x28: // read (10)
1830 case 0xa8: // read (12)
1833 uint32_t transfer_length;
1834 if (atapi_command == 0x28) {
1835 transfer_length = rd_read_16bit(controller->buffer + 7);
1837 transfer_length = rd_read_32bit(controller->buffer + 6);
1840 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1842 if (!(drive->cdrom.ready)) {
1843 PrintError("CDROM Error: Not Ready (ATA%d/%d)\n",
1844 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1845 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1846 rd_raise_interrupt(dev, channel);
1850 if (transfer_length == 0) {
1851 PrintError("READ(%d) with transfer length 0, ok\n",
1852 (atapi_command == 0x28) ? 10 : 12);
1853 rd_atapi_cmd_nop(dev, channel);
1854 rd_raise_interrupt(dev, channel);
1858 if (lba + transfer_length > drive->cdrom.capacity) {
1859 PrintError("CDROM Error: Capacity exceeded [capacity=%d] (ATA%d/%d)\n",
1860 drive->cdrom.capacity,
1861 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1862 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1863 rd_raise_interrupt(dev, channel);
1867 PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n",
1868 (atapi_command == 0x28) ? 10 : 12,
1869 lba, transfer_length);
1872 if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1873 transfer_length * 2048, true) == -1) {
1874 PrintError("CDROM Error: Atapi command send error\n");
1878 drive->cdrom.remaining_blocks = transfer_length;
1879 drive->cdrom.next_lba = lba;
1880 rd_ready_to_send_atapi(dev, channel);
1885 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1887 if (!(drive->cdrom.ready)) {
1888 PrintError("CDROM not ready in seek\n");
1889 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1890 rd_raise_interrupt(dev, channel);
1894 if (lba > drive->cdrom.capacity) {
1895 PrintError("LBA is greater than CDROM capacity in seek\n");
1896 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1897 rd_raise_interrupt(dev, channel);
1901 PrintError("\t\tcdrom: SEEK (ignored)\n");
1903 rd_atapi_cmd_nop(dev, channel);
1904 rd_raise_interrupt(dev, channel);
1908 case 0x1e: // prevent/allow medium removal
1911 if (drive->cdrom.ready) {
1912 drive->cdrom.locked = controller->buffer[4] & 1;
1913 rd_atapi_cmd_nop(dev, channel);
1915 PrintError("CD not ready in prevent/allow medium removal\n");
1916 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1919 rd_raise_interrupt(dev, channel);
1923 case 0x42: // read sub-channel
1925 //bool msf = get_packet_field(channel, 1, 1, 1);
1926 bool sub_q = get_packet_field(channel, 2, 6, 1);
1927 //uint8_t data_format = get_packet_byte(channel, 3);
1928 //uint8_t track_number = get_packet_byte(channel, 6);
1929 uint16_t alloc_length = get_packet_word(channel, 7);
1934 UNUSED(data_format);
1935 UNUSED(track_number);
1937 if (!(drive->cdrom.ready)) {
1938 PrintError("CDROM not ready in read sub-channel\n");
1939 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1940 rd_raise_interrupt(dev, channel);
1942 controller->buffer[0] = 0;
1943 controller->buffer[1] = 0; // audio not supported
1944 controller->buffer[2] = 0;
1945 controller->buffer[3] = 0;
1947 int ret_len = 4; // header size
1949 if (sub_q) { // !sub_q == header only
1950 PrintError("Read sub-channel with SubQ not implemented\n");
1951 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1952 ASC_INV_FIELD_IN_CMD_PACKET);
1953 rd_raise_interrupt(dev, channel);
1956 if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1957 PrintError("Error sending atapi command in read sub-channel\n");
1960 rd_ready_to_send_atapi(dev, channel);
1964 case 0x51: // read disc info
1966 // no-op to keep the Linux CD-ROM driver happy
1967 PrintError("Error: Read disk info no-op to keep the Linux CD-ROM driver happy\n");
1968 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1969 rd_raise_interrupt(dev, channel);
1972 case 0x55: // mode select
1973 case 0xa6: // load/unload cd
1974 case 0x4b: // pause/resume
1975 case 0x45: // play audio
1976 case 0x47: // play audio msf
1977 case 0xbc: // play cd
1978 case 0xb9: // read cd msf
1979 case 0x44: // read header
1981 case 0xbb: // set cd speed
1982 case 0x4e: // stop play/scan
1985 PrintError("ATAPI command 0x%x not implemented yet\n",
1987 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1988 rd_raise_interrupt(dev, channel);
1991 PrintError("Unknown ATAPI command 0x%x (%d)\n",
1992 atapi_command, atapi_command);
1993 // We'd better signal the error if the user chose to continue
1994 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1995 rd_raise_interrupt(dev, channel);
2007 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
2009 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2010 struct controller_t * controller = &(drive->controller);
2012 // controller->byte_count is a union of controller->cylinder_no;
2013 // lazy is used to force a data read in the buffer at the next read.
2015 PrintDebug("[rd_init_send_atapi_cmd]\n");
2017 if (controller->byte_count == 0xffff) {
2018 controller->byte_count = 0xfffe;
2021 if ((controller->byte_count & 1) &&
2022 !(alloc_length <= controller->byte_count)) {
2024 PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n",
2025 controller->byte_count,
2027 controller->byte_count - 1);
2029 controller->byte_count -= 1;
2032 if (controller->byte_count == 0) {
2033 PrintError("\t\tATAPI command with zero byte count\n");
2037 if (alloc_length < 0) {
2038 PrintError("\t\tAllocation length < 0\n");
2042 if (alloc_length == 0) {
2043 alloc_length = controller->byte_count;
2046 controller->interrupt_reason.i_o = 1;
2047 controller->interrupt_reason.c_d = 0;
2048 controller->status.busy = 0;
2049 controller->status.drq = 1;
2050 controller->status.err = 0;
2052 // no bytes transfered yet
2054 controller->buffer_index = 2048;
2056 controller->buffer_index = 0;
2059 controller->drq_index = 0;
2061 if (controller->byte_count > req_length) {
2062 controller->byte_count = req_length;
2065 if (controller->byte_count > alloc_length) {
2066 controller->byte_count = alloc_length;
2069 drive->atapi.command = command;
2070 drive->atapi.drq_bytes = controller->byte_count;
2071 drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2074 // // bias drq_bytes and total_bytes_remaining
2075 // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2076 // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2084 void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2085 PrintDebug("[rd_ready_to_send_atapi]\n");
2087 rd_raise_interrupt(dev, channel);
2094 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2096 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2097 struct controller_t * controller = &(drive->controller);
2100 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2101 PrintError("[rd_atapi_cmd_error]\n");
2102 PrintError("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n",
2103 get_channel_no(ramdisk, channel), sense_key, asc);
2106 controller->error_register = sense_key << 4;
2107 controller->interrupt_reason.i_o = 1;
2108 controller->interrupt_reason.c_d = 1;
2109 controller->interrupt_reason.rel = 0;
2110 controller->status.busy = 0;
2111 controller->status.drive_ready = 1;
2112 controller->status.write_fault = 0;
2113 controller->status.drq = 0;
2114 controller->status.err = 1;
2116 drive->sense.sense_key = sense_key;
2117 drive->sense.asc = asc;
2118 drive->sense.ascq = 0;
2123 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2125 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2126 struct controller_t * controller = &(drive->controller);
2128 PrintDebug("[rd_atapi_cmd_nop]\n");
2129 controller->interrupt_reason.i_o = 1;
2130 controller->interrupt_reason.c_d = 1;
2131 controller->interrupt_reason.rel = 0;
2132 controller->status.busy = 0;
2133 controller->status.drive_ready = 1;
2134 controller->status.drq = 0;
2135 controller->status.err = 0;
2141 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2143 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2144 struct controller_t * controller = &(drive->controller);
2148 const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2149 const char* firmware = "ALPHA1 ";
2151 drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2153 for (i = 1; i <= 9; i++) {
2154 drive->id_drive[i] = 0;
2157 for (i = 0; i < 10; i++) {
2158 drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2159 (serial_number[(i * 2) + 1]));
2162 for (i = 20; i <= 22; i++) {
2163 drive->id_drive[i] = 0;
2166 for (i = 0; i < strlen(firmware) / 2; i++) {
2167 drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2168 (firmware[(i * 2) + 1]));
2170 V3_ASSERT((23 + i) == 27);
2172 for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2173 drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2174 (drive->model_no[(i * 2) + 1]));
2177 V3_ASSERT((27 + i) == 47);
2179 drive->id_drive[47] = 0;
2180 drive->id_drive[48] = 1; // 32 bits access
2182 drive->id_drive[49] = (1 << 9); // LBA supported
2184 drive->id_drive[50] = 0;
2185 drive->id_drive[51] = 0;
2186 drive->id_drive[52] = 0;
2188 drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2190 for (i = 54; i <= 62; i++) {
2191 drive->id_drive[i] = 0;
2194 // copied from CFA540A
2195 drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2196 drive->id_drive[64] = 0x0001; // PIO
2197 drive->id_drive[65] = 0x00b4;
2198 drive->id_drive[66] = 0x00b4;
2199 drive->id_drive[67] = 0x012c;
2200 drive->id_drive[68] = 0x00b4;
2202 drive->id_drive[69] = 0;
2203 drive->id_drive[70] = 0;
2204 drive->id_drive[71] = 30; // faked
2205 drive->id_drive[72] = 30; // faked
2206 drive->id_drive[73] = 0;
2207 drive->id_drive[74] = 0;
2209 drive->id_drive[75] = 0;
2211 for (i = 76; i <= 79; i++) {
2212 drive->id_drive[i] = 0;
2215 drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2216 drive->id_drive[81] = 0;
2217 drive->id_drive[82] = 0;
2218 drive->id_drive[83] = 0;
2219 drive->id_drive[84] = 0;
2220 drive->id_drive[85] = 0;
2221 drive->id_drive[86] = 0;
2222 drive->id_drive[87] = 0;
2223 drive->id_drive[88] = 0;
2225 for (i = 89; i <= 126; i++) {
2226 drive->id_drive[i] = 0;
2229 drive->id_drive[127] = 0;
2230 drive->id_drive[128] = 0;
2232 for (i = 129; i <= 159; i++) {
2233 drive->id_drive[i] = 0;
2236 for (i = 160; i <= 255; i++) {
2237 drive->id_drive[i] = 0;
2251 void rd_init_mode_sense_single(struct vm_device * dev,
2252 struct channel_t * channel, const void* src, int size)
2254 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2255 struct controller_t * controller = &(drive->controller);
2257 PrintDebug("[rd_init_mode_sense_single]\n");
2260 controller->buffer[0] = (size + 6) >> 8;
2261 controller->buffer[1] = (size + 6) & 0xff;
2262 controller->buffer[2] = 0x70; // no media present
2263 controller->buffer[3] = 0; // reserved
2264 controller->buffer[4] = 0; // reserved
2265 controller->buffer[5] = 0; // reserved
2266 controller->buffer[6] = 0; // reserved
2267 controller->buffer[7] = 0; // reserved
2270 memcpy(controller->buffer + 8, src, size);
2275 static void rd_command_aborted(struct vm_device * dev,
2276 struct channel_t * channel, unsigned value) {
2277 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2278 struct controller_t * controller = &(drive->controller);
2280 PrintError("[rd_command_aborted]\n");
2281 PrintError("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2283 controller->current_command = 0;
2284 controller->status.busy = 0;
2285 controller->status.drive_ready = 1;
2286 controller->status.err = 1;
2287 controller->error_register = 0x04; // command ABORTED
2288 controller->status.drq = 0;
2289 controller->status.seek_complete = 0;
2290 controller->status.corrected_data = 0;
2291 controller->buffer_index = 0;
2293 rd_raise_interrupt(dev, channel);
2298 static void init_pci(struct ramdisk_t * ramdisk) {
2299 struct v3_pci_bar bars[6];
2300 struct pci_device * pci_dev;
2303 for (i = 0; i < 6; i++) {
2304 bars[i].type = PCI_BAR_NONE;
2305 bars[i].mem_hook = 0;
2306 bars[i].num_pages = 0;
2307 bars[i].bar_update = NULL;
2310 bars[4].type = PCI_BAR_MEM32;
2311 bars[4].mem_hook = 0;
2312 bars[4].num_pages = 1;
2313 bars[4].bar_update = NULL;
2315 pci_dev = v3_pci_register_device(ramdisk->pci, PCI_STD_DEVICE, 0, "IDE", -1, bars, NULL, NULL, NULL, NULL);
2318 pci_dev->config_header.vendor_id = 0x8086;
2319 pci_dev->config_header.device_id = 0x2421;
2324 static int ramdisk_init_device(struct vm_device *dev) {
2325 struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2327 PrintDebug("Initializing Ramdisk\n");
2330 rd_init_hardware(ramdisk);
2333 v3_dev_hook_io(dev, PRI_CTRL_PORT,
2334 &read_status_port, &write_ctrl_port);
2336 v3_dev_hook_io(dev, PRI_DATA_PORT,
2337 &read_data_port, &write_data_port);
2338 v3_dev_hook_io(dev, PRI_FEATURES_PORT,
2339 &read_general_port, &write_general_port);
2340 v3_dev_hook_io(dev, PRI_SECT_CNT_PORT,
2341 &read_general_port, &write_general_port);
2342 v3_dev_hook_io(dev, PRI_SECT_ADDR1_PORT,
2343 &read_general_port, &write_general_port);
2344 v3_dev_hook_io(dev, PRI_SECT_ADDR2_PORT,
2345 &read_general_port, &write_general_port);
2346 v3_dev_hook_io(dev, PRI_SECT_ADDR3_PORT,
2347 &read_general_port, &write_general_port);
2348 v3_dev_hook_io(dev, PRI_DRV_SEL_PORT,
2349 &read_general_port, &write_general_port);
2350 v3_dev_hook_io(dev, PRI_CMD_PORT,
2351 &read_status_port, &write_cmd_port);
2354 v3_dev_hook_io(dev, SEC_CTRL_PORT,
2355 &read_status_port, &write_ctrl_port);
2357 v3_dev_hook_io(dev, SEC_DATA_PORT,
2358 &read_data_port, &write_data_port);
2359 v3_dev_hook_io(dev, SEC_FEATURES_PORT,
2360 &read_general_port, &write_general_port);
2361 v3_dev_hook_io(dev, SEC_SECT_CNT_PORT,
2362 &read_general_port, &write_general_port);
2363 v3_dev_hook_io(dev, SEC_SECT_ADDR1_PORT,
2364 &read_general_port, &write_general_port);
2365 v3_dev_hook_io(dev, SEC_SECT_ADDR2_PORT,
2366 &read_general_port, &write_general_port);
2367 v3_dev_hook_io(dev, SEC_SECT_ADDR3_PORT,
2368 &read_general_port, &write_general_port);
2369 v3_dev_hook_io(dev, SEC_DRV_SEL_PORT,
2370 &read_general_port, &write_general_port);
2371 v3_dev_hook_io(dev, SEC_CMD_PORT,
2372 &read_status_port, &write_cmd_port);
2376 v3_dev_hook_io(dev, SEC_ADDR_REG_PORT,
2377 &read_general_port, &write_general_port);
2379 v3_dev_hook_io(dev, PRI_ADDR_REG_PORT,
2380 &read_general_port, &write_general_port);
2391 static int ramdisk_deinit_device(struct vm_device *dev) {
2392 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2393 rd_close_harddrive(ramdisk);
2397 static struct vm_device_ops dev_ops = {
2398 .init = ramdisk_init_device,
2399 .deinit = ramdisk_deinit_device,
2408 struct vm_device * v3_create_ramdisk(struct vm_device * pci)
2411 struct ramdisk_t *ramdisk;
2412 ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));
2413 V3_ASSERT(ramdisk != NULL);
2415 // ramdisk->pci = pci;
2417 PrintDebug("[create_ramdisk]\n");
2419 struct vm_device * device = v3_create_device("RAMDISK", &dev_ops, ramdisk);
2429 #ifdef DEBUG_RAMDISK
2431 static void rd_print_state(struct ramdisk_t * ramdisk) {
2434 struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2437 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2438 memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2444 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2446 for (device = 0; device < 2; device++){
2448 // Initialize controller state, even if device is not present
2449 PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2451 channels[channel].drives[device].controller.status.busy);
2452 PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n",
2454 channels[channel].drives[device].controller.status.drive_ready);
2455 PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n",
2457 channels[channel].drives[device].controller.status.write_fault);
2458 PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n",
2460 channels[channel].drives[device].controller.status.seek_complete);
2461 PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n",
2463 channels[channel].drives[device].controller.status.drq);
2464 PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n",
2466 channels[channel].drives[device].controller.status.corrected_data);
2467 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n",
2469 channels[channel].drives[device].controller.status.index_pulse);
2470 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n",
2472 channels[channel].drives[device].controller.status.index_pulse_count);
2473 PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n",
2475 channels[channel].drives[device].controller.status.err);
2478 PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n",
2480 channels[channel].drives[device].controller.error_register);
2481 PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n",
2483 channels[channel].drives[device].controller.head_no);
2484 PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n",
2486 channels[channel].drives[device].controller.sector_count);
2487 PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n",
2489 channels[channel].drives[device].controller.sector_no);
2490 PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n",
2492 channels[channel].drives[device].controller.cylinder_no);
2493 PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n",
2495 channels[channel].drives[device].controller.current_command);
2496 PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n",
2498 channels[channel].drives[device].controller.buffer_index);
2501 PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n",
2503 channels[channel].drives[device].controller.control.reset);
2504 PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n",
2506 channels[channel].drives[device].controller.control.disable_irq);
2509 PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n",
2511 channels[channel].drives[device].controller.reset_in_progress);
2512 PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n",
2514 channels[channel].drives[device].controller.sectors_per_block);
2515 PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n",
2517 channels[channel].drives[device].controller.lba_mode);
2518 PrintDebug("channels[%d].drives[%d].controller.features = %d\n",
2520 channels[channel].drives[device].controller.features);
2523 PrintDebug("channels[%d].drives[%d].model_no = %s\n",
2525 channels[channel].drives[device].model_no);
2526 PrintDebug("channels[%d].drives[%d].device_type = %d\n",
2528 channels[channel].drives[device].device_type);
2529 PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n",
2531 channels[channel].drives[device].cdrom.locked);
2532 PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n",
2534 channels[channel].drives[device].sense.sense_key);
2535 PrintDebug("channels[%d].drives[%d].sense.asc = %d\n",
2537 channels[channel].drives[device].sense.asc);
2538 PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n",
2540 channels[channel].drives[device].sense.ascq);
2544 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n",
2546 channels[channel].drives[device].controller.interrupt_reason.c_d);
2548 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n",
2550 channels[channel].drives[device].controller.interrupt_reason.i_o);
2552 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n",
2554 channels[channel].drives[device].controller.interrupt_reason.rel);
2556 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n",
2558 channels[channel].drives[device].controller.interrupt_reason.tag);
2560 PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n",
2562 channels[channel].drives[device].cdrom.ready);