1016
static bool cmd_nop(IDEState *s, uint8_t cmd)
1021
static bool cmd_data_set_management(IDEState *s, uint8_t cmd)
1023
switch (s->feature) {
1026
ide_sector_start_dma(s, IDE_DMA_TRIM);
1032
ide_abort_command(s);
1036
static bool cmd_identify(IDEState *s, uint8_t cmd)
1038
if (s->bs && s->drive_kind != IDE_CD) {
1039
if (s->drive_kind != IDE_CFATA) {
1042
ide_cfata_identify(s);
1044
s->status = READY_STAT | SEEK_STAT;
1045
ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1046
ide_set_irq(s->bus);
1049
if (s->drive_kind == IDE_CD) {
1050
ide_set_signature(s);
1052
ide_abort_command(s);
1058
static bool cmd_verify(IDEState *s, uint8_t cmd)
1060
bool lba48 = (cmd == WIN_VERIFY_EXT);
1062
/* do sector number check ? */
1063
ide_cmd_lba48_transform(s, lba48);
1068
static bool cmd_set_multiple_mode(IDEState *s, uint8_t cmd)
1070
if (s->drive_kind == IDE_CFATA && s->nsector == 0) {
1071
/* Disable Read and Write Multiple */
1072
s->mult_sectors = 0;
1073
} else if ((s->nsector & 0xff) != 0 &&
1074
((s->nsector & 0xff) > MAX_MULT_SECTORS ||
1075
(s->nsector & (s->nsector - 1)) != 0)) {
1076
ide_abort_command(s);
1078
s->mult_sectors = s->nsector & 0xff;
1084
static bool cmd_read_multiple(IDEState *s, uint8_t cmd)
1086
bool lba48 = (cmd == WIN_MULTREAD_EXT);
1088
if (!s->bs || !s->mult_sectors) {
1089
ide_abort_command(s);
1093
ide_cmd_lba48_transform(s, lba48);
1094
s->req_nb_sectors = s->mult_sectors;
1099
static bool cmd_write_multiple(IDEState *s, uint8_t cmd)
1101
bool lba48 = (cmd == WIN_MULTWRITE_EXT);
1104
if (!s->bs || !s->mult_sectors) {
1105
ide_abort_command(s);
1109
ide_cmd_lba48_transform(s, lba48);
1111
s->req_nb_sectors = s->mult_sectors;
1112
n = MIN(s->nsector, s->req_nb_sectors);
1114
s->status = SEEK_STAT | READY_STAT;
1115
ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
1117
s->media_changed = 1;
1122
static bool cmd_read_pio(IDEState *s, uint8_t cmd)
1124
bool lba48 = (cmd == WIN_READ_EXT);
1126
if (s->drive_kind == IDE_CD) {
1127
ide_set_signature(s); /* odd, but ATA4 8.27.5.2 requires it */
1128
ide_abort_command(s);
1133
ide_abort_command(s);
1137
ide_cmd_lba48_transform(s, lba48);
1138
s->req_nb_sectors = 1;
1144
static bool cmd_write_pio(IDEState *s, uint8_t cmd)
1146
bool lba48 = (cmd == WIN_WRITE_EXT);
1149
ide_abort_command(s);
1153
ide_cmd_lba48_transform(s, lba48);
1155
s->req_nb_sectors = 1;
1156
s->status = SEEK_STAT | READY_STAT;
1157
ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1159
s->media_changed = 1;
1164
static bool cmd_read_dma(IDEState *s, uint8_t cmd)
1166
bool lba48 = (cmd == WIN_READDMA_EXT);
1169
ide_abort_command(s);
1173
ide_cmd_lba48_transform(s, lba48);
1174
ide_sector_start_dma(s, IDE_DMA_READ);
1179
static bool cmd_write_dma(IDEState *s, uint8_t cmd)
1181
bool lba48 = (cmd == WIN_WRITEDMA_EXT);
1184
ide_abort_command(s);
1188
ide_cmd_lba48_transform(s, lba48);
1189
ide_sector_start_dma(s, IDE_DMA_WRITE);
1191
s->media_changed = 1;
1196
static bool cmd_flush_cache(IDEState *s, uint8_t cmd)
1202
static bool cmd_seek(IDEState *s, uint8_t cmd)
1204
/* XXX: Check that seek is within bounds */
1208
static bool cmd_read_native_max(IDEState *s, uint8_t cmd)
1210
bool lba48 = (cmd == WIN_READ_NATIVE_MAX_EXT);
1212
/* Refuse if no sectors are addressable (e.g. medium not inserted) */
1213
if (s->nb_sectors == 0) {
1214
ide_abort_command(s);
1218
ide_cmd_lba48_transform(s, lba48);
1219
ide_set_sector(s, s->nb_sectors - 1);
1224
static bool cmd_check_power_mode(IDEState *s, uint8_t cmd)
1226
s->nsector = 0xff; /* device active or idle */
1230
static bool cmd_set_features(IDEState *s, uint8_t cmd)
1232
uint16_t *identify_data;
1235
ide_abort_command(s);
1239
/* XXX: valid for CDROM ? */
1240
switch (s->feature) {
1241
case 0x02: /* write cache enable */
1242
bdrv_set_enable_write_cache(s->bs, true);
1243
identify_data = (uint16_t *)s->identify_data;
1244
put_le16(identify_data + 85, (1 << 14) | (1 << 5) | 1);
1246
case 0x82: /* write cache disable */
1247
bdrv_set_enable_write_cache(s->bs, false);
1248
identify_data = (uint16_t *)s->identify_data;
1249
put_le16(identify_data + 85, (1 << 14) | 1);
1252
case 0xcc: /* reverting to power-on defaults enable */
1253
case 0x66: /* reverting to power-on defaults disable */
1254
case 0xaa: /* read look-ahead enable */
1255
case 0x55: /* read look-ahead disable */
1256
case 0x05: /* set advanced power management mode */
1257
case 0x85: /* disable advanced power management mode */
1258
case 0x69: /* NOP */
1259
case 0x67: /* NOP */
1260
case 0x96: /* NOP */
1261
case 0x9a: /* NOP */
1262
case 0x42: /* enable Automatic Acoustic Mode */
1263
case 0xc2: /* disable Automatic Acoustic Mode */
1265
case 0x03: /* set transfer mode */
1267
uint8_t val = s->nsector & 0x07;
1268
identify_data = (uint16_t *)s->identify_data;
1270
switch (s->nsector >> 3) {
1271
case 0x00: /* pio default */
1272
case 0x01: /* pio mode */
1273
put_le16(identify_data + 62, 0x07);
1274
put_le16(identify_data + 63, 0x07);
1275
put_le16(identify_data + 88, 0x3f);
1277
case 0x02: /* sigle word dma mode*/
1278
put_le16(identify_data + 62, 0x07 | (1 << (val + 8)));
1279
put_le16(identify_data + 63, 0x07);
1280
put_le16(identify_data + 88, 0x3f);
1282
case 0x04: /* mdma mode */
1283
put_le16(identify_data + 62, 0x07);
1284
put_le16(identify_data + 63, 0x07 | (1 << (val + 8)));
1285
put_le16(identify_data + 88, 0x3f);
1287
case 0x08: /* udma mode */
1288
put_le16(identify_data + 62, 0x07);
1289
put_le16(identify_data + 63, 0x07);
1290
put_le16(identify_data + 88, 0x3f | (1 << (val + 8)));
1300
ide_abort_command(s);
1305
/*** ATAPI commands ***/
1307
static bool cmd_identify_packet(IDEState *s, uint8_t cmd)
1309
ide_atapi_identify(s);
1310
s->status = READY_STAT | SEEK_STAT;
1311
ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1312
ide_set_irq(s->bus);
1316
static bool cmd_exec_dev_diagnostic(IDEState *s, uint8_t cmd)
1318
ide_set_signature(s);
1320
if (s->drive_kind == IDE_CD) {
1321
s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
1322
* devices to return a clear status register
1323
* with READY_STAT *not* set. */
1325
s->status = READY_STAT | SEEK_STAT;
1326
/* The bits of the error register are not as usual for this command!
1327
* They are part of the regular output (this is why ERR_STAT isn't set)
1328
* Device 0 passed, Device 1 passed or not present. */
1330
ide_set_irq(s->bus);
1336
static bool cmd_device_reset(IDEState *s, uint8_t cmd)
1338
ide_set_signature(s);
1339
s->status = 0x00; /* NOTE: READY is _not_ set */
1345
static bool cmd_packet(IDEState *s, uint8_t cmd)
1347
/* overlapping commands not supported */
1348
if (s->feature & 0x02) {
1349
ide_abort_command(s);
1353
s->status = READY_STAT | SEEK_STAT;
1354
s->atapi_dma = s->feature & 1;
1356
ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
1362
/*** CF-ATA commands ***/
1364
static bool cmd_cfa_req_ext_error_code(IDEState *s, uint8_t cmd)
1366
s->error = 0x09; /* miscellaneous error */
1367
s->status = READY_STAT | SEEK_STAT;
1368
ide_set_irq(s->bus);
1373
static bool cmd_cfa_erase_sectors(IDEState *s, uint8_t cmd)
1375
/* WIN_SECURITY_FREEZE_LOCK has the same ID as CFA_WEAR_LEVEL and is
1376
* required for Windows 8 to work with AHCI */
1378
if (cmd == CFA_WEAR_LEVEL) {
1382
if (cmd == CFA_ERASE_SECTORS) {
1383
s->media_changed = 1;
1389
static bool cmd_cfa_translate_sector(IDEState *s, uint8_t cmd)
1391
s->status = READY_STAT | SEEK_STAT;
1393
memset(s->io_buffer, 0, 0x200);
1394
s->io_buffer[0x00] = s->hcyl; /* Cyl MSB */
1395
s->io_buffer[0x01] = s->lcyl; /* Cyl LSB */
1396
s->io_buffer[0x02] = s->select; /* Head */
1397
s->io_buffer[0x03] = s->sector; /* Sector */
1398
s->io_buffer[0x04] = ide_get_sector(s) >> 16; /* LBA MSB */
1399
s->io_buffer[0x05] = ide_get_sector(s) >> 8; /* LBA */
1400
s->io_buffer[0x06] = ide_get_sector(s) >> 0; /* LBA LSB */
1401
s->io_buffer[0x13] = 0x00; /* Erase flag */
1402
s->io_buffer[0x18] = 0x00; /* Hot count */
1403
s->io_buffer[0x19] = 0x00; /* Hot count */
1404
s->io_buffer[0x1a] = 0x01; /* Hot count */
1406
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1407
ide_set_irq(s->bus);
1412
static bool cmd_cfa_access_metadata_storage(IDEState *s, uint8_t cmd)
1414
switch (s->feature) {
1415
case 0x02: /* Inquiry Metadata Storage */
1416
ide_cfata_metadata_inquiry(s);
1418
case 0x03: /* Read Metadata Storage */
1419
ide_cfata_metadata_read(s);
1421
case 0x04: /* Write Metadata Storage */
1422
ide_cfata_metadata_write(s);
1425
ide_abort_command(s);
1429
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1430
s->status = 0x00; /* NOTE: READY is _not_ set */
1431
ide_set_irq(s->bus);
1436
static bool cmd_ibm_sense_condition(IDEState *s, uint8_t cmd)
1438
switch (s->feature) {
1439
case 0x01: /* sense temperature in device */
1440
s->nsector = 0x50; /* +20 C */
1443
ide_abort_command(s);
1451
/*** SMART commands ***/
1453
static bool cmd_smart(IDEState *s, uint8_t cmd)
1457
if (s->hcyl != 0xc2 || s->lcyl != 0x4f) {
1461
if (!s->smart_enabled && s->feature != SMART_ENABLE) {
1465
switch (s->feature) {
1467
s->smart_enabled = 0;
1471
s->smart_enabled = 1;
1474
case SMART_ATTR_AUTOSAVE:
1475
switch (s->sector) {
1477
s->smart_autosave = 0;
1480
s->smart_autosave = 1;
1488
if (!s->smart_errors) {
1497
case SMART_READ_THRESH:
1498
memset(s->io_buffer, 0, 0x200);
1499
s->io_buffer[0] = 0x01; /* smart struct version */
1501
for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) {
1502
s->io_buffer[2 + 0 + (n * 12)] = smart_attributes[n][0];
1503
s->io_buffer[2 + 1 + (n * 12)] = smart_attributes[n][11];
1507
for (n = 0; n < 511; n++) {
1508
s->io_buffer[511] += s->io_buffer[n];
1510
s->io_buffer[511] = 0x100 - s->io_buffer[511];
1512
s->status = READY_STAT | SEEK_STAT;
1513
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1514
ide_set_irq(s->bus);
1517
case SMART_READ_DATA:
1518
memset(s->io_buffer, 0, 0x200);
1519
s->io_buffer[0] = 0x01; /* smart struct version */
1521
for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) {
1523
for (i = 0; i < 11; i++) {
1524
s->io_buffer[2 + i + (n * 12)] = smart_attributes[n][i];
1528
s->io_buffer[362] = 0x02 | (s->smart_autosave ? 0x80 : 0x00);
1529
if (s->smart_selftest_count == 0) {
1530
s->io_buffer[363] = 0;
1533
s->smart_selftest_data[3 +
1534
(s->smart_selftest_count - 1) *
1537
s->io_buffer[364] = 0x20;
1538
s->io_buffer[365] = 0x01;
1539
/* offline data collection capacity: execute + self-test*/
1540
s->io_buffer[367] = (1 << 4 | 1 << 3 | 1);
1541
s->io_buffer[368] = 0x03; /* smart capability (1) */
1542
s->io_buffer[369] = 0x00; /* smart capability (2) */
1543
s->io_buffer[370] = 0x01; /* error logging supported */
1544
s->io_buffer[372] = 0x02; /* minutes for poll short test */
1545
s->io_buffer[373] = 0x36; /* minutes for poll ext test */
1546
s->io_buffer[374] = 0x01; /* minutes for poll conveyance */
1548
for (n = 0; n < 511; n++) {
1549
s->io_buffer[511] += s->io_buffer[n];
1551
s->io_buffer[511] = 0x100 - s->io_buffer[511];
1553
s->status = READY_STAT | SEEK_STAT;
1554
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1555
ide_set_irq(s->bus);
1558
case SMART_READ_LOG:
1559
switch (s->sector) {
1560
case 0x01: /* summary smart error log */
1561
memset(s->io_buffer, 0, 0x200);
1562
s->io_buffer[0] = 0x01;
1563
s->io_buffer[1] = 0x00; /* no error entries */
1564
s->io_buffer[452] = s->smart_errors & 0xff;
1565
s->io_buffer[453] = (s->smart_errors & 0xff00) >> 8;
1567
for (n = 0; n < 511; n++) {
1568
s->io_buffer[511] += s->io_buffer[n];
1570
s->io_buffer[511] = 0x100 - s->io_buffer[511];
1572
case 0x06: /* smart self test log */
1573
memset(s->io_buffer, 0, 0x200);
1574
s->io_buffer[0] = 0x01;
1575
if (s->smart_selftest_count == 0) {
1576
s->io_buffer[508] = 0;
1578
s->io_buffer[508] = s->smart_selftest_count;
1579
for (n = 2; n < 506; n++) {
1580
s->io_buffer[n] = s->smart_selftest_data[n];
1584
for (n = 0; n < 511; n++) {
1585
s->io_buffer[511] += s->io_buffer[n];
1587
s->io_buffer[511] = 0x100 - s->io_buffer[511];
1592
s->status = READY_STAT | SEEK_STAT;
1593
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1594
ide_set_irq(s->bus);
1597
case SMART_EXECUTE_OFFLINE:
1598
switch (s->sector) {
1599
case 0: /* off-line routine */
1600
case 1: /* short self test */
1601
case 2: /* extended self test */
1602
s->smart_selftest_count++;
1603
if (s->smart_selftest_count > 21) {
1604
s->smart_selftest_count = 0;
1606
n = 2 + (s->smart_selftest_count - 1) * 24;
1607
s->smart_selftest_data[n] = s->sector;
1608
s->smart_selftest_data[n + 1] = 0x00; /* OK and finished */
1609
s->smart_selftest_data[n + 2] = 0x34; /* hour count lsb */
1610
s->smart_selftest_data[n + 3] = 0x12; /* hour count msb */
1619
ide_abort_command(s);
1006
1623
#define HD_OK (1u << IDE_HD)
1007
1624
#define CD_OK (1u << IDE_CD)
1008
1625
#define CFA_OK (1u << IDE_CFATA)
1009
1626
#define HD_CFA_OK (HD_OK | CFA_OK)
1010
1627
#define ALL_OK (HD_OK | CD_OK | CFA_OK)
1629
/* Set the Disk Seek Completed status bit during completion */
1630
#define SET_DSC (1u << 8)
1012
1632
/* See ACS-2 T13/2015-D Table B.2 Command codes */
1013
static const uint8_t ide_cmd_table[0x100] = {
1633
static const struct {
1634
/* Returns true if the completion code should be run */
1635
bool (*handler)(IDEState *s, uint8_t cmd);
1637
} ide_cmd_table[0x100] = {
1014
1638
/* NOP not implemented, mandatory for CD */
1015
[CFA_REQ_EXT_ERROR_CODE] = CFA_OK,
1017
[WIN_DEVICE_RESET] = CD_OK,
1018
[WIN_RECAL] = HD_CFA_OK,
1019
[WIN_READ] = ALL_OK,
1020
[WIN_READ_ONCE] = ALL_OK,
1021
[WIN_READ_EXT] = HD_CFA_OK,
1022
[WIN_READDMA_EXT] = HD_CFA_OK,
1023
[WIN_READ_NATIVE_MAX_EXT] = HD_CFA_OK,
1024
[WIN_MULTREAD_EXT] = HD_CFA_OK,
1025
[WIN_WRITE] = HD_CFA_OK,
1026
[WIN_WRITE_ONCE] = HD_CFA_OK,
1027
[WIN_WRITE_EXT] = HD_CFA_OK,
1028
[WIN_WRITEDMA_EXT] = HD_CFA_OK,
1029
[CFA_WRITE_SECT_WO_ERASE] = CFA_OK,
1030
[WIN_MULTWRITE_EXT] = HD_CFA_OK,
1031
[WIN_WRITE_VERIFY] = HD_CFA_OK,
1032
[WIN_VERIFY] = HD_CFA_OK,
1033
[WIN_VERIFY_ONCE] = HD_CFA_OK,
1034
[WIN_VERIFY_EXT] = HD_CFA_OK,
1035
[WIN_SEEK] = HD_CFA_OK,
1036
[CFA_TRANSLATE_SECTOR] = CFA_OK,
1037
[WIN_DIAGNOSE] = ALL_OK,
1038
[WIN_SPECIFY] = HD_CFA_OK,
1039
[WIN_STANDBYNOW2] = ALL_OK,
1040
[WIN_IDLEIMMEDIATE2] = ALL_OK,
1041
[WIN_STANDBY2] = ALL_OK,
1042
[WIN_SETIDLE2] = ALL_OK,
1043
[WIN_CHECKPOWERMODE2] = ALL_OK,
1044
[WIN_SLEEPNOW2] = ALL_OK,
1045
[WIN_PACKETCMD] = CD_OK,
1046
[WIN_PIDENTIFY] = CD_OK,
1047
[WIN_SMART] = HD_CFA_OK,
1048
[CFA_ACCESS_METADATA_STORAGE] = CFA_OK,
1049
[CFA_ERASE_SECTORS] = CFA_OK,
1050
[WIN_MULTREAD] = HD_CFA_OK,
1051
[WIN_MULTWRITE] = HD_CFA_OK,
1052
[WIN_SETMULT] = HD_CFA_OK,
1053
[WIN_READDMA] = HD_CFA_OK,
1054
[WIN_READDMA_ONCE] = HD_CFA_OK,
1055
[WIN_WRITEDMA] = HD_CFA_OK,
1056
[WIN_WRITEDMA_ONCE] = HD_CFA_OK,
1057
[CFA_WRITE_MULTI_WO_ERASE] = CFA_OK,
1058
[WIN_STANDBYNOW1] = ALL_OK,
1059
[WIN_IDLEIMMEDIATE] = ALL_OK,
1060
[WIN_STANDBY] = ALL_OK,
1061
[WIN_SETIDLE1] = ALL_OK,
1062
[WIN_CHECKPOWERMODE1] = ALL_OK,
1063
[WIN_SLEEPNOW1] = ALL_OK,
1064
[WIN_FLUSH_CACHE] = ALL_OK,
1065
[WIN_FLUSH_CACHE_EXT] = HD_CFA_OK,
1066
[WIN_IDENTIFY] = ALL_OK,
1067
[WIN_SETFEATURES] = ALL_OK,
1068
[IBM_SENSE_CONDITION] = CFA_OK,
1069
[CFA_WEAR_LEVEL] = HD_CFA_OK,
1070
[WIN_READ_NATIVE_MAX] = ALL_OK,
1639
[CFA_REQ_EXT_ERROR_CODE] = { cmd_cfa_req_ext_error_code, CFA_OK },
1640
[WIN_DSM] = { cmd_data_set_management, ALL_OK },
1641
[WIN_DEVICE_RESET] = { cmd_device_reset, CD_OK },
1642
[WIN_RECAL] = { cmd_nop, HD_CFA_OK | SET_DSC},
1643
[WIN_READ] = { cmd_read_pio, ALL_OK },
1644
[WIN_READ_ONCE] = { cmd_read_pio, ALL_OK },
1645
[WIN_READ_EXT] = { cmd_read_pio, HD_CFA_OK },
1646
[WIN_READDMA_EXT] = { cmd_read_dma, HD_CFA_OK },
1647
[WIN_READ_NATIVE_MAX_EXT] = { cmd_read_native_max, HD_CFA_OK | SET_DSC },
1648
[WIN_MULTREAD_EXT] = { cmd_read_multiple, HD_CFA_OK },
1649
[WIN_WRITE] = { cmd_write_pio, HD_CFA_OK },
1650
[WIN_WRITE_ONCE] = { cmd_write_pio, HD_CFA_OK },
1651
[WIN_WRITE_EXT] = { cmd_write_pio, HD_CFA_OK },
1652
[WIN_WRITEDMA_EXT] = { cmd_write_dma, HD_CFA_OK },
1653
[CFA_WRITE_SECT_WO_ERASE] = { cmd_write_pio, CFA_OK },
1654
[WIN_MULTWRITE_EXT] = { cmd_write_multiple, HD_CFA_OK },
1655
[WIN_WRITE_VERIFY] = { cmd_write_pio, HD_CFA_OK },
1656
[WIN_VERIFY] = { cmd_verify, HD_CFA_OK | SET_DSC },
1657
[WIN_VERIFY_ONCE] = { cmd_verify, HD_CFA_OK | SET_DSC },
1658
[WIN_VERIFY_EXT] = { cmd_verify, HD_CFA_OK | SET_DSC },
1659
[WIN_SEEK] = { cmd_seek, HD_CFA_OK | SET_DSC },
1660
[CFA_TRANSLATE_SECTOR] = { cmd_cfa_translate_sector, CFA_OK },
1661
[WIN_DIAGNOSE] = { cmd_exec_dev_diagnostic, ALL_OK },
1662
[WIN_SPECIFY] = { cmd_nop, HD_CFA_OK | SET_DSC },
1663
[WIN_STANDBYNOW2] = { cmd_nop, ALL_OK },
1664
[WIN_IDLEIMMEDIATE2] = { cmd_nop, ALL_OK },
1665
[WIN_STANDBY2] = { cmd_nop, ALL_OK },
1666
[WIN_SETIDLE2] = { cmd_nop, ALL_OK },
1667
[WIN_CHECKPOWERMODE2] = { cmd_check_power_mode, ALL_OK | SET_DSC },
1668
[WIN_SLEEPNOW2] = { cmd_nop, ALL_OK },
1669
[WIN_PACKETCMD] = { cmd_packet, CD_OK },
1670
[WIN_PIDENTIFY] = { cmd_identify_packet, CD_OK },
1671
[WIN_SMART] = { cmd_smart, HD_CFA_OK | SET_DSC },
1672
[CFA_ACCESS_METADATA_STORAGE] = { cmd_cfa_access_metadata_storage, CFA_OK },
1673
[CFA_ERASE_SECTORS] = { cmd_cfa_erase_sectors, CFA_OK | SET_DSC },
1674
[WIN_MULTREAD] = { cmd_read_multiple, HD_CFA_OK },
1675
[WIN_MULTWRITE] = { cmd_write_multiple, HD_CFA_OK },
1676
[WIN_SETMULT] = { cmd_set_multiple_mode, HD_CFA_OK | SET_DSC },
1677
[WIN_READDMA] = { cmd_read_dma, HD_CFA_OK },
1678
[WIN_READDMA_ONCE] = { cmd_read_dma, HD_CFA_OK },
1679
[WIN_WRITEDMA] = { cmd_write_dma, HD_CFA_OK },
1680
[WIN_WRITEDMA_ONCE] = { cmd_write_dma, HD_CFA_OK },
1681
[CFA_WRITE_MULTI_WO_ERASE] = { cmd_write_multiple, CFA_OK },
1682
[WIN_STANDBYNOW1] = { cmd_nop, ALL_OK },
1683
[WIN_IDLEIMMEDIATE] = { cmd_nop, ALL_OK },
1684
[WIN_STANDBY] = { cmd_nop, ALL_OK },
1685
[WIN_SETIDLE1] = { cmd_nop, ALL_OK },
1686
[WIN_CHECKPOWERMODE1] = { cmd_check_power_mode, ALL_OK | SET_DSC },
1687
[WIN_SLEEPNOW1] = { cmd_nop, ALL_OK },
1688
[WIN_FLUSH_CACHE] = { cmd_flush_cache, ALL_OK },
1689
[WIN_FLUSH_CACHE_EXT] = { cmd_flush_cache, HD_CFA_OK },
1690
[WIN_IDENTIFY] = { cmd_identify, ALL_OK },
1691
[WIN_SETFEATURES] = { cmd_set_features, ALL_OK | SET_DSC },
1692
[IBM_SENSE_CONDITION] = { cmd_ibm_sense_condition, CFA_OK | SET_DSC },
1693
[CFA_WEAR_LEVEL] = { cmd_cfa_erase_sectors, HD_CFA_OK | SET_DSC },
1694
[WIN_READ_NATIVE_MAX] = { cmd_read_native_max, ALL_OK | SET_DSC },
1073
1697
static bool ide_cmd_permitted(IDEState *s, uint32_t cmd)
1075
1699
return cmd < ARRAY_SIZE(ide_cmd_table)
1076
&& (ide_cmd_table[cmd] & (1u << s->drive_kind));
1700
&& (ide_cmd_table[cmd].flags & (1u << s->drive_kind));
1079
1703
void ide_exec_cmd(IDEBus *bus, uint32_t val)
1081
uint16_t *identify_data;
1086
1708
#if defined(DEBUG_IDE)
1087
1709
printf("ide: CMD=%02x\n", val);
1098
1720
if (!ide_cmd_permitted(s, val)) {
1104
switch (s->feature) {
1109
ide_sector_start_dma(s, IDE_DMA_TRIM);
1116
if (s->bs && s->drive_kind != IDE_CD) {
1117
if (s->drive_kind != IDE_CFATA)
1120
ide_cfata_identify(s);
1121
s->status = READY_STAT | SEEK_STAT;
1122
ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1124
if (s->drive_kind == IDE_CD) {
1125
ide_set_signature(s);
1127
ide_abort_command(s);
1129
ide_set_irq(s->bus);
1134
s->status = READY_STAT | SEEK_STAT;
1135
ide_set_irq(s->bus);
1138
if (s->drive_kind == IDE_CFATA && s->nsector == 0) {
1139
/* Disable Read and Write Multiple */
1140
s->mult_sectors = 0;
1141
s->status = READY_STAT | SEEK_STAT;
1142
} else if ((s->nsector & 0xff) != 0 &&
1143
((s->nsector & 0xff) > MAX_MULT_SECTORS ||
1144
(s->nsector & (s->nsector - 1)) != 0)) {
1145
ide_abort_command(s);
1147
s->mult_sectors = s->nsector & 0xff;
1148
s->status = READY_STAT | SEEK_STAT;
1150
ide_set_irq(s->bus);
1153
case WIN_VERIFY_EXT:
1157
case WIN_VERIFY_ONCE:
1158
/* do sector number check ? */
1159
ide_cmd_lba48_transform(s, lba48);
1160
s->status = READY_STAT | SEEK_STAT;
1161
ide_set_irq(s->bus);
1169
if (s->drive_kind == IDE_CD) {
1170
ide_set_signature(s); /* odd, but ATA4 8.27.5.2 requires it */
1176
ide_cmd_lba48_transform(s, lba48);
1177
s->req_nb_sectors = 1;
1185
case WIN_WRITE_ONCE:
1186
case CFA_WRITE_SECT_WO_ERASE:
1187
case WIN_WRITE_VERIFY:
1191
ide_cmd_lba48_transform(s, lba48);
1193
s->status = SEEK_STAT | READY_STAT;
1194
s->req_nb_sectors = 1;
1195
ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1196
s->media_changed = 1;
1199
case WIN_MULTREAD_EXT:
1206
if (!s->mult_sectors) {
1209
ide_cmd_lba48_transform(s, lba48);
1210
s->req_nb_sectors = s->mult_sectors;
1214
case WIN_MULTWRITE_EXT:
1218
case CFA_WRITE_MULTI_WO_ERASE:
1222
if (!s->mult_sectors) {
1225
ide_cmd_lba48_transform(s, lba48);
1227
s->status = SEEK_STAT | READY_STAT;
1228
s->req_nb_sectors = s->mult_sectors;
1230
if (n > s->req_nb_sectors)
1231
n = s->req_nb_sectors;
1232
ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
1233
s->media_changed = 1;
1236
case WIN_READDMA_EXT:
1240
case WIN_READDMA_ONCE:
1244
ide_cmd_lba48_transform(s, lba48);
1245
ide_sector_start_dma(s, IDE_DMA_READ);
1248
case WIN_WRITEDMA_EXT:
1252
case WIN_WRITEDMA_ONCE:
1256
ide_cmd_lba48_transform(s, lba48);
1257
ide_sector_start_dma(s, IDE_DMA_WRITE);
1258
s->media_changed = 1;
1261
case WIN_READ_NATIVE_MAX_EXT:
1264
case WIN_READ_NATIVE_MAX:
1265
/* Refuse if no sectors are addressable (e.g. medium not inserted) */
1266
if (s->nb_sectors == 0) {
1269
ide_cmd_lba48_transform(s, lba48);
1270
ide_set_sector(s, s->nb_sectors - 1);
1271
s->status = READY_STAT | SEEK_STAT;
1272
ide_set_irq(s->bus);
1275
case WIN_CHECKPOWERMODE1:
1276
case WIN_CHECKPOWERMODE2:
1278
s->nsector = 0xff; /* device active or idle */
1279
s->status = READY_STAT | SEEK_STAT;
1280
ide_set_irq(s->bus);
1282
case WIN_SETFEATURES:
1285
/* XXX: valid for CDROM ? */
1286
switch(s->feature) {
1287
case 0x02: /* write cache enable */
1288
bdrv_set_enable_write_cache(s->bs, true);
1289
identify_data = (uint16_t *)s->identify_data;
1290
put_le16(identify_data + 85, (1 << 14) | (1 << 5) | 1);
1291
s->status = READY_STAT | SEEK_STAT;
1292
ide_set_irq(s->bus);
1294
case 0x82: /* write cache disable */
1295
bdrv_set_enable_write_cache(s->bs, false);
1296
identify_data = (uint16_t *)s->identify_data;
1297
put_le16(identify_data + 85, (1 << 14) | 1);
1300
case 0xcc: /* reverting to power-on defaults enable */
1301
case 0x66: /* reverting to power-on defaults disable */
1302
case 0xaa: /* read look-ahead enable */
1303
case 0x55: /* read look-ahead disable */
1304
case 0x05: /* set advanced power management mode */
1305
case 0x85: /* disable advanced power management mode */
1306
case 0x69: /* NOP */
1307
case 0x67: /* NOP */
1308
case 0x96: /* NOP */
1309
case 0x9a: /* NOP */
1310
case 0x42: /* enable Automatic Acoustic Mode */
1311
case 0xc2: /* disable Automatic Acoustic Mode */
1312
s->status = READY_STAT | SEEK_STAT;
1313
ide_set_irq(s->bus);
1315
case 0x03: { /* set transfer mode */
1316
uint8_t val = s->nsector & 0x07;
1317
identify_data = (uint16_t *)s->identify_data;
1319
switch (s->nsector >> 3) {
1320
case 0x00: /* pio default */
1321
case 0x01: /* pio mode */
1322
put_le16(identify_data + 62,0x07);
1323
put_le16(identify_data + 63,0x07);
1324
put_le16(identify_data + 88,0x3f);
1326
case 0x02: /* sigle word dma mode*/
1327
put_le16(identify_data + 62,0x07 | (1 << (val + 8)));
1328
put_le16(identify_data + 63,0x07);
1329
put_le16(identify_data + 88,0x3f);
1331
case 0x04: /* mdma mode */
1332
put_le16(identify_data + 62,0x07);
1333
put_le16(identify_data + 63,0x07 | (1 << (val + 8)));
1334
put_le16(identify_data + 88,0x3f);
1336
case 0x08: /* udma mode */
1337
put_le16(identify_data + 62,0x07);
1338
put_le16(identify_data + 63,0x07);
1339
put_le16(identify_data + 88,0x3f | (1 << (val + 8)));
1344
s->status = READY_STAT | SEEK_STAT;
1345
ide_set_irq(s->bus);
1352
case WIN_FLUSH_CACHE:
1353
case WIN_FLUSH_CACHE_EXT:
1358
case WIN_STANDBYNOW1:
1359
case WIN_STANDBYNOW2:
1360
case WIN_IDLEIMMEDIATE:
1361
case WIN_IDLEIMMEDIATE2:
1366
s->status = READY_STAT;
1367
ide_set_irq(s->bus);
1370
/* XXX: Check that seek is within bounds */
1371
s->status = READY_STAT | SEEK_STAT;
1372
ide_set_irq(s->bus);
1374
/* ATAPI commands */
1376
ide_atapi_identify(s);
1377
s->status = READY_STAT | SEEK_STAT;
1378
ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1379
ide_set_irq(s->bus);
1382
ide_set_signature(s);
1383
if (s->drive_kind == IDE_CD)
1384
s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
1385
* devices to return a clear status register
1386
* with READY_STAT *not* set. */
1388
s->status = READY_STAT | SEEK_STAT;
1389
s->error = 0x01; /* Device 0 passed, Device 1 passed or not
1392
ide_set_irq(s->bus);
1394
case WIN_DEVICE_RESET:
1395
ide_set_signature(s);
1396
s->status = 0x00; /* NOTE: READY is _not_ set */
1400
/* overlapping commands not supported */
1401
if (s->feature & 0x02)
1403
s->status = READY_STAT | SEEK_STAT;
1404
s->atapi_dma = s->feature & 1;
1406
ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
1409
/* CF-ATA commands */
1410
case CFA_REQ_EXT_ERROR_CODE:
1411
s->error = 0x09; /* miscellaneous error */
1412
s->status = READY_STAT | SEEK_STAT;
1413
ide_set_irq(s->bus);
1415
case CFA_ERASE_SECTORS:
1416
case CFA_WEAR_LEVEL:
1418
/* This one has the same ID as CFA_WEAR_LEVEL and is required for
1419
Windows 8 to work with AHCI */
1420
case WIN_SECURITY_FREEZE_LOCK:
1422
if (val == CFA_WEAR_LEVEL)
1424
if (val == CFA_ERASE_SECTORS)
1425
s->media_changed = 1;
1427
s->status = READY_STAT | SEEK_STAT;
1428
ide_set_irq(s->bus);
1430
case CFA_TRANSLATE_SECTOR:
1432
s->status = READY_STAT | SEEK_STAT;
1433
memset(s->io_buffer, 0, 0x200);
1434
s->io_buffer[0x00] = s->hcyl; /* Cyl MSB */
1435
s->io_buffer[0x01] = s->lcyl; /* Cyl LSB */
1436
s->io_buffer[0x02] = s->select; /* Head */
1437
s->io_buffer[0x03] = s->sector; /* Sector */
1438
s->io_buffer[0x04] = ide_get_sector(s) >> 16; /* LBA MSB */
1439
s->io_buffer[0x05] = ide_get_sector(s) >> 8; /* LBA */
1440
s->io_buffer[0x06] = ide_get_sector(s) >> 0; /* LBA LSB */
1441
s->io_buffer[0x13] = 0x00; /* Erase flag */
1442
s->io_buffer[0x18] = 0x00; /* Hot count */
1443
s->io_buffer[0x19] = 0x00; /* Hot count */
1444
s->io_buffer[0x1a] = 0x01; /* Hot count */
1445
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1446
ide_set_irq(s->bus);
1448
case CFA_ACCESS_METADATA_STORAGE:
1449
switch (s->feature) {
1450
case 0x02: /* Inquiry Metadata Storage */
1451
ide_cfata_metadata_inquiry(s);
1453
case 0x03: /* Read Metadata Storage */
1454
ide_cfata_metadata_read(s);
1456
case 0x04: /* Write Metadata Storage */
1457
ide_cfata_metadata_write(s);
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);
1466
case IBM_SENSE_CONDITION:
1467
switch (s->feature) {
1468
case 0x01: /* sense temperature in device */
1469
s->nsector = 0x50; /* +20 C */
1474
s->status = READY_STAT | SEEK_STAT;
1475
ide_set_irq(s->bus);
1479
if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
1481
if (!s->smart_enabled && s->feature != SMART_ENABLE)
1483
switch (s->feature) {
1485
s->smart_enabled = 0;
1486
s->status = READY_STAT | SEEK_STAT;
1487
ide_set_irq(s->bus);
1490
s->smart_enabled = 1;
1491
s->status = READY_STAT | SEEK_STAT;
1492
ide_set_irq(s->bus);
1494
case SMART_ATTR_AUTOSAVE:
1495
switch (s->sector) {
1497
s->smart_autosave = 0;
1500
s->smart_autosave = 1;
1505
s->status = READY_STAT | SEEK_STAT;
1506
ide_set_irq(s->bus);
1509
if (!s->smart_errors) {
1516
s->status = READY_STAT | SEEK_STAT;
1517
ide_set_irq(s->bus);
1519
case SMART_READ_THRESH:
1520
memset(s->io_buffer, 0, 0x200);
1521
s->io_buffer[0] = 0x01; /* smart struct version */
1522
for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) {
1523
s->io_buffer[2+0+(n*12)] = smart_attributes[n][0];
1524
s->io_buffer[2+1+(n*12)] = smart_attributes[n][11];
1526
for (n=0; n<511; n++) /* checksum */
1527
s->io_buffer[511] += s->io_buffer[n];
1528
s->io_buffer[511] = 0x100 - s->io_buffer[511];
1529
s->status = READY_STAT | SEEK_STAT;
1530
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1531
ide_set_irq(s->bus);
1533
case SMART_READ_DATA:
1534
memset(s->io_buffer, 0, 0x200);
1535
s->io_buffer[0] = 0x01; /* smart struct version */
1536
for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) {
1538
for(i = 0; i < 11; i++) {
1539
s->io_buffer[2+i+(n*12)] = smart_attributes[n][i];
1542
s->io_buffer[362] = 0x02 | (s->smart_autosave?0x80:0x00);
1543
if (s->smart_selftest_count == 0) {
1544
s->io_buffer[363] = 0;
1547
s->smart_selftest_data[3 +
1548
(s->smart_selftest_count - 1) *
1551
s->io_buffer[364] = 0x20;
1552
s->io_buffer[365] = 0x01;
1553
/* offline data collection capacity: execute + self-test*/
1554
s->io_buffer[367] = (1<<4 | 1<<3 | 1);
1555
s->io_buffer[368] = 0x03; /* smart capability (1) */
1556
s->io_buffer[369] = 0x00; /* smart capability (2) */
1557
s->io_buffer[370] = 0x01; /* error logging supported */
1558
s->io_buffer[372] = 0x02; /* minutes for poll short test */
1559
s->io_buffer[373] = 0x36; /* minutes for poll ext test */
1560
s->io_buffer[374] = 0x01; /* minutes for poll conveyance */
1562
for (n=0; n<511; n++)
1563
s->io_buffer[511] += s->io_buffer[n];
1564
s->io_buffer[511] = 0x100 - s->io_buffer[511];
1565
s->status = READY_STAT | SEEK_STAT;
1566
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1567
ide_set_irq(s->bus);
1569
case SMART_READ_LOG:
1570
switch (s->sector) {
1571
case 0x01: /* summary smart error log */
1572
memset(s->io_buffer, 0, 0x200);
1573
s->io_buffer[0] = 0x01;
1574
s->io_buffer[1] = 0x00; /* no error entries */
1575
s->io_buffer[452] = s->smart_errors & 0xff;
1576
s->io_buffer[453] = (s->smart_errors & 0xff00) >> 8;
1578
for (n=0; n<511; n++)
1579
s->io_buffer[511] += s->io_buffer[n];
1580
s->io_buffer[511] = 0x100 - s->io_buffer[511];
1582
case 0x06: /* smart self test log */
1583
memset(s->io_buffer, 0, 0x200);
1584
s->io_buffer[0] = 0x01;
1585
if (s->smart_selftest_count == 0) {
1586
s->io_buffer[508] = 0;
1588
s->io_buffer[508] = s->smart_selftest_count;
1589
for (n=2; n<506; n++)
1590
s->io_buffer[n] = s->smart_selftest_data[n];
1592
for (n=0; n<511; n++)
1593
s->io_buffer[511] += s->io_buffer[n];
1594
s->io_buffer[511] = 0x100 - s->io_buffer[511];
1599
s->status = READY_STAT | SEEK_STAT;
1600
ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
1601
ide_set_irq(s->bus);
1603
case SMART_EXECUTE_OFFLINE:
1604
switch (s->sector) {
1605
case 0: /* off-line routine */
1606
case 1: /* short self test */
1607
case 2: /* extended self test */
1608
s->smart_selftest_count++;
1609
if(s->smart_selftest_count > 21)
1610
s->smart_selftest_count = 0;
1611
n = 2 + (s->smart_selftest_count - 1) * 24;
1612
s->smart_selftest_data[n] = s->sector;
1613
s->smart_selftest_data[n+1] = 0x00; /* OK and finished */
1614
s->smart_selftest_data[n+2] = 0x34; /* hour count lsb */
1615
s->smart_selftest_data[n+3] = 0x12; /* hour count msb */
1616
s->status = READY_STAT | SEEK_STAT;
1617
ide_set_irq(s->bus);
1628
/* should not be reachable */
1630
1721
ide_abort_command(s);
1631
1722
ide_set_irq(s->bus);
1726
s->status = READY_STAT | BUSY_STAT;
1729
complete = ide_cmd_table[val].handler(s, val);
1731
s->status &= ~BUSY_STAT;
1732
assert(!!s->error == !!(s->status & ERR_STAT));
1734
if ((ide_cmd_table[val].flags & SET_DSC) && !s->error) {
1735
s->status |= SEEK_STAT;
1738
ide_set_irq(s->bus);