~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to hw/ide/core.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
568
568
    qemu_sglist_destroy(&s->sg);
569
569
}
570
570
 
 
571
static void ide_async_cmd_done(IDEState *s)
 
572
{
 
573
    if (s->bus->dma->ops->async_cmd_done) {
 
574
        s->bus->dma->ops->async_cmd_done(s->bus->dma);
 
575
    }
 
576
}
 
577
 
571
578
void ide_set_inactive(IDEState *s)
572
579
{
573
580
    s->bus->dma->aiocb = NULL;
574
581
    s->bus->dma->ops->set_inactive(s->bus->dma);
 
582
    ide_async_cmd_done(s);
575
583
}
576
584
 
577
585
void ide_dma_error(IDEState *s)
804
812
 
805
813
    bdrv_acct_done(s->bs, &s->acct);
806
814
    s->status = READY_STAT | SEEK_STAT;
 
815
    ide_async_cmd_done(s);
807
816
    ide_set_irq(s->bus);
808
817
}
809
818
 
814
823
        return;
815
824
    }
816
825
 
 
826
    s->status |= BUSY_STAT;
817
827
    bdrv_acct_start(s->bs, &s->acct, 0, BDRV_ACCT_FLUSH);
818
828
    bdrv_aio_flush(s->bs, ide_flush_cb, s);
819
829
}
1003
1013
    }
1004
1014
}
1005
1015
 
 
1016
static bool cmd_nop(IDEState *s, uint8_t cmd)
 
1017
{
 
1018
    return true;
 
1019
}
 
1020
 
 
1021
static bool cmd_data_set_management(IDEState *s, uint8_t cmd)
 
1022
{
 
1023
    switch (s->feature) {
 
1024
    case DSM_TRIM:
 
1025
        if (s->bs) {
 
1026
            ide_sector_start_dma(s, IDE_DMA_TRIM);
 
1027
            return false;
 
1028
        }
 
1029
        break;
 
1030
    }
 
1031
 
 
1032
    ide_abort_command(s);
 
1033
    return true;
 
1034
}
 
1035
 
 
1036
static bool cmd_identify(IDEState *s, uint8_t cmd)
 
1037
{
 
1038
    if (s->bs && s->drive_kind != IDE_CD) {
 
1039
        if (s->drive_kind != IDE_CFATA) {
 
1040
            ide_identify(s);
 
1041
        } else {
 
1042
            ide_cfata_identify(s);
 
1043
        }
 
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);
 
1047
        return false;
 
1048
    } else {
 
1049
        if (s->drive_kind == IDE_CD) {
 
1050
            ide_set_signature(s);
 
1051
        }
 
1052
        ide_abort_command(s);
 
1053
    }
 
1054
 
 
1055
    return true;
 
1056
}
 
1057
 
 
1058
static bool cmd_verify(IDEState *s, uint8_t cmd)
 
1059
{
 
1060
    bool lba48 = (cmd == WIN_VERIFY_EXT);
 
1061
 
 
1062
    /* do sector number check ? */
 
1063
    ide_cmd_lba48_transform(s, lba48);
 
1064
 
 
1065
    return true;
 
1066
}
 
1067
 
 
1068
static bool cmd_set_multiple_mode(IDEState *s, uint8_t cmd)
 
1069
{
 
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);
 
1077
    } else {
 
1078
        s->mult_sectors = s->nsector & 0xff;
 
1079
    }
 
1080
 
 
1081
    return true;
 
1082
}
 
1083
 
 
1084
static bool cmd_read_multiple(IDEState *s, uint8_t cmd)
 
1085
{
 
1086
    bool lba48 = (cmd == WIN_MULTREAD_EXT);
 
1087
 
 
1088
    if (!s->bs || !s->mult_sectors) {
 
1089
        ide_abort_command(s);
 
1090
        return true;
 
1091
    }
 
1092
 
 
1093
    ide_cmd_lba48_transform(s, lba48);
 
1094
    s->req_nb_sectors = s->mult_sectors;
 
1095
    ide_sector_read(s);
 
1096
    return false;
 
1097
}
 
1098
 
 
1099
static bool cmd_write_multiple(IDEState *s, uint8_t cmd)
 
