Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


broken DMA that should be working
[palacios.git] / palacios / src / devices / ide.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm.h>
21 #include <palacios/vm_guest_mem.h>
22 #include <devices/ide.h>
23 #include <devices/pci.h>
24 #include "ide-types.h"
25 #include "atapi-types.h"
26
27 #define PRI_DEFAULT_IRQ 14
28 #define SEC_DEFAULT_IRQ 15
29
30
31 #define PRI_DATA_PORT         0x1f0
32 #define PRI_FEATURES_PORT     0x1f1
33 #define PRI_SECT_CNT_PORT     0x1f2
34 #define PRI_SECT_NUM_PORT     0x1f3
35 #define PRI_CYL_LOW_PORT      0x1f4
36 #define PRI_CYL_HIGH_PORT     0x1f5
37 #define PRI_DRV_SEL_PORT      0x1f6
38 #define PRI_CMD_PORT          0x1f7
39 #define PRI_CTRL_PORT         0x3f6
40 #define PRI_ADDR_REG_PORT     0x3f7
41
42 #define SEC_DATA_PORT         0x170
43 #define SEC_FEATURES_PORT     0x171
44 #define SEC_SECT_CNT_PORT     0x172
45 #define SEC_SECT_NUM_PORT     0x173
46 #define SEC_CYL_LOW_PORT      0x174
47 #define SEC_CYL_HIGH_PORT     0x175
48 #define SEC_DRV_SEL_PORT      0x176
49 #define SEC_CMD_PORT          0x177
50 #define SEC_CTRL_PORT         0x376
51 #define SEC_ADDR_REG_PORT     0x377
52
53
54 #define PRI_DEFAULT_DMA_PORT 0xc000
55 #define SEC_DEFAULT_DMA_PORT 0xc008
56
57
58 #define DATA_BUFFER_SIZE 2048
59
60 static const char * ide_pri_port_strs[] = {"PRI_DATA", "PRI_FEATURES", "PRI_SECT_CNT", "PRI_SECT_NUM", 
61                                           "PRI_CYL_LOW", "PRI_CYL_HIGH", "PRI_DRV_SEL", "PRI_CMD",
62                                            "PRI_CTRL", "PRI_ADDR_REG"};
63
64
65 static const char * ide_sec_port_strs[] = {"SEC_DATA", "SEC_FEATURES", "SEC_SECT_CNT", "SEC_SECT_NUM", 
66                                           "SEC_CYL_LOW", "SEC_CYL_HIGH", "SEC_DRV_SEL", "SEC_CMD",
67                                            "SEC_CTRL", "SEC_ADDR_REG"};
68
69 static const char * ide_dma_port_strs[] = {"DMA_CMD", NULL, "DMA_STATUS", NULL,
70                                            "DMA_PRD0", "DMA_PRD1", "DMA_PRD2", "DMA_PRD3"};
71
72
73
74 static inline const char * io_port_to_str(uint16_t port) {
75     if ((port >= PRI_DATA_PORT) && (port <= PRI_CMD_PORT)) {
76         return ide_pri_port_strs[port - PRI_DATA_PORT];
77     } else if ((port >= SEC_DATA_PORT) && (port <= SEC_CMD_PORT)) {
78         return ide_sec_port_strs[port - SEC_DATA_PORT];
79     } else if ((port == PRI_CTRL_PORT) || (port == PRI_ADDR_REG_PORT)) {
80         return ide_pri_port_strs[port - PRI_CTRL_PORT + 8];
81     } else if ((port == SEC_CTRL_PORT) || (port == SEC_ADDR_REG_PORT)) {
82         return ide_sec_port_strs[port - SEC_CTRL_PORT + 8];
83     } 
84     return NULL;
85 }
86
87
88 static inline const char * dma_port_to_str(uint16_t port) {
89     return ide_dma_port_strs[port & 0x7];
90 }
91
92
93 static const char * ide_dev_type_strs[] = {"HARDDISK", "CDROM", "NONE"};
94
95
96 static inline const char * device_type_to_str(v3_ide_dev_type_t type) {
97     if (type > 2) {
98         return NULL;
99     }
100
101     return ide_dev_type_strs[type];
102 }
103
104
105
106 struct ide_cd_state {
107     struct atapi_sense_data sense;
108     uint_t current_lba;
109     uint8_t atapi_cmd;
110     struct atapi_error_recovery err_recovery;
111 };
112
113 struct ide_hd_state {
114
115 };
116
117 struct ide_drive {
118     // Command Registers
119
120     v3_ide_dev_type_t drive_type;
121
122     union {
123         struct v3_ide_cd_ops * cd_ops;
124         struct v3_ide_hd_ops * hd_ops;
125     };
126
127
128     union {
129         struct ide_cd_state cd_state;
130         struct ide_hd_state hd_state;
131     };
132
133     char model[41];
134
135     // Where we are in the data transfer
136     uint_t transfer_index;
137
138     // the length of a transfer
139     // calculated for easy access
140     uint_t transfer_length;
141
142
143     // We have a local data buffer that we use for IO port accesses
144     uint8_t data_buf[DATA_BUFFER_SIZE];
145
146
147     void * private_data;
148     
149     union {
150         uint8_t sector_count;             // 0x1f2,0x172
151         struct atapi_irq_flags irq_flags;
152     } __attribute__((packed));
153
154     union {
155         uint8_t sector_num;               // 0x1f3,0x173
156         uint8_t lba0;
157     };
158
159     union {
160         uint16_t cylinder;
161         uint16_t lba12;
162
163
164         struct {
165             uint8_t cylinder_low;       // 0x1f4,0x174
166             uint8_t cylinder_high;      // 0x1f5,0x175
167         } __attribute__((packed));
168
169         struct {
170             uint8_t lba1;
171             uint8_t lba2;
172         } __attribute__((packed));
173
174
175         // The transfer length requested by the CPU 
176         uint16_t req_len;
177     } __attribute__((packed));
178
179 };
180
181
182
183 struct ide_channel {
184     struct ide_drive drives[2];
185
186     // Command Registers
187     struct ide_error_reg error_reg;     // [read] 0x1f1,0x171
188
189     struct ide_features_reg features;
190
191     struct ide_drive_head_reg drive_head; // 0x1f6,0x176
192
193     struct ide_status_reg status;       // [read] 0x1f7,0x177
194     uint8_t cmd_reg;                // [write] 0x1f7,0x177
195
196     int irq; // this is temporary until we add PCI support
197
198     struct pci_device * pci_dev;
199
200     // Control Registers
201     struct ide_ctrl_reg ctrl_reg; // [write] 0x3f6,0x376
202
203     struct ide_dma_cmd_reg dma_cmd;
204     struct ide_dma_status_reg dma_status;
205     uint32_t dma_prd_addr;
206     uint_t dma_tbl_index;
207 };
208
209
210
211 struct ide_internal {
212     struct ide_channel channels[2];
213     struct vm_device * pci;
214     struct pci_device * busmaster_pci;
215 };
216
217
218
219 static inline uint16_t be_to_le_16(const uint16_t val) {
220     uint8_t * buf = (uint8_t *)&val;
221     return (buf[0] << 8) | (buf[1]) ;
222 }
223
224 static inline uint16_t le_to_be_16(const uint16_t val) {
225     return be_to_le_16(val);
226 }
227
228
229 static inline uint32_t be_to_le_32(const uint32_t val) {
230     uint8_t * buf = (uint8_t *)&val;
231     return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
232 }
233
234 static inline uint32_t le_to_be_32(const uint32_t val) {
235     return be_to_le_32(val);
236 }
237
238
239 static inline int get_channel_index(ushort_t port) {
240     if (((port & 0xfff8) == 0x1f0) ||
241         ((port & 0xfffe) == 0x3f6) || 
242         ((port & 0xfff8) == 0xc000)) {
243         return 0;
244     } else if (((port & 0xfff8) == 0x170) ||
245                ((port & 0xfffe) == 0x376) ||
246                ((port & 0xfff8) == 0xc008)) {
247         return 1;
248     }
249
250     return -1;
251 }
252
253 static inline struct ide_channel * get_selected_channel(struct ide_internal * ide, ushort_t port) {
254     int channel_idx = get_channel_index(port);    
255     return &(ide->channels[channel_idx]);
256 }
257
258 static inline struct ide_drive * get_selected_drive(struct ide_channel * channel) {
259     return &(channel->drives[channel->drive_head.drive_sel]);
260 }
261
262
263 static inline int is_lba_enabled(struct ide_channel * channel) {
264     return channel->drive_head.lba_mode;
265 }
266
267
268 static void ide_raise_irq(struct vm_device * dev, struct ide_channel * channel) {
269     if (channel->ctrl_reg.irq_disable == 0) {
270         PrintDebug("Raising IDE Interrupt %d\n", channel->irq);
271         channel->dma_status.int_gen = 1;
272         v3_raise_irq(dev->vm, channel->irq);
273     }
274 }
275
276
277 static void drive_reset(struct ide_drive * drive) {
278     drive->sector_count = 0x01;
279     drive->sector_num = 0x01;
280     
281     if (drive->drive_type == IDE_CDROM) {
282         drive->cylinder = 0xeb14;
283     } else {
284         drive->cylinder = 0x0000;
285     }
286
287
288     memset(drive->data_buf, 0, sizeof(drive->data_buf));
289     drive->transfer_index = 0;
290
291     // Send the reset signal to the connected device callbacks
292     //     channel->drives[0].reset();
293     //    channel->drives[1].reset();
294 }
295
296 static void channel_reset(struct ide_channel * channel) {
297     
298     // set busy and seek complete flags
299     channel->status.val = 0x90;
300
301     // Clear errors
302     channel->error_reg.val = 0x01;
303
304     // clear commands
305     channel->cmd_reg = 0x00;
306
307     channel->ctrl_reg.irq_disable = 0;
308 }
309
310 static void channel_reset_complete(struct ide_channel * channel) {
311     channel->status.busy = 0;
312     channel->status.ready = 1;
313
314     channel->drive_head.head_num = 0;    
315     
316     drive_reset(&(channel->drives[0]));
317     drive_reset(&(channel->drives[1]));
318 }
319
320
321 static void ide_abort_command(struct vm_device * dev, struct ide_channel * channel) {
322     channel->status.val = 0x41; // Error + ready
323     channel->error_reg.val = 0x04; // No idea...
324
325     ide_raise_irq(dev, channel);
326 }
327
328
329 // Include the ATAPI interface handlers
330 #include "atapi.h"
331
332
333
334 static int dma_read(struct vm_device * dev, struct ide_channel * channel) {
335     struct ide_drive * drive = get_selected_drive(channel);
336     struct ide_dma_prd prd_entry;
337     uint32_t prd_entry_addr = channel->dma_prd_addr + (sizeof(struct ide_dma_prd) * channel->dma_tbl_index);
338     int ret;
339
340
341     PrintDebug("PRD table address = %x\n", channel->dma_prd_addr);
342
343     ret = read_guest_pa_memory(dev->vm, prd_entry_addr, sizeof(struct ide_dma_prd), (void *)&prd_entry);
344
345     if (ret != sizeof(struct ide_dma_prd)) {
346         PrintError("Could not read PRD\n");
347         return -1;
348     }
349
350     PrintDebug("PRD Addr: %x, PDR Len: %d, EOT: %d\n", prd_entry.base_addr, prd_entry.size, prd_entry.end_of_table);
351
352     ret = write_guest_pa_memory(dev->vm, prd_entry.base_addr, prd_entry.size, drive->data_buf); 
353
354     if (ret != prd_entry.size) {
355         PrintError("Failed to copy data into guest memory... (ret=%d)\n", ret);
356         return -1;
357     }
358
359     channel->status.busy = 0;
360     channel->status.ready = 1;
361     channel->status.data_req = 0;
362     channel->status.error = 0;
363     channel->status.seek_complete = 1;
364
365     /*
366       drive->irq_flags.io_dir = 1;
367       drive->irq_flags.c_d = 1;
368       drive->irq_flags.rel = 0;
369     */
370
371
372     // set DMA status
373     channel->dma_status.active = 0;
374     channel->dma_status.err = 1;
375     channel->dma_status.int_gen = 1;
376
377     ide_raise_irq(dev, channel);
378
379     return 0;
380 }
381
382
383 static int dma_write(struct vm_device * dev, struct ide_channel * channel) {
384     // unsupported
385     PrintError("DMA writes currently not supported\n");
386     return -1;
387 }
388
389
390 /* 
391  * This is an ugly ugly ugly way to differentiate between the first and second DMA channels 
392  */
393
394 static int write_dma_port(ushort_t port_offset, void * src, uint_t length, struct vm_device * dev, struct ide_channel * channel);
395 static int read_dma_port(ushort_t port_offset, void * dst, uint_t length, struct vm_device * dev, struct ide_channel * channel);
396
397
398 static int write_pri_dma_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
399     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
400     PrintDebug("IDE: Writing PRI DMA Port %x (%s) (val=%x)\n", port, dma_port_to_str(port & 0x7), *(uint32_t *)src);
401     return write_dma_port(port & 0x7, src, length, dev, &(ide->channels[0]));
402 }
403
404 static int write_sec_dma_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
405     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
406     PrintDebug("IDE: Writing SEC DMA Port %x (%s) (val=%x)\n", port, dma_port_to_str(port & 0x7), *(uint32_t *)src);
407     return write_dma_port(port & 0x7, src, length, dev, &(ide->channels[1]));
408 }
409
410
411 static int read_pri_dma_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
412     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
413     PrintDebug("IDE: Reading PRI DMA Port %x (%s)\n", port, dma_port_to_str(port & 0x7));
414     return read_dma_port(port & 0x7, dst, length, dev, &(ide->channels[0]));
415 }
416
417 static int read_sec_dma_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
418     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
419     PrintDebug("IDE: Reading SEC DMA Port %x (%s)\n", port, dma_port_to_str(port & 0x7));
420     return read_dma_port(port & 0x7, dst, length, dev, &(ide->channels[1]));
421 }
422
423
424 #define DMA_CMD_PORT      0x00
425 #define DMA_STATUS_PORT   0x02
426 #define DMA_PRD_PORT0     0x04
427 #define DMA_PRD_PORT1     0x05
428 #define DMA_PRD_PORT2     0x06
429 #define DMA_PRD_PORT3     0x07
430
431
432 static int write_dma_port(ushort_t port_offset, void * src, uint_t length, 
433                           struct vm_device * dev, struct ide_channel * channel) {
434
435     switch (port_offset) {
436         case DMA_CMD_PORT:
437             channel->dma_cmd.val = *(uint8_t *)src;
438
439             if (channel->dma_cmd.start == 0) {
440                 channel->dma_tbl_index = 0;
441             } else {
442                 channel->dma_status.active = 1;
443
444                 if (channel->dma_cmd.read == 1) {
445                     // DMA Read
446                     if (dma_read(dev, channel) == -1) {
447                         PrintError("Failed DMA Read\n");
448                         return -1;
449                     }
450                 } else {
451                     // DMA write
452                     if (dma_write(dev, channel) == -1) {
453                         PrintError("Failed DMA Write\n");
454                         return -1;
455                     }
456                 }
457             }
458
459             break;
460             
461         case DMA_STATUS_PORT:
462             if (length != 1) {
463                 PrintError("Invalid read length for DMA status port\n");
464                 return -1;
465             }
466
467             channel->dma_status.val = *(uint8_t *)src;
468             break;
469             
470         case DMA_PRD_PORT0:
471         case DMA_PRD_PORT1:
472         case DMA_PRD_PORT2:
473         case DMA_PRD_PORT3: {
474             uint_t addr_index = port_offset & 0x3;
475             uint8_t * addr_buf = (uint8_t *)&(channel->dma_prd_addr);
476             int i = 0;
477
478             if (addr_index + length > 4) {
479                 PrintError("DMA Port space overrun port=%x len=%d\n", port_offset, length);
480                 return -1;
481             }
482
483             for (i = 0; i < length; i++) {
484                 addr_buf[addr_index + i] = *((uint8_t *)src + i);
485             }
486
487             PrintDebug("Writing PRD Port %x (val=%x)\n", port_offset, channel->dma_prd_addr);
488
489             break;
490         }
491         default:
492             PrintError("IDE: Invalid DMA Port (%s)\n", dma_port_to_str(port_offset));
493             return -1;
494     }
495
496     return length;
497 }
498
499
500 static int read_dma_port(ushort_t port_offset, void * dst, uint_t length, 
501                          struct vm_device * dev, struct ide_channel * channel) {
502
503     switch (port_offset) {
504         case DMA_CMD_PORT:
505             *(uint8_t *)dst = channel->dma_cmd.val;
506             break;
507
508         case DMA_STATUS_PORT:
509             if (length != 1) {
510                 PrintError("Invalid read length for DMA status port\n");
511                 return -1;
512             }
513
514             *(uint8_t *)dst = channel->dma_status.val;
515             break;
516
517         case DMA_PRD_PORT0:
518         case DMA_PRD_PORT1:
519         case DMA_PRD_PORT2:
520         case DMA_PRD_PORT3: {
521             uint_t addr_index = port_offset & 0x3;
522             uint8_t * addr_buf = (uint8_t *)&(channel->dma_prd_addr);
523             int i = 0;
524
525             if (addr_index + length > 4) {
526                 PrintError("DMA Port space overrun port=%x len=%d\n", port_offset, length);
527                 return -1;
528             }
529
530             for (i = 0; i < length; i++) {
531                 *((uint8_t *)dst + i) = addr_buf[addr_index + i];
532             }
533
534             break;
535         }
536         default:
537             PrintError("IDE: Invalid DMA Port (%s)\n", dma_port_to_str(port_offset));
538             return -1;
539     }
540
541     PrintDebug("\tval=%x\n", *(uint32_t *)dst);
542
543     return length;
544 }
545
546
547
548 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
549     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
550     struct ide_channel * channel = get_selected_channel(ide, port);
551     struct ide_drive * drive = get_selected_drive(channel);
552
553     if (length != 1) {
554         PrintError("Invalid Write Length on IDE command Port %x\n", port);
555         return -1;
556     }
557
558     PrintDebug("IDE: Writing Command Port %x (%s) (val=%x)\n", port, io_port_to_str(port), *(uint8_t *)src);
559     
560     channel->cmd_reg = *(uint8_t *)src;
561     
562     switch (channel->cmd_reg) {
563         
564         case 0xa0: // ATAPI Command Packet
565             if (drive->drive_type != IDE_CDROM) {
566                 ide_abort_command(dev, channel);
567             }
568             
569             drive->sector_count = 1;
570
571             channel->status.busy = 0;
572             channel->status.write_fault = 0;
573             channel->status.data_req = 1;
574             channel->status.error = 0;
575
576             // reset the data buffer...
577             drive->transfer_length = ATAPI_PACKET_SIZE;
578             drive->transfer_index = 0;
579
580             break;
581         case 0xa1: // ATAPI Identify Device Packet
582             atapi_identify_device(drive);
583
584             channel->error_reg.val = 0;
585             channel->status.val = 0x58; // ready, data_req, seek_complete
586             
587             ide_raise_irq(dev, channel);
588             break;
589         case 0xec: // Identify Device
590             if (drive->drive_type != IDE_DISK) {
591                 drive_reset(drive);
592
593                 // JRL: Should we abort here?
594                 ide_abort_command(dev, channel);
595             } else {
596                 PrintError("IDE Disks currently not implemented\n");
597                 return -1;
598             }
599             break;
600
601         case 0xef: // Set Features
602             // Prior to this the features register has been written to. 
603             // This command tells the drive to check if the new value is supported (the value is drive specific)
604             // Common is that bit0=DMA enable
605             // If valid the drive raises an interrupt, if not it aborts.
606
607             // Do some checking here...
608
609             channel->status.busy = 0;
610             channel->status.write_fault = 0;
611             channel->status.error = 0;
612             channel->status.ready = 1;
613             channel->status.seek_complete = 1;
614             
615             ide_raise_irq(dev, channel);
616             break;
617         default:
618             PrintError("Unimplemented IDE command (%x)\n", channel->cmd_reg);
619             return -1;
620     }
621
622     return length;
623 }
624
625
626 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
627     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
628     struct ide_channel * channel = get_selected_channel(ide, port);
629     struct ide_drive * drive = get_selected_drive(channel);
630
631     //    PrintDebug("IDE: Writing Data Port %x (val=%x, len=%d)\n", 
632     //         port, *(uint32_t *)src, length);
633     
634     memcpy(drive->data_buf + drive->transfer_index, src, length);    
635     drive->transfer_index += length;
636
637     // Transfer is complete, dispatch the command
638     if (drive->transfer_index >= drive->transfer_length) {
639         switch (channel->cmd_reg) {
640             case 0x30: // Write Sectors
641                 PrintError("Writing Data not yet implemented\n");
642                 return -1;
643                 
644             case 0xa0: // ATAPI packet command
645                 if (atapi_handle_packet(dev, channel) == -1) {
646                     PrintError("Error handling ATAPI packet\n");
647                     return -1;
648                 }
649                 break;
650             default:
651                 PrintError("Unhandld IDE Command %x\n", channel->cmd_reg);
652                 return -1;
653         }
654     }
655
656     return length;
657 }
658
659
660 static int read_hd_data(uint8_t * dst, uint_t length, struct vm_device * dev, struct ide_channel * channel) {
661     PrintError("Harddrive data port read not implemented\n");
662     return -1;
663 }
664
665
666
667 static int read_cd_data(uint8_t * dst, uint_t length, struct vm_device * dev, struct ide_channel * channel) {
668     struct ide_drive * drive = get_selected_drive(channel);
669     int data_offset = drive->transfer_index % DATA_BUFFER_SIZE;
670     int req_offset = drive->transfer_index % drive->req_len;
671     
672     if (drive->cd_state.atapi_cmd != 0x28) {
673         PrintDebug("IDE: Reading CD Data (len=%d) (req_len=%d)\n", length, drive->req_len);
674     }
675
676     if (drive->transfer_index >= drive->transfer_length) {
677         PrintError("Buffer Overrun... (xfer_len=%d) (cur_idx=%d) (post_idx=%d)\n", 
678                    drive->transfer_length, drive->transfer_index, 
679                    drive->transfer_index + length);
680         return -1;
681     }
682
683
684
685     if ((data_offset == 0) && (drive->transfer_index > 0)) {
686         
687         if (drive->drive_type == IDE_CDROM) {
688             if (atapi_update_data_buf(dev, channel) == -1) {
689                 PrintError("Could not update CDROM data buffer\n");
690                 return -1;
691             } 
692         } else {
693             PrintError("IDE Harddrives not implemented\n");
694             return -1;
695         }
696     }
697
698     memcpy(dst, drive->data_buf + data_offset, length);
699     
700     drive->transfer_index += length;
701
702     if ((req_offset == 0) && (drive->transfer_index > 0)) {
703         if (drive->transfer_index < drive->transfer_length) {
704             // An increment is complete, but there is still more data to be transferred...
705             
706             channel->status.data_req = 1;
707
708             drive->irq_flags.c_d = 0;
709
710             // Update the request length in the cylinder regs
711             if (atapi_update_req_len(dev, channel, drive->transfer_length - drive->transfer_index) == -1) {
712                 PrintError("Could not update request length after completed increment\n");
713                 return -1;
714             }
715         } else {
716             // This was the final read of the request
717             channel->status.data_req = 0;
718             channel->status.ready = 1;
719             
720             drive->irq_flags.c_d = 1;
721             drive->irq_flags.rel = 0;
722         }
723
724         drive->irq_flags.io_dir = 1;
725         channel->status.busy = 0;
726
727         ide_raise_irq(dev, channel);
728     }
729
730     return length;
731 }
732
733
734 static int read_drive_id(uint8_t * dst, uint_t length, struct vm_device * dev, struct ide_channel * channel) {
735     struct ide_drive * drive = get_selected_drive(channel);
736
737     channel->status.busy = 0;
738     channel->status.ready = 1;
739     channel->status.write_fault = 0;
740     channel->status.seek_complete = 1;
741     channel->status.corrected = 0;
742     channel->status.error = 0;
743                 
744     
745     memcpy(dst, drive->data_buf + drive->transfer_index, length);
746     drive->transfer_index += length;
747     
748     if (drive->transfer_index >= drive->transfer_length) {
749         channel->status.data_req = 0;
750     }
751     
752     return length;
753 }
754
755
756 static int ide_read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
757     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
758     struct ide_channel * channel = get_selected_channel(ide, port);
759     struct ide_drive * drive = get_selected_drive(channel);
760
761     //    PrintDebug("IDE: Reading Data Port %x (len=%d)\n", port, length);
762
763     if ((channel->cmd_reg == 0xec) ||
764         (channel->cmd_reg == 0xa1)) {
765         return read_drive_id((uint8_t *)dst, length, dev, channel);
766     }
767
768     if (drive->drive_type == IDE_CDROM) {
769         if (read_cd_data((uint8_t *)dst, length, dev, channel) == -1) {
770             PrintError("IDE: Could not read CD Data\n");
771             return -1;
772         }
773     } else if (drive->drive_type == IDE_DISK) {
774         if (read_hd_data((uint8_t *)dst, length, dev, channel) == -1) {
775             PrintError("IDE: Could not read HD Data\n");
776             return -1;
777         }
778     } else {
779         memset((uint8_t *)dst, 0, length);
780     }
781
782     return length;
783 }
784
785 static int write_port_std(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
786     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
787     struct ide_channel * channel = get_selected_channel(ide, port);
788     struct ide_drive * drive = get_selected_drive(channel);
789             
790     if (length != 1) {
791         PrintError("Invalid Write length on IDE port %x\n", port);
792         return -1;
793     }
794
795     PrintDebug("IDE: Writing Standard Port %x (%s) (val=%x)\n", port, io_port_to_str(port), *(uint8_t *)src);
796
797     switch (port) {
798         // reset and interrupt enable
799         case PRI_CTRL_PORT:
800         case SEC_CTRL_PORT: {
801             struct ide_ctrl_reg * tmp_ctrl = (struct ide_ctrl_reg *)src;
802
803             // only reset channel on a 0->1 reset bit transition
804             if ((!channel->ctrl_reg.soft_reset) && (tmp_ctrl->soft_reset)) {
805                 channel_reset(channel);
806             } else if ((channel->ctrl_reg.soft_reset) && (!tmp_ctrl->soft_reset)) {
807                 channel_reset_complete(channel);
808             }
809
810             channel->ctrl_reg.val = tmp_ctrl->val;          
811             break;
812         }
813         case PRI_FEATURES_PORT:
814         case SEC_FEATURES_PORT:
815             channel->features.val = *(uint8_t *)src;
816             break;
817
818         case PRI_SECT_CNT_PORT:
819         case SEC_SECT_CNT_PORT:
820             drive->sector_count = *(uint8_t *)src;
821             break;
822
823         case PRI_SECT_NUM_PORT:
824         case SEC_SECT_NUM_PORT:
825             drive->sector_num = *(uint8_t *)src;
826
827         case PRI_CYL_LOW_PORT:
828         case SEC_CYL_LOW_PORT:
829             drive->cylinder_low = *(uint8_t *)src;
830             break;
831
832         case PRI_CYL_HIGH_PORT:
833         case SEC_CYL_HIGH_PORT:
834             drive->cylinder_high = *(uint8_t *)src;
835             break;
836
837         case PRI_DRV_SEL_PORT:
838         case SEC_DRV_SEL_PORT: {
839             channel->drive_head.val = *(uint8_t *)src;
840             
841             // make sure the reserved bits are ok..
842             // JRL TODO: check with new ramdisk to make sure this is right...
843             channel->drive_head.val |= 0xa0;
844
845             drive = get_selected_drive(channel);
846
847             // Selecting a non-present device is a no-no
848             if (drive->drive_type == IDE_NONE) {
849                 PrintDebug("Attempting to select a non-present drive\n");
850                 channel->error_reg.abort = 1;
851                 channel->status.error = 1;
852             }
853
854             break;
855         }
856         default:
857             PrintError("IDE: Write to unknown Port %x\n", port);
858             return -1;
859     }
860     return length;
861 }
862
863
864 static int read_port_std(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
865     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
866     struct ide_channel * channel = get_selected_channel(ide, port);
867     struct ide_drive * drive = get_selected_drive(channel);
868     
869     if (length != 1) {
870         PrintError("Invalid Read length on IDE port %x\n", port);
871         return -1;
872     }
873     
874     PrintDebug("IDE: Reading Standard Port %x (%s)\n", port, io_port_to_str(port));
875
876     if ((port == PRI_ADDR_REG_PORT) ||
877         (port == SEC_ADDR_REG_PORT)) {
878         // unused, return 0xff
879         *(uint8_t *)dst = 0xff;
880         return length;
881     }
882
883
884     // if no drive is present just return 0 + reserved bits
885     if (drive->drive_type == IDE_NONE) {
886         if ((port == PRI_DRV_SEL_PORT) ||
887             (port == SEC_DRV_SEL_PORT)) {
888             *(uint8_t *)dst = 0xa0;
889         } else {
890             *(uint8_t *)dst = 0;
891         }
892
893         return length;
894     }
895
896     switch (port) {
897
898         // This is really the error register.
899         case PRI_FEATURES_PORT:
900         case SEC_FEATURES_PORT:
901             *(uint8_t *)dst = channel->error_reg.val;
902             break;
903             
904         case PRI_SECT_CNT_PORT:
905         case SEC_SECT_CNT_PORT:
906             *(uint8_t *)dst = drive->sector_count;
907             break;
908
909         case PRI_SECT_NUM_PORT:
910         case SEC_SECT_NUM_PORT:
911             *(uint8_t *)dst = drive->sector_num;
912             break;
913
914         case PRI_CYL_LOW_PORT:
915         case SEC_CYL_LOW_PORT:
916             *(uint8_t *)dst = drive->cylinder_low;
917             break;
918
919
920         case PRI_CYL_HIGH_PORT:
921         case SEC_CYL_HIGH_PORT:
922             *(uint8_t *)dst = drive->cylinder_high;
923             break;
924
925         case PRI_DRV_SEL_PORT:
926         case SEC_DRV_SEL_PORT:  // hard disk drive and head register 0x1f6
927             *(uint8_t *)dst = channel->drive_head.val;
928             break;
929
930         case PRI_CTRL_PORT:
931         case SEC_CTRL_PORT:
932         case PRI_CMD_PORT:
933         case SEC_CMD_PORT:
934             // Something about lowering interrupts here....
935             *(uint8_t *)dst = channel->status.val;
936             break;
937
938         default:
939             PrintError("Invalid Port: %x\n", port);
940             return -1;
941     }
942
943     PrintDebug("\tVal=%x\n", *(uint8_t *)dst);
944
945     return length;
946 }
947
948
949
950 static void init_drive(struct ide_drive * drive) {
951
952     drive->sector_count = 0x01;
953     drive->sector_num = 0x01;
954     drive->cylinder = 0x0000;
955
956     drive->drive_type = IDE_NONE;
957
958     memset(drive->model, 0, sizeof(drive->model));
959
960     drive->transfer_index = 0;
961     drive->transfer_length = 0;
962     memset(drive->data_buf, 0, sizeof(drive->data_buf));
963
964
965
966     drive->private_data = NULL;
967     drive->cd_ops = NULL;
968 }
969
970 static void init_channel(struct ide_channel * channel) {
971     int i = 0;
972
973     channel->error_reg.val = 0x01;
974     channel->drive_head.val = 0x00;
975     channel->status.val = 0x00;
976     channel->cmd_reg = 0x00;
977     channel->ctrl_reg.val = 0x08;
978
979
980     channel->dma_cmd.val = 0;
981     channel->dma_status.val = 0;
982     channel->dma_prd_addr = 0;
983     channel->dma_tbl_index = 0;
984
985     for (i = 0; i < 2; i++) {
986         init_drive(&(channel->drives[i]));
987     }
988
989 }
990
991
992 static int pci_config_update(struct pci_device * pci_dev, uint_t reg_num, int length) {
993     PrintDebug("Interupt register (Dev=%s), irq=%d\n", pci_dev->name, pci_dev->config_header.intr_line);
994
995     return 0;
996 }
997
998 static int init_ide_state(struct vm_device * dev) {
999     struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
1000     struct v3_pci_bar bars[6];
1001     struct pci_device * pci_dev = NULL;
1002     int i, j;
1003
1004     for (i = 0; i < 2; i++) {
1005         init_channel(&(ide->channels[i]));
1006
1007         // JRL: this is a terrible hack...
1008         ide->channels[i].irq = PRI_DEFAULT_IRQ + i;
1009
1010         for (j = 0; j < 6; j++) {
1011             bars[j].type = PCI_BAR_NONE;
1012         }
1013
1014
1015         bars[4].type = PCI_BAR_IO;
1016         bars[4].default_base_port = PRI_DEFAULT_DMA_PORT + (i * 0x8);
1017         bars[4].num_ports = 8;
1018         
1019         if (i == 0) {
1020             bars[4].io_read = read_pri_dma_port;
1021             bars[4].io_write = write_pri_dma_port;
1022         } else {
1023             bars[4].io_read = read_sec_dma_port;
1024             bars[4].io_write = write_sec_dma_port;
1025         }
1026
1027         pci_dev = v3_pci_register_device(ide->pci, PCI_STD_DEVICE, 0, "V3_IDE", -1, bars,
1028                                          pci_config_update, NULL, NULL, dev);
1029
1030         if (pci_dev == NULL) {
1031             PrintError("Failed to register IDE BUS %d with PCI\n", i); 
1032             return -1;
1033         }
1034
1035         ide->channels[i].pci_dev = pci_dev;
1036
1037         pci_dev->config_header.vendor_id = 0x1095;
1038         pci_dev->config_header.device_id = 0x0646;
1039         pci_dev->config_header.revision = 0x8f07;
1040         pci_dev->config_header.subclass = 0x01;
1041         pci_dev->config_header.class = 0x01;
1042
1043         pci_dev->config_header.intr_line = PRI_DEFAULT_IRQ + i;
1044         pci_dev->config_header.intr_pin = 1;
1045     }
1046
1047
1048
1049     /* Register PIIX3 Busmaster PCI device */
1050     for (j = 0; j < 6; j++) {
1051         bars[j].type = PCI_BAR_NONE;
1052     }
1053
1054     pci_dev = v3_pci_register_device(ide->pci, PCI_STD_DEVICE, 0, "PIIX3 IDE", -1, bars,
1055                                      NULL, NULL, NULL, dev);
1056     
1057     
1058     ide->busmaster_pci = pci_dev;
1059
1060     pci_dev->config_header.vendor_id = 0x8086;
1061     pci_dev->config_header.device_id = 0x7010;
1062     pci_dev->config_header.revision = 0x80;
1063     pci_dev->config_header.subclass = 0x01;
1064     pci_dev->config_header.class = 0x01;
1065
1066
1067     return 0;
1068 }
1069
1070
1071
1072 static int init_ide(struct vm_device * dev) {
1073     //struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
1074
1075     PrintDebug("IDE: Initializing IDE\n");
1076
1077     if (init_ide_state(dev) == -1) {
1078         PrintError("Failed to initialize IDE state\n");
1079         return -1;
1080     }
1081
1082
1083     v3_dev_hook_io(dev, PRI_DATA_PORT, 
1084                    &ide_read_data_port, &write_data_port);
1085     v3_dev_hook_io(dev, PRI_FEATURES_PORT, 
1086                    &read_port_std, &write_port_std);
1087     v3_dev_hook_io(dev, PRI_SECT_CNT_PORT, 
1088                    &read_port_std, &write_port_std);
1089     v3_dev_hook_io(dev, PRI_SECT_NUM_PORT, 
1090                    &read_port_std, &write_port_std);
1091     v3_dev_hook_io(dev, PRI_CYL_LOW_PORT, 
1092                    &read_port_std, &write_port_std);
1093     v3_dev_hook_io(dev, PRI_CYL_HIGH_PORT, 
1094                    &read_port_std, &write_port_std);
1095     v3_dev_hook_io(dev, PRI_DRV_SEL_PORT, 
1096                    &read_port_std, &write_port_std);
1097     v3_dev_hook_io(dev, PRI_CMD_PORT, 
1098                    &read_port_std, &write_cmd_port);
1099
1100     v3_dev_hook_io(dev, SEC_DATA_PORT, 
1101                    &ide_read_data_port, &write_data_port);
1102     v3_dev_hook_io(dev, SEC_FEATURES_PORT, 
1103                    &read_port_std, &write_port_std);
1104     v3_dev_hook_io(dev, SEC_SECT_CNT_PORT, 
1105                    &read_port_std, &write_port_std);
1106     v3_dev_hook_io(dev, SEC_SECT_NUM_PORT, 
1107                    &read_port_std, &write_port_std);
1108     v3_dev_hook_io(dev, SEC_CYL_LOW_PORT, 
1109                    &read_port_std, &write_port_std);
1110     v3_dev_hook_io(dev, SEC_CYL_HIGH_PORT, 
1111                    &read_port_std, &write_port_std);
1112     v3_dev_hook_io(dev, SEC_DRV_SEL_PORT, 
1113                    &read_port_std, &write_port_std);
1114     v3_dev_hook_io(dev, SEC_CMD_PORT, 
1115                    &read_port_std, &write_cmd_port);
1116   
1117
1118     v3_dev_hook_io(dev, PRI_CTRL_PORT, 
1119                    &read_port_std, &write_port_std);
1120
1121     v3_dev_hook_io(dev, SEC_CTRL_PORT, 
1122                    &read_port_std, &write_port_std);
1123   
1124
1125     v3_dev_hook_io(dev, SEC_ADDR_REG_PORT, 
1126                    &read_port_std, &write_port_std);
1127
1128     v3_dev_hook_io(dev, PRI_ADDR_REG_PORT, 
1129                    &read_port_std, &write_port_std);
1130
1131     return 0;
1132 }
1133
1134
1135 static int deinit_ide(struct vm_device * dev) {
1136     // unhook io ports....
1137     // deregister from PCI?
1138     return 0;
1139 }
1140
1141
1142 static struct vm_device_ops dev_ops = {
1143     .init = init_ide,
1144     .deinit = deinit_ide,
1145     .reset = NULL,
1146     .start = NULL,
1147     .stop = NULL,
1148 };
1149
1150
1151 struct vm_device *  v3_create_ide(struct vm_device * pci) {
1152     struct ide_internal * ide  = (struct ide_internal *)V3_Malloc(sizeof(struct ide_internal));  
1153     struct vm_device * device = v3_create_device("IDE", &dev_ops, ide);
1154
1155     ide->pci = pci;
1156
1157     PrintDebug("IDE: Creating IDE bus x 2\n");
1158
1159     return device;
1160 }
1161
1162
1163
1164
1165
1166 int v3_ide_register_cdrom(struct vm_device * ide_dev, 
1167                           uint_t bus_num, 
1168                           uint_t drive_num,
1169                           char * dev_name, 
1170                           struct v3_ide_cd_ops * ops, 
1171                           void * private_data) {
1172
1173     struct ide_internal * ide  = (struct ide_internal *)(ide_dev->private_data);  
1174     struct ide_channel * channel = NULL;
1175     struct ide_drive * drive = NULL;
1176
1177     V3_ASSERT((bus_num >= 0) && (bus_num < 2));
1178     V3_ASSERT((drive_num >= 0) && (drive_num < 2));
1179
1180     channel = &(ide->channels[bus_num]);
1181     drive = &(channel->drives[drive_num]);
1182     
1183     if (drive->drive_type != IDE_NONE) {
1184         PrintError("Device slot (bus=%d, drive=%d) already occupied\n", bus_num, drive_num);
1185         return -1;
1186     }
1187
1188     strncpy(drive->model, dev_name, sizeof(drive->model) - 1);
1189
1190     while (strlen((char *)(drive->model)) < 40) {
1191         strcat((char*)(drive->model), " ");
1192     }
1193
1194
1195     drive->drive_type = IDE_CDROM;
1196
1197     drive->cd_ops = ops;
1198
1199     drive->private_data = private_data;
1200
1201     return 0;
1202 }
1203
1204
1205 int v3_ide_register_harddisk(struct vm_device * ide_dev, 
1206                              uint_t bus_num, 
1207                              uint_t drive_num, 
1208                              char * dev_name, 
1209                              struct v3_ide_hd_ops * ops, 
1210                              void * private_data) {
1211
1212     struct ide_internal * ide  = (struct ide_internal *)(ide_dev->private_data);  
1213     struct ide_channel * channel = NULL;
1214     struct ide_drive * drive = NULL;
1215
1216     V3_ASSERT((bus_num >= 0) && (bus_num < 2));
1217     V3_ASSERT((drive_num >= 0) && (drive_num < 2));
1218
1219     channel = &(ide->channels[bus_num]);
1220     drive = &(channel->drives[drive_num]);
1221     
1222     if (drive->drive_type != IDE_NONE) {
1223         PrintError("Device slot (bus=%d, drive=%d) already occupied\n", bus_num, drive_num);
1224         return -1;
1225     }
1226
1227     strncpy(drive->model, dev_name, sizeof(drive->model) - 1);
1228
1229     drive->drive_type = IDE_DISK;
1230
1231     drive->hd_ops = ops;
1232
1233     drive->private_data = private_data;
1234
1235     return 0;
1236 }