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