]>
Commit | Line | Data |
---|---|---|
5391d806 | 1 | /* |
38cdea7c | 2 | * QEMU IDE disk and CD/DVD-ROM Emulator |
5fafdf24 | 3 | * |
5391d806 | 4 | * Copyright (c) 2003 Fabrice Bellard |
201a51fc | 5 | * Copyright (c) 2006 Openedhand Ltd. |
5fafdf24 | 6 | * |
5391d806 FB |
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: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
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 | |
23 | * THE SOFTWARE. | |
24 | */ | |
59f2a787 | 25 | #include <hw/hw.h> |
0d09e41a | 26 | #include <hw/i386/pc.h> |
a2cb15b0 | 27 | #include <hw/pci/pci.h> |
0d09e41a | 28 | #include <hw/isa/isa.h> |
1de7afc9 PB |
29 | #include "qemu/error-report.h" |
30 | #include "qemu/timer.h" | |
9c17d615 PB |
31 | #include "sysemu/sysemu.h" |
32 | #include "sysemu/dma.h" | |
0d09e41a | 33 | #include "hw/block/block.h" |
9c17d615 | 34 | #include "sysemu/blockdev.h" |
59f2a787 GH |
35 | |
36 | #include <hw/ide/internal.h> | |
e8b54394 | 37 | |
b93af93d BW |
38 | /* These values were based on a Seagate ST3500418AS but have been modified |
39 | to make more sense in QEMU */ | |
40 | static const int smart_attributes[][12] = { | |
41 | /* id, flags, hflags, val, wrst, raw (6 bytes), threshold */ | |
42 | /* raw read error rate*/ | |
43 | { 0x01, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06}, | |
44 | /* spin up */ | |
45 | { 0x03, 0x03, 0x00, 0x64, 0x64, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | |
46 | /* start stop count */ | |
47 | { 0x04, 0x02, 0x00, 0x64, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14}, | |
48 | /* remapped sectors */ | |
49 | { 0x05, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24}, | |
50 | /* power on hours */ | |
51 | { 0x09, 0x03, 0x00, 0x64, 0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | |
52 | /* power cycle count */ | |
53 | { 0x0c, 0x03, 0x00, 0x64, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | |
54 | /* airflow-temperature-celsius */ | |
55 | { 190, 0x03, 0x00, 0x45, 0x45, 0x1f, 0x00, 0x1f, 0x1f, 0x00, 0x00, 0x32}, | |
e8b54394 BW |
56 | }; |
57 | ||
ce4b6522 | 58 | static int ide_handle_rw_error(IDEState *s, int error, int op); |
40c4ed3f | 59 | static void ide_dummy_transfer_stop(IDEState *s); |
98087450 | 60 | |
5391d806 FB |
61 | static void padstr(char *str, const char *src, int len) |
62 | { | |
63 | int i, v; | |
64 | for(i = 0; i < len; i++) { | |
65 | if (*src) | |
66 | v = *src++; | |
67 | else | |
68 | v = ' '; | |
69b34976 | 69 | str[i^1] = v; |
5391d806 FB |
70 | } |
71 | } | |
72 | ||
67b915a5 FB |
73 | static void put_le16(uint16_t *p, unsigned int v) |
74 | { | |
0c4ad8dc | 75 | *p = cpu_to_le16(v); |
67b915a5 FB |
76 | } |
77 | ||
5391d806 FB |
78 | static void ide_identify(IDEState *s) |
79 | { | |
80 | uint16_t *p; | |
81 | unsigned int oldsize; | |
d353fb72 | 82 | IDEDevice *dev = s->unit ? s->bus->slave : s->bus->master; |
5391d806 | 83 | |
94458802 FB |
84 | if (s->identify_set) { |
85 | memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data)); | |
86 | return; | |
87 | } | |
88 | ||
5391d806 FB |
89 | memset(s->io_buffer, 0, 512); |
90 | p = (uint16_t *)s->io_buffer; | |
67b915a5 | 91 | put_le16(p + 0, 0x0040); |
5fafdf24 | 92 | put_le16(p + 1, s->cylinders); |
67b915a5 FB |
93 | put_le16(p + 3, s->heads); |
94 | put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */ | |
95 | put_le16(p + 5, 512); /* XXX: retired, remove ? */ | |
5fafdf24 | 96 | put_le16(p + 6, s->sectors); |
fa879c64 | 97 | padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */ |
67b915a5 FB |
98 | put_le16(p + 20, 3); /* XXX: retired, remove ? */ |
99 | put_le16(p + 21, 512); /* cache size in sectors */ | |
100 | put_le16(p + 22, 4); /* ecc bytes */ | |
47c06340 | 101 | padstr((char *)(p + 23), s->version, 8); /* firmware version */ |
27e0c9a1 | 102 | padstr((char *)(p + 27), s->drive_model_str, 40); /* model */ |
3b46e624 | 103 | #if MAX_MULT_SECTORS > 1 |
67b915a5 | 104 | put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS); |
5391d806 | 105 | #endif |
67b915a5 | 106 | put_le16(p + 48, 1); /* dword I/O */ |
94458802 | 107 | put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */ |
67b915a5 FB |
108 | put_le16(p + 51, 0x200); /* PIO transfer cycle */ |
109 | put_le16(p + 52, 0x200); /* DMA transfer cycle */ | |
94458802 | 110 | put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */ |
67b915a5 FB |
111 | put_le16(p + 54, s->cylinders); |
112 | put_le16(p + 55, s->heads); | |
113 | put_le16(p + 56, s->sectors); | |
5391d806 | 114 | oldsize = s->cylinders * s->heads * s->sectors; |
67b915a5 FB |
115 | put_le16(p + 57, oldsize); |
116 | put_le16(p + 58, oldsize >> 16); | |
5391d806 | 117 | if (s->mult_sectors) |
67b915a5 FB |
118 | put_le16(p + 59, 0x100 | s->mult_sectors); |
119 | put_le16(p + 60, s->nb_sectors); | |
120 | put_le16(p + 61, s->nb_sectors >> 16); | |
d1b5c20d | 121 | put_le16(p + 62, 0x07); /* single word dma0-2 supported */ |
94458802 | 122 | put_le16(p + 63, 0x07); /* mdma0-2 supported */ |
79d1d331 | 123 | put_le16(p + 64, 0x03); /* pio3-4 supported */ |
94458802 FB |
124 | put_le16(p + 65, 120); |
125 | put_le16(p + 66, 120); | |
126 | put_le16(p + 67, 120); | |
127 | put_le16(p + 68, 120); | |
d353fb72 CH |
128 | if (dev && dev->conf.discard_granularity) { |
129 | put_le16(p + 69, (1 << 14)); /* determinate TRIM behavior */ | |
130 | } | |
ccf0fd8b RE |
131 | |
132 | if (s->ncq_queues) { | |
133 | put_le16(p + 75, s->ncq_queues - 1); | |
134 | /* NCQ supported */ | |
135 | put_le16(p + 76, (1 << 8)); | |
136 | } | |
137 | ||
94458802 FB |
138 | put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */ |
139 | put_le16(p + 81, 0x16); /* conforms to ata5 */ | |
a58b8d54 CH |
140 | /* 14=NOP supported, 5=WCACHE supported, 0=SMART supported */ |
141 | put_le16(p + 82, (1 << 14) | (1 << 5) | 1); | |
c2ff060f FB |
142 | /* 13=flush_cache_ext,12=flush_cache,10=lba48 */ |
143 | put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10)); | |
95ebda85 FB |
144 | /* 14=set to 1, 8=has WWN, 1=SMART self test, 0=SMART error logging */ |
145 | if (s->wwn) { | |
146 | put_le16(p + 84, (1 << 14) | (1 << 8) | 0); | |
147 | } else { | |
148 | put_le16(p + 84, (1 << 14) | 0); | |
149 | } | |
e900a7b7 CH |
150 | /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */ |
151 | if (bdrv_enable_write_cache(s->bs)) | |
152 | put_le16(p + 85, (1 << 14) | (1 << 5) | 1); | |
153 | else | |
154 | put_le16(p + 85, (1 << 14) | 1); | |
c2ff060f | 155 | /* 13=flush_cache_ext,12=flush_cache,10=lba48 */ |
2844bdd9 | 156 | put_le16(p + 86, (1 << 13) | (1 <<12) | (1 << 10)); |
95ebda85 FB |
157 | /* 14=set to 1, 8=has WWN, 1=SMART self test, 0=SMART error logging */ |
158 | if (s->wwn) { | |
159 | put_le16(p + 87, (1 << 14) | (1 << 8) | 0); | |
160 | } else { | |
161 | put_le16(p + 87, (1 << 14) | 0); | |
162 | } | |
94458802 FB |
163 | put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */ |
164 | put_le16(p + 93, 1 | (1 << 14) | 0x2000); | |
c2ff060f FB |
165 | put_le16(p + 100, s->nb_sectors); |
166 | put_le16(p + 101, s->nb_sectors >> 16); | |
167 | put_le16(p + 102, s->nb_sectors >> 32); | |
168 | put_le16(p + 103, s->nb_sectors >> 48); | |
d353fb72 | 169 | |
57dac7ef MA |
170 | if (dev && dev->conf.physical_block_size) |
171 | put_le16(p + 106, 0x6000 | get_physical_block_exp(&dev->conf)); | |
95ebda85 FB |
172 | if (s->wwn) { |
173 | /* LE 16-bit words 111-108 contain 64-bit World Wide Name */ | |
174 | put_le16(p + 108, s->wwn >> 48); | |
175 | put_le16(p + 109, s->wwn >> 32); | |
176 | put_le16(p + 110, s->wwn >> 16); | |
177 | put_le16(p + 111, s->wwn); | |
178 | } | |
d353fb72 CH |
179 | if (dev && dev->conf.discard_granularity) { |
180 | put_le16(p + 169, 1); /* TRIM support */ | |
181 | } | |
94458802 FB |
182 | |
183 | memcpy(s->identify_data, p, sizeof(s->identify_data)); | |
184 | s->identify_set = 1; | |
5391d806 FB |
185 | } |
186 | ||
187 | static void ide_atapi_identify(IDEState *s) | |
188 | { | |
189 | uint16_t *p; | |
190 | ||
94458802 FB |
191 | if (s->identify_set) { |
192 | memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data)); | |
193 | return; | |
194 | } | |
195 | ||
5391d806 FB |
196 | memset(s->io_buffer, 0, 512); |
197 | p = (uint16_t *)s->io_buffer; | |
198 | /* Removable CDROM, 50us response, 12 byte packets */ | |
67b915a5 | 199 | put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0)); |
fa879c64 | 200 | padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */ |
67b915a5 FB |
201 | put_le16(p + 20, 3); /* buffer type */ |
202 | put_le16(p + 21, 512); /* cache size in sectors */ | |
203 | put_le16(p + 22, 4); /* ecc bytes */ | |
47c06340 | 204 | padstr((char *)(p + 23), s->version, 8); /* firmware version */ |
27e0c9a1 | 205 | padstr((char *)(p + 27), s->drive_model_str, 40); /* model */ |
67b915a5 | 206 | put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */ |
8ccad811 FB |
207 | #ifdef USE_DMA_CDROM |
208 | put_le16(p + 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */ | |
209 | put_le16(p + 53, 7); /* words 64-70, 54-58, 88 valid */ | |
d1b5c20d | 210 | put_le16(p + 62, 7); /* single word dma0-2 supported */ |
8ccad811 | 211 | put_le16(p + 63, 7); /* mdma0-2 supported */ |
8ccad811 | 212 | #else |
67b915a5 FB |
213 | put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */ |
214 | put_le16(p + 53, 3); /* words 64-70, 54-58 valid */ | |
215 | put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */ | |
8ccad811 | 216 | #endif |
79d1d331 | 217 | put_le16(p + 64, 3); /* pio3-4 supported */ |
67b915a5 FB |
218 | put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */ |
219 | put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */ | |
220 | put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */ | |
221 | put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */ | |
94458802 | 222 | |
67b915a5 FB |
223 | put_le16(p + 71, 30); /* in ns */ |
224 | put_le16(p + 72, 30); /* in ns */ | |
5391d806 | 225 | |
1bdaa28d AG |
226 | if (s->ncq_queues) { |
227 | put_le16(p + 75, s->ncq_queues - 1); | |
228 | /* NCQ supported */ | |
229 | put_le16(p + 76, (1 << 8)); | |
230 | } | |
231 | ||
67b915a5 | 232 | put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */ |
8ccad811 FB |
233 | #ifdef USE_DMA_CDROM |
234 | put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */ | |
235 | #endif | |
94458802 FB |
236 | memcpy(s->identify_data, p, sizeof(s->identify_data)); |
237 | s->identify_set = 1; | |
5391d806 FB |
238 | } |
239 | ||
201a51fc AZ |
240 | static void ide_cfata_identify(IDEState *s) |
241 | { | |
242 | uint16_t *p; | |
243 | uint32_t cur_sec; | |
201a51fc AZ |
244 | |
245 | p = (uint16_t *) s->identify_data; | |
246 | if (s->identify_set) | |
247 | goto fill_buffer; | |
248 | ||
249 | memset(p, 0, sizeof(s->identify_data)); | |
250 | ||
251 | cur_sec = s->cylinders * s->heads * s->sectors; | |
252 | ||
253 | put_le16(p + 0, 0x848a); /* CF Storage Card signature */ | |
254 | put_le16(p + 1, s->cylinders); /* Default cylinders */ | |
255 | put_le16(p + 3, s->heads); /* Default heads */ | |
256 | put_le16(p + 6, s->sectors); /* Default sectors per track */ | |
257 | put_le16(p + 7, s->nb_sectors >> 16); /* Sectors per card */ | |
258 | put_le16(p + 8, s->nb_sectors); /* Sectors per card */ | |
fa879c64 | 259 | padstr((char *)(p + 10), s->drive_serial_str, 20); /* serial number */ |
201a51fc | 260 | put_le16(p + 22, 0x0004); /* ECC bytes */ |
47c06340 | 261 | padstr((char *) (p + 23), s->version, 8); /* Firmware Revision */ |
27e0c9a1 | 262 | padstr((char *) (p + 27), s->drive_model_str, 40);/* Model number */ |
201a51fc AZ |
263 | #if MAX_MULT_SECTORS > 1 |
264 | put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS); | |
265 | #else | |
266 | put_le16(p + 47, 0x0000); | |
267 | #endif | |
268 | put_le16(p + 49, 0x0f00); /* Capabilities */ | |
269 | put_le16(p + 51, 0x0002); /* PIO cycle timing mode */ | |
270 | put_le16(p + 52, 0x0001); /* DMA cycle timing mode */ | |
271 | put_le16(p + 53, 0x0003); /* Translation params valid */ | |
272 | put_le16(p + 54, s->cylinders); /* Current cylinders */ | |
273 | put_le16(p + 55, s->heads); /* Current heads */ | |
274 | put_le16(p + 56, s->sectors); /* Current sectors */ | |
275 | put_le16(p + 57, cur_sec); /* Current capacity */ | |
276 | put_le16(p + 58, cur_sec >> 16); /* Current capacity */ | |
277 | if (s->mult_sectors) /* Multiple sector setting */ | |
278 | put_le16(p + 59, 0x100 | s->mult_sectors); | |
279 | put_le16(p + 60, s->nb_sectors); /* Total LBA sectors */ | |
280 | put_le16(p + 61, s->nb_sectors >> 16); /* Total LBA sectors */ | |
281 | put_le16(p + 63, 0x0203); /* Multiword DMA capability */ | |
282 | put_le16(p + 64, 0x0001); /* Flow Control PIO support */ | |
283 | put_le16(p + 65, 0x0096); /* Min. Multiword DMA cycle */ | |
284 | put_le16(p + 66, 0x0096); /* Rec. Multiword DMA cycle */ | |
285 | put_le16(p + 68, 0x00b4); /* Min. PIO cycle time */ | |
286 | put_le16(p + 82, 0x400c); /* Command Set supported */ | |
287 | put_le16(p + 83, 0x7068); /* Command Set supported */ | |
288 | put_le16(p + 84, 0x4000); /* Features supported */ | |
289 | put_le16(p + 85, 0x000c); /* Command Set enabled */ | |
290 | put_le16(p + 86, 0x7044); /* Command Set enabled */ | |
291 | put_le16(p + 87, 0x4000); /* Features enabled */ | |
292 | put_le16(p + 91, 0x4060); /* Current APM level */ | |
293 | put_le16(p + 129, 0x0002); /* Current features option */ | |
294 | put_le16(p + 130, 0x0005); /* Reassigned sectors */ | |
295 | put_le16(p + 131, 0x0001); /* Initial power mode */ | |
296 | put_le16(p + 132, 0x0000); /* User signature */ | |
297 | put_le16(p + 160, 0x8100); /* Power requirement */ | |
298 | put_le16(p + 161, 0x8001); /* CF command set */ | |
299 | ||
300 | s->identify_set = 1; | |
301 | ||
302 | fill_buffer: | |
303 | memcpy(s->io_buffer, p, sizeof(s->identify_data)); | |
304 | } | |
305 | ||
5391d806 FB |
306 | static void ide_set_signature(IDEState *s) |
307 | { | |
308 | s->select &= 0xf0; /* clear head */ | |
309 | /* put signature */ | |
310 | s->nsector = 1; | |
311 | s->sector = 1; | |
cd8722bb | 312 | if (s->drive_kind == IDE_CD) { |
5391d806 FB |
313 | s->lcyl = 0x14; |
314 | s->hcyl = 0xeb; | |
315 | } else if (s->bs) { | |
316 | s->lcyl = 0; | |
317 | s->hcyl = 0; | |
318 | } else { | |
319 | s->lcyl = 0xff; | |
320 | s->hcyl = 0xff; | |
321 | } | |
322 | } | |
323 | ||
d353fb72 CH |
324 | typedef struct TrimAIOCB { |
325 | BlockDriverAIOCB common; | |
326 | QEMUBH *bh; | |
327 | int ret; | |
501378c3 PB |
328 | QEMUIOVector *qiov; |
329 | BlockDriverAIOCB *aiocb; | |
330 | int i, j; | |
d353fb72 CH |
331 | } TrimAIOCB; |
332 | ||
333 | static void trim_aio_cancel(BlockDriverAIOCB *acb) | |
334 | { | |
335 | TrimAIOCB *iocb = container_of(acb, TrimAIOCB, common); | |
336 | ||
501378c3 PB |
337 | /* Exit the loop in case bdrv_aio_cancel calls ide_issue_trim_cb again. */ |
338 | iocb->j = iocb->qiov->niov - 1; | |
339 | iocb->i = (iocb->qiov->iov[iocb->j].iov_len / 8) - 1; | |
340 | ||
341 | /* Tell ide_issue_trim_cb not to trigger the completion, too. */ | |
d353fb72 CH |
342 | qemu_bh_delete(iocb->bh); |
343 | iocb->bh = NULL; | |
501378c3 PB |
344 | |
345 | if (iocb->aiocb) { | |
346 | bdrv_aio_cancel(iocb->aiocb); | |
347 | } | |
d353fb72 CH |
348 | qemu_aio_release(iocb); |
349 | } | |
350 | ||
d7331bed | 351 | static const AIOCBInfo trim_aiocb_info = { |
d353fb72 CH |
352 | .aiocb_size = sizeof(TrimAIOCB), |
353 | .cancel = trim_aio_cancel, | |
354 | }; | |
355 | ||
356 | static void ide_trim_bh_cb(void *opaque) | |
357 | { | |
358 | TrimAIOCB *iocb = opaque; | |
359 | ||
360 | iocb->common.cb(iocb->common.opaque, iocb->ret); | |
361 | ||
362 | qemu_bh_delete(iocb->bh); | |
363 | iocb->bh = NULL; | |
d353fb72 CH |
364 | qemu_aio_release(iocb); |
365 | } | |
366 | ||
501378c3 PB |
367 | static void ide_issue_trim_cb(void *opaque, int ret) |
368 | { | |
369 | TrimAIOCB *iocb = opaque; | |
370 | if (ret >= 0) { | |
371 | while (iocb->j < iocb->qiov->niov) { | |
372 | int j = iocb->j; | |
373 | while (++iocb->i < iocb->qiov->iov[j].iov_len / 8) { | |
374 | int i = iocb->i; | |
375 | uint64_t *buffer = iocb->qiov->iov[j].iov_base; | |
376 | ||
377 | /* 6-byte LBA + 2-byte range per entry */ | |
378 | uint64_t entry = le64_to_cpu(buffer[i]); | |
379 | uint64_t sector = entry & 0x0000ffffffffffffULL; | |
380 | uint16_t count = entry >> 48; | |
381 | ||
382 | if (count == 0) { | |
383 | continue; | |
384 | } | |
385 | ||
386 | /* Got an entry! Submit and exit. */ | |
387 | iocb->aiocb = bdrv_aio_discard(iocb->common.bs, sector, count, | |
388 | ide_issue_trim_cb, opaque); | |
389 | return; | |
390 | } | |
391 | ||
392 | iocb->j++; | |
393 | iocb->i = -1; | |
394 | } | |
395 | } else { | |
396 | iocb->ret = ret; | |
397 | } | |
398 | ||
399 | iocb->aiocb = NULL; | |
400 | if (iocb->bh) { | |
401 | qemu_bh_schedule(iocb->bh); | |
402 | } | |
403 | } | |
404 | ||
d353fb72 CH |
405 | BlockDriverAIOCB *ide_issue_trim(BlockDriverState *bs, |
406 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
407 | BlockDriverCompletionFunc *cb, void *opaque) | |
408 | { | |
409 | TrimAIOCB *iocb; | |
d353fb72 | 410 | |
d7331bed | 411 | iocb = qemu_aio_get(&trim_aiocb_info, bs, cb, opaque); |
d353fb72 CH |
412 | iocb->bh = qemu_bh_new(ide_trim_bh_cb, iocb); |
413 | iocb->ret = 0; | |
501378c3 PB |
414 | iocb->qiov = qiov; |
415 | iocb->i = -1; | |
416 | iocb->j = 0; | |
417 | ide_issue_trim_cb(iocb, 0); | |
d353fb72 CH |
418 | return &iocb->common; |
419 | } | |
420 | ||
5391d806 FB |
421 | static inline void ide_abort_command(IDEState *s) |
422 | { | |
423 | s->status = READY_STAT | ERR_STAT; | |
424 | s->error = ABRT_ERR; | |
425 | } | |
426 | ||
5391d806 | 427 | /* prepare data transfer and tell what to do after */ |
33231e0e KW |
428 | void ide_transfer_start(IDEState *s, uint8_t *buf, int size, |
429 | EndTransferFunc *end_transfer_func) | |
5391d806 FB |
430 | { |
431 | s->end_transfer_func = end_transfer_func; | |
432 | s->data_ptr = buf; | |
433 | s->data_end = buf + size; | |
40a6238a | 434 | if (!(s->status & ERR_STAT)) { |
7603d156 | 435 | s->status |= DRQ_STAT; |
40a6238a AG |
436 | } |
437 | s->bus->dma->ops->start_transfer(s->bus->dma); | |
5391d806 FB |
438 | } |
439 | ||
33231e0e | 440 | void ide_transfer_stop(IDEState *s) |
5391d806 FB |
441 | { |
442 | s->end_transfer_func = ide_transfer_stop; | |
443 | s->data_ptr = s->io_buffer; | |
444 | s->data_end = s->io_buffer; | |
445 | s->status &= ~DRQ_STAT; | |
446 | } | |
447 | ||
356721ae | 448 | int64_t ide_get_sector(IDEState *s) |
5391d806 FB |
449 | { |
450 | int64_t sector_num; | |
451 | if (s->select & 0x40) { | |
452 | /* lba */ | |
c2ff060f FB |
453 | if (!s->lba48) { |
454 | sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) | | |
455 | (s->lcyl << 8) | s->sector; | |
456 | } else { | |
457 | sector_num = ((int64_t)s->hob_hcyl << 40) | | |
458 | ((int64_t) s->hob_lcyl << 32) | | |
459 | ((int64_t) s->hob_sector << 24) | | |
460 | ((int64_t) s->hcyl << 16) | | |
461 | ((int64_t) s->lcyl << 8) | s->sector; | |
462 | } | |
5391d806 FB |
463 | } else { |
464 | sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors + | |
c2ff060f | 465 | (s->select & 0x0f) * s->sectors + (s->sector - 1); |
5391d806 FB |
466 | } |
467 | return sector_num; | |
468 | } | |
469 | ||
356721ae | 470 | void ide_set_sector(IDEState *s, int64_t sector_num) |
5391d806 FB |
471 | { |
472 | unsigned int cyl, r; | |
473 | if (s->select & 0x40) { | |
c2ff060f FB |
474 | if (!s->lba48) { |
475 | s->select = (s->select & 0xf0) | (sector_num >> 24); | |
476 | s->hcyl = (sector_num >> 16); | |
477 | s->lcyl = (sector_num >> 8); | |
478 | s->sector = (sector_num); | |
479 | } else { | |
480 | s->sector = sector_num; | |
481 | s->lcyl = sector_num >> 8; | |
482 | s->hcyl = sector_num >> 16; | |
483 | s->hob_sector = sector_num >> 24; | |
484 | s->hob_lcyl = sector_num >> 32; | |
485 | s->hob_hcyl = sector_num >> 40; | |
486 | } | |
5391d806 FB |
487 | } else { |
488 | cyl = sector_num / (s->heads * s->sectors); | |
489 | r = sector_num % (s->heads * s->sectors); | |
490 | s->hcyl = cyl >> 8; | |
491 | s->lcyl = cyl; | |
1b8eb456 | 492 | s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f); |
5391d806 FB |
493 | s->sector = (r % s->sectors) + 1; |
494 | } | |
495 | } | |
496 | ||
e162cfb0 AZ |
497 | static void ide_rw_error(IDEState *s) { |
498 | ide_abort_command(s); | |
9cdd03a7 | 499 | ide_set_irq(s->bus); |
e162cfb0 AZ |
500 | } |
501 | ||
58ac3211 MA |
502 | static bool ide_sect_range_ok(IDEState *s, |
503 | uint64_t sector, uint64_t nb_sectors) | |
504 | { | |
505 | uint64_t total_sectors; | |
506 | ||
507 | bdrv_get_geometry(s->bs, &total_sectors); | |
508 | if (sector > total_sectors || nb_sectors > total_sectors - sector) { | |
509 | return false; | |
510 | } | |
511 | return true; | |
512 | } | |
513 | ||
bef0fd59 SH |
514 | static void ide_sector_read_cb(void *opaque, int ret) |
515 | { | |
516 | IDEState *s = opaque; | |
517 | int n; | |
518 | ||
519 | s->pio_aiocb = NULL; | |
520 | s->status &= ~BUSY_STAT; | |
521 | ||
522 | bdrv_acct_done(s->bs, &s->acct); | |
523 | if (ret != 0) { | |
524 | if (ide_handle_rw_error(s, -ret, BM_STATUS_PIO_RETRY | | |
525 | BM_STATUS_RETRY_READ)) { | |
526 | return; | |
527 | } | |
528 | } | |
529 | ||
530 | n = s->nsector; | |
531 | if (n > s->req_nb_sectors) { | |
532 | n = s->req_nb_sectors; | |
533 | } | |
534 | ||
535 | /* Allow the guest to read the io_buffer */ | |
536 | ide_transfer_start(s, s->io_buffer, n * BDRV_SECTOR_SIZE, ide_sector_read); | |
537 | ||
538 | ide_set_irq(s->bus); | |
539 | ||
540 | ide_set_sector(s, ide_get_sector(s) + n); | |
541 | s->nsector -= n; | |
542 | } | |
543 | ||
40a6238a | 544 | void ide_sector_read(IDEState *s) |
5391d806 FB |
545 | { |
546 | int64_t sector_num; | |
bef0fd59 | 547 | int n; |
5391d806 FB |
548 | |
549 | s->status = READY_STAT | SEEK_STAT; | |
a136e5a8 | 550 | s->error = 0; /* not needed by IDE spec, but needed by Windows */ |
5391d806 FB |
551 | sector_num = ide_get_sector(s); |
552 | n = s->nsector; | |
bef0fd59 | 553 | |
5391d806 | 554 | if (n == 0) { |
5391d806 | 555 | ide_transfer_stop(s); |
bef0fd59 SH |
556 | return; |
557 | } | |
558 | ||
559 | s->status |= BUSY_STAT; | |
560 | ||
561 | if (n > s->req_nb_sectors) { | |
562 | n = s->req_nb_sectors; | |
563 | } | |
564 | ||
5391d806 | 565 | #if defined(DEBUG_IDE) |
bef0fd59 | 566 | printf("sector=%" PRId64 "\n", sector_num); |
5391d806 | 567 | #endif |
a597e79c | 568 | |
58ac3211 MA |
569 | if (!ide_sect_range_ok(s, sector_num, n)) { |
570 | ide_rw_error(s); | |
571 | return; | |
572 | } | |
573 | ||
bef0fd59 SH |
574 | s->iov.iov_base = s->io_buffer; |
575 | s->iov.iov_len = n * BDRV_SECTOR_SIZE; | |
576 | qemu_iovec_init_external(&s->qiov, &s->iov, 1); | |
577 | ||
578 | bdrv_acct_start(s->bs, &s->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ); | |
579 | s->pio_aiocb = bdrv_aio_readv(s->bs, sector_num, &s->qiov, n, | |
580 | ide_sector_read_cb, s); | |
5391d806 FB |
581 | } |
582 | ||
b61744b3 | 583 | static void dma_buf_commit(IDEState *s) |
7aea4412 | 584 | { |
1fb8648d | 585 | qemu_sglist_destroy(&s->sg); |
7aea4412 AL |
586 | } |
587 | ||
a62eaa26 KW |
588 | static void ide_async_cmd_done(IDEState *s) |
589 | { | |
590 | if (s->bus->dma->ops->async_cmd_done) { | |
591 | s->bus->dma->ops->async_cmd_done(s->bus->dma); | |
592 | } | |
593 | } | |
594 | ||
33231e0e | 595 | void ide_set_inactive(IDEState *s) |
8337606d | 596 | { |
40a6238a | 597 | s->bus->dma->aiocb = NULL; |
829b933b PB |
598 | if (s->bus->dma->ops->set_inactive) { |
599 | s->bus->dma->ops->set_inactive(s->bus->dma); | |
600 | } | |
a62eaa26 | 601 | ide_async_cmd_done(s); |
8337606d KW |
602 | } |
603 | ||
356721ae | 604 | void ide_dma_error(IDEState *s) |
e162cfb0 AZ |
605 | { |
606 | ide_transfer_stop(s); | |
607 | s->error = ABRT_ERR; | |
608 | s->status = READY_STAT | ERR_STAT; | |
40a6238a | 609 | ide_set_inactive(s); |
9cdd03a7 | 610 | ide_set_irq(s->bus); |
e162cfb0 AZ |
611 | } |
612 | ||
ce4b6522 | 613 | static int ide_handle_rw_error(IDEState *s, int error, int op) |
428c5705 | 614 | { |
1ceee0d5 | 615 | bool is_read = (op & BM_STATUS_RETRY_READ) != 0; |
3e1caa5f | 616 | BlockErrorAction action = bdrv_get_error_action(s->bs, is_read, error); |
428c5705 | 617 | |
a589569f | 618 | if (action == BLOCK_ERROR_ACTION_STOP) { |
40a6238a | 619 | s->bus->dma->ops->set_unit(s->bus->dma, s->unit); |
def93791 | 620 | s->bus->error_status = op; |
a589569f | 621 | } else if (action == BLOCK_ERROR_ACTION_REPORT) { |
ce4b6522 | 622 | if (op & BM_STATUS_DMA_RETRY) { |
b61744b3 | 623 | dma_buf_commit(s); |
428c5705 | 624 | ide_dma_error(s); |
7aea4412 | 625 | } else { |
428c5705 | 626 | ide_rw_error(s); |
7aea4412 | 627 | } |
428c5705 | 628 | } |
3e1caa5f | 629 | bdrv_error_action(s->bs, action, is_read, error); |
a589569f | 630 | return action != BLOCK_ERROR_ACTION_IGNORE; |
428c5705 AL |
631 | } |
632 | ||
cd369c46 | 633 | void ide_dma_cb(void *opaque, int ret) |
98087450 | 634 | { |
40a6238a | 635 | IDEState *s = opaque; |
8ccad811 FB |
636 | int n; |
637 | int64_t sector_num; | |
038268e2 | 638 | bool stay_active = false; |
8ccad811 | 639 | |
e162cfb0 | 640 | if (ret < 0) { |
cd369c46 CH |
641 | int op = BM_STATUS_DMA_RETRY; |
642 | ||
4e1e0051 | 643 | if (s->dma_cmd == IDE_DMA_READ) |
cd369c46 | 644 | op |= BM_STATUS_RETRY_READ; |
d353fb72 CH |
645 | else if (s->dma_cmd == IDE_DMA_TRIM) |
646 | op |= BM_STATUS_RETRY_TRIM; | |
647 | ||
cd369c46 | 648 | if (ide_handle_rw_error(s, -ret, op)) { |
ce4b6522 KW |
649 | return; |
650 | } | |
e162cfb0 AZ |
651 | } |
652 | ||
8ccad811 | 653 | n = s->io_buffer_size >> 9; |
038268e2 KW |
654 | if (n > s->nsector) { |
655 | /* The PRDs were longer than needed for this request. Shorten them so | |
656 | * we don't get a negative remainder. The Active bit must remain set | |
657 | * after the request completes. */ | |
658 | n = s->nsector; | |
659 | stay_active = true; | |
660 | } | |
661 | ||
8ccad811 FB |
662 | sector_num = ide_get_sector(s); |
663 | if (n > 0) { | |
b61744b3 | 664 | dma_buf_commit(s); |
8ccad811 FB |
665 | sector_num += n; |
666 | ide_set_sector(s, sector_num); | |
667 | s->nsector -= n; | |
8ccad811 FB |
668 | } |
669 | ||
670 | /* end of transfer ? */ | |
671 | if (s->nsector == 0) { | |
98087450 | 672 | s->status = READY_STAT | SEEK_STAT; |
9cdd03a7 | 673 | ide_set_irq(s->bus); |
cd369c46 | 674 | goto eot; |
98087450 | 675 | } |
8ccad811 FB |
676 | |
677 | /* launch next transfer */ | |
678 | n = s->nsector; | |
596bb44d | 679 | s->io_buffer_index = 0; |
8ccad811 | 680 | s->io_buffer_size = n * 512; |
4e1e0051 | 681 | if (s->bus->dma->ops->prepare_buf(s->bus->dma, ide_cmd_is_read(s)) == 0) { |
69c38b8f KW |
682 | /* The PRDs were too short. Reset the Active bit, but don't raise an |
683 | * interrupt. */ | |
72bcca73 | 684 | s->status = READY_STAT | SEEK_STAT; |
7aea4412 | 685 | goto eot; |
69c38b8f | 686 | } |
cd369c46 | 687 | |
8ccad811 | 688 | #ifdef DEBUG_AIO |
4e1e0051 CH |
689 | printf("ide_dma_cb: sector_num=%" PRId64 " n=%d, cmd_cmd=%d\n", |
690 | sector_num, n, s->dma_cmd); | |
8ccad811 | 691 | #endif |
cd369c46 | 692 | |
58ac3211 MA |
693 | if (!ide_sect_range_ok(s, sector_num, n)) { |
694 | dma_buf_commit(s); | |
695 | ide_dma_error(s); | |
696 | return; | |
697 | } | |
698 | ||
4e1e0051 CH |
699 | switch (s->dma_cmd) { |
700 | case IDE_DMA_READ: | |
cd369c46 CH |
701 | s->bus->dma->aiocb = dma_bdrv_read(s->bs, &s->sg, sector_num, |
702 | ide_dma_cb, s); | |
4e1e0051 CH |
703 | break; |
704 | case IDE_DMA_WRITE: | |
cd369c46 CH |
705 | s->bus->dma->aiocb = dma_bdrv_write(s->bs, &s->sg, sector_num, |
706 | ide_dma_cb, s); | |
4e1e0051 | 707 | break; |
d353fb72 CH |
708 | case IDE_DMA_TRIM: |
709 | s->bus->dma->aiocb = dma_bdrv_io(s->bs, &s->sg, sector_num, | |
43cf8ae6 DG |
710 | ide_issue_trim, ide_dma_cb, s, |
711 | DMA_DIRECTION_TO_DEVICE); | |
d353fb72 | 712 | break; |
cd369c46 | 713 | } |
cd369c46 CH |
714 | return; |
715 | ||
716 | eot: | |
a597e79c CH |
717 | if (s->dma_cmd == IDE_DMA_READ || s->dma_cmd == IDE_DMA_WRITE) { |
718 | bdrv_acct_done(s->bs, &s->acct); | |
719 | } | |
720 | ide_set_inactive(s); | |
038268e2 KW |
721 | if (stay_active) { |
722 | s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_DMAING); | |
723 | } | |
98087450 FB |
724 | } |
725 | ||
4e1e0051 | 726 | static void ide_sector_start_dma(IDEState *s, enum ide_dma_cmd dma_cmd) |
98087450 | 727 | { |
8ccad811 | 728 | s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT; |
98087450 FB |
729 | s->io_buffer_index = 0; |
730 | s->io_buffer_size = 0; | |
4e1e0051 | 731 | s->dma_cmd = dma_cmd; |
a597e79c CH |
732 | |
733 | switch (dma_cmd) { | |
734 | case IDE_DMA_READ: | |
735 | bdrv_acct_start(s->bs, &s->acct, s->nsector * BDRV_SECTOR_SIZE, | |
736 | BDRV_ACCT_READ); | |
737 | break; | |
738 | case IDE_DMA_WRITE: | |
739 | bdrv_acct_start(s->bs, &s->acct, s->nsector * BDRV_SECTOR_SIZE, | |
740 | BDRV_ACCT_WRITE); | |
741 | break; | |
742 | default: | |
743 | break; | |
744 | } | |
745 | ||
cd369c46 | 746 | s->bus->dma->ops->start_dma(s->bus->dma, s, ide_dma_cb); |
98087450 FB |
747 | } |
748 | ||
a09db21f FB |
749 | static void ide_sector_write_timer_cb(void *opaque) |
750 | { | |
751 | IDEState *s = opaque; | |
9cdd03a7 | 752 | ide_set_irq(s->bus); |
a09db21f FB |
753 | } |
754 | ||
e82dabd8 | 755 | static void ide_sector_write_cb(void *opaque, int ret) |
5391d806 | 756 | { |
e82dabd8 SH |
757 | IDEState *s = opaque; |
758 | int n; | |
a597e79c | 759 | |
a597e79c | 760 | bdrv_acct_done(s->bs, &s->acct); |
428c5705 | 761 | |
e82dabd8 SH |
762 | s->pio_aiocb = NULL; |
763 | s->status &= ~BUSY_STAT; | |
764 | ||
e162cfb0 | 765 | if (ret != 0) { |
e82dabd8 | 766 | if (ide_handle_rw_error(s, -ret, BM_STATUS_PIO_RETRY)) { |
428c5705 | 767 | return; |
e82dabd8 | 768 | } |
e162cfb0 AZ |
769 | } |
770 | ||
e82dabd8 SH |
771 | n = s->nsector; |
772 | if (n > s->req_nb_sectors) { | |
773 | n = s->req_nb_sectors; | |
774 | } | |
5391d806 FB |
775 | s->nsector -= n; |
776 | if (s->nsector == 0) { | |
292eef5a | 777 | /* no more sectors to write */ |
5391d806 FB |
778 | ide_transfer_stop(s); |
779 | } else { | |
e82dabd8 SH |
780 | int n1 = s->nsector; |
781 | if (n1 > s->req_nb_sectors) { | |
5391d806 | 782 | n1 = s->req_nb_sectors; |
e82dabd8 SH |
783 | } |
784 | ide_transfer_start(s, s->io_buffer, n1 * BDRV_SECTOR_SIZE, | |
785 | ide_sector_write); | |
5391d806 | 786 | } |
e82dabd8 | 787 | ide_set_sector(s, ide_get_sector(s) + n); |
3b46e624 | 788 | |
31c2a146 TS |
789 | if (win2k_install_hack && ((++s->irq_count % 16) == 0)) { |
790 | /* It seems there is a bug in the Windows 2000 installer HDD | |
791 | IDE driver which fills the disk with empty logs when the | |
792 | IDE write IRQ comes too early. This hack tries to correct | |
793 | that at the expense of slower write performances. Use this | |
794 | option _only_ to install Windows 2000. You must disable it | |
795 | for normal use. */ | |
bc72ad67 AB |
796 | timer_mod(s->sector_write_timer, |
797 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + (get_ticks_per_sec() / 1000)); | |
f7736b91 | 798 | } else { |
9cdd03a7 | 799 | ide_set_irq(s->bus); |
31c2a146 | 800 | } |
5391d806 FB |
801 | } |
802 | ||
e82dabd8 SH |
803 | void ide_sector_write(IDEState *s) |
804 | { | |
805 | int64_t sector_num; | |
806 | int n; | |
807 | ||
808 | s->status = READY_STAT | SEEK_STAT | BUSY_STAT; | |
809 | sector_num = ide_get_sector(s); | |
810 | #if defined(DEBUG_IDE) | |
811 | printf("sector=%" PRId64 "\n", sector_num); | |
812 | #endif | |
813 | n = s->nsector; | |
814 | if (n > s->req_nb_sectors) { | |
815 | n = s->req_nb_sectors; | |
816 | } | |
817 | ||
58ac3211 MA |
818 | if (!ide_sect_range_ok(s, sector_num, n)) { |
819 | ide_rw_error(s); | |
820 | return; | |
821 | } | |
822 | ||
e82dabd8 SH |
823 | s->iov.iov_base = s->io_buffer; |
824 | s->iov.iov_len = n * BDRV_SECTOR_SIZE; | |
825 | qemu_iovec_init_external(&s->qiov, &s->iov, 1); | |
826 | ||
827 | bdrv_acct_start(s->bs, &s->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ); | |
828 | s->pio_aiocb = bdrv_aio_writev(s->bs, sector_num, &s->qiov, n, | |
829 | ide_sector_write_cb, s); | |
830 | } | |
831 | ||
b0484ae4 CH |
832 | static void ide_flush_cb(void *opaque, int ret) |
833 | { | |
834 | IDEState *s = opaque; | |
835 | ||
69f72a22 PB |
836 | s->pio_aiocb = NULL; |
837 | ||
e2bcadad KW |
838 | if (ret < 0) { |
839 | /* XXX: What sector number to set here? */ | |
840 | if (ide_handle_rw_error(s, -ret, BM_STATUS_RETRY_FLUSH)) { | |
841 | return; | |
842 | } | |
843 | } | |
b0484ae4 | 844 | |
a597e79c | 845 | bdrv_acct_done(s->bs, &s->acct); |
b0484ae4 | 846 | s->status = READY_STAT | SEEK_STAT; |
a62eaa26 | 847 | ide_async_cmd_done(s); |
b0484ae4 CH |
848 | ide_set_irq(s->bus); |
849 | } | |
850 | ||
40a6238a | 851 | void ide_flush_cache(IDEState *s) |
6bcb1a79 | 852 | { |
b2df7531 | 853 | if (s->bs == NULL) { |
6bcb1a79 | 854 | ide_flush_cb(s, 0); |
b2df7531 KW |
855 | return; |
856 | } | |
857 | ||
f68ec837 | 858 | s->status |= BUSY_STAT; |
a597e79c | 859 | bdrv_acct_start(s->bs, &s->acct, 0, BDRV_ACCT_FLUSH); |
69f72a22 | 860 | s->pio_aiocb = bdrv_aio_flush(s->bs, ide_flush_cb, s); |
6bcb1a79 KW |
861 | } |
862 | ||
201a51fc AZ |
863 | static void ide_cfata_metadata_inquiry(IDEState *s) |
864 | { | |
865 | uint16_t *p; | |
866 | uint32_t spd; | |
867 | ||
868 | p = (uint16_t *) s->io_buffer; | |
869 | memset(p, 0, 0x200); | |
870 | spd = ((s->mdata_size - 1) >> 9) + 1; | |
871 | ||
872 | put_le16(p + 0, 0x0001); /* Data format revision */ | |
873 | put_le16(p + 1, 0x0000); /* Media property: silicon */ | |
874 | put_le16(p + 2, s->media_changed); /* Media status */ | |
875 | put_le16(p + 3, s->mdata_size & 0xffff); /* Capacity in bytes (low) */ | |
876 | put_le16(p + 4, s->mdata_size >> 16); /* Capacity in bytes (high) */ | |
877 | put_le16(p + 5, spd & 0xffff); /* Sectors per device (low) */ | |
878 | put_le16(p + 6, spd >> 16); /* Sectors per device (high) */ | |
879 | } | |
880 | ||
881 | static void ide_cfata_metadata_read(IDEState *s) | |
882 | { | |
883 | uint16_t *p; | |
884 | ||
885 | if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) { | |
886 | s->status = ERR_STAT; | |
887 | s->error = ABRT_ERR; | |
888 | return; | |
889 | } | |
890 | ||
891 | p = (uint16_t *) s->io_buffer; | |
892 | memset(p, 0, 0x200); | |
893 | ||
894 | put_le16(p + 0, s->media_changed); /* Media status */ | |
895 | memcpy(p + 1, s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9), | |
896 | MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9), | |
897 | s->nsector << 9), 0x200 - 2)); | |
898 | } | |
899 | ||
900 | static void ide_cfata_metadata_write(IDEState *s) | |
901 | { | |
902 | if (((s->hcyl << 16) | s->lcyl) << 9 > s->mdata_size + 2) { | |
903 | s->status = ERR_STAT; | |
904 | s->error = ABRT_ERR; | |
905 | return; | |
906 | } | |
907 | ||
908 | s->media_changed = 0; | |
909 | ||
910 | memcpy(s->mdata_storage + (((s->hcyl << 16) | s->lcyl) << 9), | |
911 | s->io_buffer + 2, | |
912 | MIN(MIN(s->mdata_size - (((s->hcyl << 16) | s->lcyl) << 9), | |
913 | s->nsector << 9), 0x200 - 2)); | |
914 | } | |
915 | ||
bd491d6a | 916 | /* called when the inserted state of the media has changed */ |
7d4b4ba5 | 917 | static void ide_cd_change_cb(void *opaque, bool load) |
bd491d6a TS |
918 | { |
919 | IDEState *s = opaque; | |
96b8f136 | 920 | uint64_t nb_sectors; |
bd491d6a | 921 | |
25ad22bc | 922 | s->tray_open = !load; |
bd491d6a TS |
923 | bdrv_get_geometry(s->bs, &nb_sectors); |
924 | s->nb_sectors = nb_sectors; | |
9118e7f0 | 925 | |
4b9b7092 AS |
926 | /* |
927 | * First indicate to the guest that a CD has been removed. That's | |
928 | * done on the next command the guest sends us. | |
929 | * | |
67cc61e4 | 930 | * Then we set UNIT_ATTENTION, by which the guest will |
4b9b7092 AS |
931 | * detect a new CD in the drive. See ide_atapi_cmd() for details. |
932 | */ | |
93c8cfd9 | 933 | s->cdrom_changed = 1; |
996faf1a | 934 | s->events.new_media = true; |
2df0a3a3 PB |
935 | s->events.eject_request = false; |
936 | ide_set_irq(s->bus); | |
937 | } | |
938 | ||
939 | static void ide_cd_eject_request_cb(void *opaque, bool force) | |
940 | { | |
941 | IDEState *s = opaque; | |
942 | ||
943 | s->events.eject_request = true; | |
944 | if (force) { | |
945 | s->tray_locked = false; | |
946 | } | |
9cdd03a7 | 947 | ide_set_irq(s->bus); |
bd491d6a TS |
948 | } |
949 | ||
c2ff060f FB |
950 | static void ide_cmd_lba48_transform(IDEState *s, int lba48) |
951 | { | |
952 | s->lba48 = lba48; | |
953 | ||
954 | /* handle the 'magic' 0 nsector count conversion here. to avoid | |
955 | * fiddling with the rest of the read logic, we just store the | |
956 | * full sector count in ->nsector and ignore ->hob_nsector from now | |
957 | */ | |
958 | if (!s->lba48) { | |
959 | if (!s->nsector) | |
960 | s->nsector = 256; | |
961 | } else { | |
962 | if (!s->nsector && !s->hob_nsector) | |
963 | s->nsector = 65536; | |
964 | else { | |
965 | int lo = s->nsector; | |
966 | int hi = s->hob_nsector; | |
967 | ||
968 | s->nsector = (hi << 8) | lo; | |
969 | } | |
970 | } | |
971 | } | |
972 | ||
bcbdc4d3 | 973 | static void ide_clear_hob(IDEBus *bus) |
c2ff060f FB |
974 | { |
975 | /* any write clears HOB high bit of device control register */ | |
bcbdc4d3 GH |
976 | bus->ifs[0].select &= ~(1 << 7); |
977 | bus->ifs[1].select &= ~(1 << 7); | |
c2ff060f FB |
978 | } |
979 | ||
356721ae | 980 | void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
caed8802 | 981 | { |
bcbdc4d3 | 982 | IDEBus *bus = opaque; |
5391d806 FB |
983 | |
984 | #ifdef DEBUG_IDE | |
985 | printf("IDE: write addr=0x%x val=0x%02x\n", addr, val); | |
986 | #endif | |
c2ff060f | 987 | |
5391d806 | 988 | addr &= 7; |
fcdd25ab AL |
989 | |
990 | /* ignore writes to command block while busy with previous command */ | |
bcbdc4d3 | 991 | if (addr != 7 && (idebus_active_if(bus)->status & (BUSY_STAT|DRQ_STAT))) |
fcdd25ab AL |
992 | return; |
993 | ||
5391d806 FB |
994 | switch(addr) { |
995 | case 0: | |
996 | break; | |
997 | case 1: | |
bcbdc4d3 | 998 | ide_clear_hob(bus); |
c45c3d00 | 999 | /* NOTE: data is written to the two drives */ |
bcbdc4d3 GH |
1000 | bus->ifs[0].hob_feature = bus->ifs[0].feature; |
1001 | bus->ifs[1].hob_feature = bus->ifs[1].feature; | |
1002 | bus->ifs[0].feature = val; | |
1003 | bus->ifs[1].feature = val; | |
5391d806 FB |
1004 | break; |
1005 | case 2: | |
bcbdc4d3 GH |
1006 | ide_clear_hob(bus); |
1007 | bus->ifs[0].hob_nsector = bus->ifs[0].nsector; | |
1008 | bus->ifs[1].hob_nsector = bus->ifs[1].nsector; | |
1009 | bus->ifs[0].nsector = val; | |
1010 | bus->ifs[1].nsector = val; | |
5391d806 FB |
1011 | break; |
1012 | case 3: | |
bcbdc4d3 GH |
1013 | ide_clear_hob(bus); |
1014 | bus->ifs[0].hob_sector = bus->ifs[0].sector; | |
1015 | bus->ifs[1].hob_sector = bus->ifs[1].sector; | |
1016 | bus->ifs[0].sector = val; | |
1017 | bus->ifs[1].sector = val; | |
5391d806 FB |
1018 | break; |
1019 | case 4: | |
bcbdc4d3 GH |
1020 | ide_clear_hob(bus); |
1021 | bus->ifs[0].hob_lcyl = bus->ifs[0].lcyl; | |
1022 | bus->ifs[1].hob_lcyl = bus->ifs[1].lcyl; | |
1023 | bus->ifs[0].lcyl = val; | |
1024 | bus->ifs[1].lcyl = val; | |
5391d806 FB |
1025 | break; |
1026 | case 5: | |
bcbdc4d3 GH |
1027 | ide_clear_hob(bus); |
1028 | bus->ifs[0].hob_hcyl = bus->ifs[0].hcyl; | |
1029 | bus->ifs[1].hob_hcyl = bus->ifs[1].hcyl; | |
1030 | bus->ifs[0].hcyl = val; | |
1031 | bus->ifs[1].hcyl = val; | |
5391d806 FB |
1032 | break; |
1033 | case 6: | |
c2ff060f | 1034 | /* FIXME: HOB readback uses bit 7 */ |
bcbdc4d3 GH |
1035 | bus->ifs[0].select = (val & ~0x10) | 0xa0; |
1036 | bus->ifs[1].select = (val | 0x10) | 0xa0; | |
5391d806 | 1037 | /* select drive */ |
bcbdc4d3 | 1038 | bus->unit = (val >> 4) & 1; |
5391d806 FB |
1039 | break; |
1040 | default: | |
1041 | case 7: | |
1042 | /* command */ | |
7cff87ff AG |
1043 | ide_exec_cmd(bus, val); |
1044 | break; | |
1045 | } | |
1046 | } | |
1047 | ||
b300337e KW |
1048 | static bool cmd_nop(IDEState *s, uint8_t cmd) |
1049 | { | |
1050 | return true; | |
1051 | } | |
1052 | ||
4286434c KW |
1053 | static bool cmd_data_set_management(IDEState *s, uint8_t cmd) |
1054 | { | |
1055 | switch (s->feature) { | |
1056 | case DSM_TRIM: | |
1057 | if (s->bs) { | |
1058 | ide_sector_start_dma(s, IDE_DMA_TRIM); | |
1059 | return false; | |
1060 | } | |
1061 | break; | |
1062 | } | |
1063 | ||
1064 | ide_abort_command(s); | |
1065 | return true; | |
1066 | } | |
1067 | ||
1c66869a KW |
1068 | static bool cmd_identify(IDEState *s, uint8_t cmd) |
1069 | { | |
1070 | if (s->bs && s->drive_kind != IDE_CD) { | |
1071 | if (s->drive_kind != IDE_CFATA) { | |
1072 | ide_identify(s); | |
1073 | } else { | |
1074 | ide_cfata_identify(s); | |
1075 | } | |
1076 | s->status = READY_STAT | SEEK_STAT; | |
1077 | ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop); | |
1078 | ide_set_irq(s->bus); | |
1079 | return false; | |
1080 | } else { | |
1081 | if (s->drive_kind == IDE_CD) { | |
1082 | ide_set_signature(s); | |
1083 | } | |
1084 | ide_abort_command(s); | |
1085 | } | |
1086 | ||
1087 | return true; | |
1088 | } | |
1089 | ||
413860cf KW |
1090 | static bool cmd_verify(IDEState *s, uint8_t cmd) |
1091 | { | |
1092 | bool lba48 = (cmd == WIN_VERIFY_EXT); | |
1093 | ||
1094 | /* do sector number check ? */ | |
1095 | ide_cmd_lba48_transform(s, lba48); | |
1096 | ||
1097 | return true; | |
1098 | } | |
1099 | ||
adf3a2c4 KW |
1100 | static bool cmd_set_multiple_mode(IDEState *s, uint8_t cmd) |
1101 | { | |
1102 | if (s->drive_kind == IDE_CFATA && s->nsector == 0) { | |
1103 | /* Disable Read and Write Multiple */ | |
1104 | s->mult_sectors = 0; | |
1105 | } else if ((s->nsector & 0xff) != 0 && | |
1106 | ((s->nsector & 0xff) > MAX_MULT_SECTORS || | |
1107 | (s->nsector & (s->nsector - 1)) != 0)) { | |
1108 | ide_abort_command(s); | |
1109 | } else { | |
1110 | s->mult_sectors = s->nsector & 0xff; | |
1111 | } | |
1112 | ||
1113 | return true; | |
1114 | } | |
1115 | ||
1116 | static bool cmd_read_multiple(IDEState *s, uint8_t cmd) | |
1117 | { | |
1118 | bool lba48 = (cmd == WIN_MULTREAD_EXT); | |
1119 | ||
1120 | if (!s->bs || !s->mult_sectors) { | |
1121 | ide_abort_command(s); | |
1122 | return true; | |
1123 | } | |
1124 | ||
1125 | ide_cmd_lba48_transform(s, lba48); | |
1126 | s->req_nb_sectors = s->mult_sectors; | |
1127 | ide_sector_read(s); | |
1128 | return false; | |
1129 | } | |
1130 | ||
1131 | static bool cmd_write_multiple(IDEState *s, uint8_t cmd) | |
1132 | { | |
1133 | bool lba48 = (cmd == WIN_MULTWRITE_EXT); | |
1134 | int n; | |
1135 | ||
1136 | if (!s->bs || !s->mult_sectors) { | |
1137 | ide_abort_command(s); | |
1138 | return true; | |
1139 | } | |
1140 | ||
1141 | ide_cmd_lba48_transform(s, lba48); | |
1142 | ||
1143 | s->req_nb_sectors = s->mult_sectors; | |
1144 | n = MIN(s->nsector, s->req_nb_sectors); | |
1145 | ||
1146 | s->status = SEEK_STAT | READY_STAT; | |
1147 | ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write); | |
1148 | ||
1149 | s->media_changed = 1; | |
1150 | ||
1151 | return false; | |
1152 | } | |
1153 | ||
0e6498ed KW |
1154 | static bool cmd_read_pio(IDEState *s, uint8_t cmd) |
1155 | { | |
1156 | bool lba48 = (cmd == WIN_READ_EXT); | |
1157 | ||
1158 | if (s->drive_kind == IDE_CD) { | |
1159 | ide_set_signature(s); /* odd, but ATA4 8.27.5.2 requires it */ | |
1160 | ide_abort_command(s); | |
1161 | return true; | |
1162 | } | |
1163 | ||
1164 | if (!s->bs) { | |
1165 | ide_abort_command(s); | |
1166 | return true; | |
1167 | } | |
1168 | ||
1169 | ide_cmd_lba48_transform(s, lba48); | |
1170 | s->req_nb_sectors = 1; | |
1171 | ide_sector_read(s); | |
1172 | ||
1173 | return false; | |
1174 | } | |
1175 | ||
1176 | static bool cmd_write_pio(IDEState *s, uint8_t cmd) | |
1177 | { | |
1178 | bool lba48 = (cmd == WIN_WRITE_EXT); | |
1179 | ||
1180 | if (!s->bs) { | |
1181 | ide_abort_command(s); | |
1182 | return true; | |
1183 | } | |
1184 | ||
1185 | ide_cmd_lba48_transform(s, lba48); | |
1186 | ||
1187 | s->req_nb_sectors = 1; | |
1188 | s->status = SEEK_STAT | READY_STAT; | |
1189 | ide_transfer_start(s, s->io_buffer, 512, ide_sector_write); | |
1190 | ||
1191 | s->media_changed = 1; | |
1192 | ||
1193 | return false; | |
1194 | } | |
1195 | ||
92a6a6f6 KW |
1196 | static bool cmd_read_dma(IDEState *s, uint8_t cmd) |
1197 | { | |
1198 | bool lba48 = (cmd == WIN_READDMA_EXT); | |
1199 | ||
1200 | if (!s->bs) { | |
1201 | ide_abort_command(s); | |
1202 | return true; | |
1203 | } | |
1204 | ||
1205 | ide_cmd_lba48_transform(s, lba48); | |
1206 | ide_sector_start_dma(s, IDE_DMA_READ); | |
1207 | ||
1208 | return false; | |
1209 | } | |
1210 | ||
1211 | static bool cmd_write_dma(IDEState *s, uint8_t cmd) | |
1212 | { | |
1213 | bool lba48 = (cmd == WIN_WRITEDMA_EXT); | |
1214 | ||
1215 | if (!s->bs) { | |
1216 | ide_abort_command(s); | |
1217 | return true; | |
1218 | } | |
1219 | ||
1220 | ide_cmd_lba48_transform(s, lba48); | |
1221 | ide_sector_start_dma(s, IDE_DMA_WRITE); | |
1222 | ||
1223 | s->media_changed = 1; | |
1224 | ||
1225 | return false; | |
1226 | } | |
1227 | ||
9afce429 KW |
1228 | static bool cmd_flush_cache(IDEState *s, uint8_t cmd) |
1229 | { | |
1230 | ide_flush_cache(s); | |
1231 | return false; | |
1232 | } | |
1233 | ||
61fdda37 KW |
1234 | static bool cmd_seek(IDEState *s, uint8_t cmd) |
1235 | { | |
1236 | /* XXX: Check that seek is within bounds */ | |
1237 | return true; | |
1238 | } | |
1239 | ||
63a82e6a KW |
1240 | static bool cmd_read_native_max(IDEState *s, uint8_t cmd) |
1241 | { | |
1242 | bool lba48 = (cmd == WIN_READ_NATIVE_MAX_EXT); | |
1243 | ||
1244 | /* Refuse if no sectors are addressable (e.g. medium not inserted) */ | |
1245 | if (s->nb_sectors == 0) { | |
1246 | ide_abort_command(s); | |
1247 | return true; | |
1248 | } | |
1249 | ||
1250 | ide_cmd_lba48_transform(s, lba48); | |
1251 | ide_set_sector(s, s->nb_sectors - 1); | |
1252 | ||
1253 | return true; | |
1254 | } | |
1255 | ||
785f6320 KW |
1256 | static bool cmd_check_power_mode(IDEState *s, uint8_t cmd) |
1257 | { | |
1258 | s->nsector = 0xff; /* device active or idle */ | |
1259 | return true; | |
1260 | } | |
1261 | ||
ee03398c KW |
1262 | static bool cmd_set_features(IDEState *s, uint8_t cmd) |
1263 | { | |
1264 | uint16_t *identify_data; | |
1265 | ||
1266 | if (!s->bs) { | |
1267 | ide_abort_command(s); | |
1268 | return true; | |
1269 | } | |
1270 | ||
1271 | /* XXX: valid for CDROM ? */ | |
1272 | switch (s->feature) { | |
1273 | case 0x02: /* write cache enable */ | |
1274 | bdrv_set_enable_write_cache(s->bs, true); | |
1275 | identify_data = (uint16_t *)s->identify_data; | |
1276 | put_le16(identify_data + 85, (1 << 14) | (1 << 5) | 1); | |
1277 | return true; | |
1278 | case 0x82: /* write cache disable */ | |
1279 | bdrv_set_enable_write_cache(s->bs, false); | |
1280 | identify_data = (uint16_t *)s->identify_data; | |
1281 | put_le16(identify_data + 85, (1 << 14) | 1); | |
1282 | ide_flush_cache(s); | |
1283 | return false; | |
1284 | case 0xcc: /* reverting to power-on defaults enable */ | |
1285 | case 0x66: /* reverting to power-on defaults disable */ | |
1286 | case 0xaa: /* read look-ahead enable */ | |
1287 | case 0x55: /* read look-ahead disable */ | |
1288 | case 0x05: /* set advanced power management mode */ | |
1289 | case 0x85: /* disable advanced power management mode */ | |
1290 | case 0x69: /* NOP */ | |
1291 | case 0x67: /* NOP */ | |
1292 | case 0x96: /* NOP */ | |
1293 | case 0x9a: /* NOP */ | |
1294 | case 0x42: /* enable Automatic Acoustic Mode */ | |
1295 | case 0xc2: /* disable Automatic Acoustic Mode */ | |
1296 | return true; | |
1297 | case 0x03: /* set transfer mode */ | |
1298 | { | |
1299 | uint8_t val = s->nsector & 0x07; | |
1300 | identify_data = (uint16_t *)s->identify_data; | |
1301 | ||
1302 | switch (s->nsector >> 3) { | |
1303 | case 0x00: /* pio default */ | |
1304 | case 0x01: /* pio mode */ | |
1305 | put_le16(identify_data + 62, 0x07); | |
1306 | put_le16(identify_data + 63, 0x07); | |
1307 | put_le16(identify_data + 88, 0x3f); | |
1308 | break; | |
1309 | case 0x02: /* sigle word dma mode*/ | |
1310 | put_le16(identify_data + 62, 0x07 | (1 << (val + 8))); | |
1311 | put_le16(identify_data + 63, 0x07); | |
1312 | put_le16(identify_data + 88, 0x3f); | |
1313 | break; | |
1314 | case 0x04: /* mdma mode */ | |
1315 | put_le16(identify_data + 62, 0x07); | |
1316 | put_le16(identify_data + 63, 0x07 | (1 << (val + 8))); | |
1317 | put_le16(identify_data + 88, 0x3f); | |
1318 | break; | |
1319 | case 0x08: /* udma mode */ | |
1320 | put_le16(identify_data + 62, 0x07); | |
1321 | put_le16(identify_data + 63, 0x07); | |
1322 | put_le16(identify_data + 88, 0x3f | (1 << (val + 8))); | |
1323 | break; | |
1324 | default: | |
1325 | goto abort_cmd; | |
1326 | } | |
1327 | return true; | |
1328 | } | |
1329 | } | |
1330 | ||
1331 | abort_cmd: | |
1332 | ide_abort_command(s); | |
1333 | return true; | |
1334 | } | |
1335 | ||
ee425c78 KW |
1336 | |
1337 | /*** ATAPI commands ***/ | |
1338 | ||
1339 | static bool cmd_identify_packet(IDEState *s, uint8_t cmd) | |
1340 | { | |
1341 | ide_atapi_identify(s); | |
1342 | s->status = READY_STAT | SEEK_STAT; | |
1343 | ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop); | |
1344 | ide_set_irq(s->bus); | |
1345 | return false; | |
1346 | } | |
1347 | ||
1348 | static bool cmd_exec_dev_diagnostic(IDEState *s, uint8_t cmd) | |
1349 | { | |
1350 | ide_set_signature(s); | |
1351 | ||
1352 | if (s->drive_kind == IDE_CD) { | |
1353 | s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet | |
1354 | * devices to return a clear status register | |
1355 | * with READY_STAT *not* set. */ | |
850484a2 | 1356 | s->error = 0x01; |
ee425c78 KW |
1357 | } else { |
1358 | s->status = READY_STAT | SEEK_STAT; | |
1359 | /* The bits of the error register are not as usual for this command! | |
1360 | * They are part of the regular output (this is why ERR_STAT isn't set) | |
1361 | * Device 0 passed, Device 1 passed or not present. */ | |
1362 | s->error = 0x01; | |
1363 | ide_set_irq(s->bus); | |
1364 | } | |
1365 | ||
1366 | return false; | |
1367 | } | |
1368 | ||
1369 | static bool cmd_device_reset(IDEState *s, uint8_t cmd) | |
1370 | { | |
1371 | ide_set_signature(s); | |
1372 | s->status = 0x00; /* NOTE: READY is _not_ set */ | |
1373 | s->error = 0x01; | |
1374 | ||
1375 | return false; | |
1376 | } | |
1377 | ||
1378 | static bool cmd_packet(IDEState *s, uint8_t cmd) | |
1379 | { | |
1380 | /* overlapping commands not supported */ | |
1381 | if (s->feature & 0x02) { | |
1382 | ide_abort_command(s); | |
1383 | return true; | |
1384 | } | |
1385 | ||
1386 | s->status = READY_STAT | SEEK_STAT; | |
1387 | s->atapi_dma = s->feature & 1; | |
1388 | s->nsector = 1; | |
1389 | ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE, | |
1390 | ide_atapi_cmd); | |
1391 | return false; | |
1392 | } | |
1393 | ||
6b1dd744 KW |
1394 | |
1395 | /*** CF-ATA commands ***/ | |
1396 | ||
1397 | static bool cmd_cfa_req_ext_error_code(IDEState *s, uint8_t cmd) | |
1398 | { | |
1399 | s->error = 0x09; /* miscellaneous error */ | |
1400 | s->status = READY_STAT | SEEK_STAT; | |
1401 | ide_set_irq(s->bus); | |
1402 | ||
1403 | return false; | |
1404 | } | |
1405 | ||
1406 | static bool cmd_cfa_erase_sectors(IDEState *s, uint8_t cmd) | |
1407 | { | |
1408 | /* WIN_SECURITY_FREEZE_LOCK has the same ID as CFA_WEAR_LEVEL and is | |
1409 | * required for Windows 8 to work with AHCI */ | |
1410 | ||
1411 | if (cmd == CFA_WEAR_LEVEL) { | |
1412 | s->nsector = 0; | |
1413 | } | |
1414 | ||
1415 | if (cmd == CFA_ERASE_SECTORS) { | |
1416 | s->media_changed = 1; | |
1417 | } | |
1418 | ||
1419 | return true; | |
1420 | } | |
1421 | ||
1422 | static bool cmd_cfa_translate_sector(IDEState *s, uint8_t cmd) | |
1423 | { | |
1424 | s->status = READY_STAT | SEEK_STAT; | |
1425 | ||
1426 | memset(s->io_buffer, 0, 0x200); | |
1427 | s->io_buffer[0x00] = s->hcyl; /* Cyl MSB */ | |
1428 | s->io_buffer[0x01] = s->lcyl; /* Cyl LSB */ | |
1429 | s->io_buffer[0x02] = s->select; /* Head */ | |
1430 | s->io_buffer[0x03] = s->sector; /* Sector */ | |
1431 | s->io_buffer[0x04] = ide_get_sector(s) >> 16; /* LBA MSB */ | |
1432 | s->io_buffer[0x05] = ide_get_sector(s) >> 8; /* LBA */ | |
1433 | s->io_buffer[0x06] = ide_get_sector(s) >> 0; /* LBA LSB */ | |
1434 | s->io_buffer[0x13] = 0x00; /* Erase flag */ | |
1435 | s->io_buffer[0x18] = 0x00; /* Hot count */ | |
1436 | s->io_buffer[0x19] = 0x00; /* Hot count */ | |
1437 | s->io_buffer[0x1a] = 0x01; /* Hot count */ | |
1438 | ||
1439 | ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop); | |
1440 | ide_set_irq(s->bus); | |
1441 | ||
1442 | return false; | |
1443 | } | |
1444 | ||
1445 | static bool cmd_cfa_access_metadata_storage(IDEState *s, uint8_t cmd) | |
1446 | { | |
1447 | switch (s->feature) { | |
1448 | case 0x02: /* Inquiry Metadata Storage */ | |
1449 | ide_cfata_metadata_inquiry(s); | |
1450 | break; | |
1451 | case 0x03: /* Read Metadata Storage */ | |
1452 | ide_cfata_metadata_read(s); | |
1453 | break; | |
1454 | case 0x04: /* Write Metadata Storage */ | |
1455 | ide_cfata_metadata_write(s); | |
1456 | break; | |
1457 | default: | |
1458 | ide_abort_command(s); | |
1459 | return true; | |
1460 | } | |
1461 | ||
1462 | ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop); | |
1463 | s->status = 0x00; /* NOTE: READY is _not_ set */ | |
1464 | ide_set_irq(s->bus); | |
1465 | ||
1466 | return false; | |
1467 | } | |
1468 | ||
1469 | static bool cmd_ibm_sense_condition(IDEState *s, uint8_t cmd) | |
1470 | { | |
1471 | switch (s->feature) { | |
1472 | case 0x01: /* sense temperature in device */ | |
1473 | s->nsector = 0x50; /* +20 C */ | |
1474 | break; | |
1475 | default: | |
1476 | ide_abort_command(s); | |
1477 | return true; | |
1478 | } | |
1479 | ||
1480 | return true; | |
1481 | } | |
1482 | ||
ff352677 KW |
1483 | |
1484 | /*** SMART commands ***/ | |
1485 | ||
1486 | static bool cmd_smart(IDEState *s, uint8_t cmd) | |
1487 | { | |
1488 | int n; | |
1489 | ||
1490 | if (s->hcyl != 0xc2 || s->lcyl != 0x4f) { | |
1491 | goto abort_cmd; | |
1492 | } | |
1493 | ||
1494 | if (!s->smart_enabled && s->feature != SMART_ENABLE) { | |
1495 | goto abort_cmd; | |
1496 | } | |
1497 | ||
1498 | switch (s->feature) { | |
1499 | case SMART_DISABLE: | |
1500 | s->smart_enabled = 0; | |
1501 | return true; | |
1502 | ||
1503 | case SMART_ENABLE: | |
1504 | s->smart_enabled = 1; | |
1505 | return true; | |
1506 | ||
1507 | case SMART_ATTR_AUTOSAVE: | |
1508 | switch (s->sector) { | |
1509 | case 0x00: | |
1510 | s->smart_autosave = 0; | |
1511 | break; | |
1512 | case 0xf1: | |
1513 | s->smart_autosave = 1; | |
1514 | break; | |
1515 | default: | |
1516 | goto abort_cmd; | |
1517 | } | |
1518 | return true; | |
1519 | ||
1520 | case SMART_STATUS: | |
1521 | if (!s->smart_errors) { | |
1522 | s->hcyl = 0xc2; | |
1523 | s->lcyl = 0x4f; | |
1524 | } else { | |
1525 | s->hcyl = 0x2c; | |
1526 | s->lcyl = 0xf4; | |
1527 | } | |
1528 | return true; | |
1529 | ||
1530 | case SMART_READ_THRESH: | |
1531 | memset(s->io_buffer, 0, 0x200); | |
1532 | s->io_buffer[0] = 0x01; /* smart struct version */ | |
1533 | ||
1534 | for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) { | |
1535 | s->io_buffer[2 + 0 + (n * 12)] = smart_attributes[n][0]; | |
1536 | s->io_buffer[2 + 1 + (n * 12)] = smart_attributes[n][11]; | |
1537 | } | |
1538 | ||
1539 | /* checksum */ | |
1540 | for (n = 0; n < 511; n++) { | |
1541 | s->io_buffer[511] += s->io_buffer[n]; | |
1542 | } | |
1543 | s->io_buffer[511] = 0x100 - s->io_buffer[511]; | |
1544 | ||
1545 | s->status = READY_STAT | SEEK_STAT; | |
1546 | ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop); | |
1547 | ide_set_irq(s->bus); | |
1548 | return false; | |
1549 | ||
1550 | case SMART_READ_DATA: | |
1551 | memset(s->io_buffer, 0, 0x200); | |
1552 | s->io_buffer[0] = 0x01; /* smart struct version */ | |
1553 | ||
1554 | for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) { | |
1555 | int i; | |
1556 | for (i = 0; i < 11; i++) { | |
1557 | s->io_buffer[2 + i + (n * 12)] = smart_attributes[n][i]; | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | s->io_buffer[362] = 0x02 | (s->smart_autosave ? 0x80 : 0x00); | |
1562 | if (s->smart_selftest_count == 0) { | |
1563 | s->io_buffer[363] = 0; | |
1564 | } else { | |
1565 | s->io_buffer[363] = | |
1566 | s->smart_selftest_data[3 + | |
1567 | (s->smart_selftest_count - 1) * | |
1568 | 24]; | |
1569 | } | |
1570 | s->io_buffer[364] = 0x20; | |
1571 | s->io_buffer[365] = 0x01; | |
1572 | /* offline data collection capacity: execute + self-test*/ | |
1573 | s->io_buffer[367] = (1 << 4 | 1 << 3 | 1); | |
1574 | s->io_buffer[368] = 0x03; /* smart capability (1) */ | |
1575 | s->io_buffer[369] = 0x00; /* smart capability (2) */ | |
1576 | s->io_buffer[370] = 0x01; /* error logging supported */ | |
1577 | s->io_buffer[372] = 0x02; /* minutes for poll short test */ | |
1578 | s->io_buffer[373] = 0x36; /* minutes for poll ext test */ | |
1579 | s->io_buffer[374] = 0x01; /* minutes for poll conveyance */ | |
1580 | ||
1581 | for (n = 0; n < 511; n++) { | |
1582 | s->io_buffer[511] += s->io_buffer[n]; | |
1583 | } | |
1584 | s->io_buffer[511] = 0x100 - s->io_buffer[511]; | |
1585 | ||
1586 | s->status = READY_STAT | SEEK_STAT; | |
1587 | ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop); | |
1588 | ide_set_irq(s->bus); | |
1589 | return false; | |
1590 | ||
1591 | case SMART_READ_LOG: | |
1592 | switch (s->sector) { | |
1593 | case 0x01: /* summary smart error log */ | |
1594 | memset(s->io_buffer, 0, 0x200); | |
1595 | s->io_buffer[0] = 0x01; | |
1596 | s->io_buffer[1] = 0x00; /* no error entries */ | |
1597 | s->io_buffer[452] = s->smart_errors & 0xff; | |
1598 | s->io_buffer[453] = (s->smart_errors & 0xff00) >> 8; | |
1599 | ||
1600 | for (n = 0; n < 511; n++) { | |
1601 | s->io_buffer[511] += s->io_buffer[n]; | |
1602 | } | |
1603 | s->io_buffer[511] = 0x100 - s->io_buffer[511]; | |
1604 | break; | |
1605 | case 0x06: /* smart self test log */ | |
1606 | memset(s->io_buffer, 0, 0x200); | |
1607 | s->io_buffer[0] = 0x01; | |
1608 | if (s->smart_selftest_count == 0) { | |
1609 | s->io_buffer[508] = 0; | |
1610 | } else { | |
1611 | s->io_buffer[508] = s->smart_selftest_count; | |
1612 | for (n = 2; n < 506; n++) { | |
1613 | s->io_buffer[n] = s->smart_selftest_data[n]; | |
1614 | } | |
1615 | } | |
1616 | ||
1617 | for (n = 0; n < 511; n++) { | |
1618 | s->io_buffer[511] += s->io_buffer[n]; | |
1619 | } | |
1620 | s->io_buffer[511] = 0x100 - s->io_buffer[511]; | |
1621 | break; | |
1622 | default: | |
1623 | goto abort_cmd; | |
1624 | } | |
1625 | s->status = READY_STAT | SEEK_STAT; | |
1626 | ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop); | |
1627 | ide_set_irq(s->bus); | |
1628 | return false; | |
1629 | ||
1630 | case SMART_EXECUTE_OFFLINE: | |
1631 | switch (s->sector) { | |
1632 | case 0: /* off-line routine */ | |
1633 | case 1: /* short self test */ | |
1634 | case 2: /* extended self test */ | |
1635 | s->smart_selftest_count++; | |
1636 | if (s->smart_selftest_count > 21) { | |
940973ae | 1637 | s->smart_selftest_count = 1; |
ff352677 KW |
1638 | } |
1639 | n = 2 + (s->smart_selftest_count - 1) * 24; | |
1640 | s->smart_selftest_data[n] = s->sector; | |
1641 | s->smart_selftest_data[n + 1] = 0x00; /* OK and finished */ | |
1642 | s->smart_selftest_data[n + 2] = 0x34; /* hour count lsb */ | |
1643 | s->smart_selftest_data[n + 3] = 0x12; /* hour count msb */ | |
1644 | break; | |
1645 | default: | |
1646 | goto abort_cmd; | |
1647 | } | |
1648 | return true; | |
1649 | } | |
1650 | ||
1651 | abort_cmd: | |
1652 | ide_abort_command(s); | |
1653 | return true; | |
1654 | } | |
1655 | ||
844505b1 MA |
1656 | #define HD_OK (1u << IDE_HD) |
1657 | #define CD_OK (1u << IDE_CD) | |
1658 | #define CFA_OK (1u << IDE_CFATA) | |
1659 | #define HD_CFA_OK (HD_OK | CFA_OK) | |
1660 | #define ALL_OK (HD_OK | CD_OK | CFA_OK) | |
1661 | ||
a0436e92 KW |
1662 | /* Set the Disk Seek Completed status bit during completion */ |
1663 | #define SET_DSC (1u << 8) | |
1664 | ||
844505b1 | 1665 | /* See ACS-2 T13/2015-D Table B.2 Command codes */ |
a0436e92 KW |
1666 | static const struct { |
1667 | /* Returns true if the completion code should be run */ | |
1668 | bool (*handler)(IDEState *s, uint8_t cmd); | |
1669 | int flags; | |
1670 | } ide_cmd_table[0x100] = { | |
844505b1 | 1671 | /* NOP not implemented, mandatory for CD */ |
6b1dd744 | 1672 | [CFA_REQ_EXT_ERROR_CODE] = { cmd_cfa_req_ext_error_code, CFA_OK }, |
4286434c | 1673 | [WIN_DSM] = { cmd_data_set_management, ALL_OK }, |
ee425c78 | 1674 | [WIN_DEVICE_RESET] = { cmd_device_reset, CD_OK }, |
b300337e | 1675 | [WIN_RECAL] = { cmd_nop, HD_CFA_OK | SET_DSC}, |
0e6498ed KW |
1676 | [WIN_READ] = { cmd_read_pio, ALL_OK }, |
1677 | [WIN_READ_ONCE] = { cmd_read_pio, ALL_OK }, | |
1678 | [WIN_READ_EXT] = { cmd_read_pio, HD_CFA_OK }, | |
92a6a6f6 | 1679 | [WIN_READDMA_EXT] = { cmd_read_dma, HD_CFA_OK }, |
63a82e6a | 1680 | [WIN_READ_NATIVE_MAX_EXT] = { cmd_read_native_max, HD_CFA_OK | SET_DSC }, |
adf3a2c4 | 1681 | [WIN_MULTREAD_EXT] = { cmd_read_multiple, HD_CFA_OK }, |
0e6498ed KW |
1682 | [WIN_WRITE] = { cmd_write_pio, HD_CFA_OK }, |
1683 | [WIN_WRITE_ONCE] = { cmd_write_pio, HD_CFA_OK }, | |
1684 | [WIN_WRITE_EXT] = { cmd_write_pio, HD_CFA_OK }, | |
92a6a6f6 | 1685 | [WIN_WRITEDMA_EXT] = { cmd_write_dma, HD_CFA_OK }, |
0e6498ed | 1686 | [CFA_WRITE_SECT_WO_ERASE] = { cmd_write_pio, CFA_OK }, |
adf3a2c4 | 1687 | [WIN_MULTWRITE_EXT] = { cmd_write_multiple, HD_CFA_OK }, |
0e6498ed | 1688 | [WIN_WRITE_VERIFY] = { cmd_write_pio, HD_CFA_OK }, |
413860cf KW |
1689 | [WIN_VERIFY] = { cmd_verify, HD_CFA_OK | SET_DSC }, |
1690 | [WIN_VERIFY_ONCE] = { cmd_verify, HD_CFA_OK | SET_DSC }, | |
1691 | [WIN_VERIFY_EXT] = { cmd_verify, HD_CFA_OK | SET_DSC }, | |
61fdda37 | 1692 | [WIN_SEEK] = { cmd_seek, HD_CFA_OK | SET_DSC }, |
6b1dd744 | 1693 | [CFA_TRANSLATE_SECTOR] = { cmd_cfa_translate_sector, CFA_OK }, |
ee425c78 | 1694 | [WIN_DIAGNOSE] = { cmd_exec_dev_diagnostic, ALL_OK }, |
b300337e KW |
1695 | [WIN_SPECIFY] = { cmd_nop, HD_CFA_OK | SET_DSC }, |
1696 | [WIN_STANDBYNOW2] = { cmd_nop, ALL_OK }, | |
1697 | [WIN_IDLEIMMEDIATE2] = { cmd_nop, ALL_OK }, | |
1698 | [WIN_STANDBY2] = { cmd_nop, ALL_OK }, | |
1699 | [WIN_SETIDLE2] = { cmd_nop, ALL_OK }, | |
785f6320 | 1700 | [WIN_CHECKPOWERMODE2] = { cmd_check_power_mode, ALL_OK | SET_DSC }, |
b300337e | 1701 | [WIN_SLEEPNOW2] = { cmd_nop, ALL_OK }, |
ee425c78 KW |
1702 | [WIN_PACKETCMD] = { cmd_packet, CD_OK }, |
1703 | [WIN_PIDENTIFY] = { cmd_identify_packet, CD_OK }, | |
ff352677 | 1704 | [WIN_SMART] = { cmd_smart, HD_CFA_OK | SET_DSC }, |
6b1dd744 KW |
1705 | [CFA_ACCESS_METADATA_STORAGE] = { cmd_cfa_access_metadata_storage, CFA_OK }, |
1706 | [CFA_ERASE_SECTORS] = { cmd_cfa_erase_sectors, CFA_OK | SET_DSC }, | |
adf3a2c4 KW |
1707 | [WIN_MULTREAD] = { cmd_read_multiple, HD_CFA_OK }, |
1708 | [WIN_MULTWRITE] = { cmd_write_multiple, HD_CFA_OK }, | |
1709 | [WIN_SETMULT] = { cmd_set_multiple_mode, HD_CFA_OK | SET_DSC }, | |
92a6a6f6 KW |
1710 | [WIN_READDMA] = { cmd_read_dma, HD_CFA_OK }, |
1711 | [WIN_READDMA_ONCE] = { cmd_read_dma, HD_CFA_OK }, | |
1712 | [WIN_WRITEDMA] = { cmd_write_dma, HD_CFA_OK }, | |
1713 | [WIN_WRITEDMA_ONCE] = { cmd_write_dma, HD_CFA_OK }, | |
adf3a2c4 | 1714 | [CFA_WRITE_MULTI_WO_ERASE] = { cmd_write_multiple, CFA_OK }, |
b300337e KW |
1715 | [WIN_STANDBYNOW1] = { cmd_nop, ALL_OK }, |
1716 | [WIN_IDLEIMMEDIATE] = { cmd_nop, ALL_OK }, | |
1717 | [WIN_STANDBY] = { cmd_nop, ALL_OK }, | |
1718 | [WIN_SETIDLE1] = { cmd_nop, ALL_OK }, | |
785f6320 | 1719 | [WIN_CHECKPOWERMODE1] = { cmd_check_power_mode, ALL_OK | SET_DSC }, |
b300337e | 1720 | [WIN_SLEEPNOW1] = { cmd_nop, ALL_OK }, |
9afce429 KW |
1721 | [WIN_FLUSH_CACHE] = { cmd_flush_cache, ALL_OK }, |
1722 | [WIN_FLUSH_CACHE_EXT] = { cmd_flush_cache, HD_CFA_OK }, | |
1c66869a | 1723 | [WIN_IDENTIFY] = { cmd_identify, ALL_OK }, |
ee03398c | 1724 | [WIN_SETFEATURES] = { cmd_set_features, ALL_OK | SET_DSC }, |
6b1dd744 KW |
1725 | [IBM_SENSE_CONDITION] = { cmd_ibm_sense_condition, CFA_OK | SET_DSC }, |
1726 | [CFA_WEAR_LEVEL] = { cmd_cfa_erase_sectors, HD_CFA_OK | SET_DSC }, | |
63a82e6a | 1727 | [WIN_READ_NATIVE_MAX] = { cmd_read_native_max, ALL_OK | SET_DSC }, |
844505b1 MA |
1728 | }; |
1729 | ||
1730 | static bool ide_cmd_permitted(IDEState *s, uint32_t cmd) | |
1731 | { | |
1732 | return cmd < ARRAY_SIZE(ide_cmd_table) | |
a0436e92 | 1733 | && (ide_cmd_table[cmd].flags & (1u << s->drive_kind)); |
844505b1 | 1734 | } |
7cff87ff AG |
1735 | |
1736 | void ide_exec_cmd(IDEBus *bus, uint32_t val) | |
1737 | { | |
1738 | IDEState *s; | |
dfe1ea8f | 1739 | bool complete; |
7cff87ff | 1740 | |
5391d806 | 1741 | #if defined(DEBUG_IDE) |
6ef2ba5e | 1742 | printf("ide: CMD=%02x\n", val); |
5391d806 | 1743 | #endif |
6ef2ba5e | 1744 | s = idebus_active_if(bus); |
66a0a2cb | 1745 | /* ignore commands to non existent slave */ |
6ef2ba5e AG |
1746 | if (s != bus->ifs && !s->bs) |
1747 | return; | |
c2ff060f | 1748 | |
6ef2ba5e AG |
1749 | /* Only DEVICE RESET is allowed while BSY or/and DRQ are set */ |
1750 | if ((s->status & (BUSY_STAT|DRQ_STAT)) && val != WIN_DEVICE_RESET) | |
1751 | return; | |
fcdd25ab | 1752 | |
844505b1 | 1753 | if (!ide_cmd_permitted(s, val)) { |
dfe1ea8f KW |
1754 | ide_abort_command(s); |
1755 | ide_set_irq(s->bus); | |
1756 | return; | |
844505b1 MA |
1757 | } |
1758 | ||
dfe1ea8f KW |
1759 | s->status = READY_STAT | BUSY_STAT; |
1760 | s->error = 0; | |
a0436e92 | 1761 | |
dfe1ea8f KW |
1762 | complete = ide_cmd_table[val].handler(s, val); |
1763 | if (complete) { | |
1764 | s->status &= ~BUSY_STAT; | |
1765 | assert(!!s->error == !!(s->status & ERR_STAT)); | |
a0436e92 | 1766 | |
dfe1ea8f KW |
1767 | if ((ide_cmd_table[val].flags & SET_DSC) && !s->error) { |
1768 | s->status |= SEEK_STAT; | |
a0436e92 KW |
1769 | } |
1770 | ||
6ef2ba5e | 1771 | ide_set_irq(s->bus); |
6ef2ba5e | 1772 | } |
5391d806 FB |
1773 | } |
1774 | ||
356721ae | 1775 | uint32_t ide_ioport_read(void *opaque, uint32_t addr1) |
5391d806 | 1776 | { |
bcbdc4d3 GH |
1777 | IDEBus *bus = opaque; |
1778 | IDEState *s = idebus_active_if(bus); | |
5391d806 | 1779 | uint32_t addr; |
c2ff060f | 1780 | int ret, hob; |
5391d806 FB |
1781 | |
1782 | addr = addr1 & 7; | |
c2ff060f FB |
1783 | /* FIXME: HOB readback uses bit 7, but it's always set right now */ |
1784 | //hob = s->select & (1 << 7); | |
1785 | hob = 0; | |
5391d806 FB |
1786 | switch(addr) { |
1787 | case 0: | |
1788 | ret = 0xff; | |
1789 | break; | |
1790 | case 1: | |
bcbdc4d3 GH |
1791 | if ((!bus->ifs[0].bs && !bus->ifs[1].bs) || |
1792 | (s != bus->ifs && !s->bs)) | |
c45c3d00 | 1793 | ret = 0; |
c2ff060f | 1794 | else if (!hob) |
c45c3d00 | 1795 | ret = s->error; |
c2ff060f FB |
1796 | else |
1797 | ret = s->hob_feature; | |
5391d806 FB |
1798 | break; |
1799 | case 2: | |
bcbdc4d3 | 1800 | if (!bus->ifs[0].bs && !bus->ifs[1].bs) |
c45c3d00 | 1801 | ret = 0; |
c2ff060f | 1802 | else if (!hob) |
c45c3d00 | 1803 | ret = s->nsector & 0xff; |
c2ff060f FB |
1804 | else |
1805 | ret = s->hob_nsector; | |
5391d806 FB |
1806 | break; |
1807 | case 3: | |
bcbdc4d3 | 1808 | if (!bus->ifs[0].bs && !bus->ifs[1].bs) |
c45c3d00 | 1809 | ret = 0; |
c2ff060f | 1810 | else if (!hob) |
c45c3d00 | 1811 | ret = s->sector; |
c2ff060f FB |
1812 | else |
1813 | ret = s->hob_sector; | |
5391d806 FB |
1814 | break; |
1815 | case 4: | |
bcbdc4d3 | 1816 | if (!bus->ifs[0].bs && !bus->ifs[1].bs) |
c45c3d00 | 1817 | ret = 0; |
c2ff060f | 1818 | else if (!hob) |
c45c3d00 | 1819 | ret = s->lcyl; |
c2ff060f FB |
1820 | else |
1821 | ret = s->hob_lcyl; | |
5391d806 FB |
1822 | break; |
1823 | case 5: | |
bcbdc4d3 | 1824 | if (!bus->ifs[0].bs && !bus->ifs[1].bs) |
c45c3d00 | 1825 | ret = 0; |
c2ff060f | 1826 | else if (!hob) |
c45c3d00 | 1827 | ret = s->hcyl; |
c2ff060f FB |
1828 | else |
1829 | ret = s->hob_hcyl; | |
5391d806 FB |
1830 | break; |
1831 | case 6: | |
bcbdc4d3 | 1832 | if (!bus->ifs[0].bs && !bus->ifs[1].bs) |
c45c3d00 FB |
1833 | ret = 0; |
1834 | else | |
7ae98627 | 1835 | ret = s->select; |
5391d806 FB |
1836 | break; |
1837 | default: | |
1838 | case 7: | |
bcbdc4d3 GH |
1839 | if ((!bus->ifs[0].bs && !bus->ifs[1].bs) || |
1840 | (s != bus->ifs && !s->bs)) | |
c45c3d00 FB |
1841 | ret = 0; |
1842 | else | |
1843 | ret = s->status; | |
9cdd03a7 | 1844 | qemu_irq_lower(bus->irq); |
5391d806 FB |
1845 | break; |
1846 | } | |
1847 | #ifdef DEBUG_IDE | |
1848 | printf("ide: read addr=0x%x val=%02x\n", addr1, ret); | |
1849 | #endif | |
1850 | return ret; | |
1851 | } | |
1852 | ||
356721ae | 1853 | uint32_t ide_status_read(void *opaque, uint32_t addr) |
5391d806 | 1854 | { |
bcbdc4d3 GH |
1855 | IDEBus *bus = opaque; |
1856 | IDEState *s = idebus_active_if(bus); | |
5391d806 | 1857 | int ret; |
7ae98627 | 1858 | |
bcbdc4d3 GH |
1859 | if ((!bus->ifs[0].bs && !bus->ifs[1].bs) || |
1860 | (s != bus->ifs && !s->bs)) | |
7ae98627 FB |
1861 | ret = 0; |
1862 | else | |
1863 | ret = s->status; | |
5391d806 FB |
1864 | #ifdef DEBUG_IDE |
1865 | printf("ide: read status addr=0x%x val=%02x\n", addr, ret); | |
1866 | #endif | |
1867 | return ret; | |
1868 | } | |
1869 | ||
356721ae | 1870 | void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val) |
5391d806 | 1871 | { |
bcbdc4d3 | 1872 | IDEBus *bus = opaque; |
5391d806 FB |
1873 | IDEState *s; |
1874 | int i; | |
1875 | ||
1876 | #ifdef DEBUG_IDE | |
1877 | printf("ide: write control addr=0x%x val=%02x\n", addr, val); | |
1878 | #endif | |
1879 | /* common for both drives */ | |
9cdd03a7 | 1880 | if (!(bus->cmd & IDE_CMD_RESET) && |
5391d806 FB |
1881 | (val & IDE_CMD_RESET)) { |
1882 | /* reset low to high */ | |
1883 | for(i = 0;i < 2; i++) { | |
bcbdc4d3 | 1884 | s = &bus->ifs[i]; |
5391d806 FB |
1885 | s->status = BUSY_STAT | SEEK_STAT; |
1886 | s->error = 0x01; | |
1887 | } | |
9cdd03a7 | 1888 | } else if ((bus->cmd & IDE_CMD_RESET) && |
5391d806 FB |
1889 | !(val & IDE_CMD_RESET)) { |
1890 | /* high to low */ | |
1891 | for(i = 0;i < 2; i++) { | |
bcbdc4d3 | 1892 | s = &bus->ifs[i]; |
cd8722bb | 1893 | if (s->drive_kind == IDE_CD) |
6b136f9e FB |
1894 | s->status = 0x00; /* NOTE: READY is _not_ set */ |
1895 | else | |
56bf1d37 | 1896 | s->status = READY_STAT | SEEK_STAT; |
5391d806 FB |
1897 | ide_set_signature(s); |
1898 | } | |
1899 | } | |
1900 | ||
9cdd03a7 | 1901 | bus->cmd = val; |
5391d806 FB |
1902 | } |
1903 | ||
40c4ed3f KW |
1904 | /* |
1905 | * Returns true if the running PIO transfer is a PIO out (i.e. data is | |
1906 | * transferred from the device to the guest), false if it's a PIO in | |
1907 | */ | |
1908 | static bool ide_is_pio_out(IDEState *s) | |
1909 | { | |
1910 | if (s->end_transfer_func == ide_sector_write || | |
1911 | s->end_transfer_func == ide_atapi_cmd) { | |
1912 | return false; | |
1913 | } else if (s->end_transfer_func == ide_sector_read || | |
1914 | s->end_transfer_func == ide_transfer_stop || | |
1915 | s->end_transfer_func == ide_atapi_cmd_reply_end || | |
1916 | s->end_transfer_func == ide_dummy_transfer_stop) { | |
1917 | return true; | |
1918 | } | |
1919 | ||
1920 | abort(); | |
1921 | } | |
1922 | ||
356721ae | 1923 | void ide_data_writew(void *opaque, uint32_t addr, uint32_t val) |
5391d806 | 1924 | { |
bcbdc4d3 GH |
1925 | IDEBus *bus = opaque; |
1926 | IDEState *s = idebus_active_if(bus); | |
5391d806 FB |
1927 | uint8_t *p; |
1928 | ||
40c4ed3f KW |
1929 | /* PIO data access allowed only when DRQ bit is set. The result of a write |
1930 | * during PIO out is indeterminate, just ignore it. */ | |
1931 | if (!(s->status & DRQ_STAT) || ide_is_pio_out(s)) { | |
fcdd25ab | 1932 | return; |
40c4ed3f | 1933 | } |
fcdd25ab | 1934 | |
5391d806 | 1935 | p = s->data_ptr; |
0c4ad8dc | 1936 | *(uint16_t *)p = le16_to_cpu(val); |
5391d806 FB |
1937 | p += 2; |
1938 | s->data_ptr = p; | |
1939 | if (p >= s->data_end) | |
1940 | s->end_transfer_func(s); | |
1941 | } | |
1942 | ||
356721ae | 1943 | uint32_t ide_data_readw(void *opaque, uint32_t addr) |
5391d806 | 1944 | { |
bcbdc4d3 GH |
1945 | IDEBus *bus = opaque; |
1946 | IDEState *s = idebus_active_if(bus); | |
5391d806 FB |
1947 | uint8_t *p; |
1948 | int ret; | |
fcdd25ab | 1949 | |
40c4ed3f KW |
1950 | /* PIO data access allowed only when DRQ bit is set. The result of a read |
1951 | * during PIO in is indeterminate, return 0 and don't move forward. */ | |
1952 | if (!(s->status & DRQ_STAT) || !ide_is_pio_out(s)) { | |
fcdd25ab | 1953 | return 0; |
40c4ed3f | 1954 | } |
fcdd25ab | 1955 | |
5391d806 | 1956 | p = s->data_ptr; |
0c4ad8dc | 1957 | ret = cpu_to_le16(*(uint16_t *)p); |
5391d806 FB |
1958 | p += 2; |
1959 | s->data_ptr = p; | |
1960 | if (p >= s->data_end) | |
1961 | s->end_transfer_func(s); | |
1962 | return ret; | |
1963 | } | |
1964 | ||
356721ae | 1965 | void ide_data_writel(void *opaque, uint32_t addr, uint32_t val) |
5391d806 | 1966 | { |
bcbdc4d3 GH |
1967 | IDEBus *bus = opaque; |
1968 | IDEState *s = idebus_active_if(bus); | |
5391d806 FB |
1969 | uint8_t *p; |
1970 | ||
40c4ed3f KW |
1971 | /* PIO data access allowed only when DRQ bit is set. The result of a write |
1972 | * during PIO out is indeterminate, just ignore it. */ | |
1973 | if (!(s->status & DRQ_STAT) || ide_is_pio_out(s)) { | |
fcdd25ab | 1974 | return; |
40c4ed3f | 1975 | } |
fcdd25ab | 1976 | |
5391d806 | 1977 | p = s->data_ptr; |
0c4ad8dc | 1978 | *(uint32_t *)p = le32_to_cpu(val); |
5391d806 FB |
1979 | p += 4; |
1980 | s->data_ptr = p; | |
1981 | if (p >= s->data_end) | |
1982 | s->end_transfer_func(s); | |
1983 | } | |
1984 | ||
356721ae | 1985 | uint32_t ide_data_readl(void *opaque, uint32_t addr) |
5391d806 | 1986 | { |
bcbdc4d3 GH |
1987 | IDEBus *bus = opaque; |
1988 | IDEState *s = idebus_active_if(bus); | |
5391d806 FB |
1989 | uint8_t *p; |
1990 | int ret; | |
3b46e624 | 1991 | |
40c4ed3f KW |
1992 | /* PIO data access allowed only when DRQ bit is set. The result of a read |
1993 | * during PIO in is indeterminate, return 0 and don't move forward. */ | |
1994 | if (!(s->status & DRQ_STAT) || !ide_is_pio_out(s)) { | |
fcdd25ab | 1995 | return 0; |
40c4ed3f | 1996 | } |
fcdd25ab | 1997 | |
5391d806 | 1998 | p = s->data_ptr; |
0c4ad8dc | 1999 | ret = cpu_to_le32(*(uint32_t *)p); |
5391d806 FB |
2000 | p += 4; |
2001 | s->data_ptr = p; | |
2002 | if (p >= s->data_end) | |
2003 | s->end_transfer_func(s); | |
2004 | return ret; | |
2005 | } | |
2006 | ||
a7dfe172 FB |
2007 | static void ide_dummy_transfer_stop(IDEState *s) |
2008 | { | |
2009 | s->data_ptr = s->io_buffer; | |
2010 | s->data_end = s->io_buffer; | |
2011 | s->io_buffer[0] = 0xff; | |
2012 | s->io_buffer[1] = 0xff; | |
2013 | s->io_buffer[2] = 0xff; | |
2014 | s->io_buffer[3] = 0xff; | |
2015 | } | |
2016 | ||
4a643563 | 2017 | static void ide_reset(IDEState *s) |
5391d806 | 2018 | { |
4a643563 BS |
2019 | #ifdef DEBUG_IDE |
2020 | printf("ide: reset\n"); | |
2021 | #endif | |
bef0fd59 SH |
2022 | |
2023 | if (s->pio_aiocb) { | |
2024 | bdrv_aio_cancel(s->pio_aiocb); | |
2025 | s->pio_aiocb = NULL; | |
2026 | } | |
2027 | ||
cd8722bb | 2028 | if (s->drive_kind == IDE_CFATA) |
201a51fc AZ |
2029 | s->mult_sectors = 0; |
2030 | else | |
2031 | s->mult_sectors = MAX_MULT_SECTORS; | |
4a643563 BS |
2032 | /* ide regs */ |
2033 | s->feature = 0; | |
2034 | s->error = 0; | |
2035 | s->nsector = 0; | |
2036 | s->sector = 0; | |
2037 | s->lcyl = 0; | |
2038 | s->hcyl = 0; | |
2039 | ||
2040 | /* lba48 */ | |
2041 | s->hob_feature = 0; | |
2042 | s->hob_sector = 0; | |
2043 | s->hob_nsector = 0; | |
2044 | s->hob_lcyl = 0; | |
2045 | s->hob_hcyl = 0; | |
2046 | ||
5391d806 | 2047 | s->select = 0xa0; |
41a2b959 | 2048 | s->status = READY_STAT | SEEK_STAT; |
4a643563 BS |
2049 | |
2050 | s->lba48 = 0; | |
2051 | ||
2052 | /* ATAPI specific */ | |
2053 | s->sense_key = 0; | |
2054 | s->asc = 0; | |
2055 | s->cdrom_changed = 0; | |
2056 | s->packet_transfer_size = 0; | |
2057 | s->elementary_transfer_size = 0; | |
2058 | s->io_buffer_index = 0; | |
2059 | s->cd_sector_size = 0; | |
2060 | s->atapi_dma = 0; | |
a7f3d65b PH |
2061 | s->tray_locked = 0; |
2062 | s->tray_open = 0; | |
4a643563 BS |
2063 | /* ATA DMA state */ |
2064 | s->io_buffer_size = 0; | |
2065 | s->req_nb_sectors = 0; | |
2066 | ||
5391d806 | 2067 | ide_set_signature(s); |
a7dfe172 FB |
2068 | /* init the transfer handler so that 0xffff is returned on data |
2069 | accesses */ | |
2070 | s->end_transfer_func = ide_dummy_transfer_stop; | |
2071 | ide_dummy_transfer_stop(s); | |
201a51fc | 2072 | s->media_changed = 0; |
5391d806 FB |
2073 | } |
2074 | ||
4a643563 BS |
2075 | void ide_bus_reset(IDEBus *bus) |
2076 | { | |
2077 | bus->unit = 0; | |
2078 | bus->cmd = 0; | |
2079 | ide_reset(&bus->ifs[0]); | |
2080 | ide_reset(&bus->ifs[1]); | |
2081 | ide_clear_hob(bus); | |
40a6238a AG |
2082 | |
2083 | /* pending async DMA */ | |
2084 | if (bus->dma->aiocb) { | |
2085 | #ifdef DEBUG_AIO | |
2086 | printf("aio_cancel\n"); | |
2087 | #endif | |
2088 | bdrv_aio_cancel(bus->dma->aiocb); | |
2089 | bus->dma->aiocb = NULL; | |
2090 | } | |
2091 | ||
2092 | /* reset dma provider too */ | |
1374bec0 PB |
2093 | if (bus->dma->ops->reset) { |
2094 | bus->dma->ops->reset(bus->dma); | |
2095 | } | |
4a643563 BS |
2096 | } |
2097 | ||
e4def80b MA |
2098 | static bool ide_cd_is_tray_open(void *opaque) |
2099 | { | |
2100 | return ((IDEState *)opaque)->tray_open; | |
2101 | } | |
2102 | ||
f107639a MA |
2103 | static bool ide_cd_is_medium_locked(void *opaque) |
2104 | { | |
2105 | return ((IDEState *)opaque)->tray_locked; | |
2106 | } | |
2107 | ||
0e49de52 | 2108 | static const BlockDevOps ide_cd_block_ops = { |
145feb17 | 2109 | .change_media_cb = ide_cd_change_cb, |
2df0a3a3 | 2110 | .eject_request_cb = ide_cd_eject_request_cb, |
e4def80b | 2111 | .is_tray_open = ide_cd_is_tray_open, |
f107639a | 2112 | .is_medium_locked = ide_cd_is_medium_locked, |
0e49de52 MA |
2113 | }; |
2114 | ||
1f56e32a | 2115 | int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind, |
95ebda85 | 2116 | const char *version, const char *serial, const char *model, |
ba801960 MA |
2117 | uint64_t wwn, |
2118 | uint32_t cylinders, uint32_t heads, uint32_t secs, | |
2119 | int chs_trans) | |
88804180 | 2120 | { |
88804180 GH |
2121 | uint64_t nb_sectors; |
2122 | ||
f8b6cc00 | 2123 | s->bs = bs; |
1f56e32a MA |
2124 | s->drive_kind = kind; |
2125 | ||
f8b6cc00 | 2126 | bdrv_get_geometry(bs, &nb_sectors); |
870111c8 MA |
2127 | s->cylinders = cylinders; |
2128 | s->heads = heads; | |
2129 | s->sectors = secs; | |
ba801960 | 2130 | s->chs_trans = chs_trans; |
870111c8 | 2131 | s->nb_sectors = nb_sectors; |
95ebda85 | 2132 | s->wwn = wwn; |
870111c8 MA |
2133 | /* The SMART values should be preserved across power cycles |
2134 | but they aren't. */ | |
2135 | s->smart_enabled = 1; | |
2136 | s->smart_autosave = 1; | |
2137 | s->smart_errors = 0; | |
2138 | s->smart_selftest_count = 0; | |
1f56e32a | 2139 | if (kind == IDE_CD) { |
0e49de52 | 2140 | bdrv_set_dev_ops(bs, &ide_cd_block_ops, s); |
1b7fd729 | 2141 | bdrv_set_guest_block_size(bs, 2048); |
7aa9c811 | 2142 | } else { |
98f28ad7 MA |
2143 | if (!bdrv_is_inserted(s->bs)) { |
2144 | error_report("Device needs media, but drive is empty"); | |
2145 | return -1; | |
2146 | } | |
7aa9c811 MA |
2147 | if (bdrv_is_read_only(bs)) { |
2148 | error_report("Can't use a read-only drive"); | |
2149 | return -1; | |
2150 | } | |
88804180 | 2151 | } |
f8b6cc00 | 2152 | if (serial) { |
aa2c91bd | 2153 | pstrcpy(s->drive_serial_str, sizeof(s->drive_serial_str), serial); |
6ced55a5 | 2154 | } else { |
88804180 GH |
2155 | snprintf(s->drive_serial_str, sizeof(s->drive_serial_str), |
2156 | "QM%05d", s->drive_serial); | |
870111c8 | 2157 | } |
27e0c9a1 FB |
2158 | if (model) { |
2159 | pstrcpy(s->drive_model_str, sizeof(s->drive_model_str), model); | |
2160 | } else { | |
2161 | switch (kind) { | |
2162 | case IDE_CD: | |
2163 | strcpy(s->drive_model_str, "QEMU DVD-ROM"); | |
2164 | break; | |
2165 | case IDE_CFATA: | |
2166 | strcpy(s->drive_model_str, "QEMU MICRODRIVE"); | |
2167 | break; | |
2168 | default: | |
2169 | strcpy(s->drive_model_str, "QEMU HARDDISK"); | |
2170 | break; | |
2171 | } | |
2172 | } | |
2173 | ||
47c06340 GH |
2174 | if (version) { |
2175 | pstrcpy(s->version, sizeof(s->version), version); | |
2176 | } else { | |
93bfef4c | 2177 | pstrcpy(s->version, sizeof(s->version), qemu_get_version()); |
47c06340 | 2178 | } |
40a6238a | 2179 | |
88804180 | 2180 | ide_reset(s); |
50fb1900 | 2181 | bdrv_iostatus_enable(bs); |
c4d74df7 | 2182 | return 0; |
88804180 GH |
2183 | } |
2184 | ||
57234ee4 | 2185 | static void ide_init1(IDEBus *bus, int unit) |
d459da0e MA |
2186 | { |
2187 | static int drive_serial = 1; | |
2188 | IDEState *s = &bus->ifs[unit]; | |
2189 | ||
2190 | s->bus = bus; | |
2191 | s->unit = unit; | |
2192 | s->drive_serial = drive_serial++; | |
1b2adf28 | 2193 | /* we need at least 2k alignment for accessing CDROMs using O_DIRECT */ |
50641c5c | 2194 | s->io_buffer_total_len = IDE_DMA_BUF_SECTORS*512 + 4; |
c925400b KW |
2195 | s->io_buffer = qemu_memalign(2048, s->io_buffer_total_len); |
2196 | memset(s->io_buffer, 0, s->io_buffer_total_len); | |
2197 | ||
d459da0e | 2198 | s->smart_selftest_data = qemu_blockalign(s->bs, 512); |
c925400b KW |
2199 | memset(s->smart_selftest_data, 0, 512); |
2200 | ||
bc72ad67 | 2201 | s->sector_write_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
d459da0e | 2202 | ide_sector_write_timer_cb, s); |
57234ee4 MA |
2203 | } |
2204 | ||
40a6238a AG |
2205 | static void ide_nop_start(IDEDMA *dma, IDEState *s, |
2206 | BlockDriverCompletionFunc *cb) | |
2207 | { | |
2208 | } | |
2209 | ||
2210 | static int ide_nop(IDEDMA *dma) | |
2211 | { | |
2212 | return 0; | |
2213 | } | |
2214 | ||
2215 | static int ide_nop_int(IDEDMA *dma, int x) | |
2216 | { | |
2217 | return 0; | |
2218 | } | |
2219 | ||
1dfb4dd9 | 2220 | static void ide_nop_restart(void *opaque, int x, RunState y) |
40a6238a AG |
2221 | { |
2222 | } | |
2223 | ||
2224 | static const IDEDMAOps ide_dma_nop_ops = { | |
2225 | .start_dma = ide_nop_start, | |
2226 | .start_transfer = ide_nop, | |
2227 | .prepare_buf = ide_nop_int, | |
2228 | .rw_buf = ide_nop_int, | |
2229 | .set_unit = ide_nop_int, | |
2230 | .add_status = ide_nop_int, | |
40a6238a | 2231 | .restart_cb = ide_nop_restart, |
40a6238a AG |
2232 | }; |
2233 | ||
2234 | static IDEDMA ide_dma_nop = { | |
2235 | .ops = &ide_dma_nop_ops, | |
2236 | .aiocb = NULL, | |
2237 | }; | |
2238 | ||
57234ee4 MA |
2239 | void ide_init2(IDEBus *bus, qemu_irq irq) |
2240 | { | |
2241 | int i; | |
2242 | ||
2243 | for(i = 0; i < 2; i++) { | |
2244 | ide_init1(bus, i); | |
2245 | ide_reset(&bus->ifs[i]); | |
870111c8 | 2246 | } |
57234ee4 | 2247 | bus->irq = irq; |
40a6238a | 2248 | bus->dma = &ide_dma_nop; |
d459da0e MA |
2249 | } |
2250 | ||
4a91d3b3 RH |
2251 | static const MemoryRegionPortio ide_portio_list[] = { |
2252 | { 0, 8, 1, .read = ide_ioport_read, .write = ide_ioport_write }, | |
2253 | { 0, 2, 2, .read = ide_data_readw, .write = ide_data_writew }, | |
2254 | { 0, 4, 4, .read = ide_data_readl, .write = ide_data_writel }, | |
2255 | PORTIO_END_OF_LIST(), | |
2256 | }; | |
2257 | ||
2258 | static const MemoryRegionPortio ide_portio2_list[] = { | |
2259 | { 0, 1, 1, .read = ide_status_read, .write = ide_cmd_write }, | |
2260 | PORTIO_END_OF_LIST(), | |
2261 | }; | |
2262 | ||
2263 | void ide_init_ioport(IDEBus *bus, ISADevice *dev, int iobase, int iobase2) | |
69b91039 | 2264 | { |
4a91d3b3 RH |
2265 | /* ??? Assume only ISA and PCI configurations, and that the PCI-ISA |
2266 | bridge has been setup properly to always register with ISA. */ | |
2267 | isa_register_portio_list(dev, iobase, ide_portio_list, bus, "ide"); | |
2268 | ||
caed8802 | 2269 | if (iobase2) { |
4a91d3b3 | 2270 | isa_register_portio_list(dev, iobase2, ide_portio2_list, bus, "ide"); |
5391d806 | 2271 | } |
5391d806 | 2272 | } |
69b91039 | 2273 | |
37159f13 | 2274 | static bool is_identify_set(void *opaque, int version_id) |
aa941b94 | 2275 | { |
37159f13 JQ |
2276 | IDEState *s = opaque; |
2277 | ||
2278 | return s->identify_set != 0; | |
2279 | } | |
2280 | ||
50641c5c JQ |
2281 | static EndTransferFunc* transfer_end_table[] = { |
2282 | ide_sector_read, | |
2283 | ide_sector_write, | |
2284 | ide_transfer_stop, | |
2285 | ide_atapi_cmd_reply_end, | |
2286 | ide_atapi_cmd, | |
2287 | ide_dummy_transfer_stop, | |
2288 | }; | |
2289 | ||
2290 | static int transfer_end_table_idx(EndTransferFunc *fn) | |
2291 | { | |
2292 | int i; | |
2293 | ||
2294 | for (i = 0; i < ARRAY_SIZE(transfer_end_table); i++) | |
2295 | if (transfer_end_table[i] == fn) | |
2296 | return i; | |
2297 | ||
2298 | return -1; | |
2299 | } | |
2300 | ||
37159f13 | 2301 | static int ide_drive_post_load(void *opaque, int version_id) |
aa941b94 | 2302 | { |
37159f13 JQ |
2303 | IDEState *s = opaque; |
2304 | ||
7cdd481c PB |
2305 | if (s->identify_set) { |
2306 | bdrv_set_enable_write_cache(s->bs, !!(s->identify_data[85] & (1 << 5))); | |
2307 | } | |
37159f13 | 2308 | return 0; |
aa941b94 AZ |
2309 | } |
2310 | ||
50641c5c JQ |
2311 | static int ide_drive_pio_post_load(void *opaque, int version_id) |
2312 | { | |
2313 | IDEState *s = opaque; | |
2314 | ||
fb60105d | 2315 | if (s->end_transfer_fn_idx >= ARRAY_SIZE(transfer_end_table)) { |
50641c5c JQ |
2316 | return -EINVAL; |
2317 | } | |
2318 | s->end_transfer_func = transfer_end_table[s->end_transfer_fn_idx]; | |
2319 | s->data_ptr = s->io_buffer + s->cur_io_buffer_offset; | |
2320 | s->data_end = s->data_ptr + s->cur_io_buffer_len; | |
2321 | ||
2322 | return 0; | |
2323 | } | |
2324 | ||
2325 | static void ide_drive_pio_pre_save(void *opaque) | |
2326 | { | |
2327 | IDEState *s = opaque; | |
2328 | int idx; | |
2329 | ||
2330 | s->cur_io_buffer_offset = s->data_ptr - s->io_buffer; | |
2331 | s->cur_io_buffer_len = s->data_end - s->data_ptr; | |
2332 | ||
2333 | idx = transfer_end_table_idx(s->end_transfer_func); | |
2334 | if (idx == -1) { | |
2335 | fprintf(stderr, "%s: invalid end_transfer_func for DRQ_STAT\n", | |
2336 | __func__); | |
2337 | s->end_transfer_fn_idx = 2; | |
2338 | } else { | |
2339 | s->end_transfer_fn_idx = idx; | |
2340 | } | |
2341 | } | |
2342 | ||
2343 | static bool ide_drive_pio_state_needed(void *opaque) | |
2344 | { | |
2345 | IDEState *s = opaque; | |
2346 | ||
fdc650d7 KW |
2347 | return ((s->status & DRQ_STAT) != 0) |
2348 | || (s->bus->error_status & BM_STATUS_PIO_RETRY); | |
50641c5c JQ |
2349 | } |
2350 | ||
db118fe7 MA |
2351 | static bool ide_tray_state_needed(void *opaque) |
2352 | { | |
2353 | IDEState *s = opaque; | |
2354 | ||
2355 | return s->tray_open || s->tray_locked; | |
2356 | } | |
2357 | ||
996faf1a AS |
2358 | static bool ide_atapi_gesn_needed(void *opaque) |
2359 | { | |
2360 | IDEState *s = opaque; | |
2361 | ||
2362 | return s->events.new_media || s->events.eject_request; | |
2363 | } | |
2364 | ||
def93791 KW |
2365 | static bool ide_error_needed(void *opaque) |
2366 | { | |
2367 | IDEBus *bus = opaque; | |
2368 | ||
2369 | return (bus->error_status != 0); | |
2370 | } | |
2371 | ||
996faf1a | 2372 | /* Fields for GET_EVENT_STATUS_NOTIFICATION ATAPI command */ |
656fbeff | 2373 | static const VMStateDescription vmstate_ide_atapi_gesn_state = { |
996faf1a AS |
2374 | .name ="ide_drive/atapi/gesn_state", |
2375 | .version_id = 1, | |
2376 | .minimum_version_id = 1, | |
35d08458 | 2377 | .fields = (VMStateField[]) { |
996faf1a AS |
2378 | VMSTATE_BOOL(events.new_media, IDEState), |
2379 | VMSTATE_BOOL(events.eject_request, IDEState), | |
0754f9ec | 2380 | VMSTATE_END_OF_LIST() |
996faf1a AS |
2381 | } |
2382 | }; | |
2383 | ||
db118fe7 MA |
2384 | static const VMStateDescription vmstate_ide_tray_state = { |
2385 | .name = "ide_drive/tray_state", | |
2386 | .version_id = 1, | |
2387 | .minimum_version_id = 1, | |
db118fe7 MA |
2388 | .fields = (VMStateField[]) { |
2389 | VMSTATE_BOOL(tray_open, IDEState), | |
2390 | VMSTATE_BOOL(tray_locked, IDEState), | |
2391 | VMSTATE_END_OF_LIST() | |
2392 | } | |
2393 | }; | |
2394 | ||
656fbeff | 2395 | static const VMStateDescription vmstate_ide_drive_pio_state = { |
50641c5c JQ |
2396 | .name = "ide_drive/pio_state", |
2397 | .version_id = 1, | |
2398 | .minimum_version_id = 1, | |
50641c5c JQ |
2399 | .pre_save = ide_drive_pio_pre_save, |
2400 | .post_load = ide_drive_pio_post_load, | |
35d08458 | 2401 | .fields = (VMStateField[]) { |
50641c5c JQ |
2402 | VMSTATE_INT32(req_nb_sectors, IDEState), |
2403 | VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1, | |
2404 | vmstate_info_uint8, uint8_t), | |
2405 | VMSTATE_INT32(cur_io_buffer_offset, IDEState), | |
2406 | VMSTATE_INT32(cur_io_buffer_len, IDEState), | |
2407 | VMSTATE_UINT8(end_transfer_fn_idx, IDEState), | |
2408 | VMSTATE_INT32(elementary_transfer_size, IDEState), | |
2409 | VMSTATE_INT32(packet_transfer_size, IDEState), | |
2410 | VMSTATE_END_OF_LIST() | |
2411 | } | |
2412 | }; | |
2413 | ||
37159f13 JQ |
2414 | const VMStateDescription vmstate_ide_drive = { |
2415 | .name = "ide_drive", | |
3abb6260 | 2416 | .version_id = 3, |
37159f13 | 2417 | .minimum_version_id = 0, |
37159f13 | 2418 | .post_load = ide_drive_post_load, |
35d08458 | 2419 | .fields = (VMStateField[]) { |
37159f13 JQ |
2420 | VMSTATE_INT32(mult_sectors, IDEState), |
2421 | VMSTATE_INT32(identify_set, IDEState), | |
2422 | VMSTATE_BUFFER_TEST(identify_data, IDEState, is_identify_set), | |
2423 | VMSTATE_UINT8(feature, IDEState), | |
2424 | VMSTATE_UINT8(error, IDEState), | |
2425 | VMSTATE_UINT32(nsector, IDEState), | |
2426 | VMSTATE_UINT8(sector, IDEState), | |
2427 | VMSTATE_UINT8(lcyl, IDEState), | |
2428 | VMSTATE_UINT8(hcyl, IDEState), | |
2429 | VMSTATE_UINT8(hob_feature, IDEState), | |
2430 | VMSTATE_UINT8(hob_sector, IDEState), | |
2431 | VMSTATE_UINT8(hob_nsector, IDEState), | |
2432 | VMSTATE_UINT8(hob_lcyl, IDEState), | |
2433 | VMSTATE_UINT8(hob_hcyl, IDEState), | |
2434 | VMSTATE_UINT8(select, IDEState), | |
2435 | VMSTATE_UINT8(status, IDEState), | |
2436 | VMSTATE_UINT8(lba48, IDEState), | |
2437 | VMSTATE_UINT8(sense_key, IDEState), | |
2438 | VMSTATE_UINT8(asc, IDEState), | |
2439 | VMSTATE_UINT8_V(cdrom_changed, IDEState, 3), | |
37159f13 | 2440 | VMSTATE_END_OF_LIST() |
50641c5c JQ |
2441 | }, |
2442 | .subsections = (VMStateSubsection []) { | |
2443 | { | |
2444 | .vmsd = &vmstate_ide_drive_pio_state, | |
2445 | .needed = ide_drive_pio_state_needed, | |
db118fe7 MA |
2446 | }, { |
2447 | .vmsd = &vmstate_ide_tray_state, | |
2448 | .needed = ide_tray_state_needed, | |
996faf1a AS |
2449 | }, { |
2450 | .vmsd = &vmstate_ide_atapi_gesn_state, | |
2451 | .needed = ide_atapi_gesn_needed, | |
50641c5c JQ |
2452 | }, { |
2453 | /* empty */ | |
2454 | } | |
37159f13 JQ |
2455 | } |
2456 | }; | |
2457 | ||
656fbeff | 2458 | static const VMStateDescription vmstate_ide_error_status = { |
def93791 KW |
2459 | .name ="ide_bus/error", |
2460 | .version_id = 1, | |
2461 | .minimum_version_id = 1, | |
35d08458 | 2462 | .fields = (VMStateField[]) { |
def93791 KW |
2463 | VMSTATE_INT32(error_status, IDEBus), |
2464 | VMSTATE_END_OF_LIST() | |
2465 | } | |
2466 | }; | |
2467 | ||
6521dc62 JQ |
2468 | const VMStateDescription vmstate_ide_bus = { |
2469 | .name = "ide_bus", | |
2470 | .version_id = 1, | |
2471 | .minimum_version_id = 1, | |
35d08458 | 2472 | .fields = (VMStateField[]) { |
6521dc62 JQ |
2473 | VMSTATE_UINT8(cmd, IDEBus), |
2474 | VMSTATE_UINT8(unit, IDEBus), | |
2475 | VMSTATE_END_OF_LIST() | |
def93791 KW |
2476 | }, |
2477 | .subsections = (VMStateSubsection []) { | |
2478 | { | |
2479 | .vmsd = &vmstate_ide_error_status, | |
2480 | .needed = ide_error_needed, | |
2481 | }, { | |
2482 | /* empty */ | |
2483 | } | |
6521dc62 JQ |
2484 | } |
2485 | }; | |
75717903 IY |
2486 | |
2487 | void ide_drive_get(DriveInfo **hd, int max_bus) | |
2488 | { | |
2489 | int i; | |
2490 | ||
2491 | if (drive_get_max_bus(IF_IDE) >= max_bus) { | |
2492 | fprintf(stderr, "qemu: too many IDE bus: %d\n", max_bus); | |
2493 | exit(1); | |
2494 | } | |
2495 | ||
2496 | for(i = 0; i < max_bus * MAX_IDE_DEVS; i++) { | |
2497 | hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS); | |
2498 | } | |
2499 | } |