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>
47 #define PrintDebug(_f, _a...)
52 * Data type definitions
55 #define INDEX_PULSE_CYCLE 10
60 #define INTR_REASON_BIT_ERR 0x01
61 #define UNABLE_FIND_TAT_CHANNEL_ERR 0x02
63 #define READ_BUF_GT_512 0x04
67 #define PRI_DATA_PORT 0x1f0
68 #define PRI_FEATURES_PORT 0x1f1
69 #define PRI_SECT_CNT_PORT 0x1f2
70 #define PRI_SECT_ADDR1_PORT 0x1f3
71 #define PRI_SECT_ADDR2_PORT 0x1f4
72 #define PRI_SECT_ADDR3_PORT 0x1f5
73 #define PRI_DRV_SEL_PORT 0x1f6
74 #define PRI_CMD_PORT 0x1f7
75 #define PRI_CTRL_PORT 0x3f6
76 #define PRI_ADDR_REG_PORT 0x3f7
78 #define SEC_DATA_PORT 0x170
79 #define SEC_FEATURES_PORT 0x171
80 #define SEC_SECT_CNT_PORT 0x172
81 #define SEC_SECT_ADDR1_PORT 0x173
82 #define SEC_SECT_ADDR2_PORT 0x174
83 #define SEC_SECT_ADDR3_PORT 0x175
84 #define SEC_DRV_SEL_PORT 0x176
85 #define SEC_CMD_PORT 0x177
86 #define SEC_CTRL_PORT 0x376
87 #define SEC_ADDR_REG_PORT 0x377
93 #define PACKET_SIZE 12
100 #define ATA_DETECT 0xf0 //0X3E8
101 #define ATA_RESET 0xf1 //0X3E9
102 #define ATA_CMD_DATA_IN 0xf2 //0X3EA
103 #define ATA_CMD_DATA_OUT 0xf3 //0X3EB
104 #define ATA_CMD_PACKET 0xf4 //0X3EC
105 #define ATAPI_GET_SENSE 0xf5 //0X3ED
106 #define ATAPI_IS_READY 0xf6 //0X3EE
107 #define ATAPI_IS_CDROM 0xf7 //0X3EF
109 #define CDEMU_INIT 0xf8 //0X2E8
110 #define CDEMU_ISACTIVE 0xf9 //0X2E9
111 #define CDEMU_EMULATED_DRIVE 0xfa //0X2EA
112 #define CDROM_BOOT 0xfb //0X2EB
115 #define HARD_DRIVE_POST 0xfc //0X2EC
118 #define ATA_DEVICE_NO 0xfd //0X2ED
119 #define ATA_DEVICE_TYPE 0xfe //0X2ED
121 #define INT13_HARDDISK 0xff //0x2ef
122 #define INT13_CDROM 0xe0 //0x2f8
123 #define INT13_CDEMU 0xe1 //0x2f9
124 #define INT13_ELTORITO 0xe2 //0x2fa
125 #define INT13_DISKETTE_FUNCTION 0xe3 //0x2fb
130 static const char cdrom_str[] = "CD-ROM";
131 static const char harddisk_str[] = "HARDDISK";
132 static const char none_str[] = "NONE";
135 static inline const char * device_type_to_str(device_type_t type) {
149 static inline void write_features(struct channel_t * channel, uchar_t value) {
150 channel->drives[0].controller.features = value;
151 channel->drives[1].controller.features = value;
155 static inline void write_sector_count(struct channel_t * channel, uchar_t value) {
156 channel->drives[0].controller.sector_count = value;
157 channel->drives[1].controller.sector_count = value;
160 static inline void write_sector_number(struct channel_t * channel, uchar_t value) {
161 channel->drives[0].controller.sector_no = value;
162 channel->drives[1].controller.sector_no = value;
166 static inline void write_cylinder_low(struct channel_t * channel, uchar_t value) {
167 channel->drives[0].controller.cylinder_no &= 0xff00;
168 channel->drives[0].controller.cylinder_no |= value;
169 channel->drives[1].controller.cylinder_no &= 0xff00;
170 channel->drives[1].controller.cylinder_no |= value;
173 static inline void write_cylinder_high(struct channel_t * channel, uchar_t value) {
174 ushort_t val2 = value;
176 channel->drives[0].controller.cylinder_no &= 0x00ff;
177 channel->drives[0].controller.cylinder_no |= (val2 & 0xff00);
179 channel->drives[1].controller.cylinder_no &= 0x00ff;
180 channel->drives[1].controller.cylinder_no |= (val2 & 0xff00);
183 static inline void write_head_no(struct channel_t * channel, uchar_t value) {
184 channel->drives[0].controller.head_no = value;
185 channel->drives[1].controller.head_no = value;
188 static inline void write_lba_mode(struct channel_t * channel, uchar_t value) {
189 channel->drives[0].controller.lba_mode = value;
190 channel->drives[1].controller.lba_mode = value;
194 static inline uint_t get_channel_no(struct ramdisk_t * ramdisk, struct channel_t * channel) {
195 return (((uchar_t *)channel - (uchar_t *)(ramdisk->channels)) / sizeof(struct channel_t));
198 static inline uint_t get_drive_no(struct channel_t * channel, struct drive_t * drive) {
199 return (((uchar_t *)drive - (uchar_t*)(channel->drives)) / sizeof(struct drive_t));
202 static inline struct drive_t * get_selected_drive(struct channel_t * channel) {
203 return &(channel->drives[channel->drive_select]);
207 static inline int is_primary_port(struct ramdisk_t * ramdisk, ushort_t port) {
211 case PRI_FEATURES_PORT:
212 case PRI_SECT_CNT_PORT:
213 case PRI_SECT_ADDR1_PORT:
214 case PRI_SECT_ADDR2_PORT:
215 case PRI_SECT_ADDR3_PORT:
216 case PRI_DRV_SEL_PORT:
227 static inline int is_secondary_port(struct ramdisk_t * ramdisk, ushort_t port) {
231 case SEC_FEATURES_PORT:
232 case SEC_SECT_CNT_PORT:
233 case SEC_SECT_ADDR1_PORT:
234 case SEC_SECT_ADDR2_PORT:
235 case SEC_SECT_ADDR3_PORT:
236 case SEC_DRV_SEL_PORT:
245 static inline int num_drives_on_channel(struct channel_t * channel) {
246 if ((channel->drives[0].device_type == IDE_NONE) &&
247 (channel->drives[1].device_type == IDE_NONE)) {
249 } else if ((channel->drives[0].device_type != IDE_NONE) &&
250 (channel->drives[1].device_type != IDE_NONE)) {
259 static inline uchar_t extract_bits(uchar_t * buf, uint_t buf_offset, uint_t bit_offset, uint_t num_bits) {
260 uchar_t val = buf[buf_offset];
261 val = val >> bit_offset;
262 val &= ((1 << num_bits) -1);
267 static inline uchar_t get_packet_field(struct channel_t * channel, uint_t packet_offset, uint_t bit_offset, uint_t num_bits) {
268 struct drive_t * drive = get_selected_drive(channel);
269 return extract_bits(drive->controller.buffer, packet_offset, bit_offset, num_bits);
273 static inline uchar_t get_packet_byte(struct channel_t * channel, uint_t offset) {
274 struct drive_t * drive = get_selected_drive(channel);
275 return drive->controller.buffer[offset];
278 static inline uint16_t get_packet_word(struct channel_t * channel, uint_t offset) {
279 struct drive_t * drive = get_selected_drive(channel);
280 uint16_t val = drive->controller.buffer[offset];
282 val |= drive->controller.buffer[offset + 1];
287 static inline uint16_t rd_read_16bit(const uint8_t* buf) {
288 return (buf[0] << 8) | buf[1];
293 static inline uint32_t rd_read_32bit(const uint8_t* buf) {
294 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
297 ////////////////////////////////////////////////////////////////////////////
305 static void rd_init_mode_sense_single(struct vm_device * dev, struct channel_t * channel, const void * src, int size);
307 static void rd_command_aborted(struct vm_device * dev, struct channel_t * channel, unsigned value);
312 static int handle_atapi_packet_command(struct vm_device * dev,
313 struct channel_t * channel,
316 static int rd_init_send_atapi_command(struct vm_device * dev,
317 struct channel_t * channel,
318 Bit8u command, int req_length,
319 int alloc_length, bool lazy);
321 static void rd_ready_to_send_atapi(struct vm_device * dev,
322 struct channel_t * channel);
324 static void rd_atapi_cmd_error(struct vm_device * dev,
325 struct channel_t * channel,
326 sense_t sense_key, asc_t asc);
328 static void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel);
329 static void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel);
336 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel);
337 static void rd_lower_irq(struct vm_device *dev, Bit32u irq);
348 static void rd_print_state(struct ramdisk_t *ramdisk);
349 static int check_bit_fields(struct controller_t * controller);
351 ////////////////////////////////////////////////////////////////////
357 int v3_ramdisk_register_cdrom(struct vm_device * dev, uint_t busID, uint_t driveID, struct cdrom_ops* cd, void * private_data) {
358 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
359 struct channel_t * channel = &(ramdisk->channels[busID]);
360 struct drive_t * drive = &(channel->drives[driveID]);
361 struct controller_t * controller = &(drive->controller);
365 if (drive->device_type != IDE_NONE) {
366 PrintError("Device already registered at this location\n");
374 strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
376 while (strlen((char *)(drive->model_no)) < 40) {
377 strcat ((char*)(drive->model_no), " ");
380 PrintDebug("CDROM on target %d/%d\n", busID, driveID);
382 drive->device_type = IDE_CDROM;
383 drive->cdrom.locked = 0;
384 drive->sense.sense_key = SENSE_NONE;
385 drive->sense.asc = 0;
386 drive->sense.ascq = 0;
388 drive->private_data = private_data;
391 if (check_bit_fields(controller) == INTR_REASON_BIT_ERR) {
392 PrintDebug("interrupt reason: bit field error\n");
393 return INTR_REASON_BIT_ERR;
397 controller->sector_count = 0;
399 drive->cdrom.cd = cd;
401 PrintDebug("\t\tCD on ata%d-%d: '%s'\n",
405 if(drive->cdrom.cd->insert_cdrom(drive->private_data)) {
406 PrintDebug("\t\tMedia present in CD-ROM drive\n");
407 drive->cdrom.ready = 1;
408 drive->cdrom.capacity = drive->cdrom.cd->capacity(drive->private_data);
409 PrintDebug("\t\tCDROM capacity is %d\n", drive->cdrom.capacity);
411 PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
412 drive->cdrom.ready = 0;
419 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
422 struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
424 PrintDebug("[rd_init_harddrive]\n");
426 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
427 memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
430 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
431 struct channel_t * channel = &(channels[channel_num]);
433 channel->ioaddr1 = 0x0;
434 channel->ioaddr2 = 0x0;
437 for (device = 0; device < 2; device++){
438 struct drive_t * drive = &(channel->drives[device]);
439 struct controller_t * controller = &(drive->controller);
441 controller->status.busy = 0;
442 controller->status.drive_ready = 1;
443 controller->status.write_fault = 0;
444 controller->status.seek_complete = 1;
445 controller->status.drq = 0;
446 controller->status.corrected_data = 0;
447 controller->status.index_pulse = 0;
448 controller->status.index_pulse_count = 0;
449 controller->status.err = 0;
451 controller->error_register = 0x01; // diagnostic code: no error
452 controller->head_no = 0;
453 controller->sector_count = 1;
454 controller->sector_no = 1;
455 controller->cylinder_no = 0;
456 controller->current_command = 0x00;
457 controller->buffer_index = 0;
459 controller->control.reset = 0;
460 controller->control.disable_irq = 0;
461 controller->reset_in_progress = 0;
463 controller->sectors_per_block = 0x80;
464 controller->lba_mode = 0;
467 controller->features = 0;
470 drive->device_type = IDE_NONE;
473 strncpy((char*)(drive->model_no), "", 40);
474 while(strlen((char *)(drive->model_no)) < 40) {
475 strcat ((char*)(drive->model_no), " ");
482 rd_print_state(ramdisk);
489 static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
494 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
499 ////////////////////////////////////////////////////////////////////
503 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
504 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
505 struct channel_t * channel = NULL;
506 struct drive_t * drive = NULL;
507 struct controller_t * controller = NULL;
511 if (is_primary_port(ramdisk, port)) {
512 channel = &(ramdisk->channels[0]);
513 } else if (is_secondary_port(ramdisk, port)) {
514 channel = &(ramdisk->channels[1]);
516 PrintError("Invalid Port: %d\n", port);
520 drive = get_selected_drive(channel);
521 controller = &(drive->controller);
524 PrintDebug("[read_data_handler] IO Read at 0x%x, on drive %d/%d (current_cmd = 0x%02x)\n",
526 get_channel_no(ramdisk, channel),
527 get_drive_no(channel, drive),
528 controller->current_command);
530 switch (controller->current_command) {
531 case 0xec: // IDENTIFY DEVICE
536 controller->status.busy = 0;
537 controller->status.drive_ready = 1;
538 controller->status.write_fault = 0;
539 controller->status.seek_complete = 1;
540 controller->status.corrected_data = 0;
541 controller->status.err = 0;
544 value32 = controller->buffer[index];
548 value32 |= (controller->buffer[index] << 8);
553 value32 |= (controller->buffer[index] << 16);
554 value32 |= (controller->buffer[index+1] << 24);
558 controller->buffer_index = index;
561 memcpy(dst, controller->buffer + controller->buffer_index, length);
562 controller->buffer_index += length;
564 if (controller->buffer_index >= 512) {
565 controller->status.drq = 0;
570 case 0xa0: //send packet cmd
572 uint_t index = controller->buffer_index;
575 PrintDebug("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n",
576 drive->atapi.command,
578 drive->cdrom.remaining_blocks);
580 // Load block if necessary
584 PrintError("\t\tindex > 2048 : 0x%x\n", index);
588 switch (drive->atapi.command) {
589 case 0x28: // read (10)
590 case 0xa8: // read (12)
593 if (!(drive->cdrom.ready)) {
594 PrintError("\t\tRead with CDROM not ready\n");
598 drive->cdrom.cd->read_block(drive->private_data, controller->buffer,
599 drive->cdrom.next_lba);
600 drive->cdrom.next_lba++;
601 drive->cdrom.remaining_blocks--;
604 if (!(drive->cdrom.remaining_blocks)) {
605 PrintDebug("\t\tLast READ block loaded {CDROM}\n");
607 PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
608 drive->cdrom.remaining_blocks);
611 // one block transfered, start at beginning
615 default: // no need to load a new block
623 value32 = controller->buffer[index + increment];
627 value32 |= (controller->buffer[index + increment] << 8);
632 value32 |= (controller->buffer[index + increment] << 16);
633 value32 |= (controller->buffer[index + increment + 1] << 24);
637 controller->buffer_index = index + increment;
638 controller->drq_index += increment;
641 /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
643 memcpy(dst, controller->buffer + index, length);
645 controller->buffer_index = index + length;
646 controller->drq_index += length;
651 if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
652 controller->status.drq = 0;
653 controller->drq_index = 0;
655 drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
657 if (drive->atapi.total_bytes_remaining > 0) {
658 // one or more blocks remaining (works only for single block commands)
660 PrintDebug("\t\tPACKET drq bytes read\n");
661 controller->interrupt_reason.i_o = 1;
662 controller->status.busy = 0;
663 controller->status.drq = 1;
664 controller->interrupt_reason.c_d = 0;
666 // set new byte count if last block
667 if (drive->atapi.total_bytes_remaining < controller->byte_count) {
668 controller->byte_count = drive->atapi.total_bytes_remaining;
670 drive->atapi.drq_bytes = controller->byte_count;
672 rd_raise_interrupt(dev, channel);
675 PrintDebug("\t\tPACKET all bytes read\n");
677 controller->interrupt_reason.i_o = 1;
678 controller->interrupt_reason.c_d = 1;
679 controller->status.drive_ready = 1;
680 controller->interrupt_reason.rel = 0;
681 controller->status.busy = 0;
682 controller->status.drq = 0;
683 controller->status.err = 0;
685 rd_raise_interrupt(dev, channel);
693 PrintDebug("\t\tread need support more command: %02x\n", controller->current_command);
703 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
704 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
705 struct channel_t * channel = NULL;
706 struct drive_t * drive = NULL;
707 struct controller_t * controller = NULL;
709 if (is_primary_port(ramdisk, port)) {
710 channel = &(ramdisk->channels[0]);
711 } else if (is_secondary_port(ramdisk, port)) {
712 channel = &(ramdisk->channels[1]);
714 PrintError("Invalid Port: %d\n", port);
718 drive = get_selected_drive(channel);
719 controller = &(drive->controller);
721 PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n",
722 port, controller->current_command);
724 switch (controller->current_command) {
725 case 0x30: // WRITE SECTORS
726 PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
731 if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
738 PrintError("\t\tIO write(0x%x): current command is %02xh\n",
739 port, controller->current_command);
751 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
752 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
753 struct channel_t * channel = NULL;
754 struct drive_t * drive = NULL;
755 struct controller_t * controller = NULL;
759 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
763 if (is_primary_port(ramdisk, port)) {
764 channel = &(ramdisk->channels[0]);
765 } else if (is_secondary_port(ramdisk, port)) {
766 channel = &(ramdisk->channels[1]);
768 PrintError("Invalid Port: %d\n", port);
772 drive = get_selected_drive(channel);
773 controller = &(drive->controller);
775 PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n",
776 port, get_channel_no(ramdisk, channel),
777 channel->drive_select);
780 if (num_drives_on_channel(channel) == 0) {
781 // (mch) Just return zero for these registers
782 memset(dst, 0, length);
786 (controller->status.busy << 7) |
787 (controller->status.drive_ready << 6) |
788 (controller->status.write_fault << 5) |
789 (controller->status.seek_complete << 4) |
790 (controller->status.drq << 3) |
791 (controller->status.corrected_data << 2) |
792 (controller->status.index_pulse << 1) |
793 (controller->status.err) );
795 memcpy(dst, &val, length);
797 controller->status.index_pulse_count++;
798 controller->status.index_pulse = 0;
800 if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
801 controller->status.index_pulse = 1;
802 controller->status.index_pulse_count = 0;
806 if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
807 rd_lower_irq(dev, channel->irq);
815 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
816 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
817 struct channel_t * channel = NULL;
818 struct drive_t * drive = NULL;
819 struct controller_t * controller = NULL;
820 uchar_t value = *(uchar_t *)src;
823 PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
827 if (is_primary_port(ramdisk, port)) {
828 channel = &(ramdisk->channels[0]);
829 } else if (is_secondary_port(ramdisk, port)) {
830 channel = &(ramdisk->channels[1]);
832 PrintError("Invalid Port: %d\n", port);
836 drive = get_selected_drive(channel);
837 controller = &(drive->controller);
840 PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
841 port, get_channel_no(ramdisk, channel),
842 channel->drive_select,
846 case 0xec: // IDENTIFY DEVICE
848 if (drive->device_type == IDE_NONE) {
849 PrintError("\t\tError: disk ata%d-%d not present, aborting\n",
850 get_channel_no(ramdisk, channel),
851 get_drive_no(channel, drive));
852 rd_command_aborted(dev, channel, value);
854 } else if (drive->device_type == IDE_CDROM) {
855 controller->head_no = 0;
856 controller->sector_count = 1;
857 controller->sector_no = 1;
858 controller->cylinder_no = 0xeb14;
859 rd_command_aborted(dev, channel, 0xec);
861 PrintError("\t\tError: Want to identify HDD!!\n");
863 SELECTED_CONTROLLER(channel).current_command = value;
864 SELECTED_CONTROLLER(channel).error_register = 0;
866 // See ATA/ATAPI-4, 8.12
867 SELECTED_CONTROLLER(channel).status.busy = 0;
868 SELECTED_CONTROLLER(channel).status.drive_ready = 1;
869 SELECTED_CONTROLLER(channel).status.write_fault = 0;
870 SELECTED_CONTROLLER(channel).status.drq = 1;
871 SELECTED_CONTROLLER(channel).status.err = 0;
873 SELECTED_CONTROLLER(channel).status.seek_complete = 1;
874 SELECTED_CONTROLLER(channel).status.corrected_data = 0;
876 SELECTED_CONTROLLER(channel).buffer_index = 0;
877 raise_interrupt(channel);
878 identify_drive(channel);
885 case 0xa1: // IDENTIFY PACKET DEVICE
887 if (drive->device_type == IDE_CDROM) {
888 controller->current_command = value;
889 controller->error_register = 0;
891 controller->status.busy = 0;
892 controller->status.drive_ready = 1;
893 controller->status.write_fault = 0;
894 controller->status.drq = 1;
895 controller->status.err = 0;
897 controller->status.seek_complete = 1;
898 controller->status.corrected_data = 0;
900 controller->buffer_index = 0;
901 rd_raise_interrupt(dev, channel);
902 rd_identify_ATAPI_drive(dev, channel);
904 rd_command_aborted(dev, channel, 0xa1);
908 case 0xa0: // SEND PACKET (atapi)
910 if (drive->device_type == IDE_CDROM) {
913 if (controller->features & (1 << 0)) {
914 PrintError("\t\tPACKET-DMA not supported");
918 if (controller->features & (1 << 1)) {
919 PrintError("\t\tPACKET-overlapped not supported");
923 // We're already ready!
924 controller->sector_count = 1;
925 controller->status.busy = 0;
926 controller->status.write_fault = 0;
929 controller->status.drq = 1;
930 controller->status.err = 0;
932 // NOTE: no interrupt here
933 controller->current_command = value;
934 controller->buffer_index = 0;
936 rd_command_aborted (dev, channel, 0xa0);
941 PrintError("\t\tneed translate command %2x\n", value);
943 /* JRL THIS NEEDS TO CHANGE */
951 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
952 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
953 struct channel_t * channel = NULL;
954 struct drive_t * master_drive = NULL;
955 struct drive_t * slave_drive = NULL;
956 struct controller_t * controller = NULL;
957 uchar_t value = *(uchar_t *)src;
958 rd_bool prev_control_reset;
961 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
965 if (is_primary_port(ramdisk, port)) {
966 channel = &(ramdisk->channels[0]);
967 } else if (is_secondary_port(ramdisk, port)) {
968 channel = &(ramdisk->channels[1]);
970 PrintError("Invalid Port: %d\n", port);
974 master_drive = &(channel->drives[0]);
975 slave_drive = &(channel->drives[1]);
977 controller = &(get_selected_drive(channel)->controller);
980 PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
981 port, get_channel_no(ramdisk, channel),
982 channel->drive_select,
985 // (mch) Even if device 1 was selected, a write to this register
986 // goes to device 0 (if device 1 is absent)
988 prev_control_reset = controller->control.reset;
990 master_drive->controller.control.reset = value & 0x04;
991 slave_drive->controller.control.reset = value & 0x04;
993 // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq = value & 0x02;
994 master_drive->controller.control.disable_irq = value & 0x02;
995 slave_drive->controller.control.disable_irq = value & 0x02;
997 PrintDebug("\t\tadpater control reg: reset controller = %d\n",
998 (unsigned) (controller->control.reset) ? 1 : 0);
999 PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
1000 (unsigned) (controller->control.disable_irq) ? 1 : 0);
1002 if ((!prev_control_reset) && (controller->control.reset)) {
1005 // transition from 0 to 1 causes all drives to reset
1006 PrintDebug("\t\thard drive: RESET\n");
1008 // (mch) Set BSY, drive not ready
1009 for (id = 0; id < 2; id++) {
1010 struct controller_t * ctrl = NULL;
1013 ctrl = &(master_drive->controller);
1014 } else if (id == 1) {
1015 ctrl = &(slave_drive->controller);
1018 ctrl->status.busy = 1;
1019 ctrl->status.drive_ready = 0;
1020 ctrl->reset_in_progress = 1;
1022 ctrl->status.write_fault = 0;
1023 ctrl->status.seek_complete = 1;
1024 ctrl->status.drq = 0;
1025 ctrl->status.corrected_data = 0;
1026 ctrl->status.err = 0;
1028 ctrl->error_register = 0x01; // diagnostic code: no error
1030 ctrl->current_command = 0x00;
1031 ctrl->buffer_index = 0;
1033 ctrl->sectors_per_block = 0x80;
1036 ctrl->control.disable_irq = 0;
1039 rd_lower_irq(dev, channel->irq);
1041 } else if ((controller->reset_in_progress) &&
1042 (!controller->control.reset)) {
1044 // Clear BSY and DRDY
1045 PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
1047 for (id = 0; id < 2; id++) {
1048 struct controller_t * ctrl = NULL;
1049 struct drive_t * drv = NULL;
1052 ctrl = &(master_drive->controller);
1054 } else if (id == 1) {
1055 ctrl = &(slave_drive->controller);
1059 ctrl->status.busy = 0;
1060 ctrl->status.drive_ready = 1;
1061 ctrl->reset_in_progress = 0;
1064 if (drv->device_type == IDE_DISK) {
1065 PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1067 ctrl->sector_count = 1;
1068 ctrl->sector_no = 1;
1069 ctrl->cylinder_no = 0;
1072 ctrl->sector_count = 1;
1073 ctrl->sector_no = 1;
1074 ctrl->cylinder_no = 0xeb14;
1079 PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n",
1080 master_drive->controller.control.disable_irq);
1081 PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n",
1082 slave_drive->controller.control.disable_irq);
1087 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1088 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1089 struct channel_t * channel = NULL;
1090 struct drive_t * drive = NULL;
1091 struct controller_t * controller = NULL;
1095 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1099 if (is_primary_port(ramdisk, port)) {
1100 channel = &(ramdisk->channels[0]);
1101 } else if (is_secondary_port(ramdisk, port)) {
1102 channel = &(ramdisk->channels[1]);
1104 PrintError("Invalid Port: %d\n", port);
1108 drive = get_selected_drive(channel);
1109 controller = &(drive->controller);
1112 PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n",
1113 port, get_channel_no(ramdisk, channel),
1114 channel->drive_select,
1115 controller->current_command);
1119 case PRI_FEATURES_PORT:
1120 case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1122 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1124 controller->status.err = 0;
1126 *(uchar_t *)dst = val;
1132 case PRI_SECT_CNT_PORT:
1133 case SEC_SECT_CNT_PORT: // hard disk sector count / interrupt reason 0x1f2
1135 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1137 *(uchar_t *)dst = val;
1142 case PRI_SECT_ADDR1_PORT:
1143 case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1145 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1147 *(uchar_t *)dst = val;
1153 case PRI_SECT_ADDR2_PORT:
1154 case SEC_SECT_ADDR2_PORT: // cylinder low 0x1f4
1156 // -- WARNING : On real hardware the controller registers are shared between drives.
1157 // So we must respond even if the select device is not present. Some OS uses this fact
1158 // to detect the disks.... minix2 for example
1159 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no & 0x00ff);
1161 *(uchar_t *)dst = val;
1167 case PRI_SECT_ADDR3_PORT:
1168 case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1170 // -- WARNING : On real hardware the controller registers are shared between drives.
1171 // So we must respond even if the select device is not present. Some OS uses this fact
1172 // to detect the disks.... minix2 for example
1173 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1175 *(uchar_t *)dst = val;
1180 case PRI_DRV_SEL_PORT:
1181 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1183 // b7 Extended data field for ECC
1184 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1185 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1186 // b6 1=LBA mode, 0=CHS mode
1190 uchar_t val = ((1 << 7) |
1191 ((controller->lba_mode > 0) << 6) |
1192 (1 << 5) | // 01b = 512 sector size
1193 (channel->drive_select << 4) |
1194 (controller->head_no << 0));
1196 *(uchar_t *)dst = val;
1201 case PRI_ADDR_REG_PORT:
1202 case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1204 // Obsolete and unsupported register. Not driven by hard
1205 // disk controller. Report all 1's. If floppy controller
1206 // is handling this address, it will call this function
1207 // set/clear D7 (the only bit it handles), then return
1208 // the combined value
1209 *(uchar_t *)dst = 0xff;
1214 PrintError("Invalid Port: %d\n", port);
1222 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1223 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1224 struct channel_t * channel = NULL;
1225 struct drive_t * drive = NULL;
1226 struct controller_t * controller = NULL;
1227 uchar_t value = *(uchar_t *)src;
1230 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1234 if (is_primary_port(ramdisk, port)) {
1235 channel = &(ramdisk->channels[0]);
1236 } else if (is_secondary_port(ramdisk, port)) {
1237 channel = &(ramdisk->channels[1]);
1239 PrintError("Invalid Port: %d\n", port);
1243 drive = get_selected_drive(channel);
1244 controller = &(drive->controller);
1247 PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n",
1248 port, value, get_channel_no(ramdisk, channel));
1252 case PRI_FEATURES_PORT:
1253 case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1255 write_features(channel, value);
1258 case PRI_SECT_CNT_PORT:
1259 case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1261 write_sector_count(channel, value);
1264 case PRI_SECT_ADDR1_PORT:
1265 case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1267 write_sector_number(channel, value);
1270 case PRI_SECT_ADDR2_PORT:
1271 case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1273 write_cylinder_low(channel, value);
1276 case PRI_SECT_ADDR3_PORT:
1277 case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1279 write_cylinder_high(channel, value);
1282 case PRI_DRV_SEL_PORT:
1283 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1285 // b7 Extended data field for ECC
1286 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1287 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1288 // b6 1=LBA mode, 0=CHS mode
1295 PrintDebug("\tDrive Select value=%x\n", value);
1297 if ((value & 0xa0) != 0xa0) {
1298 PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1301 write_head_no(channel, value & 0xf);
1302 if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1303 PrintDebug("\t\tenabling LBA mode\n");
1306 write_lba_mode(channel, (value >> 6) & 1);
1310 if (drive->cdrom.cd) {
1311 PrintDebug("\t\tSetting LBA on CDROM: %d\n", (value >> 6) & 1);
1312 drive->cdrom.cd->set_LBA(drive->private_data, (value >> 6) & 1);
1316 channel->drive_select = (value >> 4) & 0x01;
1317 drive = get_selected_drive(channel);
1319 if (drive->device_type == IDE_NONE) {
1320 PrintDebug("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1321 channel->drive_select, get_channel_no(ramdisk, channel));
1323 controller->error_register = 0x04; // aborted
1324 controller->status.err = 1;
1330 PrintError("\t\thard drive: io write to unhandled port 0x%x (value = %c)\n", port, value);
1341 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1343 // struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1344 struct drive_t * drive = get_selected_drive(channel);
1345 struct controller_t * controller = &(drive->controller);
1347 PrintDebug("[raise_interrupt] disable_irq = %02x\n", controller->control.disable_irq);
1349 if (!(controller->control.disable_irq)) {
1352 PrintDebug("\t\tRaising interrupt %d {%s}\n\n", irq, device_type_to_str(drive->device_type));
1354 dev->vm->vm_ops.raise_irq(dev->vm, irq);
1356 PrintDebug("\t\tirq is disabled\n");
1362 static void rd_lower_irq(struct vm_device *dev, Bit32u irq) // __attribute__(regparm(1))
1364 PrintDebug("[lower_irq] irq = %d\n", irq);
1365 dev->vm->vm_ops.lower_irq(dev->vm, irq);
1374 //////////////////////////////////////////////////////////////////////////
1382 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1383 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1384 struct drive_t * drive = get_selected_drive(channel);
1385 struct controller_t * controller = &(drive->controller);
1387 if (controller->buffer_index >= PACKET_SIZE) {
1388 PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n",
1389 controller->buffer_index);
1393 controller->buffer[controller->buffer_index] = value;
1394 controller->buffer[controller->buffer_index + 1] = (value >> 8);
1395 controller->buffer_index += 2;
1398 /* if packet completely writtten */
1399 if (controller->buffer_index >= PACKET_SIZE) {
1400 // complete command received
1401 Bit8u atapi_command = controller->buffer[0];
1403 PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1405 switch (atapi_command) {
1406 case 0x00: // test unit ready
1408 if (drive->cdrom.ready) {
1409 rd_atapi_cmd_nop(dev, channel);
1411 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1414 rd_raise_interrupt(dev, channel);
1418 case 0x03: // request sense
1420 int alloc_length = controller->buffer[4];
1422 if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1427 controller->buffer[0] = 0x70 | (1 << 7);
1428 controller->buffer[1] = 0;
1429 controller->buffer[2] = drive->sense.sense_key;
1430 controller->buffer[3] = drive->sense.information.arr[0];
1431 controller->buffer[4] = drive->sense.information.arr[1];
1432 controller->buffer[5] = drive->sense.information.arr[2];
1433 controller->buffer[6] = drive->sense.information.arr[3];
1434 controller->buffer[7] = 17 - 7;
1435 controller->buffer[8] = drive->sense.specific_inf.arr[0];
1436 controller->buffer[9] = drive->sense.specific_inf.arr[1];
1437 controller->buffer[10] = drive->sense.specific_inf.arr[2];
1438 controller->buffer[11] = drive->sense.specific_inf.arr[3];
1439 controller->buffer[12] = drive->sense.asc;
1440 controller->buffer[13] = drive->sense.ascq;
1441 controller->buffer[14] = drive->sense.fruc;
1442 controller->buffer[15] = drive->sense.key_spec.arr[0];
1443 controller->buffer[16] = drive->sense.key_spec.arr[1];
1444 controller->buffer[17] = drive->sense.key_spec.arr[2];
1446 rd_ready_to_send_atapi(dev, channel);
1449 case 0x1b: // start stop unit
1451 //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1452 rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1453 rd_bool Start = (controller->buffer[4] >> 0) & 1;
1456 if ((!LoEj) && (!Start)) {
1457 PrintError("FIXME: Stop disc not implemented\n");
1459 rd_atapi_cmd_nop(dev, channel);
1460 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);
1468 } else if (LoEj && !Start) { // Eject the disc
1469 rd_atapi_cmd_nop(dev, channel);
1471 if (drive->cdrom.ready) {
1473 drive->cdrom.cd->eject_cdrom(drive->private_data);
1475 drive->cdrom.ready = 0;
1476 //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1477 //bx_gui->update_drive_status_buttons();
1479 rd_raise_interrupt(dev, channel);
1480 } else { // Load the disc
1481 // My guess is that this command only closes the tray, that's a no-op for us
1482 rd_atapi_cmd_nop(dev, channel);
1483 rd_raise_interrupt(dev, channel);
1487 case 0xbd: // mechanism status
1489 uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1491 if (alloc_length == 0) {
1492 PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1496 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1500 controller->buffer[0] = 0; // reserved for non changers
1501 controller->buffer[1] = 0; // reserved for non changers
1503 controller->buffer[2] = 0; // Current LBA (TODO!)
1504 controller->buffer[3] = 0; // Current LBA (TODO!)
1505 controller->buffer[4] = 0; // Current LBA (TODO!)
1507 controller->buffer[5] = 1; // one slot
1509 controller->buffer[6] = 0; // slot table length
1510 controller->buffer[7] = 0; // slot table length
1512 rd_ready_to_send_atapi(dev, channel);
1515 case 0x5a: // mode sense
1517 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1519 Bit8u PC = controller->buffer[2] >> 6;
1520 Bit8u PageCode = controller->buffer[2] & 0x3f;
1523 case 0x0: // current values
1526 case 0x01: // error recovery
1529 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1533 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1534 sizeof(struct error_recovery_t));
1535 rd_ready_to_send_atapi(dev, channel);
1538 case 0x2a: // CD-ROM capabilities & mech. status
1541 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1545 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1547 controller->buffer[8] = 0x2a;
1548 controller->buffer[9] = 0x12;
1549 controller->buffer[10] = 0x00;
1550 controller->buffer[11] = 0x00;
1551 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1552 controller->buffer[12] = 0x70;
1553 controller->buffer[13] = (3 << 5);
1554 controller->buffer[14] = (unsigned char) (1 |
1555 (drive->cdrom.locked ? (1 << 1) : 0) |
1558 controller->buffer[15] = 0x00;
1559 controller->buffer[16] = (706 >> 8) & 0xff;
1560 controller->buffer[17] = 706 & 0xff;
1561 controller->buffer[18] = 0;
1562 controller->buffer[19] = 2;
1563 controller->buffer[20] = (512 >> 8) & 0xff;
1564 controller->buffer[21] = 512 & 0xff;
1565 controller->buffer[22] = (706 >> 8) & 0xff;
1566 controller->buffer[23] = 706 & 0xff;
1567 controller->buffer[24] = 0;
1568 controller->buffer[25] = 0;
1569 controller->buffer[26] = 0;
1570 controller->buffer[27] = 0;
1571 rd_ready_to_send_atapi(dev, channel);
1574 case 0x0d: // CD-ROM
1575 case 0x0e: // CD-ROM audio control
1578 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1580 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1581 ASC_INV_FIELD_IN_CMD_PACKET);
1582 rd_raise_interrupt(dev, channel);
1587 // not implemeted by this device
1588 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1590 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1591 ASC_INV_FIELD_IN_CMD_PACKET);
1592 rd_raise_interrupt(dev, channel);
1598 case 0x1: // changeable values
1601 case 0x01: // error recovery
1602 case 0x0d: // CD-ROM
1603 case 0x0e: // CD-ROM audio control
1604 case 0x2a: // CD-ROM capabilities & mech. status
1607 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1609 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1610 ASC_INV_FIELD_IN_CMD_PACKET);
1611 rd_raise_interrupt(dev, channel);
1616 // not implemeted by this device
1617 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1619 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1620 ASC_INV_FIELD_IN_CMD_PACKET);
1621 rd_raise_interrupt(dev, channel);
1627 case 0x2: // default values
1630 case 0x01: // error recovery
1631 case 0x0d: // CD-ROM
1632 case 0x0e: // CD-ROM audio control
1633 case 0x2a: // CD-ROM capabilities & mech. status
1635 PrintError("cdrom: MODE SENSE (dflt), code=%x\n",
1641 // not implemeted by this device
1642 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1644 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1645 ASC_INV_FIELD_IN_CMD_PACKET);
1646 rd_raise_interrupt(dev, channel);
1652 case 0x3: // saved values not implemented
1654 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1655 rd_raise_interrupt(dev, channel);
1660 PrintError("Should not get here!\n");
1667 case 0x12: // inquiry
1669 uint8_t alloc_length = controller->buffer[4];
1671 if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1675 controller->buffer[0] = 0x05; // CD-ROM
1676 controller->buffer[1] = 0x80; // Removable
1677 controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1678 controller->buffer[3] = 0x21; // ATAPI-2, as specified
1679 controller->buffer[4] = 31; // additional length (total 36)
1680 controller->buffer[5] = 0x00; // reserved
1681 controller->buffer[6] = 0x00; // reserved
1682 controller->buffer[7] = 0x00; // reserved
1685 const char* vendor_id = "VTAB ";
1687 for (i = 0; i < 8; i++) {
1688 controller->buffer[8+i] = vendor_id[i];
1692 const char* product_id = "Turbo CD-ROM ";
1693 for (i = 0; i < 16; i++) {
1694 controller->buffer[16+i] = product_id[i];
1697 // Product Revision level
1698 const char* rev_level = "1.0 ";
1699 for (i = 0; i < 4; i++) {
1700 controller->buffer[32 + i] = rev_level[i];
1703 rd_ready_to_send_atapi(dev, channel);
1706 case 0x25: // read cd-rom capacity
1708 // no allocation length???
1709 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1713 if (drive->cdrom.ready) {
1714 uint32_t capacity = drive->cdrom.capacity;
1716 PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1718 controller->buffer[0] = (capacity >> 24) & 0xff;
1719 controller->buffer[1] = (capacity >> 16) & 0xff;
1720 controller->buffer[2] = (capacity >> 8) & 0xff;
1721 controller->buffer[3] = (capacity >> 0) & 0xff;
1722 controller->buffer[4] = (2048 >> 24) & 0xff;
1723 controller->buffer[5] = (2048 >> 16) & 0xff;
1724 controller->buffer[6] = (2048 >> 8) & 0xff;
1725 controller->buffer[7] = (2048 >> 0) & 0xff;
1727 rd_ready_to_send_atapi(dev, channel);
1729 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1730 rd_raise_interrupt(dev, channel);
1736 case 0xbe: // read cd
1738 if (drive->cdrom.ready) {
1739 PrintError("Read CD with CD present not implemented\n");
1740 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1741 rd_raise_interrupt(dev, channel);
1743 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1744 rd_raise_interrupt(dev, channel);
1748 case 0x43: // read toc
1750 if (drive->cdrom.ready) {
1752 bool msf = (controller->buffer[1] >> 1) & 1;
1753 uint8_t starting_track = controller->buffer[6];
1755 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1757 uint8_t format = (controller->buffer[9] >> 6);
1762 if (!(drive->cdrom.cd->read_toc(drive->private_data, controller->buffer,
1763 &toc_length, msf, starting_track))) {
1764 PrintDebug("CDROM: Reading Table of Contents Failed\n");
1765 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1766 ASC_INV_FIELD_IN_CMD_PACKET);
1767 rd_raise_interrupt(dev, channel);
1769 if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1772 rd_ready_to_send_atapi(dev, channel);
1777 // multi session stuff. we ignore this and emulate a single session only
1778 if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1782 controller->buffer[0] = 0;
1783 controller->buffer[1] = 0x0a;
1784 controller->buffer[2] = 1;
1785 controller->buffer[3] = 1;
1787 for (i = 0; i < 8; i++) {
1788 controller->buffer[4 + i] = 0;
1791 rd_ready_to_send_atapi(dev, channel);
1796 PrintError("(READ TOC) Format %d not supported\n", format);
1800 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1801 rd_raise_interrupt(dev, channel);
1805 case 0x28: // read (10)
1806 case 0xa8: // read (12)
1809 uint32_t transfer_length;
1810 if (atapi_command == 0x28) {
1811 transfer_length = rd_read_16bit(controller->buffer + 7);
1813 transfer_length = rd_read_32bit(controller->buffer + 6);
1816 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1818 if (!(drive->cdrom.ready)) {
1819 PrintError("CDROM Error: Not Ready (ATA%d/%d)\n",
1820 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1821 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1822 rd_raise_interrupt(dev, channel);
1826 if (transfer_length == 0) {
1827 rd_atapi_cmd_nop(dev, channel);
1828 rd_raise_interrupt(dev, channel);
1829 PrintError("READ(%d) with transfer length 0, ok\n",
1830 (atapi_command == 0x28) ? 10 : 12);
1834 if (lba + transfer_length > drive->cdrom.capacity) {
1835 PrintError("CDROM Error: Capacity exceeded [capacity=%d] (ATA%d/%d)\n",
1836 drive->cdrom.capacity,
1837 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1838 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1839 rd_raise_interrupt(dev, channel);
1843 PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n",
1844 (atapi_command == 0x28) ? 10 : 12,
1845 lba, transfer_length);
1848 if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1849 transfer_length * 2048, true) == -1) {
1850 PrintError("CDROM Error: Atapi command send error\n");
1854 drive->cdrom.remaining_blocks = transfer_length;
1855 drive->cdrom.next_lba = lba;
1856 rd_ready_to_send_atapi(dev, channel);
1861 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1863 if (!(drive->cdrom.ready)) {
1864 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1865 rd_raise_interrupt(dev, channel);
1869 if (lba > drive->cdrom.capacity) {
1870 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1871 rd_raise_interrupt(dev, channel);
1875 PrintDebug("\t\tcdrom: SEEK (ignored)\n");
1877 rd_atapi_cmd_nop(dev, channel);
1878 rd_raise_interrupt(dev, channel);
1882 case 0x1e: // prevent/allow medium removal
1885 if (drive->cdrom.ready) {
1886 drive->cdrom.locked = controller->buffer[4] & 1;
1887 rd_atapi_cmd_nop(dev, channel);
1889 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1892 rd_raise_interrupt(dev, channel);
1896 case 0x42: // read sub-channel
1898 //bool msf = get_packet_field(channel,1, 1, 1);
1899 bool sub_q = get_packet_field(channel,2, 6, 1);
1900 //uint8_t data_format = get_packet_byte(channel,3);
1901 //uint8_t track_number = get_packet_byte(channel,6);
1902 uint16_t alloc_length = get_packet_word(channel,7);
1907 UNUSED(data_format);
1908 UNUSED(track_number);
1910 if (!(drive->cdrom.ready)) {
1911 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1912 rd_raise_interrupt(dev, channel);
1914 controller->buffer[0] = 0;
1915 controller->buffer[1] = 0; // audio not supported
1916 controller->buffer[2] = 0;
1917 controller->buffer[3] = 0;
1919 int ret_len = 4; // header size
1921 if (sub_q) { // !sub_q == header only
1922 PrintError("Read sub-channel with SubQ not implemented\n");
1923 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1924 ASC_INV_FIELD_IN_CMD_PACKET);
1925 rd_raise_interrupt(dev, channel);
1928 if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1931 rd_ready_to_send_atapi(dev, channel);
1935 case 0x51: // read disc info
1937 // no-op to keep the Linux CD-ROM driver happy
1938 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1939 rd_raise_interrupt(dev, channel);
1942 case 0x55: // mode select
1943 case 0xa6: // load/unload cd
1944 case 0x4b: // pause/resume
1945 case 0x45: // play audio
1946 case 0x47: // play audio msf
1947 case 0xbc: // play cd
1948 case 0xb9: // read cd msf
1949 case 0x44: // read header
1951 case 0xbb: // set cd speed
1952 case 0x4e: // stop play/scan
1955 PrintError("ATAPI command 0x%x not implemented yet\n",
1957 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1958 rd_raise_interrupt(dev, channel);
1961 PrintError("Unknown ATAPI command 0x%x (%d)\n",
1962 atapi_command, atapi_command);
1963 // We'd better signal the error if the user chose to continue
1964 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1965 rd_raise_interrupt(dev, channel);
1975 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
1977 struct drive_t * drive = &(channel->drives[channel->drive_select]);
1978 struct controller_t * controller = &(drive->controller);
1980 // controller->byte_count is a union of controller->cylinder_no;
1981 // lazy is used to force a data read in the buffer at the next read.
1983 PrintDebug("[rd_init_send_atapi_cmd]\n");
1985 if (controller->byte_count == 0xffff) {
1986 controller->byte_count = 0xfffe;
1989 if ((controller->byte_count & 1) &&
1990 !(alloc_length <= controller->byte_count)) {
1992 PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n",
1993 controller->byte_count,
1995 controller->byte_count - 1);
1997 controller->byte_count -= 1;
2000 if (controller->byte_count == 0) {
2001 PrintError("\t\tATAPI command with zero byte count\n");
2005 if (alloc_length < 0) {
2006 PrintError("\t\tAllocation length < 0\n");
2010 if (alloc_length == 0) {
2011 alloc_length = controller->byte_count;
2014 controller->interrupt_reason.i_o = 1;
2015 controller->interrupt_reason.c_d = 0;
2016 controller->status.busy = 0;
2017 controller->status.drq = 1;
2018 controller->status.err = 0;
2020 // no bytes transfered yet
2022 controller->buffer_index = 2048;
2024 controller->buffer_index = 0;
2027 controller->drq_index = 0;
2029 if (controller->byte_count > req_length) {
2030 controller->byte_count = req_length;
2033 if (controller->byte_count > alloc_length) {
2034 controller->byte_count = alloc_length;
2037 drive->atapi.command = command;
2038 drive->atapi.drq_bytes = controller->byte_count;
2039 drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2042 // // bias drq_bytes and total_bytes_remaining
2043 // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2044 // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2052 void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2053 PrintDebug("[rd_ready_to_send_atapi]\n");
2055 rd_raise_interrupt(dev, channel);
2062 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2064 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2065 struct controller_t * controller = &(drive->controller);
2067 #ifdef DEBUG_RAMDISK
2069 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2070 PrintDebug("[rd_atapi_cmd_error]\n");
2071 PrintDebug("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n",
2072 get_channel_no(ramdisk, channel), sense_key, asc);
2076 controller->error_register = sense_key << 4;
2077 controller->interrupt_reason.i_o = 1;
2078 controller->interrupt_reason.c_d = 1;
2079 controller->interrupt_reason.rel = 0;
2080 controller->status.busy = 0;
2081 controller->status.drive_ready = 1;
2082 controller->status.write_fault = 0;
2083 controller->status.drq = 0;
2084 controller->status.err = 1;
2086 drive->sense.sense_key = sense_key;
2087 drive->sense.asc = asc;
2088 drive->sense.ascq = 0;
2093 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2095 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2096 struct controller_t * controller = &(drive->controller);
2098 PrintDebug("[rd_atapi_cmd_nop]\n");
2099 controller->interrupt_reason.i_o = 1;
2100 controller->interrupt_reason.c_d = 1;
2101 controller->interrupt_reason.rel = 0;
2102 controller->status.busy = 0;
2103 controller->status.drive_ready = 1;
2104 controller->status.drq = 0;
2105 controller->status.err = 0;
2111 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2113 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2114 struct controller_t * controller = &(drive->controller);
2118 const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2119 const char* firmware = "ALPHA1 ";
2121 drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2123 for (i = 1; i <= 9; i++) {
2124 drive->id_drive[i] = 0;
2127 for (i = 0; i < 10; i++) {
2128 drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2129 (serial_number[(i * 2) + 1]));
2132 for (i = 20; i <= 22; i++) {
2133 drive->id_drive[i] = 0;
2136 for (i = 0; i < strlen(firmware)/2; i++) {
2137 drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2138 (firmware[(i * 2) + 1]));
2140 V3_ASSERT((23 + i) == 27);
2142 for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2143 drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2144 (drive->model_no[(i * 2) + 1]));
2146 V3_ASSERT((27 + i) == 47);
2148 drive->id_drive[47] = 0;
2149 drive->id_drive[48] = 1; // 32 bits access
2151 drive->id_drive[49] = (1 << 9); // LBA supported
2153 drive->id_drive[50] = 0;
2154 drive->id_drive[51] = 0;
2155 drive->id_drive[52] = 0;
2157 drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2159 for (i = 54; i <= 62; i++) {
2160 drive->id_drive[i] = 0;
2163 // copied from CFA540A
2164 drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2165 drive->id_drive[64] = 0x0001; // PIO
2166 drive->id_drive[65] = 0x00b4;
2167 drive->id_drive[66] = 0x00b4;
2168 drive->id_drive[67] = 0x012c;
2169 drive->id_drive[68] = 0x00b4;
2171 drive->id_drive[69] = 0;
2172 drive->id_drive[70] = 0;
2173 drive->id_drive[71] = 30; // faked
2174 drive->id_drive[72] = 30; // faked
2175 drive->id_drive[73] = 0;
2176 drive->id_drive[74] = 0;
2178 drive->id_drive[75] = 0;
2180 for (i = 76; i <= 79; i++) {
2181 drive->id_drive[i] = 0;
2184 drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2185 drive->id_drive[81] = 0;
2186 drive->id_drive[82] = 0;
2187 drive->id_drive[83] = 0;
2188 drive->id_drive[84] = 0;
2189 drive->id_drive[85] = 0;
2190 drive->id_drive[86] = 0;
2191 drive->id_drive[87] = 0;
2192 drive->id_drive[88] = 0;
2194 for (i = 89; i <= 126; i++) {
2195 drive->id_drive[i] = 0;
2198 drive->id_drive[127] = 0;
2199 drive->id_drive[128] = 0;
2201 for (i = 129; i <= 159; i++) {
2202 drive->id_drive[i] = 0;
2205 for (i = 160; i <= 255; i++) {
2206 drive->id_drive[i] = 0;
2209 // now convert the id_drive array (native 256 word format) to
2210 // the controller buffer (512 bytes)
2212 for (i = 0; i <= 255; i++) {
2213 temp16 = drive->id_drive[i];
2214 controller->buffer[i * 2] = temp16 & 0x00ff;
2215 controller->buffer[i * 2 + 1] = temp16 >> 8;
2228 void rd_init_mode_sense_single(struct vm_device * dev,
2229 struct channel_t * channel, const void* src, int size)
2231 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2232 struct controller_t * controller = &(drive->controller);
2234 PrintDebug("[rd_init_mode_sense_single]\n");
2237 controller->buffer[0] = (size + 6) >> 8;
2238 controller->buffer[1] = (size + 6) & 0xff;
2239 controller->buffer[2] = 0x70; // no media present
2240 controller->buffer[3] = 0; // reserved
2241 controller->buffer[4] = 0; // reserved
2242 controller->buffer[5] = 0; // reserved
2243 controller->buffer[6] = 0; // reserved
2244 controller->buffer[7] = 0; // reserved
2247 memcpy(controller->buffer + 8, src, size);
2252 static void rd_command_aborted(struct vm_device * dev,
2253 struct channel_t * channel, unsigned value) {
2254 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2255 struct controller_t * controller = &(drive->controller);
2257 PrintDebug("[rd_command_aborted]\n");
2258 PrintDebug("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2260 controller->current_command = 0;
2261 controller->status.busy = 0;
2262 controller->status.drive_ready = 1;
2263 controller->status.err = 1;
2264 controller->error_register = 0x04; // command ABORTED
2265 controller->status.drq = 0;
2266 controller->status.seek_complete = 0;
2267 controller->status.corrected_data = 0;
2268 controller->buffer_index = 0;
2270 rd_raise_interrupt(dev, channel);
2274 static int ramdisk_init_device(struct vm_device *dev) {
2275 struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2277 PrintDebug("Initializing Ramdisk\n");
2280 rd_init_hardware(ramdisk);
2283 dev_hook_io(dev, PRI_CTRL_PORT,
2284 &read_status_port, &write_ctrl_port);
2286 dev_hook_io(dev, PRI_DATA_PORT,
2287 &read_data_port, &write_data_port);
2288 dev_hook_io(dev, PRI_FEATURES_PORT,
2289 &read_general_port, &write_general_port);
2290 dev_hook_io(dev, PRI_SECT_CNT_PORT,
2291 &read_general_port, &write_general_port);
2292 dev_hook_io(dev, PRI_SECT_ADDR1_PORT,
2293 &read_general_port, &write_general_port);
2294 dev_hook_io(dev, PRI_SECT_ADDR2_PORT,
2295 &read_general_port, &write_general_port);
2296 dev_hook_io(dev, PRI_SECT_ADDR3_PORT,
2297 &read_general_port, &write_general_port);
2298 dev_hook_io(dev, PRI_DRV_SEL_PORT,
2299 &read_general_port, &write_general_port);
2300 dev_hook_io(dev, PRI_CMD_PORT,
2301 &read_status_port, &write_cmd_port);
2304 dev_hook_io(dev, SEC_CTRL_PORT,
2305 &read_status_port, &write_ctrl_port);
2307 dev_hook_io(dev, SEC_DATA_PORT,
2308 &read_data_port, &write_data_port);
2309 dev_hook_io(dev, SEC_FEATURES_PORT,
2310 &read_general_port, &write_general_port);
2311 dev_hook_io(dev, SEC_SECT_CNT_PORT,
2312 &read_general_port, &write_general_port);
2313 dev_hook_io(dev, SEC_SECT_ADDR1_PORT,
2314 &read_general_port, &write_general_port);
2315 dev_hook_io(dev, SEC_SECT_ADDR2_PORT,
2316 &read_general_port, &write_general_port);
2317 dev_hook_io(dev, SEC_SECT_ADDR3_PORT,
2318 &read_general_port, &write_general_port);
2319 dev_hook_io(dev, SEC_DRV_SEL_PORT,
2320 &read_general_port, &write_general_port);
2321 dev_hook_io(dev, SEC_CMD_PORT,
2322 &read_status_port, &write_cmd_port);
2326 dev_hook_io(dev, SEC_ADDR_REG_PORT,
2327 &read_general_port, &write_general_port);
2329 dev_hook_io(dev, PRI_ADDR_REG_PORT,
2330 &read_general_port, &write_general_port);
2339 static int ramdisk_deinit_device(struct vm_device *dev) {
2340 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2341 rd_close_harddrive(ramdisk);
2345 static struct vm_device_ops dev_ops = {
2346 .init = ramdisk_init_device,
2347 .deinit = ramdisk_deinit_device,
2356 struct vm_device *create_ramdisk()
2359 struct ramdisk_t *ramdisk;
2360 ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));
2361 V3_ASSERT(ramdisk != NULL);
2363 PrintDebug("[create_ramdisk]\n");
2365 struct vm_device *device = create_device("RAMDISK", &dev_ops, ramdisk);
2373 #ifdef DEBUG_RAMDISK
2375 static void rd_print_state(struct ramdisk_t * ramdisk) {
2378 struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2381 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2382 memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2385 PrintDebug("sizeof(*channels) = %d\n", sizeof(*channels));
2386 PrintDebug("sizeof(channles->drives[0].controller) = %d\n", sizeof((channels->drives[0].controller)));
2387 PrintDebug("sizeof(channles->drives[0].cdrom) = %d\n", sizeof((channels->drives[0].cdrom)));
2388 PrintDebug("sizeof(channles->drives[0].sense) = %d\n", sizeof((channels->drives[0].sense)));
2389 PrintDebug("sizeof(channles->drives[0].atapi) = %d\n", sizeof((channels->drives[0].atapi)));
2392 PrintDebug("sizeof(channles->drives[0].controller.status) = %d\n",
2393 sizeof((channels->drives[0].controller.status)));
2394 PrintDebug("sizeof(channles->drives[0].controller.sector_count) = %d\n",
2395 sizeof((channels->drives[0].controller.sector_count)));
2396 PrintDebug("sizeof(channles->drives[0].controller.interrupt_reason) = %d\n",
2397 sizeof((channels->drives[0].controller.interrupt_reason)));
2399 PrintDebug("sizeof(channles->drives[0].controller.cylinder_no) = %d\n",
2400 sizeof((channels->drives[0].controller.cylinder_no)));
2401 PrintDebug("sizeof(channles->drives[0].controller.byte_count) = %d\n",
2402 sizeof((channels->drives[0].controller.byte_count)));
2405 PrintDebug("sizeof(channles->drives[0].controller.control) = %d\n",
2406 sizeof((channels->drives[0].controller.control)));
2409 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2411 for (device = 0; device < 2; device++){
2413 // Initialize controller state, even if device is not present
2414 PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2416 channels[channel].drives[device].controller.status.busy);
2417 PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n",
2419 channels[channel].drives[device].controller.status.drive_ready);
2420 PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n",
2422 channels[channel].drives[device].controller.status.write_fault);
2423 PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n",
2425 channels[channel].drives[device].controller.status.seek_complete);
2426 PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n",
2428 channels[channel].drives[device].controller.status.drq);
2429 PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n",
2431 channels[channel].drives[device].controller.status.corrected_data);
2432 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n",
2434 channels[channel].drives[device].controller.status.index_pulse);
2435 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n",
2437 channels[channel].drives[device].controller.status.index_pulse_count);
2438 PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n",
2440 channels[channel].drives[device].controller.status.err);
2443 PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n",
2445 channels[channel].drives[device].controller.error_register);
2446 PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n",
2448 channels[channel].drives[device].controller.head_no);
2449 PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n",
2451 channels[channel].drives[device].controller.sector_count);
2452 PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n",
2454 channels[channel].drives[device].controller.sector_no);
2455 PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n",
2457 channels[channel].drives[device].controller.cylinder_no);
2458 PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n",
2460 channels[channel].drives[device].controller.current_command);
2461 PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n",
2463 channels[channel].drives[device].controller.buffer_index);
2466 PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n",
2468 channels[channel].drives[device].controller.control.reset);
2469 PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n",
2471 channels[channel].drives[device].controller.control.disable_irq);
2474 PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n",
2476 channels[channel].drives[device].controller.reset_in_progress);
2477 PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n",
2479 channels[channel].drives[device].controller.sectors_per_block);
2480 PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n",
2482 channels[channel].drives[device].controller.lba_mode);
2483 PrintDebug("channels[%d].drives[%d].controller.features = %d\n",
2485 channels[channel].drives[device].controller.features);
2488 PrintDebug("channels[%d].drives[%d].model_no = %s\n",
2490 channels[channel].drives[device].model_no);
2491 PrintDebug("channels[%d].drives[%d].device_type = %d\n",
2493 channels[channel].drives[device].device_type);
2494 PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n",
2496 channels[channel].drives[device].cdrom.locked);
2497 PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n",
2499 channels[channel].drives[device].sense.sense_key);
2500 PrintDebug("channels[%d].drives[%d].sense.asc = %d\n",
2502 channels[channel].drives[device].sense.asc);
2503 PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n",
2505 channels[channel].drives[device].sense.ascq);
2509 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n",
2511 channels[channel].drives[device].controller.interrupt_reason.c_d);
2513 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n",
2515 channels[channel].drives[device].controller.interrupt_reason.i_o);
2517 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n",
2519 channels[channel].drives[device].controller.interrupt_reason.rel);
2521 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n",
2523 channels[channel].drives[device].controller.interrupt_reason.tag);
2525 PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n",
2527 channels[channel].drives[device].cdrom.ready);
2536 static void trace_info(ushort_t port, void *src, uint_t length) {
2541 if (length == 1 && *((uchar_t*) src) == ATA_DETECT)
2542 PrintDebug("ata_detect()\n");
2546 if (length == 1 && *((uchar_t*) src) == ATA_RESET)
2547 PrintDebug("ata_reset()\n");
2551 if (length == 1 && *((uchar_t*) src) == ATA_CMD_DATA_IN)
2552 PrintDebug("ata_cmd_data_in()\n");
2556 if (length == 1 && *((uchar_t*) src) == ATA_CMD_DATA_OUT)
2557 PrintDebug("ata_cmd_data_out()\n");
2561 if (length == 1 && *((uchar_t*) src) == ATA_CMD_PACKET)
2562 PrintDebug("ata_cmd_packet()\n");
2566 if (length == 1 && *((uchar_t*) src) == ATAPI_GET_SENSE)
2567 PrintDebug("atapi_get_sense()\n");
2571 if (length == 1 && *((uchar_t*) src) == ATAPI_IS_READY)
2572 PrintDebug("atapi_is_ready()\n");
2576 if (length == 1 && *((uchar_t*) src) == ATAPI_IS_CDROM)
2577 PrintDebug("atapi_is_cdrom()\n");
2582 if (length == 1 && *((uchar_t*) src) == CDEMU_INIT)
2583 PrintDebug("cdemu_init()\n");
2587 if (length == 1 && *((uchar_t*) src) == CDEMU_ISACTIVE)
2588 PrintDebug("cdemu_isactive()\n");
2592 if (length == 1 && *((uchar_t*) src) == CDEMU_EMULATED_DRIVE)
2593 PrintDebug("cdemu_emulated_drive()\n");
2597 if (length == 1 && *((uchar_t*) src) == CDROM_BOOT)
2598 PrintDebug("cdrom_boot()\n");
2602 if (length == 1 && *((uchar_t*) src) == HARD_DRIVE_POST)
2603 PrintDebug("ata_hard_drive_post()\n");
2608 PrintDebug("ata_device_no(%d)\n", *((uchar_t*) src));
2613 PrintDebug("ata_device_type(%d)\n", *((uchar_t*) src));
2617 if (length == 1 && *((uchar_t*) src) == INT13_HARDDISK)
2618 PrintDebug("int13_harddrive()\n");
2622 if (length == 1 && *((uchar_t*) src) == INT13_CDROM)
2623 PrintDebug("int13_cdrom()\n");
2627 if (length == 1 && *((uchar_t*) src) == INT13_CDEMU)
2628 PrintDebug("int13_cdemu()\n");
2632 if (length == 1 && *((uchar_t*) src) == INT13_ELTORITO)
2633 PrintDebug("int13_eltorito()\n");
2637 if (length == 1 && *((uchar_t*) src) == INT13_DISKETTE_FUNCTION)
2638 PrintDebug("int13_diskette_function()\n");
2649 static int check_bit_fields(struct controller_t * controller) {
2651 controller->sector_count = 0;
2652 controller->interrupt_reason.c_d = 1;
2653 if (controller->sector_count != 0x01) {
2654 return INTR_REASON_BIT_ERR;
2657 controller->sector_count = 0;
2658 controller->interrupt_reason.i_o = 1;
2659 if (controller->sector_count != 0x02) {
2660 return INTR_REASON_BIT_ERR;
2663 controller->sector_count = 0;
2664 controller->interrupt_reason.rel = 1;
2665 if (controller->sector_count != 0x04) {
2666 return INTR_REASON_BIT_ERR;
2669 controller->sector_count = 0;
2670 controller->interrupt_reason.tag = 3;
2671 if (controller->sector_count != 0x18) {
2672 return INTR_REASON_BIT_ERR;