1100
{
 
1101
    bool lba48 = (cmd == WIN_MULTWRITE_EXT);
 
1102
    int n;
 
1103
 
 
1104
    if (!s->bs || !s->mult_sectors) {
 
1105
        ide_abort_command(s);
 
1106
        return true;
 
1107
    }
 
1108
 
 
1109
    ide_cmd_lba48_transform(s, lba48);
 
1110
 
 
1111
    s->req_nb_sectors = s->mult_sectors;
 
1112
    n = MIN(s->nsector, s->req_nb_sectors);
 
1113
 
 
1114
    s->status = SEEK_STAT | READY_STAT;
 
1115
    ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
 
1116
 
 
1117
    s->media_changed = 1;
 
1118
 
 
1119
    return false;
 
1120
}
 
1121
 
 
1122
static bool cmd_read_pio(IDEState *s, uint8_t cmd)
 
1123
{
 
1124
    bool lba48 = (cmd == WIN_READ_EXT);
 
1125
 
 
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);
 
1129
        return true;
 
1130
    }
 
1131
 
 
1132
    if (!s->bs) {
 
1133
        ide_abort_command(s);
 
1134
        return true;
 
1135
    }
 
1136
 
 
1137
    ide_cmd_lba48_transform(s, lba48);
 
1138
    s->req_nb_sectors = 1;
 
1139
    ide_sector_read(s);
 
1140
 
 
1141
    return false;
 
1142
}
 
1143
 
 
1144
static bool cmd_write_pio(IDEState *s, uint8_t cmd)
 
1145
{
 
1146
    bool lba48 = (cmd == WIN_WRITE_EXT);
 
1147
 
 
1148
    if (!s->bs) {
 
1149
        ide_abort_command(s);
 
1150
        return true;
 
1151
    }
 
1152
 
 
1153
    ide_cmd_lba48_transform(s, lba48);
 
1154
 
 
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);
 
1158
 
 
1159
    s->media_changed = 1;
 
1160
 
 
1161
    return false;
 
1162
}
 
1163
 
 
1164
static bool cmd_read_dma(IDEState *s, uint8_t cmd)
 
1165
{
 
1166
    bool lba48 = (cmd == WIN_READDMA_EXT);
 
1167
 
 
1168
    if (!s->bs) {
 
1169
        ide_abort_command(s);
 
1170
        return true;
 
1171
    }
 
1172
 
 
1173
    ide_cmd_lba48_transform(s, lba48);
 
1174
    ide_sector_start_dma(s, IDE_DMA_READ);
 
1175
 
 
1176
    return false;
 
1177
}
 
1178
 
 
1179
static bool cmd_write_dma(IDEState *s, uint8_t cmd)
 
1180
{
 
1181
    bool lba48 = (cmd == WIN_WRITEDMA_EXT);
 
1182
 
 
1183
    if (!s->bs) {
 
1184
        ide_abort_command(s);
 
1185
        return true;
 
1186
    }
 
1187
 
 
1188
    ide_cmd_lba48_transform(s, lba48);
 
1189
    ide_sector_start_dma(s, IDE_DMA_WRITE);
 
1190
 
 
1191
    s->media_changed = 1;
 
1192
 
 
1193
    return false;
 
1194
}
 
1195
 
 
1196
static bool cmd_flush_cache(IDEState *s, uint8_t cmd)
 
1197
{
 
1198
    ide_flush_cache(s);
 
1199
    return false;
 
1200
}
 
1201
 
 
1202
static bool cmd_seek(IDEState *s, uint8_t cmd)
 
1203
{
 
1204
    /* XXX: Check that seek is within bounds */
 
1205
    return true;
 
1206
}
 
1207
 
 
1208
static bool cmd_read_native_max(IDEState *s, uint8_t cmd)
 
1209
{
 
1210
    bool lba48 = (cmd == WIN_READ_NATIVE_MAX_EXT);
 
1211
 
 
1212
    /* Refuse if no sectors are addressable (e.g. medium not inserted) */
 
1213
    if (s->nb_sectors == 0) {
 
1214
        ide_abort_command(s);
 
1215
        return true;
 
1216
    }
 
1217
 
 
1218
    ide_cmd_lba48_transform(s, lba48);
 
1219
    ide_set_sector(s, s->nb_sectors - 1);
 
1220
 
 
1221
    return true;
 
1222
}
 
1223
 
 
1224
static bool cmd_check_power_mode(IDEState *s, uint8_t cmd)
 
1225
{
 
1226
    s->nsector = 0xff; /* device active or idle */
 
1227
    return true;
 
1228
}
 
1229
 
 
1230
static bool cmd_set_features(IDEState *s, uint8_t cmd)
 
1231
{
 
1232
    uint16_t *identify_data;
 
1233
 
 
1234
    if (!s->bs) {
 
1235
        ide_abort_command(s);
 
1236
        return true;
 
1237
    }
 
1238
 
 
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);
 
