sys/dev/disk/ahci/ahci_cam.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 | /* * (MPSAFE) * * Copyright (c) 2009 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * * Copyright (c) 2007 David Gwynne <dlg@openbsd.org> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $ */ /* * Implement each SATA port as its own SCSI bus on CAM. This way we can * implement future port multiplier features as individual devices on the * bus. * * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest * was written natively for DragonFly. * * NOTE-1: I was temporarily unlocking the port while making the CCB * callback, to reduce the chance of a deadlock and to improve * performance by allowing new commands to be queued. * * However, this also creates an opening where another AHCI * interrupt can come in and execute the ahci_port_intr() * function, creating a huge mess in the sequencing of the * chipset. * * So for now we don't do this. XXX */ #include "ahci.h" static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb); static void ahci_xpt_poll(struct cam_sim *sim); static void ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *at, union ccb *ccb); static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *at, union ccb *ccb); static void ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb); static void ahci_ata_complete_disk_rw(struct ata_xfer *xa); static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa); static void ahci_atapi_complete_cmd(struct ata_xfer *xa); static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data); static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis, struct scsi_sense_data *sense_data); static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at); static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at); static int ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx); static void ahci_ata_dummy_done(struct ata_xfer *xa); static void ata_fix_identify(struct ata_identify *id); static void ahci_cam_rescan(struct ahci_port *ap); static void ahci_strip_string(const char **basep, int *lenp); int ahci_cam_attach(struct ahci_port *ap) { struct cam_devq *devq; struct cam_sim *sim; int error; int unit; /* * We want at least one ccb to be available for error processing * so don't let CAM use more then ncmds - 1. */ unit = device_get_unit(ap->ap_sc->sc_dev); if (ap->ap_sc->sc_ncmds > 1) devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1); else devq = cam_simq_alloc(ap->ap_sc->sc_ncmds); if (devq == NULL) { return (ENOMEM); } /* * Give the devq enough room to run with 32 max_dev_transactions, * but set the overall max tags to 1 until NCQ is negotiated. */ sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci", (void *)ap, unit, &ap->ap_sim_lock, 32, 1, devq); cam_simq_release(devq); if (sim == NULL) { return (ENOMEM); } ap->ap_sim = sim; ahci_os_unlock_port(ap); lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE); error = xpt_bus_register(ap->ap_sim, ap->ap_num); lockmgr(&ap->ap_sim_lock, LK_RELEASE); ahci_os_lock_port(ap); if (error != CAM_SUCCESS) { ahci_cam_detach(ap); return (EINVAL); } ap->ap_flags |= AP_F_BUS_REGISTERED; if (ap->ap_probe == ATA_PROBE_NEED_IDENT) error = ahci_cam_probe(ap, NULL); else error = 0; if (error) { ahci_cam_detach(ap); return (EIO); } ap->ap_flags |= AP_F_CAM_ATTACHED; return(0); } /* * The state of the port has changed. * * If atx is NULL the physical port has changed state. * If atx is non-NULL a particular target behind a PM has changed state. * * If found is -1 the target state must be queued to a non-interrupt context. * (only works with at == NULL). * * If found is 0 the target was removed. * If found is 1 the target was inserted. */ void ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found) { struct cam_path *tmppath; int status; int target; target = atx ? atx->at_target : CAM_TARGET_WILDCARD; if (ap->ap_sim == NULL) return; if (found == CAM_TARGET_WILDCARD) { status = xpt_create_path(&tmppath, NULL, cam_sim_path(ap->ap_sim), target, CAM_LUN_WILDCARD); if (status != CAM_REQ_CMP) return; ahci_cam_rescan(ap); } else { status = xpt_create_path(&tmppath, NULL, cam_sim_path(ap->ap_sim), target, CAM_LUN_WILDCARD); if (status != CAM_REQ_CMP) return; #if 0 /* * This confuses CAM */ if (found) xpt_async(AC_FOUND_DEVICE, tmppath, NULL); else xpt_async(AC_LOST_DEVICE, tmppath, NULL); #endif } xpt_free_path(tmppath); } void ahci_cam_detach(struct ahci_port *ap) { int error __debugvar; if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0) return; lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE); if (ap->ap_sim) { xpt_freeze_simq(ap->ap_sim, 1); } if (ap->ap_flags & AP_F_BUS_REGISTERED) { error = xpt_bus_deregister(cam_sim_path(ap->ap_sim)); KKASSERT(error == 0); ap->ap_flags &= ~AP_F_BUS_REGISTERED; } if (ap->ap_sim) { cam_sim_free(ap->ap_sim); ap->ap_sim = NULL; } lockmgr(&ap->ap_sim_lock, LK_RELEASE); ap->ap_flags &= ~AP_F_CAM_ATTACHED; } /* * Once the AHCI port has been attached we need to probe for a device or * devices on the port and setup various options. * * If at is NULL we are probing the direct-attached device on the port, * which may or may not be a port multiplier. */ int ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx) { struct ata_port *at; struct ata_xfer *xa; u_int64_t capacity; u_int64_t capacity_bytes; int model_len; int firmware_len; int serial_len; int error; int devncqdepth; int i; const char *model_id; const char *firmware_id; const char *serial_id; const char *wcstr; const char *rastr; const char *scstr; const char *type; error = EIO; /* * Delayed CAM attachment for initial probe, sim may be NULL */ if (ap->ap_sim == NULL) return(0); /* * A NULL atx indicates a probe of the directly connected device. * A non-NULL atx indicates a device connected via a port multiplier. * We need to preserve atx for calls to ahci_ata_get_xfer(). * * at is always non-NULL. For directly connected devices we supply * an (at) pointing to target 0. */ if (atx == NULL) { at = ap->ap_ata[0]; /* direct attached - device 0 */ if (ap->ap_type == ATA_PORT_T_PM) { kprintf("%s: Found Port Multiplier\n", ATANAME(ap, atx)); return (0); } at->at_type = ap->ap_type; } else { at = atx; if (atx->at_type == ATA_PORT_T_PM) { kprintf("%s: Bogus device, reducing port count to %d\n", ATANAME(ap, atx), atx->at_target); if (ap->ap_pmcount > atx->at_target) ap->ap_pmcount = atx->at_target; goto err; } } if (ap->ap_type == ATA_PORT_T_NONE) goto err; if (at->at_type == ATA_PORT_T_NONE) goto err; /* * Issue identify, saving the result */ xa = ahci_ata_get_xfer(ap, atx); xa->complete = ahci_ata_dummy_done; xa->data = &at->at_identify; xa->datalen = sizeof(at->at_identify); xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL; xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; switch(at->at_type) { case ATA_PORT_T_DISK: xa->fis->command = ATA_C_IDENTIFY; type = "DISK"; break; case ATA_PORT_T_ATAPI: xa->fis->command = ATA_C_ATAPI_IDENTIFY; xa->flags |= ATA_F_AUTOSENSE; type = "ATAPI"; break; default: xa->fis->command = ATA_C_ATAPI_IDENTIFY; type = "UNKNOWN(ATAPI?)"; break; } xa->fis->features = 0; xa->fis->device = 0; xa->timeout = 1000; if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) { kprintf("%s: Detected %s device but unable to IDENTIFY\n", ATANAME(ap, atx), type); ahci_ata_put_xfer(xa); goto err; } ahci_ata_put_xfer(xa); ata_fix_identify(&at->at_identify); if (at->at_type == ATA_PORT_T_DISK && at->at_identify.nomrota_rate == 1) type = "SSD"; /* * Read capacity using SATA probe info. */ if (le16toh(at->at_identify.cmdset83) & 0x0400) { /* LBA48 feature set supported */ capacity = 0; for (i = 3; i >= 0; --i) { capacity <<= 16; capacity += le16toh(at->at_identify.addrsecxt[i]); } } else { capacity = le16toh(at->at_identify.addrsec[1]); capacity <<= 16; capacity += le16toh(at->at_identify.addrsec[0]); } if (capacity == 0) capacity = 1024 * 1024 / 512; at->at_capacity = capacity; if (atx == NULL) ap->ap_probe = ATA_PROBE_GOOD; capacity_bytes = capacity * 512; /* * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated * number of slots and limit the number of CAM ccb's to one less * so we always have a slot available for recovery. * * NCQ is not used if ap_ncqdepth is 1 or the host controller does * not support it, and in that case the driver can handle extra * ccb's. * * NCQ is currently used only with direct-attached disks. It is * not used with port multipliers or direct-attached ATAPI devices. * * Remember at least one extra CCB needs to be reserved for the * error ccb. */ if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) && ap->ap_type == ATA_PORT_T_DISK && (le16toh(at->at_identify.satacap) & (1 << 8))) { at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1; devncqdepth = at->at_ncqdepth; if (at->at_ncqdepth > ap->ap_sc->sc_ncmds) at->at_ncqdepth = ap->ap_sc->sc_ncmds; if (at->at_ncqdepth > 1) { for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) { xa = ahci_ata_get_xfer(ap, atx); if (xa->tag < at->at_ncqdepth) { xa->state = ATA_S_COMPLETE; ahci_ata_put_xfer(xa); } } if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) { cam_sim_set_max_tags(ap->ap_sim, at->at_ncqdepth - 1); } } } else { devncqdepth = 0; } model_len = sizeof(at->at_identify.model); model_id = at->at_identify.model; ahci_strip_string(&model_id, &model_len); firmware_len = sizeof(at->at_identify.firmware); firmware_id = at->at_identify.firmware; ahci_strip_string(&firmware_id, &firmware_len); serial_len = sizeof(at->at_identify.serial); serial_id = at->at_identify.serial; ahci_strip_string(&serial_id, &serial_len); /* * Generate informatiive strings. * * NOTE: We do not automatically set write caching, lookahead, * or the security state for ATAPI devices. */ if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) { if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) wcstr = "enabled"; else if (at->at_type == ATA_PORT_T_ATAPI) wcstr = "disabled"; else wcstr = "enabling"; } else { wcstr = "notsupp"; } if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) { if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) rastr = "enabled"; else if (at->at_type == ATA_PORT_T_ATAPI) rastr = "disabled"; else rastr = "enabling"; } else { rastr = "notsupp"; } if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) { if (at->at_identify.securestatus & ATA_SECURE_FROZEN) scstr = "frozen"; else if (at->at_type == ATA_PORT_T_ATAPI) scstr = "unfrozen"; else if (AhciNoFeatures & (1 << ap->ap_num)) scstr = "<disabled>"; else scstr = "freezing"; } else { scstr = "notsupp"; } kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n" "%s: tags=%d/%d satacap=%04x satacap2=%04x satafea=%04x NCQ=%s " "capacity=%lld.%02dMB\n", ATANAME(ap, atx), type, model_len, model_len, model_id, firmware_len, firmware_len, firmware_id, serial_len, serial_len, serial_id, ATANAME(ap, atx), devncqdepth, ap->ap_sc->sc_ncmds, at->at_identify.satacap, at->at_identify.satacap2, at->at_identify.satafsup, (at->at_ncqdepth > 1 ? "YES" : "NO"), (long long)capacity_bytes / (1024 * 1024), (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024) ); kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n", ATANAME(ap, atx), at->at_identify.features85, at->at_identify.features86, at->at_identify.features87, wcstr, rastr, scstr ); /* * Additional type-specific probing */ switch(at->at_type) { case ATA_PORT_T_DISK: error = ahci_cam_probe_disk(ap, atx); break; case ATA_PORT_T_ATAPI: error = ahci_cam_probe_atapi(ap, atx); break; default: error = EIO; break; } err: if (error) { at->at_probe = ATA_PROBE_FAILED; if (atx == NULL) ap->ap_probe = at->at_probe; } else { at->at_probe = ATA_PROBE_GOOD; if (atx == NULL) ap->ap_probe = at->at_probe; } return (error); } /* * DISK-specific probe after initial ident */ static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx) { struct ata_port *at; struct ata_xfer *xa; at = atx ? atx : ap->ap_ata[0]; /* * Set dummy xfer mode */ ahci_set_xfer(ap, atx); /* * Enable write cache if supported * * NOTE: "WD My Book" external disk devices have a very poor * daughter board between the the ESATA and the HD. Sending * any ATA_C_SET_FEATURES commands will break the hardware port * with a fatal protocol error. However, this device also * indicates that WRITECACHE is already on and READAHEAD is * not supported so we avoid the issue. */ if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) && (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) { xa = ahci_ata_get_xfer(ap, atx); xa->complete = ahci_ata_dummy_done; xa->fis->command = ATA_C_SET_FEATURES; xa->fis->features = ATA_SF_WRITECACHE_EN; /* xa->fis->features = ATA_SF_LOOKAHEAD_EN; */ xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; xa->fis->device = 0; xa->flags = ATA_F_PIO | ATA_F_POLL; xa->timeout = 1000; xa->datalen = 0; if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) at->at_features |= ATA_PORT_F_WCACHE; else kprintf("%s: Unable to enable write-caching\n", ATANAME(ap, atx)); ahci_ata_put_xfer(xa); } /* * Enable readahead if supported */ if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) && (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) { xa = ahci_ata_get_xfer(ap, atx); xa->complete = ahci_ata_dummy_done; xa->fis->command = ATA_C_SET_FEATURES; xa->fis->features = ATA_SF_LOOKAHEAD_EN; xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; xa->fis->device = 0; xa->flags = ATA_F_PIO | ATA_F_POLL; xa->timeout = 1000; xa->datalen = 0; if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) at->at_features |= ATA_PORT_F_RAHEAD; else kprintf("%s: Unable to enable read-ahead\n", ATANAME(ap, atx)); ahci_ata_put_xfer(xa); } /* * FREEZE LOCK the device so malicious users can't lock it on us. * As there is no harm in issuing this to devices that don't * support the security feature set we just send it, and don't bother * checking if the device sends a command abort to tell us it doesn't * support it */ if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) && (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 && (AhciNoFeatures & (1 << ap->ap_num)) == 0) { xa = ahci_ata_get_xfer(ap, atx); xa->complete = ahci_ata_dummy_done; xa->fis->command = ATA_C_SEC_FREEZE_LOCK; xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; xa->flags = ATA_F_PIO | ATA_F_POLL; xa->timeout = 1000; xa->datalen = 0; if (ahci_ata_cmd(xa) == ATA_S_COMPLETE) at->at_features |= ATA_PORT_F_FRZLCK; else kprintf("%s: Unable to set security freeze\n", ATANAME(ap, atx)); ahci_ata_put_xfer(xa); } if (at->at_identify.satafsup & SATA_FEATURE_SUP_DEVSLEEP) { if (ahci_read_log(ap, at, 0x30, 0x08, ap->ap_err_scratch)==0) { struct ata_log_address_30h_page_08h *log; uint64_t timing; log = (struct ata_log_address_30h_page_08h *) ap->ap_err_scratch; /* Check validity by checking always one bits. */ if (log->page != 8 || !(log->reserved1 & __BIT(31)) || !(log->capabilities & __BIT64(63)) || !(log->settings & __BIT64(63))) { kprintf("%s: Log 30h page 08h looks invalid\n", ATANAME(ap, atx)); goto out_devslp; } timing = log->devslp_timing; if (timing & ATA_DEVSLP_TIMING_SUPPORTED) { at->at_devsleep_deto = __SHIFTOUT(timing, ATA_DEVSLP_EXIT_TIMEOUT); at->at_devsleep_mdat = __SHIFTOUT(timing, ATA_DEVSLP_MIN_ASSERT); if (bootverbose) { kprintf("%s: DevSleep DETO=%u " "MDAT=%u\n", ATANAME(ap, atx), at->at_devsleep_deto, at->at_devsleep_mdat); } } } else { kprintf("%s: Unable to read ATA Log 30h page 08h\n", ATANAME(ap, atx)); } } out_devslp: return (0); } /* * ATAPI-specific probe after initial ident */ static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx) { ahci_set_xfer(ap, atx); return(0); } /* * Setting the transfer mode is irrelevant for the SATA transport * but some (atapi) devices seem to need it anyway. In addition * if we are running through a SATA->PATA converter for some reason * beyond my comprehension we might have to set the mode. * * We only support DMA modes for SATA attached devices, so don't bother * with legacy modes. */ static int ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx) { struct ata_port *at; struct ata_xfer *xa; u_int16_t mode; u_int16_t mask; at = atx ? atx : ap->ap_ata[0]; /* * Figure out the supported UDMA mode. Ignore other legacy modes. */ mask = le16toh(at->at_identify.ultradma); if ((mask & 0xFF) == 0 || mask == 0xFFFF) return(0); mask &= 0xFF; mode = 0x4F; while ((mask & 0x8000) == 0) { mask <<= 1; --mode; } /* * SATA atapi devices often still report a dma mode, even though * it is irrelevant for SATA transport. It is also possible that * we are running through a SATA->PATA converter and seeing the * PATA dma mode. * * In this case the device may require a (dummy) SETXFER to be * sent before it will work properly. */ xa = ahci_ata_get_xfer(ap, atx); xa->complete = ahci_ata_dummy_done; xa->fis->command = ATA_C_SET_FEATURES; xa->fis->features = ATA_SF_SETXFER; xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; xa->fis->sector_count = mode; xa->flags = ATA_F_PIO | ATA_F_POLL; xa->timeout = 1000; xa->datalen = 0; if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) { kprintf("%s: Unable to set dummy xfer mode \n", ATANAME(ap, atx)); } else if (bootverbose) { kprintf("%s: Set dummy xfer mode to %02x\n", ATANAME(ap, atx), mode); } ahci_ata_put_xfer(xa); return(0); } /* * Fix byte ordering so buffers can be accessed as * strings. */ static void ata_fix_identify(struct ata_identify *id) { u_int16_t *swap; int i; swap = (u_int16_t *)id->serial; for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++) swap[i] = bswap16(swap[i]); swap = (u_int16_t *)id->firmware; for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++) swap[i] = bswap16(swap[i]); swap = (u_int16_t *)id->model; for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++) swap[i] = bswap16(swap[i]); } /* * Dummy done callback for xa. */ static void ahci_ata_dummy_done(struct ata_xfer *xa) { } /* * Use an engineering request to initiate a target scan for devices * behind a port multiplier. * * An asynchronous bus scan is used to avoid reentrancy issues. */ static void ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) { struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; if (ccb->ccb_h.func_code == XPT_SCAN_BUS) { ap->ap_flags &= ~AP_F_SCAN_RUNNING; if (ap->ap_flags & AP_F_SCAN_REQUESTED) { ap->ap_flags &= ~AP_F_SCAN_REQUESTED; ahci_cam_rescan(ap); } ap->ap_flags |= AP_F_SCAN_COMPLETED; wakeup(&ap->ap_flags); } xpt_free_ccb(&ccb->ccb_h); } static void ahci_cam_rescan(struct ahci_port *ap) { struct cam_path *path; union ccb *ccb; int status; int i; if (ap->ap_flags & AP_F_SCAN_RUNNING) { ap->ap_flags |= AP_F_SCAN_REQUESTED; return; } ap->ap_flags |= AP_F_SCAN_RUNNING; for (i = 0; i < AHCI_MAX_PMPORTS; ++i) { ap->ap_ata[i]->at_features |= ATA_PORT_F_RESCAN; } status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); if (status != CAM_REQ_CMP) return; ccb = xpt_alloc_ccb(); xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */ ccb->ccb_h.func_code = XPT_ENG_EXEC; ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback; ccb->ccb_h.sim_priv.entries[0].ptr = ap; ccb->crcn.flags = CAM_FLAG_NONE; xpt_action_async(ccb); } static void ahci_xpt_rescan(struct ahci_port *ap) { struct cam_path *path; union ccb *ccb; int status; status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); if (status != CAM_REQ_CMP) return; ccb = xpt_alloc_ccb(); xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */ ccb->ccb_h.func_code = XPT_SCAN_BUS; ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback; ccb->ccb_h.sim_priv.entries[0].ptr = ap; ccb->crcn.flags = CAM_FLAG_NONE; xpt_action_async(ccb); } /* * Action function - dispatch command */ static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb) { struct ahci_port *ap; struct ata_port *at, *atx; struct ccb_hdr *ccbh; /* XXX lock */ ap = cam_sim_softc(sim); atx = NULL; KKASSERT(ap != NULL); ccbh = &ccb->ccb_h; /* * Early failure checks. These checks do not apply to XPT_PATH_INQ, * otherwise the bus rescan will not remove the dead devices when * unplugging a PM. * * For non-wildcards we have one target (0) and one lun (0), * unless we have a port multiplier. * * A wildcard target indicates only the general bus is being * probed. * * Calculate at and atx. at is always non-NULL. atx is only * NULL for direct-attached devices. It will be non-NULL for * devices behind a port multiplier. * * XXX What do we do with a LUN wildcard? */ if (ccbh->target_id != CAM_TARGET_WILDCARD && ccbh->func_code != XPT_PATH_INQ) { if (ap->ap_type == ATA_PORT_T_NONE) { ccbh->status = CAM_DEV_NOT_THERE; xpt_done(ccb); return; } if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) { ccbh->status = CAM_DEV_NOT_THERE; xpt_done(ccb); return; } at = ap->ap_ata[ccbh->target_id]; if (ap->ap_type == ATA_PORT_T_PM) atx = at; if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) { ccbh->status = CAM_DEV_NOT_THERE; xpt_done(ccb); return; } } else { at = ap->ap_ata[0]; } /* * Switch on the meta XPT command */ switch(ccbh->func_code) { case XPT_ENG_EXEC: /* * This routine is called after a port multiplier has been * probed. */ ccbh->status = CAM_REQ_CMP; ahci_os_lock_port(ap); ahci_port_state_machine(ap, 0); ahci_os_unlock_port(ap); xpt_done(ccb); ahci_xpt_rescan(ap); break; case XPT_PATH_INQ: /* * This command always succeeds, otherwise the bus scan * will not detach dead devices. */ ccb->cpi.version_num = 1; ccb->cpi.hba_inquiry = 0; ccb->cpi.target_sprt = 0; ccb->cpi.hba_misc = PIM_SEQSCAN; ccb->cpi.hba_eng_cnt = 0; bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags)); ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1; ccb->cpi.max_lun = 0; ccb->cpi.async_flags = 0; ccb->cpi.hpath_id = 0; ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1; ccb->cpi.unit_number = cam_sim_unit(sim); ccb->cpi.bus_id = cam_sim_bus(sim); ccb->cpi.base_transfer_speed = 150000; ccb->cpi.transport = XPORT_SATA; ccb->cpi.transport_version = 1; ccb->cpi.protocol = PROTO_SCSI; ccb->cpi.protocol_version = SCSI_REV_2; ccb->cpi.maxio = AHCI_MAXPHYS; ccbh->status = CAM_REQ_CMP; if (ccbh->target_id == CAM_TARGET_WILDCARD) { ahci_os_lock_port(ap); ahci_port_state_machine(ap, 0); ahci_os_unlock_port(ap); } else { switch(ahci_pread(ap, AHCI_PREG_SSTS) & AHCI_PREG_SSTS_SPD) { case AHCI_PREG_SSTS_SPD_GEN1: ccb->cpi.base_transfer_speed = 150000; break; case AHCI_PREG_SSTS_SPD_GEN2: ccb->cpi.base_transfer_speed = 300000; break; case AHCI_PREG_SSTS_SPD_GEN3: ccb->cpi.base_transfer_speed = 600000; break; default: /* unknown */ ccb->cpi.base_transfer_speed = 1000; break; } #if 0 if (ap->ap_type == ATA_PORT_T_NONE) ccbh->status = CAM_DEV_NOT_THERE; #endif } xpt_done(ccb); break; case XPT_RESET_DEV: ahci_os_lock_port(ap); if (ap->ap_type == ATA_PORT_T_NONE) { ccbh->status = CAM_DEV_NOT_THERE; } else { ahci_port_reset(ap, atx, 0); ccbh->status = CAM_REQ_CMP; } ahci_os_unlock_port(ap); xpt_done(ccb); break; case XPT_RESET_BUS: ahci_os_lock_port(ap); ahci_port_reset(ap, NULL, 1); ahci_os_unlock_port(ap); ccbh->status = CAM_REQ_CMP; xpt_done(ccb); break; case XPT_SET_TRAN_SETTINGS: ccbh->status = CAM_FUNC_NOTAVAIL; xpt_done(ccb); break; case XPT_GET_TRAN_SETTINGS: ccb->cts.protocol = PROTO_SCSI; ccb->cts.protocol_version = SCSI_REV_2; ccb->cts.transport = XPORT_SATA; ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED; ccb->cts.proto_specific.valid = 0; ccb->cts.xport_specific.valid = 0; ccbh->status = CAM_REQ_CMP; xpt_done(ccb); break; case XPT_CALC_GEOMETRY: cam_calc_geometry(&ccb->ccg, 1); xpt_done(ccb); break; case XPT_SCSI_IO: /* * Our parallel startup code might have only probed through * to the IDENT, so do the last step if necessary. */ if (at->at_probe == ATA_PROBE_NEED_IDENT) ahci_cam_probe(ap, atx); if (at->at_probe != ATA_PROBE_GOOD) { ccbh->status = CAM_DEV_NOT_THERE; xpt_done(ccb); break; } switch(at->at_type) { case ATA_PORT_T_DISK: ahci_xpt_scsi_disk_io(ap, atx, ccb); break; case ATA_PORT_T_ATAPI: ahci_xpt_scsi_atapi_io(ap, atx, ccb); break; default: ccbh->status = CAM_REQ_INVALID; xpt_done(ccb); break; } break; case XPT_TRIM: { scsi_cdb_t cdb; struct ccb_scsiio *csio; csio = &ccb->csio; cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ? csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); cdb->generic.opcode = TRIM; ahci_xpt_scsi_disk_io(ap, atx, ccb); break; } default: ccbh->status = CAM_REQ_INVALID; xpt_done(ccb); break; } } /* * Poll function. * * Generally this function gets called heavily when interrupts might be * non-operational, during a halt/reboot or panic. */ static void ahci_xpt_poll(struct cam_sim *sim) { struct ahci_port *ap; ap = cam_sim_softc(sim); crit_enter(); ahci_os_lock_port(ap); ahci_port_intr(ap, 1); ahci_os_unlock_port(ap); crit_exit(); } /* * Convert the SCSI command in ccb to an ata_xfer command in xa * for ATA_PORT_T_DISK operations. Set the completion function * to convert the response back, then dispatch to the OpenBSD AHCI * layer. * * AHCI DISK commands only support a limited command set, and we * fake additional commands to make it play nice with the CAM subsystem. */ static void ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx, union ccb *ccb) { struct ccb_hdr *ccbh; struct ccb_scsiio *csio; struct ata_xfer *xa; struct ata_port *at; struct ata_fis_h2d *fis; struct ata_pass_12 *atp12; struct ata_pass_16 *atp16; scsi_cdb_t cdb; union scsi_data *rdata; int rdata_len; u_int64_t capacity; u_int64_t lba; u_int32_t count; ccbh = &ccb->csio.ccb_h; csio = &ccb->csio; at = atx ? atx : ap->ap_ata[0]; /* * XXX not passing NULL at for direct attach! */ xa = ahci_ata_get_xfer(ap, atx); rdata = (void *)csio->data_ptr; rdata_len = csio->dxfer_len; /* * Build the FIS or process the csio to completion. */ cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ? csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); switch(cdb->generic.opcode) { case REQUEST_SENSE: /* * Auto-sense everything, so explicit sense requests * return no-sense. */ ccbh->status = CAM_SCSI_STATUS_ERROR; break; case INQUIRY: /* * Inquiry supported features * * [opcode, byte2, page_code, length, control] */ if (cdb->inquiry.byte2 & SI_EVPD) { ahci_xpt_page_inquiry(ap, at, ccb); } else { bzero(rdata, rdata_len); if (rdata_len < SHORT_INQUIRY_LENGTH) { ccbh->status = CAM_CCB_LEN_ERR; break; } if (rdata_len > sizeof(rdata->inquiry_data)) rdata_len = sizeof(rdata->inquiry_data); rdata->inquiry_data.device = T_DIRECT; rdata->inquiry_data.version = SCSI_REV_SPC2; rdata->inquiry_data.response_format = 2; rdata->inquiry_data.additional_length = 32; bcopy("SATA ", rdata->inquiry_data.vendor, 8); bcopy(at->at_identify.model, rdata->inquiry_data.product, sizeof(rdata->inquiry_data.product)); bcopy(at->at_identify.firmware, rdata->inquiry_data.revision, sizeof(rdata->inquiry_data.revision)); ccbh->status = CAM_REQ_CMP; } /* * Use the vendor specific area to set the TRIM status * for scsi_da */ if (at->at_identify.support_dsm) { rdata->inquiry_data.vendor_specific1[0] = at->at_identify.support_dsm &ATA_SUPPORT_DSM_TRIM; rdata->inquiry_data.vendor_specific1[1] = at->at_identify.max_dsm_blocks; } break; case READ_CAPACITY_16: if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) { ccbh->status = CAM_REQ_INVALID; break; } if (rdata_len < sizeof(rdata->read_capacity_data_16)) { ccbh->status = CAM_CCB_LEN_ERR; break; } /* fall through */ case READ_CAPACITY: if (rdata_len < sizeof(rdata->read_capacity_data)) { ccbh->status = CAM_CCB_LEN_ERR; break; } capacity = at->at_capacity; bzero(rdata, rdata_len); if (cdb->generic.opcode == READ_CAPACITY) { rdata_len = sizeof(rdata->read_capacity_data); if (capacity > 0xFFFFFFFFU) { /* * Set capacity to 0 so maxsector winds up * being 0xffffffff in CAM in order to trigger * DA_STATE_PROBE2. */ capacity = 0; } bzero(&rdata->read_capacity_data, rdata_len); scsi_ulto4b((u_int32_t)capacity - 1, rdata->read_capacity_data.addr); scsi_ulto4b(512, rdata->read_capacity_data.length); } else { rdata_len = sizeof(rdata->read_capacity_data_16); bzero(&rdata->read_capacity_data_16, rdata_len); scsi_u64to8b(capacity - 1, rdata->read_capacity_data_16.addr); scsi_ulto4b(512, rdata->read_capacity_data_16.length); } ccbh->status = CAM_REQ_CMP; break; case SYNCHRONIZE_CACHE: /* * Synchronize cache. Specification says this can take * greater then 30 seconds so give it at least 45. */ fis = xa->fis; fis->flags = ATA_H2D_FLAGS_CMD; fis->command = ATA_C_FLUSH_CACHE; fis->device = 0; if (xa->timeout < 45000) xa->timeout = 45000; xa->datalen = 0; xa->flags = 0; xa->complete = ahci_ata_complete_disk_synchronize_cache; break; case TRIM: fis = xa->fis; fis->command = ATA_C_DATA_SET_MANAGEMENT; fis->features = (u_int8_t)ATA_SF_DSM_TRIM; fis->features_exp = (u_int8_t)(ATA_SF_DSM_TRIM>> 8); xa->flags = ATA_F_WRITE; fis->flags = ATA_H2D_FLAGS_CMD; xa->data = csio->data_ptr; xa->datalen = csio->dxfer_len; xa->timeout = ccbh->timeout*50; /* milliseconds */ fis->sector_count = (u_int8_t)(xa->datalen/512); fis->sector_count_exp = (u_int8_t)((xa->datalen/512)>>8); /* * lba field is reserved and must be 0. LBAs are encoded * in the range/length array passed as data. */ lba = 0; fis->lba_low = (u_int8_t)lba; fis->lba_mid = (u_int8_t)(lba >> 8); fis->lba_high = (u_int8_t)(lba >> 16); fis->lba_low_exp = (u_int8_t)(lba >> 24); fis->lba_mid_exp = (u_int8_t)(lba >> 32); fis->lba_high_exp = (u_int8_t)(lba >> 40); fis->device = ATA_H2D_DEVICE_LBA; xa->data = csio->data_ptr; xa->complete = ahci_ata_complete_disk_rw; ccbh->status = CAM_REQ_INPROG; break; case TEST_UNIT_READY: case START_STOP_UNIT: case PREVENT_ALLOW: /* * Just silently return success */ ccbh->status = CAM_REQ_CMP; rdata_len = 0; break; case ATA_PASS_12: atp12 = &cdb->ata_pass_12; fis = xa->fis; /* * Figure out the flags to be used, depending on the direction of the * CAM request. */ switch (ccbh->flags & CAM_DIR_MASK) { case CAM_DIR_IN: xa->flags = ATA_F_READ; break; case CAM_DIR_OUT: xa->flags = ATA_F_WRITE; break; default: xa->flags = 0; } xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE; xa->data = csio->data_ptr; xa->datalen = csio->dxfer_len; xa->complete = ahci_ata_complete_disk_rw; xa->timeout = ccbh->timeout; /* * Populate the fis from the information we received through CAM * ATA passthrough. */ fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp12->flags ? */ fis->features = atp12->features; fis->sector_count = atp12->sector_count; fis->lba_low = atp12->lba_low; fis->lba_mid = atp12->lba_mid; fis->lba_high = atp12->lba_high; fis->device = atp12->device; /* maybe always 0? */ fis->command = atp12->command; fis->control = atp12->control; /* * Mark as in progress so it is sent to the device. */ ccbh->status = CAM_REQ_INPROG; break; case ATA_PASS_16: atp16 = &cdb->ata_pass_16; fis = xa->fis; /* * Figure out the flags to be used, depending on the direction of the * CAM request. */ switch (ccbh->flags & CAM_DIR_MASK) { case CAM_DIR_IN: xa->flags = ATA_F_READ; break; case CAM_DIR_OUT: xa->flags = ATA_F_WRITE; break; default: xa->flags = 0; } xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE; xa->data = csio->data_ptr; xa->datalen = csio->dxfer_len; xa->complete = ahci_ata_complete_disk_rw; xa->timeout = ccbh->timeout; /* * Populate the fis from the information we received through CAM * ATA passthrough. */ fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp16->flags ? */ fis->features = atp16->features; fis->features_exp = atp16->features_ext; fis->sector_count = atp16->sector_count; fis->sector_count_exp = atp16->sector_count_ext; fis->lba_low = atp16->lba_low; fis->lba_low_exp = atp16->lba_low_ext; fis->lba_mid = atp16->lba_mid; fis->lba_mid_exp = atp16->lba_mid_ext; fis->lba_high = atp16->lba_high; fis->lba_mid_exp = atp16->lba_mid_ext; fis->device = atp16->device; /* maybe always 0? */ fis->command = atp16->command; /* * Mark as in progress so it is sent to the device. */ ccbh->status = CAM_REQ_INPROG; break; default: switch(cdb->generic.opcode) { case READ_6: lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF; count = cdb->rw_6.length ? cdb->rw_6.length : 0x100; xa->flags = ATA_F_READ; break; case READ_10: lba = scsi_4btoul(cdb->rw_10.addr); count = scsi_2btoul(cdb->rw_10.length); xa->flags = ATA_F_READ; break; case READ_12: lba = scsi_4btoul(cdb->rw_12.addr); count = scsi_4btoul(cdb->rw_12.length); xa->flags = ATA_F_READ; break; case READ_16: lba = scsi_8btou64(cdb->rw_16.addr); count = scsi_4btoul(cdb->rw_16.length); xa->flags = ATA_F_READ; break; case WRITE_6: lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF; count = cdb->rw_6.length ? cdb->rw_6.length : 0x100; xa->flags = ATA_F_WRITE; break; case WRITE_10: lba = scsi_4btoul(cdb->rw_10.addr); count = scsi_2btoul(cdb->rw_10.length); xa->flags = ATA_F_WRITE; break; case WRITE_12: lba = scsi_4btoul(cdb->rw_12.addr); count = scsi_4btoul(cdb->rw_12.length); xa->flags = ATA_F_WRITE; break; case WRITE_16: lba = scsi_8btou64(cdb->rw_16.addr); count = scsi_4btoul(cdb->rw_16.length); xa->flags = ATA_F_WRITE; break; default: ccbh->status = CAM_REQ_INVALID; break; } if (ccbh->status != CAM_REQ_INPROG) break; fis = xa->fis; fis->flags = ATA_H2D_FLAGS_CMD; fis->lba_low = (u_int8_t)lba; fis->lba_mid = (u_int8_t)(lba >> 8); fis->lba_high = (u_int8_t)(lba >> 16); fis->device = ATA_H2D_DEVICE_LBA; /* * NCQ only for direct-attached disks, do not currently * try to use NCQ with port multipliers. */ if (at->at_ncqdepth > 1 && ap->ap_type == ATA_PORT_T_DISK && (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) && (ccbh->flags & CAM_POLLED) == 0) { /* * Use NCQ - always uses 48 bit addressing */ xa->flags |= ATA_F_NCQ; fis->command = (xa->flags & ATA_F_WRITE) ? ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA; fis->lba_low_exp = (u_int8_t)(lba >> 24); fis->lba_mid_exp = (u_int8_t)(lba >> 32); fis->lba_high_exp = (u_int8_t)(lba >> 40); fis->sector_count = xa->tag << 3; fis->features = (u_int8_t)count; fis->features_exp = (u_int8_t)(count >> 8); } else if (count > 0x100 || lba > 0x0FFFFFFFU) { /* * Use LBA48 */ fis->command = (xa->flags & ATA_F_WRITE) ? ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT; fis->lba_low_exp = (u_int8_t)(lba >> 24); fis->lba_mid_exp = (u_int8_t)(lba >> 32); fis->lba_high_exp = (u_int8_t)(lba >> 40); fis->sector_count = (u_int8_t)count; fis->sector_count_exp = (u_int8_t)(count >> 8); } else { /* * Use LBA * * NOTE: 256 sectors is supported, stored as 0. */ fis->command = (xa->flags & ATA_F_WRITE) ? ATA_C_WRITEDMA : ATA_C_READDMA; fis->device |= (u_int8_t)(lba >> 24) & 0x0F; fis->sector_count = (u_int8_t)count; } xa->lba = lba; xa->data = csio->data_ptr; xa->datalen = csio->dxfer_len; xa->complete = ahci_ata_complete_disk_rw; xa->timeout = ccbh->timeout; /* milliseconds */ #if 0 if (xa->timeout > 10000) /* XXX - debug */ xa->timeout = 10000; #endif if (ccbh->flags & CAM_POLLED) xa->flags |= ATA_F_POLL; break; } /* * If the request is still in progress the xa and FIS have * been set up (except for the PM target), and must be dispatched. * Otherwise the request was completed. */ if (ccbh->status == CAM_REQ_INPROG) { KKASSERT(xa->complete != NULL); xa->atascsi_private = ccb; ccb->ccb_h.sim_priv.entries[0].ptr = ap; ahci_os_lock_port(ap); xa->fis->flags |= at->at_target; ahci_ata_cmd(xa); ahci_os_unlock_port(ap); } else { ahci_ata_put_xfer(xa); xpt_done(ccb); } } /* * Convert the SCSI command in ccb to an ata_xfer command in xa * for ATA_PORT_T_ATAPI operations. Set the completion function * to convert the response back, then dispatch to the OpenBSD AHCI * layer. */ static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx, union ccb *ccb) { struct ccb_hdr *ccbh; struct ccb_scsiio *csio; struct ata_xfer *xa; struct ata_fis_h2d *fis; scsi_cdb_t cdbs; scsi_cdb_t cdbd; int flags; struct ata_port *at; ccbh = &ccb->csio.ccb_h; csio = &ccb->csio; at = atx ? atx : ap->ap_ata[0]; switch (ccbh->flags & CAM_DIR_MASK) { case CAM_DIR_IN: flags = ATA_F_PACKET | ATA_F_READ; break; case CAM_DIR_OUT: flags = ATA_F_PACKET | ATA_F_WRITE; break; case CAM_DIR_NONE: flags = ATA_F_PACKET; break; default: ccbh->status = CAM_REQ_INVALID; xpt_done(ccb); return; /* NOT REACHED */ } /* * Special handling to get the rfis back into host memory while * still allowing the chip to run commands in parallel to * ATAPI devices behind a PM. */ flags |= ATA_F_AUTOSENSE; /* * The command has to fit in the packet command buffer. */ if (csio->cdb_len < 6 || csio->cdb_len > 16) { ccbh->status = CAM_CCB_LEN_ERR; xpt_done(ccb); return; } /* * Initialize the XA and FIS. It is unclear how much of * this has to mimic the equivalent ATA command. * * XXX not passing NULL at for direct attach! */ xa = ahci_ata_get_xfer(ap, atx); fis = xa->fis; fis->flags = ATA_H2D_FLAGS_CMD | at->at_target; fis->command = ATA_C_PACKET; fis->device = ATA_H2D_DEVICE_LBA; fis->sector_count = xa->tag << 3; if (flags & (ATA_F_READ | ATA_F_WRITE)) { if (flags & ATA_F_WRITE) { fis->features = ATA_H2D_FEATURES_DMA | ATA_H2D_FEATURES_DIR_WRITE; } else { fis->features = ATA_H2D_FEATURES_DMA | ATA_H2D_FEATURES_DIR_READ; } } else { fis->lba_mid = 0; fis->lba_high = 0; } fis->control = ATA_FIS_CONTROL_4BIT; xa->flags = flags; xa->data = csio->data_ptr; xa->datalen = csio->dxfer_len; xa->timeout = ccbh->timeout; /* milliseconds */ if (ccbh->flags & CAM_POLLED) xa->flags |= ATA_F_POLL; /* * Copy the cdb to the packetcmd buffer in the FIS using a * convenient pointer in the xa. * * Zero-out any trailing bytes in case the ATAPI device cares. */ cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ? csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes); bcopy(cdbs, xa->packetcmd, csio->cdb_len); if (csio->cdb_len < 16) bzero(xa->packetcmd + csio->cdb_len, 16 - csio->cdb_len); #if 0 kprintf("opcode %d cdb_len %d dxfer_len %d\n", cdbs->generic.opcode, csio->cdb_len, csio->dxfer_len); #endif /* * Some ATAPI commands do not actually follow the SCSI standard. */ cdbd = (void *)xa->packetcmd; switch(cdbd->generic.opcode) { case REQUEST_SENSE: /* * Force SENSE requests to the ATAPI sense length. * * It is unclear if this is needed or not. */ if (cdbd->sense.length == SSD_FULL_SIZE) { if (bootverbose) { kprintf("%s: Shortening sense request\n", PORTNAME(ap)); } cdbd->sense.length = offsetof(struct scsi_sense_data, extra_bytes[0]); } break; case INQUIRY: /* * Some ATAPI devices can't handle long inquiry lengths, * don't ask me why. Truncate the inquiry length. */ if (cdbd->inquiry.page_code == 0 && cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) { cdbd->inquiry.length = SHORT_INQUIRY_LENGTH; } break; case READ_6: case WRITE_6: /* * Convert *_6 to *_10 commands. Most ATAPI devices * cannot handle the SCSI READ_6 and WRITE_6 commands. */ cdbd->rw_10.opcode |= 0x20; cdbd->rw_10.byte2 = 0; cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F; cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1]; cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2]; cdbd->rw_10.addr[3] = 0; cdbd->rw_10.reserved = 0; cdbd->rw_10.length[0] = 0; cdbd->rw_10.length[1] = cdbs->rw_6.length; cdbd->rw_10.control = cdbs->rw_6.control; break; default: break; } /* * And dispatch */ xa->complete = ahci_atapi_complete_cmd; xa->atascsi_private = ccb; ccb->ccb_h.sim_priv.entries[0].ptr = ap; ahci_os_lock_port(ap); ahci_ata_cmd(xa); ahci_os_unlock_port(ap); } /* * Simulate page inquiries for disk attachments. */ static void ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb) { union { struct scsi_vpd_supported_page_list list; struct scsi_vpd_unit_serial_number serno; struct scsi_vpd_unit_devid devid; char buf[256]; } *page; scsi_cdb_t cdb; int i; int j; int len; page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO); cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes); switch(cdb->inquiry.page_code) { case SVPD_SUPPORTED_PAGE_LIST: i = 0; page->list.device = T_DIRECT; page->list.page_code = SVPD_SUPPORTED_PAGE_LIST; page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST; page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER; page->list.list[i++] = SVPD_UNIT_DEVID; page->list.length = i; len = offsetof(struct scsi_vpd_supported_page_list, list[3]); break; case SVPD_UNIT_SERIAL_NUMBER: i = 0; j = sizeof(at->at_identify.serial); for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i) ; while (j > i && at->at_identify.serial[j-1] == ' ') --j; page->serno.device = T_DIRECT; page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER; page->serno.length = j - i; bcopy(at->at_identify.serial + i, page->serno.serial_num, j - i); len = offsetof(struct scsi_vpd_unit_serial_number, serial_num[j-i]); break; case SVPD_UNIT_DEVID: /* fall through for now */ default: ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; len = 0; break; } if (ccb->ccb_h.status == CAM_REQ_INPROG) { if (len <= ccb->csio.dxfer_len) { ccb->ccb_h.status = CAM_REQ_CMP; bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len); bcopy(page, ccb->csio.data_ptr, len); ccb->csio.resid = ccb->csio.dxfer_len - len; } else { ccb->ccb_h.status = CAM_CCB_LEN_ERR; } } kfree(page, M_DEVBUF); } /* * Completion function for ATA_PORT_T_DISK cache synchronization. */ static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa) { union ccb *ccb = xa->atascsi_private; struct ccb_hdr *ccbh = &ccb->ccb_h; struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; switch(xa->state) { case ATA_S_COMPLETE: ccbh->status = CAM_REQ_CMP; ccb->csio.scsi_status = SCSI_STATUS_OK; break; case ATA_S_ERROR: kprintf("%s: synchronize_cache: error\n", ATANAME(ap, xa->at)); ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; ahci_ata_dummy_sense(&ccb->csio.sense_data); break; case ATA_S_TIMEOUT: kprintf("%s: synchronize_cache: timeout\n", ATANAME(ap, xa->at)); ccbh->status = CAM_CMD_TIMEOUT; break; default: kprintf("%s: synchronize_cache: unknown state %d\n", ATANAME(ap, xa->at), xa->state); panic("%s: Unknown state", ATANAME(ap, xa->at)); ccbh->status = CAM_REQ_CMP_ERR; break; } ahci_ata_put_xfer(xa); /*ahci_os_unlock_port(ap); ILLEGAL SEE NOTE-1 AT TOP */ xpt_done(ccb); /*ahci_os_lock_port(ap);*/ } /* * Completion function for ATA_PORT_T_DISK I/O */ static void ahci_ata_complete_disk_rw(struct ata_xfer *xa) { union ccb *ccb = xa->atascsi_private; struct ccb_hdr *ccbh = &ccb->ccb_h; struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; struct ata_fis_h2d *fis; switch(xa->state) { case ATA_S_COMPLETE: ccbh->status = CAM_REQ_CMP; ccb->csio.scsi_status = SCSI_STATUS_OK; break; case ATA_S_ERROR: fis = xa->fis; kprintf("%s: disk_rw: error fiscmd=0x%02x @off=0x%016jx, %zu\n", ATANAME(ap, xa->at), fis->command, (intmax_t)xa->lba * 512, xa->datalen); ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID; ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; ahci_ata_dummy_sense(&ccb->csio.sense_data); break; case ATA_S_TIMEOUT: kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at)); ccbh->status = CAM_CMD_TIMEOUT; ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; ahci_ata_dummy_sense(&ccb->csio.sense_data); break; default: kprintf("%s: disk_rw: unknown state %d\n", ATANAME(ap, xa->at), xa->state); panic("%s: Unknown state", ATANAME(ap, xa->at)); ccbh->status = CAM_REQ_CMP_ERR; break; } ccb->csio.resid = xa->resid; ahci_ata_put_xfer(xa); /*ahci_os_unlock_port(ap); ILLEGAL SEE NOTE-1 AT TOP */ xpt_done(ccb); /*ahci_os_lock_port(ap);*/ } /* * Completion function for ATA_PORT_T_ATAPI I/O * * Sense data is returned in the rfis. */ static void ahci_atapi_complete_cmd(struct ata_xfer *xa) { union ccb *ccb = xa->atascsi_private; struct ccb_hdr *ccbh = &ccb->ccb_h; struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr; scsi_cdb_t cdb; cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ? ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes); switch(xa->state) { case ATA_S_COMPLETE: ccbh->status = CAM_REQ_CMP; ccb->csio.scsi_status = SCSI_STATUS_OK; break; case ATA_S_ERROR: ccbh->status = CAM_SCSI_STATUS_ERROR; ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data); break; case ATA_S_TIMEOUT: kprintf("%s: cmd %d: timeout\n", PORTNAME(ap), cdb->generic.opcode); ccbh->status = CAM_CMD_TIMEOUT; ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; ahci_ata_dummy_sense(&ccb->csio.sense_data); break; default: kprintf("%s: cmd %d: unknown state %d\n", PORTNAME(ap), cdb->generic.opcode, xa->state); panic("%s: Unknown state", PORTNAME(ap)); ccbh->status = CAM_REQ_CMP_ERR; break; } ccb->csio.resid = xa->resid; xa->atascsi_private = NULL; ahci_ata_put_xfer(xa); /*ahci_os_unlock_port(ap); ILLEGAL SEE NOTE-1 AT TOP */ xpt_done(ccb); /*ahci_os_lock_port(ap);*/ } /* * Construct dummy sense data for errors on DISKs */ static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data) { sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR; sense_data->segment = 0; sense_data->flags = SSD_KEY_MEDIUM_ERROR; sense_data->info[0] = 0; sense_data->info[1] = 0; sense_data->info[2] = 0; sense_data->info[3] = 0; sense_data->extra_len = 0; } /* * Construct atapi sense data for errors on ATAPI * * The ATAPI sense data is stored in the passed rfis and must be converted * to SCSI sense data. */ static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis, struct scsi_sense_data *sense_data) { sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR; sense_data->segment = 0; sense_data->flags = (rfis->error & 0xF0) >> 4; if (rfis->error & 0x04) sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST; if (rfis->error & 0x02) sense_data->flags |= SSD_EOM; if (rfis->error & 0x01) sense_data->flags |= SSD_ILI; sense_data->info[0] = 0; sense_data->info[1] = 0; sense_data->info[2] = 0; sense_data->info[3] = 0; sense_data->extra_len = 0; } static void ahci_strip_string(const char **basep, int *lenp) { const char *base = *basep; int len = *lenp; while (len && (*base == 0 || *base == ' ')) { --len; ++base; } while (len && (base[len-1] == 0 || base[len-1] == ' ')) --len; *basep = base; *lenp = len; } |