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