[PATCH] pcmcia: remove dev_list from drivers
[firefly-linux-kernel-4.4.55.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 /* $Id: nsp_cs.c,v 1.23 2003/08/18 11:09:19 elca Exp $ */
29
30 #include <linux/version.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/timer.h>
38 #include <linux/ioport.h>
39 #include <linux/delay.h>
40 #include <linux/interrupt.h>
41 #include <linux/major.h>
42 #include <linux/blkdev.h>
43 #include <linux/stat.h>
44
45 #include <asm/io.h>
46 #include <asm/irq.h>
47
48 #include <../drivers/scsi/scsi.h>
49 #include <scsi/scsi_host.h>
50
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_ioctl.h>
53
54 #include <pcmcia/cs_types.h>
55 #include <pcmcia/cs.h>
56 #include <pcmcia/cistpl.h>
57 #include <pcmcia/cisreg.h>
58 #include <pcmcia/ds.h>
59
60 #include "nsp_cs.h"
61
62 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>");
63 MODULE_DESCRIPTION("WorkBit NinjaSCSI-3 / NinjaSCSI-32Bi(16bit) PCMCIA SCSI host adapter module $Revision: 1.23 $");
64 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
65 #ifdef MODULE_LICENSE
66 MODULE_LICENSE("GPL");
67 #endif
68
69 #include "nsp_io.h"
70
71 /*====================================================================*/
72 /* Parameters that can be set with 'insmod' */
73
74 static int       nsp_burst_mode = BURST_MEM32;
75 module_param(nsp_burst_mode, int, 0);
76 MODULE_PARM_DESC(nsp_burst_mode, "Burst transfer mode (0=io8, 1=io32, 2=mem32(default))");
77
78 /* Release IO ports after configuration? */
79 static int       free_ports = 0;
80 module_param(free_ports, bool, 0);
81 MODULE_PARM_DESC(free_ports, "Release IO ports after configuration? (default: 0 (=no))");
82
83 /* /usr/src/linux/drivers/scsi/hosts.h */
84 static struct scsi_host_template nsp_driver_template = {
85         .proc_name               = "nsp_cs",
86         .proc_info               = nsp_proc_info,
87         .name                    = "WorkBit NinjaSCSI-3/32Bi(16bit)",
88 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
89         .detect                  = nsp_detect_old,
90         .release                 = nsp_release_old,
91 #endif
92         .info                    = nsp_info,
93         .queuecommand            = nsp_queuecommand,
94 /*      .eh_abort_handler        = nsp_eh_abort,*/
95         .eh_bus_reset_handler    = nsp_eh_bus_reset,
96         .eh_host_reset_handler   = nsp_eh_host_reset,
97         .can_queue               = 1,
98         .this_id                 = NSP_INITIATOR_ID,
99         .sg_tablesize            = SG_ALL,
100         .cmd_per_lun             = 1,
101         .use_clustering          = DISABLE_CLUSTERING,
102 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,2))
103         .use_new_eh_code         = 1,
104 #endif
105 };
106
107 static dev_info_t dev_info  = {"nsp_cs"};
108
109 static nsp_hw_data nsp_data_base; /* attach <-> detect glue */
110
111
112
113 /*
114  * debug, error print
115  */
116 #ifndef NSP_DEBUG
117 # define NSP_DEBUG_MASK         0x000000
118 # define nsp_msg(type, args...) nsp_cs_message("", 0, (type), args)
119 # define nsp_dbg(mask, args...) /* */
120 #else
121 # define NSP_DEBUG_MASK         0xffffff
122 # define nsp_msg(type, args...) \
123         nsp_cs_message (__FUNCTION__, __LINE__, (type), args)
124 # define nsp_dbg(mask, args...) \
125         nsp_cs_dmessage(__FUNCTION__, __LINE__, (mask), args)
126 #endif
127
128 #define NSP_DEBUG_QUEUECOMMAND          BIT(0)
129 #define NSP_DEBUG_REGISTER              BIT(1)
130 #define NSP_DEBUG_AUTOSCSI              BIT(2)
131 #define NSP_DEBUG_INTR                  BIT(3)
132 #define NSP_DEBUG_SGLIST                BIT(4)
133 #define NSP_DEBUG_BUSFREE               BIT(5)
134 #define NSP_DEBUG_CDB_CONTENTS          BIT(6)
135 #define NSP_DEBUG_RESELECTION           BIT(7)
136 #define NSP_DEBUG_MSGINOCCUR            BIT(8)
137 #define NSP_DEBUG_EEPROM                BIT(9)
138 #define NSP_DEBUG_MSGOUTOCCUR           BIT(10)
139 #define NSP_DEBUG_BUSRESET              BIT(11)
140 #define NSP_DEBUG_RESTART               BIT(12)
141 #define NSP_DEBUG_SYNC                  BIT(13)
142 #define NSP_DEBUG_WAIT                  BIT(14)
143 #define NSP_DEBUG_TARGETFLAG            BIT(15)
144 #define NSP_DEBUG_PROC                  BIT(16)
145 #define NSP_DEBUG_INIT                  BIT(17)
146 #define NSP_DEBUG_DATA_IO               BIT(18)
147 #define NSP_SPECIAL_PRINT_REGISTER      BIT(20)
148
149 #define NSP_DEBUG_BUF_LEN               150
150
151 static void nsp_cs_message(const char *func, int line, char *type, char *fmt, ...)
152 {
153         va_list args;
154         char buf[NSP_DEBUG_BUF_LEN];
155
156         va_start(args, fmt);
157         vsnprintf(buf, sizeof(buf), fmt, args);
158         va_end(args);
159
160 #ifndef NSP_DEBUG
161         printk("%snsp_cs: %s\n", type, buf);
162 #else
163         printk("%snsp_cs: %s (%d): %s\n", type, func, line, buf);
164 #endif
165 }
166
167 #ifdef NSP_DEBUG
168 static void nsp_cs_dmessage(const char *func, int line, int mask, char *fmt, ...)
169 {
170         va_list args;
171         char buf[NSP_DEBUG_BUF_LEN];
172
173         va_start(args, fmt);
174         vsnprintf(buf, sizeof(buf), fmt, args);
175         va_end(args);
176
177         if (mask & NSP_DEBUG_MASK) {
178                 printk("nsp_cs-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
179         }
180 }
181 #endif
182
183 /***********************************************************/
184
185 /*====================================================
186  * Clenaup parameters and call done() functions.
187  * You must be set SCpnt->result before call this function.
188  */
189 static void nsp_scsi_done(Scsi_Cmnd *SCpnt)
190 {
191         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
192
193         data->CurrentSC = NULL;
194
195         SCpnt->scsi_done(SCpnt);
196 }
197
198 static int nsp_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
199 {
200 #ifdef NSP_DEBUG
201         /*unsigned int host_id = SCpnt->device->host->this_id;*/
202         /*unsigned int base    = SCpnt->device->host->io_port;*/
203         unsigned char target = scmd_id(SCpnt);
204 #endif
205         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
206
207         nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "SCpnt=0x%p target=%d lun=%d buff=0x%p bufflen=%d use_sg=%d",
208                    SCpnt, target, SCpnt->device->lun, SCpnt->request_buffer, SCpnt->request_bufflen, SCpnt->use_sg);
209         //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "before CurrentSC=0x%p", data->CurrentSC);
210
211         SCpnt->scsi_done        = done;
212
213         if (data->CurrentSC != NULL) {
214                 nsp_msg(KERN_DEBUG, "CurrentSC!=NULL this can't be happen");
215                 SCpnt->result   = DID_BAD_TARGET << 16;
216                 nsp_scsi_done(SCpnt);
217                 return 0;
218         }
219
220 #if 0
221         /* XXX: pcmcia-cs generates SCSI command with "scsi_info" utility.
222                 This makes kernel crash when suspending... */
223         if (data->ScsiInfo->stop != 0) {
224                 nsp_msg(KERN_INFO, "suspending device. reject command.");
225                 SCpnt->result  = DID_BAD_TARGET << 16;
226                 nsp_scsi_done(SCpnt);
227                 return SCSI_MLQUEUE_HOST_BUSY;
228         }
229 #endif
230
231         show_command(SCpnt);
232
233         data->CurrentSC         = SCpnt;
234
235         SCpnt->SCp.Status       = CHECK_CONDITION;
236         SCpnt->SCp.Message      = 0;
237         SCpnt->SCp.have_data_in = IO_UNKNOWN;
238         SCpnt->SCp.sent_command = 0;
239         SCpnt->SCp.phase        = PH_UNDETERMINED;
240         SCpnt->resid            = SCpnt->request_bufflen;
241
242         /* setup scratch area
243            SCp.ptr              : buffer pointer
244            SCp.this_residual    : buffer length
245            SCp.buffer           : next buffer
246            SCp.buffers_residual : left buffers in list
247            SCp.phase            : current state of the command */
248         if (SCpnt->use_sg) {
249                 SCpnt->SCp.buffer           = (struct scatterlist *) SCpnt->request_buffer;
250                 SCpnt->SCp.ptr              = BUFFER_ADDR;
251                 SCpnt->SCp.this_residual    = SCpnt->SCp.buffer->length;
252                 SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
253         } else {
254                 SCpnt->SCp.ptr              = (char *) SCpnt->request_buffer;
255                 SCpnt->SCp.this_residual    = SCpnt->request_bufflen;
256                 SCpnt->SCp.buffer           = NULL;
257                 SCpnt->SCp.buffers_residual = 0;
258         }
259
260         if (nsphw_start_selection(SCpnt) == FALSE) {
261                 nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "selection fail");
262                 SCpnt->result   = DID_BUS_BUSY << 16;
263                 nsp_scsi_done(SCpnt);
264                 return 0;
265         }
266
267
268         //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "out");
269 #ifdef NSP_DEBUG
270         data->CmdId++;
271 #endif
272         return 0;
273 }
274
275 /*
276  * setup PIO FIFO transfer mode and enable/disable to data out
277  */
278 static void nsp_setup_fifo(nsp_hw_data *data, int enabled)
279 {
280         unsigned int  base = data->BaseAddress;
281         unsigned char transfer_mode_reg;
282
283         //nsp_dbg(NSP_DEBUG_DATA_IO, "enabled=%d", enabled);
284
285         if (enabled != FALSE) {
286                 transfer_mode_reg = TRANSFER_GO | BRAIND;
287         } else {
288                 transfer_mode_reg = 0;
289         }
290
291         transfer_mode_reg |= data->TransferMode;
292
293         nsp_index_write(base, TRANSFERMODE, transfer_mode_reg);
294 }
295
296 static void nsphw_init_sync(nsp_hw_data *data)
297 {
298         sync_data tmp_sync = { .SyncNegotiation = SYNC_NOT_YET,
299                                .SyncPeriod      = 0,
300                                .SyncOffset      = 0
301         };
302         int i;
303
304         /* setup sync data */
305         for ( i = 0; i < ARRAY_SIZE(data->Sync); i++ ) {
306                 data->Sync[i] = tmp_sync;
307         }
308 }
309
310 /*
311  * Initialize Ninja hardware
312  */
313 static int nsphw_init(nsp_hw_data *data)
314 {
315         unsigned int base     = data->BaseAddress;
316
317         nsp_dbg(NSP_DEBUG_INIT, "in base=0x%x", base);
318
319         data->ScsiClockDiv = CLOCK_40M | FAST_20;
320         data->CurrentSC    = NULL;
321         data->FifoCount    = 0;
322         data->TransferMode = MODE_IO8;
323
324         nsphw_init_sync(data);
325
326         /* block all interrupts */
327         nsp_write(base,       IRQCONTROL,   IRQCONTROL_ALLMASK);
328
329         /* setup SCSI interface */
330         nsp_write(base,       IFSELECT,     IF_IFSEL);
331
332         nsp_index_write(base, SCSIIRQMODE,  0);
333
334         nsp_index_write(base, TRANSFERMODE, MODE_IO8);
335         nsp_index_write(base, CLOCKDIV,     data->ScsiClockDiv);
336
337         nsp_index_write(base, PARITYCTRL,   0);
338         nsp_index_write(base, POINTERCLR,   POINTER_CLEAR     |
339                                             ACK_COUNTER_CLEAR |
340                                             REQ_COUNTER_CLEAR |
341                                             HOST_COUNTER_CLEAR);
342
343         /* setup fifo asic */
344         nsp_write(base,       IFSELECT,     IF_REGSEL);
345         nsp_index_write(base, TERMPWRCTRL,  0);
346         if ((nsp_index_read(base, OTHERCONTROL) & TPWR_SENSE) == 0) {
347                 nsp_msg(KERN_INFO, "terminator power on");
348                 nsp_index_write(base, TERMPWRCTRL, POWER_ON);
349         }
350
351         nsp_index_write(base, TIMERCOUNT,   0);
352         nsp_index_write(base, TIMERCOUNT,   0); /* requires 2 times!! */
353
354         nsp_index_write(base, SYNCREG,      0);
355         nsp_index_write(base, ACKWIDTH,     0);
356
357         /* enable interrupts and ack them */
358         nsp_index_write(base, SCSIIRQMODE,  SCSI_PHASE_CHANGE_EI |
359                                             RESELECT_EI          |
360                                             SCSI_RESET_IRQ_EI    );
361         nsp_write(base,       IRQCONTROL,   IRQCONTROL_ALLCLEAR);
362
363         nsp_setup_fifo(data, FALSE);
364
365         return TRUE;
366 }
367
368 /*
369  * Start selection phase
370  */
371 static int nsphw_start_selection(Scsi_Cmnd *SCpnt)
372 {
373         unsigned int  host_id    = SCpnt->device->host->this_id;
374         unsigned int  base       = SCpnt->device->host->io_port;
375         unsigned char target     = scmd_id(SCpnt);
376         nsp_hw_data  *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
377         int           time_out;
378         unsigned char phase, arbit;
379
380         //nsp_dbg(NSP_DEBUG_RESELECTION, "in");
381
382         phase = nsp_index_read(base, SCSIBUSMON);
383         if(phase != BUSMON_BUS_FREE) {
384                 //nsp_dbg(NSP_DEBUG_RESELECTION, "bus busy");
385                 return FALSE;
386         }
387
388         /* start arbitration */
389         //nsp_dbg(NSP_DEBUG_RESELECTION, "start arbit");
390         SCpnt->SCp.phase = PH_ARBSTART;
391         nsp_index_write(base, SETARBIT, ARBIT_GO);
392
393         time_out = 1000;
394         do {
395                 /* XXX: what a stupid chip! */
396                 arbit = nsp_index_read(base, ARBITSTATUS);
397                 //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit=%d, wait_count=%d", arbit, wait_count);
398                 udelay(1); /* hold 1.2us */
399         } while((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
400                 (time_out-- != 0));
401
402         if (!(arbit & ARBIT_WIN)) {
403                 //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit fail");
404                 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
405                 return FALSE;
406         }
407
408         /* assert select line */
409         //nsp_dbg(NSP_DEBUG_RESELECTION, "assert SEL line");
410         SCpnt->SCp.phase = PH_SELSTART;
411         udelay(3); /* wait 2.4us */
412         nsp_index_write(base, SCSIDATALATCH, BIT(host_id) | BIT(target));
413         nsp_index_write(base, SCSIBUSCTRL,   SCSI_SEL | SCSI_BSY                    | SCSI_ATN);
414         udelay(2); /* wait >1.2us */
415         nsp_index_write(base, SCSIBUSCTRL,   SCSI_SEL | SCSI_BSY | SCSI_DATAOUT_ENB | SCSI_ATN);
416         nsp_index_write(base, SETARBIT,      ARBIT_FLAG_CLEAR);
417         /*udelay(1);*/ /* wait >90ns */
418         nsp_index_write(base, SCSIBUSCTRL,   SCSI_SEL            | SCSI_DATAOUT_ENB | SCSI_ATN);
419
420         /* check selection timeout */
421         nsp_start_timer(SCpnt, 1000/51);
422         data->SelectionTimeOut = 1;
423
424         return TRUE;
425 }
426
427 struct nsp_sync_table {
428         unsigned int min_period;
429         unsigned int max_period;
430         unsigned int chip_period;
431         unsigned int ack_width;
432 };
433
434 static struct nsp_sync_table nsp_sync_table_40M[] = {
435         {0x0c, 0x0c, 0x1, 0},   /* 20MB   50ns*/
436         {0x19, 0x19, 0x3, 1},   /* 10MB  100ns*/ 
437         {0x1a, 0x25, 0x5, 2},   /* 7.5MB 150ns*/ 
438         {0x26, 0x32, 0x7, 3},   /* 5MB   200ns*/
439         {   0,    0,   0, 0},
440 };
441
442 static struct nsp_sync_table nsp_sync_table_20M[] = {
443         {0x19, 0x19, 0x1, 0},   /* 10MB  100ns*/ 
444         {0x1a, 0x25, 0x2, 0},   /* 7.5MB 150ns*/ 
445         {0x26, 0x32, 0x3, 1},   /* 5MB   200ns*/
446         {   0,    0,   0, 0},
447 };
448
449 /*
450  * setup synchronous data transfer mode
451  */
452 static int nsp_analyze_sdtr(Scsi_Cmnd *SCpnt)
453 {
454         unsigned char          target = scmd_id(SCpnt);
455 //      unsigned char          lun    = SCpnt->device->lun;
456         nsp_hw_data           *data   = (nsp_hw_data *)SCpnt->device->host->hostdata;
457         sync_data             *sync   = &(data->Sync[target]);
458         struct nsp_sync_table *sync_table;
459         unsigned int           period, offset;
460         int                    i;
461
462
463         nsp_dbg(NSP_DEBUG_SYNC, "in");
464
465         period = sync->SyncPeriod;
466         offset = sync->SyncOffset;
467
468         nsp_dbg(NSP_DEBUG_SYNC, "period=0x%x, offset=0x%x", period, offset);
469
470         if ((data->ScsiClockDiv & (BIT(0)|BIT(1))) == CLOCK_20M) {
471                 sync_table = nsp_sync_table_20M;
472         } else {
473                 sync_table = nsp_sync_table_40M;
474         }
475
476         for ( i = 0; sync_table->max_period != 0; i++, sync_table++) {
477                 if ( period >= sync_table->min_period &&
478                      period <= sync_table->max_period    ) {
479                         break;
480                 }
481         }
482
483         if (period != 0 && sync_table->max_period == 0) {
484                 /*
485                  * No proper period/offset found
486                  */
487                 nsp_dbg(NSP_DEBUG_SYNC, "no proper period/offset");
488
489                 sync->SyncPeriod      = 0;
490                 sync->SyncOffset      = 0;
491                 sync->SyncRegister    = 0;
492                 sync->AckWidth        = 0;
493
494                 return FALSE;
495         }
496
497         sync->SyncRegister    = (sync_table->chip_period << SYNCREG_PERIOD_SHIFT) |
498                                 (offset & SYNCREG_OFFSET_MASK);
499         sync->AckWidth        = sync_table->ack_width;
500
501         nsp_dbg(NSP_DEBUG_SYNC, "sync_reg=0x%x, ack_width=0x%x", sync->SyncRegister, sync->AckWidth);
502
503         return TRUE;
504 }
505
506
507 /*
508  * start ninja hardware timer
509  */
510 static void nsp_start_timer(Scsi_Cmnd *SCpnt, int time)
511 {
512         unsigned int base = SCpnt->device->host->io_port;
513         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
514
515         //nsp_dbg(NSP_DEBUG_INTR, "in SCpnt=0x%p, time=%d", SCpnt, time);
516         data->TimerCount = time;
517         nsp_index_write(base, TIMERCOUNT, time);
518 }
519
520 /*
521  * wait for bus phase change
522  */
523 static int nsp_negate_signal(Scsi_Cmnd *SCpnt, unsigned char mask, char *str)
524 {
525         unsigned int  base = SCpnt->device->host->io_port;
526         unsigned char reg;
527         int           time_out;
528
529         //nsp_dbg(NSP_DEBUG_INTR, "in");
530
531         time_out = 100;
532
533         do {
534                 reg = nsp_index_read(base, SCSIBUSMON);
535                 if (reg == 0xff) {
536                         break;
537                 }
538         } while ((time_out-- != 0) && (reg & mask) != 0);
539
540         if (time_out == 0) {
541                 nsp_msg(KERN_DEBUG, " %s signal off timeut", str);
542         }
543
544         return 0;
545 }
546
547 /*
548  * expect Ninja Irq
549  */
550 static int nsp_expect_signal(Scsi_Cmnd     *SCpnt,
551                              unsigned char  current_phase,
552                              unsigned char  mask)
553 {
554         unsigned int  base       = SCpnt->device->host->io_port;
555         int           time_out;
556         unsigned char phase, i_src;
557
558         //nsp_dbg(NSP_DEBUG_INTR, "current_phase=0x%x, mask=0x%x", current_phase, mask);
559
560         time_out = 100;
561         do {
562                 phase = nsp_index_read(base, SCSIBUSMON);
563                 if (phase == 0xff) {
564                         //nsp_dbg(NSP_DEBUG_INTR, "ret -1");
565                         return -1;
566                 }
567                 i_src = nsp_read(base, IRQSTATUS);
568                 if (i_src & IRQSTATUS_SCSI) {
569                         //nsp_dbg(NSP_DEBUG_INTR, "ret 0 found scsi signal");
570                         return 0;
571                 }
572                 if ((phase & mask) != 0 && (phase & BUSMON_PHASE_MASK) == current_phase) {
573                         //nsp_dbg(NSP_DEBUG_INTR, "ret 1 phase=0x%x", phase);
574                         return 1;
575                 }
576         } while(time_out-- != 0);
577
578         //nsp_dbg(NSP_DEBUG_INTR, "timeout");
579         return -1;
580 }
581
582 /*
583  * transfer SCSI message
584  */
585 static int nsp_xfer(Scsi_Cmnd *SCpnt, int phase)
586 {
587         unsigned int  base = SCpnt->device->host->io_port;
588         nsp_hw_data  *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
589         char         *buf  = data->MsgBuffer;
590         int           len  = min(MSGBUF_SIZE, data->MsgLen);
591         int           ptr;
592         int           ret;
593
594         //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
595         for (ptr = 0; len > 0; len--, ptr++) {
596
597                 ret = nsp_expect_signal(SCpnt, phase, BUSMON_REQ);
598                 if (ret <= 0) {
599                         nsp_dbg(NSP_DEBUG_DATA_IO, "xfer quit");
600                         return 0;
601                 }
602
603                 /* if last byte, negate ATN */
604                 if (len == 1 && SCpnt->SCp.phase == PH_MSG_OUT) {
605                         nsp_index_write(base, SCSIBUSCTRL, AUTODIRECTION | ACKENB);
606                 }
607
608                 /* read & write message */
609                 if (phase & BUSMON_IO) {
610                         nsp_dbg(NSP_DEBUG_DATA_IO, "read msg");
611                         buf[ptr] = nsp_index_read(base, SCSIDATAWITHACK);
612                 } else {
613                         nsp_dbg(NSP_DEBUG_DATA_IO, "write msg");
614                         nsp_index_write(base, SCSIDATAWITHACK, buf[ptr]);
615                 }
616                 nsp_negate_signal(SCpnt, BUSMON_ACK, "xfer<ack>");
617
618         }
619         return len;
620 }
621
622 /*
623  * get extra SCSI data from fifo
624  */
625 static int nsp_dataphase_bypass(Scsi_Cmnd *SCpnt)
626 {
627         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
628         unsigned int count;
629
630         //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
631
632         if (SCpnt->SCp.have_data_in != IO_IN) {
633                 return 0;
634         }
635
636         count = nsp_fifo_count(SCpnt);
637         if (data->FifoCount == count) {
638                 //nsp_dbg(NSP_DEBUG_DATA_IO, "not use bypass quirk");
639                 return 0;
640         }
641
642         /*
643          * XXX: NSP_QUIRK
644          * data phase skip only occures in case of SCSI_LOW_READ
645          */
646         nsp_dbg(NSP_DEBUG_DATA_IO, "use bypass quirk");
647         SCpnt->SCp.phase = PH_DATA;
648         nsp_pio_read(SCpnt);
649         nsp_setup_fifo(data, FALSE);
650
651         return 0;
652 }
653
654 /*
655  * accept reselection
656  */
657 static int nsp_reselected(Scsi_Cmnd *SCpnt)
658 {
659         unsigned int  base    = SCpnt->device->host->io_port;
660         unsigned int  host_id = SCpnt->device->host->this_id;
661         //nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
662         unsigned char bus_reg;
663         unsigned char id_reg, tmp;
664         int target;
665
666         nsp_dbg(NSP_DEBUG_RESELECTION, "in");
667
668         id_reg = nsp_index_read(base, RESELECTID);
669         tmp    = id_reg & (~BIT(host_id));
670         target = 0;
671         while(tmp != 0) {
672                 if (tmp & BIT(0)) {
673                         break;
674                 }
675                 tmp >>= 1;
676                 target++;
677         }
678
679         if (scmd_id(SCpnt) != target) {
680                 nsp_msg(KERN_ERR, "XXX: reselect ID must be %d in this implementation.", target);
681         }
682
683         nsp_negate_signal(SCpnt, BUSMON_SEL, "reselect<SEL>");
684
685         nsp_nexus(SCpnt);
686         bus_reg = nsp_index_read(base, SCSIBUSCTRL) & ~(SCSI_BSY | SCSI_ATN);
687         nsp_index_write(base, SCSIBUSCTRL, bus_reg);
688         nsp_index_write(base, SCSIBUSCTRL, bus_reg | AUTODIRECTION | ACKENB);
689
690         return TRUE;
691 }
692
693 /*
694  * count how many data transferd
695  */
696 static int nsp_fifo_count(Scsi_Cmnd *SCpnt)
697 {
698         unsigned int base = SCpnt->device->host->io_port;
699         unsigned int count;
700         unsigned int l, m, h, dummy;
701
702         nsp_index_write(base, POINTERCLR, POINTER_CLEAR | ACK_COUNTER);
703
704         l     = nsp_index_read(base, TRANSFERCOUNT);
705         m     = nsp_index_read(base, TRANSFERCOUNT);
706         h     = nsp_index_read(base, TRANSFERCOUNT);
707         dummy = nsp_index_read(base, TRANSFERCOUNT); /* required this! */
708
709         count = (h << 16) | (m << 8) | (l << 0);
710
711         //nsp_dbg(NSP_DEBUG_DATA_IO, "count=0x%x", count);
712
713         return count;
714 }
715
716 /* fifo size */
717 #define RFIFO_CRIT 64
718 #define WFIFO_CRIT 64
719
720 /*
721  * read data in DATA IN phase
722  */
723 static void nsp_pio_read(Scsi_Cmnd *SCpnt)
724 {
725         unsigned int  base      = SCpnt->device->host->io_port;
726         unsigned long mmio_base = SCpnt->device->host->base;
727         nsp_hw_data  *data      = (nsp_hw_data *)SCpnt->device->host->hostdata;
728         long          time_out;
729         int           ocount, res;
730         unsigned char stat, fifo_stat;
731
732         ocount = data->FifoCount;
733
734         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",
735                 SCpnt, SCpnt->resid, ocount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual);
736
737         time_out = 1000;
738
739         while ((time_out-- != 0) &&
740                (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0 ) ) {
741
742                 stat = nsp_index_read(base, SCSIBUSMON);
743                 stat &= BUSMON_PHASE_MASK;
744
745
746                 res = nsp_fifo_count(SCpnt) - ocount;
747                 //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);
748                 if (res == 0) { /* if some data avilable ? */
749                         if (stat == BUSPHASE_DATA_IN) { /* phase changed? */
750                                 //nsp_dbg(NSP_DEBUG_DATA_IO, " wait for data this=%d", SCpnt->SCp.this_residual);
751                                 continue;
752                         } else {
753                                 nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x", stat);
754                                 break;
755                         }
756                 }
757
758                 fifo_stat = nsp_read(base, FIFOSTATUS);
759                 if ((fifo_stat & FIFOSTATUS_FULL_EMPTY) == 0 &&
760                     stat                                == BUSPHASE_DATA_IN) {
761                         continue;
762                 }
763
764                 res = min(res, SCpnt->SCp.this_residual);
765
766                 switch (data->TransferMode) {
767                 case MODE_IO32:
768                         res &= ~(BIT(1)|BIT(0)); /* align 4 */
769                         nsp_fifo32_read(base, SCpnt->SCp.ptr, res >> 2);
770                         break;
771                 case MODE_IO8:
772                         nsp_fifo8_read (base, SCpnt->SCp.ptr, res     );
773                         break;
774
775                 case MODE_MEM32:
776                         res &= ~(BIT(1)|BIT(0)); /* align 4 */
777                         nsp_mmio_fifo32_read(mmio_base, SCpnt->SCp.ptr, res >> 2);
778                         break;
779
780                 default:
781                         nsp_dbg(NSP_DEBUG_DATA_IO, "unknown read mode");
782                         return;
783                 }
784
785                 SCpnt->resid             -= res;
786                 SCpnt->SCp.ptr           += res;
787                 SCpnt->SCp.this_residual -= res;
788                 ocount                   += res;
789                 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this_residual=0x%x ocount=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount);
790
791                 /* go to next scatter list if available */
792                 if (SCpnt->SCp.this_residual    == 0 &&
793                     SCpnt->SCp.buffers_residual != 0 ) {
794                         //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next timeout=%d", time_out);
795                         SCpnt->SCp.buffers_residual--;
796                         SCpnt->SCp.buffer++;
797                         SCpnt->SCp.ptr           = BUFFER_ADDR;
798                         SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
799                         time_out = 1000;
800
801                         //nsp_dbg(NSP_DEBUG_DATA_IO, "page: 0x%p, off: 0x%x", SCpnt->SCp.buffer->page, SCpnt->SCp.buffer->offset);
802                 }
803         }
804
805         data->FifoCount = ocount;
806
807         if (time_out == 0) {
808                 nsp_msg(KERN_DEBUG, "pio read timeout resid=%d this_residual=%d buffers_residual=%d",
809                         SCpnt->resid, SCpnt->SCp.this_residual, SCpnt->SCp.buffers_residual);
810         }
811         nsp_dbg(NSP_DEBUG_DATA_IO, "read ocount=0x%x", ocount);
812         nsp_dbg(NSP_DEBUG_DATA_IO, "r cmd=%d resid=0x%x\n", data->CmdId, SCpnt->resid);
813 }
814
815 /*
816  * write data in DATA OUT phase
817  */
818 static void nsp_pio_write(Scsi_Cmnd *SCpnt)
819 {
820         unsigned int  base      = SCpnt->device->host->io_port;
821         unsigned long mmio_base = SCpnt->device->host->base;
822         nsp_hw_data  *data      = (nsp_hw_data *)SCpnt->device->host->hostdata;
823         int           time_out;
824         int           ocount, res;
825         unsigned char stat;
826
827         ocount   = data->FifoCount;
828
829         nsp_dbg(NSP_DEBUG_DATA_IO, "in fifocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d resid=0x%x",
830                 data->FifoCount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual, SCpnt->resid);
831
832         time_out = 1000;
833
834         while ((time_out-- != 0) &&
835                (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0)) {
836                 stat = nsp_index_read(base, SCSIBUSMON);
837                 stat &= BUSMON_PHASE_MASK;
838
839                 if (stat != BUSPHASE_DATA_OUT) {
840                         res = ocount - nsp_fifo_count(SCpnt);
841
842                         nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x, res=%d\n", stat, res);
843                         /* Put back pointer */
844                         SCpnt->resid             += res;
845                         SCpnt->SCp.ptr           -= res;
846                         SCpnt->SCp.this_residual += res;
847                         ocount                   -= res;
848
849                         break;
850                 }
851
852                 res = ocount - nsp_fifo_count(SCpnt);
853                 if (res > 0) { /* write all data? */
854                         nsp_dbg(NSP_DEBUG_DATA_IO, "wait for all data out. ocount=0x%x res=%d", ocount, res);
855                         continue;
856                 }
857
858                 res = min(SCpnt->SCp.this_residual, WFIFO_CRIT);
859
860                 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, res);
861                 switch (data->TransferMode) {
862                 case MODE_IO32:
863                         res &= ~(BIT(1)|BIT(0)); /* align 4 */
864                         nsp_fifo32_write(base, SCpnt->SCp.ptr, res >> 2);
865                         break;
866                 case MODE_IO8:
867                         nsp_fifo8_write (base, SCpnt->SCp.ptr, res     );
868                         break;
869
870                 case MODE_MEM32:
871                         res &= ~(BIT(1)|BIT(0)); /* align 4 */
872                         nsp_mmio_fifo32_write(mmio_base, SCpnt->SCp.ptr, res >> 2);
873                         break;
874
875                 default:
876                         nsp_dbg(NSP_DEBUG_DATA_IO, "unknown write mode");
877                         break;
878                 }
879
880                 SCpnt->resid             -= res;
881                 SCpnt->SCp.ptr           += res;
882                 SCpnt->SCp.this_residual -= res;
883                 ocount                   += res;
884
885                 /* go to next scatter list if available */
886                 if (SCpnt->SCp.this_residual    == 0 &&
887                     SCpnt->SCp.buffers_residual != 0 ) {
888                         //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next");
889                         SCpnt->SCp.buffers_residual--;
890                         SCpnt->SCp.buffer++;
891                         SCpnt->SCp.ptr           = BUFFER_ADDR;
892                         SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
893                         time_out = 1000;
894                 }
895         }
896
897         data->FifoCount = ocount;
898
899         if (time_out == 0) {
900                 nsp_msg(KERN_DEBUG, "pio write timeout resid=0x%x", SCpnt->resid);
901         }
902         nsp_dbg(NSP_DEBUG_DATA_IO, "write ocount=0x%x", ocount);
903         nsp_dbg(NSP_DEBUG_DATA_IO, "w cmd=%d resid=0x%x\n", data->CmdId, SCpnt->resid);
904 }
905 #undef RFIFO_CRIT
906 #undef WFIFO_CRIT
907
908 /*
909  * setup synchronous/asynchronous data transfer mode
910  */
911 static int nsp_nexus(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 (SCpnt->use_sg    == 0        ||
926             SCpnt->resid % 4 != 0        ||
927             SCpnt->resid     <= PAGE_SIZE ) {
928                 data->TransferMode = MODE_IO8;
929         } else if (nsp_burst_mode == BURST_MEM32) {
930                 data->TransferMode = MODE_MEM32;
931         } else if (nsp_burst_mode == BURST_IO32) {
932                 data->TransferMode = MODE_IO32;
933         } else {
934                 data->TransferMode = MODE_IO8;
935         }
936
937         /* setup pdma fifo */
938         nsp_setup_fifo(data, TRUE);
939
940         /* clear ack counter */
941         data->FifoCount = 0;
942         nsp_index_write(base, POINTERCLR, POINTER_CLEAR     |
943                                           ACK_COUNTER_CLEAR |
944                                           REQ_COUNTER_CLEAR |
945                                           HOST_COUNTER_CLEAR);
946
947         return 0;
948 }
949
950 #include "nsp_message.c"
951 /*
952  * interrupt handler
953  */
954 static irqreturn_t nspintr(int irq, void *dev_id, struct pt_regs *regs)
955 {
956         unsigned int   base;
957         unsigned char  irq_status, irq_phase, phase;
958         Scsi_Cmnd     *tmpSC;
959         unsigned char  target, lun;
960         unsigned int  *sync_neg;
961         int            i, tmp;
962         nsp_hw_data   *data;
963
964
965         //nsp_dbg(NSP_DEBUG_INTR, "dev_id=0x%p", dev_id);
966         //nsp_dbg(NSP_DEBUG_INTR, "host=0x%p", ((scsi_info_t *)dev_id)->host);
967
968         if (                dev_id        != NULL &&
969             ((scsi_info_t *)dev_id)->host != NULL  ) {
970                 scsi_info_t *info = (scsi_info_t *)dev_id;
971
972                 data = (nsp_hw_data *)info->host->hostdata;
973         } else {
974                 nsp_dbg(NSP_DEBUG_INTR, "host data wrong");
975                 return IRQ_NONE;
976         }
977
978         //nsp_dbg(NSP_DEBUG_INTR, "&nsp_data_base=0x%p, dev_id=0x%p", &nsp_data_base, dev_id);
979
980         base = data->BaseAddress;
981         //nsp_dbg(NSP_DEBUG_INTR, "base=0x%x", base);
982
983         /*
984          * interrupt check
985          */
986         nsp_write(base, IRQCONTROL, IRQCONTROL_IRQDISABLE);
987         irq_status = nsp_read(base, IRQSTATUS);
988         //nsp_dbg(NSP_DEBUG_INTR, "irq_status=0x%x", irq_status);
989         if ((irq_status == 0xff) || ((irq_status & IRQSTATUS_MASK) == 0)) {
990                 nsp_write(base, IRQCONTROL, 0);
991                 //nsp_dbg(NSP_DEBUG_INTR, "no irq/shared irq");
992                 return IRQ_NONE;
993         }
994
995         /* XXX: IMPORTANT
996          * Do not read an irq_phase register if no scsi phase interrupt.
997          * Unless, you should lose a scsi phase interrupt.
998          */
999         phase = nsp_index_read(base, SCSIBUSMON);
1000         if((irq_status & IRQSTATUS_SCSI) != 0) {
1001                 irq_phase = nsp_index_read(base, IRQPHASESENCE);
1002         } else {
1003                 irq_phase = 0;
1004         }
1005
1006         //nsp_dbg(NSP_DEBUG_INTR, "irq_phase=0x%x", irq_phase);
1007
1008         /*
1009          * timer interrupt handler (scsi vs timer interrupts)
1010          */
1011         //nsp_dbg(NSP_DEBUG_INTR, "timercount=%d", data->TimerCount);
1012         if (data->TimerCount != 0) {
1013                 //nsp_dbg(NSP_DEBUG_INTR, "stop timer");
1014                 nsp_index_write(base, TIMERCOUNT, 0);
1015                 nsp_index_write(base, TIMERCOUNT, 0);
1016                 data->TimerCount = 0;
1017         }
1018
1019         if ((irq_status & IRQSTATUS_MASK) == IRQSTATUS_TIMER &&
1020             data->SelectionTimeOut == 0) {
1021                 //nsp_dbg(NSP_DEBUG_INTR, "timer start");
1022                 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR);
1023                 return IRQ_HANDLED;
1024         }
1025
1026         nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR | IRQCONTROL_FIFO_CLEAR);
1027
1028         if ((irq_status & IRQSTATUS_SCSI) &&
1029             (irq_phase  & SCSI_RESET_IRQ)) {
1030                 nsp_msg(KERN_ERR, "bus reset (power off?)");
1031
1032                 nsphw_init(data);
1033                 nsp_bus_reset(data);
1034
1035                 if(data->CurrentSC != NULL) {
1036                         tmpSC = data->CurrentSC;
1037                         tmpSC->result  = (DID_RESET                   << 16) |
1038                                          ((tmpSC->SCp.Message & 0xff) <<  8) |
1039                                          ((tmpSC->SCp.Status  & 0xff) <<  0);
1040                         nsp_scsi_done(tmpSC);
1041                 }
1042                 return IRQ_HANDLED;
1043         }
1044
1045         if (data->CurrentSC == NULL) {
1046                 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);
1047                 nsphw_init(data);
1048                 nsp_bus_reset(data);
1049                 return IRQ_HANDLED;
1050         }
1051
1052         tmpSC    = data->CurrentSC;
1053         target   = tmpSC->device->id;
1054         lun      = tmpSC->device->lun;
1055         sync_neg = &(data->Sync[target].SyncNegotiation);
1056
1057         /*
1058          * parse hardware SCSI irq reasons register
1059          */
1060         if (irq_status & IRQSTATUS_SCSI) {
1061                 if (irq_phase & RESELECT_IRQ) {
1062                         nsp_dbg(NSP_DEBUG_INTR, "reselect");
1063                         nsp_write(base, IRQCONTROL, IRQCONTROL_RESELECT_CLEAR);
1064                         if (nsp_reselected(tmpSC) != FALSE) {
1065                                 return IRQ_HANDLED;
1066                         }
1067                 }
1068
1069                 if ((irq_phase & (PHASE_CHANGE_IRQ | LATCHED_BUS_FREE)) == 0) {
1070                         return IRQ_HANDLED;
1071                 }
1072         }
1073
1074         //show_phase(tmpSC);
1075
1076         switch(tmpSC->SCp.phase) {
1077         case PH_SELSTART:
1078                 // *sync_neg = SYNC_NOT_YET;
1079                 if ((phase & BUSMON_BSY) == 0) {
1080                         //nsp_dbg(NSP_DEBUG_INTR, "selection count=%d", data->SelectionTimeOut);
1081                         if (data->SelectionTimeOut >= NSP_SELTIMEOUT) {
1082                                 nsp_dbg(NSP_DEBUG_INTR, "selection time out");
1083                                 data->SelectionTimeOut = 0;
1084                                 nsp_index_write(base, SCSIBUSCTRL, 0);
1085
1086                                 tmpSC->result   = DID_TIME_OUT << 16;
1087                                 nsp_scsi_done(tmpSC);
1088
1089                                 return IRQ_HANDLED;
1090                         }
1091                         data->SelectionTimeOut += 1;
1092                         nsp_start_timer(tmpSC, 1000/51);
1093                         return IRQ_HANDLED;
1094                 }
1095
1096                 /* attention assert */
1097                 //nsp_dbg(NSP_DEBUG_INTR, "attention assert");
1098                 data->SelectionTimeOut = 0;
1099                 tmpSC->SCp.phase       = PH_SELECTED;
1100                 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN);
1101                 udelay(1);
1102                 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN | AUTODIRECTION | ACKENB);
1103                 return IRQ_HANDLED;
1104
1105                 break;
1106
1107         case PH_RESELECT:
1108                 //nsp_dbg(NSP_DEBUG_INTR, "phase reselect");
1109                 // *sync_neg = SYNC_NOT_YET;
1110                 if ((phase & BUSMON_PHASE_MASK) != BUSPHASE_MESSAGE_IN) {
1111
1112                         tmpSC->result   = DID_ABORT << 16;
1113                         nsp_scsi_done(tmpSC);
1114                         return IRQ_HANDLED;
1115                 }
1116                 /* fall thru */
1117         default:
1118                 if ((irq_status & (IRQSTATUS_SCSI | IRQSTATUS_FIFO)) == 0) {
1119                         return IRQ_HANDLED;
1120                 }
1121                 break;
1122         }
1123
1124         /*
1125          * SCSI sequencer
1126          */
1127         //nsp_dbg(NSP_DEBUG_INTR, "start scsi seq");
1128
1129         /* normal disconnect */
1130         if (((tmpSC->SCp.phase == PH_MSG_IN) || (tmpSC->SCp.phase == PH_MSG_OUT)) &&
1131             (irq_phase & LATCHED_BUS_FREE) != 0 ) {
1132                 nsp_dbg(NSP_DEBUG_INTR, "normal disconnect irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1133
1134                 //*sync_neg       = SYNC_NOT_YET;
1135
1136                 if ((tmpSC->SCp.Message == MSG_COMMAND_COMPLETE)) {     /* all command complete and return status */
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 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1319         host = scsi_host_alloc(&nsp_driver_template, sizeof(nsp_hw_data));
1320 #else
1321         host = scsi_register(sht, sizeof(nsp_hw_data));
1322 #endif
1323         if (host == NULL) {
1324                 nsp_dbg(NSP_DEBUG_INIT, "host failed");
1325                 return NULL;
1326         }
1327
1328         memcpy(host->hostdata, data_b, sizeof(nsp_hw_data));
1329         data = (nsp_hw_data *)host->hostdata;
1330         data->ScsiInfo->host = host;
1331 #ifdef NSP_DEBUG
1332         data->CmdId = 0;
1333 #endif
1334
1335         nsp_dbg(NSP_DEBUG_INIT, "irq=%d,%d", data_b->IrqNumber, ((nsp_hw_data *)host->hostdata)->IrqNumber);
1336
1337         host->unique_id   = data->BaseAddress;
1338         host->io_port     = data->BaseAddress;
1339         host->n_io_port   = data->NumAddress;
1340         host->irq         = data->IrqNumber;
1341         host->base        = data->MmioAddress;
1342
1343         spin_lock_init(&(data->Lock));
1344
1345         snprintf(data->nspinfo,
1346                  sizeof(data->nspinfo),
1347                  "NinjaSCSI-3/32Bi Driver $Revision: 1.23 $ IO:0x%04lx-0x%04lx MMIO(virt addr):0x%04lx IRQ:%02d",
1348                  host->io_port, host->io_port + host->n_io_port - 1,
1349                  host->base,
1350                  host->irq);
1351         sht->name         = data->nspinfo;
1352
1353         nsp_dbg(NSP_DEBUG_INIT, "end");
1354
1355
1356         return host; /* detect done. */
1357 }
1358
1359 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1360 static int nsp_detect_old(struct scsi_host_template *sht)
1361 {
1362         if (nsp_detect(sht) == NULL) {
1363                 return 0;
1364         } else {
1365                 //MOD_INC_USE_COUNT;
1366                 return 1;
1367         }
1368 }
1369
1370
1371 static int nsp_release_old(struct Scsi_Host *shpnt)
1372 {
1373         //nsp_hw_data *data = (nsp_hw_data *)shpnt->hostdata;
1374
1375         /* PCMCIA Card Service dose same things below. */
1376         /* So we do nothing.                           */
1377         //if (shpnt->irq) {
1378         //      free_irq(shpnt->irq, data->ScsiInfo);
1379         //}
1380         //if (shpnt->io_port) {
1381         //      release_region(shpnt->io_port, shpnt->n_io_port);
1382         //}
1383
1384         //MOD_DEC_USE_COUNT;
1385
1386         return 0;
1387 }
1388 #endif
1389
1390 /*----------------------------------------------------------------*/
1391 /* return info string                                             */
1392 /*----------------------------------------------------------------*/
1393 static const char *nsp_info(struct Scsi_Host *shpnt)
1394 {
1395         nsp_hw_data *data = (nsp_hw_data *)shpnt->hostdata;
1396
1397         return data->nspinfo;
1398 }
1399
1400 #undef SPRINTF
1401 #define SPRINTF(args...) \
1402         do { \
1403                 if(length > (pos - buffer)) { \
1404                         pos += snprintf(pos, length - (pos - buffer) + 1, ## args); \
1405                         nsp_dbg(NSP_DEBUG_PROC, "buffer=0x%p pos=0x%p length=%d %d\n", buffer, pos, length,  length - (pos - buffer));\
1406                 } \
1407         } while(0)
1408 static int
1409 nsp_proc_info(
1410 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1411         struct Scsi_Host *host,
1412 #endif
1413         char  *buffer,
1414         char **start,
1415         off_t  offset,
1416         int    length,
1417 #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1418         int    hostno,
1419 #endif
1420         int    inout)
1421 {
1422         int id;
1423         char *pos = buffer;
1424         int thislength;
1425         int speed;
1426         unsigned long flags;
1427         nsp_hw_data *data;
1428 #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1429         struct Scsi_Host *host;
1430 #else
1431         int hostno;
1432 #endif
1433         if (inout) {
1434                 return -EINVAL;
1435         }
1436
1437 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1438         hostno = host->host_no;
1439 #else
1440         /* search this HBA host */
1441         host = scsi_host_hn_get(hostno);
1442         if (host == NULL) {
1443                 return -ESRCH;
1444         }
1445 #endif
1446         data = (nsp_hw_data *)host->hostdata;
1447
1448
1449         SPRINTF("NinjaSCSI status\n\n");
1450         SPRINTF("Driver version:        $Revision: 1.23 $\n");
1451         SPRINTF("SCSI host No.:         %d\n",          hostno);
1452         SPRINTF("IRQ:                   %d\n",          host->irq);
1453         SPRINTF("IO:                    0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
1454         SPRINTF("MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
1455         SPRINTF("sg_tablesize:          %d\n",          host->sg_tablesize);
1456
1457         SPRINTF("burst transfer mode:   ");
1458         switch (nsp_burst_mode) {
1459         case BURST_IO8:
1460                 SPRINTF("io8");
1461                 break;
1462         case BURST_IO32:
1463                 SPRINTF("io32");
1464                 break;
1465         case BURST_MEM32:
1466                 SPRINTF("mem32");
1467                 break;
1468         default:
1469                 SPRINTF("???");
1470                 break;
1471         }
1472         SPRINTF("\n");
1473
1474
1475         spin_lock_irqsave(&(data->Lock), flags);
1476         SPRINTF("CurrentSC:             0x%p\n\n",      data->CurrentSC);
1477         spin_unlock_irqrestore(&(data->Lock), flags);
1478
1479         SPRINTF("SDTR status\n");
1480         for(id = 0; id < ARRAY_SIZE(data->Sync); id++) {
1481
1482                 SPRINTF("id %d: ", id);
1483
1484                 if (id == host->this_id) {
1485                         SPRINTF("----- NinjaSCSI-3 host adapter\n");
1486                         continue;
1487                 }
1488
1489                 switch(data->Sync[id].SyncNegotiation) {
1490                 case SYNC_OK:
1491                         SPRINTF(" sync");
1492                         break;
1493                 case SYNC_NG:
1494                         SPRINTF("async");
1495                         break;
1496                 case SYNC_NOT_YET:
1497                         SPRINTF(" none");
1498                         break;
1499                 default:
1500                         SPRINTF("?????");
1501                         break;
1502                 }
1503
1504                 if (data->Sync[id].SyncPeriod != 0) {
1505                         speed = 1000000 / (data->Sync[id].SyncPeriod * 4);
1506
1507                         SPRINTF(" transfer %d.%dMB/s, offset %d",
1508                                 speed / 1000,
1509                                 speed % 1000,
1510                                 data->Sync[id].SyncOffset
1511                                 );
1512                 }
1513                 SPRINTF("\n");
1514         }
1515
1516         thislength = pos - (buffer + offset);
1517
1518         if(thislength < 0) {
1519                 *start = NULL;
1520                 return 0;
1521         }
1522
1523
1524         thislength = min(thislength, length);
1525         *start = buffer + offset;
1526
1527         return thislength;
1528 }
1529 #undef SPRINTF
1530
1531 /*---------------------------------------------------------------*/
1532 /* error handler                                                 */
1533 /*---------------------------------------------------------------*/
1534
1535 /*
1536 static int nsp_eh_abort(Scsi_Cmnd *SCpnt)
1537 {
1538         nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
1539
1540         return nsp_eh_bus_reset(SCpnt);
1541 }*/
1542
1543 static int nsp_bus_reset(nsp_hw_data *data)
1544 {
1545         unsigned int base = data->BaseAddress;
1546         int          i;
1547
1548         nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
1549
1550         nsp_index_write(base, SCSIBUSCTRL, SCSI_RST);
1551         mdelay(100); /* 100ms */
1552         nsp_index_write(base, SCSIBUSCTRL, 0);
1553         for(i = 0; i < 5; i++) {
1554                 nsp_index_read(base, IRQPHASESENCE); /* dummy read */
1555         }
1556
1557         nsphw_init_sync(data);
1558
1559         nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
1560
1561         return SUCCESS;
1562 }
1563
1564 static int nsp_eh_bus_reset(Scsi_Cmnd *SCpnt)
1565 {
1566         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
1567
1568         nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
1569
1570         return nsp_bus_reset(data);
1571 }
1572
1573 static int nsp_eh_host_reset(Scsi_Cmnd *SCpnt)
1574 {
1575         nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
1576
1577         nsp_dbg(NSP_DEBUG_BUSRESET, "in");
1578
1579         nsphw_init(data);
1580
1581         return SUCCESS;
1582 }
1583
1584
1585 /**********************************************************************
1586   PCMCIA functions
1587 **********************************************************************/
1588
1589 /*======================================================================
1590     nsp_cs_attach() creates an "instance" of the driver, allocating
1591     local data structures for one device.  The device is registered
1592     with Card Services.
1593
1594     The dev_link structure is initialized, but we don't actually
1595     configure the card at this point -- we wait until we receive a
1596     card insertion event.
1597 ======================================================================*/
1598 static dev_link_t *nsp_cs_attach(void)
1599 {
1600         scsi_info_t  *info;
1601         client_reg_t  client_reg;
1602         dev_link_t   *link;
1603         int           ret;
1604         nsp_hw_data  *data = &nsp_data_base;
1605
1606         nsp_dbg(NSP_DEBUG_INIT, "in");
1607
1608         /* Create new SCSI device */
1609         info = kmalloc(sizeof(*info), GFP_KERNEL);
1610         if (info == NULL) { return NULL; }
1611         memset(info, 0, sizeof(*info));
1612         link = &info->link;
1613         link->priv = info;
1614         data->ScsiInfo = info;
1615
1616         nsp_dbg(NSP_DEBUG_INIT, "info=0x%p", info);
1617
1618         /* The io structure describes IO port mapping */
1619         link->io.NumPorts1       = 0x10;
1620         link->io.Attributes1     = IO_DATA_PATH_WIDTH_AUTO;
1621         link->io.IOAddrLines     = 10;  /* not used */
1622
1623         /* Interrupt setup */
1624         link->irq.Attributes     = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1625         link->irq.IRQInfo1       = IRQ_LEVEL_ID;
1626
1627         /* Interrupt handler */
1628         link->irq.Handler        = &nspintr;
1629         link->irq.Instance       = info;
1630         link->irq.Attributes     |= (SA_SHIRQ | SA_SAMPLE_RANDOM);
1631
1632         /* General socket configuration */
1633         link->conf.Attributes    = CONF_ENABLE_IRQ;
1634         link->conf.Vcc           = 50;
1635         link->conf.IntType       = INT_MEMORY_AND_IO;
1636         link->conf.Present       = PRESENT_OPTION;
1637
1638
1639         /* Register with Card Services */
1640         link->next               = NULL;
1641         client_reg.dev_info      = &dev_info;
1642         client_reg.Version       = 0x0210;
1643         client_reg.event_callback_args.client_data = link;
1644         ret = pcmcia_register_client(&link->handle, &client_reg);
1645         if (ret != CS_SUCCESS) {
1646                 cs_error(link->handle, RegisterClient, ret);
1647                 nsp_cs_detach(link->handle);
1648                 return NULL;
1649         }
1650
1651
1652         nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
1653         return link;
1654 } /* nsp_cs_attach */
1655
1656
1657 /*======================================================================
1658     This deletes a driver "instance".  The device is de-registered
1659     with Card Services.  If it has been released, all local data
1660     structures are freed.  Otherwise, the structures will be freed
1661     when the device is released.
1662 ======================================================================*/
1663 static void nsp_cs_detach(struct pcmcia_device *p_dev)
1664 {
1665         dev_link_t *link = dev_to_instance(p_dev);
1666
1667         nsp_dbg(NSP_DEBUG_INIT, "in, link=0x%p", link);
1668
1669         if (link->state & DEV_CONFIG) {
1670                 ((scsi_info_t *)link->priv)->stop = 1;
1671                 nsp_cs_release(link);
1672         }
1673
1674         kfree(link->priv);
1675         link->priv = NULL;
1676 } /* nsp_cs_detach */
1677
1678
1679 /*======================================================================
1680     nsp_cs_config() is scheduled to run after a CARD_INSERTION event
1681     is received, to configure the PCMCIA socket, and to make the
1682     ethernet device available to the system.
1683 ======================================================================*/
1684 #define CS_CHECK(fn, ret) \
1685 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
1686 /*====================================================================*/
1687 static void nsp_cs_config(dev_link_t *link)
1688 {
1689         client_handle_t   handle = link->handle;
1690         scsi_info_t      *info   = link->priv;
1691         tuple_t           tuple;
1692         cisparse_t        parse;
1693         int               last_ret, last_fn;
1694         unsigned char     tuple_data[64];
1695         config_info_t     conf;
1696         win_req_t         req;
1697         memreq_t          map;
1698         cistpl_cftable_entry_t dflt = { 0 };
1699         struct Scsi_Host *host;
1700         nsp_hw_data      *data = &nsp_data_base;
1701 #if !(LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,74))
1702         struct scsi_device       *dev;
1703         dev_node_t      **tail, *node;
1704 #endif
1705
1706         nsp_dbg(NSP_DEBUG_INIT, "in");
1707
1708         tuple.DesiredTuple    = CISTPL_CONFIG;
1709         tuple.Attributes      = 0;
1710         tuple.TupleData       = tuple_data;
1711         tuple.TupleDataMax    = sizeof(tuple_data);
1712         tuple.TupleOffset     = 0;
1713         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
1714         CS_CHECK(GetTupleData,  pcmcia_get_tuple_data(handle, &tuple));
1715         CS_CHECK(ParseTuple,    pcmcia_parse_tuple(handle, &tuple, &parse));
1716         link->conf.ConfigBase = parse.config.base;
1717         link->conf.Present    = parse.config.rmask[0];
1718
1719         /* Configure card */
1720         link->state           |= DEV_CONFIG;
1721
1722         /* Look up the current Vcc */
1723         CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
1724         link->conf.Vcc = conf.Vcc;
1725
1726         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
1727         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
1728         while (1) {
1729                 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
1730
1731                 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
1732                                 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
1733                         goto next_entry;
1734
1735                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) { dflt = *cfg; }
1736                 if (cfg->index == 0) { goto next_entry; }
1737                 link->conf.ConfigIndex = cfg->index;
1738
1739                 /* Does this card need audio output? */
1740                 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
1741                         link->conf.Attributes |= CONF_ENABLE_SPKR;
1742                         link->conf.Status = CCSR_AUDIO_ENA;
1743                 }
1744
1745                 /* Use power settings for Vcc and Vpp if present */
1746                 /*  Note that the CIS values need to be rescaled */
1747                 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
1748                         if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM]/10000) {
1749                                 goto next_entry;
1750                         }
1751                 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
1752                         if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM]/10000) {
1753                                 goto next_entry;
1754                         }
1755                 }
1756
1757                 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM)) {
1758                         link->conf.Vpp1 = link->conf.Vpp2 =
1759                                 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
1760                 } else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM)) {
1761                         link->conf.Vpp1 = link->conf.Vpp2 =
1762                                 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
1763                 }
1764
1765                 /* Do we need to allocate an interrupt? */
1766                 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1) {
1767                         link->conf.Attributes |= CONF_ENABLE_IRQ;
1768                 }
1769
1770                 /* IO window settings */
1771                 link->io.NumPorts1 = link->io.NumPorts2 = 0;
1772                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
1773                         cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
1774                         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
1775                         if (!(io->flags & CISTPL_IO_8BIT))
1776                                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
1777                         if (!(io->flags & CISTPL_IO_16BIT))
1778                                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1779                         link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
1780                         link->io.BasePort1 = io->win[0].base;
1781                         link->io.NumPorts1 = io->win[0].len;
1782                         if (io->nwin > 1) {
1783                                 link->io.Attributes2 = link->io.Attributes1;
1784                                 link->io.BasePort2 = io->win[1].base;
1785                                 link->io.NumPorts2 = io->win[1].len;
1786                         }
1787                         /* This reserves IO space but doesn't actually enable it */
1788                         if (pcmcia_request_io(link->handle, &link->io) != 0)
1789                                 goto next_entry;
1790                 }
1791
1792                 if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) {
1793                         cistpl_mem_t *mem =
1794                                 (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
1795                         req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM;
1796                         req.Attributes |= WIN_ENABLE;
1797                         req.Base = mem->win[0].host_addr;
1798                         req.Size = mem->win[0].len;
1799                         if (req.Size < 0x1000) {
1800                                 req.Size = 0x1000;
1801                         }
1802                         req.AccessSpeed = 0;
1803                         if (pcmcia_request_window(&link->handle, &req, &link->win) != 0)
1804                                 goto next_entry;
1805                         map.Page = 0; map.CardOffset = mem->win[0].card_addr;
1806                         if (pcmcia_map_mem_page(link->win, &map) != 0)
1807                                 goto next_entry;
1808
1809                         data->MmioAddress = (unsigned long)ioremap_nocache(req.Base, req.Size);
1810                         data->MmioLength  = req.Size;
1811                 }
1812                 /* If we got this far, we're cool! */
1813                 break;
1814
1815         next_entry:
1816                 nsp_dbg(NSP_DEBUG_INIT, "next");
1817
1818                 if (link->io.NumPorts1) {
1819                         pcmcia_release_io(link->handle, &link->io);
1820                 }
1821                 CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
1822         }
1823
1824         if (link->conf.Attributes & CONF_ENABLE_IRQ) {
1825                 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
1826         }
1827         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
1828
1829         if (free_ports) {
1830                 if (link->io.BasePort1) {
1831                         release_region(link->io.BasePort1, link->io.NumPorts1);
1832                 }
1833                 if (link->io.BasePort2) {
1834                         release_region(link->io.BasePort2, link->io.NumPorts2);
1835                 }
1836         }
1837
1838         /* Set port and IRQ */
1839         data->BaseAddress = link->io.BasePort1;
1840         data->NumAddress  = link->io.NumPorts1;
1841         data->IrqNumber   = link->irq.AssignedIRQ;
1842
1843         nsp_dbg(NSP_DEBUG_INIT, "I/O[0x%x+0x%x] IRQ %d",
1844                 data->BaseAddress, data->NumAddress, data->IrqNumber);
1845
1846         if(nsphw_init(data) == FALSE) {
1847                 goto cs_failed;
1848         }
1849
1850 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,2))
1851         host = nsp_detect(&nsp_driver_template);
1852 #else
1853         scsi_register_host(&nsp_driver_template);
1854         for (host = scsi_host_get_next(NULL); host != NULL;
1855              host = scsi_host_get_next(host)) {
1856                 if (host->hostt == &nsp_driver_template) {
1857                         break;
1858                 }
1859         }
1860 #endif
1861
1862         if (host == NULL) {
1863                 nsp_dbg(NSP_DEBUG_INIT, "detect failed");
1864                 goto cs_failed;
1865         }
1866
1867
1868 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,74))
1869         scsi_add_host (host, NULL);
1870         scsi_scan_host(host);
1871
1872         snprintf(info->node.dev_name, sizeof(info->node.dev_name), "scsi%d", host->host_no);
1873         link->dev  = &info->node;
1874         info->host = host;
1875
1876 #else
1877         nsp_dbg(NSP_DEBUG_INIT, "GET_SCSI_INFO");
1878         tail = &link->dev;
1879         info->ndev = 0;
1880
1881         nsp_dbg(NSP_DEBUG_INIT, "host=0x%p", host);
1882
1883         for (dev = host->host_queue; dev != NULL; dev = dev->next) {
1884                 unsigned long id;
1885                 id = (dev->id & 0x0f) + ((dev->lun & 0x0f) << 4) +
1886                         ((dev->channel & 0x0f) << 8) +
1887                         ((dev->host->host_no & 0x0f) << 12);
1888                 node = &info->node[info->ndev];
1889                 node->minor = 0;
1890                 switch (dev->type) {
1891                 case TYPE_TAPE:
1892                         node->major = SCSI_TAPE_MAJOR;
1893                         snprintf(node->dev_name, sizeof(node->dev_name), "st#%04lx", id);
1894                         break;
1895                 case TYPE_DISK:
1896                 case TYPE_MOD:
1897                         node->major = SCSI_DISK0_MAJOR;
1898                         snprintf(node->dev_name, sizeof(node->dev_name), "sd#%04lx", id);
1899                         break;
1900                 case TYPE_ROM:
1901                 case TYPE_WORM:
1902                         node->major = SCSI_CDROM_MAJOR;
1903                         snprintf(node->dev_name, sizeof(node->dev_name), "sr#%04lx", id);
1904                         break;
1905                 default:
1906                         node->major = SCSI_GENERIC_MAJOR;
1907                         snprintf(node->dev_name, sizeof(node->dev_name), "sg#%04lx", id);
1908                         break;
1909                 }
1910                 *tail = node; tail = &node->next;
1911                 info->ndev++;
1912                 info->host = dev->host;
1913         }
1914
1915         *tail = NULL;
1916         if (info->ndev == 0) {
1917                 nsp_msg(KERN_INFO, "no SCSI devices found");
1918         }
1919         nsp_dbg(NSP_DEBUG_INIT, "host=0x%p", host);
1920 #endif
1921
1922         /* Finally, report what we've done */
1923         printk(KERN_INFO "nsp_cs: index 0x%02x: Vcc %d.%d",
1924                link->conf.ConfigIndex,
1925                link->conf.Vcc/10, link->conf.Vcc%10);
1926         if (link->conf.Vpp1) {
1927                 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
1928         }
1929         if (link->conf.Attributes & CONF_ENABLE_IRQ) {
1930                 printk(", irq %d", link->irq.AssignedIRQ);
1931         }
1932         if (link->io.NumPorts1) {
1933                 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
1934                        link->io.BasePort1+link->io.NumPorts1-1);
1935         }
1936         if (link->io.NumPorts2)
1937                 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
1938                        link->io.BasePort2+link->io.NumPorts2-1);
1939         if (link->win)
1940                 printk(", mem 0x%06lx-0x%06lx", req.Base,
1941                        req.Base+req.Size-1);
1942         printk("\n");
1943
1944         link->state &= ~DEV_CONFIG_PENDING;
1945         return;
1946
1947  cs_failed:
1948         nsp_dbg(NSP_DEBUG_INIT, "config fail");
1949         cs_error(link->handle, last_fn, last_ret);
1950         nsp_cs_release(link);
1951
1952         return;
1953 } /* nsp_cs_config */
1954 #undef CS_CHECK
1955
1956
1957 /*======================================================================
1958     After a card is removed, nsp_cs_release() will unregister the net
1959     device, and release the PCMCIA configuration.  If the device is
1960     still open, this will be postponed until it is closed.
1961 ======================================================================*/
1962 static void nsp_cs_release(dev_link_t *link)
1963 {
1964         scsi_info_t *info = link->priv;
1965         nsp_hw_data *data = NULL;
1966
1967         if (info->host == NULL) {
1968                 nsp_msg(KERN_DEBUG, "unexpected card release call.");
1969         } else {
1970                 data = (nsp_hw_data *)info->host->hostdata;
1971         }
1972
1973         nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
1974
1975         /* Unlink the device chain */
1976 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,2))
1977         if (info->host != NULL) {
1978                 scsi_remove_host(info->host);
1979         }
1980 #else
1981         scsi_unregister_host(&nsp_driver_template);
1982 #endif
1983         link->dev = NULL;
1984
1985         if (link->win) {
1986                 if (data != NULL) {
1987                         iounmap((void *)(data->MmioAddress));
1988                 }
1989                 pcmcia_release_window(link->win);
1990         }
1991         pcmcia_release_configuration(link->handle);
1992         if (link->io.NumPorts1) {
1993                 pcmcia_release_io(link->handle, &link->io);
1994         }
1995         if (link->irq.AssignedIRQ) {
1996                 pcmcia_release_irq(link->handle, &link->irq);
1997         }
1998         link->state &= ~DEV_CONFIG;
1999 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,2))
2000         if (info->host != NULL) {
2001                 scsi_host_put(info->host);
2002         }
2003 #endif
2004 } /* nsp_cs_release */
2005
2006 static int nsp_cs_suspend(struct pcmcia_device *dev)
2007 {
2008         dev_link_t *link = dev_to_instance(dev);
2009         scsi_info_t *info = link->priv;
2010         nsp_hw_data *data;
2011
2012         link->state |= DEV_SUSPEND;
2013
2014         nsp_dbg(NSP_DEBUG_INIT, "event: suspend");
2015
2016         if (info->host != NULL) {
2017                 nsp_msg(KERN_INFO, "clear SDTR status");
2018
2019                 data = (nsp_hw_data *)info->host->hostdata;
2020
2021                 nsphw_init_sync(data);
2022         }
2023
2024         info->stop = 1;
2025
2026         if (link->state & DEV_CONFIG)
2027                 pcmcia_release_configuration(link->handle);
2028
2029         return 0;
2030 }
2031
2032 static int nsp_cs_resume(struct pcmcia_device *dev)
2033 {
2034         dev_link_t *link = dev_to_instance(dev);
2035         scsi_info_t *info = link->priv;
2036         nsp_hw_data *data;
2037
2038         nsp_dbg(NSP_DEBUG_INIT, "event: resume");
2039
2040         link->state &= ~DEV_SUSPEND;
2041
2042         if (link->state & DEV_CONFIG)
2043                 pcmcia_request_configuration(link->handle, &link->conf);
2044
2045         info->stop = 0;
2046
2047         if (info->host != NULL) {
2048                 nsp_msg(KERN_INFO, "reset host and bus");
2049
2050                 data = (nsp_hw_data *)info->host->hostdata;
2051
2052                 nsphw_init   (data);
2053                 nsp_bus_reset(data);
2054         }
2055
2056         return 0;
2057 }
2058
2059 /*======================================================================
2060
2061     The card status event handler.  Mostly, this schedules other
2062     stuff to run after an event is received.  A CARD_REMOVAL event
2063     also sets some flags to discourage the net drivers from trying
2064     to talk to the card any more.
2065
2066     When a CARD_REMOVAL event is received, we immediately set a flag
2067     to block future accesses to this device.  All the functions that
2068     actually access the device should check this flag to make sure
2069     the card is still present.
2070
2071 ======================================================================*/
2072 static int nsp_cs_event(event_t                event,
2073                         int                    priority,
2074                         event_callback_args_t *args)
2075 {
2076         dev_link_t  *link = args->client_data;
2077
2078         nsp_dbg(NSP_DEBUG_INIT, "in, event=0x%08x", event);
2079
2080         switch (event) {
2081         case CS_EVENT_CARD_INSERTION:
2082                 nsp_dbg(NSP_DEBUG_INIT, "event: insert");
2083                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
2084 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,68))
2085                 info->bus    =  args->bus;
2086 #endif
2087                 nsp_cs_config(link);
2088                 break;
2089         default:
2090                 nsp_dbg(NSP_DEBUG_INIT, "event: unknown");
2091                 break;
2092         }
2093         nsp_dbg(NSP_DEBUG_INIT, "end");
2094         return 0;
2095 } /* nsp_cs_event */
2096
2097 /*======================================================================*
2098  *      module entry point
2099  *====================================================================*/
2100 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,68))
2101 static struct pcmcia_device_id nsp_cs_ids[] = {
2102         PCMCIA_DEVICE_PROD_ID123("IO DATA", "CBSC16       ", "1", 0x547e66dc, 0x0d63a3fd, 0x51de003a),
2103         PCMCIA_DEVICE_PROD_ID123("KME    ", "SCSI-CARD-001", "1", 0x534c02bc, 0x52008408, 0x51de003a),
2104         PCMCIA_DEVICE_PROD_ID123("KME    ", "SCSI-CARD-002", "1", 0x534c02bc, 0xcb09d5b2, 0x51de003a),
2105         PCMCIA_DEVICE_PROD_ID123("KME    ", "SCSI-CARD-003", "1", 0x534c02bc, 0xbc0ee524, 0x51de003a),
2106         PCMCIA_DEVICE_PROD_ID123("KME    ", "SCSI-CARD-004", "1", 0x534c02bc, 0x226a7087, 0x51de003a),
2107         PCMCIA_DEVICE_PROD_ID123("WBT", "NinjaSCSI-3", "R1.0", 0xc7ba805f, 0xfdc7c97d, 0x6973710e),
2108         PCMCIA_DEVICE_PROD_ID123("WORKBIT", "UltraNinja-16", "1", 0x28191418, 0xb70f4b09, 0x51de003a),
2109         PCMCIA_DEVICE_NULL
2110 };
2111 MODULE_DEVICE_TABLE(pcmcia, nsp_cs_ids);
2112
2113 static struct pcmcia_driver nsp_driver = {
2114         .owner          = THIS_MODULE,
2115         .drv            = {
2116                 .name   = "nsp_cs",
2117         },
2118         .attach         = nsp_cs_attach,
2119         .event          = nsp_cs_event,
2120         .remove         = nsp_cs_detach,
2121         .id_table       = nsp_cs_ids,
2122         .suspend        = nsp_cs_suspend,
2123         .resume         = nsp_cs_resume,
2124 };
2125 #endif
2126
2127 static int __init nsp_cs_init(void)
2128 {
2129 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,68))
2130         nsp_msg(KERN_INFO, "loading...");
2131
2132         return pcmcia_register_driver(&nsp_driver);
2133 #else
2134         servinfo_t serv;
2135
2136         nsp_msg(KERN_INFO, "loading...");
2137         pcmcia_get_card_services_info(&serv);
2138         if (serv.Revision != CS_RELEASE_CODE) {
2139                 nsp_msg(KERN_DEBUG, "Card Services release does not match!");
2140                 return -EINVAL;
2141         }
2142         register_pcmcia_driver(&dev_info, &nsp_cs_attach, &nsp_cs_detach);
2143
2144         nsp_dbg(NSP_DEBUG_INIT, "out");
2145         return 0;
2146 #endif
2147 }
2148
2149 static void __exit nsp_cs_exit(void)
2150 {
2151         nsp_msg(KERN_INFO, "unloading...");
2152
2153 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,68))
2154         pcmcia_unregister_driver(&nsp_driver);
2155 #else
2156         unregister_pcmcia_driver(&dev_info);
2157         /* XXX: this really needs to move into generic code.. */
2158         while (dev_list != NULL) {
2159                 if (dev_list->state & DEV_CONFIG) {
2160                         nsp_cs_release(dev_list);
2161                 }
2162                 nsp_cs_detach(dev_list);
2163         }
2164 #endif
2165 }
2166
2167
2168 module_init(nsp_cs_init)
2169 module_exit(nsp_cs_exit)
2170
2171 /* end */