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