2 * QEMU IDE disk and CD/DVD-ROM Emulator
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
28 #include "hw/isa/isa.h"
29 #include "qemu/error-report.h"
30 #include "qemu/timer.h"
31 #include "sysemu/sysemu.h"
32 #include "sysemu/blockdev.h"
33 #include "sysemu/dma.h"
34 #include "hw/block/block.h"
35 #include "sysemu/block-backend.h"
36 #include "qapi/error.h"
37 #include "qemu/cutils.h"
38 #include "sysemu/replay.h"
40 #include "hw/ide/internal.h"
43 /* These values were based on a Seagate ST3500418AS but have been modified
44 to make more sense in QEMU */
45 static const int smart_attributes[][12] = {
46 /* id, flags, hflags, val, wrst, raw (6 bytes), threshold */
47 /* raw read error rate*/
48 { 0x01, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06},
50 { 0x03, 0x03, 0x00, 0x64, 0x64, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
51 /* start stop count */
52 { 0x04, 0x02, 0x00, 0x64, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14},
53 /* remapped sectors */
54 { 0x05, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24},
56 { 0x09, 0x03, 0x00, 0x64, 0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
57 /* power cycle count */
58 { 0x0c, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
59 /* airflow-temperature-celsius */
60 { 190, 0x03, 0x00, 0x45, 0x45, 0x1f, 0x00, 0x1f, 0x1f, 0x00, 0x00, 0x32},
63 const char *IDE_DMA_CMD_lookup[IDE_DMA__COUNT] = {
64 [IDE_DMA_READ] = "DMA READ",
65 [IDE_DMA_WRITE] = "DMA WRITE",
66 [IDE_DMA_TRIM] = "DMA TRIM",
67 [IDE_DMA_ATAPI] = "DMA ATAPI"
70 static const char *IDE_DMA_CMD_str(enum ide_dma_cmd enval)
72 if ((unsigned)enval < IDE_DMA__COUNT) {
73 return IDE_DMA_CMD_lookup[enval];
75 return "DMA UNKNOWN CMD";
78 static void ide_dummy_transfer_stop(IDEState *s);
80 static void padstr(char *str, const char *src, int len)
83 for(i = 0; i < len; i++) {
92 static void put_le16(uint16_t *p, unsigned int v)
97 static void ide_identify_size(IDEState *s)
99 uint16_t *p = (uint16_t *)s->identify_data;
100 put_le16(p + 60, s->nb_sectors);
101 put_le16(p + 61, s->nb_sectors >> 16);
102 put_le16(p + 100, s->nb_sectors);
103 put_le16(p + 101, s->nb_sectors >> 16);
104 put_le16(p + 102, s->nb_sectors >> 32);
105 put_le16(p + 103, s->nb_sectors >> 48);
108 static void ide_identify(IDEState *s)
111 unsigned int oldsize;
112 IDEDevice *dev = s->unit ? s->bus->slave : s->bus->master;
114 p = (uint16_t *)s->identify_data;
115 if (s->identify_set) {
118 memset(p, 0, sizeof(s->identify_data));
120 put_le16(p + 0, 0x0040);
121 put_le16(p + 1, s->cylinders);
122 put_le16(p + 3, s->heads);
123 put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */
124 put_le16(p + 5, 512); /* XXX: retired, remove ? */
125 put_le16(p + 6, s->sectors);
126 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
127 put_le16(p + 20, 3); /* XXX: retired, remove ? */
128 put_le16(p + 21, 512); /* cache size in sectors */
129 put_le16(p + 22, 4); /* ecc bytes */
130 padstr((char *)(p + 23), s->version, 8); /* firmware version */
131 padstr((char *)(p + 27), s->drive_model_str, 40); /* model */
132 #if MAX_MULT_SECTORS > 1
133 put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
135 put_le16(p + 48, 1); /* dword I/O */
136 put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
137 put_le16(p + 51, 0x200); /* PIO transfer cycle */
138 put_le16(p + 52, 0x200); /* DMA transfer cycle */
139 put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
140 put_le16(p + 54, s->cylinders);
141 put_le16(p + 55, s->heads);
142 put_le16(p + 56, s->sectors);
143 oldsize = s->cylinders * s->heads * s->sectors;
144 put_le16(p + 57, oldsize);
145 put_le16(p + 58, oldsize >> 16);
147 put_le16(p + 59, 0x100 | s->mult_sectors);
148 /* *(p + 60) := nb_sectors -- see ide_identify_size */
149 /* *(p + 61) := nb_sectors >> 16 -- see ide_identify_size */
150 put_le16(p + 62, 0x07); /* single word dma0-2 supported */
151 put_le16(p + 63, 0x07); /* mdma0-2 supported */
152 put_le16(p + 64, 0x03); /* pio3-4 supported */
153 put_le16(p + 65, 120);
154 put_le16(p + 66, 120);
155 put_le16(p + 67, 120);
156 put_le16(p + 68, 120);
157 if (dev && dev->conf.discard_granularity) {
158 put_le16(p + 69, (1 << 14)); /* determinate TRIM behavior */
162 put_le16(p + 75, s->ncq_queues - 1);
164 put_le16(p + 76, (1 << 8));
167 put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
168 put_le16(p + 81, 0x16); /* conforms to ata5 */
169 /* 14=NOP supported, 5=WCACHE supported, 0=SMART supported */
170 put_le16(p + 82, (1 << 14) | (1 << 5) | 1);
171 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
172 put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
173 /* 14=set to 1, 8=has WWN, 1=SMART self test, 0=SMART error logging */
175 put_le16(p + 84, (1 << 14) | (1 << 8) | 0);
177 put_le16(p + 84, (1 << 14) | 0);
179 /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
180 if (blk_enable_write_cache(s->blk)) {
181 put_le16(p + 85, (1 << 14) | (1 << 5) | 1);
183 put_le16(p + 85, (1 << 14) | 1);
185 /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
186 put_le16(p + 86, (1 << 13) | (1 <<12) | (1 << 10));
187 /* 14=set to 1, 8=has WWN, 1=SMART self test, 0=SMART error logging */
189 put_le16(p + 87, (1 << 14) | (1 << 8) | 0);
191 put_le16(p + 87, (1 << 14) | 0);
193 put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
194 put_le16(p + 93, 1 | (1 << 14) | 0x2000);
195 /* *(p + 100) := nb_sectors -- see ide_identify_size */
196 /* *(p + 101) := nb_sectors >> 16 -- see ide_identify_size */
197 /* *(p + 102) := nb_sectors >> 32 -- see ide_identify_size */
198 /* *(p + 103) := nb_sectors >> 48 -- see ide_identify_size */
200 if (dev && dev->conf.physical_block_size)
201 put_le16(p + 106, 0x6000 | get_physical_block_exp(&dev->conf));
203 /* LE 16-bit words 111-108 contain 64-bit World Wide Name */
204 put_le16(p + 108, s->wwn >> 48);
205 put_le16(p + 109, s->wwn >> 32);
206 put_le16(p + 110, s->wwn >> 16);
207 put_le16(p + 111, s->wwn);
209 if (dev && dev->conf.discard_granularity) {
210 put_le16(p + 169, 1); /* TRIM support */
213 put_le16(p + 217, dev->rotation_rate); /* Nominal media rotation rate */
216 ide_identify_size(s);
220 memcpy(s->io_buffer, p, sizeof(s->identify_data));
223 static void ide_atapi_identify(IDEState *s)
227 p = (uint16_t *)s->identify_data;
228 if (s->identify_set) {
231 memset(p, 0, sizeof(s->identify_data));
233 /* Removable CDROM, 50us response, 12 byte packets */
234 put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
235 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
236 put_le16(p + 20, 3); /* buffer type */
237 put_le16(p + 21, 512); /* cache size in sectors */
238 put_le16(p + 22, 4); /* ecc bytes */
239 padstr((char *)(p + 23), s->version, 8); /* firmware version */
240 padstr((char *)(p + 27), s->drive_model_str, 40); /* model */
241 put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
243 put_le16(p + 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
244 put_le16(p + 53, 7); /* words 64-70, 54-58, 88 valid */
245 put_le16(p + 62, 7); /* single word dma0-2 supported */
246 put_le16(p + 63, 7); /* mdma0-2 supported */
248 put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */
249 put_le16(p + 53, 3); /* words 64-70, 54-58 valid */
250 put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */
252 put_le16(p + 64, 3); /* pio3-4 supported */
253 put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */
254 put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */
255 put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */
256 put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
258 put_le16(p + 71, 30); /* in ns */
259 put_le16(p + 72, 30); /* in ns */
262 put_le16(p + 75, s->ncq_queues - 1);
264 put_le16(p + 76, (1 << 8));
267 put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */
269 put_le16(p + 84, (1 << 8)); /* supports WWN for words 108-111 */
270 put_le16(p + 87, (1 << 8)); /* WWN enabled */
274 put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
278 /* LE 16-bit words 111-108 contain 64-bit World Wide Name */
279 put_le16(p + 108, s->wwn >> 48);
280 put_le16(p + 109, s->wwn >> 32);
281 put_le16(p + 110, s->wwn >> 16);
282 put_le16(p + 111, s->wwn);
288 memcpy(s->io_buffer, p, sizeof(s->identify_data));
291 static void ide_cfata_identify_size(IDEState *s)
293 uint16_t *p = (uint16_t *)s->identify_data;
294 put_le16(p + 7, s->nb_sectors >> 16); /* Sectors per card */
295 put_le16(p + 8, s->nb_sectors); /* Sectors per card */
296 put_le16(p + 60, s->nb_sectors); /* Total LBA sectors */
297 put_le16(p + 61, s->nb_sectors >> 16); /* Total LBA sectors */
300 static void ide_cfata_identify(IDEState *s)
305 p = (uint16_t *)s->identify_data;
306 if (s->identify_set) {
309 memset(p, 0, sizeof(s->identify_data));
311 cur_sec = s->cylinders * s->heads * s->sectors;
313 put_le16(p + 0, 0x848a); /* CF Storage Card signature */
314 put_le16(p + 1, s->cylinders); /* Default cylinders */
315 put_le16(p + 3, s->heads); /* Default heads */
316 put_le16(p + 6, s->sectors); /* Default sectors per track */
317 /* *(p + 7) := nb_sectors >> 16 -- see ide_cfata_identify_size */
318 /* *(p + 8) := nb_sectors -- see ide_cfata_identify_size */
319 padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */
320 put_le16(p + 22, 0x0004); /* ECC bytes */
321 padstr((char *) (p + 23), s->version, 8); /* Firmware Revision */
322 padstr((char *) (p + 27), s->drive_model_str, 40);/* Model number */
323 #if MAX_MULT_SECTORS > 1
324 put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
326 put_le16(p + 47, 0x0000);
328 put_le16(p + 49, 0x0f00); /* Capabilities */
329 put_le16(p + 51, 0x0002); /* PIO cycle timing mode */
330 put_le16(p + 52, 0x0001); /* DMA cycle timing mode */
331 put_le16(p + 53, 0x0003); /* Translation params valid */
332 put_le16(p + 54, s->cylinders); /* Current cylinders */
333 put_le16(p + 55, s->heads); /* Current heads */
334 put_le16(p + 56, s->sectors); /* Current sectors */
335 put_le16(p + 57, cur_sec); /* Current capacity */
336 put_le16(p + 58, cur_sec >> 16); /* Current capacity */
337 if (s->mult_sectors) /* Multiple sector setting */
338 put_le16(p + 59, 0x100 | s->mult_sectors);
339 /* *(p + 60) := nb_sectors -- see ide_cfata_identify_size */
340 /* *(p + 61) := nb_sectors >> 16 -- see ide_cfata_identify_size */
341 put_le16(p + 63, 0x0203); /* Multiword DMA capability */
342 put_le16(p + 64, 0x0001); /* Flow Control PIO support */
343 put_le16(p + 65, 0x0096); /* Min. Multiword DMA cycle */
344 put_le16(p + 66, 0x0096); /* Rec. Multiword DMA cycle */
345 put_le16(p + 68, 0x00b4); /* Min. PIO cycle time */
346 put_le16(p + 82, 0x400c); /* Command Set supported */
347 put_le16(p + 83, 0x7068); /* Command Set supported */
348 put_le16(p + 84, 0x4000); /* Features supported */
349 put_le16(p + 85, 0x000c); /* Command Set enabled */
350 put_le16(p + 86, 0x7044); /* Command Set enabled */
351 put_le16(p + 87, 0x4000); /* Features enabled */
352 put_le16(p + 91, 0x4060); /* Current APM level */
353 put_le16(p + 129, 0x0002); /* Current features option */
354 put_le16(p + 130, 0x0005); /* Reassigned sectors */
355 put_le16(p + 131, 0x0001); /* Initial power mode */
356 put_le16(p + 132, 0x0000); /* User signature */
357 put_le16(p + 160, 0x8100); /* Power requirement */
358 put_le16(p + 161, 0x8001); /* CF command set */
360 ide_cfata_identify_size(s);
364 memcpy(s->io_buffer, p, sizeof(s->identify_data));
367 static void ide_set_signature(IDEState *s)
369 s->select &= 0xf0; /* clear head */
373 if (s->drive_kind == IDE_CD) {
385 static bool ide_sect_range_ok(IDEState *s,
386 uint64_t sector, uint64_t nb_sectors)
388 uint64_t total_sectors;
390 blk_get_geometry(s->blk, &total_sectors);
391 if (sector > total_sectors || nb_sectors > total_sectors - sector) {
397 typedef struct TrimAIOCB {
407 static void trim_aio_cancel(BlockAIOCB *acb)
409 TrimAIOCB *iocb = container_of(acb, TrimAIOCB, common);
411 /* Exit the loop so ide_issue_trim_cb will not continue */
412 iocb->j = iocb->qiov->niov - 1;
413 iocb->i = (iocb->qiov->iov[iocb->j].iov_len / 8) - 1;
415 iocb->ret = -ECANCELED;
418 blk_aio_cancel_async(iocb->aiocb);
423 static const AIOCBInfo trim_aiocb_info = {
424 .aiocb_size = sizeof(TrimAIOCB),
425 .cancel_async = trim_aio_cancel,
428 static void ide_trim_bh_cb(void *opaque)
430 TrimAIOCB *iocb = opaque;
432 iocb->common.cb(iocb->common.opaque, iocb->ret);
434 qemu_bh_delete(iocb->bh);
436 qemu_aio_unref(iocb);
439 static void ide_issue_trim_cb(void *opaque, int ret)
441 TrimAIOCB *iocb = opaque;
442 IDEState *s = iocb->s;
445 while (iocb->j < iocb->qiov->niov) {
447 while (++iocb->i < iocb->qiov->iov[j].iov_len / 8) {
449 uint64_t *buffer = iocb->qiov->iov[j].iov_base;
451 /* 6-byte LBA + 2-byte range per entry */
452 uint64_t entry = le64_to_cpu(buffer[i]);
453 uint64_t sector = entry & 0x0000ffffffffffffULL;
454 uint16_t count = entry >> 48;
460 if (!ide_sect_range_ok(s, sector, count)) {
465 /* Got an entry! Submit and exit. */
466 iocb->aiocb = blk_aio_pdiscard(s->blk,
467 sector << BDRV_SECTOR_BITS,
468 count << BDRV_SECTOR_BITS,
469 ide_issue_trim_cb, opaque);
483 replay_bh_schedule_event(iocb->bh);
487 BlockAIOCB *ide_issue_trim(
488 int64_t offset, QEMUIOVector *qiov,
489 BlockCompletionFunc *cb, void *cb_opaque, void *opaque)
491 IDEState *s = opaque;
494 iocb = blk_aio_get(&trim_aiocb_info, s->blk, cb, cb_opaque);
496 iocb->bh = qemu_bh_new(ide_trim_bh_cb, iocb);
501 ide_issue_trim_cb(iocb, 0);
502 return &iocb->common;
505 void ide_abort_command(IDEState *s)
507 ide_transfer_stop(s);
508 s->status = READY_STAT | ERR_STAT;
512 static void ide_set_retry(IDEState *s)
514 s->bus->retry_unit = s->unit;
515 s->bus->retry_sector_num = ide_get_sector(s);
516 s->bus->retry_nsector = s->nsector;
519 static void ide_clear_retry(IDEState *s)
521 s->bus->retry_unit = -1;
522 s->bus->retry_sector_num = 0;
523 s->bus->retry_nsector = 0;
526 /* prepare data transfer and tell what to do after */
527 bool ide_transfer_start_norecurse(IDEState *s, uint8_t *buf, int size,
528 EndTransferFunc *end_transfer_func)
531 s->data_end = buf + size;
533 if (!(s->status & ERR_STAT)) {
534 s->status |= DRQ_STAT;
536 if (!s->bus->dma->ops->pio_transfer) {
537 s->end_transfer_func = end_transfer_func;
540 s->bus->dma->ops->pio_transfer(s->bus->dma);
544 void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
545 EndTransferFunc *end_transfer_func)
547 if (ide_transfer_start_norecurse(s, buf, size, end_transfer_func)) {
548 end_transfer_func(s);
552 static void ide_cmd_done(IDEState *s)
554 if (s->bus->dma->ops->cmd_done) {
555 s->bus->dma->ops->cmd_done(s->bus->dma);
559 static void ide_transfer_halt(IDEState *s)
561 s->end_transfer_func = ide_transfer_stop;
562 s->data_ptr = s->io_buffer;
563 s->data_end = s->io_buffer;
564 s->status &= ~DRQ_STAT;
567 void ide_transfer_stop(IDEState *s)
569 ide_transfer_halt(s);
573 int64_t ide_get_sector(IDEState *s)
576 if (s->select & 0x40) {
579 sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
580 (s->lcyl << 8) | s->sector;
582 sector_num = ((int64_t)s->hob_hcyl << 40) |
583 ((int64_t) s->hob_lcyl << 32) |
584 ((int64_t) s->hob_sector << 24) |
585 ((int64_t) s->hcyl << 16) |
586 ((int64_t) s->lcyl << 8) | s->sector;
589 sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
590 (s->select & 0x0f) * s->sectors + (s->sector - 1);
595 void ide_set_sector(IDEState *s, int64_t sector_num)
598 if (s->select & 0x40) {
600 s->select = (s->select & 0xf0) | (sector_num >> 24);
601 s->hcyl = (sector_num >> 16);
602 s->lcyl = (sector_num >> 8);
603 s->sector = (sector_num);
605 s->sector = sector_num;
606 s->lcyl = sector_num >> 8;
607 s->hcyl = sector_num >> 16;
608 s->hob_sector = sector_num >> 24;
609 s->hob_lcyl = sector_num >> 32;
610 s->hob_hcyl = sector_num >> 40;
613 cyl = sector_num / (s->heads * s->sectors);
614 r = sector_num % (s->heads * s->sectors);
617 s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f);
618 s->sector = (r % s->sectors) + 1;
622 static void ide_rw_error(IDEState *s) {
623 ide_abort_command(s);
627 static void ide_buffered_readv_cb(void *opaque, int ret)
629 IDEBufferedRequest *req = opaque;
630 if (!req->orphaned) {
632 assert(req->qiov.size == req->original_qiov->size);
633 qemu_iovec_from_buf(req->original_qiov, 0,
634 req->qiov.local_iov.iov_base,
635 req->original_qiov->size);
637 req->original_cb(req->original_opaque, ret);
639 QLIST_REMOVE(req, list);
640 qemu_vfree(qemu_iovec_buf(&req->qiov));
644 #define MAX_BUFFERED_REQS 16
646 BlockAIOCB *ide_buffered_readv(IDEState *s, int64_t sector_num,
647 QEMUIOVector *iov, int nb_sectors,
648 BlockCompletionFunc *cb, void *opaque)
651 IDEBufferedRequest *req;
654 QLIST_FOREACH(req, &s->buffered_requests, list) {
657 if (c > MAX_BUFFERED_REQS) {
658 return blk_abort_aio_request(s->blk, cb, opaque, -EIO);
661 req = g_new0(IDEBufferedRequest, 1);
662 req->original_qiov = iov;
663 req->original_cb = cb;
664 req->original_opaque = opaque;
665 qemu_iovec_init_buf(&req->qiov, blk_blockalign(s->blk, iov->size),
668 aioreq = blk_aio_preadv(s->blk, sector_num << BDRV_SECTOR_BITS,
669 &req->qiov, 0, ide_buffered_readv_cb, req);
671 QLIST_INSERT_HEAD(&s->buffered_requests, req, list);
676 * Cancel all pending DMA requests.
677 * Any buffered DMA requests are instantly canceled,
678 * but any pending unbuffered DMA requests must be waited on.
680 void ide_cancel_dma_sync(IDEState *s)
682 IDEBufferedRequest *req;
684 /* First invoke the callbacks of all buffered requests
685 * and flag those requests as orphaned. Ideally there
686 * are no unbuffered (Scatter Gather DMA Requests or
687 * write requests) pending and we can avoid to drain. */
688 QLIST_FOREACH(req, &s->buffered_requests, list) {
689 if (!req->orphaned) {
690 trace_ide_cancel_dma_sync_buffered(req->original_cb, req);
691 req->original_cb(req->original_opaque, -ECANCELED);
693 req->orphaned = true;
697 * We can't cancel Scatter Gather DMA in the middle of the
698 * operation or a partial (not full) DMA transfer would reach
699 * the storage so we wait for completion instead (we beahve
700 * like if the DMA was completed by the time the guest trying
701 * to cancel dma with bmdma_cmd_writeb with BM_CMD_START not
704 * In the future we'll be able to safely cancel the I/O if the
705 * whole DMA operation will be submitted to disk with a single
706 * aio operation with preadv/pwritev.
708 if (s->bus->dma->aiocb) {
709 trace_ide_cancel_dma_sync_remaining();
711 assert(s->bus->dma->aiocb == NULL);
715 static void ide_sector_read(IDEState *s);
717 static void ide_sector_read_cb(void *opaque, int ret)
719 IDEState *s = opaque;
723 s->status &= ~BUSY_STAT;
725 if (ret == -ECANCELED) {
729 if (ide_handle_rw_error(s, -ret, IDE_RETRY_PIO |
735 block_acct_done(blk_get_stats(s->blk), &s->acct);
738 if (n > s->req_nb_sectors) {
739 n = s->req_nb_sectors;
742 ide_set_sector(s, ide_get_sector(s) + n);
744 /* Allow the guest to read the io_buffer */
745 ide_transfer_start(s, s->io_buffer, n * BDRV_SECTOR_SIZE, ide_sector_read);
749 static void ide_sector_read(IDEState *s)
754 s->status = READY_STAT | SEEK_STAT;
755 s->error = 0; /* not needed by IDE spec, but needed by Windows */
756 sector_num = ide_get_sector(s);
760 ide_transfer_stop(s);
764 s->status |= BUSY_STAT;
766 if (n > s->req_nb_sectors) {
767 n = s->req_nb_sectors;
770 trace_ide_sector_read(sector_num, n);
772 if (!ide_sect_range_ok(s, sector_num, n)) {
774 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
778 qemu_iovec_init_buf(&s->qiov, s->io_buffer, n * BDRV_SECTOR_SIZE);
780 block_acct_start(blk_get_stats(s->blk), &s->acct,
781 n * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ);
782 s->pio_aiocb = ide_buffered_readv(s, sector_num, &s->qiov, n,
783 ide_sector_read_cb, s);
786 void dma_buf_commit(IDEState *s, uint32_t tx_bytes)
788 if (s->bus->dma->ops->commit_buf) {
789 s->bus->dma->ops->commit_buf(s->bus->dma, tx_bytes);
791 s->io_buffer_offset += tx_bytes;
792 qemu_sglist_destroy(&s->sg);
795 void ide_set_inactive(IDEState *s, bool more)
797 s->bus->dma->aiocb = NULL;
799 if (s->bus->dma->ops->set_inactive) {
800 s->bus->dma->ops->set_inactive(s->bus->dma, more);
805 void ide_dma_error(IDEState *s)
807 dma_buf_commit(s, 0);
808 ide_abort_command(s);
809 ide_set_inactive(s, false);
813 int ide_handle_rw_error(IDEState *s, int error, int op)
815 bool is_read = (op & IDE_RETRY_READ) != 0;
816 BlockErrorAction action = blk_get_error_action(s->blk, is_read, error);
818 if (action == BLOCK_ERROR_ACTION_STOP) {
819 assert(s->bus->retry_unit == s->unit);
820 s->bus->error_status = op;
821 } else if (action == BLOCK_ERROR_ACTION_REPORT) {
822 block_acct_failed(blk_get_stats(s->blk), &s->acct);
823 if (IS_IDE_RETRY_DMA(op)) {
825 } else if (IS_IDE_RETRY_ATAPI(op)) {
826 ide_atapi_io_error(s, -error);
831 blk_error_action(s->blk, action, is_read, error);
832 return action != BLOCK_ERROR_ACTION_IGNORE;
835 static void ide_dma_cb(void *opaque, int ret)
837 IDEState *s = opaque;
841 bool stay_active = false;
843 if (ret == -ECANCELED) {
847 if (ret == -EINVAL) {
853 if (ide_handle_rw_error(s, -ret, ide_dma_cmd_to_retry(s->dma_cmd))) {
854 s->bus->dma->aiocb = NULL;
855 dma_buf_commit(s, 0);
860 n = s->io_buffer_size >> 9;
861 if (n > s->nsector) {
862 /* The PRDs were longer than needed for this request. Shorten them so
863 * we don't get a negative remainder. The Active bit must remain set
864 * after the request completes. */
869 sector_num = ide_get_sector(s);
871 assert(n * 512 == s->sg.size);
872 dma_buf_commit(s, s->sg.size);
874 ide_set_sector(s, sector_num);
878 /* end of transfer ? */
879 if (s->nsector == 0) {
880 s->status = READY_STAT | SEEK_STAT;
885 /* launch next transfer */
887 s->io_buffer_index = 0;
888 s->io_buffer_size = n * 512;
889 if (s->bus->dma->ops->prepare_buf(s->bus->dma, s->io_buffer_size) < 512) {
890 /* The PRDs were too short. Reset the Active bit, but don't raise an
892 s->status = READY_STAT | SEEK_STAT;
893 dma_buf_commit(s, 0);
897 trace_ide_dma_cb(s, sector_num, n, IDE_DMA_CMD_str(s->dma_cmd));
899 if ((s->dma_cmd == IDE_DMA_READ || s->dma_cmd == IDE_DMA_WRITE) &&
900 !ide_sect_range_ok(s, sector_num, n)) {
902 block_acct_invalid(blk_get_stats(s->blk), s->acct.type);
906 offset = sector_num << BDRV_SECTOR_BITS;
907 switch (s->dma_cmd) {
909 s->bus->dma->aiocb = dma_blk_read(s->blk, &s->sg, offset,
910 BDRV_SECTOR_SIZE, ide_dma_cb, s);
913 s->bus->dma->aiocb = dma_blk_write(s->blk, &s->sg, offset,
914 BDRV_SECTOR_SIZE, ide_dma_cb, s);
917 s->bus->dma->aiocb = dma_blk_io(blk_get_aio_context(s->blk),
918 &s->sg, offset, BDRV_SECTOR_SIZE,
919 ide_issue_trim, s, ide_dma_cb, s,
920 DMA_DIRECTION_TO_DEVICE);
928 if (s->dma_cmd == IDE_DMA_READ || s->dma_cmd == IDE_DMA_WRITE) {
929 block_acct_done(blk_get_stats(s->blk), &s->acct);
931 ide_set_inactive(s, stay_active);
934 static void ide_sector_start_dma(IDEState *s, enum ide_dma_cmd dma_cmd)
936 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
937 s->io_buffer_size = 0;
938 s->dma_cmd = dma_cmd;
942 block_acct_start(blk_get_stats(s->blk), &s->acct,
943 s->nsector * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ);
946 block_acct_start(blk_get_stats(s->blk), &s->acct,
947 s->nsector * BDRV_SECTOR_SIZE, BLOCK_ACCT_WRITE);
953 ide_start_dma(s, ide_dma_cb);
956 void ide_start_dma(IDEState *s, BlockCompletionFunc *cb)
958 s->io_buffer_index = 0;
960 if (s->bus->dma->ops->start_dma) {
961 s->bus->dma->ops->start_dma(s->bus->dma, s, cb);
965 static void ide_sector_write(IDEState *s);
967 static void ide_sector_write_timer_cb(void *opaque)
969 IDEState *s = opaque;
973 static void ide_sector_write_cb(void *opaque, int ret)
975 IDEState *s = opaque;
978 if (ret == -ECANCELED) {
983 s->status &= ~BUSY_STAT;
986 if (ide_handle_rw_error(s, -ret, IDE_RETRY_PIO)) {
991 block_acct_done(blk_get_stats(s->blk), &s->acct);
994 if (n > s->req_nb_sectors) {
995 n = s->req_nb_sectors;
999 ide_set_sector(s, ide_get_sector(s) + n);
1000 if (s->nsector == 0) {
1001 /* no more sectors to write */
1002 ide_transfer_stop(s);
1004 int n1 = s->nsector;
1005 if (n1 > s->req_nb_sectors) {
1006 n1 = s->req_nb_sectors;
1008 ide_transfer_start(s, s->io_buffer, n1 * BDRV_SECTOR_SIZE,
1012 if (win2k_install_hack && ((++s->irq_count % 16) == 0)) {
1013 /* It seems there is a bug in the Windows 2000 installer HDD
1014 IDE driver which fills the disk with empty logs when the
1015 IDE write IRQ comes too early. This hack tries to correct
1016 that at the expense of slower write performances. Use this
1017 option _only_ to install Windows 2000. You must disable it
1019 timer_mod(s->sector_write_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1020 (NANOSECONDS_PER_SECOND / 1000));
1022 ide_set_irq(s->bus);
1026 static void ide_sector_write(IDEState *s)
1031 s->status = READY_STAT | SEEK_STAT | BUSY_STAT;
1032 sector_num = ide_get_sector(s);
1035 if (n > s->req_nb_sectors) {
1036 n = s->req_nb_sectors;
1039 trace_ide_sector_write(sector_num, n);
1041 if (!ide_sect_range_ok(s, sector_num, n)) {
1043 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_WRITE);
1047 qemu_iovec_init_buf(&s->qiov, s->io_buffer, n * BDRV_SECTOR_SIZE);
1049 block_acct_start(blk_get_stats(s->blk), &s->acct,
1050 n * BDRV_SECTOR_SIZE, BLOCK_ACCT_WRITE);
1051 s->pio_aiocb = blk_aio_pwritev(s->blk, sector_num << BDRV_SECTOR_BITS,
1052 &s->qiov, 0, ide_sector_write_cb, s);
1055 static void ide_flush_cb(void *opaque, int ret)
1057 IDEState *s = opaque;
1059 s->pio_aiocb = NULL;
1061 if (ret == -ECANCELED) {
1065 /* XXX: What sector number to set here? */
1066 if (ide_handle_rw_error(s, -ret, IDE_RETRY_FLUSH)) {
1072 block_acct_done(blk_get_stats(s->blk), &s->acct);
1074 s->status = READY_STAT | SEEK_STAT;
1076 ide_set_irq(s->bus);
1079 static void ide_flush_cache(IDEState *s)
1081 if (s->blk == NULL) {
1086 s->status |= BUSY_STAT;
1088 block_acct_start(blk_get_stats(s->blk), &s->acct, 0, BLOCK_ACCT_FLUSH);
1089 s->pio_aiocb = blk_aio_flush(s->blk, ide_flush_cb, s);
1092 static void ide_cfata_metadata_inquiry(IDEState *s)
1097 p = (uint16_t *) s->io_buffer;
1098 memset(p, 0, 0x200);
1099 spd = ((s->mdata_size - 1) >> 9) + 1;
1101 put_le16(p + 0, 0x0001); /* Data format revision */
1102 put_le16(p + 1, 0x0000); /* Media property: silicon */
1103 put_le16(p + 2, s->media_changed); /* Media status */
1104 put_le16(p + 3, s->mdata_size & 0xffff); /* Capacity in bytes (low) */
1105 put_le16(p + 4, s->mdata_size >> 16); /* Capacity in bytes (high) */
1106 put_le16(p + 5, spd & 0xffff); /* Sectors per device (low) */
1107 put_le16(p + 6, spd >> 16); /* Sectors per device (high) */
1110 static void ide_cfata_metadata_read(IDEState *s)
1114 if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
1115 s->status = ERR_STAT;
1116 s->error = ABRT_ERR;
1120 p = (uint16_t *) s->io_buffer;
1121 memset(p, 0, 0x200);
1123 put_le16(p + 0, s->media_changed); /* Media status */
1124 memcpy(p + 1, s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
1125 MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
1126 s->nsector << 9), 0x200 - 2));
1129 static void ide_cfata_metadata_write(IDEState *s)
1131 if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) {
1132 s->status = ERR_STAT;
1133 s->error = ABRT_ERR;
1137 s->media_changed = 0;
1139 memcpy(s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9),
1141 MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9),
1142 s->nsector << 9), 0x200 - 2));
1145 /* called when the inserted state of the media has changed */
1146 static void ide_cd_change_cb(void *opaque, bool load, Error **errp)
1148 IDEState *s = opaque;
1149 uint64_t nb_sectors;
1151 s->tray_open = !load;
1152 blk_get_geometry(s->blk, &nb_sectors);
1153 s->nb_sectors = nb_sectors;
1156 * First indicate to the guest that a CD has been removed. That's
1157 * done on the next command the guest sends us.
1159 * Then we set UNIT_ATTENTION, by which the guest will
1160 * detect a new CD in the drive. See ide_atapi_cmd() for details.
1162 s->cdrom_changed = 1;
1163 s->events.new_media = true;
1164 s->events.eject_request = false;
1165 ide_set_irq(s->bus);
1168 static void ide_cd_eject_request_cb(void *opaque, bool force)
1170 IDEState *s = opaque;
1172 s->events.eject_request = true;
1174 s->tray_locked = false;
1176 ide_set_irq(s->bus);
1179 static void ide_cmd_lba48_transform(IDEState *s, int lba48)
1183 /* handle the 'magic' 0 nsector count conversion here. to avoid
1184 * fiddling with the rest of the read logic, we just store the
1185 * full sector count in ->nsector and ignore ->hob_nsector from now
1191 if (!s->nsector && !s->hob_nsector)
1194 int lo = s->nsector;
1195 int hi = s->hob_nsector;
1197 s->nsector = (hi << 8) | lo;
1202 static void ide_clear_hob(IDEBus *bus)
1204 /* any write clears HOB high bit of device control register */
1205 bus->ifs[0].select &= ~(1 << 7);
1206 bus->ifs[1].select &= ~(1 << 7);
1209 /* IOport [W]rite [R]egisters */
1210 enum ATA_IOPORT_WR {
1211 ATA_IOPORT_WR_DATA = 0,
1212 ATA_IOPORT_WR_FEATURES = 1,
1213 ATA_IOPORT_WR_SECTOR_COUNT = 2,
1214 ATA_IOPORT_WR_SECTOR_NUMBER = 3,
1215 ATA_IOPORT_WR_CYLINDER_LOW = 4,
1216 ATA_IOPORT_WR_CYLINDER_HIGH = 5,
1217 ATA_IOPORT_WR_DEVICE_HEAD = 6,
1218 ATA_IOPORT_WR_COMMAND = 7,
1219 ATA_IOPORT_WR_NUM_REGISTERS,
1222 const char *ATA_IOPORT_WR_lookup[ATA_IOPORT_WR_NUM_REGISTERS] = {
1223 [ATA_IOPORT_WR_DATA] = "Data",
1224 [ATA_IOPORT_WR_FEATURES] = "Features",
1225 [ATA_IOPORT_WR_SECTOR_COUNT] = "Sector Count",
1226 [ATA_IOPORT_WR_SECTOR_NUMBER] = "Sector Number",
1227 [ATA_IOPORT_WR_CYLINDER_LOW] = "Cylinder Low",
1228 [ATA_IOPORT_WR_CYLINDER_HIGH] = "Cylinder High",
1229 [ATA_IOPORT_WR_DEVICE_HEAD] = "Device/Head",
1230 [ATA_IOPORT_WR_COMMAND] = "Command"
1233 void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
1235 IDEBus *bus = opaque;
1236 IDEState *s = idebus_active_if(bus);
1237 int reg_num = addr & 7;
1239 trace_ide_ioport_write(addr, ATA_IOPORT_WR_lookup[reg_num], val, bus, s);
1241 /* ignore writes to command block while busy with previous command */
1242 if (reg_num != 7 && (s->status & (BUSY_STAT|DRQ_STAT))) {
1249 case ATA_IOPORT_WR_FEATURES:
1251 /* NOTE: data is written to the two drives */
1252 bus->ifs[0].hob_feature = bus->ifs[0].feature;
1253 bus->ifs[1].hob_feature = bus->ifs[1].feature;
1254 bus->ifs[0].feature = val;
1255 bus->ifs[1].feature = val;
1257 case ATA_IOPORT_WR_SECTOR_COUNT:
1259 bus->ifs[0].hob_nsector = bus->ifs[0].nsector;
1260 bus->ifs[1].hob_nsector = bus->ifs[1].nsector;
1261 bus->ifs[0].nsector = val;
1262 bus->ifs[1].nsector = val;
1264 case ATA_IOPORT_WR_SECTOR_NUMBER:
1266 bus->ifs[0].hob_sector = bus->ifs[0].sector;
1267 bus->ifs[1].hob_sector = bus->ifs[1].sector;
1268 bus->ifs[0].sector = val;
1269 bus->ifs[1].sector = val;
1271 case ATA_IOPORT_WR_CYLINDER_LOW:
1273 bus->ifs[0].hob_lcyl = bus->ifs[0].lcyl;
1274 bus->ifs[1].hob_lcyl = bus->ifs[1].lcyl;
1275 bus->ifs[0].lcyl = val;
1276 bus->ifs[1].lcyl = val;
1278 case ATA_IOPORT_WR_CYLINDER_HIGH:
1280 bus->ifs[0].hob_hcyl = bus->ifs[0].hcyl;
1281 bus->ifs[1].hob_hcyl = bus->ifs[1].hcyl;
1282 bus->ifs[0].hcyl = val;
1283 bus->ifs[1].hcyl = val;
1285 case ATA_IOPORT_WR_DEVICE_HEAD:
1286 /* FIXME: HOB readback uses bit 7 */
1287 bus->ifs[0].select = (val & ~0x10) | 0xa0;
1288 bus->ifs[1].select = (val | 0x10) | 0xa0;
1290 bus->unit = (val >> 4) & 1;
1293 case ATA_IOPORT_WR_COMMAND:
1295 ide_exec_cmd(bus, val);
1300 static void ide_reset(IDEState *s)
1305 blk_aio_cancel(s->pio_aiocb);
1306 s->pio_aiocb = NULL;
1309 if (s->drive_kind == IDE_CFATA)
1310 s->mult_sectors = 0;
1312 s->mult_sectors = MAX_MULT_SECTORS;
1329 s->status = READY_STAT | SEEK_STAT;
1333 /* ATAPI specific */
1336 s->cdrom_changed = 0;
1337 s->packet_transfer_size = 0;
1338 s->elementary_transfer_size = 0;
1339 s->io_buffer_index = 0;
1340 s->cd_sector_size = 0;
1345 s->io_buffer_size = 0;
1346 s->req_nb_sectors = 0;
1348 ide_set_signature(s);
1349 /* init the transfer handler so that 0xffff is returned on data
1351 s->end_transfer_func = ide_dummy_transfer_stop;
1352 ide_dummy_transfer_stop(s);
1353 s->media_changed = 0;
1356 static bool cmd_nop(IDEState *s, uint8_t cmd)
1361 static bool cmd_device_reset(IDEState *s, uint8_t cmd)
1363 /* Halt PIO (in the DRQ phase), then DMA */
1364 ide_transfer_halt(s);
1365 ide_cancel_dma_sync(s);
1367 /* Reset any PIO commands, reset signature, etc */
1370 /* RESET: ATA8-ACS3 7.10.4 "Normal Outputs";
1371 * ATA8-ACS3 Table 184 "Device Signatures for Normal Output" */
1374 /* Do not overwrite status register */
1378 static bool cmd_data_set_management(IDEState *s, uint8_t cmd)
1380 switch (s->feature) {
1383 ide_sector_start_dma(s, IDE_DMA_TRIM);
1389 ide_abort_command(s);
1393 static bool cmd_identify(IDEState *s, uint8_t cmd)
1395 if (s->blk && s->drive_kind != IDE_CD) {
1396 if (s->drive_kind != IDE_CFATA) {
1399 ide_cfata_identify(s);
1401 s->status = READY_STAT | SEEK_STAT;
1402 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1403 ide_set_irq(s->bus);
1406 if (s->drive_kind == IDE_CD) {
1407 ide_set_signature(s);
1409 ide_abort_command(s);
1415 static bool cmd_verify(IDEState *s, uint8_t cmd)
1417 bool lba48 = (cmd == WIN_VERIFY_EXT);
1419 /* do sector number check ? */
1420 ide_cmd_lba48_transform(s, lba48);
1425 static bool cmd_set_multiple_mode(IDEState *s, uint8_t cmd)
1427 if (s->drive_kind == IDE_CFATA && s->nsector == 0) {
1428 /* Disable Read and Write Multiple */
1429 s->mult_sectors = 0;
1430 } else if ((s->nsector & 0xff) != 0 &&
1431 ((s->nsector & 0xff) > MAX_MULT_SECTORS ||
1432 (s->nsector & (s->nsector - 1)) != 0)) {
1433 ide_abort_command(s);
1435 s->mult_sectors = s->nsector & 0xff;
1441 static bool cmd_read_multiple(IDEState *s, uint8_t cmd)
1443 bool lba48 = (cmd == WIN_MULTREAD_EXT);
1445 if (!s->blk || !s->mult_sectors) {
1446 ide_abort_command(s);
1450 ide_cmd_lba48_transform(s, lba48);
1451 s->req_nb_sectors = s->mult_sectors;
1456 static bool cmd_write_multiple(IDEState *s, uint8_t cmd)
1458 bool lba48 = (cmd == WIN_MULTWRITE_EXT);
1461 if (!s->blk || !s->mult_sectors) {
1462 ide_abort_command(s);
1466 ide_cmd_lba48_transform(s, lba48);
1468 s->req_nb_sectors = s->mult_sectors;
1469 n = MIN(s->nsector, s->req_nb_sectors);
1471 s->status = SEEK_STAT | READY_STAT;
1472 ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
1474 s->media_changed = 1;
1479 static bool cmd_read_pio(IDEState *s, uint8_t cmd)
1481 bool lba48 = (cmd == WIN_READ_EXT);
1483 if (s->drive_kind == IDE_CD) {
1484 ide_set_signature(s); /* odd, but ATA4 8.27.5.2 requires it */
1485 ide_abort_command(s);
1490 ide_abort_command(s);
1494 ide_cmd_lba48_transform(s, lba48);
1495 s->req_nb_sectors = 1;
1501 static bool cmd_write_pio(IDEState *s, uint8_t cmd)
1503 bool lba48 = (cmd == WIN_WRITE_EXT);
1506 ide_abort_command(s);
1510 ide_cmd_lba48_transform(s, lba48);
1512 s->req_nb_sectors = 1;
1513 s->status = SEEK_STAT | READY_STAT;
1514 ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1516 s->media_changed = 1;
1521 static bool cmd_read_dma(IDEState *s, uint8_t cmd)
1523 bool lba48 = (cmd == WIN_READDMA_EXT);
1526 ide_abort_command(s);
1530 ide_cmd_lba48_transform(s, lba48);
1531 ide_sector_start_dma(s, IDE_DMA_READ);
1536 static bool cmd_write_dma(IDEState *s, uint8_t cmd)
1538 bool lba48 = (cmd == WIN_WRITEDMA_EXT);
1541 ide_abort_command(s);
1545 ide_cmd_lba48_transform(s, lba48);
1546 ide_sector_start_dma(s, IDE_DMA_WRITE);
1548 s->media_changed = 1;
1553 static bool cmd_flush_cache(IDEState *s, uint8_t cmd)
1559 static bool cmd_seek(IDEState *s, uint8_t cmd)
1561 /* XXX: Check that seek is within bounds */
1565 static bool cmd_read_native_max(IDEState *s, uint8_t cmd)
1567 bool lba48 = (cmd == WIN_READ_NATIVE_MAX_EXT);
1569 /* Refuse if no sectors are addressable (e.g. medium not inserted) */
1570 if (s->nb_sectors == 0) {
1571 ide_abort_command(s);
1575 ide_cmd_lba48_transform(s, lba48);
1576 ide_set_sector(s, s->nb_sectors - 1);
1581 static bool cmd_check_power_mode(IDEState *s, uint8_t cmd)
1583 s->nsector = 0xff; /* device active or idle */
1587 static bool cmd_set_features(IDEState *s, uint8_t cmd)
1589 uint16_t *identify_data;
1592 ide_abort_command(s);
1596 /* XXX: valid for CDROM ? */
1597 switch (s->feature) {
1598 case 0x02: /* write cache enable */
1599 blk_set_enable_write_cache(s->blk, true);
1600 identify_data = (uint16_t *)s->identify_data;
1601 put_le16(identify_data + 85, (1 << 14) | (1 << 5) | 1);
1603 case 0x82: /* write cache disable */
1604 blk_set_enable_write_cache(s->blk, false);
1605 identify_data = (uint16_t *)s->identify_data;
1606 put_le16(identify_data + 85, (1 << 14) | 1);
1609 case 0xcc: /* reverting to power-on defaults enable */
1610 case 0x66: /* reverting to power-on defaults disable */
1611 case 0xaa: /* read look-ahead enable */
1612 case 0x55: /* read look-ahead disable */
1613 case 0x05: /* set advanced power management mode */
1614 case 0x85: /* disable advanced power management mode */
1615 case 0x69: /* NOP */
1616 case 0x67: /* NOP */
1617 case 0x96: /* NOP */
1618 case 0x9a: /* NOP */
1619 case 0x42: /* enable Automatic Acoustic Mode */
1620 case 0xc2: /* disable Automatic Acoustic Mode */
1622 case 0x03: /* set transfer mode */
1624 uint8_t val = s->nsector & 0x07;
1625 identify_data = (uint16_t *)s->identify_data;
1627 switch (s->nsector >> 3) {
1628 case 0x00: /* pio default */
1629 case 0x01: /* pio mode */
1630 put_le16(identify_data + 62, 0x07);
1631 put_le16(identify_data + 63, 0x07);
1632 put_le16(identify_data + 88, 0x3f);
1634 case 0x02: /* sigle word dma mode*/
1635 put_le16(identify_data + 62, 0x07 | (1 << (val + 8)));
1636 put_le16(identify_data + 63, 0x07);
1637 put_le16(identify_data + 88, 0x3f);
1639 case 0x04: /* mdma mode */
1640 put_le16(identify_data + 62, 0x07);
1641 put_le16(identify_data + 63, 0x07 | (1 << (val + 8)));
1642 put_le16(identify_data + 88, 0x3f);
1644 case 0x08: /* udma mode */
1645 put_le16(identify_data + 62, 0x07);
1646 put_le16(identify_data + 63, 0x07);
1647 put_le16(identify_data + 88, 0x3f | (1 << (val + 8)));
1657 ide_abort_command(s);
1662 /*** ATAPI commands ***/
1664 static bool cmd_identify_packet(IDEState *s, uint8_t cmd)
1666 ide_atapi_identify(s);
1667 s->status = READY_STAT | SEEK_STAT;
1668 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1669 ide_set_irq(s->bus);
1673 static bool cmd_exec_dev_diagnostic(IDEState *s, uint8_t cmd)
1675 ide_set_signature(s);
1677 if (s->drive_kind == IDE_CD) {
1678 s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
1679 * devices to return a clear status register
1680 * with READY_STAT *not* set. */
1683 s->status = READY_STAT | SEEK_STAT;
1684 /* The bits of the error register are not as usual for this command!
1685 * They are part of the regular output (this is why ERR_STAT isn't set)
1686 * Device 0 passed, Device 1 passed or not present. */
1688 ide_set_irq(s->bus);
1694 static bool cmd_packet(IDEState *s, uint8_t cmd)
1696 /* overlapping commands not supported */
1697 if (s->feature & 0x02) {
1698 ide_abort_command(s);
1702 s->status = READY_STAT | SEEK_STAT;
1703 s->atapi_dma = s->feature & 1;
1705 s->dma_cmd = IDE_DMA_ATAPI;
1708 ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
1714 /*** CF-ATA commands ***/
1716 static bool cmd_cfa_req_ext_error_code(IDEState *s, uint8_t cmd)
1718 s->error = 0x09; /* miscellaneous error */
1719 s->status = READY_STAT | SEEK_STAT;
1720 ide_set_irq(s->bus);
1725 static bool cmd_cfa_erase_sectors(IDEState *s, uint8_t cmd)
1727 /* WIN_SECURITY_FREEZE_LOCK has the same ID as CFA_WEAR_LEVEL and is
1728 * required for Windows 8 to work with AHCI */
1730 if (cmd == CFA_WEAR_LEVEL) {
1734 if (cmd == CFA_ERASE_SECTORS) {
1735 s->media_changed = 1;
1741 static bool cmd_cfa_translate_sector(IDEState *s, uint8_t cmd)
1743 s->status = READY_STAT | SEEK_STAT;
1745 memset(s->io_buffer, 0, 0x200);
1746 s->io_buffer[0x00] = s->hcyl; /* Cyl MSB */
1747 s->io_buffer[0x01] = s->lcyl; /* Cyl LSB */
1748 s->io_buffer[0x02] = s->select; /* Head */
1749 s->io_buffer[0x03] = s->sector; /* Sector */
1750 s->io_buffer[0x04] = ide_get_sector(s) >> 16; /* LBA MSB */
1751 s->io_buffer[0x05] = ide_get_sector(s) >> 8; /* LBA */
1752 s->io_buffer[0x06] = ide_get_sector(s) >> 0; /* LBA LSB */
1753 s->io_buffer[0x13] = 0x00; /* Erase flag */
1754 s->io_buffer[0x18] = 0x00; /* Hot count */
1755 s->io_buffer[0x19] = 0x00; /* Hot count */
1756 s->io_buffer[0x1a] = 0x01; /* Hot count */
1758 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1759 ide_set_irq(s->bus);
1764 static bool cmd_cfa_access_metadata_storage(IDEState *s, uint8_t cmd)
1766 switch (s->feature) {
1767 case 0x02: /* Inquiry Metadata Storage */
1768 ide_cfata_metadata_inquiry(s);
1770 case 0x03: /* Read Metadata Storage */
1771 ide_cfata_metadata_read(s);
1773 case 0x04: /* Write Metadata Storage */
1774 ide_cfata_metadata_write(s);
1777 ide_abort_command(s);
1781 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1782 s->status = 0x00; /* NOTE: READY is _not_ set */
1783 ide_set_irq(s->bus);
1788 static bool cmd_ibm_sense_condition(IDEState *s, uint8_t cmd)
1790 switch (s->feature) {
1791 case 0x01: /* sense temperature in device */
1792 s->nsector = 0x50; /* +20 C */
1795 ide_abort_command(s);
1803 /*** SMART commands ***/
1805 static bool cmd_smart(IDEState *s, uint8_t cmd)
1809 if (s->hcyl != 0xc2 || s->lcyl != 0x4f) {
1813 if (!s->smart_enabled && s->feature != SMART_ENABLE) {
1817 switch (s->feature) {
1819 s->smart_enabled = 0;
1823 s->smart_enabled = 1;
1826 case SMART_ATTR_AUTOSAVE:
1827 switch (s->sector) {
1829 s->smart_autosave = 0;
1832 s->smart_autosave = 1;
1840 if (!s->smart_errors) {
1849 case SMART_READ_THRESH:
1850 memset(s->io_buffer, 0, 0x200);
1851 s->io_buffer[0] = 0x01; /* smart struct version */
1853 for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) {
1854 s->io_buffer[2 + 0 + (n * 12)] = smart_attributes[n][0];
1855 s->io_buffer[2 + 1 + (n * 12)] = smart_attributes[n][11];
1859 for (n = 0; n < 511; n++) {
1860 s->io_buffer[511] += s->io_buffer[n];
1862 s->io_buffer[511] = 0x100 - s->io_buffer[511];
1864 s->status = READY_STAT | SEEK_STAT;
1865 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1866 ide_set_irq(s->bus);
1869 case SMART_READ_DATA:
1870 memset(s->io_buffer, 0, 0x200);
1871 s->io_buffer[0] = 0x01; /* smart struct version */
1873 for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) {
1875 for (i = 0; i < 11; i++) {
1876 s->io_buffer[2 + i + (n * 12)] = smart_attributes[n][i];
1880 s->io_buffer[362] = 0x02 | (s->smart_autosave ? 0x80 : 0x00);
1881 if (s->smart_selftest_count == 0) {
1882 s->io_buffer[363] = 0;
1885 s->smart_selftest_data[3 +
1886 (s->smart_selftest_count - 1) *
1889 s->io_buffer[364] = 0x20;
1890 s->io_buffer[365] = 0x01;
1891 /* offline data collection capacity: execute + self-test*/
1892 s->io_buffer[367] = (1 << 4 | 1 << 3 | 1);
1893 s->io_buffer[368] = 0x03; /* smart capability (1) */
1894 s->io_buffer[369] = 0x00; /* smart capability (2) */
1895 s->io_buffer[370] = 0x01; /* error logging supported */
1896 s->io_buffer[372] = 0x02; /* minutes for poll short test */
1897 s->io_buffer[373] = 0x36; /* minutes for poll ext test */
1898 s->io_buffer[374] = 0x01; /* minutes for poll conveyance */
1900 for (n = 0; n < 511; n++) {
1901 s->io_buffer[511] += s->io_buffer[n];
1903 s->io_buffer[511] = 0x100 - s->io_buffer[511];
1905 s->status = READY_STAT | SEEK_STAT;
1906 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1907 ide_set_irq(s->bus);
1910 case SMART_READ_LOG:
1911 switch (s->sector) {
1912 case 0x01: /* summary smart error log */
1913 memset(s->io_buffer, 0, 0x200);
1914 s->io_buffer[0] = 0x01;
1915 s->io_buffer[1] = 0x00; /* no error entries */
1916 s->io_buffer[452] = s->smart_errors & 0xff;
1917 s->io_buffer[453] = (s->smart_errors & 0xff00) >> 8;
1919 for (n = 0; n < 511; n++) {
1920 s->io_buffer[511] += s->io_buffer[n];
1922 s->io_buffer[511] = 0x100 - s->io_buffer[511];
1924 case 0x06: /* smart self test log */
1925 memset(s->io_buffer, 0, 0x200);
1926 s->io_buffer[0] = 0x01;
1927 if (s->smart_selftest_count == 0) {
1928 s->io_buffer[508] = 0;
1930 s->io_buffer[508] = s->smart_selftest_count;
1931 for (n = 2; n < 506; n++) {
1932 s->io_buffer[n] = s->smart_selftest_data[n];
1936 for (n = 0; n < 511; n++) {
1937 s->io_buffer[511] += s->io_buffer[n];
1939 s->io_buffer[511] = 0x100 - s->io_buffer[511];
1944 s->status = READY_STAT | SEEK_STAT;
1945 ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1946 ide_set_irq(s->bus);
1949 case SMART_EXECUTE_OFFLINE:
1950 switch (s->sector) {
1951 case 0: /* off-line routine */
1952 case 1: /* short self test */
1953 case 2: /* extended self test */
1954 s->smart_selftest_count++;
1955 if (s->smart_selftest_count > 21) {
1956 s->smart_selftest_count = 1;
1958 n = 2 + (s->smart_selftest_count - 1) * 24;
1959 s->smart_selftest_data[n] = s->sector;
1960 s->smart_selftest_data[n + 1] = 0x00; /* OK and finished */
1961 s->smart_selftest_data[n + 2] = 0x34; /* hour count lsb */
1962 s->smart_selftest_data[n + 3] = 0x12; /* hour count msb */
1971 ide_abort_command(s);
1975 #define HD_OK (1u << IDE_HD)
1976 #define CD_OK (1u << IDE_CD)
1977 #define CFA_OK (1u << IDE_CFATA)
1978 #define HD_CFA_OK (HD_OK | CFA_OK)
1979 #define ALL_OK (HD_OK | CD_OK | CFA_OK)
1981 /* Set the Disk Seek Completed status bit during completion */
1982 #define SET_DSC (1u << 8)
1984 /* See ACS-2 T13/2015-D Table B.2 Command codes */
1985 static const struct {
1986 /* Returns true if the completion code should be run */
1987 bool (*handler)(IDEState *s, uint8_t cmd);
1989 } ide_cmd_table[0x100] = {
1990 /* NOP not implemented, mandatory for CD */
1991 [CFA_REQ_EXT_ERROR_CODE] = { cmd_cfa_req_ext_error_code, CFA_OK },
1992 [WIN_DSM] = { cmd_data_set_management, HD_CFA_OK },
1993 [WIN_DEVICE_RESET] = { cmd_device_reset, CD_OK },
1994 [WIN_RECAL] = { cmd_nop, HD_CFA_OK | SET_DSC},
1995 [WIN_READ] = { cmd_read_pio, ALL_OK },
1996 [WIN_READ_ONCE] = { cmd_read_pio, HD_CFA_OK },
1997 [WIN_READ_EXT] = { cmd_read_pio, HD_CFA_OK },
1998 [WIN_READDMA_EXT] = { cmd_read_dma, HD_CFA_OK },
1999 [WIN_READ_NATIVE_MAX_EXT] = { cmd_read_native_max, HD_CFA_OK | SET_DSC },
2000 [WIN_MULTREAD_EXT] = { cmd_read_multiple, HD_CFA_OK },
2001 [WIN_WRITE] = { cmd_write_pio, HD_CFA_OK },
2002 [WIN_WRITE_ONCE] = { cmd_write_pio, HD_CFA_OK },
2003 [WIN_WRITE_EXT] = { cmd_write_pio, HD_CFA_OK },
2004 [WIN_WRITEDMA_EXT] = { cmd_write_dma, HD_CFA_OK },
2005 [CFA_WRITE_SECT_WO_ERASE] = { cmd_write_pio, CFA_OK },
2006 [WIN_MULTWRITE_EXT] = { cmd_write_multiple, HD_CFA_OK },
2007 [WIN_WRITE_VERIFY] = { cmd_write_pio, HD_CFA_OK },
2008 [WIN_VERIFY] = { cmd_verify, HD_CFA_OK | SET_DSC },
2009 [WIN_VERIFY_ONCE] = { cmd_verify, HD_CFA_OK | SET_DSC },
2010 [WIN_VERIFY_EXT] = { cmd_verify, HD_CFA_OK | SET_DSC },
2011 [WIN_SEEK] = { cmd_seek, HD_CFA_OK | SET_DSC },
2012 [CFA_TRANSLATE_SECTOR] = { cmd_cfa_translate_sector, CFA_OK },
2013 [WIN_DIAGNOSE] = { cmd_exec_dev_diagnostic, ALL_OK },
2014 [WIN_SPECIFY] = { cmd_nop, HD_CFA_OK | SET_DSC },
2015 [WIN_STANDBYNOW2] = { cmd_nop, HD_CFA_OK },
2016 [WIN_IDLEIMMEDIATE2] = { cmd_nop, HD_CFA_OK },
2017 [WIN_STANDBY2] = { cmd_nop, HD_CFA_OK },
2018 [WIN_SETIDLE2] = { cmd_nop, HD_CFA_OK },
2019 [WIN_CHECKPOWERMODE2] = { cmd_check_power_mode, HD_CFA_OK | SET_DSC },
2020 [WIN_SLEEPNOW2] = { cmd_nop, HD_CFA_OK },
2021 [WIN_PACKETCMD] = { cmd_packet, CD_OK },
2022 [WIN_PIDENTIFY] = { cmd_identify_packet, CD_OK },
2023 [WIN_SMART] = { cmd_smart, HD_CFA_OK | SET_DSC },
2024 [CFA_ACCESS_METADATA_STORAGE] = { cmd_cfa_access_metadata_storage, CFA_OK },
2025 [CFA_ERASE_SECTORS] = { cmd_cfa_erase_sectors, CFA_OK | SET_DSC },
2026 [WIN_MULTREAD] = { cmd_read_multiple, HD_CFA_OK },
2027 [WIN_MULTWRITE] = { cmd_write_multiple, HD_CFA_OK },
2028 [WIN_SETMULT] = { cmd_set_multiple_mode, HD_CFA_OK | SET_DSC },
2029 [WIN_READDMA] = { cmd_read_dma, HD_CFA_OK },
2030 [WIN_READDMA_ONCE] = { cmd_read_dma, HD_CFA_OK },
2031 [WIN_WRITEDMA] = { cmd_write_dma, HD_CFA_OK },
2032 [WIN_WRITEDMA_ONCE] = { cmd_write_dma, HD_CFA_OK },
2033 [CFA_WRITE_MULTI_WO_ERASE] = { cmd_write_multiple, CFA_OK },
2034 [WIN_STANDBYNOW1] = { cmd_nop, HD_CFA_OK },
2035 [WIN_IDLEIMMEDIATE] = { cmd_nop, HD_CFA_OK },
2036 [WIN_STANDBY] = { cmd_nop, HD_CFA_OK },
2037 [WIN_SETIDLE1] = { cmd_nop, HD_CFA_OK },
2038 [WIN_CHECKPOWERMODE1] = { cmd_check_power_mode, HD_CFA_OK | SET_DSC },
2039 [WIN_SLEEPNOW1] = { cmd_nop, HD_CFA_OK },
2040 [WIN_FLUSH_CACHE] = { cmd_flush_cache, ALL_OK },
2041 [WIN_FLUSH_CACHE_EXT] = { cmd_flush_cache, HD_CFA_OK },
2042 [WIN_IDENTIFY] = { cmd_identify, ALL_OK },
2043 [WIN_SETFEATURES] = { cmd_set_features, ALL_OK | SET_DSC },
2044 [IBM_SENSE_CONDITION] = { cmd_ibm_sense_condition, CFA_OK | SET_DSC },
2045 [CFA_WEAR_LEVEL] = { cmd_cfa_erase_sectors, HD_CFA_OK | SET_DSC },
2046 [WIN_READ_NATIVE_MAX] = { cmd_read_native_max, HD_CFA_OK | SET_DSC },
2049 static bool ide_cmd_permitted(IDEState *s, uint32_t cmd)
2051 return cmd < ARRAY_SIZE(ide_cmd_table)
2052 && (ide_cmd_table[cmd].flags & (1u << s->drive_kind));
2055 void ide_exec_cmd(IDEBus *bus, uint32_t val)
2060 s = idebus_active_if(bus);
2061 trace_ide_exec_cmd(bus, s, val);
2063 /* ignore commands to non existent slave */
2064 if (s != bus->ifs && !s->blk) {
2068 /* Only RESET is allowed while BSY and/or DRQ are set,
2069 * and only to ATAPI devices. */
2070 if (s->status & (BUSY_STAT|DRQ_STAT)) {
2071 if (val != WIN_DEVICE_RESET || s->drive_kind != IDE_CD) {
2076 if (!ide_cmd_permitted(s, val)) {
2077 ide_abort_command(s);
2078 ide_set_irq(s->bus);
2082 s->status = READY_STAT | BUSY_STAT;
2084 s->io_buffer_offset = 0;
2086 complete = ide_cmd_table[val].handler(s, val);
2088 s->status &= ~BUSY_STAT;
2089 assert(!!s->error == !!(s->status & ERR_STAT));
2091 if ((ide_cmd_table[val].flags & SET_DSC) && !s->error) {
2092 s->status |= SEEK_STAT;
2096 ide_set_irq(s->bus);
2100 /* IOport [R]ead [R]egisters */
2101 enum ATA_IOPORT_RR {
2102 ATA_IOPORT_RR_DATA = 0,
2103 ATA_IOPORT_RR_ERROR = 1,
2104 ATA_IOPORT_RR_SECTOR_COUNT = 2,
2105 ATA_IOPORT_RR_SECTOR_NUMBER = 3,
2106 ATA_IOPORT_RR_CYLINDER_LOW = 4,
2107 ATA_IOPORT_RR_CYLINDER_HIGH = 5,
2108 ATA_IOPORT_RR_DEVICE_HEAD = 6,
2109 ATA_IOPORT_RR_STATUS = 7,
2110 ATA_IOPORT_RR_NUM_REGISTERS,
2113 const char *ATA_IOPORT_RR_lookup[ATA_IOPORT_RR_NUM_REGISTERS] = {
2114 [ATA_IOPORT_RR_DATA] = "Data",
2115 [ATA_IOPORT_RR_ERROR] = "Error",
2116 [ATA_IOPORT_RR_SECTOR_COUNT] = "Sector Count",
2117 [ATA_IOPORT_RR_SECTOR_NUMBER] = "Sector Number",
2118 [ATA_IOPORT_RR_CYLINDER_LOW] = "Cylinder Low",
2119 [ATA_IOPORT_RR_CYLINDER_HIGH] = "Cylinder High",
2120 [ATA_IOPORT_RR_DEVICE_HEAD] = "Device/Head",
2121 [ATA_IOPORT_RR_STATUS] = "Status"
2124 uint32_t ide_ioport_read(void *opaque, uint32_t addr)
2126 IDEBus *bus = opaque;
2127 IDEState *s = idebus_active_if(bus);
2132 /* FIXME: HOB readback uses bit 7, but it's always set right now */
2133 //hob = s->select & (1 << 7);
2136 case ATA_IOPORT_RR_DATA:
2139 case ATA_IOPORT_RR_ERROR:
2140 if ((!bus->ifs[0].blk && !bus->ifs[1].blk) ||
2141 (s != bus->ifs && !s->blk)) {
2146 ret = s->hob_feature;
2149 case ATA_IOPORT_RR_SECTOR_COUNT:
2150 if (!bus->ifs[0].blk && !bus->ifs[1].blk) {
2153 ret = s->nsector & 0xff;
2155 ret = s->hob_nsector;
2158 case ATA_IOPORT_RR_SECTOR_NUMBER:
2159 if (!bus->ifs[0].blk && !bus->ifs[1].blk) {
2164 ret = s->hob_sector;
2167 case ATA_IOPORT_RR_CYLINDER_LOW:
2168 if (!bus->ifs[0].blk && !bus->ifs[1].blk) {
2176 case ATA_IOPORT_RR_CYLINDER_HIGH:
2177 if (!bus->ifs[0].blk && !bus->ifs[1].blk) {
2185 case ATA_IOPORT_RR_DEVICE_HEAD:
2186 if (!bus->ifs[0].blk && !bus->ifs[1].blk) {
2193 case ATA_IOPORT_RR_STATUS:
2194 if ((!bus->ifs[0].blk && !bus->ifs[1].blk) ||
2195 (s != bus->ifs && !s->blk)) {
2200 qemu_irq_lower(bus->irq);
2204 trace_ide_ioport_read(addr, ATA_IOPORT_RR_lookup[reg_num], ret, bus, s);
2208 uint32_t ide_status_read(void *opaque, uint32_t addr)
2210 IDEBus *bus = opaque;
2211 IDEState *s = idebus_active_if(bus);
2214 if ((!bus->ifs[0].blk && !bus->ifs[1].blk) ||
2215 (s != bus->ifs && !s->blk)) {
2221 trace_ide_status_read(addr, ret, bus, s);
2225 void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
2227 IDEBus *bus = opaque;
2231 trace_ide_cmd_write(addr, val, bus);
2233 /* common for both drives */
2234 if (!(bus->cmd & IDE_CMD_RESET) &&
2235 (val & IDE_CMD_RESET)) {
2236 /* reset low to high */
2237 for(i = 0;i < 2; i++) {
2239 s->status = BUSY_STAT | SEEK_STAT;
2242 } else if ((bus->cmd & IDE_CMD_RESET) &&
2243 !(val & IDE_CMD_RESET)) {
2245 for(i = 0;i < 2; i++) {
2247 if (s->drive_kind == IDE_CD)
2248 s->status = 0x00; /* NOTE: READY is _not_ set */
2250 s->status = READY_STAT | SEEK_STAT;
2251 ide_set_signature(s);
2259 * Returns true if the running PIO transfer is a PIO out (i.e. data is
2260 * transferred from the device to the guest), false if it's a PIO in
2262 static bool ide_is_pio_out(IDEState *s)
2264 if (s->end_transfer_func == ide_sector_write ||
2265 s->end_transfer_func == ide_atapi_cmd) {
2267 } else if (s->end_transfer_func == ide_sector_read ||
2268 s->end_transfer_func == ide_transfer_stop ||
2269 s->end_transfer_func == ide_atapi_cmd_reply_end ||
2270 s->end_transfer_func == ide_dummy_transfer_stop) {
2277 void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
2279 IDEBus *bus = opaque;
2280 IDEState *s = idebus_active_if(bus);
2283 trace_ide_data_writew(addr, val, bus, s);
2285 /* PIO data access allowed only when DRQ bit is set. The result of a write
2286 * during PIO out is indeterminate, just ignore it. */
2287 if (!(s->status & DRQ_STAT) || ide_is_pio_out(s)) {
2292 if (p + 2 > s->data_end) {
2296 *(uint16_t *)p = le16_to_cpu(val);
2299 if (p >= s->data_end) {
2300 s->status &= ~DRQ_STAT;
2301 s->end_transfer_func(s);
2305 uint32_t ide_data_readw(void *opaque, uint32_t addr)
2307 IDEBus *bus = opaque;
2308 IDEState *s = idebus_active_if(bus);
2312 /* PIO data access allowed only when DRQ bit is set. The result of a read
2313 * during PIO in is indeterminate, return 0 and don't move forward. */
2314 if (!(s->status & DRQ_STAT) || !ide_is_pio_out(s)) {
2319 if (p + 2 > s->data_end) {
2323 ret = cpu_to_le16(*(uint16_t *)p);
2326 if (p >= s->data_end) {
2327 s->status &= ~DRQ_STAT;
2328 s->end_transfer_func(s);
2331 trace_ide_data_readw(addr, ret, bus, s);
2335 void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
2337 IDEBus *bus = opaque;
2338 IDEState *s = idebus_active_if(bus);
2341 trace_ide_data_writel(addr, val, bus, s);
2343 /* PIO data access allowed only when DRQ bit is set. The result of a write
2344 * during PIO out is indeterminate, just ignore it. */
2345 if (!(s->status & DRQ_STAT) || ide_is_pio_out(s)) {
2350 if (p + 4 > s->data_end) {
2354 *(uint32_t *)p = le32_to_cpu(val);
2357 if (p >= s->data_end) {
2358 s->status &= ~DRQ_STAT;
2359 s->end_transfer_func(s);
2363 uint32_t ide_data_readl(void *opaque, uint32_t addr)
2365 IDEBus *bus = opaque;
2366 IDEState *s = idebus_active_if(bus);
2370 /* PIO data access allowed only when DRQ bit is set. The result of a read
2371 * during PIO in is indeterminate, return 0 and don't move forward. */
2372 if (!(s->status & DRQ_STAT) || !ide_is_pio_out(s)) {
2378 if (p + 4 > s->data_end) {
2382 ret = cpu_to_le32(*(uint32_t *)p);
2385 if (p >= s->data_end) {
2386 s->status &= ~DRQ_STAT;
2387 s->end_transfer_func(s);
2391 trace_ide_data_readl(addr, ret, bus, s);
2395 static void ide_dummy_transfer_stop(IDEState *s)
2397 s->data_ptr = s->io_buffer;
2398 s->data_end = s->io_buffer;
2399 s->io_buffer[0] = 0xff;
2400 s->io_buffer[1] = 0xff;
2401 s->io_buffer[2] = 0xff;
2402 s->io_buffer[3] = 0xff;
2405 void ide_bus_reset(IDEBus *bus)
2409 ide_reset(&bus->ifs[0]);
2410 ide_reset(&bus->ifs[1]);
2413 /* pending async DMA */
2414 if (bus->dma->aiocb) {
2415 trace_ide_bus_reset_aio();
2416 blk_aio_cancel(bus->dma->aiocb);
2417 bus->dma->aiocb = NULL;
2420 /* reset dma provider too */
2421 if (bus->dma->ops->reset) {
2422 bus->dma->ops->reset(bus->dma);
2426 static bool ide_cd_is_tray_open(void *opaque)
2428 return ((IDEState *)opaque)->tray_open;
2431 static bool ide_cd_is_medium_locked(void *opaque)
2433 return ((IDEState *)opaque)->tray_locked;
2436 static void ide_resize_cb(void *opaque)
2438 IDEState *s = opaque;
2439 uint64_t nb_sectors;
2441 if (!s->identify_set) {
2445 blk_get_geometry(s->blk, &nb_sectors);
2446 s->nb_sectors = nb_sectors;
2448 /* Update the identify data buffer. */
2449 if (s->drive_kind == IDE_CFATA) {
2450 ide_cfata_identify_size(s);
2452 /* IDE_CD uses a different set of callbacks entirely. */
2453 assert(s->drive_kind != IDE_CD);
2454 ide_identify_size(s);
2458 static const BlockDevOps ide_cd_block_ops = {
2459 .change_media_cb = ide_cd_change_cb,
2460 .eject_request_cb = ide_cd_eject_request_cb,
2461 .is_tray_open = ide_cd_is_tray_open,
2462 .is_medium_locked = ide_cd_is_medium_locked,
2465 static const BlockDevOps ide_hd_block_ops = {
2466 .resize_cb = ide_resize_cb,
2469 int ide_init_drive(IDEState *s, BlockBackend *blk, IDEDriveKind kind,
2470 const char *version, const char *serial, const char *model,
2472 uint32_t cylinders, uint32_t heads, uint32_t secs,
2473 int chs_trans, Error **errp)
2475 uint64_t nb_sectors;
2478 s->drive_kind = kind;
2480 blk_get_geometry(blk, &nb_sectors);
2481 s->cylinders = cylinders;
2484 s->chs_trans = chs_trans;
2485 s->nb_sectors = nb_sectors;
2487 /* The SMART values should be preserved across power cycles
2489 s->smart_enabled = 1;
2490 s->smart_autosave = 1;
2491 s->smart_errors = 0;
2492 s->smart_selftest_count = 0;
2493 if (kind == IDE_CD) {
2494 blk_set_dev_ops(blk, &ide_cd_block_ops, s);
2495 blk_set_guest_block_size(blk, 2048);
2497 if (!blk_is_inserted(s->blk)) {
2498 error_setg(errp, "Device needs media, but drive is empty");
2501 if (blk_is_read_only(blk)) {
2502 error_setg(errp, "Can't use a read-only drive");
2505 blk_set_dev_ops(blk, &ide_hd_block_ops, s);
2508 pstrcpy(s->drive_serial_str, sizeof(s->drive_serial_str), serial);
2510 snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
2511 "QM%05d", s->drive_serial);
2514 pstrcpy(s->drive_model_str, sizeof(s->drive_model_str), model);
2518 strcpy(s->drive_model_str, "QEMU DVD-ROM");
2521 strcpy(s->drive_model_str, "QEMU MICRODRIVE");
2524 strcpy(s->drive_model_str, "QEMU HARDDISK");
2530 pstrcpy(s->version, sizeof(s->version), version);
2532 pstrcpy(s->version, sizeof(s->version), qemu_hw_version());
2536 blk_iostatus_enable(blk);
2540 static void ide_init1(IDEBus *bus, int unit)
2542 static int drive_serial = 1;
2543 IDEState *s = &bus->ifs[unit];
2547 s->drive_serial = drive_serial++;
2548 /* we need at least 2k alignment for accessing CDROMs using O_DIRECT */
2549 s->io_buffer_total_len = IDE_DMA_BUF_SECTORS*512 + 4;
2550 s->io_buffer = qemu_memalign(2048, s->io_buffer_total_len);
2551 memset(s->io_buffer, 0, s->io_buffer_total_len);
2553 s->smart_selftest_data = blk_blockalign(s->blk, 512);
2554 memset(s->smart_selftest_data, 0, 512);
2556 s->sector_write_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2557 ide_sector_write_timer_cb, s);
2560 static int ide_nop_int(IDEDMA *dma, int x)
2565 static void ide_nop(IDEDMA *dma)
2569 static int32_t ide_nop_int32(IDEDMA *dma, int32_t l)
2574 static const IDEDMAOps ide_dma_nop_ops = {
2575 .prepare_buf = ide_nop_int32,
2576 .restart_dma = ide_nop,
2577 .rw_buf = ide_nop_int,
2580 static void ide_restart_dma(IDEState *s, enum ide_dma_cmd dma_cmd)
2582 s->unit = s->bus->retry_unit;
2583 ide_set_sector(s, s->bus->retry_sector_num);
2584 s->nsector = s->bus->retry_nsector;
2585 s->bus->dma->ops->restart_dma(s->bus->dma);
2586 s->io_buffer_size = 0;
2587 s->dma_cmd = dma_cmd;
2588 ide_start_dma(s, ide_dma_cb);
2591 static void ide_restart_bh(void *opaque)
2593 IDEBus *bus = opaque;
2598 qemu_bh_delete(bus->bh);
2601 error_status = bus->error_status;
2602 if (bus->error_status == 0) {
2606 s = idebus_active_if(bus);
2607 is_read = (bus->error_status & IDE_RETRY_READ) != 0;
2609 /* The error status must be cleared before resubmitting the request: The
2610 * request may fail again, and this case can only be distinguished if the
2611 * called function can set a new error status. */
2612 bus->error_status = 0;
2614 /* The HBA has generically asked to be kicked on retry */
2615 if (error_status & IDE_RETRY_HBA) {
2616 if (s->bus->dma->ops->restart) {
2617 s->bus->dma->ops->restart(s->bus->dma);
2619 } else if (IS_IDE_RETRY_DMA(error_status)) {
2620 if (error_status & IDE_RETRY_TRIM) {
2621 ide_restart_dma(s, IDE_DMA_TRIM);
2623 ide_restart_dma(s, is_read ? IDE_DMA_READ : IDE_DMA_WRITE);
2625 } else if (IS_IDE_RETRY_PIO(error_status)) {
2629 ide_sector_write(s);
2631 } else if (error_status & IDE_RETRY_FLUSH) {
2633 } else if (IS_IDE_RETRY_ATAPI(error_status)) {
2634 assert(s->end_transfer_func == ide_atapi_cmd);
2635 ide_atapi_dma_restart(s);
2641 static void ide_restart_cb(void *opaque, int running, RunState state)
2643 IDEBus *bus = opaque;
2649 bus->bh = qemu_bh_new(ide_restart_bh, bus);
2650 qemu_bh_schedule(bus->bh);
2654 void ide_register_restart_cb(IDEBus *bus)
2656 if (bus->dma->ops->restart_dma) {
2657 bus->vmstate = qemu_add_vm_change_state_handler(ide_restart_cb, bus);
2661 static IDEDMA ide_dma_nop = {
2662 .ops = &ide_dma_nop_ops,
2666 void ide_init2(IDEBus *bus, qemu_irq irq)
2670 for(i = 0; i < 2; i++) {
2672 ide_reset(&bus->ifs[i]);
2675 bus->dma = &ide_dma_nop;
2678 void ide_exit(IDEState *s)
2680 timer_del(s->sector_write_timer);
2681 timer_free(s->sector_write_timer);
2682 qemu_vfree(s->smart_selftest_data);
2683 qemu_vfree(s->io_buffer);
2686 static bool is_identify_set(void *opaque, int version_id)
2688 IDEState *s = opaque;
2690 return s->identify_set != 0;
2693 static EndTransferFunc* transfer_end_table[] = {
2697 ide_atapi_cmd_reply_end,
2699 ide_dummy_transfer_stop,
2702 static int transfer_end_table_idx(EndTransferFunc *fn)
2706 for (i = 0; i < ARRAY_SIZE(transfer_end_table); i++)
2707 if (transfer_end_table[i] == fn)
2713 static int ide_drive_post_load(void *opaque, int version_id)
2715 IDEState *s = opaque;
2717 if (s->blk && s->identify_set) {
2718 blk_set_enable_write_cache(s->blk, !!(s->identify_data[85] & (1 << 5)));
2723 static int ide_drive_pio_post_load(void *opaque, int version_id)
2725 IDEState *s = opaque;
2727 if (s->end_transfer_fn_idx >= ARRAY_SIZE(transfer_end_table)) {
2730 s->end_transfer_func = transfer_end_table[s->end_transfer_fn_idx];
2731 s->data_ptr = s->io_buffer + s->cur_io_buffer_offset;
2732 s->data_end = s->data_ptr + s->cur_io_buffer_len;
2733 s->atapi_dma = s->feature & 1; /* as per cmd_packet */
2738 static int ide_drive_pio_pre_save(void *opaque)
2740 IDEState *s = opaque;
2743 s->cur_io_buffer_offset = s->data_ptr - s->io_buffer;
2744 s->cur_io_buffer_len = s->data_end - s->data_ptr;
2746 idx = transfer_end_table_idx(s->end_transfer_func);
2748 fprintf(stderr, "%s: invalid end_transfer_func for DRQ_STAT\n",
2750 s->end_transfer_fn_idx = 2;
2752 s->end_transfer_fn_idx = idx;
2758 static bool ide_drive_pio_state_needed(void *opaque)
2760 IDEState *s = opaque;
2762 return ((s->status & DRQ_STAT) != 0)
2763 || (s->bus->error_status & IDE_RETRY_PIO);
2766 static bool ide_tray_state_needed(void *opaque)
2768 IDEState *s = opaque;
2770 return s->tray_open || s->tray_locked;
2773 static bool ide_atapi_gesn_needed(void *opaque)
2775 IDEState *s = opaque;
2777 return s->events.new_media || s->events.eject_request;
2780 static bool ide_error_needed(void *opaque)
2782 IDEBus *bus = opaque;
2784 return (bus->error_status != 0);
2787 /* Fields for GET_EVENT_STATUS_NOTIFICATION ATAPI command */
2788 static const VMStateDescription vmstate_ide_atapi_gesn_state = {
2789 .name ="ide_drive/atapi/gesn_state",
2791 .minimum_version_id = 1,
2792 .needed = ide_atapi_gesn_needed,
2793 .fields = (VMStateField[]) {
2794 VMSTATE_BOOL(events.new_media, IDEState),
2795 VMSTATE_BOOL(events.eject_request, IDEState),
2796 VMSTATE_END_OF_LIST()
2800 static const VMStateDescription vmstate_ide_tray_state = {
2801 .name = "ide_drive/tray_state",
2803 .minimum_version_id = 1,
2804 .needed = ide_tray_state_needed,
2805 .fields = (VMStateField[]) {
2806 VMSTATE_BOOL(tray_open, IDEState),
2807 VMSTATE_BOOL(tray_locked, IDEState),
2808 VMSTATE_END_OF_LIST()
2812 static const VMStateDescription vmstate_ide_drive_pio_state = {
2813 .name = "ide_drive/pio_state",
2815 .minimum_version_id = 1,
2816 .pre_save = ide_drive_pio_pre_save,
2817 .post_load = ide_drive_pio_post_load,
2818 .needed = ide_drive_pio_state_needed,
2819 .fields = (VMStateField[]) {
2820 VMSTATE_INT32(req_nb_sectors, IDEState),
2821 VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1,
2822 vmstate_info_uint8, uint8_t),
2823 VMSTATE_INT32(cur_io_buffer_offset, IDEState),
2824 VMSTATE_INT32(cur_io_buffer_len, IDEState),
2825 VMSTATE_UINT8(end_transfer_fn_idx, IDEState),
2826 VMSTATE_INT32(elementary_transfer_size, IDEState),
2827 VMSTATE_INT32(packet_transfer_size, IDEState),
2828 VMSTATE_END_OF_LIST()
2832 const VMStateDescription vmstate_ide_drive = {
2833 .name = "ide_drive",
2835 .minimum_version_id = 0,
2836 .post_load = ide_drive_post_load,
2837 .fields = (VMStateField[]) {
2838 VMSTATE_INT32(mult_sectors, IDEState),
2839 VMSTATE_INT32(identify_set, IDEState),
2840 VMSTATE_BUFFER_TEST(identify_data, IDEState, is_identify_set),
2841 VMSTATE_UINT8(feature, IDEState),
2842 VMSTATE_UINT8(error, IDEState),
2843 VMSTATE_UINT32(nsector, IDEState),
2844 VMSTATE_UINT8(sector, IDEState),
2845 VMSTATE_UINT8(lcyl, IDEState),
2846 VMSTATE_UINT8(hcyl, IDEState),
2847 VMSTATE_UINT8(hob_feature, IDEState),
2848 VMSTATE_UINT8(hob_sector, IDEState),
2849 VMSTATE_UINT8(hob_nsector, IDEState),
2850 VMSTATE_UINT8(hob_lcyl, IDEState),
2851 VMSTATE_UINT8(hob_hcyl, IDEState),
2852 VMSTATE_UINT8(select, IDEState),
2853 VMSTATE_UINT8(status, IDEState),
2854 VMSTATE_UINT8(lba48, IDEState),
2855 VMSTATE_UINT8(sense_key, IDEState),
2856 VMSTATE_UINT8(asc, IDEState),
2857 VMSTATE_UINT8_V(cdrom_changed, IDEState, 3),
2858 VMSTATE_END_OF_LIST()
2860 .subsections = (const VMStateDescription*[]) {
2861 &vmstate_ide_drive_pio_state,
2862 &vmstate_ide_tray_state,
2863 &vmstate_ide_atapi_gesn_state,
2868 static const VMStateDescription vmstate_ide_error_status = {
2869 .name ="ide_bus/error",
2871 .minimum_version_id = 1,
2872 .needed = ide_error_needed,
2873 .fields = (VMStateField[]) {
2874 VMSTATE_INT32(error_status, IDEBus),
2875 VMSTATE_INT64_V(retry_sector_num, IDEBus, 2),
2876 VMSTATE_UINT32_V(retry_nsector, IDEBus, 2),
2877 VMSTATE_UINT8_V(retry_unit, IDEBus, 2),
2878 VMSTATE_END_OF_LIST()
2882 const VMStateDescription vmstate_ide_bus = {
2885 .minimum_version_id = 1,
2886 .fields = (VMStateField[]) {
2887 VMSTATE_UINT8(cmd, IDEBus),
2888 VMSTATE_UINT8(unit, IDEBus),
2889 VMSTATE_END_OF_LIST()
2891 .subsections = (const VMStateDescription*[]) {
2892 &vmstate_ide_error_status,
2897 void ide_drive_get(DriveInfo **hd, int n)
2901 for (i = 0; i < n; i++) {
2902 hd[i] = drive_get_by_index(IF_IDE, i);