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