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