xref: /qemu/include/hw/ide/ide-dev.h (revision 40e074a50964844fab23f7e753e9153b2b1f7b3a)
1bd217d88SThomas Huth /*
2bd217d88SThomas Huth  * ide device definitions
3bd217d88SThomas Huth  *
4bd217d88SThomas Huth  * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
5bd217d88SThomas Huth  *
6bd217d88SThomas Huth  * This code is free software; you can redistribute it and/or
7bd217d88SThomas Huth  * modify it under the terms of the GNU Lesser General Public
8bd217d88SThomas Huth  * License as published by the Free Software Foundation; either
9bd217d88SThomas Huth  * version 2.1 of the License, or (at your option) any later version.
10bd217d88SThomas Huth  *
11bd217d88SThomas Huth  * This library is distributed in the hope that it will be useful,
12bd217d88SThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13bd217d88SThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14bd217d88SThomas Huth  * Lesser General Public License for more details.
15bd217d88SThomas Huth  *
16bd217d88SThomas Huth  * You should have received a copy of the GNU Lesser General Public
17bd217d88SThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18bd217d88SThomas Huth  */
19bd217d88SThomas Huth 
20bd217d88SThomas Huth #ifndef IDE_DEV_H
21bd217d88SThomas Huth #define IDE_DEV_H
22bd217d88SThomas Huth 
235fc5934aSThomas Huth #include "sysemu/dma.h"
24bd217d88SThomas Huth #include "hw/qdev-properties.h"
25bd217d88SThomas Huth #include "hw/block/block.h"
265fc5934aSThomas Huth 
275fc5934aSThomas Huth typedef struct IDEDevice IDEDevice;
285fc5934aSThomas Huth typedef struct IDEState IDEState;
295fc5934aSThomas Huth typedef struct IDEBus IDEBus;
305fc5934aSThomas Huth 
315fc5934aSThomas Huth typedef void EndTransferFunc(IDEState *);
325fc5934aSThomas Huth 
335fc5934aSThomas Huth #define MAX_IDE_DEVS 2
345fc5934aSThomas Huth 
355fc5934aSThomas Huth #define TYPE_IDE_DEVICE "ide-device"
365fc5934aSThomas Huth OBJECT_DECLARE_TYPE(IDEDevice, IDEDeviceClass, IDE_DEVICE)
375fc5934aSThomas Huth 
385fc5934aSThomas Huth typedef enum { IDE_HD, IDE_CD, IDE_CFATA } IDEDriveKind;
395fc5934aSThomas Huth 
405fc5934aSThomas Huth struct unreported_events {
415fc5934aSThomas Huth     bool eject_request;
425fc5934aSThomas Huth     bool new_media;
435fc5934aSThomas Huth };
445fc5934aSThomas Huth 
455fc5934aSThomas Huth enum ide_dma_cmd {
465fc5934aSThomas Huth     IDE_DMA_READ = 0,
475fc5934aSThomas Huth     IDE_DMA_WRITE,
485fc5934aSThomas Huth     IDE_DMA_TRIM,
495fc5934aSThomas Huth     IDE_DMA_ATAPI,
505fc5934aSThomas Huth     IDE_DMA__COUNT
515fc5934aSThomas Huth };
525fc5934aSThomas Huth 
535fc5934aSThomas Huth /* NOTE: IDEState represents in fact one drive */
545fc5934aSThomas Huth struct IDEState {
555fc5934aSThomas Huth     IDEBus *bus;
565fc5934aSThomas Huth     uint8_t unit;
575fc5934aSThomas Huth     /* ide config */
585fc5934aSThomas Huth     IDEDriveKind drive_kind;
595fc5934aSThomas Huth     int drive_heads, drive_sectors;
605fc5934aSThomas Huth     int cylinders, heads, sectors, chs_trans;
615fc5934aSThomas Huth     int64_t nb_sectors;
625fc5934aSThomas Huth     int mult_sectors;
635fc5934aSThomas Huth     int identify_set;
645fc5934aSThomas Huth     uint8_t identify_data[512];
655fc5934aSThomas Huth     int drive_serial;
665fc5934aSThomas Huth     char drive_serial_str[21];
675fc5934aSThomas Huth     char drive_model_str[41];
685fc5934aSThomas Huth     uint64_t wwn;
695fc5934aSThomas Huth     /* ide regs */
705fc5934aSThomas Huth     uint8_t feature;
715fc5934aSThomas Huth     uint8_t error;
725fc5934aSThomas Huth     uint32_t nsector;
735fc5934aSThomas Huth     uint8_t sector;
745fc5934aSThomas Huth     uint8_t lcyl;
755fc5934aSThomas Huth     uint8_t hcyl;
765fc5934aSThomas Huth     /* other part of tf for lba48 support */
775fc5934aSThomas Huth     uint8_t hob_feature;
785fc5934aSThomas Huth     uint8_t hob_nsector;
795fc5934aSThomas Huth     uint8_t hob_sector;
805fc5934aSThomas Huth     uint8_t hob_lcyl;
815fc5934aSThomas Huth     uint8_t hob_hcyl;
825fc5934aSThomas Huth 
835fc5934aSThomas Huth     uint8_t select;
845fc5934aSThomas Huth     uint8_t status;
855fc5934aSThomas Huth 
865fc5934aSThomas Huth     bool io8;
875fc5934aSThomas Huth     bool reset_reverts;
885fc5934aSThomas Huth 
895fc5934aSThomas Huth     /* set for lba48 access */
905fc5934aSThomas Huth     uint8_t lba48;
915fc5934aSThomas Huth     BlockBackend *blk;
925fc5934aSThomas Huth     char version[9];
935fc5934aSThomas Huth     /* ATAPI specific */
945fc5934aSThomas Huth     struct unreported_events events;
955fc5934aSThomas Huth     uint8_t sense_key;
965fc5934aSThomas Huth     uint8_t asc;
975fc5934aSThomas Huth     bool tray_open;
985fc5934aSThomas Huth     bool tray_locked;
995fc5934aSThomas Huth     uint8_t cdrom_changed;
1005fc5934aSThomas Huth     int packet_transfer_size;
1015fc5934aSThomas Huth     int elementary_transfer_size;
1025fc5934aSThomas Huth     int32_t io_buffer_index;
1035fc5934aSThomas Huth     int lba;
1045fc5934aSThomas Huth     int cd_sector_size;
1055fc5934aSThomas Huth     int atapi_dma; /* true if dma is requested for the packet cmd */
1065fc5934aSThomas Huth     BlockAcctCookie acct;
1075fc5934aSThomas Huth     BlockAIOCB *pio_aiocb;
1085fc5934aSThomas Huth     QEMUIOVector qiov;
1095fc5934aSThomas Huth     QLIST_HEAD(, IDEBufferedRequest) buffered_requests;
1105fc5934aSThomas Huth     /* ATA DMA state */
1115fc5934aSThomas Huth     uint64_t io_buffer_offset;
1125fc5934aSThomas Huth     int32_t io_buffer_size;
1135fc5934aSThomas Huth     QEMUSGList sg;
1145fc5934aSThomas Huth     /* PIO transfer handling */
1155fc5934aSThomas Huth     int req_nb_sectors; /* number of sectors per interrupt */
1165fc5934aSThomas Huth     EndTransferFunc *end_transfer_func;
1175fc5934aSThomas Huth     uint8_t *data_ptr;
1185fc5934aSThomas Huth     uint8_t *data_end;
1195fc5934aSThomas Huth     uint8_t *io_buffer;
1205fc5934aSThomas Huth     /* PIO save/restore */
1215fc5934aSThomas Huth     int32_t io_buffer_total_len;
1225fc5934aSThomas Huth     int32_t cur_io_buffer_offset;
1235fc5934aSThomas Huth     int32_t cur_io_buffer_len;
1245fc5934aSThomas Huth     uint8_t end_transfer_fn_idx;
1255fc5934aSThomas Huth     QEMUTimer *sector_write_timer; /* only used for win2k install hack */
1265fc5934aSThomas Huth     uint32_t irq_count; /* counts IRQs when using win2k install hack */
1275fc5934aSThomas Huth     /* CF-ATA extended error */
1285fc5934aSThomas Huth     uint8_t ext_error;
1295fc5934aSThomas Huth     /* CF-ATA metadata storage */
1305fc5934aSThomas Huth     uint32_t mdata_size;
1315fc5934aSThomas Huth     uint8_t *mdata_storage;
1325fc5934aSThomas Huth     int media_changed;
1335fc5934aSThomas Huth     enum ide_dma_cmd dma_cmd;
1345fc5934aSThomas Huth     /* SMART */
1355fc5934aSThomas Huth     uint8_t smart_enabled;
1365fc5934aSThomas Huth     uint8_t smart_autosave;
1375fc5934aSThomas Huth     int smart_errors;
1385fc5934aSThomas Huth     uint8_t smart_selftest_count;
1395fc5934aSThomas Huth     uint8_t *smart_selftest_data;
1405fc5934aSThomas Huth     /* AHCI */
1415fc5934aSThomas Huth     int ncq_queues;
1425fc5934aSThomas Huth };
1435fc5934aSThomas Huth 
1445fc5934aSThomas Huth struct IDEDeviceClass {
1455fc5934aSThomas Huth     DeviceClass parent_class;
1465fc5934aSThomas Huth     void (*realize)(IDEDevice *dev, Error **errp);
1475fc5934aSThomas Huth };
1485fc5934aSThomas Huth 
1495fc5934aSThomas Huth struct IDEDevice {
1505fc5934aSThomas Huth     DeviceState qdev;
1515fc5934aSThomas Huth     uint32_t unit;
1525fc5934aSThomas Huth     BlockConf conf;
1535fc5934aSThomas Huth     int chs_trans;
1545fc5934aSThomas Huth     char *version;
1555fc5934aSThomas Huth     char *serial;
1565fc5934aSThomas Huth     char *model;
1575fc5934aSThomas Huth     uint64_t wwn;
1585fc5934aSThomas Huth     /*
1595fc5934aSThomas Huth      * 0x0000        - rotation rate not reported
1605fc5934aSThomas Huth      * 0x0001        - non-rotating medium (SSD)
1615fc5934aSThomas Huth      * 0x0002-0x0400 - reserved
1625fc5934aSThomas Huth      * 0x0401-0xffe  - rotations per minute
1635fc5934aSThomas Huth      * 0xffff        - reserved
1645fc5934aSThomas Huth      */
1655fc5934aSThomas Huth     uint16_t rotation_rate;
1665fc5934aSThomas Huth };
167bd217d88SThomas Huth 
168bd217d88SThomas Huth typedef struct IDEDrive {
169bd217d88SThomas Huth     IDEDevice dev;
170bd217d88SThomas Huth } IDEDrive;
171bd217d88SThomas Huth 
172bd217d88SThomas Huth #define DEFINE_IDE_DEV_PROPERTIES()                     \
173bd217d88SThomas Huth     DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf),        \
174bd217d88SThomas Huth     DEFINE_BLOCK_ERROR_PROPERTIES(IDEDrive, dev.conf),  \
175bd217d88SThomas Huth     DEFINE_PROP_STRING("ver",  IDEDrive, dev.version),  \
176bd217d88SThomas Huth     DEFINE_PROP_UINT64("wwn",  IDEDrive, dev.wwn, 0),   \
177bd217d88SThomas Huth     DEFINE_PROP_STRING("serial",  IDEDrive, dev.serial),\
178bd217d88SThomas Huth     DEFINE_PROP_STRING("model", IDEDrive, dev.model)
179bd217d88SThomas Huth 
180bd217d88SThomas Huth void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp);
181bd217d88SThomas Huth 
182*40e074a5SThomas Huth void ide_drive_get(DriveInfo **hd, int max_bus);
183*40e074a5SThomas Huth 
184bd217d88SThomas Huth #endif
185