1245
        return true;
 
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);
 
1250
        ide_flush_cache(s);
 
1251
        return false;
 
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 */
 
1264
        return true;
 
1265
    case 0x03: /* set transfer mode */
 
1266
        {
 
1267
            uint8_t val = s->nsector & 0x07;
 
1268
            identify_data = (uint16_t *)s->identify_data;
 
1269
 
 
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);
 
1276
                break;
 
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);
 
1281
                break;
 
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);
 
1286
                break;
 
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)));
 
1291
                break;
 
1292
            default:
 
1293
                goto abort_cmd;
 
1294
            }
 
1295
            return true;
 
1296
        }
 
1297
    }
 
1298
 
 
1299
abort_cmd:
 
1300
    ide_abort_command(s);
 
1301
    return true;
 
1302
}
 
1303
 
 
1304
 
 
1305
/*** ATAPI commands ***/
 
1306
 
 
1307
static bool cmd_identify_packet(IDEState *s, uint8_t cmd)
 
1308
{
 
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);
 
1313
    return false;
 
1314
}
 
1315
 
 
1316
static bool cmd_exec_dev_diagnostic(IDEState *s, uint8_t cmd)
 
1317
{
 
1318
    ide_set_signature(s);
 
1319
 
 
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. */
 
1324
    } else {
 
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. */
 
1329
        s->error = 0x01;
 
1330
        ide_set_irq(s->bus);
 
1331
    }
 
1332
 
 
1333
    return false;
 
1334
}
 
1335
 
 
1336
static bool cmd_device_reset(IDEState *s, uint8_t cmd)
 
1337
{
 
1338
    ide_set_signature(s);
 
1339
    s->status = 0x00; /* NOTE: READY is _not_ set */
 
1340
    s->error = 0x01;
 
1341
 
 
1342
    return false;
 
1343
}
 
1344
 
 
1345
static bool cmd_packet(IDEState *s, uint8_t cmd)
 
1346
{
 
1347
    /* overlapping commands not supported */
 
1348
    if (s->feature & 0x02) {
 
1349
        ide_abort_command(s);
 
1350
        return true;
 
1351
    }
 
1352
 
 
1353
    s->status = READY_STAT | SEEK_STAT;
 
1354
    s->atapi_dma = s->feature & 1;
 
1355
    s->nsector = 1;
 
1356
    ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
 
1357
                       ide_atapi_cmd);
 
1358
    return false;
 
1359
}
 
1360
 
 
1361
 
 
1362
/*** CF-ATA commands ***/
 
1363
 
 
1364
static bool cmd_cfa_req_ext_error_code(IDEState *s, uint8_t cmd)
 
1365
{
 
1366
    s->error = 0x09;    /* miscellaneous error */
 
1367
    s->status = READY_STAT | SEEK_STAT;
 
1368
    ide_set_irq(s->bus);
 
1369
 
 
1370
    return false;
 
1371
}
 
1372
 
 
1373
static bool cmd_cfa_erase_sectors(IDEState *s, uint8_t cmd)
 
1374
{
 
1375
    /* WIN_SECURITY_FREEZE_LOCK has the same ID as CFA_WEAR_LEVEL and is
 
1376
     * required for Windows 8 to work with AHCI */
 
1377
 
 
1378
    if (cmd == CFA_WEAR_LEVEL) {
 
1379
        s->nsector = 0;
 
1380
    }
 
1381
 
 
1382
    if (cmd == CFA_ERASE_SECTORS) {
 
1383
        s->media_changed = 1;
 
1384
    }
 
1385
 
 
1386
    return true;
 
1387
}
 
1388
 
 
1389
static bool cmd_cfa_translate_sector(IDEState *s, uint8_t cmd)
 
1390
{
 
1391
    s->status = READY_STAT | SEEK_STAT;
 
1392
 
 
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 */
 
1405
 
 
1406
    ide_transfer_start(s, s->io_buffer, 0x200, ide_transfer_stop);
 
1407
    ide_set_irq(s->bus);
 
1408
 
 
1409
    return false;
 
1410
}
 
1411
 
 
1412
static bool cmd_cfa_access_metadata_storage(IDEState *s, uint8_t cmd)
 
