1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/blkdev.h>
32#include <linux/delay.h>
33#include <linux/kthread.h>
34#include <linux/spinlock.h>
35
36#include <scsi/scsi.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_device.h>
39#include <scsi/scsi_driver.h>
40#include <scsi/scsi_devinfo.h>
41#include <scsi/scsi_host.h>
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
53
54#define SCSI_TIMEOUT (2*HZ)
55
56
57
58
59#define SCSI_UID_SER_NUM 'S'
60#define SCSI_UID_UNKNOWN 'Z'
61
62
63
64
65
66
67
68
69
70
71
72
73
74#define SCSI_SCAN_NO_RESPONSE 0
75#define SCSI_SCAN_TARGET_PRESENT 1
76#define SCSI_SCAN_LUN_PRESENT 2
77
78static const char *scsi_null_device_strs = "nullnullnullnull";
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, uint, S_IRUGO|S_IWUSR);
89MODULE_PARM_DESC(max_luns,
90 "last scsi LUN (should be between 1 and 2^32-1)");
91
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;
99
100module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
101MODULE_PARM_DESC(scan, "sync, async or none");
102
103
104
105
106
107
108
109
110static unsigned int max_scsi_report_luns = 511;
111
112module_param_named(max_report_luns, max_scsi_report_luns, uint, 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, uint, 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
124
125static DEFINE_SPINLOCK(async_scan_lock);
126static LIST_HEAD(scanning_hosts);
127
128struct async_scan_data {
129 struct list_head list;
130 struct Scsi_Host *shost;
131 struct completion prev_finished;
132};
133
134
135
136
137
138
139
140
141
142int scsi_complete_async_scans(void)
143{
144 struct async_scan_data *data;
145
146 do {
147 if (list_empty(&scanning_hosts))
148 return 0;
149
150
151
152
153 data = kmalloc(sizeof(*data), GFP_KERNEL);
154 if (!data)
155 msleep(1);
156 } while (!data);
157
158 data->shost = NULL;
159 init_completion(&data->prev_finished);
160
161 spin_lock(&async_scan_lock);
162
163 if (list_empty(&scanning_hosts))
164 goto done;
165 list_add_tail(&data->list, &scanning_hosts);
166 spin_unlock(&async_scan_lock);
167
168 printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
169 wait_for_completion(&data->prev_finished);
170
171 spin_lock(&async_scan_lock);
172 list_del(&data->list);
173 if (!list_empty(&scanning_hosts)) {
174 struct async_scan_data *next = list_entry(scanning_hosts.next,
175 struct async_scan_data, list);
176 complete(&next->prev_finished);
177 }
178 done:
179 spin_unlock(&async_scan_lock);
180
181 kfree(data);
182 return 0;
183}
184
185
186EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
187
188#ifndef MODULE
189
190
191
192
193
194late_initcall(scsi_complete_async_scans);
195#endif
196
197
198
199
200
201
202
203
204
205
206static void scsi_unlock_floptical(struct scsi_device *sdev,
207 unsigned char *result)
208{
209 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
210
211 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
212 scsi_cmd[0] = MODE_SENSE;
213 scsi_cmd[1] = 0;
214 scsi_cmd[2] = 0x2e;
215 scsi_cmd[3] = 0;
216 scsi_cmd[4] = 0x2a;
217 scsi_cmd[5] = 0;
218 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
219 SCSI_TIMEOUT, 3);
220}
221
222
223
224
225
226
227
228
229
230
231
232
233static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
234 unsigned int lun, void *hostdata)
235{
236 struct scsi_device *sdev;
237 int display_failure_msg = 1, ret;
238 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
239 extern void scsi_evt_thread(struct work_struct *work);
240
241 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
242 GFP_ATOMIC);
243 if (!sdev)
244 goto out;
245
246 sdev->vendor = scsi_null_device_strs;
247 sdev->model = scsi_null_device_strs;
248 sdev->rev = scsi_null_device_strs;
249 sdev->host = shost;
250 sdev->id = starget->id;
251 sdev->lun = lun;
252 sdev->channel = starget->channel;
253 sdev->sdev_state = SDEV_CREATED;
254 INIT_LIST_HEAD(&sdev->siblings);
255 INIT_LIST_HEAD(&sdev->same_target_siblings);
256 INIT_LIST_HEAD(&sdev->cmd_list);
257 INIT_LIST_HEAD(&sdev->starved_entry);
258 INIT_LIST_HEAD(&sdev->event_list);
259 spin_lock_init(&sdev->list_lock);
260 INIT_WORK(&sdev->event_work, scsi_evt_thread);
261
262 sdev->sdev_gendev.parent = get_device(&starget->dev);
263 sdev->sdev_target = starget;
264
265
266 sdev->hostdata = hostdata;
267
268
269
270 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
271
272
273
274
275 sdev->type = -1;
276
277
278
279
280
281
282 sdev->borken = 1;
283
284 sdev->request_queue = scsi_alloc_queue(sdev);
285 if (!sdev->request_queue) {
286
287
288 put_device(&starget->dev);
289 kfree(sdev);
290 goto out;
291 }
292
293 sdev->request_queue->queuedata = sdev;
294 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
295
296 scsi_sysfs_device_initialize(sdev);
297
298 if (shost->hostt->slave_alloc) {
299 ret = shost->hostt->slave_alloc(sdev);
300 if (ret) {
301
302
303
304
305 if (ret == -ENXIO)
306 display_failure_msg = 0;
307 goto out_device_destroy;
308 }
309 }
310
311 return sdev;
312
313out_device_destroy:
314 transport_destroy_device(&sdev->sdev_gendev);
315 put_device(&sdev->sdev_gendev);
316out:
317 if (display_failure_msg)
318 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
319 return NULL;
320}
321
322static void scsi_target_dev_release(struct device *dev)
323{
324 struct device *parent = dev->parent;
325 struct scsi_target *starget = to_scsi_target(dev);
326
327 kfree(starget);
328 put_device(parent);
329}
330
331int scsi_is_target_device(const struct device *dev)
332{
333 return dev->release == scsi_target_dev_release;
334}
335EXPORT_SYMBOL(scsi_is_target_device);
336
337static struct scsi_target *__scsi_find_target(struct device *parent,
338 int channel, uint id)
339{
340 struct scsi_target *starget, *found_starget = NULL;
341 struct Scsi_Host *shost = dev_to_shost(parent);
342
343
344
345 list_for_each_entry(starget, &shost->__targets, siblings) {
346 if (starget->id == id &&
347 starget->channel == channel) {
348 found_starget = starget;
349 break;
350 }
351 }
352 if (found_starget)
353 get_device(&found_starget->dev);
354
355 return found_starget;
356}
357
358
359
360
361
362
363
364
365
366
367
368
369
370static struct scsi_target *scsi_alloc_target(struct device *parent,
371 int channel, uint id)
372{
373 struct Scsi_Host *shost = dev_to_shost(parent);
374 struct device *dev = NULL;
375 unsigned long flags;
376 const int size = sizeof(struct scsi_target)
377 + shost->transportt->target_size;
378 struct scsi_target *starget;
379 struct scsi_target *found_target;
380 int error;
381
382 starget = kzalloc(size, GFP_KERNEL);
383 if (!starget) {
384 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
385 return NULL;
386 }
387 dev = &starget->dev;
388 device_initialize(dev);
389 starget->reap_ref = 1;
390 dev->parent = get_device(parent);
391 dev->release = scsi_target_dev_release;
392 sprintf(dev->bus_id, "target%d:%d:%d",
393 shost->host_no, channel, id);
394 starget->id = id;
395 starget->channel = channel;
396 INIT_LIST_HEAD(&starget->siblings);
397 INIT_LIST_HEAD(&starget->devices);
398 starget->state = STARGET_RUNNING;
399 starget->scsi_level = SCSI_2;
400 retry:
401 spin_lock_irqsave(shost->host_lock, flags);
402
403 found_target = __scsi_find_target(parent, channel, id);
404 if (found_target)
405 goto found;
406
407 list_add_tail(&starget->siblings, &shost->__targets);
408 spin_unlock_irqrestore(shost->host_lock, flags);
409
410 transport_setup_device(dev);
411 error = device_add(dev);
412 if (error) {
413 dev_err(dev, "target device_add failed, error %d\n", error);
414 spin_lock_irqsave(shost->host_lock, flags);
415 list_del_init(&starget->siblings);
416 spin_unlock_irqrestore(shost->host_lock, flags);
417 transport_destroy_device(dev);
418 put_device(parent);
419 kfree(starget);
420 return NULL;
421 }
422 transport_add_device(dev);
423 if (shost->hostt->target_alloc) {
424 error = shost->hostt->target_alloc(starget);
425
426 if(error) {
427 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
428
429
430 get_device(dev);
431 scsi_target_reap(starget);
432 put_device(dev);
433 return NULL;
434 }
435 }
436 get_device(dev);
437
438 return starget;
439
440 found:
441 found_target->reap_ref++;
442 spin_unlock_irqrestore(shost->host_lock, flags);
443 if (found_target->state != STARGET_DEL) {
444 put_device(parent);
445 kfree(starget);
446 return found_target;
447 }
448
449
450 put_device(&found_target->dev);
451 flush_scheduled_work();
452 goto retry;
453}
454
455static void scsi_target_reap_usercontext(struct work_struct *work)
456{
457 struct scsi_target *starget =
458 container_of(work, struct scsi_target, ew.work);
459 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
460 unsigned long flags;
461
462 transport_remove_device(&starget->dev);
463 device_del(&starget->dev);
464 transport_destroy_device(&starget->dev);
465 spin_lock_irqsave(shost->host_lock, flags);
466 if (shost->hostt->target_destroy)
467 shost->hostt->target_destroy(starget);
468 list_del_init(&starget->siblings);
469 spin_unlock_irqrestore(shost->host_lock, flags);
470 put_device(&starget->dev);
471}
472
473
474
475
476
477
478
479
480
481
482void scsi_target_reap(struct scsi_target *starget)
483{
484 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
485 unsigned long flags;
486
487 spin_lock_irqsave(shost->host_lock, flags);
488
489 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
490 BUG_ON(starget->state == STARGET_DEL);
491 starget->state = STARGET_DEL;
492 spin_unlock_irqrestore(shost->host_lock, flags);
493 execute_in_process_context(scsi_target_reap_usercontext,
494 &starget->ew);
495 return;
496
497 }
498 spin_unlock_irqrestore(shost->host_lock, flags);
499
500 return;
501}
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517static void sanitize_inquiry_string(unsigned char *s, int len)
518{
519 int terminated = 0;
520
521 for (; len > 0; (--len, ++s)) {
522 if (*s == 0)
523 terminated = 1;
524 if (terminated || *s < 0x20 || *s > 0x7e)
525 *s = ' ';
526 }
527}
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
544 int result_len, int *bflags)
545{
546 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
547 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
548 int response_len = 0;
549 int pass, count, result;
550 struct scsi_sense_hdr sshdr;
551
552 *bflags = 0;
553
554
555
556
557 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
558 try_inquiry_len = first_inquiry_len;
559 pass = 1;
560
561 next_pass:
562 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
563 "scsi scan: INQUIRY pass %d length %d\n",
564 pass, try_inquiry_len));
565
566
567 for (count = 0; count < 3; ++count) {
568 memset(scsi_cmd, 0, 6);
569 scsi_cmd[0] = INQUIRY;
570 scsi_cmd[4] = (unsigned char) try_inquiry_len;
571
572 memset(inq_result, 0, try_inquiry_len);
573
574 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
575 inq_result, try_inquiry_len, &sshdr,
576 HZ / 2 + HZ * scsi_inq_timeout, 3);
577
578 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
579 "with code 0x%x\n",
580 result ? "failed" : "successful", result));
581
582 if (result) {
583
584
585
586
587
588
589 if ((driver_byte(result) & DRIVER_SENSE) &&
590 scsi_sense_valid(&sshdr)) {
591 if ((sshdr.sense_key == UNIT_ATTENTION) &&
592 ((sshdr.asc == 0x28) ||
593 (sshdr.asc == 0x29)) &&
594 (sshdr.ascq == 0))
595 continue;
596 }
597 }
598 break;
599 }
600
601 if (result == 0) {
602 sanitize_inquiry_string(&inq_result[8], 8);
603 sanitize_inquiry_string(&inq_result[16], 16);
604 sanitize_inquiry_string(&inq_result[32], 4);
605
606 response_len = inq_result[4] + 5;
607 if (response_len > 255)
608 response_len = first_inquiry_len;
609
610
611
612
613
614
615
616
617 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
618 &inq_result[16]);
619
620
621
622 if (pass == 1) {
623 if (BLIST_INQUIRY_36 & *bflags)
624 next_inquiry_len = 36;
625 else if (BLIST_INQUIRY_58 & *bflags)
626 next_inquiry_len = 58;
627 else if (sdev->inquiry_len)
628 next_inquiry_len = sdev->inquiry_len;
629 else
630 next_inquiry_len = response_len;
631
632
633 if (next_inquiry_len > try_inquiry_len) {
634 try_inquiry_len = next_inquiry_len;
635 pass = 2;
636 goto next_pass;
637 }
638 }
639
640 } else if (pass == 2) {
641 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
642 "Consider BLIST_INQUIRY_36 for this device\n",
643 try_inquiry_len);
644
645
646
647 try_inquiry_len = first_inquiry_len;
648 pass = 3;
649 goto next_pass;
650 }
651
652
653
654 if (result)
655 return -EIO;
656
657
658 sdev->inquiry_len = min(try_inquiry_len, response_len);
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675 if (sdev->inquiry_len < 36) {
676 printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
677 " using 36\n", sdev->inquiry_len);
678 sdev->inquiry_len = 36;
679 }
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699 sdev->scsi_level = inq_result[2] & 0x07;
700 if (sdev->scsi_level >= 2 ||
701 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
702 sdev->scsi_level++;
703 sdev->sdev_target->scsi_level = sdev->scsi_level;
704
705 return 0;
706}
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
724 int *bflags, int async)
725{
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746 sdev->inquiry = kmemdup(inq_result,
747 max_t(size_t, sdev->inquiry_len, 36),
748 GFP_ATOMIC);
749 if (sdev->inquiry == NULL)
750 return SCSI_SCAN_NO_RESPONSE;
751
752 sdev->vendor = (char *) (sdev->inquiry + 8);
753 sdev->model = (char *) (sdev->inquiry + 16);
754 sdev->rev = (char *) (sdev->inquiry + 32);
755
756 if (*bflags & BLIST_ISROM) {
757 sdev->type = TYPE_ROM;
758 sdev->removable = 1;
759 } else {
760 sdev->type = (inq_result[0] & 0x1f);
761 sdev->removable = (inq_result[1] & 0x80) >> 7;
762 }
763
764 switch (sdev->type) {
765 case TYPE_RBC:
766 case TYPE_TAPE:
767 case TYPE_DISK:
768 case TYPE_PRINTER:
769 case TYPE_MOD:
770 case TYPE_PROCESSOR:
771 case TYPE_SCANNER:
772 case TYPE_MEDIUM_CHANGER:
773 case TYPE_ENCLOSURE:
774 case TYPE_COMM:
775 case TYPE_RAID:
776 sdev->writeable = 1;
777 break;
778 case TYPE_ROM:
779 case TYPE_WORM:
780 sdev->writeable = 0;
781 break;
782 default:
783 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
784 }
785
786 if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
787
788
789
790
791 if ((*bflags & BLIST_REPORTLUN2) == 0)
792 *bflags |= BLIST_NOREPORTLUN;
793 }
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
812 sdev->lockable = sdev->removable;
813 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
814
815 if (sdev->scsi_level >= SCSI_3 ||
816 (sdev->inquiry_len > 56 && inq_result[56] & 0x04))
817 sdev->ppr = 1;
818 if (inq_result[7] & 0x60)
819 sdev->wdtr = 1;
820 if (inq_result[7] & 0x10)
821 sdev->sdtr = 1;
822
823 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
824 "ANSI: %d%s\n", scsi_device_type(sdev->type),
825 sdev->vendor, sdev->model, sdev->rev,
826 sdev->inq_periph_qual, inq_result[2] & 0x07,
827 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
828
829 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
830 !(*bflags & BLIST_NOTQ))
831 sdev->tagged_supported = 1;
832
833
834
835
836
837
838 if ((*bflags & BLIST_BORKEN) == 0)
839 sdev->borken = 0;
840
841 if (*bflags & BLIST_NO_ULD_ATTACH)
842 sdev->no_uld_attach = 1;
843
844
845
846
847
848 if (*bflags & BLIST_SELECT_NO_ATN)
849 sdev->select_no_atn = 1;
850
851
852
853
854
855 if (*bflags & BLIST_MAX_512)
856 blk_queue_max_sectors(sdev->request_queue, 512);
857
858
859
860
861
862 if (*bflags & BLIST_NOSTARTONADD)
863 sdev->no_start_on_add = 1;
864
865 if (*bflags & BLIST_SINGLELUN)
866 sdev->single_lun = 1;
867
868 sdev->use_10_for_rw = 1;
869
870 if (*bflags & BLIST_MS_SKIP_PAGE_08)
871 sdev->skip_ms_page_8 = 1;
872
873 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
874 sdev->skip_ms_page_3f = 1;
875
876 if (*bflags & BLIST_USE_10_BYTE_MS)
877 sdev->use_10_for_ms = 1;
878
879
880
881 scsi_device_set_state(sdev, SDEV_RUNNING);
882
883 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
884 sdev->use_192_bytes_for_3f = 1;
885
886 if (*bflags & BLIST_NOT_LOCKABLE)
887 sdev->lockable = 0;
888
889 if (*bflags & BLIST_RETRY_HWERROR)
890 sdev->retry_hwerror = 1;
891
892 transport_configure_device(&sdev->sdev_gendev);
893
894 if (sdev->host->hostt->slave_configure) {
895 int ret = sdev->host->hostt->slave_configure(sdev);
896 if (ret) {
897
898
899
900
901 if (ret != -ENXIO) {
902 sdev_printk(KERN_ERR, sdev,
903 "failed to configure device\n");
904 }
905 return SCSI_SCAN_NO_RESPONSE;
906 }
907 }
908
909
910
911
912
913
914 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
915 return SCSI_SCAN_NO_RESPONSE;
916
917 return SCSI_SCAN_LUN_PRESENT;
918}
919
920static inline void scsi_destroy_sdev(struct scsi_device *sdev)
921{
922 scsi_device_set_state(sdev, SDEV_DEL);
923 if (sdev->host->hostt->slave_destroy)
924 sdev->host->hostt->slave_destroy(sdev);
925 transport_destroy_device(&sdev->sdev_gendev);
926 put_device(&sdev->sdev_gendev);
927}
928
929#ifdef CONFIG_SCSI_LOGGING
930
931
932
933
934
935
936
937
938static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
939 unsigned first, unsigned end)
940{
941 unsigned term = 0, idx;
942
943 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
944 if (inq[idx+first] > ' ') {
945 buf[idx] = inq[idx+first];
946 term = idx+1;
947 } else {
948 buf[idx] = ' ';
949 }
950 }
951 buf[term] = 0;
952 return buf;
953}
954#endif
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974static int scsi_probe_and_add_lun(struct scsi_target *starget,
975 uint lun, int *bflagsp,
976 struct scsi_device **sdevp, int rescan,
977 void *hostdata)
978{
979 struct scsi_device *sdev;
980 unsigned char *result;
981 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
982 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
983
984
985
986
987
988 sdev = scsi_device_lookup_by_target(starget, lun);
989 if (sdev) {
990 if (rescan || sdev->sdev_state != SDEV_CREATED) {
991 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
992 "scsi scan: device exists on %s\n",
993 sdev->sdev_gendev.bus_id));
994 if (sdevp)
995 *sdevp = sdev;
996 else
997 scsi_device_put(sdev);
998
999 if (bflagsp)
1000 *bflagsp = scsi_get_device_flags(sdev,
1001 sdev->vendor,
1002 sdev->model);
1003 return SCSI_SCAN_LUN_PRESENT;
1004 }
1005 scsi_device_put(sdev);
1006 } else
1007 sdev = scsi_alloc_sdev(starget, lun, hostdata);
1008 if (!sdev)
1009 goto out;
1010
1011 result = kmalloc(result_len, GFP_ATOMIC |
1012 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
1013 if (!result)
1014 goto out_free_sdev;
1015
1016 if (scsi_probe_lun(sdev, result, result_len, &bflags))
1017 goto out_free_result;
1018
1019 if (bflagsp)
1020 *bflagsp = bflags;
1021
1022
1023
1024 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1036 " peripheral qualifier of 3, device not"
1037 " added\n"))
1038 if (lun == 0) {
1039 SCSI_LOG_SCAN_BUS(1, {
1040 unsigned char vend[9];
1041 unsigned char mod[17];
1042
1043 sdev_printk(KERN_INFO, sdev,
1044 "scsi scan: consider passing scsi_mod."
1045 "dev_flags=%s:%s:0x240 or 0x1000240\n",
1046 scsi_inq_str(vend, result, 8, 16),
1047 scsi_inq_str(mod, result, 16, 32));
1048 });
1049 }
1050
1051 res = SCSI_SCAN_TARGET_PRESENT;
1052 goto out_free_result;
1053 }
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1075 (result[0] & 0x1f) == 0x1f) {
1076 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1077 "scsi scan: peripheral device type"
1078 " of 31, no device added\n"));
1079 res = SCSI_SCAN_TARGET_PRESENT;
1080 goto out_free_result;
1081 }
1082
1083 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
1084 if (res == SCSI_SCAN_LUN_PRESENT) {
1085 if (bflags & BLIST_KEY) {
1086 sdev->lockable = 0;
1087 scsi_unlock_floptical(sdev, result);
1088 }
1089 }
1090
1091 out_free_result:
1092 kfree(result);
1093 out_free_sdev:
1094 if (res == SCSI_SCAN_LUN_PRESENT) {
1095 if (sdevp) {
1096 if (scsi_device_get(sdev) == 0) {
1097 *sdevp = sdev;
1098 } else {
1099 __scsi_remove_device(sdev);
1100 res = SCSI_SCAN_NO_RESPONSE;
1101 }
1102 }
1103 } else
1104 scsi_destroy_sdev(sdev);
1105 out:
1106 return res;
1107}
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121static void scsi_sequential_lun_scan(struct scsi_target *starget,
1122 int bflags, int scsi_level, int rescan)
1123{
1124 unsigned int sparse_lun, lun, max_dev_lun;
1125 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1126
1127 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1128 "%s\n", starget->dev.bus_id));
1129
1130 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1131
1132
1133
1134
1135
1136 if (bflags & BLIST_SPARSELUN) {
1137 max_dev_lun = shost->max_lun;
1138 sparse_lun = 1;
1139 } else
1140 sparse_lun = 0;
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163 if (bflags & BLIST_FORCELUN)
1164 max_dev_lun = shost->max_lun;
1165
1166
1167
1168 if (bflags & BLIST_MAX5LUN)
1169 max_dev_lun = min(5U, max_dev_lun);
1170
1171
1172
1173
1174 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1175 max_dev_lun = min(8U, max_dev_lun);
1176
1177
1178
1179
1180
1181
1182 for (lun = 1; lun < max_dev_lun; ++lun)
1183 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1184 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1185 !sparse_lun)
1186 return;
1187}
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208int scsilun_to_int(struct scsi_lun *scsilun)
1209{
1210 int i;
1211 unsigned int lun;
1212
1213 lun = 0;
1214 for (i = 0; i < sizeof(lun); i += 2)
1215 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1216 scsilun->scsi_lun[i + 1]) << (i * 8));
1217 return lun;
1218}
1219EXPORT_SYMBOL(scsilun_to_int);
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1240{
1241 int i;
1242
1243 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1244
1245 for (i = 0; i < sizeof(lun); i += 2) {
1246 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1247 scsilun->scsi_lun[i+1] = lun & 0xFF;
1248 lun = lun >> 16;
1249 }
1250}
1251EXPORT_SYMBOL(int_to_scsilun);
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
1269 int rescan)
1270{
1271 char devname[64];
1272 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1273 unsigned int length;
1274 unsigned int lun;
1275 unsigned int num_luns;
1276 unsigned int retries;
1277 int result;
1278 struct scsi_lun *lunp, *lun_data;
1279 u8 *data;
1280 struct scsi_sense_hdr sshdr;
1281 struct scsi_device *sdev;
1282 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1283 int ret = 0;
1284
1285
1286
1287
1288
1289
1290 if (bflags & BLIST_NOREPORTLUN)
1291 return 1;
1292 if (starget->scsi_level < SCSI_2 &&
1293 starget->scsi_level != SCSI_UNKNOWN)
1294 return 1;
1295 if (starget->scsi_level < SCSI_3 &&
1296 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
1297 return 1;
1298 if (bflags & BLIST_NOLUN)
1299 return 0;
1300
1301 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1302 sdev = scsi_alloc_sdev(starget, 0, NULL);
1303 if (!sdev)
1304 return 0;
1305 if (scsi_device_get(sdev))
1306 return 0;
1307 }
1308
1309 sprintf(devname, "host %d channel %d id %d",
1310 shost->host_no, sdev->channel, sdev->id);
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1323 lun_data = kmalloc(length, GFP_ATOMIC |
1324 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
1325 if (!lun_data) {
1326 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
1327 goto out;
1328 }
1329
1330 scsi_cmd[0] = REPORT_LUNS;
1331
1332
1333
1334
1335 memset(&scsi_cmd[1], 0, 5);
1336
1337
1338
1339
1340 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1341 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1342 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1343 scsi_cmd[9] = (unsigned char) length & 0xff;
1344
1345 scsi_cmd[10] = 0;
1346 scsi_cmd[11] = 0;
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358 for (retries = 0; retries < 3; retries++) {
1359 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1360 " REPORT LUNS to %s (try %d)\n", devname,
1361 retries));
1362
1363 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
1364 lun_data, length, &sshdr,
1365 SCSI_TIMEOUT + 4 * HZ, 3);
1366
1367 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
1368 " %s (try %d) result 0x%x\n", result
1369 ? "failed" : "successful", retries, result));
1370 if (result == 0)
1371 break;
1372 else if (scsi_sense_valid(&sshdr)) {
1373 if (sshdr.sense_key != UNIT_ATTENTION)
1374 break;
1375 }
1376 }
1377
1378 if (result) {
1379
1380
1381
1382 ret = 1;
1383 goto out_err;
1384 }
1385
1386
1387
1388
1389 data = (u8 *) lun_data->scsi_lun;
1390 length = ((data[0] << 24) | (data[1] << 16) |
1391 (data[2] << 8) | (data[3] << 0));
1392
1393 num_luns = (length / sizeof(struct scsi_lun));
1394 if (num_luns > max_scsi_report_luns) {
1395 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1396 " of %d luns reported, try increasing"
1397 " max_scsi_report_luns.\n", devname,
1398 max_scsi_report_luns, num_luns);
1399 num_luns = max_scsi_report_luns;
1400 }
1401
1402 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1403 "scsi scan: REPORT LUN scan\n"));
1404
1405
1406
1407
1408
1409 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1410 lun = scsilun_to_int(lunp);
1411
1412
1413
1414
1415
1416 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1417 int i;
1418
1419
1420
1421
1422
1423
1424 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1425 data = (char *)lunp->scsi_lun;
1426 for (i = 0; i < sizeof(struct scsi_lun); i++)
1427 printk("%02x", data[i]);
1428 printk(" has a LUN larger than currently supported.\n");
1429 } else if (lun > sdev->host->max_lun) {
1430 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1431 " than allowed by the host adapter\n",
1432 devname, lun);
1433 } else {
1434 int res;
1435
1436 res = scsi_probe_and_add_lun(starget,
1437 lun, NULL, NULL, rescan, NULL);
1438 if (res == SCSI_SCAN_NO_RESPONSE) {
1439
1440
1441
1442 sdev_printk(KERN_ERR, sdev,
1443 "Unexpected response"
1444 " from lun %d while scanning, scan"
1445 " aborted\n", lun);
1446 break;
1447 }
1448 }
1449 }
1450
1451 out_err:
1452 kfree(lun_data);
1453 out:
1454 scsi_device_put(sdev);
1455 if (sdev->sdev_state == SDEV_CREATED)
1456
1457
1458
1459 scsi_destroy_sdev(sdev);
1460 return ret;
1461}
1462
1463struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1464 uint id, uint lun, void *hostdata)
1465{
1466 struct scsi_device *sdev = ERR_PTR(-ENODEV);
1467 struct device *parent = &shost->shost_gendev;
1468 struct scsi_target *starget;
1469
1470 if (strncmp(scsi_scan_type, "none", 4) == 0)
1471 return ERR_PTR(-ENODEV);
1472
1473 starget = scsi_alloc_target(parent, channel, id);
1474 if (!starget)
1475 return ERR_PTR(-ENOMEM);
1476
1477 mutex_lock(&shost->scan_mutex);
1478 if (!shost->async_scan)
1479 scsi_complete_async_scans();
1480
1481 if (scsi_host_scan_allowed(shost))
1482 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
1483 mutex_unlock(&shost->scan_mutex);
1484 scsi_target_reap(starget);
1485 put_device(&starget->dev);
1486
1487 return sdev;
1488}
1489EXPORT_SYMBOL(__scsi_add_device);
1490
1491int scsi_add_device(struct Scsi_Host *host, uint channel,
1492 uint target, uint lun)
1493{
1494 struct scsi_device *sdev =
1495 __scsi_add_device(host, channel, target, lun, NULL);
1496 if (IS_ERR(sdev))
1497 return PTR_ERR(sdev);
1498
1499 scsi_device_put(sdev);
1500 return 0;
1501}
1502EXPORT_SYMBOL(scsi_add_device);
1503
1504void scsi_rescan_device(struct device *dev)
1505{
1506 struct scsi_driver *drv;
1507
1508 if (!dev->driver)
1509 return;
1510
1511 drv = to_scsi_driver(dev->driver);
1512 if (try_module_get(drv->owner)) {
1513 if (drv->rescan)
1514 drv->rescan(dev);
1515 module_put(drv->owner);
1516 }
1517}
1518EXPORT_SYMBOL(scsi_rescan_device);
1519
1520static void __scsi_scan_target(struct device *parent, unsigned int channel,
1521 unsigned int id, unsigned int lun, int rescan)
1522{
1523 struct Scsi_Host *shost = dev_to_shost(parent);
1524 int bflags = 0;
1525 int res;
1526 struct scsi_target *starget;
1527
1528 if (shost->this_id == id)
1529
1530
1531
1532 return;
1533
1534 starget = scsi_alloc_target(parent, channel, id);
1535 if (!starget)
1536 return;
1537
1538 if (lun != SCAN_WILD_CARD) {
1539
1540
1541
1542 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1543 goto out_reap;
1544 }
1545
1546
1547
1548
1549
1550 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1551 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1552 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
1553
1554
1555
1556
1557 scsi_sequential_lun_scan(starget, bflags,
1558 starget->scsi_level, rescan);
1559 }
1560
1561 out_reap:
1562
1563
1564 scsi_target_reap(starget);
1565
1566 put_device(&starget->dev);
1567}
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585void scsi_scan_target(struct device *parent, unsigned int channel,
1586 unsigned int id, unsigned int lun, int rescan)
1587{
1588 struct Scsi_Host *shost = dev_to_shost(parent);
1589
1590 if (strncmp(scsi_scan_type, "none", 4) == 0)
1591 return;
1592
1593 mutex_lock(&shost->scan_mutex);
1594 if (!shost->async_scan)
1595 scsi_complete_async_scans();
1596
1597 if (scsi_host_scan_allowed(shost))
1598 __scsi_scan_target(parent, channel, id, lun, rescan);
1599 mutex_unlock(&shost->scan_mutex);
1600}
1601EXPORT_SYMBOL(scsi_scan_target);
1602
1603static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1604 unsigned int id, unsigned int lun, int rescan)
1605{
1606 uint order_id;
1607
1608 if (id == SCAN_WILD_CARD)
1609 for (id = 0; id < shost->max_id; ++id) {
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619 if (shost->reverse_ordering)
1620
1621
1622
1623 order_id = shost->max_id - id - 1;
1624 else
1625 order_id = id;
1626 __scsi_scan_target(&shost->shost_gendev, channel,
1627 order_id, lun, rescan);
1628 }
1629 else
1630 __scsi_scan_target(&shost->shost_gendev, channel,
1631 id, lun, rescan);
1632}
1633
1634int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1635 unsigned int id, unsigned int lun, int rescan)
1636{
1637 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1638 "%s: <%u:%u:%u>\n",
1639 __FUNCTION__, channel, id, lun));
1640
1641 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1642 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1643 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1644 return -EINVAL;
1645
1646 mutex_lock(&shost->scan_mutex);
1647 if (!shost->async_scan)
1648 scsi_complete_async_scans();
1649
1650 if (scsi_host_scan_allowed(shost)) {
1651 if (channel == SCAN_WILD_CARD)
1652 for (channel = 0; channel <= shost->max_channel;
1653 channel++)
1654 scsi_scan_channel(shost, channel, id, lun,
1655 rescan);
1656 else
1657 scsi_scan_channel(shost, channel, id, lun, rescan);
1658 }
1659 mutex_unlock(&shost->scan_mutex);
1660
1661 return 0;
1662}
1663
1664static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1665{
1666 struct scsi_device *sdev;
1667 shost_for_each_device(sdev, shost) {
1668 if (!scsi_host_scan_allowed(shost) ||
1669 scsi_sysfs_add_sdev(sdev) != 0)
1670 scsi_destroy_sdev(sdev);
1671 }
1672}
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
1685{
1686 struct async_scan_data *data;
1687 unsigned long flags;
1688
1689 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1690 return NULL;
1691
1692 if (shost->async_scan) {
1693 printk("%s called twice for host %d", __FUNCTION__,
1694 shost->host_no);
1695 dump_stack();
1696 return NULL;
1697 }
1698
1699 data = kmalloc(sizeof(*data), GFP_KERNEL);
1700 if (!data)
1701 goto err;
1702 data->shost = scsi_host_get(shost);
1703 if (!data->shost)
1704 goto err;
1705 init_completion(&data->prev_finished);
1706
1707 mutex_lock(&shost->scan_mutex);
1708 spin_lock_irqsave(shost->host_lock, flags);
1709 shost->async_scan = 1;
1710 spin_unlock_irqrestore(shost->host_lock, flags);
1711 mutex_unlock(&shost->scan_mutex);
1712
1713 spin_lock(&async_scan_lock);
1714 if (list_empty(&scanning_hosts))
1715 complete(&data->prev_finished);
1716 list_add_tail(&data->list, &scanning_hosts);
1717 spin_unlock(&async_scan_lock);
1718
1719 return data;
1720
1721 err:
1722 kfree(data);
1723 return NULL;
1724}
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734static void scsi_finish_async_scan(struct async_scan_data *data)
1735{
1736 struct Scsi_Host *shost;
1737 unsigned long flags;
1738
1739 if (!data)
1740 return;
1741
1742 shost = data->shost;
1743
1744 mutex_lock(&shost->scan_mutex);
1745
1746 if (!shost->async_scan) {
1747 printk("%s called twice for host %d", __FUNCTION__,
1748 shost->host_no);
1749 dump_stack();
1750 return;
1751 }
1752
1753 wait_for_completion(&data->prev_finished);
1754
1755 scsi_sysfs_add_devices(shost);
1756
1757 spin_lock_irqsave(shost->host_lock, flags);
1758 shost->async_scan = 0;
1759 spin_unlock_irqrestore(shost->host_lock, flags);
1760
1761 mutex_unlock(&shost->scan_mutex);
1762
1763 spin_lock(&async_scan_lock);
1764 list_del(&data->list);
1765 if (!list_empty(&scanning_hosts)) {
1766 struct async_scan_data *next = list_entry(scanning_hosts.next,
1767 struct async_scan_data, list);
1768 complete(&next->prev_finished);
1769 }
1770 spin_unlock(&async_scan_lock);
1771
1772 scsi_host_put(shost);
1773 kfree(data);
1774}
1775
1776static void do_scsi_scan_host(struct Scsi_Host *shost)
1777{
1778 if (shost->hostt->scan_finished) {
1779 unsigned long start = jiffies;
1780 if (shost->hostt->scan_start)
1781 shost->hostt->scan_start(shost);
1782
1783 while (!shost->hostt->scan_finished(shost, jiffies - start))
1784 msleep(10);
1785 } else {
1786 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1787 SCAN_WILD_CARD, 0);
1788 }
1789}
1790
1791static int do_scan_async(void *_data)
1792{
1793 struct async_scan_data *data = _data;
1794 do_scsi_scan_host(data->shost);
1795 scsi_finish_async_scan(data);
1796 return 0;
1797}
1798
1799
1800
1801
1802
1803void scsi_scan_host(struct Scsi_Host *shost)
1804{
1805 struct task_struct *p;
1806 struct async_scan_data *data;
1807
1808 if (strncmp(scsi_scan_type, "none", 4) == 0)
1809 return;
1810
1811 data = scsi_prep_async_scan(shost);
1812 if (!data) {
1813 do_scsi_scan_host(shost);
1814 return;
1815 }
1816
1817 p = kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1818 if (unlikely(IS_ERR(p)))
1819 do_scan_async(data);
1820}
1821EXPORT_SYMBOL(scsi_scan_host);
1822
1823void scsi_forget_host(struct Scsi_Host *shost)
1824{
1825 struct scsi_device *sdev;
1826 unsigned long flags;
1827
1828 restart:
1829 spin_lock_irqsave(shost->host_lock, flags);
1830 list_for_each_entry(sdev, &shost->__devices, siblings) {
1831 if (sdev->sdev_state == SDEV_DEL)
1832 continue;
1833 spin_unlock_irqrestore(shost->host_lock, flags);
1834 __scsi_remove_device(sdev);
1835 goto restart;
1836 }
1837 spin_unlock_irqrestore(shost->host_lock, flags);
1838}
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1861{
1862 struct scsi_device *sdev = NULL;
1863 struct scsi_target *starget;
1864
1865 mutex_lock(&shost->scan_mutex);
1866 if (!scsi_host_scan_allowed(shost))
1867 goto out;
1868 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1869 if (!starget)
1870 goto out;
1871
1872 sdev = scsi_alloc_sdev(starget, 0, NULL);
1873 if (sdev) {
1874 sdev->sdev_gendev.parent = get_device(&starget->dev);
1875 sdev->borken = 0;
1876 } else
1877 scsi_target_reap(starget);
1878 put_device(&starget->dev);
1879 out:
1880 mutex_unlock(&shost->scan_mutex);
1881 return sdev;
1882}
1883EXPORT_SYMBOL(scsi_get_host_dev);
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898void scsi_free_host_dev(struct scsi_device *sdev)
1899{
1900 BUG_ON(sdev->id != sdev->host->this_id);
1901
1902 scsi_destroy_sdev(sdev);
1903}
1904EXPORT_SYMBOL(scsi_free_host_dev);
1905
1906