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