[POWERPC] PS3: Kexec support
[deliverable/linux.git] / drivers / scsi / scsi_scan.c
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, int, 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, int, 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, int, 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 static DEFINE_SPINLOCK(async_scan_lock);
125 static LIST_HEAD(scanning_hosts);
126
127 struct 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 */
141 int 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 */
185 EXPORT_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 */
193 late_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 **/
205 static 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 **/
232 static 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
309 out_device_destroy:
310 transport_destroy_device(&sdev->sdev_gendev);
311 put_device(&sdev->sdev_gendev);
312 out:
313 if (display_failure_msg)
314 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
315 return NULL;
316 }
317
318 static 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
327 int scsi_is_target_device(const struct device *dev)
328 {
329 return dev->release == scsi_target_dev_release;
330 }
331 EXPORT_SYMBOL(scsi_is_target_device);
332
333 static 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 */
366 static 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
451 static 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 */
478 void 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 **/
513 static 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 **/
539 static 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 /*
607 * Get any flags for this device.
608 *
609 * XXX add a bflags to scsi_device, and replace the
610 * corresponding bit fields in scsi_device, so bflags
611 * need not be passed as an argument.
612 */
613 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
614 &inq_result[16]);
615
616 /* When the first pass succeeds we gain information about
617 * what larger transfer lengths might work. */
618 if (pass == 1) {
619 if (BLIST_INQUIRY_36 & *bflags)
620 next_inquiry_len = 36;
621 else if (BLIST_INQUIRY_58 & *bflags)
622 next_inquiry_len = 58;
623 else if (sdev->inquiry_len)
624 next_inquiry_len = sdev->inquiry_len;
625 else
626 next_inquiry_len = response_len;
627
628 /* If more data is available perform the second pass */
629 if (next_inquiry_len > try_inquiry_len) {
630 try_inquiry_len = next_inquiry_len;
631 pass = 2;
632 goto next_pass;
633 }
634 }
635
636 } else if (pass == 2) {
637 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
638 "Consider BLIST_INQUIRY_36 for this device\n",
639 try_inquiry_len);
640
641 /* If this pass failed, the third pass goes back and transfers
642 * the same amount as we successfully got in the first pass. */
643 try_inquiry_len = first_inquiry_len;
644 pass = 3;
645 goto next_pass;
646 }
647
648 /* If the last transfer attempt got an error, assume the
649 * peripheral doesn't exist or is dead. */
650 if (result)
651 return -EIO;
652
653 /* Don't report any more data than the device says is valid */
654 sdev->inquiry_len = min(try_inquiry_len, response_len);
655
656 /*
657 * XXX Abort if the response length is less than 36? If less than
658 * 32, the lookup of the device flags (above) could be invalid,
659 * and it would be possible to take an incorrect action - we do
660 * not want to hang because of a short INQUIRY. On the flip side,
661 * if the device is spun down or becoming ready (and so it gives a
662 * short INQUIRY), an abort here prevents any further use of the
663 * device, including spin up.
664 *
665 * On the whole, the best approach seems to be to assume the first
666 * 36 bytes are valid no matter what the device says. That's
667 * better than copying < 36 bytes to the inquiry-result buffer
668 * and displaying garbage for the Vendor, Product, or Revision
669 * strings.
670 */
671 if (sdev->inquiry_len < 36) {
672 printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
673 " using 36\n", sdev->inquiry_len);
674 sdev->inquiry_len = 36;
675 }
676
677 /*
678 * Related to the above issue:
679 *
680 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
681 * and if not ready, sent a START_STOP to start (maybe spin up) and
682 * then send the INQUIRY again, since the INQUIRY can change after
683 * a device is initialized.
684 *
685 * Ideally, start a device if explicitly asked to do so. This
686 * assumes that a device is spun up on power on, spun down on
687 * request, and then spun up on request.
688 */
689
690 /*
691 * The scanning code needs to know the scsi_level, even if no
692 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
693 * non-zero LUNs can be scanned.
694 */
695 sdev->scsi_level = inq_result[2] & 0x07;
696 if (sdev->scsi_level >= 2 ||
697 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
698 sdev->scsi_level++;
699 sdev->sdev_target->scsi_level = sdev->scsi_level;
700
701 return 0;
702 }
703
704 /**
705 * scsi_add_lun - allocate and fully initialze a scsi_device
706 * @sdevscan: holds information to be stored in the new scsi_device
707 * @sdevnew: store the address of the newly allocated scsi_device
708 * @inq_result: holds the result of a previous INQUIRY to the LUN
709 * @bflags: black/white list flag
710 *
711 * Description:
712 * Allocate and initialize a scsi_device matching sdevscan. Optionally
713 * set fields based on values in *@bflags. If @sdevnew is not
714 * NULL, store the address of the new scsi_device in *@sdevnew (needed
715 * when scanning a particular LUN).
716 *
717 * Return:
718 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
719 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
720 **/
721 static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
722 int *bflags, int async)
723 {
724 /*
725 * XXX do not save the inquiry, since it can change underneath us,
726 * save just vendor/model/rev.
727 *
728 * Rather than save it and have an ioctl that retrieves the saved
729 * value, have an ioctl that executes the same INQUIRY code used
730 * in scsi_probe_lun, let user level programs doing INQUIRY
731 * scanning run at their own risk, or supply a user level program
732 * that can correctly scan.
733 */
734
735 /*
736 * Copy at least 36 bytes of INQUIRY data, so that we don't
737 * dereference unallocated memory when accessing the Vendor,
738 * Product, and Revision strings. Badly behaved devices may set
739 * the INQUIRY Additional Length byte to a small value, indicating
740 * these strings are invalid, but often they contain plausible data
741 * nonetheless. It doesn't matter if the device sent < 36 bytes
742 * total, since scsi_probe_lun() initializes inq_result with 0s.
743 */
744 sdev->inquiry = kmemdup(inq_result,
745 max_t(size_t, sdev->inquiry_len, 36),
746 GFP_ATOMIC);
747 if (sdev->inquiry == NULL)
748 return SCSI_SCAN_NO_RESPONSE;
749
750 sdev->vendor = (char *) (sdev->inquiry + 8);
751 sdev->model = (char *) (sdev->inquiry + 16);
752 sdev->rev = (char *) (sdev->inquiry + 32);
753
754 if (*bflags & BLIST_ISROM) {
755 /*
756 * It would be better to modify sdev->type, and set
757 * sdev->removable; this can now be done since
758 * print_inquiry has gone away.
759 */
760 inq_result[0] = TYPE_ROM;
761 inq_result[1] |= 0x80; /* removable */
762 } else if (*bflags & BLIST_NO_ULD_ATTACH)
763 sdev->no_uld_attach = 1;
764
765 switch (sdev->type = (inq_result[0] & 0x1f)) {
766 case TYPE_RBC:
767 /* RBC devices can return SCSI-3 compliance and yet
768 * still not support REPORT LUNS, so make them act as
769 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
770 * specifically set */
771 if ((*bflags & BLIST_REPORTLUN2) == 0)
772 *bflags |= BLIST_NOREPORTLUN;
773 /* fall through */
774 case TYPE_TAPE:
775 case TYPE_DISK:
776 case TYPE_PRINTER:
777 case TYPE_MOD:
778 case TYPE_PROCESSOR:
779 case TYPE_SCANNER:
780 case TYPE_MEDIUM_CHANGER:
781 case TYPE_ENCLOSURE:
782 case TYPE_COMM:
783 case TYPE_RAID:
784 sdev->writeable = 1;
785 break;
786 case TYPE_ROM:
787 /* MMC devices can return SCSI-3 compliance and yet
788 * still not support REPORT LUNS, so make them act as
789 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
790 * specifically set */
791 if ((*bflags & BLIST_REPORTLUN2) == 0)
792 *bflags |= BLIST_NOREPORTLUN;
793 /* fall through */
794 case TYPE_WORM:
795 sdev->writeable = 0;
796 break;
797 default:
798 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
799 }
800
801 /*
802 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
803 * spec says: The device server is capable of supporting the
804 * specified peripheral device type on this logical unit. However,
805 * the physical device is not currently connected to this logical
806 * unit.
807 *
808 * The above is vague, as it implies that we could treat 001 and
809 * 011 the same. Stay compatible with previous code, and create a
810 * scsi_device for a PQ of 1
811 *
812 * Don't set the device offline here; rather let the upper
813 * level drivers eval the PQ to decide whether they should
814 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
815 */
816
817 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
818 sdev->removable = (0x80 & inq_result[1]) >> 7;
819 sdev->lockable = sdev->removable;
820 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
821
822 if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
823 inq_result[56] & 0x04))
824 sdev->ppr = 1;
825 if (inq_result[7] & 0x60)
826 sdev->wdtr = 1;
827 if (inq_result[7] & 0x10)
828 sdev->sdtr = 1;
829
830 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
831 "ANSI: %d%s\n", scsi_device_type(sdev->type),
832 sdev->vendor, sdev->model, sdev->rev,
833 sdev->inq_periph_qual, inq_result[2] & 0x07,
834 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
835
836 /*
837 * End sysfs code.
838 */
839
840 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
841 !(*bflags & BLIST_NOTQ))
842 sdev->tagged_supported = 1;
843 /*
844 * Some devices (Texel CD ROM drives) have handshaking problems
845 * when used with the Seagate controllers. borken is initialized
846 * to 1, and then set it to 0 here.
847 */
848 if ((*bflags & BLIST_BORKEN) == 0)
849 sdev->borken = 0;
850
851 /*
852 * Apparently some really broken devices (contrary to the SCSI
853 * standards) need to be selected without asserting ATN
854 */
855 if (*bflags & BLIST_SELECT_NO_ATN)
856 sdev->select_no_atn = 1;
857
858 /*
859 * Maximum 512 sector transfer length
860 * broken RA4x00 Compaq Disk Array
861 */
862 if (*bflags & BLIST_MAX_512)
863 blk_queue_max_sectors(sdev->request_queue, 512);
864
865 /*
866 * Some devices may not want to have a start command automatically
867 * issued when a device is added.
868 */
869 if (*bflags & BLIST_NOSTARTONADD)
870 sdev->no_start_on_add = 1;
871
872 if (*bflags & BLIST_SINGLELUN)
873 sdev->single_lun = 1;
874
875
876 sdev->use_10_for_rw = 1;
877
878 if (*bflags & BLIST_MS_SKIP_PAGE_08)
879 sdev->skip_ms_page_8 = 1;
880
881 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
882 sdev->skip_ms_page_3f = 1;
883
884 if (*bflags & BLIST_USE_10_BYTE_MS)
885 sdev->use_10_for_ms = 1;
886
887 /* set the device running here so that slave configure
888 * may do I/O */
889 scsi_device_set_state(sdev, SDEV_RUNNING);
890
891 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
892 sdev->use_192_bytes_for_3f = 1;
893
894 if (*bflags & BLIST_NOT_LOCKABLE)
895 sdev->lockable = 0;
896
897 if (*bflags & BLIST_RETRY_HWERROR)
898 sdev->retry_hwerror = 1;
899
900 transport_configure_device(&sdev->sdev_gendev);
901
902 if (sdev->host->hostt->slave_configure) {
903 int ret = sdev->host->hostt->slave_configure(sdev);
904 if (ret) {
905 /*
906 * if LLDD reports slave not present, don't clutter
907 * console with alloc failure messages
908 */
909 if (ret != -ENXIO) {
910 sdev_printk(KERN_ERR, sdev,
911 "failed to configure device\n");
912 }
913 return SCSI_SCAN_NO_RESPONSE;
914 }
915 }
916
917 /*
918 * Ok, the device is now all set up, we can
919 * register it and tell the rest of the kernel
920 * about it.
921 */
922 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
923 return SCSI_SCAN_NO_RESPONSE;
924
925 return SCSI_SCAN_LUN_PRESENT;
926 }
927
928 static inline void scsi_destroy_sdev(struct scsi_device *sdev)
929 {
930 scsi_device_set_state(sdev, SDEV_DEL);
931 if (sdev->host->hostt->slave_destroy)
932 sdev->host->hostt->slave_destroy(sdev);
933 transport_destroy_device(&sdev->sdev_gendev);
934 put_device(&sdev->sdev_gendev);
935 }
936
937 #ifdef CONFIG_SCSI_LOGGING
938 /**
939 * scsi_inq_str - print INQUIRY data from min to max index,
940 * strip trailing whitespace
941 * @buf: Output buffer with at least end-first+1 bytes of space
942 * @inq: Inquiry buffer (input)
943 * @first: Offset of string into inq
944 * @end: Index after last character in inq
945 */
946 static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
947 unsigned first, unsigned end)
948 {
949 unsigned term = 0, idx;
950
951 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
952 if (inq[idx+first] > ' ') {
953 buf[idx] = inq[idx+first];
954 term = idx+1;
955 } else {
956 buf[idx] = ' ';
957 }
958 }
959 buf[term] = 0;
960 return buf;
961 }
962 #endif
963
964 /**
965 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
966 * @starget: pointer to target device structure
967 * @lun: LUN of target device
968 * @sdevscan: probe the LUN corresponding to this scsi_device
969 * @sdevnew: store the value of any new scsi_device allocated
970 * @bflagsp: store bflags here if not NULL
971 *
972 * Description:
973 * Call scsi_probe_lun, if a LUN with an attached device is found,
974 * allocate and set it up by calling scsi_add_lun.
975 *
976 * Return:
977 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
978 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
979 * attached at the LUN
980 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
981 **/
982 static int scsi_probe_and_add_lun(struct scsi_target *starget,
983 uint lun, int *bflagsp,
984 struct scsi_device **sdevp, int rescan,
985 void *hostdata)
986 {
987 struct scsi_device *sdev;
988 unsigned char *result;
989 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
990 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
991
992 /*
993 * The rescan flag is used as an optimization, the first scan of a
994 * host adapter calls into here with rescan == 0.
995 */
996 sdev = scsi_device_lookup_by_target(starget, lun);
997 if (sdev) {
998 if (rescan || sdev->sdev_state != SDEV_CREATED) {
999 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1000 "scsi scan: device exists on %s\n",
1001 sdev->sdev_gendev.bus_id));
1002 if (sdevp)
1003 *sdevp = sdev;
1004 else
1005 scsi_device_put(sdev);
1006
1007 if (bflagsp)
1008 *bflagsp = scsi_get_device_flags(sdev,
1009 sdev->vendor,
1010 sdev->model);
1011 return SCSI_SCAN_LUN_PRESENT;
1012 }
1013 scsi_device_put(sdev);
1014 } else
1015 sdev = scsi_alloc_sdev(starget, lun, hostdata);
1016 if (!sdev)
1017 goto out;
1018
1019 result = kmalloc(result_len, GFP_ATOMIC |
1020 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
1021 if (!result)
1022 goto out_free_sdev;
1023
1024 if (scsi_probe_lun(sdev, result, result_len, &bflags))
1025 goto out_free_result;
1026
1027 if (bflagsp)
1028 *bflagsp = bflags;
1029 /*
1030 * result contains valid SCSI INQUIRY data.
1031 */
1032 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
1033 /*
1034 * For a Peripheral qualifier 3 (011b), the SCSI
1035 * spec says: The device server is not capable of
1036 * supporting a physical device on this logical
1037 * unit.
1038 *
1039 * For disks, this implies that there is no
1040 * logical disk configured at sdev->lun, but there
1041 * is a target id responding.
1042 */
1043 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1044 " peripheral qualifier of 3, device not"
1045 " added\n"))
1046 if (lun == 0) {
1047 SCSI_LOG_SCAN_BUS(1, {
1048 unsigned char vend[9];
1049 unsigned char mod[17];
1050
1051 sdev_printk(KERN_INFO, sdev,
1052 "scsi scan: consider passing scsi_mod."
1053 "dev_flags=%s:%s:0x240 or 0x1000240\n",
1054 scsi_inq_str(vend, result, 8, 16),
1055 scsi_inq_str(mod, result, 16, 32));
1056 });
1057 }
1058
1059 res = SCSI_SCAN_TARGET_PRESENT;
1060 goto out_free_result;
1061 }
1062
1063 /*
1064 * Some targets may set slight variations of PQ and PDT to signal
1065 * that no LUN is present, so don't add sdev in these cases.
1066 * Two specific examples are:
1067 * 1) NetApp targets: return PQ=1, PDT=0x1f
1068 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1069 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1070 *
1071 * References:
1072 * 1) SCSI SPC-3, pp. 145-146
1073 * PQ=1: "A peripheral device having the specified peripheral
1074 * device type is not connected to this logical unit. However, the
1075 * device server is capable of supporting the specified peripheral
1076 * device type on this logical unit."
1077 * PDT=0x1f: "Unknown or no device type"
1078 * 2) USB UFI 1.0, p. 20
1079 * PDT=00h Direct-access device (floppy)
1080 * PDT=1Fh none (no FDD connected to the requested logical unit)
1081 */
1082 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1083 (result[0] & 0x1f) == 0x1f) {
1084 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1085 "scsi scan: peripheral device type"
1086 " of 31, no device added\n"));
1087 res = SCSI_SCAN_TARGET_PRESENT;
1088 goto out_free_result;
1089 }
1090
1091 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
1092 if (res == SCSI_SCAN_LUN_PRESENT) {
1093 if (bflags & BLIST_KEY) {
1094 sdev->lockable = 0;
1095 scsi_unlock_floptical(sdev, result);
1096 }
1097 }
1098
1099 out_free_result:
1100 kfree(result);
1101 out_free_sdev:
1102 if (res == SCSI_SCAN_LUN_PRESENT) {
1103 if (sdevp) {
1104 if (scsi_device_get(sdev) == 0) {
1105 *sdevp = sdev;
1106 } else {
1107 __scsi_remove_device(sdev);
1108 res = SCSI_SCAN_NO_RESPONSE;
1109 }
1110 }
1111 } else
1112 scsi_destroy_sdev(sdev);
1113 out:
1114 return res;
1115 }
1116
1117 /**
1118 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1119 * @starget: pointer to target structure to scan
1120 * @bflags: black/white list flag for LUN 0
1121 *
1122 * Description:
1123 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1124 * scanned) to some maximum lun until a LUN is found with no device
1125 * attached. Use the bflags to figure out any oddities.
1126 *
1127 * Modifies sdevscan->lun.
1128 **/
1129 static void scsi_sequential_lun_scan(struct scsi_target *starget,
1130 int bflags, int scsi_level, int rescan)
1131 {
1132 unsigned int sparse_lun, lun, max_dev_lun;
1133 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1134
1135 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1136 "%s\n", starget->dev.bus_id));
1137
1138 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1139 /*
1140 * If this device is known to support sparse multiple units,
1141 * override the other settings, and scan all of them. Normally,
1142 * SCSI-3 devices should be scanned via the REPORT LUNS.
1143 */
1144 if (bflags & BLIST_SPARSELUN) {
1145 max_dev_lun = shost->max_lun;
1146 sparse_lun = 1;
1147 } else
1148 sparse_lun = 0;
1149
1150 /*
1151 * If less than SCSI_1_CSS, and no special lun scaning, stop
1152 * scanning; this matches 2.4 behaviour, but could just be a bug
1153 * (to continue scanning a SCSI_1_CSS device).
1154 *
1155 * This test is broken. We might not have any device on lun0 for
1156 * a sparselun device, and if that's the case then how would we
1157 * know the real scsi_level, eh? It might make sense to just not
1158 * scan any SCSI_1 device for non-0 luns, but that check would best
1159 * go into scsi_alloc_sdev() and just have it return null when asked
1160 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1161 *
1162 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1163 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1164 == 0))
1165 return;
1166 */
1167 /*
1168 * If this device is known to support multiple units, override
1169 * the other settings, and scan all of them.
1170 */
1171 if (bflags & BLIST_FORCELUN)
1172 max_dev_lun = shost->max_lun;
1173 /*
1174 * REGAL CDC-4X: avoid hang after LUN 4
1175 */
1176 if (bflags & BLIST_MAX5LUN)
1177 max_dev_lun = min(5U, max_dev_lun);
1178 /*
1179 * Do not scan SCSI-2 or lower device past LUN 7, unless
1180 * BLIST_LARGELUN.
1181 */
1182 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1183 max_dev_lun = min(8U, max_dev_lun);
1184
1185 /*
1186 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1187 * until we reach the max, or no LUN is found and we are not
1188 * sparse_lun.
1189 */
1190 for (lun = 1; lun < max_dev_lun; ++lun)
1191 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1192 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1193 !sparse_lun)
1194 return;
1195 }
1196
1197 /**
1198 * scsilun_to_int: convert a scsi_lun to an int
1199 * @scsilun: struct scsi_lun to be converted.
1200 *
1201 * Description:
1202 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1203 * integer, and return the result. The caller must check for
1204 * truncation before using this function.
1205 *
1206 * Notes:
1207 * The struct scsi_lun is assumed to be four levels, with each level
1208 * effectively containing a SCSI byte-ordered (big endian) short; the
1209 * addressing bits of each level are ignored (the highest two bits).
1210 * For a description of the LUN format, post SCSI-3 see the SCSI
1211 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1212 *
1213 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1214 * the integer: 0x0b030a04
1215 **/
1216 static int scsilun_to_int(struct scsi_lun *scsilun)
1217 {
1218 int i;
1219 unsigned int lun;
1220
1221 lun = 0;
1222 for (i = 0; i < sizeof(lun); i += 2)
1223 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1224 scsilun->scsi_lun[i + 1]) << (i * 8));
1225 return lun;
1226 }
1227
1228 /**
1229 * int_to_scsilun: reverts an int into a scsi_lun
1230 * @int: integer to be reverted
1231 * @scsilun: struct scsi_lun to be set.
1232 *
1233 * Description:
1234 * Reverts the functionality of the scsilun_to_int, which packed
1235 * an 8-byte lun value into an int. This routine unpacks the int
1236 * back into the lun value.
1237 * Note: the scsilun_to_int() routine does not truly handle all
1238 * 8bytes of the lun value. This functions restores only as much
1239 * as was set by the routine.
1240 *
1241 * Notes:
1242 * Given an integer : 0x0b030a04, this function returns a
1243 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1244 *
1245 **/
1246 void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1247 {
1248 int i;
1249
1250 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1251
1252 for (i = 0; i < sizeof(lun); i += 2) {
1253 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1254 scsilun->scsi_lun[i+1] = lun & 0xFF;
1255 lun = lun >> 16;
1256 }
1257 }
1258 EXPORT_SYMBOL(int_to_scsilun);
1259
1260 /**
1261 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1262 * @sdevscan: scan the host, channel, and id of this scsi_device
1263 *
1264 * Description:
1265 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1266 * command, and scan the resulting list of LUNs by calling
1267 * scsi_probe_and_add_lun.
1268 *
1269 * Modifies sdevscan->lun.
1270 *
1271 * Return:
1272 * 0: scan completed (or no memory, so further scanning is futile)
1273 * 1: no report lun scan, or not configured
1274 **/
1275 static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
1276 int rescan)
1277 {
1278 char devname[64];
1279 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1280 unsigned int length;
1281 unsigned int lun;
1282 unsigned int num_luns;
1283 unsigned int retries;
1284 int result;
1285 struct scsi_lun *lunp, *lun_data;
1286 u8 *data;
1287 struct scsi_sense_hdr sshdr;
1288 struct scsi_device *sdev;
1289 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1290 int ret = 0;
1291
1292 /*
1293 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1294 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1295 * support more than 8 LUNs.
1296 */
1297 if (bflags & BLIST_NOREPORTLUN)
1298 return 1;
1299 if (starget->scsi_level < SCSI_2 &&
1300 starget->scsi_level != SCSI_UNKNOWN)
1301 return 1;
1302 if (starget->scsi_level < SCSI_3 &&
1303 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
1304 return 1;
1305 if (bflags & BLIST_NOLUN)
1306 return 0;
1307
1308 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1309 sdev = scsi_alloc_sdev(starget, 0, NULL);
1310 if (!sdev)
1311 return 0;
1312 if (scsi_device_get(sdev))
1313 return 0;
1314 }
1315
1316 sprintf(devname, "host %d channel %d id %d",
1317 shost->host_no, sdev->channel, sdev->id);
1318
1319 /*
1320 * Allocate enough to hold the header (the same size as one scsi_lun)
1321 * plus the max number of luns we are requesting.
1322 *
1323 * Reallocating and trying again (with the exact amount we need)
1324 * would be nice, but then we need to somehow limit the size
1325 * allocated based on the available memory and the limits of
1326 * kmalloc - we don't want a kmalloc() failure of a huge value to
1327 * prevent us from finding any LUNs on this target.
1328 */
1329 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1330 lun_data = kmalloc(length, GFP_ATOMIC |
1331 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
1332 if (!lun_data) {
1333 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
1334 goto out;
1335 }
1336
1337 scsi_cmd[0] = REPORT_LUNS;
1338
1339 /*
1340 * bytes 1 - 5: reserved, set to zero.
1341 */
1342 memset(&scsi_cmd[1], 0, 5);
1343
1344 /*
1345 * bytes 6 - 9: length of the command.
1346 */
1347 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1348 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1349 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1350 scsi_cmd[9] = (unsigned char) length & 0xff;
1351
1352 scsi_cmd[10] = 0; /* reserved */
1353 scsi_cmd[11] = 0; /* control */
1354
1355 /*
1356 * We can get a UNIT ATTENTION, for example a power on/reset, so
1357 * retry a few times (like sd.c does for TEST UNIT READY).
1358 * Experience shows some combinations of adapter/devices get at
1359 * least two power on/resets.
1360 *
1361 * Illegal requests (for devices that do not support REPORT LUNS)
1362 * should come through as a check condition, and will not generate
1363 * a retry.
1364 */
1365 for (retries = 0; retries < 3; retries++) {
1366 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1367 " REPORT LUNS to %s (try %d)\n", devname,
1368 retries));
1369
1370 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
1371 lun_data, length, &sshdr,
1372 SCSI_TIMEOUT + 4 * HZ, 3);
1373
1374 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
1375 " %s (try %d) result 0x%x\n", result
1376 ? "failed" : "successful", retries, result));
1377 if (result == 0)
1378 break;
1379 else if (scsi_sense_valid(&sshdr)) {
1380 if (sshdr.sense_key != UNIT_ATTENTION)
1381 break;
1382 }
1383 }
1384
1385 if (result) {
1386 /*
1387 * The device probably does not support a REPORT LUN command
1388 */
1389 ret = 1;
1390 goto out_err;
1391 }
1392
1393 /*
1394 * Get the length from the first four bytes of lun_data.
1395 */
1396 data = (u8 *) lun_data->scsi_lun;
1397 length = ((data[0] << 24) | (data[1] << 16) |
1398 (data[2] << 8) | (data[3] << 0));
1399
1400 num_luns = (length / sizeof(struct scsi_lun));
1401 if (num_luns > max_scsi_report_luns) {
1402 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1403 " of %d luns reported, try increasing"
1404 " max_scsi_report_luns.\n", devname,
1405 max_scsi_report_luns, num_luns);
1406 num_luns = max_scsi_report_luns;
1407 }
1408
1409 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1410 "scsi scan: REPORT LUN scan\n"));
1411
1412 /*
1413 * Scan the luns in lun_data. The entry at offset 0 is really
1414 * the header, so start at 1 and go up to and including num_luns.
1415 */
1416 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1417 lun = scsilun_to_int(lunp);
1418
1419 /*
1420 * Check if the unused part of lunp is non-zero, and so
1421 * does not fit in lun.
1422 */
1423 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1424 int i;
1425
1426 /*
1427 * Output an error displaying the LUN in byte order,
1428 * this differs from what linux would print for the
1429 * integer LUN value.
1430 */
1431 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1432 data = (char *)lunp->scsi_lun;
1433 for (i = 0; i < sizeof(struct scsi_lun); i++)
1434 printk("%02x", data[i]);
1435 printk(" has a LUN larger than currently supported.\n");
1436 } else if (lun > sdev->host->max_lun) {
1437 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1438 " than allowed by the host adapter\n",
1439 devname, lun);
1440 } else {
1441 int res;
1442
1443 res = scsi_probe_and_add_lun(starget,
1444 lun, NULL, NULL, rescan, NULL);
1445 if (res == SCSI_SCAN_NO_RESPONSE) {
1446 /*
1447 * Got some results, but now none, abort.
1448 */
1449 sdev_printk(KERN_ERR, sdev,
1450 "Unexpected response"
1451 " from lun %d while scanning, scan"
1452 " aborted\n", lun);
1453 break;
1454 }
1455 }
1456 }
1457
1458 out_err:
1459 kfree(lun_data);
1460 out:
1461 scsi_device_put(sdev);
1462 if (sdev->sdev_state == SDEV_CREATED)
1463 /*
1464 * the sdev we used didn't appear in the report luns scan
1465 */
1466 scsi_destroy_sdev(sdev);
1467 return ret;
1468 }
1469
1470 struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1471 uint id, uint lun, void *hostdata)
1472 {
1473 struct scsi_device *sdev = ERR_PTR(-ENODEV);
1474 struct device *parent = &shost->shost_gendev;
1475 struct scsi_target *starget;
1476
1477 if (strncmp(scsi_scan_type, "none", 4) == 0)
1478 return ERR_PTR(-ENODEV);
1479
1480 if (!shost->async_scan)
1481 scsi_complete_async_scans();
1482
1483 starget = scsi_alloc_target(parent, channel, id);
1484 if (!starget)
1485 return ERR_PTR(-ENOMEM);
1486
1487 mutex_lock(&shost->scan_mutex);
1488 if (scsi_host_scan_allowed(shost))
1489 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
1490 mutex_unlock(&shost->scan_mutex);
1491 scsi_target_reap(starget);
1492 put_device(&starget->dev);
1493
1494 return sdev;
1495 }
1496 EXPORT_SYMBOL(__scsi_add_device);
1497
1498 int scsi_add_device(struct Scsi_Host *host, uint channel,
1499 uint target, uint lun)
1500 {
1501 struct scsi_device *sdev =
1502 __scsi_add_device(host, channel, target, lun, NULL);
1503 if (IS_ERR(sdev))
1504 return PTR_ERR(sdev);
1505
1506 scsi_device_put(sdev);
1507 return 0;
1508 }
1509 EXPORT_SYMBOL(scsi_add_device);
1510
1511 void scsi_rescan_device(struct device *dev)
1512 {
1513 struct scsi_driver *drv;
1514
1515 if (!dev->driver)
1516 return;
1517
1518 drv = to_scsi_driver(dev->driver);
1519 if (try_module_get(drv->owner)) {
1520 if (drv->rescan)
1521 drv->rescan(dev);
1522 module_put(drv->owner);
1523 }
1524 }
1525 EXPORT_SYMBOL(scsi_rescan_device);
1526
1527 static void __scsi_scan_target(struct device *parent, unsigned int channel,
1528 unsigned int id, unsigned int lun, int rescan)
1529 {
1530 struct Scsi_Host *shost = dev_to_shost(parent);
1531 int bflags = 0;
1532 int res;
1533 struct scsi_target *starget;
1534
1535 if (shost->this_id == id)
1536 /*
1537 * Don't scan the host adapter
1538 */
1539 return;
1540
1541 starget = scsi_alloc_target(parent, channel, id);
1542 if (!starget)
1543 return;
1544
1545 if (lun != SCAN_WILD_CARD) {
1546 /*
1547 * Scan for a specific host/chan/id/lun.
1548 */
1549 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1550 goto out_reap;
1551 }
1552
1553 /*
1554 * Scan LUN 0, if there is some response, scan further. Ideally, we
1555 * would not configure LUN 0 until all LUNs are scanned.
1556 */
1557 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1558 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1559 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
1560 /*
1561 * The REPORT LUN did not scan the target,
1562 * do a sequential scan.
1563 */
1564 scsi_sequential_lun_scan(starget, bflags,
1565 starget->scsi_level, rescan);
1566 }
1567
1568 out_reap:
1569 /* now determine if the target has any children at all
1570 * and if not, nuke it */
1571 scsi_target_reap(starget);
1572
1573 put_device(&starget->dev);
1574 }
1575
1576 /**
1577 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1578 * target.
1579 * @parent: host to scan
1580 * @channel: channel to scan
1581 * @id: target id to scan
1582 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1583 * @rescan: passed to LUN scanning routines
1584 *
1585 * Description:
1586 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1587 * and possibly all LUNs on the target id.
1588 *
1589 * First try a REPORT LUN scan, if that does not scan the target, do a
1590 * sequential scan of LUNs on the target id.
1591 **/
1592 void scsi_scan_target(struct device *parent, unsigned int channel,
1593 unsigned int id, unsigned int lun, int rescan)
1594 {
1595 struct Scsi_Host *shost = dev_to_shost(parent);
1596
1597 if (strncmp(scsi_scan_type, "none", 4) == 0)
1598 return;
1599
1600 if (!shost->async_scan)
1601 scsi_complete_async_scans();
1602
1603 mutex_lock(&shost->scan_mutex);
1604 if (scsi_host_scan_allowed(shost))
1605 __scsi_scan_target(parent, channel, id, lun, rescan);
1606 mutex_unlock(&shost->scan_mutex);
1607 }
1608 EXPORT_SYMBOL(scsi_scan_target);
1609
1610 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1611 unsigned int id, unsigned int lun, int rescan)
1612 {
1613 uint order_id;
1614
1615 if (id == SCAN_WILD_CARD)
1616 for (id = 0; id < shost->max_id; ++id) {
1617 /*
1618 * XXX adapter drivers when possible (FCP, iSCSI)
1619 * could modify max_id to match the current max,
1620 * not the absolute max.
1621 *
1622 * XXX add a shost id iterator, so for example,
1623 * the FC ID can be the same as a target id
1624 * without a huge overhead of sparse id's.
1625 */
1626 if (shost->reverse_ordering)
1627 /*
1628 * Scan from high to low id.
1629 */
1630 order_id = shost->max_id - id - 1;
1631 else
1632 order_id = id;
1633 __scsi_scan_target(&shost->shost_gendev, channel,
1634 order_id, lun, rescan);
1635 }
1636 else
1637 __scsi_scan_target(&shost->shost_gendev, channel,
1638 id, lun, rescan);
1639 }
1640
1641 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1642 unsigned int id, unsigned int lun, int rescan)
1643 {
1644 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1645 "%s: <%u:%u:%u>\n",
1646 __FUNCTION__, channel, id, lun));
1647
1648 if (!shost->async_scan)
1649 scsi_complete_async_scans();
1650
1651 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1652 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1653 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1654 return -EINVAL;
1655
1656 mutex_lock(&shost->scan_mutex);
1657 if (scsi_host_scan_allowed(shost)) {
1658 if (channel == SCAN_WILD_CARD)
1659 for (channel = 0; channel <= shost->max_channel;
1660 channel++)
1661 scsi_scan_channel(shost, channel, id, lun,
1662 rescan);
1663 else
1664 scsi_scan_channel(shost, channel, id, lun, rescan);
1665 }
1666 mutex_unlock(&shost->scan_mutex);
1667
1668 return 0;
1669 }
1670
1671 static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1672 {
1673 struct scsi_device *sdev;
1674 shost_for_each_device(sdev, shost) {
1675 if (scsi_sysfs_add_sdev(sdev) != 0)
1676 scsi_destroy_sdev(sdev);
1677 }
1678 }
1679
1680 /**
1681 * scsi_prep_async_scan - prepare for an async scan
1682 * @shost: the host which will be scanned
1683 * Returns: a cookie to be passed to scsi_finish_async_scan()
1684 *
1685 * Tells the midlayer this host is going to do an asynchronous scan.
1686 * It reserves the host's position in the scanning list and ensures
1687 * that other asynchronous scans started after this one won't affect the
1688 * ordering of the discovered devices.
1689 */
1690 static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
1691 {
1692 struct async_scan_data *data;
1693
1694 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1695 return NULL;
1696
1697 if (shost->async_scan) {
1698 printk("%s called twice for host %d", __FUNCTION__,
1699 shost->host_no);
1700 dump_stack();
1701 return NULL;
1702 }
1703
1704 data = kmalloc(sizeof(*data), GFP_KERNEL);
1705 if (!data)
1706 goto err;
1707 data->shost = scsi_host_get(shost);
1708 if (!data->shost)
1709 goto err;
1710 init_completion(&data->prev_finished);
1711
1712 spin_lock(&async_scan_lock);
1713 shost->async_scan = 1;
1714 if (list_empty(&scanning_hosts))
1715 complete(&data->prev_finished);
1716 list_add_tail(&data->list, &scanning_hosts);
1717 spin_unlock(&async_scan_lock);
1718
1719 return data;
1720
1721 err:
1722 kfree(data);
1723 return NULL;
1724 }
1725
1726 /**
1727 * scsi_finish_async_scan - asynchronous scan has finished
1728 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1729 *
1730 * All the devices currently attached to this host have been found.
1731 * This function announces all the devices it has found to the rest
1732 * of the system.
1733 */
1734 static void scsi_finish_async_scan(struct async_scan_data *data)
1735 {
1736 struct Scsi_Host *shost;
1737
1738 if (!data)
1739 return;
1740
1741 shost = data->shost;
1742 if (!shost->async_scan) {
1743 printk("%s called twice for host %d", __FUNCTION__,
1744 shost->host_no);
1745 dump_stack();
1746 return;
1747 }
1748
1749 wait_for_completion(&data->prev_finished);
1750
1751 scsi_sysfs_add_devices(shost);
1752
1753 spin_lock(&async_scan_lock);
1754 shost->async_scan = 0;
1755 list_del(&data->list);
1756 if (!list_empty(&scanning_hosts)) {
1757 struct async_scan_data *next = list_entry(scanning_hosts.next,
1758 struct async_scan_data, list);
1759 complete(&next->prev_finished);
1760 }
1761 spin_unlock(&async_scan_lock);
1762
1763 scsi_host_put(shost);
1764 kfree(data);
1765 }
1766
1767 static void do_scsi_scan_host(struct Scsi_Host *shost)
1768 {
1769 if (shost->hostt->scan_finished) {
1770 unsigned long start = jiffies;
1771 if (shost->hostt->scan_start)
1772 shost->hostt->scan_start(shost);
1773
1774 while (!shost->hostt->scan_finished(shost, jiffies - start))
1775 msleep(10);
1776 } else {
1777 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1778 SCAN_WILD_CARD, 0);
1779 }
1780 }
1781
1782 static int do_scan_async(void *_data)
1783 {
1784 struct async_scan_data *data = _data;
1785 do_scsi_scan_host(data->shost);
1786 scsi_finish_async_scan(data);
1787 return 0;
1788 }
1789
1790 /**
1791 * scsi_scan_host - scan the given adapter
1792 * @shost: adapter to scan
1793 **/
1794 void scsi_scan_host(struct Scsi_Host *shost)
1795 {
1796 struct async_scan_data *data;
1797
1798 if (strncmp(scsi_scan_type, "none", 4) == 0)
1799 return;
1800
1801 data = scsi_prep_async_scan(shost);
1802 if (!data) {
1803 do_scsi_scan_host(shost);
1804 return;
1805 }
1806
1807 kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1808 }
1809 EXPORT_SYMBOL(scsi_scan_host);
1810
1811 void scsi_forget_host(struct Scsi_Host *shost)
1812 {
1813 struct scsi_device *sdev;
1814 unsigned long flags;
1815
1816 restart:
1817 spin_lock_irqsave(shost->host_lock, flags);
1818 list_for_each_entry(sdev, &shost->__devices, siblings) {
1819 if (sdev->sdev_state == SDEV_DEL)
1820 continue;
1821 spin_unlock_irqrestore(shost->host_lock, flags);
1822 __scsi_remove_device(sdev);
1823 goto restart;
1824 }
1825 spin_unlock_irqrestore(shost->host_lock, flags);
1826 }
1827
1828 /*
1829 * Function: scsi_get_host_dev()
1830 *
1831 * Purpose: Create a scsi_device that points to the host adapter itself.
1832 *
1833 * Arguments: SHpnt - Host that needs a scsi_device
1834 *
1835 * Lock status: None assumed.
1836 *
1837 * Returns: The scsi_device or NULL
1838 *
1839 * Notes:
1840 * Attach a single scsi_device to the Scsi_Host - this should
1841 * be made to look like a "pseudo-device" that points to the
1842 * HA itself.
1843 *
1844 * Note - this device is not accessible from any high-level
1845 * drivers (including generics), which is probably not
1846 * optimal. We can add hooks later to attach
1847 */
1848 struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1849 {
1850 struct scsi_device *sdev = NULL;
1851 struct scsi_target *starget;
1852
1853 mutex_lock(&shost->scan_mutex);
1854 if (!scsi_host_scan_allowed(shost))
1855 goto out;
1856 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1857 if (!starget)
1858 goto out;
1859
1860 sdev = scsi_alloc_sdev(starget, 0, NULL);
1861 if (sdev) {
1862 sdev->sdev_gendev.parent = get_device(&starget->dev);
1863 sdev->borken = 0;
1864 } else
1865 scsi_target_reap(starget);
1866 put_device(&starget->dev);
1867 out:
1868 mutex_unlock(&shost->scan_mutex);
1869 return sdev;
1870 }
1871 EXPORT_SYMBOL(scsi_get_host_dev);
1872
1873 /*
1874 * Function: scsi_free_host_dev()
1875 *
1876 * Purpose: Free a scsi_device that points to the host adapter itself.
1877 *
1878 * Arguments: SHpnt - Host that needs a scsi_device
1879 *
1880 * Lock status: None assumed.
1881 *
1882 * Returns: Nothing
1883 *
1884 * Notes:
1885 */
1886 void scsi_free_host_dev(struct scsi_device *sdev)
1887 {
1888 BUG_ON(sdev->id != sdev->host->this_id);
1889
1890 scsi_destroy_sdev(sdev);
1891 }
1892 EXPORT_SYMBOL(scsi_free_host_dev);
1893
This page took 0.143209 seconds and 5 git commands to generate.