1 /* NCR53C9x.c: Generic SCSI driver code for NCR53C9x chips.
3 * Originally esp.c : EnhancedScsiProcessor Sun SCSI driver code.
5 * Copyright (C) 1995, 1998 David S. Miller (davem@caip.rutgers.edu)
7 * Most DMA dependencies put in driver specific files by
8 * Jesper Skov (jskov@cygnus.co.uk)
10 * Set up to use esp_read/esp_write (preprocessor macros in NCR53c9x.h) by
11 * Tymm Twillman (tymm@coe.missouri.edu)
16 * 1) Maybe disable parity checking in config register one for SCSI1
17 * targets. (Gilmore says parity error on the SBus can lock up
19 * 2) Add support for DMA2 pipelining.
20 * 3) Add tagged queueing.
21 * 4) Maybe change use of "esp" to something more "NCR"'ish.
24 #include <linux/module.h>
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/slab.h>
32 #include <linux/blkdev.h>
33 #include <linux/interrupt.h>
34 #include <linux/proc_fs.h>
35 #include <linux/stat.h>
36 #include <linux/init.h>
39 #include <scsi/scsi_host.h>
42 #include <asm/system.h>
43 #include <asm/ptrace.h>
44 #include <asm/pgtable.h>
48 /* Command phase enumeration. */
50 not_issued = 0x00, /* Still in the issue_SC queue. */
52 /* Various forms of selecting a target. */
53 #define in_slct_mask 0x10
54 in_slct_norm = 0x10, /* ESP is arbitrating, normal selection */
55 in_slct_stop = 0x11, /* ESP will select, then stop with IRQ */
56 in_slct_msg = 0x12, /* select, then send a message */
57 in_slct_tag = 0x13, /* select and send tagged queue msg */
58 in_slct_sneg = 0x14, /* select and acquire sync capabilities */
60 /* Any post selection activity. */
61 #define in_phases_mask 0x20
62 in_datain = 0x20, /* Data is transferring from the bus */
63 in_dataout = 0x21, /* Data is transferring to the bus */
64 in_data_done = 0x22, /* Last DMA data operation done (maybe) */
65 in_msgin = 0x23, /* Eating message from target */
66 in_msgincont = 0x24, /* Eating more msg bytes from target */
67 in_msgindone = 0x25, /* Decide what to do with what we got */
68 in_msgout = 0x26, /* Sending message to target */
69 in_msgoutdone = 0x27, /* Done sending msg out */
70 in_cmdbegin = 0x28, /* Sending cmd after abnormal selection */
71 in_cmdend = 0x29, /* Done sending slow cmd */
72 in_status = 0x2a, /* Was in status phase, finishing cmd */
73 in_freeing = 0x2b, /* freeing the bus for cmd cmplt or disc */
74 in_the_dark = 0x2c, /* Don't know what bus phase we are in */
76 /* Special states, ie. not normal bus transitions... */
77 #define in_spec_mask 0x80
78 in_abortone = 0x80, /* Aborting one command currently */
79 in_abortall = 0x81, /* Blowing away all commands we have */
80 in_resetdev = 0x82, /* SCSI target reset in progress */
81 in_resetbus = 0x83, /* SCSI bus reset in progress */
82 in_tgterror = 0x84, /* Target did something stupid */
86 /* Zero has special meaning, see skipahead[12]. */
89 /*1*/ do_phase_determine,
91 /*3*/ do_reset_complete,
96 /* The master ring of all esp hosts we are managing in this driver. */
97 static struct NCR_ESP *espchain;
98 int nesps = 0, esps_in_use = 0, esps_running = 0;
100 irqreturn_t esp_intr(int irq, void *dev_id, struct pt_regs *pregs);
102 /* Debugging routines */
103 static struct esp_cmdstrings {
106 } esp_cmd_strings[] = {
108 { ESP_CMD_NULL, "ESP_NOP", },
109 { ESP_CMD_FLUSH, "FIFO_FLUSH", },
110 { ESP_CMD_RC, "RSTESP", },
111 { ESP_CMD_RS, "RSTSCSI", },
112 /* Disconnected State Group */
113 { ESP_CMD_RSEL, "RESLCTSEQ", },
114 { ESP_CMD_SEL, "SLCTNATN", },
115 { ESP_CMD_SELA, "SLCTATN", },
116 { ESP_CMD_SELAS, "SLCTATNSTOP", },
117 { ESP_CMD_ESEL, "ENSLCTRESEL", },
118 { ESP_CMD_DSEL, "DISSELRESEL", },
119 { ESP_CMD_SA3, "SLCTATN3", },
120 { ESP_CMD_RSEL3, "RESLCTSEQ", },
121 /* Target State Group */
122 { ESP_CMD_SMSG, "SNDMSG", },
123 { ESP_CMD_SSTAT, "SNDSTATUS", },
124 { ESP_CMD_SDATA, "SNDDATA", },
125 { ESP_CMD_DSEQ, "DISCSEQ", },
126 { ESP_CMD_TSEQ, "TERMSEQ", },
127 { ESP_CMD_TCCSEQ, "TRGTCMDCOMPSEQ", },
128 { ESP_CMD_DCNCT, "DISC", },
129 { ESP_CMD_RMSG, "RCVMSG", },
130 { ESP_CMD_RCMD, "RCVCMD", },
131 { ESP_CMD_RDATA, "RCVDATA", },
132 { ESP_CMD_RCSEQ, "RCVCMDSEQ", },
133 /* Initiator State Group */
134 { ESP_CMD_TI, "TRANSINFO", },
135 { ESP_CMD_ICCSEQ, "INICMDSEQCOMP", },
136 { ESP_CMD_MOK, "MSGACCEPTED", },
137 { ESP_CMD_TPAD, "TPAD", },
138 { ESP_CMD_SATN, "SATN", },
139 { ESP_CMD_RATN, "RATN", },
141 #define NUM_ESP_COMMANDS ((sizeof(esp_cmd_strings)) / (sizeof(struct esp_cmdstrings)))
143 /* Print textual representation of an ESP command */
144 static inline void esp_print_cmd(unchar espcmd)
146 unchar dma_bit = espcmd & ESP_CMD_DMA;
150 for(i=0; i<NUM_ESP_COMMANDS; i++)
151 if(esp_cmd_strings[i].cmdchar == espcmd)
153 if(i==NUM_ESP_COMMANDS)
154 printk("ESP_Unknown");
156 printk("%s%s", esp_cmd_strings[i].text,
157 ((dma_bit) ? "+DMA" : ""));
160 /* Print the status register's value */
161 static inline void esp_print_statreg(unchar statreg)
166 phase = statreg & ESP_STAT_PMASK;
167 printk("%s,", (phase == ESP_DOP ? "DATA-OUT" :
168 (phase == ESP_DIP ? "DATA-IN" :
169 (phase == ESP_CMDP ? "COMMAND" :
170 (phase == ESP_STATP ? "STATUS" :
171 (phase == ESP_MOP ? "MSG-OUT" :
172 (phase == ESP_MIP ? "MSG_IN" :
174 if(statreg & ESP_STAT_TDONE)
175 printk("TRANS_DONE,");
176 if(statreg & ESP_STAT_TCNT)
177 printk("TCOUNT_ZERO,");
178 if(statreg & ESP_STAT_PERR)
180 if(statreg & ESP_STAT_SPAM)
182 if(statreg & ESP_STAT_INTR)
187 /* Print the interrupt register's value */
188 static inline void esp_print_ireg(unchar intreg)
191 if(intreg & ESP_INTR_S)
192 printk("SLCT_NATN ");
193 if(intreg & ESP_INTR_SATN)
195 if(intreg & ESP_INTR_RSEL)
197 if(intreg & ESP_INTR_FDONE)
199 if(intreg & ESP_INTR_BSERV)
201 if(intreg & ESP_INTR_DC)
203 if(intreg & ESP_INTR_IC)
205 if(intreg & ESP_INTR_SR)
206 printk("SCSI_BUS_RESET ");
210 /* Print the sequence step registers contents */
211 static inline void esp_print_seqreg(unchar stepreg)
213 stepreg &= ESP_STEP_VBITS;
215 (stepreg == ESP_STEP_ASEL ? "SLCT_ARB_CMPLT" :
216 (stepreg == ESP_STEP_SID ? "1BYTE_MSG_SENT" :
217 (stepreg == ESP_STEP_NCMD ? "NOT_IN_CMD_PHASE" :
218 (stepreg == ESP_STEP_PPC ? "CMD_BYTES_LOST" :
219 (stepreg == ESP_STEP_FINI4 ? "CMD_SENT_OK" :
223 static char *phase_string(int phase)
279 #ifdef DEBUG_STATE_MACHINE
280 static inline void esp_advance_phase(Scsi_Cmnd *s, int newphase)
282 ESPLOG(("<%s>", phase_string(newphase)));
283 s->SCp.sent_command = s->SCp.phase;
284 s->SCp.phase = newphase;
287 #define esp_advance_phase(__s, __newphase) \
288 (__s)->SCp.sent_command = (__s)->SCp.phase; \
289 (__s)->SCp.phase = (__newphase);
292 #ifdef DEBUG_ESP_CMDS
293 static inline void esp_cmd(struct NCR_ESP *esp, struct ESP_regs *eregs,
296 esp->espcmdlog[esp->espcmdent] = cmd;
297 esp->espcmdent = (esp->espcmdent + 1) & 31;
298 esp_write(eregs->esp_cmnd, cmd);
301 #define esp_cmd(__esp, __eregs, __cmd) esp_write((__eregs)->esp_cmnd, (__cmd))
304 /* How we use the various Linux SCSI data structures for operation.
308 * We keep track of the syncronous capabilities of a target
309 * in the device member, using sync_min_period and
310 * sync_max_offset. These are the values we directly write
311 * into the ESP registers while running a command. If offset
312 * is zero the ESP will use asynchronous transfers.
313 * If the borken flag is set we assume we shouldn't even bother
314 * trying to negotiate for synchronous transfer as this target
315 * is really stupid. If we notice the target is dropping the
316 * bus, and we have been allowing it to disconnect, we clear
317 * the disconnect flag.
320 /* Manipulation of the ESP command queues. Thanks to the aha152x driver
321 * and its author, Juergen E. Fischer, for the methods used here.
322 * Note that these are per-ESP queues, not global queues like
323 * the aha152x driver uses.
325 static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
329 new_SC->host_scribble = (unsigned char *) NULL;
333 for(end=*SC;end->host_scribble;end=(Scsi_Cmnd *)end->host_scribble)
335 end->host_scribble = (unsigned char *) new_SC;
339 static inline void prepend_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
341 new_SC->host_scribble = (unsigned char *) *SC;
345 static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd **SC)
351 *SC = (Scsi_Cmnd *) (*SC)->host_scribble;
355 static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, int target, int lun)
357 Scsi_Cmnd *ptr, *prev;
359 for(ptr = *SC, prev = NULL;
360 ptr && ((ptr->device->id != target) || (ptr->device->lun != lun));
361 prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble)
365 prev->host_scribble=ptr->host_scribble;
367 *SC=(Scsi_Cmnd *)ptr->host_scribble;
372 /* Resetting various pieces of the ESP scsi driver chipset */
374 /* Reset the ESP chip, _not_ the SCSI bus. */
375 static void esp_reset_esp(struct NCR_ESP *esp, struct ESP_regs *eregs)
377 int family_code, version, i;
380 /* Now reset the ESP chip */
381 esp_cmd(esp, eregs, ESP_CMD_RC);
382 esp_cmd(esp, eregs, ESP_CMD_NULL | ESP_CMD_DMA);
383 if(esp->erev == fast)
384 esp_write(eregs->esp_cfg2, ESP_CONFIG2_FENAB);
385 esp_cmd(esp, eregs, ESP_CMD_NULL | ESP_CMD_DMA);
387 /* This is the only point at which it is reliable to read
388 * the ID-code for a fast ESP chip variant.
390 esp->max_period = ((35 * esp->ccycle) / 1000);
391 if(esp->erev == fast) {
392 char *erev2string[] = {
398 "Symbios Logic 53CF9x-2",
402 version = esp_read(eregs->esp_uid);
403 family_code = (version & 0xf8) >> 3;
404 if(family_code == 0x02) {
405 if ((version & 7) == 2)
409 } else if(family_code == 0x0a)
410 esp->erev = fas366; /* Version is usually '5'. */
411 else if(family_code == 0x00) {
412 if ((version & 7) == 2)
413 esp->erev = fas100a; /* NCR53C9X */
415 esp->erev = espunknown;
416 } else if(family_code == 0x14) {
417 if ((version & 7) == 2)
420 esp->erev = espunknown;
421 } else if(family_code == 0x00) {
422 if ((version & 7) == 2)
423 esp->erev = fas100a; /* NCR53C9X */
425 esp->erev = espunknown;
427 esp->erev = espunknown;
428 ESPLOG(("esp%d: FAST chip is %s (family=%d, version=%d)\n",
429 esp->esp_id, erev2string[esp->erev - fas236],
430 family_code, (version & 7)));
432 esp->min_period = ((4 * esp->ccycle) / 1000);
434 esp->min_period = ((5 * esp->ccycle) / 1000);
437 /* Reload the configuration registers */
438 esp_write(eregs->esp_cfact, esp->cfact);
440 esp_write(eregs->esp_stp, 0);
442 esp_write(eregs->esp_soff, 0);
443 esp_write(eregs->esp_timeo, esp->neg_defp);
444 esp->max_period = (esp->max_period + 3)>>2;
445 esp->min_period = (esp->min_period + 3)>>2;
447 esp_write(eregs->esp_cfg1, esp->config1);
453 esp_write(eregs->esp_cfg2, esp->config2);
457 esp_write(eregs->esp_cfg2, esp->config2);
458 esp->prev_cfg3 = esp->config3[0];
459 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
462 panic("esp: FAS366 support not present, please notify "
463 "jongk@cs.utwente.nl");
468 /* Fast ESP variants */
469 esp_write(eregs->esp_cfg2, esp->config2);
471 esp->config3[i] |= ESP_CONFIG3_FCLK;
472 esp->prev_cfg3 = esp->config3[0];
473 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
478 /* Different timeout constant for these chips */
480 FSC_NEG_DEFP(esp->cfreq,
481 (esp->cfact == ESP_CCF_F0 ?
482 ESP_CCF_F7 + 1 : esp->cfact));
483 esp_write(eregs->esp_timeo, esp->neg_defp);
484 /* Enable Active Negotiation if possible */
485 if((esp->erev == fsc) && !esp->diff)
486 esp_write(eregs->esp_cfg4, ESP_CONFIG4_EAN);
490 esp_write(eregs->esp_cfg2, esp->config2);
492 esp->config3[i] |= ESP_CONFIG3_FCLOCK;
493 esp->prev_cfg3 = esp->config3[0];
494 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
498 panic("esp: what could it be... I wonder...");
502 /* Eat any bitrot in the chip */
503 trash = esp_read(eregs->esp_intrpt);
507 /* This places the ESP into a known state at boot time. */
508 void esp_bootup_reset(struct NCR_ESP *esp, struct ESP_regs *eregs)
510 volatile unchar trash;
517 esp_reset_esp(esp, eregs);
519 /* Reset the SCSI bus, but tell ESP not to generate an irq */
520 esp_write(eregs->esp_cfg1, (esp_read(eregs->esp_cfg1) | ESP_CONFIG1_SRRDISAB));
521 esp_cmd(esp, eregs, ESP_CMD_RS);
523 esp_write(eregs->esp_cfg1, esp->config1);
525 /* Eat any bitrot in the chip and we are done... */
526 trash = esp_read(eregs->esp_intrpt);
529 /* Allocate structure and insert basic data such as SCSI chip frequency
530 * data and a pointer to the device
532 struct NCR_ESP* esp_allocate(Scsi_Host_Template *tpnt, void *esp_dev)
534 struct NCR_ESP *esp, *elink;
535 struct Scsi_Host *esp_host;
537 esp_host = scsi_register(tpnt, sizeof(struct NCR_ESP));
539 panic("Cannot register ESP SCSI host");
540 esp = (struct NCR_ESP *) esp_host->hostdata;
542 panic("No esp in hostdata");
543 esp->ehost = esp_host;
545 esp->esp_id = nesps++;
547 /* Set bitshift value (only used on Amiga with multiple ESPs) */
550 /* Put into the chain of esp chips detected */
553 while(elink->next) elink = elink->next;
563 void esp_deallocate(struct NCR_ESP *esp)
565 struct NCR_ESP *elink;
567 if(espchain == esp) {
570 for(elink = espchain; elink && (elink->next != esp); elink = elink->next);
572 elink->next = esp->next;
577 /* Complete initialization of ESP structure and device
578 * Caller must have initialized appropriate parts of the ESP structure
579 * between the call to esp_allocate and this function.
581 void esp_initialize(struct NCR_ESP *esp)
583 struct ESP_regs *eregs = esp->eregs;
588 /* Check out the clock properties of the chip. */
590 /* This is getting messy but it has to be done
591 * correctly or else you get weird behavior all
592 * over the place. We are trying to basically
593 * figure out three pieces of information.
595 * a) Clock Conversion Factor
597 * This is a representation of the input
598 * crystal clock frequency going into the
599 * ESP on this machine. Any operation whose
600 * timing is longer than 400ns depends on this
601 * value being correct. For example, you'll
602 * get blips for arbitration/selection during
603 * high load or with multiple targets if this
604 * is not set correctly.
606 * b) Selection Time-Out
608 * The ESP isn't very bright and will arbitrate
609 * for the bus and try to select a target
610 * forever if you let it. This value tells
611 * the ESP when it has taken too long to
612 * negotiate and that it should interrupt
613 * the CPU so we can see what happened.
614 * The value is computed as follows (from
615 * NCR/Symbios chip docs).
617 * (Time Out Period) * (Input Clock)
618 * STO = ----------------------------------
619 * (8192) * (Clock Conversion Factor)
621 * You usually want the time out period to be
622 * around 250ms, I think we'll set it a little
623 * bit higher to account for fully loaded SCSI
624 * bus's and slow devices that don't respond so
625 * quickly to selection attempts. (yeah, I know
626 * this is out of spec. but there is a lot of
627 * buggy pieces of firmware out there so bite me)
629 * c) Imperical constants for synchronous offset
630 * and transfer period register values
632 * This entails the smallest and largest sync
633 * period we could ever handle on this ESP.
638 if(fmhz <= (5000000))
641 ccf = (((5000000 - 1) + (fmhz))/(5000000));
642 if(!ccf || ccf > 8) {
643 /* If we can't find anything reasonable,
644 * just assume 20MHZ. This is the clock
645 * frequency of the older sun4c's where I've
646 * been unable to find the clock-frequency
647 * PROM property. All other machines provide
648 * useful values it seems.
653 if(ccf==(ESP_CCF_F7+1))
654 esp->cfact = ESP_CCF_F0;
655 else if(ccf == ESP_CCF_NEVER)
656 esp->cfact = ESP_CCF_F2;
660 esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz);
661 esp->ctick = ESP_TICK(ccf, esp->ccycle);
662 esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf);
663 esp->sync_defp = SYNC_DEFP_SLOW;
665 printk("SCSI ID %d Clk %dMHz CCF=%d TOut %d ",
666 esp->scsi_id, (esp->cfreq / 1000000),
667 ccf, (int) esp->neg_defp);
669 /* Fill in ehost data */
670 esp->ehost->base = (unsigned long)eregs;
671 esp->ehost->this_id = esp->scsi_id;
672 esp->ehost->irq = esp->irq;
675 esp->scsi_id_mask = (1 << esp->scsi_id);
677 /* Probe the revision of this esp */
678 esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
679 esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
680 esp_write(eregs->esp_cfg2, esp->config2);
681 if((esp_read(eregs->esp_cfg2) & ~(ESP_CONFIG2_MAGIC)) !=
682 (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
683 printk("NCR53C90(esp100)\n");
687 esp_write(eregs->esp_cfg2, 0);
688 esp_write(eregs->esp_cfg3, 5);
689 if(esp_read(eregs->esp_cfg3) != 5) {
690 printk("NCR53C90A(esp100a)\n");
695 for(target=0; target<8; target++)
696 esp->config3[target] = 0;
698 esp_write(eregs->esp_cfg3, 0);
699 if(ccf > ESP_CCF_F5) {
700 printk("NCR53C9XF(espfast)\n");
702 esp->sync_defp = SYNC_DEFP_FAST;
704 printk("NCR53C9x(esp236)\n");
710 /* Initialize the command queues */
711 esp->current_SC = NULL;
712 esp->disconnected_SC = NULL;
713 esp->issue_SC = NULL;
715 /* Clear the state machines. */
716 esp->targets_present = 0;
717 esp->resetting_bus = 0;
720 init_waitqueue_head(&esp->reset_queue);
722 esp->fas_premature_intr_workaround = 0;
723 for(i = 0; i < 32; i++)
724 esp->espcmdlog[i] = 0;
726 for(i = 0; i < 16; i++) {
727 esp->cur_msgout[i] = 0;
728 esp->cur_msgin[i] = 0;
730 esp->prevmsgout = esp->prevmsgin = 0;
731 esp->msgout_len = esp->msgin_len = 0;
733 /* Clear the one behind caches to hold unmatchable values. */
734 esp->prev_soff = esp->prev_stp = esp->prev_cfg3 = 0xff;
736 /* Reset the thing before we try anything... */
737 esp_bootup_reset(esp, eregs);
742 /* The info function will return whatever useful
743 * information the developer sees fit. If not provided, then
744 * the name field will be used instead.
746 const char *esp_info(struct Scsi_Host *host)
750 esp = (struct NCR_ESP *) host->hostdata;
753 return "ESP100 (NCR53C90)";
755 return "ESP100A (NCR53C90A)";
757 return "ESP236 (NCR53C9x)";
759 return "Emulex FAS216";
761 return "Emulex FAS236";
763 return "QLogic FAS366";
767 return "Symbios Logic 53CF9x-2";
769 panic("Bogon ESP revision");
773 /* From Wolfgang Stanglmeier's NCR scsi driver. */
782 static void copy_mem_info(struct info_str *info, char *data, int len)
784 if (info->pos + len > info->length)
785 len = info->length - info->pos;
787 if (info->pos + len < info->offset) {
791 if (info->pos < info->offset) {
792 data += (info->offset - info->pos);
793 len -= (info->offset - info->pos);
797 memcpy(info->buffer + info->pos, data, len);
802 static int copy_info(struct info_str *info, char *fmt, ...)
809 len = vsprintf(buf, fmt, args);
812 copy_mem_info(info, buf, len);
816 static int esp_host_info(struct NCR_ESP *esp, char *ptr, off_t offset, int len)
818 struct scsi_device *sdev;
819 struct info_str info;
824 info.offset = offset;
827 copy_info(&info, "ESP Host Adapter:\n");
828 copy_info(&info, "\tESP Model\t\t");
831 copy_info(&info, "ESP100 (NCR53C90)\n");
834 copy_info(&info, "ESP100A (NCR53C90A)\n");
837 copy_info(&info, "ESP236 (NCR53C9x)\n");
840 copy_info(&info, "Emulex FAS216\n");
843 copy_info(&info, "Emulex FAS236\n");
846 copy_info(&info, "FPESP100A\n");
849 copy_info(&info, "Generic FAST\n");
852 copy_info(&info, "QLogic FAS366\n");
855 copy_info(&info, "Symbios Logic 53C9x-2\n");
859 copy_info(&info, "Unknown!\n");
862 copy_info(&info, "\tLive Targets\t\t[ ");
863 for(i = 0; i < 15; i++) {
864 if(esp->targets_present & (1 << i))
865 copy_info(&info, "%d ", i);
867 copy_info(&info, "]\n\n");
869 /* Now describe the state of each existing target. */
870 copy_info(&info, "Target #\tconfig3\t\tSync Capabilities\tDisconnect\n");
872 shost_for_each_device(sdev, esp->ehost) {
873 struct esp_device *esp_dev = sdev->hostdata;
876 if (!(esp->targets_present & (1 << id)))
879 copy_info(&info, "%d\t\t", id);
880 copy_info(&info, "%08lx\t", esp->config3[id]);
881 copy_info(&info, "[%02lx,%02lx]\t\t\t",
882 esp_dev->sync_max_offset,
883 esp_dev->sync_min_period);
884 copy_info(&info, "%s\n", esp_dev->disconnect ? "yes" : "no");
887 return info.pos > info.offset? info.pos - info.offset : 0;
890 /* ESP proc filesystem code. */
891 int esp_proc_info(struct Scsi_Host *shost, char *buffer, char **start, off_t offset, int length,
894 struct NCR_ESP *esp = (struct NCR_ESP *)shost->hostdata;
897 return -EINVAL; /* not yet */
900 return esp_host_info(esp, buffer, offset, length);
903 static void esp_get_dmabufs(struct NCR_ESP *esp, Scsi_Cmnd *sp)
905 if(sp->use_sg == 0) {
906 sp->SCp.this_residual = sp->request_bufflen;
907 sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
908 sp->SCp.buffers_residual = 0;
909 if (esp->dma_mmu_get_scsi_one)
910 esp->dma_mmu_get_scsi_one(esp, sp);
913 (char *) virt_to_phys(sp->request_buffer);
915 sp->SCp.buffer = (struct scatterlist *) sp->buffer;
916 sp->SCp.buffers_residual = sp->use_sg - 1;
917 sp->SCp.this_residual = sp->SCp.buffer->length;
918 if (esp->dma_mmu_get_scsi_sgl)
919 esp->dma_mmu_get_scsi_sgl(esp, sp);
922 (char *) virt_to_phys((page_address(sp->SCp.buffer->page) + sp->SCp.buffer->offset));
926 static void esp_release_dmabufs(struct NCR_ESP *esp, Scsi_Cmnd *sp)
928 if(sp->use_sg == 0) {
929 if (esp->dma_mmu_release_scsi_one)
930 esp->dma_mmu_release_scsi_one(esp, sp);
932 if (esp->dma_mmu_release_scsi_sgl)
933 esp->dma_mmu_release_scsi_sgl(esp, sp);
937 static void esp_restore_pointers(struct NCR_ESP *esp, Scsi_Cmnd *sp)
939 struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
941 sp->SCp.ptr = ep->saved_ptr;
942 sp->SCp.buffer = ep->saved_buffer;
943 sp->SCp.this_residual = ep->saved_this_residual;
944 sp->SCp.buffers_residual = ep->saved_buffers_residual;
947 static void esp_save_pointers(struct NCR_ESP *esp, Scsi_Cmnd *sp)
949 struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
951 ep->saved_ptr = sp->SCp.ptr;
952 ep->saved_buffer = sp->SCp.buffer;
953 ep->saved_this_residual = sp->SCp.this_residual;
954 ep->saved_buffers_residual = sp->SCp.buffers_residual;
959 * 1) Never ever panic while something is live on the bus.
960 * If there is to be any chance of syncing the disks this
961 * rule is to be obeyed.
963 * 2) Any target that causes a foul condition will no longer
964 * have synchronous transfers done to it, no questions
967 * 3) Keep register accesses to a minimum. Think about some
968 * day when we have Xbus machines this is running on and
969 * the ESP chip is on the other end of the machine on a
970 * different board from the cpu where this is running.
973 /* Fire off a command. We assume the bus is free and that the only
974 * case where we could see an interrupt is where we have disconnected
975 * commands active and they are trying to reselect us.
977 static inline void esp_check_cmd(struct NCR_ESP *esp, Scsi_Cmnd *sp)
979 switch(sp->cmd_len) {
983 esp->esp_slowcmd = 0;
987 esp->esp_slowcmd = 1;
988 esp->esp_scmdleft = sp->cmd_len;
989 esp->esp_scmdp = &sp->cmnd[0];
994 static inline void build_sync_nego_msg(struct NCR_ESP *esp, int period, int offset)
996 esp->cur_msgout[0] = EXTENDED_MESSAGE;
997 esp->cur_msgout[1] = 3;
998 esp->cur_msgout[2] = EXTENDED_SDTR;
999 esp->cur_msgout[3] = period;
1000 esp->cur_msgout[4] = offset;
1001 esp->msgout_len = 5;
1004 static void esp_exec_cmd(struct NCR_ESP *esp)
1006 struct ESP_regs *eregs = esp->eregs;
1007 struct esp_device *esp_dev;
1010 volatile unchar *cmdp = esp->esp_command;
1011 unsigned char the_esp_command;
1015 /* Hold off if we have disconnected commands and
1016 * an IRQ is showing...
1018 if(esp->disconnected_SC && esp->dma_irq_p(esp))
1021 /* Grab first member of the issue queue. */
1022 SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
1024 /* Safe to panic here because current_SC is null. */
1026 panic("esp: esp_exec_cmd and issue queue is NULL");
1028 SDptr = SCptr->device;
1029 esp_dev = SDptr->hostdata;
1030 lun = SCptr->device->lun;
1031 target = SCptr->device->id;
1034 esp->msgout_len = 0;
1036 /* Send it out whole, or piece by piece? The ESP
1037 * only knows how to automatically send out 6, 10,
1038 * and 12 byte commands. I used to think that the
1039 * Linux SCSI code would never throw anything other
1040 * than that to us, but then again there is the
1041 * SCSI generic driver which can send us anything.
1043 esp_check_cmd(esp, SCptr);
1045 /* If arbitration/selection is successful, the ESP will leave
1046 * ATN asserted, causing the target to go into message out
1047 * phase. The ESP will feed the target the identify and then
1048 * the target can only legally go to one of command,
1049 * datain/out, status, or message in phase, or stay in message
1050 * out phase (should we be trying to send a sync negotiation
1051 * message after the identify). It is not allowed to drop
1052 * BSY, but some buggy targets do and we check for this
1053 * condition in the selection complete code. Most of the time
1054 * we'll make the command bytes available to the ESP and it
1055 * will not interrupt us until it finishes command phase, we
1056 * cannot do this for command sizes the ESP does not
1057 * understand and in this case we'll get interrupted right
1058 * when the target goes into command phase.
1060 * It is absolutely _illegal_ in the presence of SCSI-2 devices
1061 * to use the ESP select w/o ATN command. When SCSI-2 devices are
1062 * present on the bus we _must_ always go straight to message out
1063 * phase with an identify message for the target. Being that
1064 * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
1065 * selections should not confuse SCSI-1 we hope.
1069 /* this targets sync is known */
1070 #ifdef CONFIG_SCSI_MAC_ESP
1073 if(esp_dev->disconnect)
1074 *cmdp++ = IDENTIFY(1, lun);
1076 *cmdp++ = IDENTIFY(0, lun);
1078 if(esp->esp_slowcmd) {
1079 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1080 esp_advance_phase(SCptr, in_slct_stop);
1082 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1083 esp_advance_phase(SCptr, in_slct_norm);
1085 } else if(!(esp->targets_present & (1<<target)) || !(esp_dev->disconnect)) {
1086 /* After the bootup SCSI code sends both the
1087 * TEST_UNIT_READY and INQUIRY commands we want
1088 * to at least attempt allowing the device to
1091 ESPMISC(("esp: Selecting device for first time. target=%d "
1092 "lun=%d\n", target, SCptr->device->lun));
1093 if(!SDptr->borken && !esp_dev->disconnect)
1094 esp_dev->disconnect = 1;
1096 *cmdp++ = IDENTIFY(0, lun);
1097 esp->prevmsgout = NOP;
1098 esp_advance_phase(SCptr, in_slct_norm);
1099 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1101 /* Take no chances... */
1102 esp_dev->sync_max_offset = 0;
1103 esp_dev->sync_min_period = 0;
1105 int toshiba_cdrom_hwbug_wkaround = 0;
1107 #ifdef CONFIG_SCSI_MAC_ESP
1108 /* Never allow synchronous transfers (disconnect OK) on
1109 * Macintosh. Well, maybe later when we figured out how to
1110 * do DMA on the machines that support it ...
1112 esp_dev->disconnect = 1;
1113 esp_dev->sync_max_offset = 0;
1114 esp_dev->sync_min_period = 0;
1119 /* We've talked to this guy before,
1120 * but never negotiated. Let's try
1123 if(!SDptr->borken) {
1124 if((SDptr->type == TYPE_ROM) &&
1125 (!strncmp(SDptr->vendor, "TOSHIBA", 7))) {
1126 /* Nice try sucker... */
1127 ESPMISC(("esp%d: Disabling sync for buggy "
1128 "Toshiba CDROM.\n", esp->esp_id));
1129 toshiba_cdrom_hwbug_wkaround = 1;
1130 build_sync_nego_msg(esp, 0, 0);
1132 build_sync_nego_msg(esp, esp->sync_defp, 15);
1135 build_sync_nego_msg(esp, 0, 0);
1140 /* A fix for broken SCSI1 targets, when they disconnect
1141 * they lock up the bus and confuse ESP. So disallow
1142 * disconnects for SCSI1 targets for now until we
1143 * find a better fix.
1145 * Addendum: This is funny, I figured out what was going
1146 * on. The blotzed SCSI1 target would disconnect,
1147 * one of the other SCSI2 targets or both would be
1148 * disconnected as well. The SCSI1 target would
1149 * stay disconnected long enough that we start
1150 * up a command on one of the SCSI2 targets. As
1151 * the ESP is arbitrating for the bus the SCSI1
1152 * target begins to arbitrate as well to reselect
1153 * the ESP. The SCSI1 target refuses to drop it's
1154 * ID bit on the data bus even though the ESP is
1155 * at ID 7 and is the obvious winner for any
1156 * arbitration. The ESP is a poor sport and refuses
1157 * to lose arbitration, it will continue indefinitely
1158 * trying to arbitrate for the bus and can only be
1159 * stopped via a chip reset or SCSI bus reset.
1160 * Therefore _no_ disconnects for SCSI1 targets
1161 * thank you very much. ;-)
1163 if(((SDptr->scsi_level < 3) && (SDptr->type != TYPE_TAPE)) ||
1164 toshiba_cdrom_hwbug_wkaround || SDptr->borken) {
1165 ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
1166 "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
1167 esp_dev->disconnect = 0;
1168 *cmdp++ = IDENTIFY(0, lun);
1170 *cmdp++ = IDENTIFY(1, lun);
1173 /* ESP fifo is only so big...
1174 * Make this look like a slow command.
1176 esp->esp_slowcmd = 1;
1177 esp->esp_scmdleft = SCptr->cmd_len;
1178 esp->esp_scmdp = &SCptr->cmnd[0];
1180 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1181 esp_advance_phase(SCptr, in_slct_msg);
1184 if(!esp->esp_slowcmd)
1185 for(i = 0; i < SCptr->cmd_len; i++)
1186 *cmdp++ = SCptr->cmnd[i];
1188 esp_write(eregs->esp_busid, (target & 7));
1189 if (esp->prev_soff != esp_dev->sync_max_offset ||
1190 esp->prev_stp != esp_dev->sync_min_period ||
1191 (esp->erev > esp100a &&
1192 esp->prev_cfg3 != esp->config3[target])) {
1193 esp->prev_soff = esp_dev->sync_max_offset;
1194 esp_write(eregs->esp_soff, esp->prev_soff);
1195 esp->prev_stp = esp_dev->sync_min_period;
1196 esp_write(eregs->esp_stp, esp->prev_stp);
1197 if(esp->erev > esp100a) {
1198 esp->prev_cfg3 = esp->config3[target];
1199 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
1202 i = (cmdp - esp->esp_command);
1204 /* Set up the DMA and ESP counters */
1205 if(esp->do_pio_cmds){
1211 * It seems this is required, at least to clean up
1212 * after failed commands when using PIO mode ...
1214 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1217 esp_write(eregs->esp_fdata, esp->esp_command[j]);
1218 the_esp_command &= ~ESP_CMD_DMA;
1220 /* Tell ESP to "go". */
1221 esp_cmd(esp, eregs, the_esp_command);
1223 /* Set up the ESP counters */
1224 esp_write(eregs->esp_tclow, i);
1225 esp_write(eregs->esp_tcmed, 0);
1226 esp->dma_init_write(esp, esp->esp_command_dvma, i);
1228 /* Tell ESP to "go". */
1229 esp_cmd(esp, eregs, the_esp_command);
1233 /* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
1234 int esp_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
1236 struct NCR_ESP *esp;
1238 /* Set up func ptr and initial driver cmd-phase. */
1239 SCpnt->scsi_done = done;
1240 SCpnt->SCp.phase = not_issued;
1242 esp = (struct NCR_ESP *) SCpnt->device->host->hostdata;
1245 esp->dma_led_on(esp);
1247 /* We use the scratch area. */
1248 ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->device->id, SCpnt->lun));
1249 ESPDISC(("N<%02x,%02x>", SCpnt->device->id, SCpnt->lun));
1251 esp_get_dmabufs(esp, SCpnt);
1252 esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
1254 SCpnt->SCp.Status = CHECK_CONDITION;
1255 SCpnt->SCp.Message = 0xff;
1256 SCpnt->SCp.sent_command = 0;
1258 /* Place into our queue. */
1259 if(SCpnt->cmnd[0] == REQUEST_SENSE) {
1260 ESPQUEUE(("RQSENSE\n"));
1261 prepend_SC(&esp->issue_SC, SCpnt);
1264 append_SC(&esp->issue_SC, SCpnt);
1267 /* Run it now if we can. */
1268 if(!esp->current_SC && !esp->resetting_bus)
1274 /* Dump driver state. */
1275 static void esp_dump_cmd(Scsi_Cmnd *SCptr)
1277 ESPLOG(("[tgt<%02x> lun<%02x> "
1278 "pphase<%s> cphase<%s>]",
1279 SCptr->device->id, SCptr->device->lun,
1280 phase_string(SCptr->SCp.sent_command),
1281 phase_string(SCptr->SCp.phase)));
1284 static void esp_dump_state(struct NCR_ESP *esp,
1285 struct ESP_regs *eregs)
1287 Scsi_Cmnd *SCptr = esp->current_SC;
1288 #ifdef DEBUG_ESP_CMDS
1292 ESPLOG(("esp%d: dumping state\n", esp->esp_id));
1294 /* Print DMA status */
1295 esp->dma_dump_state(esp);
1297 ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1298 esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
1299 ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1300 esp->esp_id, esp_read(eregs->esp_status), esp_read(eregs->esp_sstep),
1301 esp_read(eregs->esp_intrpt)));
1302 #ifdef DEBUG_ESP_CMDS
1303 printk("esp%d: last ESP cmds [", esp->esp_id);
1304 i = (esp->espcmdent - 1) & 31;
1306 esp_print_cmd(esp->espcmdlog[i]);
1310 esp_print_cmd(esp->espcmdlog[i]);
1314 esp_print_cmd(esp->espcmdlog[i]);
1318 esp_print_cmd(esp->espcmdlog[i]);
1321 #endif /* (DEBUG_ESP_CMDS) */
1324 ESPLOG(("esp%d: current command ", esp->esp_id));
1325 esp_dump_cmd(SCptr);
1328 SCptr = esp->disconnected_SC;
1329 ESPLOG(("esp%d: disconnected ", esp->esp_id));
1331 esp_dump_cmd(SCptr);
1332 SCptr = (Scsi_Cmnd *) SCptr->host_scribble;
1337 /* Abort a command. The host_lock is acquired by caller. */
1338 int esp_abort(Scsi_Cmnd *SCptr)
1340 struct NCR_ESP *esp = (struct NCR_ESP *) SCptr->device->host->hostdata;
1341 struct ESP_regs *eregs = esp->eregs;
1344 ESPLOG(("esp%d: Aborting command\n", esp->esp_id));
1345 esp_dump_state(esp, eregs);
1347 /* Wheee, if this is the current command on the bus, the
1348 * best we can do is assert ATN and wait for msgout phase.
1349 * This should even fix a hung SCSI bus when we lose state
1350 * in the driver and timeout because the eventual phase change
1351 * will cause the ESP to (eventually) give an interrupt.
1353 if(esp->current_SC == SCptr) {
1354 esp->cur_msgout[0] = ABORT;
1355 esp->msgout_len = 1;
1356 esp->msgout_ctr = 0;
1357 esp_cmd(esp, eregs, ESP_CMD_SATN);
1361 /* If it is still in the issue queue then we can safely
1362 * call the completion routine and report abort success.
1364 don = esp->dma_ports_p(esp);
1366 esp->dma_ints_off(esp);
1367 synchronize_irq(esp->irq);
1370 Scsi_Cmnd **prev, *this;
1371 for(prev = (&esp->issue_SC), this = esp->issue_SC;
1373 prev = (Scsi_Cmnd **) &(this->host_scribble),
1374 this = (Scsi_Cmnd *) this->host_scribble) {
1376 *prev = (Scsi_Cmnd *) this->host_scribble;
1377 this->host_scribble = NULL;
1378 esp_release_dmabufs(esp, this);
1379 this->result = DID_ABORT << 16;
1382 esp->dma_ints_on(esp);
1388 /* Yuck, the command to abort is disconnected, it is not
1389 * worth trying to abort it now if something else is live
1390 * on the bus at this time. So, we let the SCSI code wait
1391 * a little bit and try again later.
1393 if(esp->current_SC) {
1395 esp->dma_ints_on(esp);
1399 /* It's disconnected, we have to reconnect to re-establish
1400 * the nexus and tell the device to abort. However, we really
1401 * cannot 'reconnect' per se. Don't try to be fancy, just
1402 * indicate failure, which causes our caller to reset the whole
1407 esp->dma_ints_on(esp);
1411 /* We've sent ESP_CMD_RS to the ESP, the interrupt had just
1412 * arrived indicating the end of the SCSI bus reset. Our job
1413 * is to clean out the command queues and begin re-execution
1414 * of SCSI commands once more.
1416 static int esp_finish_reset(struct NCR_ESP *esp,
1417 struct ESP_regs *eregs)
1419 Scsi_Cmnd *sp = esp->current_SC;
1421 /* Clean up currently executing command, if any. */
1423 esp_release_dmabufs(esp, sp);
1424 sp->result = (DID_RESET << 16);
1426 esp->current_SC = NULL;
1429 /* Clean up disconnected queue, they have been invalidated
1432 if (esp->disconnected_SC) {
1433 while((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
1434 esp_release_dmabufs(esp, sp);
1435 sp->result = (DID_RESET << 16);
1440 /* SCSI bus reset is complete. */
1441 esp->resetting_bus = 0;
1442 wake_up(&esp->reset_queue);
1444 /* Ok, now it is safe to get commands going once more. */
1451 static int esp_do_resetbus(struct NCR_ESP *esp,
1452 struct ESP_regs *eregs)
1454 ESPLOG(("esp%d: Resetting scsi bus\n", esp->esp_id));
1455 esp->resetting_bus = 1;
1456 esp_cmd(esp, eregs, ESP_CMD_RS);
1461 /* Reset ESP chip, reset hanging bus, then kill active and
1462 * disconnected commands for targets without soft reset.
1464 * The host_lock is acquired by caller.
1466 int esp_reset(Scsi_Cmnd *SCptr)
1468 struct NCR_ESP *esp = (struct NCR_ESP *) SCptr->device->host->hostdata;
1470 (void) esp_do_resetbus(esp, esp->eregs);
1472 spin_unlock_irq(esp->ehost->host_lock);
1474 wait_event(esp->reset_queue, (esp->resetting_bus == 0));
1476 spin_lock_irq(esp->ehost->host_lock);
1481 /* Internal ESP done function. */
1482 static void esp_done(struct NCR_ESP *esp, int error)
1486 if(esp->current_SC) {
1487 done_SC = esp->current_SC;
1488 esp->current_SC = NULL;
1489 esp_release_dmabufs(esp, done_SC);
1490 done_SC->result = error;
1491 done_SC->scsi_done(done_SC);
1493 /* Bus is free, issue any commands in the queue. */
1494 if(esp->issue_SC && !esp->current_SC)
1497 /* Panic is safe as current_SC is null so we may still
1498 * be able to accept more commands to sync disk buffers.
1500 ESPLOG(("panicing\n"));
1501 panic("esp: done() called with NULL esp->current_SC");
1505 /* Wheee, ESP interrupt engine. */
1507 /* Forward declarations. */
1508 static int esp_do_phase_determine(struct NCR_ESP *esp,
1509 struct ESP_regs *eregs);
1510 static int esp_do_data_finale(struct NCR_ESP *esp, struct ESP_regs *eregs);
1511 static int esp_select_complete(struct NCR_ESP *esp, struct ESP_regs *eregs);
1512 static int esp_do_status(struct NCR_ESP *esp, struct ESP_regs *eregs);
1513 static int esp_do_msgin(struct NCR_ESP *esp, struct ESP_regs *eregs);
1514 static int esp_do_msgindone(struct NCR_ESP *esp, struct ESP_regs *eregs);
1515 static int esp_do_msgout(struct NCR_ESP *esp, struct ESP_regs *eregs);
1516 static int esp_do_cmdbegin(struct NCR_ESP *esp, struct ESP_regs *eregs);
1518 #define sreg_datainp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
1519 #define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
1521 /* We try to avoid some interrupts by jumping ahead and see if the ESP
1522 * has gotten far enough yet. Hence the following.
1524 static inline int skipahead1(struct NCR_ESP *esp, struct ESP_regs *eregs,
1525 Scsi_Cmnd *scp, int prev_phase, int new_phase)
1527 if(scp->SCp.sent_command != prev_phase)
1530 if(esp->dma_irq_p(esp)) {
1531 /* Yes, we are able to save an interrupt. */
1532 esp->sreg = (esp_read(eregs->esp_status) & ~(ESP_STAT_INTR));
1533 esp->ireg = esp_read(eregs->esp_intrpt);
1534 if(!(esp->ireg & ESP_INTR_SR))
1537 return do_reset_complete;
1539 /* Ho hum, target is taking forever... */
1540 scp->SCp.sent_command = new_phase; /* so we don't recurse... */
1544 static inline int skipahead2(struct NCR_ESP *esp,
1545 struct ESP_regs *eregs,
1546 Scsi_Cmnd *scp, int prev_phase1, int prev_phase2,
1549 if(scp->SCp.sent_command != prev_phase1 &&
1550 scp->SCp.sent_command != prev_phase2)
1552 if(esp->dma_irq_p(esp)) {
1553 /* Yes, we are able to save an interrupt. */
1554 esp->sreg = (esp_read(eregs->esp_status) & ~(ESP_STAT_INTR));
1555 esp->ireg = esp_read(eregs->esp_intrpt);
1556 if(!(esp->ireg & ESP_INTR_SR))
1559 return do_reset_complete;
1561 /* Ho hum, target is taking forever... */
1562 scp->SCp.sent_command = new_phase; /* so we don't recurse... */
1566 /* Misc. esp helper macros. */
1567 #define esp_setcount(__eregs, __cnt) \
1568 esp_write((__eregs)->esp_tclow, ((__cnt) & 0xff)); \
1569 esp_write((__eregs)->esp_tcmed, (((__cnt) >> 8) & 0xff))
1571 #define esp_getcount(__eregs) \
1572 ((esp_read((__eregs)->esp_tclow)&0xff) | \
1573 ((esp_read((__eregs)->esp_tcmed)&0xff) << 8))
1575 #define fcount(__esp, __eregs) \
1576 (esp_read((__eregs)->esp_fflags) & ESP_FF_FBYTES)
1578 #define fnzero(__esp, __eregs) \
1579 (esp_read((__eregs)->esp_fflags) & ESP_FF_ONOTZERO)
1581 /* XXX speculative nops unnecessary when continuing amidst a data phase
1582 * XXX even on esp100!!! another case of flooding the bus with I/O reg
1585 #define esp_maybe_nop(__esp, __eregs) \
1586 if((__esp)->erev == esp100) \
1587 esp_cmd((__esp), (__eregs), ESP_CMD_NULL)
1589 #define sreg_to_dataphase(__sreg) \
1590 ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
1592 /* The ESP100 when in synchronous data phase, can mistake a long final
1593 * REQ pulse from the target as an extra byte, it places whatever is on
1594 * the data lines into the fifo. For now, we will assume when this
1595 * happens that the target is a bit quirky and we don't want to
1596 * be talking synchronously to it anyways. Regardless, we need to
1597 * tell the ESP to eat the extraneous byte so that we can proceed
1598 * to the next phase.
1600 static inline int esp100_sync_hwbug(struct NCR_ESP *esp, struct ESP_regs *eregs,
1601 Scsi_Cmnd *sp, int fifocnt)
1603 /* Do not touch this piece of code. */
1604 if((!(esp->erev == esp100)) ||
1605 (!(sreg_datainp((esp->sreg = esp_read(eregs->esp_status))) && !fifocnt) &&
1606 !(sreg_dataoutp(esp->sreg) && !fnzero(esp, eregs)))) {
1607 if(sp->SCp.phase == in_dataout)
1608 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1611 /* Async mode for this guy. */
1612 build_sync_nego_msg(esp, 0, 0);
1614 /* Ack the bogus byte, but set ATN first. */
1615 esp_cmd(esp, eregs, ESP_CMD_SATN);
1616 esp_cmd(esp, eregs, ESP_CMD_MOK);
1621 /* This closes the window during a selection with a reselect pending, because
1622 * we use DMA for the selection process the FIFO should hold the correct
1623 * contents if we get reselected during this process. So we just need to
1624 * ack the possible illegal cmd interrupt pending on the esp100.
1626 static inline int esp100_reconnect_hwbug(struct NCR_ESP *esp,
1627 struct ESP_regs *eregs)
1629 volatile unchar junk;
1631 if(esp->erev != esp100)
1633 junk = esp_read(eregs->esp_intrpt);
1635 if(junk & ESP_INTR_SR)
1640 /* This verifies the BUSID bits during a reselection so that we know which
1641 * target is talking to us.
1643 static inline int reconnect_target(struct NCR_ESP *esp, struct ESP_regs *eregs)
1645 int it, me = esp->scsi_id_mask, targ = 0;
1647 if(2 != fcount(esp, eregs))
1649 it = esp_read(eregs->esp_fdata);
1660 /* This verifies the identify from the target so that we know which lun is
1661 * being reconnected.
1663 static inline int reconnect_lun(struct NCR_ESP *esp, struct ESP_regs *eregs)
1667 if((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
1669 lun = esp_read(eregs->esp_fdata);
1671 /* Yes, you read this correctly. We report lun of zero
1672 * if we see parity error. ESP reports parity error for
1673 * the lun byte, and this is the only way to hope to recover
1674 * because the target is connected.
1676 if(esp->sreg & ESP_STAT_PERR)
1679 /* Check for illegal bits being set in the lun. */
1680 if((lun & 0x40) || !(lun & 0x80))
1686 /* This puts the driver in a state where it can revitalize a command that
1687 * is being continued due to reselection.
1689 static inline void esp_connect(struct NCR_ESP *esp, struct ESP_regs *eregs,
1692 Scsi_Device *dp = sp->device;
1693 struct esp_device *esp_dev = dp->hostdata;
1695 if(esp->prev_soff != esp_dev->sync_max_offset ||
1696 esp->prev_stp != esp_dev->sync_min_period ||
1697 (esp->erev > esp100a &&
1698 esp->prev_cfg3 != esp->config3[sp->device->id])) {
1699 esp->prev_soff = esp_dev->sync_max_offset;
1700 esp_write(eregs->esp_soff, esp->prev_soff);
1701 esp->prev_stp = esp_dev->sync_min_period;
1702 esp_write(eregs->esp_stp, esp->prev_stp);
1703 if(esp->erev > esp100a) {
1704 esp->prev_cfg3 = esp->config3[sp->device->id];
1705 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
1708 esp->current_SC = sp;
1711 /* This will place the current working command back into the issue queue
1712 * if we are to receive a reselection amidst a selection attempt.
1714 static inline void esp_reconnect(struct NCR_ESP *esp, Scsi_Cmnd *sp)
1716 if(!esp->disconnected_SC)
1717 ESPLOG(("esp%d: Weird, being reselected but disconnected "
1718 "command queue is empty.\n", esp->esp_id));
1720 esp->current_SC = NULL;
1721 sp->SCp.phase = not_issued;
1722 append_SC(&esp->issue_SC, sp);
1725 /* Begin message in phase. */
1726 static int esp_do_msgin(struct NCR_ESP *esp, struct ESP_regs *eregs)
1728 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1729 esp_maybe_nop(esp, eregs);
1730 esp_cmd(esp, eregs, ESP_CMD_TI);
1733 esp_advance_phase(esp->current_SC, in_msgindone);
1737 static inline void advance_sg(struct NCR_ESP *esp, Scsi_Cmnd *sp)
1740 --sp->SCp.buffers_residual;
1741 sp->SCp.this_residual = sp->SCp.buffer->length;
1742 if (esp->dma_advance_sg)
1743 esp->dma_advance_sg (sp);
1745 sp->SCp.ptr = (char *) virt_to_phys((page_address(sp->SCp.buffer->page) + sp->SCp.buffer->offset));
1749 /* Please note that the way I've coded these routines is that I _always_
1750 * check for a disconnect during any and all information transfer
1751 * phases. The SCSI standard states that the target _can_ cause a BUS
1752 * FREE condition by dropping all MSG/CD/IO/BSY signals. Also note
1753 * that during information transfer phases the target controls every
1754 * change in phase, the only thing the initiator can do is "ask" for
1755 * a message out phase by driving ATN true. The target can, and sometimes
1756 * will, completely ignore this request so we cannot assume anything when
1757 * we try to force a message out phase to abort/reset a target. Most of
1758 * the time the target will eventually be nice and go to message out, so
1759 * we may have to hold on to our state about what we want to tell the target
1760 * for some period of time.
1763 /* I think I have things working here correctly. Even partial transfers
1764 * within a buffer or sub-buffer should not upset us at all no matter
1765 * how bad the target and/or ESP fucks things up.
1767 static int esp_do_data(struct NCR_ESP *esp, struct ESP_regs *eregs)
1769 Scsi_Cmnd *SCptr = esp->current_SC;
1770 int thisphase, hmuch;
1772 ESPDATA(("esp_do_data: "));
1773 esp_maybe_nop(esp, eregs);
1774 thisphase = sreg_to_dataphase(esp->sreg);
1775 esp_advance_phase(SCptr, thisphase);
1776 ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
1777 hmuch = esp->dma_can_transfer(esp, SCptr);
1780 * XXX MSch: cater for PIO transfer here; PIO used if hmuch == 0
1782 if (hmuch) { /* DMA */
1786 ESPDATA(("hmuch<%d> ", hmuch));
1787 esp->current_transfer_size = hmuch;
1788 esp_setcount(eregs, (esp->fas_premature_intr_workaround ?
1789 (hmuch + 0x40) : hmuch));
1790 esp->dma_setup(esp, (__u32)((unsigned long)SCptr->SCp.ptr),
1791 hmuch, (thisphase == in_datain));
1792 ESPDATA(("DMA|TI --> do_intr_end\n"));
1793 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
1802 int oldphase, i = 0; /* or where we left off last time ?? esp->current_data ?? */
1805 oldphase = esp_read(eregs->esp_status) & ESP_STAT_PMASK;
1808 * polled transfer; ugly, can we make this happen in a DRQ
1809 * interrupt handler ??
1810 * requires keeping track of state information in host or
1812 * Problem: I've never seen a DRQ happen on Mac, not even
1813 * with ESP_CMD_DMA ...
1816 /* figure out how much needs to be transferred */
1817 hmuch = SCptr->SCp.this_residual;
1818 ESPDATA(("hmuch<%d> pio ", hmuch));
1819 esp->current_transfer_size = hmuch;
1821 /* tell the ESP ... */
1822 esp_setcount(eregs, hmuch);
1826 int j, fifo_stuck = 0, newphase;
1827 unsigned long timeout;
1829 unsigned long flags;
1835 ESPDATA(( /*"\n"*/ "\r"));
1838 local_irq_save(flags);
1840 if(thisphase == in_datain) {
1842 esp_cmd(esp, eregs, ESP_CMD_TI);
1846 while (!((esp->sreg=esp_read(eregs->esp_status)) & ESP_STAT_INTR) && --timeout)
1849 printk("DRQ datain timeout! \n");
1851 newphase = esp->sreg & ESP_STAT_PMASK;
1853 /* see how much we got ... */
1854 fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
1861 ESPDATA(("\rgot %d st %x ph %x", fifocnt, esp->sreg, newphase));
1864 for(j=0;j<fifocnt;j++)
1865 SCptr->SCp.ptr[i++] = esp_read(eregs->esp_fdata);
1867 ESPDATA(("(%d) ", i));
1869 /* how many to go ?? */
1872 /* break if status phase !! */
1873 if(newphase == ESP_STATP) {
1875 esp->ireg = esp_read(eregs->esp_intrpt);
1880 /* how much will fit ? */
1881 int this_count = MAX_FIFO - fifocnt;
1882 if (this_count > hmuch)
1886 for(j=0;j<this_count;j++)
1887 esp_write(eregs->esp_fdata, SCptr->SCp.ptr[i++]);
1889 /* how many left if this goes out ?? */
1890 hmuch -= this_count;
1893 esp_cmd(esp, eregs, ESP_CMD_TI);
1895 /* wait for 'got it' */
1897 while (!((esp->sreg=esp_read(eregs->esp_status)) & ESP_STAT_INTR) && --timeout)
1900 printk("DRQ dataout timeout! \n");
1902 newphase = esp->sreg & ESP_STAT_PMASK;
1904 /* need to check how much was sent ?? */
1905 fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
1907 ESPDATA(("\rsent %d st %x ph %x", this_count - fifocnt, esp->sreg, newphase));
1909 ESPDATA(("(%d) ", i));
1911 /* break if status phase !! */
1912 if(newphase == ESP_STATP) {
1914 esp->ireg = esp_read(eregs->esp_intrpt);
1921 esp->ireg = esp_read(eregs->esp_intrpt);
1923 ESPDATA(("ir %x ... ", esp->ireg));
1926 ESPDATA(("done! \n"));
1929 local_irq_restore(flags);
1932 /* check new bus phase */
1933 if (newphase != oldphase && i < esp->current_transfer_size) {
1934 /* something happened; disconnect ?? */
1935 ESPDATA(("phase change, dropped out with %d done ... ", i));
1939 /* check int. status */
1940 if (esp->ireg & ESP_INTR_DC) {
1942 ESPDATA(("disconnect; %d transferred ... ", i));
1944 } else if (esp->ireg & ESP_INTR_FDONE) {
1946 ESPDATA(("function done; %d transferred ... ", i));
1950 /* XXX fixme: bail out on stall */
1951 if (fifo_stuck > 10) {
1953 ESPDATA(("fifo stall; %d transferred ... ", i));
1959 /* check successful completion ?? */
1961 if (thisphase == in_dataout)
1962 hmuch += fifocnt; /* stuck?? adjust data pointer ...*/
1964 /* tell do_data_finale how much was transferred */
1965 esp->current_transfer_size -= hmuch;
1967 /* still not completely sure on this one ... */
1968 return /*do_intr_end*/ do_work_bus /*do_phase_determine*/ ;
1977 /* See how successful the data transfer was. */
1978 static int esp_do_data_finale(struct NCR_ESP *esp,
1979 struct ESP_regs *eregs)
1981 Scsi_Cmnd *SCptr = esp->current_SC;
1982 struct esp_device *esp_dev = SCptr->device->hostdata;
1983 int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
1985 if(esp->dma_led_off)
1986 esp->dma_led_off(esp);
1988 ESPDATA(("esp_do_data_finale: "));
1990 if(SCptr->SCp.phase == in_datain) {
1991 if(esp->sreg & ESP_STAT_PERR) {
1992 /* Yuck, parity error. The ESP asserts ATN
1993 * so that we can go to message out phase
1994 * immediately and inform the target that
1995 * something bad happened.
1997 ESPLOG(("esp%d: data bad parity detected.\n",
1999 esp->cur_msgout[0] = INITIATOR_ERROR;
2000 esp->msgout_len = 1;
2003 esp->dma_drain(esp);
2005 if(esp->dma_invalidate)
2006 esp->dma_invalidate(esp);
2008 /* This could happen for the above parity error case. */
2009 if(!(esp->ireg == ESP_INTR_BSERV)) {
2010 /* Please go to msgout phase, please please please... */
2011 ESPLOG(("esp%d: !BSERV after data, probably to msgout\n",
2013 return esp_do_phase_determine(esp, eregs);
2016 /* Check for partial transfers and other horrible events. */
2017 fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
2018 ecount = esp_getcount(eregs);
2019 if(esp->fas_premature_intr_workaround)
2021 bytes_sent = esp->current_transfer_size;
2023 ESPDATA(("trans_sz=%d, ", bytes_sent));
2024 if(!(esp->sreg & ESP_STAT_TCNT))
2025 bytes_sent -= ecount;
2026 if(SCptr->SCp.phase == in_dataout)
2027 bytes_sent -= fifocnt;
2029 ESPDATA(("bytes_sent=%d (ecount=%d, fifocnt=%d), ", bytes_sent,
2032 /* If we were in synchronous mode, check for peculiarities. */
2033 if(esp_dev->sync_max_offset)
2034 bogus_data = esp100_sync_hwbug(esp, eregs, SCptr, fifocnt);
2036 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2038 /* Until we are sure of what has happened, we are certainly
2041 esp_advance_phase(SCptr, in_the_dark);
2043 /* Check for premature interrupt condition. Can happen on FAS2x6
2044 * chips. QLogic recommends a workaround by overprogramming the
2045 * transfer counters, but this makes doing scatter-gather impossible.
2046 * Until there is a way to disable scatter-gather for a single target,
2047 * and not only for the entire host adapter as it is now, the workaround
2048 * is way to expensive performance wise.
2049 * Instead, it turns out that when this happens the target has disconnected
2050 * already but it doesn't show in the interrupt register. Compensate for
2051 * that here to try and avoid a SCSI bus reset.
2053 if(!esp->fas_premature_intr_workaround && (fifocnt == 1) &&
2054 sreg_dataoutp(esp->sreg)) {
2055 ESPLOG(("esp%d: Premature interrupt, enabling workaround\n",
2058 /* Disable scatter-gather operations, they are not possible
2059 * when using this workaround.
2061 esp->ehost->sg_tablesize = 0;
2062 esp->ehost->use_clustering = ENABLE_CLUSTERING;
2063 esp->fas_premature_intr_workaround = 1;
2066 ESPLOG(("esp%d: Aborting scatter-gather operation\n",
2068 esp->cur_msgout[0] = ABORT;
2069 esp->msgout_len = 1;
2070 esp->msgout_ctr = 0;
2071 esp_cmd(esp, eregs, ESP_CMD_SATN);
2072 esp_setcount(eregs, 0xffff);
2073 esp_cmd(esp, eregs, ESP_CMD_NULL);
2074 esp_cmd(esp, eregs, ESP_CMD_TPAD | ESP_CMD_DMA);
2078 /* Just set the disconnected bit. That's what appears to
2079 * happen anyway. The state machine will pick it up when
2082 esp->ireg |= ESP_INTR_DC;
2086 if(bytes_sent < 0) {
2087 /* I've seen this happen due to lost state in this
2088 * driver. No idea why it happened, but allowing
2089 * this value to be negative caused things to
2090 * lock up. This allows greater chance of recovery.
2091 * In fact every time I've seen this, it has been
2092 * a driver bug without question.
2094 ESPLOG(("esp%d: yieee, bytes_sent < 0!\n", esp->esp_id));
2095 ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%d\n",
2097 esp->current_transfer_size, fifocnt, ecount));
2098 ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%d\n",
2100 SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
2101 ESPLOG(("esp%d: Forcing async for target %d\n", esp->esp_id,
2102 SCptr->device->id));
2103 SCptr->device->borken = 1;
2108 /* Update the state of our transfer. */
2109 SCptr->SCp.ptr += bytes_sent;
2110 SCptr->SCp.this_residual -= bytes_sent;
2111 if(SCptr->SCp.this_residual < 0) {
2113 ESPLOG(("esp%d: Data transfer overrun.\n", esp->esp_id));
2114 SCptr->SCp.this_residual = 0;
2117 /* Maybe continue. */
2119 ESPDATA(("!bogus_data, "));
2120 /* NO MATTER WHAT, we advance the scatterlist,
2121 * if the target should decide to disconnect
2122 * in between scatter chunks (which is common)
2123 * we could die horribly! I used to have the sg
2124 * advance occur only if we are going back into
2125 * (or are staying in) a data phase, you can
2126 * imagine the hell I went through trying to
2129 if(!SCptr->SCp.this_residual && SCptr->SCp.buffers_residual)
2130 advance_sg(esp, SCptr);
2131 #ifdef DEBUG_ESP_DATA
2132 if(sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
2133 ESPDATA(("to more data\n"));
2135 ESPDATA(("to new phase\n"));
2138 return esp_do_phase_determine(esp, eregs);
2140 /* Bogus data, just wait for next interrupt. */
2141 ESPLOG(("esp%d: bogus_data during end of data phase\n",
2146 /* We received a non-good status return at the end of
2147 * running a SCSI command. This is used to decide if
2148 * we should clear our synchronous transfer state for
2149 * such a device when that happens.
2151 * The idea is that when spinning up a disk or rewinding
2152 * a tape, we don't want to go into a loop re-negotiating
2153 * synchronous capabilities over and over.
2155 static int esp_should_clear_sync(Scsi_Cmnd *sp)
2157 unchar cmd1 = sp->cmnd[0];
2158 unchar cmd2 = sp->data_cmnd[0];
2160 /* These cases are for spinning up a disk and
2161 * waiting for that spinup to complete.
2163 if(cmd1 == START_STOP ||
2167 if(cmd1 == TEST_UNIT_READY ||
2168 cmd2 == TEST_UNIT_READY)
2171 /* One more special case for SCSI tape drives,
2172 * this is what is used to probe the device for
2173 * completion of a rewind or tape load operation.
2175 if(sp->device->type == TYPE_TAPE) {
2176 if(cmd1 == MODE_SENSE ||
2184 /* Either a command is completing or a target is dropping off the bus
2185 * to continue the command in the background so we can do other work.
2187 static int esp_do_freebus(struct NCR_ESP *esp, struct ESP_regs *eregs)
2189 Scsi_Cmnd *SCptr = esp->current_SC;
2192 rval = skipahead2(esp, eregs, SCptr, in_status, in_msgindone, in_freeing);
2196 if(esp->ireg != ESP_INTR_DC) {
2197 ESPLOG(("esp%d: Target will not disconnect\n", esp->esp_id));
2198 return do_reset_bus; /* target will not drop BSY... */
2200 esp->msgout_len = 0;
2201 esp->prevmsgout = NOP;
2202 if(esp->prevmsgin == COMMAND_COMPLETE) {
2203 struct esp_device *esp_dev = SCptr->device->hostdata;
2204 /* Normal end of nexus. */
2205 if(esp->disconnected_SC)
2206 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2208 if(SCptr->SCp.Status != GOOD &&
2209 SCptr->SCp.Status != CONDITION_GOOD &&
2210 ((1<<SCptr->device->id) & esp->targets_present) &&
2211 esp_dev->sync && esp_dev->sync_max_offset) {
2212 /* SCSI standard says that the synchronous capabilities
2213 * should be renegotiated at this point. Most likely
2214 * we are about to request sense from this target
2215 * in which case we want to avoid using sync
2216 * transfers until we are sure of the current target
2219 ESPMISC(("esp: Status <%d> for target %d lun %d\n",
2220 SCptr->SCp.Status, SCptr->device->id, SCptr->device->lun));
2222 /* But don't do this when spinning up a disk at
2223 * boot time while we poll for completion as it
2224 * fills up the console with messages. Also, tapes
2225 * can report not ready many times right after
2226 * loading up a tape.
2228 if(esp_should_clear_sync(SCptr) != 0)
2231 ESPDISC(("F<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
2232 esp_done(esp, ((SCptr->SCp.Status & 0xff) |
2233 ((SCptr->SCp.Message & 0xff)<<8) |
2235 } else if(esp->prevmsgin == DISCONNECT) {
2236 /* Normal disconnect. */
2237 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2238 ESPDISC(("D<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
2239 append_SC(&esp->disconnected_SC, SCptr);
2240 esp->current_SC = NULL;
2244 /* Driver bug, we do not expect a disconnect here
2245 * and should not have advanced the state engine
2248 ESPLOG(("esp%d: last msg not disc and not cmd cmplt.\n",
2250 return do_reset_bus;
2255 /* When a reselect occurs, and we cannot find the command to
2256 * reconnect to in our queues, we do this.
2258 static int esp_bad_reconnect(struct NCR_ESP *esp)
2262 ESPLOG(("esp%d: Eieeee, reconnecting unknown command!\n",
2264 ESPLOG(("QUEUE DUMP\n"));
2266 ESPLOG(("esp%d: issue_SC[", esp->esp_id));
2268 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2269 sp = (Scsi_Cmnd *) sp->host_scribble;
2272 sp = esp->current_SC;
2273 ESPLOG(("esp%d: current_SC[", esp->esp_id));
2275 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2276 sp = (Scsi_Cmnd *) sp->host_scribble;
2279 sp = esp->disconnected_SC;
2280 ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
2282 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2283 sp = (Scsi_Cmnd *) sp->host_scribble;
2286 return do_reset_bus;
2289 /* Do the needy when a target tries to reconnect to us. */
2290 static int esp_do_reconnect(struct NCR_ESP *esp,
2291 struct ESP_regs *eregs)
2296 /* Check for all bogus conditions first. */
2297 target = reconnect_target(esp, eregs);
2299 ESPDISC(("bad bus bits\n"));
2300 return do_reset_bus;
2302 lun = reconnect_lun(esp, eregs);
2304 ESPDISC(("target=%2x, bad identify msg\n", target));
2305 return do_reset_bus;
2308 /* Things look ok... */
2309 ESPDISC(("R<%02x,%02x>", target, lun));
2311 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2312 if(esp100_reconnect_hwbug(esp, eregs))
2313 return do_reset_bus;
2314 esp_cmd(esp, eregs, ESP_CMD_NULL);
2316 SCptr = remove_SC(&esp->disconnected_SC, (unchar) target, (unchar) lun);
2318 return esp_bad_reconnect(esp);
2320 esp_connect(esp, eregs, SCptr);
2321 esp_cmd(esp, eregs, ESP_CMD_MOK);
2323 /* Reconnect implies a restore pointers operation. */
2324 esp_restore_pointers(esp, SCptr);
2327 esp_advance_phase(SCptr, in_the_dark);
2331 /* End of NEXUS (hopefully), pick up status + message byte then leave if
2334 static int esp_do_status(struct NCR_ESP *esp, struct ESP_regs *eregs)
2336 Scsi_Cmnd *SCptr = esp->current_SC;
2339 rval = skipahead1(esp, eregs, SCptr, in_the_dark, in_status);
2344 ESPSTAT(("esp_do_status: "));
2345 if(intr != ESP_INTR_DC) {
2346 int message_out = 0; /* for parity problems */
2348 /* Ack the message. */
2349 ESPSTAT(("ack msg, "));
2350 esp_cmd(esp, eregs, ESP_CMD_MOK);
2353 esp->dma_poll(esp, (unsigned char *) esp->esp_command);
2355 ESPSTAT(("got something, "));
2356 /* ESP chimes in with one of
2358 * 1) function done interrupt:
2359 * both status and message in bytes
2362 * 2) bus service interrupt:
2363 * only status byte was acquired
2366 * can't happen, but we test for it
2369 * ALSO: If bad parity was detected on either
2370 * the status _or_ the message byte then
2371 * the ESP has asserted ATN on the bus
2372 * and we must therefore wait for the
2373 * next phase change.
2375 if(intr & ESP_INTR_FDONE) {
2376 /* We got it all, hallejulia. */
2377 ESPSTAT(("got both, "));
2378 SCptr->SCp.Status = esp->esp_command[0];
2379 SCptr->SCp.Message = esp->esp_command[1];
2380 esp->prevmsgin = SCptr->SCp.Message;
2381 esp->cur_msgin[0] = SCptr->SCp.Message;
2382 if(esp->sreg & ESP_STAT_PERR) {
2383 /* There was bad parity for the
2384 * message byte, the status byte
2387 message_out = MSG_PARITY_ERROR;
2389 } else if(intr == ESP_INTR_BSERV) {
2390 /* Only got status byte. */
2391 ESPLOG(("esp%d: got status only, ", esp->esp_id));
2392 if(!(esp->sreg & ESP_STAT_PERR)) {
2393 SCptr->SCp.Status = esp->esp_command[0];
2394 SCptr->SCp.Message = 0xff;
2396 /* The status byte had bad parity.
2397 * we leave the scsi_pointer Status
2398 * field alone as we set it to a default
2399 * of CHECK_CONDITION in esp_queue.
2401 message_out = INITIATOR_ERROR;
2404 /* This shouldn't happen ever. */
2405 ESPSTAT(("got bolixed\n"));
2406 esp_advance_phase(SCptr, in_the_dark);
2407 return esp_do_phase_determine(esp, eregs);
2411 ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
2412 SCptr->SCp.Message));
2413 if(SCptr->SCp.Message == COMMAND_COMPLETE) {
2414 ESPSTAT(("and was COMMAND_COMPLETE\n"));
2415 esp_advance_phase(SCptr, in_freeing);
2416 return esp_do_freebus(esp, eregs);
2418 ESPLOG(("esp%d: and _not_ COMMAND_COMPLETE\n",
2420 esp->msgin_len = esp->msgin_ctr = 1;
2421 esp_advance_phase(SCptr, in_msgindone);
2422 return esp_do_msgindone(esp, eregs);
2425 /* With luck we'll be able to let the target
2426 * know that bad parity happened, it will know
2427 * which byte caused the problems and send it
2428 * again. For the case where the status byte
2429 * receives bad parity, I do not believe most
2430 * targets recover very well. We'll see.
2432 ESPLOG(("esp%d: bad parity somewhere mout=%2x\n",
2433 esp->esp_id, message_out));
2434 esp->cur_msgout[0] = message_out;
2435 esp->msgout_len = esp->msgout_ctr = 1;
2436 esp_advance_phase(SCptr, in_the_dark);
2437 return esp_do_phase_determine(esp, eregs);
2440 /* If we disconnect now, all hell breaks loose. */
2441 ESPLOG(("esp%d: whoops, disconnect\n", esp->esp_id));
2442 esp_advance_phase(SCptr, in_the_dark);
2443 return esp_do_phase_determine(esp, eregs);
2447 static int esp_enter_status(struct NCR_ESP *esp,
2448 struct ESP_regs *eregs)
2450 unchar thecmd = ESP_CMD_ICCSEQ;
2452 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2454 if(esp->do_pio_cmds) {
2455 esp_advance_phase(esp->current_SC, in_status);
2456 esp_cmd(esp, eregs, thecmd);
2457 while(!(esp_read(esp->eregs->esp_status) & ESP_STAT_INTR));
2458 esp->esp_command[0] = esp_read(eregs->esp_fdata);
2459 while(!(esp_read(esp->eregs->esp_status) & ESP_STAT_INTR));
2460 esp->esp_command[1] = esp_read(eregs->esp_fdata);
2462 esp->esp_command[0] = esp->esp_command[1] = 0xff;
2463 esp_write(eregs->esp_tclow, 2);
2464 esp_write(eregs->esp_tcmed, 0);
2465 esp->dma_init_read(esp, esp->esp_command_dvma, 2);
2466 thecmd |= ESP_CMD_DMA;
2467 esp_cmd(esp, eregs, thecmd);
2468 esp_advance_phase(esp->current_SC, in_status);
2471 return esp_do_status(esp, eregs);
2474 static int esp_disconnect_amidst_phases(struct NCR_ESP *esp,
2475 struct ESP_regs *eregs)
2477 Scsi_Cmnd *sp = esp->current_SC;
2478 struct esp_device *esp_dev = sp->device->hostdata;
2480 /* This means real problems if we see this
2481 * here. Unless we were actually trying
2482 * to force the device to abort/reset.
2484 ESPLOG(("esp%d: Disconnect amidst phases, ", esp->esp_id));
2485 ESPLOG(("pphase<%s> cphase<%s>, ",
2486 phase_string(sp->SCp.phase),
2487 phase_string(sp->SCp.sent_command)));
2489 if(esp->disconnected_SC)
2490 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2492 switch(esp->cur_msgout[0]) {
2494 /* We didn't expect this to happen at all. */
2495 ESPLOG(("device is bolixed\n"));
2496 esp_advance_phase(sp, in_tgterror);
2497 esp_done(esp, (DID_ERROR << 16));
2500 case BUS_DEVICE_RESET:
2501 ESPLOG(("device reset successful\n"));
2502 esp_dev->sync_max_offset = 0;
2503 esp_dev->sync_min_period = 0;
2505 esp_advance_phase(sp, in_resetdev);
2506 esp_done(esp, (DID_RESET << 16));
2510 ESPLOG(("device abort successful\n"));
2511 esp_advance_phase(sp, in_abortone);
2512 esp_done(esp, (DID_ABORT << 16));
2519 static int esp_enter_msgout(struct NCR_ESP *esp,
2520 struct ESP_regs *eregs)
2522 esp_advance_phase(esp->current_SC, in_msgout);
2523 return esp_do_msgout(esp, eregs);
2526 static int esp_enter_msgin(struct NCR_ESP *esp,
2527 struct ESP_regs *eregs)
2529 esp_advance_phase(esp->current_SC, in_msgin);
2530 return esp_do_msgin(esp, eregs);
2533 static int esp_enter_cmd(struct NCR_ESP *esp,
2534 struct ESP_regs *eregs)
2536 esp_advance_phase(esp->current_SC, in_cmdbegin);
2537 return esp_do_cmdbegin(esp, eregs);
2540 static int esp_enter_badphase(struct NCR_ESP *esp,
2541 struct ESP_regs *eregs)
2543 ESPLOG(("esp%d: Bizarre bus phase %2x.\n", esp->esp_id,
2544 esp->sreg & ESP_STAT_PMASK));
2545 return do_reset_bus;
2548 typedef int (*espfunc_t)(struct NCR_ESP *,
2551 static espfunc_t phase_vector[] = {
2552 esp_do_data, /* ESP_DOP */
2553 esp_do_data, /* ESP_DIP */
2554 esp_enter_cmd, /* ESP_CMDP */
2555 esp_enter_status, /* ESP_STATP */
2556 esp_enter_badphase, /* ESP_STAT_PMSG */
2557 esp_enter_badphase, /* ESP_STAT_PMSG | ESP_STAT_PIO */
2558 esp_enter_msgout, /* ESP_MOP */
2559 esp_enter_msgin, /* ESP_MIP */
2562 /* The target has control of the bus and we have to see where it has
2565 static int esp_do_phase_determine(struct NCR_ESP *esp,
2566 struct ESP_regs *eregs)
2568 if ((esp->ireg & ESP_INTR_DC) != 0)
2569 return esp_disconnect_amidst_phases(esp, eregs);
2570 return phase_vector[esp->sreg & ESP_STAT_PMASK](esp, eregs);
2573 /* First interrupt after exec'ing a cmd comes here. */
2574 static int esp_select_complete(struct NCR_ESP *esp, struct ESP_regs *eregs)
2576 Scsi_Cmnd *SCptr = esp->current_SC;
2577 struct esp_device *esp_dev = SCptr->device->hostdata;
2578 int cmd_bytes_sent, fcnt;
2580 fcnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
2581 cmd_bytes_sent = esp->dma_bytes_sent(esp, fcnt);
2582 if(esp->dma_invalidate)
2583 esp->dma_invalidate(esp);
2585 /* Let's check to see if a reselect happened
2586 * while we we're trying to select. This must
2589 if(esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
2590 esp_reconnect(esp, SCptr);
2591 return esp_do_reconnect(esp, eregs);
2594 /* Looks like things worked, we should see a bus service &
2595 * a function complete interrupt at this point. Note we
2596 * are doing a direct comparison because we don't want to
2597 * be fooled into thinking selection was successful if
2598 * ESP_INTR_DC is set, see below.
2600 if(esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
2601 /* target speaks... */
2602 esp->targets_present |= (1<<SCptr->device->id);
2604 /* What if the target ignores the sdtr? */
2608 /* See how far, if at all, we got in getting
2609 * the information out to the target.
2611 switch(esp->seqreg) {
2615 /* Arbitration won, target selected, but
2616 * we are in some phase which is not command
2617 * phase nor is it message out phase.
2619 * XXX We've confused the target, obviously.
2620 * XXX So clear it's state, but we also end
2621 * XXX up clearing everyone elses. That isn't
2622 * XXX so nice. I'd like to just reset this
2623 * XXX target, but if I cannot even get it's
2624 * XXX attention and finish selection to talk
2625 * XXX to it, there is not much more I can do.
2626 * XXX If we have a loaded bus we're going to
2627 * XXX spend the next second or so renegotiating
2628 * XXX for synchronous transfers.
2630 ESPLOG(("esp%d: STEP_ASEL for tgt %d\n",
2631 esp->esp_id, SCptr->device->id));
2634 /* Arbitration won, target selected, went
2635 * to message out phase, sent one message
2636 * byte, then we stopped. ATN is asserted
2637 * on the SCSI bus and the target is still
2638 * there hanging on. This is a legal
2639 * sequence step if we gave the ESP a select
2642 * XXX See above, I could set the borken flag
2643 * XXX in the device struct and retry the
2644 * XXX command. But would that help for
2645 * XXX tagged capable targets?
2649 /* Arbitration won, target selected, maybe
2650 * sent the one message byte in message out
2651 * phase, but we did not go to command phase
2652 * in the end. Actually, we could have sent
2653 * only some of the message bytes if we tried
2654 * to send out the entire identify and tag
2655 * message using ESP_CMD_SA3.
2661 /* No, not the powerPC pinhead. Arbitration
2662 * won, all message bytes sent if we went to
2663 * message out phase, went to command phase
2664 * but only part of the command was sent.
2666 * XXX I've seen this, but usually in conjunction
2667 * XXX with a gross error which appears to have
2668 * XXX occurred between the time I told the
2669 * XXX ESP to arbitrate and when I got the
2670 * XXX interrupt. Could I have misloaded the
2671 * XXX command bytes into the fifo? Actually,
2672 * XXX I most likely missed a phase, and therefore
2673 * XXX went into never never land and didn't even
2674 * XXX know it. That was the old driver though.
2675 * XXX What is even more peculiar is that the ESP
2676 * XXX showed the proper function complete and
2677 * XXX bus service bits in the interrupt register.
2680 case ESP_STEP_FINI4:
2681 case ESP_STEP_FINI5:
2682 case ESP_STEP_FINI6:
2683 case ESP_STEP_FINI7:
2684 /* Account for the identify message */
2685 if(SCptr->SCp.phase == in_slct_norm)
2686 cmd_bytes_sent -= 1;
2688 esp_cmd(esp, eregs, ESP_CMD_NULL);
2690 /* Be careful, we could really get fucked during synchronous
2691 * data transfers if we try to flush the fifo now.
2693 if(!fcnt && /* Fifo is empty and... */
2694 /* either we are not doing synchronous transfers or... */
2695 (!esp_dev->sync_max_offset ||
2696 /* We are not going into data in phase. */
2697 ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
2698 esp_cmd(esp, eregs, ESP_CMD_FLUSH); /* flush is safe */
2700 /* See how far we got if this is not a slow command. */
2701 if(!esp->esp_slowcmd) {
2702 if(cmd_bytes_sent < 0)
2704 if(cmd_bytes_sent != SCptr->cmd_len) {
2705 /* Crapola, mark it as a slowcmd
2706 * so that we have some chance of
2707 * keeping the command alive with
2710 * XXX Actually, if we didn't send it all
2711 * XXX this means either we didn't set things
2712 * XXX up properly (driver bug) or the target
2713 * XXX or the ESP detected parity on one of
2714 * XXX the command bytes. This makes much
2715 * XXX more sense, and therefore this code
2716 * XXX should be changed to send out a
2717 * XXX parity error message or if the status
2718 * XXX register shows no parity error then
2719 * XXX just expect the target to bring the
2720 * XXX bus into message in phase so that it
2721 * XXX can send us the parity error message.
2724 esp->esp_slowcmd = 1;
2725 esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
2726 esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
2730 /* Now figure out where we went. */
2731 esp_advance_phase(SCptr, in_the_dark);
2732 return esp_do_phase_determine(esp, eregs);
2735 /* Did the target even make it? */
2736 if(esp->ireg == ESP_INTR_DC) {
2737 /* wheee... nobody there or they didn't like
2738 * what we told it to do, clean up.
2741 /* If anyone is off the bus, but working on
2742 * a command in the background for us, tell
2743 * the ESP to listen for them.
2745 if(esp->disconnected_SC)
2746 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2748 if(((1<<SCptr->device->id) & esp->targets_present) &&
2749 esp->seqreg && esp->cur_msgout[0] == EXTENDED_MESSAGE &&
2750 (SCptr->SCp.phase == in_slct_msg ||
2751 SCptr->SCp.phase == in_slct_stop)) {
2754 ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
2755 "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
2756 esp_dev->sync_max_offset = 0;
2757 esp_dev->sync_min_period = 0;
2758 esp_dev->sync = 1; /* so we don't negotiate again */
2760 /* Run the command again, this time though we
2761 * won't try to negotiate for synchronous transfers.
2763 * XXX I'd like to do something like send an
2764 * XXX INITIATOR_ERROR or ABORT message to the
2765 * XXX target to tell it, "Sorry I confused you,
2766 * XXX please come back and I will be nicer next
2767 * XXX time". But that requires having the target
2768 * XXX on the bus, and it has dropped BSY on us.
2770 esp->current_SC = NULL;
2771 esp_advance_phase(SCptr, not_issued);
2772 prepend_SC(&esp->issue_SC, SCptr);
2777 /* Ok, this is normal, this is what we see during boot
2778 * or whenever when we are scanning the bus for targets.
2779 * But first make sure that is really what is happening.
2781 if(((1<<SCptr->device->id) & esp->targets_present)) {
2782 ESPLOG(("esp%d: Warning, live target %d not responding to "
2783 "selection.\n", esp->esp_id, SCptr->device->id));
2785 /* This _CAN_ happen. The SCSI standard states that
2786 * the target is to _not_ respond to selection if
2787 * _it_ detects bad parity on the bus for any reason.
2788 * Therefore, we assume that if we've talked successfully
2789 * to this target before, bad parity is the problem.
2791 esp_done(esp, (DID_PARITY << 16));
2793 /* Else, there really isn't anyone there. */
2794 ESPMISC(("esp: selection failure, maybe nobody there?\n"));
2795 ESPMISC(("esp: target %d lun %d\n",
2796 SCptr->device->id, SCptr->device->lun));
2797 esp_done(esp, (DID_BAD_TARGET << 16));
2803 ESPLOG(("esp%d: Selection failure.\n", esp->esp_id));
2804 printk("esp%d: Currently -- ", esp->esp_id);
2805 esp_print_ireg(esp->ireg);
2807 esp_print_statreg(esp->sreg);
2809 esp_print_seqreg(esp->seqreg);
2811 printk("esp%d: New -- ", esp->esp_id);
2812 esp->sreg = esp_read(eregs->esp_status);
2813 esp->seqreg = esp_read(eregs->esp_sstep);
2814 esp->ireg = esp_read(eregs->esp_intrpt);
2815 esp_print_ireg(esp->ireg);
2817 esp_print_statreg(esp->sreg);
2819 esp_print_seqreg(esp->seqreg);
2821 ESPLOG(("esp%d: resetting bus\n", esp->esp_id));
2822 return do_reset_bus; /* ugh... */
2825 /* Continue reading bytes for msgin phase. */
2826 static int esp_do_msgincont(struct NCR_ESP *esp, struct ESP_regs *eregs)
2828 if(esp->ireg & ESP_INTR_BSERV) {
2829 /* in the right phase too? */
2830 if((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
2832 esp_cmd(esp, eregs, ESP_CMD_TI);
2833 esp_advance_phase(esp->current_SC, in_msgindone);
2837 /* We changed phase but ESP shows bus service,
2838 * in this case it is most likely that we, the
2839 * hacker who has been up for 20hrs straight
2840 * staring at the screen, drowned in coffee
2841 * smelling like retched cigarette ashes
2842 * have miscoded something..... so, try to
2843 * recover as best we can.
2845 ESPLOG(("esp%d: message in mis-carriage.\n", esp->esp_id));
2847 esp_advance_phase(esp->current_SC, in_the_dark);
2848 return do_phase_determine;
2851 static int check_singlebyte_msg(struct NCR_ESP *esp,
2852 struct ESP_regs *eregs)
2854 esp->prevmsgin = esp->cur_msgin[0];
2855 if(esp->cur_msgin[0] & 0x80) {
2857 ESPLOG(("esp%d: target sends identify amidst phases\n",
2859 esp_advance_phase(esp->current_SC, in_the_dark);
2861 } else if(((esp->cur_msgin[0] & 0xf0) == 0x20) ||
2862 (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
2864 esp_advance_phase(esp->current_SC, in_msgincont);
2867 esp_advance_phase(esp->current_SC, in_the_dark);
2868 switch(esp->cur_msgin[0]) {
2870 /* We don't want to hear about it. */
2871 ESPLOG(("esp%d: msg %02x which we don't know about\n", esp->esp_id,
2872 esp->cur_msgin[0]));
2873 return MESSAGE_REJECT;
2876 ESPLOG(("esp%d: target %d sends a nop\n", esp->esp_id,
2877 esp->current_SC->device->id));
2880 case RESTORE_POINTERS:
2881 /* In this case we might also have to backup the
2882 * "slow command" pointer. It is rare to get such
2883 * a save/restore pointer sequence so early in the
2884 * bus transition sequences, but cover it.
2886 if(esp->esp_slowcmd) {
2887 esp->esp_scmdleft = esp->current_SC->cmd_len;
2888 esp->esp_scmdp = &esp->current_SC->cmnd[0];
2890 esp_restore_pointers(esp, esp->current_SC);
2894 esp_save_pointers(esp, esp->current_SC);
2897 case COMMAND_COMPLETE:
2899 /* Freeing the bus, let it go. */
2900 esp->current_SC->SCp.phase = in_freeing;
2903 case MESSAGE_REJECT:
2904 ESPMISC(("msg reject, "));
2905 if(esp->prevmsgout == EXTENDED_MESSAGE) {
2906 struct esp_device *esp_dev = esp->current_SC->device->hostdata;
2908 /* Doesn't look like this target can
2909 * do synchronous or WIDE transfers.
2911 ESPSDTR(("got reject, was trying nego, clearing sync/WIDE\n"));
2914 esp_dev->sync_min_period = 0;
2915 esp_dev->sync_max_offset = 0;
2918 ESPMISC(("not sync nego, sending ABORT\n"));
2924 /* Target negotiates for synchronous transfers before we do, this
2925 * is legal although very strange. What is even funnier is that
2926 * the SCSI2 standard specifically recommends against targets doing
2927 * this because so many initiators cannot cope with this occurring.
2929 static int target_with_ants_in_pants(struct NCR_ESP *esp,
2931 struct esp_device *esp_dev)
2933 if(esp_dev->sync || SCptr->device->borken) {
2934 /* sorry, no can do */
2935 ESPSDTR(("forcing to async, "));
2936 build_sync_nego_msg(esp, 0, 0);
2939 ESPLOG(("esp%d: hoping for msgout\n", esp->esp_id));
2940 esp_advance_phase(SCptr, in_the_dark);
2941 return EXTENDED_MESSAGE;
2944 /* Ok, we'll check them out... */
2948 static void sync_report(struct NCR_ESP *esp)
2953 msg3 = esp->cur_msgin[3];
2954 msg4 = esp->cur_msgin[4];
2956 int hz = 1000000000 / (msg3 * 4);
2957 int integer = hz / 1000000;
2958 int fraction = (hz - (integer * 1000000)) / 10000;
2959 if((msg3 * 4) < 200) {
2962 type = "synchronous";
2965 /* Do not transform this back into one big printk
2966 * again, it triggers a bug in our sparc64-gcc272
2967 * sibling call optimization. -DaveM
2969 ESPLOG((KERN_INFO "esp%d: target %d ",
2970 esp->esp_id, esp->current_SC->device->id));
2971 ESPLOG(("[period %dns offset %d %d.%02dMHz ",
2972 (int) msg3 * 4, (int) msg4,
2973 integer, fraction));
2974 ESPLOG(("%s SCSI%s]\n", type,
2975 (((msg3 * 4) < 200) ? "-II" : "")));
2977 ESPLOG((KERN_INFO "esp%d: target %d asynchronous\n",
2978 esp->esp_id, esp->current_SC->device->id));
2982 static int check_multibyte_msg(struct NCR_ESP *esp,
2983 struct ESP_regs *eregs)
2985 Scsi_Cmnd *SCptr = esp->current_SC;
2986 struct esp_device *esp_dev = SCptr->device->hostdata;
2988 int message_out = 0;
2990 ESPSDTR(("chk multibyte msg: "));
2991 if(esp->cur_msgin[2] == EXTENDED_SDTR) {
2992 int period = esp->cur_msgin[3];
2993 int offset = esp->cur_msgin[4];
2995 ESPSDTR(("is sync nego response, "));
2999 /* Target negotiates first! */
3000 ESPSDTR(("target jumps the gun, "));
3001 message_out = EXTENDED_MESSAGE; /* we must respond */
3002 rval = target_with_ants_in_pants(esp, SCptr, esp_dev);
3007 ESPSDTR(("examining sdtr, "));
3009 /* Offset cannot be larger than ESP fifo size. */
3011 ESPSDTR(("offset too big %2x, ", offset));
3013 ESPSDTR(("sending back new offset\n"));
3014 build_sync_nego_msg(esp, period, offset);
3015 return EXTENDED_MESSAGE;
3018 if(offset && period > esp->max_period) {
3019 /* Yeee, async for this slow device. */
3020 ESPSDTR(("period too long %2x, ", period));
3021 build_sync_nego_msg(esp, 0, 0);
3022 ESPSDTR(("hoping for msgout\n"));
3023 esp_advance_phase(esp->current_SC, in_the_dark);
3024 return EXTENDED_MESSAGE;
3025 } else if (offset && period < esp->min_period) {
3026 ESPSDTR(("period too short %2x, ", period));
3027 period = esp->min_period;
3028 if(esp->erev > esp236)
3035 ESPSDTR(("period is ok, "));
3036 tmp = esp->ccycle / 1000;
3037 regval = (((period << 2) + tmp - 1) / tmp);
3038 if(regval && (esp->erev > esp236)) {
3047 esp_dev->sync_min_period = (regval & 0x1f);
3048 esp_dev->sync_max_offset = (offset | esp->radelay);
3049 if(esp->erev > esp236) {
3050 if(esp->erev == fas100a)
3051 bit = ESP_CONFIG3_FAST;
3053 bit = ESP_CONFIG3_FSCSI;
3055 esp->config3[SCptr->device->id] |= bit;
3057 esp->config3[SCptr->device->id] &= ~bit;
3058 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3059 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
3061 esp->prev_soff = esp_dev->sync_min_period;
3062 esp_write(eregs->esp_soff, esp->prev_soff);
3063 esp->prev_stp = esp_dev->sync_max_offset;
3064 esp_write(eregs->esp_stp, esp->prev_stp);
3066 ESPSDTR(("soff=%2x stp=%2x cfg3=%2x\n",
3067 esp_dev->sync_max_offset,
3068 esp_dev->sync_min_period,
3069 esp->config3[SCptr->device->id]));
3072 } else if(esp_dev->sync_max_offset) {
3075 /* back to async mode */
3076 ESPSDTR(("unaccaptable sync nego, forcing async\n"));
3077 esp_dev->sync_max_offset = 0;
3078 esp_dev->sync_min_period = 0;
3080 esp_write(eregs->esp_soff, 0);
3082 esp_write(eregs->esp_stp, 0);
3083 if(esp->erev > esp236) {
3084 if(esp->erev == fas100a)
3085 bit = ESP_CONFIG3_FAST;
3087 bit = ESP_CONFIG3_FSCSI;
3088 esp->config3[SCptr->device->id] &= ~bit;
3089 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3090 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
3096 ESPSDTR(("chk multibyte msg: sync is known, "));
3100 ESPLOG(("esp%d: sending sdtr back, hoping for msgout\n",
3102 build_sync_nego_msg(esp, period, offset);
3103 esp_advance_phase(SCptr, in_the_dark);
3104 return EXTENDED_MESSAGE;
3107 ESPSDTR(("returning zero\n"));
3108 esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
3110 } else if(esp->cur_msgin[2] == EXTENDED_WDTR) {
3111 ESPLOG(("esp%d: AIEEE wide msg received\n", esp->esp_id));
3112 message_out = MESSAGE_REJECT;
3113 } else if(esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
3114 ESPLOG(("esp%d: rejecting modify data ptr msg\n", esp->esp_id));
3115 message_out = MESSAGE_REJECT;
3117 esp_advance_phase(SCptr, in_the_dark);
3121 static int esp_do_msgindone(struct NCR_ESP *esp, struct ESP_regs *eregs)
3123 Scsi_Cmnd *SCptr = esp->current_SC;
3124 int message_out = 0, it = 0, rval;
3126 rval = skipahead1(esp, eregs, SCptr, in_msgin, in_msgindone);
3129 if(SCptr->SCp.sent_command != in_status) {
3130 if(!(esp->ireg & ESP_INTR_DC)) {
3131 if(esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
3132 message_out = MSG_PARITY_ERROR;
3133 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3134 } else if((it = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES))!=1) {
3135 /* We certainly dropped the ball somewhere. */
3136 message_out = INITIATOR_ERROR;
3137 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3138 } else if(!esp->msgin_len) {
3139 it = esp_read(eregs->esp_fdata);
3140 esp_advance_phase(SCptr, in_msgincont);
3142 /* it is ok and we want it */
3143 it = esp->cur_msgin[esp->msgin_ctr] =
3144 esp_read(eregs->esp_fdata);
3148 esp_advance_phase(SCptr, in_the_dark);
3152 it = esp->cur_msgin[0];
3154 if(!message_out && esp->msgin_len) {
3155 if(esp->msgin_ctr < esp->msgin_len) {
3156 esp_advance_phase(SCptr, in_msgincont);
3157 } else if(esp->msgin_len == 1) {
3158 message_out = check_singlebyte_msg(esp, eregs);
3159 } else if(esp->msgin_len == 2) {
3160 if(esp->cur_msgin[0] == EXTENDED_MESSAGE) {
3162 message_out = MESSAGE_REJECT;
3164 esp->msgin_len = (it + 2);
3165 esp_advance_phase(SCptr, in_msgincont);
3168 message_out = MESSAGE_REJECT; /* foo on you */
3171 message_out = check_multibyte_msg(esp, eregs);
3174 if(message_out < 0) {
3175 return -message_out;
3176 } else if(message_out) {
3177 if(((message_out != 1) &&
3178 ((message_out < 0x20) || (message_out & 0x80))))
3179 esp->msgout_len = 1;
3180 esp->cur_msgout[0] = message_out;
3181 esp_cmd(esp, eregs, ESP_CMD_SATN);
3182 esp_advance_phase(SCptr, in_the_dark);
3185 esp->sreg = esp_read(eregs->esp_status);
3186 esp->sreg &= ~(ESP_STAT_INTR);
3187 if((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
3188 esp_cmd(esp, eregs, ESP_CMD_MOK);
3189 if((SCptr->SCp.sent_command == in_msgindone) &&
3190 (SCptr->SCp.phase == in_freeing))
3191 return esp_do_freebus(esp, eregs);
3195 static int esp_do_cmdbegin(struct NCR_ESP *esp, struct ESP_regs *eregs)
3198 Scsi_Cmnd *SCptr = esp->current_SC;
3200 esp_advance_phase(SCptr, in_cmdend);
3201 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3202 tmp = *esp->esp_scmdp++;
3203 esp->esp_scmdleft--;
3204 esp_write(eregs->esp_fdata, tmp);
3205 esp_cmd(esp, eregs, ESP_CMD_TI);
3209 static int esp_do_cmddone(struct NCR_ESP *esp, struct ESP_regs *eregs)
3211 esp_cmd(esp, eregs, ESP_CMD_NULL);
3212 if(esp->ireg & ESP_INTR_BSERV) {
3213 esp_advance_phase(esp->current_SC, in_the_dark);
3214 return esp_do_phase_determine(esp, eregs);
3216 ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.\n",
3218 return do_reset_bus;
3221 static int esp_do_msgout(struct NCR_ESP *esp, struct ESP_regs *eregs)
3223 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3224 switch(esp->msgout_len) {
3226 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3227 esp_cmd(esp, eregs, ESP_CMD_TI);
3231 if(esp->do_pio_cmds){
3232 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3233 esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3234 esp_cmd(esp, eregs, ESP_CMD_TI);
3236 esp->esp_command[0] = esp->cur_msgout[0];
3237 esp->esp_command[1] = esp->cur_msgout[1];
3238 esp->dma_setup(esp, esp->esp_command_dvma, 2, 0);
3239 esp_setcount(eregs, 2);
3240 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3246 if(esp->do_pio_cmds){
3247 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3248 esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3249 esp_write(eregs->esp_fdata, esp->cur_msgout[2]);
3250 esp_write(eregs->esp_fdata, esp->cur_msgout[3]);
3251 esp_cmd(esp, eregs, ESP_CMD_TI);
3253 esp->esp_command[0] = esp->cur_msgout[0];
3254 esp->esp_command[1] = esp->cur_msgout[1];
3255 esp->esp_command[2] = esp->cur_msgout[2];
3256 esp->esp_command[3] = esp->cur_msgout[3];
3257 esp->dma_setup(esp, esp->esp_command_dvma, 4, 0);
3258 esp_setcount(eregs, 4);
3259 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3265 if(esp->do_pio_cmds){
3266 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3267 esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3268 esp_write(eregs->esp_fdata, esp->cur_msgout[2]);
3269 esp_write(eregs->esp_fdata, esp->cur_msgout[3]);
3270 esp_write(eregs->esp_fdata, esp->cur_msgout[4]);
3271 esp_cmd(esp, eregs, ESP_CMD_TI);
3273 esp->esp_command[0] = esp->cur_msgout[0];
3274 esp->esp_command[1] = esp->cur_msgout[1];
3275 esp->esp_command[2] = esp->cur_msgout[2];
3276 esp->esp_command[3] = esp->cur_msgout[3];
3277 esp->esp_command[4] = esp->cur_msgout[4];
3278 esp->dma_setup(esp, esp->esp_command_dvma, 5, 0);
3279 esp_setcount(eregs, 5);
3280 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3286 ESPMISC(("bogus msgout sending NOP\n"));
3287 esp->cur_msgout[0] = NOP;
3288 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3289 esp->msgout_len = 1;
3290 esp_cmd(esp, eregs, ESP_CMD_TI);
3293 esp_advance_phase(esp->current_SC, in_msgoutdone);
3297 static int esp_do_msgoutdone(struct NCR_ESP *esp,
3298 struct ESP_regs *eregs)
3300 if((esp->msgout_len > 1) && esp->dma_barrier)
3301 esp->dma_barrier(esp);
3303 if(!(esp->ireg & ESP_INTR_DC)) {
3304 esp_cmd(esp, eregs, ESP_CMD_NULL);
3305 switch(esp->sreg & ESP_STAT_PMASK) {
3307 /* whoops, parity error */
3308 ESPLOG(("esp%d: still in msgout, parity error assumed\n",
3310 if(esp->msgout_len > 1)
3311 esp_cmd(esp, eregs, ESP_CMD_SATN);
3312 esp_advance_phase(esp->current_SC, in_msgout);
3319 if(!fcount(esp, eregs) &&
3320 !(((struct esp_device *)esp->current_SC->device->hostdata)->sync_max_offset))
3321 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3327 /* If we sent out a synchronous negotiation message, update
3330 if(esp->cur_msgout[2] == EXTENDED_MESSAGE &&
3331 esp->cur_msgout[4] == EXTENDED_SDTR) {
3332 esp->snip = 1; /* anal retentiveness... */
3335 esp->prevmsgout = esp->cur_msgout[0];
3336 esp->msgout_len = 0;
3337 esp_advance_phase(esp->current_SC, in_the_dark);
3338 return esp_do_phase_determine(esp, eregs);
3341 static int esp_bus_unexpected(struct NCR_ESP *esp, struct ESP_regs *eregs)
3343 ESPLOG(("esp%d: command in weird state %2x\n",
3344 esp->esp_id, esp->current_SC->SCp.phase));
3345 return do_reset_bus;
3348 static espfunc_t bus_vector[] = {
3361 esp_do_phase_determine,
3367 /* This is the second tier in our dual-level SCSI state machine. */
3368 static int esp_work_bus(struct NCR_ESP *esp, struct ESP_regs *eregs)
3370 Scsi_Cmnd *SCptr = esp->current_SC;
3373 ESPBUS(("esp_work_bus: "));
3375 ESPBUS(("reconnect\n"));
3376 return esp_do_reconnect(esp, eregs);
3378 phase = SCptr->SCp.phase;
3379 if ((phase & 0xf0) == in_phases_mask)
3380 return bus_vector[(phase & 0x0f)](esp, eregs);
3381 else if((phase & 0xf0) == in_slct_mask)
3382 return esp_select_complete(esp, eregs);
3384 return esp_bus_unexpected(esp, eregs);
3387 static espfunc_t isvc_vector[] = {
3389 esp_do_phase_determine,
3395 /* Main interrupt handler for an esp adapter. */
3396 void esp_handle(struct NCR_ESP *esp)
3398 struct ESP_regs *eregs;
3400 int what_next = do_intr_end;
3402 SCptr = esp->current_SC;
3404 if(esp->dma_irq_entry)
3405 esp->dma_irq_entry(esp);
3407 /* Check for errors. */
3408 esp->sreg = esp_read(eregs->esp_status);
3409 esp->sreg &= (~ESP_STAT_INTR);
3410 esp->seqreg = (esp_read(eregs->esp_sstep) & ESP_STEP_VBITS);
3411 esp->ireg = esp_read(eregs->esp_intrpt); /* Unlatch intr and stat regs */
3412 ESPIRQ(("handle_irq: [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
3413 esp->sreg, esp->seqreg, esp->ireg));
3414 if(esp->sreg & (ESP_STAT_SPAM)) {
3415 /* Gross error, could be due to one of:
3417 * - top of fifo overwritten, could be because
3418 * we tried to do a synchronous transfer with
3419 * an offset greater than ESP fifo size
3421 * - top of command register overwritten
3423 * - DMA setup to go in one direction, SCSI
3424 * bus points in the other, whoops
3426 * - weird phase change during asynchronous
3427 * data phase while we are initiator
3429 ESPLOG(("esp%d: Gross error sreg=%2x\n", esp->esp_id, esp->sreg));
3431 /* If a command is live on the bus we cannot safely
3432 * reset the bus, so we'll just let the pieces fall
3433 * where they may. Here we are hoping that the
3434 * target will be able to cleanly go away soon
3435 * so we can safely reset things.
3438 ESPLOG(("esp%d: No current cmd during gross error, "
3439 "resetting bus\n", esp->esp_id));
3440 what_next = do_reset_bus;
3445 /* No current cmd is only valid at this point when there are
3446 * commands off the bus or we are trying a reset.
3448 if(!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
3449 /* Panic is safe, since current_SC is null. */
3450 ESPLOG(("esp%d: no command in esp_handle()\n", esp->esp_id));
3451 panic("esp_handle: current_SC == penguin within interrupt!");
3454 if(esp->ireg & (ESP_INTR_IC)) {
3455 /* Illegal command fed to ESP. Outside of obvious
3456 * software bugs that could cause this, there is
3457 * a condition with ESP100 where we can confuse the
3458 * ESP into an erroneous illegal command interrupt
3459 * because it does not scrape the FIFO properly
3460 * for reselection. See esp100_reconnect_hwbug()
3461 * to see how we try very hard to avoid this.
3463 ESPLOG(("esp%d: invalid command\n", esp->esp_id));
3465 esp_dump_state(esp, eregs);
3468 /* Devices with very buggy firmware can drop BSY
3469 * during a scatter list interrupt when using sync
3470 * mode transfers. We continue the transfer as
3471 * expected, the target drops the bus, the ESP
3472 * gets confused, and we get a illegal command
3473 * interrupt because the bus is in the disconnected
3474 * state now and ESP_CMD_TI is only allowed when
3475 * a nexus is alive on the bus.
3477 ESPLOG(("esp%d: Forcing async and disabling disconnect for "
3478 "target %d\n", esp->esp_id, SCptr->device->id));
3479 SCptr->device->borken = 1; /* foo on you */
3482 what_next = do_reset_bus;
3483 } else if(!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
3487 phase = SCptr->SCp.phase;
3488 if(phase & in_phases_mask) {
3489 what_next = esp_work_bus(esp, eregs);
3490 } else if(phase & in_slct_mask) {
3491 what_next = esp_select_complete(esp, eregs);
3493 ESPLOG(("esp%d: interrupt for no good reason...\n",
3495 what_next = do_intr_end;
3498 ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULL\n",
3500 what_next = do_reset_bus;
3502 } else if(esp->ireg & ESP_INTR_SR) {
3503 ESPLOG(("esp%d: SCSI bus reset interrupt\n", esp->esp_id));
3504 what_next = do_reset_complete;
3505 } else if(esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
3506 ESPLOG(("esp%d: AIEEE we have been selected by another initiator!\n",
3508 what_next = do_reset_bus;
3509 } else if(esp->ireg & ESP_INTR_RSEL) {
3512 what_next = esp_do_reconnect(esp, eregs);
3513 } else if(SCptr->SCp.phase & in_slct_mask) {
3514 /* Only selection code knows how to clean
3517 ESPDISC(("Reselected during selection attempt\n"));
3518 what_next = esp_select_complete(esp, eregs);
3520 ESPLOG(("esp%d: Reselected while bus is busy\n",
3522 what_next = do_reset_bus;
3526 /* This is tier-one in our dual level SCSI state machine. */
3528 while(what_next != do_intr_end) {
3529 if (what_next >= do_phase_determine &&
3530 what_next < do_intr_end)
3531 what_next = isvc_vector[what_next](esp, eregs);
3533 /* state is completely lost ;-( */
3534 ESPLOG(("esp%d: interrupt engine loses state, resetting bus\n",
3536 what_next = do_reset_bus;
3539 if(esp->dma_irq_exit)
3540 esp->dma_irq_exit(esp);
3544 irqreturn_t esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
3546 struct NCR_ESP *esp;
3547 unsigned long flags;
3549 struct Scsi_Host *dev = dev_id;
3551 /* Handle all ESP interrupts showing at this IRQ level. */
3552 spin_lock_irqsave(dev->host_lock, flags);
3557 if(((esp)->irq & 0xff) == irq) {
3559 if(esp->dma_irq_p(esp)) {
3562 esp->dma_ints_off(esp);
3564 ESPIRQ(("I%d(", esp->esp_id));
3568 esp->dma_ints_on(esp);
3576 spin_unlock_irqrestore(dev->host_lock, flags);
3580 /* For SMP we only service one ESP on the list list at our IRQ level! */
3581 irqreturn_t esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
3583 struct NCR_ESP *esp;
3584 unsigned long flags;
3585 struct Scsi_Host *dev = dev_id;
3587 /* Handle all ESP interrupts showing at this IRQ level. */
3588 spin_lock_irqsave(dev->host_lock, flags);
3590 if(((esp)->irq & 0xf) == irq) {
3591 if(esp->dma_irq_p(esp)) {
3592 esp->dma_ints_off(esp);
3594 ESPIRQ(("I[%d:%d](",
3595 smp_processor_id(), esp->esp_id));
3599 esp->dma_ints_on(esp);
3605 spin_unlock_irqrestore(dev->host_lock, flags);
3610 int esp_slave_alloc(Scsi_Device *SDptr)
3612 struct esp_device *esp_dev =
3613 kmalloc(sizeof(struct esp_device), GFP_ATOMIC);
3617 memset(esp_dev, 0, sizeof(struct esp_device));
3618 SDptr->hostdata = esp_dev;
3622 void esp_slave_destroy(Scsi_Device *SDptr)
3624 struct NCR_ESP *esp = (struct NCR_ESP *) SDptr->host->hostdata;
3626 esp->targets_present &= ~(1 << SDptr->id);
3627 kfree(SDptr->hostdata);
3628 SDptr->hostdata = NULL;
3632 int init_module(void) { return 0; }
3633 void cleanup_module(void) {}
3634 void esp_release(void)
3637 esps_running = esps_in_use;
3641 EXPORT_SYMBOL(esp_abort);
3642 EXPORT_SYMBOL(esp_allocate);
3643 EXPORT_SYMBOL(esp_deallocate);
3644 EXPORT_SYMBOL(esp_initialize);
3645 EXPORT_SYMBOL(esp_intr);
3646 EXPORT_SYMBOL(esp_queue);
3647 EXPORT_SYMBOL(esp_reset);
3648 EXPORT_SYMBOL(esp_slave_alloc);
3649 EXPORT_SYMBOL(esp_slave_destroy);
3650 EXPORT_SYMBOL(esps_in_use);
3652 MODULE_LICENSE("GPL");