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