initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / scsi / esp.c
blob782c31becb3ad151fb4d62d208d3f9f70ff2bd93
1 /* $Id: esp.c,v 1.101 2002/01/15 06:48:55 davem Exp $
2 * esp.c: EnhancedScsiProcessor Sun SCSI driver code.
4 * Copyright (C) 1995, 1998 David S. Miller (davem@caip.rutgers.edu)
5 */
7 /* TODO:
9 * 1) Maybe disable parity checking in config register one for SCSI1
10 * targets. (Gilmore says parity error on the SBus can lock up
11 * old sun4c's)
12 * 2) Add support for DMA2 pipelining.
13 * 3) Add tagged queueing.
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/blkdev.h>
23 #include <linux/proc_fs.h>
24 #include <linux/stat.h>
25 #include <linux/init.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
30 #include "esp.h"
32 #include <asm/sbus.h>
33 #include <asm/dma.h>
34 #include <asm/system.h>
35 #include <asm/ptrace.h>
36 #include <asm/pgtable.h>
37 #include <asm/oplib.h>
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #ifndef __sparc_v9__
41 #include <asm/machines.h>
42 #include <asm/idprom.h>
43 #endif
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_cmnd.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_eh.h>
49 #include <scsi/scsi_host.h>
50 #include <scsi/scsi_tcq.h>
52 #define DEBUG_ESP
53 /* #define DEBUG_ESP_HME */
54 /* #define DEBUG_ESP_DATA */
55 /* #define DEBUG_ESP_QUEUE */
56 /* #define DEBUG_ESP_DISCONNECT */
57 /* #define DEBUG_ESP_STATUS */
58 /* #define DEBUG_ESP_PHASES */
59 /* #define DEBUG_ESP_WORKBUS */
60 /* #define DEBUG_STATE_MACHINE */
61 /* #define DEBUG_ESP_CMDS */
62 /* #define DEBUG_ESP_IRQS */
63 /* #define DEBUG_SDTR */
64 /* #define DEBUG_ESP_SG */
66 /* Use the following to sprinkle debugging messages in a way which
67 * suits you if combinations of the above become too verbose when
68 * trying to track down a specific problem.
70 /* #define DEBUG_ESP_MISC */
72 #if defined(DEBUG_ESP)
73 #define ESPLOG(foo) printk foo
74 #else
75 #define ESPLOG(foo)
76 #endif /* (DEBUG_ESP) */
78 #if defined(DEBUG_ESP_HME)
79 #define ESPHME(foo) printk foo
80 #else
81 #define ESPHME(foo)
82 #endif
84 #if defined(DEBUG_ESP_DATA)
85 #define ESPDATA(foo) printk foo
86 #else
87 #define ESPDATA(foo)
88 #endif
90 #if defined(DEBUG_ESP_QUEUE)
91 #define ESPQUEUE(foo) printk foo
92 #else
93 #define ESPQUEUE(foo)
94 #endif
96 #if defined(DEBUG_ESP_DISCONNECT)
97 #define ESPDISC(foo) printk foo
98 #else
99 #define ESPDISC(foo)
100 #endif
102 #if defined(DEBUG_ESP_STATUS)
103 #define ESPSTAT(foo) printk foo
104 #else
105 #define ESPSTAT(foo)
106 #endif
108 #if defined(DEBUG_ESP_PHASES)
109 #define ESPPHASE(foo) printk foo
110 #else
111 #define ESPPHASE(foo)
112 #endif
114 #if defined(DEBUG_ESP_WORKBUS)
115 #define ESPBUS(foo) printk foo
116 #else
117 #define ESPBUS(foo)
118 #endif
120 #if defined(DEBUG_ESP_IRQS)
121 #define ESPIRQ(foo) printk foo
122 #else
123 #define ESPIRQ(foo)
124 #endif
126 #if defined(DEBUG_SDTR)
127 #define ESPSDTR(foo) printk foo
128 #else
129 #define ESPSDTR(foo)
130 #endif
132 #if defined(DEBUG_ESP_MISC)
133 #define ESPMISC(foo) printk foo
134 #else
135 #define ESPMISC(foo)
136 #endif
138 /* Command phase enumeration. */
139 enum {
140 not_issued = 0x00, /* Still in the issue_SC queue. */
142 /* Various forms of selecting a target. */
143 #define in_slct_mask 0x10
144 in_slct_norm = 0x10, /* ESP is arbitrating, normal selection */
145 in_slct_stop = 0x11, /* ESP will select, then stop with IRQ */
146 in_slct_msg = 0x12, /* select, then send a message */
147 in_slct_tag = 0x13, /* select and send tagged queue msg */
148 in_slct_sneg = 0x14, /* select and acquire sync capabilities */
150 /* Any post selection activity. */
151 #define in_phases_mask 0x20
152 in_datain = 0x20, /* Data is transferring from the bus */
153 in_dataout = 0x21, /* Data is transferring to the bus */
154 in_data_done = 0x22, /* Last DMA data operation done (maybe) */
155 in_msgin = 0x23, /* Eating message from target */
156 in_msgincont = 0x24, /* Eating more msg bytes from target */
157 in_msgindone = 0x25, /* Decide what to do with what we got */
158 in_msgout = 0x26, /* Sending message to target */
159 in_msgoutdone = 0x27, /* Done sending msg out */
160 in_cmdbegin = 0x28, /* Sending cmd after abnormal selection */
161 in_cmdend = 0x29, /* Done sending slow cmd */
162 in_status = 0x2a, /* Was in status phase, finishing cmd */
163 in_freeing = 0x2b, /* freeing the bus for cmd cmplt or disc */
164 in_the_dark = 0x2c, /* Don't know what bus phase we are in */
166 /* Special states, ie. not normal bus transitions... */
167 #define in_spec_mask 0x80
168 in_abortone = 0x80, /* Aborting one command currently */
169 in_abortall = 0x81, /* Blowing away all commands we have */
170 in_resetdev = 0x82, /* SCSI target reset in progress */
171 in_resetbus = 0x83, /* SCSI bus reset in progress */
172 in_tgterror = 0x84, /* Target did something stupid */
175 enum {
176 /* Zero has special meaning, see skipahead[12]. */
177 /*0*/ do_never,
179 /*1*/ do_phase_determine,
180 /*2*/ do_reset_bus,
181 /*3*/ do_reset_complete,
182 /*4*/ do_work_bus,
183 /*5*/ do_intr_end
186 /* The master ring of all esp hosts we are managing in this driver. */
187 static struct esp *espchain;
188 static spinlock_t espchain_lock = SPIN_LOCK_UNLOCKED;
189 static int esps_running = 0;
191 /* Forward declarations. */
192 static irqreturn_t esp_intr(int irq, void *dev_id, struct pt_regs *pregs);
194 /* Debugging routines */
195 struct esp_cmdstrings {
196 u8 cmdchar;
197 char *text;
198 } esp_cmd_strings[] = {
199 /* Miscellaneous */
200 { ESP_CMD_NULL, "ESP_NOP", },
201 { ESP_CMD_FLUSH, "FIFO_FLUSH", },
202 { ESP_CMD_RC, "RSTESP", },
203 { ESP_CMD_RS, "RSTSCSI", },
204 /* Disconnected State Group */
205 { ESP_CMD_RSEL, "RESLCTSEQ", },
206 { ESP_CMD_SEL, "SLCTNATN", },
207 { ESP_CMD_SELA, "SLCTATN", },
208 { ESP_CMD_SELAS, "SLCTATNSTOP", },
209 { ESP_CMD_ESEL, "ENSLCTRESEL", },
210 { ESP_CMD_DSEL, "DISSELRESEL", },
211 { ESP_CMD_SA3, "SLCTATN3", },
212 { ESP_CMD_RSEL3, "RESLCTSEQ", },
213 /* Target State Group */
214 { ESP_CMD_SMSG, "SNDMSG", },
215 { ESP_CMD_SSTAT, "SNDSTATUS", },
216 { ESP_CMD_SDATA, "SNDDATA", },
217 { ESP_CMD_DSEQ, "DISCSEQ", },
218 { ESP_CMD_TSEQ, "TERMSEQ", },
219 { ESP_CMD_TCCSEQ, "TRGTCMDCOMPSEQ", },
220 { ESP_CMD_DCNCT, "DISC", },
221 { ESP_CMD_RMSG, "RCVMSG", },
222 { ESP_CMD_RCMD, "RCVCMD", },
223 { ESP_CMD_RDATA, "RCVDATA", },
224 { ESP_CMD_RCSEQ, "RCVCMDSEQ", },
225 /* Initiator State Group */
226 { ESP_CMD_TI, "TRANSINFO", },
227 { ESP_CMD_ICCSEQ, "INICMDSEQCOMP", },
228 { ESP_CMD_MOK, "MSGACCEPTED", },
229 { ESP_CMD_TPAD, "TPAD", },
230 { ESP_CMD_SATN, "SATN", },
231 { ESP_CMD_RATN, "RATN", },
233 #define NUM_ESP_COMMANDS ((sizeof(esp_cmd_strings)) / (sizeof(struct esp_cmdstrings)))
235 /* Print textual representation of an ESP command */
236 static inline void esp_print_cmd(u8 espcmd)
238 u8 dma_bit = espcmd & ESP_CMD_DMA;
239 int i;
241 espcmd &= ~dma_bit;
242 for (i = 0; i < NUM_ESP_COMMANDS; i++)
243 if (esp_cmd_strings[i].cmdchar == espcmd)
244 break;
245 if (i == NUM_ESP_COMMANDS)
246 printk("ESP_Unknown");
247 else
248 printk("%s%s", esp_cmd_strings[i].text,
249 ((dma_bit) ? "+DMA" : ""));
252 /* Print the status register's value */
253 static inline void esp_print_statreg(u8 statreg)
255 u8 phase;
257 printk("STATUS<");
258 phase = statreg & ESP_STAT_PMASK;
259 printk("%s,", (phase == ESP_DOP ? "DATA-OUT" :
260 (phase == ESP_DIP ? "DATA-IN" :
261 (phase == ESP_CMDP ? "COMMAND" :
262 (phase == ESP_STATP ? "STATUS" :
263 (phase == ESP_MOP ? "MSG-OUT" :
264 (phase == ESP_MIP ? "MSG_IN" :
265 "unknown")))))));
266 if (statreg & ESP_STAT_TDONE)
267 printk("TRANS_DONE,");
268 if (statreg & ESP_STAT_TCNT)
269 printk("TCOUNT_ZERO,");
270 if (statreg & ESP_STAT_PERR)
271 printk("P_ERROR,");
272 if (statreg & ESP_STAT_SPAM)
273 printk("SPAM,");
274 if (statreg & ESP_STAT_INTR)
275 printk("IRQ,");
276 printk(">");
279 /* Print the interrupt register's value */
280 static inline void esp_print_ireg(u8 intreg)
282 printk("INTREG< ");
283 if (intreg & ESP_INTR_S)
284 printk("SLCT_NATN ");
285 if (intreg & ESP_INTR_SATN)
286 printk("SLCT_ATN ");
287 if (intreg & ESP_INTR_RSEL)
288 printk("RSLCT ");
289 if (intreg & ESP_INTR_FDONE)
290 printk("FDONE ");
291 if (intreg & ESP_INTR_BSERV)
292 printk("BSERV ");
293 if (intreg & ESP_INTR_DC)
294 printk("DISCNCT ");
295 if (intreg & ESP_INTR_IC)
296 printk("ILL_CMD ");
297 if (intreg & ESP_INTR_SR)
298 printk("SCSI_BUS_RESET ");
299 printk(">");
302 /* Print the sequence step registers contents */
303 static inline void esp_print_seqreg(u8 stepreg)
305 stepreg &= ESP_STEP_VBITS;
306 printk("STEP<%s>",
307 (stepreg == ESP_STEP_ASEL ? "SLCT_ARB_CMPLT" :
308 (stepreg == ESP_STEP_SID ? "1BYTE_MSG_SENT" :
309 (stepreg == ESP_STEP_NCMD ? "NOT_IN_CMD_PHASE" :
310 (stepreg == ESP_STEP_PPC ? "CMD_BYTES_LOST" :
311 (stepreg == ESP_STEP_FINI4 ? "CMD_SENT_OK" :
312 "UNKNOWN"))))));
315 static char *phase_string(int phase)
317 switch (phase) {
318 case not_issued:
319 return "UNISSUED";
320 case in_slct_norm:
321 return "SLCTNORM";
322 case in_slct_stop:
323 return "SLCTSTOP";
324 case in_slct_msg:
325 return "SLCTMSG";
326 case in_slct_tag:
327 return "SLCTTAG";
328 case in_slct_sneg:
329 return "SLCTSNEG";
330 case in_datain:
331 return "DATAIN";
332 case in_dataout:
333 return "DATAOUT";
334 case in_data_done:
335 return "DATADONE";
336 case in_msgin:
337 return "MSGIN";
338 case in_msgincont:
339 return "MSGINCONT";
340 case in_msgindone:
341 return "MSGINDONE";
342 case in_msgout:
343 return "MSGOUT";
344 case in_msgoutdone:
345 return "MSGOUTDONE";
346 case in_cmdbegin:
347 return "CMDBEGIN";
348 case in_cmdend:
349 return "CMDEND";
350 case in_status:
351 return "STATUS";
352 case in_freeing:
353 return "FREEING";
354 case in_the_dark:
355 return "CLUELESS";
356 case in_abortone:
357 return "ABORTONE";
358 case in_abortall:
359 return "ABORTALL";
360 case in_resetdev:
361 return "RESETDEV";
362 case in_resetbus:
363 return "RESETBUS";
364 case in_tgterror:
365 return "TGTERROR";
366 default:
367 return "UNKNOWN";
371 #ifdef DEBUG_STATE_MACHINE
372 static inline void esp_advance_phase(struct scsi_cmnd *s, int newphase)
374 ESPLOG(("<%s>", phase_string(newphase)));
375 s->SCp.sent_command = s->SCp.phase;
376 s->SCp.phase = newphase;
378 #else
379 #define esp_advance_phase(__s, __newphase) \
380 (__s)->SCp.sent_command = (__s)->SCp.phase; \
381 (__s)->SCp.phase = (__newphase);
382 #endif
384 #ifdef DEBUG_ESP_CMDS
385 static inline void esp_cmd(struct esp *esp, u8 cmd)
387 esp->espcmdlog[esp->espcmdent] = cmd;
388 esp->espcmdent = (esp->espcmdent + 1) & 31;
389 sbus_writeb(cmd, esp->eregs + ESP_CMD);
391 #else
392 #define esp_cmd(__esp, __cmd) \
393 sbus_writeb((__cmd), ((__esp)->eregs) + ESP_CMD)
394 #endif
396 #define ESP_INTSOFF(__dregs) \
397 sbus_writel(sbus_readl((__dregs)+DMA_CSR)&~(DMA_INT_ENAB), (__dregs)+DMA_CSR)
398 #define ESP_INTSON(__dregs) \
399 sbus_writel(sbus_readl((__dregs)+DMA_CSR)|DMA_INT_ENAB, (__dregs)+DMA_CSR)
400 #define ESP_IRQ_P(__dregs) \
401 (sbus_readl((__dregs)+DMA_CSR) & (DMA_HNDL_INTR|DMA_HNDL_ERROR))
403 /* How we use the various Linux SCSI data structures for operation.
405 * struct scsi_cmnd:
407 * We keep track of the synchronous capabilities of a target
408 * in the device member, using sync_min_period and
409 * sync_max_offset. These are the values we directly write
410 * into the ESP registers while running a command. If offset
411 * is zero the ESP will use asynchronous transfers.
412 * If the borken flag is set we assume we shouldn't even bother
413 * trying to negotiate for synchronous transfer as this target
414 * is really stupid. If we notice the target is dropping the
415 * bus, and we have been allowing it to disconnect, we clear
416 * the disconnect flag.
420 /* Manipulation of the ESP command queues. Thanks to the aha152x driver
421 * and its author, Juergen E. Fischer, for the methods used here.
422 * Note that these are per-ESP queues, not global queues like
423 * the aha152x driver uses.
425 static inline void append_SC(struct scsi_cmnd **SC, struct scsi_cmnd *new_SC)
427 struct scsi_cmnd *end;
429 new_SC->host_scribble = (unsigned char *) NULL;
430 if (!*SC)
431 *SC = new_SC;
432 else {
433 for (end=*SC;end->host_scribble;end=(struct scsi_cmnd *)end->host_scribble)
435 end->host_scribble = (unsigned char *) new_SC;
439 static inline void prepend_SC(struct scsi_cmnd **SC, struct scsi_cmnd *new_SC)
441 new_SC->host_scribble = (unsigned char *) *SC;
442 *SC = new_SC;
445 static inline struct scsi_cmnd *remove_first_SC(struct scsi_cmnd **SC)
447 struct scsi_cmnd *ptr;
448 ptr = *SC;
449 if (ptr)
450 *SC = (struct scsi_cmnd *) (*SC)->host_scribble;
451 return ptr;
454 static inline struct scsi_cmnd *remove_SC(struct scsi_cmnd **SC, int target, int lun)
456 struct scsi_cmnd *ptr, *prev;
458 for (ptr = *SC, prev = NULL;
459 ptr && ((ptr->device->id != target) || (ptr->device->lun != lun));
460 prev = ptr, ptr = (struct scsi_cmnd *) ptr->host_scribble)
462 if (ptr) {
463 if (prev)
464 prev->host_scribble=ptr->host_scribble;
465 else
466 *SC=(struct scsi_cmnd *)ptr->host_scribble;
468 return ptr;
471 /* Resetting various pieces of the ESP scsi driver chipset/buses. */
472 static void esp_reset_dma(struct esp *esp)
474 int can_do_burst16, can_do_burst32, can_do_burst64;
475 int can_do_sbus64;
476 u32 tmp;
478 can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
479 can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
480 can_do_burst64 = 0;
481 can_do_sbus64 = 0;
482 if (sbus_can_dma_64bit(esp->sdev))
483 can_do_sbus64 = 1;
484 if (sbus_can_burst64(esp->sdev))
485 can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
487 /* Punt the DVMA into a known state. */
488 if (esp->dma->revision != dvmahme) {
489 tmp = sbus_readl(esp->dregs + DMA_CSR);
490 sbus_writel(tmp | DMA_RST_SCSI, esp->dregs + DMA_CSR);
491 sbus_writel(tmp & ~DMA_RST_SCSI, esp->dregs + DMA_CSR);
493 switch (esp->dma->revision) {
494 case dvmahme:
495 /* This is the HME DVMA gate array. */
497 sbus_writel(DMA_RESET_FAS366, esp->dregs + DMA_CSR);
498 sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
500 esp->prev_hme_dmacsr = (DMA_PARITY_OFF|DMA_2CLKS|DMA_SCSI_DISAB|DMA_INT_ENAB);
501 esp->prev_hme_dmacsr &= ~(DMA_ENABLE|DMA_ST_WRITE|DMA_BRST_SZ);
503 if (can_do_burst64)
504 esp->prev_hme_dmacsr |= DMA_BRST64;
505 else if (can_do_burst32)
506 esp->prev_hme_dmacsr |= DMA_BRST32;
508 if (can_do_sbus64) {
509 esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
510 sbus_set_sbus64(esp->sdev, esp->bursts);
513 /* This chip is horrible. */
514 while (sbus_readl(esp->dregs + DMA_CSR) & DMA_PEND_READ)
515 udelay(1);
517 sbus_writel(0, esp->dregs + DMA_CSR);
518 sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
520 /* This is necessary to avoid having the SCSI channel
521 * engine lock up on us.
523 sbus_writel(0, esp->dregs + DMA_ADDR);
525 break;
526 case dvmarev2:
527 /* This is the gate array found in the sun4m
528 * NCR SBUS I/O subsystem.
530 if (esp->erev != esp100) {
531 tmp = sbus_readl(esp->dregs + DMA_CSR);
532 sbus_writel(tmp | DMA_3CLKS, esp->dregs + DMA_CSR);
534 break;
535 case dvmarev3:
536 tmp = sbus_readl(esp->dregs + DMA_CSR);
537 tmp &= ~DMA_3CLKS;
538 tmp |= DMA_2CLKS;
539 if (can_do_burst32) {
540 tmp &= ~DMA_BRST_SZ;
541 tmp |= DMA_BRST32;
543 sbus_writel(tmp, esp->dregs + DMA_CSR);
544 break;
545 case dvmaesc1:
546 /* This is the DMA unit found on SCSI/Ether cards. */
547 tmp = sbus_readl(esp->dregs + DMA_CSR);
548 tmp |= DMA_ADD_ENABLE;
549 tmp &= ~DMA_BCNT_ENAB;
550 if (!can_do_burst32 && can_do_burst16) {
551 tmp |= DMA_ESC_BURST;
552 } else {
553 tmp &= ~(DMA_ESC_BURST);
555 sbus_writel(tmp, esp->dregs + DMA_CSR);
556 break;
557 default:
558 break;
560 ESP_INTSON(esp->dregs);
563 /* Reset the ESP chip, _not_ the SCSI bus. */
564 static void __init esp_reset_esp(struct esp *esp)
566 u8 family_code, version;
567 int i;
569 /* Now reset the ESP chip */
570 esp_cmd(esp, ESP_CMD_RC);
571 esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
572 esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
574 /* Reload the configuration registers */
575 sbus_writeb(esp->cfact, esp->eregs + ESP_CFACT);
576 esp->prev_stp = 0;
577 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
578 esp->prev_soff = 0;
579 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
580 sbus_writeb(esp->neg_defp, esp->eregs + ESP_TIMEO);
582 /* This is the only point at which it is reliable to read
583 * the ID-code for a fast ESP chip variants.
585 esp->max_period = ((35 * esp->ccycle) / 1000);
586 if (esp->erev == fast) {
587 version = sbus_readb(esp->eregs + ESP_UID);
588 family_code = (version & 0xf8) >> 3;
589 if (family_code == 0x02)
590 esp->erev = fas236;
591 else if (family_code == 0x0a)
592 esp->erev = fashme; /* Version is usually '5'. */
593 else
594 esp->erev = fas100a;
595 ESPMISC(("esp%d: FAST chip is %s (family=%d, version=%d)\n",
596 esp->esp_id,
597 (esp->erev == fas236) ? "fas236" :
598 ((esp->erev == fas100a) ? "fas100a" :
599 "fasHME"), family_code, (version & 7)));
601 esp->min_period = ((4 * esp->ccycle) / 1000);
602 } else {
603 esp->min_period = ((5 * esp->ccycle) / 1000);
605 esp->max_period = (esp->max_period + 3)>>2;
606 esp->min_period = (esp->min_period + 3)>>2;
608 sbus_writeb(esp->config1, esp->eregs + ESP_CFG1);
609 switch (esp->erev) {
610 case esp100:
611 /* nothing to do */
612 break;
613 case esp100a:
614 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
615 break;
616 case esp236:
617 /* Slow 236 */
618 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
619 esp->prev_cfg3 = esp->config3[0];
620 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
621 break;
622 case fashme:
623 esp->config2 |= (ESP_CONFIG2_HME32 | ESP_CONFIG2_HMEFENAB);
624 /* fallthrough... */
625 case fas236:
626 /* Fast 236 or HME */
627 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
628 for (i = 0; i < 16; i++) {
629 if (esp->erev == fashme) {
630 u8 cfg3;
632 cfg3 = ESP_CONFIG3_FCLOCK | ESP_CONFIG3_OBPUSH;
633 if (esp->scsi_id >= 8)
634 cfg3 |= ESP_CONFIG3_IDBIT3;
635 esp->config3[i] |= cfg3;
636 } else {
637 esp->config3[i] |= ESP_CONFIG3_FCLK;
640 esp->prev_cfg3 = esp->config3[0];
641 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
642 if (esp->erev == fashme) {
643 esp->radelay = 80;
644 } else {
645 if (esp->diff)
646 esp->radelay = 0;
647 else
648 esp->radelay = 96;
650 break;
651 case fas100a:
652 /* Fast 100a */
653 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
654 for (i = 0; i < 16; i++)
655 esp->config3[i] |= ESP_CONFIG3_FCLOCK;
656 esp->prev_cfg3 = esp->config3[0];
657 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
658 esp->radelay = 32;
659 break;
660 default:
661 panic("esp: what could it be... I wonder...");
662 break;
665 /* Eat any bitrot in the chip */
666 sbus_readb(esp->eregs + ESP_INTRPT);
667 udelay(100);
670 /* This places the ESP into a known state at boot time. */
671 static void __init esp_bootup_reset(struct esp *esp)
673 u8 tmp;
675 /* Reset the DMA */
676 esp_reset_dma(esp);
678 /* Reset the ESP */
679 esp_reset_esp(esp);
681 /* Reset the SCSI bus, but tell ESP not to generate an irq */
682 tmp = sbus_readb(esp->eregs + ESP_CFG1);
683 tmp |= ESP_CONFIG1_SRRDISAB;
684 sbus_writeb(tmp, esp->eregs + ESP_CFG1);
686 esp_cmd(esp, ESP_CMD_RS);
687 udelay(400);
689 sbus_writeb(esp->config1, esp->eregs + ESP_CFG1);
691 /* Eat any bitrot in the chip and we are done... */
692 sbus_readb(esp->eregs + ESP_INTRPT);
695 static void esp_chain_add(struct esp *esp)
697 spin_lock_irq(&espchain_lock);
698 if (espchain) {
699 struct esp *elink = espchain;
700 while (elink->next)
701 elink = elink->next;
702 elink->next = esp;
703 } else {
704 espchain = esp;
706 esp->next = NULL;
707 spin_unlock_irq(&espchain_lock);
710 static void esp_chain_del(struct esp *esp)
712 spin_lock_irq(&espchain_lock);
713 if (espchain == esp) {
714 espchain = esp->next;
715 } else {
716 struct esp *elink = espchain;
717 while (elink->next != esp)
718 elink = elink->next;
719 elink->next = esp->next;
721 esp->next = NULL;
722 spin_unlock_irq(&espchain_lock);
725 static int __init esp_find_dvma(struct esp *esp, struct sbus_dev *dma_sdev)
727 struct sbus_dev *sdev = esp->sdev;
728 struct sbus_dma *dma;
730 if (dma_sdev != NULL) {
731 for_each_dvma(dma) {
732 if (dma->sdev == dma_sdev)
733 break;
735 } else {
736 for_each_dvma(dma) {
737 /* If allocated already, can't use it. */
738 if (dma->allocated)
739 continue;
741 if (dma->sdev == NULL)
742 break;
744 /* If bus + slot are the same and it has the
745 * correct OBP name, it's ours.
747 if (sdev->bus == dma->sdev->bus &&
748 sdev->slot == dma->sdev->slot &&
749 (!strcmp(dma->sdev->prom_name, "dma") ||
750 !strcmp(dma->sdev->prom_name, "espdma")))
751 break;
755 /* If we don't know how to handle the dvma,
756 * do not use this device.
758 if (dma == NULL) {
759 printk("Cannot find dvma for ESP%d's SCSI\n", esp->esp_id);
760 return -1;
762 if (dma->allocated) {
763 printk("esp%d: can't use my espdma\n", esp->esp_id);
764 return -1;
766 dma->allocated = 1;
767 esp->dma = dma;
768 esp->dregs = dma->regs;
770 return 0;
773 static int __init esp_map_regs(struct esp *esp, int hme)
775 struct sbus_dev *sdev = esp->sdev;
776 struct resource *res;
778 /* On HME, two reg sets exist, first is DVMA,
779 * second is ESP registers.
781 if (hme)
782 res = &sdev->resource[1];
783 else
784 res = &sdev->resource[0];
786 esp->eregs = sbus_ioremap(res, 0, ESP_REG_SIZE, "ESP Registers");
788 if (esp->eregs == 0)
789 return -1;
790 return 0;
793 static int __init esp_map_cmdarea(struct esp *esp)
795 struct sbus_dev *sdev = esp->sdev;
797 esp->esp_command = sbus_alloc_consistent(sdev, 16,
798 &esp->esp_command_dvma);
799 if (esp->esp_command == NULL ||
800 esp->esp_command_dvma == 0)
801 return -1;
802 return 0;
805 static int __init esp_register_irq(struct esp *esp)
807 esp->ehost->irq = esp->irq = esp->sdev->irqs[0];
809 /* We used to try various overly-clever things to
810 * reduce the interrupt processing overhead on
811 * sun4c/sun4m when multiple ESP's shared the
812 * same IRQ. It was too complex and messy to
813 * sanely maintain.
815 if (request_irq(esp->ehost->irq, esp_intr,
816 SA_SHIRQ, "ESP SCSI", esp)) {
817 printk("esp%d: Cannot acquire irq line\n",
818 esp->esp_id);
819 return -1;
822 printk("esp%d: IRQ %s ", esp->esp_id,
823 __irq_itoa(esp->ehost->irq));
825 return 0;
828 static void __init esp_get_scsi_id(struct esp *esp)
830 struct sbus_dev *sdev = esp->sdev;
832 esp->scsi_id = prom_getintdefault(esp->prom_node,
833 "initiator-id",
834 -1);
835 if (esp->scsi_id == -1)
836 esp->scsi_id = prom_getintdefault(esp->prom_node,
837 "scsi-initiator-id",
838 -1);
839 if (esp->scsi_id == -1)
840 esp->scsi_id = (sdev->bus == NULL) ? 7 :
841 prom_getintdefault(sdev->bus->prom_node,
842 "scsi-initiator-id",
844 esp->ehost->this_id = esp->scsi_id;
845 esp->scsi_id_mask = (1 << esp->scsi_id);
849 static void __init esp_get_clock_params(struct esp *esp)
851 struct sbus_dev *sdev = esp->sdev;
852 int prom_node = esp->prom_node;
853 int sbus_prom_node;
854 unsigned int fmhz;
855 u8 ccf;
857 if (sdev != NULL && sdev->bus != NULL)
858 sbus_prom_node = sdev->bus->prom_node;
859 else
860 sbus_prom_node = 0;
862 /* This is getting messy but it has to be done
863 * correctly or else you get weird behavior all
864 * over the place. We are trying to basically
865 * figure out three pieces of information.
867 * a) Clock Conversion Factor
869 * This is a representation of the input
870 * crystal clock frequency going into the
871 * ESP on this machine. Any operation whose
872 * timing is longer than 400ns depends on this
873 * value being correct. For example, you'll
874 * get blips for arbitration/selection during
875 * high load or with multiple targets if this
876 * is not set correctly.
878 * b) Selection Time-Out
880 * The ESP isn't very bright and will arbitrate
881 * for the bus and try to select a target
882 * forever if you let it. This value tells
883 * the ESP when it has taken too long to
884 * negotiate and that it should interrupt
885 * the CPU so we can see what happened.
886 * The value is computed as follows (from
887 * NCR/Symbios chip docs).
889 * (Time Out Period) * (Input Clock)
890 * STO = ----------------------------------
891 * (8192) * (Clock Conversion Factor)
893 * You usually want the time out period to be
894 * around 250ms, I think we'll set it a little
895 * bit higher to account for fully loaded SCSI
896 * bus's and slow devices that don't respond so
897 * quickly to selection attempts. (yeah, I know
898 * this is out of spec. but there is a lot of
899 * buggy pieces of firmware out there so bite me)
901 * c) Imperical constants for synchronous offset
902 * and transfer period register values
904 * This entails the smallest and largest sync
905 * period we could ever handle on this ESP.
908 fmhz = prom_getintdefault(prom_node, "clock-frequency", -1);
909 if (fmhz == -1)
910 fmhz = (!sbus_prom_node) ? 0 :
911 prom_getintdefault(sbus_prom_node, "clock-frequency", -1);
913 if (fmhz <= (5000000))
914 ccf = 0;
915 else
916 ccf = (((5000000 - 1) + (fmhz))/(5000000));
918 if (!ccf || ccf > 8) {
919 /* If we can't find anything reasonable,
920 * just assume 20MHZ. This is the clock
921 * frequency of the older sun4c's where I've
922 * been unable to find the clock-frequency
923 * PROM property. All other machines provide
924 * useful values it seems.
926 ccf = ESP_CCF_F4;
927 fmhz = (20000000);
930 if (ccf == (ESP_CCF_F7 + 1))
931 esp->cfact = ESP_CCF_F0;
932 else if (ccf == ESP_CCF_NEVER)
933 esp->cfact = ESP_CCF_F2;
934 else
935 esp->cfact = ccf;
936 esp->raw_cfact = ccf;
938 esp->cfreq = fmhz;
939 esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz);
940 esp->ctick = ESP_TICK(ccf, esp->ccycle);
941 esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf);
942 esp->sync_defp = SYNC_DEFP_SLOW;
944 printk("SCSI ID %d Clk %dMHz CCYC=%d CCF=%d TOut %d ",
945 esp->scsi_id, (fmhz / 1000000),
946 (int)esp->ccycle, (int)ccf, (int) esp->neg_defp);
949 static void __init esp_get_bursts(struct esp *esp, struct sbus_dev *dma)
951 struct sbus_dev *sdev = esp->sdev;
952 u8 bursts;
954 bursts = prom_getintdefault(esp->prom_node, "burst-sizes", 0xff);
956 if (dma) {
957 u8 tmp = prom_getintdefault(dma->prom_node,
958 "burst-sizes", 0xff);
959 if (tmp != 0xff)
960 bursts &= tmp;
963 if (sdev->bus) {
964 u8 tmp = prom_getintdefault(sdev->bus->prom_node,
965 "burst-sizes", 0xff);
966 if (tmp != 0xff)
967 bursts &= tmp;
970 if (bursts == 0xff ||
971 (bursts & DMA_BURST16) == 0 ||
972 (bursts & DMA_BURST32) == 0)
973 bursts = (DMA_BURST32 - 1);
975 esp->bursts = bursts;
978 static void __init esp_get_revision(struct esp *esp)
980 u8 tmp;
982 esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
983 esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
984 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
986 tmp = sbus_readb(esp->eregs + ESP_CFG2);
987 tmp &= ~ESP_CONFIG2_MAGIC;
988 if (tmp != (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
989 /* If what we write to cfg2 does not come back, cfg2
990 * is not implemented, therefore this must be a plain
991 * esp100.
993 esp->erev = esp100;
994 printk("NCR53C90(esp100)\n");
995 } else {
996 esp->config2 = 0;
997 esp->prev_cfg3 = esp->config3[0] = 5;
998 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
999 sbus_writeb(0, esp->eregs + ESP_CFG3);
1000 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
1002 tmp = sbus_readb(esp->eregs + ESP_CFG3);
1003 if (tmp != 5) {
1004 /* The cfg2 register is implemented, however
1005 * cfg3 is not, must be esp100a.
1007 esp->erev = esp100a;
1008 printk("NCR53C90A(esp100a)\n");
1009 } else {
1010 int target;
1012 for (target = 0; target < 16; target++)
1013 esp->config3[target] = 0;
1014 esp->prev_cfg3 = 0;
1015 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
1017 /* All of cfg{1,2,3} implemented, must be one of
1018 * the fas variants, figure out which one.
1020 if (esp->raw_cfact > ESP_CCF_F5) {
1021 esp->erev = fast;
1022 esp->sync_defp = SYNC_DEFP_FAST;
1023 printk("NCR53C9XF(espfast)\n");
1024 } else {
1025 esp->erev = esp236;
1026 printk("NCR53C9x(esp236)\n");
1028 esp->config2 = 0;
1029 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
1034 static void __init esp_init_swstate(struct esp *esp)
1036 int i;
1038 /* Command queues... */
1039 esp->current_SC = NULL;
1040 esp->disconnected_SC = NULL;
1041 esp->issue_SC = NULL;
1043 /* Target and current command state... */
1044 esp->targets_present = 0;
1045 esp->resetting_bus = 0;
1046 esp->snip = 0;
1048 init_waitqueue_head(&esp->reset_queue);
1050 /* Debugging... */
1051 for(i = 0; i < 32; i++)
1052 esp->espcmdlog[i] = 0;
1053 esp->espcmdent = 0;
1055 /* MSG phase state... */
1056 for(i = 0; i < 16; i++) {
1057 esp->cur_msgout[i] = 0;
1058 esp->cur_msgin[i] = 0;
1060 esp->prevmsgout = esp->prevmsgin = 0;
1061 esp->msgout_len = esp->msgin_len = 0;
1063 /* Clear the one behind caches to hold unmatchable values. */
1064 esp->prev_soff = esp->prev_stp = esp->prev_cfg3 = 0xff;
1065 esp->prev_hme_dmacsr = 0xffffffff;
1068 static int __init detect_one_esp(struct scsi_host_template *tpnt, struct sbus_dev *esp_dev,
1069 struct sbus_dev *espdma, struct sbus_bus *sbus,
1070 int id, int hme)
1072 struct Scsi_Host *esp_host = scsi_register(tpnt, sizeof(struct esp));
1073 struct esp *esp;
1075 if (!esp_host) {
1076 printk("ESP: Cannot register SCSI host\n");
1077 return -1;
1079 if (hme)
1080 esp_host->max_id = 16;
1081 esp = (struct esp *) esp_host->hostdata;
1082 esp->ehost = esp_host;
1083 esp->sdev = esp_dev;
1084 esp->esp_id = id;
1085 esp->prom_node = esp_dev->prom_node;
1086 prom_getstring(esp->prom_node, "name", esp->prom_name,
1087 sizeof(esp->prom_name));
1089 esp_chain_add(esp);
1090 if (esp_find_dvma(esp, espdma) < 0)
1091 goto fail_unlink;
1092 if (esp_map_regs(esp, hme) < 0) {
1093 printk("ESP registers unmappable");
1094 goto fail_dvma_release;
1096 if (esp_map_cmdarea(esp) < 0) {
1097 printk("ESP DVMA transport area unmappable");
1098 goto fail_unmap_regs;
1100 if (esp_register_irq(esp) < 0)
1101 goto fail_unmap_cmdarea;
1103 esp_get_scsi_id(esp);
1105 esp->diff = prom_getbool(esp->prom_node, "differential");
1106 if (esp->diff)
1107 printk("Differential ");
1109 esp_get_clock_params(esp);
1110 esp_get_bursts(esp, espdma);
1111 esp_get_revision(esp);
1112 esp_init_swstate(esp);
1114 esp_bootup_reset(esp);
1116 return 0;
1118 fail_unmap_cmdarea:
1119 sbus_free_consistent(esp->sdev, 16,
1120 (void *) esp->esp_command,
1121 esp->esp_command_dvma);
1123 fail_unmap_regs:
1124 sbus_iounmap(esp->eregs, ESP_REG_SIZE);
1126 fail_dvma_release:
1127 esp->dma->allocated = 0;
1129 fail_unlink:
1130 esp_chain_del(esp);
1131 scsi_unregister(esp_host);
1132 return -1;
1135 /* Detecting ESP chips on the machine. This is the simple and easy
1136 * version.
1139 #ifdef CONFIG_SUN4
1141 #include <asm/sun4paddr.h>
1143 static int __init esp_detect(struct scsi_host_template *tpnt)
1145 static struct sbus_dev esp_dev;
1146 int esps_in_use = 0;
1148 espchain = 0;
1150 if (sun4_esp_physaddr) {
1151 memset (&esp_dev, 0, sizeof(esp_dev));
1152 esp_dev.reg_addrs[0].phys_addr = sun4_esp_physaddr;
1153 esp_dev.irqs[0] = 4;
1154 esp_dev.resource[0].start = sun4_esp_physaddr;
1155 esp_dev.resource[0].end = sun4_esp_physaddr + ESP_REG_SIZE - 1;
1156 esp_dev.resource[0].flags = IORESOURCE_IO;
1158 if (!detect_one_esp(tpnt, &esp_dev, NULL, NULL, 0, 0))
1159 esps_in_use++;
1160 printk("ESP: Total of 1 ESP hosts found, %d actually in use.\n", esps_in_use);
1161 esps_running = esps_in_use;
1163 return esps_in_use;
1166 #else /* !CONFIG_SUN4 */
1168 static int __init esp_detect(struct scsi_host_template *tpnt)
1170 struct sbus_bus *sbus;
1171 struct sbus_dev *esp_dev, *sbdev_iter;
1172 int nesps = 0, esps_in_use = 0;
1174 espchain = 0;
1175 if (!sbus_root) {
1176 #ifdef CONFIG_PCI
1177 return 0;
1178 #else
1179 panic("No SBUS in esp_detect()");
1180 #endif
1182 for_each_sbus(sbus) {
1183 for_each_sbusdev(sbdev_iter, sbus) {
1184 struct sbus_dev *espdma = NULL;
1185 int hme = 0;
1187 /* Is it an esp sbus device? */
1188 esp_dev = sbdev_iter;
1189 if (strcmp(esp_dev->prom_name, "esp") &&
1190 strcmp(esp_dev->prom_name, "SUNW,esp")) {
1191 if (!strcmp(esp_dev->prom_name, "SUNW,fas")) {
1192 hme = 1;
1193 espdma = esp_dev;
1194 } else {
1195 if (!esp_dev->child ||
1196 (strcmp(esp_dev->prom_name, "espdma") &&
1197 strcmp(esp_dev->prom_name, "dma")))
1198 continue; /* nope... */
1199 espdma = esp_dev;
1200 esp_dev = esp_dev->child;
1201 if (strcmp(esp_dev->prom_name, "esp") &&
1202 strcmp(esp_dev->prom_name, "SUNW,esp"))
1203 continue; /* how can this happen? */
1207 if (detect_one_esp(tpnt, esp_dev, espdma, sbus, nesps++, hme) < 0)
1208 continue;
1210 esps_in_use++;
1211 } /* for each sbusdev */
1212 } /* for each sbus */
1213 printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps,
1214 esps_in_use);
1215 esps_running = esps_in_use;
1216 return esps_in_use;
1219 #endif /* !CONFIG_SUN4 */
1223 static int esp_release(struct Scsi_Host *host)
1225 struct esp *esp = (struct esp *) host->hostdata;
1227 ESP_INTSOFF(esp->dregs);
1228 #if 0
1229 esp_reset_dma(esp);
1230 esp_reset_esp(esp);
1231 #endif
1233 free_irq(esp->ehost->irq, esp);
1234 sbus_free_consistent(esp->sdev, 16,
1235 (void *) esp->esp_command, esp->esp_command_dvma);
1236 sbus_iounmap(esp->eregs, ESP_REG_SIZE);
1237 esp->dma->allocated = 0;
1238 esp_chain_del(esp);
1240 return 0;
1243 /* The info function will return whatever useful
1244 * information the developer sees fit. If not provided, then
1245 * the name field will be used instead.
1247 static const char *esp_info(struct Scsi_Host *host)
1249 struct esp *esp;
1251 esp = (struct esp *) host->hostdata;
1252 switch (esp->erev) {
1253 case esp100:
1254 return "Sparc ESP100 (NCR53C90)";
1255 case esp100a:
1256 return "Sparc ESP100A (NCR53C90A)";
1257 case esp236:
1258 return "Sparc ESP236";
1259 case fas236:
1260 return "Sparc ESP236-FAST";
1261 case fashme:
1262 return "Sparc ESP366-HME";
1263 case fas100a:
1264 return "Sparc ESP100A-FAST";
1265 default:
1266 return "Bogon ESP revision";
1270 /* From Wolfgang Stanglmeier's NCR scsi driver. */
1271 struct info_str
1273 char *buffer;
1274 int length;
1275 int offset;
1276 int pos;
1279 static void copy_mem_info(struct info_str *info, char *data, int len)
1281 if (info->pos + len > info->length)
1282 len = info->length - info->pos;
1284 if (info->pos + len < info->offset) {
1285 info->pos += len;
1286 return;
1288 if (info->pos < info->offset) {
1289 data += (info->offset - info->pos);
1290 len -= (info->offset - info->pos);
1293 if (len > 0) {
1294 memcpy(info->buffer + info->pos, data, len);
1295 info->pos += len;
1299 static int copy_info(struct info_str *info, char *fmt, ...)
1301 va_list args;
1302 char buf[81];
1303 int len;
1305 va_start(args, fmt);
1306 len = vsprintf(buf, fmt, args);
1307 va_end(args);
1309 copy_mem_info(info, buf, len);
1310 return len;
1313 static int esp_host_info(struct esp *esp, char *ptr, off_t offset, int len)
1315 struct scsi_device *sdev;
1316 struct info_str info;
1317 int i;
1319 info.buffer = ptr;
1320 info.length = len;
1321 info.offset = offset;
1322 info.pos = 0;
1324 copy_info(&info, "Sparc ESP Host Adapter:\n");
1325 copy_info(&info, "\tPROM node\t\t%08x\n", (unsigned int) esp->prom_node);
1326 copy_info(&info, "\tPROM name\t\t%s\n", esp->prom_name);
1327 copy_info(&info, "\tESP Model\t\t");
1328 switch (esp->erev) {
1329 case esp100:
1330 copy_info(&info, "ESP100\n");
1331 break;
1332 case esp100a:
1333 copy_info(&info, "ESP100A\n");
1334 break;
1335 case esp236:
1336 copy_info(&info, "ESP236\n");
1337 break;
1338 case fas236:
1339 copy_info(&info, "FAS236\n");
1340 break;
1341 case fas100a:
1342 copy_info(&info, "FAS100A\n");
1343 break;
1344 case fast:
1345 copy_info(&info, "FAST\n");
1346 break;
1347 case fashme:
1348 copy_info(&info, "Happy Meal FAS\n");
1349 break;
1350 case espunknown:
1351 default:
1352 copy_info(&info, "Unknown!\n");
1353 break;
1355 copy_info(&info, "\tDMA Revision\t\t");
1356 switch (esp->dma->revision) {
1357 case dvmarev0:
1358 copy_info(&info, "Rev 0\n");
1359 break;
1360 case dvmaesc1:
1361 copy_info(&info, "ESC Rev 1\n");
1362 break;
1363 case dvmarev1:
1364 copy_info(&info, "Rev 1\n");
1365 break;
1366 case dvmarev2:
1367 copy_info(&info, "Rev 2\n");
1368 break;
1369 case dvmarev3:
1370 copy_info(&info, "Rev 3\n");
1371 break;
1372 case dvmarevplus:
1373 copy_info(&info, "Rev 1+\n");
1374 break;
1375 case dvmahme:
1376 copy_info(&info, "Rev HME/FAS\n");
1377 break;
1378 default:
1379 copy_info(&info, "Unknown!\n");
1380 break;
1382 copy_info(&info, "\tLive Targets\t\t[ ");
1383 for (i = 0; i < 15; i++) {
1384 if (esp->targets_present & (1 << i))
1385 copy_info(&info, "%d ", i);
1387 copy_info(&info, "]\n\n");
1389 /* Now describe the state of each existing target. */
1390 copy_info(&info, "Target #\tconfig3\t\tSync Capabilities\tDisconnect\tWide\n");
1392 shost_for_each_device(sdev, esp->ehost) {
1393 struct esp_device *esp_dev = sdev->hostdata;
1394 uint id = sdev->id;
1396 if (!(esp->targets_present & (1 << id)))
1397 continue;
1399 copy_info(&info, "%d\t\t", id);
1400 copy_info(&info, "%08lx\t", esp->config3[id]);
1401 copy_info(&info, "[%02lx,%02lx]\t\t\t",
1402 esp_dev->sync_max_offset,
1403 esp_dev->sync_min_period);
1404 copy_info(&info, "%s\t\t",
1405 esp_dev->disconnect ? "yes" : "no");
1406 copy_info(&info, "%s\n",
1407 (esp->config3[id] & ESP_CONFIG3_EWIDE) ? "yes" : "no");
1409 return info.pos > info.offset? info.pos - info.offset : 0;
1412 /* ESP proc filesystem code. */
1413 static int esp_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
1414 int length, int inout)
1416 struct esp *esp;
1418 if (inout)
1419 return -EINVAL; /* not yet */
1421 for_each_esp(esp) {
1422 if (esp->ehost == host)
1423 break;
1425 if (!esp)
1426 return -EINVAL;
1428 if (start)
1429 *start = buffer;
1431 return esp_host_info(esp, buffer, offset, length);
1434 static void esp_get_dmabufs(struct esp *esp, struct scsi_cmnd *sp)
1436 if (sp->use_sg == 0) {
1437 sp->SCp.this_residual = sp->request_bufflen;
1438 sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
1439 sp->SCp.buffers_residual = 0;
1440 if (sp->request_bufflen) {
1441 sp->SCp.have_data_in = sbus_map_single(esp->sdev, sp->SCp.buffer,
1442 sp->SCp.this_residual,
1443 sp->sc_data_direction);
1444 sp->SCp.ptr = (char *) ((unsigned long)sp->SCp.have_data_in);
1445 } else {
1446 sp->SCp.ptr = NULL;
1448 } else {
1449 sp->SCp.buffer = (struct scatterlist *) sp->buffer;
1450 sp->SCp.buffers_residual = sbus_map_sg(esp->sdev,
1451 sp->SCp.buffer,
1452 sp->use_sg,
1453 sp->sc_data_direction);
1454 sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
1455 sp->SCp.ptr = (char *) ((unsigned long)sg_dma_address(sp->SCp.buffer));
1459 static void esp_release_dmabufs(struct esp *esp, struct scsi_cmnd *sp)
1461 if (sp->use_sg) {
1462 sbus_unmap_sg(esp->sdev, sp->buffer, sp->use_sg,
1463 sp->sc_data_direction);
1464 } else if (sp->request_bufflen) {
1465 sbus_unmap_single(esp->sdev,
1466 sp->SCp.have_data_in,
1467 sp->request_bufflen,
1468 sp->sc_data_direction);
1472 static void esp_restore_pointers(struct esp *esp, struct scsi_cmnd *sp)
1474 struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
1476 sp->SCp.ptr = ep->saved_ptr;
1477 sp->SCp.buffer = ep->saved_buffer;
1478 sp->SCp.this_residual = ep->saved_this_residual;
1479 sp->SCp.buffers_residual = ep->saved_buffers_residual;
1482 static void esp_save_pointers(struct esp *esp, struct scsi_cmnd *sp)
1484 struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
1486 ep->saved_ptr = sp->SCp.ptr;
1487 ep->saved_buffer = sp->SCp.buffer;
1488 ep->saved_this_residual = sp->SCp.this_residual;
1489 ep->saved_buffers_residual = sp->SCp.buffers_residual;
1492 /* Some rules:
1494 * 1) Never ever panic while something is live on the bus.
1495 * If there is to be any chance of syncing the disks this
1496 * rule is to be obeyed.
1498 * 2) Any target that causes a foul condition will no longer
1499 * have synchronous transfers done to it, no questions
1500 * asked.
1502 * 3) Keep register accesses to a minimum. Think about some
1503 * day when we have Xbus machines this is running on and
1504 * the ESP chip is on the other end of the machine on a
1505 * different board from the cpu where this is running.
1508 /* Fire off a command. We assume the bus is free and that the only
1509 * case where we could see an interrupt is where we have disconnected
1510 * commands active and they are trying to reselect us.
1512 static inline void esp_check_cmd(struct esp *esp, struct scsi_cmnd *sp)
1514 switch (sp->cmd_len) {
1515 case 6:
1516 case 10:
1517 case 12:
1518 esp->esp_slowcmd = 0;
1519 break;
1521 default:
1522 esp->esp_slowcmd = 1;
1523 esp->esp_scmdleft = sp->cmd_len;
1524 esp->esp_scmdp = &sp->cmnd[0];
1525 break;
1529 static inline void build_sync_nego_msg(struct esp *esp, int period, int offset)
1531 esp->cur_msgout[0] = EXTENDED_MESSAGE;
1532 esp->cur_msgout[1] = 3;
1533 esp->cur_msgout[2] = EXTENDED_SDTR;
1534 esp->cur_msgout[3] = period;
1535 esp->cur_msgout[4] = offset;
1536 esp->msgout_len = 5;
1539 /* SIZE is in bits, currently HME only supports 16 bit wide transfers. */
1540 static inline void build_wide_nego_msg(struct esp *esp, int size)
1542 esp->cur_msgout[0] = EXTENDED_MESSAGE;
1543 esp->cur_msgout[1] = 2;
1544 esp->cur_msgout[2] = EXTENDED_WDTR;
1545 switch (size) {
1546 case 32:
1547 esp->cur_msgout[3] = 2;
1548 break;
1549 case 16:
1550 esp->cur_msgout[3] = 1;
1551 break;
1552 case 8:
1553 default:
1554 esp->cur_msgout[3] = 0;
1555 break;
1558 esp->msgout_len = 4;
1561 static void esp_exec_cmd(struct esp *esp)
1563 struct scsi_cmnd *SCptr;
1564 struct scsi_device *SDptr;
1565 struct esp_device *esp_dev;
1566 volatile u8 *cmdp = esp->esp_command;
1567 u8 the_esp_command;
1568 int lun, target;
1569 int i;
1571 /* Hold off if we have disconnected commands and
1572 * an IRQ is showing...
1574 if (esp->disconnected_SC && ESP_IRQ_P(esp->dregs))
1575 return;
1577 /* Grab first member of the issue queue. */
1578 SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
1580 /* Safe to panic here because current_SC is null. */
1581 if (!SCptr)
1582 panic("esp: esp_exec_cmd and issue queue is NULL");
1584 SDptr = SCptr->device;
1585 esp_dev = SDptr->hostdata;
1586 lun = SCptr->device->lun;
1587 target = SCptr->device->id;
1589 esp->snip = 0;
1590 esp->msgout_len = 0;
1592 /* Send it out whole, or piece by piece? The ESP
1593 * only knows how to automatically send out 6, 10,
1594 * and 12 byte commands. I used to think that the
1595 * Linux SCSI code would never throw anything other
1596 * than that to us, but then again there is the
1597 * SCSI generic driver which can send us anything.
1599 esp_check_cmd(esp, SCptr);
1601 /* If arbitration/selection is successful, the ESP will leave
1602 * ATN asserted, causing the target to go into message out
1603 * phase. The ESP will feed the target the identify and then
1604 * the target can only legally go to one of command,
1605 * datain/out, status, or message in phase, or stay in message
1606 * out phase (should we be trying to send a sync negotiation
1607 * message after the identify). It is not allowed to drop
1608 * BSY, but some buggy targets do and we check for this
1609 * condition in the selection complete code. Most of the time
1610 * we'll make the command bytes available to the ESP and it
1611 * will not interrupt us until it finishes command phase, we
1612 * cannot do this for command sizes the ESP does not
1613 * understand and in this case we'll get interrupted right
1614 * when the target goes into command phase.
1616 * It is absolutely _illegal_ in the presence of SCSI-2 devices
1617 * to use the ESP select w/o ATN command. When SCSI-2 devices are
1618 * present on the bus we _must_ always go straight to message out
1619 * phase with an identify message for the target. Being that
1620 * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
1621 * selections should not confuse SCSI-1 we hope.
1624 if (esp_dev->sync) {
1625 /* this targets sync is known */
1626 #ifndef __sparc_v9__
1627 do_sync_known:
1628 #endif
1629 if (esp_dev->disconnect)
1630 *cmdp++ = IDENTIFY(1, lun);
1631 else
1632 *cmdp++ = IDENTIFY(0, lun);
1634 if (esp->esp_slowcmd) {
1635 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1636 esp_advance_phase(SCptr, in_slct_stop);
1637 } else {
1638 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1639 esp_advance_phase(SCptr, in_slct_norm);
1641 } else if (!(esp->targets_present & (1<<target)) || !(esp_dev->disconnect)) {
1642 /* After the bootup SCSI code sends both the
1643 * TEST_UNIT_READY and INQUIRY commands we want
1644 * to at least attempt allowing the device to
1645 * disconnect.
1647 ESPMISC(("esp: Selecting device for first time. target=%d "
1648 "lun=%d\n", target, SCptr->device->lun));
1649 if (!SDptr->borken && !esp_dev->disconnect)
1650 esp_dev->disconnect = 1;
1652 *cmdp++ = IDENTIFY(0, lun);
1653 esp->prevmsgout = NOP;
1654 esp_advance_phase(SCptr, in_slct_norm);
1655 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1657 /* Take no chances... */
1658 esp_dev->sync_max_offset = 0;
1659 esp_dev->sync_min_period = 0;
1660 } else {
1661 /* Sorry, I have had way too many problems with
1662 * various CDROM devices on ESP. -DaveM
1664 int cdrom_hwbug_wkaround = 0;
1666 #ifndef __sparc_v9__
1667 /* Never allow disconnects or synchronous transfers on
1668 * SparcStation1 and SparcStation1+. Allowing those
1669 * to be enabled seems to lockup the machine completely.
1671 if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
1672 (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
1673 /* But we are nice and allow tapes and removable
1674 * disks (but not CDROMs) to disconnect.
1676 if(SDptr->type == TYPE_TAPE ||
1677 (SDptr->type != TYPE_ROM && SDptr->removable))
1678 esp_dev->disconnect = 1;
1679 else
1680 esp_dev->disconnect = 0;
1681 esp_dev->sync_max_offset = 0;
1682 esp_dev->sync_min_period = 0;
1683 esp_dev->sync = 1;
1684 esp->snip = 0;
1685 goto do_sync_known;
1687 #endif /* !(__sparc_v9__) */
1689 /* We've talked to this guy before,
1690 * but never negotiated. Let's try,
1691 * need to attempt WIDE first, before
1692 * sync nego, as per SCSI 2 standard.
1694 if (esp->erev == fashme && !esp_dev->wide) {
1695 if (!SDptr->borken &&
1696 SDptr->type != TYPE_ROM &&
1697 SDptr->removable == 0) {
1698 build_wide_nego_msg(esp, 16);
1699 esp_dev->wide = 1;
1700 esp->wnip = 1;
1701 goto after_nego_msg_built;
1702 } else {
1703 esp_dev->wide = 1;
1704 /* Fall through and try sync. */
1708 if (!SDptr->borken) {
1709 if ((SDptr->type == TYPE_ROM)) {
1710 /* Nice try sucker... */
1711 ESPMISC(("esp%d: Disabling sync for buggy "
1712 "CDROM.\n", esp->esp_id));
1713 cdrom_hwbug_wkaround = 1;
1714 build_sync_nego_msg(esp, 0, 0);
1715 } else if (SDptr->removable != 0) {
1716 ESPMISC(("esp%d: Not negotiating sync/wide but "
1717 "allowing disconnect for removable media.\n",
1718 esp->esp_id));
1719 build_sync_nego_msg(esp, 0, 0);
1720 } else {
1721 build_sync_nego_msg(esp, esp->sync_defp, 15);
1723 } else {
1724 build_sync_nego_msg(esp, 0, 0);
1726 esp_dev->sync = 1;
1727 esp->snip = 1;
1729 after_nego_msg_built:
1730 /* A fix for broken SCSI1 targets, when they disconnect
1731 * they lock up the bus and confuse ESP. So disallow
1732 * disconnects for SCSI1 targets for now until we
1733 * find a better fix.
1735 * Addendum: This is funny, I figured out what was going
1736 * on. The blotzed SCSI1 target would disconnect,
1737 * one of the other SCSI2 targets or both would be
1738 * disconnected as well. The SCSI1 target would
1739 * stay disconnected long enough that we start
1740 * up a command on one of the SCSI2 targets. As
1741 * the ESP is arbitrating for the bus the SCSI1
1742 * target begins to arbitrate as well to reselect
1743 * the ESP. The SCSI1 target refuses to drop it's
1744 * ID bit on the data bus even though the ESP is
1745 * at ID 7 and is the obvious winner for any
1746 * arbitration. The ESP is a poor sport and refuses
1747 * to lose arbitration, it will continue indefinitely
1748 * trying to arbitrate for the bus and can only be
1749 * stopped via a chip reset or SCSI bus reset.
1750 * Therefore _no_ disconnects for SCSI1 targets
1751 * thank you very much. ;-)
1753 if(((SDptr->scsi_level < 3) &&
1754 (SDptr->type != TYPE_TAPE) &&
1755 SDptr->removable == 0) ||
1756 cdrom_hwbug_wkaround || SDptr->borken) {
1757 ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
1758 "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
1759 esp_dev->disconnect = 0;
1760 *cmdp++ = IDENTIFY(0, lun);
1761 } else {
1762 *cmdp++ = IDENTIFY(1, lun);
1765 /* ESP fifo is only so big...
1766 * Make this look like a slow command.
1768 esp->esp_slowcmd = 1;
1769 esp->esp_scmdleft = SCptr->cmd_len;
1770 esp->esp_scmdp = &SCptr->cmnd[0];
1772 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1773 esp_advance_phase(SCptr, in_slct_msg);
1776 if (!esp->esp_slowcmd)
1777 for (i = 0; i < SCptr->cmd_len; i++)
1778 *cmdp++ = SCptr->cmnd[i];
1780 /* HME sucks... */
1781 if (esp->erev == fashme)
1782 sbus_writeb((target & 0xf) | (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT),
1783 esp->eregs + ESP_BUSID);
1784 else
1785 sbus_writeb(target & 7, esp->eregs + ESP_BUSID);
1786 if (esp->prev_soff != esp_dev->sync_max_offset ||
1787 esp->prev_stp != esp_dev->sync_min_period ||
1788 (esp->erev > esp100a &&
1789 esp->prev_cfg3 != esp->config3[target])) {
1790 esp->prev_soff = esp_dev->sync_max_offset;
1791 esp->prev_stp = esp_dev->sync_min_period;
1792 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
1793 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
1794 if (esp->erev > esp100a) {
1795 esp->prev_cfg3 = esp->config3[target];
1796 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
1799 i = (cmdp - esp->esp_command);
1801 if (esp->erev == fashme) {
1802 esp_cmd(esp, ESP_CMD_FLUSH); /* Grrr! */
1804 /* Set up the DMA and HME counters */
1805 sbus_writeb(i, esp->eregs + ESP_TCLOW);
1806 sbus_writeb(0, esp->eregs + ESP_TCMED);
1807 sbus_writeb(0, esp->eregs + FAS_RLO);
1808 sbus_writeb(0, esp->eregs + FAS_RHI);
1809 esp_cmd(esp, the_esp_command);
1811 /* Talk about touchy hardware... */
1812 esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
1813 (DMA_SCSI_DISAB | DMA_ENABLE)) &
1814 ~(DMA_ST_WRITE));
1815 sbus_writel(16, esp->dregs + DMA_COUNT);
1816 sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
1817 sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
1818 } else {
1819 u32 tmp;
1821 /* Set up the DMA and ESP counters */
1822 sbus_writeb(i, esp->eregs + ESP_TCLOW);
1823 sbus_writeb(0, esp->eregs + ESP_TCMED);
1824 tmp = sbus_readl(esp->dregs + DMA_CSR);
1825 tmp &= ~DMA_ST_WRITE;
1826 tmp |= DMA_ENABLE;
1827 sbus_writel(tmp, esp->dregs + DMA_CSR);
1828 if (esp->dma->revision == dvmaesc1) {
1829 if (i) /* Workaround ESC gate array SBUS rerun bug. */
1830 sbus_writel(PAGE_SIZE, esp->dregs + DMA_COUNT);
1832 sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
1834 /* Tell ESP to "go". */
1835 esp_cmd(esp, the_esp_command);
1839 /* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
1840 static int esp_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1842 struct esp *esp;
1844 /* Set up func ptr and initial driver cmd-phase. */
1845 SCpnt->scsi_done = done;
1846 SCpnt->SCp.phase = not_issued;
1848 /* We use the scratch area. */
1849 ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->device->id, SCpnt->device->lun));
1850 ESPDISC(("N<%02x,%02x>", SCpnt->device->id, SCpnt->device->lun));
1852 esp = (struct esp *) SCpnt->device->host->hostdata;
1853 esp_get_dmabufs(esp, SCpnt);
1854 esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
1856 SCpnt->SCp.Status = CHECK_CONDITION;
1857 SCpnt->SCp.Message = 0xff;
1858 SCpnt->SCp.sent_command = 0;
1860 /* Place into our queue. */
1861 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
1862 ESPQUEUE(("RQSENSE\n"));
1863 prepend_SC(&esp->issue_SC, SCpnt);
1864 } else {
1865 ESPQUEUE(("\n"));
1866 append_SC(&esp->issue_SC, SCpnt);
1869 /* Run it now if we can. */
1870 if (!esp->current_SC && !esp->resetting_bus)
1871 esp_exec_cmd(esp);
1873 return 0;
1876 /* Dump driver state. */
1877 static void esp_dump_cmd(struct scsi_cmnd *SCptr)
1879 ESPLOG(("[tgt<%02x> lun<%02x> "
1880 "pphase<%s> cphase<%s>]",
1881 SCptr->device->id, SCptr->device->lun,
1882 phase_string(SCptr->SCp.sent_command),
1883 phase_string(SCptr->SCp.phase)));
1886 static void esp_dump_state(struct esp *esp)
1888 struct scsi_cmnd *SCptr = esp->current_SC;
1889 #ifdef DEBUG_ESP_CMDS
1890 int i;
1891 #endif
1893 ESPLOG(("esp%d: dumping state\n", esp->esp_id));
1894 ESPLOG(("esp%d: dma -- cond_reg<%08x> addr<%08x>\n",
1895 esp->esp_id,
1896 sbus_readl(esp->dregs + DMA_CSR),
1897 sbus_readl(esp->dregs + DMA_ADDR)));
1898 ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1899 esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
1900 ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1901 esp->esp_id,
1902 sbus_readb(esp->eregs + ESP_STATUS),
1903 sbus_readb(esp->eregs + ESP_SSTEP),
1904 sbus_readb(esp->eregs + ESP_INTRPT)));
1905 #ifdef DEBUG_ESP_CMDS
1906 printk("esp%d: last ESP cmds [", esp->esp_id);
1907 i = (esp->espcmdent - 1) & 31;
1908 printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
1909 i = (i - 1) & 31;
1910 printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
1911 i = (i - 1) & 31;
1912 printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
1913 i = (i - 1) & 31;
1914 printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
1915 printk("]\n");
1916 #endif /* (DEBUG_ESP_CMDS) */
1918 if (SCptr) {
1919 ESPLOG(("esp%d: current command ", esp->esp_id));
1920 esp_dump_cmd(SCptr);
1922 ESPLOG(("\n"));
1923 SCptr = esp->disconnected_SC;
1924 ESPLOG(("esp%d: disconnected ", esp->esp_id));
1925 while (SCptr) {
1926 esp_dump_cmd(SCptr);
1927 SCptr = (struct scsi_cmnd *) SCptr->host_scribble;
1929 ESPLOG(("\n"));
1932 /* Abort a command. The host_lock is acquired by caller. */
1933 static int esp_abort(struct scsi_cmnd *SCptr)
1935 struct esp *esp = (struct esp *) SCptr->device->host->hostdata;
1936 int don;
1938 ESPLOG(("esp%d: Aborting command\n", esp->esp_id));
1939 esp_dump_state(esp);
1941 /* Wheee, if this is the current command on the bus, the
1942 * best we can do is assert ATN and wait for msgout phase.
1943 * This should even fix a hung SCSI bus when we lose state
1944 * in the driver and timeout because the eventual phase change
1945 * will cause the ESP to (eventually) give an interrupt.
1947 if (esp->current_SC == SCptr) {
1948 esp->cur_msgout[0] = ABORT;
1949 esp->msgout_len = 1;
1950 esp->msgout_ctr = 0;
1951 esp_cmd(esp, ESP_CMD_SATN);
1952 return SUCCESS;
1955 /* If it is still in the issue queue then we can safely
1956 * call the completion routine and report abort success.
1958 don = (sbus_readl(esp->dregs + DMA_CSR) & DMA_INT_ENAB);
1959 if (don) {
1960 ESP_INTSOFF(esp->dregs);
1962 if (esp->issue_SC) {
1963 struct scsi_cmnd **prev, *this;
1964 for (prev = (&esp->issue_SC), this = esp->issue_SC;
1965 this != NULL;
1966 prev = (struct scsi_cmnd **) &(this->host_scribble),
1967 this = (struct scsi_cmnd *) this->host_scribble) {
1969 if (this == SCptr) {
1970 *prev = (struct scsi_cmnd *) this->host_scribble;
1971 this->host_scribble = NULL;
1973 esp_release_dmabufs(esp, this);
1974 this->result = DID_ABORT << 16;
1975 this->scsi_done(this);
1977 if (don)
1978 ESP_INTSON(esp->dregs);
1980 return SUCCESS;
1985 /* Yuck, the command to abort is disconnected, it is not
1986 * worth trying to abort it now if something else is live
1987 * on the bus at this time. So, we let the SCSI code wait
1988 * a little bit and try again later.
1990 if (esp->current_SC) {
1991 if (don)
1992 ESP_INTSON(esp->dregs);
1993 return FAILED;
1996 /* It's disconnected, we have to reconnect to re-establish
1997 * the nexus and tell the device to abort. However, we really
1998 * cannot 'reconnect' per se. Don't try to be fancy, just
1999 * indicate failure, which causes our caller to reset the whole
2000 * bus.
2003 if (don)
2004 ESP_INTSON(esp->dregs);
2006 return FAILED;
2009 /* We've sent ESP_CMD_RS to the ESP, the interrupt had just
2010 * arrived indicating the end of the SCSI bus reset. Our job
2011 * is to clean out the command queues and begin re-execution
2012 * of SCSI commands once more.
2014 static int esp_finish_reset(struct esp *esp)
2016 struct scsi_cmnd *sp = esp->current_SC;
2018 /* Clean up currently executing command, if any. */
2019 if (sp != NULL) {
2020 esp->current_SC = NULL;
2022 esp_release_dmabufs(esp, sp);
2023 sp->result = (DID_RESET << 16);
2025 sp->scsi_done(sp);
2028 /* Clean up disconnected queue, they have been invalidated
2029 * by the bus reset.
2031 if (esp->disconnected_SC) {
2032 while ((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
2033 esp_release_dmabufs(esp, sp);
2034 sp->result = (DID_RESET << 16);
2036 sp->scsi_done(sp);
2040 /* SCSI bus reset is complete. */
2041 esp->resetting_bus = 0;
2042 wake_up(&esp->reset_queue);
2044 /* Ok, now it is safe to get commands going once more. */
2045 if (esp->issue_SC)
2046 esp_exec_cmd(esp);
2048 return do_intr_end;
2051 static int esp_do_resetbus(struct esp *esp)
2053 ESPLOG(("esp%d: Resetting scsi bus\n", esp->esp_id));
2054 esp->resetting_bus = 1;
2055 esp_cmd(esp, ESP_CMD_RS);
2057 return do_intr_end;
2060 /* Reset ESP chip, reset hanging bus, then kill active and
2061 * disconnected commands for targets without soft reset.
2063 * The host_lock is acquired by caller.
2065 static int esp_reset(struct scsi_cmnd *SCptr)
2067 struct esp *esp = (struct esp *) SCptr->device->host->hostdata;
2069 (void) esp_do_resetbus(esp);
2071 spin_unlock_irq(esp->ehost->host_lock);
2073 wait_event(esp->reset_queue, (esp->resetting_bus == 0));
2075 spin_lock_irq(esp->ehost->host_lock);
2077 return SUCCESS;
2080 /* Internal ESP done function. */
2081 static void esp_done(struct esp *esp, int error)
2083 struct scsi_cmnd *done_SC = esp->current_SC;
2085 esp->current_SC = NULL;
2087 esp_release_dmabufs(esp, done_SC);
2088 done_SC->result = error;
2090 done_SC->scsi_done(done_SC);
2092 /* Bus is free, issue any commands in the queue. */
2093 if (esp->issue_SC && !esp->current_SC)
2094 esp_exec_cmd(esp);
2098 /* Wheee, ESP interrupt engine. */
2100 /* Forward declarations. */
2101 static int esp_do_phase_determine(struct esp *esp);
2102 static int esp_do_data_finale(struct esp *esp);
2103 static int esp_select_complete(struct esp *esp);
2104 static int esp_do_status(struct esp *esp);
2105 static int esp_do_msgin(struct esp *esp);
2106 static int esp_do_msgindone(struct esp *esp);
2107 static int esp_do_msgout(struct esp *esp);
2108 static int esp_do_cmdbegin(struct esp *esp);
2110 #define sreg_datainp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
2111 #define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
2113 /* Read any bytes found in the FAS366 fifo, storing them into
2114 * the ESP driver software state structure.
2116 static void hme_fifo_read(struct esp *esp)
2118 u8 count = 0;
2119 u8 status = esp->sreg;
2121 /* Cannot safely frob the fifo for these following cases, but
2122 * we must always read the fifo when the reselect interrupt
2123 * is pending.
2125 if (((esp->ireg & ESP_INTR_RSEL) == 0) &&
2126 (sreg_datainp(status) ||
2127 sreg_dataoutp(status) ||
2128 (esp->current_SC &&
2129 esp->current_SC->SCp.phase == in_data_done))) {
2130 ESPHME(("<wkaround_skipped>"));
2131 } else {
2132 unsigned long fcnt = sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES;
2134 /* The HME stores bytes in multiples of 2 in the fifo. */
2135 ESPHME(("hme_fifo[fcnt=%d", (int)fcnt));
2136 while (fcnt) {
2137 esp->hme_fifo_workaround_buffer[count++] =
2138 sbus_readb(esp->eregs + ESP_FDATA);
2139 esp->hme_fifo_workaround_buffer[count++] =
2140 sbus_readb(esp->eregs + ESP_FDATA);
2141 ESPHME(("<%02x,%02x>", esp->hme_fifo_workaround_buffer[count-2], esp->hme_fifo_workaround_buffer[count-1]));
2142 fcnt--;
2144 if (sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_F1BYTE) {
2145 ESPHME(("<poke_byte>"));
2146 sbus_writeb(0, esp->eregs + ESP_FDATA);
2147 esp->hme_fifo_workaround_buffer[count++] =
2148 sbus_readb(esp->eregs + ESP_FDATA);
2149 ESPHME(("<%02x,0x00>", esp->hme_fifo_workaround_buffer[count-1]));
2150 ESPHME(("CMD_FLUSH"));
2151 esp_cmd(esp, ESP_CMD_FLUSH);
2152 } else {
2153 ESPHME(("no_xtra_byte"));
2156 ESPHME(("wkarnd_cnt=%d]", (int)count));
2157 esp->hme_fifo_workaround_count = count;
2160 static inline void hme_fifo_push(struct esp *esp, u8 *bytes, u8 count)
2162 esp_cmd(esp, ESP_CMD_FLUSH);
2163 while (count) {
2164 u8 tmp = *bytes++;
2165 sbus_writeb(tmp, esp->eregs + ESP_FDATA);
2166 sbus_writeb(0, esp->eregs + ESP_FDATA);
2167 count--;
2171 /* We try to avoid some interrupts by jumping ahead and see if the ESP
2172 * has gotten far enough yet. Hence the following.
2174 static inline int skipahead1(struct esp *esp, struct scsi_cmnd *scp,
2175 int prev_phase, int new_phase)
2177 if (scp->SCp.sent_command != prev_phase)
2178 return 0;
2179 if (ESP_IRQ_P(esp->dregs)) {
2180 /* Yes, we are able to save an interrupt. */
2181 if (esp->erev == fashme)
2182 esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
2183 esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
2184 esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
2185 if (esp->erev == fashme) {
2186 /* This chip is really losing. */
2187 ESPHME(("HME["));
2188 /* Must latch fifo before reading the interrupt
2189 * register else garbage ends up in the FIFO
2190 * which confuses the driver utterly.
2191 * Happy Meal indeed....
2193 ESPHME(("fifo_workaround]"));
2194 if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
2195 (esp->sreg2 & ESP_STAT2_F1BYTE))
2196 hme_fifo_read(esp);
2198 if (!(esp->ireg & ESP_INTR_SR))
2199 return 0;
2200 else
2201 return do_reset_complete;
2203 /* Ho hum, target is taking forever... */
2204 scp->SCp.sent_command = new_phase; /* so we don't recurse... */
2205 return do_intr_end;
2208 static inline int skipahead2(struct esp *esp, struct scsi_cmnd *scp,
2209 int prev_phase1, int prev_phase2, int new_phase)
2211 if (scp->SCp.sent_command != prev_phase1 &&
2212 scp->SCp.sent_command != prev_phase2)
2213 return 0;
2214 if (ESP_IRQ_P(esp->dregs)) {
2215 /* Yes, we are able to save an interrupt. */
2216 if (esp->erev == fashme)
2217 esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
2218 esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
2219 esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
2220 if (esp->erev == fashme) {
2221 /* This chip is really losing. */
2222 ESPHME(("HME["));
2224 /* Must latch fifo before reading the interrupt
2225 * register else garbage ends up in the FIFO
2226 * which confuses the driver utterly.
2227 * Happy Meal indeed....
2229 ESPHME(("fifo_workaround]"));
2230 if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
2231 (esp->sreg2 & ESP_STAT2_F1BYTE))
2232 hme_fifo_read(esp);
2234 if (!(esp->ireg & ESP_INTR_SR))
2235 return 0;
2236 else
2237 return do_reset_complete;
2239 /* Ho hum, target is taking forever... */
2240 scp->SCp.sent_command = new_phase; /* so we don't recurse... */
2241 return do_intr_end;
2244 /* Now some dma helpers. */
2245 static void dma_setup(struct esp *esp, __u32 addr, int count, int write)
2247 u32 nreg = sbus_readl(esp->dregs + DMA_CSR);
2249 if (write)
2250 nreg |= DMA_ST_WRITE;
2251 else
2252 nreg &= ~(DMA_ST_WRITE);
2253 nreg |= DMA_ENABLE;
2254 sbus_writel(nreg, esp->dregs + DMA_CSR);
2255 if (esp->dma->revision == dvmaesc1) {
2256 /* This ESC gate array sucks! */
2257 __u32 src = addr;
2258 __u32 dest = src + count;
2260 if (dest & (PAGE_SIZE - 1))
2261 count = PAGE_ALIGN(count);
2262 sbus_writel(count, esp->dregs + DMA_COUNT);
2264 sbus_writel(addr, esp->dregs + DMA_ADDR);
2267 static void dma_drain(struct esp *esp)
2269 u32 tmp;
2271 if (esp->dma->revision == dvmahme)
2272 return;
2273 if ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_FIFO_ISDRAIN) {
2274 switch (esp->dma->revision) {
2275 default:
2276 tmp |= DMA_FIFO_STDRAIN;
2277 sbus_writel(tmp, esp->dregs + DMA_CSR);
2279 case dvmarev3:
2280 case dvmaesc1:
2281 while (sbus_readl(esp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
2282 udelay(1);
2287 static void dma_invalidate(struct esp *esp)
2289 u32 tmp;
2291 if (esp->dma->revision == dvmahme) {
2292 sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
2294 esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
2295 (DMA_PARITY_OFF | DMA_2CLKS |
2296 DMA_SCSI_DISAB | DMA_INT_ENAB)) &
2297 ~(DMA_ST_WRITE | DMA_ENABLE));
2299 sbus_writel(0, esp->dregs + DMA_CSR);
2300 sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
2302 /* This is necessary to avoid having the SCSI channel
2303 * engine lock up on us.
2305 sbus_writel(0, esp->dregs + DMA_ADDR);
2306 } else {
2307 while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
2308 udelay(1);
2310 tmp &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
2311 tmp |= DMA_FIFO_INV;
2312 sbus_writel(tmp, esp->dregs + DMA_CSR);
2313 tmp &= ~DMA_FIFO_INV;
2314 sbus_writel(tmp, esp->dregs + DMA_CSR);
2318 static inline void dma_flashclear(struct esp *esp)
2320 dma_drain(esp);
2321 dma_invalidate(esp);
2324 static int dma_can_transfer(struct esp *esp, struct scsi_cmnd *sp)
2326 __u32 base, end, sz;
2328 if (esp->dma->revision == dvmarev3) {
2329 sz = sp->SCp.this_residual;
2330 if (sz > 0x1000000)
2331 sz = 0x1000000;
2332 } else {
2333 base = ((__u32)((unsigned long)sp->SCp.ptr));
2334 base &= (0x1000000 - 1);
2335 end = (base + sp->SCp.this_residual);
2336 if (end > 0x1000000)
2337 end = 0x1000000;
2338 sz = (end - base);
2340 return sz;
2343 /* Misc. esp helper macros. */
2344 #define esp_setcount(__eregs, __cnt, __hme) \
2345 sbus_writeb(((__cnt)&0xff), (__eregs) + ESP_TCLOW); \
2346 sbus_writeb((((__cnt)>>8)&0xff), (__eregs) + ESP_TCMED); \
2347 if (__hme) { \
2348 sbus_writeb((((__cnt)>>16)&0xff), (__eregs) + FAS_RLO); \
2349 sbus_writeb(0, (__eregs) + FAS_RHI); \
2352 #define esp_getcount(__eregs, __hme) \
2353 ((sbus_readb((__eregs) + ESP_TCLOW)&0xff) | \
2354 ((sbus_readb((__eregs) + ESP_TCMED)&0xff) << 8) | \
2355 ((__hme) ? sbus_readb((__eregs) + FAS_RLO) << 16 : 0))
2357 #define fcount(__esp) \
2358 (((__esp)->erev == fashme) ? \
2359 (__esp)->hme_fifo_workaround_count : \
2360 sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_FBYTES)
2362 #define fnzero(__esp) \
2363 (((__esp)->erev == fashme) ? 0 : \
2364 sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_ONOTZERO)
2366 /* XXX speculative nops unnecessary when continuing amidst a data phase
2367 * XXX even on esp100!!! another case of flooding the bus with I/O reg
2368 * XXX writes...
2370 #define esp_maybe_nop(__esp) \
2371 if ((__esp)->erev == esp100) \
2372 esp_cmd((__esp), ESP_CMD_NULL)
2374 #define sreg_to_dataphase(__sreg) \
2375 ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
2377 /* The ESP100 when in synchronous data phase, can mistake a long final
2378 * REQ pulse from the target as an extra byte, it places whatever is on
2379 * the data lines into the fifo. For now, we will assume when this
2380 * happens that the target is a bit quirky and we don't want to
2381 * be talking synchronously to it anyways. Regardless, we need to
2382 * tell the ESP to eat the extraneous byte so that we can proceed
2383 * to the next phase.
2385 static int esp100_sync_hwbug(struct esp *esp, struct scsi_cmnd *sp, int fifocnt)
2387 /* Do not touch this piece of code. */
2388 if ((!(esp->erev == esp100)) ||
2389 (!(sreg_datainp((esp->sreg = sbus_readb(esp->eregs + ESP_STATUS))) &&
2390 !fifocnt) &&
2391 !(sreg_dataoutp(esp->sreg) && !fnzero(esp)))) {
2392 if (sp->SCp.phase == in_dataout)
2393 esp_cmd(esp, ESP_CMD_FLUSH);
2394 return 0;
2395 } else {
2396 /* Async mode for this guy. */
2397 build_sync_nego_msg(esp, 0, 0);
2399 /* Ack the bogus byte, but set ATN first. */
2400 esp_cmd(esp, ESP_CMD_SATN);
2401 esp_cmd(esp, ESP_CMD_MOK);
2402 return 1;
2406 /* This closes the window during a selection with a reselect pending, because
2407 * we use DMA for the selection process the FIFO should hold the correct
2408 * contents if we get reselected during this process. So we just need to
2409 * ack the possible illegal cmd interrupt pending on the esp100.
2411 static inline int esp100_reconnect_hwbug(struct esp *esp)
2413 u8 tmp;
2415 if (esp->erev != esp100)
2416 return 0;
2417 tmp = sbus_readb(esp->eregs + ESP_INTRPT);
2418 if (tmp & ESP_INTR_SR)
2419 return 1;
2420 return 0;
2423 /* This verifies the BUSID bits during a reselection so that we know which
2424 * target is talking to us.
2426 static inline int reconnect_target(struct esp *esp)
2428 int it, me = esp->scsi_id_mask, targ = 0;
2430 if (2 != fcount(esp))
2431 return -1;
2432 if (esp->erev == fashme) {
2433 /* HME does not latch it's own BUS ID bits during
2434 * a reselection. Also the target number is given
2435 * as an unsigned char, not as a sole bit number
2436 * like the other ESP's do.
2437 * Happy Meal indeed....
2439 targ = esp->hme_fifo_workaround_buffer[0];
2440 } else {
2441 it = sbus_readb(esp->eregs + ESP_FDATA);
2442 if (!(it & me))
2443 return -1;
2444 it &= ~me;
2445 if (it & (it - 1))
2446 return -1;
2447 while (!(it & 1))
2448 targ++, it >>= 1;
2450 return targ;
2453 /* This verifies the identify from the target so that we know which lun is
2454 * being reconnected.
2456 static inline int reconnect_lun(struct esp *esp)
2458 int lun;
2460 if ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
2461 return -1;
2462 if (esp->erev == fashme)
2463 lun = esp->hme_fifo_workaround_buffer[1];
2464 else
2465 lun = sbus_readb(esp->eregs + ESP_FDATA);
2467 /* Yes, you read this correctly. We report lun of zero
2468 * if we see parity error. ESP reports parity error for
2469 * the lun byte, and this is the only way to hope to recover
2470 * because the target is connected.
2472 if (esp->sreg & ESP_STAT_PERR)
2473 return 0;
2475 /* Check for illegal bits being set in the lun. */
2476 if ((lun & 0x40) || !(lun & 0x80))
2477 return -1;
2479 return lun & 7;
2482 /* This puts the driver in a state where it can revitalize a command that
2483 * is being continued due to reselection.
2485 static inline void esp_connect(struct esp *esp, struct scsi_cmnd *sp)
2487 struct esp_device *esp_dev = sp->device->hostdata;
2489 if (esp->prev_soff != esp_dev->sync_max_offset ||
2490 esp->prev_stp != esp_dev->sync_min_period ||
2491 (esp->erev > esp100a &&
2492 esp->prev_cfg3 != esp->config3[sp->device->id])) {
2493 esp->prev_soff = esp_dev->sync_max_offset;
2494 esp->prev_stp = esp_dev->sync_min_period;
2495 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
2496 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
2497 if (esp->erev > esp100a) {
2498 esp->prev_cfg3 = esp->config3[sp->device->id];
2499 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
2502 esp->current_SC = sp;
2505 /* This will place the current working command back into the issue queue
2506 * if we are to receive a reselection amidst a selection attempt.
2508 static inline void esp_reconnect(struct esp *esp, struct scsi_cmnd *sp)
2510 if (!esp->disconnected_SC)
2511 ESPLOG(("esp%d: Weird, being reselected but disconnected "
2512 "command queue is empty.\n", esp->esp_id));
2513 esp->snip = 0;
2514 esp->current_SC = 0;
2515 sp->SCp.phase = not_issued;
2516 append_SC(&esp->issue_SC, sp);
2519 /* Begin message in phase. */
2520 static int esp_do_msgin(struct esp *esp)
2522 /* Must be very careful with the fifo on the HME */
2523 if ((esp->erev != fashme) ||
2524 !(sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_FEMPTY))
2525 esp_cmd(esp, ESP_CMD_FLUSH);
2526 esp_maybe_nop(esp);
2527 esp_cmd(esp, ESP_CMD_TI);
2528 esp->msgin_len = 1;
2529 esp->msgin_ctr = 0;
2530 esp_advance_phase(esp->current_SC, in_msgindone);
2531 return do_work_bus;
2534 /* This uses various DMA csr fields and the fifo flags count value to
2535 * determine how many bytes were successfully sent/received by the ESP.
2537 static inline int esp_bytes_sent(struct esp *esp, int fifo_count)
2539 int rval = sbus_readl(esp->dregs + DMA_ADDR) - esp->esp_command_dvma;
2541 if (esp->dma->revision == dvmarev1)
2542 rval -= (4 - ((sbus_readl(esp->dregs + DMA_CSR) & DMA_READ_AHEAD)>>11));
2543 return rval - fifo_count;
2546 static inline void advance_sg(struct scsi_cmnd *sp)
2548 ++sp->SCp.buffer;
2549 --sp->SCp.buffers_residual;
2550 sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
2551 sp->SCp.ptr = (char *)((unsigned long)sg_dma_address(sp->SCp.buffer));
2554 /* Please note that the way I've coded these routines is that I _always_
2555 * check for a disconnect during any and all information transfer
2556 * phases. The SCSI standard states that the target _can_ cause a BUS
2557 * FREE condition by dropping all MSG/CD/IO/BSY signals. Also note
2558 * that during information transfer phases the target controls every
2559 * change in phase, the only thing the initiator can do is "ask" for
2560 * a message out phase by driving ATN true. The target can, and sometimes
2561 * will, completely ignore this request so we cannot assume anything when
2562 * we try to force a message out phase to abort/reset a target. Most of
2563 * the time the target will eventually be nice and go to message out, so
2564 * we may have to hold on to our state about what we want to tell the target
2565 * for some period of time.
2568 /* I think I have things working here correctly. Even partial transfers
2569 * within a buffer or sub-buffer should not upset us at all no matter
2570 * how bad the target and/or ESP fucks things up.
2572 static int esp_do_data(struct esp *esp)
2574 struct scsi_cmnd *SCptr = esp->current_SC;
2575 int thisphase, hmuch;
2577 ESPDATA(("esp_do_data: "));
2578 esp_maybe_nop(esp);
2579 thisphase = sreg_to_dataphase(esp->sreg);
2580 esp_advance_phase(SCptr, thisphase);
2581 ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
2582 hmuch = dma_can_transfer(esp, SCptr);
2583 if (hmuch > (64 * 1024) && (esp->erev != fashme))
2584 hmuch = (64 * 1024);
2585 ESPDATA(("hmuch<%d> ", hmuch));
2586 esp->current_transfer_size = hmuch;
2588 if (esp->erev == fashme) {
2589 u32 tmp = esp->prev_hme_dmacsr;
2591 /* Always set the ESP count registers first. */
2592 esp_setcount(esp->eregs, hmuch, 1);
2594 /* Get the DMA csr computed. */
2595 tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
2596 if (thisphase == in_datain)
2597 tmp |= DMA_ST_WRITE;
2598 else
2599 tmp &= ~(DMA_ST_WRITE);
2600 esp->prev_hme_dmacsr = tmp;
2602 ESPDATA(("DMA|TI --> do_intr_end\n"));
2603 if (thisphase == in_datain) {
2604 sbus_writel(hmuch, esp->dregs + DMA_COUNT);
2605 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
2606 } else {
2607 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
2608 sbus_writel(hmuch, esp->dregs + DMA_COUNT);
2610 sbus_writel((__u32)((unsigned long)SCptr->SCp.ptr), esp->dregs+DMA_ADDR);
2611 sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
2612 } else {
2613 esp_setcount(esp->eregs, hmuch, 0);
2614 dma_setup(esp, ((__u32)((unsigned long)SCptr->SCp.ptr)),
2615 hmuch, (thisphase == in_datain));
2616 ESPDATA(("DMA|TI --> do_intr_end\n"));
2617 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
2619 return do_intr_end;
2622 /* See how successful the data transfer was. */
2623 static int esp_do_data_finale(struct esp *esp)
2625 struct scsi_cmnd *SCptr = esp->current_SC;
2626 struct esp_device *esp_dev = SCptr->device->hostdata;
2627 int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
2629 ESPDATA(("esp_do_data_finale: "));
2631 if (SCptr->SCp.phase == in_datain) {
2632 if (esp->sreg & ESP_STAT_PERR) {
2633 /* Yuck, parity error. The ESP asserts ATN
2634 * so that we can go to message out phase
2635 * immediately and inform the target that
2636 * something bad happened.
2638 ESPLOG(("esp%d: data bad parity detected.\n",
2639 esp->esp_id));
2640 esp->cur_msgout[0] = INITIATOR_ERROR;
2641 esp->msgout_len = 1;
2643 dma_drain(esp);
2645 dma_invalidate(esp);
2647 /* This could happen for the above parity error case. */
2648 if (esp->ireg != ESP_INTR_BSERV) {
2649 /* Please go to msgout phase, please please please... */
2650 ESPLOG(("esp%d: !BSERV after data, probably to msgout\n",
2651 esp->esp_id));
2652 return esp_do_phase_determine(esp);
2655 /* Check for partial transfers and other horrible events.
2656 * Note, here we read the real fifo flags register even
2657 * on HME broken adapters because we skip the HME fifo
2658 * workaround code in esp_handle() if we are doing data
2659 * phase things. We don't want to fuck directly with
2660 * the fifo like that, especially if doing synchronous
2661 * transfers! Also, will need to double the count on
2662 * HME if we are doing wide transfers, as the HME fifo
2663 * will move and count 16-bit quantities during wide data.
2664 * SMCC _and_ Qlogic can both bite me.
2666 fifocnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
2667 if (esp->erev != fashme)
2668 ecount = esp_getcount(esp->eregs, 0);
2669 bytes_sent = esp->current_transfer_size;
2671 ESPDATA(("trans_sz(%d), ", bytes_sent));
2672 if (esp->erev == fashme) {
2673 if (!(esp->sreg & ESP_STAT_TCNT)) {
2674 ecount = esp_getcount(esp->eregs, 1);
2675 bytes_sent -= ecount;
2678 /* Always subtract any cruft remaining in the FIFO. */
2679 if (esp->prev_cfg3 & ESP_CONFIG3_EWIDE)
2680 fifocnt <<= 1;
2681 if (SCptr->SCp.phase == in_dataout)
2682 bytes_sent -= fifocnt;
2684 /* I have an IBM disk which exhibits the following
2685 * behavior during writes to it. It disconnects in
2686 * the middle of a partial transfer, the current sglist
2687 * buffer is 1024 bytes, the disk stops data transfer
2688 * at 512 bytes.
2690 * However the FAS366 reports that 32 more bytes were
2691 * transferred than really were. This is precisely
2692 * the size of a fully loaded FIFO in wide scsi mode.
2693 * The FIFO state recorded indicates that it is empty.
2695 * I have no idea if this is a bug in the FAS366 chip
2696 * or a bug in the firmware on this IBM disk. In any
2697 * event the following seems to be a good workaround. -DaveM
2699 if (bytes_sent != esp->current_transfer_size &&
2700 SCptr->SCp.phase == in_dataout) {
2701 int mask = (64 - 1);
2703 if ((esp->prev_cfg3 & ESP_CONFIG3_EWIDE) == 0)
2704 mask >>= 1;
2706 if (bytes_sent & mask)
2707 bytes_sent -= (bytes_sent & mask);
2709 } else {
2710 if (!(esp->sreg & ESP_STAT_TCNT))
2711 bytes_sent -= ecount;
2712 if (SCptr->SCp.phase == in_dataout)
2713 bytes_sent -= fifocnt;
2716 ESPDATA(("bytes_sent(%d), ", bytes_sent));
2718 /* If we were in synchronous mode, check for peculiarities. */
2719 if (esp->erev == fashme) {
2720 if (esp_dev->sync_max_offset) {
2721 if (SCptr->SCp.phase == in_dataout)
2722 esp_cmd(esp, ESP_CMD_FLUSH);
2723 } else {
2724 esp_cmd(esp, ESP_CMD_FLUSH);
2726 } else {
2727 if (esp_dev->sync_max_offset)
2728 bogus_data = esp100_sync_hwbug(esp, SCptr, fifocnt);
2729 else
2730 esp_cmd(esp, ESP_CMD_FLUSH);
2733 /* Until we are sure of what has happened, we are certainly
2734 * in the dark.
2736 esp_advance_phase(SCptr, in_the_dark);
2738 if (bytes_sent < 0) {
2739 /* I've seen this happen due to lost state in this
2740 * driver. No idea why it happened, but allowing
2741 * this value to be negative caused things to
2742 * lock up. This allows greater chance of recovery.
2743 * In fact every time I've seen this, it has been
2744 * a driver bug without question.
2746 ESPLOG(("esp%d: yieee, bytes_sent < 0!\n", esp->esp_id));
2747 ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%d\n",
2748 esp->esp_id,
2749 esp->current_transfer_size, fifocnt, ecount));
2750 ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%d\n",
2751 esp->esp_id,
2752 SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
2753 ESPLOG(("esp%d: Forcing async for target %d\n", esp->esp_id,
2754 SCptr->device->id));
2755 SCptr->device->borken = 1;
2756 esp_dev->sync = 0;
2757 bytes_sent = 0;
2760 /* Update the state of our transfer. */
2761 SCptr->SCp.ptr += bytes_sent;
2762 SCptr->SCp.this_residual -= bytes_sent;
2763 if (SCptr->SCp.this_residual < 0) {
2764 /* shit */
2765 ESPLOG(("esp%d: Data transfer overrun.\n", esp->esp_id));
2766 SCptr->SCp.this_residual = 0;
2769 /* Maybe continue. */
2770 if (!bogus_data) {
2771 ESPDATA(("!bogus_data, "));
2773 /* NO MATTER WHAT, we advance the scatterlist,
2774 * if the target should decide to disconnect
2775 * in between scatter chunks (which is common)
2776 * we could die horribly! I used to have the sg
2777 * advance occur only if we are going back into
2778 * (or are staying in) a data phase, you can
2779 * imagine the hell I went through trying to
2780 * figure this out.
2782 if (SCptr->use_sg && !SCptr->SCp.this_residual)
2783 advance_sg(SCptr);
2784 if (sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
2785 ESPDATA(("to more data\n"));
2786 return esp_do_data(esp);
2788 ESPDATA(("to new phase\n"));
2789 return esp_do_phase_determine(esp);
2791 /* Bogus data, just wait for next interrupt. */
2792 ESPLOG(("esp%d: bogus_data during end of data phase\n",
2793 esp->esp_id));
2794 return do_intr_end;
2797 /* We received a non-good status return at the end of
2798 * running a SCSI command. This is used to decide if
2799 * we should clear our synchronous transfer state for
2800 * such a device when that happens.
2802 * The idea is that when spinning up a disk or rewinding
2803 * a tape, we don't want to go into a loop re-negotiating
2804 * synchronous capabilities over and over.
2806 static int esp_should_clear_sync(struct scsi_cmnd *sp)
2808 u8 cmd1 = sp->cmnd[0];
2809 u8 cmd2 = sp->data_cmnd[0];
2811 /* These cases are for spinning up a disk and
2812 * waiting for that spinup to complete.
2814 if (cmd1 == START_STOP ||
2815 cmd2 == START_STOP)
2816 return 0;
2818 if (cmd1 == TEST_UNIT_READY ||
2819 cmd2 == TEST_UNIT_READY)
2820 return 0;
2822 /* One more special case for SCSI tape drives,
2823 * this is what is used to probe the device for
2824 * completion of a rewind or tape load operation.
2826 if (sp->device->type == TYPE_TAPE) {
2827 if (cmd1 == MODE_SENSE ||
2828 cmd2 == MODE_SENSE)
2829 return 0;
2832 return 1;
2835 /* Either a command is completing or a target is dropping off the bus
2836 * to continue the command in the background so we can do other work.
2838 static int esp_do_freebus(struct esp *esp)
2840 struct scsi_cmnd *SCptr = esp->current_SC;
2841 struct esp_device *esp_dev = SCptr->device->hostdata;
2842 int rval;
2844 rval = skipahead2(esp, SCptr, in_status, in_msgindone, in_freeing);
2845 if (rval)
2846 return rval;
2847 if (esp->ireg != ESP_INTR_DC) {
2848 ESPLOG(("esp%d: Target will not disconnect\n", esp->esp_id));
2849 return do_reset_bus; /* target will not drop BSY... */
2851 esp->msgout_len = 0;
2852 esp->prevmsgout = NOP;
2853 if (esp->prevmsgin == COMMAND_COMPLETE) {
2854 /* Normal end of nexus. */
2855 if (esp->disconnected_SC || (esp->erev == fashme))
2856 esp_cmd(esp, ESP_CMD_ESEL);
2858 if (SCptr->SCp.Status != GOOD &&
2859 SCptr->SCp.Status != CONDITION_GOOD &&
2860 ((1<<SCptr->device->id) & esp->targets_present) &&
2861 esp_dev->sync &&
2862 esp_dev->sync_max_offset) {
2863 /* SCSI standard says that the synchronous capabilities
2864 * should be renegotiated at this point. Most likely
2865 * we are about to request sense from this target
2866 * in which case we want to avoid using sync
2867 * transfers until we are sure of the current target
2868 * state.
2870 ESPMISC(("esp: Status <%d> for target %d lun %d\n",
2871 SCptr->SCp.Status, SCptr->device->id, SCptr->device->lun));
2873 /* But don't do this when spinning up a disk at
2874 * boot time while we poll for completion as it
2875 * fills up the console with messages. Also, tapes
2876 * can report not ready many times right after
2877 * loading up a tape.
2879 if (esp_should_clear_sync(SCptr) != 0)
2880 esp_dev->sync = 0;
2882 ESPDISC(("F<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
2883 esp_done(esp, ((SCptr->SCp.Status & 0xff) |
2884 ((SCptr->SCp.Message & 0xff)<<8) |
2885 (DID_OK << 16)));
2886 } else if (esp->prevmsgin == DISCONNECT) {
2887 /* Normal disconnect. */
2888 esp_cmd(esp, ESP_CMD_ESEL);
2889 ESPDISC(("D<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
2890 append_SC(&esp->disconnected_SC, SCptr);
2891 esp->current_SC = NULL;
2892 if (esp->issue_SC)
2893 esp_exec_cmd(esp);
2894 } else {
2895 /* Driver bug, we do not expect a disconnect here
2896 * and should not have advanced the state engine
2897 * to in_freeing.
2899 ESPLOG(("esp%d: last msg not disc and not cmd cmplt.\n",
2900 esp->esp_id));
2901 return do_reset_bus;
2903 return do_intr_end;
2906 /* When a reselect occurs, and we cannot find the command to
2907 * reconnect to in our queues, we do this.
2909 static int esp_bad_reconnect(struct esp *esp)
2911 struct scsi_cmnd *sp;
2913 ESPLOG(("esp%d: Eieeee, reconnecting unknown command!\n",
2914 esp->esp_id));
2915 ESPLOG(("QUEUE DUMP\n"));
2916 sp = esp->issue_SC;
2917 ESPLOG(("esp%d: issue_SC[", esp->esp_id));
2918 while (sp) {
2919 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2920 sp = (struct scsi_cmnd *) sp->host_scribble;
2922 ESPLOG(("]\n"));
2923 sp = esp->current_SC;
2924 ESPLOG(("esp%d: current_SC[", esp->esp_id));
2925 if (sp)
2926 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2927 else
2928 ESPLOG(("<NULL>"));
2929 ESPLOG(("]\n"));
2930 sp = esp->disconnected_SC;
2931 ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
2932 while (sp) {
2933 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2934 sp = (struct scsi_cmnd *) sp->host_scribble;
2936 ESPLOG(("]\n"));
2937 return do_reset_bus;
2940 /* Do the needy when a target tries to reconnect to us. */
2941 static int esp_do_reconnect(struct esp *esp)
2943 int lun, target;
2944 struct scsi_cmnd *SCptr;
2946 /* Check for all bogus conditions first. */
2947 target = reconnect_target(esp);
2948 if (target < 0) {
2949 ESPDISC(("bad bus bits\n"));
2950 return do_reset_bus;
2952 lun = reconnect_lun(esp);
2953 if (lun < 0) {
2954 ESPDISC(("target=%2x, bad identify msg\n", target));
2955 return do_reset_bus;
2958 /* Things look ok... */
2959 ESPDISC(("R<%02x,%02x>", target, lun));
2961 /* Must not flush FIFO or DVMA on HME. */
2962 if (esp->erev != fashme) {
2963 esp_cmd(esp, ESP_CMD_FLUSH);
2964 if (esp100_reconnect_hwbug(esp))
2965 return do_reset_bus;
2966 esp_cmd(esp, ESP_CMD_NULL);
2969 SCptr = remove_SC(&esp->disconnected_SC, (u8) target, (u8) lun);
2970 if (!SCptr)
2971 return esp_bad_reconnect(esp);
2973 esp_connect(esp, SCptr);
2974 esp_cmd(esp, ESP_CMD_MOK);
2976 if (esp->erev == fashme)
2977 sbus_writeb(((SCptr->device->id & 0xf) |
2978 (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT)),
2979 esp->eregs + ESP_BUSID);
2981 /* Reconnect implies a restore pointers operation. */
2982 esp_restore_pointers(esp, SCptr);
2984 esp->snip = 0;
2985 esp_advance_phase(SCptr, in_the_dark);
2986 return do_intr_end;
2989 /* End of NEXUS (hopefully), pick up status + message byte then leave if
2990 * all goes well.
2992 static int esp_do_status(struct esp *esp)
2994 struct scsi_cmnd *SCptr = esp->current_SC;
2995 int intr, rval;
2997 rval = skipahead1(esp, SCptr, in_the_dark, in_status);
2998 if (rval)
2999 return rval;
3000 intr = esp->ireg;
3001 ESPSTAT(("esp_do_status: "));
3002 if (intr != ESP_INTR_DC) {
3003 int message_out = 0; /* for parity problems */
3005 /* Ack the message. */
3006 ESPSTAT(("ack msg, "));
3007 esp_cmd(esp, ESP_CMD_MOK);
3009 if (esp->erev != fashme) {
3010 dma_flashclear(esp);
3012 /* Wait till the first bits settle. */
3013 while (esp->esp_command[0] == 0xff)
3014 udelay(1);
3015 } else {
3016 esp->esp_command[0] = esp->hme_fifo_workaround_buffer[0];
3017 esp->esp_command[1] = esp->hme_fifo_workaround_buffer[1];
3020 ESPSTAT(("got something, "));
3021 /* ESP chimes in with one of
3023 * 1) function done interrupt:
3024 * both status and message in bytes
3025 * are available
3027 * 2) bus service interrupt:
3028 * only status byte was acquired
3030 * 3) Anything else:
3031 * can't happen, but we test for it
3032 * anyways
3034 * ALSO: If bad parity was detected on either
3035 * the status _or_ the message byte then
3036 * the ESP has asserted ATN on the bus
3037 * and we must therefore wait for the
3038 * next phase change.
3040 if (intr & ESP_INTR_FDONE) {
3041 /* We got it all, hallejulia. */
3042 ESPSTAT(("got both, "));
3043 SCptr->SCp.Status = esp->esp_command[0];
3044 SCptr->SCp.Message = esp->esp_command[1];
3045 esp->prevmsgin = SCptr->SCp.Message;
3046 esp->cur_msgin[0] = SCptr->SCp.Message;
3047 if (esp->sreg & ESP_STAT_PERR) {
3048 /* There was bad parity for the
3049 * message byte, the status byte
3050 * was ok.
3052 message_out = MSG_PARITY_ERROR;
3054 } else if (intr == ESP_INTR_BSERV) {
3055 /* Only got status byte. */
3056 ESPLOG(("esp%d: got status only, ", esp->esp_id));
3057 if (!(esp->sreg & ESP_STAT_PERR)) {
3058 SCptr->SCp.Status = esp->esp_command[0];
3059 SCptr->SCp.Message = 0xff;
3060 } else {
3061 /* The status byte had bad parity.
3062 * we leave the scsi_pointer Status
3063 * field alone as we set it to a default
3064 * of CHECK_CONDITION in esp_queue.
3066 message_out = INITIATOR_ERROR;
3068 } else {
3069 /* This shouldn't happen ever. */
3070 ESPSTAT(("got bolixed\n"));
3071 esp_advance_phase(SCptr, in_the_dark);
3072 return esp_do_phase_determine(esp);
3075 if (!message_out) {
3076 ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
3077 SCptr->SCp.Message));
3078 if (SCptr->SCp.Message == COMMAND_COMPLETE) {
3079 ESPSTAT(("and was COMMAND_COMPLETE\n"));
3080 esp_advance_phase(SCptr, in_freeing);
3081 return esp_do_freebus(esp);
3082 } else {
3083 ESPLOG(("esp%d: and _not_ COMMAND_COMPLETE\n",
3084 esp->esp_id));
3085 esp->msgin_len = esp->msgin_ctr = 1;
3086 esp_advance_phase(SCptr, in_msgindone);
3087 return esp_do_msgindone(esp);
3089 } else {
3090 /* With luck we'll be able to let the target
3091 * know that bad parity happened, it will know
3092 * which byte caused the problems and send it
3093 * again. For the case where the status byte
3094 * receives bad parity, I do not believe most
3095 * targets recover very well. We'll see.
3097 ESPLOG(("esp%d: bad parity somewhere mout=%2x\n",
3098 esp->esp_id, message_out));
3099 esp->cur_msgout[0] = message_out;
3100 esp->msgout_len = esp->msgout_ctr = 1;
3101 esp_advance_phase(SCptr, in_the_dark);
3102 return esp_do_phase_determine(esp);
3104 } else {
3105 /* If we disconnect now, all hell breaks loose. */
3106 ESPLOG(("esp%d: whoops, disconnect\n", esp->esp_id));
3107 esp_advance_phase(SCptr, in_the_dark);
3108 return esp_do_phase_determine(esp);
3112 static int esp_enter_status(struct esp *esp)
3114 u8 thecmd = ESP_CMD_ICCSEQ;
3116 esp_cmd(esp, ESP_CMD_FLUSH);
3117 if (esp->erev != fashme) {
3118 u32 tmp;
3120 esp->esp_command[0] = esp->esp_command[1] = 0xff;
3121 sbus_writeb(2, esp->eregs + ESP_TCLOW);
3122 sbus_writeb(0, esp->eregs + ESP_TCMED);
3123 tmp = sbus_readl(esp->dregs + DMA_CSR);
3124 tmp |= (DMA_ST_WRITE | DMA_ENABLE);
3125 sbus_writel(tmp, esp->dregs + DMA_CSR);
3126 if (esp->dma->revision == dvmaesc1)
3127 sbus_writel(0x100, esp->dregs + DMA_COUNT);
3128 sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
3129 thecmd |= ESP_CMD_DMA;
3131 esp_cmd(esp, thecmd);
3132 esp_advance_phase(esp->current_SC, in_status);
3134 return esp_do_status(esp);
3137 static int esp_disconnect_amidst_phases(struct esp *esp)
3139 struct scsi_cmnd *sp = esp->current_SC;
3140 struct esp_device *esp_dev = sp->device->hostdata;
3142 /* This means real problems if we see this
3143 * here. Unless we were actually trying
3144 * to force the device to abort/reset.
3146 ESPLOG(("esp%d Disconnect amidst phases, ", esp->esp_id));
3147 ESPLOG(("pphase<%s> cphase<%s>, ",
3148 phase_string(sp->SCp.phase),
3149 phase_string(sp->SCp.sent_command)));
3151 if (esp->disconnected_SC != NULL || (esp->erev == fashme))
3152 esp_cmd(esp, ESP_CMD_ESEL);
3154 switch (esp->cur_msgout[0]) {
3155 default:
3156 /* We didn't expect this to happen at all. */
3157 ESPLOG(("device is bolixed\n"));
3158 esp_advance_phase(sp, in_tgterror);
3159 esp_done(esp, (DID_ERROR << 16));
3160 break;
3162 case BUS_DEVICE_RESET:
3163 ESPLOG(("device reset successful\n"));
3164 esp_dev->sync_max_offset = 0;
3165 esp_dev->sync_min_period = 0;
3166 esp_dev->sync = 0;
3167 esp_advance_phase(sp, in_resetdev);
3168 esp_done(esp, (DID_RESET << 16));
3169 break;
3171 case ABORT:
3172 ESPLOG(("device abort successful\n"));
3173 esp_advance_phase(sp, in_abortone);
3174 esp_done(esp, (DID_ABORT << 16));
3175 break;
3178 return do_intr_end;
3181 static int esp_enter_msgout(struct esp *esp)
3183 esp_advance_phase(esp->current_SC, in_msgout);
3184 return esp_do_msgout(esp);
3187 static int esp_enter_msgin(struct esp *esp)
3189 esp_advance_phase(esp->current_SC, in_msgin);
3190 return esp_do_msgin(esp);
3193 static int esp_enter_cmd(struct esp *esp)
3195 esp_advance_phase(esp->current_SC, in_cmdbegin);
3196 return esp_do_cmdbegin(esp);
3199 static int esp_enter_badphase(struct esp *esp)
3201 ESPLOG(("esp%d: Bizarre bus phase %2x.\n", esp->esp_id,
3202 esp->sreg & ESP_STAT_PMASK));
3203 return do_reset_bus;
3206 typedef int (*espfunc_t)(struct esp *);
3208 static espfunc_t phase_vector[] = {
3209 esp_do_data, /* ESP_DOP */
3210 esp_do_data, /* ESP_DIP */
3211 esp_enter_cmd, /* ESP_CMDP */
3212 esp_enter_status, /* ESP_STATP */
3213 esp_enter_badphase, /* ESP_STAT_PMSG */
3214 esp_enter_badphase, /* ESP_STAT_PMSG | ESP_STAT_PIO */
3215 esp_enter_msgout, /* ESP_MOP */
3216 esp_enter_msgin, /* ESP_MIP */
3219 /* The target has control of the bus and we have to see where it has
3220 * taken us.
3222 static int esp_do_phase_determine(struct esp *esp)
3224 if ((esp->ireg & ESP_INTR_DC) != 0)
3225 return esp_disconnect_amidst_phases(esp);
3226 return phase_vector[esp->sreg & ESP_STAT_PMASK](esp);
3229 /* First interrupt after exec'ing a cmd comes here. */
3230 static int esp_select_complete(struct esp *esp)
3232 struct scsi_cmnd *SCptr = esp->current_SC;
3233 struct esp_device *esp_dev = SCptr->device->hostdata;
3234 int cmd_bytes_sent, fcnt;
3236 if (esp->erev != fashme)
3237 esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
3239 if (esp->erev == fashme)
3240 fcnt = esp->hme_fifo_workaround_count;
3241 else
3242 fcnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
3244 cmd_bytes_sent = esp_bytes_sent(esp, fcnt);
3245 dma_invalidate(esp);
3247 /* Let's check to see if a reselect happened
3248 * while we we're trying to select. This must
3249 * be checked first.
3251 if (esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
3252 esp_reconnect(esp, SCptr);
3253 return esp_do_reconnect(esp);
3256 /* Looks like things worked, we should see a bus service &
3257 * a function complete interrupt at this point. Note we
3258 * are doing a direct comparison because we don't want to
3259 * be fooled into thinking selection was successful if
3260 * ESP_INTR_DC is set, see below.
3262 if (esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
3263 /* target speaks... */
3264 esp->targets_present |= (1<<SCptr->device->id);
3266 /* What if the target ignores the sdtr? */
3267 if (esp->snip)
3268 esp_dev->sync = 1;
3270 /* See how far, if at all, we got in getting
3271 * the information out to the target.
3273 switch (esp->seqreg) {
3274 default:
3276 case ESP_STEP_ASEL:
3277 /* Arbitration won, target selected, but
3278 * we are in some phase which is not command
3279 * phase nor is it message out phase.
3281 * XXX We've confused the target, obviously.
3282 * XXX So clear it's state, but we also end
3283 * XXX up clearing everyone elses. That isn't
3284 * XXX so nice. I'd like to just reset this
3285 * XXX target, but if I cannot even get it's
3286 * XXX attention and finish selection to talk
3287 * XXX to it, there is not much more I can do.
3288 * XXX If we have a loaded bus we're going to
3289 * XXX spend the next second or so renegotiating
3290 * XXX for synchronous transfers.
3292 ESPLOG(("esp%d: STEP_ASEL for tgt %d\n",
3293 esp->esp_id, SCptr->device->id));
3295 case ESP_STEP_SID:
3296 /* Arbitration won, target selected, went
3297 * to message out phase, sent one message
3298 * byte, then we stopped. ATN is asserted
3299 * on the SCSI bus and the target is still
3300 * there hanging on. This is a legal
3301 * sequence step if we gave the ESP a select
3302 * and stop command.
3304 * XXX See above, I could set the borken flag
3305 * XXX in the device struct and retry the
3306 * XXX command. But would that help for
3307 * XXX tagged capable targets?
3310 case ESP_STEP_NCMD:
3311 /* Arbitration won, target selected, maybe
3312 * sent the one message byte in message out
3313 * phase, but we did not go to command phase
3314 * in the end. Actually, we could have sent
3315 * only some of the message bytes if we tried
3316 * to send out the entire identify and tag
3317 * message using ESP_CMD_SA3.
3319 cmd_bytes_sent = 0;
3320 break;
3322 case ESP_STEP_PPC:
3323 /* No, not the powerPC pinhead. Arbitration
3324 * won, all message bytes sent if we went to
3325 * message out phase, went to command phase
3326 * but only part of the command was sent.
3328 * XXX I've seen this, but usually in conjunction
3329 * XXX with a gross error which appears to have
3330 * XXX occurred between the time I told the
3331 * XXX ESP to arbitrate and when I got the
3332 * XXX interrupt. Could I have misloaded the
3333 * XXX command bytes into the fifo? Actually,
3334 * XXX I most likely missed a phase, and therefore
3335 * XXX went into never never land and didn't even
3336 * XXX know it. That was the old driver though.
3337 * XXX What is even more peculiar is that the ESP
3338 * XXX showed the proper function complete and
3339 * XXX bus service bits in the interrupt register.
3342 case ESP_STEP_FINI4:
3343 case ESP_STEP_FINI5:
3344 case ESP_STEP_FINI6:
3345 case ESP_STEP_FINI7:
3346 /* Account for the identify message */
3347 if (SCptr->SCp.phase == in_slct_norm)
3348 cmd_bytes_sent -= 1;
3351 if (esp->erev != fashme)
3352 esp_cmd(esp, ESP_CMD_NULL);
3354 /* Be careful, we could really get fucked during synchronous
3355 * data transfers if we try to flush the fifo now.
3357 if ((esp->erev != fashme) && /* not a Happy Meal and... */
3358 !fcnt && /* Fifo is empty and... */
3359 /* either we are not doing synchronous transfers or... */
3360 (!esp_dev->sync_max_offset ||
3361 /* We are not going into data in phase. */
3362 ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
3363 esp_cmd(esp, ESP_CMD_FLUSH); /* flush is safe */
3365 /* See how far we got if this is not a slow command. */
3366 if (!esp->esp_slowcmd) {
3367 if (cmd_bytes_sent < 0)
3368 cmd_bytes_sent = 0;
3369 if (cmd_bytes_sent != SCptr->cmd_len) {
3370 /* Crapola, mark it as a slowcmd
3371 * so that we have some chance of
3372 * keeping the command alive with
3373 * good luck.
3375 * XXX Actually, if we didn't send it all
3376 * XXX this means either we didn't set things
3377 * XXX up properly (driver bug) or the target
3378 * XXX or the ESP detected parity on one of
3379 * XXX the command bytes. This makes much
3380 * XXX more sense, and therefore this code
3381 * XXX should be changed to send out a
3382 * XXX parity error message or if the status
3383 * XXX register shows no parity error then
3384 * XXX just expect the target to bring the
3385 * XXX bus into message in phase so that it
3386 * XXX can send us the parity error message.
3387 * XXX SCSI sucks...
3389 esp->esp_slowcmd = 1;
3390 esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
3391 esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
3395 /* Now figure out where we went. */
3396 esp_advance_phase(SCptr, in_the_dark);
3397 return esp_do_phase_determine(esp);
3400 /* Did the target even make it? */
3401 if (esp->ireg == ESP_INTR_DC) {
3402 /* wheee... nobody there or they didn't like
3403 * what we told it to do, clean up.
3406 /* If anyone is off the bus, but working on
3407 * a command in the background for us, tell
3408 * the ESP to listen for them.
3410 if (esp->disconnected_SC)
3411 esp_cmd(esp, ESP_CMD_ESEL);
3413 if (((1<<SCptr->device->id) & esp->targets_present) &&
3414 esp->seqreg != 0 &&
3415 (esp->cur_msgout[0] == EXTENDED_MESSAGE) &&
3416 (SCptr->SCp.phase == in_slct_msg ||
3417 SCptr->SCp.phase == in_slct_stop)) {
3418 /* shit */
3419 esp->snip = 0;
3420 ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
3421 "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
3422 esp_dev->sync_max_offset = 0;
3423 esp_dev->sync_min_period = 0;
3424 esp_dev->sync = 1; /* so we don't negotiate again */
3426 /* Run the command again, this time though we
3427 * won't try to negotiate for synchronous transfers.
3429 * XXX I'd like to do something like send an
3430 * XXX INITIATOR_ERROR or ABORT message to the
3431 * XXX target to tell it, "Sorry I confused you,
3432 * XXX please come back and I will be nicer next
3433 * XXX time". But that requires having the target
3434 * XXX on the bus, and it has dropped BSY on us.
3436 esp->current_SC = NULL;
3437 esp_advance_phase(SCptr, not_issued);
3438 prepend_SC(&esp->issue_SC, SCptr);
3439 esp_exec_cmd(esp);
3440 return do_intr_end;
3443 /* Ok, this is normal, this is what we see during boot
3444 * or whenever when we are scanning the bus for targets.
3445 * But first make sure that is really what is happening.
3447 if (((1<<SCptr->device->id) & esp->targets_present)) {
3448 ESPLOG(("esp%d: Warning, live target %d not responding to "
3449 "selection.\n", esp->esp_id, SCptr->device->id));
3451 /* This _CAN_ happen. The SCSI standard states that
3452 * the target is to _not_ respond to selection if
3453 * _it_ detects bad parity on the bus for any reason.
3454 * Therefore, we assume that if we've talked successfully
3455 * to this target before, bad parity is the problem.
3457 esp_done(esp, (DID_PARITY << 16));
3458 } else {
3459 /* Else, there really isn't anyone there. */
3460 ESPMISC(("esp: selection failure, maybe nobody there?\n"));
3461 ESPMISC(("esp: target %d lun %d\n",
3462 SCptr->device->id, SCptr->device->lun));
3463 esp_done(esp, (DID_BAD_TARGET << 16));
3465 return do_intr_end;
3468 ESPLOG(("esp%d: Selection failure.\n", esp->esp_id));
3469 printk("esp%d: Currently -- ", esp->esp_id);
3470 esp_print_ireg(esp->ireg); printk(" ");
3471 esp_print_statreg(esp->sreg); printk(" ");
3472 esp_print_seqreg(esp->seqreg); printk("\n");
3473 printk("esp%d: New -- ", esp->esp_id);
3474 esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
3475 esp->seqreg = sbus_readb(esp->eregs + ESP_SSTEP);
3476 esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
3477 esp_print_ireg(esp->ireg); printk(" ");
3478 esp_print_statreg(esp->sreg); printk(" ");
3479 esp_print_seqreg(esp->seqreg); printk("\n");
3480 ESPLOG(("esp%d: resetting bus\n", esp->esp_id));
3481 return do_reset_bus; /* ugh... */
3484 /* Continue reading bytes for msgin phase. */
3485 static int esp_do_msgincont(struct esp *esp)
3487 if (esp->ireg & ESP_INTR_BSERV) {
3488 /* in the right phase too? */
3489 if ((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
3490 /* phew... */
3491 esp_cmd(esp, ESP_CMD_TI);
3492 esp_advance_phase(esp->current_SC, in_msgindone);
3493 return do_intr_end;
3496 /* We changed phase but ESP shows bus service,
3497 * in this case it is most likely that we, the
3498 * hacker who has been up for 20hrs straight
3499 * staring at the screen, drowned in coffee
3500 * smelling like retched cigarette ashes
3501 * have miscoded something..... so, try to
3502 * recover as best we can.
3504 ESPLOG(("esp%d: message in mis-carriage.\n", esp->esp_id));
3506 esp_advance_phase(esp->current_SC, in_the_dark);
3507 return do_phase_determine;
3510 static int check_singlebyte_msg(struct esp *esp)
3512 esp->prevmsgin = esp->cur_msgin[0];
3513 if (esp->cur_msgin[0] & 0x80) {
3514 /* wheee... */
3515 ESPLOG(("esp%d: target sends identify amidst phases\n",
3516 esp->esp_id));
3517 esp_advance_phase(esp->current_SC, in_the_dark);
3518 return 0;
3519 } else if (((esp->cur_msgin[0] & 0xf0) == 0x20) ||
3520 (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
3521 esp->msgin_len = 2;
3522 esp_advance_phase(esp->current_SC, in_msgincont);
3523 return 0;
3525 esp_advance_phase(esp->current_SC, in_the_dark);
3526 switch (esp->cur_msgin[0]) {
3527 default:
3528 /* We don't want to hear about it. */
3529 ESPLOG(("esp%d: msg %02x which we don't know about\n", esp->esp_id,
3530 esp->cur_msgin[0]));
3531 return MESSAGE_REJECT;
3533 case NOP:
3534 ESPLOG(("esp%d: target %d sends a nop\n", esp->esp_id,
3535 esp->current_SC->device->id));
3536 return 0;
3538 case RESTORE_POINTERS:
3539 /* In this case we might also have to backup the
3540 * "slow command" pointer. It is rare to get such
3541 * a save/restore pointer sequence so early in the
3542 * bus transition sequences, but cover it.
3544 if (esp->esp_slowcmd) {
3545 esp->esp_scmdleft = esp->current_SC->cmd_len;
3546 esp->esp_scmdp = &esp->current_SC->cmnd[0];
3548 esp_restore_pointers(esp, esp->current_SC);
3549 return 0;
3551 case SAVE_POINTERS:
3552 esp_save_pointers(esp, esp->current_SC);
3553 return 0;
3555 case COMMAND_COMPLETE:
3556 case DISCONNECT:
3557 /* Freeing the bus, let it go. */
3558 esp->current_SC->SCp.phase = in_freeing;
3559 return 0;
3561 case MESSAGE_REJECT:
3562 ESPMISC(("msg reject, "));
3563 if (esp->prevmsgout == EXTENDED_MESSAGE) {
3564 struct esp_device *esp_dev = esp->current_SC->device->hostdata;
3566 /* Doesn't look like this target can
3567 * do synchronous or WIDE transfers.
3569 ESPSDTR(("got reject, was trying nego, clearing sync/WIDE\n"));
3570 esp_dev->sync = 1;
3571 esp_dev->wide = 1;
3572 esp_dev->sync_min_period = 0;
3573 esp_dev->sync_max_offset = 0;
3574 return 0;
3575 } else {
3576 ESPMISC(("not sync nego, sending ABORT\n"));
3577 return ABORT;
3582 /* Target negotiates for synchronous transfers before we do, this
3583 * is legal although very strange. What is even funnier is that
3584 * the SCSI2 standard specifically recommends against targets doing
3585 * this because so many initiators cannot cope with this occurring.
3587 static int target_with_ants_in_pants(struct esp *esp,
3588 struct scsi_cmnd *SCptr,
3589 struct esp_device *esp_dev)
3591 if (esp_dev->sync || SCptr->device->borken) {
3592 /* sorry, no can do */
3593 ESPSDTR(("forcing to async, "));
3594 build_sync_nego_msg(esp, 0, 0);
3595 esp_dev->sync = 1;
3596 esp->snip = 1;
3597 ESPLOG(("esp%d: hoping for msgout\n", esp->esp_id));
3598 esp_advance_phase(SCptr, in_the_dark);
3599 return EXTENDED_MESSAGE;
3602 /* Ok, we'll check them out... */
3603 return 0;
3606 static void sync_report(struct esp *esp)
3608 int msg3, msg4;
3609 char *type;
3611 msg3 = esp->cur_msgin[3];
3612 msg4 = esp->cur_msgin[4];
3613 if (msg4) {
3614 int hz = 1000000000 / (msg3 * 4);
3615 int integer = hz / 1000000;
3616 int fraction = (hz - (integer * 1000000)) / 10000;
3617 if ((esp->erev == fashme) &&
3618 (esp->config3[esp->current_SC->device->id] & ESP_CONFIG3_EWIDE)) {
3619 type = "FAST-WIDE";
3620 integer <<= 1;
3621 fraction <<= 1;
3622 } else if ((msg3 * 4) < 200) {
3623 type = "FAST";
3624 } else {
3625 type = "synchronous";
3628 /* Do not transform this back into one big printk
3629 * again, it triggers a bug in our sparc64-gcc272
3630 * sibling call optimization. -DaveM
3632 ESPLOG((KERN_INFO "esp%d: target %d ",
3633 esp->esp_id, esp->current_SC->device->id));
3634 ESPLOG(("[period %dns offset %d %d.%02dMHz ",
3635 (int) msg3 * 4, (int) msg4,
3636 integer, fraction));
3637 ESPLOG(("%s SCSI%s]\n", type,
3638 (((msg3 * 4) < 200) ? "-II" : "")));
3639 } else {
3640 ESPLOG((KERN_INFO "esp%d: target %d asynchronous\n",
3641 esp->esp_id, esp->current_SC->device->id));
3645 static int check_multibyte_msg(struct esp *esp)
3647 struct scsi_cmnd *SCptr = esp->current_SC;
3648 struct esp_device *esp_dev = SCptr->device->hostdata;
3649 u8 regval = 0;
3650 int message_out = 0;
3652 ESPSDTR(("chk multibyte msg: "));
3653 if (esp->cur_msgin[2] == EXTENDED_SDTR) {
3654 int period = esp->cur_msgin[3];
3655 int offset = esp->cur_msgin[4];
3657 ESPSDTR(("is sync nego response, "));
3658 if (!esp->snip) {
3659 int rval;
3661 /* Target negotiates first! */
3662 ESPSDTR(("target jumps the gun, "));
3663 message_out = EXTENDED_MESSAGE; /* we must respond */
3664 rval = target_with_ants_in_pants(esp, SCptr, esp_dev);
3665 if (rval)
3666 return rval;
3669 ESPSDTR(("examining sdtr, "));
3671 /* Offset cannot be larger than ESP fifo size. */
3672 if (offset > 15) {
3673 ESPSDTR(("offset too big %2x, ", offset));
3674 offset = 15;
3675 ESPSDTR(("sending back new offset\n"));
3676 build_sync_nego_msg(esp, period, offset);
3677 return EXTENDED_MESSAGE;
3680 if (offset && period > esp->max_period) {
3681 /* Yeee, async for this slow device. */
3682 ESPSDTR(("period too long %2x, ", period));
3683 build_sync_nego_msg(esp, 0, 0);
3684 ESPSDTR(("hoping for msgout\n"));
3685 esp_advance_phase(esp->current_SC, in_the_dark);
3686 return EXTENDED_MESSAGE;
3687 } else if (offset && period < esp->min_period) {
3688 ESPSDTR(("period too short %2x, ", period));
3689 period = esp->min_period;
3690 if (esp->erev > esp236)
3691 regval = 4;
3692 else
3693 regval = 5;
3694 } else if (offset) {
3695 int tmp;
3697 ESPSDTR(("period is ok, "));
3698 tmp = esp->ccycle / 1000;
3699 regval = (((period << 2) + tmp - 1) / tmp);
3700 if (regval && ((esp->erev == fas100a ||
3701 esp->erev == fas236 ||
3702 esp->erev == fashme))) {
3703 if (period >= 50)
3704 regval--;
3708 if (offset) {
3709 u8 bit;
3711 esp_dev->sync_min_period = (regval & 0x1f);
3712 esp_dev->sync_max_offset = (offset | esp->radelay);
3713 if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
3714 if ((esp->erev == fas100a) || (esp->erev == fashme))
3715 bit = ESP_CONFIG3_FAST;
3716 else
3717 bit = ESP_CONFIG3_FSCSI;
3718 if (period < 50) {
3719 /* On FAS366, if using fast-20 synchronous transfers
3720 * we need to make sure the REQ/ACK assert/deassert
3721 * control bits are clear.
3723 if (esp->erev == fashme)
3724 esp_dev->sync_max_offset &= ~esp->radelay;
3725 esp->config3[SCptr->device->id] |= bit;
3726 } else {
3727 esp->config3[SCptr->device->id] &= ~bit;
3729 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3730 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
3732 esp->prev_soff = esp_dev->sync_max_offset;
3733 esp->prev_stp = esp_dev->sync_min_period;
3734 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
3735 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
3736 ESPSDTR(("soff=%2x stp=%2x cfg3=%2x\n",
3737 esp_dev->sync_max_offset,
3738 esp_dev->sync_min_period,
3739 esp->config3[SCptr->device->id]));
3741 esp->snip = 0;
3742 } else if (esp_dev->sync_max_offset) {
3743 u8 bit;
3745 /* back to async mode */
3746 ESPSDTR(("unaccaptable sync nego, forcing async\n"));
3747 esp_dev->sync_max_offset = 0;
3748 esp_dev->sync_min_period = 0;
3749 esp->prev_soff = 0;
3750 esp->prev_stp = 0;
3751 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
3752 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
3753 if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
3754 if ((esp->erev == fas100a) || (esp->erev == fashme))
3755 bit = ESP_CONFIG3_FAST;
3756 else
3757 bit = ESP_CONFIG3_FSCSI;
3758 esp->config3[SCptr->device->id] &= ~bit;
3759 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3760 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
3764 sync_report(esp);
3766 ESPSDTR(("chk multibyte msg: sync is known, "));
3767 esp_dev->sync = 1;
3769 if (message_out) {
3770 ESPLOG(("esp%d: sending sdtr back, hoping for msgout\n",
3771 esp->esp_id));
3772 build_sync_nego_msg(esp, period, offset);
3773 esp_advance_phase(SCptr, in_the_dark);
3774 return EXTENDED_MESSAGE;
3777 ESPSDTR(("returning zero\n"));
3778 esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
3779 return 0;
3780 } else if (esp->cur_msgin[2] == EXTENDED_WDTR) {
3781 int size = 8 << esp->cur_msgin[3];
3783 esp->wnip = 0;
3784 if (esp->erev != fashme) {
3785 ESPLOG(("esp%d: AIEEE wide msg received and not HME.\n",
3786 esp->esp_id));
3787 message_out = MESSAGE_REJECT;
3788 } else if (size > 16) {
3789 ESPLOG(("esp%d: AIEEE wide transfer for %d size "
3790 "not supported.\n", esp->esp_id, size));
3791 message_out = MESSAGE_REJECT;
3792 } else {
3793 /* Things look good; let's see what we got. */
3794 if (size == 16) {
3795 /* Set config 3 register for this target. */
3796 esp->config3[SCptr->device->id] |= ESP_CONFIG3_EWIDE;
3797 } else {
3798 /* Just make sure it was one byte sized. */
3799 if (size != 8) {
3800 ESPLOG(("esp%d: Aieee, wide nego of %d size.\n",
3801 esp->esp_id, size));
3802 message_out = MESSAGE_REJECT;
3803 goto finish;
3805 /* Pure paranoia. */
3806 esp->config3[SCptr->device->id] &= ~(ESP_CONFIG3_EWIDE);
3808 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3809 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
3811 /* Regardless, next try for sync transfers. */
3812 build_sync_nego_msg(esp, esp->sync_defp, 15);
3813 esp_dev->sync = 1;
3814 esp->snip = 1;
3815 message_out = EXTENDED_MESSAGE;
3817 } else if (esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
3818 ESPLOG(("esp%d: rejecting modify data ptr msg\n", esp->esp_id));
3819 message_out = MESSAGE_REJECT;
3821 finish:
3822 esp_advance_phase(SCptr, in_the_dark);
3823 return message_out;
3826 static int esp_do_msgindone(struct esp *esp)
3828 struct scsi_cmnd *SCptr = esp->current_SC;
3829 int message_out = 0, it = 0, rval;
3831 rval = skipahead1(esp, SCptr, in_msgin, in_msgindone);
3832 if (rval)
3833 return rval;
3834 if (SCptr->SCp.sent_command != in_status) {
3835 if (!(esp->ireg & ESP_INTR_DC)) {
3836 if (esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
3837 message_out = MSG_PARITY_ERROR;
3838 esp_cmd(esp, ESP_CMD_FLUSH);
3839 } else if (esp->erev != fashme &&
3840 (it = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES)) != 1) {
3841 /* We certainly dropped the ball somewhere. */
3842 message_out = INITIATOR_ERROR;
3843 esp_cmd(esp, ESP_CMD_FLUSH);
3844 } else if (!esp->msgin_len) {
3845 if (esp->erev == fashme)
3846 it = esp->hme_fifo_workaround_buffer[0];
3847 else
3848 it = sbus_readb(esp->eregs + ESP_FDATA);
3849 esp_advance_phase(SCptr, in_msgincont);
3850 } else {
3851 /* it is ok and we want it */
3852 if (esp->erev == fashme)
3853 it = esp->cur_msgin[esp->msgin_ctr] =
3854 esp->hme_fifo_workaround_buffer[0];
3855 else
3856 it = esp->cur_msgin[esp->msgin_ctr] =
3857 sbus_readb(esp->eregs + ESP_FDATA);
3858 esp->msgin_ctr++;
3860 } else {
3861 esp_advance_phase(SCptr, in_the_dark);
3862 return do_work_bus;
3864 } else {
3865 it = esp->cur_msgin[0];
3867 if (!message_out && esp->msgin_len) {
3868 if (esp->msgin_ctr < esp->msgin_len) {
3869 esp_advance_phase(SCptr, in_msgincont);
3870 } else if (esp->msgin_len == 1) {
3871 message_out = check_singlebyte_msg(esp);
3872 } else if (esp->msgin_len == 2) {
3873 if (esp->cur_msgin[0] == EXTENDED_MESSAGE) {
3874 if ((it + 2) >= 15) {
3875 message_out = MESSAGE_REJECT;
3876 } else {
3877 esp->msgin_len = (it + 2);
3878 esp_advance_phase(SCptr, in_msgincont);
3880 } else {
3881 message_out = MESSAGE_REJECT; /* foo on you */
3883 } else {
3884 message_out = check_multibyte_msg(esp);
3887 if (message_out < 0) {
3888 return -message_out;
3889 } else if (message_out) {
3890 if (((message_out != 1) &&
3891 ((message_out < 0x20) || (message_out & 0x80))))
3892 esp->msgout_len = 1;
3893 esp->cur_msgout[0] = message_out;
3894 esp_cmd(esp, ESP_CMD_SATN);
3895 esp_advance_phase(SCptr, in_the_dark);
3896 esp->msgin_len = 0;
3898 esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
3899 esp->sreg &= ~(ESP_STAT_INTR);
3900 if ((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
3901 esp_cmd(esp, ESP_CMD_MOK);
3902 if ((SCptr->SCp.sent_command == in_msgindone) &&
3903 (SCptr->SCp.phase == in_freeing))
3904 return esp_do_freebus(esp);
3905 return do_intr_end;
3908 static int esp_do_cmdbegin(struct esp *esp)
3910 struct scsi_cmnd *SCptr = esp->current_SC;
3912 esp_advance_phase(SCptr, in_cmdend);
3913 if (esp->erev == fashme) {
3914 u32 tmp = sbus_readl(esp->dregs + DMA_CSR);
3915 int i;
3917 for (i = 0; i < esp->esp_scmdleft; i++)
3918 esp->esp_command[i] = *esp->esp_scmdp++;
3919 esp->esp_scmdleft = 0;
3920 esp_cmd(esp, ESP_CMD_FLUSH);
3921 esp_setcount(esp->eregs, i, 1);
3922 esp_cmd(esp, (ESP_CMD_DMA | ESP_CMD_TI));
3923 tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
3924 tmp &= ~(DMA_ST_WRITE);
3925 sbus_writel(i, esp->dregs + DMA_COUNT);
3926 sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
3927 sbus_writel(tmp, esp->dregs + DMA_CSR);
3928 } else {
3929 u8 tmp;
3931 esp_cmd(esp, ESP_CMD_FLUSH);
3932 tmp = *esp->esp_scmdp++;
3933 esp->esp_scmdleft--;
3934 sbus_writeb(tmp, esp->eregs + ESP_FDATA);
3935 esp_cmd(esp, ESP_CMD_TI);
3937 return do_intr_end;
3940 static int esp_do_cmddone(struct esp *esp)
3942 if (esp->erev == fashme)
3943 dma_invalidate(esp);
3944 else
3945 esp_cmd(esp, ESP_CMD_NULL);
3947 if (esp->ireg & ESP_INTR_BSERV) {
3948 esp_advance_phase(esp->current_SC, in_the_dark);
3949 return esp_do_phase_determine(esp);
3952 ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.\n",
3953 esp->esp_id));
3954 return do_reset_bus;
3957 static int esp_do_msgout(struct esp *esp)
3959 esp_cmd(esp, ESP_CMD_FLUSH);
3960 switch (esp->msgout_len) {
3961 case 1:
3962 if (esp->erev == fashme)
3963 hme_fifo_push(esp, &esp->cur_msgout[0], 1);
3964 else
3965 sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
3967 esp_cmd(esp, ESP_CMD_TI);
3968 break;
3970 case 2:
3971 esp->esp_command[0] = esp->cur_msgout[0];
3972 esp->esp_command[1] = esp->cur_msgout[1];
3974 if (esp->erev == fashme) {
3975 hme_fifo_push(esp, &esp->cur_msgout[0], 2);
3976 esp_cmd(esp, ESP_CMD_TI);
3977 } else {
3978 dma_setup(esp, esp->esp_command_dvma, 2, 0);
3979 esp_setcount(esp->eregs, 2, 0);
3980 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
3982 break;
3984 case 4:
3985 esp->esp_command[0] = esp->cur_msgout[0];
3986 esp->esp_command[1] = esp->cur_msgout[1];
3987 esp->esp_command[2] = esp->cur_msgout[2];
3988 esp->esp_command[3] = esp->cur_msgout[3];
3989 esp->snip = 1;
3991 if (esp->erev == fashme) {
3992 hme_fifo_push(esp, &esp->cur_msgout[0], 4);
3993 esp_cmd(esp, ESP_CMD_TI);
3994 } else {
3995 dma_setup(esp, esp->esp_command_dvma, 4, 0);
3996 esp_setcount(esp->eregs, 4, 0);
3997 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
3999 break;
4001 case 5:
4002 esp->esp_command[0] = esp->cur_msgout[0];
4003 esp->esp_command[1] = esp->cur_msgout[1];
4004 esp->esp_command[2] = esp->cur_msgout[2];
4005 esp->esp_command[3] = esp->cur_msgout[3];
4006 esp->esp_command[4] = esp->cur_msgout[4];
4007 esp->snip = 1;
4009 if (esp->erev == fashme) {
4010 hme_fifo_push(esp, &esp->cur_msgout[0], 5);
4011 esp_cmd(esp, ESP_CMD_TI);
4012 } else {
4013 dma_setup(esp, esp->esp_command_dvma, 5, 0);
4014 esp_setcount(esp->eregs, 5, 0);
4015 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
4017 break;
4019 default:
4020 /* whoops */
4021 ESPMISC(("bogus msgout sending NOP\n"));
4022 esp->cur_msgout[0] = NOP;
4024 if (esp->erev == fashme) {
4025 hme_fifo_push(esp, &esp->cur_msgout[0], 1);
4026 } else {
4027 sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
4030 esp->msgout_len = 1;
4031 esp_cmd(esp, ESP_CMD_TI);
4032 break;
4035 esp_advance_phase(esp->current_SC, in_msgoutdone);
4036 return do_intr_end;
4039 static int esp_do_msgoutdone(struct esp *esp)
4041 if (esp->msgout_len > 1) {
4042 /* XXX HME/FAS ATN deassert workaround required,
4043 * XXX no DMA flushing, only possible ESP_CMD_FLUSH
4044 * XXX to kill the fifo.
4046 if (esp->erev != fashme) {
4047 u32 tmp;
4049 while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
4050 udelay(1);
4051 tmp &= ~DMA_ENABLE;
4052 sbus_writel(tmp, esp->dregs + DMA_CSR);
4053 dma_invalidate(esp);
4054 } else {
4055 esp_cmd(esp, ESP_CMD_FLUSH);
4058 if (!(esp->ireg & ESP_INTR_DC)) {
4059 if (esp->erev != fashme)
4060 esp_cmd(esp, ESP_CMD_NULL);
4061 switch (esp->sreg & ESP_STAT_PMASK) {
4062 case ESP_MOP:
4063 /* whoops, parity error */
4064 ESPLOG(("esp%d: still in msgout, parity error assumed\n",
4065 esp->esp_id));
4066 if (esp->msgout_len > 1)
4067 esp_cmd(esp, ESP_CMD_SATN);
4068 esp_advance_phase(esp->current_SC, in_msgout);
4069 return do_work_bus;
4071 case ESP_DIP:
4072 break;
4074 default:
4075 /* Happy Meal fifo is touchy... */
4076 if ((esp->erev != fashme) &&
4077 !fcount(esp) &&
4078 !(((struct esp_device *)esp->current_SC->device->hostdata)->sync_max_offset))
4079 esp_cmd(esp, ESP_CMD_FLUSH);
4080 break;
4083 } else {
4084 ESPLOG(("esp%d: disconnect, resetting bus\n", esp->esp_id));
4085 return do_reset_bus;
4088 /* If we sent out a synchronous negotiation message, update
4089 * our state.
4091 if (esp->cur_msgout[2] == EXTENDED_MESSAGE &&
4092 esp->cur_msgout[4] == EXTENDED_SDTR) {
4093 esp->snip = 1; /* anal retentiveness... */
4096 esp->prevmsgout = esp->cur_msgout[0];
4097 esp->msgout_len = 0;
4098 esp_advance_phase(esp->current_SC, in_the_dark);
4099 return esp_do_phase_determine(esp);
4102 static int esp_bus_unexpected(struct esp *esp)
4104 ESPLOG(("esp%d: command in weird state %2x\n",
4105 esp->esp_id, esp->current_SC->SCp.phase));
4106 return do_reset_bus;
4109 static espfunc_t bus_vector[] = {
4110 esp_do_data_finale,
4111 esp_do_data_finale,
4112 esp_bus_unexpected,
4113 esp_do_msgin,
4114 esp_do_msgincont,
4115 esp_do_msgindone,
4116 esp_do_msgout,
4117 esp_do_msgoutdone,
4118 esp_do_cmdbegin,
4119 esp_do_cmddone,
4120 esp_do_status,
4121 esp_do_freebus,
4122 esp_do_phase_determine,
4123 esp_bus_unexpected,
4124 esp_bus_unexpected,
4125 esp_bus_unexpected,
4128 /* This is the second tier in our dual-level SCSI state machine. */
4129 static int esp_work_bus(struct esp *esp)
4131 struct scsi_cmnd *SCptr = esp->current_SC;
4132 unsigned int phase;
4134 ESPBUS(("esp_work_bus: "));
4135 if (!SCptr) {
4136 ESPBUS(("reconnect\n"));
4137 return esp_do_reconnect(esp);
4139 phase = SCptr->SCp.phase;
4140 if ((phase & 0xf0) == in_phases_mask)
4141 return bus_vector[(phase & 0x0f)](esp);
4142 else if ((phase & 0xf0) == in_slct_mask)
4143 return esp_select_complete(esp);
4144 else
4145 return esp_bus_unexpected(esp);
4148 static espfunc_t isvc_vector[] = {
4150 esp_do_phase_determine,
4151 esp_do_resetbus,
4152 esp_finish_reset,
4153 esp_work_bus
4156 /* Main interrupt handler for an esp adapter. */
4157 static void esp_handle(struct esp *esp)
4159 struct scsi_cmnd *SCptr;
4160 int what_next = do_intr_end;
4162 SCptr = esp->current_SC;
4164 /* Check for errors. */
4165 esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
4166 esp->sreg &= (~ESP_STAT_INTR);
4167 if (esp->erev == fashme) {
4168 esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
4169 esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
4172 if (esp->sreg & (ESP_STAT_SPAM)) {
4173 /* Gross error, could be due to one of:
4175 * - top of fifo overwritten, could be because
4176 * we tried to do a synchronous transfer with
4177 * an offset greater than ESP fifo size
4179 * - top of command register overwritten
4181 * - DMA setup to go in one direction, SCSI
4182 * bus points in the other, whoops
4184 * - weird phase change during asynchronous
4185 * data phase while we are initiator
4187 ESPLOG(("esp%d: Gross error sreg=%2x\n", esp->esp_id, esp->sreg));
4189 /* If a command is live on the bus we cannot safely
4190 * reset the bus, so we'll just let the pieces fall
4191 * where they may. Here we are hoping that the
4192 * target will be able to cleanly go away soon
4193 * so we can safely reset things.
4195 if (!SCptr) {
4196 ESPLOG(("esp%d: No current cmd during gross error, "
4197 "resetting bus\n", esp->esp_id));
4198 what_next = do_reset_bus;
4199 goto state_machine;
4203 if (sbus_readl(esp->dregs + DMA_CSR) & DMA_HNDL_ERROR) {
4204 /* A DMA gate array error. Here we must
4205 * be seeing one of two things. Either the
4206 * virtual to physical address translation
4207 * on the SBUS could not occur, else the
4208 * translation it did get pointed to a bogus
4209 * page. Ho hum...
4211 ESPLOG(("esp%d: DMA error %08x\n", esp->esp_id,
4212 sbus_readl(esp->dregs + DMA_CSR)));
4214 /* DMA gate array itself must be reset to clear the
4215 * error condition.
4217 esp_reset_dma(esp);
4219 what_next = do_reset_bus;
4220 goto state_machine;
4223 esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT); /* Unlatch intr reg */
4225 if (esp->erev == fashme) {
4226 /* This chip is really losing. */
4227 ESPHME(("HME["));
4229 ESPHME(("sreg2=%02x,", esp->sreg2));
4230 /* Must latch fifo before reading the interrupt
4231 * register else garbage ends up in the FIFO
4232 * which confuses the driver utterly.
4234 if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
4235 (esp->sreg2 & ESP_STAT2_F1BYTE)) {
4236 ESPHME(("fifo_workaround]"));
4237 hme_fifo_read(esp);
4238 } else {
4239 ESPHME(("no_fifo_workaround]"));
4243 /* No current cmd is only valid at this point when there are
4244 * commands off the bus or we are trying a reset.
4246 if (!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
4247 /* Panic is safe, since current_SC is null. */
4248 ESPLOG(("esp%d: no command in esp_handle()\n", esp->esp_id));
4249 panic("esp_handle: current_SC == penguin within interrupt!");
4252 if (esp->ireg & (ESP_INTR_IC)) {
4253 /* Illegal command fed to ESP. Outside of obvious
4254 * software bugs that could cause this, there is
4255 * a condition with esp100 where we can confuse the
4256 * ESP into an erroneous illegal command interrupt
4257 * because it does not scrape the FIFO properly
4258 * for reselection. See esp100_reconnect_hwbug()
4259 * to see how we try very hard to avoid this.
4261 ESPLOG(("esp%d: invalid command\n", esp->esp_id));
4263 esp_dump_state(esp);
4265 if (SCptr != NULL) {
4266 /* Devices with very buggy firmware can drop BSY
4267 * during a scatter list interrupt when using sync
4268 * mode transfers. We continue the transfer as
4269 * expected, the target drops the bus, the ESP
4270 * gets confused, and we get a illegal command
4271 * interrupt because the bus is in the disconnected
4272 * state now and ESP_CMD_TI is only allowed when
4273 * a nexus is alive on the bus.
4275 ESPLOG(("esp%d: Forcing async and disabling disconnect for "
4276 "target %d\n", esp->esp_id, SCptr->device->id));
4277 SCptr->device->borken = 1; /* foo on you */
4280 what_next = do_reset_bus;
4281 } else if (!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
4282 if (SCptr) {
4283 unsigned int phase = SCptr->SCp.phase;
4285 if (phase & in_phases_mask) {
4286 what_next = esp_work_bus(esp);
4287 } else if (phase & in_slct_mask) {
4288 what_next = esp_select_complete(esp);
4289 } else {
4290 ESPLOG(("esp%d: interrupt for no good reason...\n",
4291 esp->esp_id));
4292 what_next = do_intr_end;
4294 } else {
4295 ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULL\n",
4296 esp->esp_id));
4297 what_next = do_reset_bus;
4299 } else if (esp->ireg & ESP_INTR_SR) {
4300 ESPLOG(("esp%d: SCSI bus reset interrupt\n", esp->esp_id));
4301 what_next = do_reset_complete;
4302 } else if (esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
4303 ESPLOG(("esp%d: AIEEE we have been selected by another initiator!\n",
4304 esp->esp_id));
4305 what_next = do_reset_bus;
4306 } else if (esp->ireg & ESP_INTR_RSEL) {
4307 if (SCptr == NULL) {
4308 /* This is ok. */
4309 what_next = esp_do_reconnect(esp);
4310 } else if (SCptr->SCp.phase & in_slct_mask) {
4311 /* Only selection code knows how to clean
4312 * up properly.
4314 ESPDISC(("Reselected during selection attempt\n"));
4315 what_next = esp_select_complete(esp);
4316 } else {
4317 ESPLOG(("esp%d: Reselected while bus is busy\n",
4318 esp->esp_id));
4319 what_next = do_reset_bus;
4323 /* This is tier-one in our dual level SCSI state machine. */
4324 state_machine:
4325 while (what_next != do_intr_end) {
4326 if (what_next >= do_phase_determine &&
4327 what_next < do_intr_end) {
4328 what_next = isvc_vector[what_next](esp);
4329 } else {
4330 /* state is completely lost ;-( */
4331 ESPLOG(("esp%d: interrupt engine loses state, resetting bus\n",
4332 esp->esp_id));
4333 what_next = do_reset_bus;
4338 /* Service only the ESP described by dev_id. */
4339 static irqreturn_t esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
4341 struct esp *esp = dev_id;
4342 unsigned long flags;
4344 spin_lock_irqsave(esp->ehost->host_lock, flags);
4345 if (ESP_IRQ_P(esp->dregs)) {
4346 ESP_INTSOFF(esp->dregs);
4348 ESPIRQ(("I[%d:%d](", smp_processor_id(), esp->esp_id));
4349 esp_handle(esp);
4350 ESPIRQ((")"));
4352 ESP_INTSON(esp->dregs);
4354 spin_unlock_irqrestore(esp->ehost->host_lock, flags);
4356 return IRQ_HANDLED;
4359 static int esp_slave_alloc(struct scsi_device *SDptr)
4361 struct esp_device *esp_dev =
4362 kmalloc(sizeof(struct esp_device), GFP_ATOMIC);
4364 if (!esp_dev)
4365 return -ENOMEM;
4366 memset(esp_dev, 0, sizeof(struct esp_device));
4367 SDptr->hostdata = esp_dev;
4368 return 0;
4371 static void esp_slave_destroy(struct scsi_device *SDptr)
4373 struct esp *esp = (struct esp *) SDptr->host->hostdata;
4375 esp->targets_present &= ~(1 << SDptr->id);
4376 kfree(SDptr->hostdata);
4377 SDptr->hostdata = NULL;
4380 static struct scsi_host_template driver_template = {
4381 .proc_name = "esp",
4382 .proc_info = esp_proc_info,
4383 .name = "Sun ESP 100/100a/200",
4384 .detect = esp_detect,
4385 .slave_alloc = esp_slave_alloc,
4386 .slave_destroy = esp_slave_destroy,
4387 .release = esp_release,
4388 .info = esp_info,
4389 .queuecommand = esp_queue,
4390 .eh_abort_handler = esp_abort,
4391 .eh_bus_reset_handler = esp_reset,
4392 .can_queue = 7,
4393 .this_id = 7,
4394 .sg_tablesize = SG_ALL,
4395 .cmd_per_lun = 1,
4396 .use_clustering = ENABLE_CLUSTERING,
4399 #include "scsi_module.c"
4401 MODULE_LICENSE("GPL");