1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * scsi_scan.c 4 * 5 * Copyright (C) 2000 Eric Youngdale, 6 * Copyright (C) 2002 Patrick Mansfield 7 * 8 * The general scanning/probing algorithm is as follows, exceptions are 9 * made to it depending on device specific flags, compilation options, and 10 * global variable (boot or module load time) settings. 11 * 12 * A specific LUN is scanned via an INQUIRY command; if the LUN has a 13 * device attached, a scsi_device is allocated and setup for it. 14 * 15 * For every id of every channel on the given host: 16 * 17 * Scan LUN 0; if the target responds to LUN 0 (even if there is no 18 * device or storage attached to LUN 0): 19 * 20 * If LUN 0 has a device attached, allocate and setup a 21 * scsi_device for it. 22 * 23 * If target is SCSI-3 or up, issue a REPORT LUN, and scan 24 * all of the LUNs returned by the REPORT LUN; else, 25 * sequentially scan LUNs up until some maximum is reached, 26 * or a LUN is seen that cannot have a device attached to it. 27 */ 28 29 #include <linux/module.h> 30 #include <linux/moduleparam.h> 31 #include <linux/init.h> 32 #include <linux/blkdev.h> 33 #include <linux/delay.h> 34 #include <linux/kthread.h> 35 #include <linux/spinlock.h> 36 #include <linux/async.h> 37 #include <linux/slab.h> 38 #include <asm/unaligned.h> 39 40 #include <scsi/scsi.h> 41 #include <scsi/scsi_cmnd.h> 42 #include <scsi/scsi_device.h> 43 #include <scsi/scsi_driver.h> 44 #include <scsi/scsi_devinfo.h> 45 #include <scsi/scsi_host.h> 46 #include <scsi/scsi_transport.h> 47 #include <scsi/scsi_dh.h> 48 #include <scsi/scsi_eh.h> 49 50 #include "scsi_priv.h" 51 #include "scsi_logging.h" 52 53 #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \ 54 " SCSI scanning, some SCSI devices might not be configured\n" 55 56 /* 57 * Default timeout 58 */ 59 #define SCSI_TIMEOUT (2*HZ) 60 #define SCSI_REPORT_LUNS_TIMEOUT (30*HZ) 61 62 /* 63 * Prefix values for the SCSI id's (stored in sysfs name field) 64 */ 65 #define SCSI_UID_SER_NUM 'S' 66 #define SCSI_UID_UNKNOWN 'Z' 67 68 /* 69 * Return values of some of the scanning functions. 70 * 71 * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this 72 * includes allocation or general failures preventing IO from being sent. 73 * 74 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available 75 * on the given LUN. 76 * 77 * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a 78 * given LUN. 79 */ 80 #define SCSI_SCAN_NO_RESPONSE 0 81 #define SCSI_SCAN_TARGET_PRESENT 1 82 #define SCSI_SCAN_LUN_PRESENT 2 83 84 static const char *scsi_null_device_strs = "nullnullnullnull"; 85 86 #define MAX_SCSI_LUNS 512 87 88 static u64 max_scsi_luns = MAX_SCSI_LUNS; 89 90 module_param_named(max_luns, max_scsi_luns, ullong, S_IRUGO|S_IWUSR); 91 MODULE_PARM_DESC(max_luns, 92 "last scsi LUN (should be between 1 and 2^64-1)"); 93 94 #ifdef CONFIG_SCSI_SCAN_ASYNC 95 #define SCSI_SCAN_TYPE_DEFAULT "async" 96 #else 97 #define SCSI_SCAN_TYPE_DEFAULT "sync" 98 #endif 99 100 char scsi_scan_type[7] = SCSI_SCAN_TYPE_DEFAULT; 101 102 module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), 103 S_IRUGO|S_IWUSR); 104 MODULE_PARM_DESC(scan, "sync, async, manual, or none. " 105 "Setting to 'manual' disables automatic scanning, but allows " 106 "for manual device scan via the 'scan' sysfs attribute."); 107 108 static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ + 18; 109 110 module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR); 111 MODULE_PARM_DESC(inq_timeout, 112 "Timeout (in seconds) waiting for devices to answer INQUIRY." 113 " Default is 20. Some devices may need more; most need less."); 114 115 /* This lock protects only this list */ 116 static DEFINE_SPINLOCK(async_scan_lock); 117 static LIST_HEAD(scanning_hosts); 118 119 struct async_scan_data { 120 struct list_head list; 121 struct Scsi_Host *shost; 122 struct completion prev_finished; 123 }; 124 125 /** 126 * scsi_enable_async_suspend - Enable async suspend and resume 127 */ 128 void scsi_enable_async_suspend(struct device *dev) 129 { 130 /* 131 * If a user has disabled async probing a likely reason is due to a 132 * storage enclosure that does not inject staggered spin-ups. For 133 * safety, make resume synchronous as well in that case. 134 */ 135 if (strncmp(scsi_scan_type, "async", 5) != 0) 136 return; 137 /* Enable asynchronous suspend and resume. */ 138 device_enable_async_suspend(dev); 139 } 140 141 /** 142 * scsi_complete_async_scans - Wait for asynchronous scans to complete 143 * 144 * When this function returns, any host which started scanning before 145 * this function was called will have finished its scan. Hosts which 146 * started scanning after this function was called may or may not have 147 * finished. 148 */ 149 int scsi_complete_async_scans(void) 150 { 151 struct async_scan_data *data; 152 153 do { 154 if (list_empty(&scanning_hosts)) 155 return 0; 156 /* If we can't get memory immediately, that's OK. Just 157 * sleep a little. Even if we never get memory, the async 158 * scans will finish eventually. 159 */ 160 data = kmalloc(sizeof(*data), GFP_KERNEL); 161 if (!data) 162 msleep(1); 163 } while (!data); 164 165 data->shost = NULL; 166 init_completion(&data->prev_finished); 167 168 spin_lock(&async_scan_lock); 169 /* Check that there's still somebody else on the list */ 170 if (list_empty(&scanning_hosts)) 171 goto done; 172 list_add_tail(&data->list, &scanning_hosts); 173 spin_unlock(&async_scan_lock); 174 175 printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n"); 176 wait_for_completion(&data->prev_finished); 177 178 spin_lock(&async_scan_lock); 179 list_del(&data->list); 180 if (!list_empty(&scanning_hosts)) { 181 struct async_scan_data *next = list_entry(scanning_hosts.next, 182 struct async_scan_data, list); 183 complete(&next->prev_finished); 184 } 185 done: 186 spin_unlock(&async_scan_lock); 187 188 kfree(data); 189 return 0; 190 } 191 192 /** 193 * scsi_unlock_floptical - unlock device via a special MODE SENSE command 194 * @sdev: scsi device to send command to 195 * @result: area to store the result of the MODE SENSE 196 * 197 * Description: 198 * Send a vendor specific MODE SENSE (not a MODE SELECT) command. 199 * Called for BLIST_KEY devices. 200 **/ 201 static void scsi_unlock_floptical(struct scsi_device *sdev, 202 unsigned char *result) 203 { 204 unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 205 206 sdev_printk(KERN_NOTICE, sdev, "unlocking floptical drive\n"); 207 scsi_cmd[0] = MODE_SENSE; 208 scsi_cmd[1] = 0; 209 scsi_cmd[2] = 0x2e; 210 scsi_cmd[3] = 0; 211 scsi_cmd[4] = 0x2a; /* size */ 212 scsi_cmd[5] = 0; 213 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL, 214 SCSI_TIMEOUT, 3, NULL); 215 } 216 217 /** 218 * scsi_alloc_sdev - allocate and setup a scsi_Device 219 * @starget: which target to allocate a &scsi_device for 220 * @lun: which lun 221 * @hostdata: usually NULL and set by ->slave_alloc instead 222 * 223 * Description: 224 * Allocate, initialize for io, and return a pointer to a scsi_Device. 225 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and 226 * adds scsi_Device to the appropriate list. 227 * 228 * Return value: 229 * scsi_Device pointer, or NULL on failure. 230 **/ 231 static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, 232 u64 lun, void *hostdata) 233 { 234 unsigned int depth; 235 struct scsi_device *sdev; 236 struct request_queue *q; 237 int display_failure_msg = 1, ret; 238 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 239 240 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size, 241 GFP_KERNEL); 242 if (!sdev) 243 goto out; 244 245 sdev->vendor = scsi_null_device_strs; 246 sdev->model = scsi_null_device_strs; 247 sdev->rev = scsi_null_device_strs; 248 sdev->host = shost; 249 sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD; 250 sdev->id = starget->id; 251 sdev->lun = lun; 252 sdev->channel = starget->channel; 253 mutex_init(&sdev->state_mutex); 254 sdev->sdev_state = SDEV_CREATED; 255 INIT_LIST_HEAD(&sdev->siblings); 256 INIT_LIST_HEAD(&sdev->same_target_siblings); 257 INIT_LIST_HEAD(&sdev->starved_entry); 258 INIT_LIST_HEAD(&sdev->event_list); 259 spin_lock_init(&sdev->list_lock); 260 mutex_init(&sdev->inquiry_mutex); 261 INIT_WORK(&sdev->event_work, scsi_evt_thread); 262 INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue); 263 264 sdev->sdev_gendev.parent = get_device(&starget->dev); 265 sdev->sdev_target = starget; 266 267 /* usually NULL and set by ->slave_alloc instead */ 268 sdev->hostdata = hostdata; 269 270 /* if the device needs this changing, it may do so in the 271 * slave_configure function */ 272 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED; 273 274 /* 275 * Some low level driver could use device->type 276 */ 277 sdev->type = -1; 278 279 /* 280 * Assume that the device will have handshaking problems, 281 * and then fix this field later if it turns out it 282 * doesn't 283 */ 284 sdev->borken = 1; 285 286 sdev->sg_reserved_size = INT_MAX; 287 288 q = blk_mq_init_queue(&sdev->host->tag_set); 289 if (IS_ERR(q)) { 290 /* release fn is set up in scsi_sysfs_device_initialise, so 291 * have to free and put manually here */ 292 put_device(&starget->dev); 293 kfree(sdev); 294 goto out; 295 } 296 sdev->request_queue = q; 297 q->queuedata = sdev; 298 __scsi_init_queue(sdev->host, q); 299 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q); 300 WARN_ON_ONCE(!blk_get_queue(q)); 301 302 depth = sdev->host->cmd_per_lun ?: 1; 303 304 /* 305 * Use .can_queue as budget map's depth because we have to 306 * support adjusting queue depth from sysfs. Meantime use 307 * default device queue depth to figure out sbitmap shift 308 * since we use this queue depth most of times. 309 */ 310 if (sbitmap_init_node(&sdev->budget_map, 311 scsi_device_max_queue_depth(sdev), 312 sbitmap_calculate_shift(depth), 313 GFP_KERNEL, sdev->request_queue->node, 314 false, true)) { 315 put_device(&starget->dev); 316 kfree(sdev); 317 goto out; 318 } 319 320 scsi_change_queue_depth(sdev, depth); 321 322 scsi_sysfs_device_initialize(sdev); 323 324 if (shost->hostt->slave_alloc) { 325 ret = shost->hostt->slave_alloc(sdev); 326 if (ret) { 327 /* 328 * if LLDD reports slave not present, don't clutter 329 * console with alloc failure messages 330 */ 331 if (ret == -ENXIO) 332 display_failure_msg = 0; 333 goto out_device_destroy; 334 } 335 } 336 337 return sdev; 338 339 out_device_destroy: 340 __scsi_remove_device(sdev); 341 out: 342 if (display_failure_msg) 343 printk(ALLOC_FAILURE_MSG, __func__); 344 return NULL; 345 } 346 347 static void scsi_target_destroy(struct scsi_target *starget) 348 { 349 struct device *dev = &starget->dev; 350 struct Scsi_Host *shost = dev_to_shost(dev->parent); 351 unsigned long flags; 352 353 BUG_ON(starget->state == STARGET_DEL); 354 starget->state = STARGET_DEL; 355 transport_destroy_device(dev); 356 spin_lock_irqsave(shost->host_lock, flags); 357 if (shost->hostt->target_destroy) 358 shost->hostt->target_destroy(starget); 359 list_del_init(&starget->siblings); 360 spin_unlock_irqrestore(shost->host_lock, flags); 361 put_device(dev); 362 } 363 364 static void scsi_target_dev_release(struct device *dev) 365 { 366 struct device *parent = dev->parent; 367 struct scsi_target *starget = to_scsi_target(dev); 368 369 kfree(starget); 370 put_device(parent); 371 } 372 373 static struct device_type scsi_target_type = { 374 .name = "scsi_target", 375 .release = scsi_target_dev_release, 376 }; 377 378 int scsi_is_target_device(const struct device *dev) 379 { 380 return dev->type == &scsi_target_type; 381 } 382 EXPORT_SYMBOL(scsi_is_target_device); 383 384 static struct scsi_target *__scsi_find_target(struct device *parent, 385 int channel, uint id) 386 { 387 struct scsi_target *starget, *found_starget = NULL; 388 struct Scsi_Host *shost = dev_to_shost(parent); 389 /* 390 * Search for an existing target for this sdev. 391 */ 392 list_for_each_entry(starget, &shost->__targets, siblings) { 393 if (starget->id == id && 394 starget->channel == channel) { 395 found_starget = starget; 396 break; 397 } 398 } 399 if (found_starget) 400 get_device(&found_starget->dev); 401 402 return found_starget; 403 } 404 405 /** 406 * scsi_target_reap_ref_release - remove target from visibility 407 * @kref: the reap_ref in the target being released 408 * 409 * Called on last put of reap_ref, which is the indication that no device 410 * under this target is visible anymore, so render the target invisible in 411 * sysfs. Note: we have to be in user context here because the target reaps 412 * should be done in places where the scsi device visibility is being removed. 413 */ 414 static void scsi_target_reap_ref_release(struct kref *kref) 415 { 416 struct scsi_target *starget 417 = container_of(kref, struct scsi_target, reap_ref); 418 419 /* 420 * if we get here and the target is still in a CREATED state that 421 * means it was allocated but never made visible (because a scan 422 * turned up no LUNs), so don't call device_del() on it. 423 */ 424 if ((starget->state != STARGET_CREATED) && 425 (starget->state != STARGET_CREATED_REMOVE)) { 426 transport_remove_device(&starget->dev); 427 device_del(&starget->dev); 428 } 429 scsi_target_destroy(starget); 430 } 431 432 static void scsi_target_reap_ref_put(struct scsi_target *starget) 433 { 434 kref_put(&starget->reap_ref, scsi_target_reap_ref_release); 435 } 436 437 /** 438 * scsi_alloc_target - allocate a new or find an existing target 439 * @parent: parent of the target (need not be a scsi host) 440 * @channel: target channel number (zero if no channels) 441 * @id: target id number 442 * 443 * Return an existing target if one exists, provided it hasn't already 444 * gone into STARGET_DEL state, otherwise allocate a new target. 445 * 446 * The target is returned with an incremented reference, so the caller 447 * is responsible for both reaping and doing a last put 448 */ 449 static struct scsi_target *scsi_alloc_target(struct device *parent, 450 int channel, uint id) 451 { 452 struct Scsi_Host *shost = dev_to_shost(parent); 453 struct device *dev = NULL; 454 unsigned long flags; 455 const int size = sizeof(struct scsi_target) 456 + shost->transportt->target_size; 457 struct scsi_target *starget; 458 struct scsi_target *found_target; 459 int error, ref_got; 460 461 starget = kzalloc(size, GFP_KERNEL); 462 if (!starget) { 463 printk(KERN_ERR "%s: allocation failure\n", __func__); 464 return NULL; 465 } 466 dev = &starget->dev; 467 device_initialize(dev); 468 kref_init(&starget->reap_ref); 469 dev->parent = get_device(parent); 470 dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id); 471 dev->bus = &scsi_bus_type; 472 dev->type = &scsi_target_type; 473 scsi_enable_async_suspend(dev); 474 starget->id = id; 475 starget->channel = channel; 476 starget->can_queue = 0; 477 INIT_LIST_HEAD(&starget->siblings); 478 INIT_LIST_HEAD(&starget->devices); 479 starget->state = STARGET_CREATED; 480 starget->scsi_level = SCSI_2; 481 starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED; 482 retry: 483 spin_lock_irqsave(shost->host_lock, flags); 484 485 found_target = __scsi_find_target(parent, channel, id); 486 if (found_target) 487 goto found; 488 489 list_add_tail(&starget->siblings, &shost->__targets); 490 spin_unlock_irqrestore(shost->host_lock, flags); 491 /* allocate and add */ 492 transport_setup_device(dev); 493 if (shost->hostt->target_alloc) { 494 error = shost->hostt->target_alloc(starget); 495 496 if(error) { 497 if (error != -ENXIO) 498 dev_err(dev, "target allocation failed, error %d\n", error); 499 /* don't want scsi_target_reap to do the final 500 * put because it will be under the host lock */ 501 scsi_target_destroy(starget); 502 return NULL; 503 } 504 } 505 get_device(dev); 506 507 return starget; 508 509 found: 510 /* 511 * release routine already fired if kref is zero, so if we can still 512 * take the reference, the target must be alive. If we can't, it must 513 * be dying and we need to wait for a new target 514 */ 515 ref_got = kref_get_unless_zero(&found_target->reap_ref); 516 517 spin_unlock_irqrestore(shost->host_lock, flags); 518 if (ref_got) { 519 put_device(dev); 520 return found_target; 521 } 522 /* 523 * Unfortunately, we found a dying target; need to wait until it's 524 * dead before we can get a new one. There is an anomaly here. We 525 * *should* call scsi_target_reap() to balance the kref_get() of the 526 * reap_ref above. However, since the target being released, it's 527 * already invisible and the reap_ref is irrelevant. If we call 528 * scsi_target_reap() we might spuriously do another device_del() on 529 * an already invisible target. 530 */ 531 put_device(&found_target->dev); 532 /* 533 * length of time is irrelevant here, we just want to yield the CPU 534 * for a tick to avoid busy waiting for the target to die. 535 */ 536 msleep(1); 537 goto retry; 538 } 539 540 /** 541 * scsi_target_reap - check to see if target is in use and destroy if not 542 * @starget: target to be checked 543 * 544 * This is used after removing a LUN or doing a last put of the target 545 * it checks atomically that nothing is using the target and removes 546 * it if so. 547 */ 548 void scsi_target_reap(struct scsi_target *starget) 549 { 550 /* 551 * serious problem if this triggers: STARGET_DEL is only set in the if 552 * the reap_ref drops to zero, so we're trying to do another final put 553 * on an already released kref 554 */ 555 BUG_ON(starget->state == STARGET_DEL); 556 scsi_target_reap_ref_put(starget); 557 } 558 559 /** 560 * scsi_sanitize_inquiry_string - remove non-graphical chars from an 561 * INQUIRY result string 562 * @s: INQUIRY result string to sanitize 563 * @len: length of the string 564 * 565 * Description: 566 * The SCSI spec says that INQUIRY vendor, product, and revision 567 * strings must consist entirely of graphic ASCII characters, 568 * padded on the right with spaces. Since not all devices obey 569 * this rule, we will replace non-graphic or non-ASCII characters 570 * with spaces. Exception: a NUL character is interpreted as a 571 * string terminator, so all the following characters are set to 572 * spaces. 573 **/ 574 void scsi_sanitize_inquiry_string(unsigned char *s, int len) 575 { 576 int terminated = 0; 577 578 for (; len > 0; (--len, ++s)) { 579 if (*s == 0) 580 terminated = 1; 581 if (terminated || *s < 0x20 || *s > 0x7e) 582 *s = ' '; 583 } 584 } 585 EXPORT_SYMBOL(scsi_sanitize_inquiry_string); 586 587 /** 588 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY 589 * @sdev: scsi_device to probe 590 * @inq_result: area to store the INQUIRY result 591 * @result_len: len of inq_result 592 * @bflags: store any bflags found here 593 * 594 * Description: 595 * Probe the lun associated with @req using a standard SCSI INQUIRY; 596 * 597 * If the INQUIRY is successful, zero is returned and the 598 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length 599 * are copied to the scsi_device any flags value is stored in *@bflags. 600 **/ 601 static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result, 602 int result_len, blist_flags_t *bflags) 603 { 604 unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 605 int first_inquiry_len, try_inquiry_len, next_inquiry_len; 606 int response_len = 0; 607 int pass, count, result; 608 struct scsi_sense_hdr sshdr; 609 610 *bflags = 0; 611 612 /* Perform up to 3 passes. The first pass uses a conservative 613 * transfer length of 36 unless sdev->inquiry_len specifies a 614 * different value. */ 615 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36; 616 try_inquiry_len = first_inquiry_len; 617 pass = 1; 618 619 next_pass: 620 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 621 "scsi scan: INQUIRY pass %d length %d\n", 622 pass, try_inquiry_len)); 623 624 /* Each pass gets up to three chances to ignore Unit Attention */ 625 for (count = 0; count < 3; ++count) { 626 int resid; 627 628 memset(scsi_cmd, 0, 6); 629 scsi_cmd[0] = INQUIRY; 630 scsi_cmd[4] = (unsigned char) try_inquiry_len; 631 632 memset(inq_result, 0, try_inquiry_len); 633 634 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, 635 inq_result, try_inquiry_len, &sshdr, 636 HZ / 2 + HZ * scsi_inq_timeout, 3, 637 &resid); 638 639 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 640 "scsi scan: INQUIRY %s with code 0x%x\n", 641 result ? "failed" : "successful", result)); 642 643 if (result > 0) { 644 /* 645 * not-ready to ready transition [asc/ascq=0x28/0x0] 646 * or power-on, reset [asc/ascq=0x29/0x0], continue. 647 * INQUIRY should not yield UNIT_ATTENTION 648 * but many buggy devices do so anyway. 649 */ 650 if (scsi_status_is_check_condition(result) && 651 scsi_sense_valid(&sshdr)) { 652 if ((sshdr.sense_key == UNIT_ATTENTION) && 653 ((sshdr.asc == 0x28) || 654 (sshdr.asc == 0x29)) && 655 (sshdr.ascq == 0)) 656 continue; 657 } 658 } else if (result == 0) { 659 /* 660 * if nothing was transferred, we try 661 * again. It's a workaround for some USB 662 * devices. 663 */ 664 if (resid == try_inquiry_len) 665 continue; 666 } 667 break; 668 } 669 670 if (result == 0) { 671 scsi_sanitize_inquiry_string(&inq_result[8], 8); 672 scsi_sanitize_inquiry_string(&inq_result[16], 16); 673 scsi_sanitize_inquiry_string(&inq_result[32], 4); 674 675 response_len = inq_result[4] + 5; 676 if (response_len > 255) 677 response_len = first_inquiry_len; /* sanity */ 678 679 /* 680 * Get any flags for this device. 681 * 682 * XXX add a bflags to scsi_device, and replace the 683 * corresponding bit fields in scsi_device, so bflags 684 * need not be passed as an argument. 685 */ 686 *bflags = scsi_get_device_flags(sdev, &inq_result[8], 687 &inq_result[16]); 688 689 /* When the first pass succeeds we gain information about 690 * what larger transfer lengths might work. */ 691 if (pass == 1) { 692 if (BLIST_INQUIRY_36 & *bflags) 693 next_inquiry_len = 36; 694 else if (sdev->inquiry_len) 695 next_inquiry_len = sdev->inquiry_len; 696 else 697 next_inquiry_len = response_len; 698 699 /* If more data is available perform the second pass */ 700 if (next_inquiry_len > try_inquiry_len) { 701 try_inquiry_len = next_inquiry_len; 702 pass = 2; 703 goto next_pass; 704 } 705 } 706 707 } else if (pass == 2) { 708 sdev_printk(KERN_INFO, sdev, 709 "scsi scan: %d byte inquiry failed. " 710 "Consider BLIST_INQUIRY_36 for this device\n", 711 try_inquiry_len); 712 713 /* If this pass failed, the third pass goes back and transfers 714 * the same amount as we successfully got in the first pass. */ 715 try_inquiry_len = first_inquiry_len; 716 pass = 3; 717 goto next_pass; 718 } 719 720 /* If the last transfer attempt got an error, assume the 721 * peripheral doesn't exist or is dead. */ 722 if (result) 723 return -EIO; 724 725 /* Don't report any more data than the device says is valid */ 726 sdev->inquiry_len = min(try_inquiry_len, response_len); 727 728 /* 729 * XXX Abort if the response length is less than 36? If less than 730 * 32, the lookup of the device flags (above) could be invalid, 731 * and it would be possible to take an incorrect action - we do 732 * not want to hang because of a short INQUIRY. On the flip side, 733 * if the device is spun down or becoming ready (and so it gives a 734 * short INQUIRY), an abort here prevents any further use of the 735 * device, including spin up. 736 * 737 * On the whole, the best approach seems to be to assume the first 738 * 36 bytes are valid no matter what the device says. That's 739 * better than copying < 36 bytes to the inquiry-result buffer 740 * and displaying garbage for the Vendor, Product, or Revision 741 * strings. 742 */ 743 if (sdev->inquiry_len < 36) { 744 if (!sdev->host->short_inquiry) { 745 shost_printk(KERN_INFO, sdev->host, 746 "scsi scan: INQUIRY result too short (%d)," 747 " using 36\n", sdev->inquiry_len); 748 sdev->host->short_inquiry = 1; 749 } 750 sdev->inquiry_len = 36; 751 } 752 753 /* 754 * Related to the above issue: 755 * 756 * XXX Devices (disk or all?) should be sent a TEST UNIT READY, 757 * and if not ready, sent a START_STOP to start (maybe spin up) and 758 * then send the INQUIRY again, since the INQUIRY can change after 759 * a device is initialized. 760 * 761 * Ideally, start a device if explicitly asked to do so. This 762 * assumes that a device is spun up on power on, spun down on 763 * request, and then spun up on request. 764 */ 765 766 /* 767 * The scanning code needs to know the scsi_level, even if no 768 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so 769 * non-zero LUNs can be scanned. 770 */ 771 sdev->scsi_level = inq_result[2] & 0x07; 772 if (sdev->scsi_level >= 2 || 773 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1)) 774 sdev->scsi_level++; 775 sdev->sdev_target->scsi_level = sdev->scsi_level; 776 777 /* 778 * If SCSI-2 or lower, and if the transport requires it, 779 * store the LUN value in CDB[1]. 780 */ 781 sdev->lun_in_cdb = 0; 782 if (sdev->scsi_level <= SCSI_2 && 783 sdev->scsi_level != SCSI_UNKNOWN && 784 !sdev->host->no_scsi2_lun_in_cdb) 785 sdev->lun_in_cdb = 1; 786 787 return 0; 788 } 789 790 /** 791 * scsi_add_lun - allocate and fully initialze a scsi_device 792 * @sdev: holds information to be stored in the new scsi_device 793 * @inq_result: holds the result of a previous INQUIRY to the LUN 794 * @bflags: black/white list flag 795 * @async: 1 if this device is being scanned asynchronously 796 * 797 * Description: 798 * Initialize the scsi_device @sdev. Optionally set fields based 799 * on values in *@bflags. 800 * 801 * Return: 802 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 803 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 804 **/ 805 static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, 806 blist_flags_t *bflags, int async) 807 { 808 int ret; 809 810 /* 811 * XXX do not save the inquiry, since it can change underneath us, 812 * save just vendor/model/rev. 813 * 814 * Rather than save it and have an ioctl that retrieves the saved 815 * value, have an ioctl that executes the same INQUIRY code used 816 * in scsi_probe_lun, let user level programs doing INQUIRY 817 * scanning run at their own risk, or supply a user level program 818 * that can correctly scan. 819 */ 820 821 /* 822 * Copy at least 36 bytes of INQUIRY data, so that we don't 823 * dereference unallocated memory when accessing the Vendor, 824 * Product, and Revision strings. Badly behaved devices may set 825 * the INQUIRY Additional Length byte to a small value, indicating 826 * these strings are invalid, but often they contain plausible data 827 * nonetheless. It doesn't matter if the device sent < 36 bytes 828 * total, since scsi_probe_lun() initializes inq_result with 0s. 829 */ 830 sdev->inquiry = kmemdup(inq_result, 831 max_t(size_t, sdev->inquiry_len, 36), 832 GFP_KERNEL); 833 if (sdev->inquiry == NULL) 834 return SCSI_SCAN_NO_RESPONSE; 835 836 sdev->vendor = (char *) (sdev->inquiry + 8); 837 sdev->model = (char *) (sdev->inquiry + 16); 838 sdev->rev = (char *) (sdev->inquiry + 32); 839 840 if (strncmp(sdev->vendor, "ATA ", 8) == 0) { 841 /* 842 * sata emulation layer device. This is a hack to work around 843 * the SATL power management specifications which state that 844 * when the SATL detects the device has gone into standby 845 * mode, it shall respond with NOT READY. 846 */ 847 sdev->allow_restart = 1; 848 } 849 850 if (*bflags & BLIST_ISROM) { 851 sdev->type = TYPE_ROM; 852 sdev->removable = 1; 853 } else { 854 sdev->type = (inq_result[0] & 0x1f); 855 sdev->removable = (inq_result[1] & 0x80) >> 7; 856 857 /* 858 * some devices may respond with wrong type for 859 * well-known logical units. Force well-known type 860 * to enumerate them correctly. 861 */ 862 if (scsi_is_wlun(sdev->lun) && sdev->type != TYPE_WLUN) { 863 sdev_printk(KERN_WARNING, sdev, 864 "%s: correcting incorrect peripheral device type 0x%x for W-LUN 0x%16xhN\n", 865 __func__, sdev->type, (unsigned int)sdev->lun); 866 sdev->type = TYPE_WLUN; 867 } 868 869 } 870 871 if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) { 872 /* RBC and MMC devices can return SCSI-3 compliance and yet 873 * still not support REPORT LUNS, so make them act as 874 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is 875 * specifically set */ 876 if ((*bflags & BLIST_REPORTLUN2) == 0) 877 *bflags |= BLIST_NOREPORTLUN; 878 } 879 880 /* 881 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI 882 * spec says: The device server is capable of supporting the 883 * specified peripheral device type on this logical unit. However, 884 * the physical device is not currently connected to this logical 885 * unit. 886 * 887 * The above is vague, as it implies that we could treat 001 and 888 * 011 the same. Stay compatible with previous code, and create a 889 * scsi_device for a PQ of 1 890 * 891 * Don't set the device offline here; rather let the upper 892 * level drivers eval the PQ to decide whether they should 893 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. 894 */ 895 896 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7; 897 sdev->lockable = sdev->removable; 898 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2); 899 900 if (sdev->scsi_level >= SCSI_3 || 901 (sdev->inquiry_len > 56 && inq_result[56] & 0x04)) 902 sdev->ppr = 1; 903 if (inq_result[7] & 0x60) 904 sdev->wdtr = 1; 905 if (inq_result[7] & 0x10) 906 sdev->sdtr = 1; 907 908 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d " 909 "ANSI: %d%s\n", scsi_device_type(sdev->type), 910 sdev->vendor, sdev->model, sdev->rev, 911 sdev->inq_periph_qual, inq_result[2] & 0x07, 912 (inq_result[3] & 0x0f) == 1 ? " CCS" : ""); 913 914 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) && 915 !(*bflags & BLIST_NOTQ)) { 916 sdev->tagged_supported = 1; 917 sdev->simple_tags = 1; 918 } 919 920 /* 921 * Some devices (Texel CD ROM drives) have handshaking problems 922 * when used with the Seagate controllers. borken is initialized 923 * to 1, and then set it to 0 here. 924 */ 925 if ((*bflags & BLIST_BORKEN) == 0) 926 sdev->borken = 0; 927 928 if (*bflags & BLIST_NO_ULD_ATTACH) 929 sdev->no_uld_attach = 1; 930 931 /* 932 * Apparently some really broken devices (contrary to the SCSI 933 * standards) need to be selected without asserting ATN 934 */ 935 if (*bflags & BLIST_SELECT_NO_ATN) 936 sdev->select_no_atn = 1; 937 938 /* 939 * Maximum 512 sector transfer length 940 * broken RA4x00 Compaq Disk Array 941 */ 942 if (*bflags & BLIST_MAX_512) 943 blk_queue_max_hw_sectors(sdev->request_queue, 512); 944 /* 945 * Max 1024 sector transfer length for targets that report incorrect 946 * max/optimal lengths and relied on the old block layer safe default 947 */ 948 else if (*bflags & BLIST_MAX_1024) 949 blk_queue_max_hw_sectors(sdev->request_queue, 1024); 950 951 /* 952 * Some devices may not want to have a start command automatically 953 * issued when a device is added. 954 */ 955 if (*bflags & BLIST_NOSTARTONADD) 956 sdev->no_start_on_add = 1; 957 958 if (*bflags & BLIST_SINGLELUN) 959 scsi_target(sdev)->single_lun = 1; 960 961 sdev->use_10_for_rw = 1; 962 963 /* some devices don't like REPORT SUPPORTED OPERATION CODES 964 * and will simply timeout causing sd_mod init to take a very 965 * very long time */ 966 if (*bflags & BLIST_NO_RSOC) 967 sdev->no_report_opcodes = 1; 968 969 /* set the device running here so that slave configure 970 * may do I/O */ 971 mutex_lock(&sdev->state_mutex); 972 ret = scsi_device_set_state(sdev, SDEV_RUNNING); 973 if (ret) 974 ret = scsi_device_set_state(sdev, SDEV_BLOCK); 975 mutex_unlock(&sdev->state_mutex); 976 977 if (ret) { 978 sdev_printk(KERN_ERR, sdev, 979 "in wrong state %s to complete scan\n", 980 scsi_device_state_name(sdev->sdev_state)); 981 return SCSI_SCAN_NO_RESPONSE; 982 } 983 984 if (*bflags & BLIST_NOT_LOCKABLE) 985 sdev->lockable = 0; 986 987 if (*bflags & BLIST_RETRY_HWERROR) 988 sdev->retry_hwerror = 1; 989 990 if (*bflags & BLIST_NO_DIF) 991 sdev->no_dif = 1; 992 993 if (*bflags & BLIST_UNMAP_LIMIT_WS) 994 sdev->unmap_limit_for_ws = 1; 995 996 if (*bflags & BLIST_IGN_MEDIA_CHANGE) 997 sdev->ignore_media_change = 1; 998 999 sdev->eh_timeout = SCSI_DEFAULT_EH_TIMEOUT; 1000 1001 if (*bflags & BLIST_TRY_VPD_PAGES) 1002 sdev->try_vpd_pages = 1; 1003 else if (*bflags & BLIST_SKIP_VPD_PAGES) 1004 sdev->skip_vpd_pages = 1; 1005 1006 transport_configure_device(&sdev->sdev_gendev); 1007 1008 if (sdev->host->hostt->slave_configure) { 1009 ret = sdev->host->hostt->slave_configure(sdev); 1010 if (ret) { 1011 /* 1012 * if LLDD reports slave not present, don't clutter 1013 * console with alloc failure messages 1014 */ 1015 if (ret != -ENXIO) { 1016 sdev_printk(KERN_ERR, sdev, 1017 "failed to configure device\n"); 1018 } 1019 return SCSI_SCAN_NO_RESPONSE; 1020 } 1021 } 1022 1023 if (sdev->scsi_level >= SCSI_3) 1024 scsi_attach_vpd(sdev); 1025 1026 sdev->max_queue_depth = sdev->queue_depth; 1027 WARN_ON_ONCE(sdev->max_queue_depth > sdev->budget_map.depth); 1028 sdev->sdev_bflags = *bflags; 1029 1030 /* 1031 * Ok, the device is now all set up, we can 1032 * register it and tell the rest of the kernel 1033 * about it. 1034 */ 1035 if (!async && scsi_sysfs_add_sdev(sdev) != 0) 1036 return SCSI_SCAN_NO_RESPONSE; 1037 1038 return SCSI_SCAN_LUN_PRESENT; 1039 } 1040 1041 #ifdef CONFIG_SCSI_LOGGING 1042 /** 1043 * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace 1044 * @buf: Output buffer with at least end-first+1 bytes of space 1045 * @inq: Inquiry buffer (input) 1046 * @first: Offset of string into inq 1047 * @end: Index after last character in inq 1048 */ 1049 static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq, 1050 unsigned first, unsigned end) 1051 { 1052 unsigned term = 0, idx; 1053 1054 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) { 1055 if (inq[idx+first] > ' ') { 1056 buf[idx] = inq[idx+first]; 1057 term = idx+1; 1058 } else { 1059 buf[idx] = ' '; 1060 } 1061 } 1062 buf[term] = 0; 1063 return buf; 1064 } 1065 #endif 1066 1067 /** 1068 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it 1069 * @starget: pointer to target device structure 1070 * @lun: LUN of target device 1071 * @bflagsp: store bflags here if not NULL 1072 * @sdevp: probe the LUN corresponding to this scsi_device 1073 * @rescan: if not equal to SCSI_SCAN_INITIAL skip some code only 1074 * needed on first scan 1075 * @hostdata: passed to scsi_alloc_sdev() 1076 * 1077 * Description: 1078 * Call scsi_probe_lun, if a LUN with an attached device is found, 1079 * allocate and set it up by calling scsi_add_lun. 1080 * 1081 * Return: 1082 * 1083 * - SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 1084 * - SCSI_SCAN_TARGET_PRESENT: target responded, but no device is 1085 * attached at the LUN 1086 * - SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 1087 **/ 1088 static int scsi_probe_and_add_lun(struct scsi_target *starget, 1089 u64 lun, blist_flags_t *bflagsp, 1090 struct scsi_device **sdevp, 1091 enum scsi_scan_mode rescan, 1092 void *hostdata) 1093 { 1094 struct scsi_device *sdev; 1095 unsigned char *result; 1096 blist_flags_t bflags; 1097 int res = SCSI_SCAN_NO_RESPONSE, result_len = 256; 1098 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 1099 1100 /* 1101 * The rescan flag is used as an optimization, the first scan of a 1102 * host adapter calls into here with rescan == 0. 1103 */ 1104 sdev = scsi_device_lookup_by_target(starget, lun); 1105 if (sdev) { 1106 if (rescan != SCSI_SCAN_INITIAL || !scsi_device_created(sdev)) { 1107 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 1108 "scsi scan: device exists on %s\n", 1109 dev_name(&sdev->sdev_gendev))); 1110 if (sdevp) 1111 *sdevp = sdev; 1112 else 1113 scsi_device_put(sdev); 1114 1115 if (bflagsp) 1116 *bflagsp = scsi_get_device_flags(sdev, 1117 sdev->vendor, 1118 sdev->model); 1119 return SCSI_SCAN_LUN_PRESENT; 1120 } 1121 scsi_device_put(sdev); 1122 } else 1123 sdev = scsi_alloc_sdev(starget, lun, hostdata); 1124 if (!sdev) 1125 goto out; 1126 1127 result = kmalloc(result_len, GFP_KERNEL); 1128 if (!result) 1129 goto out_free_sdev; 1130 1131 if (scsi_probe_lun(sdev, result, result_len, &bflags)) 1132 goto out_free_result; 1133 1134 if (bflagsp) 1135 *bflagsp = bflags; 1136 /* 1137 * result contains valid SCSI INQUIRY data. 1138 */ 1139 if ((result[0] >> 5) == 3) { 1140 /* 1141 * For a Peripheral qualifier 3 (011b), the SCSI 1142 * spec says: The device server is not capable of 1143 * supporting a physical device on this logical 1144 * unit. 1145 * 1146 * For disks, this implies that there is no 1147 * logical disk configured at sdev->lun, but there 1148 * is a target id responding. 1149 */ 1150 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:" 1151 " peripheral qualifier of 3, device not" 1152 " added\n")) 1153 if (lun == 0) { 1154 SCSI_LOG_SCAN_BUS(1, { 1155 unsigned char vend[9]; 1156 unsigned char mod[17]; 1157 1158 sdev_printk(KERN_INFO, sdev, 1159 "scsi scan: consider passing scsi_mod." 1160 "dev_flags=%s:%s:0x240 or 0x1000240\n", 1161 scsi_inq_str(vend, result, 8, 16), 1162 scsi_inq_str(mod, result, 16, 32)); 1163 }); 1164 1165 } 1166 1167 res = SCSI_SCAN_TARGET_PRESENT; 1168 goto out_free_result; 1169 } 1170 1171 /* 1172 * Some targets may set slight variations of PQ and PDT to signal 1173 * that no LUN is present, so don't add sdev in these cases. 1174 * Two specific examples are: 1175 * 1) NetApp targets: return PQ=1, PDT=0x1f 1176 * 2) IBM/2145 targets: return PQ=1, PDT=0 1177 * 3) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved" 1178 * in the UFI 1.0 spec (we cannot rely on reserved bits). 1179 * 1180 * References: 1181 * 1) SCSI SPC-3, pp. 145-146 1182 * PQ=1: "A peripheral device having the specified peripheral 1183 * device type is not connected to this logical unit. However, the 1184 * device server is capable of supporting the specified peripheral 1185 * device type on this logical unit." 1186 * PDT=0x1f: "Unknown or no device type" 1187 * 2) USB UFI 1.0, p. 20 1188 * PDT=00h Direct-access device (floppy) 1189 * PDT=1Fh none (no FDD connected to the requested logical unit) 1190 */ 1191 if (((result[0] >> 5) == 1 || 1192 (starget->pdt_1f_for_no_lun && (result[0] & 0x1f) == 0x1f)) && 1193 !scsi_is_wlun(lun)) { 1194 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 1195 "scsi scan: peripheral device type" 1196 " of 31, no device added\n")); 1197 res = SCSI_SCAN_TARGET_PRESENT; 1198 goto out_free_result; 1199 } 1200 1201 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan); 1202 if (res == SCSI_SCAN_LUN_PRESENT) { 1203 if (bflags & BLIST_KEY) { 1204 sdev->lockable = 0; 1205 scsi_unlock_floptical(sdev, result); 1206 } 1207 } 1208 1209 out_free_result: 1210 kfree(result); 1211 out_free_sdev: 1212 if (res == SCSI_SCAN_LUN_PRESENT) { 1213 if (sdevp) { 1214 if (scsi_device_get(sdev) == 0) { 1215 *sdevp = sdev; 1216 } else { 1217 __scsi_remove_device(sdev); 1218 res = SCSI_SCAN_NO_RESPONSE; 1219 } 1220 } 1221 } else 1222 __scsi_remove_device(sdev); 1223 out: 1224 return res; 1225 } 1226 1227 /** 1228 * scsi_sequential_lun_scan - sequentially scan a SCSI target 1229 * @starget: pointer to target structure to scan 1230 * @bflags: black/white list flag for LUN 0 1231 * @scsi_level: Which version of the standard does this device adhere to 1232 * @rescan: passed to scsi_probe_add_lun() 1233 * 1234 * Description: 1235 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been 1236 * scanned) to some maximum lun until a LUN is found with no device 1237 * attached. Use the bflags to figure out any oddities. 1238 * 1239 * Modifies sdevscan->lun. 1240 **/ 1241 static void scsi_sequential_lun_scan(struct scsi_target *starget, 1242 blist_flags_t bflags, int scsi_level, 1243 enum scsi_scan_mode rescan) 1244 { 1245 uint max_dev_lun; 1246 u64 sparse_lun, lun; 1247 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 1248 1249 SCSI_LOG_SCAN_BUS(3, starget_printk(KERN_INFO, starget, 1250 "scsi scan: Sequential scan\n")); 1251 1252 max_dev_lun = min(max_scsi_luns, shost->max_lun); 1253 /* 1254 * If this device is known to support sparse multiple units, 1255 * override the other settings, and scan all of them. Normally, 1256 * SCSI-3 devices should be scanned via the REPORT LUNS. 1257 */ 1258 if (bflags & BLIST_SPARSELUN) { 1259 max_dev_lun = shost->max_lun; 1260 sparse_lun = 1; 1261 } else 1262 sparse_lun = 0; 1263 1264 /* 1265 * If less than SCSI_1_CCS, and no special lun scanning, stop 1266 * scanning; this matches 2.4 behaviour, but could just be a bug 1267 * (to continue scanning a SCSI_1_CCS device). 1268 * 1269 * This test is broken. We might not have any device on lun0 for 1270 * a sparselun device, and if that's the case then how would we 1271 * know the real scsi_level, eh? It might make sense to just not 1272 * scan any SCSI_1 device for non-0 luns, but that check would best 1273 * go into scsi_alloc_sdev() and just have it return null when asked 1274 * to alloc an sdev for lun > 0 on an already found SCSI_1 device. 1275 * 1276 if ((sdevscan->scsi_level < SCSI_1_CCS) && 1277 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN)) 1278 == 0)) 1279 return; 1280 */ 1281 /* 1282 * If this device is known to support multiple units, override 1283 * the other settings, and scan all of them. 1284 */ 1285 if (bflags & BLIST_FORCELUN) 1286 max_dev_lun = shost->max_lun; 1287 /* 1288 * REGAL CDC-4X: avoid hang after LUN 4 1289 */ 1290 if (bflags & BLIST_MAX5LUN) 1291 max_dev_lun = min(5U, max_dev_lun); 1292 /* 1293 * Do not scan SCSI-2 or lower device past LUN 7, unless 1294 * BLIST_LARGELUN. 1295 */ 1296 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN)) 1297 max_dev_lun = min(8U, max_dev_lun); 1298 else 1299 max_dev_lun = min(256U, max_dev_lun); 1300 1301 /* 1302 * We have already scanned LUN 0, so start at LUN 1. Keep scanning 1303 * until we reach the max, or no LUN is found and we are not 1304 * sparse_lun. 1305 */ 1306 for (lun = 1; lun < max_dev_lun; ++lun) 1307 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, 1308 NULL) != SCSI_SCAN_LUN_PRESENT) && 1309 !sparse_lun) 1310 return; 1311 } 1312 1313 /** 1314 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results 1315 * @starget: which target 1316 * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN 1317 * @rescan: nonzero if we can skip code only needed on first scan 1318 * 1319 * Description: 1320 * Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command. 1321 * Scan the resulting list of LUNs by calling scsi_probe_and_add_lun. 1322 * 1323 * If BLINK_REPORTLUN2 is set, scan a target that supports more than 8 1324 * LUNs even if it's older than SCSI-3. 1325 * If BLIST_NOREPORTLUN is set, return 1 always. 1326 * If BLIST_NOLUN is set, return 0 always. 1327 * If starget->no_report_luns is set, return 1 always. 1328 * 1329 * Return: 1330 * 0: scan completed (or no memory, so further scanning is futile) 1331 * 1: could not scan with REPORT LUN 1332 **/ 1333 static int scsi_report_lun_scan(struct scsi_target *starget, blist_flags_t bflags, 1334 enum scsi_scan_mode rescan) 1335 { 1336 unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 1337 unsigned int length; 1338 u64 lun; 1339 unsigned int num_luns; 1340 unsigned int retries; 1341 int result; 1342 struct scsi_lun *lunp, *lun_data; 1343 struct scsi_sense_hdr sshdr; 1344 struct scsi_device *sdev; 1345 struct Scsi_Host *shost = dev_to_shost(&starget->dev); 1346 int ret = 0; 1347 1348 /* 1349 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set. 1350 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does 1351 * support more than 8 LUNs. 1352 * Don't attempt if the target doesn't support REPORT LUNS. 1353 */ 1354 if (bflags & BLIST_NOREPORTLUN) 1355 return 1; 1356 if (starget->scsi_level < SCSI_2 && 1357 starget->scsi_level != SCSI_UNKNOWN) 1358 return 1; 1359 if (starget->scsi_level < SCSI_3 && 1360 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8)) 1361 return 1; 1362 if (bflags & BLIST_NOLUN) 1363 return 0; 1364 if (starget->no_report_luns) 1365 return 1; 1366 1367 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) { 1368 sdev = scsi_alloc_sdev(starget, 0, NULL); 1369 if (!sdev) 1370 return 0; 1371 if (scsi_device_get(sdev)) { 1372 __scsi_remove_device(sdev); 1373 return 0; 1374 } 1375 } 1376 1377 /* 1378 * Allocate enough to hold the header (the same size as one scsi_lun) 1379 * plus the number of luns we are requesting. 511 was the default 1380 * value of the now removed max_report_luns parameter. 1381 */ 1382 length = (511 + 1) * sizeof(struct scsi_lun); 1383 retry: 1384 lun_data = kmalloc(length, GFP_KERNEL); 1385 if (!lun_data) { 1386 printk(ALLOC_FAILURE_MSG, __func__); 1387 goto out; 1388 } 1389 1390 scsi_cmd[0] = REPORT_LUNS; 1391 1392 /* 1393 * bytes 1 - 5: reserved, set to zero. 1394 */ 1395 memset(&scsi_cmd[1], 0, 5); 1396 1397 /* 1398 * bytes 6 - 9: length of the command. 1399 */ 1400 put_unaligned_be32(length, &scsi_cmd[6]); 1401 1402 scsi_cmd[10] = 0; /* reserved */ 1403 scsi_cmd[11] = 0; /* control */ 1404 1405 /* 1406 * We can get a UNIT ATTENTION, for example a power on/reset, so 1407 * retry a few times (like sd.c does for TEST UNIT READY). 1408 * Experience shows some combinations of adapter/devices get at 1409 * least two power on/resets. 1410 * 1411 * Illegal requests (for devices that do not support REPORT LUNS) 1412 * should come through as a check condition, and will not generate 1413 * a retry. 1414 */ 1415 for (retries = 0; retries < 3; retries++) { 1416 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 1417 "scsi scan: Sending REPORT LUNS to (try %d)\n", 1418 retries)); 1419 1420 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, 1421 lun_data, length, &sshdr, 1422 SCSI_REPORT_LUNS_TIMEOUT, 3, NULL); 1423 1424 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 1425 "scsi scan: REPORT LUNS" 1426 " %s (try %d) result 0x%x\n", 1427 result ? "failed" : "successful", 1428 retries, result)); 1429 if (result == 0) 1430 break; 1431 else if (scsi_sense_valid(&sshdr)) { 1432 if (sshdr.sense_key != UNIT_ATTENTION) 1433 break; 1434 } 1435 } 1436 1437 if (result) { 1438 /* 1439 * The device probably does not support a REPORT LUN command 1440 */ 1441 ret = 1; 1442 goto out_err; 1443 } 1444 1445 /* 1446 * Get the length from the first four bytes of lun_data. 1447 */ 1448 if (get_unaligned_be32(lun_data->scsi_lun) + 1449 sizeof(struct scsi_lun) > length) { 1450 length = get_unaligned_be32(lun_data->scsi_lun) + 1451 sizeof(struct scsi_lun); 1452 kfree(lun_data); 1453 goto retry; 1454 } 1455 length = get_unaligned_be32(lun_data->scsi_lun); 1456 1457 num_luns = (length / sizeof(struct scsi_lun)); 1458 1459 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 1460 "scsi scan: REPORT LUN scan\n")); 1461 1462 /* 1463 * Scan the luns in lun_data. The entry at offset 0 is really 1464 * the header, so start at 1 and go up to and including num_luns. 1465 */ 1466 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) { 1467 lun = scsilun_to_int(lunp); 1468 1469 if (lun > sdev->host->max_lun) { 1470 sdev_printk(KERN_WARNING, sdev, 1471 "lun%llu has a LUN larger than" 1472 " allowed by the host adapter\n", lun); 1473 } else { 1474 int res; 1475 1476 res = scsi_probe_and_add_lun(starget, 1477 lun, NULL, NULL, rescan, NULL); 1478 if (res == SCSI_SCAN_NO_RESPONSE) { 1479 /* 1480 * Got some results, but now none, abort. 1481 */ 1482 sdev_printk(KERN_ERR, sdev, 1483 "Unexpected response" 1484 " from lun %llu while scanning, scan" 1485 " aborted\n", (unsigned long long)lun); 1486 break; 1487 } 1488 } 1489 } 1490 1491 out_err: 1492 kfree(lun_data); 1493 out: 1494 if (scsi_device_created(sdev)) 1495 /* 1496 * the sdev we used didn't appear in the report luns scan 1497 */ 1498 __scsi_remove_device(sdev); 1499 scsi_device_put(sdev); 1500 return ret; 1501 } 1502 1503 struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel, 1504 uint id, u64 lun, void *hostdata) 1505 { 1506 struct scsi_device *sdev = ERR_PTR(-ENODEV); 1507 struct device *parent = &shost->shost_gendev; 1508 struct scsi_target *starget; 1509 1510 if (strncmp(scsi_scan_type, "none", 4) == 0) 1511 return ERR_PTR(-ENODEV); 1512 1513 starget = scsi_alloc_target(parent, channel, id); 1514 if (!starget) 1515 return ERR_PTR(-ENOMEM); 1516 scsi_autopm_get_target(starget); 1517 1518 mutex_lock(&shost->scan_mutex); 1519 if (!shost->async_scan) 1520 scsi_complete_async_scans(); 1521 1522 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 1523 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata); 1524 scsi_autopm_put_host(shost); 1525 } 1526 mutex_unlock(&shost->scan_mutex); 1527 scsi_autopm_put_target(starget); 1528 /* 1529 * paired with scsi_alloc_target(). Target will be destroyed unless 1530 * scsi_probe_and_add_lun made an underlying device visible 1531 */ 1532 scsi_target_reap(starget); 1533 put_device(&starget->dev); 1534 1535 return sdev; 1536 } 1537 EXPORT_SYMBOL(__scsi_add_device); 1538 1539 int scsi_add_device(struct Scsi_Host *host, uint channel, 1540 uint target, u64 lun) 1541 { 1542 struct scsi_device *sdev = 1543 __scsi_add_device(host, channel, target, lun, NULL); 1544 if (IS_ERR(sdev)) 1545 return PTR_ERR(sdev); 1546 1547 scsi_device_put(sdev); 1548 return 0; 1549 } 1550 EXPORT_SYMBOL(scsi_add_device); 1551 1552 void scsi_rescan_device(struct device *dev) 1553 { 1554 struct scsi_device *sdev = to_scsi_device(dev); 1555 1556 device_lock(dev); 1557 1558 scsi_attach_vpd(sdev); 1559 1560 if (sdev->handler && sdev->handler->rescan) 1561 sdev->handler->rescan(sdev); 1562 1563 if (dev->driver && try_module_get(dev->driver->owner)) { 1564 struct scsi_driver *drv = to_scsi_driver(dev->driver); 1565 1566 if (drv->rescan) 1567 drv->rescan(dev); 1568 module_put(dev->driver->owner); 1569 } 1570 device_unlock(dev); 1571 } 1572 EXPORT_SYMBOL(scsi_rescan_device); 1573 1574 static void __scsi_scan_target(struct device *parent, unsigned int channel, 1575 unsigned int id, u64 lun, enum scsi_scan_mode rescan) 1576 { 1577 struct Scsi_Host *shost = dev_to_shost(parent); 1578 blist_flags_t bflags = 0; 1579 int res; 1580 struct scsi_target *starget; 1581 1582 if (shost->this_id == id) 1583 /* 1584 * Don't scan the host adapter 1585 */ 1586 return; 1587 1588 starget = scsi_alloc_target(parent, channel, id); 1589 if (!starget) 1590 return; 1591 scsi_autopm_get_target(starget); 1592 1593 if (lun != SCAN_WILD_CARD) { 1594 /* 1595 * Scan for a specific host/chan/id/lun. 1596 */ 1597 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL); 1598 goto out_reap; 1599 } 1600 1601 /* 1602 * Scan LUN 0, if there is some response, scan further. Ideally, we 1603 * would not configure LUN 0 until all LUNs are scanned. 1604 */ 1605 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL); 1606 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) { 1607 if (scsi_report_lun_scan(starget, bflags, rescan) != 0) 1608 /* 1609 * The REPORT LUN did not scan the target, 1610 * do a sequential scan. 1611 */ 1612 scsi_sequential_lun_scan(starget, bflags, 1613 starget->scsi_level, rescan); 1614 } 1615 1616 out_reap: 1617 scsi_autopm_put_target(starget); 1618 /* 1619 * paired with scsi_alloc_target(): determine if the target has 1620 * any children at all and if not, nuke it 1621 */ 1622 scsi_target_reap(starget); 1623 1624 put_device(&starget->dev); 1625 } 1626 1627 /** 1628 * scsi_scan_target - scan a target id, possibly including all LUNs on the target. 1629 * @parent: host to scan 1630 * @channel: channel to scan 1631 * @id: target id to scan 1632 * @lun: Specific LUN to scan or SCAN_WILD_CARD 1633 * @rescan: passed to LUN scanning routines; SCSI_SCAN_INITIAL for 1634 * no rescan, SCSI_SCAN_RESCAN to rescan existing LUNs, 1635 * and SCSI_SCAN_MANUAL to force scanning even if 1636 * 'scan=manual' is set. 1637 * 1638 * Description: 1639 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0, 1640 * and possibly all LUNs on the target id. 1641 * 1642 * First try a REPORT LUN scan, if that does not scan the target, do a 1643 * sequential scan of LUNs on the target id. 1644 **/ 1645 void scsi_scan_target(struct device *parent, unsigned int channel, 1646 unsigned int id, u64 lun, enum scsi_scan_mode rescan) 1647 { 1648 struct Scsi_Host *shost = dev_to_shost(parent); 1649 1650 if (strncmp(scsi_scan_type, "none", 4) == 0) 1651 return; 1652 1653 if (rescan != SCSI_SCAN_MANUAL && 1654 strncmp(scsi_scan_type, "manual", 6) == 0) 1655 return; 1656 1657 mutex_lock(&shost->scan_mutex); 1658 if (!shost->async_scan) 1659 scsi_complete_async_scans(); 1660 1661 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 1662 __scsi_scan_target(parent, channel, id, lun, rescan); 1663 scsi_autopm_put_host(shost); 1664 } 1665 mutex_unlock(&shost->scan_mutex); 1666 } 1667 EXPORT_SYMBOL(scsi_scan_target); 1668 1669 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel, 1670 unsigned int id, u64 lun, 1671 enum scsi_scan_mode rescan) 1672 { 1673 uint order_id; 1674 1675 if (id == SCAN_WILD_CARD) 1676 for (id = 0; id < shost->max_id; ++id) { 1677 /* 1678 * XXX adapter drivers when possible (FCP, iSCSI) 1679 * could modify max_id to match the current max, 1680 * not the absolute max. 1681 * 1682 * XXX add a shost id iterator, so for example, 1683 * the FC ID can be the same as a target id 1684 * without a huge overhead of sparse id's. 1685 */ 1686 if (shost->reverse_ordering) 1687 /* 1688 * Scan from high to low id. 1689 */ 1690 order_id = shost->max_id - id - 1; 1691 else 1692 order_id = id; 1693 __scsi_scan_target(&shost->shost_gendev, channel, 1694 order_id, lun, rescan); 1695 } 1696 else 1697 __scsi_scan_target(&shost->shost_gendev, channel, 1698 id, lun, rescan); 1699 } 1700 1701 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel, 1702 unsigned int id, u64 lun, 1703 enum scsi_scan_mode rescan) 1704 { 1705 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost, 1706 "%s: <%u:%u:%llu>\n", 1707 __func__, channel, id, lun)); 1708 1709 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) || 1710 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) || 1711 ((lun != SCAN_WILD_CARD) && (lun >= shost->max_lun))) 1712 return -EINVAL; 1713 1714 mutex_lock(&shost->scan_mutex); 1715 if (!shost->async_scan) 1716 scsi_complete_async_scans(); 1717 1718 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 1719 if (channel == SCAN_WILD_CARD) 1720 for (channel = 0; channel <= shost->max_channel; 1721 channel++) 1722 scsi_scan_channel(shost, channel, id, lun, 1723 rescan); 1724 else 1725 scsi_scan_channel(shost, channel, id, lun, rescan); 1726 scsi_autopm_put_host(shost); 1727 } 1728 mutex_unlock(&shost->scan_mutex); 1729 1730 return 0; 1731 } 1732 1733 static void scsi_sysfs_add_devices(struct Scsi_Host *shost) 1734 { 1735 struct scsi_device *sdev; 1736 shost_for_each_device(sdev, shost) { 1737 /* target removed before the device could be added */ 1738 if (sdev->sdev_state == SDEV_DEL) 1739 continue; 1740 /* If device is already visible, skip adding it to sysfs */ 1741 if (sdev->is_visible) 1742 continue; 1743 if (!scsi_host_scan_allowed(shost) || 1744 scsi_sysfs_add_sdev(sdev) != 0) 1745 __scsi_remove_device(sdev); 1746 } 1747 } 1748 1749 /** 1750 * scsi_prep_async_scan - prepare for an async scan 1751 * @shost: the host which will be scanned 1752 * Returns: a cookie to be passed to scsi_finish_async_scan() 1753 * 1754 * Tells the midlayer this host is going to do an asynchronous scan. 1755 * It reserves the host's position in the scanning list and ensures 1756 * that other asynchronous scans started after this one won't affect the 1757 * ordering of the discovered devices. 1758 */ 1759 static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) 1760 { 1761 struct async_scan_data *data = NULL; 1762 unsigned long flags; 1763 1764 if (strncmp(scsi_scan_type, "sync", 4) == 0) 1765 return NULL; 1766 1767 mutex_lock(&shost->scan_mutex); 1768 if (shost->async_scan) { 1769 shost_printk(KERN_DEBUG, shost, "%s called twice\n", __func__); 1770 goto err; 1771 } 1772 1773 data = kmalloc(sizeof(*data), GFP_KERNEL); 1774 if (!data) 1775 goto err; 1776 data->shost = scsi_host_get(shost); 1777 if (!data->shost) 1778 goto err; 1779 init_completion(&data->prev_finished); 1780 1781 spin_lock_irqsave(shost->host_lock, flags); 1782 shost->async_scan = 1; 1783 spin_unlock_irqrestore(shost->host_lock, flags); 1784 mutex_unlock(&shost->scan_mutex); 1785 1786 spin_lock(&async_scan_lock); 1787 if (list_empty(&scanning_hosts)) 1788 complete(&data->prev_finished); 1789 list_add_tail(&data->list, &scanning_hosts); 1790 spin_unlock(&async_scan_lock); 1791 1792 return data; 1793 1794 err: 1795 mutex_unlock(&shost->scan_mutex); 1796 kfree(data); 1797 return NULL; 1798 } 1799 1800 /** 1801 * scsi_finish_async_scan - asynchronous scan has finished 1802 * @data: cookie returned from earlier call to scsi_prep_async_scan() 1803 * 1804 * All the devices currently attached to this host have been found. 1805 * This function announces all the devices it has found to the rest 1806 * of the system. 1807 */ 1808 static void scsi_finish_async_scan(struct async_scan_data *data) 1809 { 1810 struct Scsi_Host *shost; 1811 unsigned long flags; 1812 1813 if (!data) 1814 return; 1815 1816 shost = data->shost; 1817 1818 mutex_lock(&shost->scan_mutex); 1819 1820 if (!shost->async_scan) { 1821 shost_printk(KERN_INFO, shost, "%s called twice\n", __func__); 1822 dump_stack(); 1823 mutex_unlock(&shost->scan_mutex); 1824 return; 1825 } 1826 1827 wait_for_completion(&data->prev_finished); 1828 1829 scsi_sysfs_add_devices(shost); 1830 1831 spin_lock_irqsave(shost->host_lock, flags); 1832 shost->async_scan = 0; 1833 spin_unlock_irqrestore(shost->host_lock, flags); 1834 1835 mutex_unlock(&shost->scan_mutex); 1836 1837 spin_lock(&async_scan_lock); 1838 list_del(&data->list); 1839 if (!list_empty(&scanning_hosts)) { 1840 struct async_scan_data *next = list_entry(scanning_hosts.next, 1841 struct async_scan_data, list); 1842 complete(&next->prev_finished); 1843 } 1844 spin_unlock(&async_scan_lock); 1845 1846 scsi_autopm_put_host(shost); 1847 scsi_host_put(shost); 1848 kfree(data); 1849 } 1850 1851 static void do_scsi_scan_host(struct Scsi_Host *shost) 1852 { 1853 if (shost->hostt->scan_finished) { 1854 unsigned long start = jiffies; 1855 if (shost->hostt->scan_start) 1856 shost->hostt->scan_start(shost); 1857 1858 while (!shost->hostt->scan_finished(shost, jiffies - start)) 1859 msleep(10); 1860 } else { 1861 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD, 1862 SCAN_WILD_CARD, 0); 1863 } 1864 } 1865 1866 static void do_scan_async(void *_data, async_cookie_t c) 1867 { 1868 struct async_scan_data *data = _data; 1869 struct Scsi_Host *shost = data->shost; 1870 1871 do_scsi_scan_host(shost); 1872 scsi_finish_async_scan(data); 1873 } 1874 1875 /** 1876 * scsi_scan_host - scan the given adapter 1877 * @shost: adapter to scan 1878 **/ 1879 void scsi_scan_host(struct Scsi_Host *shost) 1880 { 1881 struct async_scan_data *data; 1882 1883 if (strncmp(scsi_scan_type, "none", 4) == 0 || 1884 strncmp(scsi_scan_type, "manual", 6) == 0) 1885 return; 1886 if (scsi_autopm_get_host(shost) < 0) 1887 return; 1888 1889 data = scsi_prep_async_scan(shost); 1890 if (!data) { 1891 do_scsi_scan_host(shost); 1892 scsi_autopm_put_host(shost); 1893 return; 1894 } 1895 1896 /* register with the async subsystem so wait_for_device_probe() 1897 * will flush this work 1898 */ 1899 async_schedule(do_scan_async, data); 1900 1901 /* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */ 1902 } 1903 EXPORT_SYMBOL(scsi_scan_host); 1904 1905 void scsi_forget_host(struct Scsi_Host *shost) 1906 { 1907 struct scsi_device *sdev; 1908 unsigned long flags; 1909 1910 restart: 1911 spin_lock_irqsave(shost->host_lock, flags); 1912 list_for_each_entry(sdev, &shost->__devices, siblings) { 1913 if (sdev->sdev_state == SDEV_DEL) 1914 continue; 1915 spin_unlock_irqrestore(shost->host_lock, flags); 1916 __scsi_remove_device(sdev); 1917 goto restart; 1918 } 1919 spin_unlock_irqrestore(shost->host_lock, flags); 1920 } 1921 1922