Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / scsi / NCR53C9x.c
1 /* NCR53C9x.c:  Generic SCSI driver code for NCR53C9x chips.
2  *
3  * Originally esp.c : EnhancedScsiProcessor Sun SCSI driver code.
4  *
5  * Copyright (C) 1995, 1998 David S. Miller (davem@caip.rutgers.edu)
6  *
7  * Most DMA dependencies put in driver specific files by 
8  * Jesper Skov (jskov@cygnus.co.uk)
9  *
10  * Set up to use esp_read/esp_write (preprocessor macros in NCR53c9x.h) by
11  * Tymm Twillman (tymm@coe.missouri.edu)
12  */
13
14 /* TODO:
15  *
16  * 1) Maybe disable parity checking in config register one for SCSI1
17  *    targets.  (Gilmore says parity error on the SBus can lock up
18  *    old sun4c's)
19  * 2) Add support for DMA2 pipelining.
20  * 3) Add tagged queueing.
21  * 4) Maybe change use of "esp" to something more "NCR"'ish.
22  */
23
24 #ifdef MODULE
25 #include <linux/module.h>
26 #endif
27
28 #include <linux/config.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/malloc.h>
34 #include <linux/blk.h>
35 #include <linux/proc_fs.h>
36 #include <linux/stat.h>
37 #include <linux/init.h>
38
39 #include "scsi.h"
40 #include "hosts.h"
41 #include "NCR53C9x.h"
42
43 #include <asm/system.h>
44 #include <asm/ptrace.h>
45 #include <asm/pgtable.h>
46 #include <asm/io.h>
47 #include <asm/irq.h>
48
49 /* Command phase enumeration. */
50 enum {
51         not_issued    = 0x00,  /* Still in the issue_SC queue.          */
52
53         /* Various forms of selecting a target. */
54 #define in_slct_mask    0x10
55         in_slct_norm  = 0x10,  /* ESP is arbitrating, normal selection  */
56         in_slct_stop  = 0x11,  /* ESP will select, then stop with IRQ   */
57         in_slct_msg   = 0x12,  /* select, then send a message           */
58         in_slct_tag   = 0x13,  /* select and send tagged queue msg      */
59         in_slct_sneg  = 0x14,  /* select and acquire sync capabilities  */
60
61         /* Any post selection activity. */
62 #define in_phases_mask  0x20
63         in_datain     = 0x20,  /* Data is transferring from the bus     */
64         in_dataout    = 0x21,  /* Data is transferring to the bus       */
65         in_data_done  = 0x22,  /* Last DMA data operation done (maybe)  */
66         in_msgin      = 0x23,  /* Eating message from target            */
67         in_msgincont  = 0x24,  /* Eating more msg bytes from target     */
68         in_msgindone  = 0x25,  /* Decide what to do with what we got    */
69         in_msgout     = 0x26,  /* Sending message to target             */
70         in_msgoutdone = 0x27,  /* Done sending msg out                  */
71         in_cmdbegin   = 0x28,  /* Sending cmd after abnormal selection  */
72         in_cmdend     = 0x29,  /* Done sending slow cmd                 */
73         in_status     = 0x2a,  /* Was in status phase, finishing cmd    */
74         in_freeing    = 0x2b,  /* freeing the bus for cmd cmplt or disc */
75         in_the_dark   = 0x2c,  /* Don't know what bus phase we are in   */
76
77         /* Special states, ie. not normal bus transitions... */
78 #define in_spec_mask    0x80
79         in_abortone   = 0x80,  /* Aborting one command currently        */
80         in_abortall   = 0x81,  /* Blowing away all commands we have     */
81         in_resetdev   = 0x82,  /* SCSI target reset in progress         */
82         in_resetbus   = 0x83,  /* SCSI bus reset in progress            */
83         in_tgterror   = 0x84,  /* Target did something stupid           */
84 };
85
86 enum {
87         /* Zero has special meaning, see skipahead[12]. */
88 /*0*/   do_never,
89
90 /*1*/   do_phase_determine,
91 /*2*/   do_reset_bus,
92 /*3*/   do_reset_complete,
93 /*4*/   do_work_bus,
94 /*5*/   do_intr_end
95 };
96
97 /* The master ring of all esp hosts we are managing in this driver. */
98 struct NCR_ESP *espchain = 0;
99 int nesps = 0, esps_in_use = 0, esps_running = 0;
100
101 void esp_intr(int irq, void *dev_id, struct pt_regs *pregs);
102
103 /* Debugging routines */
104 struct esp_cmdstrings {
105         unchar cmdchar;
106         char *text;
107 } esp_cmd_strings[] = {
108         /* Miscellaneous */
109         { ESP_CMD_NULL, "ESP_NOP", },
110         { ESP_CMD_FLUSH, "FIFO_FLUSH", },
111         { ESP_CMD_RC, "RSTESP", },
112         { ESP_CMD_RS, "RSTSCSI", },
113         /* Disconnected State Group */
114         { ESP_CMD_RSEL, "RESLCTSEQ", },
115         { ESP_CMD_SEL, "SLCTNATN", },
116         { ESP_CMD_SELA, "SLCTATN", },
117         { ESP_CMD_SELAS, "SLCTATNSTOP", },
118         { ESP_CMD_ESEL, "ENSLCTRESEL", },
119         { ESP_CMD_DSEL, "DISSELRESEL", },
120         { ESP_CMD_SA3, "SLCTATN3", },
121         { ESP_CMD_RSEL3, "RESLCTSEQ", },
122         /* Target State Group */
123         { ESP_CMD_SMSG, "SNDMSG", },
124         { ESP_CMD_SSTAT, "SNDSTATUS", },
125         { ESP_CMD_SDATA, "SNDDATA", },
126         { ESP_CMD_DSEQ, "DISCSEQ", },
127         { ESP_CMD_TSEQ, "TERMSEQ", },
128         { ESP_CMD_TCCSEQ, "TRGTCMDCOMPSEQ", },
129         { ESP_CMD_DCNCT, "DISC", },
130         { ESP_CMD_RMSG, "RCVMSG", },
131         { ESP_CMD_RCMD, "RCVCMD", },
132         { ESP_CMD_RDATA, "RCVDATA", },
133         { ESP_CMD_RCSEQ, "RCVCMDSEQ", },
134         /* Initiator State Group */
135         { ESP_CMD_TI, "TRANSINFO", },
136         { ESP_CMD_ICCSEQ, "INICMDSEQCOMP", },
137         { ESP_CMD_MOK, "MSGACCEPTED", },
138         { ESP_CMD_TPAD, "TPAD", },
139         { ESP_CMD_SATN, "SATN", },
140         { ESP_CMD_RATN, "RATN", },
141 };
142 #define NUM_ESP_COMMANDS  ((sizeof(esp_cmd_strings)) / (sizeof(struct esp_cmdstrings)))
143
144 /* Print textual representation of an ESP command */
145 static inline void esp_print_cmd(unchar espcmd)
146 {
147         unchar dma_bit = espcmd & ESP_CMD_DMA;
148         int i;
149
150         espcmd &= ~dma_bit;
151         for(i=0; i<NUM_ESP_COMMANDS; i++)
152                 if(esp_cmd_strings[i].cmdchar == espcmd)
153                         break;
154         if(i==NUM_ESP_COMMANDS)
155                 printk("ESP_Unknown");
156         else
157                 printk("%s%s", esp_cmd_strings[i].text,
158                        ((dma_bit) ? "+DMA" : ""));
159 }
160
161 /* Print the status register's value */
162 static inline void esp_print_statreg(unchar statreg)
163 {
164         unchar phase;
165
166         printk("STATUS<");
167         phase = statreg & ESP_STAT_PMASK;
168         printk("%s,", (phase == ESP_DOP ? "DATA-OUT" :
169                        (phase == ESP_DIP ? "DATA-IN" :
170                         (phase == ESP_CMDP ? "COMMAND" :
171                          (phase == ESP_STATP ? "STATUS" :
172                           (phase == ESP_MOP ? "MSG-OUT" :
173                            (phase == ESP_MIP ? "MSG_IN" :
174                             "unknown")))))));
175         if(statreg & ESP_STAT_TDONE)
176                 printk("TRANS_DONE,");
177         if(statreg & ESP_STAT_TCNT)
178                 printk("TCOUNT_ZERO,");
179         if(statreg & ESP_STAT_PERR)
180                 printk("P_ERROR,");
181         if(statreg & ESP_STAT_SPAM)
182                 printk("SPAM,");
183         if(statreg & ESP_STAT_INTR)
184                 printk("IRQ,");
185         printk(">");
186 }
187
188 /* Print the interrupt register's value */
189 static inline void esp_print_ireg(unchar intreg)
190 {
191         printk("INTREG< ");
192         if(intreg & ESP_INTR_S)
193                 printk("SLCT_NATN ");
194         if(intreg & ESP_INTR_SATN)
195                 printk("SLCT_ATN ");
196         if(intreg & ESP_INTR_RSEL)
197                 printk("RSLCT ");
198         if(intreg & ESP_INTR_FDONE)
199                 printk("FDONE ");
200         if(intreg & ESP_INTR_BSERV)
201                 printk("BSERV ");
202         if(intreg & ESP_INTR_DC)
203                 printk("DISCNCT ");
204         if(intreg & ESP_INTR_IC)
205                 printk("ILL_CMD ");
206         if(intreg & ESP_INTR_SR)
207                 printk("SCSI_BUS_RESET ");
208         printk(">");
209 }
210
211 /* Print the sequence step registers contents */
212 static inline void esp_print_seqreg(unchar stepreg)
213 {
214         stepreg &= ESP_STEP_VBITS;
215         printk("STEP<%s>",
216                (stepreg == ESP_STEP_ASEL ? "SLCT_ARB_CMPLT" :
217                 (stepreg == ESP_STEP_SID ? "1BYTE_MSG_SENT" :
218                  (stepreg == ESP_STEP_NCMD ? "NOT_IN_CMD_PHASE" :
219                   (stepreg == ESP_STEP_PPC ? "CMD_BYTES_LOST" :
220                    (stepreg == ESP_STEP_FINI4 ? "CMD_SENT_OK" :
221                     "UNKNOWN"))))));
222 }
223
224 static char *phase_string(int phase)
225 {
226         switch(phase) {
227         case not_issued:
228                 return "UNISSUED";
229         case in_slct_norm:
230                 return "SLCTNORM";
231         case in_slct_stop:
232                 return "SLCTSTOP";
233         case in_slct_msg:
234                 return "SLCTMSG";
235         case in_slct_tag:
236                 return "SLCTTAG";
237         case in_slct_sneg:
238                 return "SLCTSNEG";
239         case in_datain:
240                 return "DATAIN";
241         case in_dataout:
242                 return "DATAOUT";
243         case in_data_done:
244                 return "DATADONE";
245         case in_msgin:
246                 return "MSGIN";
247         case in_msgincont:
248                 return "MSGINCONT";
249         case in_msgindone:
250                 return "MSGINDONE";
251         case in_msgout:
252                 return "MSGOUT";
253         case in_msgoutdone:
254                 return "MSGOUTDONE";
255         case in_cmdbegin:
256                 return "CMDBEGIN";
257         case in_cmdend:
258                 return "CMDEND";
259         case in_status:
260                 return "STATUS";
261         case in_freeing:
262                 return "FREEING";
263         case in_the_dark:
264                 return "CLUELESS";
265         case in_abortone:
266                 return "ABORTONE";
267         case in_abortall:
268                 return "ABORTALL";
269         case in_resetdev:
270                 return "RESETDEV";
271         case in_resetbus:
272                 return "RESETBUS";
273         case in_tgterror:
274                 return "TGTERROR";
275         default:
276                 return "UNKNOWN";
277         };
278 }
279
280 #ifdef DEBUG_STATE_MACHINE
281 static inline void esp_advance_phase(Scsi_Cmnd *s, int newphase)
282 {
283         ESPLOG(("<%s>", phase_string(newphase)));
284         s->SCp.sent_command = s->SCp.phase;
285         s->SCp.phase = newphase;
286 }
287 #else
288 #define esp_advance_phase(__s, __newphase) \
289         (__s)->SCp.sent_command = (__s)->SCp.phase; \
290         (__s)->SCp.phase = (__newphase);
291 #endif
292
293 #ifdef DEBUG_ESP_CMDS
294 extern inline void esp_cmd(struct NCR_ESP *esp, struct ESP_regs *eregs,
295                            unchar cmd)
296 {
297         esp->espcmdlog[esp->espcmdent] = cmd;
298         esp->espcmdent = (esp->espcmdent + 1) & 31;
299         esp_write(eregs->esp_cmnd, cmd);
300 }
301 #else
302 #define esp_cmd(__esp, __eregs, __cmd)  esp_write((__eregs)->esp_cmnd, (__cmd))
303 #endif
304
305 /* How we use the various Linux SCSI data structures for operation.
306  *
307  * struct scsi_cmnd:
308  *
309  *   We keep track of the syncronous capabilities of a target
310  *   in the device member, using sync_min_period and
311  *   sync_max_offset.  These are the values we directly write
312  *   into the ESP registers while running a command.  If offset
313  *   is zero the ESP will use asynchronous transfers.
314  *   If the borken flag is set we assume we shouldn't even bother
315  *   trying to negotiate for synchronous transfer as this target
316  *   is really stupid.  If we notice the target is dropping the
317  *   bus, and we have been allowing it to disconnect, we clear
318  *   the disconnect flag.
319  */
320
321 /* Manipulation of the ESP command queues.  Thanks to the aha152x driver
322  * and its author, Juergen E. Fischer, for the methods used here.
323  * Note that these are per-ESP queues, not global queues like
324  * the aha152x driver uses.
325  */
326 static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
327 {
328         Scsi_Cmnd *end;
329
330         new_SC->host_scribble = (unsigned char *) NULL;
331         if(!*SC)
332                 *SC = new_SC;
333         else {
334                 for(end=*SC;end->host_scribble;end=(Scsi_Cmnd *)end->host_scribble)
335                         ;
336                 end->host_scribble = (unsigned char *) new_SC;
337         }
338 }
339
340 static inline void prepend_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
341 {
342         new_SC->host_scribble = (unsigned char *) *SC;
343         *SC = new_SC;
344 }
345
346 static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd **SC)
347 {
348         Scsi_Cmnd *ptr;
349
350         ptr = *SC;
351         if(ptr)
352                 *SC = (Scsi_Cmnd *) (*SC)->host_scribble;
353         return ptr;
354 }
355
356 static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, int target, int lun)
357 {
358         Scsi_Cmnd *ptr, *prev;
359
360         for(ptr = *SC, prev = NULL;
361             ptr && ((ptr->target != target) || (ptr->lun != lun));
362             prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble)
363                 ;
364         if(ptr) {
365                 if(prev)
366                         prev->host_scribble=ptr->host_scribble;
367                 else
368                         *SC=(Scsi_Cmnd *)ptr->host_scribble;
369         }
370         return ptr;
371 }
372
373 /* Resetting various pieces of the ESP scsi driver chipset */
374
375 /* Reset the ESP chip, _not_ the SCSI bus. */
376 static void esp_reset_esp(struct NCR_ESP *esp, struct ESP_regs *eregs)
377 {
378         int family_code, version, i;
379         volatile int trash;
380
381         /* Now reset the ESP chip */
382         esp_cmd(esp, eregs, ESP_CMD_RC);
383         esp_cmd(esp, eregs, ESP_CMD_NULL | ESP_CMD_DMA);
384         if(esp->erev == fast)
385                 esp_write(eregs->esp_cfg2, ESP_CONFIG2_FENAB);
386         esp_cmd(esp, eregs, ESP_CMD_NULL | ESP_CMD_DMA);
387
388         /* This is the only point at which it is reliable to read
389          * the ID-code for a fast ESP chip variant.
390          */
391         esp->max_period = ((35 * esp->ccycle) / 1000);
392         if(esp->erev == fast) {
393                 char *erev2string[] = {
394                         "Emulex FAS236",
395                         "Emulex FPESP100A",
396                         "fast",
397                         "QLogic FAS366",
398                         "Emulex FAS216",
399                         "Symbios Logic 53CF9x-2",
400                         "unknown!"
401                 };
402                         
403                 version = esp_read(eregs->esp_uid);
404                 family_code = (version & 0xf8) >> 3;
405                 if(family_code == 0x02) {
406                         if ((version & 7) == 2)
407                                 esp->erev = fas216;     
408                         else
409                                 esp->erev = fas236;
410                 } else if(family_code == 0x0a)
411                         esp->erev = fas366; /* Version is usually '5'. */
412                 else if(family_code == 0x00) {
413                         if ((version & 7) == 2)
414                                 esp->erev = fas100a; /* NCR53C9X */
415                         else
416                                 esp->erev = espunknown;
417                 } else if(family_code == 0x14) {
418                         if ((version & 7) == 2)
419                                 esp->erev = fsc;
420                         else
421                                 esp->erev = espunknown;
422                 } else if(family_code == 0x00) {
423                         if ((version & 7) == 2)
424                                 esp->erev = fas100a; /* NCR53C9X */
425                         else
426                                 esp->erev = espunknown;
427                 } else
428                         esp->erev = espunknown;
429                 ESPLOG(("esp%d: FAST chip is %s (family=%d, version=%d)\n",
430                         esp->esp_id, erev2string[esp->erev - fas236],
431                         family_code, (version & 7)));
432
433                 esp->min_period = ((4 * esp->ccycle) / 1000);
434         } else {
435                 esp->min_period = ((5 * esp->ccycle) / 1000);
436         }
437
438         /* Reload the configuration registers */
439         esp_write(eregs->esp_cfact, esp->cfact);
440         esp->prev_stp = 0;
441         esp_write(eregs->esp_stp, 0);
442         esp->prev_soff = 0;
443         esp_write(eregs->esp_soff, 0);
444         esp_write(eregs->esp_timeo, esp->neg_defp);
445         esp->max_period = (esp->max_period + 3)>>2;
446         esp->min_period = (esp->min_period + 3)>>2;
447
448         esp_write(eregs->esp_cfg1, esp->config1);
449         switch(esp->erev) {
450         case esp100:
451                 /* nothing to do */
452                 break;
453         case esp100a:
454                 esp_write(eregs->esp_cfg2, esp->config2);
455                 break;
456         case esp236:
457                 /* Slow 236 */
458                 esp_write(eregs->esp_cfg2, esp->config2);
459                 esp->prev_cfg3 = esp->config3[0];
460                 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
461                 break;
462         case fas366:
463                 panic("esp: FAS366 support not present, please notify "
464                       "jongk@cs.utwente.nl");
465                 break;
466         case fas216:
467         case fas236:
468         case fsc:
469                 /* Fast ESP variants */
470                 esp_write(eregs->esp_cfg2, esp->config2);
471                 for(i=0; i<8; i++)
472                         esp->config3[i] |= ESP_CONFIG3_FCLK;
473                 esp->prev_cfg3 = esp->config3[0];
474                 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
475                 if(esp->diff)
476                         esp->radelay = 0;
477                 else
478                         esp->radelay = 16;
479                 /* Different timeout constant for these chips */
480                 esp->neg_defp =
481                         FSC_NEG_DEFP(esp->cfreq,
482                                      (esp->cfact == ESP_CCF_F0 ?
483                                       ESP_CCF_F7 + 1 : esp->cfact));
484                 esp_write(eregs->esp_timeo, esp->neg_defp);
485                 /* Enable Active Negotiation if possible */
486                 if((esp->erev == fsc) && !esp->diff)
487                         esp_write(eregs->esp_cfg4, ESP_CONFIG4_EAN);
488                 break;
489         case fas100a:
490                 /* Fast 100a */
491                 esp_write(eregs->esp_cfg2, esp->config2);
492                 for(i=0; i<8; i++)
493                         esp->config3[i] |= ESP_CONFIG3_FCLOCK;
494                 esp->prev_cfg3 = esp->config3[0];
495                 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
496                 esp->radelay = 32;
497                 break;
498         default:
499                 panic("esp: what could it be... I wonder...");
500                 break;
501         };
502
503         /* Eat any bitrot in the chip */
504         trash = esp_read(eregs->esp_intrpt);
505         udelay(100);
506 }
507
508 /* This places the ESP into a known state at boot time. */
509 void esp_bootup_reset(struct NCR_ESP *esp, struct ESP_regs *eregs)
510 {
511         volatile unchar trash;
512
513         /* Reset the DMA */
514         if(esp->dma_reset)
515                 esp->dma_reset(esp);
516
517         /* Reset the ESP */
518         esp_reset_esp(esp, eregs);
519
520         /* Reset the SCSI bus, but tell ESP not to generate an irq */
521         esp_write(eregs->esp_cfg1, (esp_read(eregs->esp_cfg1) | ESP_CONFIG1_SRRDISAB));
522         esp_cmd(esp, eregs, ESP_CMD_RS);
523         udelay(400);
524         esp_write(eregs->esp_cfg1, esp->config1);
525
526         /* Eat any bitrot in the chip and we are done... */
527         trash = esp_read(eregs->esp_intrpt);
528 }
529
530 /* Allocate structure and insert basic data such as SCSI chip frequency
531  * data and a pointer to the device
532  */
533 struct NCR_ESP* esp_allocate(Scsi_Host_Template *tpnt, void *esp_dev)
534 {
535         struct NCR_ESP *esp, *elink;
536         struct Scsi_Host *esp_host;
537
538         esp_host = scsi_register(tpnt, sizeof(struct NCR_ESP));
539         if(!esp_host)
540                 panic("Cannot register ESP SCSI host");
541         esp = (struct NCR_ESP *) esp_host->hostdata;
542         if(!esp)
543                 panic("No esp in hostdata");
544         esp->ehost = esp_host;
545         esp->edev = esp_dev;
546         esp->esp_id = nesps++;
547
548         /* Set bitshift value (only used on Amiga with multiple ESPs) */
549         esp->shift = 2;
550
551         /* Put into the chain of esp chips detected */
552         if(espchain) {
553                 elink = espchain;
554                 while(elink->next) elink = elink->next;
555                 elink->next = esp;
556         } else {
557                 espchain = esp;
558         }
559         esp->next = 0;
560
561         return esp;
562 }
563
564 void esp_deallocate(struct NCR_ESP *esp)
565 {
566         struct NCR_ESP *elink;
567
568         if(espchain == esp) {
569                 espchain = 0;
570         } else {
571                 for(elink = espchain; elink && (elink->next != esp); elink = elink->next);
572                 if(elink) 
573                         elink->next = esp->next;
574         }
575         nesps--;
576 }
577
578 /* Complete initialization of ESP structure and device
579  * Caller must have initialized appropriate parts of the ESP structure
580  * between the call to esp_allocate and this function.
581  */
582 void esp_initialize(struct NCR_ESP *esp)
583 {
584         struct ESP_regs *eregs = esp->eregs;
585         unsigned int fmhz;
586         unchar ccf;
587         int i;
588         
589         /* Check out the clock properties of the chip. */
590
591         /* This is getting messy but it has to be done
592          * correctly or else you get weird behavior all
593          * over the place.  We are trying to basically
594          * figure out three pieces of information.
595          *
596          * a) Clock Conversion Factor
597          *
598          *    This is a representation of the input
599          *    crystal clock frequency going into the
600          *    ESP on this machine.  Any operation whose
601          *    timing is longer than 400ns depends on this
602          *    value being correct.  For example, you'll
603          *    get blips for arbitration/selection during
604          *    high load or with multiple targets if this
605          *    is not set correctly.
606          *
607          * b) Selection Time-Out
608          *
609          *    The ESP isn't very bright and will arbitrate
610          *    for the bus and try to select a target
611          *    forever if you let it.  This value tells
612          *    the ESP when it has taken too long to
613          *    negotiate and that it should interrupt
614          *    the CPU so we can see what happened.
615          *    The value is computed as follows (from
616          *    NCR/Symbios chip docs).
617          *
618          *          (Time Out Period) *  (Input Clock)
619          *    STO = ----------------------------------
620          *          (8192) * (Clock Conversion Factor)
621          *
622          *    You usually want the time out period to be
623          *    around 250ms, I think we'll set it a little
624          *    bit higher to account for fully loaded SCSI
625          *    bus's and slow devices that don't respond so
626          *    quickly to selection attempts. (yeah, I know
627          *    this is out of spec. but there is a lot of
628          *    buggy pieces of firmware out there so bite me)
629          *
630          * c) Imperical constants for synchronous offset
631          *    and transfer period register values
632          *
633          *    This entails the smallest and largest sync
634          *    period we could ever handle on this ESP.
635          */
636         
637         fmhz = esp->cfreq;
638
639         if(fmhz <= (5000000))
640                 ccf = 0;
641         else
642                 ccf = (((5000000 - 1) + (fmhz))/(5000000));
643         if(!ccf || ccf > 8) {
644                 /* If we can't find anything reasonable,
645                  * just assume 20MHZ.  This is the clock
646                  * frequency of the older sun4c's where I've
647                  * been unable to find the clock-frequency
648                  * PROM property.  All other machines provide
649                  * useful values it seems.
650                  */
651                 ccf = ESP_CCF_F4;
652                 fmhz = (20000000);
653         }
654         if(ccf==(ESP_CCF_F7+1))
655                 esp->cfact = ESP_CCF_F0;
656         else if(ccf == ESP_CCF_NEVER)
657                 esp->cfact = ESP_CCF_F2;
658         else
659                 esp->cfact = ccf;
660         esp->cfreq = fmhz;
661         esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz);
662         esp->ctick = ESP_TICK(ccf, esp->ccycle);
663         esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf);
664         esp->sync_defp = SYNC_DEFP_SLOW;
665
666         printk("SCSI ID %d Clk %dMHz CCF=%d TOut %d ",
667                esp->scsi_id, (esp->cfreq / 1000000),
668                ccf, (int) esp->neg_defp);
669
670         /* Fill in ehost data */
671         esp->ehost->base = (unsigned long)eregs;
672         esp->ehost->this_id = esp->scsi_id;
673         esp->ehost->irq = esp->irq;
674
675         /* SCSI id mask */
676         esp->scsi_id_mask = (1 << esp->scsi_id);
677
678         /* Probe the revision of this esp */
679         esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
680         esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
681         esp_write(eregs->esp_cfg2, esp->config2);
682         if((esp_read(eregs->esp_cfg2) & ~(ESP_CONFIG2_MAGIC)) !=
683            (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
684                 printk("NCR53C90(esp100)\n");
685                 esp->erev = esp100;
686         } else {
687                 esp->config2 = 0;
688                 esp_write(eregs->esp_cfg2, 0);
689                 esp_write(eregs->esp_cfg3, 5);
690                 if(esp_read(eregs->esp_cfg3) != 5) {
691                         printk("NCR53C90A(esp100a)\n");
692                         esp->erev = esp100a;
693                 } else {
694                         int target;
695
696                         for(target=0; target<8; target++)
697                                 esp->config3[target] = 0;
698                         esp->prev_cfg3 = 0;
699                         esp_write(eregs->esp_cfg3, 0);
700                         if(ccf > ESP_CCF_F5) {
701                                 printk("NCR53C9XF(espfast)\n");
702                                 esp->erev = fast;
703                                 esp->sync_defp = SYNC_DEFP_FAST;
704                         } else {
705                                 printk("NCR53C9x(esp236)\n");
706                                 esp->erev = esp236;
707                         }
708                 }
709         }                               
710
711         /* Initialize the command queues */
712         esp->current_SC = 0;
713         esp->disconnected_SC = 0;
714         esp->issue_SC = 0;
715
716         /* Clear the state machines. */
717         esp->targets_present = 0;
718         esp->resetting_bus = 0;
719         esp->snip = 0;
720         esp->fas_premature_intr_workaround = 0;
721         for(i = 0; i < 32; i++)
722                 esp->espcmdlog[i] = 0;
723         esp->espcmdent = 0;
724         for(i = 0; i < 16; i++) {
725                 esp->cur_msgout[i] = 0;
726                 esp->cur_msgin[i] = 0;
727         }
728         esp->prevmsgout = esp->prevmsgin = 0;
729         esp->msgout_len = esp->msgin_len = 0;
730
731         /* Clear the one behind caches to hold unmatchable values. */
732         esp->prev_soff = esp->prev_stp = esp->prev_cfg3 = 0xff;
733
734         /* Reset the thing before we try anything... */
735         esp_bootup_reset(esp, eregs);
736
737 #ifdef MODULE
738         MOD_INC_USE_COUNT;
739 #endif
740         esps_in_use++;
741 }
742
743 /* The info function will return whatever useful
744  * information the developer sees fit.  If not provided, then
745  * the name field will be used instead.
746  */
747 const char *esp_info(struct Scsi_Host *host)
748 {
749         struct NCR_ESP *esp;
750
751         esp = (struct NCR_ESP *) host->hostdata;
752         switch(esp->erev) {
753         case esp100:
754                 return "ESP100 (NCR53C90)";
755         case esp100a:
756                 return "ESP100A (NCR53C90A)";
757         case esp236:
758                 return "ESP236 (NCR53C9x)";
759         case fas216:
760                 return "Emulex FAS216";
761         case fas236:
762                 return "Emulex FAS236";
763         case fas366:
764                 return "QLogic FAS366";
765         case fas100a:
766                 return "FPESP100A";
767         case fsc:
768                 return "Symbios Logic 53CF9x-2";
769         default:
770                 panic("Bogon ESP revision");
771         };
772 }
773
774 /* From Wolfgang Stanglmeier's NCR scsi driver. */
775 struct info_str
776 {
777         char *buffer;
778         int length;
779         int offset;
780         int pos;
781 };
782
783 static void copy_mem_info(struct info_str *info, char *data, int len)
784 {
785         if (info->pos + len > info->length)
786                 len = info->length - info->pos;
787
788         if (info->pos + len < info->offset) {
789                 info->pos += len;
790                 return;
791         }
792         if (info->pos < info->offset) {
793                 data += (info->offset - info->pos);
794                 len  -= (info->offset - info->pos);
795         }
796
797         if (len > 0) {
798                 memcpy(info->buffer + info->pos, data, len);
799                 info->pos += len;
800         }
801 }
802
803 static int copy_info(struct info_str *info, char *fmt, ...)
804 {
805         va_list args;
806         char buf[81];
807         int len;
808
809         va_start(args, fmt);
810         len = vsprintf(buf, fmt, args);
811         va_end(args);
812
813         copy_mem_info(info, buf, len);
814         return len;
815 }
816
817 static int esp_host_info(struct NCR_ESP *esp, char *ptr, off_t offset, int len)
818 {
819         struct info_str info;
820         int i;
821
822         info.buffer     = ptr;
823         info.length     = len;
824         info.offset     = offset;
825         info.pos        = 0;
826
827         copy_info(&info, "ESP Host Adapter:\n");
828         copy_info(&info, "\tESP Model\t\t");
829         switch(esp->erev) {
830         case esp100:
831                 copy_info(&info, "ESP100 (NCR53C90)\n");
832                 break;
833         case esp100a:
834                 copy_info(&info, "ESP100A (NCR53C90A)\n");
835                 break;
836         case esp236:
837                 copy_info(&info, "ESP236 (NCR53C9x)\n");
838                 break;
839         case fas216:
840                 copy_info(&info, "Emulex FAS216\n");
841                 break;
842         case fas236:
843                 copy_info(&info, "Emulex FAS236\n");
844                 break;
845         case fas100a:
846                 copy_info(&info, "FPESP100A\n");
847                 break;
848         case fast:
849                 copy_info(&info, "Generic FAST\n");
850                 break;
851         case fas366:
852                 copy_info(&info, "QLogic FAS366\n");
853                 break;
854         case fsc:
855                 copy_info(&info, "Symbios Logic 53C9x-2\n");
856                 break;
857         case espunknown:
858         default:
859                 copy_info(&info, "Unknown!\n");
860                 break;
861         };
862         copy_info(&info, "\tLive Targets\t\t[ ");
863         for(i = 0; i < 15; i++) {
864                 if(esp->targets_present & (1 << i))
865                         copy_info(&info, "%d ", i);
866         }
867         copy_info(&info, "]\n\n");
868         
869         /* Now describe the state of each existing target. */
870         copy_info(&info, "Target #\tconfig3\t\tSync Capabilities\tDisconnect\n");
871         for(i = 0; i < 15; i++) {
872                 if(esp->targets_present & (1 << i)) {
873                         Scsi_Device *SDptr = esp->ehost->host_queue;
874
875                         while((SDptr->host != esp->ehost) &&
876                               (SDptr->id != i) &&
877                               (SDptr->next))
878                                 SDptr = SDptr->next;
879
880                         copy_info(&info, "%d\t\t", i);
881                         copy_info(&info, "%08lx\t", esp->config3[i]);
882                         copy_info(&info, "[%02lx,%02lx]\t\t\t", SDptr->sync_max_offset,
883                                   SDptr->sync_min_period);
884                         copy_info(&info, "%s\n", SDptr->disconnect ? "yes" : "no");
885                 }
886         }
887
888         return info.pos > info.offset? info.pos - info.offset : 0;
889 }
890
891 /* ESP proc filesystem code. */
892 int esp_proc_info(char *buffer, char **start, off_t offset, int length,
893                   int hostno, int inout)
894 {
895         struct NCR_ESP *esp;
896
897         if(inout)
898                 return -EINVAL; /* not yet */
899
900         for_each_esp(esp) {
901                 if(esp->ehost->host_no == hostno)
902                         break;
903         }
904         if(!esp)
905                 return -EINVAL;
906
907         if(start)
908                 *start = buffer;
909
910         return esp_host_info(esp, buffer, offset, length);
911 }
912
913 static void esp_get_dmabufs(struct NCR_ESP *esp, Scsi_Cmnd *sp)
914 {
915         if(sp->use_sg == 0) {
916                 sp->SCp.this_residual = sp->request_bufflen;
917                 sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
918                 sp->SCp.buffers_residual = 0;
919                 if (esp->dma_mmu_get_scsi_one)
920                         esp->dma_mmu_get_scsi_one(esp, sp);
921                 else
922                         sp->SCp.have_data_in = (int) sp->SCp.ptr =
923                                 (char *) virt_to_phys(sp->request_buffer);
924         } else {
925                 sp->SCp.buffer = (struct scatterlist *) sp->buffer;
926                 sp->SCp.buffers_residual = sp->use_sg - 1;
927                 sp->SCp.this_residual = sp->SCp.buffer->length;
928                 if (esp->dma_mmu_get_scsi_sgl)
929                         esp->dma_mmu_get_scsi_sgl(esp, sp);
930                 else
931                         sp->SCp.ptr =
932                                 (char *) virt_to_phys(sp->SCp.buffer->address);
933         }
934 }
935
936 static void esp_release_dmabufs(struct NCR_ESP *esp, Scsi_Cmnd *sp)
937 {
938         if(sp->use_sg == 0) {
939                 if (esp->dma_mmu_release_scsi_one)
940                         esp->dma_mmu_release_scsi_one(esp, sp);
941         } else {
942                 if (esp->dma_mmu_release_scsi_sgl)
943                         esp->dma_mmu_release_scsi_sgl(esp, sp);
944         }
945 }
946
947 static void esp_restore_pointers(struct NCR_ESP *esp, Scsi_Cmnd *sp)
948 {
949         struct esp_pointers *ep = &esp->data_pointers[sp->target];
950
951         sp->SCp.ptr = ep->saved_ptr;
952         sp->SCp.buffer = ep->saved_buffer;
953         sp->SCp.this_residual = ep->saved_this_residual;
954         sp->SCp.buffers_residual = ep->saved_buffers_residual;
955 }
956
957 static void esp_save_pointers(struct NCR_ESP *esp, Scsi_Cmnd *sp)
958 {
959         struct esp_pointers *ep = &esp->data_pointers[sp->target];
960
961         ep->saved_ptr = sp->SCp.ptr;
962         ep->saved_buffer = sp->SCp.buffer;
963         ep->saved_this_residual = sp->SCp.this_residual;
964         ep->saved_buffers_residual = sp->SCp.buffers_residual;
965 }
966
967 /* Some rules:
968  *
969  *   1) Never ever panic while something is live on the bus.
970  *      If there is to be any chance of syncing the disks this
971  *      rule is to be obeyed.
972  *
973  *   2) Any target that causes a foul condition will no longer
974  *      have synchronous transfers done to it, no questions
975  *      asked.
976  *
977  *   3) Keep register accesses to a minimum.  Think about some
978  *      day when we have Xbus machines this is running on and
979  *      the ESP chip is on the other end of the machine on a
980  *      different board from the cpu where this is running.
981  */
982
983 /* Fire off a command.  We assume the bus is free and that the only
984  * case where we could see an interrupt is where we have disconnected
985  * commands active and they are trying to reselect us.
986  */
987 static inline void esp_check_cmd(struct NCR_ESP *esp, Scsi_Cmnd *sp)
988 {
989         switch(sp->cmd_len) {
990         case 6:
991         case 10:
992         case 12:
993                 esp->esp_slowcmd = 0;
994                 break;
995
996         default:
997                 esp->esp_slowcmd = 1;
998                 esp->esp_scmdleft = sp->cmd_len;
999                 esp->esp_scmdp = &sp->cmnd[0];
1000                 break;
1001         };
1002 }
1003
1004 static inline void build_sync_nego_msg(struct NCR_ESP *esp, int period, int offset)
1005 {
1006         esp->cur_msgout[0] = EXTENDED_MESSAGE;
1007         esp->cur_msgout[1] = 3;
1008         esp->cur_msgout[2] = EXTENDED_SDTR;
1009         esp->cur_msgout[3] = period;
1010         esp->cur_msgout[4] = offset;
1011         esp->msgout_len = 5;
1012 }
1013
1014 static void esp_exec_cmd(struct NCR_ESP *esp)
1015 {
1016         struct ESP_regs *eregs = esp->eregs;
1017         Scsi_Cmnd *SCptr;
1018         Scsi_Device *SDptr;
1019         volatile unchar *cmdp = esp->esp_command;
1020         unsigned char the_esp_command;
1021         int lun, target;
1022         int i;
1023
1024         /* Hold off if we have disconnected commands and
1025          * an IRQ is showing...
1026          */
1027         if(esp->disconnected_SC && esp->dma_irq_p(esp))
1028                 return;
1029
1030         /* Grab first member of the issue queue. */
1031         SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
1032
1033         /* Safe to panic here because current_SC is null. */
1034         if(!SCptr)
1035                 panic("esp: esp_exec_cmd and issue queue is NULL");
1036
1037         SDptr = SCptr->device;
1038         lun = SCptr->lun;
1039         target = SCptr->target;
1040
1041         esp->snip = 0;
1042         esp->msgout_len = 0;
1043
1044         /* Send it out whole, or piece by piece?   The ESP
1045          * only knows how to automatically send out 6, 10,
1046          * and 12 byte commands.  I used to think that the
1047          * Linux SCSI code would never throw anything other
1048          * than that to us, but then again there is the
1049          * SCSI generic driver which can send us anything.
1050          */
1051         esp_check_cmd(esp, SCptr);
1052
1053         /* If arbitration/selection is successful, the ESP will leave
1054          * ATN asserted, causing the target to go into message out
1055          * phase.  The ESP will feed the target the identify and then
1056          * the target can only legally go to one of command,
1057          * datain/out, status, or message in phase, or stay in message
1058          * out phase (should we be trying to send a sync negotiation
1059          * message after the identify).  It is not allowed to drop
1060          * BSY, but some buggy targets do and we check for this
1061          * condition in the selection complete code.  Most of the time
1062          * we'll make the command bytes available to the ESP and it
1063          * will not interrupt us until it finishes command phase, we
1064          * cannot do this for command sizes the ESP does not
1065          * understand and in this case we'll get interrupted right
1066          * when the target goes into command phase.
1067          *
1068          * It is absolutely _illegal_ in the presence of SCSI-2 devices
1069          * to use the ESP select w/o ATN command.  When SCSI-2 devices are
1070          * present on the bus we _must_ always go straight to message out
1071          * phase with an identify message for the target.  Being that
1072          * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
1073          * selections should not confuse SCSI-1 we hope.
1074          */
1075
1076         if(SDptr->sync) {
1077                 /* this targets sync is known */
1078 #ifdef CONFIG_SCSI_MAC_ESP
1079 do_sync_known:
1080 #endif
1081                 if(SDptr->disconnect)
1082                         *cmdp++ = IDENTIFY(1, lun);
1083                 else
1084                         *cmdp++ = IDENTIFY(0, lun);
1085
1086                 if(esp->esp_slowcmd) {
1087                         the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1088                         esp_advance_phase(SCptr, in_slct_stop);
1089                 } else {
1090                         the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1091                         esp_advance_phase(SCptr, in_slct_norm);
1092                 }
1093         } else if(!(esp->targets_present & (1<<target)) || !(SDptr->disconnect)) {
1094                 /* After the bootup SCSI code sends both the
1095                  * TEST_UNIT_READY and INQUIRY commands we want
1096                  * to at least attempt allowing the device to
1097                  * disconnect.
1098                  */
1099                 ESPMISC(("esp: Selecting device for first time. target=%d "
1100                          "lun=%d\n", target, SCptr->lun));
1101                 if(!SDptr->borken && !SDptr->disconnect)
1102                         SDptr->disconnect = 1;
1103
1104                 *cmdp++ = IDENTIFY(0, lun);
1105                 esp->prevmsgout = NOP;
1106                 esp_advance_phase(SCptr, in_slct_norm);
1107                 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1108
1109                 /* Take no chances... */
1110                 SDptr->sync_max_offset = 0;
1111                 SDptr->sync_min_period = 0;
1112         } else {
1113                 int toshiba_cdrom_hwbug_wkaround = 0;
1114
1115 #ifdef CONFIG_SCSI_MAC_ESP
1116                 /* Never allow synchronous transfers (disconnect OK) on
1117                  * Macintosh. Well, maybe later when we figured out how to 
1118                  * do DMA on the machines that support it ...
1119                  */
1120                 SDptr->disconnect = 1;
1121                 SDptr->sync_max_offset = 0;
1122                 SDptr->sync_min_period = 0;
1123                 SDptr->sync = 1;
1124                 esp->snip = 0;
1125                 goto do_sync_known;
1126 #endif
1127                 /* We've talked to this guy before,
1128                  * but never negotiated.  Let's try
1129                  * sync negotiation.
1130                  */
1131                 if(!SDptr->borken) {
1132                         if((SDptr->type == TYPE_ROM) &&
1133                            (!strncmp(SDptr->vendor, "TOSHIBA", 7))) {
1134                                 /* Nice try sucker... */
1135                                 ESPMISC(("esp%d: Disabling sync for buggy "
1136                                          "Toshiba CDROM.\n", esp->esp_id));
1137                                 toshiba_cdrom_hwbug_wkaround = 1;
1138                                 build_sync_nego_msg(esp, 0, 0);
1139                         } else {
1140                                 build_sync_nego_msg(esp, esp->sync_defp, 15);
1141                         }
1142                 } else {
1143                         build_sync_nego_msg(esp, 0, 0);
1144                 }
1145                 SDptr->sync = 1;
1146                 esp->snip = 1;
1147
1148                 /* A fix for broken SCSI1 targets, when they disconnect
1149                  * they lock up the bus and confuse ESP.  So disallow
1150                  * disconnects for SCSI1 targets for now until we
1151                  * find a better fix.
1152                  *
1153                  * Addendum: This is funny, I figured out what was going
1154                  *           on.  The blotzed SCSI1 target would disconnect,
1155                  *           one of the other SCSI2 targets or both would be
1156                  *           disconnected as well.  The SCSI1 target would
1157                  *           stay disconnected long enough that we start
1158                  *           up a command on one of the SCSI2 targets.  As
1159                  *           the ESP is arbitrating for the bus the SCSI1
1160                  *           target begins to arbitrate as well to reselect
1161                  *           the ESP.  The SCSI1 target refuses to drop it's
1162                  *           ID bit on the data bus even though the ESP is
1163                  *           at ID 7 and is the obvious winner for any
1164                  *           arbitration.  The ESP is a poor sport and refuses
1165                  *           to lose arbitration, it will continue indefinately
1166                  *           trying to arbitrate for the bus and can only be
1167                  *           stopped via a chip reset or SCSI bus reset.
1168                  *           Therefore _no_ disconnects for SCSI1 targets
1169                  *           thank you very much. ;-)
1170                  */
1171                 if(((SDptr->scsi_level < 3) && (SDptr->type != TYPE_TAPE)) ||
1172                    toshiba_cdrom_hwbug_wkaround || SDptr->borken) {
1173                         ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
1174                                  "lun %d\n", esp->esp_id, SCptr->target, SCptr->lun));
1175                         SDptr->disconnect = 0;
1176                         *cmdp++ = IDENTIFY(0, lun);
1177                 } else {
1178                         *cmdp++ = IDENTIFY(1, lun);
1179                 }
1180
1181                 /* ESP fifo is only so big...
1182                  * Make this look like a slow command.
1183                  */
1184                 esp->esp_slowcmd = 1;
1185                 esp->esp_scmdleft = SCptr->cmd_len;
1186                 esp->esp_scmdp = &SCptr->cmnd[0];
1187
1188                 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1189                 esp_advance_phase(SCptr, in_slct_msg);
1190         }
1191
1192         if(!esp->esp_slowcmd)
1193                 for(i = 0; i < SCptr->cmd_len; i++)
1194                         *cmdp++ = SCptr->cmnd[i];
1195
1196         esp_write(eregs->esp_busid, (target & 7));
1197         if (esp->prev_soff != SDptr->sync_max_offset ||
1198             esp->prev_stp  != SDptr->sync_min_period ||
1199             (esp->erev > esp100a &&
1200              esp->prev_cfg3 != esp->config3[target])) {
1201                 esp->prev_soff = SDptr->sync_max_offset;
1202                 esp_write(eregs->esp_soff, esp->prev_soff);
1203                 esp->prev_stp = SDptr->sync_min_period;
1204                 esp_write(eregs->esp_stp, esp->prev_stp); 
1205                 if(esp->erev > esp100a) {
1206                         esp->prev_cfg3 = esp->config3[target];
1207                         esp_write(eregs->esp_cfg3, esp->prev_cfg3);
1208                 }
1209         }
1210         i = (cmdp - esp->esp_command);
1211
1212         /* Set up the DMA and ESP counters */
1213         if(esp->do_pio_cmds){
1214                 int j = 0;
1215
1216                 /* 
1217                  * XXX MSch:
1218                  *
1219                  * It seems this is required, at least to clean up
1220                  * after failed commands when using PIO mode ...
1221                  */
1222                 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1223
1224                 for(;j<i;j++)
1225                         esp_write(eregs->esp_fdata, esp->esp_command[j]);
1226                 the_esp_command &= ~ESP_CMD_DMA;
1227
1228                 /* Tell ESP to "go". */
1229                 esp_cmd(esp, eregs, the_esp_command);
1230         } else {
1231                 /* Set up the ESP counters */
1232                 esp_write(eregs->esp_tclow, i);
1233                 esp_write(eregs->esp_tcmed, 0);
1234                 esp->dma_init_write(esp, esp->esp_command_dvma, i);
1235
1236                 /* Tell ESP to "go". */
1237                 esp_cmd(esp, eregs, the_esp_command);
1238         }
1239 }
1240
1241 /* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
1242 int esp_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
1243 {
1244         struct NCR_ESP *esp;
1245
1246         /* Set up func ptr and initial driver cmd-phase. */
1247         SCpnt->scsi_done = done;
1248         SCpnt->SCp.phase = not_issued;
1249
1250         esp = (struct NCR_ESP *) SCpnt->host->hostdata;
1251
1252         if(esp->dma_led_on)
1253                 esp->dma_led_on(esp);
1254
1255         /* We use the scratch area. */
1256         ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->target, SCpnt->lun));
1257         ESPDISC(("N<%02x,%02x>", SCpnt->target, SCpnt->lun));
1258
1259         esp_get_dmabufs(esp, SCpnt);
1260         esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
1261
1262         SCpnt->SCp.Status           = CHECK_CONDITION;
1263         SCpnt->SCp.Message          = 0xff;
1264         SCpnt->SCp.sent_command     = 0;
1265
1266         /* Place into our queue. */
1267         if(SCpnt->cmnd[0] == REQUEST_SENSE) {
1268                 ESPQUEUE(("RQSENSE\n"));
1269                 prepend_SC(&esp->issue_SC, SCpnt);
1270         } else {
1271                 ESPQUEUE(("\n"));
1272                 append_SC(&esp->issue_SC, SCpnt);
1273         }
1274
1275         /* Run it now if we can. */
1276         if(!esp->current_SC && !esp->resetting_bus)
1277                 esp_exec_cmd(esp);
1278
1279         return 0;
1280 }
1281
1282 /* Only queuing supported in this ESP driver. */
1283 int esp_command(Scsi_Cmnd *SCpnt)
1284 {
1285 #ifdef DEBUG_ESP
1286         struct NCR_ESP *esp = (struct NCR_ESP *) SCpnt->host->hostdata;
1287 #endif
1288
1289         ESPLOG(("esp%d: esp_command() called...\n", esp->esp_id));
1290         return -1;
1291 }
1292
1293 /* Dump driver state. */
1294 static void esp_dump_cmd(Scsi_Cmnd *SCptr)
1295 {
1296         ESPLOG(("[tgt<%02x> lun<%02x> "
1297                 "pphase<%s> cphase<%s>]",
1298                 SCptr->target, SCptr->lun,
1299                 phase_string(SCptr->SCp.sent_command),
1300                 phase_string(SCptr->SCp.phase)));
1301 }
1302
1303 static void esp_dump_state(struct NCR_ESP *esp, 
1304                            struct ESP_regs *eregs)
1305 {
1306         Scsi_Cmnd *SCptr = esp->current_SC;
1307 #ifdef DEBUG_ESP_CMDS
1308         int i;
1309 #endif
1310
1311         ESPLOG(("esp%d: dumping state\n", esp->esp_id));
1312         
1313         /* Print DMA status */
1314         esp->dma_dump_state(esp);
1315
1316         ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1317                 esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
1318         ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1319                 esp->esp_id, esp_read(eregs->esp_status), esp_read(eregs->esp_sstep),
1320                 esp_read(eregs->esp_intrpt)));
1321 #ifdef DEBUG_ESP_CMDS
1322         printk("esp%d: last ESP cmds [", esp->esp_id);
1323         i = (esp->espcmdent - 1) & 31;
1324         printk("<");
1325         esp_print_cmd(esp->espcmdlog[i]);
1326         printk(">");
1327         i = (i - 1) & 31;
1328         printk("<");
1329         esp_print_cmd(esp->espcmdlog[i]);
1330         printk(">");
1331         i = (i - 1) & 31;
1332         printk("<");
1333         esp_print_cmd(esp->espcmdlog[i]);
1334         printk(">");
1335         i = (i - 1) & 31;
1336         printk("<");
1337         esp_print_cmd(esp->espcmdlog[i]);
1338         printk(">");
1339         printk("]\n");
1340 #endif /* (DEBUG_ESP_CMDS) */
1341
1342         if(SCptr) {
1343                 ESPLOG(("esp%d: current command ", esp->esp_id));
1344                 esp_dump_cmd(SCptr);
1345         }
1346         ESPLOG(("\n"));
1347         SCptr = esp->disconnected_SC;
1348         ESPLOG(("esp%d: disconnected ", esp->esp_id));
1349         while(SCptr) {
1350                 esp_dump_cmd(SCptr);
1351                 SCptr = (Scsi_Cmnd *) SCptr->host_scribble;
1352         }
1353         ESPLOG(("\n"));
1354 }
1355
1356 /* Abort a command. */
1357 int esp_abort(Scsi_Cmnd *SCptr)
1358 {
1359         struct NCR_ESP *esp = (struct NCR_ESP *) SCptr->host->hostdata;
1360         struct ESP_regs *eregs = esp->eregs;
1361         int don;
1362
1363         ESPLOG(("esp%d: Aborting command\n", esp->esp_id));
1364         esp_dump_state(esp, eregs);
1365
1366         /* Wheee, if this is the current command on the bus, the
1367          * best we can do is assert ATN and wait for msgout phase.
1368          * This should even fix a hung SCSI bus when we lose state
1369          * in the driver and timeout because the eventual phase change
1370          * will cause the ESP to (eventually) give an interrupt.
1371          */
1372         if(esp->current_SC == SCptr) {
1373                 esp->cur_msgout[0] = ABORT;
1374                 esp->msgout_len = 1;
1375                 esp->msgout_ctr = 0;
1376                 esp_cmd(esp, eregs, ESP_CMD_SATN);
1377                 return SCSI_ABORT_PENDING;
1378         }
1379
1380         /* If it is still in the issue queue then we can safely
1381          * call the completion routine and report abort success.
1382          */
1383         don = esp->dma_ports_p(esp);
1384         if(don) {
1385                 esp->dma_ints_off(esp);
1386                 synchronize_irq();
1387         }
1388         if(esp->issue_SC) {
1389                 Scsi_Cmnd **prev, *this;
1390                 for(prev = (&esp->issue_SC), this = esp->issue_SC;
1391                     this;
1392                     prev = (Scsi_Cmnd **) &(this->host_scribble),
1393                     this = (Scsi_Cmnd *) this->host_scribble) {
1394                         if(this == SCptr) {
1395                                 *prev = (Scsi_Cmnd *) this->host_scribble;
1396                                 this->host_scribble = NULL;
1397                                 esp_release_dmabufs(esp, this);
1398                                 this->result = DID_ABORT << 16;
1399                                 this->done(this);
1400                                 if(don)
1401                                         esp->dma_ints_on(esp);
1402                                 return SCSI_ABORT_SUCCESS;
1403                         }
1404                 }
1405         }
1406
1407         /* Yuck, the command to abort is disconnected, it is not
1408          * worth trying to abort it now if something else is live
1409          * on the bus at this time.  So, we let the SCSI code wait
1410          * a little bit and try again later.
1411          */
1412         if(esp->current_SC) {
1413                 if(don)
1414                         esp->dma_ints_on(esp);
1415                 return SCSI_ABORT_BUSY;
1416         }
1417
1418         /* It's disconnected, we have to reconnect to re-establish
1419          * the nexus and tell the device to abort.  However, we really
1420          * cannot 'reconnect' per se, therefore we tell the upper layer
1421          * the safest thing we can.  This is, wait a bit, if nothing
1422          * happens, we are really hung so reset the bus.
1423          */
1424
1425         if(don)
1426                 esp->dma_ints_on(esp);
1427         return SCSI_ABORT_SNOOZE;
1428 }
1429
1430 /* We've sent ESP_CMD_RS to the ESP, the interrupt had just
1431  * arrived indicating the end of the SCSI bus reset.  Our job
1432  * is to clean out the command queues and begin re-execution
1433  * of SCSI commands once more.
1434  */
1435 static int esp_finish_reset(struct NCR_ESP *esp,
1436                             struct ESP_regs *eregs)
1437 {
1438         Scsi_Cmnd *sp = esp->current_SC;
1439
1440         /* Clean up currently executing command, if any. */
1441         if (sp != NULL) {
1442                 esp_release_dmabufs(esp, sp);
1443                 sp->result = (DID_RESET << 16);
1444                 sp->scsi_done(sp);
1445                 esp->current_SC = NULL;
1446         }
1447
1448         /* Clean up disconnected queue, they have been invalidated
1449          * by the bus reset.
1450          */
1451         if (esp->disconnected_SC) {
1452                 while((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
1453                         esp_release_dmabufs(esp, sp);
1454                         sp->result = (DID_RESET << 16);
1455                         sp->scsi_done(sp);
1456                 }
1457         }
1458
1459         /* SCSI bus reset is complete. */
1460         esp->resetting_bus = 0;
1461
1462         /* Ok, now it is safe to get commands going once more. */
1463         if(esp->issue_SC)
1464                 esp_exec_cmd(esp);
1465
1466         return do_intr_end;
1467 }
1468
1469 static int esp_do_resetbus(struct NCR_ESP *esp,
1470                            struct ESP_regs *eregs)
1471 {
1472         ESPLOG(("esp%d: Resetting scsi bus\n", esp->esp_id));
1473         esp->resetting_bus = 1;
1474         esp_cmd(esp, eregs, ESP_CMD_RS);
1475
1476         return do_intr_end;
1477 }
1478
1479 /* Reset ESP chip, reset hanging bus, then kill active and
1480  * disconnected commands for targets without soft reset.
1481  */
1482 int esp_reset(Scsi_Cmnd *SCptr, unsigned int how)
1483 {
1484         struct NCR_ESP *esp = (struct NCR_ESP *) SCptr->host->hostdata;
1485
1486         (void) esp_do_resetbus(esp, esp->eregs);
1487         return SCSI_RESET_PENDING;
1488 }
1489
1490 /* Internal ESP done function. */
1491 static void esp_done(struct NCR_ESP *esp, int error)
1492 {
1493         Scsi_Cmnd *done_SC;
1494
1495         if(esp->current_SC) {
1496                 done_SC = esp->current_SC;
1497                 esp->current_SC = NULL;
1498                 esp_release_dmabufs(esp, done_SC);
1499                 done_SC->result = error;
1500                 done_SC->scsi_done(done_SC);
1501
1502                 /* Bus is free, issue any commands in the queue. */
1503                 if(esp->issue_SC && !esp->current_SC)
1504                         esp_exec_cmd(esp);
1505         } else {
1506                 /* Panic is safe as current_SC is null so we may still
1507                  * be able to accept more commands to sync disk buffers.
1508                  */
1509                 ESPLOG(("panicing\n"));
1510                 panic("esp: done() called with NULL esp->current_SC");
1511         }
1512 }
1513
1514 /* Wheee, ESP interrupt engine. */  
1515
1516 /* Forward declarations. */
1517 static int esp_do_phase_determine(struct NCR_ESP *esp, 
1518                                   struct ESP_regs *eregs);
1519 static int esp_do_data_finale(struct NCR_ESP *esp, struct ESP_regs *eregs);
1520 static int esp_select_complete(struct NCR_ESP *esp, struct ESP_regs *eregs);
1521 static int esp_do_status(struct NCR_ESP *esp, struct ESP_regs *eregs);
1522 static int esp_do_msgin(struct NCR_ESP *esp, struct ESP_regs *eregs);
1523 static int esp_do_msgindone(struct NCR_ESP *esp, struct ESP_regs *eregs);
1524 static int esp_do_msgout(struct NCR_ESP *esp, struct ESP_regs *eregs);
1525 static int esp_do_cmdbegin(struct NCR_ESP *esp, struct ESP_regs *eregs);
1526
1527 #define sreg_datainp(__sreg)  (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
1528 #define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
1529
1530 /* We try to avoid some interrupts by jumping ahead and see if the ESP
1531  * has gotten far enough yet.  Hence the following.
1532  */
1533 static inline int skipahead1(struct NCR_ESP *esp, struct ESP_regs *eregs,
1534                              Scsi_Cmnd *scp, int prev_phase, int new_phase)
1535 {
1536         if(scp->SCp.sent_command != prev_phase)
1537                 return 0;
1538
1539         if(esp->dma_irq_p(esp)) {
1540                 /* Yes, we are able to save an interrupt. */
1541                 esp->sreg = (esp_read(eregs->esp_status) & ~(ESP_STAT_INTR));
1542                 esp->ireg = esp_read(eregs->esp_intrpt);
1543                 if(!(esp->ireg & ESP_INTR_SR))
1544                         return 0;
1545                 else
1546                         return do_reset_complete;
1547         }
1548         /* Ho hum, target is taking forever... */
1549         scp->SCp.sent_command = new_phase; /* so we don't recurse... */
1550         return do_intr_end;
1551 }
1552
1553 static inline int skipahead2(struct NCR_ESP *esp,
1554                              struct ESP_regs *eregs,
1555                              Scsi_Cmnd *scp, int prev_phase1, int prev_phase2,
1556                              int new_phase)
1557 {
1558         if(scp->SCp.sent_command != prev_phase1 &&
1559            scp->SCp.sent_command != prev_phase2)
1560                 return 0;
1561         if(esp->dma_irq_p(esp)) {
1562                 /* Yes, we are able to save an interrupt. */
1563                 esp->sreg = (esp_read(eregs->esp_status) & ~(ESP_STAT_INTR));
1564                 esp->ireg = esp_read(eregs->esp_intrpt);
1565                 if(!(esp->ireg & ESP_INTR_SR))
1566                         return 0;
1567                 else
1568                         return do_reset_complete;
1569         }
1570         /* Ho hum, target is taking forever... */
1571         scp->SCp.sent_command = new_phase; /* so we don't recurse... */
1572         return do_intr_end;
1573 }
1574
1575 /* Misc. esp helper macros. */
1576 #define esp_setcount(__eregs, __cnt) \
1577         esp_write((__eregs)->esp_tclow, ((__cnt) & 0xff)); \
1578         esp_write((__eregs)->esp_tcmed, (((__cnt) >> 8) & 0xff))
1579
1580 #define esp_getcount(__eregs) \
1581         ((esp_read((__eregs)->esp_tclow)&0xff) | \
1582          ((esp_read((__eregs)->esp_tcmed)&0xff) << 8))
1583
1584 #define fcount(__esp, __eregs) \
1585         (esp_read((__eregs)->esp_fflags) & ESP_FF_FBYTES)
1586
1587 #define fnzero(__esp, __eregs) \
1588         (esp_read((__eregs)->esp_fflags) & ESP_FF_ONOTZERO)
1589
1590 /* XXX speculative nops unnecessary when continuing amidst a data phase
1591  * XXX even on esp100!!!  another case of flooding the bus with I/O reg
1592  * XXX writes...
1593  */
1594 #define esp_maybe_nop(__esp, __eregs) \
1595         if((__esp)->erev == esp100) \
1596                 esp_cmd((__esp), (__eregs), ESP_CMD_NULL)
1597
1598 #define sreg_to_dataphase(__sreg) \
1599         ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
1600
1601 /* The ESP100 when in synchronous data phase, can mistake a long final
1602  * REQ pulse from the target as an extra byte, it places whatever is on
1603  * the data lines into the fifo.  For now, we will assume when this
1604  * happens that the target is a bit quirky and we don't want to
1605  * be talking synchronously to it anyways.  Regardless, we need to
1606  * tell the ESP to eat the extraneous byte so that we can proceed
1607  * to the next phase.
1608  */
1609 static inline int esp100_sync_hwbug(struct NCR_ESP *esp, struct ESP_regs *eregs,
1610                                     Scsi_Cmnd *sp, int fifocnt)
1611 {
1612         /* Do not touch this piece of code. */
1613         if((!(esp->erev == esp100)) ||
1614            (!(sreg_datainp((esp->sreg = esp_read(eregs->esp_status))) && !fifocnt) &&
1615             !(sreg_dataoutp(esp->sreg) && !fnzero(esp, eregs)))) {
1616                 if(sp->SCp.phase == in_dataout)
1617                         esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1618                 return 0;
1619         } else {
1620                 /* Async mode for this guy. */
1621                 build_sync_nego_msg(esp, 0, 0);
1622
1623                 /* Ack the bogus byte, but set ATN first. */
1624                 esp_cmd(esp, eregs, ESP_CMD_SATN);
1625                 esp_cmd(esp, eregs, ESP_CMD_MOK);
1626                 return 1;
1627         }
1628 }
1629
1630 /* This closes the window during a selection with a reselect pending, because
1631  * we use DMA for the selection process the FIFO should hold the correct
1632  * contents if we get reselected during this process.  So we just need to
1633  * ack the possible illegal cmd interrupt pending on the esp100.
1634  */
1635 static inline int esp100_reconnect_hwbug(struct NCR_ESP *esp,
1636                                          struct ESP_regs *eregs)
1637 {
1638         volatile unchar junk;
1639
1640         if(esp->erev != esp100)
1641                 return 0;
1642         junk = esp_read(eregs->esp_intrpt);
1643
1644         if(junk & ESP_INTR_SR)
1645                 return 1;
1646         return 0;
1647 }
1648
1649 /* This verifies the BUSID bits during a reselection so that we know which
1650  * target is talking to us.
1651  */
1652 static inline int reconnect_target(struct NCR_ESP *esp, struct ESP_regs *eregs)
1653 {
1654         int it, me = esp->scsi_id_mask, targ = 0;
1655
1656         if(2 != fcount(esp, eregs))
1657                 return -1;
1658         it = esp_read(eregs->esp_fdata);
1659         if(!(it & me))
1660                 return -1;
1661         it &= ~me;
1662         if(it & (it - 1))
1663                 return -1;
1664         while(!(it & 1))
1665                 targ++, it >>= 1;
1666         return targ;
1667 }
1668
1669 /* This verifies the identify from the target so that we know which lun is
1670  * being reconnected.
1671  */
1672 static inline int reconnect_lun(struct NCR_ESP *esp, struct ESP_regs *eregs)
1673 {
1674         int lun;
1675
1676         if((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
1677                 return -1;
1678         lun = esp_read(eregs->esp_fdata);
1679
1680         /* Yes, you read this correctly.  We report lun of zero
1681          * if we see parity error.  ESP reports parity error for
1682          * the lun byte, and this is the only way to hope to recover
1683          * because the target is connected.
1684          */
1685         if(esp->sreg & ESP_STAT_PERR)
1686                 return 0;
1687
1688         /* Check for illegal bits being set in the lun. */
1689         if((lun & 0x40) || !(lun & 0x80))
1690                 return -1;
1691
1692         return lun & 7;
1693 }
1694
1695 /* This puts the driver in a state where it can revitalize a command that
1696  * is being continued due to reselection.
1697  */
1698 static inline void esp_connect(struct NCR_ESP *esp, struct ESP_regs *eregs,
1699                                Scsi_Cmnd *sp)
1700 {
1701         Scsi_Device *dp = sp->device;
1702
1703         if(esp->prev_soff  != dp->sync_max_offset ||
1704            esp->prev_stp   != dp->sync_min_period ||
1705            (esp->erev > esp100a &&
1706             esp->prev_cfg3 != esp->config3[sp->target])) {
1707                 esp->prev_soff = dp->sync_max_offset;
1708                 esp_write(eregs->esp_soff, esp->prev_soff);
1709                 esp->prev_stp = dp->sync_min_period;
1710                 esp_write(eregs->esp_stp, esp->prev_stp);
1711                 if(esp->erev > esp100a) {
1712                         esp->prev_cfg3 = esp->config3[sp->target];
1713                         esp_write(eregs->esp_cfg3, esp->prev_cfg3);
1714                 } 
1715         }
1716         esp->current_SC = sp;
1717 }
1718
1719 /* This will place the current working command back into the issue queue
1720  * if we are to receive a reselection amidst a selection attempt.
1721  */
1722 static inline void esp_reconnect(struct NCR_ESP *esp, Scsi_Cmnd *sp)
1723 {
1724         if(!esp->disconnected_SC)
1725                 ESPLOG(("esp%d: Weird, being reselected but disconnected "
1726                         "command queue is empty.\n", esp->esp_id));
1727         esp->snip = 0;
1728         esp->current_SC = 0;
1729         sp->SCp.phase = not_issued;
1730         append_SC(&esp->issue_SC, sp);
1731 }
1732
1733 /* Begin message in phase. */
1734 static int esp_do_msgin(struct NCR_ESP *esp, struct ESP_regs *eregs)
1735 {
1736         esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1737         esp_maybe_nop(esp, eregs);
1738         esp_cmd(esp, eregs, ESP_CMD_TI);
1739         esp->msgin_len = 1;
1740         esp->msgin_ctr = 0;
1741         esp_advance_phase(esp->current_SC, in_msgindone);
1742         return do_work_bus;
1743 }
1744
1745 static inline void advance_sg(struct NCR_ESP *esp, Scsi_Cmnd *sp)
1746 {
1747         ++sp->SCp.buffer;
1748         --sp->SCp.buffers_residual;
1749         sp->SCp.this_residual = sp->SCp.buffer->length;
1750         if (esp->dma_advance_sg)
1751                 esp->dma_advance_sg (sp);
1752         else
1753                 sp->SCp.ptr = (char *)virt_to_phys(sp->SCp.buffer->address);
1754 }
1755
1756 /* Please note that the way I've coded these routines is that I _always_
1757  * check for a disconnect during any and all information transfer
1758  * phases.  The SCSI standard states that the target _can_ cause a BUS
1759  * FREE condition by dropping all MSG/CD/IO/BSY signals.  Also note
1760  * that during information transfer phases the target controls every
1761  * change in phase, the only thing the initiator can do is "ask" for
1762  * a message out phase by driving ATN true.  The target can, and sometimes
1763  * will, completely ignore this request so we cannot assume anything when
1764  * we try to force a message out phase to abort/reset a target.  Most of
1765  * the time the target will eventually be nice and go to message out, so
1766  * we may have to hold on to our state about what we want to tell the target
1767  * for some period of time.
1768  */
1769
1770 /* I think I have things working here correctly.  Even partial transfers
1771  * within a buffer or sub-buffer should not upset us at all no matter
1772  * how bad the target and/or ESP fucks things up.
1773  */
1774 static int esp_do_data(struct NCR_ESP *esp, struct ESP_regs *eregs)
1775 {
1776         Scsi_Cmnd *SCptr = esp->current_SC;
1777         int thisphase, hmuch;
1778
1779         ESPDATA(("esp_do_data: "));
1780         esp_maybe_nop(esp, eregs);
1781         thisphase = sreg_to_dataphase(esp->sreg);
1782         esp_advance_phase(SCptr, thisphase);
1783         ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
1784         hmuch = esp->dma_can_transfer(esp, SCptr);
1785
1786         /*
1787          * XXX MSch: cater for PIO transfer here; PIO used if hmuch == 0
1788          */
1789         if (hmuch) {    /* DMA */
1790                 /*
1791                  * DMA
1792                  */
1793                 ESPDATA(("hmuch<%d> ", hmuch));
1794                 esp->current_transfer_size = hmuch;
1795                 esp_setcount(eregs, (esp->fas_premature_intr_workaround ?
1796                                      (hmuch + 0x40) : hmuch));
1797                 esp->dma_setup(esp, (__u32)((unsigned long)SCptr->SCp.ptr), 
1798                                hmuch, (thisphase == in_datain));
1799                 ESPDATA(("DMA|TI --> do_intr_end\n"));
1800                 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
1801                 return do_intr_end;
1802                 /*
1803                  * end DMA
1804                  */
1805         } else {
1806                 /*
1807                  * PIO
1808                  */
1809                 int oldphase, i = 0; /* or where we left off last time ?? esp->current_data ?? */
1810                 int fifocnt = 0;
1811
1812                 oldphase = esp_read(eregs->esp_status) & ESP_STAT_PMASK;
1813
1814                 /*
1815                  * polled transfer; ugly, can we make this happen in a DRQ 
1816                  * interrupt handler ??
1817                  * requires keeping track of state information in host or 
1818                  * command struct!
1819                  * Problem: I've never seen a DRQ happen on Mac, not even
1820                  * with ESP_CMD_DMA ...
1821                  */
1822
1823                 /* figure out how much needs to be transfered */
1824                 hmuch = SCptr->SCp.this_residual;
1825                 ESPDATA(("hmuch<%d> pio ", hmuch));
1826                 esp->current_transfer_size = hmuch;
1827
1828                 /* tell the ESP ... */
1829                 esp_setcount(eregs, hmuch);
1830
1831                 /* loop */
1832                 while (hmuch) {
1833                         int j, fifo_stuck = 0, newphase;
1834                         unsigned long flags, timeout;
1835 #if 0
1836                         if ( i % 10 )
1837                                 ESPDATA(("\r"));
1838                         else
1839                                 ESPDATA(( /*"\n"*/ "\r"));
1840 #endif
1841                         save_flags(flags);
1842 #if 0
1843                         cli();
1844 #endif
1845                         if(thisphase == in_datain) {
1846                                 /* 'go' ... */ 
1847                                 esp_cmd(esp, eregs, ESP_CMD_TI);
1848
1849                                 /* wait for data */
1850                                 timeout = 1000000;
1851                                 while (!((esp->sreg=esp_read(eregs->esp_status)) & ESP_STAT_INTR) && --timeout)
1852                                         udelay(2);
1853                                 if (timeout == 0)
1854                                         printk("DRQ datain timeout! \n");
1855
1856                                 newphase = esp->sreg & ESP_STAT_PMASK;
1857
1858                                 /* see how much we got ... */
1859                                 fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
1860
1861                                 if (!fifocnt)
1862                                         fifo_stuck++;
1863                                 else
1864                                         fifo_stuck = 0;
1865
1866                                 ESPDATA(("\rgot %d st %x ph %x", fifocnt, esp->sreg, newphase));
1867
1868                                 /* read fifo */
1869                                 for(j=0;j<fifocnt;j++)
1870                                         SCptr->SCp.ptr[i++] = esp_read(eregs->esp_fdata);
1871
1872                                 ESPDATA(("(%d) ", i));
1873
1874                                 /* how many to go ?? */
1875                                 hmuch -= fifocnt;
1876
1877                                 /* break if status phase !! */
1878                                 if(newphase == ESP_STATP) {
1879                                         /* clear int. */
1880                                         esp->ireg = esp_read(eregs->esp_intrpt);
1881                                         break;
1882                                 }
1883                         } else {
1884 #define MAX_FIFO 8
1885                                 /* how much will fit ? */
1886                                 int this_count = MAX_FIFO - fifocnt;
1887                                 if (this_count > hmuch)
1888                                         this_count = hmuch;
1889
1890                                 /* fill fifo */
1891                                 for(j=0;j<this_count;j++)
1892                                         esp_write(eregs->esp_fdata, SCptr->SCp.ptr[i++]);
1893
1894                                 /* how many left if this goes out ?? */
1895                                 hmuch -= this_count;
1896
1897                                 /* 'go' ... */ 
1898                                 esp_cmd(esp, eregs, ESP_CMD_TI);
1899
1900                                 /* wait for 'got it' */
1901                                 timeout = 1000000;
1902                                 while (!((esp->sreg=esp_read(eregs->esp_status)) & ESP_STAT_INTR) && --timeout)
1903                                         udelay(2);
1904                                 if (timeout == 0)
1905                                         printk("DRQ dataout timeout!  \n");
1906
1907                                 newphase = esp->sreg & ESP_STAT_PMASK;
1908
1909                                 /* need to check how much was sent ?? */
1910                                 fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
1911
1912                                 ESPDATA(("\rsent %d st %x ph %x", this_count - fifocnt, esp->sreg, newphase));
1913
1914                                 ESPDATA(("(%d) ", i));
1915
1916                                 /* break if status phase !! */
1917                                 if(newphase == ESP_STATP) {
1918                                         /* clear int. */
1919                                         esp->ireg = esp_read(eregs->esp_intrpt);
1920                                         break;
1921                                 }
1922
1923                         }
1924
1925                         /* clear int. */
1926                         esp->ireg = esp_read(eregs->esp_intrpt);
1927
1928                         ESPDATA(("ir %x ... ", esp->ireg));
1929
1930                         if (hmuch == 0)
1931                                 ESPDATA(("done! \n"));
1932
1933                         restore_flags(flags);
1934
1935                         /* check new bus phase */
1936                         if (newphase != oldphase && i < esp->current_transfer_size) {
1937                                 /* something happened; disconnect ?? */
1938                                 ESPDATA(("phase change, dropped out with %d done ... ", i));
1939                                 break;
1940                         }
1941
1942                         /* check int. status */
1943                         if (esp->ireg & ESP_INTR_DC) {
1944                                 /* disconnect */
1945                                 ESPDATA(("disconnect; %d transfered ... ", i));
1946                                 break;
1947                         } else if (esp->ireg & ESP_INTR_FDONE) {
1948                                 /* function done */
1949                                 ESPDATA(("function done; %d transfered ... ", i));
1950                                 break;
1951                         }
1952
1953                         /* XXX fixme: bail out on stall */
1954                         if (fifo_stuck > 10) {
1955                                 /* we're stuck */
1956                                 ESPDATA(("fifo stall; %d transfered ... ", i));
1957                                 break;
1958                         }
1959                 }
1960
1961                 ESPDATA(("\n"));
1962                 /* check successful completion ?? */
1963
1964                 if (thisphase == in_dataout)
1965                         hmuch += fifocnt; /* stuck?? adjust data pointer ...*/
1966
1967                 /* tell do_data_finale how much was transfered */
1968                 esp->current_transfer_size -= hmuch;
1969
1970                 /* still not completely sure on this one ... */         
1971                 return /*do_intr_end*/ do_work_bus /*do_phase_determine*/ ;
1972
1973                 /*
1974                  * end PIO
1975                  */
1976         }
1977         return do_intr_end;
1978 }
1979
1980 /* See how successful the data transfer was. */
1981 static int esp_do_data_finale(struct NCR_ESP *esp,
1982                               struct ESP_regs *eregs)
1983 {
1984         Scsi_Cmnd *SCptr = esp->current_SC;
1985         int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
1986
1987         if(esp->dma_led_off)
1988                 esp->dma_led_off(esp);
1989
1990         ESPDATA(("esp_do_data_finale: "));
1991
1992         if(SCptr->SCp.phase == in_datain) {
1993                 if(esp->sreg & ESP_STAT_PERR) {
1994                         /* Yuck, parity error.  The ESP asserts ATN
1995                          * so that we can go to message out phase
1996                          * immediately and inform the target that
1997                          * something bad happened.
1998                          */
1999                         ESPLOG(("esp%d: data bad parity detected.\n",
2000                                 esp->esp_id));
2001                         esp->cur_msgout[0] = INITIATOR_ERROR;
2002                         esp->msgout_len = 1;
2003                 }
2004                 if(esp->dma_drain)
2005                         esp->dma_drain(esp);
2006         }
2007         if(esp->dma_invalidate)
2008                 esp->dma_invalidate(esp);
2009
2010         /* This could happen for the above parity error case. */
2011         if(!(esp->ireg == ESP_INTR_BSERV)) {
2012                 /* Please go to msgout phase, please please please... */
2013                 ESPLOG(("esp%d: !BSERV after data, probably to msgout\n",
2014                         esp->esp_id));
2015                 return esp_do_phase_determine(esp, eregs);
2016         }       
2017
2018         /* Check for partial transfers and other horrible events. */
2019         fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
2020         ecount = esp_getcount(eregs);
2021         if(esp->fas_premature_intr_workaround)
2022                 ecount -= 0x40;
2023         bytes_sent = esp->current_transfer_size;
2024
2025         ESPDATA(("trans_sz=%d, ", bytes_sent));
2026         if(!(esp->sreg & ESP_STAT_TCNT))
2027                 bytes_sent -= ecount;
2028         if(SCptr->SCp.phase == in_dataout)
2029                 bytes_sent -= fifocnt;
2030
2031         ESPDATA(("bytes_sent=%d (ecount=%d, fifocnt=%d), ", bytes_sent,
2032                  ecount, fifocnt));
2033
2034         /* If we were in synchronous mode, check for peculiarities. */
2035         if(SCptr->device->sync_max_offset)
2036                 bogus_data = esp100_sync_hwbug(esp, eregs, SCptr, fifocnt);
2037         else
2038                 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2039
2040         /* Until we are sure of what has happened, we are certainly
2041          * in the dark.
2042          */
2043         esp_advance_phase(SCptr, in_the_dark);
2044
2045         /* Check for premature interrupt condition. Can happen on FAS2x6
2046          * chips. QLogic recommends a workaround by overprogramming the
2047          * transfer counters, but this makes doing scatter-gather impossible.
2048          * Until there is a way to disable scatter-gather for a single target,
2049          * and not only for the entire host adapter as it is now, the workaround
2050          * is way to expensive performance wise.
2051          * Instead, it turns out that when this happens the target has disconnected
2052          * allready but it doesn't show in the interrupt register. Compensate for
2053          * that here to try and avoid a SCSI bus reset.
2054          */
2055         if(!esp->fas_premature_intr_workaround && (fifocnt == 1) &&
2056            sreg_dataoutp(esp->sreg)) {
2057                 ESPLOG(("esp%d: Premature interrupt, enabling workaround\n",
2058                         esp->esp_id));
2059 #if 0
2060                 /* Disable scatter-gather operations, they are not possible
2061                  * when using this workaround.
2062                  */
2063                 esp->ehost->sg_tablesize = 0;
2064                 esp->ehost->use_clustering = ENABLE_CLUSTERING;
2065                 esp->fas_premature_intr_workaround = 1;
2066                 bytes_sent = 0;
2067                 if(SCptr->use_sg) {
2068                         ESPLOG(("esp%d: Aborting scatter-gather operation\n",
2069                                 esp->esp_id));
2070                         esp->cur_msgout[0] = ABORT;
2071                         esp->msgout_len = 1;
2072                         esp->msgout_ctr = 0;
2073                         esp_cmd(esp, eregs, ESP_CMD_SATN);
2074                         esp_setcount(eregs, 0xffff);
2075                         esp_cmd(esp, eregs, ESP_CMD_NULL);
2076                         esp_cmd(esp, eregs, ESP_CMD_TPAD | ESP_CMD_DMA);
2077                         return do_intr_end;
2078                 }
2079 #else
2080                 /* Just set the disconnected bit. That's what appears to
2081                  * happen anyway. The state machine will pick it up when
2082                  * we return.
2083                  */
2084                 esp->ireg |= ESP_INTR_DC;
2085 #endif
2086         }
2087
2088         if(bytes_sent < 0) {
2089                 /* I've seen this happen due to lost state in this
2090                  * driver.  No idea why it happened, but allowing
2091                  * this value to be negative caused things to
2092                  * lock up.  This allows greater chance of recovery.
2093                  * In fact every time I've seen this, it has been
2094                  * a driver bug without question.
2095                  */
2096                 ESPLOG(("esp%d: yieee, bytes_sent < 0!\n", esp->esp_id));
2097                 ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%d\n",
2098                         esp->esp_id,
2099                         esp->current_transfer_size, fifocnt, ecount));
2100                 ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%d\n",
2101                         esp->esp_id,
2102                         SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
2103                 ESPLOG(("esp%d: Forcing async for target %d\n", esp->esp_id, 
2104                         SCptr->target));
2105                 SCptr->device->borken = 1;
2106                 SCptr->device->sync = 0;
2107                 bytes_sent = 0;
2108         }
2109
2110         /* Update the state of our transfer. */
2111         SCptr->SCp.ptr += bytes_sent;
2112         SCptr->SCp.this_residual -= bytes_sent;
2113         if(SCptr->SCp.this_residual < 0) {
2114                 /* shit */
2115                 ESPLOG(("esp%d: Data transfer overrun.\n", esp->esp_id));
2116                 SCptr->SCp.this_residual = 0;
2117         }
2118
2119         /* Maybe continue. */
2120         if(!bogus_data) {
2121                 ESPDATA(("!bogus_data, "));
2122                 /* NO MATTER WHAT, we advance the scatterlist,
2123                  * if the target should decide to disconnect
2124                  * in between scatter chunks (which is common)
2125                  * we could die horribly!  I used to have the sg
2126                  * advance occur only if we are going back into
2127                  * (or are staying in) a data phase, you can
2128                  * imagine the hell I went through trying to
2129                  * figure this out.
2130                  */
2131                 if(!SCptr->SCp.this_residual && SCptr->SCp.buffers_residual)
2132                         advance_sg(esp, SCptr);
2133 #ifdef DEBUG_ESP_DATA
2134                 if(sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
2135                         ESPDATA(("to more data\n"));
2136                 } else {
2137                         ESPDATA(("to new phase\n"));
2138                 }
2139 #endif
2140                 return esp_do_phase_determine(esp, eregs);
2141         }
2142         /* Bogus data, just wait for next interrupt. */
2143         ESPLOG(("esp%d: bogus_data during end of data phase\n",
2144                 esp->esp_id));
2145         return do_intr_end;
2146 }
2147
2148 /* We received a non-good status return at the end of
2149  * running a SCSI command.  This is used to decide if
2150  * we should clear our synchronous transfer state for
2151  * such a device when that happens.
2152  *
2153  * The idea is that when spinning up a disk or rewinding
2154  * a tape, we don't want to go into a loop re-negotiating
2155  * synchronous capabilities over and over.
2156  */
2157 static int esp_should_clear_sync(Scsi_Cmnd *sp)
2158 {
2159         unchar cmd1 = sp->cmnd[0];
2160         unchar cmd2 = sp->data_cmnd[0];
2161
2162         /* These cases are for spinning up a disk and
2163          * waiting for that spinup to complete.
2164          */
2165         if(cmd1 == START_STOP ||
2166            cmd2 == START_STOP)
2167                 return 0;
2168
2169         if(cmd1 == TEST_UNIT_READY ||
2170            cmd2 == TEST_UNIT_READY)
2171                 return 0;
2172
2173         /* One more special case for SCSI tape drives,
2174          * this is what is used to probe the device for
2175          * completion of a rewind or tape load operation.
2176          */
2177         if(sp->device->type == TYPE_TAPE) {
2178                 if(cmd1 == MODE_SENSE ||
2179                    cmd2 == MODE_SENSE)
2180                         return 0;
2181         }
2182
2183         return 1;
2184 }
2185
2186 /* Either a command is completing or a target is dropping off the bus
2187  * to continue the command in the background so we can do other work.
2188  */
2189 static int esp_do_freebus(struct NCR_ESP *esp, struct ESP_regs *eregs)
2190 {
2191         Scsi_Cmnd *SCptr = esp->current_SC;
2192         int rval;
2193
2194         rval = skipahead2(esp, eregs, SCptr, in_status, in_msgindone, in_freeing);
2195         if(rval)
2196                 return rval;
2197
2198         if(esp->ireg != ESP_INTR_DC) {
2199                 ESPLOG(("esp%d: Target will not disconnect\n", esp->esp_id));
2200                 return do_reset_bus; /* target will not drop BSY... */
2201         }
2202         esp->msgout_len = 0;
2203         esp->prevmsgout = NOP;
2204         if(esp->prevmsgin == COMMAND_COMPLETE) {
2205                 /* Normal end of nexus. */
2206                 if(esp->disconnected_SC)
2207                         esp_cmd(esp, eregs, ESP_CMD_ESEL);
2208
2209                 if(SCptr->SCp.Status != GOOD &&
2210                    SCptr->SCp.Status != CONDITION_GOOD &&
2211                    ((1<<SCptr->target) & esp->targets_present) &&
2212                    SCptr->device->sync &&
2213                    SCptr->device->sync_max_offset) {
2214                         /* SCSI standard says that the synchronous capabilities
2215                          * should be renegotiated at this point.  Most likely
2216                          * we are about to request sense from this target
2217                          * in which case we want to avoid using sync
2218                          * transfers until we are sure of the current target
2219                          * state.
2220                          */
2221                         ESPMISC(("esp: Status <%d> for target %d lun %d\n",
2222                                  SCptr->SCp.Status, SCptr->target, SCptr->lun));
2223
2224                         /* But don't do this when spinning up a disk at
2225                          * boot time while we poll for completion as it
2226                          * fills up the console with messages.  Also, tapes
2227                          * can report not ready many times right after
2228                          * loading up a tape.
2229                          */
2230                         if(esp_should_clear_sync(SCptr) != 0)
2231                                 SCptr->device->sync = 0;
2232                 }
2233                 ESPDISC(("F<%02x,%02x>", SCptr->target, SCptr->lun));
2234                 esp_done(esp, ((SCptr->SCp.Status & 0xff) |
2235                                ((SCptr->SCp.Message & 0xff)<<8) |
2236                                (DID_OK << 16)));
2237         } else if(esp->prevmsgin == DISCONNECT) {
2238                 /* Normal disconnect. */
2239                 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2240                 ESPDISC(("D<%02x,%02x>", SCptr->target, SCptr->lun));
2241                 append_SC(&esp->disconnected_SC, SCptr);
2242                 esp->current_SC = NULL;
2243                 if(esp->issue_SC)
2244                         esp_exec_cmd(esp);
2245         } else {
2246                 /* Driver bug, we do not expect a disconnect here
2247                  * and should not have advanced the state engine
2248                  * to in_freeing.
2249                  */
2250                 ESPLOG(("esp%d: last msg not disc and not cmd cmplt.\n",
2251                         esp->esp_id));
2252                 return do_reset_bus;
2253         }
2254         return do_intr_end;
2255 }
2256
2257 /* When a reselect occurs, and we cannot find the command to
2258  * reconnect to in our queues, we do this.
2259  */
2260 static int esp_bad_reconnect(struct NCR_ESP *esp)
2261 {
2262         Scsi_Cmnd *sp;
2263
2264         ESPLOG(("esp%d: Eieeee, reconnecting unknown command!\n",
2265                 esp->esp_id));
2266         ESPLOG(("QUEUE DUMP\n"));
2267         sp = esp->issue_SC;
2268         ESPLOG(("esp%d: issue_SC[", esp->esp_id));
2269         while(sp) {
2270                 ESPLOG(("<%02x,%02x>", sp->target, sp->lun));
2271                 sp = (Scsi_Cmnd *) sp->host_scribble;
2272         }
2273         ESPLOG(("]\n"));
2274         sp = esp->current_SC;
2275         ESPLOG(("esp%d: current_SC[", esp->esp_id));
2276         while(sp) {
2277                 ESPLOG(("<%02x,%02x>", sp->target, sp->lun));
2278                 sp = (Scsi_Cmnd *) sp->host_scribble;
2279         }
2280         ESPLOG(("]\n"));
2281         sp = esp->disconnected_SC;
2282         ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
2283         while(sp) {
2284                 ESPLOG(("<%02x,%02x>", sp->target, sp->lun));
2285                 sp = (Scsi_Cmnd *) sp->host_scribble;
2286         }
2287         ESPLOG(("]\n"));
2288         return do_reset_bus;
2289 }
2290
2291 /* Do the needy when a target tries to reconnect to us. */
2292 static int esp_do_reconnect(struct NCR_ESP *esp, 
2293                             struct ESP_regs *eregs)
2294 {
2295         int lun, target;
2296         Scsi_Cmnd *SCptr;
2297
2298         /* Check for all bogus conditions first. */
2299         target = reconnect_target(esp, eregs);
2300         if(target < 0) {
2301                 ESPDISC(("bad bus bits\n"));
2302                 return do_reset_bus;
2303         }
2304         lun = reconnect_lun(esp, eregs);
2305         if(lun < 0) {
2306                 ESPDISC(("target=%2x, bad identify msg\n", target));
2307                 return do_reset_bus;
2308         }
2309
2310         /* Things look ok... */
2311         ESPDISC(("R<%02x,%02x>", target, lun));
2312
2313         esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2314         if(esp100_reconnect_hwbug(esp, eregs))
2315                 return do_reset_bus;
2316         esp_cmd(esp, eregs, ESP_CMD_NULL);
2317
2318         SCptr = remove_SC(&esp->disconnected_SC, (unchar) target, (unchar) lun);
2319         if(!SCptr)
2320                 return esp_bad_reconnect(esp);
2321
2322         esp_connect(esp, eregs, SCptr);
2323         esp_cmd(esp, eregs, ESP_CMD_MOK);
2324
2325         /* Reconnect implies a restore pointers operation. */
2326         esp_restore_pointers(esp, SCptr);
2327
2328         esp->snip = 0;
2329         esp_advance_phase(SCptr, in_the_dark);
2330         return do_intr_end;
2331 }
2332
2333 /* End of NEXUS (hopefully), pick up status + message byte then leave if
2334  * all goes well.
2335  */
2336 static int esp_do_status(struct NCR_ESP *esp, struct ESP_regs *eregs)
2337 {
2338         Scsi_Cmnd *SCptr = esp->current_SC;
2339         int intr, rval;
2340
2341         rval = skipahead1(esp, eregs, SCptr, in_the_dark, in_status);
2342         if(rval)
2343                 return rval;
2344
2345         intr = esp->ireg;
2346         ESPSTAT(("esp_do_status: "));
2347         if(intr != ESP_INTR_DC) {
2348                 int message_out = 0; /* for parity problems */
2349
2350                 /* Ack the message. */
2351                 ESPSTAT(("ack msg, "));
2352                 esp_cmd(esp, eregs, ESP_CMD_MOK);
2353
2354                 if(esp->dma_poll)
2355                         esp->dma_poll(esp, (unsigned char *) esp->esp_command);
2356
2357                 ESPSTAT(("got something, "));
2358                 /* ESP chimes in with one of
2359                  *
2360                  * 1) function done interrupt:
2361                  *      both status and message in bytes
2362                  *      are available
2363                  *
2364                  * 2) bus service interrupt:
2365                  *      only status byte was acquired
2366                  *
2367                  * 3) Anything else:
2368                  *      can't happen, but we test for it
2369                  *      anyways
2370                  *
2371                  * ALSO: If bad parity was detected on either
2372                  *       the status _or_ the message byte then
2373                  *       the ESP has asserted ATN on the bus
2374                  *       and we must therefore wait for the
2375                  *       next phase change.
2376                  */
2377                 if(intr & ESP_INTR_FDONE) {
2378                         /* We got it all, hallejulia. */
2379                         ESPSTAT(("got both, "));
2380                         SCptr->SCp.Status = esp->esp_command[0];
2381                         SCptr->SCp.Message = esp->esp_command[1];
2382                         esp->prevmsgin = SCptr->SCp.Message;
2383                         esp->cur_msgin[0] = SCptr->SCp.Message;
2384                         if(esp->sreg & ESP_STAT_PERR) {
2385                                 /* There was bad parity for the
2386                                  * message byte, the status byte
2387                                  * was ok.
2388                                  */
2389                                 message_out = MSG_PARITY_ERROR;
2390                         }
2391                 } else if(intr == ESP_INTR_BSERV) {
2392                         /* Only got status byte. */
2393                         ESPLOG(("esp%d: got status only, ", esp->esp_id));
2394                         if(!(esp->sreg & ESP_STAT_PERR)) {
2395                                 SCptr->SCp.Status = esp->esp_command[0];
2396                                 SCptr->SCp.Message = 0xff;
2397                         } else {
2398                                 /* The status byte had bad parity.
2399                                  * we leave the scsi_pointer Status
2400                                  * field alone as we set it to a default
2401                                  * of CHECK_CONDITION in esp_queue.
2402                                  */
2403                                 message_out = INITIATOR_ERROR;
2404                         }
2405                 } else {
2406                         /* This shouldn't happen ever. */
2407                         ESPSTAT(("got bolixed\n"));
2408                         esp_advance_phase(SCptr, in_the_dark);
2409                         return esp_do_phase_determine(esp, eregs);
2410                 }
2411
2412                 if(!message_out) {
2413                         ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
2414                                 SCptr->SCp.Message));
2415                         if(SCptr->SCp.Message == COMMAND_COMPLETE) {
2416                                 ESPSTAT(("and was COMMAND_COMPLETE\n"));
2417                                 esp_advance_phase(SCptr, in_freeing);
2418                                 return esp_do_freebus(esp, eregs);
2419                         } else {
2420                                 ESPLOG(("esp%d: and _not_ COMMAND_COMPLETE\n",
2421                                         esp->esp_id));
2422                                 esp->msgin_len = esp->msgin_ctr = 1;
2423                                 esp_advance_phase(SCptr, in_msgindone);
2424                                 return esp_do_msgindone(esp, eregs);
2425                         }
2426                 } else {
2427                         /* With luck we'll be able to let the target
2428                          * know that bad parity happened, it will know
2429                          * which byte caused the problems and send it
2430                          * again.  For the case where the status byte
2431                          * receives bad parity, I do not believe most
2432                          * targets recover very well.  We'll see.
2433                          */
2434                         ESPLOG(("esp%d: bad parity somewhere mout=%2x\n",
2435                                 esp->esp_id, message_out));
2436                         esp->cur_msgout[0] = message_out;
2437                         esp->msgout_len = esp->msgout_ctr = 1;
2438                         esp_advance_phase(SCptr, in_the_dark);
2439                         return esp_do_phase_determine(esp, eregs);
2440                 }
2441         } else {
2442                 /* If we disconnect now, all hell breaks loose. */
2443                 ESPLOG(("esp%d: whoops, disconnect\n", esp->esp_id));
2444                 esp_advance_phase(SCptr, in_the_dark);
2445                 return esp_do_phase_determine(esp, eregs);
2446         }
2447 }
2448
2449 static int esp_enter_status(struct NCR_ESP *esp,
2450                             struct ESP_regs *eregs)
2451 {
2452         unchar thecmd = ESP_CMD_ICCSEQ;
2453
2454         esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2455
2456         if(esp->do_pio_cmds) {
2457                 esp_advance_phase(esp->current_SC, in_status);
2458                 esp_cmd(esp, eregs, thecmd);
2459                 while(!(esp_read(esp->eregs->esp_status) & ESP_STAT_INTR));
2460                 esp->esp_command[0] = esp_read(eregs->esp_fdata);
2461                 while(!(esp_read(esp->eregs->esp_status) & ESP_STAT_INTR));
2462                 esp->esp_command[1] = esp_read(eregs->esp_fdata);
2463         } else {
2464                 esp->esp_command[0] = esp->esp_command[1] = 0xff;
2465                 esp_write(eregs->esp_tclow, 2);
2466                 esp_write(eregs->esp_tcmed, 0);
2467                 esp->dma_init_read(esp, esp->esp_command_dvma, 2);
2468                 thecmd |= ESP_CMD_DMA;
2469                 esp_cmd(esp, eregs, thecmd);
2470                 esp_advance_phase(esp->current_SC, in_status);
2471         }
2472
2473         return esp_do_status(esp, eregs);
2474 }
2475
2476 static int esp_disconnect_amidst_phases(struct NCR_ESP *esp,
2477                                         struct ESP_regs *eregs)
2478 {
2479         Scsi_Cmnd *sp = esp->current_SC;
2480         Scsi_Device *dp = sp->device;
2481
2482         /* This means real problems if we see this
2483          * here.  Unless we were actually trying
2484          * to force the device to abort/reset.
2485          */
2486         ESPLOG(("esp%d: Disconnect amidst phases, ", esp->esp_id));
2487         ESPLOG(("pphase<%s> cphase<%s>, ",
2488                 phase_string(sp->SCp.phase),
2489                 phase_string(sp->SCp.sent_command)));
2490
2491         if(esp->disconnected_SC)
2492                 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2493
2494         switch(esp->cur_msgout[0]) {
2495         default:
2496                 /* We didn't expect this to happen at all. */
2497                 ESPLOG(("device is bolixed\n"));
2498                 esp_advance_phase(sp, in_tgterror);
2499                 esp_done(esp, (DID_ERROR << 16));
2500                 break;
2501
2502         case BUS_DEVICE_RESET:
2503                 ESPLOG(("device reset successful\n"));
2504                 dp->sync_max_offset = 0;
2505                 dp->sync_min_period = 0;
2506                 dp->sync = 0;
2507                 esp_advance_phase(sp, in_resetdev);
2508                 esp_done(esp, (DID_RESET << 16));
2509                 break;
2510
2511         case ABORT:
2512                 ESPLOG(("device abort successful\n"));
2513                 esp_advance_phase(sp, in_abortone);
2514                 esp_done(esp, (DID_ABORT << 16));
2515                 break;
2516
2517         };
2518         return do_intr_end;
2519 }
2520
2521 static int esp_enter_msgout(struct NCR_ESP *esp,
2522                             struct ESP_regs *eregs)
2523 {
2524         esp_advance_phase(esp->current_SC, in_msgout);
2525         return esp_do_msgout(esp, eregs);
2526 }
2527
2528 static int esp_enter_msgin(struct NCR_ESP *esp,
2529                            struct ESP_regs *eregs)
2530 {
2531         esp_advance_phase(esp->current_SC, in_msgin);
2532         return esp_do_msgin(esp, eregs);
2533 }
2534
2535 static int esp_enter_cmd(struct NCR_ESP *esp,
2536                          struct ESP_regs *eregs)
2537 {
2538         esp_advance_phase(esp->current_SC, in_cmdbegin);
2539         return esp_do_cmdbegin(esp, eregs);
2540 }
2541
2542 static int esp_enter_badphase(struct NCR_ESP *esp,
2543                               struct ESP_regs *eregs)
2544 {
2545         ESPLOG(("esp%d: Bizarre bus phase %2x.\n", esp->esp_id,
2546                 esp->sreg & ESP_STAT_PMASK));
2547         return do_reset_bus;
2548 }
2549
2550 typedef int (*espfunc_t)(struct NCR_ESP *,
2551                          struct ESP_regs *);
2552
2553 static espfunc_t phase_vector[] = {
2554         esp_do_data,            /* ESP_DOP */
2555         esp_do_data,            /* ESP_DIP */
2556         esp_enter_cmd,          /* ESP_CMDP */
2557         esp_enter_status,       /* ESP_STATP */
2558         esp_enter_badphase,     /* ESP_STAT_PMSG */
2559         esp_enter_badphase,     /* ESP_STAT_PMSG | ESP_STAT_PIO */
2560         esp_enter_msgout,       /* ESP_MOP */
2561         esp_enter_msgin,        /* ESP_MIP */
2562 };
2563
2564 /* The target has control of the bus and we have to see where it has
2565  * taken us.
2566  */
2567 static int esp_do_phase_determine(struct NCR_ESP *esp,
2568                                   struct ESP_regs *eregs)
2569 {
2570         if ((esp->ireg & ESP_INTR_DC) != 0)
2571                 return esp_disconnect_amidst_phases(esp, eregs);
2572         return phase_vector[esp->sreg & ESP_STAT_PMASK](esp, eregs);
2573 }
2574
2575 /* First interrupt after exec'ing a cmd comes here. */
2576 static int esp_select_complete(struct NCR_ESP *esp, struct ESP_regs *eregs)
2577 {
2578         Scsi_Cmnd *SCptr = esp->current_SC;
2579         Scsi_Device *SDptr = SCptr->device;
2580         int cmd_bytes_sent, fcnt;
2581
2582         fcnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
2583         cmd_bytes_sent = esp->dma_bytes_sent(esp, fcnt);
2584         if(esp->dma_invalidate)
2585                 esp->dma_invalidate(esp);
2586
2587         /* Let's check to see if a reselect happened
2588          * while we we're trying to select.  This must
2589          * be checked first.
2590          */
2591         if(esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
2592                 esp_reconnect(esp, SCptr);
2593                 return esp_do_reconnect(esp, eregs);
2594         }
2595
2596         /* Looks like things worked, we should see a bus service &
2597          * a function complete interrupt at this point.  Note we
2598          * are doing a direct comparison because we don't want to
2599          * be fooled into thinking selection was successful if
2600          * ESP_INTR_DC is set, see below.
2601          */
2602         if(esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
2603                 /* target speaks... */
2604                 esp->targets_present |= (1<<SCptr->target);
2605
2606                 /* What if the target ignores the sdtr? */
2607                 if(esp->snip)
2608                         SDptr->sync = 1;
2609
2610                 /* See how far, if at all, we got in getting
2611                  * the information out to the target.
2612                  */
2613                 switch(esp->seqreg) {
2614                 default:
2615
2616                 case ESP_STEP_ASEL:
2617                         /* Arbitration won, target selected, but
2618                          * we are in some phase which is not command
2619                          * phase nor is it message out phase.
2620                          *
2621                          * XXX We've confused the target, obviously.
2622                          * XXX So clear it's state, but we also end
2623                          * XXX up clearing everyone elses.  That isn't
2624                          * XXX so nice.  I'd like to just reset this
2625                          * XXX target, but if I cannot even get it's
2626                          * XXX attention and finish selection to talk
2627                          * XXX to it, there is not much more I can do.
2628                          * XXX If we have a loaded bus we're going to
2629                          * XXX spend the next second or so renegotiating
2630                          * XXX for synchronous transfers.
2631                          */
2632                         ESPLOG(("esp%d: STEP_ASEL for tgt %d\n",
2633                                 esp->esp_id, SCptr->target));
2634
2635                 case ESP_STEP_SID:
2636                         /* Arbitration won, target selected, went
2637                          * to message out phase, sent one message
2638                          * byte, then we stopped.  ATN is asserted
2639                          * on the SCSI bus and the target is still
2640                          * there hanging on.  This is a legal
2641                          * sequence step if we gave the ESP a select
2642                          * and stop command.
2643                          *
2644                          * XXX See above, I could set the borken flag
2645                          * XXX in the device struct and retry the
2646                          * XXX command.  But would that help for
2647                          * XXX tagged capable targets?
2648                          */
2649
2650                 case ESP_STEP_NCMD:
2651                         /* Arbitration won, target selected, maybe
2652                          * sent the one message byte in message out
2653                          * phase, but we did not go to command phase
2654                          * in the end.  Actually, we could have sent
2655                          * only some of the message bytes if we tried
2656                          * to send out the entire identify and tag
2657                          * message using ESP_CMD_SA3.
2658                          */
2659                         cmd_bytes_sent = 0;
2660                         break;
2661
2662                 case ESP_STEP_PPC:
2663                         /* No, not the powerPC pinhead.  Arbitration
2664                          * won, all message bytes sent if we went to
2665                          * message out phase, went to command phase
2666                          * but only part of the command was sent.
2667                          *
2668                          * XXX I've seen this, but usually in conjunction
2669                          * XXX with a gross error which appears to have
2670                          * XXX occurred between the time I told the
2671                          * XXX ESP to arbitrate and when I got the
2672                          * XXX interrupt.  Could I have misloaded the
2673                          * XXX command bytes into the fifo?  Actually,
2674                          * XXX I most likely missed a phase, and therefore
2675                          * XXX went into never never land and didn't even
2676                          * XXX know it.  That was the old driver though.
2677                          * XXX What is even more peculiar is that the ESP
2678                          * XXX showed the proper function complete and
2679                          * XXX bus service bits in the interrupt register.
2680                          */
2681
2682                 case ESP_STEP_FINI4:
2683                 case ESP_STEP_FINI5:
2684                 case ESP_STEP_FINI6:
2685                 case ESP_STEP_FINI7:
2686                         /* Account for the identify message */
2687                         if(SCptr->SCp.phase == in_slct_norm)
2688                                 cmd_bytes_sent -= 1;
2689                 };
2690                 esp_cmd(esp, eregs, ESP_CMD_NULL);
2691
2692                 /* Be careful, we could really get fucked during synchronous
2693                  * data transfers if we try to flush the fifo now.
2694                  */
2695                 if(!fcnt && /* Fifo is empty and... */
2696                    /* either we are not doing synchronous transfers or... */
2697                    (!SDptr->sync_max_offset ||
2698                     /* We are not going into data in phase. */
2699                     ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
2700                         esp_cmd(esp, eregs, ESP_CMD_FLUSH); /* flush is safe */
2701
2702                 /* See how far we got if this is not a slow command. */
2703                 if(!esp->esp_slowcmd) {
2704                         if(cmd_bytes_sent < 0)
2705                                 cmd_bytes_sent = 0;
2706                         if(cmd_bytes_sent != SCptr->cmd_len) {
2707                                 /* Crapola, mark it as a slowcmd
2708                                  * so that we have some chance of
2709                                  * keeping the command alive with
2710                                  * good luck.
2711                                  *
2712                                  * XXX Actually, if we didn't send it all
2713                                  * XXX this means either we didn't set things
2714                                  * XXX up properly (driver bug) or the target
2715                                  * XXX or the ESP detected parity on one of
2716                                  * XXX the command bytes.  This makes much
2717                                  * XXX more sense, and therefore this code
2718                                  * XXX should be changed to send out a
2719                                  * XXX parity error message or if the status
2720                                  * XXX register shows no parity error then
2721                                  * XXX just expect the target to bring the
2722                                  * XXX bus into message in phase so that it
2723                                  * XXX can send us the parity error message.
2724                                  * XXX SCSI sucks...
2725                                  */
2726                                 esp->esp_slowcmd = 1;
2727                                 esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
2728                                 esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
2729                         }
2730                 }
2731
2732                 /* Now figure out where we went. */
2733                 esp_advance_phase(SCptr, in_the_dark);
2734                 return esp_do_phase_determine(esp, eregs);
2735         }
2736
2737         /* Did the target even make it? */
2738         if(esp->ireg == ESP_INTR_DC) {
2739                 /* wheee... nobody there or they didn't like
2740                  * what we told it to do, clean up.
2741                  */
2742
2743                 /* If anyone is off the bus, but working on
2744                  * a command in the background for us, tell
2745                  * the ESP to listen for them.
2746                  */
2747                 if(esp->disconnected_SC)
2748                         esp_cmd(esp, eregs, ESP_CMD_ESEL);
2749
2750                 if(((1<<SCptr->target) & esp->targets_present) &&
2751                    esp->seqreg && esp->cur_msgout[0] == EXTENDED_MESSAGE &&
2752                    (SCptr->SCp.phase == in_slct_msg ||
2753                     SCptr->SCp.phase == in_slct_stop)) {
2754                         /* shit */
2755                         esp->snip = 0;
2756                         ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
2757                                 "lun %d\n", esp->esp_id, SCptr->target, SCptr->lun));
2758                         SDptr->sync_max_offset = 0;
2759                         SDptr->sync_min_period = 0;
2760                         SDptr->sync = 1; /* so we don't negotiate again */
2761
2762                         /* Run the command again, this time though we
2763                          * won't try to negotiate for synchronous transfers.
2764                          *
2765                          * XXX I'd like to do something like send an
2766                          * XXX INITIATOR_ERROR or ABORT message to the
2767                          * XXX target to tell it, "Sorry I confused you,
2768                          * XXX please come back and I will be nicer next
2769                          * XXX time".  But that requires having the target
2770                          * XXX on the bus, and it has dropped BSY on us.
2771                          */
2772                         esp->current_SC = NULL;
2773                         esp_advance_phase(SCptr, not_issued);
2774                         prepend_SC(&esp->issue_SC, SCptr);
2775                         esp_exec_cmd(esp);
2776                         return do_intr_end;
2777                 }
2778
2779                 /* Ok, this is normal, this is what we see during boot
2780                  * or whenever when we are scanning the bus for targets.
2781                  * But first make sure that is really what is happening.
2782                  */
2783                 if(((1<<SCptr->target) & esp->targets_present)) {
2784                         ESPLOG(("esp%d: Warning, live target %d not responding to "
2785                                 "selection.\n", esp->esp_id, SCptr->target));
2786
2787                         /* This _CAN_ happen.  The SCSI standard states that
2788                          * the target is to _not_ respond to selection if
2789                          * _it_ detects bad parity on the bus for any reason.
2790                          * Therefore, we assume that if we've talked successfully
2791                          * to this target before, bad parity is the problem.
2792                          */
2793                         esp_done(esp, (DID_PARITY << 16));
2794                 } else {
2795                         /* Else, there really isn't anyone there. */
2796                         ESPMISC(("esp: selection failure, maybe nobody there?\n"));
2797                         ESPMISC(("esp: target %d lun %d\n",
2798                                  SCptr->target, SCptr->lun));
2799                         esp_done(esp, (DID_BAD_TARGET << 16));
2800                 }
2801                 return do_intr_end;
2802         }
2803
2804
2805         ESPLOG(("esp%d: Selection failure.\n", esp->esp_id));
2806         printk("esp%d: Currently -- ", esp->esp_id);
2807         esp_print_ireg(esp->ireg);
2808         printk(" ");
2809         esp_print_statreg(esp->sreg);
2810         printk(" ");
2811         esp_print_seqreg(esp->seqreg);
2812         printk("\n");
2813         printk("esp%d: New -- ", esp->esp_id);
2814         esp->sreg = esp_read(eregs->esp_status);
2815         esp->seqreg = esp_read(eregs->esp_sstep);
2816         esp->ireg = esp_read(eregs->esp_intrpt);
2817         esp_print_ireg(esp->ireg);
2818         printk(" ");
2819         esp_print_statreg(esp->sreg);
2820         printk(" ");
2821         esp_print_seqreg(esp->seqreg);
2822         printk("\n");
2823         ESPLOG(("esp%d: resetting bus\n", esp->esp_id));
2824         return do_reset_bus; /* ugh... */
2825 }
2826
2827 /* Continue reading bytes for msgin phase. */
2828 static int esp_do_msgincont(struct NCR_ESP *esp, struct ESP_regs *eregs)
2829 {
2830         if(esp->ireg & ESP_INTR_BSERV) {
2831                 /* in the right phase too? */
2832                 if((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
2833                         /* phew... */
2834                         esp_cmd(esp, eregs, ESP_CMD_TI);
2835                         esp_advance_phase(esp->current_SC, in_msgindone);
2836                         return do_intr_end;
2837                 }
2838
2839                 /* We changed phase but ESP shows bus service,
2840                  * in this case it is most likely that we, the
2841                  * hacker who has been up for 20hrs straight
2842                  * staring at the screen, drowned in coffee
2843                  * smelling like retched cigarette ashes
2844                  * have miscoded something..... so, try to
2845                  * recover as best we can.
2846                  */
2847                 ESPLOG(("esp%d: message in mis-carriage.\n", esp->esp_id));
2848         }
2849         esp_advance_phase(esp->current_SC, in_the_dark);
2850         return do_phase_determine;
2851 }
2852
2853 static int check_singlebyte_msg(struct NCR_ESP *esp,
2854                                 struct ESP_regs *eregs)
2855 {
2856         esp->prevmsgin = esp->cur_msgin[0];
2857         if(esp->cur_msgin[0] & 0x80) {
2858                 /* wheee... */
2859                 ESPLOG(("esp%d: target sends identify amidst phases\n",
2860                         esp->esp_id));
2861                 esp_advance_phase(esp->current_SC, in_the_dark);
2862                 return 0;
2863         } else if(((esp->cur_msgin[0] & 0xf0) == 0x20) ||
2864                   (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
2865                 esp->msgin_len = 2;
2866                 esp_advance_phase(esp->current_SC, in_msgincont);
2867                 return 0;
2868         }
2869         esp_advance_phase(esp->current_SC, in_the_dark);
2870         switch(esp->cur_msgin[0]) {
2871         default:
2872                 /* We don't want to hear about it. */
2873                 ESPLOG(("esp%d: msg %02x which we don't know about\n", esp->esp_id,
2874                         esp->cur_msgin[0]));
2875                 return MESSAGE_REJECT;
2876
2877         case NOP:
2878                 ESPLOG(("esp%d: target %d sends a nop\n", esp->esp_id,
2879                         esp->current_SC->target));
2880                 return 0;
2881
2882         case RESTORE_POINTERS:
2883                 /* In this case we might also have to backup the
2884                  * "slow command" pointer.  It is rare to get such
2885                  * a save/restore pointer sequence so early in the
2886                  * bus transition sequences, but cover it.
2887                  */
2888                 if(esp->esp_slowcmd) {
2889                         esp->esp_scmdleft = esp->current_SC->cmd_len;
2890                         esp->esp_scmdp = &esp->current_SC->cmnd[0];
2891                 }
2892                 esp_restore_pointers(esp, esp->current_SC);
2893                 return 0;
2894
2895         case SAVE_POINTERS:
2896                 esp_save_pointers(esp, esp->current_SC);
2897                 return 0;
2898
2899         case COMMAND_COMPLETE:
2900         case DISCONNECT:
2901                 /* Freeing the bus, let it go. */
2902                 esp->current_SC->SCp.phase = in_freeing;
2903                 return 0;
2904
2905         case MESSAGE_REJECT:
2906                 ESPMISC(("msg reject, "));
2907                 if(esp->prevmsgout == EXTENDED_MESSAGE) {
2908                         Scsi_Device *SDptr = esp->current_SC->device;
2909
2910                         /* Doesn't look like this target can
2911                          * do synchronous or WIDE transfers.
2912                          */
2913                         ESPSDTR(("got reject, was trying nego, clearing sync/WIDE\n"));
2914                         SDptr->sync = 1;
2915                         SDptr->wide = 1;
2916                         SDptr->sync_min_period = 0;
2917                         SDptr->sync_max_offset = 0;
2918                         return 0;
2919                 } else {
2920                         ESPMISC(("not sync nego, sending ABORT\n"));
2921                         return ABORT;
2922                 }
2923         };
2924 }
2925
2926 /* Target negotiates for synchronous transfers before we do, this
2927  * is legal although very strange.  What is even funnier is that
2928  * the SCSI2 standard specifically recommends against targets doing
2929  * this because so many initiators cannot cope with this occuring.
2930  */
2931 static int target_with_ants_in_pants(struct NCR_ESP *esp,
2932                                      Scsi_Cmnd *SCptr,
2933                                      Scsi_Device *SDptr)
2934 {
2935         if(SDptr->sync || SDptr->borken) {
2936                 /* sorry, no can do */
2937                 ESPSDTR(("forcing to async, "));
2938                 build_sync_nego_msg(esp, 0, 0);
2939                 SDptr->sync = 1;
2940                 esp->snip = 1;
2941                 ESPLOG(("esp%d: hoping for msgout\n", esp->esp_id));
2942                 esp_advance_phase(SCptr, in_the_dark);
2943                 return EXTENDED_MESSAGE;
2944         }
2945
2946         /* Ok, we'll check them out... */
2947         return 0;
2948 }
2949
2950 static void sync_report(struct NCR_ESP *esp)
2951 {
2952         int msg3, msg4;
2953         char *type;
2954
2955         msg3 = esp->cur_msgin[3];
2956         msg4 = esp->cur_msgin[4];
2957         if(msg4) {
2958                 int hz = 1000000000 / (msg3 * 4);
2959                 int integer = hz / 1000000;
2960                 int fraction = (hz - (integer * 1000000)) / 10000;
2961                 if((msg3 * 4) < 200) {
2962                         type = "FAST";
2963                 } else {
2964                         type = "synchronous";
2965                 }
2966
2967                 /* Do not transform this back into one big printk
2968                  * again, it triggers a bug in our sparc64-gcc272
2969                  * sibling call optimization.  -DaveM
2970                  */
2971                 ESPLOG((KERN_INFO "esp%d: target %d ",
2972                         esp->esp_id, esp->current_SC->target));
2973                 ESPLOG(("[period %dns offset %d %d.%02dMHz ",
2974                         (int) msg3 * 4, (int) msg4,
2975                         integer, fraction));
2976                 ESPLOG(("%s SCSI%s]\n", type,
2977                         (((msg3 * 4) < 200) ? "-II" : "")));
2978         } else {
2979                 ESPLOG((KERN_INFO "esp%d: target %d asynchronous\n",
2980                         esp->esp_id, esp->current_SC->target));
2981         }
2982 }
2983
2984 static int check_multibyte_msg(struct NCR_ESP *esp,
2985                                struct ESP_regs *eregs)
2986 {
2987         Scsi_Cmnd *SCptr = esp->current_SC;
2988         Scsi_Device *SDptr = SCptr->device;
2989         unchar regval = 0;
2990         int message_out = 0;
2991
2992         ESPSDTR(("chk multibyte msg: "));
2993         if(esp->cur_msgin[2] == EXTENDED_SDTR) {
2994                 int period = esp->cur_msgin[3];
2995                 int offset = esp->cur_msgin[4];
2996
2997                 ESPSDTR(("is sync nego response, "));
2998                 if(!esp->snip) {
2999                         int rval;
3000
3001                         /* Target negotiates first! */
3002                         ESPSDTR(("target jumps the gun, "));
3003                         message_out = EXTENDED_MESSAGE; /* we must respond */
3004                         rval = target_with_ants_in_pants(esp, SCptr, SDptr);
3005                         if(rval)
3006                                 return rval;
3007                 }
3008
3009                 ESPSDTR(("examining sdtr, "));
3010
3011                 /* Offset cannot be larger than ESP fifo size. */
3012                 if(offset > 15) {
3013                         ESPSDTR(("offset too big %2x, ", offset));
3014                         offset = 15;
3015                         ESPSDTR(("sending back new offset\n"));
3016                         build_sync_nego_msg(esp, period, offset);
3017                         return EXTENDED_MESSAGE;
3018                 }
3019
3020                 if(offset && period > esp->max_period) {
3021                         /* Yeee, async for this slow device. */
3022                         ESPSDTR(("period too long %2x, ", period));
3023                         build_sync_nego_msg(esp, 0, 0);
3024                         ESPSDTR(("hoping for msgout\n"));
3025                         esp_advance_phase(esp->current_SC, in_the_dark);
3026                         return EXTENDED_MESSAGE;
3027                 } else if (offset && period < esp->min_period) {
3028                         ESPSDTR(("period too short %2x, ", period));
3029                         period = esp->min_period;
3030                         if(esp->erev > esp236)
3031                                 regval = 4;
3032                         else
3033                                 regval = 5;
3034                 } else if(offset) {
3035                         int tmp;
3036
3037                         ESPSDTR(("period is ok, "));
3038                         tmp = esp->ccycle / 1000;
3039                         regval = (((period << 2) + tmp - 1) / tmp);
3040                         if(regval && (esp->erev > esp236)) {
3041                                 if(period >= 50)
3042                                         regval--;
3043                         }
3044                 }
3045
3046                 if(offset) {
3047                         unchar bit;
3048
3049                         SDptr->sync_min_period = (regval & 0x1f);
3050                         SDptr->sync_max_offset = (offset | esp->radelay);
3051                         if(esp->erev > esp236) {
3052                                 if(esp->erev == fas100a)
3053                                         bit = ESP_CONFIG3_FAST;
3054                                 else
3055                                         bit = ESP_CONFIG3_FSCSI;
3056                                 if(period < 50)
3057                                         esp->config3[SCptr->target] |= bit;
3058                                 else
3059                                         esp->config3[SCptr->target] &= ~bit;
3060                                 esp->prev_cfg3 = esp->config3[SCptr->target];
3061                                 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
3062                         }
3063                         esp->prev_soff = SDptr->sync_min_period;
3064                         esp_write(eregs->esp_soff, esp->prev_soff);
3065                         esp->prev_stp = SDptr->sync_max_offset;
3066                         esp_write(eregs->esp_stp, esp->prev_stp);
3067
3068                         ESPSDTR(("soff=%2x stp=%2x cfg3=%2x\n",
3069                                 SDptr->sync_max_offset,
3070                                 SDptr->sync_min_period,
3071                                 esp->config3[SCptr->target]));
3072
3073                         esp->snip = 0;
3074                 } else if(SDptr->sync_max_offset) {
3075                         unchar bit;
3076
3077                         /* back to async mode */
3078                         ESPSDTR(("unaccaptable sync nego, forcing async\n"));
3079                         SDptr->sync_max_offset = 0;
3080                         SDptr->sync_min_period = 0;
3081                         esp->prev_soff = 0;
3082                         esp_write(eregs->esp_soff, 0);
3083                         esp->prev_stp = 0;
3084                         esp_write(eregs->esp_stp, 0);
3085                         if(esp->erev > esp236) {
3086                                 if(esp->erev == fas100a)
3087                                         bit = ESP_CONFIG3_FAST;
3088                                 else
3089                                         bit = ESP_CONFIG3_FSCSI;
3090                                 esp->config3[SCptr->target] &= ~bit;
3091                                 esp->prev_cfg3 = esp->config3[SCptr->target];
3092                                 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
3093                         }
3094                 }
3095
3096                 sync_report(esp);
3097
3098                 ESPSDTR(("chk multibyte msg: sync is known, "));
3099                 SDptr->sync = 1;
3100
3101                 if(message_out) {
3102                         ESPLOG(("esp%d: sending sdtr back, hoping for msgout\n",
3103                                 esp->esp_id));
3104                         build_sync_nego_msg(esp, period, offset);
3105                         esp_advance_phase(SCptr, in_the_dark);
3106                         return EXTENDED_MESSAGE;
3107                 }
3108
3109                 ESPSDTR(("returning zero\n"));
3110                 esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
3111                 return 0;
3112         } else if(esp->cur_msgin[2] == EXTENDED_WDTR) {
3113                 ESPLOG(("esp%d: AIEEE wide msg received\n", esp->esp_id));
3114                 message_out = MESSAGE_REJECT;
3115         } else if(esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
3116                 ESPLOG(("esp%d: rejecting modify data ptr msg\n", esp->esp_id));
3117                 message_out = MESSAGE_REJECT;
3118         }
3119         esp_advance_phase(SCptr, in_the_dark);
3120         return message_out;
3121 }
3122
3123 static int esp_do_msgindone(struct NCR_ESP *esp, struct ESP_regs *eregs)
3124 {
3125         Scsi_Cmnd *SCptr = esp->current_SC;
3126         int message_out = 0, it = 0, rval;
3127
3128         rval = skipahead1(esp, eregs, SCptr, in_msgin, in_msgindone);
3129         if(rval)
3130                 return rval;
3131         if(SCptr->SCp.sent_command != in_status) {
3132                 if(!(esp->ireg & ESP_INTR_DC)) {
3133                         if(esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
3134                                 message_out = MSG_PARITY_ERROR;
3135                                 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3136                         } else if((it = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES))!=1) {
3137                                 /* We certainly dropped the ball somewhere. */
3138                                 message_out = INITIATOR_ERROR;
3139                                 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3140                         } else if(!esp->msgin_len) {
3141                                 it = esp_read(eregs->esp_fdata);
3142                                 esp_advance_phase(SCptr, in_msgincont);
3143                         } else {
3144                                 /* it is ok and we want it */
3145                                 it = esp->cur_msgin[esp->msgin_ctr] =
3146                                         esp_read(eregs->esp_fdata);
3147                                 esp->msgin_ctr++;
3148                         }
3149                 } else {
3150                         esp_advance_phase(SCptr, in_the_dark);
3151                         return do_work_bus;
3152                 }
3153         } else {
3154                 it = esp->cur_msgin[0];
3155         }
3156         if(!message_out && esp->msgin_len) {
3157                 if(esp->msgin_ctr < esp->msgin_len) {
3158                         esp_advance_phase(SCptr, in_msgincont);
3159                 } else if(esp->msgin_len == 1) {
3160                         message_out = check_singlebyte_msg(esp, eregs);
3161                 } else if(esp->msgin_len == 2) {
3162                         if(esp->cur_msgin[0] == EXTENDED_MESSAGE) {
3163                                 if((it+2) >= 15) {
3164                                         message_out = MESSAGE_REJECT;
3165                                 } else {
3166                                         esp->msgin_len = (it + 2);
3167                                         esp_advance_phase(SCptr, in_msgincont);
3168                                 }
3169                         } else {
3170                                 message_out = MESSAGE_REJECT; /* foo on you */
3171                         }
3172                 } else {
3173                         message_out = check_multibyte_msg(esp, eregs);
3174                 }
3175         }
3176         if(message_out < 0) {
3177                 return -message_out;
3178         } else if(message_out) {
3179                 if(((message_out != 1) &&
3180                     ((message_out < 0x20) || (message_out & 0x80))))
3181                         esp->msgout_len = 1;
3182                 esp->cur_msgout[0] = message_out;
3183                 esp_cmd(esp, eregs, ESP_CMD_SATN);
3184                 esp_advance_phase(SCptr, in_the_dark);
3185                 esp->msgin_len = 0;
3186         }
3187         esp->sreg = esp_read(eregs->esp_status);
3188         esp->sreg &= ~(ESP_STAT_INTR);
3189         if((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
3190                 esp_cmd(esp, eregs, ESP_CMD_MOK);
3191         if((SCptr->SCp.sent_command == in_msgindone) &&
3192             (SCptr->SCp.phase == in_freeing))
3193                 return esp_do_freebus(esp, eregs);
3194         return do_intr_end;
3195 }
3196
3197 static int esp_do_cmdbegin(struct NCR_ESP *esp, struct ESP_regs *eregs)
3198 {
3199         unsigned char tmp;
3200         Scsi_Cmnd *SCptr = esp->current_SC;
3201
3202         esp_advance_phase(SCptr, in_cmdend);
3203         esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3204         tmp = *esp->esp_scmdp++;
3205         esp->esp_scmdleft--;
3206         esp_write(eregs->esp_fdata, tmp);
3207         esp_cmd(esp, eregs, ESP_CMD_TI);
3208         return do_intr_end;
3209 }
3210
3211 static int esp_do_cmddone(struct NCR_ESP *esp, struct ESP_regs *eregs)
3212 {
3213         esp_cmd(esp, eregs, ESP_CMD_NULL);
3214         if(esp->ireg & ESP_INTR_BSERV) {
3215                 esp_advance_phase(esp->current_SC, in_the_dark);
3216                 return esp_do_phase_determine(esp, eregs);
3217         }
3218         ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.\n",
3219                 esp->esp_id));
3220         return do_reset_bus;
3221 }
3222
3223 static int esp_do_msgout(struct NCR_ESP *esp, struct ESP_regs *eregs)
3224 {
3225         esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3226         switch(esp->msgout_len) {
3227         case 1:
3228                 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3229                 esp_cmd(esp, eregs, ESP_CMD_TI);
3230                 break;
3231
3232         case 2:
3233                 if(esp->do_pio_cmds){
3234                         esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3235                         esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3236                         esp_cmd(esp, eregs, ESP_CMD_TI);
3237                 } else {
3238                         esp->esp_command[0] = esp->cur_msgout[0];
3239                         esp->esp_command[1] = esp->cur_msgout[1];
3240                         esp->dma_setup(esp, esp->esp_command_dvma, 2, 0);
3241                         esp_setcount(eregs, 2);
3242                         esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3243                 }
3244                 break;
3245
3246         case 4:
3247                 esp->snip = 1;
3248                 if(esp->do_pio_cmds){
3249                         esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3250                         esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3251                         esp_write(eregs->esp_fdata, esp->cur_msgout[2]);
3252                         esp_write(eregs->esp_fdata, esp->cur_msgout[3]);
3253                         esp_cmd(esp, eregs, ESP_CMD_TI);
3254                 } else {
3255                         esp->esp_command[0] = esp->cur_msgout[0];
3256                         esp->esp_command[1] = esp->cur_msgout[1];
3257                         esp->esp_command[2] = esp->cur_msgout[2];
3258                         esp->esp_command[3] = esp->cur_msgout[3];
3259                         esp->dma_setup(esp, esp->esp_command_dvma, 4, 0);
3260                         esp_setcount(eregs, 4);
3261                         esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3262                 }
3263                 break;
3264
3265         case 5:
3266                 esp->snip = 1;
3267                 if(esp->do_pio_cmds){
3268                         esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3269                         esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3270                         esp_write(eregs->esp_fdata, esp->cur_msgout[2]);
3271                         esp_write(eregs->esp_fdata, esp->cur_msgout[3]);
3272                         esp_write(eregs->esp_fdata, esp->cur_msgout[4]);
3273                         esp_cmd(esp, eregs, ESP_CMD_TI);
3274                 } else {
3275                         esp->esp_command[0] = esp->cur_msgout[0];
3276                         esp->esp_command[1] = esp->cur_msgout[1];
3277                         esp->esp_command[2] = esp->cur_msgout[2];
3278                         esp->esp_command[3] = esp->cur_msgout[3];
3279                         esp->esp_command[4] = esp->cur_msgout[4];
3280                         esp->dma_setup(esp, esp->esp_command_dvma, 5, 0);
3281                         esp_setcount(eregs, 5);
3282                         esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3283                 }
3284                 break;
3285
3286         default:
3287                 /* whoops */
3288                 ESPMISC(("bogus msgout sending NOP\n"));
3289                 esp->cur_msgout[0] = NOP;
3290                 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3291                 esp->msgout_len = 1;
3292                 esp_cmd(esp, eregs, ESP_CMD_TI);
3293                 break;
3294         }
3295         esp_advance_phase(esp->current_SC, in_msgoutdone);
3296         return do_intr_end;
3297 }
3298
3299 static int esp_do_msgoutdone(struct NCR_ESP *esp, 
3300                              struct ESP_regs *eregs)
3301 {
3302         if((esp->msgout_len > 1) && esp->dma_barrier)
3303                 esp->dma_barrier(esp);
3304
3305         if(!(esp->ireg & ESP_INTR_DC)) {
3306                 esp_cmd(esp, eregs, ESP_CMD_NULL);
3307                 switch(esp->sreg & ESP_STAT_PMASK) {
3308                 case ESP_MOP:
3309                         /* whoops, parity error */
3310                         ESPLOG(("esp%d: still in msgout, parity error assumed\n",
3311                                 esp->esp_id));
3312                         if(esp->msgout_len > 1)
3313                                 esp_cmd(esp, eregs, ESP_CMD_SATN);
3314                         esp_advance_phase(esp->current_SC, in_msgout);
3315                         return do_work_bus;
3316
3317                 case ESP_DIP:
3318                         break;
3319
3320                 default:
3321                         if(!fcount(esp, eregs) &&
3322                            !(esp->current_SC->device->sync_max_offset))
3323                                 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3324                         break;
3325
3326                 };
3327         }
3328
3329         /* If we sent out a synchronous negotiation message, update
3330          * our state.
3331          */
3332         if(esp->cur_msgout[2] == EXTENDED_MESSAGE &&
3333            esp->cur_msgout[4] == EXTENDED_SDTR) {
3334                 esp->snip = 1; /* anal retentiveness... */
3335         }
3336
3337         esp->prevmsgout = esp->cur_msgout[0];
3338         esp->msgout_len = 0;
3339         esp_advance_phase(esp->current_SC, in_the_dark);
3340         return esp_do_phase_determine(esp, eregs);
3341 }
3342
3343 static int esp_bus_unexpected(struct NCR_ESP *esp, struct ESP_regs *eregs)
3344 {
3345         ESPLOG(("esp%d: command in weird state %2x\n",
3346                 esp->esp_id, esp->current_SC->SCp.phase));
3347         return do_reset_bus;
3348 }
3349
3350 static espfunc_t bus_vector[] = {
3351         esp_do_data_finale,
3352         esp_do_data_finale,
3353         esp_bus_unexpected,
3354         esp_do_msgin,
3355         esp_do_msgincont,
3356         esp_do_msgindone,
3357         esp_do_msgout,
3358         esp_do_msgoutdone,
3359         esp_do_cmdbegin,
3360         esp_do_cmddone,
3361         esp_do_status,
3362         esp_do_freebus,
3363         esp_do_phase_determine,
3364         esp_bus_unexpected,
3365         esp_bus_unexpected,
3366         esp_bus_unexpected,
3367 };
3368
3369 /* This is the second tier in our dual-level SCSI state machine. */
3370 static int esp_work_bus(struct NCR_ESP *esp, struct ESP_regs *eregs)
3371 {
3372         Scsi_Cmnd *SCptr = esp->current_SC;
3373         unsigned int phase;
3374
3375         ESPBUS(("esp_work_bus: "));
3376         if(!SCptr) {
3377                 ESPBUS(("reconnect\n"));
3378                 return esp_do_reconnect(esp, eregs);
3379         }
3380         phase = SCptr->SCp.phase;
3381         if ((phase & 0xf0) == in_phases_mask)
3382                 return bus_vector[(phase & 0x0f)](esp, eregs);
3383         else if((phase & 0xf0) == in_slct_mask)
3384                 return esp_select_complete(esp, eregs);
3385         else
3386                 return esp_bus_unexpected(esp, eregs);
3387 }
3388
3389 static espfunc_t isvc_vector[] = {
3390         0,
3391         esp_do_phase_determine,
3392         esp_do_resetbus,
3393         esp_finish_reset,
3394         esp_work_bus
3395 };
3396
3397 /* Main interrupt handler for an esp adapter. */
3398 void esp_handle(struct NCR_ESP *esp)
3399 {
3400         struct ESP_regs *eregs;
3401         Scsi_Cmnd *SCptr;
3402         int what_next = do_intr_end;
3403         eregs = esp->eregs;
3404         SCptr = esp->current_SC;
3405
3406         if(esp->dma_irq_entry)
3407                 esp->dma_irq_entry(esp);
3408
3409         /* Check for errors. */
3410         esp->sreg = esp_read(eregs->esp_status);
3411         esp->sreg &= (~ESP_STAT_INTR);
3412         esp->seqreg = (esp_read(eregs->esp_sstep) & ESP_STEP_VBITS);
3413         esp->ireg = esp_read(eregs->esp_intrpt);   /* Unlatch intr and stat regs */
3414         ESPIRQ(("handle_irq: [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
3415                 esp->sreg, esp->seqreg, esp->ireg));
3416         if(esp->sreg & (ESP_STAT_SPAM)) {
3417                 /* Gross error, could be due to one of:
3418                  *
3419                  * - top of fifo overwritten, could be because
3420                  *   we tried to do a synchronous transfer with
3421                  *   an offset greater than ESP fifo size
3422                  *
3423                  * - top of command register overwritten
3424                  *
3425                  * - DMA setup to go in one direction, SCSI
3426                  *   bus points in the other, whoops
3427                  *
3428                  * - weird phase change during asynchronous
3429                  *   data phase while we are initiator
3430                  */
3431                 ESPLOG(("esp%d: Gross error sreg=%2x\n", esp->esp_id, esp->sreg));
3432
3433                 /* If a command is live on the bus we cannot safely
3434                  * reset the bus, so we'll just let the pieces fall
3435                  * where they may.  Here we are hoping that the
3436                  * target will be able to cleanly go away soon
3437                  * so we can safely reset things.
3438                  */
3439                 if(!SCptr) {
3440                         ESPLOG(("esp%d: No current cmd during gross error, "
3441                                 "resetting bus\n", esp->esp_id));
3442                         what_next = do_reset_bus;
3443                         goto state_machine;
3444                 }
3445         }
3446
3447         /* No current cmd is only valid at this point when there are
3448          * commands off the bus or we are trying a reset.
3449          */
3450         if(!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
3451                 /* Panic is safe, since current_SC is null. */
3452                 ESPLOG(("esp%d: no command in esp_handle()\n", esp->esp_id));
3453                 panic("esp_handle: current_SC == penguin within interrupt!");
3454         }
3455
3456         if(esp->ireg & (ESP_INTR_IC)) {
3457                 /* Illegal command fed to ESP.  Outside of obvious
3458                  * software bugs that could cause this, there is
3459                  * a condition with ESP100 where we can confuse the
3460                  * ESP into an erroneous illegal command interrupt
3461                  * because it does not scrape the FIFO properly
3462                  * for reselection.  See esp100_reconnect_hwbug()
3463                  * to see how we try very hard to avoid this.
3464                  */
3465                 ESPLOG(("esp%d: illegal command\n", esp->esp_id));
3466
3467                 esp_dump_state(esp, eregs);
3468
3469                 if(SCptr) {
3470                         /* Devices with very buggy firmware can drop BSY
3471                          * during a scatter list interrupt when using sync
3472                          * mode transfers.  We continue the transfer as
3473                          * expected, the target drops the bus, the ESP
3474                          * gets confused, and we get a illegal command
3475                          * interrupt because the bus is in the disconnected
3476                          * state now and ESP_CMD_TI is only allowed when
3477                          * a nexus is alive on the bus.
3478                          */
3479                         ESPLOG(("esp%d: Forcing async and disabling disconnect for "
3480                                 "target %d\n", esp->esp_id, SCptr->target));
3481                         SCptr->device->borken = 1; /* foo on you */
3482                 }
3483
3484                 what_next = do_reset_bus;
3485         } else if(!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
3486                 int phase;
3487
3488                 if(SCptr) {
3489                         phase = SCptr->SCp.phase;
3490                         if(phase & in_phases_mask) {
3491                                 what_next = esp_work_bus(esp, eregs);
3492                         } else if(phase & in_slct_mask) {
3493                                 what_next = esp_select_complete(esp, eregs);
3494                         } else {
3495                                 ESPLOG(("esp%d: interrupt for no good reason...\n",
3496                                         esp->esp_id));
3497                                 what_next = do_intr_end;
3498                         }
3499                 } else {
3500                         ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULL\n",
3501                                 esp->esp_id));
3502                         what_next = do_reset_bus;
3503                 }
3504         } else if(esp->ireg & ESP_INTR_SR) {
3505                 ESPLOG(("esp%d: SCSI bus reset interrupt\n", esp->esp_id));
3506                 what_next = do_reset_complete;
3507         } else if(esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
3508                 ESPLOG(("esp%d: AIEEE we have been selected by another initiator!\n",
3509                         esp->esp_id));
3510                 what_next = do_reset_bus;
3511         } else if(esp->ireg & ESP_INTR_RSEL) {
3512                 if(!SCptr) {
3513                         /* This is ok. */
3514                         what_next = esp_do_reconnect(esp, eregs);
3515                 } else if(SCptr->SCp.phase & in_slct_mask) {
3516                         /* Only selection code knows how to clean
3517                          * up properly.
3518                          */
3519                         ESPDISC(("Reselected during selection attempt\n"));
3520                         what_next = esp_select_complete(esp, eregs);
3521                 } else {
3522                         ESPLOG(("esp%d: Reselected while bus is busy\n",
3523                                 esp->esp_id));
3524                         what_next = do_reset_bus;
3525                 }
3526         }
3527
3528         /* This is tier-one in our dual level SCSI state machine. */
3529 state_machine:
3530         while(what_next != do_intr_end) {
3531                 if (what_next >= do_phase_determine &&
3532                     what_next < do_intr_end)
3533                         what_next = isvc_vector[what_next](esp, eregs);
3534                 else {
3535                         /* state is completely lost ;-( */
3536                         ESPLOG(("esp%d: interrupt engine loses state, resetting bus\n",
3537                                 esp->esp_id));
3538                         what_next = do_reset_bus;
3539                 }
3540         }
3541         if(esp->dma_irq_exit)
3542                 esp->dma_irq_exit(esp);
3543 }
3544
3545 #ifndef CONFIG_SMP
3546 void esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
3547 {
3548         struct NCR_ESP *esp;
3549         unsigned long flags;
3550         int again;
3551
3552         /* Handle all ESP interrupts showing at this IRQ level. */
3553         spin_lock_irqsave(&io_request_lock, flags);
3554 repeat:
3555         again = 0;
3556         for_each_esp(esp) {
3557 #ifndef __mips__            
3558                 if(((esp)->irq & 0xff) == irq) {
3559 #endif              
3560                         if(esp->dma_irq_p(esp)) {
3561                                 again = 1;
3562
3563                                 esp->dma_ints_off(esp);
3564
3565                                 ESPIRQ(("I%d(", esp->esp_id));
3566                                 esp_handle(esp);
3567                                 ESPIRQ((")"));
3568
3569                                 esp->dma_ints_on(esp);
3570                         }
3571 #ifndef __mips__                    
3572                 }
3573 #endif      
3574         }
3575         if(again)
3576                 goto repeat;
3577         spin_unlock_irqrestore(&io_request_lock, flags);
3578 }
3579 #else
3580 /* For SMP we only service one ESP on the list list at our IRQ level! */
3581 void esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
3582 {
3583         struct NCR_ESP *esp;
3584         unsigned long flags;
3585
3586         /* Handle all ESP interrupts showing at this IRQ level. */
3587         spin_lock_irqsave(&io_request_lock, flags);
3588         for_each_esp(esp) {
3589                 if(((esp)->irq & 0xf) == irq) {
3590                         if(esp->dma_irq_p(esp)) {
3591                                 esp->dma_ints_off(esp);
3592
3593                                 ESPIRQ(("I[%d:%d](",
3594                                         smp_processor_id(), esp->esp_id));
3595                                 esp_handle(esp);
3596                                 ESPIRQ((")"));
3597
3598                                 esp->dma_ints_on(esp);
3599                                 goto out;
3600                         }
3601                 }
3602         }
3603 out:
3604         spin_unlock_irqrestore(&io_request_lock, flags);
3605 }
3606 #endif
3607
3608 #ifdef MODULE
3609 int init_module(void) { return 0; }
3610 void cleanup_module(void) {}
3611 void esp_release(void)
3612 {
3613         MOD_DEC_USE_COUNT;
3614         esps_in_use--;
3615         esps_running = esps_in_use;
3616 }
3617 #endif