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