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