blob: 0827df2ebb6ddd1d8a622baa554971f0046d0312 [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/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +010057 * Prefix values for the SCSI id's (stored in sysfs name field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
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
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600184/* Only exported for the benefit of scsi_wait_scan */
185EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/**
188 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
James Bottomley39216032005-06-15 18:48:29 -0500189 * @sdev: scsi device to send command to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * @result: area to store the result of the MODE SENSE
191 *
192 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500193 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * Called for BLIST_KEY devices.
195 **/
James Bottomley39216032005-06-15 18:48:29 -0500196static void scsi_unlock_floptical(struct scsi_device *sdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned char *result)
198{
199 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
200
201 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
202 scsi_cmd[0] = MODE_SENSE;
203 scsi_cmd[1] = 0;
204 scsi_cmd[2] = 0x2e;
205 scsi_cmd[3] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500206 scsi_cmd[4] = 0x2a; /* size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 scsi_cmd[5] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500208 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
209 SCSI_TIMEOUT, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * scsi_alloc_sdev - allocate and setup a scsi_Device
214 *
215 * Description:
216 * Allocate, initialize for io, and return a pointer to a scsi_Device.
217 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
218 * adds scsi_Device to the appropriate list.
219 *
220 * Return value:
221 * scsi_Device pointer, or NULL on failure.
222 **/
223static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
224 unsigned int lun, void *hostdata)
225{
226 struct scsi_device *sdev;
227 int display_failure_msg = 1, ret;
228 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
229
Jes Sorensen24669f752006-01-16 10:31:18 -0500230 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 GFP_ATOMIC);
232 if (!sdev)
233 goto out;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 sdev->vendor = scsi_null_device_strs;
236 sdev->model = scsi_null_device_strs;
237 sdev->rev = scsi_null_device_strs;
238 sdev->host = shost;
239 sdev->id = starget->id;
240 sdev->lun = lun;
241 sdev->channel = starget->channel;
242 sdev->sdev_state = SDEV_CREATED;
243 INIT_LIST_HEAD(&sdev->siblings);
244 INIT_LIST_HEAD(&sdev->same_target_siblings);
245 INIT_LIST_HEAD(&sdev->cmd_list);
246 INIT_LIST_HEAD(&sdev->starved_entry);
247 spin_lock_init(&sdev->list_lock);
248
249 sdev->sdev_gendev.parent = get_device(&starget->dev);
250 sdev->sdev_target = starget;
251
252 /* usually NULL and set by ->slave_alloc instead */
253 sdev->hostdata = hostdata;
254
255 /* if the device needs this changing, it may do so in the
256 * slave_configure function */
257 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
258
259 /*
260 * Some low level driver could use device->type
261 */
262 sdev->type = -1;
263
264 /*
265 * Assume that the device will have handshaking problems,
266 * and then fix this field later if it turns out it
267 * doesn't
268 */
269 sdev->borken = 1;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 sdev->request_queue = scsi_alloc_queue(sdev);
272 if (!sdev->request_queue) {
273 /* release fn is set up in scsi_sysfs_device_initialise, so
274 * have to free and put manually here */
275 put_device(&starget->dev);
Dave Jones93f560892006-03-09 10:21:27 -0500276 kfree(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto out;
278 }
279
280 sdev->request_queue->queuedata = sdev;
281 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
282
283 scsi_sysfs_device_initialize(sdev);
284
285 if (shost->hostt->slave_alloc) {
286 ret = shost->hostt->slave_alloc(sdev);
287 if (ret) {
288 /*
289 * if LLDD reports slave not present, don't clutter
290 * console with alloc failure messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292 if (ret == -ENXIO)
293 display_failure_msg = 0;
294 goto out_device_destroy;
295 }
296 }
297
298 return sdev;
299
300out_device_destroy:
301 transport_destroy_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 put_device(&sdev->sdev_gendev);
303out:
304 if (display_failure_msg)
305 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
306 return NULL;
307}
308
309static void scsi_target_dev_release(struct device *dev)
310{
311 struct device *parent = dev->parent;
312 struct scsi_target *starget = to_scsi_target(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 kfree(starget);
315 put_device(parent);
316}
317
318int scsi_is_target_device(const struct device *dev)
319{
320 return dev->release == scsi_target_dev_release;
321}
322EXPORT_SYMBOL(scsi_is_target_device);
323
324static struct scsi_target *__scsi_find_target(struct device *parent,
325 int channel, uint id)
326{
327 struct scsi_target *starget, *found_starget = NULL;
328 struct Scsi_Host *shost = dev_to_shost(parent);
329 /*
330 * Search for an existing target for this sdev.
331 */
332 list_for_each_entry(starget, &shost->__targets, siblings) {
333 if (starget->id == id &&
334 starget->channel == channel) {
335 found_starget = starget;
336 break;
337 }
338 }
339 if (found_starget)
340 get_device(&found_starget->dev);
341
342 return found_starget;
343}
344
James Bottomley884d25c2006-09-05 16:26:41 -0500345/**
346 * scsi_alloc_target - allocate a new or find an existing target
347 * @parent: parent of the target (need not be a scsi host)
348 * @channel: target channel number (zero if no channels)
349 * @id: target id number
350 *
351 * Return an existing target if one exists, provided it hasn't already
352 * gone into STARGET_DEL state, otherwise allocate a new target.
353 *
354 * The target is returned with an incremented reference, so the caller
355 * is responsible for both reaping and doing a last put
356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static struct scsi_target *scsi_alloc_target(struct device *parent,
358 int channel, uint id)
359{
360 struct Scsi_Host *shost = dev_to_shost(parent);
361 struct device *dev = NULL;
362 unsigned long flags;
363 const int size = sizeof(struct scsi_target)
364 + shost->transportt->target_size;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -0400365 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct scsi_target *found_target;
Brian King32f95792006-02-22 14:28:24 -0600367 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Jes Sorensen24669f752006-01-16 10:31:18 -0500369 starget = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!starget) {
371 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
372 return NULL;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 dev = &starget->dev;
375 device_initialize(dev);
376 starget->reap_ref = 1;
377 dev->parent = get_device(parent);
378 dev->release = scsi_target_dev_release;
379 sprintf(dev->bus_id, "target%d:%d:%d",
380 shost->host_no, channel, id);
381 starget->id = id;
382 starget->channel = channel;
383 INIT_LIST_HEAD(&starget->siblings);
384 INIT_LIST_HEAD(&starget->devices);
James Bottomleyffedb452006-02-23 14:27:18 -0600385 starget->state = STARGET_RUNNING;
Alan Stern7c9d6f12007-01-08 11:12:32 -0500386 starget->scsi_level = SCSI_2;
James Bottomleyffedb452006-02-23 14:27:18 -0600387 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_lock_irqsave(shost->host_lock, flags);
389
390 found_target = __scsi_find_target(parent, channel, id);
391 if (found_target)
392 goto found;
393
394 list_add_tail(&starget->siblings, &shost->__targets);
395 spin_unlock_irqrestore(shost->host_lock, flags);
396 /* allocate and add */
James Bottomleya283bd32005-05-24 12:06:38 -0500397 transport_setup_device(dev);
Brian King32f95792006-02-22 14:28:24 -0600398 error = device_add(dev);
399 if (error) {
400 dev_err(dev, "target device_add failed, error %d\n", error);
401 spin_lock_irqsave(shost->host_lock, flags);
402 list_del_init(&starget->siblings);
403 spin_unlock_irqrestore(shost->host_lock, flags);
404 transport_destroy_device(dev);
405 put_device(parent);
406 kfree(starget);
407 return NULL;
408 }
James Bottomleya283bd32005-05-24 12:06:38 -0500409 transport_add_device(dev);
410 if (shost->hostt->target_alloc) {
Brian King32f95792006-02-22 14:28:24 -0600411 error = shost->hostt->target_alloc(starget);
James Bottomleya283bd32005-05-24 12:06:38 -0500412
413 if(error) {
414 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
415 /* don't want scsi_target_reap to do the final
416 * put because it will be under the host lock */
417 get_device(dev);
418 scsi_target_reap(starget);
419 put_device(dev);
420 return NULL;
421 }
422 }
James Bottomley884d25c2006-09-05 16:26:41 -0500423 get_device(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return starget;
426
427 found:
428 found_target->reap_ref++;
429 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600430 if (found_target->state != STARGET_DEL) {
James Bottomley884d25c2006-09-05 16:26:41 -0500431 put_device(parent);
James Bottomleyffedb452006-02-23 14:27:18 -0600432 kfree(starget);
433 return found_target;
434 }
435 /* Unfortunately, we found a dying target; need to
436 * wait until it's dead before we can get a new one */
437 put_device(&found_target->dev);
438 flush_scheduled_work();
439 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
David Howells65f27f32006-11-22 14:55:48 +0000442static void scsi_target_reap_usercontext(struct work_struct *work)
James Bottomley65110b22006-02-14 10:48:46 -0600443{
David Howells65f27f32006-11-22 14:55:48 +0000444 struct scsi_target *starget =
445 container_of(work, struct scsi_target, ew.work);
James Bottomley863a9302005-12-15 20:01:43 -0800446 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
447 unsigned long flags;
448
James Bottomleyffedb452006-02-23 14:27:18 -0600449 transport_remove_device(&starget->dev);
450 device_del(&starget->dev);
451 transport_destroy_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800452 spin_lock_irqsave(shost->host_lock, flags);
Mike Andersona50a5e32006-03-14 11:18:46 -0800453 if (shost->hostt->target_destroy)
454 shost->hostt->target_destroy(starget);
James Bottomleyffedb452006-02-23 14:27:18 -0600455 list_del_init(&starget->siblings);
James Bottomley863a9302005-12-15 20:01:43 -0800456 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600457 put_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/**
461 * scsi_target_reap - check to see if target is in use and destroy if not
462 *
463 * @starget: target to be checked
464 *
465 * This is used after removing a LUN or doing a last put of the target
466 * it checks atomically that nothing is using the target and removes
467 * it if so.
468 */
469void scsi_target_reap(struct scsi_target *starget)
470{
James Bottomleyffedb452006-02-23 14:27:18 -0600471 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
472 unsigned long flags;
473
474 spin_lock_irqsave(shost->host_lock, flags);
475
476 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
477 BUG_ON(starget->state == STARGET_DEL);
478 starget->state = STARGET_DEL;
479 spin_unlock_irqrestore(shost->host_lock, flags);
480 execute_in_process_context(scsi_target_reap_usercontext,
David Howells65f27f32006-11-22 14:55:48 +0000481 &starget->ew);
James Bottomleyffedb452006-02-23 14:27:18 -0600482 return;
483
484 }
485 spin_unlock_irqrestore(shost->host_lock, flags);
486
487 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/**
Alan Sterne5b3cd42006-08-21 15:53:25 -0400491 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
492 * @s: INQUIRY result string to sanitize
493 * @len: length of the string
494 *
495 * Description:
496 * The SCSI spec says that INQUIRY vendor, product, and revision
497 * strings must consist entirely of graphic ASCII characters,
498 * padded on the right with spaces. Since not all devices obey
499 * this rule, we will replace non-graphic or non-ASCII characters
500 * with spaces. Exception: a NUL character is interpreted as a
501 * string terminator, so all the following characters are set to
502 * spaces.
503 **/
504static void sanitize_inquiry_string(unsigned char *s, int len)
505{
506 int terminated = 0;
507
508 for (; len > 0; (--len, ++s)) {
509 if (*s == 0)
510 terminated = 1;
511 if (terminated || *s < 0x20 || *s > 0x7e)
512 *s = ' ';
513 }
514}
515
516/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
James Bottomley39216032005-06-15 18:48:29 -0500518 * @sdev: scsi_device to probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * @inq_result: area to store the INQUIRY result
James Bottomley39216032005-06-15 18:48:29 -0500520 * @result_len: len of inq_result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * @bflags: store any bflags found here
522 *
523 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500524 * Probe the lun associated with @req using a standard SCSI INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 *
James Bottomley39216032005-06-15 18:48:29 -0500526 * If the INQUIRY is successful, zero is returned and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100528 * are copied to the scsi_device any flags value is stored in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400530static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
James Bottomley39216032005-06-15 18:48:29 -0500531 int result_len, int *bflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
534 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
535 int response_len = 0;
James Bottomley39216032005-06-15 18:48:29 -0500536 int pass, count, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct scsi_sense_hdr sshdr;
538
539 *bflags = 0;
540
541 /* Perform up to 3 passes. The first pass uses a conservative
542 * transfer length of 36 unless sdev->inquiry_len specifies a
543 * different value. */
544 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
545 try_inquiry_len = first_inquiry_len;
546 pass = 1;
547
548 next_pass:
James Bottomley9ccfc752005-10-02 11:45:08 -0500549 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
550 "scsi scan: INQUIRY pass %d length %d\n",
551 pass, try_inquiry_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* Each pass gets up to three chances to ignore Unit Attention */
554 for (count = 0; count < 3; ++count) {
555 memset(scsi_cmd, 0, 6);
556 scsi_cmd[0] = INQUIRY;
557 scsi_cmd[4] = (unsigned char) try_inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 memset(inq_result, 0, try_inquiry_len);
James Bottomley39216032005-06-15 18:48:29 -0500560
561 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500562 inq_result, try_inquiry_len, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -0500563 HZ / 2 + HZ * scsi_inq_timeout, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
566 "with code 0x%x\n",
James Bottomley39216032005-06-15 18:48:29 -0500567 result ? "failed" : "successful", result));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
James Bottomley39216032005-06-15 18:48:29 -0500569 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
571 * not-ready to ready transition [asc/ascq=0x28/0x0]
572 * or power-on, reset [asc/ascq=0x29/0x0], continue.
573 * INQUIRY should not yield UNIT_ATTENTION
574 * but many buggy devices do so anyway.
575 */
James Bottomley39216032005-06-15 18:48:29 -0500576 if ((driver_byte(result) & DRIVER_SENSE) &&
James Bottomleyea73a9f2005-08-28 11:33:52 -0500577 scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if ((sshdr.sense_key == UNIT_ATTENTION) &&
579 ((sshdr.asc == 0x28) ||
580 (sshdr.asc == 0x29)) &&
581 (sshdr.ascq == 0))
582 continue;
583 }
584 }
585 break;
586 }
587
James Bottomley39216032005-06-15 18:48:29 -0500588 if (result == 0) {
Alan Sterne5b3cd42006-08-21 15:53:25 -0400589 sanitize_inquiry_string(&inq_result[8], 8);
590 sanitize_inquiry_string(&inq_result[16], 16);
591 sanitize_inquiry_string(&inq_result[32], 4);
592
593 response_len = inq_result[4] + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (response_len > 255)
595 response_len = first_inquiry_len; /* sanity */
596
597 /*
598 * Get any flags for this device.
599 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100600 * XXX add a bflags to scsi_device, and replace the
601 * corresponding bit fields in scsi_device, so bflags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * need not be passed as an argument.
603 */
604 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
605 &inq_result[16]);
606
607 /* When the first pass succeeds we gain information about
608 * what larger transfer lengths might work. */
609 if (pass == 1) {
610 if (BLIST_INQUIRY_36 & *bflags)
611 next_inquiry_len = 36;
612 else if (BLIST_INQUIRY_58 & *bflags)
613 next_inquiry_len = 58;
614 else if (sdev->inquiry_len)
615 next_inquiry_len = sdev->inquiry_len;
616 else
617 next_inquiry_len = response_len;
618
619 /* If more data is available perform the second pass */
620 if (next_inquiry_len > try_inquiry_len) {
621 try_inquiry_len = next_inquiry_len;
622 pass = 2;
623 goto next_pass;
624 }
625 }
626
627 } else if (pass == 2) {
628 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
629 "Consider BLIST_INQUIRY_36 for this device\n",
630 try_inquiry_len);
631
632 /* If this pass failed, the third pass goes back and transfers
633 * the same amount as we successfully got in the first pass. */
634 try_inquiry_len = first_inquiry_len;
635 pass = 3;
636 goto next_pass;
637 }
638
639 /* If the last transfer attempt got an error, assume the
640 * peripheral doesn't exist or is dead. */
James Bottomley39216032005-06-15 18:48:29 -0500641 if (result)
642 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Don't report any more data than the device says is valid */
645 sdev->inquiry_len = min(try_inquiry_len, response_len);
646
647 /*
648 * XXX Abort if the response length is less than 36? If less than
649 * 32, the lookup of the device flags (above) could be invalid,
650 * and it would be possible to take an incorrect action - we do
651 * not want to hang because of a short INQUIRY. On the flip side,
652 * if the device is spun down or becoming ready (and so it gives a
653 * short INQUIRY), an abort here prevents any further use of the
654 * device, including spin up.
655 *
Alan Sterne423ee32007-02-16 01:46:38 -0800656 * On the whole, the best approach seems to be to assume the first
657 * 36 bytes are valid no matter what the device says. That's
658 * better than copying < 36 bytes to the inquiry-result buffer
659 * and displaying garbage for the Vendor, Product, or Revision
660 * strings.
661 */
662 if (sdev->inquiry_len < 36) {
663 printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
664 " using 36\n", sdev->inquiry_len);
665 sdev->inquiry_len = 36;
666 }
667
668 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * Related to the above issue:
670 *
671 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
672 * and if not ready, sent a START_STOP to start (maybe spin up) and
673 * then send the INQUIRY again, since the INQUIRY can change after
674 * a device is initialized.
675 *
676 * Ideally, start a device if explicitly asked to do so. This
677 * assumes that a device is spun up on power on, spun down on
678 * request, and then spun up on request.
679 */
680
681 /*
682 * The scanning code needs to know the scsi_level, even if no
683 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
684 * non-zero LUNs can be scanned.
685 */
686 sdev->scsi_level = inq_result[2] & 0x07;
687 if (sdev->scsi_level >= 2 ||
688 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
689 sdev->scsi_level++;
James Bottomley6f3a2022005-09-22 20:33:28 -0500690 sdev->sdev_target->scsi_level = sdev->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
James Bottomley39216032005-06-15 18:48:29 -0500692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695/**
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100696 * scsi_add_lun - allocate and fully initialze a scsi_device
Matthew Wilcox6d877682007-07-11 12:54:55 -0600697 * @sdev: holds information to be stored in the new scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * @inq_result: holds the result of a previous INQUIRY to the LUN
699 * @bflags: black/white list flag
Matthew Wilcox6d877682007-07-11 12:54:55 -0600700 * @async: 1 if this device is being scanned asynchronously
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 *
702 * Description:
Matthew Wilcox6d877682007-07-11 12:54:55 -0600703 * Initialize the scsi_device @sdev. Optionally set fields based
704 * on values in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 *
706 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100707 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
708 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400710static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600711 int *bflags, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 /*
714 * XXX do not save the inquiry, since it can change underneath us,
715 * save just vendor/model/rev.
716 *
717 * Rather than save it and have an ioctl that retrieves the saved
718 * value, have an ioctl that executes the same INQUIRY code used
719 * in scsi_probe_lun, let user level programs doing INQUIRY
720 * scanning run at their own risk, or supply a user level program
721 * that can correctly scan.
722 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Alan Stern09123d22006-11-10 12:27:57 -0800724 /*
725 * Copy at least 36 bytes of INQUIRY data, so that we don't
726 * dereference unallocated memory when accessing the Vendor,
727 * Product, and Revision strings. Badly behaved devices may set
728 * the INQUIRY Additional Length byte to a small value, indicating
729 * these strings are invalid, but often they contain plausible data
730 * nonetheless. It doesn't matter if the device sent < 36 bytes
731 * total, since scsi_probe_lun() initializes inq_result with 0s.
732 */
733 sdev->inquiry = kmemdup(inq_result,
734 max_t(size_t, sdev->inquiry_len, 36),
735 GFP_ATOMIC);
736 if (sdev->inquiry == NULL)
737 return SCSI_SCAN_NO_RESPONSE;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 sdev->vendor = (char *) (sdev->inquiry + 8);
740 sdev->model = (char *) (sdev->inquiry + 16);
741 sdev->rev = (char *) (sdev->inquiry + 32);
742
743 if (*bflags & BLIST_ISROM) {
Matthew Wilcox6d877682007-07-11 12:54:55 -0600744 sdev->type = TYPE_ROM;
745 sdev->removable = 1;
746 } else {
747 sdev->type = (inq_result[0] & 0x1f);
748 sdev->removable = (inq_result[1] & 0x80) >> 7;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Matthew Wilcox6d877682007-07-11 12:54:55 -0600751 switch (sdev->type) {
James Bottomleyddaf6fc2006-12-13 10:10:40 -0600752 case TYPE_RBC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 case TYPE_TAPE:
754 case TYPE_DISK:
755 case TYPE_PRINTER:
756 case TYPE_MOD:
757 case TYPE_PROCESSOR:
758 case TYPE_SCANNER:
759 case TYPE_MEDIUM_CHANGER:
760 case TYPE_ENCLOSURE:
761 case TYPE_COMM:
James Bottomley4d7db042006-03-31 20:07:45 -0600762 case TYPE_RAID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 sdev->writeable = 1;
764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 case TYPE_ROM:
James Bottomleyddaf6fc2006-12-13 10:10:40 -0600766 case TYPE_WORM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 sdev->writeable = 0;
768 break;
769 default:
770 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
771 }
772
Matthew Wilcox6d877682007-07-11 12:54:55 -0600773 if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
774 /* RBC and MMC devices can return SCSI-3 compliance and yet
775 * still not support REPORT LUNS, so make them act as
776 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
777 * specifically set */
778 if ((*bflags & BLIST_REPORTLUN2) == 0)
779 *bflags |= BLIST_NOREPORTLUN;
780 }
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /*
783 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
784 * spec says: The device server is capable of supporting the
785 * specified peripheral device type on this logical unit. However,
786 * the physical device is not currently connected to this logical
787 * unit.
788 *
789 * The above is vague, as it implies that we could treat 001 and
790 * 011 the same. Stay compatible with previous code, and create a
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100791 * scsi_device for a PQ of 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 *
793 * Don't set the device offline here; rather let the upper
794 * level drivers eval the PQ to decide whether they should
795 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
796 */
797
798 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 sdev->lockable = sdev->removable;
800 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
801
Matthew Wilcox6d877682007-07-11 12:54:55 -0600802 if (sdev->scsi_level >= SCSI_3 ||
803 (sdev->inquiry_len > 56 && inq_result[56] & 0x04))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 sdev->ppr = 1;
805 if (inq_result[7] & 0x60)
806 sdev->wdtr = 1;
807 if (inq_result[7] & 0x10)
808 sdev->sdtr = 1;
809
James Bottomley19ac0db2006-08-06 18:15:22 -0500810 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600811 "ANSI: %d%s\n", scsi_device_type(sdev->type),
812 sdev->vendor, sdev->model, sdev->rev,
813 sdev->inq_periph_qual, inq_result[2] & 0x07,
814 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
817 !(*bflags & BLIST_NOTQ))
818 sdev->tagged_supported = 1;
Matthew Wilcox6d877682007-07-11 12:54:55 -0600819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * Some devices (Texel CD ROM drives) have handshaking problems
822 * when used with the Seagate controllers. borken is initialized
823 * to 1, and then set it to 0 here.
824 */
825 if ((*bflags & BLIST_BORKEN) == 0)
826 sdev->borken = 0;
827
Matthew Wilcox6d877682007-07-11 12:54:55 -0600828 if (*bflags & BLIST_NO_ULD_ATTACH)
829 sdev->no_uld_attach = 1;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /*
832 * Apparently some really broken devices (contrary to the SCSI
833 * standards) need to be selected without asserting ATN
834 */
835 if (*bflags & BLIST_SELECT_NO_ATN)
836 sdev->select_no_atn = 1;
837
838 /*
James Bottomley4d7db042006-03-31 20:07:45 -0600839 * Maximum 512 sector transfer length
840 * broken RA4x00 Compaq Disk Array
841 */
842 if (*bflags & BLIST_MAX_512)
843 blk_queue_max_sectors(sdev->request_queue, 512);
844
845 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * Some devices may not want to have a start command automatically
847 * issued when a device is added.
848 */
849 if (*bflags & BLIST_NOSTARTONADD)
850 sdev->no_start_on_add = 1;
851
852 if (*bflags & BLIST_SINGLELUN)
853 sdev->single_lun = 1;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 sdev->use_10_for_rw = 1;
856
857 if (*bflags & BLIST_MS_SKIP_PAGE_08)
858 sdev->skip_ms_page_8 = 1;
859
860 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
861 sdev->skip_ms_page_3f = 1;
862
863 if (*bflags & BLIST_USE_10_BYTE_MS)
864 sdev->use_10_for_ms = 1;
865
866 /* set the device running here so that slave configure
867 * may do I/O */
868 scsi_device_set_state(sdev, SDEV_RUNNING);
869
870 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
871 sdev->use_192_bytes_for_3f = 1;
872
873 if (*bflags & BLIST_NOT_LOCKABLE)
874 sdev->lockable = 0;
875
876 if (*bflags & BLIST_RETRY_HWERROR)
877 sdev->retry_hwerror = 1;
878
879 transport_configure_device(&sdev->sdev_gendev);
880
Christoph Hellwig93805092006-02-17 12:11:29 +0100881 if (sdev->host->hostt->slave_configure) {
882 int ret = sdev->host->hostt->slave_configure(sdev);
883 if (ret) {
884 /*
885 * if LLDD reports slave not present, don't clutter
886 * console with alloc failure messages
887 */
888 if (ret != -ENXIO) {
889 sdev_printk(KERN_ERR, sdev,
890 "failed to configure device\n");
891 }
892 return SCSI_SCAN_NO_RESPONSE;
893 }
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /*
897 * Ok, the device is now all set up, we can
898 * register it and tell the rest of the kernel
899 * about it.
900 */
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600901 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
Alan Sternb24b1032005-07-27 11:43:46 -0700902 return SCSI_SCAN_NO_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return SCSI_SCAN_LUN_PRESENT;
905}
906
James Bottomley6f3a2022005-09-22 20:33:28 -0500907static inline void scsi_destroy_sdev(struct scsi_device *sdev)
908{
Brian King309bd272006-06-27 11:10:43 -0500909 scsi_device_set_state(sdev, SDEV_DEL);
James Bottomley6f3a2022005-09-22 20:33:28 -0500910 if (sdev->host->hostt->slave_destroy)
911 sdev->host->hostt->slave_destroy(sdev);
912 transport_destroy_device(&sdev->sdev_gendev);
913 put_device(&sdev->sdev_gendev);
914}
915
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700916#ifdef CONFIG_SCSI_LOGGING
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200917/**
918 * scsi_inq_str - print INQUIRY data from min to max index,
919 * strip trailing whitespace
920 * @buf: Output buffer with at least end-first+1 bytes of space
921 * @inq: Inquiry buffer (input)
922 * @first: Offset of string into inq
923 * @end: Index after last character in inq
924 */
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700925static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200926 unsigned first, unsigned end)
927{
928 unsigned term = 0, idx;
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700929
930 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
931 if (inq[idx+first] > ' ') {
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200932 buf[idx] = inq[idx+first];
933 term = idx+1;
934 } else {
935 buf[idx] = ' ';
936 }
937 }
938 buf[term] = 0;
939 return buf;
940}
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700941#endif
James Bottomley6f3a2022005-09-22 20:33:28 -0500942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943/**
944 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
945 * @starget: pointer to target device structure
946 * @lun: LUN of target device
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100947 * @sdevscan: probe the LUN corresponding to this scsi_device
948 * @sdevnew: store the value of any new scsi_device allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * @bflagsp: store bflags here if not NULL
950 *
951 * Description:
952 * Call scsi_probe_lun, if a LUN with an attached device is found,
953 * allocate and set it up by calling scsi_add_lun.
954 *
955 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100956 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
958 * attached at the LUN
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100959 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 **/
961static int scsi_probe_and_add_lun(struct scsi_target *starget,
962 uint lun, int *bflagsp,
963 struct scsi_device **sdevp, int rescan,
964 void *hostdata)
965{
966 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 unsigned char *result;
James Bottomley39216032005-06-15 18:48:29 -0500968 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
970
971 /*
972 * The rescan flag is used as an optimization, the first scan of a
973 * host adapter calls into here with rescan == 0.
974 */
James Bottomley6f3a2022005-09-22 20:33:28 -0500975 sdev = scsi_device_lookup_by_target(starget, lun);
976 if (sdev) {
977 if (rescan || sdev->sdev_state != SDEV_CREATED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
979 "scsi scan: device exists on %s\n",
980 sdev->sdev_gendev.bus_id));
981 if (sdevp)
982 *sdevp = sdev;
983 else
984 scsi_device_put(sdev);
985
986 if (bflagsp)
987 *bflagsp = scsi_get_device_flags(sdev,
988 sdev->vendor,
989 sdev->model);
990 return SCSI_SCAN_LUN_PRESENT;
991 }
James Bottomley6f3a2022005-09-22 20:33:28 -0500992 scsi_device_put(sdev);
993 } else
994 sdev = scsi_alloc_sdev(starget, lun, hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!sdev)
996 goto out;
James Bottomley39216032005-06-15 18:48:29 -0500997
998 result = kmalloc(result_len, GFP_ATOMIC |
Al Virobc861202005-04-24 12:28:34 -0700999 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!result)
James Bottomley39216032005-06-15 18:48:29 -05001001 goto out_free_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
James Bottomley39216032005-06-15 18:48:29 -05001003 if (scsi_probe_lun(sdev, result, result_len, &bflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 goto out_free_result;
1005
Kurt Garloff4186ab12006-04-03 15:16:48 +02001006 if (bflagsp)
1007 *bflagsp = bflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * result contains valid SCSI INQUIRY data.
1010 */
Kurt Garloff13f7e5a2006-04-03 15:20:08 +02001011 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * For a Peripheral qualifier 3 (011b), the SCSI
1014 * spec says: The device server is not capable of
1015 * supporting a physical device on this logical
1016 * unit.
1017 *
1018 * For disks, this implies that there is no
1019 * logical disk configured at sdev->lun, but there
1020 * is a target id responding.
1021 */
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001022 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1023 " peripheral qualifier of 3, device not"
1024 " added\n"))
1025 if (lun == 0) {
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001026 SCSI_LOG_SCAN_BUS(1, {
1027 unsigned char vend[9];
1028 unsigned char mod[17];
1029
1030 sdev_printk(KERN_INFO, sdev,
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001031 "scsi scan: consider passing scsi_mod."
Kurt Garloff3424a652007-01-09 02:28:54 +01001032 "dev_flags=%s:%s:0x240 or 0x1000240\n",
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001033 scsi_inq_str(vend, result, 8, 16),
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001034 scsi_inq_str(mod, result, 16, 32));
1035 });
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 res = SCSI_SCAN_TARGET_PRESENT;
1039 goto out_free_result;
1040 }
1041
Alan Stern1bfc5d92006-02-09 15:26:18 -05001042 /*
dave wysochanski84961f22006-08-09 14:56:32 -04001043 * Some targets may set slight variations of PQ and PDT to signal
1044 * that no LUN is present, so don't add sdev in these cases.
1045 * Two specific examples are:
1046 * 1) NetApp targets: return PQ=1, PDT=0x1f
1047 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1048 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1049 *
1050 * References:
1051 * 1) SCSI SPC-3, pp. 145-146
1052 * PQ=1: "A peripheral device having the specified peripheral
1053 * device type is not connected to this logical unit. However, the
1054 * device server is capable of supporting the specified peripheral
1055 * device type on this logical unit."
1056 * PDT=0x1f: "Unknown or no device type"
1057 * 2) USB UFI 1.0, p. 20
1058 * PDT=00h Direct-access device (floppy)
1059 * PDT=1Fh none (no FDD connected to the requested logical unit)
Alan Stern1bfc5d92006-02-09 15:26:18 -05001060 */
dave wysochanski84961f22006-08-09 14:56:32 -04001061 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1062 (result[0] & 0x1f) == 0x1f) {
Alan Stern1bfc5d92006-02-09 15:26:18 -05001063 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1064 "scsi scan: peripheral device type"
1065 " of 31, no device added\n"));
1066 res = SCSI_SCAN_TARGET_PRESENT;
1067 goto out_free_result;
1068 }
1069
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001070 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (res == SCSI_SCAN_LUN_PRESENT) {
1072 if (bflags & BLIST_KEY) {
1073 sdev->lockable = 0;
James Bottomley39216032005-06-15 18:48:29 -05001074 scsi_unlock_floptical(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 out_free_result:
1079 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 out_free_sdev:
1081 if (res == SCSI_SCAN_LUN_PRESENT) {
1082 if (sdevp) {
Alan Sternb70d37b2005-07-26 10:30:40 -04001083 if (scsi_device_get(sdev) == 0) {
1084 *sdevp = sdev;
1085 } else {
1086 __scsi_remove_device(sdev);
1087 res = SCSI_SCAN_NO_RESPONSE;
1088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
James Bottomley6f3a2022005-09-22 20:33:28 -05001090 } else
1091 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 out:
1093 return res;
1094}
1095
1096/**
1097 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1098 * @starget: pointer to target structure to scan
1099 * @bflags: black/white list flag for LUN 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 *
1101 * Description:
1102 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1103 * scanned) to some maximum lun until a LUN is found with no device
1104 * attached. Use the bflags to figure out any oddities.
1105 *
1106 * Modifies sdevscan->lun.
1107 **/
1108static void scsi_sequential_lun_scan(struct scsi_target *starget,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001109 int bflags, int scsi_level, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 unsigned int sparse_lun, lun, max_dev_lun;
1112 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1113
1114 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1115 "%s\n", starget->dev.bus_id));
1116
1117 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1118 /*
1119 * If this device is known to support sparse multiple units,
1120 * override the other settings, and scan all of them. Normally,
1121 * SCSI-3 devices should be scanned via the REPORT LUNS.
1122 */
1123 if (bflags & BLIST_SPARSELUN) {
1124 max_dev_lun = shost->max_lun;
1125 sparse_lun = 1;
1126 } else
1127 sparse_lun = 0;
1128
1129 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * If less than SCSI_1_CSS, and no special lun scaning, stop
1131 * scanning; this matches 2.4 behaviour, but could just be a bug
1132 * (to continue scanning a SCSI_1_CSS device).
1133 *
1134 * This test is broken. We might not have any device on lun0 for
1135 * a sparselun device, and if that's the case then how would we
1136 * know the real scsi_level, eh? It might make sense to just not
1137 * scan any SCSI_1 device for non-0 luns, but that check would best
1138 * go into scsi_alloc_sdev() and just have it return null when asked
1139 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1140 *
1141 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1142 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1143 == 0))
1144 return;
1145 */
1146 /*
1147 * If this device is known to support multiple units, override
1148 * the other settings, and scan all of them.
1149 */
1150 if (bflags & BLIST_FORCELUN)
1151 max_dev_lun = shost->max_lun;
1152 /*
1153 * REGAL CDC-4X: avoid hang after LUN 4
1154 */
1155 if (bflags & BLIST_MAX5LUN)
1156 max_dev_lun = min(5U, max_dev_lun);
1157 /*
1158 * Do not scan SCSI-2 or lower device past LUN 7, unless
1159 * BLIST_LARGELUN.
1160 */
1161 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1162 max_dev_lun = min(8U, max_dev_lun);
1163
1164 /*
1165 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1166 * until we reach the max, or no LUN is found and we are not
1167 * sparse_lun.
1168 */
1169 for (lun = 1; lun < max_dev_lun; ++lun)
1170 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1171 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1172 !sparse_lun)
1173 return;
1174}
1175
1176/**
1177 * scsilun_to_int: convert a scsi_lun to an int
1178 * @scsilun: struct scsi_lun to be converted.
1179 *
1180 * Description:
1181 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1182 * integer, and return the result. The caller must check for
1183 * truncation before using this function.
1184 *
1185 * Notes:
1186 * The struct scsi_lun is assumed to be four levels, with each level
1187 * effectively containing a SCSI byte-ordered (big endian) short; the
1188 * addressing bits of each level are ignored (the highest two bits).
1189 * For a description of the LUN format, post SCSI-3 see the SCSI
1190 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1191 *
1192 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1193 * the integer: 0x0b030a04
1194 **/
Christof Schmitt462b7852007-06-19 10:25:30 +02001195int scsilun_to_int(struct scsi_lun *scsilun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 int i;
1198 unsigned int lun;
1199
1200 lun = 0;
1201 for (i = 0; i < sizeof(lun); i += 2)
1202 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1203 scsilun->scsi_lun[i + 1]) << (i * 8));
1204 return lun;
1205}
Christof Schmitt462b7852007-06-19 10:25:30 +02001206EXPORT_SYMBOL(scsilun_to_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208/**
James.Smart@Emulex.Com2f4701d2005-07-13 22:05:03 -04001209 * int_to_scsilun: reverts an int into a scsi_lun
1210 * @int: integer to be reverted
1211 * @scsilun: struct scsi_lun to be set.
1212 *
1213 * Description:
1214 * Reverts the functionality of the scsilun_to_int, which packed
1215 * an 8-byte lun value into an int. This routine unpacks the int
1216 * back into the lun value.
1217 * Note: the scsilun_to_int() routine does not truly handle all
1218 * 8bytes of the lun value. This functions restores only as much
1219 * as was set by the routine.
1220 *
1221 * Notes:
1222 * Given an integer : 0x0b030a04, this function returns a
1223 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1224 *
1225 **/
1226void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1227{
1228 int i;
1229
1230 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1231
1232 for (i = 0; i < sizeof(lun); i += 2) {
1233 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1234 scsilun->scsi_lun[i+1] = lun & 0xFF;
1235 lun = lun >> 16;
1236 }
1237}
1238EXPORT_SYMBOL(int_to_scsilun);
1239
1240/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001242 * @sdevscan: scan the host, channel, and id of this scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *
1244 * Description:
1245 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1246 * command, and scan the resulting list of LUNs by calling
1247 * scsi_probe_and_add_lun.
1248 *
1249 * Modifies sdevscan->lun.
1250 *
1251 * Return:
1252 * 0: scan completed (or no memory, so further scanning is futile)
1253 * 1: no report lun scan, or not configured
1254 **/
James Bottomley6f3a2022005-09-22 20:33:28 -05001255static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 int rescan)
1257{
1258 char devname[64];
1259 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1260 unsigned int length;
1261 unsigned int lun;
1262 unsigned int num_luns;
1263 unsigned int retries;
James Bottomley39216032005-06-15 18:48:29 -05001264 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 struct scsi_lun *lunp, *lun_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 u8 *data;
1267 struct scsi_sense_hdr sshdr;
James Bottomley6f3a2022005-09-22 20:33:28 -05001268 struct scsi_device *sdev;
1269 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
Alan Stern2ef89192005-11-08 15:51:55 -05001270 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /*
1273 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1274 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1275 * support more than 8 LUNs.
1276 */
James Bottomley4d7db042006-03-31 20:07:45 -06001277 if (bflags & BLIST_NOREPORTLUN)
1278 return 1;
1279 if (starget->scsi_level < SCSI_2 &&
1280 starget->scsi_level != SCSI_UNKNOWN)
1281 return 1;
1282 if (starget->scsi_level < SCSI_3 &&
1283 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 1;
1285 if (bflags & BLIST_NOLUN)
1286 return 0;
1287
James Bottomley6f3a2022005-09-22 20:33:28 -05001288 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1289 sdev = scsi_alloc_sdev(starget, 0, NULL);
1290 if (!sdev)
1291 return 0;
1292 if (scsi_device_get(sdev))
1293 return 0;
1294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 sprintf(devname, "host %d channel %d id %d",
James Bottomley6f3a2022005-09-22 20:33:28 -05001297 shost->host_no, sdev->channel, sdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /*
1300 * Allocate enough to hold the header (the same size as one scsi_lun)
1301 * plus the max number of luns we are requesting.
1302 *
1303 * Reallocating and trying again (with the exact amount we need)
1304 * would be nice, but then we need to somehow limit the size
1305 * allocated based on the available memory and the limits of
1306 * kmalloc - we don't want a kmalloc() failure of a huge value to
1307 * prevent us from finding any LUNs on this target.
1308 */
1309 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1310 lun_data = kmalloc(length, GFP_ATOMIC |
1311 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
James Bottomley6f3a2022005-09-22 20:33:28 -05001312 if (!lun_data) {
1313 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
James Bottomley39216032005-06-15 18:48:29 -05001314 goto out;
James Bottomley6f3a2022005-09-22 20:33:28 -05001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 scsi_cmd[0] = REPORT_LUNS;
1318
1319 /*
1320 * bytes 1 - 5: reserved, set to zero.
1321 */
1322 memset(&scsi_cmd[1], 0, 5);
1323
1324 /*
1325 * bytes 6 - 9: length of the command.
1326 */
1327 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1328 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1329 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1330 scsi_cmd[9] = (unsigned char) length & 0xff;
1331
1332 scsi_cmd[10] = 0; /* reserved */
1333 scsi_cmd[11] = 0; /* control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /*
1336 * We can get a UNIT ATTENTION, for example a power on/reset, so
1337 * retry a few times (like sd.c does for TEST UNIT READY).
1338 * Experience shows some combinations of adapter/devices get at
1339 * least two power on/resets.
1340 *
1341 * Illegal requests (for devices that do not support REPORT LUNS)
1342 * should come through as a check condition, and will not generate
1343 * a retry.
1344 */
1345 for (retries = 0; retries < 3; retries++) {
1346 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1347 " REPORT LUNS to %s (try %d)\n", devname,
1348 retries));
James Bottomley39216032005-06-15 18:48:29 -05001349
James Bottomley39216032005-06-15 18:48:29 -05001350 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001351 lun_data, length, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -05001352 SCSI_TIMEOUT + 4 * HZ, 3);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
James Bottomley39216032005-06-15 18:48:29 -05001355 " %s (try %d) result 0x%x\n", result
1356 ? "failed" : "successful", retries, result));
1357 if (result == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 break;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001359 else if (scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (sshdr.sense_key != UNIT_ATTENTION)
1361 break;
1362 }
1363 }
1364
James Bottomley39216032005-06-15 18:48:29 -05001365 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /*
1367 * The device probably does not support a REPORT LUN command
1368 */
Alan Stern2ef89192005-11-08 15:51:55 -05001369 ret = 1;
1370 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /*
1374 * Get the length from the first four bytes of lun_data.
1375 */
1376 data = (u8 *) lun_data->scsi_lun;
1377 length = ((data[0] << 24) | (data[1] << 16) |
1378 (data[2] << 8) | (data[3] << 0));
1379
1380 num_luns = (length / sizeof(struct scsi_lun));
1381 if (num_luns > max_scsi_report_luns) {
1382 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1383 " of %d luns reported, try increasing"
1384 " max_scsi_report_luns.\n", devname,
1385 max_scsi_report_luns, num_luns);
1386 num_luns = max_scsi_report_luns;
1387 }
1388
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001389 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1390 "scsi scan: REPORT LUN scan\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
1393 * Scan the luns in lun_data. The entry at offset 0 is really
1394 * the header, so start at 1 and go up to and including num_luns.
1395 */
1396 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1397 lun = scsilun_to_int(lunp);
1398
1399 /*
1400 * Check if the unused part of lunp is non-zero, and so
1401 * does not fit in lun.
1402 */
1403 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1404 int i;
1405
1406 /*
1407 * Output an error displaying the LUN in byte order,
1408 * this differs from what linux would print for the
1409 * integer LUN value.
1410 */
1411 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1412 data = (char *)lunp->scsi_lun;
1413 for (i = 0; i < sizeof(struct scsi_lun); i++)
1414 printk("%02x", data[i]);
1415 printk(" has a LUN larger than currently supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 } else if (lun > sdev->host->max_lun) {
1417 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1418 " than allowed by the host adapter\n",
1419 devname, lun);
1420 } else {
1421 int res;
1422
1423 res = scsi_probe_and_add_lun(starget,
1424 lun, NULL, NULL, rescan, NULL);
1425 if (res == SCSI_SCAN_NO_RESPONSE) {
1426 /*
1427 * Got some results, but now none, abort.
1428 */
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001429 sdev_printk(KERN_ERR, sdev,
1430 "Unexpected response"
1431 " from lun %d while scanning, scan"
1432 " aborted\n", lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 break;
1434 }
1435 }
1436 }
1437
Alan Stern2ef89192005-11-08 15:51:55 -05001438 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 kfree(lun_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 out:
James Bottomley6f3a2022005-09-22 20:33:28 -05001441 scsi_device_put(sdev);
1442 if (sdev->sdev_state == SDEV_CREATED)
1443 /*
1444 * the sdev we used didn't appear in the report luns scan
1445 */
1446 scsi_destroy_sdev(sdev);
Alan Stern2ef89192005-11-08 15:51:55 -05001447 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
1450struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1451 uint id, uint lun, void *hostdata)
1452{
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001453 struct scsi_device *sdev = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 struct device *parent = &shost->shost_gendev;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001455 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Matthew Wilcox938e2ac2007-01-15 18:07:09 -07001457 if (strncmp(scsi_scan_type, "none", 4) == 0)
1458 return ERR_PTR(-ENODEV);
1459
1460 if (!shost->async_scan)
1461 scsi_complete_async_scans();
1462
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001463 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (!starget)
1465 return ERR_PTR(-ENOMEM);
1466
Arjan van de Ven0b950672006-01-11 13:16:10 +01001467 mutex_lock(&shost->scan_mutex);
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001468 if (scsi_host_scan_allowed(shost))
1469 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001470 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 scsi_target_reap(starget);
1472 put_device(&starget->dev);
1473
1474 return sdev;
1475}
1476EXPORT_SYMBOL(__scsi_add_device);
1477
James Bottomley146f7262005-09-10 12:44:09 -05001478int scsi_add_device(struct Scsi_Host *host, uint channel,
1479 uint target, uint lun)
1480{
1481 struct scsi_device *sdev =
1482 __scsi_add_device(host, channel, target, lun, NULL);
1483 if (IS_ERR(sdev))
1484 return PTR_ERR(sdev);
1485
1486 scsi_device_put(sdev);
1487 return 0;
1488}
1489EXPORT_SYMBOL(scsi_add_device);
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491void scsi_rescan_device(struct device *dev)
1492{
1493 struct scsi_driver *drv;
1494
1495 if (!dev->driver)
1496 return;
1497
1498 drv = to_scsi_driver(dev->driver);
1499 if (try_module_get(drv->owner)) {
1500 if (drv->rescan)
1501 drv->rescan(dev);
1502 module_put(drv->owner);
1503 }
1504}
1505EXPORT_SYMBOL(scsi_rescan_device);
1506
Alan Sterne517d312005-07-26 10:18:45 -04001507static void __scsi_scan_target(struct device *parent, unsigned int channel,
1508 unsigned int id, unsigned int lun, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct Scsi_Host *shost = dev_to_shost(parent);
1511 int bflags = 0;
1512 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct scsi_target *starget;
1514
1515 if (shost->this_id == id)
1516 /*
1517 * Don't scan the host adapter
1518 */
1519 return;
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (!starget)
1523 return;
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (lun != SCAN_WILD_CARD) {
1526 /*
1527 * Scan for a specific host/chan/id/lun.
1528 */
1529 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1530 goto out_reap;
1531 }
1532
1533 /*
1534 * Scan LUN 0, if there is some response, scan further. Ideally, we
1535 * would not configure LUN 0 until all LUNs are scanned.
1536 */
James Bottomley6f3a2022005-09-22 20:33:28 -05001537 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1538 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1539 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /*
1541 * The REPORT LUN did not scan the target,
1542 * do a sequential scan.
1543 */
1544 scsi_sequential_lun_scan(starget, bflags,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001545 starget->scsi_level, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 out_reap:
1549 /* now determine if the target has any children at all
1550 * and if not, nuke it */
1551 scsi_target_reap(starget);
1552
1553 put_device(&starget->dev);
1554}
Alan Sterne517d312005-07-26 10:18:45 -04001555
1556/**
1557 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1558 * target.
1559 * @parent: host to scan
1560 * @channel: channel to scan
1561 * @id: target id to scan
1562 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1563 * @rescan: passed to LUN scanning routines
1564 *
1565 * Description:
1566 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1567 * and possibly all LUNs on the target id.
1568 *
1569 * First try a REPORT LUN scan, if that does not scan the target, do a
1570 * sequential scan of LUNs on the target id.
1571 **/
1572void scsi_scan_target(struct device *parent, unsigned int channel,
1573 unsigned int id, unsigned int lun, int rescan)
1574{
1575 struct Scsi_Host *shost = dev_to_shost(parent);
1576
Matthew Wilcox93b45af2006-11-22 13:24:53 -07001577 if (strncmp(scsi_scan_type, "none", 4) == 0)
1578 return;
1579
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001580 if (!shost->async_scan)
1581 scsi_complete_async_scans();
1582
Arjan van de Ven0b950672006-01-11 13:16:10 +01001583 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001584 if (scsi_host_scan_allowed(shost))
1585 __scsi_scan_target(parent, channel, id, lun, rescan);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001586 mutex_unlock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588EXPORT_SYMBOL(scsi_scan_target);
1589
1590static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1591 unsigned int id, unsigned int lun, int rescan)
1592{
1593 uint order_id;
1594
1595 if (id == SCAN_WILD_CARD)
1596 for (id = 0; id < shost->max_id; ++id) {
1597 /*
1598 * XXX adapter drivers when possible (FCP, iSCSI)
1599 * could modify max_id to match the current max,
1600 * not the absolute max.
1601 *
1602 * XXX add a shost id iterator, so for example,
1603 * the FC ID can be the same as a target id
1604 * without a huge overhead of sparse id's.
1605 */
1606 if (shost->reverse_ordering)
1607 /*
1608 * Scan from high to low id.
1609 */
1610 order_id = shost->max_id - id - 1;
1611 else
1612 order_id = id;
Alan Sterne517d312005-07-26 10:18:45 -04001613 __scsi_scan_target(&shost->shost_gendev, channel,
1614 order_id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616 else
Alan Sterne517d312005-07-26 10:18:45 -04001617 __scsi_scan_target(&shost->shost_gendev, channel,
1618 id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1622 unsigned int id, unsigned int lun, int rescan)
1623{
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001624 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1625 "%s: <%u:%u:%u>\n",
1626 __FUNCTION__, channel, id, lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001628 if (!shost->async_scan)
1629 scsi_complete_async_scans();
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
Amit Arora091686d2006-05-19 16:14:50 -07001632 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1634 return -EINVAL;
1635
Arjan van de Ven0b950672006-01-11 13:16:10 +01001636 mutex_lock(&shost->scan_mutex);
Mike Anderson82f29462005-06-16 11:14:33 -07001637 if (scsi_host_scan_allowed(shost)) {
1638 if (channel == SCAN_WILD_CARD)
1639 for (channel = 0; channel <= shost->max_channel;
1640 channel++)
1641 scsi_scan_channel(shost, channel, id, lun,
1642 rescan);
1643 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 scsi_scan_channel(shost, channel, id, lun, rescan);
Mike Anderson82f29462005-06-16 11:14:33 -07001645 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001646 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 return 0;
1649}
1650
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001651static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1652{
1653 struct scsi_device *sdev;
1654 shost_for_each_device(sdev, shost) {
1655 if (scsi_sysfs_add_sdev(sdev) != 0)
1656 scsi_destroy_sdev(sdev);
1657 }
1658}
1659
1660/**
1661 * scsi_prep_async_scan - prepare for an async scan
1662 * @shost: the host which will be scanned
1663 * Returns: a cookie to be passed to scsi_finish_async_scan()
1664 *
1665 * Tells the midlayer this host is going to do an asynchronous scan.
1666 * It reserves the host's position in the scanning list and ensures
1667 * that other asynchronous scans started after this one won't affect the
1668 * ordering of the discovered devices.
1669 */
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001670static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001671{
1672 struct async_scan_data *data;
1673
1674 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1675 return NULL;
1676
1677 if (shost->async_scan) {
1678 printk("%s called twice for host %d", __FUNCTION__,
1679 shost->host_no);
1680 dump_stack();
1681 return NULL;
1682 }
1683
1684 data = kmalloc(sizeof(*data), GFP_KERNEL);
1685 if (!data)
1686 goto err;
1687 data->shost = scsi_host_get(shost);
1688 if (!data->shost)
1689 goto err;
1690 init_completion(&data->prev_finished);
1691
1692 spin_lock(&async_scan_lock);
1693 shost->async_scan = 1;
1694 if (list_empty(&scanning_hosts))
1695 complete(&data->prev_finished);
1696 list_add_tail(&data->list, &scanning_hosts);
1697 spin_unlock(&async_scan_lock);
1698
1699 return data;
1700
1701 err:
1702 kfree(data);
1703 return NULL;
1704}
1705
1706/**
1707 * scsi_finish_async_scan - asynchronous scan has finished
1708 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1709 *
1710 * All the devices currently attached to this host have been found.
1711 * This function announces all the devices it has found to the rest
1712 * of the system.
1713 */
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001714static void scsi_finish_async_scan(struct async_scan_data *data)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001715{
1716 struct Scsi_Host *shost;
1717
1718 if (!data)
1719 return;
1720
1721 shost = data->shost;
1722 if (!shost->async_scan) {
1723 printk("%s called twice for host %d", __FUNCTION__,
1724 shost->host_no);
1725 dump_stack();
1726 return;
1727 }
1728
1729 wait_for_completion(&data->prev_finished);
1730
1731 scsi_sysfs_add_devices(shost);
1732
1733 spin_lock(&async_scan_lock);
1734 shost->async_scan = 0;
1735 list_del(&data->list);
1736 if (!list_empty(&scanning_hosts)) {
1737 struct async_scan_data *next = list_entry(scanning_hosts.next,
1738 struct async_scan_data, list);
1739 complete(&next->prev_finished);
1740 }
1741 spin_unlock(&async_scan_lock);
1742
1743 scsi_host_put(shost);
1744 kfree(data);
1745}
1746
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001747static void do_scsi_scan_host(struct Scsi_Host *shost)
1748{
1749 if (shost->hostt->scan_finished) {
1750 unsigned long start = jiffies;
1751 if (shost->hostt->scan_start)
1752 shost->hostt->scan_start(shost);
1753
1754 while (!shost->hostt->scan_finished(shost, jiffies - start))
1755 msleep(10);
1756 } else {
1757 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1758 SCAN_WILD_CARD, 0);
1759 }
1760}
1761
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001762static int do_scan_async(void *_data)
1763{
1764 struct async_scan_data *data = _data;
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001765 do_scsi_scan_host(data->shost);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001766 scsi_finish_async_scan(data);
1767 return 0;
1768}
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770/**
1771 * scsi_scan_host - scan the given adapter
1772 * @shost: adapter to scan
1773 **/
1774void scsi_scan_host(struct Scsi_Host *shost)
1775{
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001776 struct async_scan_data *data;
1777
1778 if (strncmp(scsi_scan_type, "none", 4) == 0)
1779 return;
1780
1781 data = scsi_prep_async_scan(shost);
1782 if (!data) {
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001783 do_scsi_scan_host(shost);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001784 return;
1785 }
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001786
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001787 kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789EXPORT_SYMBOL(scsi_scan_host);
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791void scsi_forget_host(struct Scsi_Host *shost)
1792{
Alan Sterna64358d2005-07-26 10:27:10 -04001793 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 unsigned long flags;
1795
Alan Sterna64358d2005-07-26 10:27:10 -04001796 restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001798 list_for_each_entry(sdev, &shost->__devices, siblings) {
1799 if (sdev->sdev_state == SDEV_DEL)
1800 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 spin_unlock_irqrestore(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001802 __scsi_remove_device(sdev);
1803 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 spin_unlock_irqrestore(shost->host_lock, flags);
1806}
1807
1808/*
1809 * Function: scsi_get_host_dev()
1810 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001811 * Purpose: Create a scsi_device that points to the host adapter itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001813 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 *
1815 * Lock status: None assumed.
1816 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001817 * Returns: The scsi_device or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 *
1819 * Notes:
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001820 * Attach a single scsi_device to the Scsi_Host - this should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 * be made to look like a "pseudo-device" that points to the
1822 * HA itself.
1823 *
1824 * Note - this device is not accessible from any high-level
1825 * drivers (including generics), which is probably not
1826 * optimal. We can add hooks later to attach
1827 */
1828struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1829{
Alan Sterne517d312005-07-26 10:18:45 -04001830 struct scsi_device *sdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct scsi_target *starget;
1832
Arjan van de Ven0b950672006-01-11 13:16:10 +01001833 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001834 if (!scsi_host_scan_allowed(shost))
1835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1837 if (!starget)
Alan Sterne517d312005-07-26 10:18:45 -04001838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 sdev = scsi_alloc_sdev(starget, 0, NULL);
1841 if (sdev) {
1842 sdev->sdev_gendev.parent = get_device(&starget->dev);
1843 sdev->borken = 0;
James Bottomley884d25c2006-09-05 16:26:41 -05001844 } else
1845 scsi_target_reap(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 put_device(&starget->dev);
Alan Sterne517d312005-07-26 10:18:45 -04001847 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +01001848 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return sdev;
1850}
1851EXPORT_SYMBOL(scsi_get_host_dev);
1852
1853/*
1854 * Function: scsi_free_host_dev()
1855 *
1856 * Purpose: Free a scsi_device that points to the host adapter itself.
1857 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001858 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 *
1860 * Lock status: None assumed.
1861 *
1862 * Returns: Nothing
1863 *
1864 * Notes:
1865 */
1866void scsi_free_host_dev(struct scsi_device *sdev)
1867{
1868 BUG_ON(sdev->id != sdev->host->this_id);
1869
James Bottomley6f3a2022005-09-22 20:33:28 -05001870 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872EXPORT_SYMBOL(scsi_free_host_dev);
1873