This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / scsi / scsi_scan.c
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
12  * device attached, a Scsi_Device is allocated and setup for it.
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
20  *              Scsi_Device for it.
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
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/blkdev.h>
33 #include <asm/semaphore.h>
34
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/scsi_driver.h>
38 #include <scsi/scsi_devinfo.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_request.h>
41 #include <scsi/scsi_transport.h>
42
43 #include "scsi_priv.h"
44 #include "scsi_logging.h"
45
46 #define ALLOC_FAILURE_MSG       KERN_ERR "%s: Allocation failure during" \
47         " SCSI scanning, some SCSI devices might not be configured\n"
48
49 /*
50  * Default timeout
51  */
52 #define SCSI_TIMEOUT (2*HZ)
53
54 /*
55  * Prefix values for the SCSI id's (stored in driverfs name field)
56  */
57 #define SCSI_UID_SER_NUM 'S'
58 #define SCSI_UID_UNKNOWN 'Z'
59
60 /*
61  * Return values of some of the scanning functions.
62  *
63  * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
64  * includes allocation or general failures preventing IO from being sent.
65  *
66  * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
67  * on the given LUN.
68  *
69  * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
70  * given LUN.
71  */
72 #define SCSI_SCAN_NO_RESPONSE           0
73 #define SCSI_SCAN_TARGET_PRESENT        1
74 #define SCSI_SCAN_LUN_PRESENT           2
75
76 static char *scsi_null_device_strs = "nullnullnullnull";
77
78 #define MAX_SCSI_LUNS   512
79
80 #ifdef CONFIG_SCSI_MULTI_LUN
81 static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
82 #else
83 static unsigned int max_scsi_luns = 1;
84 #endif
85
86 module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR);
87 MODULE_PARM_DESC(max_luns,
88                  "last scsi LUN (should be between 1 and 2^32-1)");
89
90 /*
91  * max_scsi_report_luns: the maximum number of LUNS that will be
92  * returned from the REPORT LUNS command. 8 times this value must
93  * be allocated. In theory this could be up to an 8 byte value, but
94  * in practice, the maximum number of LUNs suppored by any device
95  * is about 16k.
96  */
97 static unsigned int max_scsi_report_luns = 511;
98
99 module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR);
100 MODULE_PARM_DESC(max_report_luns,
101                  "REPORT LUNS maximum number of LUNS received (should be"
102                  " between 1 and 16384)");
103
104 static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
105
106 module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR);
107 MODULE_PARM_DESC(inq_timeout, 
108                  "Timeout (in seconds) waiting for devices to answer INQUIRY."
109                  " Default is 5. Some non-compliant devices need more.");
110
111 /**
112  * scsi_unlock_floptical - unlock device via a special MODE SENSE command
113  * @sreq:       used to send the command
114  * @result:     area to store the result of the MODE SENSE
115  *
116  * Description:
117  *     Send a vendor specific MODE SENSE (not a MODE SELECT) command using
118  *     @sreq to unlock a device, storing the (unused) results into result.
119  *     Called for BLIST_KEY devices.
120  **/
121 static void scsi_unlock_floptical(struct scsi_request *sreq,
122                                   unsigned char *result)
123 {
124         unsigned char scsi_cmd[MAX_COMMAND_SIZE];
125
126         printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
127         scsi_cmd[0] = MODE_SENSE;
128         scsi_cmd[1] = 0;
129         scsi_cmd[2] = 0x2e;
130         scsi_cmd[3] = 0;
131         scsi_cmd[4] = 0x2a;     /* size */
132         scsi_cmd[5] = 0;
133         sreq->sr_cmd_len = 0;
134         sreq->sr_data_direction = DMA_FROM_DEVICE;
135         scsi_wait_req(sreq, scsi_cmd, result, 0x2a /* size */, SCSI_TIMEOUT, 3);
136 }
137
138 /**
139  * print_inquiry - printk the inquiry information
140  * @inq_result: printk this SCSI INQUIRY
141  *
142  * Description:
143  *     printk the vendor, model, and other information found in the
144  *     INQUIRY data in @inq_result.
145  *
146  * Notes:
147  *     Remove this, and replace with a hotplug event that logs any
148  *     relevant information.
149  **/
150 static void print_inquiry(unsigned char *inq_result)
151 {
152         int i;
153
154         printk(KERN_NOTICE "  Vendor: ");
155         for (i = 8; i < 16; i++)
156                 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
157                         printk("%c", inq_result[i]);
158                 else
159                         printk(" ");
160
161         printk("  Model: ");
162         for (i = 16; i < 32; i++)
163                 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
164                         printk("%c", inq_result[i]);
165                 else
166                         printk(" ");
167
168         printk("  Rev: ");
169         for (i = 32; i < 36; i++)
170                 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
171                         printk("%c", inq_result[i]);
172                 else
173                         printk(" ");
174
175         printk("\n");
176
177         i = inq_result[0] & 0x1f;
178
179         printk(KERN_NOTICE "  Type:   %s ",
180                i <
181                MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
182                "Unknown          ");
183         printk("                 ANSI SCSI revision: %02x",
184                inq_result[2] & 0x07);
185         if ((inq_result[2] & 0x07) == 1 && (inq_result[3] & 0x0f) == 1)
186                 printk(" CCS\n");
187         else
188                 printk("\n");
189 }
190
191 /**
192  * scsi_alloc_sdev - allocate and setup a scsi_Device
193  *
194  * Description:
195  *     Allocate, initialize for io, and return a pointer to a scsi_Device.
196  *     Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
197  *     adds scsi_Device to the appropriate list.
198  *
199  * Return value:
200  *     scsi_Device pointer, or NULL on failure.
201  **/
202 static struct scsi_device *scsi_alloc_sdev(struct Scsi_Host *shost,
203                 uint channel, uint id, uint lun, void *hostdata)
204 {
205         struct scsi_device *sdev;
206         unsigned long flags;
207         int display_failure_msg = 1, ret;
208
209         sdev = kmalloc(sizeof(*sdev) + shost->transportt->device_size,
210                        GFP_ATOMIC);
211         if (!sdev)
212                 goto out;
213
214         memset(sdev, 0, sizeof(*sdev));
215         sdev->vendor = scsi_null_device_strs;
216         sdev->model = scsi_null_device_strs;
217         sdev->rev = scsi_null_device_strs;
218         sdev->host = shost;
219         sdev->id = id;
220         sdev->lun = lun;
221         sdev->channel = channel;
222         sdev->sdev_state = SDEV_CREATED;
223         INIT_LIST_HEAD(&sdev->siblings);
224         INIT_LIST_HEAD(&sdev->same_target_siblings);
225         INIT_LIST_HEAD(&sdev->cmd_list);
226         INIT_LIST_HEAD(&sdev->starved_entry);
227         spin_lock_init(&sdev->list_lock);
228
229         /* usually NULL and set by ->slave_alloc instead */
230         sdev->hostdata = hostdata;
231
232         /* if the device needs this changing, it may do so in the
233          * slave_configure function */
234         sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
235
236         /*
237          * Some low level driver could use device->type
238          */
239         sdev->type = -1;
240
241         /*
242          * Assume that the device will have handshaking problems,
243          * and then fix this field later if it turns out it
244          * doesn't
245          */
246         sdev->borken = 1;
247
248         spin_lock_init(&sdev->sdev_lock);
249         sdev->request_queue = scsi_alloc_queue(sdev);
250         if (!sdev->request_queue)
251                 goto out_free_dev;
252
253         sdev->request_queue->queuedata = sdev;
254         scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
255
256         if (shost->transportt->device_setup) {
257                 if (shost->transportt->device_setup(sdev))
258                         goto out_free_queue;
259         }
260
261         if (shost->hostt->slave_alloc) {
262                 ret = shost->hostt->slave_alloc(sdev);
263                 if (ret) {
264                         /*
265                          * if LLDD reports slave not present, don't clutter
266                          * console with alloc failure messages
267                          */
268                         if (ret == -ENXIO)
269                                 display_failure_msg = 0;
270                         goto out_device_destroy;
271                 }
272         }
273
274         if (scsi_sysfs_device_initialize(sdev) != 0)
275                 goto out_cleanup_slave;
276
277
278         /* NOTE: this target initialisation code depends critically on
279          * lun scanning being sequential. */
280         if (scsi_sysfs_target_initialize(sdev))
281                 goto out_remove_siblings;
282
283         return sdev;
284
285 out_remove_siblings:
286         spin_lock_irqsave(shost->host_lock, flags);
287         list_del(&sdev->siblings);
288         list_del(&sdev->same_target_siblings);
289         spin_unlock_irqrestore(shost->host_lock, flags);
290 out_cleanup_slave:
291         if (shost->hostt->slave_destroy)
292                 shost->hostt->slave_destroy(sdev);
293 out_device_destroy:
294         if (shost->transportt->device_destroy)
295                 shost->transportt->device_destroy(sdev);
296 out_free_queue:
297         scsi_free_queue(sdev->request_queue);
298 out_free_dev:
299         kfree(sdev);
300 out:
301         if (display_failure_msg)
302                 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
303         return NULL;
304 }
305
306 /**
307  * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
308  * @sreq:       used to send the INQUIRY
309  * @inq_result: area to store the INQUIRY result
310  * @bflags:     store any bflags found here
311  *
312  * Description:
313  *     Probe the lun associated with @sreq using a standard SCSI INQUIRY;
314  *
315  *     If the INQUIRY is successful, sreq->sr_result is zero and: the
316  *     INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
317  *     are copied to the Scsi_Device at @sreq->sr_device (sdev);
318  *     any flags value is stored in *@bflags.
319  **/
320 static void scsi_probe_lun(struct scsi_request *sreq, char *inq_result,
321                            int *bflags)
322 {
323         struct scsi_device *sdev = sreq->sr_device;     /* a bit ugly */
324         unsigned char scsi_cmd[MAX_COMMAND_SIZE];
325         int possible_inq_resp_len;
326         int count = 0;
327
328         *bflags = 0;
329  repeat_inquiry:
330         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY to host %d"
331                         " channel %d id %d lun %d\n", sdev->host->host_no,
332                         sdev->channel, sdev->id, sdev->lun));
333
334         memset(scsi_cmd, 0, 6);
335         scsi_cmd[0] = INQUIRY;
336         scsi_cmd[4] = 36;       /* issue conservative alloc_length */
337         sreq->sr_cmd_len = 0;
338         sreq->sr_data_direction = DMA_FROM_DEVICE;
339
340         memset(inq_result, 0, 36);
341         scsi_wait_req(sreq, (void *) scsi_cmd, (void *) inq_result, 36,
342                       HZ/2 + HZ*scsi_inq_timeout, 3);
343
344         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: 1st INQUIRY %s with"
345                         " code 0x%x\n", sreq->sr_result ?
346                         "failed" : "successful", sreq->sr_result));
347         ++count;
348
349         if (sreq->sr_result) {
350                 if ((driver_byte(sreq->sr_result) & DRIVER_SENSE) != 0 &&
351                     (sreq->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION &&
352                     (sreq->sr_sense_buffer[12] == 0x28 ||
353                      sreq->sr_sense_buffer[12] == 0x29) &&
354                     sreq->sr_sense_buffer[13] == 0) {
355                         /* not-ready to ready transition or power-on - good */
356                         /* dpg: bogus? INQUIRY never returns UNIT_ATTENTION */
357                         /* Supposedly, but many buggy devices do so anyway */
358                         if (count < 3)
359                                 goto repeat_inquiry;
360                 }
361                 /*
362                  * assume no peripheral if any other sort of error
363                  */
364                 return;
365         }
366
367         /*
368          * Get any flags for this device.
369          *
370          * XXX add a bflags to Scsi_Device, and replace the corresponding
371          * bit fields in Scsi_Device, so bflags need not be passed as an
372          * argument.
373          */
374         *bflags |= scsi_get_device_flags(sdev, &inq_result[8], &inq_result[16]);
375
376         possible_inq_resp_len = (unsigned char) inq_result[4] + 5;
377         if (BLIST_INQUIRY_36 & *bflags)
378                 possible_inq_resp_len = 36;
379         else if (BLIST_INQUIRY_58 & *bflags)
380                 possible_inq_resp_len = 58;
381         else if (possible_inq_resp_len > 255)
382                 possible_inq_resp_len = 36;     /* sanity */
383
384         if (possible_inq_resp_len > 36) {       /* do additional INQUIRY */
385                 memset(scsi_cmd, 0, 6);
386                 scsi_cmd[0] = INQUIRY;
387                 scsi_cmd[4] = (unsigned char) possible_inq_resp_len;
388                 sreq->sr_cmd_len = 0;
389                 sreq->sr_data_direction = DMA_FROM_DEVICE;
390                 /*
391                  * re-zero inq_result just to be safe.
392                  */
393                 memset(inq_result, 0, possible_inq_resp_len);
394                 scsi_wait_req(sreq, (void *) scsi_cmd,
395                               (void *) inq_result,
396                               possible_inq_resp_len, (1+scsi_inq_timeout)*(HZ/2), 3);
397                 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: 2nd INQUIRY"
398                                 " %s with code 0x%x\n", sreq->sr_result ?
399                                 "failed" : "successful", sreq->sr_result));
400                 if (sreq->sr_result) {
401                         /* if the longer inquiry has failed, flag the device
402                          * as only accepting 36 byte inquiries and retry the
403                          * 36 byte inquiry */
404                         printk(KERN_INFO "scsi scan: %d byte inquiry failed"
405                                " with code %d.  Consider BLIST_INQUIRY_36 for"
406                                " this device\n", possible_inq_resp_len,
407                                sreq->sr_result);
408                         *bflags = BLIST_INQUIRY_36;
409                         goto repeat_inquiry;
410                 }
411
412                 /*
413                  * The INQUIRY can change, this means the length can change.
414                  */
415                 possible_inq_resp_len = (unsigned char) inq_result[4] + 5;
416                 if (BLIST_INQUIRY_58 & *bflags)
417                         possible_inq_resp_len = 58;
418                 else if (possible_inq_resp_len > 255)
419                         possible_inq_resp_len = 36;     /* sanity */
420         }
421
422         sdev->inquiry_len = possible_inq_resp_len;
423
424         /*
425          * XXX Abort if the response length is less than 36? If less than
426          * 32, the lookup of the device flags (above) could be invalid,
427          * and it would be possible to take an incorrect action - we do
428          * not want to hang because of a short INQUIRY. On the flip side,
429          * if the device is spun down or becoming ready (and so it gives a
430          * short INQUIRY), an abort here prevents any further use of the
431          * device, including spin up.
432          *
433          * Related to the above issue:
434          *
435          * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
436          * and if not ready, sent a START_STOP to start (maybe spin up) and
437          * then send the INQUIRY again, since the INQUIRY can change after
438          * a device is initialized.
439          *
440          * Ideally, start a device if explicitly asked to do so.  This
441          * assumes that a device is spun up on power on, spun down on
442          * request, and then spun up on request.
443          */
444
445         /*
446          * The scanning code needs to know the scsi_level, even if no
447          * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
448          * non-zero LUNs can be scanned.
449          */
450         sdev->scsi_level = inq_result[2] & 0x07;
451         if (sdev->scsi_level >= 2 ||
452             (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
453                 sdev->scsi_level++;
454
455         return;
456 }
457
458 /**
459  * scsi_add_lun - allocate and fully initialze a Scsi_Device
460  * @sdevscan:   holds information to be stored in the new Scsi_Device
461  * @sdevnew:    store the address of the newly allocated Scsi_Device
462  * @inq_result: holds the result of a previous INQUIRY to the LUN
463  * @bflags:     black/white list flag
464  *
465  * Description:
466  *     Allocate and initialize a Scsi_Device matching sdevscan. Optionally
467  *     set fields based on values in *@bflags. If @sdevnew is not
468  *     NULL, store the address of the new Scsi_Device in *@sdevnew (needed
469  *     when scanning a particular LUN).
470  *
471  * Return:
472  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device
473  *     SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized
474  **/
475 static int scsi_add_lun(struct scsi_device *sdev, char *inq_result, int *bflags)
476 {
477         /*
478          * XXX do not save the inquiry, since it can change underneath us,
479          * save just vendor/model/rev.
480          *
481          * Rather than save it and have an ioctl that retrieves the saved
482          * value, have an ioctl that executes the same INQUIRY code used
483          * in scsi_probe_lun, let user level programs doing INQUIRY
484          * scanning run at their own risk, or supply a user level program
485          * that can correctly scan.
486          */
487         sdev->inquiry = kmalloc(sdev->inquiry_len, GFP_ATOMIC);
488         if (sdev->inquiry == NULL) {
489                 return SCSI_SCAN_NO_RESPONSE;
490         }
491
492         memcpy(sdev->inquiry, inq_result, sdev->inquiry_len);
493         sdev->vendor = (char *) (sdev->inquiry + 8);
494         sdev->model = (char *) (sdev->inquiry + 16);
495         sdev->rev = (char *) (sdev->inquiry + 32);
496
497         if (*bflags & BLIST_ISROM) {
498                 /*
499                  * It would be better to modify sdev->type, and set
500                  * sdev->removable, but then the print_inquiry() output
501                  * would not show TYPE_ROM; if print_inquiry() is removed
502                  * the issue goes away.
503                  */
504                 inq_result[0] = TYPE_ROM;
505                 inq_result[1] |= 0x80;  /* removable */
506         }
507
508         switch (sdev->type = (inq_result[0] & 0x1f)) {
509         case TYPE_TAPE:
510         case TYPE_DISK:
511         case TYPE_PRINTER:
512         case TYPE_MOD:
513         case TYPE_PROCESSOR:
514         case TYPE_SCANNER:
515         case TYPE_MEDIUM_CHANGER:
516         case TYPE_ENCLOSURE:
517         case TYPE_COMM:
518                 sdev->writeable = 1;
519                 break;
520         case TYPE_WORM:
521         case TYPE_ROM:
522                 sdev->writeable = 0;
523                 break;
524         default:
525                 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
526         }
527
528         print_inquiry(inq_result);
529
530         /*
531          * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
532          * spec says: The device server is capable of supporting the
533          * specified peripheral device type on this logical unit. However,
534          * the physical device is not currently connected to this logical
535          * unit.
536          *
537          * The above is vague, as it implies that we could treat 001 and
538          * 011 the same. Stay compatible with previous code, and create a
539          * Scsi_Device for a PQ of 1
540          *
541          * Don't set the device offline here; rather let the upper
542          * level drivers eval the PQ to decide whether they should
543          * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
544          */ 
545
546         sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
547         sdev->removable = (0x80 & inq_result[1]) >> 7;
548         sdev->lockable = sdev->removable;
549         sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
550
551         if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
552                 inq_result[56] & 0x04))
553                 sdev->ppr = 1;
554         if (inq_result[7] & 0x60)
555                 sdev->wdtr = 1;
556         if (inq_result[7] & 0x10)
557                 sdev->sdtr = 1;
558
559         sprintf(sdev->devfs_name, "scsi/host%d/bus%d/target%d/lun%d",
560                                 sdev->host->host_no, sdev->channel,
561                                 sdev->id, sdev->lun);
562
563         /*
564          * End driverfs/devfs code.
565          */
566
567         if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
568             !(*bflags & BLIST_NOTQ))
569                 sdev->tagged_supported = 1;
570         /*
571          * Some devices (Texel CD ROM drives) have handshaking problems
572          * when used with the Seagate controllers. borken is initialized
573          * to 1, and then set it to 0 here.
574          */
575         if ((*bflags & BLIST_BORKEN) == 0)
576                 sdev->borken = 0;
577
578         /*
579          * Some devices may not want to have a start command automatically
580          * issued when a device is added.
581          */
582         if (*bflags & BLIST_NOSTARTONADD)
583                 sdev->no_start_on_add = 1;
584
585         if (*bflags & BLIST_SINGLELUN)
586                 sdev->single_lun = 1;
587
588
589         sdev->use_10_for_rw = 1;
590
591         if (*bflags & BLIST_MS_SKIP_PAGE_08)
592                 sdev->skip_ms_page_8 = 1;
593
594         if (*bflags & BLIST_MS_SKIP_PAGE_3F)
595                 sdev->skip_ms_page_3f = 1;
596
597         if (*bflags & BLIST_USE_10_BYTE_MS)
598                 sdev->use_10_for_ms = 1;
599
600         /* set the device running here so that slave configure
601          * may do I/O */
602         scsi_device_set_state(sdev, SDEV_RUNNING);
603
604         if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
605                 sdev->use_192_bytes_for_3f = 1;
606
607         if (*bflags & BLIST_NOT_LOCKABLE)
608                 sdev->lockable = 0;
609
610         if (sdev->host->transportt->device_configure)
611                 sdev->host->transportt->device_configure(sdev);
612
613         if (sdev->host->hostt->slave_configure)
614                 sdev->host->hostt->slave_configure(sdev);
615
616         /*
617          * Ok, the device is now all set up, we can
618          * register it and tell the rest of the kernel
619          * about it.
620          */
621         scsi_sysfs_add_sdev(sdev);
622
623         return SCSI_SCAN_LUN_PRESENT;
624 }
625
626 /**
627  * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
628  * @sdevscan:   probe the LUN corresponding to this Scsi_Device
629  * @sdevnew:    store the value of any new Scsi_Device allocated
630  * @bflagsp:    store bflags here if not NULL
631  *
632  * Description:
633  *     Call scsi_probe_lun, if a LUN with an attached device is found,
634  *     allocate and set it up by calling scsi_add_lun.
635  *
636  * Return:
637  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device
638  *     SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
639  *         attached at the LUN
640  *     SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized
641  **/
642 static int scsi_probe_and_add_lun(struct Scsi_Host *host,
643                 uint channel, uint id, uint lun, int *bflagsp,
644                 struct scsi_device **sdevp, int rescan, void *hostdata)
645 {
646         struct scsi_device *sdev;
647         struct scsi_request *sreq;
648         unsigned char *result;
649         int bflags, res = SCSI_SCAN_NO_RESPONSE;
650
651         /*
652          * The rescan flag is used as an optimization, the first scan of a
653          * host adapter calls into here with rescan == 0.
654          */
655         if (rescan) {
656                 sdev = scsi_device_lookup(host, channel, id, lun);
657                 if (sdev) {
658                         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
659                                 "scsi scan: device exists on <%d:%d:%d:%d>\n",
660                                 host->host_no, channel, id, lun));
661                         if (sdevp)
662                                 *sdevp = sdev;
663                         if (bflagsp)
664                                 *bflagsp = scsi_get_device_flags(sdev,
665                                                                  sdev->vendor,
666                                                                  sdev->model);
667                         /* XXX: bandaid until callers do refcounting */
668                         scsi_device_put(sdev);
669                         return SCSI_SCAN_LUN_PRESENT;
670                 }
671         }
672
673         sdev = scsi_alloc_sdev(host, channel, id, lun, hostdata);
674         if (!sdev)
675                 goto out;
676         sreq = scsi_allocate_request(sdev, GFP_ATOMIC);
677         if (!sreq)
678                 goto out_free_sdev;
679         result = kmalloc(256, GFP_ATOMIC |
680                         (host->unchecked_isa_dma) ? __GFP_DMA : 0);
681         if (!result)
682                 goto out_free_sreq;
683
684         scsi_probe_lun(sreq, result, &bflags);
685         if (sreq->sr_result)
686                 goto out_free_result;
687
688         /*
689          * result contains valid SCSI INQUIRY data.
690          */
691         if ((result[0] >> 5) == 3) {
692                 /*
693                  * For a Peripheral qualifier 3 (011b), the SCSI
694                  * spec says: The device server is not capable of
695                  * supporting a physical device on this logical
696                  * unit.
697                  *
698                  * For disks, this implies that there is no
699                  * logical disk configured at sdev->lun, but there
700                  * is a target id responding.
701                  */
702                 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
703                                         "scsi scan: peripheral qualifier of 3,"
704                                         " no device added\n"));
705                 res = SCSI_SCAN_TARGET_PRESENT;
706                 goto out_free_result;
707         }
708
709         res = scsi_add_lun(sdev, result, &bflags);
710         if (res == SCSI_SCAN_LUN_PRESENT) {
711                 if (bflags & BLIST_KEY) {
712                         sdev->lockable = 0;
713                         scsi_unlock_floptical(sreq, result);
714                 }
715                 if (bflagsp)
716                         *bflagsp = bflags;
717         }
718
719  out_free_result:
720         kfree(result);
721  out_free_sreq:
722         scsi_release_request(sreq);
723  out_free_sdev:
724         if (res == SCSI_SCAN_LUN_PRESENT) {
725                 if (sdevp)
726                         *sdevp = sdev;
727         } else {
728                 if (sdev->host->hostt->slave_destroy)
729                         sdev->host->hostt->slave_destroy(sdev);
730                 if (sdev->host->transportt->device_destroy)
731                         sdev->host->transportt->device_destroy(sdev);
732                 put_device(&sdev->sdev_gendev);
733         }
734  out:
735         return res;
736 }
737
738 /**
739  * scsi_sequential_lun_scan - sequentially scan a SCSI target
740  * @sdevscan:   scan the host, channel, and id of this Scsi_Device
741  * @bflags:     black/white list flag for LUN 0
742  * @lun0_res:   result of scanning LUN 0
743  *
744  * Description:
745  *     Generally, scan from LUN 1 (LUN 0 is assumed to already have been
746  *     scanned) to some maximum lun until a LUN is found with no device
747  *     attached. Use the bflags to figure out any oddities.
748  *
749  *     Modifies sdevscan->lun.
750  **/
751 static void scsi_sequential_lun_scan(struct Scsi_Host *shost, uint channel,
752                 uint id, int bflags, int lun0_res, int scsi_level, int rescan)
753 {
754         unsigned int sparse_lun, lun, max_dev_lun;
755
756         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
757                         " host %d channel %d id %d\n", shost->host_no,
758                         channel, id));
759
760         max_dev_lun = min(max_scsi_luns, shost->max_lun);
761         /*
762          * If this device is known to support sparse multiple units,
763          * override the other settings, and scan all of them. Normally,
764          * SCSI-3 devices should be scanned via the REPORT LUNS.
765          */
766         if (bflags & BLIST_SPARSELUN) {
767                 max_dev_lun = shost->max_lun;
768                 sparse_lun = 1;
769         } else
770                 sparse_lun = 0;
771
772         /*
773          * If not sparse lun and no device attached at LUN 0 do not scan
774          * any further.
775          */
776         if (!sparse_lun && (lun0_res != SCSI_SCAN_LUN_PRESENT))
777                 return;
778
779         /*
780          * If less than SCSI_1_CSS, and no special lun scaning, stop
781          * scanning; this matches 2.4 behaviour, but could just be a bug
782          * (to continue scanning a SCSI_1_CSS device).
783          *
784          * This test is broken.  We might not have any device on lun0 for
785          * a sparselun device, and if that's the case then how would we
786          * know the real scsi_level, eh?  It might make sense to just not
787          * scan any SCSI_1 device for non-0 luns, but that check would best
788          * go into scsi_alloc_sdev() and just have it return null when asked
789          * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
790          *
791         if ((sdevscan->scsi_level < SCSI_1_CCS) &&
792             ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
793              == 0))
794                 return;
795          */
796         /*
797          * If this device is known to support multiple units, override
798          * the other settings, and scan all of them.
799          */
800         if (bflags & BLIST_FORCELUN)
801                 max_dev_lun = shost->max_lun;
802         /*
803          * REGAL CDC-4X: avoid hang after LUN 4
804          */
805         if (bflags & BLIST_MAX5LUN)
806                 max_dev_lun = min(5U, max_dev_lun);
807         /*
808          * Do not scan SCSI-2 or lower device past LUN 7, unless
809          * BLIST_LARGELUN.
810          */
811         if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
812                 max_dev_lun = min(8U, max_dev_lun);
813
814         /*
815          * We have already scanned LUN 0, so start at LUN 1. Keep scanning
816          * until we reach the max, or no LUN is found and we are not
817          * sparse_lun.
818          */
819         for (lun = 1; lun < max_dev_lun; ++lun)
820                 if ((scsi_probe_and_add_lun(shost, channel, id, lun,
821                       NULL, NULL, rescan, NULL) != SCSI_SCAN_LUN_PRESENT) &&
822                     !sparse_lun)
823                         return;
824 }
825
826 /**
827  * scsilun_to_int: convert a scsi_lun to an int
828  * @scsilun:    struct scsi_lun to be converted.
829  *
830  * Description:
831  *     Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
832  *     integer, and return the result. The caller must check for
833  *     truncation before using this function.
834  *
835  * Notes:
836  *     The struct scsi_lun is assumed to be four levels, with each level
837  *     effectively containing a SCSI byte-ordered (big endian) short; the
838  *     addressing bits of each level are ignored (the highest two bits).
839  *     For a description of the LUN format, post SCSI-3 see the SCSI
840  *     Architecture Model, for SCSI-3 see the SCSI Controller Commands.
841  *
842  *     Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
843  *     the integer: 0x0b030a04
844  **/
845 static int scsilun_to_int(struct scsi_lun *scsilun)
846 {
847         int i;
848         unsigned int lun;
849
850         lun = 0;
851         for (i = 0; i < sizeof(lun); i += 2)
852                 lun = lun | (((scsilun->scsi_lun[i] << 8) |
853                               scsilun->scsi_lun[i + 1]) << (i * 8));
854         return lun;
855 }
856
857 /**
858  * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
859  * @sdevscan:   scan the host, channel, and id of this Scsi_Device
860  *
861  * Description:
862  *     If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
863  *     command, and scan the resulting list of LUNs by calling
864  *     scsi_probe_and_add_lun.
865  *
866  *     Modifies sdevscan->lun.
867  *
868  * Return:
869  *     0: scan completed (or no memory, so further scanning is futile)
870  *     1: no report lun scan, or not configured
871  **/
872 static int scsi_report_lun_scan(struct scsi_device *sdev, int bflags,
873                                 int rescan)
874 {
875         char devname[64];
876         unsigned char scsi_cmd[MAX_COMMAND_SIZE];
877         unsigned int length;
878         unsigned int lun;
879         unsigned int num_luns;
880         unsigned int retries;
881         struct scsi_lun *lunp, *lun_data;
882         struct scsi_request *sreq;
883         u8 *data;
884
885         /*
886          * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
887          * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
888          * support more than 8 LUNs.
889          */
890         if ((bflags & BLIST_NOREPORTLUN) || 
891              sdev->scsi_level < SCSI_2 ||
892             (sdev->scsi_level < SCSI_3 && 
893              (!(bflags & BLIST_REPORTLUN2) || sdev->host->max_lun <= 8)) )
894                 return 1;
895         if (bflags & BLIST_NOLUN)
896                 return 0;
897
898         sreq = scsi_allocate_request(sdev, GFP_ATOMIC);
899         if (!sreq)
900                 goto out;
901
902         sprintf(devname, "host %d channel %d id %d",
903                 sdev->host->host_no, sdev->channel, sdev->id);
904
905         /*
906          * Allocate enough to hold the header (the same size as one scsi_lun)
907          * plus the max number of luns we are requesting.
908          *
909          * Reallocating and trying again (with the exact amount we need)
910          * would be nice, but then we need to somehow limit the size
911          * allocated based on the available memory and the limits of
912          * kmalloc - we don't want a kmalloc() failure of a huge value to
913          * prevent us from finding any LUNs on this target.
914          */
915         length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
916         lun_data = kmalloc(length, GFP_ATOMIC |
917                            (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
918         if (!lun_data)
919                 goto out_release_request;
920
921         scsi_cmd[0] = REPORT_LUNS;
922
923         /*
924          * bytes 1 - 5: reserved, set to zero.
925          */
926         memset(&scsi_cmd[1], 0, 5);
927
928         /*
929          * bytes 6 - 9: length of the command.
930          */
931         scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
932         scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
933         scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
934         scsi_cmd[9] = (unsigned char) length & 0xff;
935
936         scsi_cmd[10] = 0;       /* reserved */
937         scsi_cmd[11] = 0;       /* control */
938         sreq->sr_cmd_len = 0;
939         sreq->sr_data_direction = DMA_FROM_DEVICE;
940
941         /*
942          * We can get a UNIT ATTENTION, for example a power on/reset, so
943          * retry a few times (like sd.c does for TEST UNIT READY).
944          * Experience shows some combinations of adapter/devices get at
945          * least two power on/resets.
946          *
947          * Illegal requests (for devices that do not support REPORT LUNS)
948          * should come through as a check condition, and will not generate
949          * a retry.
950          */
951         for (retries = 0; retries < 3; retries++) {
952                 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
953                                 " REPORT LUNS to %s (try %d)\n", devname,
954                                 retries));
955                 scsi_wait_req(sreq, scsi_cmd, lun_data, length,
956                                 SCSI_TIMEOUT + 4*HZ, 3);
957                 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
958                                 " %s (try %d) result 0x%x\n", sreq->sr_result
959                                 ?  "failed" : "successful", retries,
960                                 sreq->sr_result));
961                 if (sreq->sr_result == 0 ||
962                     sreq->sr_sense_buffer[2] != UNIT_ATTENTION)
963                         break;
964         }
965
966         if (sreq->sr_result) {
967                 /*
968                  * The device probably does not support a REPORT LUN command
969                  */
970                 kfree(lun_data);
971                 scsi_release_request(sreq);
972                 return 1;
973         }
974         scsi_release_request(sreq);
975
976         /*
977          * Get the length from the first four bytes of lun_data.
978          */
979         data = (u8 *) lun_data->scsi_lun;
980         length = ((data[0] << 24) | (data[1] << 16) |
981                   (data[2] << 8) | (data[3] << 0));
982
983         num_luns = (length / sizeof(struct scsi_lun));
984         if (num_luns > max_scsi_report_luns) {
985                 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
986                        " of %d luns reported, try increasing"
987                        " max_scsi_report_luns.\n", devname,
988                        max_scsi_report_luns, num_luns);
989                 num_luns = max_scsi_report_luns;
990         }
991
992         SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUN scan of"
993                         " host %d channel %d id %d\n", sdev->host->host_no,
994                         sdev->channel, sdev->id));
995
996         /*
997          * Scan the luns in lun_data. The entry at offset 0 is really
998          * the header, so start at 1 and go up to and including num_luns.
999          */
1000         for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1001                 lun = scsilun_to_int(lunp);
1002
1003                 /*
1004                  * Check if the unused part of lunp is non-zero, and so
1005                  * does not fit in lun.
1006                  */
1007                 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1008                         int i;
1009
1010                         /*
1011                          * Output an error displaying the LUN in byte order,
1012                          * this differs from what linux would print for the
1013                          * integer LUN value.
1014                          */
1015                         printk(KERN_WARNING "scsi: %s lun 0x", devname);
1016                         data = (char *)lunp->scsi_lun;
1017                         for (i = 0; i < sizeof(struct scsi_lun); i++)
1018                                 printk("%02x", data[i]);
1019                         printk(" has a LUN larger than currently supported.\n");
1020                 } else if (lun == 0) {
1021                         /*
1022                          * LUN 0 has already been scanned.
1023                          */
1024                 } else if (lun > sdev->host->max_lun) {
1025                         printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1026                                " than allowed by the host adapter\n",
1027                                devname, lun);
1028                 } else {
1029                         int res;
1030
1031                         res = scsi_probe_and_add_lun(sdev->host, sdev->channel,
1032                                 sdev->id, lun, NULL, NULL, rescan, NULL);
1033                         if (res == SCSI_SCAN_NO_RESPONSE) {
1034                                 /*
1035                                  * Got some results, but now none, abort.
1036                                  */
1037                                 printk(KERN_ERR "scsi: Unexpected response"
1038                                        " from %s lun %d while scanning, scan"
1039                                        " aborted\n", devname, lun);
1040                                 break;
1041                         }
1042                 }
1043         }
1044
1045         kfree(lun_data);
1046         return 0;
1047
1048  out_release_request:
1049         scsi_release_request(sreq);
1050  out:
1051         /*
1052          * We are out of memory, don't try scanning any further.
1053          */
1054         printk(ALLOC_FAILURE_MSG, __FUNCTION__);
1055         return 0;
1056 }
1057
1058 struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1059                 uint id, uint lun, void *hostdata)
1060 {
1061         struct scsi_device *sdev;
1062         int res;
1063
1064         down(&shost->scan_mutex);
1065         res = scsi_probe_and_add_lun(shost, channel, id, lun, NULL,
1066                                      &sdev, 1, hostdata);
1067         if (res != SCSI_SCAN_LUN_PRESENT)
1068                 sdev = ERR_PTR(-ENODEV);
1069         up(&shost->scan_mutex);
1070
1071         return sdev;
1072 }
1073
1074 void scsi_rescan_device(struct device *dev)
1075 {
1076         struct scsi_driver *drv;
1077         
1078         if (!dev->driver)
1079                 return;
1080
1081         drv = to_scsi_driver(dev->driver);
1082         if (try_module_get(drv->owner)) {
1083                 if (drv->rescan)
1084                         drv->rescan(dev);
1085                 module_put(drv->owner);
1086         }
1087 }
1088
1089 /**
1090  * scsi_scan_target - scan a target id, possibly including all LUNs on the
1091  *     target.
1092  * @sdevsca:    Scsi_Device handle for scanning
1093  * @shost:      host to scan
1094  * @channel:    channel to scan
1095  * @id:         target id to scan
1096  *
1097  * Description:
1098  *     Scan the target id on @shost, @channel, and @id. Scan at least LUN
1099  *     0, and possibly all LUNs on the target id.
1100  *
1101  *     Use the pre-allocated @sdevscan as a handle for the scanning. This
1102  *     function sets sdevscan->host, sdevscan->id and sdevscan->lun; the
1103  *     scanning functions modify sdevscan->lun.
1104  *
1105  *     First try a REPORT LUN scan, if that does not scan the target, do a
1106  *     sequential scan of LUNs on the target id.
1107  **/
1108 static void scsi_scan_target(struct Scsi_Host *shost, unsigned int channel,
1109                              unsigned int id, unsigned int lun, int rescan)
1110 {
1111         int bflags = 0;
1112         int res;
1113         struct scsi_device *sdev;
1114
1115         if (shost->this_id == id)
1116                 /*
1117                  * Don't scan the host adapter
1118                  */
1119                 return;
1120
1121         if (lun != SCAN_WILD_CARD) {
1122                 /*
1123                  * Scan for a specific host/chan/id/lun.
1124                  */
1125                 scsi_probe_and_add_lun(shost, channel, id, lun, NULL, NULL,
1126                                        rescan, NULL);
1127                 return;
1128         }
1129
1130         /*
1131          * Scan LUN 0, if there is some response, scan further. Ideally, we
1132          * would not configure LUN 0 until all LUNs are scanned.
1133          */
1134         res = scsi_probe_and_add_lun(shost, channel, id, 0, &bflags, &sdev,
1135                                      rescan, NULL);
1136         if (res == SCSI_SCAN_LUN_PRESENT) {
1137                 if (scsi_report_lun_scan(sdev, bflags, rescan) != 0)
1138                         /*
1139                          * The REPORT LUN did not scan the target,
1140                          * do a sequential scan.
1141                          */
1142                         scsi_sequential_lun_scan(shost, channel, id, bflags,
1143                                         res, sdev->scsi_level, rescan);
1144         } else if (res == SCSI_SCAN_TARGET_PRESENT) {
1145                 /*
1146                  * There's a target here, but lun 0 is offline so we
1147                  * can't use the report_lun scan.  Fall back to a
1148                  * sequential lun scan with a bflags of SPARSELUN and
1149                  * a default scsi level of SCSI_2
1150                  */
1151                 scsi_sequential_lun_scan(shost, channel, id, BLIST_SPARSELUN,
1152                                 SCSI_SCAN_TARGET_PRESENT, SCSI_2, rescan);
1153         }
1154 }
1155
1156 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1157                               unsigned int id, unsigned int lun, int rescan)
1158 {
1159         uint order_id;
1160
1161         if (id == SCAN_WILD_CARD)
1162                 for (id = 0; id < shost->max_id; ++id) {
1163                         /*
1164                          * XXX adapter drivers when possible (FCP, iSCSI)
1165                          * could modify max_id to match the current max,
1166                          * not the absolute max.
1167                          *
1168                          * XXX add a shost id iterator, so for example,
1169                          * the FC ID can be the same as a target id
1170                          * without a huge overhead of sparse id's.
1171                          */
1172                         if (shost->reverse_ordering)
1173                                 /*
1174                                  * Scan from high to low id.
1175                                  */
1176                                 order_id = shost->max_id - id - 1;
1177                         else
1178                                 order_id = id;
1179                         scsi_scan_target(shost, channel, order_id, lun, rescan);
1180                 }
1181         else
1182                 scsi_scan_target(shost, channel, id, lun, rescan);
1183 }
1184
1185 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1186                             unsigned int id, unsigned int lun, int rescan)
1187 {
1188         SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "%s: <%u:%u:%u:%u>\n",
1189                 __FUNCTION__, shost->host_no, channel, id, lun));
1190
1191         if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1192             ((id != SCAN_WILD_CARD) && (id > shost->max_id)) ||
1193             ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1194                 return -EINVAL;
1195
1196         down(&shost->scan_mutex);
1197         if (channel == SCAN_WILD_CARD) 
1198                 for (channel = 0; channel <= shost->max_channel; channel++)
1199                         scsi_scan_channel(shost, channel, id, lun, rescan);
1200         else
1201                 scsi_scan_channel(shost, channel, id, lun, rescan);
1202         up(&shost->scan_mutex);
1203
1204         return 0;
1205 }
1206
1207 /**
1208  * scsi_scan_host - scan the given adapter
1209  * @shost:      adapter to scan
1210  **/
1211 void scsi_scan_host(struct Scsi_Host *shost)
1212 {
1213         scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1214                                 SCAN_WILD_CARD, 0);
1215 }
1216
1217 void scsi_forget_host(struct Scsi_Host *shost)
1218 {
1219         struct scsi_device *sdev, *tmp;
1220         unsigned long flags;
1221
1222         /*
1223          * Ok, this look a bit strange.  We always look for the first device
1224          * on the list as scsi_remove_device removes them from it - thus we
1225          * also have to release the lock.
1226          * We don't need to get another reference to the device before
1227          * releasing the lock as we already own the reference from
1228          * scsi_register_device that's release in scsi_remove_device.  And
1229          * after that we don't look at sdev anymore.
1230          */
1231         spin_lock_irqsave(shost->host_lock, flags);
1232         list_for_each_entry_safe(sdev, tmp, &shost->__devices, siblings) {
1233                 spin_unlock_irqrestore(shost->host_lock, flags);
1234                 scsi_remove_device(sdev);
1235                 spin_lock_irqsave(shost->host_lock, flags);
1236         }
1237         spin_unlock_irqrestore(shost->host_lock, flags);
1238 }
1239
1240 /*
1241  * Function:    scsi_get_host_dev()
1242  *
1243  * Purpose:     Create a Scsi_Device that points to the host adapter itself.
1244  *
1245  * Arguments:   SHpnt   - Host that needs a Scsi_Device
1246  *
1247  * Lock status: None assumed.
1248  *
1249  * Returns:     The Scsi_Device or NULL
1250  *
1251  * Notes:
1252  *      Attach a single Scsi_Device to the Scsi_Host - this should
1253  *      be made to look like a "pseudo-device" that points to the
1254  *      HA itself.
1255  *
1256  *      Note - this device is not accessible from any high-level
1257  *      drivers (including generics), which is probably not
1258  *      optimal.  We can add hooks later to attach 
1259  */
1260 struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1261 {
1262         struct scsi_device *sdev;
1263
1264         sdev = scsi_alloc_sdev(shost, 0, shost->this_id, 0, NULL);
1265         if (sdev) {
1266                 sdev->borken = 0;
1267         }
1268         return sdev;
1269 }
1270
1271 /*
1272  * Function:    scsi_free_host_dev()
1273  *
1274  * Purpose:     Free a scsi_device that points to the host adapter itself.
1275  *
1276  * Arguments:   SHpnt   - Host that needs a Scsi_Device
1277  *
1278  * Lock status: None assumed.
1279  *
1280  * Returns:     Nothing
1281  *
1282  * Notes:
1283  */
1284 void scsi_free_host_dev(struct scsi_device *sdev)
1285 {
1286         BUG_ON(sdev->id != sdev->host->this_id);
1287
1288         if (sdev->host->hostt->slave_destroy)
1289                 sdev->host->hostt->slave_destroy(sdev);
1290         if (sdev->host->transportt->device_destroy)
1291                 sdev->host->transportt->device_destroy(sdev);
1292         put_device(&sdev->sdev_gendev);
1293 }