1#include <linux/module.h>
2#include <linux/delay.h>
3#include <linux/signal.h>
4#include <linux/sched.h>
5#include <linux/errno.h>
6#include <linux/pci.h>
7#include <linux/string.h>
8#include <linux/blk.h>
9#include <linux/init.h>
10#include <linux/spinlock.h>
11
12#include <asm/io.h>
13#include <asm/system.h>
14
15#include "scsi.h"
16#include "hosts.h"
17#include "AM53C974.h"
18#include "constants.h"
19#include "sd.h"
20
21/* AM53/79C974 (PCscsi) driver release 0.5
22
23 * The architecture and much of the code of this device
24 * driver was originally developed by Drew Eckhardt for
25 * the NCR5380. The following copyrights apply:
26 *  For the architecture and all pieces of code which can also be found
27 *    in the NCR5380 device driver:
28 *   Copyright 1993, Drew Eckhardt
29 *      Visionary Computing
30 *      (Unix and Linux consulting and custom programming)
31 *      drew@colorado.edu
32 *      +1 (303) 666-5836
33 *
34 *  The AM53C974_nobios_detect code was originally developed by
35 *   Robin Cutshaw (robin@xfree86.org) and is used here in a
36 *   slightly modified form.
37 *
38 *  PCI detection rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
39 *
40 *  For the remaining code:
41 *    Copyright 1994, D. Frieauff
42 *    EMail: fri@rsx42sun0.dofn.de
43 *    Phone: x49-7545-8-2256 , x49-7541-42305
44 */
45
46/*
47 * $Log: AM53C974.c,v $
48 * Revision 1.1.1.1  2008/10/15 03:26:47  james26_jang
49 * Initial.
50 *
51 * Revision 1.1.1.1  2008/07/21 09:15:20  james26_jang
52 * New UI, New QoS, New wireless driver(4.151.10.29), ipmonitor.
53 *
54 * Revision 1.1.1.1  2008/07/02 14:39:34  james26_jang
55 * 4.100.10.29, New QoS and New UI.
56 *
57 * Revision 1.1.1.1  2003/02/03 22:37:51  mhuang
58 * LINUX_2_4 branch snapshot from linux-mips.org CVS
59 *
60 */
61
62#ifdef AM53C974_DEBUG
63#define DEB(x) x
64#ifdef AM53C974_DEBUG_KEYWAIT
65#define KEYWAIT() AM53C974_keywait()
66#else
67#define KEYWAIT()
68#endif
69#ifdef AM53C974_DEBUG_INIT
70#define DEB_INIT(x) x
71#else
72#define DEB_INIT(x)
73#endif
74#ifdef AM53C974_DEBUG_MSG
75#define DEB_MSG(x) x
76#else
77#define DEB_MSG(x)
78#endif
79#ifdef AM53C974_DEB_RESEL
80#define DEB_RESEL(x) x
81#else
82#define DEB_RESEL(x)
83#endif
84#ifdef AM53C974_DEBUG_QUEUE
85#define DEB_QUEUE(x) x
86#define LIST(x,y) {printk("LINE:%d   Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
87#define REMOVE(w,x,y,z) {printk("LINE:%d   Removing: %p->%p  %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
88#else
89#define DEB_QUEUE(x)
90#define LIST(x,y)
91#define REMOVE(w,x,y,z)
92#endif
93#ifdef AM53C974_DEBUG_INFO
94#define DEB_INFO(x) x
95#else
96#define DEB_INFO(x)
97#endif
98#ifdef AM53C974_DEBUG_LINKED
99#define DEB_LINKED(x) x
100#else
101#define DEB_LINKED(x)
102#endif
103#ifdef AM53C974_DEBUG_INTR
104#define DEB_INTR(x) x
105#else
106#define DEB_INTR(x)
107#endif
108#else
109#define DEB_INIT(x)
110#define DEB(x)
111#define DEB_QUEUE(x)
112#define LIST(x,y)
113#define REMOVE(w,x,y,z)
114#define DEB_INFO(x)
115#define DEB_LINKED(x)
116#define DEB_INTR(x)
117#define DEB_MSG(x)
118#define DEB_RESEL(x)
119#define KEYWAIT()
120#endif
121#ifdef AM53C974_DEBUG_ABORT
122#define DEB_ABORT(x) x
123#else
124#define DEB_ABORT(x)
125#endif
126
127#ifdef VERBOSE_AM53C974_DEBUG
128#define VDEB(x) x
129#else
130#define VDEB(x)
131#endif
132
133#define INSIDE(x,l,h) ( ((x) >= (l)) && ((x) <= (h)) )
134
135
136#include <scsi/scsicam.h>
137
138/***************************************************************************************
139* Default setting of the controller's SCSI id. Edit and uncomment this only if your    *
140* BIOS does not correctly initialize the controller's SCSI id.                         *
141* If you don't get a warning during boot, it is correctly initialized.                 *
142****************************************************************************************/
143/* #define AM53C974_SCSI_ID 7 */
144
145/***************************************************************************************
146* Default settings for sync. negotiation enable, transfer rate and sync. offset.       *
147* These settings can be replaced by LILO overrides (append) with the following syntax:          *
148* AM53C974=host-scsi-id, target-scsi-id, max-rate, max-offset                          *
149* Sync. negotiation is disabled by default and will be enabled for those targets which *
150* are specified in the LILO override                                                   *
151****************************************************************************************/
152#define DEFAULT_SYNC_NEGOTIATION_ENABLED 0	/* 0 or 1 */
153#define DEFAULT_RATE			 5	/* MHz, min: 3; max: 10 */
154#define DEFAULT_SYNC_OFFSET		 0	/* bytes, min: 0; max: 15; use 0 for async. mode */
155
156/***************************************************************************************
157* If defined, don't allow targets to disconnect during commands.  This will reduce     *
158* performance, but may be worthwhile if you suspect the driver of corrupting data when *
159* a disconnect happens.                                                                *
160***************************************************************************************/
161#define AM53C974_PROHIBIT_DISCONNECT
162
163/* --------------------- don't edit below here  --------------------- */
164
165#define AM53C974_DRIVER_REVISION_MAJOR 0
166#define AM53C974_DRIVER_REVISION_MINOR 5
167#define SEPARATOR_LINE  \
168"--------------------------------------------------------------------------\n"
169
170/* debug control */
171/* #define AM53C974_DEBUG */
172/* #define AM53C974_DEBUG_MSG */
173/* #define AM53C974_DEBUG_KEYWAIT */
174/* #define AM53C974_DEBUG_INIT */
175/* #define AM53C974_DEBUG_QUEUE */
176/* #define AM53C974_DEBUG_INFO */
177/* #define AM53C974_DEBUG_LINKED */
178/* #define VERBOSE_AM53C974_DEBUG */
179/* #define AM53C974_DEBUG_INTR */
180/* #define AM53C974_DEB_RESEL */
181#define AM53C974_DEBUG_ABORT
182/* #define AM53C974_OPTION_DEBUG_PROBE_ONLY */
183
184/* special options/constants */
185#define DEF_CLK                 40	/* chip clock freq. in MHz */
186#define MIN_PERIOD               4	/* for negotiation: min. number of clocks per cycle */
187#define MAX_PERIOD              13	/* for negotiation: max. number of clocks per cycle */
188#define MAX_OFFSET              15	/* for negotiation: max. offset (0=async) */
189
190#define DEF_SCSI_TIMEOUT        245	/* STIMREG value, 40 Mhz */
191#define DEF_STP                 8	/* STPREG value assuming 5.0 MB/sec, FASTCLK, FASTSCSI */
192#define DEF_SOF_RAD             0	/* REQ/ACK deassertion delay */
193#define DEF_SOF_RAA             0	/* REQ/ACK assertion delay */
194#define DEF_ETM                 0	/* CNTLREG1, ext. timing mode */
195#define DEF_PERE                1	/* CNTLREG1, parity error reporting */
196#define DEF_CLKF                0	/* CLKFREG,  0=40 Mhz */
197#define DEF_ENF                 1	/* CNTLREG2, enable features */
198#define DEF_ADIDCHK             0	/* CNTLREG3, additional ID check */
199#define DEF_FASTSCSI            1	/* CNTLREG3, fast SCSI */
200#define DEF_FASTCLK             1	/* CNTLREG3, fast clocking, 5 MB/sec at 40MHz chip clk */
201#define DEF_GLITCH              1	/* CNTLREG4, glitch eater, 0=12ns, 1=35ns, 2=25ns, 3=off */
202#define DEF_PWD                 0	/* CNTLREG4, reduced power feature */
203#define DEF_RAE                 0	/* CNTLREG4, RAE active negation on REQ, ACK only */
204#define DEF_RADE                1	/* 1CNTLREG4, active negation on REQ, ACK and data */
205
206/*** SCSI block ***/
207#define CTCLREG		    	0x00	/* r      current transf. count, low byte    */
208#define CTCMREG		   	0x04	/* r      current transf. count, middle byte */
209#define CTCHREG		    	0x38	/* r      current transf. count, high byte   */
210#define STCLREG		    	0x00	/* w      start transf. count, low byte      */
211#define STCMREG		    	0x04	/* w      start transf. count, middle byte   */
212#define STCHREG		    	0x38	/* w      start transf. count, high byte     */
213#define FFREG		    	0x08	/* rw     SCSI FIFO reg.                     */
214#define STIMREG		    	0x14	/* w      SCSI timeout reg.                  */
215
216#define SDIDREG		    	0x10	/* w      SCSI destination ID reg.           */
217#define SDIREG_MASK		0x07	/* mask                                      */
218
219#define STPREG		    	0x18	/* w      synchronous transf. period reg.    */
220#define STPREG_STP		0x1F	/* synchr. transfer period                   */
221
222#define CLKFREG		    	0x24	/* w      clock factor reg.                  */
223#define CLKFREG_MASK		0x07	/* mask                                      */
224
225#define CMDREG		    	0x0C	/* rw     SCSI command reg.                  */
226#define CMDREG_DMA         	0x80	/* set DMA mode (set together with opcodes below) */
227#define CMDREG_IT          	0x10	/* information transfer              */
228#define CMDREG_ICCS		0x11	/* initiator command complete steps          */
229#define CMDREG_MA		0x12	/* message accepted                          */
230#define CMDREG_TPB		0x98	/* transfer pad bytes, DMA mode only         */
231#define CMDREG_SATN		0x1A	/* set ATN                                   */
232#define CMDREG_RATN		0x1B	/* reset ATN                                 */
233#define CMDREG_SOAS		0x41	/* select without ATN steps                  */
234#define CMDREG_SAS		0x42	/* select with ATN steps (1 msg byte)        */
235#define CMDREG_SASS		0x43	/* select with ATN and stop steps            */
236#define CMDREG_ESR		0x44	/* enable selection/reselection      */
237#define CMDREG_DSR		0x45	/* disable selection/reselection     */
238#define CMDREG_SA3S		0x46	/* select with ATN 3 steps  (3 msg bytes)  */
239#define CMDREG_NOP		0x00	/* no operation                      */
240#define CMDREG_CFIFO		0x01	/* clear FIFO                                */
241#define CMDREG_RDEV		0x02	/* reset device                      */
242#define CMDREG_RBUS		0x03	/* reset SCSI bus                            */
243
244#define STATREG		    	0x10	/* r      SCSI status reg.                   */
245#define STATREG_INT		0x80	/* SCSI interrupt condition detected         */
246#define STATREG_IOE		0x40	/* SCSI illegal operation error detected   */
247#define STATREG_PE		0x20	/* SCSI parity error detected                */
248#define STATREG_CTZ		0x10	/* CTC reg decremented to zero               */
249#define STATREG_MSG		0x04	/* SCSI MSG phase (latched?)                 */
250#define STATREG_CD		0x02	/* SCSI C/D phase (latched?)                 */
251#define STATREG_IO		0x01	/* SCSI I/O phase (latched?)                 */
252#define STATREG_PHASE           0x07	/* SCSI phase mask                           */
253
254#define INSTREG		    	0x14	/* r      interrupt status reg.              */
255#define INSTREG_SRST		0x80	/* SCSI reset detected                       */
256#define INSTREG_ICMD		0x40	/* SCSI invalid command detected     */
257#define INSTREG_DIS		0x20	/* target disconnected or sel/resel timeout */
258#define INSTREG_SR		0x10	/* device on bus has service request       */
259#define INSTREG_SO		0x08	/* successful operation                      */
260#define INSTREG_RESEL		0x04	/* device reselected as initiator    */
261
262#define ISREG		    	0x18	/* r      internal state reg.                */
263#define ISREG_SOF		0x08	/* synchronous offset flag (act. low)        */
264#define ISREG_IS		0x07	/* status of intermediate op.                */
265#define ISREG_OK_NO_STOP        0x04	/* selection successful                    */
266#define ISREG_OK_STOP           0x01	/* selection successful                    */
267
268#define CFIREG		    	0x1C	/* r      current FIFO/internal state reg.   */
269#define CFIREG_IS		0xE0	/* status of intermediate op.                */
270#define CFIREG_CF		0x1F	/* number of bytes in SCSI FIFO              */
271
272#define SOFREG		    	0x1C	/* w      synchr. offset reg.                */
273#define SOFREG_RAD		0xC0	/* REQ/ACK deassertion delay (sync.)         */
274#define SOFREG_RAA		0x30	/* REQ/ACK assertion delay (sync.)           */
275#define SOFREG_SO		0x0F	/* synch. offset (sync.)             */
276
277#define CNTLREG1	    	0x20	/* rw     control register one               */
278#define CNTLREG1_ETM		0x80	/* set extended timing mode                  */
279#define CNTLREG1_DISR		0x40	/* disable interrupt on SCSI reset           */
280#define CNTLREG1_PERE		0x10	/* enable parity error reporting     */
281#define CNTLREG1_SID		0x07	/* host adapter SCSI ID                      */
282
283#define CNTLREG2	    	0x2C	/* rw     control register two               */
284#define CNTLREG2_ENF		0x40	/* enable features                           */
285
286#define CNTLREG3	    	0x30	/* rw     control register three             */
287#define CNTLREG3_ADIDCHK	0x80	/* additional ID check                       */
288#define CNTLREG3_FASTSCSI	0x10	/* fast SCSI                                 */
289#define CNTLREG3_FASTCLK	0x08	/* fast SCSI clocking                        */
290
291#define CNTLREG4	    	0x34	/* rw     control register four              */
292#define CNTLREG4_GLITCH		0xC0	/* glitch eater                              */
293#define CNTLREG4_PWD		0x20	/* reduced power feature             */
294#define CNTLREG4_RAE		0x08	/* write only, active negot. ctrl.           */
295#define CNTLREG4_RADE		0x04	/* active negot. ctrl.                       */
296#define CNTLREG4_RES		0x10	/* reserved bit, must be 1                   */
297
298/*** DMA block ***/
299#define DMACMD		    	0x40	/* rw     command                            */
300#define DMACMD_DIR		0x80	/* transfer direction (1=read from device) */
301#define DMACMD_INTE_D		0x40	/* DMA transfer interrupt enable     */
302#define DMACMD_INTE_P		0x20	/* page transfer interrupt enable            */
303#define DMACMD_MDL		0x10	/* map to memory descriptor list     */
304#define DMACMD_DIAG		0x04	/* diagnostics, set to 0             */
305#define DMACMD_IDLE 		0x00	/* idle cmd                                  */
306#define DMACMD_BLAST		0x01	/* flush FIFO to memory                      */
307#define DMACMD_ABORT		0x02	/* terminate DMA                     */
308#define DMACMD_START		0x03	/* start DMA                                 */
309
310#define DMASTATUS	      	0x54	/* r      status register                    */
311#define DMASTATUS_BCMPLT	0x20	/* BLAST complete                    */
312#define DMASTATUS_SCSIINT	0x10	/* SCSI interrupt pending            */
313#define DMASTATUS_DONE		0x08	/* DMA transfer terminated                   */
314#define DMASTATUS_ABORT		0x04	/* DMA transfer aborted                      */
315#define DMASTATUS_ERROR		0x02	/* DMA transfer error                        */
316#define DMASTATUS_PWDN		0x02	/* power down indicator                      */
317
318#define DMASTC		    	0x44	/* rw     starting transfer count            */
319#define DMASPA		    	0x48	/* rw     starting physical address          */
320#define DMAWBC		    	0x4C	/* r      working byte counter               */
321#define DMAWAC		    	0x50	/* r      working address counter            */
322#define DMASMDLA	    	0x58	/* rw     starting MDL address               */
323#define DMAWMAC		    	0x5C	/* r      working MDL counter                */
324
325/*** SCSI phases ***/
326#define PHASE_MSGIN             0x07
327#define PHASE_MSGOUT            0x06
328#define PHASE_RES_1             0x05
329#define PHASE_RES_0             0x04
330#define PHASE_STATIN            0x03
331#define PHASE_CMDOUT            0x02
332#define PHASE_DATAIN            0x01
333#define PHASE_DATAOUT           0x00
334
335
336#define AM53C974_local_declare()	unsigned long io_port
337#define AM53C974_setio(instance)	io_port = instance->io_port
338#define AM53C974_read_8(addr)           inb(io_port + (addr))
339#define AM53C974_write_8(addr,x)        outb((x), io_port + (addr))
340#define AM53C974_read_16(addr)          inw(io_port + (addr))
341#define AM53C974_write_16(addr,x)       outw((x), io_port + (addr))
342#define AM53C974_read_32(addr)          inl(io_port + (addr))
343#define AM53C974_write_32(addr,x)       outl((x), io_port + (addr))
344
345#define AM53C974_poll_int()             { do { statreg = AM53C974_read_8(STATREG); } \
346                                             while (!(statreg & STATREG_INT)) ; \
347                                          AM53C974_read_8(INSTREG) ; }	/* clear int */
348#define AM53C974_cfifo()		(AM53C974_read_8(CFIREG) & CFIREG_CF)
349
350/* These are "special" values for the tag parameter passed to AM53C974_select. */
351#define TAG_NEXT	-1	/* Use next free tag */
352#define TAG_NONE	-2	/* Establish I_T_L nexus instead of I_T_L_Q
353				   * even on SCSI-II devices */
354
355/************ LILO overrides *************/
356typedef struct _override_t {
357	int host_scsi_id;	/* SCSI id of the bus controller */
358	int target_scsi_id;	/* SCSI id of target */
359	int max_rate;		/* max. transfer rate */
360	int max_offset;		/* max. sync. offset, 0 = asynchronous */
361} override_t;
362
363
364#ifdef AM53C974_DEBUG
365static void AM53C974_print_phase(struct Scsi_Host *instance);
366static void AM53C974_print_queues(struct Scsi_Host *instance);
367#endif				/* AM53C974_DEBUG */
368static void AM53C974_print(struct Scsi_Host *instance);
369static void AM53C974_keywait(void);
370static __inline__ int AM53C974_pci_detect(Scsi_Host_Template * tpnt);
371static int AM53C974_init(Scsi_Host_Template * tpnt, struct pci_dev *pdev);
372static void AM53C974_config_after_reset(struct Scsi_Host *instance);
373static __inline__ void initialize_SCp(Scsi_Cmnd * cmd);
374static __inline__ void run_main(void);
375static void AM53C974_main(void);
376static void AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs);
377static void do_AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs);
378static void AM53C974_intr_disconnect(struct Scsi_Host *instance);
379static int AM53C974_sync_neg(struct Scsi_Host *instance, int target, unsigned char *msg);
380static __inline__ void AM53C974_set_async(struct Scsi_Host *instance, int target);
381static __inline__ void AM53C974_set_sync(struct Scsi_Host *instance, int target);
382static void AM53C974_information_transfer(struct Scsi_Host *instance,
383			      unsigned char statreg, unsigned char isreg,
384			      unsigned char instreg, unsigned char cfifo,
385					  unsigned char dmastatus);
386static int AM53C974_message(struct Scsi_Host *instance, Scsi_Cmnd * cmd, unsigned char msg);
387static void AM53C974_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag);
388static void AM53C974_intr_reselect(struct Scsi_Host *instance, unsigned char statreg);
389static __inline__ void AM53C974_transfer_dma(struct Scsi_Host *instance, short dir,
390				       unsigned long length, char *data);
391static void AM53C974_dma_blast(struct Scsi_Host *instance, unsigned char dmastatus,
392			       unsigned char statreg);
393static void AM53C974_intr_bus_reset(struct Scsi_Host *instance);
394
395static struct Scsi_Host *first_instance;
396static Scsi_Host_Template *the_template;
397static struct Scsi_Host *first_host;	/* Head of list of AMD boards */
398static volatile int main_running;
399static int commandline_current;
400override_t overrides[7] =
401{
402	{-1, 0, 0, 0},};	/* LILO overrides */
403
404#ifdef AM53C974_DEBUG
405static int deb_stop = 1;
406
407static struct {
408	unsigned char value;
409	char *name;
410} phases[] = {
411
412	{
413		PHASE_DATAOUT, "DATAOUT"
414	}, {
415		PHASE_DATAIN, "DATAIN"
416	}, {
417		PHASE_CMDOUT, "CMDOUT"
418	},
419	{
420		PHASE_STATIN, "STATIN"
421	}, {
422		PHASE_MSGOUT, "MSGOUT"
423	}, {
424		PHASE_MSGIN, "MSGIN"
425	},
426	{
427		PHASE_RES_0, "RESERVED 0"
428	}, {
429		PHASE_RES_1, "RESERVED 1"
430	}
431};
432
433/**************************************************************************
434 * Function : void AM53C974_print_phase(struct Scsi_Host *instance)
435 *
436 * Purpose : print the current SCSI phase for debugging purposes
437 *
438 * Input : instance - which AM53C974
439 **************************************************************************/
440static void AM53C974_print_phase(struct Scsi_Host *instance)
441{
442	AM53C974_local_declare();
443	unsigned char statreg, latched;
444	int i;
445	AM53C974_setio(instance);
446
447	latched = (AM53C974_read_8(CNTLREG2)) & CNTLREG2_ENF;
448	statreg = AM53C974_read_8(STATREG);
449	for (i = 0; (phases[i].value != PHASE_RES_1) &&
450	     (phases[i].value != (statreg & STATREG_PHASE)); ++i);
451	if (latched)
452		printk("scsi%d : phase %s, latched at end of last command\n", instance->host_no, phases[i].name);
453	else
454		printk("scsi%d : phase %s, real time\n", instance->host_no, phases[i].name);
455}
456
457/**************************************************************************
458 * Function : void AM53C974_print_queues(struct Scsi_Host *instance)
459 *
460 * Purpose : print commands in the various queues
461 *
462 * Inputs : instance - which AM53C974
463 **************************************************************************/
464static void AM53C974_print_queues(struct Scsi_Host *instance)
465{
466	unsigned long flags;
467	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
468	Scsi_Cmnd *ptr;
469
470	printk("AM53C974: coroutine is%s running.\n", main_running ? "" : "n't");
471
472	save_flags(flags);
473	cli();
474
475	if (!hostdata->connected) {
476		printk("scsi%d: no currently connected command\n", instance->host_no);
477	} else {
478		print_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected);
479	}
480	if (!hostdata->sel_cmd) {
481		printk("scsi%d: no currently arbitrating command\n", instance->host_no);
482	} else {
483		print_Scsi_Cmnd((Scsi_Cmnd *) hostdata->sel_cmd);
484	}
485
486	printk("scsi%d: issue_queue ", instance->host_no);
487	if (!hostdata->issue_queue)
488		printk("empty\n");
489	else {
490		printk(":\n");
491		for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
492			print_Scsi_Cmnd(ptr);
493	}
494
495	printk("scsi%d: disconnected_queue ", instance->host_no);
496	if (!hostdata->disconnected_queue)
497		printk("empty\n");
498	else {
499		printk(":\n");
500		for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
501			print_Scsi_Cmnd(ptr);
502	}
503
504	restore_flags(flags);
505}
506
507#endif				/* AM53C974_DEBUG */
508
509/**************************************************************************
510 * Function : void AM53C974_print(struct Scsi_Host *instance)
511 *
512 * Purpose : dump the chip registers for debugging purposes
513 *
514 * Input : instance - which AM53C974
515 **************************************************************************/
516static void AM53C974_print(struct Scsi_Host *instance)
517{
518	AM53C974_local_declare();
519	unsigned long flags;
520	unsigned long ctcreg, dmastc, dmaspa, dmawbc, dmawac;
521	unsigned char cmdreg, statreg, isreg, cfireg, cntlreg[4], dmacmd,
522	 dmastatus;
523	AM53C974_setio(instance);
524
525	save_flags(flags);
526	cli();
527	ctcreg = AM53C974_read_8(CTCHREG) << 16;
528	ctcreg |= AM53C974_read_8(CTCMREG) << 8;
529	ctcreg |= AM53C974_read_8(CTCLREG);
530	cmdreg = AM53C974_read_8(CMDREG);
531	statreg = AM53C974_read_8(STATREG);
532	isreg = AM53C974_read_8(ISREG);
533	cfireg = AM53C974_read_8(CFIREG);
534	cntlreg[0] = AM53C974_read_8(CNTLREG1);
535	cntlreg[1] = AM53C974_read_8(CNTLREG2);
536	cntlreg[2] = AM53C974_read_8(CNTLREG3);
537	cntlreg[3] = AM53C974_read_8(CNTLREG4);
538	dmacmd = AM53C974_read_8(DMACMD);
539	dmastc = AM53C974_read_32(DMASTC);
540	dmaspa = AM53C974_read_32(DMASPA);
541	dmawbc = AM53C974_read_32(DMAWBC);
542	dmawac = AM53C974_read_32(DMAWAC);
543	dmastatus = AM53C974_read_8(DMASTATUS);
544	restore_flags(flags);
545
546	printk("AM53C974 register dump:\n");
547	printk("IO base: 0x%04lx; CTCREG: 0x%04lx; CMDREG: 0x%02x; STATREG: 0x%02x; ISREG: 0x%02x\n",
548	       io_port, ctcreg, cmdreg, statreg, isreg);
549	printk("CFIREG: 0x%02x; CNTLREG1-4: 0x%02x; 0x%02x; 0x%02x; 0x%02x\n",
550	       cfireg, cntlreg[0], cntlreg[1], cntlreg[2], cntlreg[3]);
551	printk("DMACMD: 0x%02x; DMASTC: 0x%04lx; DMASPA: 0x%04lx\n", dmacmd, dmastc, dmaspa);
552	printk("DMAWBC: 0x%04lx; DMAWAC: 0x%04lx; DMASTATUS: 0x%02x\n", dmawbc, dmawac, dmastatus);
553	printk("---------------------------------------------------------\n");
554}
555
556/**************************************************************************
557* Function : void AM53C974_keywait(void)
558*
559* Purpose : wait until a key is pressed, if it was the 'r' key leave singlestep mode;
560*           this function is used for debugging only
561*
562* Input : none
563**************************************************************************/
564static void AM53C974_keywait(void)
565{
566	unsigned long flags;
567#ifdef AM53C974_DEBUG
568	int key;
569
570	if (!deb_stop)
571		return;
572#endif
573
574	save_flags(flags);
575	cli();
576	while ((inb_p(0x64) & 0x01) != 0x01);
577#ifdef AM53C974_DEBUG
578	key = inb(0x60);
579	if (key == 0x93)
580		deb_stop = 0;	/* don't stop if 'r' was pressed */
581#endif
582	restore_flags(flags);
583}
584
585#ifndef MODULE
586/**************************************************************************
587* Function : AM53C974_setup(char *str)
588*
589* Purpose : LILO command line initialization of the overrides array,
590*
591* Input : str - parameter string.
592*
593* Returns : 1.
594*
595* NOTE : this function needs to be declared as an external function
596*         in init/main.c and included there in the bootsetups list
597***************************************************************************/
598static int AM53C974_setup(char *str)
599{
600	int ints[5];
601
602	get_options(str, ARRAY_SIZE(ints), ints);
603
604	if (ints[0] < 4)
605		printk("AM53C974_setup: wrong number of parameters;\n correct syntax is: AM53C974=host-scsi-id, target-scsi-id, max-rate, max-offset\n");
606	else {
607		if (commandline_current < (sizeof(overrides) / sizeof(override_t))) {
608			if ((ints[1] < 0) || (ints[1] > 7) ||
609			    (ints[2] < 0) || (ints[2] > 7) ||
610			    (ints[1] == ints[2]) ||
611			    (ints[3] < (DEF_CLK / MAX_PERIOD)) || (ints[3] > (DEF_CLK / MIN_PERIOD)) ||
612			    (ints[4] < 0) || (ints[4] > MAX_OFFSET))
613				printk("AM53C974_setup: illegal parameter\n");
614			else {
615				overrides[commandline_current].host_scsi_id = ints[1];
616				overrides[commandline_current].target_scsi_id = ints[2];
617				overrides[commandline_current].max_rate = ints[3];
618				overrides[commandline_current].max_offset = ints[4];
619				commandline_current++;
620			}
621		} else
622			printk("AM53C974_setup: too many overrides\n");
623	}
624
625	return 1;
626}
627__setup("AM53C974=", AM53C974_setup);
628
629#endif /* !MODULE */
630
631/**************************************************************************
632* Function : int AM53C974_pci_detect(Scsi_Host_Template *tpnt)
633*
634* Purpose : detects and initializes AM53C974 SCSI chips with PCI Bios
635*
636* Inputs : tpnt - host template
637*
638* Returns : number of host adapters detected
639**************************************************************************/
640static int __init AM53C974_pci_detect(Scsi_Host_Template * tpnt)
641{
642	int count = 0;		/* number of boards detected */
643	struct pci_dev *pdev = NULL;
644	unsigned short command;
645
646	while ((pdev = pci_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_SCSI, pdev))) {
647		if (pci_enable_device(pdev))
648			continue;
649		pci_read_config_word(pdev, PCI_COMMAND, &command);
650
651		/* check whether device is I/O mapped -- should be */
652		if (!(command & PCI_COMMAND_IO))
653			continue;
654
655		pci_set_master (pdev);
656
657		/* everything seems OK now, so initialize */
658		if (AM53C974_init(tpnt, pdev))
659			count++;
660	}
661	return (count);
662}
663
664/**************************************************************************
665* Function : int AM53C974_init(Scsi_Host_Template *tpnt, struct pci_dev *pdev)
666*
667* Purpose : initializes instance and corresponding AM53/79C974 chip,
668*
669* Inputs : tpnt - template, pci_config - PCI configuration,
670*
671* Returns : 1 on success, 0 on failure.
672*
673* NOTE: If no override for the controller's SCSI id is given and AM53C974_SCSI_ID
674*       is not defined we assume that the SCSI address of this controller is correctly
675*       set up by the BIOS (as reflected by contents of register CNTLREG1).
676*       This is the only BIOS assistance we need.
677**************************************************************************/
678static int __init  AM53C974_init(Scsi_Host_Template * tpnt, struct pci_dev *pdev)
679{
680	AM53C974_local_declare();
681	int i, j;
682	struct Scsi_Host *instance, *search;
683	struct AM53C974_hostdata *hostdata;
684
685#ifdef AM53C974_OPTION_DEBUG_PROBE_ONLY
686	printk("AM53C974: probe only enabled, aborting initialization\n");
687	return 0;
688#endif
689
690	instance = scsi_register(tpnt, sizeof(struct AM53C974_hostdata));
691	if (!instance) {
692		printk(KERN_WARNING "AM53C974: Unable to register host, aborting.\n");
693		return 0;
694	}
695	scsi_set_pci_device(instance, pdev);
696	hostdata = (struct AM53C974_hostdata *) instance->hostdata;
697	instance->base = 0;
698	instance->io_port = pci_resource_start(pdev, 0);
699	instance->irq = pdev->irq;
700	instance->dma_channel = -1;
701	AM53C974_setio(instance);
702
703#ifdef AM53C974_SCSI_ID
704	instance->this_id = AM53C974_SCSI_ID;
705	AM53C974_write_8(CNTLREG1, instance->this_id & CNTLREG1_SID);
706#else
707	instance->this_id = AM53C974_read_8(CNTLREG1) & CNTLREG1_SID;
708	if (instance->this_id != 7)
709		printk("scsi%d: WARNING: unusual hostadapter SCSI id %d; please verify!\n",
710		       instance->host_no, instance->this_id);
711#endif
712
713	for (i = 0; i < sizeof(hostdata->msgout); i++) {
714		hostdata->msgout[i] = NOP;
715		hostdata->last_message[i] = NOP;
716	}
717	for (i = 0; i < 8; i++) {
718		hostdata->busy[i] = 0;
719		hostdata->sync_per[i] = DEF_STP;
720		hostdata->sync_off[i] = 0;
721		hostdata->sync_neg[i] = 0;
722		hostdata->sync_en[i] = DEFAULT_SYNC_NEGOTIATION_ENABLED;
723		hostdata->max_rate[i] = DEFAULT_RATE;
724		hostdata->max_offset[i] = DEFAULT_SYNC_OFFSET;
725	}
726
727/* overwrite defaults by LILO overrides */
728	for (i = 0; i < commandline_current; i++) {
729		if (overrides[i].host_scsi_id == instance->this_id) {
730			j = overrides[i].target_scsi_id;
731			hostdata->sync_en[j] = 1;
732			hostdata->max_rate[j] = overrides[i].max_rate;
733			hostdata->max_offset[j] = overrides[i].max_offset;
734		}
735	}
736
737	hostdata->sel_cmd = NULL;
738	hostdata->connected = NULL;
739	hostdata->issue_queue = NULL;
740	hostdata->disconnected_queue = NULL;
741	hostdata->in_reset = 0;
742	hostdata->aborted = 0;
743	hostdata->selecting = 0;
744	hostdata->disconnecting = 0;
745	hostdata->dma_busy = 0;
746
747/* Set up an interrupt handler if we aren't already sharing an IRQ with another board */
748	for (search = first_host;
749	     search && (((the_template != NULL) && (search->hostt != the_template)) ||
750		 (search->irq != instance->irq) || (search == instance));
751	     search = search->next);
752	if (!search) {
753		if (request_irq(instance->irq, do_AM53C974_intr, SA_SHIRQ, "AM53C974", instance)) {
754			printk("scsi%d: IRQ%d not free, detaching\n", instance->host_no, instance->irq);
755			scsi_unregister(instance);
756			return 0;
757		}
758	} else {
759		printk("scsi%d: using interrupt handler previously installed for scsi%d\n",
760		       instance->host_no, search->host_no);
761	}
762
763	if (!the_template) {
764		the_template = instance->hostt;
765		first_instance = instance;
766	}
767/* do hard reset */
768	AM53C974_write_8(CMDREG, CMDREG_RDEV);	/* reset device */
769	udelay(5);
770	AM53C974_write_8(CMDREG, CMDREG_NOP);
771	AM53C974_write_8(CNTLREG1, CNTLREG1_DISR | instance->this_id);
772	AM53C974_write_8(CMDREG, CMDREG_RBUS);	/* reset SCSI bus */
773	udelay(10);
774	AM53C974_config_after_reset(instance);
775	mdelay(500);
776	return (1);
777}
778
779/*********************************************************************
780* Function : AM53C974_config_after_reset(struct Scsi_Host *instance) *
781*                                                                    *
782* Purpose : initializes chip registers after reset                   *
783*                                                                    *
784* Inputs : instance - which AM53C974                                 *
785*                                                                    *
786* Returns : nothing                                                  *
787**********************************************************************/
788static void AM53C974_config_after_reset(struct Scsi_Host *instance)
789{
790	AM53C974_local_declare();
791	AM53C974_setio(instance);
792
793/* clear SCSI FIFO */
794	AM53C974_write_8(CMDREG, CMDREG_CFIFO);
795
796/* configure device */
797	AM53C974_write_8(STIMREG, DEF_SCSI_TIMEOUT);
798	AM53C974_write_8(STPREG, DEF_STP & STPREG_STP);
799	AM53C974_write_8(SOFREG, (DEF_SOF_RAD << 6) | (DEF_SOF_RAA << 4));
800	AM53C974_write_8(CLKFREG, DEF_CLKF & CLKFREG_MASK);
801	AM53C974_write_8(CNTLREG1, (DEF_ETM << 7) | CNTLREG1_DISR | (DEF_PERE << 4) | instance->this_id);
802	AM53C974_write_8(CNTLREG2, (DEF_ENF << 6));
803	AM53C974_write_8(CNTLREG3, (DEF_ADIDCHK << 7) | (DEF_FASTSCSI << 4) | (DEF_FASTCLK << 3));
804	AM53C974_write_8(CNTLREG4, (DEF_GLITCH << 6) | (DEF_PWD << 5) | (DEF_RAE << 3) | (DEF_RADE << 2) | CNTLREG4_RES);
805}
806
807/***********************************************************************
808* Function : const char *AM53C974_info(struct Scsi_Host *instance)     *
809*                                                                      *
810* Purpose : return device driver information                           *
811*                                                                      *
812* Inputs : instance - which AM53C974                                   *
813*                                                                      *
814* Returns : info string                                                *
815************************************************************************/
816static const char *AM53C974_info(struct Scsi_Host *instance)
817{
818	static char info[100];
819
820	sprintf(info, "AM53/79C974 PCscsi driver rev. %d.%d; host I/O address: 0x%lx; irq: %d\n",
821	  AM53C974_DRIVER_REVISION_MAJOR, AM53C974_DRIVER_REVISION_MINOR,
822		instance->io_port, instance->irq);
823	return (info);
824}
825
826/**************************************************************************
827* Function : int AM53C974_command (Scsi_Cmnd *SCpnt)                      *
828*                                                                         *
829* Purpose : the unqueued SCSI command function, replaced by the           *
830*           AM53C974_queue_command function                               *
831*                                                                         *
832* Inputs : SCpnt - pointer to command structure                           *
833*                                                                         *
834* Returns :status, see hosts.h for details                                *
835***************************************************************************/
836static int AM53C974_command(Scsi_Cmnd * SCpnt)
837{
838	DEB(printk("AM53C974_command called\n"));
839	return 0;
840}
841
842/**************************************************************************
843* Function : void initialize_SCp(Scsi_Cmnd *cmd)                          *
844*                                                                         *
845* Purpose : initialize the saved data pointers for cmd to point to the    *
846*	    start of the buffer.                                          *
847*                                                                         *
848* Inputs : cmd - Scsi_Cmnd structure to have pointers reset.              *
849*                                                                         *
850* Returns : nothing                                                       *
851**************************************************************************/
852static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
853{
854	if (cmd->use_sg) {
855		cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
856		cmd->SCp.buffers_residual = cmd->use_sg - 1;
857		cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
858		cmd->SCp.this_residual = cmd->SCp.buffer->length;
859	} else {
860		cmd->SCp.buffer = NULL;
861		cmd->SCp.buffers_residual = 0;
862		cmd->SCp.ptr = (char *) cmd->request_buffer;
863		cmd->SCp.this_residual = cmd->request_bufflen;
864	}
865}
866
867/**************************************************************************
868* Function : run_main(void)                                               *
869*                                                                         *
870* Purpose : insure that the coroutine is running and will process our     *
871* 	    request.  main_running is checked/set here (in an inline      *
872*           function rather than in AM53C974_main itself to reduce the    *
873*           chances of stack overflow.                                    *
874*                                                                         *
875*                                                                         *
876* Inputs : none                                                           *
877*                                                                         *
878* Returns : nothing                                                       *
879**************************************************************************/
880static __inline__ void run_main(void)
881{
882	unsigned long flags;
883	save_flags(flags);
884	cli();
885	if (!main_running) {
886		/* main_running is cleared in AM53C974_main once it can't do
887		   more work, and AM53C974_main exits with interrupts disabled. */
888		main_running = 1;
889		AM53C974_main();
890	}
891	restore_flags(flags);
892}
893
894/**************************************************************************
895* Function : int AM53C974_queue_command(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
896*
897* Purpose : writes SCSI command into AM53C974 FIFO
898*
899* Inputs : cmd - SCSI command, done - function called on completion, with
900*	a pointer to the command descriptor.
901*
902* Returns : status, see hosts.h for details
903*
904* Side effects :
905*      cmd is added to the per instance issue_queue, with minor
906*	twiddling done to the host specific fields of cmd.  If the
907*	main coroutine is not running, it is restarted.
908**************************************************************************/
909static int AM53C974_queue_command(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
910{
911	unsigned long flags;
912	struct Scsi_Host *instance = cmd->host;
913	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
914	Scsi_Cmnd *tmp;
915
916	save_flags(flags);
917	cli();
918	DEB_QUEUE(printk(SEPARATOR_LINE));
919	DEB_QUEUE(printk("scsi%d: AM53C974_queue_command called\n", instance->host_no));
920	DEB_QUEUE(printk("cmd=%02x target=%02x lun=%02x bufflen=%d use_sg = %02x\n",
921			 cmd->cmnd[0], cmd->target, cmd->lun, cmd->request_bufflen, cmd->use_sg));
922
923/* We use the host_scribble field as a pointer to the next command in a queue */
924	cmd->host_scribble = NULL;
925	cmd->scsi_done = done;
926	cmd->result = 0;
927	cmd->device->disconnect = 0;
928
929/* Insert the cmd into the issue queue. Note that REQUEST SENSE
930 * commands are added to the head of the queue since any command will
931 * clear the contingent allegiance condition that exists and the
932 * sense data is only guaranteed to be valid while the condition exists. */
933	if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
934		LIST(cmd, hostdata->issue_queue);
935		cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
936		hostdata->issue_queue = cmd;
937	} else {
938		for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble;
939		     tmp = (Scsi_Cmnd *) tmp->host_scribble);
940		LIST(cmd, tmp);
941		tmp->host_scribble = (unsigned char *) cmd;
942	}
943
944	DEB_QUEUE(printk("scsi%d : command added to %s of queue\n", instance->host_no,
945		     (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"));
946
947/* Run the coroutine if it isn't already running. */
948	run_main();
949	restore_flags(flags);
950	return 0;
951}
952
953/**************************************************************************
954 * Function : AM53C974_main (void)
955 *
956 * Purpose : AM53C974_main is a coroutine that runs as long as more work can
957 *	be done on the AM53C974 host adapters in a system.  Both
958 *	AM53C974_queue_command() and AM53C974_intr() will try to start it
959 *	in case it is not running.
960 *
961 * NOTE : AM53C974_main exits with interrupts *disabled*, the caller should
962 *  reenable them.  This prevents reentrancy and kernel stack overflow.
963 **************************************************************************/
964static void AM53C974_main(void)
965{
966	AM53C974_local_declare();
967	unsigned long flags;
968	Scsi_Cmnd *tmp, *prev;
969	struct Scsi_Host *instance;
970	struct AM53C974_hostdata *hostdata;
971	int done;
972
973/* We run (with interrupts disabled) until we're sure that none of
974 * the host adapters have anything that can be done, at which point
975 * we set main_running to 0 and exit. */
976
977	save_flags(flags);
978	cli();		/* Freeze request queues */
979	do {
980		done = 1;
981		for (instance = first_instance; instance && instance->hostt == the_template;
982		     instance = instance->next) {
983			hostdata = (struct AM53C974_hostdata *) instance->hostdata;
984			AM53C974_setio(instance);
985			/* start to select target if we are not connected and not in the
986			   selection process */
987			if (!hostdata->connected && !hostdata->sel_cmd) {
988				/* Search through the issue_queue for a command destined for a target
989				   that is not busy. */
990				for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp;
991				     prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble) {
992					/*  When we find one, remove it from the issue queue. */
993					if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
994						if (prev) {
995							REMOVE(prev, (Scsi_Cmnd *) (prev->host_scribble), tmp,
996							       (Scsi_Cmnd *) (tmp->host_scribble));
997							prev->host_scribble = tmp->host_scribble;
998						} else {
999							REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
1000							hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1001						}
1002						tmp->host_scribble = NULL;
1003
1004						/* go into selection mode, disable reselection and wait for
1005						   SO interrupt which will continue with the selection process */
1006						hostdata->selecting = 1;
1007						hostdata->sel_cmd = tmp;
1008						AM53C974_write_8(CMDREG, CMDREG_DSR);
1009						break;
1010					}	/* if target/lun is not busy */
1011				}	/* for */
1012			}
1013			/* if (!hostdata->connected) */
1014			else {
1015				DEB(printk("main: connected; cmd = 0x%lx, sel_cmd = 0x%lx\n",
1016					   (long) hostdata->connected, (long) hostdata->sel_cmd));
1017			}
1018		}		/* for instance */
1019	} while (!done);
1020	main_running = 0;
1021	restore_flags(flags);
1022}
1023
1024/************************************************************************
1025* Function : AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs) *
1026*                                                                       *
1027* Purpose : interrupt handler                                           *
1028*                                                                       *
1029* Inputs : irq - interrupt line, regs - ?                               *
1030*                                                                       *
1031* Returns : nothing                                                     *
1032************************************************************************/
1033static void do_AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs)
1034{
1035	unsigned long flags;
1036
1037	spin_lock_irqsave(&io_request_lock, flags);
1038	AM53C974_intr(irq, dev_id, regs);
1039	spin_unlock_irqrestore(&io_request_lock, flags);
1040}
1041
1042/************************************************************************
1043* Function : AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs) *
1044*                                                                       *
1045* Purpose : interrupt handler                                           *
1046*                                                                       *
1047* Inputs : irq - interrupt line, regs - ?                               *
1048*                                                                       *
1049* Returns : nothing                                                     *
1050************************************************************************/
1051static void AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs)
1052{
1053	AM53C974_local_declare();
1054	struct Scsi_Host *instance;
1055	struct AM53C974_hostdata *hostdata;
1056	unsigned char cmdreg, dmastatus, statreg, isreg, instreg, cfifo;
1057
1058/* find AM53C974 hostadapter responsible for this interrupt */
1059	for (instance = first_instance; instance; instance = instance->next)
1060		if ((instance->irq == irq) && (instance->hostt == the_template))
1061			goto FOUND;
1062	return;
1063
1064/* found; now decode and process */
1065	FOUND:
1066	    hostdata = (struct AM53C974_hostdata *) instance->hostdata;
1067	AM53C974_setio(instance);
1068	dmastatus = AM53C974_read_8(DMASTATUS);
1069
1070	DEB_INTR(printk(SEPARATOR_LINE));
1071	DEB_INTR(printk("AM53C974 interrupt; dmastatus=0x%02x\n", dmastatus));
1072	KEYWAIT();
1073
1074/*** DMA related interrupts ***/
1075	if (hostdata->connected && (dmastatus & (DMASTATUS_ERROR | DMASTATUS_PWDN |
1076						 DMASTATUS_ABORT))) {
1077		/* DMA error or POWERDOWN */
1078		printk("scsi%d: DMA error or powerdown; dmastatus: 0x%02x\n",
1079		       instance->host_no, dmastatus);
1080#ifdef AM53C974_DEBUG
1081		deb_stop = 1;
1082#endif
1083		panic("scsi%d: cannot recover\n", instance->host_no);
1084	}
1085	if (hostdata->connected && (dmastatus & DMASTATUS_DONE)) {
1086		/* DMA transfer done */
1087		unsigned long residual;
1088		unsigned long flags;
1089		save_flags(flags);
1090		cli();
1091		if (!(AM53C974_read_8(DMACMD) & DMACMD_DIR)) {
1092			do {
1093				dmastatus = AM53C974_read_8(DMASTATUS);
1094				residual = AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
1095				    (AM53C974_read_8(CTCHREG) << 16);
1096				residual += AM53C974_read_8(CFIREG) & CFIREG_CF;
1097			} while (!(dmastatus & DMASTATUS_SCSIINT) && residual);
1098			residual = AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
1099			    (AM53C974_read_8(CTCHREG) << 16);
1100			residual += AM53C974_read_8(CFIREG) & CFIREG_CF;
1101		} else
1102			residual = 0;
1103		hostdata->connected->SCp.ptr += hostdata->connected->SCp.this_residual - residual;
1104		hostdata->connected->SCp.this_residual = residual;
1105
1106		AM53C974_write_8(DMACMD, DMACMD_IDLE);
1107
1108		/* if service request missed before, process it now (ugly) */
1109		if (hostdata->dma_busy) {
1110			hostdata->dma_busy = 0;
1111			cmdreg = AM53C974_read_8(CMDREG);
1112			statreg = AM53C974_read_8(STATREG);
1113			isreg = AM53C974_read_8(ISREG);
1114			instreg = AM53C974_read_8(INSTREG);
1115			cfifo = AM53C974_cfifo();
1116			AM53C974_information_transfer(instance, statreg, isreg, instreg, cfifo,
1117						      dmastatus);
1118		}
1119		restore_flags(flags);
1120	}
1121	if (!(dmastatus & DMASTATUS_SCSIINT)) {
1122		return;
1123	}
1124/*** SCSI related interrupts ***/
1125	cmdreg = AM53C974_read_8(CMDREG);
1126	statreg = AM53C974_read_8(STATREG);
1127	isreg = AM53C974_read_8(ISREG);
1128	instreg = AM53C974_read_8(INSTREG);
1129	cfifo = AM53C974_cfifo();
1130
1131	DEB_INTR(printk("scsi%d: statreg: 0x%02x; isreg: 0x%02x; instreg: 0x%02x; cfifo: 0x%02x\n",
1132		     instance->host_no, statreg, isreg, instreg, cfifo));
1133
1134	if (statreg & STATREG_PE) {
1135		/* parity error */
1136#ifdef AM53C974_DEBUG
1137		deb_stop = 1;
1138#endif
1139		printk("scsi%d : PARITY error\n", instance->host_no);
1140		if (hostdata->connected)
1141			hostdata->sync_off[hostdata->connected->target] = 0;	/* setup asynchronous transfer */
1142		hostdata->aborted = 1;
1143	}
1144	if (statreg & STATREG_IOE) {
1145		/* illegal operation error */
1146#ifdef AM53C974_DEBUG
1147		deb_stop = 1;
1148#endif
1149		printk("scsi%d : ILLEGAL OPERATION error\n", instance->host_no);
1150		printk("cmdreg:  0x%02x; dmacmd:  0x%02x; statreg: 0x%02x; \n"
1151		   "isreg:   0x%02x; instreg: 0x%02x; cfifo:   0x%02x\n",
1152		       cmdreg, AM53C974_read_8(DMACMD), statreg, isreg, instreg, cfifo);
1153	}
1154	if (hostdata->in_reset && (instreg & INSTREG_SRST)) {
1155		unsigned long flags;
1156		/* RESET INTERRUPT */
1157#ifdef AM53C974_DEBUG
1158		deb_stop = 1;
1159#endif
1160		DEB(printk("Bus reset interrupt received\n"));
1161		AM53C974_intr_bus_reset(instance);
1162		save_flags(flags);
1163		cli();
1164		if (hostdata->connected) {
1165			hostdata->connected->result = DID_RESET << 16;
1166			hostdata->connected->scsi_done((Scsi_Cmnd *) hostdata->connected);
1167			hostdata->connected = NULL;
1168		} else {
1169			if (hostdata->sel_cmd) {
1170				hostdata->sel_cmd->result = DID_RESET << 16;
1171				hostdata->sel_cmd->scsi_done((Scsi_Cmnd *) hostdata->sel_cmd);
1172				hostdata->sel_cmd = NULL;
1173			}
1174		}
1175		restore_flags(flags);
1176		if (hostdata->in_reset == 1)
1177			goto EXIT;
1178		else
1179			return;
1180	}
1181	if (instreg & INSTREG_ICMD) {
1182		/* INVALID COMMAND INTERRUPT */
1183#ifdef AM53C974_DEBUG
1184		deb_stop = 1;
1185#endif
1186		printk("scsi%d: Invalid command interrupt\n", instance->host_no);
1187		printk("cmdreg:  0x%02x; dmacmd:  0x%02x; statreg: 0x%02x; dmastatus: 0x%02x; \n"
1188		   "isreg:   0x%02x; instreg: 0x%02x; cfifo:   0x%02x\n",
1189		       cmdreg, AM53C974_read_8(DMACMD), statreg, dmastatus, isreg, instreg, cfifo);
1190		panic("scsi%d: cannot recover\n", instance->host_no);
1191	}
1192	if (instreg & INSTREG_DIS) {
1193		unsigned long flags;
1194		/* DISCONNECT INTERRUPT */
1195		DEB_INTR(printk("Disconnect interrupt received; "));
1196		save_flags(flags);
1197		cli();
1198		AM53C974_intr_disconnect(instance);
1199		restore_flags(flags);
1200		goto EXIT;
1201	}
1202	if (instreg & INSTREG_RESEL) {
1203		unsigned long flags;
1204		/* RESELECTION INTERRUPT */
1205		DEB_INTR(printk("Reselection interrupt received\n"));
1206		save_flags(flags);
1207		cli();
1208		AM53C974_intr_reselect(instance, statreg);
1209		restore_flags(flags);
1210		goto EXIT;
1211	}
1212	if (instreg & INSTREG_SO) {
1213		DEB_INTR(printk("Successful operation interrupt received\n"));
1214		if (hostdata->selecting) {
1215			unsigned long flags;
1216			DEB_INTR(printk("DSR completed, starting select\n"));
1217			save_flags(flags);
1218			cli();
1219			AM53C974_select(instance, (Scsi_Cmnd *) hostdata->sel_cmd,
1220			  (hostdata->sel_cmd->cmnd[0] == REQUEST_SENSE) ?
1221					TAG_NONE : TAG_NEXT);
1222			hostdata->selecting = 0;
1223			AM53C974_set_sync(instance, hostdata->sel_cmd->target);
1224			restore_flags(flags);
1225			return;
1226		}
1227		if (hostdata->sel_cmd != NULL) {
1228			if (((isreg & ISREG_IS) != ISREG_OK_NO_STOP) &&
1229			    ((isreg & ISREG_IS) != ISREG_OK_STOP)) {
1230				unsigned long flags;
1231				/* UNSUCCESSFUL SELECTION */
1232				DEB_INTR(printk("unsuccessful selection\n"));
1233				save_flags(flags);
1234				cli();
1235				hostdata->dma_busy = 0;
1236				LIST(hostdata->sel_cmd, hostdata->issue_queue);
1237				hostdata->sel_cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
1238				hostdata->issue_queue = hostdata->sel_cmd;
1239				hostdata->sel_cmd = NULL;
1240				hostdata->selecting = 0;
1241				restore_flags(flags);
1242				goto EXIT;
1243			} else {
1244				unsigned long flags;
1245				/* SUCCESSFUL SELECTION */
1246				DEB(printk("successful selection; cmd=0x%02lx\n", (long) hostdata->sel_cmd));
1247				save_flags(flags);
1248				cli();
1249				hostdata->dma_busy = 0;
1250				hostdata->disconnecting = 0;
1251				hostdata->connected = hostdata->sel_cmd;
1252				hostdata->sel_cmd = NULL;
1253				hostdata->selecting = 0;
1254#ifdef SCSI2
1255				if (!hostdata->connected->device->tagged_queue)
1256#endif
1257					hostdata->busy[hostdata->connected->target] |= (1 << hostdata->connected->lun);
1258				/* very strange -- use_sg is sometimes nonzero for request sense commands !! */
1259				if ((hostdata->connected->cmnd[0] == REQUEST_SENSE) && hostdata->connected->use_sg) {
1260					DEB(printk("scsi%d: REQUEST_SENSE command with nonzero use_sg\n", instance->host_no));
1261					KEYWAIT();
1262					hostdata->connected->use_sg = 0;
1263				}
1264				initialize_SCp((Scsi_Cmnd *) hostdata->connected);
1265				hostdata->connected->SCp.phase = PHASE_CMDOUT;
1266				AM53C974_information_transfer(instance, statreg, isreg, instreg, cfifo, dmastatus);
1267				restore_flags(flags);
1268				return;
1269			}
1270		} else {
1271			unsigned long flags;
1272			save_flags(flags);
1273			cli();
1274			AM53C974_information_transfer(instance, statreg, isreg, instreg, cfifo, dmastatus);
1275			restore_flags(flags);
1276			return;
1277		}
1278	}
1279	if (instreg & INSTREG_SR) {
1280		DEB_INTR(printk("Service request interrupt received, "));
1281		if (hostdata->connected) {
1282			unsigned long flags;
1283			DEB_INTR(printk("calling information_transfer\n"));
1284			save_flags(flags);
1285			cli();
1286			AM53C974_information_transfer(instance, statreg, isreg, instreg, cfifo, dmastatus);
1287			restore_flags(flags);
1288		} else {
1289			printk("scsi%d: weird: service request when no command connected\n", instance->host_no);
1290			AM53C974_write_8(CMDREG, CMDREG_CFIFO);
1291		}		/* clear FIFO */
1292		return;
1293	}
1294	EXIT:
1295	    DEB_INTR(printk("intr: starting main\n"));
1296	run_main();
1297	DEB_INTR(printk("end of intr\n"));
1298}
1299
1300/**************************************************************************
1301* Function : AM53C974_intr_disconnect(struct Scsi_Host *instance)
1302*
1303* Purpose : manage target disconnection
1304*
1305* Inputs : instance -- which AM53C974
1306*
1307* Returns : nothing
1308**************************************************************************/
1309static void AM53C974_intr_disconnect(struct Scsi_Host *instance)
1310{
1311	AM53C974_local_declare();
1312	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
1313	Scsi_Cmnd *cmd;
1314	AM53C974_setio(instance);
1315
1316	if (hostdata->sel_cmd != NULL) {
1317		/* normal selection timeout, typical for nonexisting targets */
1318		cmd = (Scsi_Cmnd *) hostdata->sel_cmd;
1319		DEB_INTR(printk("bad target\n"));
1320		cmd->result = DID_BAD_TARGET << 16;
1321		goto EXIT_FINISHED;
1322	}
1323	if (!hostdata->connected) {
1324		/* can happen if controller was reset, a device tried to reconnect,
1325		   failed and disconnects now */
1326		AM53C974_write_8(CMDREG, CMDREG_CFIFO);
1327		return;
1328	}
1329	if (hostdata->disconnecting) {
1330		/* target sent disconnect message, so we are prepared */
1331		cmd = (Scsi_Cmnd *) hostdata->connected;
1332		AM53C974_set_async(instance, cmd->target);
1333		DEB_INTR(printk("scsi%d : disc. from cmnd %d for ta %d, lun %d\n",
1334		instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
1335		if (cmd->device->disconnect) {
1336			/* target wants to reselect later */
1337			DEB_INTR(printk("ok, re-enabling selection\n"));
1338			LIST(cmd, hostdata->disconnected_queue);
1339			cmd->host_scribble = (unsigned char *) hostdata->disconnected_queue;
1340			hostdata->disconnected_queue = cmd;
1341			DEB_QUEUE(printk("scsi%d : command for target %d lun %d this %d was moved from connected to"
1342					 "  the disconnected_queue\n", instance->host_no, cmd->target,
1343					 cmd->lun, hostdata->disconnected_queue->SCp.this_residual));
1344			DEB_QUEUE(AM53C974_print_queues(instance));
1345			goto EXIT_UNFINISHED;
1346		} else {
1347			/* target does not want to reselect later, we are really finished */
1348#ifdef AM53C974_DEBUG
1349			if (cmd->cmnd[0] == REQUEST_SENSE) {
1350				int i;
1351				printk("Request sense data dump:\n");
1352				for (i = 0; i < cmd->request_bufflen; i++) {
1353					printk("%02x ", *((char *) (cmd->request_buffer) + i));
1354					if (i && !(i % 16))
1355						printk("\n");
1356				}
1357				printk("\n");
1358			}
1359#endif
1360			goto EXIT_FINISHED;
1361		}		/* !cmd->device->disconnect */
1362	}			/* if (hostdata->disconnecting) */
1363	/* no disconnect message received; unexpected disconnection */
1364	cmd = (Scsi_Cmnd *) hostdata->connected;
1365	if (cmd) {
1366#ifdef AM53C974_DEBUG
1367		deb_stop = 1;
1368#endif
1369		AM53C974_set_async(instance, cmd->target);
1370		printk("scsi%d: Unexpected disconnect; phase: %d; target: %d; this_residual: %d; buffers_residual: %d; message: %d\n",
1371		       instance->host_no, cmd->SCp.phase, cmd->target, cmd->SCp.this_residual, cmd->SCp.buffers_residual,
1372		       cmd->SCp.Message);
1373		printk("cmdreg: 0x%02x; statreg: 0x%02x; isreg: 0x%02x; cfifo: 0x%02x\n",
1374		       AM53C974_read_8(CMDREG), AM53C974_read_8(STATREG), AM53C974_read_8(ISREG),
1375		       AM53C974_read_8(CFIREG) & CFIREG_CF);
1376
1377		if ((hostdata->last_message[0] == EXTENDED_MESSAGE) &&
1378		    (hostdata->last_message[2] == EXTENDED_SDTR)) {
1379			/* sync. negotiation was aborted, setup asynchronous transfer with target */
1380			hostdata->sync_off[cmd->target] = 0;
1381		}
1382		if (hostdata->aborted || hostdata->msgout[0] == ABORT)
1383			cmd->result = DID_ABORT << 16;
1384		else
1385			cmd->result = DID_ERROR << 16;
1386		goto EXIT_FINISHED;
1387	}
1388	EXIT_FINISHED:
1389	    hostdata->aborted = 0;
1390	hostdata->msgout[0] = NOP;
1391	hostdata->sel_cmd = NULL;
1392	hostdata->connected = NULL;
1393	hostdata->selecting = 0;
1394	hostdata->disconnecting = 0;
1395	hostdata->dma_busy = 0;
1396	hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1397	AM53C974_write_8(CMDREG, CMDREG_CFIFO);
1398	DEB(printk("disconnect; issue_queue: 0x%lx, disconnected_queue: 0x%lx\n",
1399		   (long) hostdata->issue_queue, (long) hostdata->disconnected_queue));
1400	cmd->scsi_done(cmd);
1401
1402	if (!hostdata->selecting) {
1403		AM53C974_set_async(instance, cmd->target);
1404		AM53C974_write_8(CMDREG, CMDREG_ESR);
1405	}			/* allow reselect */
1406	return;
1407
1408	EXIT_UNFINISHED:
1409	    hostdata->msgout[0] = NOP;
1410	hostdata->sel_cmd = NULL;
1411	hostdata->connected = NULL;
1412	hostdata->aborted = 0;
1413	hostdata->selecting = 0;
1414	hostdata->disconnecting = 0;
1415	hostdata->dma_busy = 0;
1416	DEB(printk("disconnect; issue_queue: 0x%lx, disconnected_queue: 0x%lx\n",
1417		   (long) hostdata->issue_queue, (long) hostdata->disconnected_queue));
1418	if (!hostdata->selecting) {
1419		AM53C974_set_async(instance, cmd->target);
1420		AM53C974_write_8(CMDREG, CMDREG_ESR);
1421	}			/* allow reselect */
1422	return;
1423}
1424
1425/**************************************************************************
1426* Function : int AM53C974_sync_neg(struct Scsi_Host *instance, int target, unsigned char *msg)
1427*
1428* Purpose : setup message string for sync. negotiation
1429*
1430* Inputs : instance -- which AM53C974
1431*          target -- which SCSI target to deal with
1432*          msg -- input message string
1433*
1434* Returns : 0 if parameters accepted or 1 if not accepted
1435*
1436* Side effects: hostdata is changed
1437*
1438* Note: we assume here that fastclk is enabled
1439**************************************************************************/
1440static int AM53C974_sync_neg(struct Scsi_Host *instance, int target, unsigned char *msg)
1441{
1442	AM53C974_local_declare();
1443	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
1444	int period, offset, i, rate, rate_rem;
1445	AM53C974_setio(instance);
1446
1447	period = (DEF_CLK * msg[3] * 8 + 1000) / 2000;
1448	if (period < MIN_PERIOD) {
1449		period = MIN_PERIOD;
1450		hostdata->msgout[3] = period / 4;
1451	} else if (period > MAX_PERIOD) {
1452		period = MAX_PERIOD;
1453		hostdata->msgout[3] = period / 4;
1454	} else
1455		hostdata->msgout[3] = msg[3];
1456	offset = msg[4];
1457	if (offset > MAX_OFFSET)
1458		offset = MAX_OFFSET;
1459	hostdata->msgout[4] = offset;
1460	hostdata->sync_per[target] = period;
1461	hostdata->sync_off[target] = offset;
1462	for (i = 0; i < 3; i++)
1463		hostdata->msgout[i] = msg[i];
1464	if ((hostdata->msgout[3] != msg[3]) || (msg[4] != offset))
1465		return (1);
1466
1467	rate = DEF_CLK / period;
1468	rate_rem = 10 * (DEF_CLK - period * rate) / period;
1469
1470	if (offset)
1471		printk("\ntarget %d: rate=%d.%d Mhz, synchronous, sync offset=%d bytes\n",
1472		       target, rate, rate_rem, offset);
1473	else
1474		printk("\ntarget %d: rate=%d.%d Mhz, asynchronous\n", target, rate, rate_rem);
1475
1476	return (0);
1477}
1478
1479/**************************************************************************
1480* Function : AM53C974_set_async(struct Scsi_Host *instance, int target)
1481*
1482* Purpose : put controller into async. mode
1483*
1484* Inputs : instance -- which AM53C974
1485*          target -- which SCSI target to deal with
1486*
1487* Returns : nothing
1488**************************************************************************/
1489static __inline__ void AM53C974_set_async(struct Scsi_Host *instance, int target)
1490{
1491	AM53C974_local_declare();
1492	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
1493	AM53C974_setio(instance);
1494
1495	AM53C974_write_8(STPREG, hostdata->sync_per[target]);
1496	AM53C974_write_8(SOFREG, (DEF_SOF_RAD << 6) | (DEF_SOF_RAA << 4));
1497}
1498
1499/**************************************************************************
1500* Function : AM53C974_set_sync(struct Scsi_Host *instance, int target)
1501*
1502* Purpose : put controller into sync. mode
1503*
1504* Inputs : instance -- which AM53C974
1505*          target -- which SCSI target to deal with
1506*
1507* Returns : nothing
1508**************************************************************************/
1509static __inline__ void AM53C974_set_sync(struct Scsi_Host *instance, int target)
1510{
1511	AM53C974_local_declare();
1512	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
1513	AM53C974_setio(instance);
1514
1515	AM53C974_write_8(STPREG, hostdata->sync_per[target]);
1516	AM53C974_write_8(SOFREG, (SOFREG_SO & hostdata->sync_off[target]) |
1517			 (DEF_SOF_RAD << 6) | (DEF_SOF_RAA << 4));
1518}
1519
1520/***********************************************************************
1521* Function : AM53C974_information_transfer(struct Scsi_Host *instance, *
1522*                          unsigned char statreg, unsigned char isreg, *
1523*                         unsigned char instreg, unsigned char cfifo,  *
1524*                         unsigned char dmastatus)                     *
1525*                                                                      *
1526* Purpose : handle phase changes                                       *
1527*                                                                      *
1528* Inputs : instance - which AM53C974                                   *
1529*          statreg - status register                                     *
1530*          isreg - internal state register                             *
1531*          instreg - interrupt status register                         *
1532*          cfifo - number of bytes in FIFO                             *
1533*          dmastatus - dma status register                             *
1534*                                                                      *
1535* Returns : nothing                                                    *
1536************************************************************************/
1537static void AM53C974_information_transfer(struct Scsi_Host *instance,
1538			      unsigned char statreg, unsigned char isreg,
1539			      unsigned char instreg, unsigned char cfifo,
1540					  unsigned char dmastatus)
1541{
1542	AM53C974_local_declare();
1543	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
1544	Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
1545	int ret, i, len, residual = -1;
1546	AM53C974_setio(instance);
1547
1548	DEB_INFO(printk(SEPARATOR_LINE));
1549	switch (statreg & STATREG_PHASE) {	/* scsi phase */
1550		case PHASE_DATAOUT:
1551		    DEB_INFO(printk("Dataout phase; cmd=0x%lx, sel_cmd=0x%lx, this_residual=%d, buffers_residual=%d\n",
1552				    (long) hostdata->connected, (long) hostdata->sel_cmd, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
1553		cmd->SCp.phase = PHASE_DATAOUT;
1554		goto PHASE_DATA_IO;
1555
1556		case PHASE_DATAIN:
1557		    DEB_INFO(printk("Datain phase; cmd=0x%lx, sel_cmd=0x%lx, this_residual=%d, buffers_residual=%d\n",
1558				    (long) hostdata->connected, (long) hostdata->sel_cmd, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
1559		cmd->SCp.phase = PHASE_DATAIN;
1560		PHASE_DATA_IO:
1561		    if (hostdata->aborted) {
1562			AM53C974_write_8(DMACMD, DMACMD_IDLE);
1563			AM53C974_write_8(CMDREG, CMDREG_CFIFO);
1564			AM53C974_write_8(CMDREG, CMDREG_SATN);
1565			return;
1566		}
1567		if ((!cmd->SCp.this_residual) && cmd->SCp.buffers_residual) {
1568			cmd->SCp.buffer++;
1569			cmd->SCp.buffers_residual--;
1570			cmd->SCp.ptr = (unsigned char *) cmd->SCp.buffer->address;
1571			cmd->SCp.this_residual = cmd->SCp.buffer->length;
1572		}
1573		if (cmd->SCp.this_residual) {
1574			if (!(AM53C974_read_8(DMACMD) & DMACMD_START)) {
1575				hostdata->dma_busy = 0;
1576				AM53C974_transfer_dma(instance, statreg & STATREG_IO,
1577				  (unsigned long) cmd->SCp.this_residual,
1578						      cmd->SCp.ptr);
1579			} else
1580				hostdata->dma_busy = 1;
1581		}
1582		return;
1583
1584		case PHASE_MSGIN:
1585		    DEB_INFO(printk("Message-In phase; cmd=0x%lx, sel_cmd=0x%lx\n",
1586		  (long) hostdata->connected, (long) hostdata->sel_cmd));
1587		AM53C974_set_async(instance, cmd->target);
1588		if (cmd->SCp.phase == PHASE_DATAIN)
1589			AM53C974_dma_blast(instance, dmastatus, statreg);
1590		if ((cmd->SCp.phase == PHASE_DATAOUT) && (AM53C974_read_8(DMACMD) & DMACMD_START)) {
1591			AM53C974_write_8(DMACMD, DMACMD_IDLE);
1592			residual = cfifo + (AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
1593				       (AM53C974_read_8(CTCHREG) << 16));
1594			cmd->SCp.ptr += cmd->SCp.this_residual - residual;
1595			cmd->SCp.this_residual = residual;
1596			if (cfifo) {
1597				AM53C974_write_8(CMDREG, CMDREG_CFIFO);
1598				cfifo = 0;
1599			}
1600		}
1601		if (cmd->SCp.phase == PHASE_STATIN) {
1602			while ((AM53C974_read_8(CFIREG) & CFIREG_CF) < 2);
1603			cmd->SCp.Status = AM53C974_read_8(FFREG);
1604			cmd->SCp.Message = AM53C974_read_8(FFREG);
1605			DEB_INFO(printk("Message-In phase; status=0x%02x, message=0x%02x\n",
1606				     cmd->SCp.Status, cmd->SCp.Message));
1607			ret = AM53C974_message(instance, cmd, cmd->SCp.Message);
1608		} else {
1609			if (!cfifo) {
1610				AM53C974_write_8(CMDREG, CMDREG_IT);
1611				AM53C974_poll_int();
1612				cmd->SCp.Message = AM53C974_read_8(FFREG);
1613			}
1614			ret = AM53C974_message(instance, cmd, cmd->SCp.Message);
1615		}
1616		cmd->SCp.phase = PHASE_MSGIN;
1617		AM53C974_set_sync(instance, cmd->target);
1618		break;
1619		case PHASE_MSGOUT:
1620		    DEB_INFO(printk("Message-Out phase; cfifo=%d; msgout[0]=0x%02x\n",
1621				    AM53C974_read_8(CFIREG) & CFIREG_CF, hostdata->msgout[0]));
1622		AM53C974_write_8(DMACMD, DMACMD_IDLE);
1623		AM53C974_set_async(instance, cmd->target);
1624		for (i = 0; i < sizeof(hostdata->last_message); i++)
1625			hostdata->last_message[i] = hostdata->msgout[i];
1626		if ((hostdata->msgout[0] == 0) || INSIDE(hostdata->msgout[0], 0x02, 0x1F) ||
1627		    INSIDE(hostdata->msgout[0], 0x80, 0xFF))
1628			len = 1;
1629		else {
1630			if (hostdata->msgout[0] == EXTENDED_MESSAGE) {
1631#ifdef AM53C974_DEBUG_INFO
1632				printk("Extended message dump:\n");
1633				for (i = 0; i < hostdata->msgout[1] + 2; i++) {
1634					printk("%02x ", hostdata->msgout[i]);
1635					if (i && !(i % 16))
1636						printk("\n");
1637				}
1638				printk("\n");
1639#endif
1640				len = hostdata->msgout[1] + 2;
1641			} else
1642				len = 2;
1643		}
1644		for (i = 0; i < len; i++)
1645			AM53C974_write_8(FFREG, hostdata->msgout[i]);
1646		AM53C974_write_8(CMDREG, CMDREG_IT);
1647		cmd->SCp.phase = PHASE_MSGOUT;
1648		hostdata->msgout[0] = NOP;
1649		AM53C974_set_sync(instance, cmd->target);
1650		break;
1651
1652		case PHASE_CMDOUT:
1653		    DEB_INFO(printk("Command-Out phase\n"));
1654		AM53C974_set_async(instance, cmd->target);
1655		for (i = 0; i < cmd->cmd_len; i++)
1656			AM53C974_write_8(FFREG, cmd->cmnd[i]);
1657		AM53C974_write_8(CMDREG, CMDREG_IT);
1658		cmd->SCp.phase = PHASE_CMDOUT;
1659		AM53C974_set_sync(instance, cmd->target);
1660		break;
1661
1662		case PHASE_STATIN:
1663		    DEB_INFO(printk("Status phase\n"));
1664		if (cmd->SCp.phase == PHASE_DATAIN)
1665			AM53C974_dma_blast(instance, dmastatus, statreg);
1666		AM53C974_set_async(instance, cmd->target);
1667		if (cmd->SCp.phase == PHASE_DATAOUT) {
1668			unsigned long residual;
1669
1670			if (AM53C974_read_8(DMACMD) & DMACMD_START) {
1671				AM53C974_write_8(DMACMD, DMACMD_IDLE);
1672				residual = cfifo + (AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
1673				       (AM53C974_read_8(CTCHREG) << 16));
1674				cmd->SCp.ptr += cmd->SCp.this_residual - residual;
1675				cmd->SCp.this_residual = residual;
1676			}
1677			if (cfifo) {
1678				AM53C974_write_8(CMDREG, CMDREG_CFIFO);
1679				cfifo = 0;
1680			}
1681		}
1682		cmd->SCp.phase = PHASE_STATIN;
1683		AM53C974_write_8(CMDREG, CMDREG_ICCS);	/* command complete */
1684		break;
1685
1686		case PHASE_RES_0:
1687		    case PHASE_RES_1:
1688#ifdef AM53C974_DEBUG
1689		    deb_stop = 1;
1690#endif
1691		DEB_INFO(printk("Reserved phase\n"));
1692		break;
1693	}
1694	KEYWAIT();
1695}
1696
1697/******************************************************************************
1698* Function : int AM53C974_message(struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1699*                                 unsigned char msg)
1700*
1701* Purpose : handle SCSI messages
1702*
1703* Inputs : instance -- which AM53C974
1704*          cmd -- SCSI command the message belongs to
1705*          msg -- message id byte
1706*
1707* Returns : 1 on success, 0 on failure.
1708**************************************************************************/
1709static int AM53C974_message(struct Scsi_Host *instance, Scsi_Cmnd * cmd,
1710			    unsigned char msg)
1711{
1712	AM53C974_local_declare();
1713	static unsigned char extended_msg[10];
1714	unsigned char statreg;
1715	int len, ret = 0;
1716	unsigned char *p;
1717#ifdef AM53C974_DEBUG_MSG
1718	int j;
1719#endif
1720	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
1721	AM53C974_setio(instance);
1722
1723	DEB_MSG(printk(SEPARATOR_LINE));
1724
1725/* Linking lets us reduce the time required to get the
1726 * next command out to the device, hopefully this will
1727 * mean we don't waste another revolution due to the delays
1728 * required by ARBITRATION and another SELECTION.
1729 * In the current implementation proposal, low level drivers
1730 * merely have to start the next command, pointed to by
1731 * next_link, done() is called as with unlinked commands. */
1732	switch (msg) {
1733#ifdef LINKED
1734		case LINKED_CMD_COMPLETE:
1735		    case LINKED_FLG_CMD_COMPLETE:
1736		/* Accept message by releasing ACK */
1737		    DEB_LINKED(printk("scsi%d : target %d lun %d linked command complete.\n",
1738			      instance->host_no, cmd->target, cmd->lun));
1739		/* Sanity check : A linked command should only terminate with
1740		 * one of these messages if there are more linked commands available. */
1741		if (!cmd->next_link) {
1742			printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
1743			       instance->host_no, cmd->target, cmd->lun);
1744			hostdata->aborted = 1;
1745			AM53C974_write_8(CMDREG, CMDREG_SATN);
1746			AM53C974_write_8(CMDREG, CMDREG_MA);
1747			break;
1748		}
1749		if (hostdata->aborted) {
1750			DEB_ABORT(printk("ATN set for cmnd %d upon reception of LINKED_CMD_COMPLETE or"
1751					 "LINKED_FLG_CMD_COMPLETE message\n", cmd->cmnd[0]));
1752			AM53C974_write_8(CMDREG, CMDREG_SATN);
1753		}
1754		AM53C974_write_8(CMDREG, CMDREG_MA);
1755
1756		initialize_SCp(cmd->next_link);
1757		/* The next command is still part of this process */
1758		cmd->next_link->tag = cmd->tag;
1759		cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1760		DEB_LINKED(printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
1761			      instance->host_no, cmd->target, cmd->lun));
1762		cmd->scsi_done(cmd);
1763		cmd = hostdata->connected;
1764		break;
1765
1766#endif				/* def LINKED */
1767
1768		case ABORT:
1769		    case COMMAND_COMPLETE:
1770		    DEB_MSG(printk("scsi%d: command complete message received; cmd %d for target %d, lun %d\n",
1771		instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
1772		hostdata->disconnecting = 1;
1773		cmd->device->disconnect = 0;
1774
1775		/* I'm not sure what the correct thing to do here is :
1776
1777		 * If the command that just executed is NOT a request
1778		 * sense, the obvious thing to do is to set the result
1779		 * code to the values of the stored parameters.
1780		 * If it was a REQUEST SENSE command, we need some way
1781		 * to differentiate between the failure code of the original
1782		 * and the failure code of the REQUEST sense - the obvious
1783		 * case is success, where we fall through and leave the result
1784		 * code unchanged.
1785		 *
1786		 * The non-obvious place is where the REQUEST SENSE failed  */
1787		if (cmd->cmnd[0] != REQUEST_SENSE)
1788			cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1789		else if (cmd->SCp.Status != GOOD)
1790			cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1791		if (hostdata->aborted) {
1792			AM53C974_write_8(CMDREG, CMDREG_SATN);
1793			AM53C974_write_8(CMDREG, CMDREG_MA);
1794			DEB_ABORT(printk("ATN set for cmnd %d upon reception of ABORT or"
1795			    "COMMAND_COMPLETE message\n", cmd->cmnd[0]));
1796			break;
1797		}
1798		if ((cmd->cmnd[0] != REQUEST_SENSE) && (cmd->SCp.Status == CHECK_CONDITION)) {
1799			DEB_MSG(printk("scsi%d : performing request sense\n", instance->host_no));
1800			cmd->cmnd[0] = REQUEST_SENSE;
1801			cmd->cmnd[1] &= 0xe0;
1802			cmd->cmnd[2] = 0;
1803			cmd->cmnd[3] = 0;
1804			cmd->cmnd[4] = sizeof(cmd->sense_buffer);
1805			cmd->cmnd[5] = 0;
1806			cmd->SCp.buffer = NULL;
1807			cmd->SCp.buffers_residual = 0;
1808			cmd->SCp.ptr = (char *) cmd->sense_buffer;
1809			cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
1810			LIST(cmd, hostdata->issue_queue);
1811			cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
1812			hostdata->issue_queue = (Scsi_Cmnd *) cmd;
1813			DEB_MSG(printk("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no));
1814		}
1815		/* Accept message by clearing ACK */
1816		AM53C974_write_8(CMDREG, CMDREG_MA);
1817		break;
1818
1819		case MESSAGE_REJECT:
1820		    DEB_MSG(printk("scsi%d: reject message received; cmd %d for target %d, lun %d\n",
1821		instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
1822		switch (hostdata->last_message[0]) {
1823			case EXTENDED_MESSAGE:
1824			    if (hostdata->last_message[2] == EXTENDED_SDTR) {
1825				/* sync. negotiation was rejected, setup asynchronous transfer with target */
1826				printk("\ntarget %d: rate=%d Mhz, asynchronous (sync. negotiation rejected)\n",
1827				       cmd->target, DEF_CLK / DEF_STP);
1828				hostdata->sync_off[cmd->target] = 0;
1829				hostdata->sync_per[cmd->target] = DEF_STP;
1830			}
1831			break;
1832			case HEAD_OF_QUEUE_TAG:
1833			    case ORDERED_QUEUE_TAG:
1834			    case SIMPLE_QUEUE_TAG:
1835			    cmd->device->tagged_queue = 0;
1836			hostdata->busy[cmd->target] |= (1 << cmd->lun);
1837			break;
1838			default:
1839			    break;
1840		}
1841		if (hostdata->aborted)
1842			AM53C974_write_8(CMDREG, CMDREG_SATN);
1843		AM53C974_write_8(CMDREG, CMDREG_MA);
1844		break;
1845
1846		case DISCONNECT:
1847		    DEB_MSG(printk("scsi%d: disconnect message received; cmd %d for target %d, lun %d\n",
1848		instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
1849		cmd->device->disconnect = 1;
1850		hostdata->disconnecting = 1;
1851		AM53C974_write_8(CMDREG, CMDREG_MA);	/* Accept message by clearing ACK */
1852		break;
1853
1854		case SAVE_POINTERS:
1855		    case RESTORE_POINTERS:
1856		    DEB_MSG(printk("scsi%d: save/restore pointers message received; cmd %d for target %d, lun %d\n",
1857		instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
1858		/* The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1859		 * operation, in violation of the SCSI spec so we can safely
1860		 * ignore SAVE/RESTORE pointers calls.
1861		 *
1862		 * Unfortunately, some disks violate the SCSI spec and
1863		 * don't issue the required SAVE_POINTERS message before
1864		 * disconnecting, and we have to break spec to remain
1865		 * compatible. */
1866		if (hostdata->aborted) {
1867			DEB_ABORT(printk("ATN set for cmnd %d upon reception of SAVE/REST. POINTERS message\n",
1868					 cmd->cmnd[0]));
1869			AM53C974_write_8(CMDREG, CMDREG_SATN);
1870		}
1871		AM53C974_write_8(CMDREG, CMDREG_MA);
1872		break;
1873
1874		case EXTENDED_MESSAGE:
1875		    DEB_MSG(printk("scsi%d: extended message received; cmd %d for target %d, lun %d\n",
1876		instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
1877		/* Extended messages are sent in the following format :
1878		 * Byte
1879		 * 0           EXTENDED_MESSAGE == 1
1880		 * 1           length (includes one byte for code, doesn't include first two bytes)
1881		 * 2           code
1882		 * 3..length+1 arguments
1883		 */
1884		/* BEWARE!! THIS CODE IS EXTREMELY UGLY */
1885		extended_msg[0] = EXTENDED_MESSAGE;
1886		AM53C974_read_8(INSTREG);	/* clear int */
1887		AM53C974_write_8(CMDREG, CMDREG_MA);	/* ack. msg byte, then wait for SO */
1888		AM53C974_poll_int();
1889		/* get length */
1890		AM53C974_write_8(CMDREG, CMDREG_IT);
1891		AM53C974_poll_int();
1892		AM53C974_write_8(CMDREG, CMDREG_MA);	/* ack. msg byte, then wait for SO */
1893		AM53C974_poll_int();
1894		extended_msg[1] = len = AM53C974_read_8(FFREG);		/* get length */
1895		p = extended_msg + 2;
1896		/* read the remaining (len) bytes */
1897		while (len) {
1898			AM53C974_write_8(CMDREG, CMDREG_IT);
1899			AM53C974_poll_int();
1900			if (len > 1) {
1901				AM53C974_write_8(CMDREG, CMDREG_MA);	/* ack. msg byte, then wait for SO */
1902				AM53C974_poll_int();
1903			}
1904			*p = AM53C974_read_8(FFREG);
1905			p++;
1906			len--;
1907		}
1908
1909#ifdef AM53C974_DEBUG_MSG
1910		printk("scsi%d: received extended message: ", instance->host_no);
1911		for (j = 0; j < extended_msg[1] + 2; j++) {
1912			printk("0x%02x ", extended_msg[j]);
1913			if (j && !(j % 16))
1914				printk("\n");
1915		}
1916		printk("\n");
1917#endif
1918
1919		/* check message */
1920		if (extended_msg[2] == EXTENDED_SDTR)
1921			ret = AM53C974_sync_neg(instance, cmd->target, extended_msg);
1922		if (ret || hostdata->aborted)
1923			AM53C974_write_8(CMDREG, CMDREG_SATN);
1924
1925		AM53C974_write_8(CMDREG, CMDREG_MA);
1926		break;
1927
1928		default:
1929		    printk("scsi%d: unknown message 0x%02x received\n", instance->host_no, msg);
1930#ifdef AM53C974_DEBUG
1931		deb_stop = 1;
1932#endif
1933		/* reject message */
1934		hostdata->msgout[0] = MESSAGE_REJECT;
1935		AM53C974_write_8(CMDREG, CMDREG_SATN);
1936		AM53C974_write_8(CMDREG, CMDREG_MA);
1937		return (0);
1938		break;
1939
1940	}			/* switch (msg) */
1941	KEYWAIT();
1942	return (1);
1943}
1944
1945/**************************************************************************
1946* Function : AM53C974_select(struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
1947*
1948* Purpose : try to establish nexus for the command;
1949*           start sync negotiation via start stop and transfer the command in
1950*           cmdout phase in case of an inquiry or req. sense command with no
1951*           sync. neg. performed yet
1952*
1953* Inputs : instance -- which AM53C974
1954*          cmd -- command which requires the selection
1955*          tag -- tagged queueing
1956*
1957* Returns : nothing
1958*
1959* Note: this function initializes the selection process, which is continued
1960*       in the interrupt handler
1961**************************************************************************/
1962static void AM53C974_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag)
1963{
1964	AM53C974_local_declare();
1965	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
1966	unsigned char cfifo, tmp[3];
1967	unsigned int i, len, cmd_size = COMMAND_SIZE(cmd->cmnd[0]);
1968	AM53C974_setio(instance);
1969
1970	cfifo = AM53C974_cfifo();
1971	if (cfifo) {
1972		printk("scsi%d: select error; %d residual bytes in FIFO\n", instance->host_no, cfifo);
1973		AM53C974_write_8(CMDREG, CMDREG_CFIFO);		/* clear FIFO */
1974	}
1975#ifdef AM53C974_PROHIBIT_DISCONNECT
1976	tmp[0] = IDENTIFY(0, cmd->lun);
1977#else
1978	tmp[0] = IDENTIFY(1, cmd->lun);
1979#endif
1980
1981#ifdef SCSI2
1982	if (cmd->device->tagged_queue && (tag != TAG_NONE)) {
1983		tmp[1] = SIMPLE_QUEUE_TAG;
1984		if (tag == TAG_NEXT) {
1985			/* 0 is TAG_NONE, used to imply no tag for this command */
1986			if (cmd->device->current_tag == 0)
1987				cmd->device->current_tag = 1;
1988			cmd->tag = cmd->device->current_tag;
1989			cmd->device->current_tag++;
1990		} else
1991			cmd->tag = (unsigned char) tag;
1992		tmp[2] = cmd->tag;
1993		hostdata->last_message[0] = SIMPLE_QUEUE_TAG;
1994		len = 3;
1995		AM53C974_write_8(FFREG, tmp[0]);
1996		AM53C974_write_8(FFREG, tmp[1]);
1997		AM53C974_write_8(FFREG, tmp[2]);
1998	} else
1999#endif				/* def SCSI2 */
2000	{
2001		len = 1;
2002		AM53C974_write_8(FFREG, tmp[0]);
2003		cmd->tag = 0;
2004	}
2005
2006/* in case of an inquiry or req. sense command with no sync. neg performed yet, we start
2007   sync negotiation via start stops and transfer the command in cmdout phase */
2008	if (((cmd->cmnd[0] == INQUIRY) || (cmd->cmnd[0] == REQUEST_SENSE)) &&
2009	    !(hostdata->sync_neg[cmd->target]) && hostdata->sync_en[cmd->target]) {
2010		hostdata->sync_neg[cmd->target] = 1;
2011		hostdata->msgout[0] = EXTENDED_MESSAGE;
2012		hostdata->msgout[1] = 3;
2013		hostdata->msgout[2] = EXTENDED_SDTR;
2014		hostdata->msgout[3] = 250 / (int) hostdata->max_rate[cmd->target];
2015		hostdata->msgout[4] = hostdata->max_offset[cmd->target];
2016		len += 5;
2017	}
2018	AM53C974_write_8(SDIDREG, SDIREG_MASK & cmd->target);	/* setup dest. id  */
2019	AM53C974_write_8(STIMREG, DEF_SCSI_TIMEOUT);	/* setup timeout reg */
2020	switch (len) {
2021	case 1:
2022		for (i = 0; i < cmd_size; i++)
2023			AM53C974_write_8(FFREG, cmd->cmnd[i]);
2024		AM53C974_write_8(CMDREG, CMDREG_SAS);	/* select with ATN, 1 msg byte */
2025		hostdata->msgout[0] = NOP;
2026		break;
2027	case 3:
2028		for (i = 0; i < cmd_size; i++)
2029			AM53C974_write_8(FFREG, cmd->cmnd[i]);
2030		AM53C974_write_8(CMDREG, CMDREG_SA3S);	/* select with ATN, 3 msg bytes */
2031		hostdata->msgout[0] = NOP;
2032		break;
2033		default:
2034		    AM53C974_write_8(CMDREG, CMDREG_SASS);	/* select with ATN, stop steps; continue in message out phase */
2035		break;
2036	}
2037}
2038
2039/**************************************************************************
2040* Function : AM53C974_intr_select(struct Scsi_Host *instance, unsigned char statreg)
2041*
2042* Purpose : handle reselection
2043*
2044* Inputs : instance -- which AM53C974
2045*          statreg -- status register
2046*
2047* Returns : nothing
2048*
2049* side effects: manipulates hostdata
2050**************************************************************************/
2051static void AM53C974_intr_reselect(struct Scsi_Host *instance, unsigned char statreg)
2052{
2053	AM53C974_local_declare();
2054	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
2055	unsigned char cfifo, msg[3], lun, t, target = 0;
2056#ifdef SCSI2
2057	unsigned char tag;
2058#endif
2059	Scsi_Cmnd *tmp = NULL, *prev;
2060	AM53C974_setio(instance);
2061
2062	cfifo = AM53C974_cfifo();
2063
2064	if (hostdata->selecting) {
2065		/* caught reselect interrupt in selection process;
2066		   put selecting command back into the issue queue and continue with the
2067		   reselecting command */
2068		DEB_RESEL(printk("AM53C974_intr_reselect: in selection process\n"));
2069		LIST(hostdata->sel_cmd, hostdata->issue_queue);
2070		hostdata->sel_cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
2071		hostdata->issue_queue = hostdata->sel_cmd;
2072		hostdata->sel_cmd = NULL;
2073		hostdata->selecting = 0;
2074	}
2075/* 2 bytes must be in the FIFO now */
2076	if (cfifo != 2) {
2077		printk("scsi %d: error: %d bytes in fifo, 2 expected\n", instance->host_no, cfifo);
2078		hostdata->aborted = 1;
2079		goto EXIT_ABORT;
2080	}
2081/* determine target which reselected */
2082	t = AM53C974_read_8(FFREG);
2083	if (!(t & (1 << instance->this_id))) {
2084		printk("scsi %d: error: invalid host id\n", instance->host_no);
2085		hostdata->aborted = 1;
2086		goto EXIT_ABORT;
2087	}
2088	t ^= (1 << instance->this_id);
2089	target = 0;
2090	while (t != 1) {
2091		t >>= 1;
2092		target++;
2093	}
2094	DEB_RESEL(printk("scsi %d: reselect; target: %d\n", instance->host_no, target));
2095
2096	if (hostdata->aborted)
2097		goto EXIT_ABORT;
2098
2099	if ((statreg & STATREG_PHASE) != PHASE_MSGIN) {
2100		printk("scsi %d: error: upon reselection interrupt not in MSGIN\n", instance->host_no);
2101		hostdata->aborted = 1;
2102		goto EXIT_ABORT;
2103	}
2104	msg[0] = AM53C974_read_8(FFREG);
2105	if (!msg[0] & 0x80) {
2106		printk("scsi%d: error: expecting IDENTIFY message, got ", instance->host_no);
2107		print_msg(msg);
2108		hostdata->aborted = 1;
2109		goto EXIT_ABORT;
2110	}
2111	lun = (msg[0] & 0x07);
2112
2113/* We need to add code for SCSI-II to track which devices have
2114 * I_T_L_Q nexuses established, and which have simple I_T_L
2115 * nexuses so we can chose to do additional data transfer. */
2116#ifdef SCSI2
2117#error "SCSI-II tagged queueing is not supported yet"
2118#endif
2119
2120/* Find the command corresponding to the I_T_L or I_T_L_Q  nexus we
2121 * just reestablished, and remove it from the disconnected queue. */
2122	for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
2123	     tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
2124		if ((target == tmp->target) && (lun == tmp->lun)
2125#ifdef SCSI2
2126		    && (tag == tmp->tag)
2127#endif
2128		    ) {
2129			if (prev) {
2130				REMOVE(prev, (Scsi_Cmnd *) (prev->host_scribble), tmp,
2131				     (Scsi_Cmnd *) (tmp->host_scribble));
2132				prev->host_scribble = tmp->host_scribble;
2133			} else {
2134				REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
2135				hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2136			}
2137			tmp->host_scribble = NULL;
2138			hostdata->connected = tmp;
2139			break;
2140		}
2141	if (!tmp) {
2142#ifdef SCSI2
2143		printk("scsi%d: warning : target %d lun %d tag %d not in disconnect_queue.\n",
2144		       instance->host_no, target, lun, tag);
2145#else
2146		printk("scsi%d: warning : target %d lun %d not in disconnect_queue.\n",
2147		       instance->host_no, target, lun);
2148#endif
2149		/* Since we have an established nexus that we can't do anything with, we must abort it. */
2150		hostdata->aborted = 1;
2151		DEB(AM53C974_keywait());
2152		goto EXIT_ABORT;
2153	} else
2154		goto EXIT_OK;
2155
2156	EXIT_ABORT:
2157	    AM53C974_write_8(CMDREG, CMDREG_SATN);
2158	AM53C974_write_8(CMDREG, CMDREG_MA);
2159	return;
2160
2161	EXIT_OK:
2162	    DEB_RESEL(printk("scsi%d: nexus established, target = %d, lun = %d, tag = %d\n",
2163			 instance->host_no, target, tmp->lun, tmp->tag));
2164	AM53C974_set_sync(instance, target);
2165	AM53C974_write_8(SDIDREG, SDIREG_MASK & target);	/* setup dest. id  */
2166	AM53C974_write_8(CMDREG, CMDREG_MA);
2167	hostdata->dma_busy = 0;
2168	hostdata->connected->SCp.phase = PHASE_CMDOUT;
2169}
2170
2171/**************************************************************************
2172* Function : AM53C974_transfer_dma(struct Scsi_Host *instance, short dir,
2173*                                  unsigned long length, char *data)
2174*
2175* Purpose : setup DMA transfer
2176*
2177* Inputs : instance -- which AM53C974
2178*          dir -- direction flag, 0: write to device, read from memory;
2179*                                 1: read from device, write to memory
2180*          length -- number of bytes to transfer to from buffer
2181*          data -- pointer to data buffer
2182*
2183* Returns : nothing
2184**************************************************************************/
2185static __inline__ void AM53C974_transfer_dma(struct Scsi_Host *instance, short dir,
2186					unsigned long length, char *data)
2187{
2188	AM53C974_local_declare();
2189	AM53C974_setio(instance);
2190
2191	AM53C974_write_8(CMDREG, CMDREG_NOP);
2192	AM53C974_write_8(DMACMD, (dir << 7) | DMACMD_INTE_D);	/* idle command */
2193	AM53C974_write_8(STCLREG, (unsigned char) (length & 0xff));
2194	AM53C974_write_8(STCMREG, (unsigned char) ((length & 0xff00) >> 8));
2195	AM53C974_write_8(STCHREG, (unsigned char) ((length & 0xff0000) >> 16));
2196	AM53C974_write_32(DMASTC, length & 0xffffff);
2197	AM53C974_write_32(DMASPA, virt_to_bus(data));
2198	AM53C974_write_8(CMDREG, CMDREG_IT | CMDREG_DMA);
2199	AM53C974_write_8(DMACMD, (dir << 7) | DMACMD_INTE_D | DMACMD_START);
2200}
2201
2202/**************************************************************************
2203* Function : AM53C974_dma_blast(struct Scsi_Host *instance, unsigned char dmastatus,
2204*                               unsigned char statreg)
2205*
2206* Purpose : cleanup DMA transfer
2207*
2208* Inputs : instance -- which AM53C974
2209*          dmastatus -- dma status register
2210*          statreg -- status register
2211*
2212* Returns : nothing
2213**************************************************************************/
2214static void AM53C974_dma_blast(struct Scsi_Host *instance, unsigned char dmastatus,
2215			       unsigned char statreg)
2216{
2217	AM53C974_local_declare();
2218	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
2219	unsigned long ctcreg;
2220	int dir = statreg & STATREG_IO;
2221	int cfifo, pio, i = 0;
2222	AM53C974_setio(instance);
2223
2224	do {
2225		cfifo = AM53C974_cfifo();
2226		i++;
2227	} while (cfifo && (i < 50000));
2228	pio = (i == 50000) ? 1 : 0;
2229
2230	if (statreg & STATREG_CTZ) {
2231		AM53C974_write_8(DMACMD, DMACMD_IDLE);
2232		return;
2233	}
2234	if (dmastatus & DMASTATUS_DONE) {
2235		AM53C974_write_8(DMACMD, DMACMD_IDLE);
2236		return;
2237	}
2238	AM53C974_write_8(DMACMD, ((dir << 7) & DMACMD_DIR) | DMACMD_BLAST);
2239	while (!(AM53C974_read_8(DMASTATUS) & DMASTATUS_BCMPLT));
2240	AM53C974_write_8(DMACMD, DMACMD_IDLE);
2241
2242	if (pio) {
2243		/* transfer residual bytes via PIO */
2244		unsigned char *wac = (unsigned char *) AM53C974_read_32(DMAWAC);
2245		printk("pio mode, residual=%d\n", AM53C974_read_8(CFIREG) & CFIREG_CF);
2246		while (AM53C974_read_8(CFIREG) & CFIREG_CF)
2247			*(wac++) = AM53C974_read_8(FFREG);
2248	}
2249	ctcreg = AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
2250	    (AM53C974_read_8(CTCHREG) << 16);
2251
2252	hostdata->connected->SCp.ptr += hostdata->connected->SCp.this_residual - ctcreg;
2253	hostdata->connected->SCp.this_residual = ctcreg;
2254}
2255
2256/**************************************************************************
2257* Function : AM53C974_intr_bus_reset(struct Scsi_Host *instance)
2258*
2259* Purpose : handle bus reset interrupt
2260*
2261* Inputs : instance -- which AM53C974
2262*
2263* Returns : nothing
2264**************************************************************************/
2265static void AM53C974_intr_bus_reset(struct Scsi_Host *instance)
2266{
2267	AM53C974_local_declare();
2268	unsigned char cntlreg1;
2269	AM53C974_setio(instance);
2270
2271	AM53C974_write_8(CMDREG, CMDREG_CFIFO);
2272	AM53C974_write_8(CMDREG, CMDREG_NOP);
2273
2274	cntlreg1 = AM53C974_read_8(CNTLREG1);
2275	AM53C974_write_8(CNTLREG1, cntlreg1 | CNTLREG1_DISR);
2276}
2277
2278/**************************************************************************
2279* Function : int AM53C974_abort(Scsi_Cmnd *cmd)
2280*
2281* Purpose : abort a command
2282*
2283* Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2284* 	host byte of the result field to, if zero DID_ABORTED is
2285*	used.
2286*
2287* Returns : 0 - success, -1 on failure.
2288 **************************************************************************/
2289static int AM53C974_abort(Scsi_Cmnd * cmd)
2290{
2291	AM53C974_local_declare();
2292	unsigned long flags;
2293	struct Scsi_Host *instance = cmd->host;
2294	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
2295	Scsi_Cmnd *tmp, **prev;
2296
2297#ifdef AM53C974_DEBUG
2298	deb_stop = 1;
2299#endif
2300	save_flags(flags);
2301	cli();
2302	AM53C974_setio(instance);
2303
2304	DEB_ABORT(printk(SEPARATOR_LINE));
2305	DEB_ABORT(printk("scsi%d : AM53C974_abort called -- trouble starts!!\n", instance->host_no));
2306	DEB_ABORT(AM53C974_print(instance));
2307	DEB_ABORT(AM53C974_keywait());
2308
2309/* Case 1 : If the command is the currently executing command,
2310   we'll set the aborted flag and return control so that the
2311   information transfer routine can exit cleanly. */
2312	if ((hostdata->connected == cmd) || (hostdata->sel_cmd == cmd)) {
2313		DEB_ABORT(printk("scsi%d: aborting connected command\n", instance->host_no));
2314		hostdata->aborted = 1;
2315		hostdata->msgout[0] = ABORT;
2316		restore_flags(flags);
2317		return (SCSI_ABORT_PENDING);
2318	}
2319/* Case 2 : If the command hasn't been issued yet,
2320   we simply remove it from the issue queue. */
2321	for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue),
2322	     tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp;
2323	     prev = (Scsi_Cmnd **) & (tmp->host_scribble),
2324	     tmp = (Scsi_Cmnd *) tmp->host_scribble) {
2325		if (cmd == tmp) {
2326			DEB_ABORT(printk("scsi%d : abort removed command from issue queue.\n", instance->host_no));
2327			REMOVE(5, *prev, tmp, tmp->host_scribble);
2328			(*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2329			tmp->host_scribble = NULL;
2330			tmp->result = DID_ABORT << 16;
2331			restore_flags(flags);
2332			tmp->done(tmp);
2333			return (SCSI_ABORT_SUCCESS);
2334		}
2335#ifdef AM53C974_DEBUG_ABORT
2336		else {
2337			if (prev == (Scsi_Cmnd **) tmp)
2338				printk("scsi%d : LOOP\n", instance->host_no);
2339		}
2340#endif
2341	}
2342
2343/* Case 3 : If any commands are connected, we're going to fail the abort
2344 *        and let the high level SCSI driver retry at a later time or
2345 *          issue a reset.
2346 *
2347 *          Timeouts, and therefore aborted commands, will be highly unlikely
2348 *          and handling them cleanly in this situation would make the common
2349 *          case of noresets less efficient, and would pollute our code.  So,
2350 *          we fail. */
2351	if (hostdata->connected || hostdata->sel_cmd) {
2352		DEB_ABORT(printk("scsi%d : abort failed, other command connected.\n", instance->host_no));
2353		restore_flags(flags);
2354		return (SCSI_ABORT_NOT_RUNNING);
2355	}
2356/* Case 4: If the command is currently disconnected from the bus, and
2357 *       there are no connected commands, we reconnect the I_T_L or
2358 *         I_T_L_Q nexus associated with it, go into message out, and send
2359 *         an abort message. */
2360	for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
2361	     tmp = (Scsi_Cmnd *) tmp->host_scribble) {
2362		if (cmd == tmp) {
2363			DEB_ABORT(printk("scsi%d: aborting disconnected command\n", instance->host_no));
2364			hostdata->aborted = 1;
2365			hostdata->msgout[0] = ABORT;
2366			hostdata->selecting = 1;
2367			hostdata->sel_cmd = tmp;
2368			AM53C974_write_8(CMDREG, CMDREG_DSR);
2369			restore_flags(flags);
2370			return (SCSI_ABORT_PENDING);
2371		}
2372	}
2373
2374/* Case 5 : If we reached this point, the command was not found in any of
2375 *        the queues.
2376 *
2377 * We probably reached this point because of an unlikely race condition
2378 * between the command completing successfully and the abortion code,
2379 * so we won't panic, but we will notify the user in case something really
2380 * broke. */
2381	DEB_ABORT(printk("scsi%d : abort failed, command not found.\n", instance->host_no));
2382	restore_flags(flags);
2383	return (SCSI_ABORT_NOT_RUNNING);
2384}
2385
2386static int AM53C974_reset(Scsi_Cmnd * cmd, unsigned int reset_flags)
2387{
2388	AM53C974_local_declare();
2389	unsigned long flags;
2390	int i;
2391	struct Scsi_Host *instance = cmd->host;
2392	struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
2393	AM53C974_setio(instance);
2394
2395	save_flags(flags);
2396	cli();
2397	DEB(printk("AM53C974_reset called; "));
2398
2399	printk("AM53C974_reset called\n");
2400	AM53C974_print(instance);
2401	AM53C974_keywait();
2402
2403/* do hard reset */
2404	AM53C974_write_8(CMDREG, CMDREG_RDEV);
2405	AM53C974_write_8(CMDREG, CMDREG_NOP);
2406	hostdata->msgout[0] = NOP;
2407	for (i = 0; i < 8; i++) {
2408		hostdata->busy[i] = 0;
2409		hostdata->sync_per[i] = DEF_STP;
2410		hostdata->sync_off[i] = 0;
2411		hostdata->sync_neg[i] = 0;
2412	}
2413	hostdata->last_message[0] = NOP;
2414	hostdata->sel_cmd = NULL;
2415	hostdata->connected = NULL;
2416	hostdata->issue_queue = NULL;
2417	hostdata->disconnected_queue = NULL;
2418	hostdata->in_reset = 0;
2419	hostdata->aborted = 0;
2420	hostdata->selecting = 0;
2421	hostdata->disconnecting = 0;
2422	hostdata->dma_busy = 0;
2423
2424/* reset bus */
2425	AM53C974_write_8(CNTLREG1, CNTLREG1_DISR | instance->this_id);	/* disable interrupt upon SCSI RESET */
2426	AM53C974_write_8(CMDREG, CMDREG_RBUS);	/* reset SCSI bus */
2427	udelay(40);
2428	AM53C974_config_after_reset(instance);
2429
2430	restore_flags(flags);
2431	cmd->result = DID_RESET << 16;
2432	cmd->scsi_done(cmd);
2433	return SCSI_ABORT_SUCCESS;
2434}
2435
2436
2437/*
2438 * AM53C974_release()
2439 *
2440 * Release resources allocated for a single AM53C974 adapter.
2441 */
2442static int AM53C974_release(struct Scsi_Host *shp)
2443{
2444	free_irq(shp->irq, shp);
2445	scsi_unregister(shp);
2446	return 0;
2447}
2448
2449
2450/* You can specify overrides=a,b,c,d in the same format at AM53C974=a,b,c,d
2451   on boot up */
2452MODULE_PARM(overrides, "1-32i");
2453MODULE_LICENSE("GPL");
2454
2455
2456static Scsi_Host_Template driver_template = AM53C974;
2457#include "scsi_module.c"
2458