1413
{
 
1414
    switch (s->feature) {
 
1415
    case 0x02:  /* Inquiry Metadata Storage */
 
1416
        ide_cfata_metadata_inquiry(s);
 
1417
        break;
 
1418
    case 0x03:  /* Read Metadata Storage */
 
1419
        ide_cfata_metadata_read(s);
 
1420
        break;
 
1421
    case 0x04:  /* Write Metadata Storage */
 
1422
        ide_cfata_metadata_write(s);
 
1423
        break;
 
1424
    default:
 
1425
        ide_abort_command(s);
 
1426
        return true;
 
1427
    }
 
1428
 
 
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);
 
1432
 
 
1433
    return false;
 
1434
}
 
1435
 
 
1436
static bool cmd_ibm_sense_condition(IDEState *s, uint8_t cmd)
 
1437
{
 
1438
    switch (s->feature) {
 
1439
    case 0x01:  /* sense temperature in device */
 
1440
        s->nsector = 0x50;      /* +20 C */
 
1441
        break;
 
1442
    default:
 
1443
        ide_abort_command(s);
 
1444
        return true;
 
1445
    }
 
1446
 
 
1447
    return true;
 
1448
}
 
1449
 
 
1450
 
 
1451
/*** SMART commands ***/
 
1452
 
 
1453
static bool cmd_smart(IDEState *s, uint8_t cmd)
 
1454
{
 
1455
    int n;
 
1456
 
 
1457
    if (s->hcyl != 0xc2 || s->lcyl != 0x4f) {
 
1458
        goto abort_cmd;
 
1459
    }
 
1460
 
 
1461
    if (!s->smart_enabled && s->feature != SMART_ENABLE) {
 
1462
        goto abort_cmd;
 
1463
    }
 
1464
 
 
1465
    switch (s->feature) {
 
1466
    case SMART_DISABLE:
 
1467
        s->smart_enabled = 0;
 
1468
        return true;
 
1469
 
 
1470
    case SMART_ENABLE:
 
1471
        s->smart_enabled = 1;
 
1472
        return true;
 
1473
 
 
1474
    case SMART_ATTR_AUTOSAVE:
 
1475
        switch (s->sector) {
 
1476
        case 0x00:
 
1477
            s->smart_autosave = 0;
 
1478
            break;
 
1479
        case 0xf1:
 
1480
            s->smart_autosave = 1;
 
1481
            break;
 
1482
        default:
 
1483
            goto abort_cmd;
 
1484
        }
 
1485
        return true;
 
1486
 
 
1487
    case SMART_STATUS:
 
1488
        if (!s->smart_errors) {
 
1489
            s->hcyl = 0xc2;
 
1490
            s->lcyl = 0x4f;
 
1491
        } else {
 
1492
            s->hcyl = 0x2c;
 
1493
            s->lcyl = 0xf4;
 
1494
        }
 
1495
        return true;
 
1496
 
 
1497
    case SMART_READ_THRESH:
 
1498
        memset(s->io_buffer, 0, 0x200);
 
1499
        s->io_buffer[0] = 0x01; /* smart struct version */
 
1500
 
 
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];
 
1504
        }
 
1505
 
 
1506
        /* checksum */
 
1507
        for (n = 0; n < 511; n++) {
 
1508
            s->io_buffer[511] += s->io_buffer[n];
 
1509
        }
 
1510
        s->io_buffer[511] = 0x100 - s->io_buffer[511];
 
1511
 
 
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);
 
1515
        return false;
 
1516
 
 
1517
    case SMART_READ_DATA:
 
1518
        memset(s->io_buffer, 0, 0x200);
 
1519
        s->io_buffer[0] = 0x01; /* smart struct version */
 
1520
 
 
1521
        for (n = 0; n < ARRAY_SIZE(smart_attributes); n++) {
 
1522
            int i;
 
1523
            for (i = 0; i < 11; i++) {
 
1524
                s->io_buffer[2 + i + (n * 12)] = smart_attributes[n][i];
 
1525
            }
 
1526
        }
 
1527
 
 
1528
        s->io_buffer[362] = 0x02 | (s->smart_autosave ? 0x80 : 0x00);
 
1529
        if (s->smart_selftest_count == 0) {
 
1530
            s->io_buffer[363] = 0;
 
1531
        } else {
 
1532
            s->io_buffer[363] =
 
1533
                s->smart_selftest_data[3 +
 
1534
                           (s->smart_selftest_count - 1) *
 
1535
                           24];
 
1536
        }
 
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 */
 
1547
 
 
1548
        for (n = 0; n < 511; n++) {
 
1549
            s->io_buffer[511] += s->io_buffer[n];
 
1550
        }
 
1551
        s->io_buffer[511] = 0x100 - s->io_buffer[511];
 
1552
 
 
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);
 
1556
        return false;
 
1557
 
 
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;
 
