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
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/module.h>
41#include <linux/moduleparam.h>
42#include <linux/kernel.h>
43#include <linux/timer.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/blkdev.h>
47#include <linux/delay.h>
48#include <linux/init.h>
49#include <linux/completion.h>
50#include <linux/unistd.h>
51#include <linux/spinlock.h>
52#include <linux/kmod.h>
53#include <linux/interrupt.h>
54#include <linux/notifier.h>
55#include <linux/cpu.h>
56#include <linux/mutex.h>
57
58#include <scsi/scsi.h>
59#include <scsi/scsi_cmnd.h>
60#include <scsi/scsi_dbg.h>
61#include <scsi/scsi_device.h>
62#include <scsi/scsi_driver.h>
63#include <scsi/scsi_eh.h>
64#include <scsi/scsi_host.h>
65#include <scsi/scsi_tcq.h>
66
67#include "scsi_priv.h"
68#include "scsi_logging.h"
69
70#define CREATE_TRACE_POINTS
71#include <trace/events/scsi.h>
72
73static void scsi_done(struct scsi_cmnd *cmd);
74
75
76
77
78
79#define MIN_RESET_DELAY (2*HZ)
80
81
82#define MIN_RESET_PERIOD (15*HZ)
83
84
85
86
87
88unsigned int scsi_logging_level;
89#if defined(CONFIG_SCSI_LOGGING)
90EXPORT_SYMBOL(scsi_logging_level);
91#endif
92
93
94
95
96
97static const char *const scsi_device_types[] = {
98 "Direct-Access ",
99 "Sequential-Access",
100 "Printer ",
101 "Processor ",
102 "WORM ",
103 "CD-ROM ",
104 "Scanner ",
105 "Optical Device ",
106 "Medium Changer ",
107 "Communications ",
108 "ASC IT8 ",
109 "ASC IT8 ",
110 "RAID ",
111 "Enclosure ",
112 "Direct-Access-RBC",
113 "Optical card ",
114 "Bridge controller",
115 "Object storage ",
116 "Automation/Drive ",
117};
118
119
120
121
122
123
124const char * scsi_device_type(unsigned type)
125{
126 if (type == 0x1e)
127 return "Well-known LUN ";
128 if (type == 0x1f)
129 return "No Device ";
130 if (type >= ARRAY_SIZE(scsi_device_types))
131 return "Unknown ";
132 return scsi_device_types[type];
133}
134
135EXPORT_SYMBOL(scsi_device_type);
136
137struct scsi_host_cmd_pool {
138 struct kmem_cache *cmd_slab;
139 struct kmem_cache *sense_slab;
140 unsigned int users;
141 char *cmd_name;
142 char *sense_name;
143 unsigned int slab_flags;
144 gfp_t gfp_mask;
145};
146
147static struct scsi_host_cmd_pool scsi_cmd_pool = {
148 .cmd_name = "scsi_cmd_cache",
149 .sense_name = "scsi_sense_cache",
150 .slab_flags = SLAB_HWCACHE_ALIGN,
151};
152
153static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
154 .cmd_name = "scsi_cmd_cache(DMA)",
155 .sense_name = "scsi_sense_cache(DMA)",
156 .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
157 .gfp_mask = __GFP_DMA,
158};
159
160static DEFINE_MUTEX(host_cmd_pool_mutex);
161
162
163
164
165
166
167
168
169
170static struct scsi_cmnd *
171scsi_pool_alloc_command(struct scsi_host_cmd_pool *pool, gfp_t gfp_mask)
172{
173 struct scsi_cmnd *cmd;
174
175 cmd = kmem_cache_zalloc(pool->cmd_slab, gfp_mask | pool->gfp_mask);
176 if (!cmd)
177 return NULL;
178
179 cmd->sense_buffer = kmem_cache_alloc(pool->sense_slab,
180 gfp_mask | pool->gfp_mask);
181 if (!cmd->sense_buffer) {
182 kmem_cache_free(pool->cmd_slab, cmd);
183 return NULL;
184 }
185
186 return cmd;
187}
188
189
190
191
192
193
194
195
196
197static void
198scsi_pool_free_command(struct scsi_host_cmd_pool *pool,
199 struct scsi_cmnd *cmd)
200{
201 if (cmd->prot_sdb)
202 kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
203
204 kmem_cache_free(pool->sense_slab, cmd->sense_buffer);
205 kmem_cache_free(pool->cmd_slab, cmd);
206}
207
208
209
210
211
212
213
214
215
216static struct scsi_cmnd *
217scsi_host_alloc_command(struct Scsi_Host *shost, gfp_t gfp_mask)
218{
219 struct scsi_cmnd *cmd;
220
221 cmd = scsi_pool_alloc_command(shost->cmd_pool, gfp_mask);
222 if (!cmd)
223 return NULL;
224
225 if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
226 cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp_mask);
227
228 if (!cmd->prot_sdb) {
229 scsi_pool_free_command(shost->cmd_pool, cmd);
230 return NULL;
231 }
232 }
233
234 return cmd;
235}
236
237
238
239
240
241
242
243
244
245struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost, gfp_t gfp_mask)
246{
247 struct scsi_cmnd *cmd = scsi_host_alloc_command(shost, gfp_mask);
248
249 if (unlikely(!cmd)) {
250 unsigned long flags;
251
252 spin_lock_irqsave(&shost->free_list_lock, flags);
253 if (likely(!list_empty(&shost->free_list))) {
254 cmd = list_entry(shost->free_list.next,
255 struct scsi_cmnd, list);
256 list_del_init(&cmd->list);
257 }
258 spin_unlock_irqrestore(&shost->free_list_lock, flags);
259
260 if (cmd) {
261 void *buf, *prot;
262
263 buf = cmd->sense_buffer;
264 prot = cmd->prot_sdb;
265
266 memset(cmd, 0, sizeof(*cmd));
267
268 cmd->sense_buffer = buf;
269 cmd->prot_sdb = prot;
270 }
271 }
272
273 return cmd;
274}
275EXPORT_SYMBOL_GPL(__scsi_get_command);
276
277
278
279
280
281
282
283
284struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask)
285{
286 struct scsi_cmnd *cmd;
287
288
289 if (!get_device(&dev->sdev_gendev))
290 return NULL;
291
292 cmd = __scsi_get_command(dev->host, gfp_mask);
293
294 if (likely(cmd != NULL)) {
295 unsigned long flags;
296
297 cmd->device = dev;
298 INIT_LIST_HEAD(&cmd->list);
299 spin_lock_irqsave(&dev->list_lock, flags);
300 list_add_tail(&cmd->list, &dev->cmd_list);
301 spin_unlock_irqrestore(&dev->list_lock, flags);
302 cmd->jiffies_at_alloc = jiffies;
303 } else
304 put_device(&dev->sdev_gendev);
305
306 return cmd;
307}
308EXPORT_SYMBOL(scsi_get_command);
309
310
311
312
313
314
315
316void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd,
317 struct device *dev)
318{
319 unsigned long flags;
320
321
322 spin_lock_irqsave(&shost->free_list_lock, flags);
323 if (unlikely(list_empty(&shost->free_list))) {
324 list_add(&cmd->list, &shost->free_list);
325 cmd = NULL;
326 }
327 spin_unlock_irqrestore(&shost->free_list_lock, flags);
328
329 if (likely(cmd != NULL))
330 scsi_pool_free_command(shost->cmd_pool, cmd);
331
332 put_device(dev);
333}
334EXPORT_SYMBOL(__scsi_put_command);
335
336
337
338
339
340
341
342
343
344void scsi_put_command(struct scsi_cmnd *cmd)
345{
346 struct scsi_device *sdev = cmd->device;
347 unsigned long flags;
348
349
350 spin_lock_irqsave(&cmd->device->list_lock, flags);
351 BUG_ON(list_empty(&cmd->list));
352 list_del_init(&cmd->list);
353 spin_unlock_irqrestore(&cmd->device->list_lock, flags);
354
355 __scsi_put_command(cmd->device->host, cmd, &sdev->sdev_gendev);
356}
357EXPORT_SYMBOL(scsi_put_command);
358
359static struct scsi_host_cmd_pool *scsi_get_host_cmd_pool(gfp_t gfp_mask)
360{
361 struct scsi_host_cmd_pool *retval = NULL, *pool;
362
363
364
365
366 mutex_lock(&host_cmd_pool_mutex);
367 pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool :
368 &scsi_cmd_pool;
369 if (!pool->users) {
370 pool->cmd_slab = kmem_cache_create(pool->cmd_name,
371 sizeof(struct scsi_cmnd), 0,
372 pool->slab_flags, NULL);
373 if (!pool->cmd_slab)
374 goto fail;
375
376 pool->sense_slab = kmem_cache_create(pool->sense_name,
377 SCSI_SENSE_BUFFERSIZE, 0,
378 pool->slab_flags, NULL);
379 if (!pool->sense_slab) {
380 kmem_cache_destroy(pool->cmd_slab);
381 goto fail;
382 }
383 }
384
385 pool->users++;
386 retval = pool;
387 fail:
388 mutex_unlock(&host_cmd_pool_mutex);
389 return retval;
390}
391
392static void scsi_put_host_cmd_pool(gfp_t gfp_mask)
393{
394 struct scsi_host_cmd_pool *pool;
395
396 mutex_lock(&host_cmd_pool_mutex);
397 pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool :
398 &scsi_cmd_pool;
399
400
401
402
403
404 BUG_ON(pool->users == 0);
405
406 if (!--pool->users) {
407 kmem_cache_destroy(pool->cmd_slab);
408 kmem_cache_destroy(pool->sense_slab);
409 }
410 mutex_unlock(&host_cmd_pool_mutex);
411}
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426struct scsi_cmnd *scsi_allocate_command(gfp_t gfp_mask)
427{
428 struct scsi_host_cmd_pool *pool = scsi_get_host_cmd_pool(gfp_mask);
429
430 if (!pool)
431 return NULL;
432
433 return scsi_pool_alloc_command(pool, gfp_mask);
434}
435EXPORT_SYMBOL(scsi_allocate_command);
436
437
438
439
440
441
442
443
444
445
446void scsi_free_command(gfp_t gfp_mask, struct scsi_cmnd *cmd)
447{
448 struct scsi_host_cmd_pool *pool = scsi_get_host_cmd_pool(gfp_mask);
449
450
451
452
453
454
455
456 BUG_ON(!pool);
457
458 scsi_pool_free_command(pool, cmd);
459
460
461
462
463
464 scsi_put_host_cmd_pool(gfp_mask);
465 scsi_put_host_cmd_pool(gfp_mask);
466}
467EXPORT_SYMBOL(scsi_free_command);
468
469
470
471
472
473
474
475
476
477
478
479int scsi_setup_command_freelist(struct Scsi_Host *shost)
480{
481 struct scsi_cmnd *cmd;
482 const gfp_t gfp_mask = shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL;
483
484 spin_lock_init(&shost->free_list_lock);
485 INIT_LIST_HEAD(&shost->free_list);
486
487 shost->cmd_pool = scsi_get_host_cmd_pool(gfp_mask);
488
489 if (!shost->cmd_pool)
490 return -ENOMEM;
491
492
493
494
495 cmd = scsi_host_alloc_command(shost, gfp_mask);
496 if (!cmd) {
497 scsi_put_host_cmd_pool(gfp_mask);
498 shost->cmd_pool = NULL;
499 return -ENOMEM;
500 }
501 list_add(&cmd->list, &shost->free_list);
502 return 0;
503}
504
505
506
507
508
509void scsi_destroy_command_freelist(struct Scsi_Host *shost)
510{
511
512
513
514
515 if (!shost->cmd_pool)
516 return;
517
518 while (!list_empty(&shost->free_list)) {
519 struct scsi_cmnd *cmd;
520
521 cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
522 list_del_init(&cmd->list);
523 scsi_pool_free_command(shost->cmd_pool, cmd);
524 }
525 shost->cmd_pool = NULL;
526 scsi_put_host_cmd_pool(shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL);
527}
528
529#ifdef CONFIG_SCSI_LOGGING
530void scsi_log_send(struct scsi_cmnd *cmd)
531{
532 unsigned int level;
533
534
535
536
537
538
539
540
541
542
543
544
545 if (unlikely(scsi_logging_level)) {
546 level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
547 SCSI_LOG_MLQUEUE_BITS);
548 if (level > 1) {
549 scmd_printk(KERN_INFO, cmd, "Send: ");
550 if (level > 2)
551 printk("0x%p ", cmd);
552 printk("\n");
553 scsi_print_command(cmd);
554 if (level > 3) {
555 printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
556 " queuecommand 0x%p\n",
557 scsi_sglist(cmd), scsi_bufflen(cmd),
558 cmd->device->host->hostt->queuecommand);
559
560 }
561 }
562 }
563}
564
565void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
566{
567 unsigned int level;
568
569
570
571
572
573
574
575
576
577
578
579
580
581 if (unlikely(scsi_logging_level)) {
582 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
583 SCSI_LOG_MLCOMPLETE_BITS);
584 if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
585 (level > 1)) {
586 scmd_printk(KERN_INFO, cmd, "Done: ");
587 if (level > 2)
588 printk("0x%p ", cmd);
589
590
591
592
593 switch (disposition) {
594 case SUCCESS:
595 printk("SUCCESS\n");
596 break;
597 case NEEDS_RETRY:
598 printk("RETRY\n");
599 break;
600 case ADD_TO_MLQUEUE:
601 printk("MLQUEUE\n");
602 break;
603 case FAILED:
604 printk("FAILED\n");
605 break;
606 case TIMEOUT_ERROR:
607
608
609
610 printk("TIMEOUT\n");
611 break;
612 default:
613 printk("UNKNOWN\n");
614 }
615 scsi_print_result(cmd);
616 scsi_print_command(cmd);
617 if (status_byte(cmd->result) & CHECK_CONDITION)
618 scsi_print_sense("", cmd);
619 if (level > 3)
620 scmd_printk(KERN_INFO, cmd,
621 "scsi host busy %d failed %d\n",
622 cmd->device->host->host_busy,
623 cmd->device->host->host_failed);
624 }
625 }
626}
627#endif
628
629
630
631
632
633
634
635
636
637void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
638{
639 cmd->serial_number = host->cmd_serial_number++;
640 if (cmd->serial_number == 0)
641 cmd->serial_number = host->cmd_serial_number++;
642}
643EXPORT_SYMBOL(scsi_cmd_get_serial);
644
645
646
647
648
649
650
651
652int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
653{
654 struct Scsi_Host *host = cmd->device->host;
655 unsigned long timeout;
656 int rtn = 0;
657
658 atomic_inc(&cmd->device->iorequest_cnt);
659
660
661 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
662
663
664
665 cmd->result = DID_NO_CONNECT << 16;
666 scsi_done(cmd);
667
668 goto out;
669 }
670
671
672 if (unlikely(scsi_device_blocked(cmd->device))) {
673
674
675
676
677
678
679
680
681 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
682
683 SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n"));
684
685
686
687
688
689 goto out;
690 }
691
692
693
694
695 if (cmd->device->scsi_level <= SCSI_2 &&
696 cmd->device->scsi_level != SCSI_UNKNOWN) {
697 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
698 (cmd->device->lun << 5 & 0xe0);
699 }
700
701
702
703
704
705 timeout = host->last_reset + MIN_RESET_DELAY;
706
707 if (host->resetting && time_before(jiffies, timeout)) {
708 int ticks_remaining = timeout - jiffies;
709
710
711
712
713
714
715
716
717
718 while (--ticks_remaining >= 0)
719 mdelay(1 + 999 / HZ);
720 host->resetting = 0;
721 }
722
723 scsi_log_send(cmd);
724
725
726
727
728
729 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
730 SCSI_LOG_MLQUEUE(3,
731 printk("queuecommand : command too long. "
732 "cdb_size=%d host->max_cmd_len=%d\n",
733 cmd->cmd_len, cmd->device->host->max_cmd_len));
734 cmd->result = (DID_ABORT << 16);
735
736 scsi_done(cmd);
737 goto out;
738 }
739
740 if (unlikely(host->shost_state == SHOST_DEL)) {
741 cmd->result = (DID_NO_CONNECT << 16);
742 scsi_done(cmd);
743 } else {
744 trace_scsi_dispatch_cmd_start(cmd);
745 cmd->scsi_done = scsi_done;
746 rtn = host->hostt->queuecommand(host, cmd);
747 }
748
749 if (rtn) {
750 trace_scsi_dispatch_cmd_error(cmd, rtn);
751 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
752 rtn != SCSI_MLQUEUE_TARGET_BUSY)
753 rtn = SCSI_MLQUEUE_HOST_BUSY;
754
755 scsi_queue_insert(cmd, rtn);
756
757 SCSI_LOG_MLQUEUE(3,
758 printk("queuecommand : request rejected\n"));
759 }
760
761 out:
762 SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
763 return rtn;
764}
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779static void scsi_done(struct scsi_cmnd *cmd)
780{
781 trace_scsi_dispatch_cmd_done(cmd);
782 blk_complete_request(cmd->request);
783}
784
785
786static struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
787{
788 return *(struct scsi_driver **)cmd->request->rq_disk->private_data;
789}
790
791
792
793
794
795
796
797
798
799void scsi_finish_command(struct scsi_cmnd *cmd)
800{
801 struct scsi_device *sdev = cmd->device;
802 struct scsi_target *starget = scsi_target(sdev);
803 struct Scsi_Host *shost = sdev->host;
804 struct scsi_driver *drv;
805 unsigned int good_bytes;
806
807 scsi_device_unbusy(sdev);
808
809
810
811
812
813
814
815
816
817 shost->host_blocked = 0;
818 starget->target_blocked = 0;
819 sdev->device_blocked = 0;
820
821
822
823
824
825 if (SCSI_SENSE_VALID(cmd))
826 cmd->result |= (DRIVER_SENSE << 24);
827
828 SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
829 "Notifying upper driver of completion "
830 "(result %x)\n", cmd->result));
831
832 good_bytes = scsi_bufflen(cmd);
833 if (cmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
834 int old_good_bytes = good_bytes;
835 drv = scsi_cmd_to_driver(cmd);
836 if (drv->done)
837 good_bytes = drv->done(cmd);
838
839
840
841
842
843
844 if (good_bytes == old_good_bytes)
845 good_bytes -= scsi_get_resid(cmd);
846 }
847 scsi_io_completion(cmd, good_bytes);
848}
849EXPORT_SYMBOL(scsi_finish_command);
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
870{
871 unsigned long flags;
872
873
874
875
876 if (tags <= 0)
877 return;
878
879 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
880
881
882
883
884
885
886
887
888
889
890 if (!sdev->host->bqt) {
891 if (blk_queue_tagged(sdev->request_queue) &&
892 blk_queue_resize_tags(sdev->request_queue, tags) != 0)
893 goto out;
894 }
895
896 sdev->queue_depth = tags;
897 switch (tagged) {
898 case MSG_ORDERED_TAG:
899 sdev->ordered_tags = 1;
900 sdev->simple_tags = 1;
901 break;
902 case MSG_SIMPLE_TAG:
903 sdev->ordered_tags = 0;
904 sdev->simple_tags = 1;
905 break;
906 default:
907 sdev_printk(KERN_WARNING, sdev,
908 "scsi_adjust_queue_depth, bad queue type, "
909 "disabled\n");
910 case 0:
911 sdev->ordered_tags = sdev->simple_tags = 0;
912 sdev->queue_depth = tags;
913 break;
914 }
915 out:
916 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
917}
918EXPORT_SYMBOL(scsi_adjust_queue_depth);
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939int scsi_track_queue_full(struct scsi_device *sdev, int depth)
940{
941
942
943
944
945
946
947 if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4))
948 return 0;
949
950 sdev->last_queue_full_time = jiffies;
951 if (sdev->last_queue_full_depth != depth) {
952 sdev->last_queue_full_count = 1;
953 sdev->last_queue_full_depth = depth;
954 } else {
955 sdev->last_queue_full_count++;
956 }
957
958 if (sdev->last_queue_full_count <= 10)
959 return 0;
960 if (sdev->last_queue_full_depth < 8) {
961
962 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
963 return -1;
964 }
965
966 if (sdev->ordered_tags)
967 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
968 else
969 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
970 return depth;
971}
972EXPORT_SYMBOL(scsi_track_queue_full);
973
974
975
976
977
978
979
980
981
982
983
984
985
986static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer,
987 u8 page, unsigned len)
988{
989 int result;
990 unsigned char cmd[16];
991
992 cmd[0] = INQUIRY;
993 cmd[1] = 1;
994 cmd[2] = page;
995 cmd[3] = len >> 8;
996 cmd[4] = len & 0xff;
997 cmd[5] = 0;
998
999
1000
1001
1002
1003 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer,
1004 len, NULL, 30 * HZ, 3, NULL);
1005 if (result)
1006 return result;
1007
1008
1009 if (buffer[1] != page)
1010 return -EIO;
1011
1012 return 0;
1013}
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
1030 int buf_len)
1031{
1032 int i, result;
1033
1034
1035 result = scsi_vpd_inquiry(sdev, buf, 0, buf_len);
1036 if (result)
1037 goto fail;
1038
1039
1040 if (page == 0)
1041 return 0;
1042
1043 for (i = 0; i < min((int)buf[3], buf_len - 4); i++)
1044 if (buf[i + 4] == page)
1045 goto found;
1046
1047 if (i < buf[3] && i >= buf_len - 4)
1048
1049 goto found;
1050
1051 goto fail;
1052
1053 found:
1054 result = scsi_vpd_inquiry(sdev, buf, page, buf_len);
1055 if (result)
1056 goto fail;
1057
1058 return 0;
1059
1060 fail:
1061 return -EINVAL;
1062}
1063EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073int scsi_device_get(struct scsi_device *sdev)
1074{
1075 if (sdev->sdev_state == SDEV_DEL)
1076 return -ENXIO;
1077 if (!get_device(&sdev->sdev_gendev))
1078 return -ENXIO;
1079
1080
1081 try_module_get(sdev->host->hostt->module);
1082
1083 return 0;
1084}
1085EXPORT_SYMBOL(scsi_device_get);
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095void scsi_device_put(struct scsi_device *sdev)
1096{
1097#ifdef CONFIG_MODULE_UNLOAD
1098 struct module *module = sdev->host->hostt->module;
1099
1100
1101
1102 if (module && module_refcount(module) != 0)
1103 module_put(module);
1104#endif
1105 put_device(&sdev->sdev_gendev);
1106}
1107EXPORT_SYMBOL(scsi_device_put);
1108
1109
1110struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
1111 struct scsi_device *prev)
1112{
1113 struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
1114 struct scsi_device *next = NULL;
1115 unsigned long flags;
1116
1117 spin_lock_irqsave(shost->host_lock, flags);
1118 while (list->next != &shost->__devices) {
1119 next = list_entry(list->next, struct scsi_device, siblings);
1120
1121 if (!scsi_device_get(next))
1122 break;
1123 next = NULL;
1124 list = list->next;
1125 }
1126 spin_unlock_irqrestore(shost->host_lock, flags);
1127
1128 if (prev)
1129 scsi_device_put(prev);
1130 return next;
1131}
1132EXPORT_SYMBOL(__scsi_iterate_devices);
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144void starget_for_each_device(struct scsi_target *starget, void *data,
1145 void (*fn)(struct scsi_device *, void *))
1146{
1147 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1148 struct scsi_device *sdev;
1149
1150 shost_for_each_device(sdev, shost) {
1151 if ((sdev->channel == starget->channel) &&
1152 (sdev->id == starget->id))
1153 fn(sdev, data);
1154 }
1155}
1156EXPORT_SYMBOL(starget_for_each_device);
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172void __starget_for_each_device(struct scsi_target *starget, void *data,
1173 void (*fn)(struct scsi_device *, void *))
1174{
1175 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1176 struct scsi_device *sdev;
1177
1178 __shost_for_each_device(sdev, shost) {
1179 if ((sdev->channel == starget->channel) &&
1180 (sdev->id == starget->id))
1181 fn(sdev, data);
1182 }
1183}
1184EXPORT_SYMBOL(__starget_for_each_device);
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
1202 uint lun)
1203{
1204 struct scsi_device *sdev;
1205
1206 list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
1207 if (sdev->sdev_state == SDEV_DEL)
1208 continue;
1209 if (sdev->lun ==lun)
1210 return sdev;
1211 }
1212
1213 return NULL;
1214}
1215EXPORT_SYMBOL(__scsi_device_lookup_by_target);
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
1227 uint lun)
1228{
1229 struct scsi_device *sdev;
1230 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1231 unsigned long flags;
1232
1233 spin_lock_irqsave(shost->host_lock, flags);
1234 sdev = __scsi_device_lookup_by_target(starget, lun);
1235 if (sdev && scsi_device_get(sdev))
1236 sdev = NULL;
1237 spin_unlock_irqrestore(shost->host_lock, flags);
1238
1239 return sdev;
1240}
1241EXPORT_SYMBOL(scsi_device_lookup_by_target);
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
1260 uint channel, uint id, uint lun)
1261{
1262 struct scsi_device *sdev;
1263
1264 list_for_each_entry(sdev, &shost->__devices, siblings) {
1265 if (sdev->channel == channel && sdev->id == id &&
1266 sdev->lun ==lun)
1267 return sdev;
1268 }
1269
1270 return NULL;
1271}
1272EXPORT_SYMBOL(__scsi_device_lookup);
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
1286 uint channel, uint id, uint lun)
1287{
1288 struct scsi_device *sdev;
1289 unsigned long flags;
1290
1291 spin_lock_irqsave(shost->host_lock, flags);
1292 sdev = __scsi_device_lookup(shost, channel, id, lun);
1293 if (sdev && scsi_device_get(sdev))
1294 sdev = NULL;
1295 spin_unlock_irqrestore(shost->host_lock, flags);
1296
1297 return sdev;
1298}
1299EXPORT_SYMBOL(scsi_device_lookup);
1300
1301MODULE_DESCRIPTION("SCSI core");
1302MODULE_LICENSE("GPL");
1303
1304module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
1305MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
1306
1307static int __init init_scsi(void)
1308{
1309 int error;
1310
1311 error = scsi_init_queue();
1312 if (error)
1313 return error;
1314 error = scsi_init_procfs();
1315 if (error)
1316 goto cleanup_queue;
1317 error = scsi_init_devinfo();
1318 if (error)
1319 goto cleanup_procfs;
1320 error = scsi_init_hosts();
1321 if (error)
1322 goto cleanup_devlist;
1323 error = scsi_init_sysctl();
1324 if (error)
1325 goto cleanup_hosts;
1326 error = scsi_sysfs_register();
1327 if (error)
1328 goto cleanup_sysctl;
1329
1330 scsi_netlink_init();
1331
1332 printk(KERN_NOTICE "SCSI subsystem initialized\n");
1333 return 0;
1334
1335cleanup_sysctl:
1336 scsi_exit_sysctl();
1337cleanup_hosts:
1338 scsi_exit_hosts();
1339cleanup_devlist:
1340 scsi_exit_devinfo();
1341cleanup_procfs:
1342 scsi_exit_procfs();
1343cleanup_queue:
1344 scsi_exit_queue();
1345 printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
1346 -error);
1347 return error;
1348}
1349
1350static void __exit exit_scsi(void)
1351{
1352 scsi_netlink_exit();
1353 scsi_sysfs_unregister();
1354 scsi_exit_sysctl();
1355 scsi_exit_hosts();
1356 scsi_exit_devinfo();
1357 scsi_exit_procfs();
1358 scsi_exit_queue();
1359}
1360
1361subsys_initcall(init_scsi);
1362module_exit(exit_scsi);
1363