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