📄 ide.c
字号:
off2 = (off1 + TARGET_PAGE_SIZE)/2; p1->buf_size = off2 - off1; p1->page_offset = off1; p2->buf_size = TARGET_PAGE_SIZE - off2; p2->page_offset = off2;}static inline void__buffered_pio_flush(struct pio_buffer *piobuf, IDEState *s, uint32_t pointer){ uint8_t *buf = (uint8_t *)buffered_pio_page + piobuf->page_offset; memcpy(s->data_ptr, buf, pointer); s->data_ptr += pointer;}static inline voidbuffered_pio_flush(struct pio_buffer *piobuf){ IDEState *s = piobuf->opaque; uint32_t pointer = piobuf->pointer; if (s != NULL && pointer > 0) __buffered_pio_flush(piobuf, s, pointer);}static inline voidbuffered_pio_reset(IDEState *s){ struct pio_buffer *piobuf; if ((unsigned)s->drive_serial - 1 < 2) /* 1,2 */ piobuf = &buffered_pio_page->pio[PIO_BUFFER_IDE_PRIMARY]; else if ((unsigned)s->drive_serial - 3 < 2) /* 3,4 */ piobuf = &buffered_pio_page->pio[PIO_BUFFER_IDE_SECONDARY]; else return; buffered_pio_flush(piobuf); piobuf->pointer = 0; piobuf->data_end = 0; piobuf->opaque = NULL;}static inline voidbuffered_pio_write(IDEState *s, uint32_t addr, int size){ struct pio_buffer *piobuf = piobuf_by_addr(addr); int data_end; if (!piobuf) return; buffered_pio_flush(piobuf); data_end = s->data_end - s->data_ptr - size; if (data_end <= 0) data_end = 0; else if (data_end > piobuf->buf_size) data_end = piobuf->buf_size; piobuf->pointer = 0; piobuf->data_end = data_end; piobuf->opaque = s;}static inline voidbuffered_pio_read(IDEState *s, uint32_t addr, int size){ struct pio_buffer *piobuf = piobuf_by_addr(addr); int data_end; if (!piobuf) return; s->data_ptr += piobuf->pointer; data_end = s->data_end - s->data_ptr - size; if (data_end <= 0) { data_end = 0; } else { uint8_t *buf = (uint8_t *)buffered_pio_page + piobuf->page_offset; if (data_end > piobuf->buf_size) data_end = piobuf->buf_size; memcpy(buf, s->data_ptr + size, data_end); } piobuf->pointer = 0; piobuf->data_end = data_end; piobuf->opaque = NULL;}/* * buffered pio reads are undone. It results in normal pio when the domain * is restored. * buffered pio writes are handled before saving domain. * However currently pci_ide_save/load() just discards a pending transfer. XXX */static void__handle_buffered_pio(struct pio_buffer *piobuf){ IDEState *s = piobuf->opaque; uint32_t pointer = piobuf->pointer; if (pointer == 0) return;/* no buffered pio */ if (s != NULL) { /* written data are pending in pio_buffer. process it */ __buffered_pio_flush(piobuf, s, pointer); } else { /* data are buffered for pio read in pio_buffer. * undone buffering by buffered_pio_read() */ if (pointer > s->data_ptr - s->io_buffer) pointer = s->data_ptr - s->io_buffer; s->data_ptr -= pointer; } piobuf->pointer = 0; piobuf->data_end = 0; piobuf->opaque = NULL;}voidhandle_buffered_pio(void){ struct pio_buffer *p1, *p2; if (!buffered_pio_page) return; p1 = &buffered_pio_page->pio[PIO_BUFFER_IDE_PRIMARY]; p2 = &buffered_pio_page->pio[PIO_BUFFER_IDE_SECONDARY]; __handle_buffered_pio(p1); __handle_buffered_pio(p2);}#else /* !__ia64__ */#define buffered_pio_init() do {} while (0)#define buffered_pio_reset(I) do {} while (0)#define buffered_pio_write(I,A,S) do {} while (0)#define buffered_pio_read(I,A,S) do {} while (0)#endifstatic void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb);static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);static void padstr(char *str, const char *src, int len){ int i, v; for(i = 0; i < len; i++) { if (*src) v = *src++; else v = ' '; *(char *)((long)str ^ 1) = v; str++; }}static void padstr8(uint8_t *buf, int buf_size, const char *src){ int i; for(i = 0; i < buf_size; i++) { if (*src) buf[i] = *src++; else buf[i] = ' '; }}static void put_le16(uint16_t *p, unsigned int v){ *p = cpu_to_le16(v);}static void ide_identify(IDEState *s){ uint16_t *p; unsigned int oldsize; char buf[20]; if (s->identify_set) { memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data)); return; } memset(s->io_buffer, 0, 512); p = (uint16_t *)s->io_buffer; put_le16(p + 0, 0x0040); put_le16(p + 1, s->cylinders); put_le16(p + 3, s->heads); put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */ put_le16(p + 5, 512); /* XXX: retired, remove ? */ put_le16(p + 6, s->sectors); snprintf(buf, sizeof(buf), "QM%05d", s->drive_serial); padstr((uint8_t *)(p + 10), buf, 20); /* serial number */ put_le16(p + 20, 3); /* XXX: retired, remove ? */ put_le16(p + 21, 512); /* cache size in sectors */ put_le16(p + 22, 4); /* ecc bytes */ padstr((uint8_t *)(p + 23), QEMU_VERSION, 8); /* firmware version */ padstr((uint8_t *)(p + 27), "QEMU HARDDISK", 40); /* model */#if MAX_MULT_SECTORS > 1 put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);#endif put_le16(p + 48, 1); /* dword I/O */ put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */ put_le16(p + 51, 0x200); /* PIO transfer cycle */ put_le16(p + 52, 0x200); /* DMA transfer cycle */ put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */ put_le16(p + 54, s->cylinders); put_le16(p + 55, s->heads); put_le16(p + 56, s->sectors); oldsize = s->cylinders * s->heads * s->sectors; put_le16(p + 57, oldsize); put_le16(p + 58, oldsize >> 16); if (s->mult_sectors) put_le16(p + 59, 0x100 | s->mult_sectors); put_le16(p + 60, s->nb_sectors); put_le16(p + 61, s->nb_sectors >> 16); put_le16(p + 63, 0x07); /* mdma0-2 supported */ put_le16(p + 65, 120); put_le16(p + 66, 120); put_le16(p + 67, 120); put_le16(p + 68, 120); put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */ put_le16(p + 81, 0x16); /* conforms to ata5 */ /* 14=nop 5=write_cache */ put_le16(p + 82, (1 << 14) | (1 << 5)); /* 13=flush_cache_ext,12=flush_cache,10=lba48 */ put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10)); put_le16(p + 84, (1 << 14)); /* 14=nop 5=write_cache */ put_le16(p + 85, (1 << 14) | (s->write_cache << 5)); /* 13=flush_cache_ext,12=flush_cache,10=lba48 */ put_le16(p + 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10)); put_le16(p + 87, (1 << 14)); put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */ put_le16(p + 93, 1 | (1 << 14) | 0x2000); put_le16(p + 100, s->nb_sectors); put_le16(p + 101, s->nb_sectors >> 16); put_le16(p + 102, s->nb_sectors >> 32); put_le16(p + 103, s->nb_sectors >> 48); memcpy(s->identify_data, p, sizeof(s->identify_data)); s->identify_set = 1;}static void ide_atapi_identify(IDEState *s){ uint16_t *p; char buf[20]; if (s->identify_set) { memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data)); return; } memset(s->io_buffer, 0, 512); p = (uint16_t *)s->io_buffer; /* Removable CDROM, 50us response, 12 byte packets */ put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0)); snprintf(buf, sizeof(buf), "QM%05d", s->drive_serial); padstr((uint8_t *)(p + 10), buf, 20); /* serial number */ put_le16(p + 20, 3); /* buffer type */ put_le16(p + 21, 512); /* cache size in sectors */ put_le16(p + 22, 4); /* ecc bytes */ padstr((uint8_t *)(p + 23), QEMU_VERSION, 8); /* firmware version */ padstr((uint8_t *)(p + 27), "QEMU CD-ROM", 40); /* model */ put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */#ifdef USE_DMA_CDROM put_le16(p + 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */ put_le16(p + 53, 7); /* words 64-70, 54-58, 88 valid */ put_le16(p + 63, 7); /* mdma0-2 supported */ put_le16(p + 64, 0x3f); /* PIO modes supported */#else put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */ put_le16(p + 53, 3); /* words 64-70, 54-58 valid */ put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */ put_le16(p + 64, 1); /* PIO modes */#endif put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */ put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */ put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */ put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */ put_le16(p + 71, 30); /* in ns */ put_le16(p + 72, 30); /* in ns */ put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */#ifdef USE_DMA_CDROM put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */#endif memcpy(s->identify_data, p, sizeof(s->identify_data)); s->identify_set = 1;}static void ide_set_signature(IDEState *s){ s->select &= 0xf0; /* clear head */ /* put signature */ s->nsector = 1; s->sector = 1; if (s->is_cdrom) { s->lcyl = 0x14; s->hcyl = 0xeb; } else if (s->bs) { s->lcyl = 0; s->hcyl = 0; } else { s->lcyl = 0xff; s->hcyl = 0xff; }}static inline void ide_abort_command(IDEState *s){ s->status = READY_STAT | ERR_STAT; s->error = ABRT_ERR;}static inline void ide_set_irq(IDEState *s){ BMDMAState *bm = s->bmdma; if (!s->bs) return; /* yikes */ if (!(s->cmd & IDE_CMD_DISABLE_IRQ)) { if (bm) { bm->status |= BM_STATUS_INT; } s->set_irq(s->irq_opaque, s->irq, 1); }}/* prepare data transfer and tell what to do after */static void ide_transfer_start(IDEState *s, uint8_t *buf, int size, EndTransferFunc *end_transfer_func){ s->end_transfer_func = end_transfer_func; s->data_ptr = buf; s->data_end = buf + size; /* don't violate the HSM */ if (!(s->status & ERR_STAT)) s->status |= DRQ_STAT; buffered_pio_reset(s);}static void ide_transfer_stop(IDEState *s){ s->end_transfer_func = ide_transfer_stop; s->data_ptr = s->io_buffer; s->data_end = s->io_buffer; s->status &= ~DRQ_STAT; buffered_pio_reset(s);}static int64_t ide_get_sector(IDEState *s){ int64_t sector_num; if (s->select & 0x40) { /* lba */ if (!s->lba48) { sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) | (s->lcyl << 8) | s->sector; } else { sector_num = ((int64_t)s->hob_hcyl << 40) | ((int64_t) s->hob_lcyl << 32) | ((int64_t) s->hob_sector << 24) | ((int64_t) s->hcyl << 16) | ((int64_t) s->lcyl << 8) | s->sector; } } else { sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors + (s->select & 0x0f) * s->sectors + (s->sector - 1); } return sector_num;}static void ide_set_sector(IDEState *s, int64_t sector_num){ unsigned int cyl, r; if (s->select & 0x40) { if (!s->lba48) { s->select = (s->select & 0xf0) | (sector_num >> 24); s->hcyl = (sector_num >> 16); s->lcyl = (sector_num >> 8); s->sector = (sector_num); } else { s->sector = sector_num; s->lcyl = sector_num >> 8; s->hcyl = sector_num >> 16; s->hob_sector = sector_num >> 24; s->hob_lcyl = sector_num >> 32; s->hob_hcyl = sector_num >> 40; } } else { cyl = sector_num / (s->heads * s->sectors); r = sector_num % (s->heads * s->sectors); s->hcyl = cyl >> 8; s->lcyl = cyl; s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f); s->sector = (r % s->sectors) + 1; }}static void ide_sector_read(IDEState *s){ int64_t sector_num; int n; s->status = READY_STAT | SEEK_STAT; s->error = 0; /* not needed by IDE spec, but needed by Windows */ sector_num = ide_get_sector(s); n = s->nsector; if (n == 0) { /* no more sector to read from disk */ ide_transfer_stop(s); } else {#if defined(DEBUG_IDE) printf("read sector=%Ld\n", sector_num);#endif if (n > s->req_nb_sectors) n = s->req_nb_sectors; if (bdrv_read(s->bs, sector_num, s->io_buffer, n) != 0) { ide_abort_command(s); ide_set_irq(s); return; } ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read); ide_set_irq(s); ide_set_sector(s, sector_num + n); s->nsector -= n; }}/* return 0 if buffer completed */static int dma_buf_rw(BMDMAState *bm, int is_write){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -