xref: /openbmc/qemu/include/hw/ide/ide-dev.h (revision 5fc5934a)
1 /*
2  * ide device definitions
3  *
4  * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
5  *
6  * This code is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef IDE_DEV_H
21 #define IDE_DEV_H
22 
23 #include "sysemu/dma.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/block/block.h"
26 
27 typedef struct IDEDevice IDEDevice;
28 typedef struct IDEState IDEState;
29 typedef struct IDEBus IDEBus;
30 
31 typedef void EndTransferFunc(IDEState *);
32 
33 #define MAX_IDE_DEVS 2
34 
35 #define TYPE_IDE_DEVICE "ide-device"
36 OBJECT_DECLARE_TYPE(IDEDevice, IDEDeviceClass, IDE_DEVICE)
37 
38 typedef enum { IDE_HD, IDE_CD, IDE_CFATA } IDEDriveKind;
39 
40 struct unreported_events {
41     bool eject_request;
42     bool new_media;
43 };
44 
45 enum ide_dma_cmd {
46     IDE_DMA_READ = 0,
47     IDE_DMA_WRITE,
48     IDE_DMA_TRIM,
49     IDE_DMA_ATAPI,
50     IDE_DMA__COUNT
51 };
52 
53 /* NOTE: IDEState represents in fact one drive */
54 struct IDEState {
55     IDEBus *bus;
56     uint8_t unit;
57     /* ide config */
58     IDEDriveKind drive_kind;
59     int drive_heads, drive_sectors;
60     int cylinders, heads, sectors, chs_trans;
61     int64_t nb_sectors;
62     int mult_sectors;
63     int identify_set;
64     uint8_t identify_data[512];
65     int drive_serial;
66     char drive_serial_str[21];
67     char drive_model_str[41];
68     uint64_t wwn;
69     /* ide regs */
70     uint8_t feature;
71     uint8_t error;
72     uint32_t nsector;
73     uint8_t sector;
74     uint8_t lcyl;
75     uint8_t hcyl;
76     /* other part of tf for lba48 support */
77     uint8_t hob_feature;
78     uint8_t hob_nsector;
79     uint8_t hob_sector;
80     uint8_t hob_lcyl;
81     uint8_t hob_hcyl;
82 
83     uint8_t select;
84     uint8_t status;
85 
86     bool io8;
87     bool reset_reverts;
88 
89     /* set for lba48 access */
90     uint8_t lba48;
91     BlockBackend *blk;
92     char version[9];
93     /* ATAPI specific */
94     struct unreported_events events;
95     uint8_t sense_key;
96     uint8_t asc;
97     bool tray_open;
98     bool tray_locked;
99     uint8_t cdrom_changed;
100     int packet_transfer_size;
101     int elementary_transfer_size;
102     int32_t io_buffer_index;
103     int lba;
104     int cd_sector_size;
105     int atapi_dma; /* true if dma is requested for the packet cmd */
106     BlockAcctCookie acct;
107     BlockAIOCB *pio_aiocb;
108     QEMUIOVector qiov;
109     QLIST_HEAD(, IDEBufferedRequest) buffered_requests;
110     /* ATA DMA state */
111     uint64_t io_buffer_offset;
112     int32_t io_buffer_size;
113     QEMUSGList sg;
114     /* PIO transfer handling */
115     int req_nb_sectors; /* number of sectors per interrupt */
116     EndTransferFunc *end_transfer_func;
117     uint8_t *data_ptr;
118     uint8_t *data_end;
119     uint8_t *io_buffer;
120     /* PIO save/restore */
121     int32_t io_buffer_total_len;
122     int32_t cur_io_buffer_offset;
123     int32_t cur_io_buffer_len;
124     uint8_t end_transfer_fn_idx;
125     QEMUTimer *sector_write_timer; /* only used for win2k install hack */
126     uint32_t irq_count; /* counts IRQs when using win2k install hack */
127     /* CF-ATA extended error */
128     uint8_t ext_error;
129     /* CF-ATA metadata storage */
130     uint32_t mdata_size;
131     uint8_t *mdata_storage;
132     int media_changed;
133     enum ide_dma_cmd dma_cmd;
134     /* SMART */
135     uint8_t smart_enabled;
136     uint8_t smart_autosave;
137     int smart_errors;
138     uint8_t smart_selftest_count;
139     uint8_t *smart_selftest_data;
140     /* AHCI */
141     int ncq_queues;
142 };
143 
144 struct IDEDeviceClass {
145     DeviceClass parent_class;
146     void (*realize)(IDEDevice *dev, Error **errp);
147 };
148 
149 struct IDEDevice {
150     DeviceState qdev;
151     uint32_t unit;
152     BlockConf conf;
153     int chs_trans;
154     char *version;
155     char *serial;
156     char *model;
157     uint64_t wwn;
158     /*
159      * 0x0000        - rotation rate not reported
160      * 0x0001        - non-rotating medium (SSD)
161      * 0x0002-0x0400 - reserved
162      * 0x0401-0xffe  - rotations per minute
163      * 0xffff        - reserved
164      */
165     uint16_t rotation_rate;
166 };
167 
168 typedef struct IDEDrive {
169     IDEDevice dev;
170 } IDEDrive;
171 
172 #define DEFINE_IDE_DEV_PROPERTIES()                     \
173     DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf),        \
174     DEFINE_BLOCK_ERROR_PROPERTIES(IDEDrive, dev.conf),  \
175     DEFINE_PROP_STRING("ver",  IDEDrive, dev.version),  \
176     DEFINE_PROP_UINT64("wwn",  IDEDrive, dev.wwn, 0),   \
177     DEFINE_PROP_STRING("serial",  IDEDrive, dev.serial),\
178     DEFINE_PROP_STRING("model", IDEDrive, dev.model)
179 
180 void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp);
181 
182 #endif
183