2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <devices/ide.h>
22 #include "ide-types.h"
23 #include "atapi-types.h"
25 #define PRI_DEFAULT_IRQ 14
26 #define SEC_DEFAULT_IRQ 15
29 #define PRI_DATA_PORT 0x1f0
30 #define PRI_FEATURES_PORT 0x1f1
31 #define PRI_SECT_CNT_PORT 0x1f2
32 #define PRI_SECT_NUM_PORT 0x1f3
33 #define PRI_CYL_LOW_PORT 0x1f4
34 #define PRI_CYL_HIGH_PORT 0x1f5
35 #define PRI_DRV_SEL_PORT 0x1f6
36 #define PRI_CMD_PORT 0x1f7
37 #define PRI_CTRL_PORT 0x3f6
38 #define PRI_ADDR_REG_PORT 0x3f7
40 #define SEC_DATA_PORT 0x170
41 #define SEC_FEATURES_PORT 0x171
42 #define SEC_SECT_CNT_PORT 0x172
43 #define SEC_SECT_NUM_PORT 0x173
44 #define SEC_CYL_LOW_PORT 0x174
45 #define SEC_CYL_HIGH_PORT 0x175
46 #define SEC_DRV_SEL_PORT 0x176
47 #define SEC_CMD_PORT 0x177
48 #define SEC_CTRL_PORT 0x376
49 #define SEC_ADDR_REG_PORT 0x377
52 #define DATA_BUFFER_SIZE 2048
54 static const char * ide_dev_type_strs[] = {"HARDDISK", "CDROM", "NONE"};
57 static inline const char * device_type_to_str(v3_ide_dev_type_t type) {
62 return ide_dev_type_strs[type];
68 struct atapi_sense_data sense;
79 v3_ide_dev_type_t drive_type;
82 struct v3_ide_cd_ops * cd_ops;
83 struct v3_ide_hd_ops * hd_ops;
88 struct ide_cd_state cd_state;
89 struct ide_hd_state hd_state;
94 // Where we are in the data transfer
95 uint_t transfer_index;
97 // the length of a transfer
98 // calculated for easy access
99 uint_t transfer_length;
101 // We have a local data buffer that we use for IO port accesses
102 uint8_t data_buf[DATA_BUFFER_SIZE];
106 uint8_t sector_count; // 0x1f2,0x172
107 uint8_t sector_num; // 0x1f3,0x173
111 uint8_t cylinder_low; // 0x1f4,0x174
112 uint8_t cylinder_high; // 0x1f5,0x175
113 } __attribute__((packed));
114 } __attribute__((packed));
122 struct ide_drive drives[2];
125 struct ide_error_reg error_reg; // [read] 0x1f1,0x171
127 struct ide_features_reg features;
129 struct ide_drive_head_reg drive_head; // 0x1f6,0x176
131 struct ide_status_reg status; // [read] 0x1f7,0x177
132 uint8_t cmd_reg; // [write] 0x1f7,0x177
134 int irq; // this is temporary until we add PCI support
137 struct ide_ctrl_reg ctrl_reg; // [write] 0x3f6,0x376
142 struct ide_internal {
143 struct ide_channel channels[2];
148 static inline uint32_t be_to_le_16(const uint16_t val) {
149 uint8_t * buf = (uint8_t *)&val;
150 return (buf[0] << 8) | (buf[1]) ;
154 static inline uint32_t be_to_le_32(const uint32_t val) {
155 uint8_t * buf = (uint8_t *)&val;
156 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
160 static inline int get_channel_index(ushort_t port) {
161 if (((port & 0xfff8) == 0x1f0) ||
162 ((port & 0xfffe) == 0x3f6)) {
164 } else if (((port & 0xfff8) == 0x170) ||
165 ((port & 0xfffe) == 0x376)) {
172 static inline struct ide_channel * get_selected_channel(struct ide_internal * ide, ushort_t port) {
173 int channel_idx = get_channel_index(port);
174 return &(ide->channels[channel_idx]);
177 static inline struct ide_drive * get_selected_drive(struct ide_channel * channel) {
178 return &(channel->drives[channel->drive_head.drive_sel]);
182 static inline int is_lba_enabled(struct ide_channel * channel) {
183 return channel->drive_head.lba_mode;
187 static void ide_raise_irq(struct vm_device * dev, struct ide_channel * channel) {
188 if (channel->ctrl_reg.irq_disable == 0) {
189 v3_raise_irq(dev->vm, channel->irq);
194 static void drive_reset(struct ide_drive * drive) {
195 drive->sector_count = 0x01;
196 drive->sector_num = 0x01;
198 if (drive->drive_type == IDE_CDROM) {
199 drive->cylinder = 0xeb14;
201 drive->cylinder = 0x0000;
205 memset(drive->data_buf, 0, sizeof(drive->data_buf));
206 drive->transfer_length = 0;
207 drive->transfer_index = 0;
209 // Send the reset signal to the connected device callbacks
210 // channel->drives[0].reset();
211 // channel->drives[1].reset();
214 static void channel_reset(struct ide_channel * channel) {
216 // set busy and seek complete flags
217 channel->status.val = 0x90;
220 channel->error_reg.val = 0x01;
223 channel->cmd_reg = 0x00;
225 channel->ctrl_reg.irq_disable = 0;
228 static void channel_reset_complete(struct ide_channel * channel) {
229 channel->status.busy = 0;
230 channel->status.ready = 1;
232 channel->drive_head.head_num = 0;
234 drive_reset(&(channel->drives[0]));
235 drive_reset(&(channel->drives[1]));
239 static void ide_abort_command(struct vm_device * dev, struct ide_channel * channel) {
240 channel->status.val = 0x41; // Error + ready
241 channel->error_reg.val = 0x04; // No idea...
243 ide_raise_irq(dev, channel);
249 // Include the ATAPI interface handlers
255 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
256 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
257 struct ide_channel * channel = get_selected_channel(ide, port);
258 struct ide_drive * drive = get_selected_drive(channel);
261 PrintError("Invalid Write Length on IDE command Port %x\n", port);
265 PrintDebug("IDE: Writing Command Port %x (val=%x)\n", port, *(uint8_t *)src);
267 channel->cmd_reg = *(uint8_t *)src;
269 switch (channel->cmd_reg) {
271 case 0xa0: // ATAPI Command Packet
272 if (drive->drive_type != IDE_CDROM) {
273 ide_abort_command(dev, channel);
276 drive->sector_count = 1;
278 channel->status.busy = 0;
279 channel->status.write_fault = 0;
280 channel->status.data_req = 1;
281 channel->status.error = 0;
283 // reset the data buffer...
284 drive->transfer_length = ATAPI_PACKET_SIZE;
285 drive->transfer_index = 0;
288 case 0xa1: // ATAPI Identify Device Packet
289 atapi_identify_device(drive);
291 channel->error_reg.val = 0;
292 channel->status.val = 0x58; // ready, data_req, seek_complete
294 ide_raise_irq(dev, channel);
296 case 0xec: // Identify Device
297 if (drive->drive_type != IDE_DISK) {
300 // JRL: Should we abort here?
301 ide_abort_command(dev, channel);
303 PrintError("IDE Disks currently not implemented\n");
308 PrintError("Unimplemented IDE command (%x)\n", channel->cmd_reg);
316 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
317 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
318 struct ide_channel * channel = get_selected_channel(ide, port);
319 struct ide_drive * drive = get_selected_drive(channel);
321 PrintDebug("IDE: Writing Data Port %x (val=%x, len=%d)\n", port, *(uint32_t *)src, length);
323 memcpy(drive->data_buf + drive->transfer_index, src, length);
324 drive->transfer_index += length;
326 // Transfer is complete, dispatch the command
327 if (drive->transfer_index >= drive->transfer_length) {
328 switch (channel->cmd_reg) {
329 case 0x30: // Write Sectors
330 PrintError("Writing Data not yet implemented\n");
333 case 0xa0: // ATAPI packet command
334 if (atapi_handle_packet(dev, channel) == -1) {
335 PrintError("Error handling ATAPI packet\n");
340 PrintError("Unhandld IDE Command %x\n", channel->cmd_reg);
349 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
350 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
351 struct ide_channel * channel = get_selected_channel(ide, port);
352 struct ide_drive * drive = get_selected_drive(channel);
353 int data_offset = drive->transfer_index % DATA_BUFFER_SIZE;
355 PrintDebug("IDE: Reading Data Port %x\n", port);
357 if (data_offset == DATA_BUFFER_SIZE) {
358 // check for more data to transfer, if there isn't any then that's a problem...
360 * if (ide_update_buffer(drive) == -1) {
368 // check for overruns...
369 // We will return the data padded with 0's
370 if (drive->transfer_index + length > drive->transfer_length) {
371 length = drive->transfer_length - drive->transfer_index;
372 memset(dst, 0, length);
375 memcpy(dst, drive->data_buf + data_offset, length);
377 drive->transfer_index += length;
380 if (drive->transfer_index >= drive->transfer_length) {
381 channel->status.data_req = 0;
387 static int write_port_std(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
388 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
389 struct ide_channel * channel = get_selected_channel(ide, port);
390 struct ide_drive * drive = get_selected_drive(channel);
393 PrintError("Invalid Write length on IDE port %x\n", port);
397 PrintDebug("IDE: Writing Standard Port %x (val=%x)\n", port, *(uint8_t *)src);
401 // reset and interrupt enable
403 case SEC_CTRL_PORT: {
404 struct ide_ctrl_reg * tmp_ctrl = (struct ide_ctrl_reg *)src;
406 // only reset channel on a 0->1 reset bit transition
407 if ((!channel->ctrl_reg.soft_reset) && (tmp_ctrl->soft_reset)) {
408 channel_reset(channel);
409 } else if ((channel->ctrl_reg.soft_reset) && (!tmp_ctrl->soft_reset)) {
410 channel_reset_complete(channel);
413 channel->ctrl_reg.val = tmp_ctrl->val;
416 case PRI_FEATURES_PORT:
417 case SEC_FEATURES_PORT:
418 channel->features.val = *(uint8_t *)src;
421 case PRI_SECT_CNT_PORT:
422 case SEC_SECT_CNT_PORT:
423 drive->sector_count = *(uint8_t *)src;
426 case PRI_SECT_NUM_PORT:
427 case SEC_SECT_NUM_PORT:
428 drive->sector_num = *(uint8_t *)src;
430 case PRI_CYL_LOW_PORT:
431 case SEC_CYL_LOW_PORT:
432 drive->cylinder_low = *(uint8_t *)src;
435 case PRI_CYL_HIGH_PORT:
436 case SEC_CYL_HIGH_PORT:
437 drive->cylinder_high = *(uint8_t *)src;
440 case PRI_DRV_SEL_PORT:
441 case SEC_DRV_SEL_PORT: {
442 channel->drive_head.val = *(uint8_t *)src;
444 // make sure the reserved bits are ok..
445 // JRL TODO: check with new ramdisk to make sure this is right...
446 channel->drive_head.val |= 0xa0;
448 drive = get_selected_drive(channel);
450 // Selecting a non-present device is a no-no
451 if (drive->drive_type == IDE_NONE) {
452 PrintDebug("Attempting to select a non-present drive\n");
453 channel->error_reg.abort = 1;
454 channel->status.error = 1;
460 PrintError("IDE: Write to unknown Port %x\n", port);
467 static int read_port_std(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
468 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
469 struct ide_channel * channel = get_selected_channel(ide, port);
470 struct ide_drive * drive = get_selected_drive(channel);
473 PrintError("Invalid Read length on IDE port %x\n", port);
477 PrintDebug("IDE: Reading Standard Port %x\n", port);
480 if ((port == PRI_ADDR_REG_PORT) ||
481 (port == SEC_ADDR_REG_PORT)) {
482 // unused, return 0xff
483 *(uint8_t *)dst = 0xff;
488 // if no drive is present just return 0 + reserved bits
489 if (drive->drive_type == IDE_NONE) {
490 if ((port == PRI_DRV_SEL_PORT) ||
491 (port == SEC_DRV_SEL_PORT)) {
492 *(uint8_t *)dst = 0xa0;
502 // This is really the error register.
503 case PRI_FEATURES_PORT:
504 case SEC_FEATURES_PORT:
505 *(uint8_t *)dst = channel->error_reg.val;
508 case PRI_SECT_CNT_PORT:
509 case SEC_SECT_CNT_PORT:
510 *(uint8_t *)dst = drive->sector_count;
513 case PRI_SECT_NUM_PORT:
514 case SEC_SECT_NUM_PORT:
515 *(uint8_t *)dst = drive->sector_num;
518 case PRI_CYL_LOW_PORT:
519 case SEC_CYL_LOW_PORT:
520 *(uint8_t *)dst = drive->cylinder_low;
524 case PRI_CYL_HIGH_PORT:
525 case SEC_CYL_HIGH_PORT:
526 *(uint8_t *)dst = drive->cylinder_high;
529 case PRI_DRV_SEL_PORT:
530 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
531 *(uint8_t *)dst = channel->drive_head.val;
538 // Something about lowering interrupts here....
539 *(uint8_t *)dst = channel->status.val;
543 PrintError("Invalid Port: %x\n", port);
547 PrintDebug("\tVal=%x\n", *(uint8_t *)dst);
554 static void init_drive(struct ide_drive * drive) {
556 drive->sector_count = 0x01;
557 drive->sector_num = 0x01;
558 drive->cylinder = 0x0000;
560 drive->drive_type = IDE_NONE;
562 memset(drive->model, 0, sizeof(drive->model));
564 drive->transfer_index = 0;
565 drive->transfer_length = 0;
566 memset(drive->data_buf, 0, sizeof(drive->data_buf));
568 drive->private_data = NULL;
569 drive->cd_ops = NULL;
572 static void init_channel(struct ide_channel * channel) {
575 channel->error_reg.val = 0x01;
576 channel->drive_head.val = 0x00;
577 channel->status.val = 0x00;
578 channel->cmd_reg = 0x00;
579 channel->ctrl_reg.val = 0x08;
582 for (i = 0; i < 2; i++) {
583 init_drive(&(channel->drives[i]));
588 static void init_ide_state(struct ide_internal * ide) {
591 for (i = 0; i < 2; i++) {
592 init_channel(&(ide->channels[i]));
594 // JRL: this is a terrible hack...
595 ide->channels[i].irq = PRI_DEFAULT_IRQ + i;
601 static int init_ide(struct vm_device * dev) {
602 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
604 PrintDebug("IDE: Initializing IDE\n");
609 v3_dev_hook_io(dev, PRI_CTRL_PORT,
610 &read_port_std, &write_port_std);
612 v3_dev_hook_io(dev, PRI_DATA_PORT,
613 &read_data_port, &write_data_port);
614 v3_dev_hook_io(dev, PRI_FEATURES_PORT,
615 &read_port_std, &write_port_std);
616 v3_dev_hook_io(dev, PRI_SECT_CNT_PORT,
617 &read_port_std, &write_port_std);
618 v3_dev_hook_io(dev, PRI_SECT_NUM_PORT,
619 &read_port_std, &write_port_std);
620 v3_dev_hook_io(dev, PRI_CYL_LOW_PORT,
621 &read_port_std, &write_port_std);
622 v3_dev_hook_io(dev, PRI_CYL_HIGH_PORT,
623 &read_port_std, &write_port_std);
624 v3_dev_hook_io(dev, PRI_DRV_SEL_PORT,
625 &read_port_std, &write_port_std);
626 v3_dev_hook_io(dev, PRI_CMD_PORT,
627 &read_port_std, &write_cmd_port);
630 v3_dev_hook_io(dev, SEC_CTRL_PORT,
631 &read_port_std, &write_port_std);
633 v3_dev_hook_io(dev, SEC_DATA_PORT,
634 &read_data_port, &write_data_port);
635 v3_dev_hook_io(dev, SEC_FEATURES_PORT,
636 &read_port_std, &write_port_std);
637 v3_dev_hook_io(dev, SEC_SECT_CNT_PORT,
638 &read_port_std, &write_port_std);
639 v3_dev_hook_io(dev, SEC_SECT_NUM_PORT,
640 &read_port_std, &write_port_std);
641 v3_dev_hook_io(dev, SEC_CYL_LOW_PORT,
642 &read_port_std, &write_port_std);
643 v3_dev_hook_io(dev, SEC_CYL_HIGH_PORT,
644 &read_port_std, &write_port_std);
645 v3_dev_hook_io(dev, SEC_DRV_SEL_PORT,
646 &read_port_std, &write_port_std);
647 v3_dev_hook_io(dev, SEC_CMD_PORT,
648 &read_port_std, &write_cmd_port);
652 v3_dev_hook_io(dev, SEC_ADDR_REG_PORT,
653 &read_port_std, &write_port_std);
655 v3_dev_hook_io(dev, PRI_ADDR_REG_PORT,
656 &read_port_std, &write_port_std);
662 static int deinit_ide(struct vm_device * dev) {
663 // unhook io ports....
664 // deregister from PCI?
669 static struct vm_device_ops dev_ops = {
671 .deinit = deinit_ide,
678 struct vm_device * v3_create_ide() {
679 struct ide_internal * ide = (struct ide_internal *)V3_Malloc(sizeof(struct ide_internal));
680 struct vm_device * device = v3_create_device("IDE", &dev_ops, ide);
684 PrintDebug("IDE: Creating IDE bus x 2\n");
693 int v3_ide_register_cdrom(struct vm_device * ide_dev,
697 struct v3_ide_cd_ops * ops,
698 void * private_data) {
700 struct ide_internal * ide = (struct ide_internal *)(ide_dev->private_data);
701 struct ide_channel * channel = NULL;
702 struct ide_drive * drive = NULL;
704 V3_ASSERT((bus_num >= 0) && (bus_num < 2));
705 V3_ASSERT((drive_num >= 0) && (drive_num < 2));
707 channel = &(ide->channels[bus_num]);
708 drive = &(channel->drives[drive_num]);
710 if (drive->drive_type != IDE_NONE) {
711 PrintError("Device slot (bus=%d, drive=%d) already occupied\n", bus_num, drive_num);
715 strncpy(drive->model, dev_name, sizeof(drive->model) - 1);
717 while (strlen((char *)(drive->model)) < 40) {
718 strcat((char*)(drive->model), " ");
722 drive->drive_type = IDE_CDROM;
726 drive->private_data = private_data;
732 int v3_ide_register_harddisk(struct vm_device * ide_dev,
736 struct v3_ide_hd_ops * ops,
737 void * private_data) {
739 struct ide_internal * ide = (struct ide_internal *)(ide_dev->private_data);
740 struct ide_channel * channel = NULL;
741 struct ide_drive * drive = NULL;
743 V3_ASSERT((bus_num >= 0) && (bus_num < 2));
744 V3_ASSERT((drive_num >= 0) && (drive_num < 2));
746 channel = &(ide->channels[bus_num]);
747 drive = &(channel->drives[drive_num]);
749 if (drive->drive_type != IDE_NONE) {
750 PrintError("Device slot (bus=%d, drive=%d) already occupied\n", bus_num, drive_num);
754 strncpy(drive->model, dev_name, sizeof(drive->model) - 1);
756 drive->drive_type = IDE_DISK;
760 drive->private_data = private_data;