sys/bus/cam/scsi/scsi_da.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 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 | /* * Implementation of SCSI Direct Access Peripheral driver for CAM. * * Copyright (c) 1997 Justin T. Gibbs. * All rights reserved. * * 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, * without modification, immediately at the beginning of the file. * 2. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. * * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.46 2003/10/21 22:18:19 thomas Exp $ */ #include <sys/param.h> #ifdef _KERNEL #include <sys/systm.h> #include <sys/kernel.h> #include <sys/buf.h> #include <sys/sysctl.h> #include <sys/taskqueue.h> #include <sys/lock.h> #include <sys/caps.h> #include <sys/conf.h> #include <sys/devicestat.h> #include <sys/disk.h> #include <sys/dtype.h> #include <sys/eventhandler.h> #include <sys/malloc.h> #include <sys/cons.h> #include <sys/proc.h> #include <sys/buf2.h> #endif /* _KERNEL */ #ifdef _KERNEL #include <vm/pmap.h> #endif #ifndef _KERNEL #include <stdio.h> #include <string.h> #endif /* _KERNEL */ #include <sys/camlib.h> #include "../cam.h" #include "../cam_ccb.h" #include "../cam_extend.h" #include "../cam_periph.h" #include "../cam_xpt_periph.h" #include "../cam_sim.h" #include "scsi_daio.h" #include "scsi_message.h" #ifndef _KERNEL #include "scsi_da.h" #endif /* !_KERNEL */ #ifdef _KERNEL typedef enum { DA_STATE_PROBE, DA_STATE_PROBE2, DA_STATE_NORMAL } da_state; typedef enum { DA_FLAG_PACK_INVALID = 0x001, DA_FLAG_NEW_PACK = 0x002, DA_FLAG_PACK_LOCKED = 0x004, DA_FLAG_PACK_REMOVABLE = 0x008, DA_FLAG_TAGGED_QUEUING = 0x010, DA_FLAG_RETRY_UA = 0x080, DA_FLAG_OPEN = 0x100, DA_FLAG_SCTX_INIT = 0x200, DA_FLAG_RD_LIMIT = 0x400, DA_FLAG_WR_LIMIT = 0x800, DA_FLAG_CAN_TRIM = 0x1000, DA_FLAG_CAP_MUTE = 0x2000 } da_flags; typedef enum { DA_Q_NONE = 0x00, DA_Q_NO_SYNC_CACHE = 0x01, DA_Q_NO_6_BYTE = 0x02, DA_Q_NO_PREVENT = 0x04 } da_quirks; typedef enum { DA_CCB_POLLED = 0x00, DA_CCB_PROBE = 0x01, DA_CCB_PROBE2 = 0x02, DA_CCB_BUFFER_IO = 0x03, DA_CCB_WAITING = 0x04, DA_CCB_DUMP = 0x05, DA_CCB_TRIM = 0x06, DA_CCB_TYPE_MASK = 0x0F, DA_CCB_RETRY_UA = 0x10 } da_ccb_state; /* Offsets into our private area for storing information */ #define ccb_state ppriv_field0 #define ccb_bio ppriv_ptr1 struct disk_params { u_int8_t heads; u_int32_t cylinders; u_int8_t secs_per_track; u_int32_t secsize; /* Number of bytes/sector */ u_int64_t sectors; /* total number sectors */ }; #define TRIM_MAX_BLOCKS 8 #define TRIM_MAX_RANGES TRIM_MAX_BLOCKS * 64 struct trim_request { uint8_t data[TRIM_MAX_RANGES * 8]; struct bio *bios[TRIM_MAX_RANGES]; }; struct da_softc { struct bio_queue_head bio_queue_rd; struct bio_queue_head bio_queue_wr; struct bio_queue_head bio_queue_trim; struct devstat device_stats; SLIST_ENTRY(da_softc) links; LIST_HEAD(, ccb_hdr) pending_ccbs; da_state state; da_flags flags; da_quirks quirks; int minimum_cmd_size; int outstanding_cmds_rd; /* outstanding read requests */ int outstanding_cmds_wr; /* outstanding write requests */ int tps_ticks; long tps_rd; /* read bandwidth exponential/tick */ long tps_wr; /* write bandwidth exponential/tick */ int trim_max_ranges; int trim_running; int trim_enabled; struct disk_params params; struct disk disk; union ccb saved_ccb; struct task sysctl_task; struct sysctl_ctx_list sysctl_ctx; struct sysctl_oid *sysctl_tree; struct trim_request trim_req; }; struct da_quirk_entry { struct scsi_inquiry_pattern inq_pat; da_quirks quirks; }; static const char quantum[] = "QUANTUM"; static const char microp[] = "MICROP"; static struct da_quirk_entry da_quirk_table[] = { /* SPI, FC devices */ { /* * Fujitsu M2513A MO drives. * Tested devices: M2513A2 firmware versions 1200 & 1300. * (dip switch selects whether T_DIRECT or T_OPTICAL device) * Reported by: W.Scholten <whs@xs4all.nl> */ {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* See above. */ {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * This particular Fujitsu drive doesn't like the * synchronize cache command. * Reported by: Tom Jackson <toj@gorilla.net> */ {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * This drive doesn't like the synchronize cache command * either. Reported by: Matthew Jacob <mjacob@feral.com> * in NetBSD PR kern/6027, August 24, 1998. */ {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * This drive doesn't like the synchronize cache command * either. Reported by: Hellmuth Michaelis (hm@kts.org) * (PR 8882). */ {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * Doesn't like the synchronize cache command. * Reported by: Blaz Zupan <blaz@gold.amis.net> */ {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * Doesn't like the synchronize cache command. * Reported by: Blaz Zupan <blaz@gold.amis.net> */ {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * Doesn't like the synchronize cache command. */ {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * Doesn't like the synchronize cache command. * Reported by: walter@pelissero.de */ {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * Doesn't work correctly with 6 byte reads/writes. * Returns illegal request, and points to byte 9 of the * 6-byte CDB. * Reported by: Adam McDougall <bsdx@spawnet.com> */ {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"}, /*quirks*/ DA_Q_NO_6_BYTE }, { /* See above. */ {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"}, /*quirks*/ DA_Q_NO_6_BYTE }, { /* * Doesn't like the synchronize cache command. * Reported by: walter@pelissero.de */ {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * The CISS RAID controllers do not support SYNC_CACHE */ {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, { /* * The same goes for the mly(4) controllers */ {T_DIRECT, SIP_MEDIA_FIXED, "MLY*", "*", "MYLX"}, /*quirks*/ DA_Q_NO_SYNC_CACHE }, /* * USB mass storage devices supported by umass(4) * * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so * it does not have to be specified here. */ { /* * Creative Nomad MUVO mp3 player (USB) * PR: kern/53094 */ {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"}, /*quirks*/ DA_Q_NO_PREVENT }, { /* * Sigmatel USB Flash MP3 Player * PR: kern/57046 */ {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"}, /*quirks*/ DA_Q_NO_PREVENT }, { /* * SEAGRAND NP-900 MP3 Player * PR: kern/64563 */ {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"}, /*quirks*/ DA_Q_NO_PREVENT }, { /* * Creative MUVO Slim mp3 player (USB) * PR: usb/86131 */ {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim", "*"}, /*quirks*/ DA_Q_NO_PREVENT }, { /* * Philips USB Key Audio KEY013 * PR: usb/68412 */ {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"}, /*quirks*/ DA_Q_NO_PREVENT }, }; static d_open_t daopen; static d_close_t daclose; static d_strategy_t dastrategy; static d_dump_t dadump; static d_ioctl_t daioctl; static periph_init_t dainit; static void daasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg); static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS); static periph_ctor_t daregister; static periph_dtor_t dacleanup; static periph_start_t dastart; static periph_oninv_t daoninvalidate; static void dadone(struct cam_periph *periph, union ccb *done_ccb); static int daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags); static void daprevent(struct cam_periph *periph, int action); static int dagetcapacity(struct cam_periph *periph, int ccbflags); static int dacheckmedia(struct cam_periph *periph); static void dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector); static void daflushbioq(struct bio_queue_head *bioq, int error); static void dashutdown(void *arg, int howto); #ifndef DA_DEFAULT_TIMEOUT #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */ #endif #ifndef DA_DEFAULT_RETRY #define DA_DEFAULT_RETRY 4 #endif __read_mostly int da_retry_count = DA_DEFAULT_RETRY; __read_mostly int da_default_timeout = DA_DEFAULT_TIMEOUT; __read_mostly static int da_balance_enable = 1; __read_mostly static int da_balance_ratio = 100; /* read-to-write */ __read_mostly static int da_balance_debug = 0; SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0, "CAM Direct Access Disk driver"); SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW, &da_retry_count, 0, "Normal I/O retry count"); TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count); SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW, &da_default_timeout, 0, "Normal I/O timeout (in seconds)"); TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout); SYSCTL_INT(_kern_cam_da, OID_AUTO, balance_enable, CTLFLAG_RW, &da_balance_enable, 0, "Enable tps balancing"); SYSCTL_INT(_kern_cam_da, OID_AUTO, balance_ratio, CTLFLAG_RW, &da_balance_ratio, 0, "Set read-to-write ratio 100=1:1"); SYSCTL_INT(_kern_cam_da, OID_AUTO, balance_debug, CTLFLAG_RW, &da_balance_debug, 0, "Enable tps balance debugging"); static struct periph_driver dadriver = { dainit, "da", TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0 }; PERIPHDRIVER_DECLARE(da, dadriver); static struct dev_ops da_ops = { { "da", 0, D_DISK | D_MPSAFE }, .d_open = daopen, .d_close = daclose, .d_read = physread, .d_write = physwrite, .d_strategy = dastrategy, .d_dump = dadump, .d_ioctl = daioctl }; static struct extend_array *daperiphs; MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers"); static int daioctl(struct dev_ioctl_args *ap) { int unit; int error = 0; struct buf *bp; struct cam_periph *periph; int byte_count; off_t *del_num = (off_t*)ap->a_data; off_t bytes_left; off_t bytes_start; cdev_t dev = ap->a_head.a_dev; unit = dkunit(dev); periph = cam_extend_get(daperiphs, unit); if (periph == NULL) return(ENXIO); switch (ap->a_cmd) { case DAIOCTRIM: { bytes_left = del_num[1]; bytes_start = del_num[0]; /* TRIM occurs on 512-byte sectors. */ KKASSERT((bytes_left % 512) == 0); KKASSERT((bytes_start% 512) == 0); /* Break TRIM up into int-sized commands because of b_bcount */ while(bytes_left) { /* * Rather than than squezing out more blocks in b_bcount * and having to break up the TRIM request in da_start(), * we ensure we can always TRIM this many bytes with one * TRIM command (this happens if the device only * supports one TRIM block). * * With min TRIM blksize of 1, TRIM command free * 4194240 blks(64*65535): each LBA range can address * 65535 blks and there 64 such ranges in a 512-byte * block. And, 4194240 * 512 = 0x7FFF8000 * */ byte_count = MIN(bytes_left,0x7FFF8000); bp = getnewbuf(0, 0, 0, 1); bp->b_cmd = BUF_CMD_FREEBLKS; bp->b_bio1.bio_offset = bytes_start; bp->b_bcount = byte_count; bp->b_bio1.bio_flags |= BIO_SYNC; bp->b_bio1.bio_done = biodone_sync; dev_dstrategy(ap->a_head.a_dev, &bp->b_bio1); if (biowait(&bp->b_bio1, "TRIM")) { kprintf("Error:%d\n", bp->b_error); brelse(bp); return(bp->b_error ? bp->b_error : EIO); } brelse(bp); bytes_left -= byte_count; bytes_start += byte_count; } break; } default: return(EINVAL); } return(error); } static int daopen(struct dev_open_args *ap) { cdev_t dev = ap->a_head.a_dev; struct cam_periph *periph; struct da_softc *softc; struct disk_info info; int unit; int error; /* * Disallow CAM access if RESTRICTEDROOT */ if (caps_priv_check_self(SYSCAP_RESTRICTEDROOT)) return (EPERM); unit = dkunit(dev); periph = cam_extend_get(daperiphs, unit); if (periph == NULL) { return (ENXIO); } if (cam_periph_acquire(periph) != CAM_REQ_CMP) { return(ENXIO); } cam_periph_lock(periph); if ((error = cam_periph_hold(periph, PCATCH)) != 0) { cam_periph_unlock(periph); cam_periph_release(periph); return (error); } unit = periph->unit_number; softc = (struct da_softc *)periph->softc; CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daopen: dev=%s (unit %d)\n", devtoname(dev), unit)); if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { /* Invalidate our pack information. */ disk_invalidate(&softc->disk); softc->flags &= ~DA_FLAG_PACK_INVALID; } error = dacheckmedia(periph); softc->flags |= DA_FLAG_OPEN; if (error == 0) { struct ccb_getdev *cgd; /* Build disk information structure */ bzero(&info, sizeof(info)); info.d_type = DTYPE_SCSI; /* * Grab the inquiry data to get the vendor and product names. * Put them in the typename and packname for the label. */ cgd = &xpt_alloc_ccb()->cgd; xpt_setup_ccb(&cgd->ccb_h, periph->path, /*priority*/ 1); cgd->ccb_h.func_code = XPT_GDEV_TYPE; xpt_action((union ccb *)cgd); xpt_free_ccb(&cgd->ccb_h); /* * Check to see whether or not the blocksize is set yet. * If it isn't, set it and then clear the blocksize * unavailable flag for the device statistics. */ if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){ softc->device_stats.block_size = softc->params.secsize; softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE; } } if (error == 0) { softc->flags &= ~DA_FLAG_CAP_MUTE; if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 && (softc->quirks & DA_Q_NO_PREVENT) == 0) daprevent(periph, PR_PREVENT); } else { softc->flags |= DA_FLAG_CAP_MUTE; softc->flags &= ~DA_FLAG_OPEN; cam_periph_release(periph); } cam_periph_unhold(periph, 1); return (error); } static int daclose(struct dev_close_args *ap) { cdev_t dev = ap->a_head.a_dev; struct cam_periph *periph; struct da_softc *softc; int unit; int error; unit = dkunit(dev); periph = cam_extend_get(daperiphs, unit); if (periph == NULL) return (ENXIO); cam_periph_lock(periph); if ((error = cam_periph_hold(periph, 0)) != 0) { cam_periph_unlock(periph); cam_periph_release(periph); return (error); } softc = (struct da_softc *)periph->softc; if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { union ccb *ccb; ccb = cam_periph_getccb(periph, /*priority*/1); ccb->ccb_h.ccb_state = DA_CCB_POLLED; scsi_synchronize_cache(&ccb->csio, /*retries*/1, /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG, /*begin_lba*/0,/* Cover the whole disk */ /*lb_count*/0, SSD_FULL_SIZE, 5 * 60 * 1000); cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0, /*sense_flags*/SF_RETRY_UA, &softc->device_stats); if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR) { int asc, ascq; int sense_key, error_code; scsi_extract_sense(&ccb->csio.sense_data, &error_code, &sense_key, &asc, &ascq); if (sense_key != SSD_KEY_ILLEGAL_REQUEST) scsi_sense_print(&ccb->csio); } else { xpt_print(periph->path, "Synchronize cache " "failed, status == 0x%x, scsi status == " "0x%x\n", ccb->csio.ccb_h.status, ccb->csio.scsi_status); } } if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) cam_release_devq(ccb->ccb_h.path, /*relsim_flags*/0, /*reduction*/0, /*timeout*/0, /*getcount_only*/0); xpt_release_ccb(ccb); } if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) { if ((softc->quirks & DA_Q_NO_PREVENT) == 0) daprevent(periph, PR_ALLOW); /* * If we've got removeable media, mark the blocksize as * unavailable, since it could change when new media is * inserted. */ softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE; } /* * Don't compound any ref counting software bugs with more. */ if (softc->flags & DA_FLAG_OPEN) { softc->flags &= ~DA_FLAG_OPEN; cam_periph_release(periph); } else { xpt_print(periph->path, "daclose() called on an already closed device!\n"); } cam_periph_unhold(periph, 1); return (0); } /* * Actually translate the requested transfer into one the physical driver * can understand. The transfer is described by a buf and will include * only one physical transfer. */ static int dastrategy(struct dev_strategy_args *ap) { cdev_t dev = ap->a_head.a_dev; struct bio *bio = ap->a_bio; struct buf *bp = bio->bio_buf; struct cam_periph *periph; struct da_softc *softc; u_int unit; unit = dkunit(dev); periph = cam_extend_get(daperiphs, unit); if (periph == NULL) { bp->b_error = ENXIO; goto bad; } softc = (struct da_softc *)periph->softc; cam_periph_lock(periph); #if 0 /* * check it's not too big a transfer for our adapter */ scsi_minphys(bp, &sd_switch); #endif /* * Mask interrupts so that the pack cannot be invalidated until * after we are in the queue. Otherwise, we might not properly * clean up one of the buffers. */ /* * If the device has been made invalid, error out */ if ((softc->flags & DA_FLAG_PACK_INVALID)) { cam_periph_unlock(periph); bp->b_error = ENXIO; goto bad; } /* * Place it in the queue of disk activities for this disk */ if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) bioqdisksort(&softc->bio_queue_wr, bio); else if (bp->b_cmd == BUF_CMD_FREEBLKS) bioqdisksort(&softc->bio_queue_trim, bio); else bioqdisksort(&softc->bio_queue_rd, bio); /* * Schedule ourselves for performing the work. */ xpt_schedule(periph, /* XXX priority */1); cam_periph_unlock(periph); return(0); bad: bp->b_flags |= B_ERROR; /* * Correctly set the buf to indicate a completed xfer */ bp->b_resid = bp->b_bcount; biodone(bio); return(0); } static int dadump(struct dev_dump_args *ap) { cdev_t dev = ap->a_head.a_dev; struct cam_periph *periph; struct da_softc *softc; u_int unit; u_int32_t secsize; struct ccb_scsiio *csio; unit = dkunit(dev); periph = cam_extend_get(daperiphs, unit); if (periph == NULL) return (ENXIO); softc = (struct da_softc *)periph->softc; cam_periph_lock(periph); secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */ if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { cam_periph_unlock(periph); return (ENXIO); } csio = &xpt_alloc_ccb()->csio; /* * because length == 0 means we are supposed to flush cache, we only * try to write something if length > 0. */ if (ap->a_length > 0) { xpt_setup_ccb(&csio->ccb_h, periph->path, /*priority*/1); csio->ccb_h.flags |= CAM_POLLED; csio->ccb_h.ccb_state = DA_CCB_DUMP; scsi_read_write(csio, /*retries*/1, dadone, MSG_ORDERED_Q_TAG, /*read*/FALSE, /*byte2*/0, /*minimum_cmd_size*/ softc->minimum_cmd_size, ap->a_offset / secsize, ap->a_length / secsize, /*data_ptr*/(u_int8_t *) ap->a_virtual, /*dxfer_len*/ap->a_length, /*sense_len*/SSD_FULL_SIZE, DA_DEFAULT_TIMEOUT * 1000); xpt_polled_action((union ccb *)csio); if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { kprintf("Aborting dump due to I/O error.\n"); if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR) scsi_sense_print(csio); else kprintf("status == 0x%x, scsi status == 0x%x\n", csio->ccb_h.status, csio->scsi_status); cam_periph_unlock(periph); xpt_free_ccb(&csio->ccb_h); return(EIO); } goto done; } /* * Sync the disk cache contents to the physical media. */ if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { xpt_setup_ccb(&csio->ccb_h, periph->path, /*priority*/1); csio->ccb_h.ccb_state = DA_CCB_DUMP; scsi_synchronize_cache(csio, /*retries*/1, /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG, /*begin_lba*/0,/* Cover the whole disk */ /*lb_count*/0, SSD_FULL_SIZE, 5 * 60 * 1000); xpt_polled_action((union ccb *)csio); if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR) { int asc, ascq; int sense_key, error_code; scsi_extract_sense(&csio->sense_data, &error_code, &sense_key, &asc, &ascq); if (sense_key != SSD_KEY_ILLEGAL_REQUEST) scsi_sense_print(csio); } else { xpt_print(periph->path, "Synchronize cache " "failed, status == 0x%x, scsi status == " "0x%x\n", csio->ccb_h.status, csio->scsi_status); } } } done: cam_periph_unlock(periph); xpt_free_ccb(&csio->ccb_h); return (0); } static void dainit(void) { cam_status status; /* * Create our extend array for storing the devices we attach to. */ daperiphs = cam_extend_new(); if (daperiphs == NULL) { kprintf("da: Failed to alloc extend array!\n"); return; } /* * Install a global async callback. This callback will * receive async callbacks like "new device found". */ status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL); if (status != CAM_REQ_CMP) { kprintf("da: Failed to attach master async callback " "due to status 0x%x!\n", status); } else { /* Register our shutdown event handler */ if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, NULL, SHUTDOWN_PRI_SECOND)) == NULL) kprintf("%s: shutdown event registration failed!\n", __func__); } } static void daoninvalidate(struct cam_periph *periph) { struct da_softc *softc; softc = (struct da_softc *)periph->softc; /* * De-register any async callbacks. */ xpt_register_async(0, daasync, periph, periph->path); softc->flags |= DA_FLAG_PACK_INVALID; /* * Return all queued I/O with ENXIO. * XXX Handle any transactions queued to the card * with XPT_ABORT_CCB. */ daflushbioq(&softc->bio_queue_trim, ENXIO); daflushbioq(&softc->bio_queue_wr, ENXIO); daflushbioq(&softc->bio_queue_rd, ENXIO); xpt_print(periph->path, "lost device\n"); } static void daflushbioq(struct bio_queue_head *bioq, int error) { struct bio *q_bio; struct buf *q_bp; while ((q_bio = bioq_first(bioq)) != NULL){ bioq_remove(bioq, q_bio); q_bp = q_bio->bio_buf; q_bp->b_resid = q_bp->b_bcount; q_bp->b_error = error; q_bp->b_flags |= B_ERROR; biodone(q_bio); } } static void dacleanup(struct cam_periph *periph) { struct da_softc *softc; softc = (struct da_softc *)periph->softc; devstat_remove_entry(&softc->device_stats); cam_extend_release(daperiphs, periph->unit_number); xpt_print(periph->path, "removing device entry\n"); /* * If we can't free the sysctl tree, oh well... */ if ((softc->flags & DA_FLAG_SCTX_INIT) != 0 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { xpt_print(periph->path, "can't remove sysctl context\n"); } periph->softc = NULL; if (softc->disk.d_rawdev) { cam_periph_unlock(periph); disk_destroy(&softc->disk); cam_periph_lock(periph); } kfree(softc, M_DEVBUF); } static void daasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) { struct cam_periph *periph; periph = (struct cam_periph *)callback_arg; switch (code) { case AC_FOUND_DEVICE: { struct ccb_getdev *cgd; cam_status status; cgd = (struct ccb_getdev *)arg; if (cgd == NULL) break; if (SID_TYPE(&cgd->inq_data) != T_DIRECT && SID_TYPE(&cgd->inq_data) != T_RBC && SID_TYPE(&cgd->inq_data) != T_OPTICAL) break; /* * Don't complain if a valid peripheral is already attached. */ periph = cam_periph_find(cgd->ccb_h.path, "da"); if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0) break; /* * Allocate a peripheral instance for * this device and start the probe * process. */ status = cam_periph_alloc(daregister, daoninvalidate, dacleanup, dastart, "da", CAM_PERIPH_BIO, cgd->ccb_h.path, daasync, AC_FOUND_DEVICE, cgd); if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) { kprintf("%s: Unable to attach to new device " "due to status 0x%x\n", __func__, status); } break; } case AC_SENT_BDR: case AC_BUS_RESET: { struct da_softc *softc; struct ccb_hdr *ccbh; softc = (struct da_softc *)periph->softc; /* * Don't fail on the expected unit attention * that will occur. */ softc->flags |= DA_FLAG_RETRY_UA; LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le) ccbh->ccb_state |= DA_CCB_RETRY_UA; /* FALLTHROUGH*/ } default: cam_periph_async(periph, code, path, arg); break; } } static void dasysctlinit(void *context, int pending) { struct cam_periph *periph; struct da_softc *softc; char tmpstr[80], tmpstr2[80]; periph = (struct cam_periph *)context; if (cam_periph_acquire(periph) != CAM_REQ_CMP) { return; } softc = (struct da_softc *)periph->softc; ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number); ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); sysctl_ctx_free(&softc->sysctl_ctx); sysctl_ctx_init(&softc->sysctl_ctx); softc->flags |= DA_FLAG_SCTX_INIT; softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2, CTLFLAG_RD, 0, tmpstr); if (softc->sysctl_tree == NULL) { kprintf("%s: unable to allocate sysctl tree\n", __func__); cam_periph_release(periph); return; } /* * Now register the sysctl handler, so the user can the value on * the fly. */ SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I", "Minimum CDB size"); /* Only create the option if the device supports TRIM */ if (softc->disk.d_info.d_trimflag) { SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "trim_enabled", CTLFLAG_RW, &softc->trim_enabled, 0, "Enable TRIM for this device (SSD))"); } cam_periph_release(periph); } static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS) { int error, value; value = *(int *)arg1; error = sysctl_handle_int(oidp, &value, 0, req); if ((error != 0) || (req->newptr == NULL)) return (error); /* * Acceptable values here are 6, 10 or 12, or 16. */ if (value < 6) value = 6; else if ((value > 6) && (value <= 10)) value = 10; else if ((value > 10) && (value <= 12)) value = 12; else if (value > 12) value = 16; *(int *)arg1 = value; return (0); } static cam_status daregister(struct cam_periph *periph, void *arg) { struct da_softc *softc; struct ccb_pathinq *cpi; struct ccb_getdev *cgd; char tmpstr[80]; caddr_t match; cgd = (struct ccb_getdev *)arg; if (periph == NULL) { kprintf("%s: periph was NULL!!\n", __func__); return(CAM_REQ_CMP_ERR); } if (cgd == NULL) { kprintf("%s: no getdev CCB, can't register device\n", __func__); return(CAM_REQ_CMP_ERR); } softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO); sysctl_ctx_init(&softc->sysctl_ctx); LIST_INIT(&softc->pending_ccbs); softc->state = DA_STATE_PROBE; bioq_init(&softc->bio_queue_trim); bioq_init(&softc->bio_queue_rd); bioq_init(&softc->bio_queue_wr); if (SID_IS_REMOVABLE(&cgd->inq_data)) softc->flags |= DA_FLAG_PACK_REMOVABLE; if ((cgd->inq_data.flags & SID_CmdQue) != 0) softc->flags |= DA_FLAG_TAGGED_QUEUING; /* Used to get TRIM status from AHCI driver */ if (cgd->inq_data.vendor_specific1[0] == 1) { /* * max number of lba ranges an SSD can handle in a single * TRIM command. vendor_specific1[1] is the num of 512-byte * blocks the SSD reports that can be passed in a TRIM cmd. */ softc->trim_max_ranges = min(cgd->inq_data.vendor_specific1[1] * 64, TRIM_MAX_RANGES); } periph->softc = softc; cam_extend_set(daperiphs, periph->unit_number, periph); /* * See if this device has any quirks. */ match = cam_quirkmatch((caddr_t)&cgd->inq_data, (caddr_t)da_quirk_table, NELEM(da_quirk_table), sizeof(*da_quirk_table), scsi_inquiry_match); if (match != NULL) softc->quirks = ((struct da_quirk_entry *)match)->quirks; else softc->quirks = DA_Q_NONE; /* * Unconditionally disable the synchronize cache command for * usb attachments. It's just impossible to determine if the * device supports it or not and if it doesn't the port can * brick. */ if (strncmp(periph->sim->sim_name, "umass", 5) == 0) { softc->quirks |= DA_Q_NO_SYNC_CACHE; } TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph); /* Check if the SIM does not want 6 byte commands */ cpi = &xpt_alloc_ccb()->cpi; xpt_setup_ccb(&cpi->ccb_h, periph->path, /*priority*/1); cpi->ccb_h.func_code = XPT_PATH_INQ; xpt_action((union ccb *)cpi); if (cpi->ccb_h.status == CAM_REQ_CMP && (cpi->hba_misc & PIM_NO_6_BYTE)) softc->quirks |= DA_Q_NO_6_BYTE; /* * RBC devices don't have to support READ(6), only READ(10). */ if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC) softc->minimum_cmd_size = 10; else softc->minimum_cmd_size = 6; /* * Load the user's default, if any. */ ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size", periph->unit_number); TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size); /* * 6, 10, 12, and 16 are the currently permissible values. */ if (softc->minimum_cmd_size < 6) softc->minimum_cmd_size = 6; else if ((softc->minimum_cmd_size > 6) && (softc->minimum_cmd_size <= 10)) softc->minimum_cmd_size = 10; else if ((softc->minimum_cmd_size > 10) && (softc->minimum_cmd_size <= 12)) softc->minimum_cmd_size = 12; else if (softc->minimum_cmd_size > 12) softc->minimum_cmd_size = 16; /* * The DA driver supports a blocksize, but * we don't know the blocksize until we do * a read capacity. So, set a flag to * indicate that the blocksize is * unavailable right now. We'll clear the * flag as soon as we've done a read capacity. */ devstat_add_entry(&softc->device_stats, "da", periph->unit_number, 0, DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_DISK); /* * Register this media as a disk */ CAM_SIM_UNLOCK(periph->sim); disk_create(periph->unit_number, &softc->disk, &da_ops); if (cpi->maxio == 0 || cpi->maxio > MAXPHYS) softc->disk.d_rawdev->si_iosize_max = MAXPHYS; else softc->disk.d_rawdev->si_iosize_max = cpi->maxio; if (bootverbose) { kprintf("%s%d: si_iosize_max:%d\n", periph->periph_name, periph->unit_number, softc->disk.d_rawdev->si_iosize_max); } CAM_SIM_LOCK(periph->sim); /* * Add async callbacks for bus reset and * bus device reset calls. I don't bother * checking if this fails as, in most cases, * the system will function just fine without * them and the only alternative would be to * not attach the device on failure. */ xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE, daasync, periph, periph->path); /* * Take an exclusive refcount on the periph while dastart is called * to finish the probe. The reference will be dropped in dadone at * the end of probe. */ xpt_free_ccb(&cpi->ccb_h); cam_periph_hold(periph, 0); xpt_schedule(periph, /*priority*/5); return(CAM_REQ_CMP); } static void dastart(struct cam_periph *periph, union ccb *start_ccb) { struct da_softc *softc; softc = (struct da_softc *)periph->softc; switch (softc->state) { case DA_STATE_NORMAL: { /* Pull a buffer from the queue and get going on it */ struct bio *bio; struct bio *bio_rd; struct bio *bio_wr; struct buf *bp; u_int8_t tag_code; int rd_limit; int wr_limit; /* * See if there is a buf with work for us to do.. */ bio_rd = bioq_first(&softc->bio_queue_rd); bio_wr = bioq_first(&softc->bio_queue_wr); if (periph->immediate_priority <= periph->pinfo.priority) { CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, ("queuing for immediate ccb\n")); start_ccb->ccb_h.ccb_state = DA_CCB_WAITING; SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, periph_links.sle); periph->immediate_priority = CAM_PRIORITY_NONE; wakeup(&periph->ccb_list); if (bio_rd || bio_wr) { /* * Have more work to do, so ensure we stay * scheduled */ xpt_schedule(periph, /* XXX priority */1); } break; } /* Run the trim command if not already running */ if (!softc->trim_running && (bio = bioq_first(&softc->bio_queue_trim)) != NULL) { struct trim_request *req = &softc->trim_req; struct bio *bio1; int bps = 0, ranges = 0; softc->trim_running = 1; bzero(req, sizeof(*req)); bio1 = bio; while (1) { uint64_t lba; int count; bp = bio1->bio_buf; count = bp->b_bcount / softc->params.secsize; lba = bio1->bio_offset/softc->params.secsize; bioq_remove(&softc->bio_queue_trim, bio1); while (count > 0) { int c = min(count, 0xffff); int off = ranges * 8; req->data[off + 0] = lba & 0xff; req->data[off + 1] = (lba >> 8) & 0xff; req->data[off + 2] = (lba >> 16) & 0xff; req->data[off + 3] = (lba >> 24) & 0xff; req->data[off + 4] = (lba >> 32) & 0xff; req->data[off + 5] = (lba >> 40) & 0xff; req->data[off + 6] = c & 0xff; req->data[off + 7] = (c >> 8) & 0xff; lba += c; count -= c; ranges++; } /* Try to merge multiple TRIM requests */ req->bios[bps++] = bio1; bio1 = bioq_first(&softc->bio_queue_trim); if (bio1 == NULL || bio1->bio_buf->b_bcount / softc->params.secsize > (softc->trim_max_ranges - ranges) * 0xffff) break; } cam_fill_csio(&start_ccb->csio, 1/*retries*/, dadone, CAM_DIR_OUT, MSG_SIMPLE_Q_TAG, req->data, ((ranges +63)/64)*512, SSD_FULL_SIZE, sizeof(struct scsi_rw_6), da_default_timeout*2); start_ccb->ccb_h.ccb_state = DA_CCB_TRIM; LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h, periph_links.le); start_ccb->csio.ccb_h.func_code = XPT_TRIM; start_ccb->ccb_h.ccb_bio = bio; devstat_start_transaction(&softc->device_stats); xpt_action(start_ccb); xpt_schedule(periph, 1); break; } /* * Select a read or write buffer to queue. Limit the number * of tags dedicated to reading or writing, giving reads * precedence. * * Writes to modern hard drives go into the HDs cache and * return completion nearly instantly. That is until the * cache becomes full. When the HDs cache becomes full * write commands will begin to stall. If all available * tags are taken up by writes which saturate the drive * reads will become tag-starved. * * A similar situation can occur with reads. With many * parallel readers all tags can be taken up by reads * and prevent any writes from draining, even if the HD's * cache is not full. */ rd_limit = periph->sim->max_tagged_dev_openings * 2 / 3 + 1; wr_limit = rd_limit; /* * When TPS balancing is enabled we force wr_limit to 0 * as necessary to balance the read TPS against the write * TPS. A lower or higher read:write ratio may be selected * via da_balance_ratio. * * wr_limit forcing stops queueing writes. This is generally * necessary because devices buffer writes and may starve * reads even when plenty of read tags are available. * * When no reads are being done, normalize tps_rd to avoid * instantly crowbaring the write tps. */ if (da_balance_enable && periph->sim->max_tagged_dev_openings >= 8 && (bio_rd || softc->outstanding_cmds_rd) && (bio_wr || softc->outstanding_cmds_wr) && softc->tps_rd * 100 < softc->tps_wr * da_balance_ratio) { wr_limit = 0; } else if (bio_rd == NULL && softc->outstanding_cmds_rd == 0 && softc->tps_rd < softc->tps_wr * 2) { softc->tps_rd += 100; } if (softc->tps_ticks != ticks) { softc->tps_ticks = ticks; softc->tps_rd = (softc->tps_rd * (hz - 1)) / hz; softc->tps_wr = (softc->tps_wr * (hz - 1)) / hz; } #if 1 /* DEBUGGING */ static time_t savets; if (da_balance_debug && time_uptime != savets && (bio_rd || bio_wr || softc->outstanding_cmds_rd || softc->outstanding_cmds_wr)) { kprintf("softc=%p %d/%d %d/%d tps %ld/%ld\n", softc, softc->outstanding_cmds_rd, rd_limit, softc->outstanding_cmds_wr, wr_limit, softc->tps_rd / 100, softc->tps_wr / 100); savets = time_uptime; } #endif if (bio_rd && softc->outstanding_cmds_rd < rd_limit) { bio = bio_rd; bioq_remove(&softc->bio_queue_rd, bio); softc->tps_rd += 100; } else if (bio_wr && softc->outstanding_cmds_wr < wr_limit) { bio = bio_wr; bioq_remove(&softc->bio_queue_wr, bio); softc->tps_wr += 100; } else { if (bio_rd) softc->flags |= DA_FLAG_RD_LIMIT; if (bio_wr) softc->flags |= DA_FLAG_WR_LIMIT; xpt_release_ccb(start_ccb); break; } /* * We can queue new work. */ bp = bio->bio_buf; devstat_start_transaction(&softc->device_stats); tag_code = MSG_SIMPLE_Q_TAG; switch(bp->b_cmd) { case BUF_CMD_READ: case BUF_CMD_WRITE: /* * Block read/write op */ KKASSERT(bio->bio_offset % softc->params.secsize == 0); scsi_read_write( &start_ccb->csio, da_retry_count, /* retries */ dadone, tag_code, (bp->b_cmd == BUF_CMD_READ), 0, /* byte2 */ softc->minimum_cmd_size, bio->bio_offset / softc->params.secsize, bp->b_bcount / softc->params.secsize, bp->b_data, bp->b_bcount, SSD_FULL_SIZE, /* sense_len */ da_default_timeout * 1000 ); break; case BUF_CMD_FLUSH: /* * Silently complete a flush request if the device * cannot handle it. */ if (softc->quirks & DA_Q_NO_SYNC_CACHE) { xpt_release_ccb(start_ccb); start_ccb = NULL; devstat_end_transaction_buf( &softc->device_stats, bp); biodone(bio); } else { scsi_synchronize_cache( &start_ccb->csio, 1, /* retries */ dadone, /* cbfcnp */ MSG_SIMPLE_Q_TAG, 0, /* lba */ 0, /* count (whole disk) */ SSD_FULL_SIZE, da_default_timeout*1000 /* timeout */ ); } break; case BUF_CMD_FREEBLKS: if (softc->disk.d_info.d_trimflag & DA_FLAG_CAN_TRIM){ start_ccb->csio.ccb_h.func_code = XPT_TRIM; break; } default: xpt_release_ccb(start_ccb); start_ccb = NULL; panic("dastart: unrecognized bio cmd %d", bp->b_cmd); break; /* NOT REACHED */ } /* * Block out any asyncronous callbacks * while we touch the pending ccb list. */ if (start_ccb) { start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO; LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h, periph_links.le); if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) { ++softc->outstanding_cmds_wr; } else { ++softc->outstanding_cmds_rd; } /* We expect a unit attention from this device */ if ((softc->flags & DA_FLAG_RETRY_UA) != 0) { start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA; softc->flags &= ~DA_FLAG_RETRY_UA; } start_ccb->ccb_h.ccb_bio = bio; xpt_action(start_ccb); } /* * Be sure we stay scheduled if we have more work to do. */ if (bioq_first(&softc->bio_queue_rd) || bioq_first(&softc->bio_queue_wr)) { xpt_schedule(periph, 1); } break; } case DA_STATE_PROBE: { struct ccb_scsiio *csio; struct scsi_read_capacity_data *rcap; rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO); csio = &start_ccb->csio; scsi_read_capacity(csio, /*retries*/4, dadone, MSG_SIMPLE_Q_TAG, rcap, SSD_FULL_SIZE, /*timeout*/5000); start_ccb->ccb_h.ccb_bio = NULL; start_ccb->ccb_h.ccb_state = DA_CCB_PROBE; xpt_action(start_ccb); break; } case DA_STATE_PROBE2: { struct ccb_scsiio *csio; struct scsi_read_capacity_data_16 *rcaplong; rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA, M_INTWAIT | M_ZERO); csio = &start_ccb->csio; scsi_read_capacity_16(csio, /*retries*/ 4, /*cbfcnp*/ dadone, /*tag_action*/ MSG_SIMPLE_Q_TAG, /*lba*/ 0, /*reladr*/ 0, /*pmi*/ 0, rcaplong, /*sense_len*/ SSD_FULL_SIZE, /*timeout*/ 60000); start_ccb->ccb_h.ccb_bio = NULL; start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2; xpt_action(start_ccb); break; } } } static int cmd6workaround(union ccb *ccb) { struct scsi_rw_6 cmd6; struct scsi_rw_10 *cmd10; struct da_softc *softc; u_int8_t *cdb; int frozen; cdb = ccb->csio.cdb_io.cdb_bytes; /* Translation only possible if CDB is an array and cmd is R/W6 */ if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 || (*cdb != READ_6 && *cdb != WRITE_6)) return 0; xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, " "increasing minimum_cmd_size to 10.\n"); softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc; softc->minimum_cmd_size = 10; bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6)); cmd10 = (struct scsi_rw_10 *)cdb; cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10; cmd10->byte2 = 0; scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr); cmd10->reserved = 0; scsi_ulto2b(cmd6.length, cmd10->length); cmd10->control = cmd6.control; ccb->csio.cdb_len = sizeof(*cmd10); /* Requeue request, unfreezing queue if necessary */ frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; ccb->ccb_h.status = CAM_REQUEUE_REQ; xpt_action(ccb); if (frozen) { cam_release_devq(ccb->ccb_h.path, /*relsim_flags*/0, /*reduction*/0, /*timeout*/0, /*getcount_only*/0); } return (ERESTART); } static void dadone(struct cam_periph *periph, union ccb *done_ccb) { struct da_softc *softc; struct ccb_scsiio *csio; struct disk_info info; softc = (struct da_softc *)periph->softc; csio = &done_ccb->csio; switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) { case DA_CCB_BUFFER_IO: case DA_CCB_TRIM: { struct buf *bp; struct bio *bio; int mustsched = 0; bio = (struct bio *)done_ccb->ccb_h.ccb_bio; bp = bio->bio_buf; if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { int error; int sf; if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0) sf = SF_RETRY_UA; else sf = 0; error = daerror(done_ccb, CAM_RETRY_SELTO, sf); if (error == ERESTART) { /* * A retry was scheuled, so * just return. */ return; } if (error != 0) { if (error == ENXIO) { /* * Catastrophic error. Mark our pack as * invalid. */ /* * XXX See if this is really a media * XXX change first? */ xpt_print(periph->path, "Invalidating pack\n"); softc->flags |= DA_FLAG_PACK_INVALID; } /* * Return all queued write I/O's with EIO * so the client can retry these I/Os in the * proper order should it attempt to recover. * * Leave read I/O's alone. */ daflushbioq(&softc->bio_queue_wr, EIO); bp->b_error = error; bp->b_resid = bp->b_bcount; bp->b_flags |= B_ERROR; } else { bp->b_resid = csio->resid; bp->b_error = 0; if (bp->b_resid != 0) bp->b_flags |= B_ERROR; } if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) cam_release_devq(done_ccb->ccb_h.path, /*relsim_flags*/0, /*reduction*/0, /*timeout*/0, /*getcount_only*/0); } else { if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) panic("REQ_CMP with QFRZN"); bp->b_resid = csio->resid; if (csio->resid > 0) bp->b_flags |= B_ERROR; } /* * Schedule the peripheral to pipeline further reads and * writes. A completed write wakes up more pending writes. * A completed read must wake up on either pending reads * or writes due to TPS balancing. * * Block out any asyncronous callbacks while we touch the * pending ccb list. */ LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) { --softc->outstanding_cmds_wr; if (softc->flags & DA_FLAG_WR_LIMIT) { softc->flags &= ~DA_FLAG_WR_LIMIT; mustsched = 1; } } else { --softc->outstanding_cmds_rd; if (softc->flags & (DA_FLAG_RD_LIMIT | DA_FLAG_WR_LIMIT)) { softc->flags &= ~(DA_FLAG_RD_LIMIT | DA_FLAG_WR_LIMIT); mustsched = 1; } } devstat_end_transaction_buf(&softc->device_stats, bp); if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) == DA_CCB_TRIM) { struct trim_request *req = (struct trim_request *) csio->data_ptr; int i; for (i = 1; i < softc->trim_max_ranges && req->bios[i]; i++) { struct bio *bp1 = req->bios[i]; bp1->bio_buf->b_resid = bp->b_resid; bp1->bio_buf->b_error = bp->b_error; if (bp->b_flags & B_ERROR) bp1->bio_buf->b_flags |= B_ERROR; biodone(bp1); } softc->trim_running = 0; biodone(bio); xpt_schedule(periph,1); } else biodone(bio); if (mustsched) xpt_schedule(periph, /*priority*/1); break; } case DA_CCB_PROBE: case DA_CCB_PROBE2: { struct scsi_read_capacity_data *rdcap; struct scsi_read_capacity_data_16 *rcaplong; char announce_buf[80]; int doinfo = 0; rdcap = NULL; rcaplong = NULL; if (softc->state == DA_STATE_PROBE) rdcap =(struct scsi_read_capacity_data *)csio->data_ptr; else rcaplong = (struct scsi_read_capacity_data_16 *) csio->data_ptr; bzero(&info, sizeof(info)); info.d_type = DTYPE_SCSI; info.d_serialno = xpt_path_serialno(periph->path); if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { struct disk_params *dp; uint32_t block_size; uint64_t maxsector; if (softc->state == DA_STATE_PROBE) { block_size = scsi_4btoul(rdcap->length); maxsector = scsi_4btoul(rdcap->addr); /* * According to SBC-2, if the standard 10 * byte READ CAPACITY command returns 2^32, * we should issue the 16 byte version of * the command, since the device in question * has more sectors than can be represented * with the short version of the command. */ if (maxsector == 0xffffffff) { softc->state = DA_STATE_PROBE2; kfree(rdcap, M_SCSIDA); xpt_release_ccb(done_ccb); xpt_schedule(periph, /*priority*/5); return; } } else { block_size = scsi_4btoul(rcaplong->length); maxsector = scsi_8btou64(rcaplong->addr); } dasetgeom(periph, block_size, maxsector); dp = &softc->params; ksnprintf(announce_buf, sizeof(announce_buf), "%juMB (%ju %u byte sectors: %dH %dS/T %dC)", (uintmax_t) (((uintmax_t)dp->secsize * dp->sectors) / (1024*1024)), (uintmax_t)dp->sectors, dp->secsize, dp->heads, dp->secs_per_track, dp->cylinders); info.d_media_blksize = softc->params.secsize; info.d_media_blocks = softc->params.sectors; info.d_media_size = 0; info.d_secpertrack = softc->params.secs_per_track; info.d_nheads = softc->params.heads; info.d_ncylinders = softc->params.cylinders; info.d_secpercyl = softc->params.heads * softc->params.secs_per_track; info.d_serialno = xpt_path_serialno(periph->path); doinfo = 1; } else { int error; announce_buf[0] = '\0'; /* * Retry any UNIT ATTENTION type errors. They * are expected at boot. */ error = daerror(done_ccb, CAM_RETRY_SELTO, SF_RETRY_UA|SF_NO_PRINT); if (error == ERESTART) { /* * A retry was scheuled, so * just return. */ return; } else if (error != 0) { struct scsi_sense_data *sense; int asc, ascq; int sense_key, error_code; int have_sense; cam_status status; struct ccb_getdev *cgd; /* Don't wedge this device's queue */ status = done_ccb->ccb_h.status; if ((status & CAM_DEV_QFRZN) != 0) cam_release_devq(done_ccb->ccb_h.path, /*relsim_flags*/0, /*reduction*/0, /*timeout*/0, /*getcount_only*/0); cgd = &xpt_alloc_ccb()->cgd; xpt_setup_ccb(&cgd->ccb_h, done_ccb->ccb_h.path, /* priority */ 1); cgd->ccb_h.func_code = XPT_GDEV_TYPE; xpt_action((union ccb *)cgd); if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) || ((status & CAM_AUTOSNS_VALID) == 0)) have_sense = FALSE; else have_sense = TRUE; if (have_sense) { sense = &csio->sense_data; scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq); } /* * Attach to anything that claims to be a * direct access or optical disk device, * as long as it doesn't return a "Logical * unit not supported" (0x25) error. */ if ((have_sense) && (asc != 0x25) && (error_code == SSD_CURRENT_ERROR)) { const char *sense_key_desc; const char *asc_desc; scsi_sense_desc(sense_key, asc, ascq, &cgd->inq_data, &sense_key_desc, &asc_desc); ksnprintf(announce_buf, sizeof(announce_buf), "Attempt to query device " "size failed: %s, %s", sense_key_desc, asc_desc); info.d_media_blksize = 512; doinfo = 1; } else { if (have_sense) scsi_sense_print( &done_ccb->csio); else { xpt_print(periph->path, "got CAM status %#x\n", done_ccb->ccb_h.status); } xpt_print(periph->path, "fatal error, " "failed to attach to device\n"); /* * Free up resources. */ cam_periph_invalidate(periph); } xpt_free_ccb(&cgd->ccb_h); } } kfree(csio->data_ptr, M_SCSIDA); if (announce_buf[0] != '\0') xpt_announce_periph(periph, announce_buf); if (softc->trim_max_ranges) { info.d_trimflag |= DA_FLAG_CAN_TRIM; kprintf("%s%d: supports TRIM\n", periph->periph_name, periph->unit_number); } softc->state = DA_STATE_NORMAL; /* * Since our peripheral may be invalidated by an error * above or an external event, we must release our CCB * before releasing the probe lock on the peripheral. * The peripheral will only go away once the last lock * is removed, and we need it around for the CCB release * operation. */ xpt_release_ccb(done_ccb); cam_periph_unhold(periph, 0); if (doinfo) { CAM_SIM_UNLOCK(periph->sim); disk_setdiskinfo(&softc->disk, &info); CAM_SIM_LOCK(periph->sim); /* * Create our sysctl variables, now that we know * we have successfully attached. */ taskqueue_enqueue(taskqueue_thread[mycpuid], &softc->sysctl_task); } return; } case DA_CCB_WAITING: { /* Caller will release the CCB */ wakeup(&done_ccb->ccb_h.cbfcnp); return; } case DA_CCB_DUMP: /* No-op. We're polling */ return; case DA_CCB_POLLED: /* Caller releases ccb */ wakeup(&done_ccb->ccb_h.cbfcnp); return; default: break; } xpt_release_ccb(done_ccb); } static int daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) { struct da_softc *softc; struct cam_periph *periph; int error; periph = xpt_path_periph(ccb->ccb_h.path); softc = (struct da_softc *)periph->softc; /* * Automatically detect devices that do not support * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs. */ error = 0; if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { error = cmd6workaround(ccb); } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR) && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { int sense_key, error_code, asc, ascq; scsi_extract_sense(&ccb->csio.sense_data, &error_code, &sense_key, &asc, &ascq); if (sense_key == SSD_KEY_ILLEGAL_REQUEST) error = cmd6workaround(ccb); } if (error == ERESTART) return (ERESTART); /* * XXX * Until we have a better way of doing pack validation, * don't treat UAs as errors. */ sense_flags |= SF_RETRY_UA; return(cam_periph_error(ccb, cam_flags, sense_flags, &softc->saved_ccb)); } static void daprevent(struct cam_periph *periph, int action) { struct da_softc *softc; union ccb *ccb; int error; softc = (struct da_softc *)periph->softc; if (((action == PR_ALLOW) && (softc->flags & DA_FLAG_PACK_LOCKED) == 0) || ((action == PR_PREVENT) && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) { return; } ccb = cam_periph_getccb(periph, /*priority*/1); ccb->ccb_h.ccb_state = DA_CCB_POLLED; scsi_prevent(&ccb->csio, /*retries*/1, /*cbcfp*/dadone, MSG_SIMPLE_Q_TAG, action, SSD_FULL_SIZE, 5000); error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO, SF_RETRY_UA, &softc->device_stats); if (error == 0) { if (action == PR_ALLOW) softc->flags &= ~DA_FLAG_PACK_LOCKED; else softc->flags |= DA_FLAG_PACK_LOCKED; } xpt_release_ccb(ccb); } /* * Check media on open, e.g. card reader devices which had no initial media. */ static int dacheckmedia(struct cam_periph *periph) { struct disk_params *dp; struct da_softc *softc; struct disk_info info; int error; int mute; softc = (struct da_softc *)periph->softc; dp = &softc->params; if (softc->flags & DA_FLAG_CAP_MUTE) /* additional ccb flags */ mute = CAM_QUIET; else mute = 0; error = dagetcapacity(periph, mute); /* * Only reprobe on initial open and if the media is removable. * * NOTE: If we setdiskinfo() it will take the device probe * a bit of time to probe the slices and partitions, * and mess up booting. So avoid if nothing has changed. * XXX */ if (softc->flags & DA_FLAG_OPEN) return (error); if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0) return (error); bzero(&info, sizeof(info)); info.d_type = DTYPE_SCSI; info.d_serialno = xpt_path_serialno(periph->path); if (error == 0) { CAM_SIM_UNLOCK(periph->sim); info.d_media_blksize = softc->params.secsize; info.d_media_blocks = softc->params.sectors; info.d_media_size = 0; info.d_secpertrack = softc->params.secs_per_track; info.d_nheads = softc->params.heads; info.d_ncylinders = softc->params.cylinders; info.d_secpercyl = softc->params.heads * softc->params.secs_per_track; info.d_serialno = xpt_path_serialno(periph->path); if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) { kprintf("%s%d: open removable media: " "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n", periph->periph_name, periph->unit_number, (uintmax_t)(((uintmax_t)dp->secsize * dp->sectors) / (1024*1024)), (uintmax_t)dp->sectors, dp->secsize, dp->heads, dp->secs_per_track, dp->cylinders); disk_setdiskinfo(&softc->disk, &info); } CAM_SIM_LOCK(periph->sim); } else { if (!mute || bootverbose) { kprintf("%s%d: open removable media: " "no media present\n", periph->periph_name, periph->unit_number); } info.d_media_blksize = 512; disk_setdiskinfo(&softc->disk, &info); } return (error); } static int dagetcapacity(struct cam_periph *periph, int ccbflags) { struct da_softc *softc; union ccb *ccb; struct scsi_read_capacity_data *rcap; struct scsi_read_capacity_data_16 *rcaplong; uint32_t block_len; uint64_t maxsector; int error; softc = (struct da_softc *)periph->softc; block_len = 0; maxsector = 0; error = 0; /* Do a read capacity */ rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong), M_SCSIDA, M_INTWAIT); ccb = cam_periph_getccb(periph, /*priority*/1); ccb->ccb_h.ccb_state = DA_CCB_POLLED; scsi_read_capacity(&ccb->csio, /*retries*/4, /*cbfncp*/dadone, MSG_SIMPLE_Q_TAG, rcap, SSD_FULL_SIZE, /*timeout*/60000); ccb->ccb_h.ccb_bio = NULL; ccb->ccb_h.flags |= ccbflags; error = cam_periph_runccb(ccb, daerror, /*cam_flags*/CAM_RETRY_SELTO, /*sense_flags*/SF_RETRY_UA, &softc->device_stats); if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) cam_release_devq(ccb->ccb_h.path, /*relsim_flags*/0, /*reduction*/0, /*timeout*/0, /*getcount_only*/0); if (error == 0) { block_len = scsi_4btoul(rcap->length); maxsector = scsi_4btoul(rcap->addr); if (maxsector != 0xffffffff) goto done; } else goto done; rcaplong = (struct scsi_read_capacity_data_16 *)rcap; scsi_read_capacity_16(&ccb->csio, /*retries*/ 4, /*cbfcnp*/ dadone, /*tag_action*/ MSG_SIMPLE_Q_TAG, /*lba*/ 0, /*reladr*/ 0, /*pmi*/ 0, rcaplong, /*sense_len*/ SSD_FULL_SIZE, /*timeout*/ 60000); ccb->ccb_h.ccb_bio = NULL; error = cam_periph_runccb(ccb, daerror, /*cam_flags*/CAM_RETRY_SELTO, /*sense_flags*/SF_RETRY_UA, &softc->device_stats); if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) cam_release_devq(ccb->ccb_h.path, /*relsim_flags*/0, /*reduction*/0, /*timeout*/0, /*getcount_only*/0); if (error == 0) { block_len = scsi_4btoul(rcaplong->length); maxsector = scsi_8btou64(rcaplong->addr); } done: if (error == 0) dasetgeom(periph, block_len, maxsector); xpt_release_ccb(ccb); kfree(rcap, M_SCSIDA); return (error); } static void dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector) { struct ccb_calc_geometry *ccg; struct da_softc *softc; struct disk_params *dp; softc = (struct da_softc *)periph->softc; dp = &softc->params; dp->secsize = block_len; dp->sectors = maxsector + 1; /* * Have the controller provide us with a geometry * for this disk. The only time the geometry * matters is when we boot and the controller * is the only one knowledgeable enough to come * up with something that will make this a bootable * device. */ ccg = &xpt_alloc_ccb()->ccg; xpt_setup_ccb(&ccg->ccb_h, periph->path, /*priority*/1); ccg->ccb_h.func_code = XPT_CALC_GEOMETRY; ccg->block_size = dp->secsize; ccg->volume_size = dp->sectors; ccg->heads = 0; ccg->secs_per_track = 0; ccg->cylinders = 0; xpt_action((union ccb*)ccg); if ((ccg->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { /* * We don't know what went wrong here- but just pick * a geometry so we don't have nasty things like divide * by zero. */ dp->heads = 255; dp->secs_per_track = 255; dp->cylinders = dp->sectors / (255 * 255); if (dp->cylinders == 0) { dp->cylinders = 1; } } else { dp->heads = ccg->heads; dp->secs_per_track = ccg->secs_per_track; dp->cylinders = ccg->cylinders; } xpt_free_ccb(&ccg->ccb_h); } /* * Step through all DA peripheral drivers, and if the device is still open, * sync the disk cache to physical media. */ static void dashutdown(void * arg, int howto) { struct cam_periph *periph; struct da_softc *softc; TAILQ_FOREACH(periph, &dadriver.units, unit_links) { union ccb *ccb; cam_periph_lock(periph); softc = (struct da_softc *)periph->softc; /* * We only sync the cache if the drive is still open, and * if the drive is capable of it.. */ if (((softc->flags & DA_FLAG_OPEN) == 0) || (softc->quirks & DA_Q_NO_SYNC_CACHE)) { cam_periph_unlock(periph); continue; } ccb = xpt_alloc_ccb(); xpt_setup_ccb(&ccb->ccb_h, periph->path, /*priority*/1); ccb->ccb_h.ccb_state = DA_CCB_DUMP; scsi_synchronize_cache(&ccb->csio, /*retries*/1, /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG, /*begin_lba*/0, /* whole disk */ /*lb_count*/0, SSD_FULL_SIZE, 60 * 60 * 1000); xpt_polled_action(ccb); if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR) && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)){ int error_code, sense_key, asc, ascq; scsi_extract_sense(&ccb->csio.sense_data, &error_code, &sense_key, &asc, &ascq); if (sense_key != SSD_KEY_ILLEGAL_REQUEST) scsi_sense_print(&ccb->csio); } else { xpt_print(periph->path, "Synchronize " "cache failed, status == 0x%x, scsi status " "== 0x%x\n", ccb->ccb_h.status, ccb->csio.scsi_status); } } if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) cam_release_devq(ccb->ccb_h.path, /*relsim_flags*/0, /*reduction*/0, /*timeout*/0, /*getcount_only*/0); cam_periph_unlock(periph); xpt_free_ccb(&ccb->ccb_h); } } #else /* !_KERNEL */ /* * XXX This is only left out of the kernel build to silence warnings. If, * for some reason this function is used in the kernel, the ifdefs should * be moved so it is included both in the kernel and userland. */ void scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave, u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout) { struct scsi_format_unit *scsi_cmd; scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes; scsi_cmd->opcode = FORMAT_UNIT; scsi_cmd->byte2 = byte2; scsi_ulto2b(ileave, scsi_cmd->interleave); cam_fill_csio(csio, retries, cbfcnp, /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, tag_action, data_ptr, dxfer_len, sense_len, sizeof(*scsi_cmd), timeout); } #endif /* _KERNEL */ |