1566
 
 
1567
            for (n = 0; n < 511; n++) {
 
1568
                s->io_buffer[511] += s->io_buffer[n];
 
1569
            }
 
1570
            s->io_buffer[511] = 0x100 - s->io_buffer[511];
 
1571
            break;
 
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;
 
1577
            } else {
 
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];
 
1581
                }
 
1582
            }
 
1583
 
 
1584
            for (n = 0; n < 511; n++) {
 
1585
                s->io_buffer[511] += s->io_buffer[n];
 
1586
            }
 
1587
            s->io_buffer[511] = 0x100 - s->io_buffer[511];
 
1588
            break;
 
1589
        default:
 
1590
            goto abort_cmd;
 
1591
        }
 
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);
 
1595
        return false;
 
1596
 
 
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;
 
1605
            }
 
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 */
 
1611
            break;
 
1612
        default:
 
1613
            goto abort_cmd;
 
1614
        }
 
1615
        return true;
 
1616
    }
 
1617
 
 
1618
abort_cmd:
 
1619
    ide_abort_command(s);
 
1620
    return true;
 
1621
}
 
1622
 
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)
1011
1628
 
 
1629
/* Set the Disk Seek Completed status bit during completion */
 
1630
#define SET_DSC (1u << 8)
 
1631
 
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);
 
1636
    int flags;
 
1637
} ide_cmd_table[0x100] = {
1014
1638
    /* NOP not implemented, mandatory for CD */
1015
 
    [CFA_REQ_EXT_ERROR_CODE]            = CFA_OK,
1016
 
    [WIN_DSM]                           = ALL_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 },
1071
1695
};
1072
1696
 
1073
1697
static bool ide_cmd_permitted(IDEState *s, uint32_t cmd)
1074
1698
{
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));
1077
1701
}
1078
1702
 
1079
1703
void ide_exec_cmd(IDEBus *bus, uint32_t val)
1080
1704
{
1081
 
    uint16_t *identify_data;
1082
1705
    IDEState *s;
1083
 
    int n;
1084
 
    int lba48 = 0;
 
1706
    bool complete;
1085
1707
 
1086
1708
#if defined(DEBUG_IDE)
1087
1709
    printf("ide: CMD=%02x\n", val);
1096
1718
        return;
1097
1719
 
1098
1720
    if (!ide_cmd_permitted(s, val)) {
1099
 
        goto abort_cmd;
1100
 
    }
1101
 
 
1102
 
    switch(val) {
1103
 
    case WIN_DSM:
1104
 
        switch (s->feature) {
1105
 
        case DSM_TRIM:
1106
 
            if (!s->bs) {
1107
 
                goto abort_cmd;
1108
 
            }
1109
 
            ide_sector_start_dma(s, IDE_DMA_TRIM);
1110
 
            break;
1111
 
        default:
1112
 
            goto abort_cmd;
1113
 
        }
1114
 
        break;
1115
 
    case WIN_IDENTIFY:
1116
 
        if (s->bs && s->drive_kind != IDE_CD) {
1117
 
            if (s->drive_kind != IDE_CFATA)
1118
 
                ide_identify(s);
1119
 
            else
1120
 
                ide_cfata_identify(s);
1121
 
            s->status = READY_STAT | SEEK_STAT;
1122
 
            ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1123
 
        } else {
1124
 
            if (s->drive_kind == IDE_CD) {
1125
 
                ide_set_signature(s);
1126
 
            }
1127
 
            ide_abort_command(s);
1128
 
        }
1129
 
        ide_set_irq(s->bus);
1130
 
        break;
1131
 
    case WIN_SPECIFY:
1132
 
    case WIN_RECAL:
1133
 
        s->error = 0;
1134
 
        s->status = READY_STAT | SEEK_STAT;
1135
 
        ide_set_irq(s->bus);
1136
 
        break;
1137
 
    case WIN_SETMULT:
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);
1146
 
        } else {
1147
 
            s->mult_sectors = s->nsector & 0xff;
1148
 
            s->status = READY_STAT | SEEK_STAT;
1149
 
        }
1150
 
        ide_set_irq(s->bus);
1151
 
        break;
1152
 
 
1153
 
    case WIN_VERIFY_EXT:
1154
 
        lba48 = 1;
1155
 
        /* fall through */
1156
 
    case WIN_VERIFY:
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);
1162
 
        break;
1163
 
 
1164
 
    case WIN_READ_EXT:
1165
 
        lba48 = 1;
1166
 
        /* fall through */
1167
 
    case WIN_READ:
