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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267#undef HAVE_OLD_UX4F_FIRMWARE
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408#include <linux/string.h>
409#include <linux/kernel.h>
410#include <linux/ioport.h>
411#include <linux/delay.h>
412#include <asm/io.h>
413#include <asm/system.h>
414#include <asm/byteorder.h>
415#include <linux/proc_fs.h>
416#include <linux/blkdev.h>
417#include <linux/interrupt.h>
418#include <linux/stat.h>
419#include <linux/pci.h>
420#include <linux/init.h>
421#include <linux/ctype.h>
422#include <linux/spinlock.h>
423#include <linux/slab.h>
424#include <asm/dma.h>
425#include <asm/irq.h>
426
427#include <scsi/scsi.h>
428#include <scsi/scsi_cmnd.h>
429#include <scsi/scsi_device.h>
430#include <scsi/scsi_host.h>
431#include <scsi/scsi_tcq.h>
432#include <scsi/scsicam.h>
433
434static int u14_34f_detect(struct scsi_host_template *);
435static int u14_34f_release(struct Scsi_Host *);
436static int u14_34f_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
437static int u14_34f_eh_abort(struct scsi_cmnd *);
438static int u14_34f_eh_host_reset(struct scsi_cmnd *);
439static int u14_34f_bios_param(struct scsi_device *, struct block_device *,
440 sector_t, int *);
441static int u14_34f_slave_configure(struct scsi_device *);
442
443static struct scsi_host_template driver_template = {
444 .name = "UltraStor 14F/34F rev. 8.10.00 ",
445 .detect = u14_34f_detect,
446 .release = u14_34f_release,
447 .queuecommand = u14_34f_queuecommand,
448 .eh_abort_handler = u14_34f_eh_abort,
449 .eh_host_reset_handler = u14_34f_eh_host_reset,
450 .bios_param = u14_34f_bios_param,
451 .slave_configure = u14_34f_slave_configure,
452 .this_id = 7,
453 .unchecked_isa_dma = 1,
454 .use_clustering = ENABLE_CLUSTERING,
455 };
456
457#if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
458#error "Adjust your <asm/byteorder.h> defines"
459#endif
460
461
462#define PRODUCT_ID1 0x56
463#define PRODUCT_ID2 0x40
464
465
466#define ISA 0
467#define ESA 1
468
469#define OP_HOST_ADAPTER 0x1
470#define OP_SCSI 0x2
471#define OP_RESET 0x4
472#define DTD_SCSI 0x0
473#define DTD_IN 0x1
474#define DTD_OUT 0x2
475#define DTD_NONE 0x3
476#define HA_CMD_INQUIRY 0x1
477#define HA_CMD_SELF_DIAG 0x2
478#define HA_CMD_READ_BUFF 0x3
479#define HA_CMD_WRITE_BUFF 0x4
480
481#undef DEBUG_LINKED_COMMANDS
482#undef DEBUG_DETECT
483#undef DEBUG_INTERRUPT
484#undef DEBUG_RESET
485#undef DEBUG_GENERATE_ERRORS
486#undef DEBUG_GENERATE_ABORTS
487#undef DEBUG_GEOMETRY
488
489#define MAX_ISA 3
490#define MAX_VESA 1
491#define MAX_EISA 0
492#define MAX_PCI 0
493#define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
494#define MAX_CHANNEL 1
495#define MAX_LUN 8
496#define MAX_TARGET 8
497#define MAX_MAILBOXES 16
498#define MAX_SGLIST 32
499#define MAX_SAFE_SGLIST 16
500#define MAX_INTERNAL_RETRIES 64
501#define MAX_CMD_PER_LUN 2
502#define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
503
504#define SKIP ULONG_MAX
505#define FALSE 0
506#define TRUE 1
507#define FREE 0
508#define IN_USE 1
509#define LOCKED 2
510#define IN_RESET 3
511#define IGNORE 4
512#define READY 5
513#define ABORTING 6
514#define NO_DMA 0xff
515#define MAXLOOP 10000
516#define TAG_DISABLED 0
517#define TAG_SIMPLE 1
518#define TAG_ORDERED 2
519
520#define REG_LCL_MASK 0
521#define REG_LCL_INTR 1
522#define REG_SYS_MASK 2
523#define REG_SYS_INTR 3
524#define REG_PRODUCT_ID1 4
525#define REG_PRODUCT_ID2 5
526#define REG_CONFIG1 6
527#define REG_CONFIG2 7
528#define REG_OGM 8
529#define REG_ICM 12
530#define REGION_SIZE 13UL
531#define BSY_ASSERTED 0x01
532#define IRQ_ASSERTED 0x01
533#define CMD_RESET 0xc0
534#define CMD_OGM_INTR 0x01
535#define CMD_CLR_INTR 0x01
536#define CMD_ENA_INTR 0x81
537#define ASOK 0x00
538#define ASST 0x91
539
540#define YESNO(a) ((a) ? 'y' : 'n')
541#define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
542
543#define PACKED __attribute__((packed))
544
545struct sg_list {
546 unsigned int address;
547 unsigned int num_bytes;
548 };
549
550
551struct mscp {
552
553#if defined(__BIG_ENDIAN_BITFIELD)
554 unsigned char sg:1, ca:1, dcn:1, xdir:2, opcode:3;
555 unsigned char lun: 3, channel:2, target:3;
556#else
557 unsigned char opcode: 3,
558 xdir: 2,
559 dcn: 1,
560 ca: 1,
561 sg: 1;
562 unsigned char target: 3,
563 channel: 2,
564 lun: 3;
565#endif
566
567 unsigned int data_address PACKED;
568 unsigned int data_len PACKED;
569 unsigned int link_address PACKED;
570 unsigned char clink_id;
571 unsigned char use_sg;
572 unsigned char sense_len;
573 unsigned char cdb_len;
574 unsigned char cdb[12];
575 unsigned char adapter_status;
576 unsigned char target_status;
577 unsigned int sense_addr PACKED;
578
579
580 struct scsi_cmnd *SCpnt;
581 unsigned int cpp_index;
582
583
584
585 dma_addr_t cp_dma_addr;
586 struct sg_list *sglist;
587 };
588
589#define CP_TAIL_SIZE (sizeof(struct sglist *) + sizeof(dma_addr_t))
590
591struct hostdata {
592 struct mscp cp[MAX_MAILBOXES];
593 unsigned int cp_stat[MAX_MAILBOXES];
594 unsigned int last_cp_used;
595 unsigned int iocount;
596 int board_number;
597 char board_name[16];
598 int in_reset;
599 int target_to[MAX_TARGET][MAX_CHANNEL];
600 int target_redo[MAX_TARGET][MAX_CHANNEL];
601 unsigned int retries;
602 unsigned long last_retried_pid;
603 unsigned char subversion;
604 struct pci_dev *pdev;
605 unsigned char heads;
606 unsigned char sectors;
607 char board_id[256];
608 };
609
610static struct Scsi_Host *sh[MAX_BOARDS + 1];
611static const char *driver_name = "Ux4F";
612static char sha[MAX_BOARDS];
613static DEFINE_SPINLOCK(driver_lock);
614
615
616static unsigned int num_boards = MAX_BOARDS;
617
618static unsigned long io_port[] = {
619
620
621 SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
622 SKIP, SKIP,
623
624
625 0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140,
626
627
628 0x0
629 };
630
631#define HD(board) ((struct hostdata *) &sh[board]->hostdata)
632#define BN(board) (HD(board)->board_name)
633
634
635#define H2DEV(x) cpu_to_le32(x)
636#define DEV2H(x) le32_to_cpu(x)
637
638static irqreturn_t do_interrupt_handler(int, void *);
639static void flush_dev(struct scsi_device *, unsigned long, unsigned int, unsigned int);
640static int do_trace = FALSE;
641static int setup_done = FALSE;
642static int link_statistics;
643static int ext_tran = FALSE;
644
645#if defined(HAVE_OLD_UX4F_FIRMWARE)
646static int have_old_firmware = TRUE;
647#else
648static int have_old_firmware = FALSE;
649#endif
650
651#if defined(CONFIG_SCSI_U14_34F_TAGGED_QUEUE)
652static int tag_mode = TAG_SIMPLE;
653#else
654static int tag_mode = TAG_DISABLED;
655#endif
656
657#if defined(CONFIG_SCSI_U14_34F_LINKED_COMMANDS)
658static int linked_comm = TRUE;
659#else
660static int linked_comm = FALSE;
661#endif
662
663#if defined(CONFIG_SCSI_U14_34F_MAX_TAGS)
664static int max_queue_depth = CONFIG_SCSI_U14_34F_MAX_TAGS;
665#else
666static int max_queue_depth = MAX_CMD_PER_LUN;
667#endif
668
669#define MAX_INT_PARAM 10
670#define MAX_BOOT_OPTIONS_SIZE 256
671static char boot_options[MAX_BOOT_OPTIONS_SIZE];
672
673#if defined(MODULE)
674#include <linux/module.h>
675#include <linux/moduleparam.h>
676
677module_param_string(u14_34f, boot_options, MAX_BOOT_OPTIONS_SIZE, 0);
678MODULE_PARM_DESC(u14_34f, " equivalent to the \"u14-34f=...\" kernel boot " \
679"option." \
680" Example: modprobe u14-34f \"u14_34f=0x340,0x330,lc:y,tm:0,mq:4\"");
681MODULE_AUTHOR("Dario Ballabio");
682MODULE_LICENSE("GPL");
683MODULE_DESCRIPTION("UltraStor 14F/34F SCSI Driver");
684
685#endif
686
687static int u14_34f_slave_configure(struct scsi_device *dev) {
688 int j, tqd, utqd;
689 char *tag_suffix, *link_suffix;
690 struct Scsi_Host *host = dev->host;
691
692 j = ((struct hostdata *) host->hostdata)->board_number;
693
694 utqd = MAX_CMD_PER_LUN;
695 tqd = max_queue_depth;
696
697 if (TLDEV(dev->type) && dev->tagged_supported)
698
699 if (tag_mode == TAG_SIMPLE) {
700 scsi_adjust_queue_depth(dev, MSG_SIMPLE_TAG, tqd);
701 tag_suffix = ", simple tags";
702 }
703 else if (tag_mode == TAG_ORDERED) {
704 scsi_adjust_queue_depth(dev, MSG_ORDERED_TAG, tqd);
705 tag_suffix = ", ordered tags";
706 }
707 else {
708 scsi_adjust_queue_depth(dev, 0, tqd);
709 tag_suffix = ", no tags";
710 }
711
712 else if (TLDEV(dev->type) && linked_comm) {
713 scsi_adjust_queue_depth(dev, 0, tqd);
714 tag_suffix = ", untagged";
715 }
716
717 else {
718 scsi_adjust_queue_depth(dev, 0, utqd);
719 tag_suffix = "";
720 }
721
722 if (TLDEV(dev->type) && linked_comm && dev->queue_depth > 2)
723 link_suffix = ", sorted";
724 else if (TLDEV(dev->type))
725 link_suffix = ", unsorted";
726 else
727 link_suffix = "";
728
729 sdev_printk(KERN_INFO, dev, "cmds/lun %d%s%s.\n",
730 dev->queue_depth, link_suffix, tag_suffix);
731
732 return FALSE;
733}
734
735static int wait_on_busy(unsigned long iobase, unsigned int loop) {
736
737 while (inb(iobase + REG_LCL_INTR) & BSY_ASSERTED) {
738 udelay(1L);
739 if (--loop == 0) return TRUE;
740 }
741
742 return FALSE;
743}
744
745static int board_inquiry(unsigned int j) {
746 struct mscp *cpp;
747 dma_addr_t id_dma_addr;
748 unsigned int limit = 0;
749 unsigned long time;
750
751 id_dma_addr = pci_map_single(HD(j)->pdev, HD(j)->board_id,
752 sizeof(HD(j)->board_id), PCI_DMA_BIDIRECTIONAL);
753 cpp = &HD(j)->cp[0];
754 cpp->cp_dma_addr = pci_map_single(HD(j)->pdev, cpp, sizeof(struct mscp),
755 PCI_DMA_BIDIRECTIONAL);
756 memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
757 cpp->opcode = OP_HOST_ADAPTER;
758 cpp->xdir = DTD_IN;
759 cpp->data_address = H2DEV(id_dma_addr);
760 cpp->data_len = H2DEV(sizeof(HD(j)->board_id));
761 cpp->cdb_len = 6;
762 cpp->cdb[0] = HA_CMD_INQUIRY;
763
764 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
765 printk("%s: board_inquiry, adapter busy.\n", BN(j));
766 return TRUE;
767 }
768
769 HD(j)->cp_stat[0] = IGNORE;
770
771
772 outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
773
774
775 outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
776
777
778 outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
779
780 spin_unlock_irq(&driver_lock);
781 time = jiffies;
782 while ((jiffies - time) < HZ && limit++ < 20000) udelay(100L);
783 spin_lock_irq(&driver_lock);
784
785 if (cpp->adapter_status || HD(j)->cp_stat[0] != FREE) {
786 HD(j)->cp_stat[0] = FREE;
787 printk("%s: board_inquiry, err 0x%x.\n", BN(j), cpp->adapter_status);
788 return TRUE;
789 }
790
791 pci_unmap_single(HD(j)->pdev, cpp->cp_dma_addr, sizeof(struct mscp),
792 PCI_DMA_BIDIRECTIONAL);
793 pci_unmap_single(HD(j)->pdev, id_dma_addr, sizeof(HD(j)->board_id),
794 PCI_DMA_BIDIRECTIONAL);
795 return FALSE;
796}
797
798static int port_detect \
799 (unsigned long port_base, unsigned int j, struct scsi_host_template *tpnt) {
800 unsigned char irq, dma_channel, subversion, i;
801 unsigned char in_byte;
802 char *bus_type, dma_name[16];
803
804
805 unsigned long bios_segment_table[8] = {
806 0,
807 0xc4000, 0xc8000, 0xcc000, 0xd0000,
808 0xd4000, 0xd8000, 0xdc000
809 };
810
811
812 unsigned char interrupt_table[4] = { 15, 14, 11, 10 };
813
814
815 unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
816
817
818 struct {
819 unsigned char heads;
820 unsigned char sectors;
821 } mapping_table[4] = {
822 { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 }
823 };
824
825 struct config_1 {
826
827#if defined(__BIG_ENDIAN_BITFIELD)
828 unsigned char dma_channel: 2, interrupt:2,
829 removable_disks_as_fixed:1, bios_segment: 3;
830#else
831 unsigned char bios_segment: 3, removable_disks_as_fixed: 1,
832 interrupt: 2, dma_channel: 2;
833#endif
834
835 } config_1;
836
837 struct config_2 {
838
839#if defined(__BIG_ENDIAN_BITFIELD)
840 unsigned char tfr_port: 2, bios_drive_number: 1,
841 mapping_mode: 2, ha_scsi_id: 3;
842#else
843 unsigned char ha_scsi_id: 3, mapping_mode: 2,
844 bios_drive_number: 1, tfr_port: 2;
845#endif
846
847 } config_2;
848
849 char name[16];
850
851 sprintf(name, "%s%d", driver_name, j);
852
853 if (!request_region(port_base, REGION_SIZE, driver_name)) {
854#if defined(DEBUG_DETECT)
855 printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
856#endif
857 goto fail;
858 }
859
860 spin_lock_irq(&driver_lock);
861
862 if (inb(port_base + REG_PRODUCT_ID1) != PRODUCT_ID1) goto freelock;
863
864 in_byte = inb(port_base + REG_PRODUCT_ID2);
865
866 if ((in_byte & 0xf0) != PRODUCT_ID2) goto freelock;
867
868 *(char *)&config_1 = inb(port_base + REG_CONFIG1);
869 *(char *)&config_2 = inb(port_base + REG_CONFIG2);
870
871 irq = interrupt_table[config_1.interrupt];
872 dma_channel = dma_channel_table[config_1.dma_channel];
873 subversion = (in_byte & 0x0f);
874
875
876 if (request_irq(irq, do_interrupt_handler,
877 IRQF_DISABLED | ((subversion == ESA) ? IRQF_SHARED : 0),
878 driver_name, (void *) &sha[j])) {
879 printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
880 goto freelock;
881 }
882
883 if (subversion == ISA && request_dma(dma_channel, driver_name)) {
884 printk("%s: unable to allocate DMA channel %u, detaching.\n",
885 name, dma_channel);
886 goto freeirq;
887 }
888
889 if (have_old_firmware) tpnt->use_clustering = DISABLE_CLUSTERING;
890
891 spin_unlock_irq(&driver_lock);
892 sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
893 spin_lock_irq(&driver_lock);
894
895 if (sh[j] == NULL) {
896 printk("%s: unable to register host, detaching.\n", name);
897 goto freedma;
898 }
899
900 sh[j]->io_port = port_base;
901 sh[j]->unique_id = port_base;
902 sh[j]->n_io_port = REGION_SIZE;
903 sh[j]->base = bios_segment_table[config_1.bios_segment];
904 sh[j]->irq = irq;
905 sh[j]->sg_tablesize = MAX_SGLIST;
906 sh[j]->this_id = config_2.ha_scsi_id;
907 sh[j]->can_queue = MAX_MAILBOXES;
908 sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
909
910#if defined(DEBUG_DETECT)
911 {
912 unsigned char sys_mask, lcl_mask;
913
914 sys_mask = inb(sh[j]->io_port + REG_SYS_MASK);
915 lcl_mask = inb(sh[j]->io_port + REG_LCL_MASK);
916 printk("SYS_MASK 0x%x, LCL_MASK 0x%x.\n", sys_mask, lcl_mask);
917 }
918#endif
919
920
921 if (sh[j]->this_id == 0) sh[j]->this_id = -1;
922
923
924 if (sh[j]->base == 0) outb(CMD_ENA_INTR, sh[j]->io_port + REG_SYS_MASK);
925
926 memset(HD(j), 0, sizeof(struct hostdata));
927 HD(j)->heads = mapping_table[config_2.mapping_mode].heads;
928 HD(j)->sectors = mapping_table[config_2.mapping_mode].sectors;
929 HD(j)->subversion = subversion;
930 HD(j)->pdev = NULL;
931 HD(j)->board_number = j;
932
933 if (have_old_firmware) sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
934
935 if (HD(j)->subversion == ESA) {
936 sh[j]->unchecked_isa_dma = FALSE;
937 sh[j]->dma_channel = NO_DMA;
938 sprintf(BN(j), "U34F%d", j);
939 bus_type = "VESA";
940 }
941 else {
942 unsigned long flags;
943 sh[j]->unchecked_isa_dma = TRUE;
944
945 flags=claim_dma_lock();
946 disable_dma(dma_channel);
947 clear_dma_ff(dma_channel);
948 set_dma_mode(dma_channel, DMA_MODE_CASCADE);
949 enable_dma(dma_channel);
950 release_dma_lock(flags);
951
952 sh[j]->dma_channel = dma_channel;
953 sprintf(BN(j), "U14F%d", j);
954 bus_type = "ISA";
955 }
956
957 sh[j]->max_channel = MAX_CHANNEL - 1;
958 sh[j]->max_id = MAX_TARGET;
959 sh[j]->max_lun = MAX_LUN;
960
961 if (HD(j)->subversion == ISA && !board_inquiry(j)) {
962 HD(j)->board_id[40] = 0;
963
964 if (strcmp(&HD(j)->board_id[32], "06000600")) {
965 printk("%s: %s.\n", BN(j), &HD(j)->board_id[8]);
966 printk("%s: firmware %s is outdated, FW PROM should be 28004-006.\n",
967 BN(j), &HD(j)->board_id[32]);
968 sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
969 sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
970 }
971 }
972
973 if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
974 else sprintf(dma_name, "DMA %u", dma_channel);
975
976 spin_unlock_irq(&driver_lock);
977
978 for (i = 0; i < sh[j]->can_queue; i++)
979 HD(j)->cp[i].cp_dma_addr = pci_map_single(HD(j)->pdev,
980 &HD(j)->cp[i], sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
981
982 for (i = 0; i < sh[j]->can_queue; i++)
983 if (! ((&HD(j)->cp[i])->sglist = kmalloc(
984 sh[j]->sg_tablesize * sizeof(struct sg_list),
985 (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
986 printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
987 goto release;
988 }
989
990 if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
991 max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
992
993 if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
994
995 if (tag_mode != TAG_DISABLED && tag_mode != TAG_SIMPLE)
996 tag_mode = TAG_ORDERED;
997
998 if (j == 0) {
999 printk("UltraStor 14F/34F: Copyright (C) 1994-2003 Dario Ballabio.\n");
1000 printk("%s config options -> of:%c, tm:%d, lc:%c, mq:%d, et:%c.\n",
1001 driver_name, YESNO(have_old_firmware), tag_mode,
1002 YESNO(linked_comm), max_queue_depth, YESNO(ext_tran));
1003 }
1004
1005 printk("%s: %s 0x%03lx, BIOS 0x%05x, IRQ %u, %s, SG %d, MB %d.\n",
1006 BN(j), bus_type, (unsigned long)sh[j]->io_port, (int)sh[j]->base,
1007 sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
1008
1009 if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
1010 printk("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
1011 BN(j), sh[j]->max_id, sh[j]->max_lun);
1012
1013 for (i = 0; i <= sh[j]->max_channel; i++)
1014 printk("%s: SCSI channel %u enabled, host target ID %d.\n",
1015 BN(j), i, sh[j]->this_id);
1016
1017 return TRUE;
1018
1019freedma:
1020 if (subversion == ISA) free_dma(dma_channel);
1021freeirq:
1022 free_irq(irq, &sha[j]);
1023freelock:
1024 spin_unlock_irq(&driver_lock);
1025 release_region(port_base, REGION_SIZE);
1026fail:
1027 return FALSE;
1028
1029release:
1030 u14_34f_release(sh[j]);
1031 return FALSE;
1032}
1033
1034static void internal_setup(char *str, int *ints) {
1035 int i, argc = ints[0];
1036 char *cur = str, *pc;
1037
1038 if (argc > 0) {
1039
1040 if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
1041
1042 for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
1043
1044 io_port[i] = 0;
1045 setup_done = TRUE;
1046 }
1047
1048 while (cur && (pc = strchr(cur, ':'))) {
1049 int val = 0, c = *++pc;
1050
1051 if (c == 'n' || c == 'N') val = FALSE;
1052 else if (c == 'y' || c == 'Y') val = TRUE;
1053 else val = (int) simple_strtoul(pc, NULL, 0);
1054
1055 if (!strncmp(cur, "lc:", 3)) linked_comm = val;
1056 else if (!strncmp(cur, "of:", 3)) have_old_firmware = val;
1057 else if (!strncmp(cur, "tm:", 3)) tag_mode = val;
1058 else if (!strncmp(cur, "tc:", 3)) tag_mode = val;
1059 else if (!strncmp(cur, "mq:", 3)) max_queue_depth = val;
1060 else if (!strncmp(cur, "ls:", 3)) link_statistics = val;
1061 else if (!strncmp(cur, "et:", 3)) ext_tran = val;
1062
1063 if ((cur = strchr(cur, ','))) ++cur;
1064 }
1065
1066 return;
1067}
1068
1069static int option_setup(char *str) {
1070 int ints[MAX_INT_PARAM];
1071 char *cur = str;
1072 int i = 1;
1073
1074 while (cur && isdigit(*cur) && i < MAX_INT_PARAM) {
1075 ints[i++] = simple_strtoul(cur, NULL, 0);
1076
1077 if ((cur = strchr(cur, ',')) != NULL) cur++;
1078 }
1079
1080 ints[0] = i - 1;
1081 internal_setup(cur, ints);
1082 return 1;
1083}
1084
1085static int u14_34f_detect(struct scsi_host_template *tpnt) {
1086 unsigned int j = 0, k;
1087
1088 tpnt->proc_name = "u14-34f";
1089
1090 if(strlen(boot_options)) option_setup(boot_options);
1091
1092#if defined(MODULE)
1093
1094 if(io_port[0] != SKIP) {
1095 setup_done = TRUE;
1096 io_port[MAX_INT_PARAM] = 0;
1097 }
1098#endif
1099
1100 for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
1101
1102 for (k = 0; io_port[k]; k++) {
1103
1104 if (io_port[k] == SKIP) continue;
1105
1106 if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
1107 }
1108
1109 num_boards = j;
1110 return j;
1111}
1112
1113static void map_dma(unsigned int i, unsigned int j) {
1114 unsigned int data_len = 0;
1115 unsigned int k, pci_dir;
1116 int count;
1117 struct scatterlist *sg;
1118 struct mscp *cpp;
1119 struct scsi_cmnd *SCpnt;
1120
1121 cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1122 pci_dir = SCpnt->sc_data_direction;
1123
1124 if (SCpnt->sense_buffer)
1125 cpp->sense_addr = H2DEV(pci_map_single(HD(j)->pdev, SCpnt->sense_buffer,
1126 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE));
1127
1128 cpp->sense_len = SCSI_SENSE_BUFFERSIZE;
1129
1130 if (scsi_bufflen(SCpnt)) {
1131 count = scsi_dma_map(SCpnt);
1132 BUG_ON(count < 0);
1133
1134 scsi_for_each_sg(SCpnt, sg, count, k) {
1135 cpp->sglist[k].address = H2DEV(sg_dma_address(sg));
1136 cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(sg));
1137 data_len += sg->length;
1138 }
1139
1140 cpp->sg = TRUE;
1141 cpp->use_sg = scsi_sg_count(SCpnt);
1142 cpp->data_address =
1143 H2DEV(pci_map_single(HD(j)->pdev, cpp->sglist,
1144 cpp->use_sg * sizeof(struct sg_list),
1145 pci_dir));
1146 cpp->data_len = H2DEV(data_len);
1147
1148 } else {
1149 pci_dir = PCI_DMA_BIDIRECTIONAL;
1150 cpp->data_len = H2DEV(scsi_bufflen(SCpnt));
1151 }
1152}
1153
1154static void unmap_dma(unsigned int i, unsigned int j) {
1155 unsigned int pci_dir;
1156 struct mscp *cpp;
1157 struct scsi_cmnd *SCpnt;
1158
1159 cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1160 pci_dir = SCpnt->sc_data_direction;
1161
1162 if (DEV2H(cpp->sense_addr))
1163 pci_unmap_single(HD(j)->pdev, DEV2H(cpp->sense_addr),
1164 DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1165
1166 scsi_dma_unmap(SCpnt);
1167
1168 if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
1169
1170 if (DEV2H(cpp->data_address))
1171 pci_unmap_single(HD(j)->pdev, DEV2H(cpp->data_address),
1172 DEV2H(cpp->data_len), pci_dir);
1173}
1174
1175static void sync_dma(unsigned int i, unsigned int j) {
1176 unsigned int pci_dir;
1177 struct mscp *cpp;
1178 struct scsi_cmnd *SCpnt;
1179
1180 cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1181 pci_dir = SCpnt->sc_data_direction;
1182
1183 if (DEV2H(cpp->sense_addr))
1184 pci_dma_sync_single_for_cpu(HD(j)->pdev, DEV2H(cpp->sense_addr),
1185 DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1186
1187 if (scsi_sg_count(SCpnt))
1188 pci_dma_sync_sg_for_cpu(HD(j)->pdev, scsi_sglist(SCpnt),
1189 scsi_sg_count(SCpnt), pci_dir);
1190
1191 if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
1192
1193 if (DEV2H(cpp->data_address))
1194 pci_dma_sync_single_for_cpu(HD(j)->pdev, DEV2H(cpp->data_address),
1195 DEV2H(cpp->data_len), pci_dir);
1196}
1197
1198static void scsi_to_dev_dir(unsigned int i, unsigned int j) {
1199 unsigned int k;
1200
1201 static const unsigned char data_out_cmds[] = {
1202 0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
1203 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
1204 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
1205 };
1206
1207 static const unsigned char data_none_cmds[] = {
1208 0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
1209 0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
1210 0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
1211 };
1212
1213 struct mscp *cpp;
1214 struct scsi_cmnd *SCpnt;
1215
1216 cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1217
1218 if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
1219 cpp->xdir = DTD_IN;
1220 return;
1221 }
1222 else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
1223 cpp->xdir = DTD_OUT;
1224 return;
1225 }
1226 else if (SCpnt->sc_data_direction == DMA_NONE) {
1227 cpp->xdir = DTD_NONE;
1228 return;
1229 }
1230
1231 if (SCpnt->sc_data_direction != DMA_BIDIRECTIONAL)
1232 panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n", BN(j));
1233
1234 cpp->xdir = DTD_IN;
1235
1236 for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
1237 if (SCpnt->cmnd[0] == data_out_cmds[k]) {
1238 cpp->xdir = DTD_OUT;
1239 break;
1240 }
1241
1242 if (cpp->xdir == DTD_IN)
1243 for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
1244 if (SCpnt->cmnd[0] == data_none_cmds[k]) {
1245 cpp->xdir = DTD_NONE;
1246 break;
1247 }
1248
1249}
1250
1251static int u14_34f_queuecommand_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *)) {
1252 unsigned int i, j, k;
1253 struct mscp *cpp;
1254
1255
1256 j = ((struct hostdata *) SCpnt->device->host->hostdata)->board_number;
1257
1258 if (SCpnt->host_scribble)
1259 panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
1260 BN(j), SCpnt->serial_number, SCpnt);
1261
1262
1263
1264 i = HD(j)->last_cp_used + 1;
1265
1266 for (k = 0; k < sh[j]->can_queue; k++, i++) {
1267
1268 if (i >= sh[j]->can_queue) i = 0;
1269
1270 if (HD(j)->cp_stat[i] == FREE) {
1271 HD(j)->last_cp_used = i;
1272 break;
1273 }
1274 }
1275
1276 if (k == sh[j]->can_queue) {
1277 printk("%s: qcomm, no free mailbox.\n", BN(j));
1278 return 1;
1279 }
1280
1281
1282 cpp = &HD(j)->cp[i];
1283
1284 memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
1285 SCpnt->scsi_done = done;
1286 cpp->cpp_index = i;
1287 SCpnt->host_scribble = (unsigned char *) &cpp->cpp_index;
1288
1289 if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
1290 BN(j), i, SCpnt->device->channel, SCpnt->device->id,
1291 SCpnt->device->lun, SCpnt->serial_number);
1292
1293 cpp->opcode = OP_SCSI;
1294 cpp->channel = SCpnt->device->channel;
1295 cpp->target = SCpnt->device->id;
1296 cpp->lun = SCpnt->device->lun;
1297 cpp->SCpnt = SCpnt;
1298 cpp->cdb_len = SCpnt->cmd_len;
1299 memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
1300
1301
1302 scsi_to_dev_dir(i, j);
1303
1304
1305 map_dma(i, j);
1306
1307 if (linked_comm && SCpnt->device->queue_depth > 2
1308 && TLDEV(SCpnt->device->type)) {
1309 HD(j)->cp_stat[i] = READY;
1310 flush_dev(SCpnt->device, blk_rq_pos(SCpnt->request), j, FALSE);
1311 return 0;
1312 }
1313
1314 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1315 unmap_dma(i, j);
1316 SCpnt->host_scribble = NULL;
1317 scmd_printk(KERN_INFO, SCpnt,
1318 "qcomm, pid %ld, adapter busy.\n", SCpnt->serial_number);
1319 return 1;
1320 }
1321
1322
1323 outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
1324
1325
1326 outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
1327
1328 HD(j)->cp_stat[i] = IN_USE;
1329 return 0;
1330}
1331
1332static DEF_SCSI_QCMD(u14_34f_queuecommand)
1333
1334static int u14_34f_eh_abort(struct scsi_cmnd *SCarg) {
1335 unsigned int i, j;
1336
1337 j = ((struct hostdata *) SCarg->device->host->hostdata)->board_number;
1338
1339 if (SCarg->host_scribble == NULL) {
1340 scmd_printk(KERN_INFO, SCarg, "abort, pid %ld inactive.\n",
1341 SCarg->serial_number);
1342 return SUCCESS;
1343 }
1344
1345 i = *(unsigned int *)SCarg->host_scribble;
1346 scmd_printk(KERN_INFO, SCarg, "abort, mbox %d, pid %ld.\n",
1347 i, SCarg->serial_number);
1348
1349 if (i >= sh[j]->can_queue)
1350 panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1351
1352 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1353 printk("%s: abort, timeout error.\n", BN(j));
1354 return FAILED;
1355 }
1356
1357 if (HD(j)->cp_stat[i] == FREE) {
1358 printk("%s: abort, mbox %d is free.\n", BN(j), i);
1359 return SUCCESS;
1360 }
1361
1362 if (HD(j)->cp_stat[i] == IN_USE) {
1363 printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1364
1365 if (SCarg != HD(j)->cp[i].SCpnt)
1366 panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1367 BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1368
1369 if (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED)
1370 printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1371
1372 return FAILED;
1373 }
1374
1375 if (HD(j)->cp_stat[i] == IN_RESET) {
1376 printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1377 return FAILED;
1378 }
1379
1380 if (HD(j)->cp_stat[i] == LOCKED) {
1381 printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1382 return SUCCESS;
1383 }
1384
1385 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1386 unmap_dma(i, j);
1387 SCarg->result = DID_ABORT << 16;
1388 SCarg->host_scribble = NULL;
1389 HD(j)->cp_stat[i] = FREE;
1390 printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1391 BN(j), i, SCarg->serial_number);
1392 SCarg->scsi_done(SCarg);
1393 return SUCCESS;
1394 }
1395
1396 panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1397}
1398
1399static int u14_34f_eh_host_reset(struct scsi_cmnd *SCarg) {
1400 unsigned int i, j, k, c, limit = 0;
1401 unsigned long time;
1402 int arg_done = FALSE;
1403 struct scsi_cmnd *SCpnt;
1404
1405 j = ((struct hostdata *) SCarg->device->host->hostdata)->board_number;
1406 scmd_printk(KERN_INFO, SCarg, "reset, enter, pid %ld.\n", SCarg->serial_number);
1407
1408 spin_lock_irq(sh[j]->host_lock);
1409
1410 if (SCarg->host_scribble == NULL)
1411 printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->serial_number);
1412
1413 if (HD(j)->in_reset) {
1414 printk("%s: reset, exit, already in reset.\n", BN(j));
1415 spin_unlock_irq(sh[j]->host_lock);
1416 return FAILED;
1417 }
1418
1419 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1420 printk("%s: reset, exit, timeout error.\n", BN(j));
1421 spin_unlock_irq(sh[j]->host_lock);
1422 return FAILED;
1423 }
1424
1425 HD(j)->retries = 0;
1426
1427 for (c = 0; c <= sh[j]->max_channel; c++)
1428 for (k = 0; k < sh[j]->max_id; k++) {
1429 HD(j)->target_redo[k][c] = TRUE;
1430 HD(j)->target_to[k][c] = 0;
1431 }
1432
1433 for (i = 0; i < sh[j]->can_queue; i++) {
1434
1435 if (HD(j)->cp_stat[i] == FREE) continue;
1436
1437 if (HD(j)->cp_stat[i] == LOCKED) {
1438 HD(j)->cp_stat[i] = FREE;
1439 printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1440 continue;
1441 }
1442
1443 if (!(SCpnt = HD(j)->cp[i].SCpnt))
1444 panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1445
1446 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1447 HD(j)->cp_stat[i] = ABORTING;
1448 printk("%s: reset, mbox %d aborting, pid %ld.\n",
1449 BN(j), i, SCpnt->serial_number);
1450 }
1451
1452 else {
1453 HD(j)->cp_stat[i] = IN_RESET;
1454 printk("%s: reset, mbox %d in reset, pid %ld.\n",
1455 BN(j), i, SCpnt->serial_number);
1456 }
1457
1458 if (SCpnt->host_scribble == NULL)
1459 panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1460
1461 if (*(unsigned int *)SCpnt->host_scribble != i)
1462 panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1463
1464 if (SCpnt->scsi_done == NULL)
1465 panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1466
1467 if (SCpnt == SCarg) arg_done = TRUE;
1468 }
1469
1470 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1471 printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1472 spin_unlock_irq(sh[j]->host_lock);
1473 return FAILED;
1474 }
1475
1476 outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
1477 printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1478
1479#if defined(DEBUG_RESET)
1480 do_trace = TRUE;
1481#endif
1482
1483 HD(j)->in_reset = TRUE;
1484
1485 spin_unlock_irq(sh[j]->host_lock);
1486 time = jiffies;
1487 while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1488 spin_lock_irq(sh[j]->host_lock);
1489
1490 printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1491
1492 for (i = 0; i < sh[j]->can_queue; i++) {
1493
1494 if (HD(j)->cp_stat[i] == IN_RESET) {
1495 SCpnt = HD(j)->cp[i].SCpnt;
1496 unmap_dma(i, j);
1497 SCpnt->result = DID_RESET << 16;
1498 SCpnt->host_scribble = NULL;
1499
1500
1501 HD(j)->cp_stat[i] = LOCKED;
1502
1503 printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1504 BN(j), i, SCpnt->serial_number);
1505 }
1506
1507 else if (HD(j)->cp_stat[i] == ABORTING) {
1508 SCpnt = HD(j)->cp[i].SCpnt;
1509 unmap_dma(i, j);
1510 SCpnt->result = DID_RESET << 16;
1511 SCpnt->host_scribble = NULL;
1512
1513
1514 HD(j)->cp_stat[i] = FREE;
1515
1516 printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1517 BN(j), i, SCpnt->serial_number);
1518 }
1519
1520 else
1521
1522
1523 continue;
1524
1525 SCpnt->scsi_done(SCpnt);
1526 }
1527
1528 HD(j)->in_reset = FALSE;
1529 do_trace = FALSE;
1530
1531 if (arg_done) printk("%s: reset, exit, pid %ld done.\n", BN(j), SCarg->serial_number);
1532 else printk("%s: reset, exit.\n", BN(j));
1533
1534 spin_unlock_irq(sh[j]->host_lock);
1535 return SUCCESS;
1536}
1537
1538static int u14_34f_bios_param(struct scsi_device *disk,
1539 struct block_device *bdev, sector_t capacity, int *dkinfo) {
1540 unsigned int j = 0;
1541 unsigned int size = capacity;
1542
1543 dkinfo[0] = HD(j)->heads;
1544 dkinfo[1] = HD(j)->sectors;
1545 dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
1546
1547 if (ext_tran && (scsicam_bios_param(bdev, capacity, dkinfo) < 0)) {
1548 dkinfo[0] = 255;
1549 dkinfo[1] = 63;
1550 dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
1551 }
1552
1553#if defined (DEBUG_GEOMETRY)
1554 printk ("%s: bios_param, head=%d, sec=%d, cyl=%d.\n", driver_name,
1555 dkinfo[0], dkinfo[1], dkinfo[2]);
1556#endif
1557
1558 return FALSE;
1559}
1560
1561static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
1562 unsigned int rev) {
1563 unsigned int i, j, k, y;
1564 unsigned long x;
1565
1566 for (i = 0; i < n - 1; i++) {
1567 k = i;
1568
1569 for (j = k + 1; j < n; j++)
1570 if (rev) {
1571 if (sk[j] > sk[k]) k = j;
1572 }
1573 else {
1574 if (sk[j] < sk[k]) k = j;
1575 }
1576
1577 if (k != i) {
1578 x = sk[k]; sk[k] = sk[i]; sk[i] = x;
1579 y = da[k]; da[k] = da[i]; da[i] = y;
1580 }
1581 }
1582
1583 return;
1584 }
1585
1586static int reorder(unsigned int j, unsigned long cursec,
1587 unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
1588 struct scsi_cmnd *SCpnt;
1589 struct mscp *cpp;
1590 unsigned int k, n;
1591 unsigned int rev = FALSE, s = TRUE, r = TRUE;
1592 unsigned int input_only = TRUE, overlap = FALSE;
1593 unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
1594 unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
1595 unsigned long ioseek = 0;
1596
1597 static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
1598 static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
1599 static unsigned int readysorted = 0, revcount = 0;
1600 static unsigned long seeksorted = 0, seeknosort = 0;
1601
1602 if (link_statistics && !(++flushcount % link_statistics))
1603 printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
1604 " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
1605 ovlcount, readycount, readysorted, sortcount, revcount,
1606 seeknosort / (readycount + 1),
1607 seeksorted / (readycount + 1));
1608
1609 if (n_ready <= 1) return FALSE;
1610
1611 for (n = 0; n < n_ready; n++) {
1612 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1613
1614 if (!(cpp->xdir == DTD_IN)) input_only = FALSE;
1615
1616 if (blk_rq_pos(SCpnt->request) < minsec)
1617 minsec = blk_rq_pos(SCpnt->request);
1618 if (blk_rq_pos(SCpnt->request) > maxsec)
1619 maxsec = blk_rq_pos(SCpnt->request);
1620
1621 sl[n] = blk_rq_pos(SCpnt->request);
1622 ioseek += blk_rq_sectors(SCpnt->request);
1623
1624 if (!n) continue;
1625
1626 if (sl[n] < sl[n - 1]) s = FALSE;
1627 if (sl[n] > sl[n - 1]) r = FALSE;
1628
1629 if (link_statistics) {
1630 if (sl[n] > sl[n - 1])
1631 seek += sl[n] - sl[n - 1];
1632 else
1633 seek += sl[n - 1] - sl[n];
1634 }
1635
1636 }
1637
1638 if (link_statistics) {
1639 if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
1640 }
1641
1642 if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
1643
1644 if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
1645
1646 if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
1647
1648 if (!input_only) for (n = 0; n < n_ready; n++) {
1649 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1650 ll[n] = blk_rq_sectors(SCpnt->request); pl[n] = SCpnt->serial_number;
1651
1652 if (!n) continue;
1653
1654 if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
1655 || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
1656 }
1657
1658 if (overlap) sort(pl, il, n_ready, FALSE);
1659
1660 if (link_statistics) {
1661 if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
1662 batchcount++; readycount += n_ready; seeknosort += seek / 1024;
1663 if (input_only) inputcount++;
1664 if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
1665 else seeksorted += (iseek + maxsec - minsec) / 1024;
1666 if (rev && !r) { revcount++; readysorted += n_ready; }
1667 if (!rev && !s) { sortcount++; readysorted += n_ready; }
1668 }
1669
1670#if defined(DEBUG_LINKED_COMMANDS)
1671 if (link_statistics && (overlap || !(flushcount % link_statistics)))
1672 for (n = 0; n < n_ready; n++) {
1673 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1674 printk("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %u"\
1675 " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
1676 (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
1677 SCpnt->lun, SCpnt->serial_number, k, flushcount, n_ready,
1678 blk_rq_pos(SCpnt->request), blk_rq_sectors(SCpnt->request),
1679 cursec, YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
1680 YESNO(overlap), cpp->xdir);
1681 }
1682#endif
1683 return overlap;
1684}
1685
1686static void flush_dev(struct scsi_device *dev, unsigned long cursec, unsigned int j,
1687 unsigned int ihdlr) {
1688 struct scsi_cmnd *SCpnt;
1689 struct mscp *cpp;
1690 unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
1691
1692 for (k = 0; k < sh[j]->can_queue; k++) {
1693
1694 if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
1695
1696 cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1697
1698 if (SCpnt->device != dev) continue;
1699
1700 if (HD(j)->cp_stat[k] == IN_USE) return;
1701
1702 il[n_ready++] = k;
1703 }
1704
1705 if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
1706
1707 for (n = 0; n < n_ready; n++) {
1708 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1709
1710 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1711 scmd_printk(KERN_INFO, SCpnt,
1712 "%s, pid %ld, mbox %d, adapter"
1713 " busy, will abort.\n", (ihdlr ? "ihdlr" : "qcomm"),
1714 SCpnt->serial_number, k);
1715 HD(j)->cp_stat[k] = ABORTING;
1716 continue;
1717 }
1718
1719 outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
1720 outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
1721 HD(j)->cp_stat[k] = IN_USE;
1722 }
1723
1724}
1725
1726static irqreturn_t ihdlr(unsigned int j)
1727{
1728 struct scsi_cmnd *SCpnt;
1729 unsigned int i, k, c, status, tstatus, reg, ret;
1730 struct mscp *spp, *cpp;
1731 int irq = sh[j]->irq;
1732
1733
1734 if (!((reg = inb(sh[j]->io_port + REG_SYS_INTR)) & IRQ_ASSERTED)) goto none;
1735
1736 HD(j)->iocount++;
1737
1738 if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
1739 HD(j)->iocount);
1740
1741
1742 if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
1743 outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
1744 printk("%s: ihdlr, busy timeout error, irq %d, reg 0x%x, count %d.\n",
1745 BN(j), irq, reg, HD(j)->iocount);
1746 goto none;
1747 }
1748
1749 ret = inl(sh[j]->io_port + REG_ICM);
1750
1751
1752 outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
1753
1754
1755 for (i = 0; i < sh[j]->can_queue; i++)
1756 if (H2DEV(HD(j)->cp[i].cp_dma_addr) == ret) break;
1757
1758 if (i >= sh[j]->can_queue)
1759 panic("%s: ihdlr, invalid mscp bus address %p, cp0 %p.\n", BN(j),
1760 (void *)ret, (void *)H2DEV(HD(j)->cp[0].cp_dma_addr));
1761
1762 cpp = &(HD(j)->cp[i]);
1763 spp = cpp;
1764
1765#if defined(DEBUG_GENERATE_ABORTS)
1766 if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) goto handled;
1767#endif
1768
1769 if (HD(j)->cp_stat[i] == IGNORE) {
1770 HD(j)->cp_stat[i] = FREE;
1771 goto handled;
1772 }
1773 else if (HD(j)->cp_stat[i] == LOCKED) {
1774 HD(j)->cp_stat[i] = FREE;
1775 printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
1776 HD(j)->iocount);
1777 goto handled;
1778 }
1779 else if (HD(j)->cp_stat[i] == FREE) {
1780 printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
1781 HD(j)->iocount);
1782 goto handled;
1783 }
1784 else if (HD(j)->cp_stat[i] == IN_RESET)
1785 printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
1786 else if (HD(j)->cp_stat[i] != IN_USE)
1787 panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
1788 BN(j), i, HD(j)->cp_stat[i]);
1789
1790 HD(j)->cp_stat[i] = FREE;
1791 SCpnt = cpp->SCpnt;
1792
1793 if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
1794
1795 if (SCpnt->host_scribble == NULL)
1796 panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", BN(j), i,
1797 SCpnt->serial_number, SCpnt);
1798
1799 if (*(unsigned int *)SCpnt->host_scribble != i)
1800 panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
1801 BN(j), i, SCpnt->serial_number, *(unsigned int *)SCpnt->host_scribble);
1802
1803 sync_dma(i, j);
1804
1805 if (linked_comm && SCpnt->device->queue_depth > 2
1806 && TLDEV(SCpnt->device->type))
1807 flush_dev(SCpnt->device, blk_rq_pos(SCpnt->request), j, TRUE);
1808
1809 tstatus = status_byte(spp->target_status);
1810
1811#if defined(DEBUG_GENERATE_ERRORS)
1812 if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
1813 spp->adapter_status = 0x01;
1814#endif
1815
1816 switch (spp->adapter_status) {
1817 case ASOK:
1818
1819
1820 if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
1821 status = DID_ERROR << 16;
1822
1823
1824 else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
1825 && HD(j)->target_redo[scmd_id(SCpnt)][scmd_channel(SCpnt)])
1826 status = DID_BUS_BUSY << 16;
1827
1828
1829 else if (tstatus == CHECK_CONDITION
1830 && SCpnt->device->type == TYPE_DISK
1831 && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
1832 status = DID_BUS_BUSY << 16;
1833
1834 else
1835 status = DID_OK << 16;
1836
1837 if (tstatus == GOOD)
1838 HD(j)->target_redo[scmd_id(SCpnt)][scmd_channel(SCpnt)] = FALSE;
1839
1840 if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
1841 (!(tstatus == CHECK_CONDITION && HD(j)->iocount <= 1000 &&
1842 (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
1843 scmd_printk(KERN_INFO, SCpnt,
1844 "ihdlr, pid %ld, target_status 0x%x, sense key 0x%x.\n",
1845 SCpnt->serial_number, spp->target_status,
1846 SCpnt->sense_buffer[2]);
1847
1848 HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)] = 0;
1849
1850 if (HD(j)->last_retried_pid == SCpnt->serial_number) HD(j)->retries = 0;
1851
1852 break;
1853 case ASST:
1854
1855 if (HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)] > 1)
1856 status = DID_ERROR << 16;
1857 else {
1858 status = DID_TIME_OUT << 16;
1859 HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)]++;
1860 }
1861
1862 break;
1863
1864
1865 case 0x93:
1866 case 0x94:
1867 case 0x96:
1868 case 0xa3:
1869
1870 for (c = 0; c <= sh[j]->max_channel; c++)
1871 for (k = 0; k < sh[j]->max_id; k++)
1872 HD(j)->target_redo[k][c] = TRUE;
1873
1874
1875 case 0x92:
1876
1877 if (SCpnt->device->type != TYPE_TAPE
1878 && HD(j)->retries < MAX_INTERNAL_RETRIES) {
1879
1880#if defined(DID_SOFT_ERROR)
1881 status = DID_SOFT_ERROR << 16;
1882#else
1883 status = DID_BUS_BUSY << 16;
1884#endif
1885
1886 HD(j)->retries++;
1887 HD(j)->last_retried_pid = SCpnt->serial_number;
1888 }
1889 else
1890 status = DID_ERROR << 16;
1891
1892 break;
1893 case 0x01:
1894 case 0x02:
1895 case 0x03:
1896 case 0x84:
1897 case 0x9b:
1898 case 0x9f:
1899 case 0xff:
1900 default:
1901 status = DID_ERROR << 16;
1902 break;
1903 }
1904
1905 SCpnt->result = status | spp->target_status;
1906
1907#if defined(DEBUG_INTERRUPT)
1908 if (SCpnt->result || do_trace)
1909#else
1910 if ((spp->adapter_status != ASOK && HD(j)->iocount > 1000) ||
1911 (spp->adapter_status != ASOK &&
1912 spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
1913 do_trace || msg_byte(spp->target_status))
1914#endif
1915 scmd_printk(KERN_INFO, SCpnt, "ihdlr, mbox %2d, err 0x%x:%x,"\
1916 " pid %ld, reg 0x%x, count %d.\n",
1917 i, spp->adapter_status, spp->target_status, SCpnt->serial_number,
1918 reg, HD(j)->iocount);
1919
1920 unmap_dma(i, j);
1921
1922
1923 SCpnt->host_scribble = NULL;
1924
1925 SCpnt->scsi_done(SCpnt);
1926
1927 if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
1928 HD(j)->iocount);
1929
1930handled:
1931 return IRQ_HANDLED;
1932none:
1933 return IRQ_NONE;
1934}
1935
1936static irqreturn_t do_interrupt_handler(int irq, void *shap) {
1937 unsigned int j;
1938 unsigned long spin_flags;
1939 irqreturn_t ret;
1940
1941
1942 if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return IRQ_NONE;
1943
1944 spin_lock_irqsave(sh[j]->host_lock, spin_flags);
1945 ret = ihdlr(j);
1946 spin_unlock_irqrestore(sh[j]->host_lock, spin_flags);
1947 return ret;
1948}
1949
1950static int u14_34f_release(struct Scsi_Host *shpnt) {
1951 unsigned int i, j;
1952
1953 for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
1954
1955 if (sh[j] == NULL)
1956 panic("%s: release, invalid Scsi_Host pointer.\n", driver_name);
1957
1958 for (i = 0; i < sh[j]->can_queue; i++)
1959 kfree((&HD(j)->cp[i])->sglist);
1960
1961 for (i = 0; i < sh[j]->can_queue; i++)
1962 pci_unmap_single(HD(j)->pdev, HD(j)->cp[i].cp_dma_addr,
1963 sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
1964
1965 free_irq(sh[j]->irq, &sha[j]);
1966
1967 if (sh[j]->dma_channel != NO_DMA)
1968 free_dma(sh[j]->dma_channel);
1969
1970 release_region(sh[j]->io_port, sh[j]->n_io_port);
1971 scsi_unregister(sh[j]);
1972 return FALSE;
1973}
1974
1975#include "scsi_module.c"
1976
1977#ifndef MODULE
1978__setup("u14-34f=", option_setup);
1979#endif
1980