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