1168
 
    case WIN_READ_ONCE:
1169
 
        if (s->drive_kind == IDE_CD) {
1170
 
            ide_set_signature(s); /* odd, but ATA4 8.27.5.2 requires it */
1171
 
            goto abort_cmd;
1172
 
        }
1173
 
        if (!s->bs) {
1174
 
            goto abort_cmd;
1175
 
        }
1176
 
        ide_cmd_lba48_transform(s, lba48);
1177
 
        s->req_nb_sectors = 1;
1178
 
        ide_sector_read(s);
1179
 
        break;
1180
 
 
1181
 
    case WIN_WRITE_EXT:
1182
 
        lba48 = 1;
1183
 
        /* fall through */
1184
 
    case WIN_WRITE:
1185
 
    case WIN_WRITE_ONCE:
1186
 
    case CFA_WRITE_SECT_WO_ERASE:
1187
 
    case WIN_WRITE_VERIFY:
1188
 
        if (!s->bs) {
1189
 
            goto abort_cmd;
1190
 
        }
1191
 
        ide_cmd_lba48_transform(s, lba48);
1192
 
        s->error = 0;
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;
1197
 
        break;
1198
 
 
1199
 
    case WIN_MULTREAD_EXT:
1200
 
        lba48 = 1;
1201
 
        /* fall through */
1202
 
    case WIN_MULTREAD:
1203
 
        if (!s->bs) {
1204
 
            goto abort_cmd;
1205
 
        }
1206
 
        if (!s->mult_sectors) {
1207
 
            goto abort_cmd;
1208
 
        }
1209
 
        ide_cmd_lba48_transform(s, lba48);
1210
 
        s->req_nb_sectors = s->mult_sectors;
1211
 
        ide_sector_read(s);
1212
 
        break;
1213
 
 
1214
 
    case WIN_MULTWRITE_EXT:
1215
 
        lba48 = 1;
1216
 
        /* fall through */
1217
 
    case WIN_MULTWRITE:
1218
 
    case CFA_WRITE_MULTI_WO_ERASE:
1219
 
        if (!s->bs) {
1220
 
            goto abort_cmd;
1221
 
        }
1222
 
        if (!s->mult_sectors) {
1223
 
            goto abort_cmd;
1224
 
        }
1225
 
        ide_cmd_lba48_transform(s, lba48);
1226
 
        s->error = 0;
1227
 
        s->status = SEEK_STAT | READY_STAT;
1228
 
        s->req_nb_sectors = s->mult_sectors;
1229
 
        n = s->nsector;
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;
1234
 
        break;
1235
 
 
1236
 
    case WIN_READDMA_EXT:
1237
 
        lba48 = 1;
1238
 
        /* fall through */
1239
 
    case WIN_READDMA:
1240
 
    case WIN_READDMA_ONCE:
1241
 
        if (!s->bs) {
1242
 
            goto abort_cmd;
1243
 
        }
1244
 
        ide_cmd_lba48_transform(s, lba48);
1245
 
        ide_sector_start_dma(s, IDE_DMA_READ);
1246
 
        break;
1247
 
 
1248
 
    case WIN_WRITEDMA_EXT:
1249
 
        lba48 = 1;
1250
 
        /* fall through */
1251
 
    case WIN_WRITEDMA:
1252
 
    case WIN_WRITEDMA_ONCE:
1253
 
        if (!s->bs) {
1254
 
            goto abort_cmd;
1255
 
        }
1256
 
        ide_cmd_lba48_transform(s, lba48);
1257
 
        ide_sector_start_dma(s, IDE_DMA_WRITE);
1258
 
        s->media_changed = 1;
1259
 
        break;
1260
 
 
1261
 
    case WIN_READ_NATIVE_MAX_EXT:
1262
 
        lba48 = 1;
1263
 
        /* fall through */
1264
 
    case WIN_READ_NATIVE_MAX:
1265
 
        /* Refuse if no sectors are addressable (e.g. medium not inserted) */
1266
 
        if (s->nb_sectors == 0) {
1267
 
            goto abort_cmd;
1268
 
        }
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);
1273
 
        break;
1274
 
 
1275
 
    case WIN_CHECKPOWERMODE1:
1276
 
    case WIN_CHECKPOWERMODE2:
1277
 
        s->error = 0;
1278
 
        s->nsector = 0xff; /* device active or idle */
1279
 
        s->status = READY_STAT | SEEK_STAT;
1280
 
        ide_set_irq(s->bus);
1281
 
        break;
1282
 
    case WIN_SETFEATURES:
