]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/pcmcia/nsp_cs.c
Merge tag 'drm-misc-fixes-2020-02-07' of git://anongit.freedesktop.org/drm/drm-misc...
[linux.git] / drivers / scsi / pcmcia / nsp_cs.c
1 /*======================================================================
2
3     NinjaSCSI-3 / NinjaSCSI-32Bi PCMCIA SCSI host adapter card driver
4       By: YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
5
6     Ver.2.8   Support 32bit MMIO mode
7               Support Synchronous Data Transfer Request (SDTR) mode
8     Ver.2.0   Support 32bit PIO mode
9     Ver.1.1.2 Fix for scatter list buffer exceeds
10     Ver.1.1   Support scatter list
11     Ver.0.1   Initial version
12
13     This software may be used and distributed according to the terms of
14     the GNU General Public License.
15
16 ======================================================================*/
17
18 /***********************************************************************
19     This driver is for these PCcards.
20
21         I-O DATA PCSC-F  (Workbit NinjaSCSI-3)
22                         "WBT", "NinjaSCSI-3", "R1.0"
23         I-O DATA CBSC-II (Workbit NinjaSCSI-32Bi in 16bit mode)
24                         "IO DATA", "CBSC16       ", "1"
25
26 ***********************************************************************/
27
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/timer.h>
34 #include <linux/ioport.h>
35 #include <linux/delay.h>
36 #include <linux/interrupt.h>
37 #include <linux/major.h>
38 #include <linux/blkdev.h>
39 #include <linux/stat.h>
40
41 #include <asm/io.h>
42 #include <asm/irq.h>
43
44 #include <../drivers/scsi/scsi.h>
45 #include <scsi/scsi_host.h>
46
47 #include <scsi/scsi.h>
48 #include <scsi/scsi_ioctl.h>
49
50 #include <pcmcia/cistpl.h>
51 #include <pcmcia/cisreg.h>
52 #include <pcmcia/ds.h>
53
54 #include "nsp_cs.h"
55
56 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>");
57 MODULE_DESCRIPTION("WorkBit NinjaSCSI-3 / NinjaSCSI-32Bi(16bit) PCMCIA SCSI host adapter module");
58 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
59 MODULE_LICENSE("GPL");
60
61 #include "nsp_io.h"
62
63 /*====================================================================*/
64 /* Parameters that can be set with 'insmod' */
65
66 static int       nsp_burst_mode = BURST_MEM32;
67 module_param(nsp_burst_mode, int, 0);
68 MODULE_PARM_DESC(nsp_burst_mode, "Burst transfer mode (0=io8, 1=io32, 2=mem32(default))");
69
70 /* Release IO ports after configuration? */
71 static bool       free_ports = 0;
72 module_param(free_ports, bool, 0);
73 MODULE_PARM_DESC(free_ports, "Release IO ports after configuration? (default: 0 (=no))");
74
75 static struct scsi_host_template nsp_driver_template = {
76         .proc_name               = "nsp_cs",
77         .show_info               = nsp_show_info,
78         .name                    = "WorkBit NinjaSCSI-3/32Bi(16bit)",
79         .info                    = nsp_info,
80         .queuecommand            = nsp_queuecommand,
81 /*      .eh_abort_handler        = nsp_eh_abort,*/
82         .eh_bus_reset_handler    = nsp_eh_bus_reset,
83         .eh_host_reset_handler   = nsp_eh_host_reset,
84         .can_queue               = 1,
85         .this_id                 = NSP_INITIATOR_ID,
86         .sg_tablesize            = SG_ALL,
87         .dma_boundary            = PAGE_SIZE - 1,
88 };
89
90 static nsp_hw_data nsp_data_base; /* attach <-> detect glue */
91
92
93
94 /*
95  * debug, error print
96  */
97 #ifndef NSP_DEBUG
98 # define NSP_DEBUG_MASK         0x000000
99 # define nsp_msg(type, args...) nsp_cs_message("", 0, (type), args)
100 # define nsp_dbg(mask, args...) /* */
101 #else
102 # define NSP_DEBUG_MASK         0xffffff
103 # define nsp_msg(type, args...) \
104         nsp_cs_message (__func__, __LINE__, (type), args)
105 # define nsp_dbg(mask, args...) \
106         nsp_cs_dmessage(__func__, __LINE__, (mask), args)
107 #endif
108
109 #define NSP_DEBUG_QUEUECOMMAND          BIT(0)
110 #define NSP_DEBUG_REGISTER              BIT(1)
111 #define NSP_DEBUG_AUTOSCSI              BIT(2)
112 #define NSP_DEBUG_INTR                  BIT(3)
113 #define NSP_DEBUG_SGLIST                BIT(4)
114 #define NSP_DEBUG_BUSFREE               BIT(5)
115 #define NSP_DEBUG_CDB_CONTENTS          BIT(6)
116 #define NSP_DEBUG_RESELECTION           BIT(7)
117 #define NSP_DEBUG_MSGINOCCUR            BIT(8)
118 #define NSP_DEBUG_EEPROM                BIT(9)
119 #define NSP_DEBUG_MSGOUTOCCUR           BIT(10)
120 #define NSP_DEBUG_BUSRESET              BIT(11)
121 #define NSP_DEBUG_RESTART               BIT(12)
122 #define NSP_DEBUG_SYNC                  BIT(13)
123 #define NSP_DEBUG_WAIT                  BIT(14)
124 #define NSP_DEBUG_TARGETFLAG            BIT(15)
125 #define NSP_DEBUG_PROC                  BIT(16)
126 #define NSP_DEBUG_INIT                  BIT(17)
127 #define NSP_DEBUG_DATA_IO               BIT(18)
128 #define NSP_SPECIAL_PRINT_REGISTER      BIT(20)
129
130 #define NSP_DEBUG_BUF_LEN               150
131
132 static inline void nsp_inc_resid(struct scsi_cmnd *SCpnt, int residInc)
133 {
134         scsi_set_resid(SCpnt, scsi_get_resid(SCpnt) + residInc);
135 }
136
137 static void nsp_cs_message(const char *func, int line, char *type, char *fmt, ...)
138 {
139         va_list args;
140         char buf[NSP_DEBUG_BUF_LEN];
141
142         va_start(args, fmt);
143         vsnprintf(buf, sizeof(buf), fmt, args);
144         va_end(args);
145
146 #ifndef NSP_DEBUG
147         printk("%snsp_cs: %s\n", type, buf);
148 #else
149         printk("%snsp_cs: %s (%d): %s\n", type, func, line, buf);
150 #endif
151 }
152
153 #ifdef NSP_DEBUG
154 static void nsp_cs_dmessage(const char *func, int line, int mask, char *fmt, ...)
155 {
156         va_list args;
157         char buf[NSP_DEBUG_BUF_LEN];
158
159         va_start(args, fmt);
160         vsnprintf(buf, sizeof(buf), fmt, args);
161         va_end(args);
162
163         if (mask & NSP_DEBUG_MASK) {
164                 printk("nsp_cs-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
165         }
166 }
167 #endif
168
169 /***********************************************************/
170
171 /*====================================================
172  * Clenaup parameters and call done() functions.
173  * You must be set SCpnt->result before call this function.
174  */
175 static void nsp_scsi_done(struct scsi_cmnd *SCpnt)
176 {
177         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
178
179         data->CurrentSC = NULL;
180
181         SCpnt->scsi_done(SCpnt);
182 }
183
184 static int nsp_queuecommand_lck(struct scsi_cmnd *SCpnt,
185                             void (*done)(struct scsi_cmnd *))
186 {
187 #ifdef NSP_DEBUG
188         /*unsigned int host_id = SCpnt->device->host->this_id;*/
189         /*unsigned int base    = SCpnt->device->host->io_port;*/
190         unsigned char target = scmd_id(SCpnt);
191 #endif
192         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
193
194         nsp_dbg(NSP_DEBUG_QUEUECOMMAND,
195                 "SCpnt=0x%p target=%d lun=%llu sglist=0x%p bufflen=%d sg_count=%d",
196                 SCpnt, target, SCpnt->device->lun, scsi_sglist(SCpnt),
197                 scsi_bufflen(SCpnt), scsi_sg_count(SCpnt));
198         //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "before CurrentSC=0x%p", data->CurrentSC);
199
200         SCpnt->scsi_done        = done;
201
202         if (data->CurrentSC != NULL) {
203                 nsp_msg(KERN_DEBUG, "CurrentSC!=NULL this can't be happen");
204                 SCpnt->result   = DID_BAD_TARGET << 16;
205                 nsp_scsi_done(SCpnt);
206                 return 0;
207         }
208
209 #if 0
210         /* XXX: pcmcia-cs generates SCSI command with "scsi_info" utility.
211                 This makes kernel crash when suspending... */
212         if (data->ScsiInfo->stop != 0) {
213                 nsp_msg(KERN_INFO, "suspending device. reject command.");
214                 SCpnt->result  = DID_BAD_TARGET << 16;
215                 nsp_scsi_done(SCpnt);
216                 return SCSI_MLQUEUE_HOST_BUSY;
217         }
218 #endif
219
220         show_command(SCpnt);
221
222         data->CurrentSC         = SCpnt;
223
224         SCpnt->SCp.Status       = CHECK_CONDITION;
225         SCpnt->SCp.Message      = 0;
226         SCpnt->SCp.have_data_in = IO_UNKNOWN;
227         SCpnt->SCp.sent_command = 0;
228         SCpnt->SCp.phase        = PH_UNDETERMINED;
229         scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
230
231         /* setup scratch area
232            SCp.ptr              : buffer pointer
233            SCp.this_residual    : buffer length
234            SCp.buffer           : next buffer
235            SCp.buffers_residual : left buffers in list
236            SCp.phase            : current state of the command */
237         if (scsi_bufflen(SCpnt)) {
238                 SCpnt->SCp.buffer           = scsi_sglist(SCpnt);
239                 SCpnt->SCp.ptr              = BUFFER_ADDR;
240                 SCpnt->SCp.this_residual    = SCpnt->SCp.buffer->length;
241                 SCpnt->SCp.buffers_residual = scsi_sg_count(SCpnt) - 1;
242         } else {
243                 SCpnt->SCp.ptr              = NULL;
244                 SCpnt->SCp.this_residual    = 0;
245                 SCpnt->SCp.buffer           = NULL;
246                 SCpnt->SCp.buffers_residual = 0;
247         }
248
249         if (nsphw_start_selection(SCpnt) == FALSE) {
250                 nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "selection fail");
251                 SCpnt->result   = DID_BUS_BUSY << 16;
252                 nsp_scsi_done(SCpnt);
253                 return 0;
254         }
255
256
257         //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "out");
258 #ifdef NSP_DEBUG
259         data->CmdId++;
260 #endif
261         return 0;
262 }
263
264 static DEF_SCSI_QCMD(nsp_queuecommand)
265
266 /*
267  * setup PIO FIFO transfer mode and enable/disable to data out
268  */
269 static void nsp_setup_fifo(nsp_hw_data *data, int enabled)
270 {
271         unsigned int  base = data->BaseAddress;
272         unsigned char transfer_mode_reg;
273
274         //nsp_dbg(NSP_DEBUG_DATA_IO, "enabled=%d", enabled);
275
276         if (enabled != FALSE) {
277                 transfer_mode_reg = TRANSFER_GO | BRAIND;
278         } else {
279                 transfer_mode_reg = 0;
280         }
281
282         transfer_mode_reg |= data->TransferMode;
283
284         nsp_index_write(base, TRANSFERMODE, transfer_mode_reg);
285 }
286
287 static void nsphw_init_sync(nsp_hw_data *data)
288 {
289         sync_data tmp_sync = { .SyncNegotiation = SYNC_NOT_YET,
290                                .SyncPeriod      = 0,
291                                .SyncOffset      = 0
292         };
293         int i;
294
295         /* setup sync data */
296         for ( i = 0; i < ARRAY_SIZE(data->Sync); i++ ) {
297                 data->Sync[i] = tmp_sync;
298         }
299 }
300
301 /*
302  * Initialize Ninja hardware
303  */
304 static int nsphw_init(nsp_hw_data *data)
305 {
306         unsigned int base     = data->BaseAddress;
307
308         nsp_dbg(NSP_DEBUG_INIT, "in base=0x%x", base);
309
310         data->ScsiClockDiv = CLOCK_40M | FAST_20;
311         data->CurrentSC    = NULL;
312         data->FifoCount    = 0;
313         data->TransferMode = MODE_IO8;
314
315         nsphw_init_sync(data);
316
317         /* block all interrupts */
318         nsp_write(base,       IRQCONTROL,   IRQCONTROL_ALLMASK);
319
320         /* setup SCSI interface */
321         nsp_write(base,       IFSELECT,     IF_IFSEL);
322
323         nsp_index_write(base, SCSIIRQMODE,  0);
324
325         nsp_index_write(base, TRANSFERMODE, MODE_IO8);
326         nsp_index_write(base, CLOCKDIV,     data->ScsiClockDiv);
327
328         nsp_index_write(base, PARITYCTRL,   0);
329         nsp_index_write(base, POINTERCLR,   POINTER_CLEAR     |
330                                             ACK_COUNTER_CLEAR |
331                                             REQ_COUNTER_CLEAR |
332                                             HOST_COUNTER_CLEAR);
333
334         /* setup fifo asic */
335         nsp_write(base,       IFSELECT,     IF_REGSEL);
336         nsp_index_write(base, TERMPWRCTRL,  0);
337         if ((nsp_index_read(base, OTHERCONTROL) & TPWR_SENSE) == 0) {
338                 nsp_msg(KERN_INFO, "terminator power on");
339                 nsp_index_write(base, TERMPWRCTRL, POWER_ON);
340         }
341
342         nsp_index_write(base, TIMERCOUNT,   0);
343         nsp_index_write(base, TIMERCOUNT,   0); /* requires 2 times!! */
344
345         nsp_index_write(base, SYNCREG,      0);
346         nsp_index_write(base, ACKWIDTH,     0);
347
348         /* enable interrupts and ack them */
349         nsp_index_write(base, SCSIIRQMODE,  SCSI_PHASE_CHANGE_EI |
350                                             RESELECT_EI          |
351                                             SCSI_RESET_IRQ_EI    );
352         nsp_write(base,       IRQCONTROL,   IRQCONTROL_ALLCLEAR);
353
354         nsp_setup_fifo(data, FALSE);
355
356         return TRUE;
357 }
358
359 /*
360  * Start selection phase
361  */
362 static int nsphw_start_selection(struct scsi_cmnd *SCpnt)
363 {
364         unsigned int  host_id    = SCpnt->device->host->this_id;
365         unsigned int  base       = SCpnt->device->host->io_port;
366         unsigned char target     = scmd_id(SCpnt);
367         nsp_hw_data  *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
368         int           time_out;
369         unsigned char phase, arbit;
370
371         //nsp_dbg(NSP_DEBUG_RESELECTION, "in");
372
373         phase = nsp_index_read(base, SCSIBUSMON);
374         if(phase != BUSMON_BUS_FREE) {
375                 //nsp_dbg(NSP_DEBUG_RESELECTION, "bus busy");
376                 return FALSE;
377         }
378
379         /* start arbitration */
380         //nsp_dbg(NSP_DEBUG_RESELECTION, "start arbit");
381         SCpnt->SCp.phase = PH_ARBSTART;
382         nsp_index_write(base, SETARBIT, ARBIT_GO);
383
384         time_out = 1000;
385         do {
386                 /* XXX: what a stupid chip! */
387                 arbit = nsp_index_read(base, ARBITSTATUS);
388                 //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit=%d, wait_count=%d", arbit, wait_count);
389                 udelay(1); /* hold 1.2us */
390         } while((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
391                 (time_out-- != 0));
392
393         if (!(arbit & ARBIT_WIN)) {
394                 //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit fail");
395                 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
396                 return FALSE;
397         }
398
399         /* assert select line */
400         //nsp_dbg(NSP_DEBUG_RESELECTION, "assert SEL line");
401         SCpnt->SCp.phase = PH_SELSTART;
402         udelay(3); /* wait 2.4us */
403         nsp_index_write(base, SCSIDATALATCH, BIT(host_id) | BIT(target));
404         nsp_index_write(base, SCSIBUSCTRL,   SCSI_SEL | SCSI_BSY                    | SCSI_ATN);
405         udelay(2); /* wait >1.2us */
406         nsp_index_write(base, SCSIBUSCTRL,   SCSI_SEL | SCSI_BSY | SCSI_DATAOUT_ENB | SCSI_ATN);
407         nsp_index_write(base, SETARBIT,      ARBIT_FLAG_CLEAR);
408         /*udelay(1);*/ /* wait >90ns */
409         nsp_index_write(base, SCSIBUSCTRL,   SCSI_SEL            | SCSI_DATAOUT_ENB | SCSI_ATN);
410
411         /* check selection timeout */
412         nsp_start_timer(SCpnt, 1000/51);
413         data->SelectionTimeOut = 1;
414
415         return TRUE;
416 }
417
418 struct nsp_sync_table {
419         unsigned int min_period;
420         unsigned int max_period;
421         unsigned int chip_period;
422         unsigned int ack_width;
423 };
424
425 static struct nsp_sync_table nsp_sync_table_40M[] = {
426         {0x0c, 0x0c, 0x1, 0},   /* 20MB   50ns*/
427         {0x19, 0x19, 0x3, 1},   /* 10MB  100ns*/ 
428         {0x1a, 0x25, 0x5, 2},   /* 7.5MB 150ns*/ 
429         {0x26, 0x32, 0x7, 3},   /* 5MB   200ns*/
430         {   0,    0,   0, 0},
431 };
432
433 static struct nsp_sync_table nsp_sync_table_20M[] = {
434         {0x19, 0x19, 0x1, 0},   /* 10MB  100ns*/ 
435         {0x1a, 0x25, 0x2, 0},   /* 7.5MB 150ns*/ 
436         {0x26, 0x32, 0x3, 1},   /* 5MB   200ns*/
437         {   0,    0,   0, 0},
438 };
439
440 /*
441  * setup synchronous data transfer mode
442  */
443 static int nsp_analyze_sdtr(struct scsi_cmnd *SCpnt)
444 {
445         unsigned char          target = scmd_id(SCpnt);
446 //      unsigned char          lun    = SCpnt->device->lun;
447         nsp_hw_data           *data   = (nsp_hw_data *)SCpnt->device->host->hostdata;
448         sync_data             *sync   = &(data->Sync[target]);
449         struct nsp_sync_table *sync_table;
450         unsigned int           period, offset;
451         int                    i;
452
453
454         nsp_dbg(NSP_DEBUG_SYNC, "in");
455
456         period = sync->SyncPeriod;
457         offset = sync->SyncOffset;
458
459         nsp_dbg(NSP_DEBUG_SYNC, "period=0x%x, offset=0x%x", period, offset);
460
461         if ((data->ScsiClockDiv & (BIT(0)|BIT(1))) == CLOCK_20M) {
462                 sync_table = nsp_sync_table_20M;
463         } else {
464                 sync_table = nsp_sync_table_40M;
465         }
466
467         for ( i = 0; sync_table->max_period != 0; i++, sync_table++) {
468                 if ( period >= sync_table->min_period &&
469                      period <= sync_table->max_period    ) {
470                         break;
471                 }
472         }
473
474         if (period != 0 && sync_table->max_period == 0) {
475                 /*
476                  * No proper period/offset found
477                  */
478                 nsp_dbg(NSP_DEBUG_SYNC, "no proper period/offset");
479
480                 sync->SyncPeriod      = 0;
481                 sync->SyncOffset      = 0;
482                 sync->SyncRegister    = 0;
483                 sync->AckWidth        = 0;
484
485                 return FALSE;
486         }
487
488         sync->SyncRegister    = (sync_table->chip_period << SYNCREG_PERIOD_SHIFT) |
489                                 (offset & SYNCREG_OFFSET_MASK);
490         sync->AckWidth        = sync_table->ack_width;
491
492         nsp_dbg(NSP_DEBUG_SYNC, "sync_reg=0x%x, ack_width=0x%x", sync->SyncRegister, sync->AckWidth);
493
494         return TRUE;
495 }
496
497
498 /*
499  * start ninja hardware timer
500  */
501 static void nsp_start_timer(struct scsi_cmnd *SCpnt, int time)
502 {
503         unsigned int base = SCpnt->device->host->io_port;
504         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
505
506         //nsp_dbg(NSP_DEBUG_INTR, "in SCpnt=0x%p, time=%d", SCpnt, time);
507         data->TimerCount = time;
508         nsp_index_write(base, TIMERCOUNT, time);
509 }
510
511 /*
512  * wait for bus phase change
513  */
514 static int nsp_negate_signal(struct scsi_cmnd *SCpnt, unsigned char mask,
515                              char *str)
516 {
517         unsigned int  base = SCpnt->device->host->io_port;
518         unsigned char reg;
519         int           time_out;
520
521         //nsp_dbg(NSP_DEBUG_INTR, "in");
522
523         time_out = 100;
524
525         do {
526                 reg = nsp_index_read(base, SCSIBUSMON);
527                 if (reg == 0xff) {
528                         break;
529                 }
530         } while ((--time_out != 0) && (reg & mask) != 0);
531
532         if (time_out == 0) {
533                 nsp_msg(KERN_DEBUG, " %s signal off timeout", str);
534         }
535
536         return 0;
537 }
538
539 /*
540  * expect Ninja Irq
541  */
542 static int nsp_expect_signal(struct scsi_cmnd *SCpnt,
543                              unsigned char current_phase,
544                              unsigned char mask)
545 {
546         unsigned int  base       = SCpnt->device->host->io_port;
547         int           time_out;
548         unsigned char phase, i_src;
549
550         //nsp_dbg(NSP_DEBUG_INTR, "current_phase=0x%x, mask=0x%x", current_phase, mask);
551
552         time_out = 100;
553         do {
554                 phase = nsp_index_read(base, SCSIBUSMON);
555                 if (phase == 0xff) {
556                         //nsp_dbg(NSP_DEBUG_INTR, "ret -1");
557                         return -1;
558                 }
559                 i_src = nsp_read(base, IRQSTATUS);
560                 if (i_src & IRQSTATUS_SCSI) {
561                         //nsp_dbg(NSP_DEBUG_INTR, "ret 0 found scsi signal");
562                         return 0;
563                 }
564                 if ((phase & mask) != 0 && (phase & BUSMON_PHASE_MASK) == current_phase) {
565                         //nsp_dbg(NSP_DEBUG_INTR, "ret 1 phase=0x%x", phase);
566                         return 1;
567                 }
568         } while(time_out-- != 0);
569
570         //nsp_dbg(NSP_DEBUG_INTR, "timeout");
571         return -1;
572 }
573
574 /*
575  * transfer SCSI message
576  */
577 static int nsp_xfer(struct scsi_cmnd *SCpnt, int phase)
578 {
579         unsigned int  base = SCpnt->device->host->io_port;
580         nsp_hw_data  *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
581         char         *buf  = data->MsgBuffer;
582         int           len  = min(MSGBUF_SIZE, data->MsgLen);
583         int           ptr;
584         int           ret;
585
586         //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
587         for (ptr = 0; len > 0; len--, ptr++) {
588
589                 ret = nsp_expect_signal(SCpnt, phase, BUSMON_REQ);
590                 if (ret <= 0) {
591                         nsp_dbg(NSP_DEBUG_DATA_IO, "xfer quit");
592                         return 0;
593                 }
594
595                 /* if last byte, negate ATN */
596                 if (len == 1 && SCpnt->SCp.phase == PH_MSG_OUT) {
597                         nsp_index_write(base, SCSIBUSCTRL, AUTODIRECTION | ACKENB);
598                 }
599
600                 /* read & write message */
601                 if (phase & BUSMON_IO) {
602                         nsp_dbg(NSP_DEBUG_DATA_IO, "read msg");
603                         buf[ptr] = nsp_index_read(base, SCSIDATAWITHACK);
604                 } else {
605                         nsp_dbg(NSP_DEBUG_DATA_IO, "write msg");
606                         nsp_index_write(base, SCSIDATAWITHACK, buf[ptr]);
607                 }
608                 nsp_negate_signal(SCpnt, BUSMON_ACK, "xfer<ack>");
609
610         }
611         return len;
612 }
613
614 /*
615  * get extra SCSI data from fifo
616  */
617 static int nsp_dataphase_bypass(struct scsi_cmnd *SCpnt)
618 {
619         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
620         unsigned int count;
621
622         //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
623
624         if (SCpnt->SCp.have_data_in != IO_IN) {
625                 return 0;
626         }
627
628         count = nsp_fifo_count(SCpnt);
629         if (data->FifoCount == count) {
630                 //nsp_dbg(NSP_DEBUG_DATA_IO, "not use bypass quirk");
631                 return 0;
632         }
633
634         /*
635          * XXX: NSP_QUIRK
636          * data phase skip only occures in case of SCSI_LOW_READ
637          */
638         nsp_dbg(NSP_DEBUG_DATA_IO, "use bypass quirk");
639         SCpnt->SCp.phase = PH_DATA;
640         nsp_pio_read(SCpnt);
641         nsp_setup_fifo(data, FALSE);
642
643         return 0;
644 }
645
646 /*
647  * accept reselection
648  */
649 static int nsp_reselected(struct scsi_cmnd *SCpnt)
650 {
651         unsigned int  base    = SCpnt->device->host->io_port;
652         unsigned int  host_id = SCpnt->device->host->this_id;
653         //nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
654         unsigned char bus_reg;
655         unsigned char id_reg, tmp;
656         int target;
657
658         nsp_dbg(NSP_DEBUG_RESELECTION, "in");
659
660         id_reg = nsp_index_read(base, RESELECTID);
661         tmp    = id_reg & (~BIT(host_id));
662         target = 0;
663         while(tmp != 0) {
664                 if (tmp & BIT(0)) {
665                         break;
666                 }
667                 tmp >>= 1;
668                 target++;
669         }
670
671         if (scmd_id(SCpnt) != target) {
672                 nsp_msg(KERN_ERR, "XXX: reselect ID must be %d in this implementation.", target);
673         }
674
675         nsp_negate_signal(SCpnt, BUSMON_SEL, "reselect<SEL>");
676
677         nsp_nexus(SCpnt);
678         bus_reg = nsp_index_read(base, SCSIBUSCTRL) & ~(SCSI_BSY | SCSI_ATN);
679         nsp_index_write(base, SCSIBUSCTRL, bus_reg);
680         nsp_index_write(base, SCSIBUSCTRL, bus_reg | AUTODIRECTION | ACKENB);
681
682         return TRUE;
683 }
684
685 /*
686  * count how many data transferd
687  */
688 static int nsp_fifo_count(struct scsi_cmnd *SCpnt)
689 {
690         unsigned int base = SCpnt->device->host->io_port;
691         unsigned int count;
692         unsigned int l, m, h, dummy;
693
694         nsp_index_write(base, POINTERCLR, POINTER_CLEAR | ACK_COUNTER);
695
696         l     = nsp_index_read(base, TRANSFERCOUNT);
697         m     = nsp_index_read(base, TRANSFERCOUNT);
698         h     = nsp_index_read(base, TRANSFERCOUNT);
699         dummy = nsp_index_read(base, TRANSFERCOUNT); /* required this! */
700
701         count = (h << 16) | (m << 8) | (l << 0);
702
703         //nsp_dbg(NSP_DEBUG_DATA_IO, "count=0x%x", count);
704
705         return count;
706 }
707
708 /* fifo size */
709 #define RFIFO_CRIT 64
710 #define WFIFO_CRIT 64
711
712 /*
713  * read data in DATA IN phase
714  */
715 static void nsp_pio_read(struct scsi_cmnd *SCpnt)
716 {
717         unsigned int  base      = SCpnt->device->host->io_port;
718         unsigned long mmio_base = SCpnt->device->host->base;
719         nsp_hw_data  *data      = (nsp_hw_data *)SCpnt->device->host->hostdata;
720         long          time_out;
721         int           ocount, res;
722         unsigned char stat, fifo_stat;
723
724         ocount = data->FifoCount;
725
726         nsp_dbg(NSP_DEBUG_DATA_IO, "in SCpnt=0x%p resid=%d ocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d",
727                 SCpnt, scsi_get_resid(SCpnt), ocount, SCpnt->SCp.ptr,
728                 SCpnt->SCp.this_residual, SCpnt->SCp.buffer,
729                 SCpnt->SCp.buffers_residual);
730
731         time_out = 1000;
732
733         while ((time_out-- != 0) &&
734                (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0 ) ) {
735
736                 stat = nsp_index_read(base, SCSIBUSMON);
737                 stat &= BUSMON_PHASE_MASK;
738
739
740                 res = nsp_fifo_count(SCpnt) - ocount;
741                 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x ocount=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount, res);
742                 if (res == 0) { /* if some data available ? */
743                         if (stat == BUSPHASE_DATA_IN) { /* phase changed? */
744                                 //nsp_dbg(NSP_DEBUG_DATA_IO, " wait for data this=%d", SCpnt->SCp.this_residual);
745                                 continue;
746                         } else {
747                                 nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x", stat);
748                                 break;
749                         }
750                 }
751
752                 fifo_stat = nsp_read(base, FIFOSTATUS);
753                 if ((fifo_stat & FIFOSTATUS_FULL_EMPTY) == 0 &&
754                     stat                                == BUSPHASE_DATA_IN) {
755                         continue;
756                 }
757
758                 res = min(res, SCpnt->SCp.this_residual);
759
760                 switch (data->TransferMode) {
761                 case MODE_IO32:
762                         res &= ~(BIT(1)|BIT(0)); /* align 4 */
763                         nsp_fifo32_read(base, SCpnt->SCp.ptr, res >> 2);
764                         break;
765                 case MODE_IO8:
766                         nsp_fifo8_read (base, SCpnt->SCp.ptr, res     );
767                         break;
768
769                 case MODE_MEM32:
770                         res &= ~(BIT(1)|BIT(0)); /* align 4 */
771                         nsp_mmio_fifo32_read(mmio_base, SCpnt->SCp.ptr, res >> 2);
772                         break;
773
774                 default:
775                         nsp_dbg(NSP_DEBUG_DATA_IO, "unknown read mode");
776                         return;
777                 }
778
779                 nsp_inc_resid(SCpnt, -res);
780                 SCpnt->SCp.ptr           += res;
781                 SCpnt->SCp.this_residual -= res;
782                 ocount                   += res;
783                 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this_residual=0x%x ocount=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount);
784
785                 /* go to next scatter list if available */
786                 if (SCpnt->SCp.this_residual    == 0 &&
787                     SCpnt->SCp.buffers_residual != 0 ) {
788                         //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next timeout=%d", time_out);
789                         SCpnt->SCp.buffers_residual--;
790                         SCpnt->SCp.buffer = sg_next(SCpnt->SCp.buffer);
791                         SCpnt->SCp.ptr           = BUFFER_ADDR;
792                         SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
793                         time_out = 1000;
794
795                         //nsp_dbg(NSP_DEBUG_DATA_IO, "page: 0x%p, off: 0x%x", SCpnt->SCp.buffer->page, SCpnt->SCp.buffer->offset);
796                 }
797         }
798
799         data->FifoCount = ocount;
800
801         if (time_out < 0) {
802                 nsp_msg(KERN_DEBUG, "pio read timeout resid=%d this_residual=%d buffers_residual=%d",
803                         scsi_get_resid(SCpnt), SCpnt->SCp.this_residual,
804                         SCpnt->SCp.buffers_residual);
805         }
806         nsp_dbg(NSP_DEBUG_DATA_IO, "read ocount=0x%x", ocount);
807         nsp_dbg(NSP_DEBUG_DATA_IO, "r cmd=%d resid=0x%x\n", data->CmdId,
808                                                         scsi_get_resid(SCpnt));
809 }
810
811 /*
812  * write data in DATA OUT phase
813  */
814 static void nsp_pio_write(struct scsi_cmnd *SCpnt)
815 {
816         unsigned int  base      = SCpnt->device->host->io_port;
817         unsigned long mmio_base = SCpnt->device->host->base;
818         nsp_hw_data  *data      = (nsp_hw_data *)SCpnt->device->host->hostdata;
819         int           time_out;
820         int           ocount, res;
821         unsigned char stat;
822
823         ocount   = data->FifoCount;
824
825         nsp_dbg(NSP_DEBUG_DATA_IO, "in fifocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d resid=0x%x",
826                 data->FifoCount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual,
827                 SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual,
828                 scsi_get_resid(SCpnt));
829
830         time_out = 1000;
831
832         while ((time_out-- != 0) &&
833                (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0)) {
834                 stat = nsp_index_read(base, SCSIBUSMON);
835                 stat &= BUSMON_PHASE_MASK;
836
837                 if (stat != BUSPHASE_DATA_OUT) {
838                         res = ocount - nsp_fifo_count(SCpnt);
839
840                         nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x, res=%d\n", stat, res);
841                         /* Put back pointer */
842                         nsp_inc_resid(SCpnt, res);
843                         SCpnt->SCp.ptr           -= res;
844                         SCpnt->SCp.this_residual += res;
845                         ocount                   -= res;
846
847                         break;
848                 }
849
850                 res = ocount - nsp_fifo_count(SCpnt);
851                 if (res > 0) { /* write all data? */
852                         nsp_dbg(NSP_DEBUG_DATA_IO, "wait for all data out. ocount=0x%x res=%d", ocount, res);
853                         continue;
854                 }
855
856                 res = min(SCpnt->SCp.this_residual, WFIFO_CRIT);
857
858                 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, res);
859                 switch (data->TransferMode) {
860                 case MODE_IO32:
861                         res &= ~(BIT(1)|BIT(0)); /* align 4 */
862                         nsp_fifo32_write(base, SCpnt->SCp.ptr, res >> 2);
863                         break;
864                 case MODE_IO8:
865                         nsp_fifo8_write (base, SCpnt->SCp.ptr, res     );
866                         break;
867
868                 case MODE_MEM32:
869                         res &= ~(BIT(1)|BIT(0)); /* align 4 */
870                         nsp_mmio_fifo32_write(mmio_base, SCpnt->SCp.ptr, res >> 2);
871                         break;
872
873                 default:
874                         nsp_dbg(NSP_DEBUG_DATA_IO, "unknown write mode");
875                         break;
876                 }
877
878                 nsp_inc_resid(SCpnt, -res);
879                 SCpnt->SCp.ptr           += res;
880                 SCpnt->SCp.this_residual -= res;
881                 ocount                   += res;
882
883                 /* go to next scatter list if available */
884                 if (SCpnt->SCp.this_residual    == 0 &&
885                     SCpnt->SCp.buffers_residual != 0 ) {
886                         //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next");
887                         SCpnt->SCp.buffers_residual--;
888                         SCpnt->SCp.buffer = sg_next(SCpnt->SCp.buffer);
889                         SCpnt->SCp.ptr           = BUFFER_ADDR;
890                         SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
891                         time_out = 1000;
892                 }
893         }
894
895         data->FifoCount = ocount;
896
897         if (time_out < 0) {
898                 nsp_msg(KERN_DEBUG, "pio write timeout resid=0x%x",
899                                                         scsi_get_resid(SCpnt));
900         }
901         nsp_dbg(NSP_DEBUG_DATA_IO, "write ocount=0x%x", ocount);
902         nsp_dbg(NSP_DEBUG_DATA_IO, "w cmd=%d resid=0x%x\n", data->CmdId,
903                                                         scsi_get_resid(SCpnt));
904 }
905 #undef RFIFO_CRIT
906 #undef WFIFO_CRIT
907
908 /*
909  * setup synchronous/asynchronous data transfer mode
910  */
911 static int nsp_nexus(struct scsi_cmnd *SCpnt)
912 {
913         unsigned int   base   = SCpnt->device->host->io_port;
914         unsigned char  target = scmd_id(SCpnt);
915 //      unsigned char  lun    = SCpnt->device->lun;
916         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
917         sync_data     *sync   = &(data->Sync[target]);
918
919         //nsp_dbg(NSP_DEBUG_DATA_IO, "in SCpnt=0x%p", SCpnt);
920
921         /* setup synch transfer registers */
922         nsp_index_write(base, SYNCREG,  sync->SyncRegister);
923         nsp_index_write(base, ACKWIDTH, sync->AckWidth);
924
925         if (scsi_get_resid(SCpnt) % 4 != 0 ||
926             scsi_get_resid(SCpnt) <= PAGE_SIZE ) {
927                 data->TransferMode = MODE_IO8;
928         } else if (nsp_burst_mode == BURST_MEM32) {
929                 data->TransferMode = MODE_MEM32;
930         } else if (nsp_burst_mode == BURST_IO32) {
931                 data->TransferMode = MODE_IO32;
932         } else {
933                 data->TransferMode = MODE_IO8;
934         }
935
936         /* setup pdma fifo */
937         nsp_setup_fifo(data, TRUE);
938
939         /* clear ack counter */
940         data->FifoCount = 0;
941         nsp_index_write(base, POINTERCLR, POINTER_CLEAR     |
942                                           ACK_COUNTER_CLEAR |
943                                           REQ_COUNTER_CLEAR |
944                                           HOST_COUNTER_CLEAR);
945
946         return 0;
947 }
948
949 #include "nsp_message.c"
950 /*
951  * interrupt handler
952  */
953 static irqreturn_t nspintr(int irq, void *dev_id)
954 {
955         unsigned int   base;
956         unsigned char  irq_status, irq_phase, phase;
957         struct scsi_cmnd *tmpSC;
958         unsigned char  target, lun;
959         unsigned int  *sync_neg;
960         int            i, tmp;
961         nsp_hw_data   *data;
962
963
964         //nsp_dbg(NSP_DEBUG_INTR, "dev_id=0x%p", dev_id);
965         //nsp_dbg(NSP_DEBUG_INTR, "host=0x%p", ((scsi_info_t *)dev_id)->host);
966
967         if (                dev_id        != NULL &&
968             ((scsi_info_t *)dev_id)->host != NULL  ) {
969                 scsi_info_t *info = (scsi_info_t *)dev_id;
970
971                 data = (nsp_hw_data *)info->host->hostdata;
972         } else {
973                 nsp_dbg(NSP_DEBUG_INTR, "host data wrong");
974                 return IRQ_NONE;
975         }
976
977         //nsp_dbg(NSP_DEBUG_INTR, "&nsp_data_base=0x%p, dev_id=0x%p", &nsp_data_base, dev_id);
978
979         base = data->BaseAddress;
980         //nsp_dbg(NSP_DEBUG_INTR, "base=0x%x", base);
981
982         /*
983          * interrupt check
984          */
985         nsp_write(base, IRQCONTROL, IRQCONTROL_IRQDISABLE);
986         irq_status = nsp_read(base, IRQSTATUS);
987         //nsp_dbg(NSP_DEBUG_INTR, "irq_status=0x%x", irq_status);
988         if ((irq_status == 0xff) || ((irq_status & IRQSTATUS_MASK) == 0)) {
989                 nsp_write(base, IRQCONTROL, 0);
990                 //nsp_dbg(NSP_DEBUG_INTR, "no irq/shared irq");
991                 return IRQ_NONE;
992         }
993
994         /* XXX: IMPORTANT
995          * Do not read an irq_phase register if no scsi phase interrupt.
996          * Unless, you should lose a scsi phase interrupt.
997          */
998         phase = nsp_index_read(base, SCSIBUSMON);
999         if((irq_status & IRQSTATUS_SCSI) != 0) {
1000                 irq_phase = nsp_index_read(base, IRQPHASESENCE);
1001         } else {
1002                 irq_phase = 0;
1003         }
1004
1005         //nsp_dbg(NSP_DEBUG_INTR, "irq_phase=0x%x", irq_phase);
1006
1007         /*
1008          * timer interrupt handler (scsi vs timer interrupts)
1009          */
1010         //nsp_dbg(NSP_DEBUG_INTR, "timercount=%d", data->TimerCount);
1011         if (data->TimerCount != 0) {
1012                 //nsp_dbg(NSP_DEBUG_INTR, "stop timer");
1013                 nsp_index_write(base, TIMERCOUNT, 0);
1014                 nsp_index_write(base, TIMERCOUNT, 0);
1015                 data->TimerCount = 0;
1016         }
1017
1018         if ((irq_status & IRQSTATUS_MASK) == IRQSTATUS_TIMER &&
1019             data->SelectionTimeOut == 0) {
1020                 //nsp_dbg(NSP_DEBUG_INTR, "timer start");
1021                 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR);
1022                 return IRQ_HANDLED;
1023         }
1024
1025         nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR | IRQCONTROL_FIFO_CLEAR);
1026
1027         if ((irq_status & IRQSTATUS_SCSI) &&
1028             (irq_phase  & SCSI_RESET_IRQ)) {
1029                 nsp_msg(KERN_ERR, "bus reset (power off?)");
1030
1031                 nsphw_init(data);
1032                 nsp_bus_reset(data);
1033
1034                 if(data->CurrentSC != NULL) {
1035                         tmpSC = data->CurrentSC;
1036                         tmpSC->result  = (DID_RESET                   << 16) |
1037                                          ((tmpSC->SCp.Message & 0xff) <<  8) |
1038                                          ((tmpSC->SCp.Status  & 0xff) <<  0);
1039                         nsp_scsi_done(tmpSC);
1040                 }
1041                 return IRQ_HANDLED;
1042         }
1043
1044         if (data->CurrentSC == NULL) {
1045                 nsp_msg(KERN_ERR, "CurrentSC==NULL irq_status=0x%x phase=0x%x irq_phase=0x%x this can't be happen. reset everything", irq_status, phase, irq_phase);
1046                 nsphw_init(data);
1047                 nsp_bus_reset(data);
1048                 return IRQ_HANDLED;
1049         }
1050
1051         tmpSC    = data->CurrentSC;
1052         target   = tmpSC->device->id;
1053         lun      = tmpSC->device->lun;
1054         sync_neg = &(data->Sync[target].SyncNegotiation);
1055
1056         /*
1057          * parse hardware SCSI irq reasons register
1058          */
1059         if (irq_status & IRQSTATUS_SCSI) {
1060                 if (irq_phase & RESELECT_IRQ) {
1061                         nsp_dbg(NSP_DEBUG_INTR, "reselect");
1062                         nsp_write(base, IRQCONTROL, IRQCONTROL_RESELECT_CLEAR);
1063                         if (nsp_reselected(tmpSC) != FALSE) {
1064                                 return IRQ_HANDLED;
1065                         }
1066                 }
1067
1068                 if ((irq_phase & (PHASE_CHANGE_IRQ | LATCHED_BUS_FREE)) == 0) {
1069                         return IRQ_HANDLED;
1070                 }
1071         }
1072
1073         //show_phase(tmpSC);
1074
1075         switch(tmpSC->SCp.phase) {
1076         case PH_SELSTART:
1077                 // *sync_neg = SYNC_NOT_YET;
1078                 if ((phase & BUSMON_BSY) == 0) {
1079                         //nsp_dbg(NSP_DEBUG_INTR, "selection count=%d", data->SelectionTimeOut);
1080                         if (data->SelectionTimeOut >= NSP_SELTIMEOUT) {
1081                                 nsp_dbg(NSP_DEBUG_INTR, "selection time out");
1082                                 data->SelectionTimeOut = 0;
1083                                 nsp_index_write(base, SCSIBUSCTRL, 0);
1084
1085                                 tmpSC->result   = DID_TIME_OUT << 16;
1086                                 nsp_scsi_done(tmpSC);
1087
1088                                 return IRQ_HANDLED;
1089                         }
1090                         data->SelectionTimeOut += 1;
1091                         nsp_start_timer(tmpSC, 1000/51);
1092                         return IRQ_HANDLED;
1093                 }
1094
1095                 /* attention assert */
1096                 //nsp_dbg(NSP_DEBUG_INTR, "attention assert");
1097                 data->SelectionTimeOut = 0;
1098                 tmpSC->SCp.phase       = PH_SELECTED;
1099                 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN);
1100                 udelay(1);
1101                 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN | AUTODIRECTION | ACKENB);
1102                 return IRQ_HANDLED;
1103
1104                 break;
1105
1106         case PH_RESELECT:
1107                 //nsp_dbg(NSP_DEBUG_INTR, "phase reselect");
1108                 // *sync_neg = SYNC_NOT_YET;
1109                 if ((phase & BUSMON_PHASE_MASK) != BUSPHASE_MESSAGE_IN) {
1110
1111                         tmpSC->result   = DID_ABORT << 16;
1112                         nsp_scsi_done(tmpSC);
1113                         return IRQ_HANDLED;
1114                 }
1115                 /* fall thru */
1116         default:
1117                 if ((irq_status & (IRQSTATUS_SCSI | IRQSTATUS_FIFO)) == 0) {
1118                         return IRQ_HANDLED;
1119                 }
1120                 break;
1121         }
1122
1123         /*
1124          * SCSI sequencer
1125          */
1126         //nsp_dbg(NSP_DEBUG_INTR, "start scsi seq");
1127
1128         /* normal disconnect */
1129         if (((tmpSC->SCp.phase == PH_MSG_IN) || (tmpSC->SCp.phase == PH_MSG_OUT)) &&
1130             (irq_phase & LATCHED_BUS_FREE) != 0 ) {
1131                 nsp_dbg(NSP_DEBUG_INTR, "normal disconnect irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1132
1133                 //*sync_neg       = SYNC_NOT_YET;
1134
1135                 /* all command complete and return status */
1136                 if (tmpSC->SCp.Message == MSG_COMMAND_COMPLETE) {
1137                         tmpSC->result = (DID_OK                      << 16) |
1138                                         ((tmpSC->SCp.Message & 0xff) <<  8) |
1139                                         ((tmpSC->SCp.Status  & 0xff) <<  0);
1140                         nsp_dbg(NSP_DEBUG_INTR, "command complete result=0x%x", tmpSC->result);
1141                         nsp_scsi_done(tmpSC);
1142
1143                         return IRQ_HANDLED;
1144                 }
1145
1146                 return IRQ_HANDLED;
1147         }
1148
1149
1150         /* check unexpected bus free state */
1151         if (phase == 0) {
1152                 nsp_msg(KERN_DEBUG, "unexpected bus free. irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1153
1154                 *sync_neg       = SYNC_NG;
1155                 tmpSC->result   = DID_ERROR << 16;
1156                 nsp_scsi_done(tmpSC);
1157                 return IRQ_HANDLED;
1158         }
1159
1160         switch (phase & BUSMON_PHASE_MASK) {
1161         case BUSPHASE_COMMAND:
1162                 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_COMMAND");
1163                 if ((phase & BUSMON_REQ) == 0) {
1164                         nsp_dbg(NSP_DEBUG_INTR, "REQ == 0");
1165                         return IRQ_HANDLED;
1166                 }
1167
1168                 tmpSC->SCp.phase = PH_COMMAND;
1169
1170                 nsp_nexus(tmpSC);
1171
1172                 /* write scsi command */
1173                 nsp_dbg(NSP_DEBUG_INTR, "cmd_len=%d", tmpSC->cmd_len);
1174                 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER);
1175                 for (i = 0; i < tmpSC->cmd_len; i++) {
1176                         nsp_index_write(base, COMMANDDATA, tmpSC->cmnd[i]);
1177                 }
1178                 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER | AUTO_COMMAND_GO);
1179                 break;
1180
1181         case BUSPHASE_DATA_OUT:
1182                 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_DATA_OUT");
1183
1184                 tmpSC->SCp.phase        = PH_DATA;
1185                 tmpSC->SCp.have_data_in = IO_OUT;
1186
1187                 nsp_pio_write(tmpSC);
1188
1189                 break;
1190
1191         case BUSPHASE_DATA_IN:
1192                 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_DATA_IN");
1193
1194                 tmpSC->SCp.phase        = PH_DATA;
1195                 tmpSC->SCp.have_data_in = IO_IN;
1196
1197                 nsp_pio_read(tmpSC);
1198
1199                 break;
1200
1201         case BUSPHASE_STATUS:
1202                 nsp_dataphase_bypass(tmpSC);
1203                 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_STATUS");
1204
1205                 tmpSC->SCp.phase = PH_STATUS;
1206
1207                 tmpSC->SCp.Status = nsp_index_read(base, SCSIDATAWITHACK);
1208                 nsp_dbg(NSP_DEBUG_INTR, "message=0x%x status=0x%x", tmpSC->SCp.Message, tmpSC->SCp.Status);
1209
1210                 break;
1211
1212         case BUSPHASE_MESSAGE_OUT:
1213                 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_MESSAGE_OUT");
1214                 if ((phase & BUSMON_REQ) == 0) {
1215                         goto timer_out;
1216                 }
1217
1218                 tmpSC->SCp.phase = PH_MSG_OUT;
1219
1220                 //*sync_neg = SYNC_NOT_YET;
1221
1222                 data->MsgLen = i = 0;
1223                 data->MsgBuffer[i] = IDENTIFY(TRUE, lun); i++;
1224
1225                 if (*sync_neg == SYNC_NOT_YET) {
1226                         data->Sync[target].SyncPeriod = 0;
1227                         data->Sync[target].SyncOffset = 0;
1228
1229                         /**/
1230                         data->MsgBuffer[i] = MSG_EXTENDED; i++;
1231                         data->MsgBuffer[i] = 3;            i++;
1232                         data->MsgBuffer[i] = MSG_EXT_SDTR; i++;
1233                         data->MsgBuffer[i] = 0x0c;         i++;
1234                         data->MsgBuffer[i] = 15;           i++;
1235                         /**/
1236                 }
1237                 data->MsgLen = i;
1238
1239                 nsp_analyze_sdtr(tmpSC);
1240                 show_message(data);
1241                 nsp_message_out(tmpSC);
1242                 break;
1243
1244         case BUSPHASE_MESSAGE_IN:
1245                 nsp_dataphase_bypass(tmpSC);
1246                 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_MESSAGE_IN");
1247                 if ((phase & BUSMON_REQ) == 0) {
1248                         goto timer_out;
1249                 }
1250
1251                 tmpSC->SCp.phase = PH_MSG_IN;
1252                 nsp_message_in(tmpSC);
1253
1254                 /**/
1255                 if (*sync_neg == SYNC_NOT_YET) {
1256                         //nsp_dbg(NSP_DEBUG_INTR, "sync target=%d,lun=%d",target,lun);
1257
1258                         if (data->MsgLen       >= 5            &&
1259                             data->MsgBuffer[0] == MSG_EXTENDED &&
1260                             data->MsgBuffer[1] == 3            &&
1261                             data->MsgBuffer[2] == MSG_EXT_SDTR ) {
1262                                 data->Sync[target].SyncPeriod = data->MsgBuffer[3];
1263                                 data->Sync[target].SyncOffset = data->MsgBuffer[4];
1264                                 //nsp_dbg(NSP_DEBUG_INTR, "sync ok, %d %d", data->MsgBuffer[3], data->MsgBuffer[4]);
1265                                 *sync_neg = SYNC_OK;
1266                         } else {
1267                                 data->Sync[target].SyncPeriod = 0;
1268                                 data->Sync[target].SyncOffset = 0;
1269                                 *sync_neg = SYNC_NG;
1270                         }
1271                         nsp_analyze_sdtr(tmpSC);
1272                 }
1273                 /**/
1274
1275                 /* search last messeage byte */
1276                 tmp = -1;
1277                 for (i = 0; i < data->MsgLen; i++) {
1278                         tmp = data->MsgBuffer[i];
1279                         if (data->MsgBuffer[i] == MSG_EXTENDED) {
1280                                 i += (1 + data->MsgBuffer[i+1]);
1281                         }
1282                 }
1283                 tmpSC->SCp.Message = tmp;
1284
1285                 nsp_dbg(NSP_DEBUG_INTR, "message=0x%x len=%d", tmpSC->SCp.Message, data->MsgLen);
1286                 show_message(data);
1287
1288                 break;
1289
1290         case BUSPHASE_SELECT:
1291         default:
1292                 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE other");
1293
1294                 break;
1295         }
1296
1297         //nsp_dbg(NSP_DEBUG_INTR, "out");
1298         return IRQ_HANDLED;     
1299
1300 timer_out:
1301         nsp_start_timer(tmpSC, 1000/102);
1302         return IRQ_HANDLED;
1303 }
1304
1305 #ifdef NSP_DEBUG
1306 #include "nsp_debug.c"
1307 #endif  /* NSP_DEBUG */
1308
1309 /*----------------------------------------------------------------*/
1310 /* look for ninja3 card and init if found                         */
1311 /*----------------------------------------------------------------*/
1312 static struct Scsi_Host *nsp_detect(struct scsi_host_template *sht)
1313 {
1314         struct Scsi_Host *host; /* registered host structure */
1315         nsp_hw_data *data_b = &nsp_data_base, *data;
1316
1317         nsp_dbg(NSP_DEBUG_INIT, "this_id=%d", sht->this_id);
1318         host = scsi_host_alloc(&nsp_driver_template, sizeof(nsp_hw_data));
1319         if (host == NULL) {
1320                 nsp_dbg(NSP_DEBUG_INIT, "host failed");
1321                 return NULL;
1322         }
1323
1324         memcpy(host->hostdata, data_b, sizeof(nsp_hw_data));
1325         data = (nsp_hw_data *)host->hostdata;
1326         data->ScsiInfo->host = host;
1327 #ifdef NSP_DEBUG
1328         data->CmdId = 0;
1329 #endif
1330
1331         nsp_dbg(NSP_DEBUG_INIT, "irq=%d,%d", data_b->IrqNumber, ((nsp_hw_data *)host->hostdata)->IrqNumber);
1332
1333         host->unique_id   = data->BaseAddress;
1334         host->io_port     = data->BaseAddress;
1335         host->n_io_port   = data->NumAddress;
1336         host->irq         = data->IrqNumber;
1337         host->base        = data->MmioAddress;
1338
1339         spin_lock_init(&(data->Lock));
1340
1341         snprintf(data->nspinfo,
1342                  sizeof(data->nspinfo),
1343                  "NinjaSCSI-3/32Bi Driver $Revision: 1.23 $ IO:0x%04lx-0x%04lx MMIO(virt addr):0x%04lx IRQ:%02d",
1344                  host->io_port, host->io_port + host->n_io_port - 1,
1345                  host->base,
1346                  host->irq);
1347         sht->name         = data->nspinfo;
1348
1349         nsp_dbg(NSP_DEBUG_INIT, "end");
1350
1351
1352         return host; /* detect done. */
1353 }
1354
1355 /*----------------------------------------------------------------*/
1356 /* return info string                                             */
1357 /*----------------------------------------------------------------*/
1358 static const char *nsp_info(struct Scsi_Host *shpnt)
1359 {
1360         nsp_hw_data *data = (nsp_hw_data *)shpnt->hostdata;
1361
1362         return data->nspinfo;
1363 }
1364
1365 static int nsp_show_info(struct seq_file *m, struct Scsi_Host *host)
1366 {
1367         int id;
1368         int speed;
1369         unsigned long flags;
1370         nsp_hw_data *data;
1371         int hostno;
1372
1373         hostno = host->host_no;
1374         data = (nsp_hw_data *)host->hostdata;
1375
1376         seq_puts(m, "NinjaSCSI status\n\n"
1377                 "Driver version:        $Revision: 1.23 $\n");
1378         seq_printf(m, "SCSI host No.:         %d\n",          hostno);
1379         seq_printf(m, "IRQ:                   %d\n",          host->irq);
1380         seq_printf(m, "IO:                    0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
1381         seq_printf(m, "MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
1382         seq_printf(m, "sg_tablesize:          %d\n",          host->sg_tablesize);
1383
1384         seq_puts(m, "burst transfer mode:   ");
1385         switch (nsp_burst_mode) {
1386         case BURST_IO8:
1387                 seq_puts(m, "io8");
1388                 break;
1389         case BURST_IO32:
1390                 seq_puts(m, "io32");
1391                 break;
1392         case BURST_MEM32:
1393                 seq_puts(m, "mem32");
1394                 break;
1395         default:
1396                 seq_puts(m, "???");
1397                 break;
1398         }
1399         seq_putc(m, '\n');
1400
1401
1402         spin_lock_irqsave(&(data->Lock), flags);
1403         seq_printf(m, "CurrentSC:             0x%p\n\n",      data->CurrentSC);
1404         spin_unlock_irqrestore(&(data->Lock), flags);
1405
1406         seq_puts(m, "SDTR status\n");
1407         for(id = 0; id < ARRAY_SIZE(data->Sync); id++) {
1408
1409                 seq_printf(m, "id %d: ", id);
1410
1411                 if (id == host->this_id) {
1412                         seq_puts(m, "----- NinjaSCSI-3 host adapter\n");
1413                         continue;
1414                 }
1415
1416                 switch(data->Sync[id].SyncNegotiation) {
1417                 case SYNC_OK:
1418                         seq_puts(m, " sync");
1419                         break;
1420                 case SYNC_NG:
1421                         seq_puts(m, "async");
1422                         break;
1423                 case SYNC_NOT_YET:
1424                         seq_puts(m, " none");
1425                         break;
1426                 default:
1427                         seq_puts(m, "?????");
1428                         break;
1429                 }
1430
1431                 if (data->Sync[id].SyncPeriod != 0) {
1432                         speed = 1000000 / (data->Sync[id].SyncPeriod * 4);
1433
1434                         seq_printf(m, " transfer %d.%dMB/s, offset %d",
1435                                 speed / 1000,
1436                                 speed % 1000,
1437                                 data->Sync[id].SyncOffset
1438                                 );
1439                 }
1440                 seq_putc(m, '\n');
1441         }
1442         return 0;
1443 }
1444
1445 /*---------------------------------------------------------------*/
1446 /* error handler                                                 */
1447 /*---------------------------------------------------------------*/
1448
1449 /*
1450 static int nsp_eh_abort(struct scsi_cmnd *SCpnt)
1451 {
1452         nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
1453
1454         return nsp_eh_bus_reset(SCpnt);
1455 }*/
1456
1457 static int nsp_bus_reset(nsp_hw_data *data)
1458 {
1459         unsigned int base = data->BaseAddress;
1460         int          i;
1461
1462         nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
1463
1464         nsp_index_write(base, SCSIBUSCTRL, SCSI_RST);
1465         mdelay(100); /* 100ms */
1466         nsp_index_write(base, SCSIBUSCTRL, 0);
1467         for(i = 0; i < 5; i++) {
1468                 nsp_index_read(base, IRQPHASESENCE); /* dummy read */
1469         }
1470
1471         nsphw_init_sync(data);
1472
1473         nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
1474
1475         return SUCCESS;
1476 }
1477
1478 static int nsp_eh_bus_reset(struct scsi_cmnd *SCpnt)
1479 {
1480         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
1481
1482         nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
1483
1484         return nsp_bus_reset(data);
1485 }
1486
1487 static int nsp_eh_host_reset(struct scsi_cmnd *SCpnt)
1488 {
1489         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
1490
1491         nsp_dbg(NSP_DEBUG_BUSRESET, "in");
1492
1493         nsphw_init(data);
1494
1495         return SUCCESS;
1496 }
1497
1498
1499 /**********************************************************************
1500   PCMCIA functions
1501 **********************************************************************/
1502
1503 static int nsp_cs_probe(struct pcmcia_device *link)
1504 {
1505         scsi_info_t  *info;
1506         nsp_hw_data  *data = &nsp_data_base;
1507         int ret;
1508
1509         nsp_dbg(NSP_DEBUG_INIT, "in");
1510
1511         /* Create new SCSI device */
1512         info = kzalloc(sizeof(*info), GFP_KERNEL);
1513         if (info == NULL) { return -ENOMEM; }
1514         info->p_dev = link;
1515         link->priv = info;
1516         data->ScsiInfo = info;
1517
1518         nsp_dbg(NSP_DEBUG_INIT, "info=0x%p", info);
1519
1520         ret = nsp_cs_config(link);
1521
1522         nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
1523         return ret;
1524 } /* nsp_cs_attach */
1525
1526
1527 static void nsp_cs_detach(struct pcmcia_device *link)
1528 {
1529         nsp_dbg(NSP_DEBUG_INIT, "in, link=0x%p", link);
1530
1531         ((scsi_info_t *)link->priv)->stop = 1;
1532         nsp_cs_release(link);
1533
1534         kfree(link->priv);
1535         link->priv = NULL;
1536 } /* nsp_cs_detach */
1537
1538
1539 static int nsp_cs_config_check(struct pcmcia_device *p_dev, void *priv_data)
1540 {
1541         nsp_hw_data             *data = priv_data;
1542
1543         if (p_dev->config_index == 0)
1544                 return -ENODEV;
1545
1546         /* This reserves IO space but doesn't actually enable it */
1547         if (pcmcia_request_io(p_dev) != 0)
1548                 goto next_entry;
1549
1550         if (resource_size(p_dev->resource[2])) {
1551                 p_dev->resource[2]->flags |= (WIN_DATA_WIDTH_16 |
1552                                         WIN_MEMORY_TYPE_CM |
1553                                         WIN_ENABLE);
1554                 if (p_dev->resource[2]->end < 0x1000)
1555                         p_dev->resource[2]->end = 0x1000;
1556                 if (pcmcia_request_window(p_dev, p_dev->resource[2], 0) != 0)
1557                         goto next_entry;
1558                 if (pcmcia_map_mem_page(p_dev, p_dev->resource[2],
1559                                                 p_dev->card_addr) != 0)
1560                         goto next_entry;
1561
1562                 data->MmioAddress = (unsigned long)
1563                         ioremap(p_dev->resource[2]->start,
1564                                         resource_size(p_dev->resource[2]));
1565                 data->MmioLength  = resource_size(p_dev->resource[2]);
1566         }
1567         /* If we got this far, we're cool! */
1568         return 0;
1569
1570 next_entry:
1571         nsp_dbg(NSP_DEBUG_INIT, "next");
1572         pcmcia_disable_device(p_dev);
1573         return -ENODEV;
1574 }
1575
1576 static int nsp_cs_config(struct pcmcia_device *link)
1577 {
1578         int               ret;
1579         scsi_info_t      *info   = link->priv;
1580         struct Scsi_Host *host;
1581         nsp_hw_data      *data = &nsp_data_base;
1582
1583         nsp_dbg(NSP_DEBUG_INIT, "in");
1584
1585         link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_CHECK_VCC |
1586                 CONF_AUTO_SET_VPP | CONF_AUTO_AUDIO | CONF_AUTO_SET_IOMEM |
1587                 CONF_AUTO_SET_IO;
1588
1589         ret = pcmcia_loop_config(link, nsp_cs_config_check, data);
1590         if (ret)
1591                 goto cs_failed;
1592
1593         if (pcmcia_request_irq(link, nspintr))
1594                 goto cs_failed;
1595
1596         ret = pcmcia_enable_device(link);
1597         if (ret)
1598                 goto cs_failed;
1599
1600         if (free_ports) {
1601                 if (link->resource[0]) {
1602                         release_region(link->resource[0]->start,
1603                                         resource_size(link->resource[0]));
1604                 }
1605                 if (link->resource[1]) {
1606                         release_region(link->resource[1]->start,
1607                                         resource_size(link->resource[1]));
1608                 }
1609         }
1610
1611         /* Set port and IRQ */
1612         data->BaseAddress = link->resource[0]->start;
1613         data->NumAddress  = resource_size(link->resource[0]);
1614         data->IrqNumber   = link->irq;
1615
1616         nsp_dbg(NSP_DEBUG_INIT, "I/O[0x%x+0x%x] IRQ %d",
1617                 data->BaseAddress, data->NumAddress, data->IrqNumber);
1618
1619         if(nsphw_init(data) == FALSE) {
1620                 goto cs_failed;
1621         }
1622
1623         host = nsp_detect(&nsp_driver_template);
1624
1625         if (host == NULL) {
1626                 nsp_dbg(NSP_DEBUG_INIT, "detect failed");
1627                 goto cs_failed;
1628         }
1629
1630
1631         ret = scsi_add_host (host, NULL);
1632         if (ret)
1633                 goto cs_failed;
1634
1635         scsi_scan_host(host);
1636
1637         info->host = host;
1638
1639         return 0;
1640
1641  cs_failed:
1642         nsp_dbg(NSP_DEBUG_INIT, "config fail");
1643         nsp_cs_release(link);
1644
1645         return -ENODEV;
1646 } /* nsp_cs_config */
1647
1648
1649 static void nsp_cs_release(struct pcmcia_device *link)
1650 {
1651         scsi_info_t *info = link->priv;
1652         nsp_hw_data *data = NULL;
1653
1654         if (info->host == NULL) {
1655                 nsp_msg(KERN_DEBUG, "unexpected card release call.");
1656         } else {
1657                 data = (nsp_hw_data *)info->host->hostdata;
1658         }
1659
1660         nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
1661
1662         /* Unlink the device chain */
1663         if (info->host != NULL) {
1664                 scsi_remove_host(info->host);
1665         }
1666
1667         if (resource_size(link->resource[2])) {
1668                 if (data != NULL) {
1669                         iounmap((void *)(data->MmioAddress));
1670                 }
1671         }
1672         pcmcia_disable_device(link);
1673
1674         if (info->host != NULL) {
1675                 scsi_host_put(info->host);
1676         }
1677 } /* nsp_cs_release */
1678
1679 static int nsp_cs_suspend(struct pcmcia_device *link)
1680 {
1681         scsi_info_t *info = link->priv;
1682         nsp_hw_data *data;
1683
1684         nsp_dbg(NSP_DEBUG_INIT, "event: suspend");
1685
1686         if (info->host != NULL) {
1687                 nsp_msg(KERN_INFO, "clear SDTR status");
1688
1689                 data = (nsp_hw_data *)info->host->hostdata;
1690
1691                 nsphw_init_sync(data);
1692         }
1693
1694         info->stop = 1;
1695
1696         return 0;
1697 }
1698
1699 static int nsp_cs_resume(struct pcmcia_device *link)
1700 {
1701         scsi_info_t *info = link->priv;
1702         nsp_hw_data *data;
1703
1704         nsp_dbg(NSP_DEBUG_INIT, "event: resume");
1705
1706         info->stop = 0;
1707
1708         if (info->host != NULL) {
1709                 nsp_msg(KERN_INFO, "reset host and bus");
1710
1711                 data = (nsp_hw_data *)info->host->hostdata;
1712
1713                 nsphw_init   (data);
1714                 nsp_bus_reset(data);
1715         }
1716
1717         return 0;
1718 }
1719
1720 /*======================================================================*
1721  *      module entry point
1722  *====================================================================*/
1723 static const struct pcmcia_device_id nsp_cs_ids[] = {
1724         PCMCIA_DEVICE_PROD_ID123("IO DATA", "CBSC16       ", "1", 0x547e66dc, 0x0d63a3fd, 0x51de003a),
1725         PCMCIA_DEVICE_PROD_ID123("KME    ", "SCSI-CARD-001", "1", 0x534c02bc, 0x52008408, 0x51de003a),
1726         PCMCIA_DEVICE_PROD_ID123("KME    ", "SCSI-CARD-002", "1", 0x534c02bc, 0xcb09d5b2, 0x51de003a),
1727         PCMCIA_DEVICE_PROD_ID123("KME    ", "SCSI-CARD-003", "1", 0x534c02bc, 0xbc0ee524, 0x51de003a),
1728         PCMCIA_DEVICE_PROD_ID123("KME    ", "SCSI-CARD-004", "1", 0x534c02bc, 0x226a7087, 0x51de003a),
1729         PCMCIA_DEVICE_PROD_ID123("WBT", "NinjaSCSI-3", "R1.0", 0xc7ba805f, 0xfdc7c97d, 0x6973710e),
1730         PCMCIA_DEVICE_PROD_ID123("WORKBIT", "UltraNinja-16", "1", 0x28191418, 0xb70f4b09, 0x51de003a),
1731         PCMCIA_DEVICE_NULL
1732 };
1733 MODULE_DEVICE_TABLE(pcmcia, nsp_cs_ids);
1734
1735 static struct pcmcia_driver nsp_driver = {
1736         .owner          = THIS_MODULE,
1737         .name           = "nsp_cs",
1738         .probe          = nsp_cs_probe,
1739         .remove         = nsp_cs_detach,
1740         .id_table       = nsp_cs_ids,
1741         .suspend        = nsp_cs_suspend,
1742         .resume         = nsp_cs_resume,
1743 };
1744 module_pcmcia_driver(nsp_driver);
1745
1746 /* end */