28 #include <linux/module.h>
36 #include <linux/slab.h>
38 #include <scsi/scsi.h>
50 #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
51 " SCSI scanning, some SCSI devices might not be configured\n"
56 #define SCSI_TIMEOUT (2*HZ)
61 #define SCSI_UID_SER_NUM 'S'
62 #define SCSI_UID_UNKNOWN 'Z'
76 #define SCSI_SCAN_NO_RESPONSE 0
77 #define SCSI_SCAN_TARGET_PRESENT 1
78 #define SCSI_SCAN_LUN_PRESENT 2
80 static const char *scsi_null_device_strs =
"nullnullnullnull";
82 #define MAX_SCSI_LUNS 512
84 #ifdef CONFIG_SCSI_MULTI_LUN
87 static unsigned int max_scsi_luns = 1;
92 "last scsi LUN (should be between 1 and 2^32-1)");
94 #ifdef CONFIG_SCSI_SCAN_ASYNC
95 #define SCSI_SCAN_TYPE_DEFAULT "async"
97 #define SCSI_SCAN_TYPE_DEFAULT "sync"
112 static unsigned int max_scsi_report_luns = 511;
116 "REPORT LUNS maximum number of LUNS received (should be"
117 " between 1 and 16384)");
123 "Timeout (in seconds) waiting for devices to answer INQUIRY."
124 " Default is 20. Some devices may need more; most need less.");
149 if (list_empty(&scanning_hosts))
163 spin_lock(&async_scan_lock);
165 if (list_empty(&scanning_hosts))
168 spin_unlock(&async_scan_lock);
173 spin_lock(&async_scan_lock);
175 if (!list_empty(&scanning_hosts)) {
181 spin_unlock(&async_scan_lock);
196 static void scsi_unlock_floptical(
struct scsi_device *sdev,
230 int display_failure_msg = 1,
ret;
235 sdev = kzalloc(
sizeof(*sdev) + shost->
transportt->device_size,
240 sdev->
vendor = scsi_null_device_strs;
241 sdev->
model = scsi_null_device_strs;
242 sdev->
rev = scsi_null_device_strs;
245 sdev->
id = starget->
id;
294 if (shost->
hostt->slave_alloc) {
295 ret = shost->
hostt->slave_alloc(sdev);
302 display_failure_msg = 0;
303 goto out_device_destroy;
312 if (display_failure_msg)
317 static void scsi_target_destroy(
struct scsi_target *starget)
325 if (shost->
hostt->target_destroy)
326 shost->
hostt->target_destroy(starget);
328 spin_unlock_irqrestore(shost->
host_lock, flags);
332 static void scsi_target_dev_release(
struct device *dev)
342 .name =
"scsi_target",
343 .release = scsi_target_dev_release,
348 return dev->
type == &scsi_target_type;
356 struct Scsi_Host *shost = dev_to_shost(parent);
361 if (starget->
id ==
id &&
363 found_starget = starget;
370 return found_starget;
386 int channel,
uint id)
388 struct Scsi_Host *shost = dev_to_shost(parent);
392 + shost->transportt->target_size;
408 dev->
type = &scsi_target_type;
413 INIT_LIST_HEAD(&starget->
devices);
420 found_target = __scsi_find_target(parent, channel,
id);
425 spin_unlock_irqrestore(shost->
host_lock, flags);
428 if (shost->
hostt->target_alloc) {
429 error = shost->
hostt->target_alloc(starget);
432 dev_printk(
KERN_ERR, dev,
"target allocation failed, error %d\n", error);
435 scsi_target_destroy(starget);
445 spin_unlock_irqrestore(shost->
host_lock, flags);
464 scsi_target_destroy(starget);
477 struct Scsi_Host *shost = dev_to_shost(starget->
dev.parent);
483 state = starget->
state;
488 spin_unlock_irqrestore(shost->
host_lock, flags);
495 scsi_target_destroy(starget);
515 static void sanitize_inquiry_string(
unsigned char *
s,
int len)
519 for (; len > 0; (--len, ++
s)) {
541 static int scsi_probe_lun(
struct scsi_device *sdev,
unsigned char *inq_result,
542 int result_len,
int *bflags)
545 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
546 int response_len = 0;
556 try_inquiry_len = first_inquiry_len;
561 "scsi scan: INQUIRY pass %d length %d\n",
562 pass, try_inquiry_len));
565 for (count = 0; count < 3; ++
count) {
570 scsi_cmd[4] = (
unsigned char) try_inquiry_len;
572 memset(inq_result, 0, try_inquiry_len);
575 inq_result, try_inquiry_len, &sshdr,
576 HZ / 2 +
HZ * scsi_inq_timeout, 3,
581 result ?
"failed" :
"successful", result));
591 scsi_sense_valid(&sshdr)) {
593 ((sshdr.asc == 0x28) ||
594 (sshdr.asc == 0x29)) &&
604 if (resid == try_inquiry_len)
611 sanitize_inquiry_string(&inq_result[8], 8);
612 sanitize_inquiry_string(&inq_result[16], 16);
613 sanitize_inquiry_string(&inq_result[32], 4);
615 response_len = inq_result[4] + 5;
616 if (response_len > 255)
617 response_len = first_inquiry_len;
633 next_inquiry_len = 36;
635 next_inquiry_len = 58;
639 next_inquiry_len = response_len;
642 if (next_inquiry_len > try_inquiry_len) {
643 try_inquiry_len = next_inquiry_len;
649 }
else if (pass == 2) {
651 "Consider BLIST_INQUIRY_36 for this device\n",
656 try_inquiry_len = first_inquiry_len;
710 (sdev->
scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
732 static int scsi_add_lun(
struct scsi_device *sdev,
unsigned char *inq_result,
733 int *bflags,
int async)
781 sdev->
type = (inq_result[0] & 0x1f);
782 sdev->
removable = (inq_result[1] & 0x80) >> 7;
785 switch (sdev->
type) {
835 sdev->
soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
840 if (inq_result[7] & 0x60)
842 if (inq_result[7] & 0x10)
849 (inq_result[3] & 0x0f) == 1 ?
" CCS" :
"");
909 "in wrong state %s to complete scan\n",
929 if (sdev->
host->hostt->slave_configure) {
930 ret = sdev->
host->hostt->slave_configure(sdev);
938 "failed to configure device\n");
957 #ifdef CONFIG_SCSI_LOGGING
965 static unsigned char *scsi_inq_str(
unsigned char *
buf,
unsigned char *inq,
968 unsigned term = 0,
idx;
970 for (
idx = 0;
idx + first < end &&
idx + first < inq[4] + 5;
idx++) {
971 if (inq[
idx+first] >
' ') {
1002 static int scsi_probe_and_add_lun(
struct scsi_target *starget,
1003 uint lun,
int *bflagsp,
1010 struct Scsi_Host *shost = dev_to_shost(starget->
dev.parent);
1018 if (rescan || !scsi_device_created(sdev)) {
1020 "scsi scan: device exists on %s\n",
1021 dev_name(&sdev->sdev_gendev)));
1035 sdev = scsi_alloc_sdev(starget, lun, hostdata);
1044 if (scsi_probe_lun(sdev, result, result_len, &bflags))
1045 goto out_free_result;
1064 " peripheral qualifier of 3, device not"
1068 unsigned char vend[9];
1069 unsigned char mod[17];
1072 "scsi scan: consider passing scsi_mod."
1073 "dev_flags=%s:%s:0x240 or 0x1000240\n",
1074 scsi_inq_str(vend, result, 8, 16),
1075 scsi_inq_str(mod, result, 16, 32));
1081 goto out_free_result;
1104 (result[0] & 0x1f) == 0x1f &&
1105 !scsi_is_wlun(lun)) {
1107 "scsi scan: peripheral device type"
1108 " of 31, no device added\n"));
1110 goto out_free_result;
1113 res = scsi_add_lun(sdev, result, &bflags, shost->
async_scan);
1117 scsi_unlock_floptical(sdev, result);
1153 static void scsi_sequential_lun_scan(
struct scsi_target *starget,
1156 unsigned int sparse_lun,
lun, max_dev_lun;
1157 struct Scsi_Host *shost = dev_to_shost(starget->
dev.parent);
1160 "%s\n", dev_name(&starget->
dev)));
1162 max_dev_lun =
min(max_scsi_luns, shost->
max_lun);
1201 max_dev_lun =
min(5
U, max_dev_lun);
1207 max_dev_lun =
min(8
U, max_dev_lun);
1214 for (lun = 1; lun < max_dev_lun; ++
lun)
1215 if ((scsi_probe_and_add_lun(starget, lun,
NULL,
NULL, rescan,
1246 for (i = 0; i <
sizeof(
lun); i += 2)
1247 lun = lun | (((scsilun->
scsi_lun[i] << 8) |
1248 scsilun->
scsi_lun[i + 1]) << (i * 8));
1277 for (i = 0; i <
sizeof(
lun); i += 2) {
1278 scsilun->
scsi_lun[
i] = (lun >> 8) & 0xFF;
1279 scsilun->
scsi_lun[i+1] = lun & 0xFF;
1305 static int scsi_report_lun_scan(
struct scsi_target *starget,
int bflags,
1312 unsigned int num_luns;
1342 sdev = scsi_alloc_sdev(starget, 0,
NULL);
1351 sprintf(devname,
"host %d channel %d id %d",
1364 length = (max_scsi_report_luns + 1) *
sizeof(
struct scsi_lun);
1377 memset(&scsi_cmd[1], 0, 5);
1382 scsi_cmd[6] = (
unsigned char) (length >> 24) & 0xff;
1383 scsi_cmd[7] = (
unsigned char) (length >> 16) & 0xff;
1384 scsi_cmd[8] = (
unsigned char) (length >> 8) & 0xff;
1385 scsi_cmd[9] = (
unsigned char) length & 0xff;
1400 for (retries = 0; retries < 3; retries++) {
1402 " REPORT LUNS to %s (try %d)\n", devname,
1406 lun_data, length, &sshdr,
1410 " %s (try %d) result 0x%x\n", result
1411 ?
"failed" :
"successful", retries, result));
1414 else if (scsi_sense_valid(&sshdr)) {
1432 length = ((data[0] << 24) | (data[1] << 16) |
1433 (data[2] << 8) | (data[3] << 0));
1435 num_luns = (length /
sizeof(
struct scsi_lun));
1436 if (num_luns > max_scsi_report_luns) {
1438 " of %d luns reported, try increasing"
1439 " max_scsi_report_luns.\n", devname,
1440 max_scsi_report_luns, num_luns);
1441 num_luns = max_scsi_report_luns;
1445 "scsi scan: REPORT LUN scan\n"));
1451 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1470 printk(
" has a LUN larger than currently supported.\n");
1471 }
else if (lun > sdev->
host->max_lun) {
1473 " than allowed by the host adapter\n",
1478 res = scsi_probe_and_add_lun(starget,
1485 "Unexpected response"
1486 " from lun %d while scanning, scan"
1497 if (scsi_device_created(sdev))
1509 struct device *parent = &shost->shost_gendev;
1512 if (
strncmp(scsi_scan_type,
"none", 4) == 0)
1515 starget = scsi_alloc_target(parent, channel,
id);
1518 scsi_autopm_get_target(starget);
1524 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1525 scsi_probe_and_add_lun(starget, lun,
NULL, &sdev, 1, hostdata);
1526 scsi_autopm_put_host(shost);
1529 scsi_autopm_put_target(starget);
1543 return PTR_ERR(sdev);
1558 if (try_module_get(drv->
owner)) {
1561 module_put(drv->
owner);
1566 static void __scsi_scan_target(
struct device *parent,
unsigned int channel,
1567 unsigned int id,
unsigned int lun,
int rescan)
1569 struct Scsi_Host *shost = dev_to_shost(parent);
1580 starget = scsi_alloc_target(parent, channel,
id);
1583 scsi_autopm_get_target(starget);
1589 scsi_probe_and_add_lun(starget, lun,
NULL,
NULL, rescan,
NULL);
1597 res = scsi_probe_and_add_lun(starget, 0, &bflags,
NULL, rescan,
NULL);
1599 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
1604 scsi_sequential_lun_scan(starget, bflags,
1609 scsi_autopm_put_target(starget);
1633 unsigned int id,
unsigned int lun,
int rescan)
1635 struct Scsi_Host *shost = dev_to_shost(parent);
1637 if (
strncmp(scsi_scan_type,
"none", 4) == 0)
1644 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1645 __scsi_scan_target(parent, channel,
id, lun, rescan);
1646 scsi_autopm_put_host(shost);
1652 static void scsi_scan_channel(
struct Scsi_Host *shost,
unsigned int channel,
1653 unsigned int id,
unsigned int lun,
int rescan)
1658 for (
id = 0;
id < shost->
max_id; ++
id) {
1672 order_id = shost->
max_id -
id - 1;
1675 __scsi_scan_target(&shost->shost_gendev, channel,
1676 order_id, lun, rescan);
1679 __scsi_scan_target(&shost->shost_gendev, channel,
1684 unsigned int id,
unsigned int lun,
int rescan)
1688 __func__, channel,
id, lun));
1699 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1703 scsi_scan_channel(shost, channel,
id, lun,
1706 scsi_scan_channel(shost, channel,
id, lun, rescan);
1707 scsi_autopm_put_host(shost);
1714 static void scsi_sysfs_add_devices(
struct Scsi_Host *shost)
1721 if (!scsi_host_scan_allowed(shost) ||
1740 unsigned long flags;
1742 if (
strncmp(scsi_scan_type,
"sync", 4) == 0)
1746 printk(
"%s called twice for host %d", __func__,
1763 spin_unlock_irqrestore(shost->
host_lock, flags);
1766 spin_lock(&async_scan_lock);
1767 if (list_empty(&scanning_hosts))
1770 spin_unlock(&async_scan_lock);
1790 unsigned long flags;
1795 shost = data->
shost;
1800 printk(
"%s called twice for host %d", __func__,
1809 scsi_sysfs_add_devices(shost);
1813 spin_unlock_irqrestore(shost->
host_lock, flags);
1817 spin_lock(&async_scan_lock);
1819 if (!list_empty(&scanning_hosts)) {
1824 spin_unlock(&async_scan_lock);
1826 scsi_autopm_put_host(shost);
1831 static void do_scsi_scan_host(
struct Scsi_Host *shost)
1833 if (shost->
hostt->scan_finished) {
1835 if (shost->
hostt->scan_start)
1836 shost->
hostt->scan_start(shost);
1838 while (!shost->
hostt->scan_finished(shost, jiffies - start))
1851 do_scsi_scan_host(shost);
1852 scsi_finish_async_scan(data);
1863 if (
strncmp(scsi_scan_type,
"none", 4) == 0)
1865 if (scsi_autopm_get_host(shost) < 0)
1868 data = scsi_prep_async_scan(shost);
1870 do_scsi_scan_host(shost);
1871 scsi_autopm_put_host(shost);
1887 unsigned long flags;
1894 spin_unlock_irqrestore(shost->
host_lock, flags);
1898 spin_unlock_irqrestore(shost->
host_lock, flags);
1924 if (!scsi_host_scan_allowed(shost))
1926 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->
this_id);
1930 sdev = scsi_alloc_sdev(starget, 0,
NULL);