1283
 
        if (!s->bs)
1284
 
            goto abort_cmd;
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);
1293
 
            break;
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);
1298
 
            ide_flush_cache(s);
1299
 
            break;
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);
1314
 
            break;
1315
 
        case 0x03: { /* set transfer mode */
1316
 
                uint8_t val = s->nsector & 0x07;
1317
 
                identify_data = (uint16_t *)s->identify_data;
1318
 
 
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);
1325
 
                        break;
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);
1330
 
                        break;
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);
1335
 
                        break;
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)));
1340
 
                        break;
1341
 
                default:
1342
 
                        goto abort_cmd;
1343
 
                }
1344
 
            s->status = READY_STAT | SEEK_STAT;
1345
 
            ide_set_irq(s->bus);
1346
 
            break;
1347
 
        }
1348
 
        default:
1349
 
            goto abort_cmd;
1350
 
        }
1351
 
        break;
1352
 
    case WIN_FLUSH_CACHE:
1353
 
    case WIN_FLUSH_CACHE_EXT:
1354
 
        ide_flush_cache(s);
1355
 
        break;
1356
 
    case WIN_STANDBY:
1357
 
    case WIN_STANDBY2:
1358
 
    case WIN_STANDBYNOW1:
1359
 
    case WIN_STANDBYNOW2:
1360
 
    case WIN_IDLEIMMEDIATE:
1361
 
    case WIN_IDLEIMMEDIATE2:
1362
 
    case WIN_SETIDLE1:
1363
 
    case WIN_SETIDLE2:
1364
 
    case WIN_SLEEPNOW1:
1365
 
    case WIN_SLEEPNOW2:
1366
 
        s->status = READY_STAT;
1367
 
        ide_set_irq(s->bus);
1368
 
        break;
1369
 
    case WIN_SEEK:
1370
 
        /* XXX: Check that seek is within bounds */
1371
 
        s->status = READY_STAT | SEEK_STAT;
1372
 
        ide_set_irq(s->bus);
1373
 
        break;
1374
 
        /* ATAPI commands */
1375
 
    case WIN_PIDENTIFY:
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);
1380
 
        break;
1381
 
    case WIN_DIAGNOSE:
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. */
1387
 
        else
1388
 
            s->status = READY_STAT | SEEK_STAT;
1389
 
        s->error = 0x01; /* Device 0 passed, Device 1 passed or not
1390
 
                          * present.
1391
 
                          */
1392
 
        ide_set_irq(s->bus);
1393
 
        break;
1394
 
    case WIN_DEVICE_RESET:
1395
 
        ide_set_signature(s);
1396
 
        s->status = 0x00; /* NOTE: READY is _not_ set */
1397
 
        s->error = 0x01;
1398
 
        break;
1399
 
    case WIN_PACKETCMD:
1400
 
        /* overlapping commands not supported */
1401
 
        if (s->feature & 0x02)
1402
 
            goto abort_cmd;
1403
 
        s->status = READY_STAT | SEEK_STAT;
1404
 
        s->atapi_dma = s->feature & 1;
1405
 
        s->nsector = 1;
1406
 
        ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
1407
 
                           ide_atapi_cmd);
1408
 
        break;
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);
1414
 
        break;
1415
 
    case CFA_ERASE_SECTORS:
1416
 
    case CFA_WEAR_LEVEL:
1417
 
#if 0
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:
1421
 
#endif
1422
 
        if (val == CFA_WEAR_LEVEL)
1423
 
            s->nsector = 0;
1424
 
        if (val == CFA_ERASE_SECTORS)
1425
 
            s->media_changed = 1;
1426
 
        s->error = 0x00;
1427
 
        s->status = READY_STAT | SEEK_STAT;
1428
 
        ide_set_irq(s->bus);
1429
 
        break;
1430
 
    case CFA_TRANSLATE_SECTOR:
1431
 
        s->error = 0x00;
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);
1447
 
        break;
1448
 
    case CFA_ACCESS_METADATA_STORAGE:
