Use ARRAY_SIZE() macro in i386 relocs.c file
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / scsi_scan.c
CommitLineData
1da177e4
LT
1/*
2 * scsi_scan.c
3 *
4 * Copyright (C) 2000 Eric Youngdale,
5 * Copyright (C) 2002 Patrick Mansfield
6 *
7 * The general scanning/probing algorithm is as follows, exceptions are
8 * made to it depending on device specific flags, compilation options, and
9 * global variable (boot or module load time) settings.
10 *
11 * A specific LUN is scanned via an INQUIRY command; if the LUN has a
f64a181d 12 * device attached, a scsi_device is allocated and setup for it.
1da177e4
LT
13 *
14 * For every id of every channel on the given host:
15 *
16 * Scan LUN 0; if the target responds to LUN 0 (even if there is no
17 * device or storage attached to LUN 0):
18 *
19 * If LUN 0 has a device attached, allocate and setup a
f64a181d 20 * scsi_device for it.
1da177e4
LT
21 *
22 * If target is SCSI-3 or up, issue a REPORT LUN, and scan
23 * all of the LUNs returned by the REPORT LUN; else,
24 * sequentially scan LUNs up until some maximum is reached,
25 * or a LUN is seen that cannot have a device attached to it.
26 */
27
1da177e4
LT
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/blkdev.h>
3e082a91
MW
32#include <linux/delay.h>
33#include <linux/kthread.h>
34#include <linux/spinlock.h>
1da177e4
LT
35
36#include <scsi/scsi.h>
beb40487 37#include <scsi/scsi_cmnd.h>
1da177e4
LT
38#include <scsi/scsi_device.h>
39#include <scsi/scsi_driver.h>
40#include <scsi/scsi_devinfo.h>
41#include <scsi/scsi_host.h>
1da177e4
LT
42#include <scsi/scsi_transport.h>
43#include <scsi/scsi_eh.h>
44
45#include "scsi_priv.h"
46#include "scsi_logging.h"
47
48#define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
49 " SCSI scanning, some SCSI devices might not be configured\n"
50
51/*
52 * Default timeout
53 */
54#define SCSI_TIMEOUT (2*HZ)
55
56/*
57 * Prefix values for the SCSI id's (stored in 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
0ad78200 78static const char *scsi_null_device_strs = "nullnullnullnull";
1da177e4
LT
79
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
21db1882
MW
92#ifdef CONFIG_SCSI_SCAN_ASYNC
93#define SCSI_SCAN_TYPE_DEFAULT "async"
94#else
95#define SCSI_SCAN_TYPE_DEFAULT "sync"
96#endif
97
98static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT;
3e082a91
MW
99
100module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
101MODULE_PARM_DESC(scan, "sync, async or none");
102
1da177e4
LT
103/*
104 * max_scsi_report_luns: the maximum number of LUNS that will be
105 * returned from the REPORT LUNS command. 8 times this value must
106 * be allocated. In theory this could be up to an 8 byte value, but
107 * in practice, the maximum number of LUNs suppored by any device
108 * is about 16k.
109 */
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
3e082a91
MW
124static 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 *
8bcc2412
MW
136 * When this function returns, any host which started scanning before
137 * this function was called will have finished its scan. Hosts which
138 * started scanning after this function was called may or may not have
139 * finished.
3e082a91
MW
140 */
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);
8bcc2412
MW
172 if (!list_empty(&scanning_hosts)) {
173 struct async_scan_data *next = list_entry(scanning_hosts.next,
174 struct async_scan_data, list);
175 complete(&next->prev_finished);
176 }
3e082a91
MW
177 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
1da177e4
LT
189/**
190 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
39216033 191 * @sdev: scsi device to send command to
1da177e4
LT
192 * @result: area to store the result of the MODE SENSE
193 *
194 * Description:
39216033 195 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
1da177e4
LT
196 * Called for BLIST_KEY devices.
197 **/
39216033 198static void scsi_unlock_floptical(struct scsi_device *sdev,
1da177e4
LT
199 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;
39216033 208 scsi_cmd[4] = 0x2a; /* size */
1da177e4 209 scsi_cmd[5] = 0;
39216033
JB
210 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
211 SCSI_TIMEOUT, 3);
1da177e4
LT
212}
213
1da177e4
LT
214/**
215 * 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
24669f75 232 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
1da177e4
LT
233 GFP_ATOMIC);
234 if (!sdev)
235 goto out;
236
1da177e4
LT
237 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
1da177e4
LT
273 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);
93f56089 278 kfree(sdev);
1da177e4
LT
279 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
1da177e4
LT
293 */
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);
1da177e4
LT
304 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);
a283bd37 315
1da177e4
LT
316 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
884d25cc
JB
347/**
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 */
1da177e4
LT
359static 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;
5c44cd2a 367 struct scsi_target *starget;
1da177e4 368 struct scsi_target *found_target;
32f95792 369 int error;
1da177e4 370
24669f75 371 starget = kzalloc(size, GFP_KERNEL);
1da177e4
LT
372 if (!starget) {
373 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
374 return NULL;
375 }
1da177e4
LT
376 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);
ffedb452
JB
387 starget->state = STARGET_RUNNING;
388 retry:
1da177e4
LT
389 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 */
a283bd37 398 transport_setup_device(dev);
32f95792
BK
399 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 }
a283bd37
JB
410 transport_add_device(dev);
411 if (shost->hostt->target_alloc) {
32f95792 412 error = shost->hostt->target_alloc(starget);
a283bd37
JB
413
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 }
884d25cc 424 get_device(dev);
a283bd37 425
1da177e4
LT
426 return starget;
427
428 found:
429 found_target->reap_ref++;
430 spin_unlock_irqrestore(shost->host_lock, flags);
ffedb452 431 if (found_target->state != STARGET_DEL) {
884d25cc 432 put_device(parent);
ffedb452
JB
433 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;
1da177e4
LT
441}
442
65f27f38 443static void scsi_target_reap_usercontext(struct work_struct *work)
65110b21 444{
65f27f38
DH
445 struct scsi_target *starget =
446 container_of(work, struct scsi_target, ew.work);
863a930a
JB
447 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
448 unsigned long flags;
449
ffedb452
JB
450 transport_remove_device(&starget->dev);
451 device_del(&starget->dev);
452 transport_destroy_device(&starget->dev);
863a930a 453 spin_lock_irqsave(shost->host_lock, flags);
a50a5e37
MA
454 if (shost->hostt->target_destroy)
455 shost->hostt->target_destroy(starget);
ffedb452 456 list_del_init(&starget->siblings);
863a930a 457 spin_unlock_irqrestore(shost->host_lock, flags);
ffedb452 458 put_device(&starget->dev);
863a930a
JB
459}
460
1da177e4
LT
461/**
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{
ffedb452
JB
472 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,
65f27f38 482 &starget->ew);
ffedb452
JB
483 return;
484
485 }
486 spin_unlock_irqrestore(shost->host_lock, flags);
487
488 return;
1da177e4
LT
489}
490
e5b3cd42
AS
491/**
492 * 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
1da177e4
LT
517/**
518 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
39216033 519 * @sdev: scsi_device to probe
1da177e4 520 * @inq_result: area to store the INQUIRY result
39216033 521 * @result_len: len of inq_result
1da177e4
LT
522 * @bflags: store any bflags found here
523 *
524 * Description:
39216033 525 * Probe the lun associated with @req using a standard SCSI INQUIRY;
1da177e4 526 *
39216033 527 * If the INQUIRY is successful, zero is returned and the
1da177e4 528 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
f64a181d 529 * are copied to the scsi_device any flags value is stored in *@bflags.
1da177e4 530 **/
e5b3cd42 531static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
39216033 532 int result_len, int *bflags)
1da177e4 533{
1da177e4
LT
534 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
535 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
536 int response_len = 0;
39216033 537 int pass, count, result;
1da177e4
LT
538 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:
9ccfc756
JB
550 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
551 "scsi scan: INQUIRY pass %d length %d\n",
552 pass, try_inquiry_len));
1da177e4
LT
553
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;
1da177e4
LT
559
560 memset(inq_result, 0, try_inquiry_len);
39216033
JB
561
562 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
ea73a9f2 563 inq_result, try_inquiry_len, &sshdr,
39216033 564 HZ / 2 + HZ * scsi_inq_timeout, 3);
1da177e4
LT
565
566 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
567 "with code 0x%x\n",
39216033 568 result ? "failed" : "successful", result));
1da177e4 569
39216033 570 if (result) {
1da177e4
LT
571 /*
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 */
39216033 577 if ((driver_byte(result) & DRIVER_SENSE) &&
ea73a9f2 578 scsi_sense_valid(&sshdr)) {
1da177e4
LT
579 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
39216033 589 if (result == 0) {
e5b3cd42
AS
590 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;
1da177e4
LT
595 if (response_len > 255)
596 response_len = first_inquiry_len; /* sanity */
597
598 /*
599 * Get any flags for this device.
600 *
f64a181d
CH
601 * XXX add a bflags to scsi_device, and replace the
602 * corresponding bit fields in scsi_device, so bflags
1da177e4
LT
603 * 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. */
39216033
JB
642 if (result)
643 return -EIO;
1da177e4
LT
644
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 *
657 * Related to the above issue:
658 *
659 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
660 * and if not ready, sent a START_STOP to start (maybe spin up) and
661 * then send the INQUIRY again, since the INQUIRY can change after
662 * a device is initialized.
663 *
664 * Ideally, start a device if explicitly asked to do so. This
665 * assumes that a device is spun up on power on, spun down on
666 * request, and then spun up on request.
667 */
668
669 /*
670 * The scanning code needs to know the scsi_level, even if no
671 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
672 * non-zero LUNs can be scanned.
673 */
674 sdev->scsi_level = inq_result[2] & 0x07;
675 if (sdev->scsi_level >= 2 ||
676 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
677 sdev->scsi_level++;
6f3a2024 678 sdev->sdev_target->scsi_level = sdev->scsi_level;
1da177e4 679
39216033 680 return 0;
1da177e4
LT
681}
682
683/**
f64a181d
CH
684 * scsi_add_lun - allocate and fully initialze a scsi_device
685 * @sdevscan: holds information to be stored in the new scsi_device
686 * @sdevnew: store the address of the newly allocated scsi_device
1da177e4
LT
687 * @inq_result: holds the result of a previous INQUIRY to the LUN
688 * @bflags: black/white list flag
689 *
690 * Description:
f64a181d 691 * Allocate and initialize a scsi_device matching sdevscan. Optionally
1da177e4 692 * set fields based on values in *@bflags. If @sdevnew is not
f64a181d 693 * NULL, store the address of the new scsi_device in *@sdevnew (needed
1da177e4
LT
694 * when scanning a particular LUN).
695 *
696 * Return:
f64a181d
CH
697 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
698 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
1da177e4 699 **/
e5b3cd42 700static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
3e082a91 701 int *bflags, int async)
1da177e4
LT
702{
703 /*
704 * XXX do not save the inquiry, since it can change underneath us,
705 * save just vendor/model/rev.
706 *
707 * Rather than save it and have an ioctl that retrieves the saved
708 * value, have an ioctl that executes the same INQUIRY code used
709 * in scsi_probe_lun, let user level programs doing INQUIRY
710 * scanning run at their own risk, or supply a user level program
711 * that can correctly scan.
712 */
09123d23
AS
713
714 /*
715 * Copy at least 36 bytes of INQUIRY data, so that we don't
716 * dereference unallocated memory when accessing the Vendor,
717 * Product, and Revision strings. Badly behaved devices may set
718 * the INQUIRY Additional Length byte to a small value, indicating
719 * these strings are invalid, but often they contain plausible data
720 * nonetheless. It doesn't matter if the device sent < 36 bytes
721 * total, since scsi_probe_lun() initializes inq_result with 0s.
722 */
723 sdev->inquiry = kmemdup(inq_result,
724 max_t(size_t, sdev->inquiry_len, 36),
725 GFP_ATOMIC);
726 if (sdev->inquiry == NULL)
1da177e4 727 return SCSI_SCAN_NO_RESPONSE;
1da177e4 728
1da177e4
LT
729 sdev->vendor = (char *) (sdev->inquiry + 8);
730 sdev->model = (char *) (sdev->inquiry + 16);
731 sdev->rev = (char *) (sdev->inquiry + 32);
732
733 if (*bflags & BLIST_ISROM) {
734 /*
735 * It would be better to modify sdev->type, and set
4ff36718
MW
736 * sdev->removable; this can now be done since
737 * print_inquiry has gone away.
1da177e4
LT
738 */
739 inq_result[0] = TYPE_ROM;
740 inq_result[1] |= 0x80; /* removable */
741 } else if (*bflags & BLIST_NO_ULD_ATTACH)
742 sdev->no_uld_attach = 1;
743
744 switch (sdev->type = (inq_result[0] & 0x1f)) {
ddaf6fc8
JB
745 case TYPE_RBC:
746 /* RBC devices can return SCSI-3 compliance and yet
747 * still not support REPORT LUNS, so make them act as
748 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
749 * specifically set */
750 if ((*bflags & BLIST_REPORTLUN2) == 0)
751 *bflags |= BLIST_NOREPORTLUN;
752 /* fall through */
1da177e4
LT
753 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:
4d7db04a 762 case TYPE_RAID:
1da177e4
LT
763 sdev->writeable = 1;
764 break;
1da177e4 765 case TYPE_ROM:
ddaf6fc8
JB
766 /* MMC devices can return SCSI-3 compliance and yet
767 * still not support REPORT LUNS, so make them act as
768 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
769 * specifically set */
770 if ((*bflags & BLIST_REPORTLUN2) == 0)
771 *bflags |= BLIST_NOREPORTLUN;
772 /* fall through */
773 case TYPE_WORM:
1da177e4
LT
774 sdev->writeable = 0;
775 break;
776 default:
777 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
778 }
779
1da177e4
LT
780 /*
781 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
782 * spec says: The device server is capable of supporting the
783 * specified peripheral device type on this logical unit. However,
784 * the physical device is not currently connected to this logical
785 * unit.
786 *
787 * The above is vague, as it implies that we could treat 001 and
788 * 011 the same. Stay compatible with previous code, and create a
f64a181d 789 * scsi_device for a PQ of 1
1da177e4
LT
790 *
791 * Don't set the device offline here; rather let the upper
792 * level drivers eval the PQ to decide whether they should
793 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
794 */
795
796 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
797 sdev->removable = (0x80 & inq_result[1]) >> 7;
798 sdev->lockable = sdev->removable;
799 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
800
801 if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
802 inq_result[56] & 0x04))
803 sdev->ppr = 1;
804 if (inq_result[7] & 0x60)
805 sdev->wdtr = 1;
806 if (inq_result[7] & 0x10)
807 sdev->sdtr = 1;
808
19ac0db3 809 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
4ff36718
MW
810 "ANSI: %d%s\n", scsi_device_type(sdev->type),
811 sdev->vendor, sdev->model, sdev->rev,
812 sdev->inq_periph_qual, inq_result[2] & 0x07,
813 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
814
1da177e4 815 /*
5e3c34c1 816 * End sysfs code.
1da177e4
LT
817 */
818
819 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
820 !(*bflags & BLIST_NOTQ))
821 sdev->tagged_supported = 1;
822 /*
823 * Some devices (Texel CD ROM drives) have handshaking problems
824 * when used with the Seagate controllers. borken is initialized
825 * to 1, and then set it to 0 here.
826 */
827 if ((*bflags & BLIST_BORKEN) == 0)
828 sdev->borken = 0;
829
830 /*
831 * Apparently some really broken devices (contrary to the SCSI
832 * standards) need to be selected without asserting ATN
833 */
834 if (*bflags & BLIST_SELECT_NO_ATN)
835 sdev->select_no_atn = 1;
836
4d7db04a
JB
837 /*
838 * Maximum 512 sector transfer length
839 * broken RA4x00 Compaq Disk Array
840 */
841 if (*bflags & BLIST_MAX_512)
842 blk_queue_max_sectors(sdev->request_queue, 512);
843
1da177e4
LT
844 /*
845 * Some devices may not want to have a start command automatically
846 * issued when a device is added.
847 */
848 if (*bflags & BLIST_NOSTARTONADD)
849 sdev->no_start_on_add = 1;
850
851 if (*bflags & BLIST_SINGLELUN)
852 sdev->single_lun = 1;
853
854
855 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
93805091
CH
881 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 }
1da177e4
LT
895
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 */
3e082a91 901 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
b24b1033 902 return SCSI_SCAN_NO_RESPONSE;
1da177e4
LT
903
904 return SCSI_SCAN_LUN_PRESENT;
905}
906
6f3a2024
JB
907static inline void scsi_destroy_sdev(struct scsi_device *sdev)
908{
309bd271 909 scsi_device_set_state(sdev, SDEV_DEL);
6f3a2024
JB
910 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
c5f2e640 916#ifdef CONFIG_SCSI_LOGGING
6c7154c9
KG
917/**
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 */
c5f2e640 925static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
6c7154c9
KG
926 unsigned first, unsigned end)
927{
928 unsigned term = 0, idx;
c5f2e640
AM
929
930 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
931 if (inq[idx+first] > ' ') {
6c7154c9
KG
932 buf[idx] = inq[idx+first];
933 term = idx+1;
934 } else {
935 buf[idx] = ' ';
936 }
937 }
938 buf[term] = 0;
939 return buf;
940}
c5f2e640 941#endif
6f3a2024 942
1da177e4
LT
943/**
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
f64a181d
CH
947 * @sdevscan: probe the LUN corresponding to this scsi_device
948 * @sdevnew: store the value of any new scsi_device allocated
1da177e4
LT
949 * @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:
f64a181d 956 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
1da177e4
LT
957 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
958 * attached at the LUN
f64a181d 959 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
1da177e4
LT
960 **/
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;
1da177e4 967 unsigned char *result;
39216033 968 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
1da177e4
LT
969 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 */
6f3a2024
JB
975 sdev = scsi_device_lookup_by_target(starget, lun);
976 if (sdev) {
977 if (rescan || sdev->sdev_state != SDEV_CREATED) {
1da177e4
LT
978 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 }
6f3a2024
JB
992 scsi_device_put(sdev);
993 } else
994 sdev = scsi_alloc_sdev(starget, lun, hostdata);
1da177e4
LT
995 if (!sdev)
996 goto out;
39216033
JB
997
998 result = kmalloc(result_len, GFP_ATOMIC |
bc86120a 999 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
1da177e4 1000 if (!result)
39216033 1001 goto out_free_sdev;
1da177e4 1002
39216033 1003 if (scsi_probe_lun(sdev, result, result_len, &bflags))
1da177e4
LT
1004 goto out_free_result;
1005
4186ab19
KG
1006 if (bflagsp)
1007 *bflagsp = bflags;
1da177e4
LT
1008 /*
1009 * result contains valid SCSI INQUIRY data.
1010 */
13f7e5ac 1011 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
1da177e4
LT
1012 /*
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 */
6c7154c9
KG
1022 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) {
c5f2e640
AM
1026 SCSI_LOG_SCAN_BUS(1, {
1027 unsigned char vend[9];
1028 unsigned char mod[17];
1029
1030 sdev_printk(KERN_INFO, sdev,
6c7154c9 1031 "scsi scan: consider passing scsi_mod."
3424a65d 1032 "dev_flags=%s:%s:0x240 or 0x1000240\n",
6c7154c9 1033 scsi_inq_str(vend, result, 8, 16),
c5f2e640
AM
1034 scsi_inq_str(mod, result, 16, 32));
1035 });
6c7154c9
KG
1036 }
1037
1da177e4
LT
1038 res = SCSI_SCAN_TARGET_PRESENT;
1039 goto out_free_result;
1040 }
1041
1bfc5d9d 1042 /*
84961f28 1043 * 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)
1bfc5d9d 1060 */
84961f28 1061 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1062 (result[0] & 0x1f) == 0x1f) {
1bfc5d9d
AS
1063 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
3e082a91 1070 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
1da177e4
LT
1071 if (res == SCSI_SCAN_LUN_PRESENT) {
1072 if (bflags & BLIST_KEY) {
1073 sdev->lockable = 0;
39216033 1074 scsi_unlock_floptical(sdev, result);
1da177e4 1075 }
1da177e4
LT
1076 }
1077
1078 out_free_result:
1079 kfree(result);
1da177e4
LT
1080 out_free_sdev:
1081 if (res == SCSI_SCAN_LUN_PRESENT) {
1082 if (sdevp) {
b70d37bf
AS
1083 if (scsi_device_get(sdev) == 0) {
1084 *sdevp = sdev;
1085 } else {
1086 __scsi_remove_device(sdev);
1087 res = SCSI_SCAN_NO_RESPONSE;
1088 }
1da177e4 1089 }
6f3a2024
JB
1090 } else
1091 scsi_destroy_sdev(sdev);
1da177e4
LT
1092 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
1da177e4
LT
1100 *
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,
4186ab19 1109 int bflags, int scsi_level, int rescan)
1da177e4
LT
1110{
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
1da177e4
LT
1129 /*
1130 * 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 **/
1195static int scsilun_to_int(struct scsi_lun *scsilun)
1196{
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}
1206
2f4701d8
JSEC
1207/**
1208 * int_to_scsilun: reverts an int into a scsi_lun
1209 * @int: integer to be reverted
1210 * @scsilun: struct scsi_lun to be set.
1211 *
1212 * Description:
1213 * Reverts the functionality of the scsilun_to_int, which packed
1214 * an 8-byte lun value into an int. This routine unpacks the int
1215 * back into the lun value.
1216 * Note: the scsilun_to_int() routine does not truly handle all
1217 * 8bytes of the lun value. This functions restores only as much
1218 * as was set by the routine.
1219 *
1220 * Notes:
1221 * Given an integer : 0x0b030a04, this function returns a
1222 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1223 *
1224 **/
1225void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1226{
1227 int i;
1228
1229 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1230
1231 for (i = 0; i < sizeof(lun); i += 2) {
1232 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1233 scsilun->scsi_lun[i+1] = lun & 0xFF;
1234 lun = lun >> 16;
1235 }
1236}
1237EXPORT_SYMBOL(int_to_scsilun);
1238
1da177e4
LT
1239/**
1240 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
f64a181d 1241 * @sdevscan: scan the host, channel, and id of this scsi_device
1da177e4
LT
1242 *
1243 * Description:
1244 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1245 * command, and scan the resulting list of LUNs by calling
1246 * scsi_probe_and_add_lun.
1247 *
1248 * Modifies sdevscan->lun.
1249 *
1250 * Return:
1251 * 0: scan completed (or no memory, so further scanning is futile)
1252 * 1: no report lun scan, or not configured
1253 **/
6f3a2024 1254static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
1da177e4
LT
1255 int rescan)
1256{
1257 char devname[64];
1258 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1259 unsigned int length;
1260 unsigned int lun;
1261 unsigned int num_luns;
1262 unsigned int retries;
39216033 1263 int result;
1da177e4 1264 struct scsi_lun *lunp, *lun_data;
1da177e4
LT
1265 u8 *data;
1266 struct scsi_sense_hdr sshdr;
6f3a2024
JB
1267 struct scsi_device *sdev;
1268 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
2ef89198 1269 int ret = 0;
1da177e4
LT
1270
1271 /*
1272 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1273 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1274 * support more than 8 LUNs.
1275 */
4d7db04a
JB
1276 if (bflags & BLIST_NOREPORTLUN)
1277 return 1;
1278 if (starget->scsi_level < SCSI_2 &&
1279 starget->scsi_level != SCSI_UNKNOWN)
1280 return 1;
1281 if (starget->scsi_level < SCSI_3 &&
1282 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
1da177e4
LT
1283 return 1;
1284 if (bflags & BLIST_NOLUN)
1285 return 0;
1286
6f3a2024
JB
1287 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1288 sdev = scsi_alloc_sdev(starget, 0, NULL);
1289 if (!sdev)
1290 return 0;
1291 if (scsi_device_get(sdev))
1292 return 0;
1293 }
1294
1da177e4 1295 sprintf(devname, "host %d channel %d id %d",
6f3a2024 1296 shost->host_no, sdev->channel, sdev->id);
1da177e4
LT
1297
1298 /*
1299 * Allocate enough to hold the header (the same size as one scsi_lun)
1300 * plus the max number of luns we are requesting.
1301 *
1302 * Reallocating and trying again (with the exact amount we need)
1303 * would be nice, but then we need to somehow limit the size
1304 * allocated based on the available memory and the limits of
1305 * kmalloc - we don't want a kmalloc() failure of a huge value to
1306 * prevent us from finding any LUNs on this target.
1307 */
1308 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1309 lun_data = kmalloc(length, GFP_ATOMIC |
1310 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
6f3a2024
JB
1311 if (!lun_data) {
1312 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
39216033 1313 goto out;
6f3a2024 1314 }
1da177e4
LT
1315
1316 scsi_cmd[0] = REPORT_LUNS;
1317
1318 /*
1319 * bytes 1 - 5: reserved, set to zero.
1320 */
1321 memset(&scsi_cmd[1], 0, 5);
1322
1323 /*
1324 * bytes 6 - 9: length of the command.
1325 */
1326 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1327 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1328 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1329 scsi_cmd[9] = (unsigned char) length & 0xff;
1330
1331 scsi_cmd[10] = 0; /* reserved */
1332 scsi_cmd[11] = 0; /* control */
1da177e4
LT
1333
1334 /*
1335 * We can get a UNIT ATTENTION, for example a power on/reset, so
1336 * retry a few times (like sd.c does for TEST UNIT READY).
1337 * Experience shows some combinations of adapter/devices get at
1338 * least two power on/resets.
1339 *
1340 * Illegal requests (for devices that do not support REPORT LUNS)
1341 * should come through as a check condition, and will not generate
1342 * a retry.
1343 */
1344 for (retries = 0; retries < 3; retries++) {
1345 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1346 " REPORT LUNS to %s (try %d)\n", devname,
1347 retries));
39216033 1348
39216033 1349 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
ea73a9f2 1350 lun_data, length, &sshdr,
39216033
JB
1351 SCSI_TIMEOUT + 4 * HZ, 3);
1352
1da177e4 1353 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
39216033
JB
1354 " %s (try %d) result 0x%x\n", result
1355 ? "failed" : "successful", retries, result));
1356 if (result == 0)
1da177e4 1357 break;
ea73a9f2 1358 else if (scsi_sense_valid(&sshdr)) {
1da177e4
LT
1359 if (sshdr.sense_key != UNIT_ATTENTION)
1360 break;
1361 }
1362 }
1363
39216033 1364 if (result) {
1da177e4
LT
1365 /*
1366 * The device probably does not support a REPORT LUN command
1367 */
2ef89198
AS
1368 ret = 1;
1369 goto out_err;
1da177e4 1370 }
1da177e4
LT
1371
1372 /*
1373 * Get the length from the first four bytes of lun_data.
1374 */
1375 data = (u8 *) lun_data->scsi_lun;
1376 length = ((data[0] << 24) | (data[1] << 16) |
1377 (data[2] << 8) | (data[3] << 0));
1378
1379 num_luns = (length / sizeof(struct scsi_lun));
1380 if (num_luns > max_scsi_report_luns) {
1381 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1382 " of %d luns reported, try increasing"
1383 " max_scsi_report_luns.\n", devname,
1384 max_scsi_report_luns, num_luns);
1385 num_luns = max_scsi_report_luns;
1386 }
1387
3bf743e7
JG
1388 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1389 "scsi scan: REPORT LUN scan\n"));
1da177e4
LT
1390
1391 /*
1392 * Scan the luns in lun_data. The entry at offset 0 is really
1393 * the header, so start at 1 and go up to and including num_luns.
1394 */
1395 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1396 lun = scsilun_to_int(lunp);
1397
1398 /*
1399 * Check if the unused part of lunp is non-zero, and so
1400 * does not fit in lun.
1401 */
1402 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1403 int i;
1404
1405 /*
1406 * Output an error displaying the LUN in byte order,
1407 * this differs from what linux would print for the
1408 * integer LUN value.
1409 */
1410 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1411 data = (char *)lunp->scsi_lun;
1412 for (i = 0; i < sizeof(struct scsi_lun); i++)
1413 printk("%02x", data[i]);
1414 printk(" has a LUN larger than currently supported.\n");
1da177e4
LT
1415 } else if (lun > sdev->host->max_lun) {
1416 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1417 " than allowed by the host adapter\n",
1418 devname, lun);
1419 } else {
1420 int res;
1421
1422 res = scsi_probe_and_add_lun(starget,
1423 lun, NULL, NULL, rescan, NULL);
1424 if (res == SCSI_SCAN_NO_RESPONSE) {
1425 /*
1426 * Got some results, but now none, abort.
1427 */
3bf743e7
JG
1428 sdev_printk(KERN_ERR, sdev,
1429 "Unexpected response"
1430 " from lun %d while scanning, scan"
1431 " aborted\n", lun);
1da177e4
LT
1432 break;
1433 }
1434 }
1435 }
1436
2ef89198 1437 out_err:
1da177e4 1438 kfree(lun_data);
1da177e4 1439 out:
6f3a2024
JB
1440 scsi_device_put(sdev);
1441 if (sdev->sdev_state == SDEV_CREATED)
1442 /*
1443 * the sdev we used didn't appear in the report luns scan
1444 */
1445 scsi_destroy_sdev(sdev);
2ef89198 1446 return ret;
1da177e4
LT
1447}
1448
1449struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1450 uint id, uint lun, void *hostdata)
1451{
a97a83a0 1452 struct scsi_device *sdev = ERR_PTR(-ENODEV);
1da177e4 1453 struct device *parent = &shost->shost_gendev;
e02f3f59 1454 struct scsi_target *starget;
1da177e4 1455
938e2ac0
MW
1456 if (strncmp(scsi_scan_type, "none", 4) == 0)
1457 return ERR_PTR(-ENODEV);
1458
1459 if (!shost->async_scan)
1460 scsi_complete_async_scans();
1461
e02f3f59 1462 starget = scsi_alloc_target(parent, channel, id);
1da177e4
LT
1463 if (!starget)
1464 return ERR_PTR(-ENOMEM);
1465
0b950672 1466 mutex_lock(&shost->scan_mutex);
a97a83a0
MW
1467 if (scsi_host_scan_allowed(shost))
1468 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
0b950672 1469 mutex_unlock(&shost->scan_mutex);
1da177e4
LT
1470 scsi_target_reap(starget);
1471 put_device(&starget->dev);
1472
1473 return sdev;
1474}
1475EXPORT_SYMBOL(__scsi_add_device);
1476
146f7262
JB
1477int scsi_add_device(struct Scsi_Host *host, uint channel,
1478 uint target, uint lun)
1479{
1480 struct scsi_device *sdev =
1481 __scsi_add_device(host, channel, target, lun, NULL);
1482 if (IS_ERR(sdev))
1483 return PTR_ERR(sdev);
1484
1485 scsi_device_put(sdev);
1486 return 0;
1487}
1488EXPORT_SYMBOL(scsi_add_device);
1489
1da177e4
LT
1490void scsi_rescan_device(struct device *dev)
1491{
1492 struct scsi_driver *drv;
1493
1494 if (!dev->driver)
1495 return;
1496
1497 drv = to_scsi_driver(dev->driver);
1498 if (try_module_get(drv->owner)) {
1499 if (drv->rescan)
1500 drv->rescan(dev);
1501 module_put(drv->owner);
1502 }
1503}
1504EXPORT_SYMBOL(scsi_rescan_device);
1505
e517d313
AS
1506static void __scsi_scan_target(struct device *parent, unsigned int channel,
1507 unsigned int id, unsigned int lun, int rescan)
1da177e4
LT
1508{
1509 struct Scsi_Host *shost = dev_to_shost(parent);
1510 int bflags = 0;
1511 int res;
1da177e4
LT
1512 struct scsi_target *starget;
1513
1514 if (shost->this_id == id)
1515 /*
1516 * Don't scan the host adapter
1517 */
1518 return;
1519
1da177e4 1520 starget = scsi_alloc_target(parent, channel, id);
1da177e4
LT
1521 if (!starget)
1522 return;
1523
1da177e4
LT
1524 if (lun != SCAN_WILD_CARD) {
1525 /*
1526 * Scan for a specific host/chan/id/lun.
1527 */
1528 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1529 goto out_reap;
1530 }
1531
1532 /*
1533 * Scan LUN 0, if there is some response, scan further. Ideally, we
1534 * would not configure LUN 0 until all LUNs are scanned.
1535 */
6f3a2024
JB
1536 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1537 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1538 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
1da177e4
LT
1539 /*
1540 * The REPORT LUN did not scan the target,
1541 * do a sequential scan.
1542 */
1543 scsi_sequential_lun_scan(starget, bflags,
4186ab19 1544 starget->scsi_level, rescan);
1da177e4 1545 }
1da177e4
LT
1546
1547 out_reap:
1548 /* now determine if the target has any children at all
1549 * and if not, nuke it */
1550 scsi_target_reap(starget);
1551
1552 put_device(&starget->dev);
1553}
e517d313
AS
1554
1555/**
1556 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1557 * target.
1558 * @parent: host to scan
1559 * @channel: channel to scan
1560 * @id: target id to scan
1561 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1562 * @rescan: passed to LUN scanning routines
1563 *
1564 * Description:
1565 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1566 * and possibly all LUNs on the target id.
1567 *
1568 * First try a REPORT LUN scan, if that does not scan the target, do a
1569 * sequential scan of LUNs on the target id.
1570 **/
1571void scsi_scan_target(struct device *parent, unsigned int channel,
1572 unsigned int id, unsigned int lun, int rescan)
1573{
1574 struct Scsi_Host *shost = dev_to_shost(parent);
1575
93b45af5
MW
1576 if (strncmp(scsi_scan_type, "none", 4) == 0)
1577 return;
1578
3e082a91
MW
1579 if (!shost->async_scan)
1580 scsi_complete_async_scans();
1581
0b950672 1582 mutex_lock(&shost->scan_mutex);
e517d313
AS
1583 if (scsi_host_scan_allowed(shost))
1584 __scsi_scan_target(parent, channel, id, lun, rescan);
0b950672 1585 mutex_unlock(&shost->scan_mutex);
e517d313 1586}
1da177e4
LT
1587EXPORT_SYMBOL(scsi_scan_target);
1588
1589static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1590 unsigned int id, unsigned int lun, int rescan)
1591{
1592 uint order_id;
1593
1594 if (id == SCAN_WILD_CARD)
1595 for (id = 0; id < shost->max_id; ++id) {
1596 /*
1597 * XXX adapter drivers when possible (FCP, iSCSI)
1598 * could modify max_id to match the current max,
1599 * not the absolute max.
1600 *
1601 * XXX add a shost id iterator, so for example,
1602 * the FC ID can be the same as a target id
1603 * without a huge overhead of sparse id's.
1604 */
1605 if (shost->reverse_ordering)
1606 /*
1607 * Scan from high to low id.
1608 */
1609 order_id = shost->max_id - id - 1;
1610 else
1611 order_id = id;
e517d313
AS
1612 __scsi_scan_target(&shost->shost_gendev, channel,
1613 order_id, lun, rescan);
1da177e4
LT
1614 }
1615 else
e517d313
AS
1616 __scsi_scan_target(&shost->shost_gendev, channel,
1617 id, lun, rescan);
1da177e4
LT
1618}
1619
1620int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1621 unsigned int id, unsigned int lun, int rescan)
1622{
3bf743e7
JG
1623 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1624 "%s: <%u:%u:%u>\n",
1625 __FUNCTION__, channel, id, lun));
1da177e4 1626
3e082a91
MW
1627 if (!shost->async_scan)
1628 scsi_complete_async_scans();
1629
1da177e4 1630 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
091686d3 1631 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1da177e4
LT
1632 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1633 return -EINVAL;
1634
0b950672 1635 mutex_lock(&shost->scan_mutex);
82f29467
MA
1636 if (scsi_host_scan_allowed(shost)) {
1637 if (channel == SCAN_WILD_CARD)
1638 for (channel = 0; channel <= shost->max_channel;
1639 channel++)
1640 scsi_scan_channel(shost, channel, id, lun,
1641 rescan);
1642 else
1da177e4 1643 scsi_scan_channel(shost, channel, id, lun, rescan);
82f29467 1644 }
0b950672 1645 mutex_unlock(&shost->scan_mutex);
1da177e4
LT
1646
1647 return 0;
1648}
1649
3e082a91
MW
1650static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1651{
1652 struct scsi_device *sdev;
1653 shost_for_each_device(sdev, shost) {
1654 if (scsi_sysfs_add_sdev(sdev) != 0)
1655 scsi_destroy_sdev(sdev);
1656 }
1657}
1658
1659/**
1660 * scsi_prep_async_scan - prepare for an async scan
1661 * @shost: the host which will be scanned
1662 * Returns: a cookie to be passed to scsi_finish_async_scan()
1663 *
1664 * Tells the midlayer this host is going to do an asynchronous scan.
1665 * It reserves the host's position in the scanning list and ensures
1666 * that other asynchronous scans started after this one won't affect the
1667 * ordering of the discovered devices.
1668 */
1aa8fab2 1669static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
3e082a91
MW
1670{
1671 struct async_scan_data *data;
1672
1673 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1674 return NULL;
1675
1676 if (shost->async_scan) {
1677 printk("%s called twice for host %d", __FUNCTION__,
1678 shost->host_no);
1679 dump_stack();
1680 return NULL;
1681 }
1682
1683 data = kmalloc(sizeof(*data), GFP_KERNEL);
1684 if (!data)
1685 goto err;
1686 data->shost = scsi_host_get(shost);
1687 if (!data->shost)
1688 goto err;
1689 init_completion(&data->prev_finished);
1690
1691 spin_lock(&async_scan_lock);
1692 shost->async_scan = 1;
1693 if (list_empty(&scanning_hosts))
1694 complete(&data->prev_finished);
1695 list_add_tail(&data->list, &scanning_hosts);
1696 spin_unlock(&async_scan_lock);
1697
1698 return data;
1699
1700 err:
1701 kfree(data);
1702 return NULL;
1703}
1704
1705/**
1706 * scsi_finish_async_scan - asynchronous scan has finished
1707 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1708 *
1709 * All the devices currently attached to this host have been found.
1710 * This function announces all the devices it has found to the rest
1711 * of the system.
1712 */
1aa8fab2 1713static void scsi_finish_async_scan(struct async_scan_data *data)
3e082a91
MW
1714{
1715 struct Scsi_Host *shost;
1716
1717 if (!data)
1718 return;
1719
1720 shost = data->shost;
1721 if (!shost->async_scan) {
1722 printk("%s called twice for host %d", __FUNCTION__,
1723 shost->host_no);
1724 dump_stack();
1725 return;
1726 }
1727
1728 wait_for_completion(&data->prev_finished);
1729
1730 scsi_sysfs_add_devices(shost);
1731
1732 spin_lock(&async_scan_lock);
1733 shost->async_scan = 0;
1734 list_del(&data->list);
1735 if (!list_empty(&scanning_hosts)) {
1736 struct async_scan_data *next = list_entry(scanning_hosts.next,
1737 struct async_scan_data, list);
1738 complete(&next->prev_finished);
1739 }
1740 spin_unlock(&async_scan_lock);
1741
1742 scsi_host_put(shost);
1743 kfree(data);
1744}
1745
1aa8fab2 1746static void do_scsi_scan_host(struct Scsi_Host *shost)
3e082a91 1747{
1aa8fab2
MW
1748 if (shost->hostt->scan_finished) {
1749 unsigned long start = jiffies;
1750 if (shost->hostt->scan_start)
1751 shost->hostt->scan_start(shost);
1752
1753 while (!shost->hostt->scan_finished(shost, jiffies - start))
1754 msleep(10);
1755 } else {
1756 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
3e082a91 1757 SCAN_WILD_CARD, 0);
1aa8fab2
MW
1758 }
1759}
3e082a91 1760
1aa8fab2
MW
1761static int do_scan_async(void *_data)
1762{
1763 struct async_scan_data *data = _data;
1764 do_scsi_scan_host(data->shost);
3e082a91
MW
1765 scsi_finish_async_scan(data);
1766 return 0;
1767}
1768
1da177e4
LT
1769/**
1770 * scsi_scan_host - scan the given adapter
1771 * @shost: adapter to scan
1772 **/
1773void scsi_scan_host(struct Scsi_Host *shost)
1774{
3e082a91
MW
1775 struct async_scan_data *data;
1776
1777 if (strncmp(scsi_scan_type, "none", 4) == 0)
1778 return;
1779
1780 data = scsi_prep_async_scan(shost);
1781 if (!data) {
1aa8fab2 1782 do_scsi_scan_host(shost);
3e082a91
MW
1783 return;
1784 }
1aa8fab2 1785
3e082a91 1786 kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1da177e4
LT
1787}
1788EXPORT_SYMBOL(scsi_scan_host);
1789
1da177e4
LT
1790void scsi_forget_host(struct Scsi_Host *shost)
1791{
a64358db 1792 struct scsi_device *sdev;
1da177e4
LT
1793 unsigned long flags;
1794
a64358db 1795 restart:
1da177e4 1796 spin_lock_irqsave(shost->host_lock, flags);
a64358db
AS
1797 list_for_each_entry(sdev, &shost->__devices, siblings) {
1798 if (sdev->sdev_state == SDEV_DEL)
1799 continue;
1da177e4 1800 spin_unlock_irqrestore(shost->host_lock, flags);
a64358db
AS
1801 __scsi_remove_device(sdev);
1802 goto restart;
1da177e4
LT
1803 }
1804 spin_unlock_irqrestore(shost->host_lock, flags);
1805}
1806
1807/*
1808 * Function: scsi_get_host_dev()
1809 *
f64a181d 1810 * Purpose: Create a scsi_device that points to the host adapter itself.
1da177e4 1811 *
f64a181d 1812 * Arguments: SHpnt - Host that needs a scsi_device
1da177e4
LT
1813 *
1814 * Lock status: None assumed.
1815 *
f64a181d 1816 * Returns: The scsi_device or NULL
1da177e4
LT
1817 *
1818 * Notes:
f64a181d 1819 * Attach a single scsi_device to the Scsi_Host - this should
1da177e4
LT
1820 * be made to look like a "pseudo-device" that points to the
1821 * HA itself.
1822 *
1823 * Note - this device is not accessible from any high-level
1824 * drivers (including generics), which is probably not
1825 * optimal. We can add hooks later to attach
1826 */
1827struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1828{
e517d313 1829 struct scsi_device *sdev = NULL;
1da177e4
LT
1830 struct scsi_target *starget;
1831
0b950672 1832 mutex_lock(&shost->scan_mutex);
e517d313
AS
1833 if (!scsi_host_scan_allowed(shost))
1834 goto out;
1da177e4
LT
1835 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1836 if (!starget)
e517d313 1837 goto out;
1da177e4
LT
1838
1839 sdev = scsi_alloc_sdev(starget, 0, NULL);
1840 if (sdev) {
1841 sdev->sdev_gendev.parent = get_device(&starget->dev);
1842 sdev->borken = 0;
884d25cc
JB
1843 } else
1844 scsi_target_reap(starget);
1da177e4 1845 put_device(&starget->dev);
e517d313 1846 out:
0b950672 1847 mutex_unlock(&shost->scan_mutex);
1da177e4
LT
1848 return sdev;
1849}
1850EXPORT_SYMBOL(scsi_get_host_dev);
1851
1852/*
1853 * Function: scsi_free_host_dev()
1854 *
1855 * Purpose: Free a scsi_device that points to the host adapter itself.
1856 *
f64a181d 1857 * Arguments: SHpnt - Host that needs a scsi_device
1da177e4
LT
1858 *
1859 * Lock status: None assumed.
1860 *
1861 * Returns: Nothing
1862 *
1863 * Notes:
1864 */
1865void scsi_free_host_dev(struct scsi_device *sdev)
1866{
1867 BUG_ON(sdev->id != sdev->host->this_id);
1868
6f3a2024 1869 scsi_destroy_sdev(sdev);
1da177e4
LT
1870}
1871EXPORT_SYMBOL(scsi_free_host_dev);
1872