aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/sd.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2009-03-28 16:30:43 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2009-03-28 16:30:43 -0400
commitd54b3538b0bfb31351d02d1669d4a978d2abfc5f (patch)
tree5ce539ecba525b30bbfb1c46c55487099264947e /drivers/scsi/sd.c
parent5d80f8e5a9dc9c9a94d4aeaa567e219a808b8a4a (diff)
parentaf50bb993dfa673cf21ab812efe620d7e0c36319 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6: (119 commits) [SCSI] scsi_dh_rdac: Retry for NOT_READY check condition [SCSI] mpt2sas: make global symbols unique [SCSI] sd: Make revalidate less chatty [SCSI] sd: Try READ CAPACITY 16 first for SBC-2 devices [SCSI] sd: Refactor sd_read_capacity() [SCSI] mpt2sas v00.100.11.15 [SCSI] mpt2sas: add MPT2SAS_MINOR(221) to miscdevice.h [SCSI] ch: Add scsi type modalias [SCSI] 3w-9xxx: add power management support [SCSI] bsg: add linux/types.h include to bsg.h [SCSI] cxgb3i: fix function descriptions [SCSI] libiscsi: fix possbile null ptr session command cleanup [SCSI] iscsi class: remove host no argument from session creation callout [SCSI] libiscsi: pass session failure a session struct [SCSI] iscsi lib: remove qdepth param from iscsi host allocation [SCSI] iscsi lib: have lib create work queue for transmitting IO [SCSI] iscsi class: fix lock dep warning on logout [SCSI] libiscsi: don't cap queue depth in iscsi modules [SCSI] iscsi_tcp: replace scsi_debug/tcp_debug logging with iscsi conn logging [SCSI] libiscsi_tcp: replace tcp_debug/scsi_debug logging with session/conn logging ...
Diffstat (limited to 'drivers/scsi/sd.c')
-rw-r--r--drivers/scsi/sd.c349
1 files changed, 228 insertions, 121 deletions
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 4970ae4a62d6..aeab5d9dff27 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1273,42 +1273,126 @@ disable:
1273 sdkp->capacity = 0; 1273 sdkp->capacity = 0;
1274} 1274}
1275 1275
1276/* 1276static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
1277 * read disk capacity 1277 struct scsi_sense_hdr *sshdr, int sense_valid,
1278 */ 1278 int the_result)
1279static void 1279{
1280sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer) 1280 sd_print_result(sdkp, the_result);
1281 if (driver_byte(the_result) & DRIVER_SENSE)
1282 sd_print_sense_hdr(sdkp, sshdr);
1283 else
1284 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1285
1286 /*
1287 * Set dirty bit for removable devices if not ready -
1288 * sometimes drives will not report this properly.
1289 */
1290 if (sdp->removable &&
1291 sense_valid && sshdr->sense_key == NOT_READY)
1292 sdp->changed = 1;
1293
1294 /*
1295 * We used to set media_present to 0 here to indicate no media
1296 * in the drive, but some drives fail read capacity even with
1297 * media present, so we can't do that.
1298 */
1299 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1300}
1301
1302#define RC16_LEN 32
1303#if RC16_LEN > SD_BUF_SIZE
1304#error RC16_LEN must not be more than SD_BUF_SIZE
1305#endif
1306
1307static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
1308 unsigned char *buffer)
1281{ 1309{
1282 unsigned char cmd[16]; 1310 unsigned char cmd[16];
1283 int the_result, retries;
1284 int sector_size = 0;
1285 /* Force READ CAPACITY(16) when PROTECT=1 */
1286 int longrc = scsi_device_protection(sdkp->device) ? 1 : 0;
1287 struct scsi_sense_hdr sshdr; 1311 struct scsi_sense_hdr sshdr;
1288 int sense_valid = 0; 1312 int sense_valid = 0;
1289 struct scsi_device *sdp = sdkp->device; 1313 int the_result;
1314 int retries = 3;
1315 unsigned long long lba;
1316 unsigned sector_size;
1290 1317
1291repeat:
1292 retries = 3;
1293 do { 1318 do {
1294 if (longrc) { 1319 memset(cmd, 0, 16);
1295 memset((void *) cmd, 0, 16); 1320 cmd[0] = SERVICE_ACTION_IN;
1296 cmd[0] = SERVICE_ACTION_IN; 1321 cmd[1] = SAI_READ_CAPACITY_16;
1297 cmd[1] = SAI_READ_CAPACITY_16; 1322 cmd[13] = RC16_LEN;
1298 cmd[13] = 13; 1323 memset(buffer, 0, RC16_LEN);
1299 memset((void *) buffer, 0, 13); 1324
1300 } else { 1325 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1301 cmd[0] = READ_CAPACITY; 1326 buffer, RC16_LEN, &sshdr,
1302 memset((void *) &cmd[1], 0, 9); 1327 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1303 memset((void *) buffer, 0, 8); 1328
1329 if (media_not_present(sdkp, &sshdr))
1330 return -ENODEV;
1331
1332 if (the_result) {
1333 sense_valid = scsi_sense_valid(&sshdr);
1334 if (sense_valid &&
1335 sshdr.sense_key == ILLEGAL_REQUEST &&
1336 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
1337 sshdr.ascq == 0x00)
1338 /* Invalid Command Operation Code or
1339 * Invalid Field in CDB, just retry
1340 * silently with RC10 */
1341 return -EINVAL;
1304 } 1342 }
1305 1343 retries--;
1344
1345 } while (the_result && retries);
1346
1347 if (the_result) {
1348 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1349 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1350 return -EINVAL;
1351 }
1352
1353 sector_size = (buffer[8] << 24) | (buffer[9] << 16) |
1354 (buffer[10] << 8) | buffer[11];
1355 lba = (((u64)buffer[0] << 56) | ((u64)buffer[1] << 48) |
1356 ((u64)buffer[2] << 40) | ((u64)buffer[3] << 32) |
1357 ((u64)buffer[4] << 24) | ((u64)buffer[5] << 16) |
1358 ((u64)buffer[6] << 8) | (u64)buffer[7]);
1359
1360 sd_read_protection_type(sdkp, buffer);
1361
1362 if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
1363 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1364 "kernel compiled with support for large block "
1365 "devices.\n");
1366 sdkp->capacity = 0;
1367 return -EOVERFLOW;
1368 }
1369
1370 sdkp->capacity = lba + 1;
1371 return sector_size;
1372}
1373
1374static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
1375 unsigned char *buffer)
1376{
1377 unsigned char cmd[16];
1378 struct scsi_sense_hdr sshdr;
1379 int sense_valid = 0;
1380 int the_result;
1381 int retries = 3;
1382 sector_t lba;
1383 unsigned sector_size;
1384
1385 do {
1386 cmd[0] = READ_CAPACITY;
1387 memset(&cmd[1], 0, 9);
1388 memset(buffer, 0, 8);
1389
1306 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, 1390 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1307 buffer, longrc ? 13 : 8, &sshdr, 1391 buffer, 8, &sshdr,
1308 SD_TIMEOUT, SD_MAX_RETRIES, NULL); 1392 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1309 1393
1310 if (media_not_present(sdkp, &sshdr)) 1394 if (media_not_present(sdkp, &sshdr))
1311 return; 1395 return -ENODEV;
1312 1396
1313 if (the_result) 1397 if (the_result)
1314 sense_valid = scsi_sense_valid(&sshdr); 1398 sense_valid = scsi_sense_valid(&sshdr);
@@ -1316,85 +1400,96 @@ repeat:
1316 1400
1317 } while (the_result && retries); 1401 } while (the_result && retries);
1318 1402
1319 if (the_result && !longrc) { 1403 if (the_result) {
1320 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n"); 1404 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1321 sd_print_result(sdkp, the_result); 1405 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1322 if (driver_byte(the_result) & DRIVER_SENSE) 1406 return -EINVAL;
1323 sd_print_sense_hdr(sdkp, &sshdr); 1407 }
1324 else
1325 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1326 1408
1327 /* Set dirty bit for removable devices if not ready - 1409 sector_size = (buffer[4] << 24) | (buffer[5] << 16) |
1328 * sometimes drives will not report this properly. */ 1410 (buffer[6] << 8) | buffer[7];
1329 if (sdp->removable && 1411 lba = (buffer[0] << 24) | (buffer[1] << 16) |
1330 sense_valid && sshdr.sense_key == NOT_READY) 1412 (buffer[2] << 8) | buffer[3];
1331 sdp->changed = 1;
1332 1413
1333 /* Either no media are present but the drive didn't tell us, 1414 if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
1334 or they are present but the read capacity command fails */ 1415 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1335 /* sdkp->media_present = 0; -- not always correct */ 1416 "kernel compiled with support for large block "
1336 sdkp->capacity = 0; /* unknown mapped to zero - as usual */ 1417 "devices.\n");
1418 sdkp->capacity = 0;
1419 return -EOVERFLOW;
1420 }
1337 1421
1338 return; 1422 sdkp->capacity = lba + 1;
1339 } else if (the_result && longrc) { 1423 return sector_size;
1340 /* READ CAPACITY(16) has been failed */ 1424}
1341 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1342 sd_print_result(sdkp, the_result);
1343 sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n");
1344 1425
1345 sdkp->capacity = 1 + (sector_t) 0xffffffff; 1426static int sd_try_rc16_first(struct scsi_device *sdp)
1346 goto got_data; 1427{
1347 } 1428 if (sdp->scsi_level > SCSI_SPC_2)
1348 1429 return 1;
1349 if (!longrc) { 1430 if (scsi_device_protection(sdp))
1350 sector_size = (buffer[4] << 24) | 1431 return 1;
1351 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; 1432 return 0;
1352 if (buffer[0] == 0xff && buffer[1] == 0xff && 1433}
1353 buffer[2] == 0xff && buffer[3] == 0xff) { 1434
1354 if(sizeof(sdkp->capacity) > 4) { 1435/*
1355 sd_printk(KERN_NOTICE, sdkp, "Very big device. " 1436 * read disk capacity
1356 "Trying to use READ CAPACITY(16).\n"); 1437 */
1357 longrc = 1; 1438static void
1358 goto repeat; 1439sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1359 } 1440{
1360 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use " 1441 int sector_size;
1361 "a kernel compiled with support for large " 1442 struct scsi_device *sdp = sdkp->device;
1362 "block devices.\n"); 1443 sector_t old_capacity = sdkp->capacity;
1363 sdkp->capacity = 0; 1444
1445 if (sd_try_rc16_first(sdp)) {
1446 sector_size = read_capacity_16(sdkp, sdp, buffer);
1447 if (sector_size == -EOVERFLOW)
1364 goto got_data; 1448 goto got_data;
1365 } 1449 if (sector_size == -ENODEV)
1366 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) | 1450 return;
1367 (buffer[1] << 16) | 1451 if (sector_size < 0)
1368 (buffer[2] << 8) | 1452 sector_size = read_capacity_10(sdkp, sdp, buffer);
1369 buffer[3]); 1453 if (sector_size < 0)
1454 return;
1370 } else { 1455 } else {
1371 sdkp->capacity = 1 + (((u64)buffer[0] << 56) | 1456 sector_size = read_capacity_10(sdkp, sdp, buffer);
1372 ((u64)buffer[1] << 48) | 1457 if (sector_size == -EOVERFLOW)
1373 ((u64)buffer[2] << 40) | 1458 goto got_data;
1374 ((u64)buffer[3] << 32) | 1459 if (sector_size < 0)
1375 ((sector_t)buffer[4] << 24) | 1460 return;
1376 ((sector_t)buffer[5] << 16) | 1461 if ((sizeof(sdkp->capacity) > 4) &&
1377 ((sector_t)buffer[6] << 8) | 1462 (sdkp->capacity > 0xffffffffULL)) {
1378 (sector_t)buffer[7]); 1463 int old_sector_size = sector_size;
1379 1464 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1380 sector_size = (buffer[8] << 24) | 1465 "Trying to use READ CAPACITY(16).\n");
1381 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11]; 1466 sector_size = read_capacity_16(sdkp, sdp, buffer);
1382 1467 if (sector_size < 0) {
1383 sd_read_protection_type(sdkp, buffer); 1468 sd_printk(KERN_NOTICE, sdkp,
1384 } 1469 "Using 0xffffffff as device size\n");
1385 1470 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1386 /* Some devices return the total number of sectors, not the 1471 sector_size = old_sector_size;
1387 * highest sector number. Make the necessary adjustment. */ 1472 goto got_data;
1388 if (sdp->fix_capacity) { 1473 }
1389 --sdkp->capacity; 1474 }
1475 }
1390 1476
1391 /* Some devices have version which report the correct sizes 1477 /* Some devices are known to return the total number of blocks,
1392 * and others which do not. We guess size according to a heuristic 1478 * not the highest block number. Some devices have versions
1393 * and err on the side of lowering the capacity. */ 1479 * which do this and others which do not. Some devices we might
1394 } else { 1480 * suspect of doing this but we don't know for certain.
1395 if (sdp->guess_capacity) 1481 *
1396 if (sdkp->capacity & 0x01) /* odd sizes are odd */ 1482 * If we know the reported capacity is wrong, decrement it. If
1397 --sdkp->capacity; 1483 * we can only guess, then assume the number of blocks is even
1484 * (usually true but not always) and err on the side of lowering
1485 * the capacity.
1486 */
1487 if (sdp->fix_capacity ||
1488 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
1489 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
1490 "from its reported value: %llu\n",
1491 (unsigned long long) sdkp->capacity);
1492 --sdkp->capacity;
1398 } 1493 }
1399 1494
1400got_data: 1495got_data:
@@ -1437,10 +1532,11 @@ got_data:
1437 string_get_size(sz, STRING_UNITS_10, cap_str_10, 1532 string_get_size(sz, STRING_UNITS_10, cap_str_10,
1438 sizeof(cap_str_10)); 1533 sizeof(cap_str_10));
1439 1534
1440 sd_printk(KERN_NOTICE, sdkp, 1535 if (sdkp->first_scan || old_capacity != sdkp->capacity)
1441 "%llu %d-byte hardware sectors: (%s/%s)\n", 1536 sd_printk(KERN_NOTICE, sdkp,
1442 (unsigned long long)sdkp->capacity, 1537 "%llu %d-byte hardware sectors: (%s/%s)\n",
1443 sector_size, cap_str_10, cap_str_2); 1538 (unsigned long long)sdkp->capacity,
1539 sector_size, cap_str_10, cap_str_2);
1444 } 1540 }
1445 1541
1446 /* Rescale capacity to 512-byte units */ 1542 /* Rescale capacity to 512-byte units */
@@ -1477,6 +1573,7 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1477 int res; 1573 int res;
1478 struct scsi_device *sdp = sdkp->device; 1574 struct scsi_device *sdp = sdkp->device;
1479 struct scsi_mode_data data; 1575 struct scsi_mode_data data;
1576 int old_wp = sdkp->write_prot;
1480 1577
1481 set_disk_ro(sdkp->disk, 0); 1578 set_disk_ro(sdkp->disk, 0);
1482 if (sdp->skip_ms_page_3f) { 1579 if (sdp->skip_ms_page_3f) {
@@ -1517,11 +1614,13 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1517 } else { 1614 } else {
1518 sdkp->write_prot = ((data.device_specific & 0x80) != 0); 1615 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1519 set_disk_ro(sdkp->disk, sdkp->write_prot); 1616 set_disk_ro(sdkp->disk, sdkp->write_prot);
1520 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n", 1617 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
1521 sdkp->write_prot ? "on" : "off"); 1618 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1522 sd_printk(KERN_DEBUG, sdkp, 1619 sdkp->write_prot ? "on" : "off");
1523 "Mode Sense: %02x %02x %02x %02x\n", 1620 sd_printk(KERN_DEBUG, sdkp,
1524 buffer[0], buffer[1], buffer[2], buffer[3]); 1621 "Mode Sense: %02x %02x %02x %02x\n",
1622 buffer[0], buffer[1], buffer[2], buffer[3]);
1623 }
1525 } 1624 }
1526} 1625}
1527 1626
@@ -1539,6 +1638,9 @@ sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
1539 int modepage; 1638 int modepage;
1540 struct scsi_mode_data data; 1639 struct scsi_mode_data data;
1541 struct scsi_sense_hdr sshdr; 1640 struct scsi_sense_hdr sshdr;
1641 int old_wce = sdkp->WCE;
1642 int old_rcd = sdkp->RCD;
1643 int old_dpofua = sdkp->DPOFUA;
1542 1644
1543 if (sdp->skip_ms_page_8) 1645 if (sdp->skip_ms_page_8)
1544 goto defaults; 1646 goto defaults;
@@ -1610,12 +1712,14 @@ sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
1610 sdkp->DPOFUA = 0; 1712 sdkp->DPOFUA = 0;
1611 } 1713 }
1612 1714
1613 sd_printk(KERN_NOTICE, sdkp, 1715 if (sdkp->first_scan || old_wce != sdkp->WCE ||
1614 "Write cache: %s, read cache: %s, %s\n", 1716 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
1615 sdkp->WCE ? "enabled" : "disabled", 1717 sd_printk(KERN_NOTICE, sdkp,
1616 sdkp->RCD ? "disabled" : "enabled", 1718 "Write cache: %s, read cache: %s, %s\n",
1617 sdkp->DPOFUA ? "supports DPO and FUA" 1719 sdkp->WCE ? "enabled" : "disabled",
1618 : "doesn't support DPO or FUA"); 1720 sdkp->RCD ? "disabled" : "enabled",
1721 sdkp->DPOFUA ? "supports DPO and FUA"
1722 : "doesn't support DPO or FUA");
1619 1723
1620 return; 1724 return;
1621 } 1725 }
@@ -1711,15 +1815,6 @@ static int sd_revalidate_disk(struct gendisk *disk)
1711 goto out; 1815 goto out;
1712 } 1816 }
1713 1817
1714 /* defaults, until the device tells us otherwise */
1715 sdp->sector_size = 512;
1716 sdkp->capacity = 0;
1717 sdkp->media_present = 1;
1718 sdkp->write_prot = 0;
1719 sdkp->WCE = 0;
1720 sdkp->RCD = 0;
1721 sdkp->ATO = 0;
1722
1723 sd_spinup_disk(sdkp); 1818 sd_spinup_disk(sdkp);
1724 1819
1725 /* 1820 /*
@@ -1733,6 +1828,8 @@ static int sd_revalidate_disk(struct gendisk *disk)
1733 sd_read_app_tag_own(sdkp, buffer); 1828 sd_read_app_tag_own(sdkp, buffer);
1734 } 1829 }
1735 1830
1831 sdkp->first_scan = 0;
1832
1736 /* 1833 /*
1737 * We now have all cache related info, determine how we deal 1834 * We now have all cache related info, determine how we deal
1738 * with ordered requests. Note that as the current SCSI 1835 * with ordered requests. Note that as the current SCSI
@@ -1843,6 +1940,16 @@ static void sd_probe_async(void *data, async_cookie_t cookie)
1843 gd->private_data = &sdkp->driver; 1940 gd->private_data = &sdkp->driver;
1844 gd->queue = sdkp->device->request_queue; 1941 gd->queue = sdkp->device->request_queue;
1845 1942
1943 /* defaults, until the device tells us otherwise */
1944 sdp->sector_size = 512;
1945 sdkp->capacity = 0;
1946 sdkp->media_present = 1;
1947 sdkp->write_prot = 0;
1948 sdkp->WCE = 0;
1949 sdkp->RCD = 0;
1950 sdkp->ATO = 0;
1951 sdkp->first_scan = 1;
1952
1846 sd_revalidate_disk(gd); 1953 sd_revalidate_disk(gd);
1847 1954
1848 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn); 1955 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);