1449
 
        switch (s->feature) {
1450
 
        case 0x02:      /* Inquiry Metadata Storage */
1451
 
            ide_cfata_metadata_inquiry(s);
1452
 
            break;
1453
 
        case 0x03:      /* Read Metadata Storage */
1454
 
            ide_cfata_metadata_read(s);
1455
 
            break;
1456
 
        case 0x04:      /* Write Metadata Storage */
1457
 
            ide_cfata_metadata_write(s);
1458
 
            break;
1459
 
        default:
1460
 
            goto abort_cmd;
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
 
        break;
1466
 
    case IBM_SENSE_CONDITION:
1467
 
        switch (s->feature) {
1468
 
        case 0x01:  /* sense temperature in device */
1469
 
            s->nsector = 0x50;      /* +20 C */
1470
 
            break;
1471
 
        default:
1472
 
            goto abort_cmd;
1473
 
        }
1474
 
        s->status = READY_STAT | SEEK_STAT;
1475
 
        ide_set_irq(s->bus);
1476
 
        break;
1477
 
 
1478
 
    case WIN_SMART:
1479
 
        if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
1480
 
                goto abort_cmd;
1481
 
        if (!s->smart_enabled && s->feature != SMART_ENABLE)
1482
 
                goto abort_cmd;
1483
 
        switch (s->feature) {
1484
 
        case SMART_DISABLE:
1485
 
                s->smart_enabled = 0;
1486
 
                s->status = READY_STAT | SEEK_STAT;
1487
 
                ide_set_irq(s->bus);
1488
 
                break;
1489
 
        case SMART_ENABLE:
1490
 
                s->smart_enabled = 1;
1491
 
                s->status = READY_STAT | SEEK_STAT;
1492
 
                ide_set_irq(s->bus);
1493
 
                break;
1494
 
        case SMART_ATTR_AUTOSAVE:
1495
 
                switch (s->sector) {
1496
 
                case 0x00:
1497
 
                s->smart_autosave = 0;
1498
 
                break;
1499
 
                case 0xf1:
1500
 
                s->smart_autosave = 1;
1501
 
                break;
1502
 
                default:
1503
 
                goto abort_cmd;
1504
 
                }
1505
 
                s->status = READY_STAT | SEEK_STAT;
1506
 
                ide_set_irq(s->bus);
1507
 
                break;
1508
 
        case SMART_STATUS:
1509
 
                if (!s->smart_errors) {
1510
 
                s->hcyl = 0xc2;
1511
 
                s->lcyl = 0x4f;
1512
 
                } else {
1513
 
                s->hcyl = 0x2c;
1514
 
                s->lcyl = 0xf4;
1515
 
                }
1516
 
                s->status = READY_STAT | SEEK_STAT;
1517
 
                ide_set_irq(s->bus);
1518
 
                break;
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];
1525
 
                }
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);
1532
 
                break;
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++) {
1537
 
                    int i;
1538
 
                    for(i = 0; i < 11; i++) {
1539
 
                        s->io_buffer[2+i+(n*12)] = smart_attributes[n][i];
1540
 
                    }
1541
 
                }
1542
 
                s->io_buffer[362] = 0x02 | (s->smart_autosave?0x80:0x00);
1543
 
                if (s->smart_selftest_count == 0) {
1544
 
                s->io_buffer[363] = 0;
1545
 
                } else {
1546
 
                s->io_buffer[363] =
1547
 
                        s->smart_selftest_data[3 + 
1548
 
                                           (s->smart_selftest_count - 1) *
1549
 
                                           24];
1550
 
                }
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 */
1561
 
 
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);
1568
 
                break;
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;
1577
 
 
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];
1581
 
                break;
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;
1587
 
                } else {
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];
1591
 
                }
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];
1595
 
                break;
1596
 
                default:
1597
 
                goto abort_cmd;
1598
 
                }
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);
1602
 
                break;
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);
1618
 
                break;
1619
 
                default:
1620
 
                goto abort_cmd;
1621
 
                }
1622
 
                break;
1623
 
        default:
1624
 
                goto abort_cmd;
1625
 
        }
1626
 
        break;
1627
 
    default:
1628
 
        /* should not be reachable */
1629
 
    abort_cmd:
1630
1721
        ide_abort_command(s);
1631
1722
        ide_set_irq(s->bus);
1632
 
        break;
 
1723
        return;
 
1724
    }
 
1725
 
 
1726
    s->status = READY_STAT | BUSY_STAT;
 
1727
    s->error = 0;
 
1728
 
 
1729
    complete = ide_cmd_table[val].handler(s, val);
 
1730
    if (complete) {
 
1731
        s->status &= ~BUSY_STAT;
 
1732
        assert(!!s->error == !!(s->status & ERR_STAT));
 
1733
 
 
1734
        if ((ide_cmd_table[val].flags & SET_DSC) && !s->error) {
 
1735
            s->status |= SEEK_STAT;
 
1736
        }
 
1737
 
 
1738
        ide_set_irq(s->bus);
1633
1739
    }
1634
1740
}
1635
1741