blob: 7757e558d523ad197e42c187f2c3bce7a3743071 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scsi_scan.c
3 *
4 * Copyright (C) 2000 Eric Youngdale,
5 * Copyright (C) 2002 Patrick Mansfield
6 *
7 * The general scanning/probing algorithm is as follows, exceptions are
8 * made to it depending on device specific flags, compilation options, and
9 * global variable (boot or module load time) settings.
10 *
11 * A specific LUN is scanned via an INQUIRY command; if the LUN has a
Christoph Hellwigf64a1812005-10-31 18:32:08 +010012 * device attached, a scsi_device is allocated and setup for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * For every id of every channel on the given host:
15 *
16 * Scan LUN 0; if the target responds to LUN 0 (even if there is no
17 * device or storage attached to LUN 0):
18 *
19 * If LUN 0 has a device attached, allocate and setup a
Christoph Hellwigf64a1812005-10-31 18:32:08 +010020 * scsi_device for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * If target is SCSI-3 or up, issue a REPORT LUN, and scan
23 * all of the LUNs returned by the REPORT LUN; else,
24 * sequentially scan LUNs up until some maximum is reached,
25 * or a LUN is seen that cannot have a device attached to it.
26 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/blkdev.h>
Matthew Wilcox3e082a92006-09-28 15:19:20 -060032#include <linux/delay.h>
33#include <linux/kthread.h>
34#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020037#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <scsi/scsi_device.h>
39#include <scsi/scsi_driver.h>
40#include <scsi/scsi_devinfo.h>
41#include <scsi/scsi_host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <scsi/scsi_transport.h>
43#include <scsi/scsi_eh.h>
44
45#include "scsi_priv.h"
46#include "scsi_logging.h"
47
48#define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
49 " SCSI scanning, some SCSI devices might not be configured\n"
50
51/*
52 * Default timeout
53 */
54#define SCSI_TIMEOUT (2*HZ)
55
56/*
57 * Prefix values for the SCSI id's (stored in driverfs name field)
58 */
59#define SCSI_UID_SER_NUM 'S'
60#define SCSI_UID_UNKNOWN 'Z'
61
62/*
63 * Return values of some of the scanning functions.
64 *
65 * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
66 * includes allocation or general failures preventing IO from being sent.
67 *
68 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
69 * on the given LUN.
70 *
71 * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
72 * given LUN.
73 */
74#define SCSI_SCAN_NO_RESPONSE 0
75#define SCSI_SCAN_TARGET_PRESENT 1
76#define SCSI_SCAN_LUN_PRESENT 2
77
Arjan van de Ven0ad78202005-11-28 16:22:25 +010078static const char *scsi_null_device_strs = "nullnullnullnull";
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define MAX_SCSI_LUNS 512
81
82#ifdef CONFIG_SCSI_MULTI_LUN
83static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
84#else
85static unsigned int max_scsi_luns = 1;
86#endif
87
88module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR);
89MODULE_PARM_DESC(max_luns,
90 "last scsi LUN (should be between 1 and 2^32-1)");
91
Matthew Wilcox21db1882006-11-22 13:24:52 -070092#ifdef CONFIG_SCSI_SCAN_ASYNC
93#define SCSI_SCAN_TYPE_DEFAULT "async"
94#else
95#define SCSI_SCAN_TYPE_DEFAULT "sync"
96#endif
97
98static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT;
Matthew Wilcox3e082a92006-09-28 15:19:20 -060099
100module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
101MODULE_PARM_DESC(scan, "sync, async or none");
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * max_scsi_report_luns: the maximum number of LUNS that will be
105 * returned from the REPORT LUNS command. 8 times this value must
106 * be allocated. In theory this could be up to an 8 byte value, but
107 * in practice, the maximum number of LUNs suppored by any device
108 * is about 16k.
109 */
110static unsigned int max_scsi_report_luns = 511;
111
112module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR);
113MODULE_PARM_DESC(max_report_luns,
114 "REPORT LUNS maximum number of LUNS received (should be"
115 " between 1 and 16384)");
116
117static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
118
119module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR);
120MODULE_PARM_DESC(inq_timeout,
121 "Timeout (in seconds) waiting for devices to answer INQUIRY."
122 " Default is 5. Some non-compliant devices need more.");
123
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600124static DEFINE_SPINLOCK(async_scan_lock);
125static LIST_HEAD(scanning_hosts);
126
127struct async_scan_data {
128 struct list_head list;
129 struct Scsi_Host *shost;
130 struct completion prev_finished;
131};
132
133/**
134 * scsi_complete_async_scans - Wait for asynchronous scans to complete
135 *
Matthew Wilcox8bcc2412006-12-07 19:29:27 -0700136 * When this function returns, any host which started scanning before
137 * this function was called will have finished its scan. Hosts which
138 * started scanning after this function was called may or may not have
139 * finished.
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600140 */
141int scsi_complete_async_scans(void)
142{
143 struct async_scan_data *data;
144
145 do {
146 if (list_empty(&scanning_hosts))
147 return 0;
148 /* If we can't get memory immediately, that's OK. Just
149 * sleep a little. Even if we never get memory, the async
150 * scans will finish eventually.
151 */
152 data = kmalloc(sizeof(*data), GFP_KERNEL);
153 if (!data)
154 msleep(1);
155 } while (!data);
156
157 data->shost = NULL;
158 init_completion(&data->prev_finished);
159
160 spin_lock(&async_scan_lock);
161 /* Check that there's still somebody else on the list */
162 if (list_empty(&scanning_hosts))
163 goto done;
164 list_add_tail(&data->list, &scanning_hosts);
165 spin_unlock(&async_scan_lock);
166
167 printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
168 wait_for_completion(&data->prev_finished);
169
170 spin_lock(&async_scan_lock);
171 list_del(&data->list);
Matthew Wilcox8bcc2412006-12-07 19:29:27 -0700172 if (!list_empty(&scanning_hosts)) {
173 struct async_scan_data *next = list_entry(scanning_hosts.next,
174 struct async_scan_data, list);
175 complete(&next->prev_finished);
176 }
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600177 done:
178 spin_unlock(&async_scan_lock);
179
180 kfree(data);
181 return 0;
182}
183
184#ifdef MODULE
185/* Only exported for the benefit of scsi_wait_scan */
186EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
187#endif
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/**
190 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
James Bottomley39216032005-06-15 18:48:29 -0500191 * @sdev: scsi device to send command to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * @result: area to store the result of the MODE SENSE
193 *
194 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500195 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * Called for BLIST_KEY devices.
197 **/
James Bottomley39216032005-06-15 18:48:29 -0500198static void scsi_unlock_floptical(struct scsi_device *sdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned char *result)
200{
201 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
202
203 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
204 scsi_cmd[0] = MODE_SENSE;
205 scsi_cmd[1] = 0;
206 scsi_cmd[2] = 0x2e;
207 scsi_cmd[3] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500208 scsi_cmd[4] = 0x2a; /* size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 scsi_cmd[5] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500210 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
211 SCSI_TIMEOUT, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * scsi_alloc_sdev - allocate and setup a scsi_Device
216 *
217 * Description:
218 * Allocate, initialize for io, and return a pointer to a scsi_Device.
219 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
220 * adds scsi_Device to the appropriate list.
221 *
222 * Return value:
223 * scsi_Device pointer, or NULL on failure.
224 **/
225static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
226 unsigned int lun, void *hostdata)
227{
228 struct scsi_device *sdev;
229 int display_failure_msg = 1, ret;
230 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
231
Jes Sorensen24669f752006-01-16 10:31:18 -0500232 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 GFP_ATOMIC);
234 if (!sdev)
235 goto out;
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 sdev->vendor = scsi_null_device_strs;
238 sdev->model = scsi_null_device_strs;
239 sdev->rev = scsi_null_device_strs;
240 sdev->host = shost;
241 sdev->id = starget->id;
242 sdev->lun = lun;
243 sdev->channel = starget->channel;
244 sdev->sdev_state = SDEV_CREATED;
245 INIT_LIST_HEAD(&sdev->siblings);
246 INIT_LIST_HEAD(&sdev->same_target_siblings);
247 INIT_LIST_HEAD(&sdev->cmd_list);
248 INIT_LIST_HEAD(&sdev->starved_entry);
249 spin_lock_init(&sdev->list_lock);
250
251 sdev->sdev_gendev.parent = get_device(&starget->dev);
252 sdev->sdev_target = starget;
253
254 /* usually NULL and set by ->slave_alloc instead */
255 sdev->hostdata = hostdata;
256
257 /* if the device needs this changing, it may do so in the
258 * slave_configure function */
259 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
260
261 /*
262 * Some low level driver could use device->type
263 */
264 sdev->type = -1;
265
266 /*
267 * Assume that the device will have handshaking problems,
268 * and then fix this field later if it turns out it
269 * doesn't
270 */
271 sdev->borken = 1;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 sdev->request_queue = scsi_alloc_queue(sdev);
274 if (!sdev->request_queue) {
275 /* release fn is set up in scsi_sysfs_device_initialise, so
276 * have to free and put manually here */
277 put_device(&starget->dev);
Dave Jones93f560892006-03-09 10:21:27 -0500278 kfree(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 goto out;
280 }
281
282 sdev->request_queue->queuedata = sdev;
283 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
284
285 scsi_sysfs_device_initialize(sdev);
286
287 if (shost->hostt->slave_alloc) {
288 ret = shost->hostt->slave_alloc(sdev);
289 if (ret) {
290 /*
291 * if LLDD reports slave not present, don't clutter
292 * console with alloc failure messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294 if (ret == -ENXIO)
295 display_failure_msg = 0;
296 goto out_device_destroy;
297 }
298 }
299
300 return sdev;
301
302out_device_destroy:
303 transport_destroy_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 put_device(&sdev->sdev_gendev);
305out:
306 if (display_failure_msg)
307 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
308 return NULL;
309}
310
311static void scsi_target_dev_release(struct device *dev)
312{
313 struct device *parent = dev->parent;
314 struct scsi_target *starget = to_scsi_target(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 kfree(starget);
317 put_device(parent);
318}
319
320int scsi_is_target_device(const struct device *dev)
321{
322 return dev->release == scsi_target_dev_release;
323}
324EXPORT_SYMBOL(scsi_is_target_device);
325
326static struct scsi_target *__scsi_find_target(struct device *parent,
327 int channel, uint id)
328{
329 struct scsi_target *starget, *found_starget = NULL;
330 struct Scsi_Host *shost = dev_to_shost(parent);
331 /*
332 * Search for an existing target for this sdev.
333 */
334 list_for_each_entry(starget, &shost->__targets, siblings) {
335 if (starget->id == id &&
336 starget->channel == channel) {
337 found_starget = starget;
338 break;
339 }
340 }
341 if (found_starget)
342 get_device(&found_starget->dev);
343
344 return found_starget;
345}
346
James Bottomley884d25c2006-09-05 16:26:41 -0500347/**
348 * scsi_alloc_target - allocate a new or find an existing target
349 * @parent: parent of the target (need not be a scsi host)
350 * @channel: target channel number (zero if no channels)
351 * @id: target id number
352 *
353 * Return an existing target if one exists, provided it hasn't already
354 * gone into STARGET_DEL state, otherwise allocate a new target.
355 *
356 * The target is returned with an incremented reference, so the caller
357 * is responsible for both reaping and doing a last put
358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static struct scsi_target *scsi_alloc_target(struct device *parent,
360 int channel, uint id)
361{
362 struct Scsi_Host *shost = dev_to_shost(parent);
363 struct device *dev = NULL;
364 unsigned long flags;
365 const int size = sizeof(struct scsi_target)
366 + shost->transportt->target_size;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -0400367 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct scsi_target *found_target;
Brian King32f95792006-02-22 14:28:24 -0600369 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jes Sorensen24669f752006-01-16 10:31:18 -0500371 starget = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!starget) {
373 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
374 return NULL;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dev = &starget->dev;
377 device_initialize(dev);
378 starget->reap_ref = 1;
379 dev->parent = get_device(parent);
380 dev->release = scsi_target_dev_release;
381 sprintf(dev->bus_id, "target%d:%d:%d",
382 shost->host_no, channel, id);
383 starget->id = id;
384 starget->channel = channel;
385 INIT_LIST_HEAD(&starget->siblings);
386 INIT_LIST_HEAD(&starget->devices);
James Bottomleyffedb452006-02-23 14:27:18 -0600387 starget->state = STARGET_RUNNING;
388 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 spin_lock_irqsave(shost->host_lock, flags);
390
391 found_target = __scsi_find_target(parent, channel, id);
392 if (found_target)
393 goto found;
394
395 list_add_tail(&starget->siblings, &shost->__targets);
396 spin_unlock_irqrestore(shost->host_lock, flags);
397 /* allocate and add */
James Bottomleya283bd32005-05-24 12:06:38 -0500398 transport_setup_device(dev);
Brian King32f95792006-02-22 14:28:24 -0600399 error = device_add(dev);
400 if (error) {
401 dev_err(dev, "target device_add failed, error %d\n", error);
402 spin_lock_irqsave(shost->host_lock, flags);
403 list_del_init(&starget->siblings);
404 spin_unlock_irqrestore(shost->host_lock, flags);
405 transport_destroy_device(dev);
406 put_device(parent);
407 kfree(starget);
408 return NULL;
409 }
James Bottomleya283bd32005-05-24 12:06:38 -0500410 transport_add_device(dev);
411 if (shost->hostt->target_alloc) {
Brian King32f95792006-02-22 14:28:24 -0600412 error = shost->hostt->target_alloc(starget);
James Bottomleya283bd32005-05-24 12:06:38 -0500413
414 if(error) {
415 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
416 /* don't want scsi_target_reap to do the final
417 * put because it will be under the host lock */
418 get_device(dev);
419 scsi_target_reap(starget);
420 put_device(dev);
421 return NULL;
422 }
423 }
James Bottomley884d25c2006-09-05 16:26:41 -0500424 get_device(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return starget;
427
428 found:
429 found_target->reap_ref++;
430 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600431 if (found_target->state != STARGET_DEL) {
James Bottomley884d25c2006-09-05 16:26:41 -0500432 put_device(parent);
James Bottomleyffedb452006-02-23 14:27:18 -0600433 kfree(starget);
434 return found_target;
435 }
436 /* Unfortunately, we found a dying target; need to
437 * wait until it's dead before we can get a new one */
438 put_device(&found_target->dev);
439 flush_scheduled_work();
440 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
David Howells65f27f32006-11-22 14:55:48 +0000443static void scsi_target_reap_usercontext(struct work_struct *work)
James Bottomley65110b22006-02-14 10:48:46 -0600444{
David Howells65f27f32006-11-22 14:55:48 +0000445 struct scsi_target *starget =
446 container_of(work, struct scsi_target, ew.work);
James Bottomley863a9302005-12-15 20:01:43 -0800447 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
448 unsigned long flags;
449
James Bottomleyffedb452006-02-23 14:27:18 -0600450 transport_remove_device(&starget->dev);
451 device_del(&starget->dev);
452 transport_destroy_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800453 spin_lock_irqsave(shost->host_lock, flags);
Mike Andersona50a5e32006-03-14 11:18:46 -0800454 if (shost->hostt->target_destroy)
455 shost->hostt->target_destroy(starget);
James Bottomleyffedb452006-02-23 14:27:18 -0600456 list_del_init(&starget->siblings);
James Bottomley863a9302005-12-15 20:01:43 -0800457 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600458 put_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/**
462 * scsi_target_reap - check to see if target is in use and destroy if not
463 *
464 * @starget: target to be checked
465 *
466 * This is used after removing a LUN or doing a last put of the target
467 * it checks atomically that nothing is using the target and removes
468 * it if so.
469 */
470void scsi_target_reap(struct scsi_target *starget)
471{
James Bottomleyffedb452006-02-23 14:27:18 -0600472 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
473 unsigned long flags;
474
475 spin_lock_irqsave(shost->host_lock, flags);
476
477 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
478 BUG_ON(starget->state == STARGET_DEL);
479 starget->state = STARGET_DEL;
480 spin_unlock_irqrestore(shost->host_lock, flags);
481 execute_in_process_context(scsi_target_reap_usercontext,
David Howells65f27f32006-11-22 14:55:48 +0000482 &starget->ew);
James Bottomleyffedb452006-02-23 14:27:18 -0600483 return;
484
485 }
486 spin_unlock_irqrestore(shost->host_lock, flags);
487
488 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/**
Alan Sterne5b3cd42006-08-21 15:53:25 -0400492 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
493 * @s: INQUIRY result string to sanitize
494 * @len: length of the string
495 *
496 * Description:
497 * The SCSI spec says that INQUIRY vendor, product, and revision
498 * strings must consist entirely of graphic ASCII characters,
499 * padded on the right with spaces. Since not all devices obey
500 * this rule, we will replace non-graphic or non-ASCII characters
501 * with spaces. Exception: a NUL character is interpreted as a
502 * string terminator, so all the following characters are set to
503 * spaces.
504 **/
505static void sanitize_inquiry_string(unsigned char *s, int len)
506{
507 int terminated = 0;
508
509 for (; len > 0; (--len, ++s)) {
510 if (*s == 0)
511 terminated = 1;
512 if (terminated || *s < 0x20 || *s > 0x7e)
513 *s = ' ';
514 }
515}
516
517/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
James Bottomley39216032005-06-15 18:48:29 -0500519 * @sdev: scsi_device to probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * @inq_result: area to store the INQUIRY result
James Bottomley39216032005-06-15 18:48:29 -0500521 * @result_len: len of inq_result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * @bflags: store any bflags found here
523 *
524 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500525 * Probe the lun associated with @req using a standard SCSI INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *
James Bottomley39216032005-06-15 18:48:29 -0500527 * If the INQUIRY is successful, zero is returned and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100529 * are copied to the scsi_device any flags value is stored in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400531static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
James Bottomley39216032005-06-15 18:48:29 -0500532 int result_len, int *bflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
535 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
536 int response_len = 0;
James Bottomley39216032005-06-15 18:48:29 -0500537 int pass, count, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct scsi_sense_hdr sshdr;
539
540 *bflags = 0;
541
542 /* Perform up to 3 passes. The first pass uses a conservative
543 * transfer length of 36 unless sdev->inquiry_len specifies a
544 * different value. */
545 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
546 try_inquiry_len = first_inquiry_len;
547 pass = 1;
548
549 next_pass:
James Bottomley9ccfc752005-10-02 11:45:08 -0500550 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
551 "scsi scan: INQUIRY pass %d length %d\n",
552 pass, try_inquiry_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* Each pass gets up to three chances to ignore Unit Attention */
555 for (count = 0; count < 3; ++count) {
556 memset(scsi_cmd, 0, 6);
557 scsi_cmd[0] = INQUIRY;
558 scsi_cmd[4] = (unsigned char) try_inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 memset(inq_result, 0, try_inquiry_len);
James Bottomley39216032005-06-15 18:48:29 -0500561
562 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500563 inq_result, try_inquiry_len, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -0500564 HZ / 2 + HZ * scsi_inq_timeout, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
567 "with code 0x%x\n",
James Bottomley39216032005-06-15 18:48:29 -0500568 result ? "failed" : "successful", result));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
James Bottomley39216032005-06-15 18:48:29 -0500570 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /*
572 * not-ready to ready transition [asc/ascq=0x28/0x0]
573 * or power-on, reset [asc/ascq=0x29/0x0], continue.
574 * INQUIRY should not yield UNIT_ATTENTION
575 * but many buggy devices do so anyway.
576 */
James Bottomley39216032005-06-15 18:48:29 -0500577 if ((driver_byte(result) & DRIVER_SENSE) &&
James Bottomleyea73a9f2005-08-28 11:33:52 -0500578 scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if ((sshdr.sense_key == UNIT_ATTENTION) &&
580 ((sshdr.asc == 0x28) ||
581 (sshdr.asc == 0x29)) &&
582 (sshdr.ascq == 0))
583 continue;
584 }
585 }
586 break;
587 }
588
James Bottomley39216032005-06-15 18:48:29 -0500589 if (result == 0) {
Alan Sterne5b3cd42006-08-21 15:53:25 -0400590 sanitize_inquiry_string(&inq_result[8], 8);
591 sanitize_inquiry_string(&inq_result[16], 16);
592 sanitize_inquiry_string(&inq_result[32], 4);
593
594 response_len = inq_result[4] + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (response_len > 255)
596 response_len = first_inquiry_len; /* sanity */
597
598 /*
599 * Get any flags for this device.
600 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100601 * XXX add a bflags to scsi_device, and replace the
602 * corresponding bit fields in scsi_device, so bflags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * need not be passed as an argument.
604 */
605 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
606 &inq_result[16]);
607
608 /* When the first pass succeeds we gain information about
609 * what larger transfer lengths might work. */
610 if (pass == 1) {
611 if (BLIST_INQUIRY_36 & *bflags)
612 next_inquiry_len = 36;
613 else if (BLIST_INQUIRY_58 & *bflags)
614 next_inquiry_len = 58;
615 else if (sdev->inquiry_len)
616 next_inquiry_len = sdev->inquiry_len;
617 else
618 next_inquiry_len = response_len;
619
620 /* If more data is available perform the second pass */
621 if (next_inquiry_len > try_inquiry_len) {
622 try_inquiry_len = next_inquiry_len;
623 pass = 2;
624 goto next_pass;
625 }
626 }
627
628 } else if (pass == 2) {
629 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
630 "Consider BLIST_INQUIRY_36 for this device\n",
631 try_inquiry_len);
632
633 /* If this pass failed, the third pass goes back and transfers
634 * the same amount as we successfully got in the first pass. */
635 try_inquiry_len = first_inquiry_len;
636 pass = 3;
637 goto next_pass;
638 }
639
640 /* If the last transfer attempt got an error, assume the
641 * peripheral doesn't exist or is dead. */
James Bottomley39216032005-06-15 18:48:29 -0500642 if (result)
643 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* Don't report any more data than the device says is valid */
646 sdev->inquiry_len = min(try_inquiry_len, response_len);
647
648 /*
649 * XXX Abort if the response length is less than 36? If less than
650 * 32, the lookup of the device flags (above) could be invalid,
651 * and it would be possible to take an incorrect action - we do
652 * not want to hang because of a short INQUIRY. On the flip side,
653 * if the device is spun down or becoming ready (and so it gives a
654 * short INQUIRY), an abort here prevents any further use of the
655 * device, including spin up.
656 *
Alan Sterne423ee32007-02-16 01:46:38 -0800657 * On the whole, the best approach seems to be to assume the first
658 * 36 bytes are valid no matter what the device says. That's
659 * better than copying < 36 bytes to the inquiry-result buffer
660 * and displaying garbage for the Vendor, Product, or Revision
661 * strings.
662 */
663 if (sdev->inquiry_len < 36) {
664 printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
665 " using 36\n", sdev->inquiry_len);
666 sdev->inquiry_len = 36;
667 }
668
669 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 * Related to the above issue:
671 *
672 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
673 * and if not ready, sent a START_STOP to start (maybe spin up) and
674 * then send the INQUIRY again, since the INQUIRY can change after
675 * a device is initialized.
676 *
677 * Ideally, start a device if explicitly asked to do so. This
678 * assumes that a device is spun up on power on, spun down on
679 * request, and then spun up on request.
680 */
681
682 /*
683 * The scanning code needs to know the scsi_level, even if no
684 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
685 * non-zero LUNs can be scanned.
686 */
687 sdev->scsi_level = inq_result[2] & 0x07;
688 if (sdev->scsi_level >= 2 ||
689 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
690 sdev->scsi_level++;
James Bottomley6f3a2022005-09-22 20:33:28 -0500691 sdev->sdev_target->scsi_level = sdev->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
James Bottomley39216032005-06-15 18:48:29 -0500693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/**
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100697 * scsi_add_lun - allocate and fully initialze a scsi_device
698 * @sdevscan: holds information to be stored in the new scsi_device
699 * @sdevnew: store the address of the newly allocated scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 * @inq_result: holds the result of a previous INQUIRY to the LUN
701 * @bflags: black/white list flag
702 *
703 * Description:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100704 * Allocate and initialize a scsi_device matching sdevscan. Optionally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * set fields based on values in *@bflags. If @sdevnew is not
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100706 * NULL, store the address of the new scsi_device in *@sdevnew (needed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * when scanning a particular LUN).
708 *
709 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100710 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
711 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400713static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600714 int *bflags, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 /*
717 * XXX do not save the inquiry, since it can change underneath us,
718 * save just vendor/model/rev.
719 *
720 * Rather than save it and have an ioctl that retrieves the saved
721 * value, have an ioctl that executes the same INQUIRY code used
722 * in scsi_probe_lun, let user level programs doing INQUIRY
723 * scanning run at their own risk, or supply a user level program
724 * that can correctly scan.
725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Alan Stern09123d22006-11-10 12:27:57 -0800727 /*
728 * Copy at least 36 bytes of INQUIRY data, so that we don't
729 * dereference unallocated memory when accessing the Vendor,
730 * Product, and Revision strings. Badly behaved devices may set
731 * the INQUIRY Additional Length byte to a small value, indicating
732 * these strings are invalid, but often they contain plausible data
733 * nonetheless. It doesn't matter if the device sent < 36 bytes
734 * total, since scsi_probe_lun() initializes inq_result with 0s.
735 */
736 sdev->inquiry = kmemdup(inq_result,
737 max_t(size_t, sdev->inquiry_len, 36),
738 GFP_ATOMIC);
739 if (sdev->inquiry == NULL)
740 return SCSI_SCAN_NO_RESPONSE;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 sdev->vendor = (char *) (sdev->inquiry + 8);
743 sdev->model = (char *) (sdev->inquiry + 16);
744 sdev->rev = (char *) (sdev->inquiry + 32);
745
746 if (*bflags & BLIST_ISROM) {
747 /*
748 * It would be better to modify sdev->type, and set
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600749 * sdev->removable; this can now be done since
750 * print_inquiry has gone away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
752 inq_result[0] = TYPE_ROM;
753 inq_result[1] |= 0x80; /* removable */
754 } else if (*bflags & BLIST_NO_ULD_ATTACH)
755 sdev->no_uld_attach = 1;
756
757 switch (sdev->type = (inq_result[0] & 0x1f)) {
James Bottomleyddaf6fc2006-12-13 10:10:40 -0600758 case TYPE_RBC:
759 /* RBC devices can return SCSI-3 compliance and yet
760 * still not support REPORT LUNS, so make them act as
761 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
762 * specifically set */
763 if ((*bflags & BLIST_REPORTLUN2) == 0)
764 *bflags |= BLIST_NOREPORTLUN;
765 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 case TYPE_TAPE:
767 case TYPE_DISK:
768 case TYPE_PRINTER:
769 case TYPE_MOD:
770 case TYPE_PROCESSOR:
771 case TYPE_SCANNER:
772 case TYPE_MEDIUM_CHANGER:
773 case TYPE_ENCLOSURE:
774 case TYPE_COMM:
James Bottomley4d7db042006-03-31 20:07:45 -0600775 case TYPE_RAID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 sdev->writeable = 1;
777 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 case TYPE_ROM:
James Bottomleyddaf6fc2006-12-13 10:10:40 -0600779 /* MMC devices can return SCSI-3 compliance and yet
780 * still not support REPORT LUNS, so make them act as
781 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
782 * specifically set */
783 if ((*bflags & BLIST_REPORTLUN2) == 0)
784 *bflags |= BLIST_NOREPORTLUN;
785 /* fall through */
786 case TYPE_WORM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 sdev->writeable = 0;
788 break;
789 default:
790 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
795 * spec says: The device server is capable of supporting the
796 * specified peripheral device type on this logical unit. However,
797 * the physical device is not currently connected to this logical
798 * unit.
799 *
800 * The above is vague, as it implies that we could treat 001 and
801 * 011 the same. Stay compatible with previous code, and create a
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100802 * scsi_device for a PQ of 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
804 * Don't set the device offline here; rather let the upper
805 * level drivers eval the PQ to decide whether they should
806 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
807 */
808
809 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
810 sdev->removable = (0x80 & inq_result[1]) >> 7;
811 sdev->lockable = sdev->removable;
812 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
813
814 if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
815 inq_result[56] & 0x04))
816 sdev->ppr = 1;
817 if (inq_result[7] & 0x60)
818 sdev->wdtr = 1;
819 if (inq_result[7] & 0x10)
820 sdev->sdtr = 1;
821
James Bottomley19ac0db2006-08-06 18:15:22 -0500822 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600823 "ANSI: %d%s\n", scsi_device_type(sdev->type),
824 sdev->vendor, sdev->model, sdev->rev,
825 sdev->inq_periph_qual, inq_result[2] & 0x07,
826 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /*
Greg KH5e3c34c2006-01-18 16:17:46 -0800829 * End sysfs code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 */
831
832 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
833 !(*bflags & BLIST_NOTQ))
834 sdev->tagged_supported = 1;
835 /*
836 * Some devices (Texel CD ROM drives) have handshaking problems
837 * when used with the Seagate controllers. borken is initialized
838 * to 1, and then set it to 0 here.
839 */
840 if ((*bflags & BLIST_BORKEN) == 0)
841 sdev->borken = 0;
842
843 /*
844 * Apparently some really broken devices (contrary to the SCSI
845 * standards) need to be selected without asserting ATN
846 */
847 if (*bflags & BLIST_SELECT_NO_ATN)
848 sdev->select_no_atn = 1;
849
850 /*
James Bottomley4d7db042006-03-31 20:07:45 -0600851 * Maximum 512 sector transfer length
852 * broken RA4x00 Compaq Disk Array
853 */
854 if (*bflags & BLIST_MAX_512)
855 blk_queue_max_sectors(sdev->request_queue, 512);
856
857 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * Some devices may not want to have a start command automatically
859 * issued when a device is added.
860 */
861 if (*bflags & BLIST_NOSTARTONADD)
862 sdev->no_start_on_add = 1;
863
864 if (*bflags & BLIST_SINGLELUN)
865 sdev->single_lun = 1;
866
867
868 sdev->use_10_for_rw = 1;
869
870 if (*bflags & BLIST_MS_SKIP_PAGE_08)
871 sdev->skip_ms_page_8 = 1;
872
873 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
874 sdev->skip_ms_page_3f = 1;
875
876 if (*bflags & BLIST_USE_10_BYTE_MS)
877 sdev->use_10_for_ms = 1;
878
879 /* set the device running here so that slave configure
880 * may do I/O */
881 scsi_device_set_state(sdev, SDEV_RUNNING);
882
883 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
884 sdev->use_192_bytes_for_3f = 1;
885
886 if (*bflags & BLIST_NOT_LOCKABLE)
887 sdev->lockable = 0;
888
889 if (*bflags & BLIST_RETRY_HWERROR)
890 sdev->retry_hwerror = 1;
891
892 transport_configure_device(&sdev->sdev_gendev);
893
Christoph Hellwig93805092006-02-17 12:11:29 +0100894 if (sdev->host->hostt->slave_configure) {
895 int ret = sdev->host->hostt->slave_configure(sdev);
896 if (ret) {
897 /*
898 * if LLDD reports slave not present, don't clutter
899 * console with alloc failure messages
900 */
901 if (ret != -ENXIO) {
902 sdev_printk(KERN_ERR, sdev,
903 "failed to configure device\n");
904 }
905 return SCSI_SCAN_NO_RESPONSE;
906 }
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /*
910 * Ok, the device is now all set up, we can
911 * register it and tell the rest of the kernel
912 * about it.
913 */
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600914 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
Alan Sternb24b1032005-07-27 11:43:46 -0700915 return SCSI_SCAN_NO_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 return SCSI_SCAN_LUN_PRESENT;
918}
919
James Bottomley6f3a2022005-09-22 20:33:28 -0500920static inline void scsi_destroy_sdev(struct scsi_device *sdev)
921{
Brian King309bd272006-06-27 11:10:43 -0500922 scsi_device_set_state(sdev, SDEV_DEL);
James Bottomley6f3a2022005-09-22 20:33:28 -0500923 if (sdev->host->hostt->slave_destroy)
924 sdev->host->hostt->slave_destroy(sdev);
925 transport_destroy_device(&sdev->sdev_gendev);
926 put_device(&sdev->sdev_gendev);
927}
928
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700929#ifdef CONFIG_SCSI_LOGGING
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200930/**
931 * scsi_inq_str - print INQUIRY data from min to max index,
932 * strip trailing whitespace
933 * @buf: Output buffer with at least end-first+1 bytes of space
934 * @inq: Inquiry buffer (input)
935 * @first: Offset of string into inq
936 * @end: Index after last character in inq
937 */
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700938static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200939 unsigned first, unsigned end)
940{
941 unsigned term = 0, idx;
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700942
943 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
944 if (inq[idx+first] > ' ') {
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200945 buf[idx] = inq[idx+first];
946 term = idx+1;
947 } else {
948 buf[idx] = ' ';
949 }
950 }
951 buf[term] = 0;
952 return buf;
953}
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700954#endif
James Bottomley6f3a2022005-09-22 20:33:28 -0500955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/**
957 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
958 * @starget: pointer to target device structure
959 * @lun: LUN of target device
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100960 * @sdevscan: probe the LUN corresponding to this scsi_device
961 * @sdevnew: store the value of any new scsi_device allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * @bflagsp: store bflags here if not NULL
963 *
964 * Description:
965 * Call scsi_probe_lun, if a LUN with an attached device is found,
966 * allocate and set it up by calling scsi_add_lun.
967 *
968 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100969 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
971 * attached at the LUN
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100972 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 **/
974static int scsi_probe_and_add_lun(struct scsi_target *starget,
975 uint lun, int *bflagsp,
976 struct scsi_device **sdevp, int rescan,
977 void *hostdata)
978{
979 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 unsigned char *result;
James Bottomley39216032005-06-15 18:48:29 -0500981 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
983
984 /*
985 * The rescan flag is used as an optimization, the first scan of a
986 * host adapter calls into here with rescan == 0.
987 */
James Bottomley6f3a2022005-09-22 20:33:28 -0500988 sdev = scsi_device_lookup_by_target(starget, lun);
989 if (sdev) {
990 if (rescan || sdev->sdev_state != SDEV_CREATED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
992 "scsi scan: device exists on %s\n",
993 sdev->sdev_gendev.bus_id));
994 if (sdevp)
995 *sdevp = sdev;
996 else
997 scsi_device_put(sdev);
998
999 if (bflagsp)
1000 *bflagsp = scsi_get_device_flags(sdev,
1001 sdev->vendor,
1002 sdev->model);
1003 return SCSI_SCAN_LUN_PRESENT;
1004 }
James Bottomley6f3a2022005-09-22 20:33:28 -05001005 scsi_device_put(sdev);
1006 } else
1007 sdev = scsi_alloc_sdev(starget, lun, hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!sdev)
1009 goto out;
James Bottomley39216032005-06-15 18:48:29 -05001010
1011 result = kmalloc(result_len, GFP_ATOMIC |
Al Virobc861202005-04-24 12:28:34 -07001012 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (!result)
James Bottomley39216032005-06-15 18:48:29 -05001014 goto out_free_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
James Bottomley39216032005-06-15 18:48:29 -05001016 if (scsi_probe_lun(sdev, result, result_len, &bflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 goto out_free_result;
1018
Kurt Garloff4186ab12006-04-03 15:16:48 +02001019 if (bflagsp)
1020 *bflagsp = bflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /*
1022 * result contains valid SCSI INQUIRY data.
1023 */
Kurt Garloff13f7e5a2006-04-03 15:20:08 +02001024 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * For a Peripheral qualifier 3 (011b), the SCSI
1027 * spec says: The device server is not capable of
1028 * supporting a physical device on this logical
1029 * unit.
1030 *
1031 * For disks, this implies that there is no
1032 * logical disk configured at sdev->lun, but there
1033 * is a target id responding.
1034 */
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001035 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1036 " peripheral qualifier of 3, device not"
1037 " added\n"))
1038 if (lun == 0) {
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001039 SCSI_LOG_SCAN_BUS(1, {
1040 unsigned char vend[9];
1041 unsigned char mod[17];
1042
1043 sdev_printk(KERN_INFO, sdev,
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001044 "scsi scan: consider passing scsi_mod."
Kurt Garloff3424a652007-01-09 02:28:54 +01001045 "dev_flags=%s:%s:0x240 or 0x1000240\n",
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001046 scsi_inq_str(vend, result, 8, 16),
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001047 scsi_inq_str(mod, result, 16, 32));
1048 });
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001049 }
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 res = SCSI_SCAN_TARGET_PRESENT;
1052 goto out_free_result;
1053 }
1054
Alan Stern1bfc5d92006-02-09 15:26:18 -05001055 /*
dave wysochanski84961f22006-08-09 14:56:32 -04001056 * Some targets may set slight variations of PQ and PDT to signal
1057 * that no LUN is present, so don't add sdev in these cases.
1058 * Two specific examples are:
1059 * 1) NetApp targets: return PQ=1, PDT=0x1f
1060 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1061 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1062 *
1063 * References:
1064 * 1) SCSI SPC-3, pp. 145-146
1065 * PQ=1: "A peripheral device having the specified peripheral
1066 * device type is not connected to this logical unit. However, the
1067 * device server is capable of supporting the specified peripheral
1068 * device type on this logical unit."
1069 * PDT=0x1f: "Unknown or no device type"
1070 * 2) USB UFI 1.0, p. 20
1071 * PDT=00h Direct-access device (floppy)
1072 * PDT=1Fh none (no FDD connected to the requested logical unit)
Alan Stern1bfc5d92006-02-09 15:26:18 -05001073 */
dave wysochanski84961f22006-08-09 14:56:32 -04001074 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1075 (result[0] & 0x1f) == 0x1f) {
Alan Stern1bfc5d92006-02-09 15:26:18 -05001076 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1077 "scsi scan: peripheral device type"
1078 " of 31, no device added\n"));
1079 res = SCSI_SCAN_TARGET_PRESENT;
1080 goto out_free_result;
1081 }
1082
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001083 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (res == SCSI_SCAN_LUN_PRESENT) {
1085 if (bflags & BLIST_KEY) {
1086 sdev->lockable = 0;
James Bottomley39216032005-06-15 18:48:29 -05001087 scsi_unlock_floptical(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
1091 out_free_result:
1092 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 out_free_sdev:
1094 if (res == SCSI_SCAN_LUN_PRESENT) {
1095 if (sdevp) {
Alan Sternb70d37b2005-07-26 10:30:40 -04001096 if (scsi_device_get(sdev) == 0) {
1097 *sdevp = sdev;
1098 } else {
1099 __scsi_remove_device(sdev);
1100 res = SCSI_SCAN_NO_RESPONSE;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
James Bottomley6f3a2022005-09-22 20:33:28 -05001103 } else
1104 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 out:
1106 return res;
1107}
1108
1109/**
1110 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1111 * @starget: pointer to target structure to scan
1112 * @bflags: black/white list flag for LUN 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
1114 * Description:
1115 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1116 * scanned) to some maximum lun until a LUN is found with no device
1117 * attached. Use the bflags to figure out any oddities.
1118 *
1119 * Modifies sdevscan->lun.
1120 **/
1121static void scsi_sequential_lun_scan(struct scsi_target *starget,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001122 int bflags, int scsi_level, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 unsigned int sparse_lun, lun, max_dev_lun;
1125 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1126
1127 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1128 "%s\n", starget->dev.bus_id));
1129
1130 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1131 /*
1132 * If this device is known to support sparse multiple units,
1133 * override the other settings, and scan all of them. Normally,
1134 * SCSI-3 devices should be scanned via the REPORT LUNS.
1135 */
1136 if (bflags & BLIST_SPARSELUN) {
1137 max_dev_lun = shost->max_lun;
1138 sparse_lun = 1;
1139 } else
1140 sparse_lun = 0;
1141
1142 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * If less than SCSI_1_CSS, and no special lun scaning, stop
1144 * scanning; this matches 2.4 behaviour, but could just be a bug
1145 * (to continue scanning a SCSI_1_CSS device).
1146 *
1147 * This test is broken. We might not have any device on lun0 for
1148 * a sparselun device, and if that's the case then how would we
1149 * know the real scsi_level, eh? It might make sense to just not
1150 * scan any SCSI_1 device for non-0 luns, but that check would best
1151 * go into scsi_alloc_sdev() and just have it return null when asked
1152 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1153 *
1154 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1155 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1156 == 0))
1157 return;
1158 */
1159 /*
1160 * If this device is known to support multiple units, override
1161 * the other settings, and scan all of them.
1162 */
1163 if (bflags & BLIST_FORCELUN)
1164 max_dev_lun = shost->max_lun;
1165 /*
1166 * REGAL CDC-4X: avoid hang after LUN 4
1167 */
1168 if (bflags & BLIST_MAX5LUN)
1169 max_dev_lun = min(5U, max_dev_lun);
1170 /*
1171 * Do not scan SCSI-2 or lower device past LUN 7, unless
1172 * BLIST_LARGELUN.
1173 */
1174 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1175 max_dev_lun = min(8U, max_dev_lun);
1176
1177 /*
1178 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1179 * until we reach the max, or no LUN is found and we are not
1180 * sparse_lun.
1181 */
1182 for (lun = 1; lun < max_dev_lun; ++lun)
1183 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1184 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1185 !sparse_lun)
1186 return;
1187}
1188
1189/**
1190 * scsilun_to_int: convert a scsi_lun to an int
1191 * @scsilun: struct scsi_lun to be converted.
1192 *
1193 * Description:
1194 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1195 * integer, and return the result. The caller must check for
1196 * truncation before using this function.
1197 *
1198 * Notes:
1199 * The struct scsi_lun is assumed to be four levels, with each level
1200 * effectively containing a SCSI byte-ordered (big endian) short; the
1201 * addressing bits of each level are ignored (the highest two bits).
1202 * For a description of the LUN format, post SCSI-3 see the SCSI
1203 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1204 *
1205 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1206 * the integer: 0x0b030a04
1207 **/
1208static int scsilun_to_int(struct scsi_lun *scsilun)
1209{
1210 int i;
1211 unsigned int lun;
1212
1213 lun = 0;
1214 for (i = 0; i < sizeof(lun); i += 2)
1215 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1216 scsilun->scsi_lun[i + 1]) << (i * 8));
1217 return lun;
1218}
1219
1220/**
James.Smart@Emulex.Com2f4701d2005-07-13 22:05:03 -04001221 * int_to_scsilun: reverts an int into a scsi_lun
1222 * @int: integer to be reverted
1223 * @scsilun: struct scsi_lun to be set.
1224 *
1225 * Description:
1226 * Reverts the functionality of the scsilun_to_int, which packed
1227 * an 8-byte lun value into an int. This routine unpacks the int
1228 * back into the lun value.
1229 * Note: the scsilun_to_int() routine does not truly handle all
1230 * 8bytes of the lun value. This functions restores only as much
1231 * as was set by the routine.
1232 *
1233 * Notes:
1234 * Given an integer : 0x0b030a04, this function returns a
1235 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1236 *
1237 **/
1238void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1239{
1240 int i;
1241
1242 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1243
1244 for (i = 0; i < sizeof(lun); i += 2) {
1245 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1246 scsilun->scsi_lun[i+1] = lun & 0xFF;
1247 lun = lun >> 16;
1248 }
1249}
1250EXPORT_SYMBOL(int_to_scsilun);
1251
1252/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001254 * @sdevscan: scan the host, channel, and id of this scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 *
1256 * Description:
1257 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1258 * command, and scan the resulting list of LUNs by calling
1259 * scsi_probe_and_add_lun.
1260 *
1261 * Modifies sdevscan->lun.
1262 *
1263 * Return:
1264 * 0: scan completed (or no memory, so further scanning is futile)
1265 * 1: no report lun scan, or not configured
1266 **/
James Bottomley6f3a2022005-09-22 20:33:28 -05001267static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int rescan)
1269{
1270 char devname[64];
1271 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1272 unsigned int length;
1273 unsigned int lun;
1274 unsigned int num_luns;
1275 unsigned int retries;
James Bottomley39216032005-06-15 18:48:29 -05001276 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct scsi_lun *lunp, *lun_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 u8 *data;
1279 struct scsi_sense_hdr sshdr;
James Bottomley6f3a2022005-09-22 20:33:28 -05001280 struct scsi_device *sdev;
1281 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
Alan Stern2ef89192005-11-08 15:51:55 -05001282 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /*
1285 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1286 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1287 * support more than 8 LUNs.
1288 */
James Bottomley4d7db042006-03-31 20:07:45 -06001289 if (bflags & BLIST_NOREPORTLUN)
1290 return 1;
1291 if (starget->scsi_level < SCSI_2 &&
1292 starget->scsi_level != SCSI_UNKNOWN)
1293 return 1;
1294 if (starget->scsi_level < SCSI_3 &&
1295 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 1;
1297 if (bflags & BLIST_NOLUN)
1298 return 0;
1299
James Bottomley6f3a2022005-09-22 20:33:28 -05001300 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1301 sdev = scsi_alloc_sdev(starget, 0, NULL);
1302 if (!sdev)
1303 return 0;
1304 if (scsi_device_get(sdev))
1305 return 0;
1306 }
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 sprintf(devname, "host %d channel %d id %d",
James Bottomley6f3a2022005-09-22 20:33:28 -05001309 shost->host_no, sdev->channel, sdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /*
1312 * Allocate enough to hold the header (the same size as one scsi_lun)
1313 * plus the max number of luns we are requesting.
1314 *
1315 * Reallocating and trying again (with the exact amount we need)
1316 * would be nice, but then we need to somehow limit the size
1317 * allocated based on the available memory and the limits of
1318 * kmalloc - we don't want a kmalloc() failure of a huge value to
1319 * prevent us from finding any LUNs on this target.
1320 */
1321 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1322 lun_data = kmalloc(length, GFP_ATOMIC |
1323 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
James Bottomley6f3a2022005-09-22 20:33:28 -05001324 if (!lun_data) {
1325 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
James Bottomley39216032005-06-15 18:48:29 -05001326 goto out;
James Bottomley6f3a2022005-09-22 20:33:28 -05001327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 scsi_cmd[0] = REPORT_LUNS;
1330
1331 /*
1332 * bytes 1 - 5: reserved, set to zero.
1333 */
1334 memset(&scsi_cmd[1], 0, 5);
1335
1336 /*
1337 * bytes 6 - 9: length of the command.
1338 */
1339 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1340 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1341 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1342 scsi_cmd[9] = (unsigned char) length & 0xff;
1343
1344 scsi_cmd[10] = 0; /* reserved */
1345 scsi_cmd[11] = 0; /* control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /*
1348 * We can get a UNIT ATTENTION, for example a power on/reset, so
1349 * retry a few times (like sd.c does for TEST UNIT READY).
1350 * Experience shows some combinations of adapter/devices get at
1351 * least two power on/resets.
1352 *
1353 * Illegal requests (for devices that do not support REPORT LUNS)
1354 * should come through as a check condition, and will not generate
1355 * a retry.
1356 */
1357 for (retries = 0; retries < 3; retries++) {
1358 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1359 " REPORT LUNS to %s (try %d)\n", devname,
1360 retries));
James Bottomley39216032005-06-15 18:48:29 -05001361
James Bottomley39216032005-06-15 18:48:29 -05001362 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001363 lun_data, length, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -05001364 SCSI_TIMEOUT + 4 * HZ, 3);
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
James Bottomley39216032005-06-15 18:48:29 -05001367 " %s (try %d) result 0x%x\n", result
1368 ? "failed" : "successful", retries, result));
1369 if (result == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 break;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001371 else if (scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (sshdr.sense_key != UNIT_ATTENTION)
1373 break;
1374 }
1375 }
1376
James Bottomley39216032005-06-15 18:48:29 -05001377 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /*
1379 * The device probably does not support a REPORT LUN command
1380 */
Alan Stern2ef89192005-11-08 15:51:55 -05001381 ret = 1;
1382 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /*
1386 * Get the length from the first four bytes of lun_data.
1387 */
1388 data = (u8 *) lun_data->scsi_lun;
1389 length = ((data[0] << 24) | (data[1] << 16) |
1390 (data[2] << 8) | (data[3] << 0));
1391
1392 num_luns = (length / sizeof(struct scsi_lun));
1393 if (num_luns > max_scsi_report_luns) {
1394 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1395 " of %d luns reported, try increasing"
1396 " max_scsi_report_luns.\n", devname,
1397 max_scsi_report_luns, num_luns);
1398 num_luns = max_scsi_report_luns;
1399 }
1400
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001401 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1402 "scsi scan: REPORT LUN scan\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /*
1405 * Scan the luns in lun_data. The entry at offset 0 is really
1406 * the header, so start at 1 and go up to and including num_luns.
1407 */
1408 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1409 lun = scsilun_to_int(lunp);
1410
1411 /*
1412 * Check if the unused part of lunp is non-zero, and so
1413 * does not fit in lun.
1414 */
1415 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1416 int i;
1417
1418 /*
1419 * Output an error displaying the LUN in byte order,
1420 * this differs from what linux would print for the
1421 * integer LUN value.
1422 */
1423 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1424 data = (char *)lunp->scsi_lun;
1425 for (i = 0; i < sizeof(struct scsi_lun); i++)
1426 printk("%02x", data[i]);
1427 printk(" has a LUN larger than currently supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 } else if (lun > sdev->host->max_lun) {
1429 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1430 " than allowed by the host adapter\n",
1431 devname, lun);
1432 } else {
1433 int res;
1434
1435 res = scsi_probe_and_add_lun(starget,
1436 lun, NULL, NULL, rescan, NULL);
1437 if (res == SCSI_SCAN_NO_RESPONSE) {
1438 /*
1439 * Got some results, but now none, abort.
1440 */
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001441 sdev_printk(KERN_ERR, sdev,
1442 "Unexpected response"
1443 " from lun %d while scanning, scan"
1444 " aborted\n", lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
1446 }
1447 }
1448 }
1449
Alan Stern2ef89192005-11-08 15:51:55 -05001450 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 kfree(lun_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 out:
James Bottomley6f3a2022005-09-22 20:33:28 -05001453 scsi_device_put(sdev);
1454 if (sdev->sdev_state == SDEV_CREATED)
1455 /*
1456 * the sdev we used didn't appear in the report luns scan
1457 */
1458 scsi_destroy_sdev(sdev);
Alan Stern2ef89192005-11-08 15:51:55 -05001459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1463 uint id, uint lun, void *hostdata)
1464{
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001465 struct scsi_device *sdev = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct device *parent = &shost->shost_gendev;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001467 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Matthew Wilcox938e2ac2007-01-15 18:07:09 -07001469 if (strncmp(scsi_scan_type, "none", 4) == 0)
1470 return ERR_PTR(-ENODEV);
1471
1472 if (!shost->async_scan)
1473 scsi_complete_async_scans();
1474
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001475 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (!starget)
1477 return ERR_PTR(-ENOMEM);
1478
Arjan van de Ven0b950672006-01-11 13:16:10 +01001479 mutex_lock(&shost->scan_mutex);
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001480 if (scsi_host_scan_allowed(shost))
1481 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001482 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 scsi_target_reap(starget);
1484 put_device(&starget->dev);
1485
1486 return sdev;
1487}
1488EXPORT_SYMBOL(__scsi_add_device);
1489
James Bottomley146f7262005-09-10 12:44:09 -05001490int scsi_add_device(struct Scsi_Host *host, uint channel,
1491 uint target, uint lun)
1492{
1493 struct scsi_device *sdev =
1494 __scsi_add_device(host, channel, target, lun, NULL);
1495 if (IS_ERR(sdev))
1496 return PTR_ERR(sdev);
1497
1498 scsi_device_put(sdev);
1499 return 0;
1500}
1501EXPORT_SYMBOL(scsi_add_device);
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503void scsi_rescan_device(struct device *dev)
1504{
1505 struct scsi_driver *drv;
1506
1507 if (!dev->driver)
1508 return;
1509
1510 drv = to_scsi_driver(dev->driver);
1511 if (try_module_get(drv->owner)) {
1512 if (drv->rescan)
1513 drv->rescan(dev);
1514 module_put(drv->owner);
1515 }
1516}
1517EXPORT_SYMBOL(scsi_rescan_device);
1518
Alan Sterne517d312005-07-26 10:18:45 -04001519static void __scsi_scan_target(struct device *parent, unsigned int channel,
1520 unsigned int id, unsigned int lun, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct Scsi_Host *shost = dev_to_shost(parent);
1523 int bflags = 0;
1524 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 struct scsi_target *starget;
1526
1527 if (shost->this_id == id)
1528 /*
1529 * Don't scan the host adapter
1530 */
1531 return;
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (!starget)
1535 return;
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (lun != SCAN_WILD_CARD) {
1538 /*
1539 * Scan for a specific host/chan/id/lun.
1540 */
1541 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1542 goto out_reap;
1543 }
1544
1545 /*
1546 * Scan LUN 0, if there is some response, scan further. Ideally, we
1547 * would not configure LUN 0 until all LUNs are scanned.
1548 */
James Bottomley6f3a2022005-09-22 20:33:28 -05001549 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1550 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1551 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /*
1553 * The REPORT LUN did not scan the target,
1554 * do a sequential scan.
1555 */
1556 scsi_sequential_lun_scan(starget, bflags,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001557 starget->scsi_level, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 out_reap:
1561 /* now determine if the target has any children at all
1562 * and if not, nuke it */
1563 scsi_target_reap(starget);
1564
1565 put_device(&starget->dev);
1566}
Alan Sterne517d312005-07-26 10:18:45 -04001567
1568/**
1569 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1570 * target.
1571 * @parent: host to scan
1572 * @channel: channel to scan
1573 * @id: target id to scan
1574 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1575 * @rescan: passed to LUN scanning routines
1576 *
1577 * Description:
1578 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1579 * and possibly all LUNs on the target id.
1580 *
1581 * First try a REPORT LUN scan, if that does not scan the target, do a
1582 * sequential scan of LUNs on the target id.
1583 **/
1584void scsi_scan_target(struct device *parent, unsigned int channel,
1585 unsigned int id, unsigned int lun, int rescan)
1586{
1587 struct Scsi_Host *shost = dev_to_shost(parent);
1588
Matthew Wilcox93b45af2006-11-22 13:24:53 -07001589 if (strncmp(scsi_scan_type, "none", 4) == 0)
1590 return;
1591
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001592 if (!shost->async_scan)
1593 scsi_complete_async_scans();
1594
Arjan van de Ven0b950672006-01-11 13:16:10 +01001595 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001596 if (scsi_host_scan_allowed(shost))
1597 __scsi_scan_target(parent, channel, id, lun, rescan);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001598 mutex_unlock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600EXPORT_SYMBOL(scsi_scan_target);
1601
1602static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1603 unsigned int id, unsigned int lun, int rescan)
1604{
1605 uint order_id;
1606
1607 if (id == SCAN_WILD_CARD)
1608 for (id = 0; id < shost->max_id; ++id) {
1609 /*
1610 * XXX adapter drivers when possible (FCP, iSCSI)
1611 * could modify max_id to match the current max,
1612 * not the absolute max.
1613 *
1614 * XXX add a shost id iterator, so for example,
1615 * the FC ID can be the same as a target id
1616 * without a huge overhead of sparse id's.
1617 */
1618 if (shost->reverse_ordering)
1619 /*
1620 * Scan from high to low id.
1621 */
1622 order_id = shost->max_id - id - 1;
1623 else
1624 order_id = id;
Alan Sterne517d312005-07-26 10:18:45 -04001625 __scsi_scan_target(&shost->shost_gendev, channel,
1626 order_id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628 else
Alan Sterne517d312005-07-26 10:18:45 -04001629 __scsi_scan_target(&shost->shost_gendev, channel,
1630 id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1634 unsigned int id, unsigned int lun, int rescan)
1635{
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001636 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1637 "%s: <%u:%u:%u>\n",
1638 __FUNCTION__, channel, id, lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001640 if (!shost->async_scan)
1641 scsi_complete_async_scans();
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
Amit Arora091686d2006-05-19 16:14:50 -07001644 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1646 return -EINVAL;
1647
Arjan van de Ven0b950672006-01-11 13:16:10 +01001648 mutex_lock(&shost->scan_mutex);
Mike Anderson82f29462005-06-16 11:14:33 -07001649 if (scsi_host_scan_allowed(shost)) {
1650 if (channel == SCAN_WILD_CARD)
1651 for (channel = 0; channel <= shost->max_channel;
1652 channel++)
1653 scsi_scan_channel(shost, channel, id, lun,
1654 rescan);
1655 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 scsi_scan_channel(shost, channel, id, lun, rescan);
Mike Anderson82f29462005-06-16 11:14:33 -07001657 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001658 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 return 0;
1661}
1662
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001663static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1664{
1665 struct scsi_device *sdev;
1666 shost_for_each_device(sdev, shost) {
1667 if (scsi_sysfs_add_sdev(sdev) != 0)
1668 scsi_destroy_sdev(sdev);
1669 }
1670}
1671
1672/**
1673 * scsi_prep_async_scan - prepare for an async scan
1674 * @shost: the host which will be scanned
1675 * Returns: a cookie to be passed to scsi_finish_async_scan()
1676 *
1677 * Tells the midlayer this host is going to do an asynchronous scan.
1678 * It reserves the host's position in the scanning list and ensures
1679 * that other asynchronous scans started after this one won't affect the
1680 * ordering of the discovered devices.
1681 */
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001682static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001683{
1684 struct async_scan_data *data;
1685
1686 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1687 return NULL;
1688
1689 if (shost->async_scan) {
1690 printk("%s called twice for host %d", __FUNCTION__,
1691 shost->host_no);
1692 dump_stack();
1693 return NULL;
1694 }
1695
1696 data = kmalloc(sizeof(*data), GFP_KERNEL);
1697 if (!data)
1698 goto err;
1699 data->shost = scsi_host_get(shost);
1700 if (!data->shost)
1701 goto err;
1702 init_completion(&data->prev_finished);
1703
1704 spin_lock(&async_scan_lock);
1705 shost->async_scan = 1;
1706 if (list_empty(&scanning_hosts))
1707 complete(&data->prev_finished);
1708 list_add_tail(&data->list, &scanning_hosts);
1709 spin_unlock(&async_scan_lock);
1710
1711 return data;
1712
1713 err:
1714 kfree(data);
1715 return NULL;
1716}
1717
1718/**
1719 * scsi_finish_async_scan - asynchronous scan has finished
1720 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1721 *
1722 * All the devices currently attached to this host have been found.
1723 * This function announces all the devices it has found to the rest
1724 * of the system.
1725 */
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001726static void scsi_finish_async_scan(struct async_scan_data *data)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001727{
1728 struct Scsi_Host *shost;
1729
1730 if (!data)
1731 return;
1732
1733 shost = data->shost;
1734 if (!shost->async_scan) {
1735 printk("%s called twice for host %d", __FUNCTION__,
1736 shost->host_no);
1737 dump_stack();
1738 return;
1739 }
1740
1741 wait_for_completion(&data->prev_finished);
1742
1743 scsi_sysfs_add_devices(shost);
1744
1745 spin_lock(&async_scan_lock);
1746 shost->async_scan = 0;
1747 list_del(&data->list);
1748 if (!list_empty(&scanning_hosts)) {
1749 struct async_scan_data *next = list_entry(scanning_hosts.next,
1750 struct async_scan_data, list);
1751 complete(&next->prev_finished);
1752 }
1753 spin_unlock(&async_scan_lock);
1754
1755 scsi_host_put(shost);
1756 kfree(data);
1757}
1758
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001759static void do_scsi_scan_host(struct Scsi_Host *shost)
1760{
1761 if (shost->hostt->scan_finished) {
1762 unsigned long start = jiffies;
1763 if (shost->hostt->scan_start)
1764 shost->hostt->scan_start(shost);
1765
1766 while (!shost->hostt->scan_finished(shost, jiffies - start))
1767 msleep(10);
1768 } else {
1769 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1770 SCAN_WILD_CARD, 0);
1771 }
1772}
1773
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001774static int do_scan_async(void *_data)
1775{
1776 struct async_scan_data *data = _data;
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001777 do_scsi_scan_host(data->shost);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001778 scsi_finish_async_scan(data);
1779 return 0;
1780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/**
1783 * scsi_scan_host - scan the given adapter
1784 * @shost: adapter to scan
1785 **/
1786void scsi_scan_host(struct Scsi_Host *shost)
1787{
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001788 struct async_scan_data *data;
1789
1790 if (strncmp(scsi_scan_type, "none", 4) == 0)
1791 return;
1792
1793 data = scsi_prep_async_scan(shost);
1794 if (!data) {
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001795 do_scsi_scan_host(shost);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001796 return;
1797 }
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001798
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001799 kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801EXPORT_SYMBOL(scsi_scan_host);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803void scsi_forget_host(struct Scsi_Host *shost)
1804{
Alan Sterna64358d2005-07-26 10:27:10 -04001805 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 unsigned long flags;
1807
Alan Sterna64358d2005-07-26 10:27:10 -04001808 restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001810 list_for_each_entry(sdev, &shost->__devices, siblings) {
1811 if (sdev->sdev_state == SDEV_DEL)
1812 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 spin_unlock_irqrestore(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001814 __scsi_remove_device(sdev);
1815 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 spin_unlock_irqrestore(shost->host_lock, flags);
1818}
1819
1820/*
1821 * Function: scsi_get_host_dev()
1822 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001823 * Purpose: Create a scsi_device that points to the host adapter itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001825 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 *
1827 * Lock status: None assumed.
1828 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001829 * Returns: The scsi_device or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 *
1831 * Notes:
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001832 * Attach a single scsi_device to the Scsi_Host - this should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 * be made to look like a "pseudo-device" that points to the
1834 * HA itself.
1835 *
1836 * Note - this device is not accessible from any high-level
1837 * drivers (including generics), which is probably not
1838 * optimal. We can add hooks later to attach
1839 */
1840struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1841{
Alan Sterne517d312005-07-26 10:18:45 -04001842 struct scsi_device *sdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 struct scsi_target *starget;
1844
Arjan van de Ven0b950672006-01-11 13:16:10 +01001845 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001846 if (!scsi_host_scan_allowed(shost))
1847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1849 if (!starget)
Alan Sterne517d312005-07-26 10:18:45 -04001850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 sdev = scsi_alloc_sdev(starget, 0, NULL);
1853 if (sdev) {
1854 sdev->sdev_gendev.parent = get_device(&starget->dev);
1855 sdev->borken = 0;
James Bottomley884d25c2006-09-05 16:26:41 -05001856 } else
1857 scsi_target_reap(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 put_device(&starget->dev);
Alan Sterne517d312005-07-26 10:18:45 -04001859 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +01001860 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return sdev;
1862}
1863EXPORT_SYMBOL(scsi_get_host_dev);
1864
1865/*
1866 * Function: scsi_free_host_dev()
1867 *
1868 * Purpose: Free a scsi_device that points to the host adapter itself.
1869 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001870 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 *
1872 * Lock status: None assumed.
1873 *
1874 * Returns: Nothing
1875 *
1876 * Notes:
1877 */
1878void scsi_free_host_dev(struct scsi_device *sdev)
1879{
1880 BUG_ON(sdev->id != sdev->host->this_id);
1881
James Bottomley6f3a2022005-09-22 20:33:28 -05001882 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884EXPORT_SYMBOL(scsi_free_host_dev);
1885