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)
9 * 1) Maybe disable parity checking in config register one for SCSI1
10 * targets. (Gilmore says parity error on the SBus can lock up
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>
35 #include <asm/system.h>
36 #include <asm/ptrace.h>
37 #include <asm/pgtable.h>
38 #include <asm/oplib.h>
43 #include <asm/machines.h>
44 #include <asm/idprom.h>
47 #include <linux/module.h>
50 /* #define DEBUG_ESP_HME */
51 /* #define DEBUG_ESP_DATA */
52 /* #define DEBUG_ESP_QUEUE */
53 /* #define DEBUG_ESP_DISCONNECT */
54 /* #define DEBUG_ESP_STATUS */
55 /* #define DEBUG_ESP_PHASES */
56 /* #define DEBUG_ESP_WORKBUS */
57 /* #define DEBUG_STATE_MACHINE */
58 /* #define DEBUG_ESP_CMDS */
59 /* #define DEBUG_ESP_IRQS */
60 /* #define DEBUG_SDTR */
61 /* #define DEBUG_ESP_SG */
63 /* Use the following to sprinkle debugging messages in a way which
64 * suits you if combinations of the above become too verbose when
65 * trying to track down a specific problem.
67 /* #define DEBUG_ESP_MISC */
69 #if defined(DEBUG_ESP)
70 #define ESPLOG(foo) printk foo
73 #endif /* (DEBUG_ESP) */
75 #if defined(DEBUG_ESP_HME)
76 #define ESPHME(foo) printk foo
81 #if defined(DEBUG_ESP_DATA)
82 #define ESPDATA(foo) printk foo
87 #if defined(DEBUG_ESP_QUEUE)
88 #define ESPQUEUE(foo) printk foo
93 #if defined(DEBUG_ESP_DISCONNECT)
94 #define ESPDISC(foo) printk foo
99 #if defined(DEBUG_ESP_STATUS)
100 #define ESPSTAT(foo) printk foo
105 #if defined(DEBUG_ESP_PHASES)
106 #define ESPPHASE(foo) printk foo
108 #define ESPPHASE(foo)
111 #if defined(DEBUG_ESP_WORKBUS)
112 #define ESPBUS(foo) printk foo
117 #if defined(DEBUG_ESP_IRQS)
118 #define ESPIRQ(foo) printk foo
123 #if defined(DEBUG_SDTR)
124 #define ESPSDTR(foo) printk foo
129 #if defined(DEBUG_ESP_MISC)
130 #define ESPMISC(foo) printk foo
135 /* Command phase enumeration. */
137 not_issued = 0x00, /* Still in the issue_SC queue. */
139 /* Various forms of selecting a target. */
140 #define in_slct_mask 0x10
141 in_slct_norm = 0x10, /* ESP is arbitrating, normal selection */
142 in_slct_stop = 0x11, /* ESP will select, then stop with IRQ */
143 in_slct_msg = 0x12, /* select, then send a message */
144 in_slct_tag = 0x13, /* select and send tagged queue msg */
145 in_slct_sneg = 0x14, /* select and acquire sync capabilities */
147 /* Any post selection activity. */
148 #define in_phases_mask 0x20
149 in_datain = 0x20, /* Data is transferring from the bus */
150 in_dataout = 0x21, /* Data is transferring to the bus */
151 in_data_done = 0x22, /* Last DMA data operation done (maybe) */
152 in_msgin = 0x23, /* Eating message from target */
153 in_msgincont = 0x24, /* Eating more msg bytes from target */
154 in_msgindone = 0x25, /* Decide what to do with what we got */
155 in_msgout = 0x26, /* Sending message to target */
156 in_msgoutdone = 0x27, /* Done sending msg out */
157 in_cmdbegin = 0x28, /* Sending cmd after abnormal selection */
158 in_cmdend = 0x29, /* Done sending slow cmd */
159 in_status = 0x2a, /* Was in status phase, finishing cmd */
160 in_freeing = 0x2b, /* freeing the bus for cmd cmplt or disc */
161 in_the_dark = 0x2c, /* Don't know what bus phase we are in */
163 /* Special states, ie. not normal bus transitions... */
164 #define in_spec_mask 0x80
165 in_abortone = 0x80, /* Aborting one command currently */
166 in_abortall = 0x81, /* Blowing away all commands we have */
167 in_resetdev = 0x82, /* SCSI target reset in progress */
168 in_resetbus = 0x83, /* SCSI bus reset in progress */
169 in_tgterror = 0x84, /* Target did something stupid */
173 /* Zero has special meaning, see skipahead[12]. */
176 /*1*/ do_phase_determine,
178 /*3*/ do_reset_complete,
183 /* The master ring of all esp hosts we are managing in this driver. */
184 static struct esp *espchain;
185 static spinlock_t espchain_lock = SPIN_LOCK_UNLOCKED;
186 static int esps_running = 0;
188 /* Forward declarations. */
189 static irqreturn_t esp_intr(int irq, void *dev_id, struct pt_regs *pregs);
191 /* Debugging routines */
192 struct esp_cmdstrings {
195 } esp_cmd_strings[] = {
197 { ESP_CMD_NULL, "ESP_NOP", },
198 { ESP_CMD_FLUSH, "FIFO_FLUSH", },
199 { ESP_CMD_RC, "RSTESP", },
200 { ESP_CMD_RS, "RSTSCSI", },
201 /* Disconnected State Group */
202 { ESP_CMD_RSEL, "RESLCTSEQ", },
203 { ESP_CMD_SEL, "SLCTNATN", },
204 { ESP_CMD_SELA, "SLCTATN", },
205 { ESP_CMD_SELAS, "SLCTATNSTOP", },
206 { ESP_CMD_ESEL, "ENSLCTRESEL", },
207 { ESP_CMD_DSEL, "DISSELRESEL", },
208 { ESP_CMD_SA3, "SLCTATN3", },
209 { ESP_CMD_RSEL3, "RESLCTSEQ", },
210 /* Target State Group */
211 { ESP_CMD_SMSG, "SNDMSG", },
212 { ESP_CMD_SSTAT, "SNDSTATUS", },
213 { ESP_CMD_SDATA, "SNDDATA", },
214 { ESP_CMD_DSEQ, "DISCSEQ", },
215 { ESP_CMD_TSEQ, "TERMSEQ", },
216 { ESP_CMD_TCCSEQ, "TRGTCMDCOMPSEQ", },
217 { ESP_CMD_DCNCT, "DISC", },
218 { ESP_CMD_RMSG, "RCVMSG", },
219 { ESP_CMD_RCMD, "RCVCMD", },
220 { ESP_CMD_RDATA, "RCVDATA", },
221 { ESP_CMD_RCSEQ, "RCVCMDSEQ", },
222 /* Initiator State Group */
223 { ESP_CMD_TI, "TRANSINFO", },
224 { ESP_CMD_ICCSEQ, "INICMDSEQCOMP", },
225 { ESP_CMD_MOK, "MSGACCEPTED", },
226 { ESP_CMD_TPAD, "TPAD", },
227 { ESP_CMD_SATN, "SATN", },
228 { ESP_CMD_RATN, "RATN", },
230 #define NUM_ESP_COMMANDS ((sizeof(esp_cmd_strings)) / (sizeof(struct esp_cmdstrings)))
232 /* Print textual representation of an ESP command */
233 static inline void esp_print_cmd(u8 espcmd)
235 u8 dma_bit = espcmd & ESP_CMD_DMA;
239 for (i = 0; i < NUM_ESP_COMMANDS; i++)
240 if (esp_cmd_strings[i].cmdchar == espcmd)
242 if (i == NUM_ESP_COMMANDS)
243 printk("ESP_Unknown");
245 printk("%s%s", esp_cmd_strings[i].text,
246 ((dma_bit) ? "+DMA" : ""));
249 /* Print the status register's value */
250 static inline void esp_print_statreg(u8 statreg)
255 phase = statreg & ESP_STAT_PMASK;
256 printk("%s,", (phase == ESP_DOP ? "DATA-OUT" :
257 (phase == ESP_DIP ? "DATA-IN" :
258 (phase == ESP_CMDP ? "COMMAND" :
259 (phase == ESP_STATP ? "STATUS" :
260 (phase == ESP_MOP ? "MSG-OUT" :
261 (phase == ESP_MIP ? "MSG_IN" :
263 if (statreg & ESP_STAT_TDONE)
264 printk("TRANS_DONE,");
265 if (statreg & ESP_STAT_TCNT)
266 printk("TCOUNT_ZERO,");
267 if (statreg & ESP_STAT_PERR)
269 if (statreg & ESP_STAT_SPAM)
271 if (statreg & ESP_STAT_INTR)
276 /* Print the interrupt register's value */
277 static inline void esp_print_ireg(u8 intreg)
280 if (intreg & ESP_INTR_S)
281 printk("SLCT_NATN ");
282 if (intreg & ESP_INTR_SATN)
284 if (intreg & ESP_INTR_RSEL)
286 if (intreg & ESP_INTR_FDONE)
288 if (intreg & ESP_INTR_BSERV)
290 if (intreg & ESP_INTR_DC)
292 if (intreg & ESP_INTR_IC)
294 if (intreg & ESP_INTR_SR)
295 printk("SCSI_BUS_RESET ");
299 /* Print the sequence step registers contents */
300 static inline void esp_print_seqreg(u8 stepreg)
302 stepreg &= ESP_STEP_VBITS;
304 (stepreg == ESP_STEP_ASEL ? "SLCT_ARB_CMPLT" :
305 (stepreg == ESP_STEP_SID ? "1BYTE_MSG_SENT" :
306 (stepreg == ESP_STEP_NCMD ? "NOT_IN_CMD_PHASE" :
307 (stepreg == ESP_STEP_PPC ? "CMD_BYTES_LOST" :
308 (stepreg == ESP_STEP_FINI4 ? "CMD_SENT_OK" :
312 static char *phase_string(int phase)
368 #ifdef DEBUG_STATE_MACHINE
369 static inline void esp_advance_phase(Scsi_Cmnd *s, int newphase)
371 ESPLOG(("<%s>", phase_string(newphase)));
372 s->SCp.sent_command = s->SCp.phase;
373 s->SCp.phase = newphase;
376 #define esp_advance_phase(__s, __newphase) \
377 (__s)->SCp.sent_command = (__s)->SCp.phase; \
378 (__s)->SCp.phase = (__newphase);
381 #ifdef DEBUG_ESP_CMDS
382 static inline void esp_cmd(struct esp *esp, u8 cmd)
384 esp->espcmdlog[esp->espcmdent] = cmd;
385 esp->espcmdent = (esp->espcmdent + 1) & 31;
386 sbus_writeb(cmd, esp->eregs + ESP_CMD);
389 #define esp_cmd(__esp, __cmd) \
390 sbus_writeb((__cmd), ((__esp)->eregs) + ESP_CMD)
393 #define ESP_INTSOFF(__dregs) \
394 sbus_writel(sbus_readl((__dregs)+DMA_CSR)&~(DMA_INT_ENAB), (__dregs)+DMA_CSR)
395 #define ESP_INTSON(__dregs) \
396 sbus_writel(sbus_readl((__dregs)+DMA_CSR)|DMA_INT_ENAB, (__dregs)+DMA_CSR)
397 #define ESP_IRQ_P(__dregs) \
398 (sbus_readl((__dregs)+DMA_CSR) & (DMA_HNDL_INTR|DMA_HNDL_ERROR))
400 /* How we use the various Linux SCSI data structures for operation.
404 * We keep track of the synchronous capabilities of a target
405 * in the device member, using sync_min_period and
406 * sync_max_offset. These are the values we directly write
407 * into the ESP registers while running a command. If offset
408 * is zero the ESP will use asynchronous transfers.
409 * If the borken flag is set we assume we shouldn't even bother
410 * trying to negotiate for synchronous transfer as this target
411 * is really stupid. If we notice the target is dropping the
412 * bus, and we have been allowing it to disconnect, we clear
413 * the disconnect flag.
417 /* Manipulation of the ESP command queues. Thanks to the aha152x driver
418 * and its author, Juergen E. Fischer, for the methods used here.
419 * Note that these are per-ESP queues, not global queues like
420 * the aha152x driver uses.
422 static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
426 new_SC->host_scribble = (unsigned char *) NULL;
430 for (end=*SC;end->host_scribble;end=(Scsi_Cmnd *)end->host_scribble)
432 end->host_scribble = (unsigned char *) new_SC;
436 static inline void prepend_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
438 new_SC->host_scribble = (unsigned char *) *SC;
442 static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd **SC)
447 *SC = (Scsi_Cmnd *) (*SC)->host_scribble;
451 static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, int target, int lun)
453 Scsi_Cmnd *ptr, *prev;
455 for (ptr = *SC, prev = NULL;
456 ptr && ((ptr->device->id != target) || (ptr->device->lun != lun));
457 prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble)
461 prev->host_scribble=ptr->host_scribble;
463 *SC=(Scsi_Cmnd *)ptr->host_scribble;
468 /* Resetting various pieces of the ESP scsi driver chipset/buses. */
469 static void esp_reset_dma(struct esp *esp)
471 int can_do_burst16, can_do_burst32, can_do_burst64;
475 can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
476 can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
479 if (sbus_can_dma_64bit(esp->sdev))
481 if (sbus_can_burst64(esp->sdev))
482 can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
484 /* Punt the DVMA into a known state. */
485 if (esp->dma->revision != dvmahme) {
486 tmp = sbus_readl(esp->dregs + DMA_CSR);
487 sbus_writel(tmp | DMA_RST_SCSI, esp->dregs + DMA_CSR);
488 sbus_writel(tmp & ~DMA_RST_SCSI, esp->dregs + DMA_CSR);
490 switch (esp->dma->revision) {
492 /* This is the HME DVMA gate array. */
494 sbus_writel(DMA_RESET_FAS366, esp->dregs + DMA_CSR);
495 sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
497 esp->prev_hme_dmacsr = (DMA_PARITY_OFF|DMA_2CLKS|DMA_SCSI_DISAB|DMA_INT_ENAB);
498 esp->prev_hme_dmacsr &= ~(DMA_ENABLE|DMA_ST_WRITE|DMA_BRST_SZ);
501 esp->prev_hme_dmacsr |= DMA_BRST64;
502 else if (can_do_burst32)
503 esp->prev_hme_dmacsr |= DMA_BRST32;
506 esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
507 sbus_set_sbus64(esp->sdev, esp->bursts);
510 /* This chip is horrible. */
511 while (sbus_readl(esp->dregs + DMA_CSR) & DMA_PEND_READ)
514 sbus_writel(0, esp->dregs + DMA_CSR);
515 sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
517 /* This is necessary to avoid having the SCSI channel
518 * engine lock up on us.
520 sbus_writel(0, esp->dregs + DMA_ADDR);
524 /* This is the gate array found in the sun4m
525 * NCR SBUS I/O subsystem.
527 if (esp->erev != esp100) {
528 tmp = sbus_readl(esp->dregs + DMA_CSR);
529 sbus_writel(tmp | DMA_3CLKS, esp->dregs + DMA_CSR);
533 tmp = sbus_readl(esp->dregs + DMA_CSR);
536 if (can_do_burst32) {
540 sbus_writel(tmp, esp->dregs + DMA_CSR);
543 /* This is the DMA unit found on SCSI/Ether cards. */
544 tmp = sbus_readl(esp->dregs + DMA_CSR);
545 tmp |= DMA_ADD_ENABLE;
546 tmp &= ~DMA_BCNT_ENAB;
547 if (!can_do_burst32 && can_do_burst16) {
548 tmp |= DMA_ESC_BURST;
550 tmp &= ~(DMA_ESC_BURST);
552 sbus_writel(tmp, esp->dregs + DMA_CSR);
557 ESP_INTSON(esp->dregs);
560 /* Reset the ESP chip, _not_ the SCSI bus. */
561 static void __init esp_reset_esp(struct esp *esp)
563 u8 family_code, version;
566 /* Now reset the ESP chip */
567 esp_cmd(esp, ESP_CMD_RC);
568 esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
569 esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
571 /* Reload the configuration registers */
572 sbus_writeb(esp->cfact, esp->eregs + ESP_CFACT);
574 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
576 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
577 sbus_writeb(esp->neg_defp, esp->eregs + ESP_TIMEO);
579 /* This is the only point at which it is reliable to read
580 * the ID-code for a fast ESP chip variants.
582 esp->max_period = ((35 * esp->ccycle) / 1000);
583 if (esp->erev == fast) {
584 version = sbus_readb(esp->eregs + ESP_UID);
585 family_code = (version & 0xf8) >> 3;
586 if (family_code == 0x02)
588 else if (family_code == 0x0a)
589 esp->erev = fashme; /* Version is usually '5'. */
592 ESPMISC(("esp%d: FAST chip is %s (family=%d, version=%d)\n",
594 (esp->erev == fas236) ? "fas236" :
595 ((esp->erev == fas100a) ? "fas100a" :
596 "fasHME"), family_code, (version & 7)));
598 esp->min_period = ((4 * esp->ccycle) / 1000);
600 esp->min_period = ((5 * esp->ccycle) / 1000);
602 esp->max_period = (esp->max_period + 3)>>2;
603 esp->min_period = (esp->min_period + 3)>>2;
605 sbus_writeb(esp->config1, esp->eregs + ESP_CFG1);
611 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
615 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
616 esp->prev_cfg3 = esp->config3[0];
617 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
620 esp->config2 |= (ESP_CONFIG2_HME32 | ESP_CONFIG2_HMEFENAB);
623 /* Fast 236 or HME */
624 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
625 for (i = 0; i < 16; i++) {
626 if (esp->erev == fashme) {
629 cfg3 = ESP_CONFIG3_FCLOCK | ESP_CONFIG3_OBPUSH;
630 if (esp->scsi_id >= 8)
631 cfg3 |= ESP_CONFIG3_IDBIT3;
632 esp->config3[i] |= cfg3;
634 esp->config3[i] |= ESP_CONFIG3_FCLK;
637 esp->prev_cfg3 = esp->config3[0];
638 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
639 if (esp->erev == fashme) {
650 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
651 for (i = 0; i < 16; i++)
652 esp->config3[i] |= ESP_CONFIG3_FCLOCK;
653 esp->prev_cfg3 = esp->config3[0];
654 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
658 panic("esp: what could it be... I wonder...");
662 /* Eat any bitrot in the chip */
663 sbus_readb(esp->eregs + ESP_INTRPT);
667 /* This places the ESP into a known state at boot time. */
668 static void __init esp_bootup_reset(struct esp *esp)
678 /* Reset the SCSI bus, but tell ESP not to generate an irq */
679 tmp = sbus_readb(esp->eregs + ESP_CFG1);
680 tmp |= ESP_CONFIG1_SRRDISAB;
681 sbus_writeb(tmp, esp->eregs + ESP_CFG1);
683 esp_cmd(esp, ESP_CMD_RS);
686 sbus_writeb(esp->config1, esp->eregs + ESP_CFG1);
688 /* Eat any bitrot in the chip and we are done... */
689 sbus_readb(esp->eregs + ESP_INTRPT);
692 static void esp_chain_add(struct esp *esp)
694 spin_lock_irq(&espchain_lock);
696 struct esp *elink = espchain;
704 spin_unlock_irq(&espchain_lock);
707 static void esp_chain_del(struct esp *esp)
709 spin_lock_irq(&espchain_lock);
710 if (espchain == esp) {
711 espchain = esp->next;
713 struct esp *elink = espchain;
714 while (elink->next != esp)
716 elink->next = esp->next;
719 spin_unlock_irq(&espchain_lock);
722 static int __init esp_find_dvma(struct esp *esp, struct sbus_dev *dma_sdev)
724 struct sbus_dev *sdev = esp->sdev;
725 struct sbus_dma *dma;
727 if (dma_sdev != NULL) {
729 if (dma->sdev == dma_sdev)
734 /* If allocated already, can't use it. */
738 if (dma->sdev == NULL)
741 /* If bus + slot are the same and it has the
742 * correct OBP name, it's ours.
744 if (sdev->bus == dma->sdev->bus &&
745 sdev->slot == dma->sdev->slot &&
746 (!strcmp(dma->sdev->prom_name, "dma") ||
747 !strcmp(dma->sdev->prom_name, "espdma")))
752 /* If we don't know how to handle the dvma,
753 * do not use this device.
756 printk("Cannot find dvma for ESP%d's SCSI\n", esp->esp_id);
759 if (dma->allocated) {
760 printk("esp%d: can't use my espdma\n", esp->esp_id);
765 esp->dregs = dma->regs;
770 static int __init esp_map_regs(struct esp *esp, int hme)
772 struct sbus_dev *sdev = esp->sdev;
773 struct resource *res;
775 /* On HME, two reg sets exist, first is DVMA,
776 * second is ESP registers.
779 res = &sdev->resource[1];
781 res = &sdev->resource[0];
783 esp->eregs = sbus_ioremap(res, 0, ESP_REG_SIZE, "ESP Registers");
790 static int __init esp_map_cmdarea(struct esp *esp)
792 struct sbus_dev *sdev = esp->sdev;
794 esp->esp_command = sbus_alloc_consistent(sdev, 16,
795 &esp->esp_command_dvma);
796 if (esp->esp_command == NULL ||
797 esp->esp_command_dvma == 0)
802 static int __init esp_register_irq(struct esp *esp)
804 esp->ehost->irq = esp->irq = esp->sdev->irqs[0];
806 /* We used to try various overly-clever things to
807 * reduce the interrupt processing overhead on
808 * sun4c/sun4m when multiple ESP's shared the
809 * same IRQ. It was too complex and messy to
812 if (request_irq(esp->ehost->irq, esp_intr,
813 SA_SHIRQ, "ESP SCSI", esp)) {
814 printk("esp%d: Cannot acquire irq line\n",
819 printk("esp%d: IRQ %s ", esp->esp_id,
820 __irq_itoa(esp->ehost->irq));
825 static void __init esp_get_scsi_id(struct esp *esp)
827 struct sbus_dev *sdev = esp->sdev;
829 esp->scsi_id = prom_getintdefault(esp->prom_node,
832 if (esp->scsi_id == -1)
833 esp->scsi_id = prom_getintdefault(esp->prom_node,
836 if (esp->scsi_id == -1)
837 esp->scsi_id = (sdev->bus == NULL) ? 7 :
838 prom_getintdefault(sdev->bus->prom_node,
841 esp->ehost->this_id = esp->scsi_id;
842 esp->scsi_id_mask = (1 << esp->scsi_id);
846 static void __init esp_get_clock_params(struct esp *esp)
848 struct sbus_dev *sdev = esp->sdev;
849 int prom_node = esp->prom_node;
854 if (sdev != NULL && sdev->bus != NULL)
855 sbus_prom_node = sdev->bus->prom_node;
859 /* This is getting messy but it has to be done
860 * correctly or else you get weird behavior all
861 * over the place. We are trying to basically
862 * figure out three pieces of information.
864 * a) Clock Conversion Factor
866 * This is a representation of the input
867 * crystal clock frequency going into the
868 * ESP on this machine. Any operation whose
869 * timing is longer than 400ns depends on this
870 * value being correct. For example, you'll
871 * get blips for arbitration/selection during
872 * high load or with multiple targets if this
873 * is not set correctly.
875 * b) Selection Time-Out
877 * The ESP isn't very bright and will arbitrate
878 * for the bus and try to select a target
879 * forever if you let it. This value tells
880 * the ESP when it has taken too long to
881 * negotiate and that it should interrupt
882 * the CPU so we can see what happened.
883 * The value is computed as follows (from
884 * NCR/Symbios chip docs).
886 * (Time Out Period) * (Input Clock)
887 * STO = ----------------------------------
888 * (8192) * (Clock Conversion Factor)
890 * You usually want the time out period to be
891 * around 250ms, I think we'll set it a little
892 * bit higher to account for fully loaded SCSI
893 * bus's and slow devices that don't respond so
894 * quickly to selection attempts. (yeah, I know
895 * this is out of spec. but there is a lot of
896 * buggy pieces of firmware out there so bite me)
898 * c) Imperical constants for synchronous offset
899 * and transfer period register values
901 * This entails the smallest and largest sync
902 * period we could ever handle on this ESP.
905 fmhz = prom_getintdefault(prom_node, "clock-frequency", -1);
907 fmhz = (!sbus_prom_node) ? 0 :
908 prom_getintdefault(sbus_prom_node, "clock-frequency", -1);
910 if (fmhz <= (5000000))
913 ccf = (((5000000 - 1) + (fmhz))/(5000000));
915 if (!ccf || ccf > 8) {
916 /* If we can't find anything reasonable,
917 * just assume 20MHZ. This is the clock
918 * frequency of the older sun4c's where I've
919 * been unable to find the clock-frequency
920 * PROM property. All other machines provide
921 * useful values it seems.
927 if (ccf == (ESP_CCF_F7 + 1))
928 esp->cfact = ESP_CCF_F0;
929 else if (ccf == ESP_CCF_NEVER)
930 esp->cfact = ESP_CCF_F2;
933 esp->raw_cfact = ccf;
936 esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz);
937 esp->ctick = ESP_TICK(ccf, esp->ccycle);
938 esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf);
939 esp->sync_defp = SYNC_DEFP_SLOW;
941 printk("SCSI ID %d Clk %dMHz CCYC=%d CCF=%d TOut %d ",
942 esp->scsi_id, (fmhz / 1000000),
943 (int)esp->ccycle, (int)ccf, (int) esp->neg_defp);
946 static void __init esp_get_bursts(struct esp *esp, struct sbus_dev *dma)
948 struct sbus_dev *sdev = esp->sdev;
951 bursts = prom_getintdefault(esp->prom_node, "burst-sizes", 0xff);
954 u8 tmp = prom_getintdefault(dma->prom_node,
955 "burst-sizes", 0xff);
961 u8 tmp = prom_getintdefault(sdev->bus->prom_node,
962 "burst-sizes", 0xff);
967 if (bursts == 0xff ||
968 (bursts & DMA_BURST16) == 0 ||
969 (bursts & DMA_BURST32) == 0)
970 bursts = (DMA_BURST32 - 1);
972 esp->bursts = bursts;
975 static void __init esp_get_revision(struct esp *esp)
979 esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
980 esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
981 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
983 tmp = sbus_readb(esp->eregs + ESP_CFG2);
984 tmp &= ~ESP_CONFIG2_MAGIC;
985 if (tmp != (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
986 /* If what we write to cfg2 does not come back, cfg2
987 * is not implemented, therefore this must be a plain
991 printk("NCR53C90(esp100)\n");
994 esp->prev_cfg3 = esp->config3[0] = 5;
995 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
996 sbus_writeb(0, esp->eregs + ESP_CFG3);
997 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
999 tmp = sbus_readb(esp->eregs + ESP_CFG3);
1001 /* The cfg2 register is implemented, however
1002 * cfg3 is not, must be esp100a.
1004 esp->erev = esp100a;
1005 printk("NCR53C90A(esp100a)\n");
1009 for (target = 0; target < 16; target++)
1010 esp->config3[target] = 0;
1012 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
1014 /* All of cfg{1,2,3} implemented, must be one of
1015 * the fas variants, figure out which one.
1017 if (esp->raw_cfact > ESP_CCF_F5) {
1019 esp->sync_defp = SYNC_DEFP_FAST;
1020 printk("NCR53C9XF(espfast)\n");
1023 printk("NCR53C9x(esp236)\n");
1026 sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
1031 static void __init esp_init_swstate(struct esp *esp)
1035 /* Command queues... */
1036 esp->current_SC = NULL;
1037 esp->disconnected_SC = NULL;
1038 esp->issue_SC = NULL;
1040 /* Target and current command state... */
1041 esp->targets_present = 0;
1042 esp->resetting_bus = 0;
1045 init_waitqueue_head(&esp->reset_queue);
1048 for(i = 0; i < 32; i++)
1049 esp->espcmdlog[i] = 0;
1052 /* MSG phase state... */
1053 for(i = 0; i < 16; i++) {
1054 esp->cur_msgout[i] = 0;
1055 esp->cur_msgin[i] = 0;
1057 esp->prevmsgout = esp->prevmsgin = 0;
1058 esp->msgout_len = esp->msgin_len = 0;
1060 /* Clear the one behind caches to hold unmatchable values. */
1061 esp->prev_soff = esp->prev_stp = esp->prev_cfg3 = 0xff;
1062 esp->prev_hme_dmacsr = 0xffffffff;
1065 static int __init detect_one_esp(Scsi_Host_Template *tpnt, struct sbus_dev *esp_dev,
1066 struct sbus_dev *espdma, struct sbus_bus *sbus,
1069 struct Scsi_Host *esp_host = scsi_register(tpnt, sizeof(struct esp));
1073 printk("ESP: Cannot register SCSI host\n");
1077 esp_host->max_id = 16;
1078 esp = (struct esp *) esp_host->hostdata;
1079 esp->ehost = esp_host;
1080 esp->sdev = esp_dev;
1082 esp->prom_node = esp_dev->prom_node;
1083 prom_getstring(esp->prom_node, "name", esp->prom_name,
1084 sizeof(esp->prom_name));
1087 if (esp_find_dvma(esp, espdma) < 0)
1089 if (esp_map_regs(esp, hme) < 0) {
1090 printk("ESP registers unmappable");
1091 goto fail_dvma_release;
1093 if (esp_map_cmdarea(esp) < 0) {
1094 printk("ESP DVMA transport area unmappable");
1095 goto fail_unmap_regs;
1097 if (esp_register_irq(esp) < 0)
1098 goto fail_unmap_cmdarea;
1100 esp_get_scsi_id(esp);
1102 esp->diff = prom_getbool(esp->prom_node, "differential");
1104 printk("Differential ");
1106 esp_get_clock_params(esp);
1107 esp_get_bursts(esp, espdma);
1108 esp_get_revision(esp);
1109 esp_init_swstate(esp);
1111 esp_bootup_reset(esp);
1116 sbus_free_consistent(esp->sdev, 16,
1117 (void *) esp->esp_command,
1118 esp->esp_command_dvma);
1121 sbus_iounmap(esp->eregs, ESP_REG_SIZE);
1124 esp->dma->allocated = 0;
1128 scsi_unregister(esp_host);
1132 /* Detecting ESP chips on the machine. This is the simple and easy
1138 #include <asm/sun4paddr.h>
1140 static int __init esp_detect(Scsi_Host_Template *tpnt)
1142 static struct sbus_dev esp_dev;
1143 int esps_in_use = 0;
1147 if (sun4_esp_physaddr) {
1148 memset (&esp_dev, 0, sizeof(esp_dev));
1149 esp_dev.reg_addrs[0].phys_addr = sun4_esp_physaddr;
1150 esp_dev.irqs[0] = 4;
1151 esp_dev.resource[0].start = sun4_esp_physaddr;
1152 esp_dev.resource[0].end = sun4_esp_physaddr + ESP_REG_SIZE - 1;
1153 esp_dev.resource[0].flags = IORESOURCE_IO;
1155 if (!detect_one_esp(tpnt, &esp_dev, NULL, NULL, 0, 0))
1157 printk("ESP: Total of 1 ESP hosts found, %d actually in use.\n", esps_in_use);
1158 esps_running = esps_in_use;
1163 #else /* !CONFIG_SUN4 */
1165 static int __init esp_detect(Scsi_Host_Template *tpnt)
1167 struct sbus_bus *sbus;
1168 struct sbus_dev *esp_dev, *sbdev_iter;
1169 int nesps = 0, esps_in_use = 0;
1176 panic("No SBUS in esp_detect()");
1179 for_each_sbus(sbus) {
1180 for_each_sbusdev(sbdev_iter, sbus) {
1181 struct sbus_dev *espdma = NULL;
1184 /* Is it an esp sbus device? */
1185 esp_dev = sbdev_iter;
1186 if (strcmp(esp_dev->prom_name, "esp") &&
1187 strcmp(esp_dev->prom_name, "SUNW,esp")) {
1188 if (!strcmp(esp_dev->prom_name, "SUNW,fas")) {
1192 if (!esp_dev->child ||
1193 (strcmp(esp_dev->prom_name, "espdma") &&
1194 strcmp(esp_dev->prom_name, "dma")))
1195 continue; /* nope... */
1197 esp_dev = esp_dev->child;
1198 if (strcmp(esp_dev->prom_name, "esp") &&
1199 strcmp(esp_dev->prom_name, "SUNW,esp"))
1200 continue; /* how can this happen? */
1204 if (detect_one_esp(tpnt, esp_dev, espdma, sbus, nesps++, hme) < 0)
1208 } /* for each sbusdev */
1209 } /* for each sbus */
1210 printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps,
1212 esps_running = esps_in_use;
1216 #endif /* !CONFIG_SUN4 */
1220 static int esp_release(struct Scsi_Host *host)
1222 struct esp *esp = (struct esp *) host->hostdata;
1224 ESP_INTSOFF(esp->dregs);
1230 free_irq(esp->ehost->irq, esp);
1231 sbus_free_consistent(esp->sdev, 16,
1232 (void *) esp->esp_command, esp->esp_command_dvma);
1233 sbus_iounmap(esp->eregs, ESP_REG_SIZE);
1234 esp->dma->allocated = 0;
1240 /* The info function will return whatever useful
1241 * information the developer sees fit. If not provided, then
1242 * the name field will be used instead.
1244 static const char *esp_info(struct Scsi_Host *host)
1248 esp = (struct esp *) host->hostdata;
1249 switch (esp->erev) {
1251 return "Sparc ESP100 (NCR53C90)";
1253 return "Sparc ESP100A (NCR53C90A)";
1255 return "Sparc ESP236";
1257 return "Sparc ESP236-FAST";
1259 return "Sparc ESP366-HME";
1261 return "Sparc ESP100A-FAST";
1263 return "Bogon ESP revision";
1267 /* From Wolfgang Stanglmeier's NCR scsi driver. */
1276 static void copy_mem_info(struct info_str *info, char *data, int len)
1278 if (info->pos + len > info->length)
1279 len = info->length - info->pos;
1281 if (info->pos + len < info->offset) {
1285 if (info->pos < info->offset) {
1286 data += (info->offset - info->pos);
1287 len -= (info->offset - info->pos);
1291 memcpy(info->buffer + info->pos, data, len);
1296 static int copy_info(struct info_str *info, char *fmt, ...)
1302 va_start(args, fmt);
1303 len = vsprintf(buf, fmt, args);
1306 copy_mem_info(info, buf, len);
1310 static int esp_host_info(struct esp *esp, char *ptr, off_t offset, int len)
1312 struct scsi_device *sdev;
1313 struct info_str info;
1318 info.offset = offset;
1321 copy_info(&info, "Sparc ESP Host Adapter:\n");
1322 copy_info(&info, "\tPROM node\t\t%08x\n", (unsigned int) esp->prom_node);
1323 copy_info(&info, "\tPROM name\t\t%s\n", esp->prom_name);
1324 copy_info(&info, "\tESP Model\t\t");
1325 switch (esp->erev) {
1327 copy_info(&info, "ESP100\n");
1330 copy_info(&info, "ESP100A\n");
1333 copy_info(&info, "ESP236\n");
1336 copy_info(&info, "FAS236\n");
1339 copy_info(&info, "FAS100A\n");
1342 copy_info(&info, "FAST\n");
1345 copy_info(&info, "Happy Meal FAS\n");
1349 copy_info(&info, "Unknown!\n");
1352 copy_info(&info, "\tDMA Revision\t\t");
1353 switch (esp->dma->revision) {
1355 copy_info(&info, "Rev 0\n");
1358 copy_info(&info, "ESC Rev 1\n");
1361 copy_info(&info, "Rev 1\n");
1364 copy_info(&info, "Rev 2\n");
1367 copy_info(&info, "Rev 3\n");
1370 copy_info(&info, "Rev 1+\n");
1373 copy_info(&info, "Rev HME/FAS\n");
1376 copy_info(&info, "Unknown!\n");
1379 copy_info(&info, "\tLive Targets\t\t[ ");
1380 for (i = 0; i < 15; i++) {
1381 if (esp->targets_present & (1 << i))
1382 copy_info(&info, "%d ", i);
1384 copy_info(&info, "]\n\n");
1386 /* Now describe the state of each existing target. */
1387 copy_info(&info, "Target #\tconfig3\t\tSync Capabilities\tDisconnect\tWide\n");
1389 shost_for_each_device(sdev, esp->ehost) {
1390 struct esp_device *esp_dev = sdev->hostdata;
1393 if (!(esp->targets_present & (1 << id)))
1396 copy_info(&info, "%d\t\t", id);
1397 copy_info(&info, "%08lx\t", esp->config3[id]);
1398 copy_info(&info, "[%02lx,%02lx]\t\t\t",
1399 esp_dev->sync_max_offset,
1400 esp_dev->sync_min_period);
1401 copy_info(&info, "%s\t\t",
1402 esp_dev->disconnect ? "yes" : "no");
1403 copy_info(&info, "%s\n",
1404 (esp->config3[id] & ESP_CONFIG3_EWIDE) ? "yes" : "no");
1406 return info.pos > info.offset? info.pos - info.offset : 0;
1409 /* ESP proc filesystem code. */
1410 static int esp_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
1411 int length, int inout)
1416 return -EINVAL; /* not yet */
1419 if (esp->ehost == host)
1428 return esp_host_info(esp, buffer, offset, length);
1431 static void esp_get_dmabufs(struct esp *esp, Scsi_Cmnd *sp)
1433 if (sp->use_sg == 0) {
1434 sp->SCp.this_residual = sp->request_bufflen;
1435 sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
1436 sp->SCp.buffers_residual = 0;
1437 if (sp->request_bufflen) {
1438 sp->SCp.have_data_in = sbus_map_single(esp->sdev, sp->SCp.buffer,
1439 sp->SCp.this_residual,
1440 scsi_to_sbus_dma_dir(sp->sc_data_direction));
1441 sp->SCp.ptr = (char *) ((unsigned long)sp->SCp.have_data_in);
1446 sp->SCp.buffer = (struct scatterlist *) sp->buffer;
1447 sp->SCp.buffers_residual = sbus_map_sg(esp->sdev,
1450 scsi_to_sbus_dma_dir(sp->sc_data_direction));
1451 sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
1452 sp->SCp.ptr = (char *) ((unsigned long)sg_dma_address(sp->SCp.buffer));
1456 static void esp_release_dmabufs(struct esp *esp, Scsi_Cmnd *sp)
1459 sbus_unmap_sg(esp->sdev, sp->buffer, sp->use_sg,
1460 scsi_to_sbus_dma_dir(sp->sc_data_direction));
1461 } else if (sp->request_bufflen) {
1462 sbus_unmap_single(esp->sdev,
1463 sp->SCp.have_data_in,
1464 sp->request_bufflen,
1465 scsi_to_sbus_dma_dir(sp->sc_data_direction));
1469 static void esp_restore_pointers(struct esp *esp, Scsi_Cmnd *sp)
1471 struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
1473 sp->SCp.ptr = ep->saved_ptr;
1474 sp->SCp.buffer = ep->saved_buffer;
1475 sp->SCp.this_residual = ep->saved_this_residual;
1476 sp->SCp.buffers_residual = ep->saved_buffers_residual;
1479 static void esp_save_pointers(struct esp *esp, Scsi_Cmnd *sp)
1481 struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
1483 ep->saved_ptr = sp->SCp.ptr;
1484 ep->saved_buffer = sp->SCp.buffer;
1485 ep->saved_this_residual = sp->SCp.this_residual;
1486 ep->saved_buffers_residual = sp->SCp.buffers_residual;
1491 * 1) Never ever panic while something is live on the bus.
1492 * If there is to be any chance of syncing the disks this
1493 * rule is to be obeyed.
1495 * 2) Any target that causes a foul condition will no longer
1496 * have synchronous transfers done to it, no questions
1499 * 3) Keep register accesses to a minimum. Think about some
1500 * day when we have Xbus machines this is running on and
1501 * the ESP chip is on the other end of the machine on a
1502 * different board from the cpu where this is running.
1505 /* Fire off a command. We assume the bus is free and that the only
1506 * case where we could see an interrupt is where we have disconnected
1507 * commands active and they are trying to reselect us.
1509 static inline void esp_check_cmd(struct esp *esp, Scsi_Cmnd *sp)
1511 switch (sp->cmd_len) {
1515 esp->esp_slowcmd = 0;
1519 esp->esp_slowcmd = 1;
1520 esp->esp_scmdleft = sp->cmd_len;
1521 esp->esp_scmdp = &sp->cmnd[0];
1526 static inline void build_sync_nego_msg(struct esp *esp, int period, int offset)
1528 esp->cur_msgout[0] = EXTENDED_MESSAGE;
1529 esp->cur_msgout[1] = 3;
1530 esp->cur_msgout[2] = EXTENDED_SDTR;
1531 esp->cur_msgout[3] = period;
1532 esp->cur_msgout[4] = offset;
1533 esp->msgout_len = 5;
1536 /* SIZE is in bits, currently HME only supports 16 bit wide transfers. */
1537 static inline void build_wide_nego_msg(struct esp *esp, int size)
1539 esp->cur_msgout[0] = EXTENDED_MESSAGE;
1540 esp->cur_msgout[1] = 2;
1541 esp->cur_msgout[2] = EXTENDED_WDTR;
1544 esp->cur_msgout[3] = 2;
1547 esp->cur_msgout[3] = 1;
1551 esp->cur_msgout[3] = 0;
1555 esp->msgout_len = 4;
1558 static void esp_exec_cmd(struct esp *esp)
1562 struct esp_device *esp_dev;
1563 volatile u8 *cmdp = esp->esp_command;
1568 /* Hold off if we have disconnected commands and
1569 * an IRQ is showing...
1571 if (esp->disconnected_SC && ESP_IRQ_P(esp->dregs))
1574 /* Grab first member of the issue queue. */
1575 SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
1577 /* Safe to panic here because current_SC is null. */
1579 panic("esp: esp_exec_cmd and issue queue is NULL");
1581 SDptr = SCptr->device;
1582 esp_dev = SDptr->hostdata;
1583 lun = SCptr->device->lun;
1584 target = SCptr->device->id;
1587 esp->msgout_len = 0;
1589 /* Send it out whole, or piece by piece? The ESP
1590 * only knows how to automatically send out 6, 10,
1591 * and 12 byte commands. I used to think that the
1592 * Linux SCSI code would never throw anything other
1593 * than that to us, but then again there is the
1594 * SCSI generic driver which can send us anything.
1596 esp_check_cmd(esp, SCptr);
1598 /* If arbitration/selection is successful, the ESP will leave
1599 * ATN asserted, causing the target to go into message out
1600 * phase. The ESP will feed the target the identify and then
1601 * the target can only legally go to one of command,
1602 * datain/out, status, or message in phase, or stay in message
1603 * out phase (should we be trying to send a sync negotiation
1604 * message after the identify). It is not allowed to drop
1605 * BSY, but some buggy targets do and we check for this
1606 * condition in the selection complete code. Most of the time
1607 * we'll make the command bytes available to the ESP and it
1608 * will not interrupt us until it finishes command phase, we
1609 * cannot do this for command sizes the ESP does not
1610 * understand and in this case we'll get interrupted right
1611 * when the target goes into command phase.
1613 * It is absolutely _illegal_ in the presence of SCSI-2 devices
1614 * to use the ESP select w/o ATN command. When SCSI-2 devices are
1615 * present on the bus we _must_ always go straight to message out
1616 * phase with an identify message for the target. Being that
1617 * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
1618 * selections should not confuse SCSI-1 we hope.
1621 if (esp_dev->sync) {
1622 /* this targets sync is known */
1623 #ifndef __sparc_v9__
1626 if (esp_dev->disconnect)
1627 *cmdp++ = IDENTIFY(1, lun);
1629 *cmdp++ = IDENTIFY(0, lun);
1631 if (esp->esp_slowcmd) {
1632 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1633 esp_advance_phase(SCptr, in_slct_stop);
1635 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1636 esp_advance_phase(SCptr, in_slct_norm);
1638 } else if (!(esp->targets_present & (1<<target)) || !(esp_dev->disconnect)) {
1639 /* After the bootup SCSI code sends both the
1640 * TEST_UNIT_READY and INQUIRY commands we want
1641 * to at least attempt allowing the device to
1644 ESPMISC(("esp: Selecting device for first time. target=%d "
1645 "lun=%d\n", target, SCptr->device->lun));
1646 if (!SDptr->borken && !esp_dev->disconnect)
1647 esp_dev->disconnect = 1;
1649 *cmdp++ = IDENTIFY(0, lun);
1650 esp->prevmsgout = NOP;
1651 esp_advance_phase(SCptr, in_slct_norm);
1652 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1654 /* Take no chances... */
1655 esp_dev->sync_max_offset = 0;
1656 esp_dev->sync_min_period = 0;
1658 /* Sorry, I have had way too many problems with
1659 * various CDROM devices on ESP. -DaveM
1661 int cdrom_hwbug_wkaround = 0;
1663 #ifndef __sparc_v9__
1664 /* Never allow disconnects or synchronous transfers on
1665 * SparcStation1 and SparcStation1+. Allowing those
1666 * to be enabled seems to lockup the machine completely.
1668 if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
1669 (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
1670 /* But we are nice and allow tapes and removable
1671 * disks (but not CDROMs) to disconnect.
1673 if(SDptr->type == TYPE_TAPE ||
1674 (SDptr->type != TYPE_ROM && SDptr->removable))
1675 esp_dev->disconnect = 1;
1677 esp_dev->disconnect = 0;
1678 esp_dev->sync_max_offset = 0;
1679 esp_dev->sync_min_period = 0;
1684 #endif /* !(__sparc_v9__) */
1686 /* We've talked to this guy before,
1687 * but never negotiated. Let's try,
1688 * need to attempt WIDE first, before
1689 * sync nego, as per SCSI 2 standard.
1691 if (esp->erev == fashme && !esp_dev->wide) {
1692 if (!SDptr->borken &&
1693 SDptr->type != TYPE_ROM &&
1694 SDptr->removable == 0) {
1695 build_wide_nego_msg(esp, 16);
1698 goto after_nego_msg_built;
1701 /* Fall through and try sync. */
1705 if (!SDptr->borken) {
1706 if ((SDptr->type == TYPE_ROM)) {
1707 /* Nice try sucker... */
1708 ESPMISC(("esp%d: Disabling sync for buggy "
1709 "CDROM.\n", esp->esp_id));
1710 cdrom_hwbug_wkaround = 1;
1711 build_sync_nego_msg(esp, 0, 0);
1712 } else if (SDptr->removable != 0) {
1713 ESPMISC(("esp%d: Not negotiating sync/wide but "
1714 "allowing disconnect for removable media.\n",
1716 build_sync_nego_msg(esp, 0, 0);
1718 build_sync_nego_msg(esp, esp->sync_defp, 15);
1721 build_sync_nego_msg(esp, 0, 0);
1726 after_nego_msg_built:
1727 /* A fix for broken SCSI1 targets, when they disconnect
1728 * they lock up the bus and confuse ESP. So disallow
1729 * disconnects for SCSI1 targets for now until we
1730 * find a better fix.
1732 * Addendum: This is funny, I figured out what was going
1733 * on. The blotzed SCSI1 target would disconnect,
1734 * one of the other SCSI2 targets or both would be
1735 * disconnected as well. The SCSI1 target would
1736 * stay disconnected long enough that we start
1737 * up a command on one of the SCSI2 targets. As
1738 * the ESP is arbitrating for the bus the SCSI1
1739 * target begins to arbitrate as well to reselect
1740 * the ESP. The SCSI1 target refuses to drop it's
1741 * ID bit on the data bus even though the ESP is
1742 * at ID 7 and is the obvious winner for any
1743 * arbitration. The ESP is a poor sport and refuses
1744 * to lose arbitration, it will continue indefinitely
1745 * trying to arbitrate for the bus and can only be
1746 * stopped via a chip reset or SCSI bus reset.
1747 * Therefore _no_ disconnects for SCSI1 targets
1748 * thank you very much. ;-)
1750 if(((SDptr->scsi_level < 3) &&
1751 (SDptr->type != TYPE_TAPE) &&
1752 SDptr->removable == 0) ||
1753 cdrom_hwbug_wkaround || SDptr->borken) {
1754 ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
1755 "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
1756 esp_dev->disconnect = 0;
1757 *cmdp++ = IDENTIFY(0, lun);
1759 *cmdp++ = IDENTIFY(1, lun);
1762 /* ESP fifo is only so big...
1763 * Make this look like a slow command.
1765 esp->esp_slowcmd = 1;
1766 esp->esp_scmdleft = SCptr->cmd_len;
1767 esp->esp_scmdp = &SCptr->cmnd[0];
1769 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1770 esp_advance_phase(SCptr, in_slct_msg);
1773 if (!esp->esp_slowcmd)
1774 for (i = 0; i < SCptr->cmd_len; i++)
1775 *cmdp++ = SCptr->cmnd[i];
1778 if (esp->erev == fashme)
1779 sbus_writeb((target & 0xf) | (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT),
1780 esp->eregs + ESP_BUSID);
1782 sbus_writeb(target & 7, esp->eregs + ESP_BUSID);
1783 if (esp->prev_soff != esp_dev->sync_max_offset ||
1784 esp->prev_stp != esp_dev->sync_min_period ||
1785 (esp->erev > esp100a &&
1786 esp->prev_cfg3 != esp->config3[target])) {
1787 esp->prev_soff = esp_dev->sync_max_offset;
1788 esp->prev_stp = esp_dev->sync_min_period;
1789 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
1790 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
1791 if (esp->erev > esp100a) {
1792 esp->prev_cfg3 = esp->config3[target];
1793 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
1796 i = (cmdp - esp->esp_command);
1798 if (esp->erev == fashme) {
1799 esp_cmd(esp, ESP_CMD_FLUSH); /* Grrr! */
1801 /* Set up the DMA and HME counters */
1802 sbus_writeb(i, esp->eregs + ESP_TCLOW);
1803 sbus_writeb(0, esp->eregs + ESP_TCMED);
1804 sbus_writeb(0, esp->eregs + FAS_RLO);
1805 sbus_writeb(0, esp->eregs + FAS_RHI);
1806 esp_cmd(esp, the_esp_command);
1808 /* Talk about touchy hardware... */
1809 esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
1810 (DMA_SCSI_DISAB | DMA_ENABLE)) &
1812 sbus_writel(16, esp->dregs + DMA_COUNT);
1813 sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
1814 sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
1818 /* Set up the DMA and ESP counters */
1819 sbus_writeb(i, esp->eregs + ESP_TCLOW);
1820 sbus_writeb(0, esp->eregs + ESP_TCMED);
1821 tmp = sbus_readl(esp->dregs + DMA_CSR);
1822 tmp &= ~DMA_ST_WRITE;
1824 sbus_writel(tmp, esp->dregs + DMA_CSR);
1825 if (esp->dma->revision == dvmaesc1) {
1826 if (i) /* Workaround ESC gate array SBUS rerun bug. */
1827 sbus_writel(PAGE_SIZE, esp->dregs + DMA_COUNT);
1829 sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
1831 /* Tell ESP to "go". */
1832 esp_cmd(esp, the_esp_command);
1836 /* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
1837 static int esp_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
1841 /* Set up func ptr and initial driver cmd-phase. */
1842 SCpnt->scsi_done = done;
1843 SCpnt->SCp.phase = not_issued;
1845 /* We use the scratch area. */
1846 ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->device->id, SCpnt->device->lun));
1847 ESPDISC(("N<%02x,%02x>", SCpnt->device->id, SCpnt->device->lun));
1849 esp = (struct esp *) SCpnt->device->host->hostdata;
1850 esp_get_dmabufs(esp, SCpnt);
1851 esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
1853 SCpnt->SCp.Status = CHECK_CONDITION;
1854 SCpnt->SCp.Message = 0xff;
1855 SCpnt->SCp.sent_command = 0;
1857 /* Place into our queue. */
1858 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
1859 ESPQUEUE(("RQSENSE\n"));
1860 prepend_SC(&esp->issue_SC, SCpnt);
1863 append_SC(&esp->issue_SC, SCpnt);
1866 /* Run it now if we can. */
1867 if (!esp->current_SC && !esp->resetting_bus)
1873 /* Dump driver state. */
1874 static void esp_dump_cmd(Scsi_Cmnd *SCptr)
1876 ESPLOG(("[tgt<%02x> lun<%02x> "
1877 "pphase<%s> cphase<%s>]",
1878 SCptr->device->id, SCptr->device->lun,
1879 phase_string(SCptr->SCp.sent_command),
1880 phase_string(SCptr->SCp.phase)));
1883 static void esp_dump_state(struct esp *esp)
1885 Scsi_Cmnd *SCptr = esp->current_SC;
1886 #ifdef DEBUG_ESP_CMDS
1890 ESPLOG(("esp%d: dumping state\n", esp->esp_id));
1891 ESPLOG(("esp%d: dma -- cond_reg<%08x> addr<%08x>\n",
1893 sbus_readl(esp->dregs + DMA_CSR),
1894 sbus_readl(esp->dregs + DMA_ADDR)));
1895 ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1896 esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
1897 ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1899 sbus_readb(esp->eregs + ESP_STATUS),
1900 sbus_readb(esp->eregs + ESP_SSTEP),
1901 sbus_readb(esp->eregs + ESP_INTRPT)));
1902 #ifdef DEBUG_ESP_CMDS
1903 printk("esp%d: last ESP cmds [", esp->esp_id);
1904 i = (esp->espcmdent - 1) & 31;
1905 printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
1907 printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
1909 printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
1911 printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
1913 #endif /* (DEBUG_ESP_CMDS) */
1916 ESPLOG(("esp%d: current command ", esp->esp_id));
1917 esp_dump_cmd(SCptr);
1920 SCptr = esp->disconnected_SC;
1921 ESPLOG(("esp%d: disconnected ", esp->esp_id));
1923 esp_dump_cmd(SCptr);
1924 SCptr = (Scsi_Cmnd *) SCptr->host_scribble;
1929 /* Abort a command. The host_lock is acquired by caller. */
1930 static int esp_abort(Scsi_Cmnd *SCptr)
1932 struct esp *esp = (struct esp *) SCptr->device->host->hostdata;
1935 ESPLOG(("esp%d: Aborting command\n", esp->esp_id));
1936 esp_dump_state(esp);
1938 /* Wheee, if this is the current command on the bus, the
1939 * best we can do is assert ATN and wait for msgout phase.
1940 * This should even fix a hung SCSI bus when we lose state
1941 * in the driver and timeout because the eventual phase change
1942 * will cause the ESP to (eventually) give an interrupt.
1944 if (esp->current_SC == SCptr) {
1945 esp->cur_msgout[0] = ABORT;
1946 esp->msgout_len = 1;
1947 esp->msgout_ctr = 0;
1948 esp_cmd(esp, ESP_CMD_SATN);
1952 /* If it is still in the issue queue then we can safely
1953 * call the completion routine and report abort success.
1955 don = (sbus_readl(esp->dregs + DMA_CSR) & DMA_INT_ENAB);
1957 ESP_INTSOFF(esp->dregs);
1959 if (esp->issue_SC) {
1960 Scsi_Cmnd **prev, *this;
1961 for (prev = (&esp->issue_SC), this = esp->issue_SC;
1963 prev = (Scsi_Cmnd **) &(this->host_scribble),
1964 this = (Scsi_Cmnd *) this->host_scribble) {
1966 if (this == SCptr) {
1967 *prev = (Scsi_Cmnd *) this->host_scribble;
1968 this->host_scribble = NULL;
1970 esp_release_dmabufs(esp, this);
1971 this->result = DID_ABORT << 16;
1972 this->scsi_done(this);
1975 ESP_INTSON(esp->dregs);
1982 /* Yuck, the command to abort is disconnected, it is not
1983 * worth trying to abort it now if something else is live
1984 * on the bus at this time. So, we let the SCSI code wait
1985 * a little bit and try again later.
1987 if (esp->current_SC) {
1989 ESP_INTSON(esp->dregs);
1993 /* It's disconnected, we have to reconnect to re-establish
1994 * the nexus and tell the device to abort. However, we really
1995 * cannot 'reconnect' per se. Don't try to be fancy, just
1996 * indicate failure, which causes our caller to reset the whole
2001 ESP_INTSON(esp->dregs);
2006 /* We've sent ESP_CMD_RS to the ESP, the interrupt had just
2007 * arrived indicating the end of the SCSI bus reset. Our job
2008 * is to clean out the command queues and begin re-execution
2009 * of SCSI commands once more.
2011 static int esp_finish_reset(struct esp *esp)
2013 Scsi_Cmnd *sp = esp->current_SC;
2015 /* Clean up currently executing command, if any. */
2017 esp->current_SC = NULL;
2019 esp_release_dmabufs(esp, sp);
2020 sp->result = (DID_RESET << 16);
2025 /* Clean up disconnected queue, they have been invalidated
2028 if (esp->disconnected_SC) {
2029 while ((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
2030 esp_release_dmabufs(esp, sp);
2031 sp->result = (DID_RESET << 16);
2037 /* SCSI bus reset is complete. */
2038 esp->resetting_bus = 0;
2039 wake_up(&esp->reset_queue);
2041 /* Ok, now it is safe to get commands going once more. */
2048 static int esp_do_resetbus(struct esp *esp)
2050 ESPLOG(("esp%d: Resetting scsi bus\n", esp->esp_id));
2051 esp->resetting_bus = 1;
2052 esp_cmd(esp, ESP_CMD_RS);
2057 /* Reset ESP chip, reset hanging bus, then kill active and
2058 * disconnected commands for targets without soft reset.
2060 * The host_lock is acquired by caller.
2062 static int esp_reset(Scsi_Cmnd *SCptr)
2064 struct esp *esp = (struct esp *) SCptr->device->host->hostdata;
2066 (void) esp_do_resetbus(esp);
2068 spin_unlock_irq(esp->ehost->host_lock);
2070 wait_event(esp->reset_queue, (esp->resetting_bus == 0));
2072 spin_lock_irq(esp->ehost->host_lock);
2077 /* Internal ESP done function. */
2078 static void esp_done(struct esp *esp, int error)
2080 Scsi_Cmnd *done_SC = esp->current_SC;
2082 esp->current_SC = NULL;
2084 esp_release_dmabufs(esp, done_SC);
2085 done_SC->result = error;
2087 done_SC->scsi_done(done_SC);
2089 /* Bus is free, issue any commands in the queue. */
2090 if (esp->issue_SC && !esp->current_SC)
2095 /* Wheee, ESP interrupt engine. */
2097 /* Forward declarations. */
2098 static int esp_do_phase_determine(struct esp *esp);
2099 static int esp_do_data_finale(struct esp *esp);
2100 static int esp_select_complete(struct esp *esp);
2101 static int esp_do_status(struct esp *esp);
2102 static int esp_do_msgin(struct esp *esp);
2103 static int esp_do_msgindone(struct esp *esp);
2104 static int esp_do_msgout(struct esp *esp);
2105 static int esp_do_cmdbegin(struct esp *esp);
2107 #define sreg_datainp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
2108 #define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
2110 /* Read any bytes found in the FAS366 fifo, storing them into
2111 * the ESP driver software state structure.
2113 static void hme_fifo_read(struct esp *esp)
2116 u8 status = esp->sreg;
2118 /* Cannot safely frob the fifo for these following cases, but
2119 * we must always read the fifo when the reselect interrupt
2122 if (((esp->ireg & ESP_INTR_RSEL) == 0) &&
2123 (sreg_datainp(status) ||
2124 sreg_dataoutp(status) ||
2126 esp->current_SC->SCp.phase == in_data_done))) {
2127 ESPHME(("<wkaround_skipped>"));
2129 unsigned long fcnt = sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES;
2131 /* The HME stores bytes in multiples of 2 in the fifo. */
2132 ESPHME(("hme_fifo[fcnt=%d", (int)fcnt));
2134 esp->hme_fifo_workaround_buffer[count++] =
2135 sbus_readb(esp->eregs + ESP_FDATA);
2136 esp->hme_fifo_workaround_buffer[count++] =
2137 sbus_readb(esp->eregs + ESP_FDATA);
2138 ESPHME(("<%02x,%02x>", esp->hme_fifo_workaround_buffer[count-2], esp->hme_fifo_workaround_buffer[count-1]));
2141 if (sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_F1BYTE) {
2142 ESPHME(("<poke_byte>"));
2143 sbus_writeb(0, esp->eregs + ESP_FDATA);
2144 esp->hme_fifo_workaround_buffer[count++] =
2145 sbus_readb(esp->eregs + ESP_FDATA);
2146 ESPHME(("<%02x,0x00>", esp->hme_fifo_workaround_buffer[count-1]));
2147 ESPHME(("CMD_FLUSH"));
2148 esp_cmd(esp, ESP_CMD_FLUSH);
2150 ESPHME(("no_xtra_byte"));
2153 ESPHME(("wkarnd_cnt=%d]", (int)count));
2154 esp->hme_fifo_workaround_count = count;
2157 static inline void hme_fifo_push(struct esp *esp, u8 *bytes, u8 count)
2159 esp_cmd(esp, ESP_CMD_FLUSH);
2162 sbus_writeb(tmp, esp->eregs + ESP_FDATA);
2163 sbus_writeb(0, esp->eregs + ESP_FDATA);
2168 /* We try to avoid some interrupts by jumping ahead and see if the ESP
2169 * has gotten far enough yet. Hence the following.
2171 static inline int skipahead1(struct esp *esp, Scsi_Cmnd *scp,
2172 int prev_phase, int new_phase)
2174 if (scp->SCp.sent_command != prev_phase)
2176 if (ESP_IRQ_P(esp->dregs)) {
2177 /* Yes, we are able to save an interrupt. */
2178 if (esp->erev == fashme)
2179 esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
2180 esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
2181 esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
2182 if (esp->erev == fashme) {
2183 /* This chip is really losing. */
2185 /* Must latch fifo before reading the interrupt
2186 * register else garbage ends up in the FIFO
2187 * which confuses the driver utterly.
2188 * Happy Meal indeed....
2190 ESPHME(("fifo_workaround]"));
2191 if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
2192 (esp->sreg2 & ESP_STAT2_F1BYTE))
2195 if (!(esp->ireg & ESP_INTR_SR))
2198 return do_reset_complete;
2200 /* Ho hum, target is taking forever... */
2201 scp->SCp.sent_command = new_phase; /* so we don't recurse... */
2205 static inline int skipahead2(struct esp *esp, Scsi_Cmnd *scp,
2206 int prev_phase1, int prev_phase2, int new_phase)
2208 if (scp->SCp.sent_command != prev_phase1 &&
2209 scp->SCp.sent_command != prev_phase2)
2211 if (ESP_IRQ_P(esp->dregs)) {
2212 /* Yes, we are able to save an interrupt. */
2213 if (esp->erev == fashme)
2214 esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
2215 esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
2216 esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
2217 if (esp->erev == fashme) {
2218 /* This chip is really losing. */
2221 /* Must latch fifo before reading the interrupt
2222 * register else garbage ends up in the FIFO
2223 * which confuses the driver utterly.
2224 * Happy Meal indeed....
2226 ESPHME(("fifo_workaround]"));
2227 if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
2228 (esp->sreg2 & ESP_STAT2_F1BYTE))
2231 if (!(esp->ireg & ESP_INTR_SR))
2234 return do_reset_complete;
2236 /* Ho hum, target is taking forever... */
2237 scp->SCp.sent_command = new_phase; /* so we don't recurse... */
2241 /* Now some dma helpers. */
2242 static void dma_setup(struct esp *esp, __u32 addr, int count, int write)
2244 u32 nreg = sbus_readl(esp->dregs + DMA_CSR);
2247 nreg |= DMA_ST_WRITE;
2249 nreg &= ~(DMA_ST_WRITE);
2251 sbus_writel(nreg, esp->dregs + DMA_CSR);
2252 if (esp->dma->revision == dvmaesc1) {
2253 /* This ESC gate array sucks! */
2255 __u32 dest = src + count;
2257 if (dest & (PAGE_SIZE - 1))
2258 count = PAGE_ALIGN(count);
2259 sbus_writel(count, esp->dregs + DMA_COUNT);
2261 sbus_writel(addr, esp->dregs + DMA_ADDR);
2264 static void dma_drain(struct esp *esp)
2268 if (esp->dma->revision == dvmahme)
2270 if ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_FIFO_ISDRAIN) {
2271 switch (esp->dma->revision) {
2273 tmp |= DMA_FIFO_STDRAIN;
2274 sbus_writel(tmp, esp->dregs + DMA_CSR);
2278 while (sbus_readl(esp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
2284 static void dma_invalidate(struct esp *esp)
2288 if (esp->dma->revision == dvmahme) {
2289 sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
2291 esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
2292 (DMA_PARITY_OFF | DMA_2CLKS |
2293 DMA_SCSI_DISAB | DMA_INT_ENAB)) &
2294 ~(DMA_ST_WRITE | DMA_ENABLE));
2296 sbus_writel(0, esp->dregs + DMA_CSR);
2297 sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
2299 /* This is necessary to avoid having the SCSI channel
2300 * engine lock up on us.
2302 sbus_writel(0, esp->dregs + DMA_ADDR);
2304 while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
2307 tmp &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
2308 tmp |= DMA_FIFO_INV;
2309 sbus_writel(tmp, esp->dregs + DMA_CSR);
2310 tmp &= ~DMA_FIFO_INV;
2311 sbus_writel(tmp, esp->dregs + DMA_CSR);
2315 static inline void dma_flashclear(struct esp *esp)
2318 dma_invalidate(esp);
2321 static int dma_can_transfer(struct esp *esp, Scsi_Cmnd *sp)
2323 __u32 base, end, sz;
2325 if (esp->dma->revision == dvmarev3) {
2326 sz = sp->SCp.this_residual;
2330 base = ((__u32)((unsigned long)sp->SCp.ptr));
2331 base &= (0x1000000 - 1);
2332 end = (base + sp->SCp.this_residual);
2333 if (end > 0x1000000)
2340 /* Misc. esp helper macros. */
2341 #define esp_setcount(__eregs, __cnt, __hme) \
2342 sbus_writeb(((__cnt)&0xff), (__eregs) + ESP_TCLOW); \
2343 sbus_writeb((((__cnt)>>8)&0xff), (__eregs) + ESP_TCMED); \
2345 sbus_writeb((((__cnt)>>16)&0xff), (__eregs) + FAS_RLO); \
2346 sbus_writeb(0, (__eregs) + FAS_RHI); \
2349 #define esp_getcount(__eregs, __hme) \
2350 ((sbus_readb((__eregs) + ESP_TCLOW)&0xff) | \
2351 ((sbus_readb((__eregs) + ESP_TCMED)&0xff) << 8) | \
2352 ((__hme) ? sbus_readb((__eregs) + FAS_RLO) << 16 : 0))
2354 #define fcount(__esp) \
2355 (((__esp)->erev == fashme) ? \
2356 (__esp)->hme_fifo_workaround_count : \
2357 sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_FBYTES)
2359 #define fnzero(__esp) \
2360 (((__esp)->erev == fashme) ? 0 : \
2361 sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_ONOTZERO)
2363 /* XXX speculative nops unnecessary when continuing amidst a data phase
2364 * XXX even on esp100!!! another case of flooding the bus with I/O reg
2367 #define esp_maybe_nop(__esp) \
2368 if ((__esp)->erev == esp100) \
2369 esp_cmd((__esp), ESP_CMD_NULL)
2371 #define sreg_to_dataphase(__sreg) \
2372 ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
2374 /* The ESP100 when in synchronous data phase, can mistake a long final
2375 * REQ pulse from the target as an extra byte, it places whatever is on
2376 * the data lines into the fifo. For now, we will assume when this
2377 * happens that the target is a bit quirky and we don't want to
2378 * be talking synchronously to it anyways. Regardless, we need to
2379 * tell the ESP to eat the extraneous byte so that we can proceed
2380 * to the next phase.
2382 static int esp100_sync_hwbug(struct esp *esp, Scsi_Cmnd *sp, int fifocnt)
2384 /* Do not touch this piece of code. */
2385 if ((!(esp->erev == esp100)) ||
2386 (!(sreg_datainp((esp->sreg = sbus_readb(esp->eregs + ESP_STATUS))) &&
2388 !(sreg_dataoutp(esp->sreg) && !fnzero(esp)))) {
2389 if (sp->SCp.phase == in_dataout)
2390 esp_cmd(esp, ESP_CMD_FLUSH);
2393 /* Async mode for this guy. */
2394 build_sync_nego_msg(esp, 0, 0);
2396 /* Ack the bogus byte, but set ATN first. */
2397 esp_cmd(esp, ESP_CMD_SATN);
2398 esp_cmd(esp, ESP_CMD_MOK);
2403 /* This closes the window during a selection with a reselect pending, because
2404 * we use DMA for the selection process the FIFO should hold the correct
2405 * contents if we get reselected during this process. So we just need to
2406 * ack the possible illegal cmd interrupt pending on the esp100.
2408 static inline int esp100_reconnect_hwbug(struct esp *esp)
2412 if (esp->erev != esp100)
2414 tmp = sbus_readb(esp->eregs + ESP_INTRPT);
2415 if (tmp & ESP_INTR_SR)
2420 /* This verifies the BUSID bits during a reselection so that we know which
2421 * target is talking to us.
2423 static inline int reconnect_target(struct esp *esp)
2425 int it, me = esp->scsi_id_mask, targ = 0;
2427 if (2 != fcount(esp))
2429 if (esp->erev == fashme) {
2430 /* HME does not latch it's own BUS ID bits during
2431 * a reselection. Also the target number is given
2432 * as an unsigned char, not as a sole bit number
2433 * like the other ESP's do.
2434 * Happy Meal indeed....
2436 targ = esp->hme_fifo_workaround_buffer[0];
2438 it = sbus_readb(esp->eregs + ESP_FDATA);
2450 /* This verifies the identify from the target so that we know which lun is
2451 * being reconnected.
2453 static inline int reconnect_lun(struct esp *esp)
2457 if ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
2459 if (esp->erev == fashme)
2460 lun = esp->hme_fifo_workaround_buffer[1];
2462 lun = sbus_readb(esp->eregs + ESP_FDATA);
2464 /* Yes, you read this correctly. We report lun of zero
2465 * if we see parity error. ESP reports parity error for
2466 * the lun byte, and this is the only way to hope to recover
2467 * because the target is connected.
2469 if (esp->sreg & ESP_STAT_PERR)
2472 /* Check for illegal bits being set in the lun. */
2473 if ((lun & 0x40) || !(lun & 0x80))
2479 /* This puts the driver in a state where it can revitalize a command that
2480 * is being continued due to reselection.
2482 static inline void esp_connect(struct esp *esp, Scsi_Cmnd *sp)
2484 struct esp_device *esp_dev = sp->device->hostdata;
2486 if (esp->prev_soff != esp_dev->sync_max_offset ||
2487 esp->prev_stp != esp_dev->sync_min_period ||
2488 (esp->erev > esp100a &&
2489 esp->prev_cfg3 != esp->config3[sp->device->id])) {
2490 esp->prev_soff = esp_dev->sync_max_offset;
2491 esp->prev_stp = esp_dev->sync_min_period;
2492 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
2493 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
2494 if (esp->erev > esp100a) {
2495 esp->prev_cfg3 = esp->config3[sp->device->id];
2496 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
2499 esp->current_SC = sp;
2502 /* This will place the current working command back into the issue queue
2503 * if we are to receive a reselection amidst a selection attempt.
2505 static inline void esp_reconnect(struct esp *esp, Scsi_Cmnd *sp)
2507 if (!esp->disconnected_SC)
2508 ESPLOG(("esp%d: Weird, being reselected but disconnected "
2509 "command queue is empty.\n", esp->esp_id));
2511 esp->current_SC = 0;
2512 sp->SCp.phase = not_issued;
2513 append_SC(&esp->issue_SC, sp);
2516 /* Begin message in phase. */
2517 static int esp_do_msgin(struct esp *esp)
2519 /* Must be very careful with the fifo on the HME */
2520 if ((esp->erev != fashme) ||
2521 !(sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_FEMPTY))
2522 esp_cmd(esp, ESP_CMD_FLUSH);
2524 esp_cmd(esp, ESP_CMD_TI);
2527 esp_advance_phase(esp->current_SC, in_msgindone);
2531 /* This uses various DMA csr fields and the fifo flags count value to
2532 * determine how many bytes were successfully sent/received by the ESP.
2534 static inline int esp_bytes_sent(struct esp *esp, int fifo_count)
2536 int rval = sbus_readl(esp->dregs + DMA_ADDR) - esp->esp_command_dvma;
2538 if (esp->dma->revision == dvmarev1)
2539 rval -= (4 - ((sbus_readl(esp->dregs + DMA_CSR) & DMA_READ_AHEAD)>>11));
2540 return rval - fifo_count;
2543 static inline void advance_sg(Scsi_Cmnd *sp)
2546 --sp->SCp.buffers_residual;
2547 sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
2548 sp->SCp.ptr = (char *)((unsigned long)sg_dma_address(sp->SCp.buffer));
2551 /* Please note that the way I've coded these routines is that I _always_
2552 * check for a disconnect during any and all information transfer
2553 * phases. The SCSI standard states that the target _can_ cause a BUS
2554 * FREE condition by dropping all MSG/CD/IO/BSY signals. Also note
2555 * that during information transfer phases the target controls every
2556 * change in phase, the only thing the initiator can do is "ask" for
2557 * a message out phase by driving ATN true. The target can, and sometimes
2558 * will, completely ignore this request so we cannot assume anything when
2559 * we try to force a message out phase to abort/reset a target. Most of
2560 * the time the target will eventually be nice and go to message out, so
2561 * we may have to hold on to our state about what we want to tell the target
2562 * for some period of time.
2565 /* I think I have things working here correctly. Even partial transfers
2566 * within a buffer or sub-buffer should not upset us at all no matter
2567 * how bad the target and/or ESP fucks things up.
2569 static int esp_do_data(struct esp *esp)
2571 Scsi_Cmnd *SCptr = esp->current_SC;
2572 int thisphase, hmuch;
2574 ESPDATA(("esp_do_data: "));
2576 thisphase = sreg_to_dataphase(esp->sreg);
2577 esp_advance_phase(SCptr, thisphase);
2578 ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
2579 hmuch = dma_can_transfer(esp, SCptr);
2580 if (hmuch > (64 * 1024) && (esp->erev != fashme))
2581 hmuch = (64 * 1024);
2582 ESPDATA(("hmuch<%d> ", hmuch));
2583 esp->current_transfer_size = hmuch;
2585 if (esp->erev == fashme) {
2586 u32 tmp = esp->prev_hme_dmacsr;
2588 /* Always set the ESP count registers first. */
2589 esp_setcount(esp->eregs, hmuch, 1);
2591 /* Get the DMA csr computed. */
2592 tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
2593 if (thisphase == in_datain)
2594 tmp |= DMA_ST_WRITE;
2596 tmp &= ~(DMA_ST_WRITE);
2597 esp->prev_hme_dmacsr = tmp;
2599 ESPDATA(("DMA|TI --> do_intr_end\n"));
2600 if (thisphase == in_datain) {
2601 sbus_writel(hmuch, esp->dregs + DMA_COUNT);
2602 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
2604 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
2605 sbus_writel(hmuch, esp->dregs + DMA_COUNT);
2607 sbus_writel((__u32)((unsigned long)SCptr->SCp.ptr), esp->dregs+DMA_ADDR);
2608 sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
2610 esp_setcount(esp->eregs, hmuch, 0);
2611 dma_setup(esp, ((__u32)((unsigned long)SCptr->SCp.ptr)),
2612 hmuch, (thisphase == in_datain));
2613 ESPDATA(("DMA|TI --> do_intr_end\n"));
2614 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
2619 /* See how successful the data transfer was. */
2620 static int esp_do_data_finale(struct esp *esp)
2622 Scsi_Cmnd *SCptr = esp->current_SC;
2623 struct esp_device *esp_dev = SCptr->device->hostdata;
2624 int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
2626 ESPDATA(("esp_do_data_finale: "));
2628 if (SCptr->SCp.phase == in_datain) {
2629 if (esp->sreg & ESP_STAT_PERR) {
2630 /* Yuck, parity error. The ESP asserts ATN
2631 * so that we can go to message out phase
2632 * immediately and inform the target that
2633 * something bad happened.
2635 ESPLOG(("esp%d: data bad parity detected.\n",
2637 esp->cur_msgout[0] = INITIATOR_ERROR;
2638 esp->msgout_len = 1;
2642 dma_invalidate(esp);
2644 /* This could happen for the above parity error case. */
2645 if (esp->ireg != ESP_INTR_BSERV) {
2646 /* Please go to msgout phase, please please please... */
2647 ESPLOG(("esp%d: !BSERV after data, probably to msgout\n",
2649 return esp_do_phase_determine(esp);
2652 /* Check for partial transfers and other horrible events.
2653 * Note, here we read the real fifo flags register even
2654 * on HME broken adapters because we skip the HME fifo
2655 * workaround code in esp_handle() if we are doing data
2656 * phase things. We don't want to fuck directly with
2657 * the fifo like that, especially if doing synchronous
2658 * transfers! Also, will need to double the count on
2659 * HME if we are doing wide transfers, as the HME fifo
2660 * will move and count 16-bit quantities during wide data.
2661 * SMCC _and_ Qlogic can both bite me.
2663 fifocnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
2664 if (esp->erev != fashme)
2665 ecount = esp_getcount(esp->eregs, 0);
2666 bytes_sent = esp->current_transfer_size;
2668 ESPDATA(("trans_sz(%d), ", bytes_sent));
2669 if (esp->erev == fashme) {
2670 if (!(esp->sreg & ESP_STAT_TCNT)) {
2671 ecount = esp_getcount(esp->eregs, 1);
2672 bytes_sent -= ecount;
2675 /* Always subtract any cruft remaining in the FIFO. */
2676 if (esp->prev_cfg3 & ESP_CONFIG3_EWIDE)
2678 if (SCptr->SCp.phase == in_dataout)
2679 bytes_sent -= fifocnt;
2681 /* I have an IBM disk which exhibits the following
2682 * behavior during writes to it. It disconnects in
2683 * the middle of a partial transfer, the current sglist
2684 * buffer is 1024 bytes, the disk stops data transfer
2687 * However the FAS366 reports that 32 more bytes were
2688 * transferred than really were. This is precisely
2689 * the size of a fully loaded FIFO in wide scsi mode.
2690 * The FIFO state recorded indicates that it is empty.
2692 * I have no idea if this is a bug in the FAS366 chip
2693 * or a bug in the firmware on this IBM disk. In any
2694 * event the following seems to be a good workaround. -DaveM
2696 if (bytes_sent != esp->current_transfer_size &&
2697 SCptr->SCp.phase == in_dataout) {
2698 int mask = (64 - 1);
2700 if ((esp->prev_cfg3 & ESP_CONFIG3_EWIDE) == 0)
2703 if (bytes_sent & mask)
2704 bytes_sent -= (bytes_sent & mask);
2707 if (!(esp->sreg & ESP_STAT_TCNT))
2708 bytes_sent -= ecount;
2709 if (SCptr->SCp.phase == in_dataout)
2710 bytes_sent -= fifocnt;
2713 ESPDATA(("bytes_sent(%d), ", bytes_sent));
2715 /* If we were in synchronous mode, check for peculiarities. */
2716 if (esp->erev == fashme) {
2717 if (esp_dev->sync_max_offset) {
2718 if (SCptr->SCp.phase == in_dataout)
2719 esp_cmd(esp, ESP_CMD_FLUSH);
2721 esp_cmd(esp, ESP_CMD_FLUSH);
2724 if (esp_dev->sync_max_offset)
2725 bogus_data = esp100_sync_hwbug(esp, SCptr, fifocnt);
2727 esp_cmd(esp, ESP_CMD_FLUSH);
2730 /* Until we are sure of what has happened, we are certainly
2733 esp_advance_phase(SCptr, in_the_dark);
2735 if (bytes_sent < 0) {
2736 /* I've seen this happen due to lost state in this
2737 * driver. No idea why it happened, but allowing
2738 * this value to be negative caused things to
2739 * lock up. This allows greater chance of recovery.
2740 * In fact every time I've seen this, it has been
2741 * a driver bug without question.
2743 ESPLOG(("esp%d: yieee, bytes_sent < 0!\n", esp->esp_id));
2744 ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%d\n",
2746 esp->current_transfer_size, fifocnt, ecount));
2747 ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%d\n",
2749 SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
2750 ESPLOG(("esp%d: Forcing async for target %d\n", esp->esp_id,
2751 SCptr->device->id));
2752 SCptr->device->borken = 1;
2757 /* Update the state of our transfer. */
2758 SCptr->SCp.ptr += bytes_sent;
2759 SCptr->SCp.this_residual -= bytes_sent;
2760 if (SCptr->SCp.this_residual < 0) {
2762 ESPLOG(("esp%d: Data transfer overrun.\n", esp->esp_id));
2763 SCptr->SCp.this_residual = 0;
2766 /* Maybe continue. */
2768 ESPDATA(("!bogus_data, "));
2770 /* NO MATTER WHAT, we advance the scatterlist,
2771 * if the target should decide to disconnect
2772 * in between scatter chunks (which is common)
2773 * we could die horribly! I used to have the sg
2774 * advance occur only if we are going back into
2775 * (or are staying in) a data phase, you can
2776 * imagine the hell I went through trying to
2779 if (SCptr->use_sg && !SCptr->SCp.this_residual)
2781 if (sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
2782 ESPDATA(("to more data\n"));
2783 return esp_do_data(esp);
2785 ESPDATA(("to new phase\n"));
2786 return esp_do_phase_determine(esp);
2788 /* Bogus data, just wait for next interrupt. */
2789 ESPLOG(("esp%d: bogus_data during end of data phase\n",
2794 /* We received a non-good status return at the end of
2795 * running a SCSI command. This is used to decide if
2796 * we should clear our synchronous transfer state for
2797 * such a device when that happens.
2799 * The idea is that when spinning up a disk or rewinding
2800 * a tape, we don't want to go into a loop re-negotiating
2801 * synchronous capabilities over and over.
2803 static int esp_should_clear_sync(Scsi_Cmnd *sp)
2805 u8 cmd1 = sp->cmnd[0];
2806 u8 cmd2 = sp->data_cmnd[0];
2808 /* These cases are for spinning up a disk and
2809 * waiting for that spinup to complete.
2811 if (cmd1 == START_STOP ||
2815 if (cmd1 == TEST_UNIT_READY ||
2816 cmd2 == TEST_UNIT_READY)
2819 /* One more special case for SCSI tape drives,
2820 * this is what is used to probe the device for
2821 * completion of a rewind or tape load operation.
2823 if (sp->device->type == TYPE_TAPE) {
2824 if (cmd1 == MODE_SENSE ||
2832 /* Either a command is completing or a target is dropping off the bus
2833 * to continue the command in the background so we can do other work.
2835 static int esp_do_freebus(struct esp *esp)
2837 Scsi_Cmnd *SCptr = esp->current_SC;
2838 struct esp_device *esp_dev = SCptr->device->hostdata;
2841 rval = skipahead2(esp, SCptr, in_status, in_msgindone, in_freeing);
2844 if (esp->ireg != ESP_INTR_DC) {
2845 ESPLOG(("esp%d: Target will not disconnect\n", esp->esp_id));
2846 return do_reset_bus; /* target will not drop BSY... */
2848 esp->msgout_len = 0;
2849 esp->prevmsgout = NOP;
2850 if (esp->prevmsgin == COMMAND_COMPLETE) {
2851 /* Normal end of nexus. */
2852 if (esp->disconnected_SC || (esp->erev == fashme))
2853 esp_cmd(esp, ESP_CMD_ESEL);
2855 if (SCptr->SCp.Status != GOOD &&
2856 SCptr->SCp.Status != CONDITION_GOOD &&
2857 ((1<<SCptr->device->id) & esp->targets_present) &&
2859 esp_dev->sync_max_offset) {
2860 /* SCSI standard says that the synchronous capabilities
2861 * should be renegotiated at this point. Most likely
2862 * we are about to request sense from this target
2863 * in which case we want to avoid using sync
2864 * transfers until we are sure of the current target
2867 ESPMISC(("esp: Status <%d> for target %d lun %d\n",
2868 SCptr->SCp.Status, SCptr->device->id, SCptr->device->lun));
2870 /* But don't do this when spinning up a disk at
2871 * boot time while we poll for completion as it
2872 * fills up the console with messages. Also, tapes
2873 * can report not ready many times right after
2874 * loading up a tape.
2876 if (esp_should_clear_sync(SCptr) != 0)
2879 ESPDISC(("F<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
2880 esp_done(esp, ((SCptr->SCp.Status & 0xff) |
2881 ((SCptr->SCp.Message & 0xff)<<8) |
2883 } else if (esp->prevmsgin == DISCONNECT) {
2884 /* Normal disconnect. */
2885 esp_cmd(esp, ESP_CMD_ESEL);
2886 ESPDISC(("D<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
2887 append_SC(&esp->disconnected_SC, SCptr);
2888 esp->current_SC = NULL;
2892 /* Driver bug, we do not expect a disconnect here
2893 * and should not have advanced the state engine
2896 ESPLOG(("esp%d: last msg not disc and not cmd cmplt.\n",
2898 return do_reset_bus;
2903 /* When a reselect occurs, and we cannot find the command to
2904 * reconnect to in our queues, we do this.
2906 static int esp_bad_reconnect(struct esp *esp)
2910 ESPLOG(("esp%d: Eieeee, reconnecting unknown command!\n",
2912 ESPLOG(("QUEUE DUMP\n"));
2914 ESPLOG(("esp%d: issue_SC[", esp->esp_id));
2916 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2917 sp = (Scsi_Cmnd *) sp->host_scribble;
2920 sp = esp->current_SC;
2921 ESPLOG(("esp%d: current_SC[", esp->esp_id));
2923 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2927 sp = esp->disconnected_SC;
2928 ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
2930 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2931 sp = (Scsi_Cmnd *) sp->host_scribble;
2934 return do_reset_bus;
2937 /* Do the needy when a target tries to reconnect to us. */
2938 static int esp_do_reconnect(struct esp *esp)
2943 /* Check for all bogus conditions first. */
2944 target = reconnect_target(esp);
2946 ESPDISC(("bad bus bits\n"));
2947 return do_reset_bus;
2949 lun = reconnect_lun(esp);
2951 ESPDISC(("target=%2x, bad identify msg\n", target));
2952 return do_reset_bus;
2955 /* Things look ok... */
2956 ESPDISC(("R<%02x,%02x>", target, lun));
2958 /* Must not flush FIFO or DVMA on HME. */
2959 if (esp->erev != fashme) {
2960 esp_cmd(esp, ESP_CMD_FLUSH);
2961 if (esp100_reconnect_hwbug(esp))
2962 return do_reset_bus;
2963 esp_cmd(esp, ESP_CMD_NULL);
2966 SCptr = remove_SC(&esp->disconnected_SC, (u8) target, (u8) lun);
2968 return esp_bad_reconnect(esp);
2970 esp_connect(esp, SCptr);
2971 esp_cmd(esp, ESP_CMD_MOK);
2973 if (esp->erev == fashme)
2974 sbus_writeb(((SCptr->device->id & 0xf) |
2975 (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT)),
2976 esp->eregs + ESP_BUSID);
2978 /* Reconnect implies a restore pointers operation. */
2979 esp_restore_pointers(esp, SCptr);
2982 esp_advance_phase(SCptr, in_the_dark);
2986 /* End of NEXUS (hopefully), pick up status + message byte then leave if
2989 static int esp_do_status(struct esp *esp)
2991 Scsi_Cmnd *SCptr = esp->current_SC;
2994 rval = skipahead1(esp, SCptr, in_the_dark, in_status);
2998 ESPSTAT(("esp_do_status: "));
2999 if (intr != ESP_INTR_DC) {
3000 int message_out = 0; /* for parity problems */
3002 /* Ack the message. */
3003 ESPSTAT(("ack msg, "));
3004 esp_cmd(esp, ESP_CMD_MOK);
3006 if (esp->erev != fashme) {
3007 dma_flashclear(esp);
3009 /* Wait till the first bits settle. */
3010 while (esp->esp_command[0] == 0xff)
3013 esp->esp_command[0] = esp->hme_fifo_workaround_buffer[0];
3014 esp->esp_command[1] = esp->hme_fifo_workaround_buffer[1];
3017 ESPSTAT(("got something, "));
3018 /* ESP chimes in with one of
3020 * 1) function done interrupt:
3021 * both status and message in bytes
3024 * 2) bus service interrupt:
3025 * only status byte was acquired
3028 * can't happen, but we test for it
3031 * ALSO: If bad parity was detected on either
3032 * the status _or_ the message byte then
3033 * the ESP has asserted ATN on the bus
3034 * and we must therefore wait for the
3035 * next phase change.
3037 if (intr & ESP_INTR_FDONE) {
3038 /* We got it all, hallejulia. */
3039 ESPSTAT(("got both, "));
3040 SCptr->SCp.Status = esp->esp_command[0];
3041 SCptr->SCp.Message = esp->esp_command[1];
3042 esp->prevmsgin = SCptr->SCp.Message;
3043 esp->cur_msgin[0] = SCptr->SCp.Message;
3044 if (esp->sreg & ESP_STAT_PERR) {
3045 /* There was bad parity for the
3046 * message byte, the status byte
3049 message_out = MSG_PARITY_ERROR;
3051 } else if (intr == ESP_INTR_BSERV) {
3052 /* Only got status byte. */
3053 ESPLOG(("esp%d: got status only, ", esp->esp_id));
3054 if (!(esp->sreg & ESP_STAT_PERR)) {
3055 SCptr->SCp.Status = esp->esp_command[0];
3056 SCptr->SCp.Message = 0xff;
3058 /* The status byte had bad parity.
3059 * we leave the scsi_pointer Status
3060 * field alone as we set it to a default
3061 * of CHECK_CONDITION in esp_queue.
3063 message_out = INITIATOR_ERROR;
3066 /* This shouldn't happen ever. */
3067 ESPSTAT(("got bolixed\n"));
3068 esp_advance_phase(SCptr, in_the_dark);
3069 return esp_do_phase_determine(esp);
3073 ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
3074 SCptr->SCp.Message));
3075 if (SCptr->SCp.Message == COMMAND_COMPLETE) {
3076 ESPSTAT(("and was COMMAND_COMPLETE\n"));
3077 esp_advance_phase(SCptr, in_freeing);
3078 return esp_do_freebus(esp);
3080 ESPLOG(("esp%d: and _not_ COMMAND_COMPLETE\n",
3082 esp->msgin_len = esp->msgin_ctr = 1;
3083 esp_advance_phase(SCptr, in_msgindone);
3084 return esp_do_msgindone(esp);
3087 /* With luck we'll be able to let the target
3088 * know that bad parity happened, it will know
3089 * which byte caused the problems and send it
3090 * again. For the case where the status byte
3091 * receives bad parity, I do not believe most
3092 * targets recover very well. We'll see.
3094 ESPLOG(("esp%d: bad parity somewhere mout=%2x\n",
3095 esp->esp_id, message_out));
3096 esp->cur_msgout[0] = message_out;
3097 esp->msgout_len = esp->msgout_ctr = 1;
3098 esp_advance_phase(SCptr, in_the_dark);
3099 return esp_do_phase_determine(esp);
3102 /* If we disconnect now, all hell breaks loose. */
3103 ESPLOG(("esp%d: whoops, disconnect\n", esp->esp_id));
3104 esp_advance_phase(SCptr, in_the_dark);
3105 return esp_do_phase_determine(esp);
3109 static int esp_enter_status(struct esp *esp)
3111 u8 thecmd = ESP_CMD_ICCSEQ;
3113 esp_cmd(esp, ESP_CMD_FLUSH);
3114 if (esp->erev != fashme) {
3117 esp->esp_command[0] = esp->esp_command[1] = 0xff;
3118 sbus_writeb(2, esp->eregs + ESP_TCLOW);
3119 sbus_writeb(0, esp->eregs + ESP_TCMED);
3120 tmp = sbus_readl(esp->dregs + DMA_CSR);
3121 tmp |= (DMA_ST_WRITE | DMA_ENABLE);
3122 sbus_writel(tmp, esp->dregs + DMA_CSR);
3123 if (esp->dma->revision == dvmaesc1)
3124 sbus_writel(0x100, esp->dregs + DMA_COUNT);
3125 sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
3126 thecmd |= ESP_CMD_DMA;
3128 esp_cmd(esp, thecmd);
3129 esp_advance_phase(esp->current_SC, in_status);
3131 return esp_do_status(esp);
3134 static int esp_disconnect_amidst_phases(struct esp *esp)
3136 Scsi_Cmnd *sp = esp->current_SC;
3137 struct esp_device *esp_dev = sp->device->hostdata;
3139 /* This means real problems if we see this
3140 * here. Unless we were actually trying
3141 * to force the device to abort/reset.
3143 ESPLOG(("esp%d Disconnect amidst phases, ", esp->esp_id));
3144 ESPLOG(("pphase<%s> cphase<%s>, ",
3145 phase_string(sp->SCp.phase),
3146 phase_string(sp->SCp.sent_command)));
3148 if (esp->disconnected_SC != NULL || (esp->erev == fashme))
3149 esp_cmd(esp, ESP_CMD_ESEL);
3151 switch (esp->cur_msgout[0]) {
3153 /* We didn't expect this to happen at all. */
3154 ESPLOG(("device is bolixed\n"));
3155 esp_advance_phase(sp, in_tgterror);
3156 esp_done(esp, (DID_ERROR << 16));
3159 case BUS_DEVICE_RESET:
3160 ESPLOG(("device reset successful\n"));
3161 esp_dev->sync_max_offset = 0;
3162 esp_dev->sync_min_period = 0;
3164 esp_advance_phase(sp, in_resetdev);
3165 esp_done(esp, (DID_RESET << 16));
3169 ESPLOG(("device abort successful\n"));
3170 esp_advance_phase(sp, in_abortone);
3171 esp_done(esp, (DID_ABORT << 16));
3178 static int esp_enter_msgout(struct esp *esp)
3180 esp_advance_phase(esp->current_SC, in_msgout);
3181 return esp_do_msgout(esp);
3184 static int esp_enter_msgin(struct esp *esp)
3186 esp_advance_phase(esp->current_SC, in_msgin);
3187 return esp_do_msgin(esp);
3190 static int esp_enter_cmd(struct esp *esp)
3192 esp_advance_phase(esp->current_SC, in_cmdbegin);
3193 return esp_do_cmdbegin(esp);
3196 static int esp_enter_badphase(struct esp *esp)
3198 ESPLOG(("esp%d: Bizarre bus phase %2x.\n", esp->esp_id,
3199 esp->sreg & ESP_STAT_PMASK));
3200 return do_reset_bus;
3203 typedef int (*espfunc_t)(struct esp *);
3205 static espfunc_t phase_vector[] = {
3206 esp_do_data, /* ESP_DOP */
3207 esp_do_data, /* ESP_DIP */
3208 esp_enter_cmd, /* ESP_CMDP */
3209 esp_enter_status, /* ESP_STATP */
3210 esp_enter_badphase, /* ESP_STAT_PMSG */
3211 esp_enter_badphase, /* ESP_STAT_PMSG | ESP_STAT_PIO */
3212 esp_enter_msgout, /* ESP_MOP */
3213 esp_enter_msgin, /* ESP_MIP */
3216 /* The target has control of the bus and we have to see where it has
3219 static int esp_do_phase_determine(struct esp *esp)
3221 if ((esp->ireg & ESP_INTR_DC) != 0)
3222 return esp_disconnect_amidst_phases(esp);
3223 return phase_vector[esp->sreg & ESP_STAT_PMASK](esp);
3226 /* First interrupt after exec'ing a cmd comes here. */
3227 static int esp_select_complete(struct esp *esp)
3229 Scsi_Cmnd *SCptr = esp->current_SC;
3230 struct esp_device *esp_dev = SCptr->device->hostdata;
3231 int cmd_bytes_sent, fcnt;
3233 if (esp->erev != fashme)
3234 esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
3236 if (esp->erev == fashme)
3237 fcnt = esp->hme_fifo_workaround_count;
3239 fcnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
3241 cmd_bytes_sent = esp_bytes_sent(esp, fcnt);
3242 dma_invalidate(esp);
3244 /* Let's check to see if a reselect happened
3245 * while we we're trying to select. This must
3248 if (esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
3249 esp_reconnect(esp, SCptr);
3250 return esp_do_reconnect(esp);
3253 /* Looks like things worked, we should see a bus service &
3254 * a function complete interrupt at this point. Note we
3255 * are doing a direct comparison because we don't want to
3256 * be fooled into thinking selection was successful if
3257 * ESP_INTR_DC is set, see below.
3259 if (esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
3260 /* target speaks... */
3261 esp->targets_present |= (1<<SCptr->device->id);
3263 /* What if the target ignores the sdtr? */
3267 /* See how far, if at all, we got in getting
3268 * the information out to the target.
3270 switch (esp->seqreg) {
3274 /* Arbitration won, target selected, but
3275 * we are in some phase which is not command
3276 * phase nor is it message out phase.
3278 * XXX We've confused the target, obviously.
3279 * XXX So clear it's state, but we also end
3280 * XXX up clearing everyone elses. That isn't
3281 * XXX so nice. I'd like to just reset this
3282 * XXX target, but if I cannot even get it's
3283 * XXX attention and finish selection to talk
3284 * XXX to it, there is not much more I can do.
3285 * XXX If we have a loaded bus we're going to
3286 * XXX spend the next second or so renegotiating
3287 * XXX for synchronous transfers.
3289 ESPLOG(("esp%d: STEP_ASEL for tgt %d\n",
3290 esp->esp_id, SCptr->device->id));
3293 /* Arbitration won, target selected, went
3294 * to message out phase, sent one message
3295 * byte, then we stopped. ATN is asserted
3296 * on the SCSI bus and the target is still
3297 * there hanging on. This is a legal
3298 * sequence step if we gave the ESP a select
3301 * XXX See above, I could set the borken flag
3302 * XXX in the device struct and retry the
3303 * XXX command. But would that help for
3304 * XXX tagged capable targets?
3308 /* Arbitration won, target selected, maybe
3309 * sent the one message byte in message out
3310 * phase, but we did not go to command phase
3311 * in the end. Actually, we could have sent
3312 * only some of the message bytes if we tried
3313 * to send out the entire identify and tag
3314 * message using ESP_CMD_SA3.
3320 /* No, not the powerPC pinhead. Arbitration
3321 * won, all message bytes sent if we went to
3322 * message out phase, went to command phase
3323 * but only part of the command was sent.
3325 * XXX I've seen this, but usually in conjunction
3326 * XXX with a gross error which appears to have
3327 * XXX occurred between the time I told the
3328 * XXX ESP to arbitrate and when I got the
3329 * XXX interrupt. Could I have misloaded the
3330 * XXX command bytes into the fifo? Actually,
3331 * XXX I most likely missed a phase, and therefore
3332 * XXX went into never never land and didn't even
3333 * XXX know it. That was the old driver though.
3334 * XXX What is even more peculiar is that the ESP
3335 * XXX showed the proper function complete and
3336 * XXX bus service bits in the interrupt register.
3339 case ESP_STEP_FINI4:
3340 case ESP_STEP_FINI5:
3341 case ESP_STEP_FINI6:
3342 case ESP_STEP_FINI7:
3343 /* Account for the identify message */
3344 if (SCptr->SCp.phase == in_slct_norm)
3345 cmd_bytes_sent -= 1;
3348 if (esp->erev != fashme)
3349 esp_cmd(esp, ESP_CMD_NULL);
3351 /* Be careful, we could really get fucked during synchronous
3352 * data transfers if we try to flush the fifo now.
3354 if ((esp->erev != fashme) && /* not a Happy Meal and... */
3355 !fcnt && /* Fifo is empty and... */
3356 /* either we are not doing synchronous transfers or... */
3357 (!esp_dev->sync_max_offset ||
3358 /* We are not going into data in phase. */
3359 ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
3360 esp_cmd(esp, ESP_CMD_FLUSH); /* flush is safe */
3362 /* See how far we got if this is not a slow command. */
3363 if (!esp->esp_slowcmd) {
3364 if (cmd_bytes_sent < 0)
3366 if (cmd_bytes_sent != SCptr->cmd_len) {
3367 /* Crapola, mark it as a slowcmd
3368 * so that we have some chance of
3369 * keeping the command alive with
3372 * XXX Actually, if we didn't send it all
3373 * XXX this means either we didn't set things
3374 * XXX up properly (driver bug) or the target
3375 * XXX or the ESP detected parity on one of
3376 * XXX the command bytes. This makes much
3377 * XXX more sense, and therefore this code
3378 * XXX should be changed to send out a
3379 * XXX parity error message or if the status
3380 * XXX register shows no parity error then
3381 * XXX just expect the target to bring the
3382 * XXX bus into message in phase so that it
3383 * XXX can send us the parity error message.
3386 esp->esp_slowcmd = 1;
3387 esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
3388 esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
3392 /* Now figure out where we went. */
3393 esp_advance_phase(SCptr, in_the_dark);
3394 return esp_do_phase_determine(esp);
3397 /* Did the target even make it? */
3398 if (esp->ireg == ESP_INTR_DC) {
3399 /* wheee... nobody there or they didn't like
3400 * what we told it to do, clean up.
3403 /* If anyone is off the bus, but working on
3404 * a command in the background for us, tell
3405 * the ESP to listen for them.
3407 if (esp->disconnected_SC)
3408 esp_cmd(esp, ESP_CMD_ESEL);
3410 if (((1<<SCptr->device->id) & esp->targets_present) &&
3412 (esp->cur_msgout[0] == EXTENDED_MESSAGE) &&
3413 (SCptr->SCp.phase == in_slct_msg ||
3414 SCptr->SCp.phase == in_slct_stop)) {
3417 ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
3418 "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
3419 esp_dev->sync_max_offset = 0;
3420 esp_dev->sync_min_period = 0;
3421 esp_dev->sync = 1; /* so we don't negotiate again */
3423 /* Run the command again, this time though we
3424 * won't try to negotiate for synchronous transfers.
3426 * XXX I'd like to do something like send an
3427 * XXX INITIATOR_ERROR or ABORT message to the
3428 * XXX target to tell it, "Sorry I confused you,
3429 * XXX please come back and I will be nicer next
3430 * XXX time". But that requires having the target
3431 * XXX on the bus, and it has dropped BSY on us.
3433 esp->current_SC = NULL;
3434 esp_advance_phase(SCptr, not_issued);
3435 prepend_SC(&esp->issue_SC, SCptr);
3440 /* Ok, this is normal, this is what we see during boot
3441 * or whenever when we are scanning the bus for targets.
3442 * But first make sure that is really what is happening.
3444 if (((1<<SCptr->device->id) & esp->targets_present)) {
3445 ESPLOG(("esp%d: Warning, live target %d not responding to "
3446 "selection.\n", esp->esp_id, SCptr->device->id));
3448 /* This _CAN_ happen. The SCSI standard states that
3449 * the target is to _not_ respond to selection if
3450 * _it_ detects bad parity on the bus for any reason.
3451 * Therefore, we assume that if we've talked successfully
3452 * to this target before, bad parity is the problem.
3454 esp_done(esp, (DID_PARITY << 16));
3456 /* Else, there really isn't anyone there. */
3457 ESPMISC(("esp: selection failure, maybe nobody there?\n"));
3458 ESPMISC(("esp: target %d lun %d\n",
3459 SCptr->device->id, SCptr->device->lun));
3460 esp_done(esp, (DID_BAD_TARGET << 16));
3465 ESPLOG(("esp%d: Selection failure.\n", esp->esp_id));
3466 printk("esp%d: Currently -- ", esp->esp_id);
3467 esp_print_ireg(esp->ireg); printk(" ");
3468 esp_print_statreg(esp->sreg); printk(" ");
3469 esp_print_seqreg(esp->seqreg); printk("\n");
3470 printk("esp%d: New -- ", esp->esp_id);
3471 esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
3472 esp->seqreg = sbus_readb(esp->eregs + ESP_SSTEP);
3473 esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
3474 esp_print_ireg(esp->ireg); printk(" ");
3475 esp_print_statreg(esp->sreg); printk(" ");
3476 esp_print_seqreg(esp->seqreg); printk("\n");
3477 ESPLOG(("esp%d: resetting bus\n", esp->esp_id));
3478 return do_reset_bus; /* ugh... */
3481 /* Continue reading bytes for msgin phase. */
3482 static int esp_do_msgincont(struct esp *esp)
3484 if (esp->ireg & ESP_INTR_BSERV) {
3485 /* in the right phase too? */
3486 if ((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
3488 esp_cmd(esp, ESP_CMD_TI);
3489 esp_advance_phase(esp->current_SC, in_msgindone);
3493 /* We changed phase but ESP shows bus service,
3494 * in this case it is most likely that we, the
3495 * hacker who has been up for 20hrs straight
3496 * staring at the screen, drowned in coffee
3497 * smelling like retched cigarette ashes
3498 * have miscoded something..... so, try to
3499 * recover as best we can.
3501 ESPLOG(("esp%d: message in mis-carriage.\n", esp->esp_id));
3503 esp_advance_phase(esp->current_SC, in_the_dark);
3504 return do_phase_determine;
3507 static int check_singlebyte_msg(struct esp *esp)
3509 esp->prevmsgin = esp->cur_msgin[0];
3510 if (esp->cur_msgin[0] & 0x80) {
3512 ESPLOG(("esp%d: target sends identify amidst phases\n",
3514 esp_advance_phase(esp->current_SC, in_the_dark);
3516 } else if (((esp->cur_msgin[0] & 0xf0) == 0x20) ||
3517 (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
3519 esp_advance_phase(esp->current_SC, in_msgincont);
3522 esp_advance_phase(esp->current_SC, in_the_dark);
3523 switch (esp->cur_msgin[0]) {
3525 /* We don't want to hear about it. */
3526 ESPLOG(("esp%d: msg %02x which we don't know about\n", esp->esp_id,
3527 esp->cur_msgin[0]));
3528 return MESSAGE_REJECT;
3531 ESPLOG(("esp%d: target %d sends a nop\n", esp->esp_id,
3532 esp->current_SC->device->id));
3535 case RESTORE_POINTERS:
3536 /* In this case we might also have to backup the
3537 * "slow command" pointer. It is rare to get such
3538 * a save/restore pointer sequence so early in the
3539 * bus transition sequences, but cover it.
3541 if (esp->esp_slowcmd) {
3542 esp->esp_scmdleft = esp->current_SC->cmd_len;
3543 esp->esp_scmdp = &esp->current_SC->cmnd[0];
3545 esp_restore_pointers(esp, esp->current_SC);
3549 esp_save_pointers(esp, esp->current_SC);
3552 case COMMAND_COMPLETE:
3554 /* Freeing the bus, let it go. */
3555 esp->current_SC->SCp.phase = in_freeing;
3558 case MESSAGE_REJECT:
3559 ESPMISC(("msg reject, "));
3560 if (esp->prevmsgout == EXTENDED_MESSAGE) {
3561 struct esp_device *esp_dev = esp->current_SC->device->hostdata;
3563 /* Doesn't look like this target can
3564 * do synchronous or WIDE transfers.
3566 ESPSDTR(("got reject, was trying nego, clearing sync/WIDE\n"));
3569 esp_dev->sync_min_period = 0;
3570 esp_dev->sync_max_offset = 0;
3573 ESPMISC(("not sync nego, sending ABORT\n"));
3579 /* Target negotiates for synchronous transfers before we do, this
3580 * is legal although very strange. What is even funnier is that
3581 * the SCSI2 standard specifically recommends against targets doing
3582 * this because so many initiators cannot cope with this occurring.
3584 static int target_with_ants_in_pants(struct esp *esp,
3586 struct esp_device *esp_dev)
3588 if (esp_dev->sync || SCptr->device->borken) {
3589 /* sorry, no can do */
3590 ESPSDTR(("forcing to async, "));
3591 build_sync_nego_msg(esp, 0, 0);
3594 ESPLOG(("esp%d: hoping for msgout\n", esp->esp_id));
3595 esp_advance_phase(SCptr, in_the_dark);
3596 return EXTENDED_MESSAGE;
3599 /* Ok, we'll check them out... */
3603 static void sync_report(struct esp *esp)
3608 msg3 = esp->cur_msgin[3];
3609 msg4 = esp->cur_msgin[4];
3611 int hz = 1000000000 / (msg3 * 4);
3612 int integer = hz / 1000000;
3613 int fraction = (hz - (integer * 1000000)) / 10000;
3614 if ((esp->erev == fashme) &&
3615 (esp->config3[esp->current_SC->device->id] & ESP_CONFIG3_EWIDE)) {
3619 } else if ((msg3 * 4) < 200) {
3622 type = "synchronous";
3625 /* Do not transform this back into one big printk
3626 * again, it triggers a bug in our sparc64-gcc272
3627 * sibling call optimization. -DaveM
3629 ESPLOG((KERN_INFO "esp%d: target %d ",
3630 esp->esp_id, esp->current_SC->device->id));
3631 ESPLOG(("[period %dns offset %d %d.%02dMHz ",
3632 (int) msg3 * 4, (int) msg4,
3633 integer, fraction));
3634 ESPLOG(("%s SCSI%s]\n", type,
3635 (((msg3 * 4) < 200) ? "-II" : "")));
3637 ESPLOG((KERN_INFO "esp%d: target %d asynchronous\n",
3638 esp->esp_id, esp->current_SC->device->id));
3642 static int check_multibyte_msg(struct esp *esp)
3644 Scsi_Cmnd *SCptr = esp->current_SC;
3645 struct esp_device *esp_dev = SCptr->device->hostdata;
3647 int message_out = 0;
3649 ESPSDTR(("chk multibyte msg: "));
3650 if (esp->cur_msgin[2] == EXTENDED_SDTR) {
3651 int period = esp->cur_msgin[3];
3652 int offset = esp->cur_msgin[4];
3654 ESPSDTR(("is sync nego response, "));
3658 /* Target negotiates first! */
3659 ESPSDTR(("target jumps the gun, "));
3660 message_out = EXTENDED_MESSAGE; /* we must respond */
3661 rval = target_with_ants_in_pants(esp, SCptr, esp_dev);
3666 ESPSDTR(("examining sdtr, "));
3668 /* Offset cannot be larger than ESP fifo size. */
3670 ESPSDTR(("offset too big %2x, ", offset));
3672 ESPSDTR(("sending back new offset\n"));
3673 build_sync_nego_msg(esp, period, offset);
3674 return EXTENDED_MESSAGE;
3677 if (offset && period > esp->max_period) {
3678 /* Yeee, async for this slow device. */
3679 ESPSDTR(("period too long %2x, ", period));
3680 build_sync_nego_msg(esp, 0, 0);
3681 ESPSDTR(("hoping for msgout\n"));
3682 esp_advance_phase(esp->current_SC, in_the_dark);
3683 return EXTENDED_MESSAGE;
3684 } else if (offset && period < esp->min_period) {
3685 ESPSDTR(("period too short %2x, ", period));
3686 period = esp->min_period;
3687 if (esp->erev > esp236)
3691 } else if (offset) {
3694 ESPSDTR(("period is ok, "));
3695 tmp = esp->ccycle / 1000;
3696 regval = (((period << 2) + tmp - 1) / tmp);
3697 if (regval && ((esp->erev == fas100a ||
3698 esp->erev == fas236 ||
3699 esp->erev == fashme))) {
3708 esp_dev->sync_min_period = (regval & 0x1f);
3709 esp_dev->sync_max_offset = (offset | esp->radelay);
3710 if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
3711 if ((esp->erev == fas100a) || (esp->erev == fashme))
3712 bit = ESP_CONFIG3_FAST;
3714 bit = ESP_CONFIG3_FSCSI;
3716 /* On FAS366, if using fast-20 synchronous transfers
3717 * we need to make sure the REQ/ACK assert/deassert
3718 * control bits are clear.
3720 if (esp->erev == fashme)
3721 esp_dev->sync_max_offset &= ~esp->radelay;
3722 esp->config3[SCptr->device->id] |= bit;
3724 esp->config3[SCptr->device->id] &= ~bit;
3726 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3727 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
3729 esp->prev_soff = esp_dev->sync_max_offset;
3730 esp->prev_stp = esp_dev->sync_min_period;
3731 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
3732 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
3733 ESPSDTR(("soff=%2x stp=%2x cfg3=%2x\n",
3734 esp_dev->sync_max_offset,
3735 esp_dev->sync_min_period,
3736 esp->config3[SCptr->device->id]));
3739 } else if (esp_dev->sync_max_offset) {
3742 /* back to async mode */
3743 ESPSDTR(("unaccaptable sync nego, forcing async\n"));
3744 esp_dev->sync_max_offset = 0;
3745 esp_dev->sync_min_period = 0;
3748 sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
3749 sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
3750 if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
3751 if ((esp->erev == fas100a) || (esp->erev == fashme))
3752 bit = ESP_CONFIG3_FAST;
3754 bit = ESP_CONFIG3_FSCSI;
3755 esp->config3[SCptr->device->id] &= ~bit;
3756 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3757 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
3763 ESPSDTR(("chk multibyte msg: sync is known, "));
3767 ESPLOG(("esp%d: sending sdtr back, hoping for msgout\n",
3769 build_sync_nego_msg(esp, period, offset);
3770 esp_advance_phase(SCptr, in_the_dark);
3771 return EXTENDED_MESSAGE;
3774 ESPSDTR(("returning zero\n"));
3775 esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
3777 } else if (esp->cur_msgin[2] == EXTENDED_WDTR) {
3778 int size = 8 << esp->cur_msgin[3];
3781 if (esp->erev != fashme) {
3782 ESPLOG(("esp%d: AIEEE wide msg received and not HME.\n",
3784 message_out = MESSAGE_REJECT;
3785 } else if (size > 16) {
3786 ESPLOG(("esp%d: AIEEE wide transfer for %d size "
3787 "not supported.\n", esp->esp_id, size));
3788 message_out = MESSAGE_REJECT;
3790 /* Things look good; let's see what we got. */
3792 /* Set config 3 register for this target. */
3793 esp->config3[SCptr->device->id] |= ESP_CONFIG3_EWIDE;
3795 /* Just make sure it was one byte sized. */
3797 ESPLOG(("esp%d: Aieee, wide nego of %d size.\n",
3798 esp->esp_id, size));
3799 message_out = MESSAGE_REJECT;
3802 /* Pure paranoia. */
3803 esp->config3[SCptr->device->id] &= ~(ESP_CONFIG3_EWIDE);
3805 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3806 sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
3808 /* Regardless, next try for sync transfers. */
3809 build_sync_nego_msg(esp, esp->sync_defp, 15);
3812 message_out = EXTENDED_MESSAGE;
3814 } else if (esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
3815 ESPLOG(("esp%d: rejecting modify data ptr msg\n", esp->esp_id));
3816 message_out = MESSAGE_REJECT;
3819 esp_advance_phase(SCptr, in_the_dark);
3823 static int esp_do_msgindone(struct esp *esp)
3825 Scsi_Cmnd *SCptr = esp->current_SC;
3826 int message_out = 0, it = 0, rval;
3828 rval = skipahead1(esp, SCptr, in_msgin, in_msgindone);
3831 if (SCptr->SCp.sent_command != in_status) {
3832 if (!(esp->ireg & ESP_INTR_DC)) {
3833 if (esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
3834 message_out = MSG_PARITY_ERROR;
3835 esp_cmd(esp, ESP_CMD_FLUSH);
3836 } else if (esp->erev != fashme &&
3837 (it = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES)) != 1) {
3838 /* We certainly dropped the ball somewhere. */
3839 message_out = INITIATOR_ERROR;
3840 esp_cmd(esp, ESP_CMD_FLUSH);
3841 } else if (!esp->msgin_len) {
3842 if (esp->erev == fashme)
3843 it = esp->hme_fifo_workaround_buffer[0];
3845 it = sbus_readb(esp->eregs + ESP_FDATA);
3846 esp_advance_phase(SCptr, in_msgincont);
3848 /* it is ok and we want it */
3849 if (esp->erev == fashme)
3850 it = esp->cur_msgin[esp->msgin_ctr] =
3851 esp->hme_fifo_workaround_buffer[0];
3853 it = esp->cur_msgin[esp->msgin_ctr] =
3854 sbus_readb(esp->eregs + ESP_FDATA);
3858 esp_advance_phase(SCptr, in_the_dark);
3862 it = esp->cur_msgin[0];
3864 if (!message_out && esp->msgin_len) {
3865 if (esp->msgin_ctr < esp->msgin_len) {
3866 esp_advance_phase(SCptr, in_msgincont);
3867 } else if (esp->msgin_len == 1) {
3868 message_out = check_singlebyte_msg(esp);
3869 } else if (esp->msgin_len == 2) {
3870 if (esp->cur_msgin[0] == EXTENDED_MESSAGE) {
3871 if ((it + 2) >= 15) {
3872 message_out = MESSAGE_REJECT;
3874 esp->msgin_len = (it + 2);
3875 esp_advance_phase(SCptr, in_msgincont);
3878 message_out = MESSAGE_REJECT; /* foo on you */
3881 message_out = check_multibyte_msg(esp);
3884 if (message_out < 0) {
3885 return -message_out;
3886 } else if (message_out) {
3887 if (((message_out != 1) &&
3888 ((message_out < 0x20) || (message_out & 0x80))))
3889 esp->msgout_len = 1;
3890 esp->cur_msgout[0] = message_out;
3891 esp_cmd(esp, ESP_CMD_SATN);
3892 esp_advance_phase(SCptr, in_the_dark);
3895 esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
3896 esp->sreg &= ~(ESP_STAT_INTR);
3897 if ((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
3898 esp_cmd(esp, ESP_CMD_MOK);
3899 if ((SCptr->SCp.sent_command == in_msgindone) &&
3900 (SCptr->SCp.phase == in_freeing))
3901 return esp_do_freebus(esp);
3905 static int esp_do_cmdbegin(struct esp *esp)
3907 Scsi_Cmnd *SCptr = esp->current_SC;
3909 esp_advance_phase(SCptr, in_cmdend);
3910 if (esp->erev == fashme) {
3911 u32 tmp = sbus_readl(esp->dregs + DMA_CSR);
3914 for (i = 0; i < esp->esp_scmdleft; i++)
3915 esp->esp_command[i] = *esp->esp_scmdp++;
3916 esp->esp_scmdleft = 0;
3917 esp_cmd(esp, ESP_CMD_FLUSH);
3918 esp_setcount(esp->eregs, i, 1);
3919 esp_cmd(esp, (ESP_CMD_DMA | ESP_CMD_TI));
3920 tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
3921 tmp &= ~(DMA_ST_WRITE);
3922 sbus_writel(i, esp->dregs + DMA_COUNT);
3923 sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
3924 sbus_writel(tmp, esp->dregs + DMA_CSR);
3928 esp_cmd(esp, ESP_CMD_FLUSH);
3929 tmp = *esp->esp_scmdp++;
3930 esp->esp_scmdleft--;
3931 sbus_writeb(tmp, esp->eregs + ESP_FDATA);
3932 esp_cmd(esp, ESP_CMD_TI);
3937 static int esp_do_cmddone(struct esp *esp)
3939 if (esp->erev == fashme)
3940 dma_invalidate(esp);
3942 esp_cmd(esp, ESP_CMD_NULL);
3944 if (esp->ireg & ESP_INTR_BSERV) {
3945 esp_advance_phase(esp->current_SC, in_the_dark);
3946 return esp_do_phase_determine(esp);
3949 ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.\n",
3951 return do_reset_bus;
3954 static int esp_do_msgout(struct esp *esp)
3956 esp_cmd(esp, ESP_CMD_FLUSH);
3957 switch (esp->msgout_len) {
3959 if (esp->erev == fashme)
3960 hme_fifo_push(esp, &esp->cur_msgout[0], 1);
3962 sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
3964 esp_cmd(esp, ESP_CMD_TI);
3968 esp->esp_command[0] = esp->cur_msgout[0];
3969 esp->esp_command[1] = esp->cur_msgout[1];
3971 if (esp->erev == fashme) {
3972 hme_fifo_push(esp, &esp->cur_msgout[0], 2);
3973 esp_cmd(esp, ESP_CMD_TI);
3975 dma_setup(esp, esp->esp_command_dvma, 2, 0);
3976 esp_setcount(esp->eregs, 2, 0);
3977 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
3982 esp->esp_command[0] = esp->cur_msgout[0];
3983 esp->esp_command[1] = esp->cur_msgout[1];
3984 esp->esp_command[2] = esp->cur_msgout[2];
3985 esp->esp_command[3] = esp->cur_msgout[3];
3988 if (esp->erev == fashme) {
3989 hme_fifo_push(esp, &esp->cur_msgout[0], 4);
3990 esp_cmd(esp, ESP_CMD_TI);
3992 dma_setup(esp, esp->esp_command_dvma, 4, 0);
3993 esp_setcount(esp->eregs, 4, 0);
3994 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
3999 esp->esp_command[0] = esp->cur_msgout[0];
4000 esp->esp_command[1] = esp->cur_msgout[1];
4001 esp->esp_command[2] = esp->cur_msgout[2];
4002 esp->esp_command[3] = esp->cur_msgout[3];
4003 esp->esp_command[4] = esp->cur_msgout[4];
4006 if (esp->erev == fashme) {
4007 hme_fifo_push(esp, &esp->cur_msgout[0], 5);
4008 esp_cmd(esp, ESP_CMD_TI);
4010 dma_setup(esp, esp->esp_command_dvma, 5, 0);
4011 esp_setcount(esp->eregs, 5, 0);
4012 esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
4018 ESPMISC(("bogus msgout sending NOP\n"));
4019 esp->cur_msgout[0] = NOP;
4021 if (esp->erev == fashme) {
4022 hme_fifo_push(esp, &esp->cur_msgout[0], 1);
4024 sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
4027 esp->msgout_len = 1;
4028 esp_cmd(esp, ESP_CMD_TI);
4032 esp_advance_phase(esp->current_SC, in_msgoutdone);
4036 static int esp_do_msgoutdone(struct esp *esp)
4038 if (esp->msgout_len > 1) {
4039 /* XXX HME/FAS ATN deassert workaround required,
4040 * XXX no DMA flushing, only possible ESP_CMD_FLUSH
4041 * XXX to kill the fifo.
4043 if (esp->erev != fashme) {
4046 while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
4049 sbus_writel(tmp, esp->dregs + DMA_CSR);
4050 dma_invalidate(esp);
4052 esp_cmd(esp, ESP_CMD_FLUSH);
4055 if (!(esp->ireg & ESP_INTR_DC)) {
4056 if (esp->erev != fashme)
4057 esp_cmd(esp, ESP_CMD_NULL);
4058 switch (esp->sreg & ESP_STAT_PMASK) {
4060 /* whoops, parity error */
4061 ESPLOG(("esp%d: still in msgout, parity error assumed\n",
4063 if (esp->msgout_len > 1)
4064 esp_cmd(esp, ESP_CMD_SATN);
4065 esp_advance_phase(esp->current_SC, in_msgout);
4072 /* Happy Meal fifo is touchy... */
4073 if ((esp->erev != fashme) &&
4075 !(((struct esp_device *)esp->current_SC->device->hostdata)->sync_max_offset))
4076 esp_cmd(esp, ESP_CMD_FLUSH);
4081 ESPLOG(("esp%d: disconnect, resetting bus\n", esp->esp_id));
4082 return do_reset_bus;
4085 /* If we sent out a synchronous negotiation message, update
4088 if (esp->cur_msgout[2] == EXTENDED_MESSAGE &&
4089 esp->cur_msgout[4] == EXTENDED_SDTR) {
4090 esp->snip = 1; /* anal retentiveness... */
4093 esp->prevmsgout = esp->cur_msgout[0];
4094 esp->msgout_len = 0;
4095 esp_advance_phase(esp->current_SC, in_the_dark);
4096 return esp_do_phase_determine(esp);
4099 static int esp_bus_unexpected(struct esp *esp)
4101 ESPLOG(("esp%d: command in weird state %2x\n",
4102 esp->esp_id, esp->current_SC->SCp.phase));
4103 return do_reset_bus;
4106 static espfunc_t bus_vector[] = {
4119 esp_do_phase_determine,
4125 /* This is the second tier in our dual-level SCSI state machine. */
4126 static int esp_work_bus(struct esp *esp)
4128 Scsi_Cmnd *SCptr = esp->current_SC;
4131 ESPBUS(("esp_work_bus: "));
4133 ESPBUS(("reconnect\n"));
4134 return esp_do_reconnect(esp);
4136 phase = SCptr->SCp.phase;
4137 if ((phase & 0xf0) == in_phases_mask)
4138 return bus_vector[(phase & 0x0f)](esp);
4139 else if ((phase & 0xf0) == in_slct_mask)
4140 return esp_select_complete(esp);
4142 return esp_bus_unexpected(esp);
4145 static espfunc_t isvc_vector[] = {
4147 esp_do_phase_determine,
4153 /* Main interrupt handler for an esp adapter. */
4154 static void esp_handle(struct esp *esp)
4157 int what_next = do_intr_end;
4159 SCptr = esp->current_SC;
4161 /* Check for errors. */
4162 esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
4163 esp->sreg &= (~ESP_STAT_INTR);
4164 if (esp->erev == fashme) {
4165 esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
4166 esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
4169 if (esp->sreg & (ESP_STAT_SPAM)) {
4170 /* Gross error, could be due to one of:
4172 * - top of fifo overwritten, could be because
4173 * we tried to do a synchronous transfer with
4174 * an offset greater than ESP fifo size
4176 * - top of command register overwritten
4178 * - DMA setup to go in one direction, SCSI
4179 * bus points in the other, whoops
4181 * - weird phase change during asynchronous
4182 * data phase while we are initiator
4184 ESPLOG(("esp%d: Gross error sreg=%2x\n", esp->esp_id, esp->sreg));
4186 /* If a command is live on the bus we cannot safely
4187 * reset the bus, so we'll just let the pieces fall
4188 * where they may. Here we are hoping that the
4189 * target will be able to cleanly go away soon
4190 * so we can safely reset things.
4193 ESPLOG(("esp%d: No current cmd during gross error, "
4194 "resetting bus\n", esp->esp_id));
4195 what_next = do_reset_bus;
4200 if (sbus_readl(esp->dregs + DMA_CSR) & DMA_HNDL_ERROR) {
4201 /* A DMA gate array error. Here we must
4202 * be seeing one of two things. Either the
4203 * virtual to physical address translation
4204 * on the SBUS could not occur, else the
4205 * translation it did get pointed to a bogus
4208 ESPLOG(("esp%d: DMA error %08x\n", esp->esp_id,
4209 sbus_readl(esp->dregs + DMA_CSR)));
4211 /* DMA gate array itself must be reset to clear the
4216 what_next = do_reset_bus;
4220 esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT); /* Unlatch intr reg */
4222 if (esp->erev == fashme) {
4223 /* This chip is really losing. */
4226 ESPHME(("sreg2=%02x,", esp->sreg2));
4227 /* Must latch fifo before reading the interrupt
4228 * register else garbage ends up in the FIFO
4229 * which confuses the driver utterly.
4231 if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
4232 (esp->sreg2 & ESP_STAT2_F1BYTE)) {
4233 ESPHME(("fifo_workaround]"));
4236 ESPHME(("no_fifo_workaround]"));
4240 /* No current cmd is only valid at this point when there are
4241 * commands off the bus or we are trying a reset.
4243 if (!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
4244 /* Panic is safe, since current_SC is null. */
4245 ESPLOG(("esp%d: no command in esp_handle()\n", esp->esp_id));
4246 panic("esp_handle: current_SC == penguin within interrupt!");
4249 if (esp->ireg & (ESP_INTR_IC)) {
4250 /* Illegal command fed to ESP. Outside of obvious
4251 * software bugs that could cause this, there is
4252 * a condition with esp100 where we can confuse the
4253 * ESP into an erroneous illegal command interrupt
4254 * because it does not scrape the FIFO properly
4255 * for reselection. See esp100_reconnect_hwbug()
4256 * to see how we try very hard to avoid this.
4258 ESPLOG(("esp%d: invalid command\n", esp->esp_id));
4260 esp_dump_state(esp);
4262 if (SCptr != NULL) {
4263 /* Devices with very buggy firmware can drop BSY
4264 * during a scatter list interrupt when using sync
4265 * mode transfers. We continue the transfer as
4266 * expected, the target drops the bus, the ESP
4267 * gets confused, and we get a illegal command
4268 * interrupt because the bus is in the disconnected
4269 * state now and ESP_CMD_TI is only allowed when
4270 * a nexus is alive on the bus.
4272 ESPLOG(("esp%d: Forcing async and disabling disconnect for "
4273 "target %d\n", esp->esp_id, SCptr->device->id));
4274 SCptr->device->borken = 1; /* foo on you */
4277 what_next = do_reset_bus;
4278 } else if (!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
4280 unsigned int phase = SCptr->SCp.phase;
4282 if (phase & in_phases_mask) {
4283 what_next = esp_work_bus(esp);
4284 } else if (phase & in_slct_mask) {
4285 what_next = esp_select_complete(esp);
4287 ESPLOG(("esp%d: interrupt for no good reason...\n",
4289 what_next = do_intr_end;
4292 ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULL\n",
4294 what_next = do_reset_bus;
4296 } else if (esp->ireg & ESP_INTR_SR) {
4297 ESPLOG(("esp%d: SCSI bus reset interrupt\n", esp->esp_id));
4298 what_next = do_reset_complete;
4299 } else if (esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
4300 ESPLOG(("esp%d: AIEEE we have been selected by another initiator!\n",
4302 what_next = do_reset_bus;
4303 } else if (esp->ireg & ESP_INTR_RSEL) {
4304 if (SCptr == NULL) {
4306 what_next = esp_do_reconnect(esp);
4307 } else if (SCptr->SCp.phase & in_slct_mask) {
4308 /* Only selection code knows how to clean
4311 ESPDISC(("Reselected during selection attempt\n"));
4312 what_next = esp_select_complete(esp);
4314 ESPLOG(("esp%d: Reselected while bus is busy\n",
4316 what_next = do_reset_bus;
4320 /* This is tier-one in our dual level SCSI state machine. */
4322 while (what_next != do_intr_end) {
4323 if (what_next >= do_phase_determine &&
4324 what_next < do_intr_end) {
4325 what_next = isvc_vector[what_next](esp);
4327 /* state is completely lost ;-( */
4328 ESPLOG(("esp%d: interrupt engine loses state, resetting bus\n",
4330 what_next = do_reset_bus;
4335 /* Service only the ESP described by dev_id. */
4336 static irqreturn_t esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
4338 struct esp *esp = dev_id;
4339 unsigned long flags;
4341 spin_lock_irqsave(esp->ehost->host_lock, flags);
4342 if (ESP_IRQ_P(esp->dregs)) {
4343 ESP_INTSOFF(esp->dregs);
4345 ESPIRQ(("I[%d:%d](", smp_processor_id(), esp->esp_id));
4349 ESP_INTSON(esp->dregs);
4351 spin_unlock_irqrestore(esp->ehost->host_lock, flags);
4356 static int esp_slave_alloc(Scsi_Device *SDptr)
4358 struct esp_device *esp_dev =
4359 kmalloc(sizeof(struct esp_device), GFP_ATOMIC);
4363 memset(esp_dev, 0, sizeof(struct esp_device));
4364 SDptr->hostdata = esp_dev;
4368 static void esp_slave_destroy(Scsi_Device *SDptr)
4370 struct esp *esp = (struct esp *) SDptr->host->hostdata;
4372 esp->targets_present &= ~(1 << SDptr->id);
4373 kfree(SDptr->hostdata);
4374 SDptr->hostdata = NULL;
4377 static Scsi_Host_Template driver_template = {
4379 .proc_info = esp_proc_info,
4380 .name = "Sun ESP 100/100a/200",
4381 .detect = esp_detect,
4382 .slave_alloc = esp_slave_alloc,
4383 .slave_destroy = esp_slave_destroy,
4384 .release = esp_release,
4386 .queuecommand = esp_queue,
4387 .eh_abort_handler = esp_abort,
4388 .eh_bus_reset_handler = esp_reset,
4391 .sg_tablesize = SG_ALL,
4393 .use_clustering = ENABLE_CLUSTERING,
4396 #include "scsi_module.c"
4398 MODULE_LICENSE("GPL");