IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / NCR53C9x.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
26#include <linux/kernel.h>
27#include <linux/delay.h>
28#include <linux/types.h>
29#include <linux/string.h>
30#include <linux/slab.h>
31#include <linux/blkdev.h>
32#include <linux/interrupt.h>
33#include <linux/proc_fs.h>
34#include <linux/stat.h>
35#include <linux/init.h>
36
37#include "scsi.h"
38#include <scsi/scsi_host.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. */
48enum {
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
84enum {
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. */
7dfaa5f4 96static struct NCR_ESP *espchain;
1da177e4
LT
97int nesps = 0, esps_in_use = 0, esps_running = 0;
98
7d12e780 99irqreturn_t esp_intr(int irq, void *dev_id);
1da177e4
LT
100
101/* Debugging routines */
102static 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 */
143static 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 */
160static 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 */
187static 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 */
210static 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
222static 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
279static 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
292static 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 */
324static 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
338static 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
344static 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
354static 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->device->id != target) || (ptr->device->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. */
374static 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. */
507void 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 */
d0be4a7d 531struct NCR_ESP* esp_allocate(struct scsi_host_template *tpnt, void *esp_dev)
1da177e4
LT
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 = NULL;
558
559 return esp;
560}
561
562void esp_deallocate(struct NCR_ESP *esp)
563{
564 struct NCR_ESP *elink;
565
566 if(espchain == esp) {
567 espchain = NULL;
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 */
580void 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 = NULL;
711 esp->disconnected_SC = NULL;
712 esp->issue_SC = NULL;
713
714 /* Clear the state machines. */
715 esp->targets_present = 0;
716 esp->resetting_bus = 0;
717 esp->snip = 0;
718
719 init_waitqueue_head(&esp->reset_queue);
720
721 esp->fas_premature_intr_workaround = 0;
722 for(i = 0; i < 32; i++)
723 esp->espcmdlog[i] = 0;
724 esp->espcmdent = 0;
725 for(i = 0; i < 16; i++) {
726 esp->cur_msgout[i] = 0;
727 esp->cur_msgin[i] = 0;
728 }
729 esp->prevmsgout = esp->prevmsgin = 0;
730 esp->msgout_len = esp->msgin_len = 0;
731
732 /* Clear the one behind caches to hold unmatchable values. */
733 esp->prev_soff = esp->prev_stp = esp->prev_cfg3 = 0xff;
734
735 /* Reset the thing before we try anything... */
736 esp_bootup_reset(esp, eregs);
737
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 */
745const 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. */
773struct info_str
774{
775 char *buffer;
776 int length;
777 int offset;
778 int pos;
779};
780
781static 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
801static 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
815static int esp_host_info(struct NCR_ESP *esp, char *ptr, off_t offset, int len)
816{
817 struct scsi_device *sdev;
818 struct info_str info;
819 int i;
820
821 info.buffer = ptr;
822 info.length = len;
823 info.offset = offset;
824 info.pos = 0;
825
826 copy_info(&info, "ESP Host Adapter:\n");
827 copy_info(&info, "\tESP Model\t\t");
828 switch(esp->erev) {
829 case esp100:
830 copy_info(&info, "ESP100 (NCR53C90)\n");
831 break;
832 case esp100a:
833 copy_info(&info, "ESP100A (NCR53C90A)\n");
834 break;
835 case esp236:
836 copy_info(&info, "ESP236 (NCR53C9x)\n");
837 break;
838 case fas216:
839 copy_info(&info, "Emulex FAS216\n");
840 break;
841 case fas236:
842 copy_info(&info, "Emulex FAS236\n");
843 break;
844 case fas100a:
845 copy_info(&info, "FPESP100A\n");
846 break;
847 case fast:
848 copy_info(&info, "Generic FAST\n");
849 break;
850 case fas366:
851 copy_info(&info, "QLogic FAS366\n");
852 break;
853 case fsc:
854 copy_info(&info, "Symbios Logic 53C9x-2\n");
855 break;
856 case espunknown:
857 default:
858 copy_info(&info, "Unknown!\n");
859 break;
860 };
861 copy_info(&info, "\tLive Targets\t\t[ ");
862 for(i = 0; i < 15; i++) {
863 if(esp->targets_present & (1 << i))
864 copy_info(&info, "%d ", i);
865 }
866 copy_info(&info, "]\n\n");
867
868 /* Now describe the state of each existing target. */
869 copy_info(&info, "Target #\tconfig3\t\tSync Capabilities\tDisconnect\n");
870
871 shost_for_each_device(sdev, esp->ehost) {
872 struct esp_device *esp_dev = sdev->hostdata;
873 uint id = sdev->id;
874
875 if (!(esp->targets_present & (1 << id)))
876 continue;
877
878 copy_info(&info, "%d\t\t", id);
879 copy_info(&info, "%08lx\t", esp->config3[id]);
880 copy_info(&info, "[%02lx,%02lx]\t\t\t",
881 esp_dev->sync_max_offset,
882 esp_dev->sync_min_period);
883 copy_info(&info, "%s\n", esp_dev->disconnect ? "yes" : "no");
884 }
885
886 return info.pos > info.offset? info.pos - info.offset : 0;
887}
888
889/* ESP proc filesystem code. */
890int esp_proc_info(struct Scsi_Host *shost, char *buffer, char **start, off_t offset, int length,
891 int inout)
892{
893 struct NCR_ESP *esp = (struct NCR_ESP *)shost->hostdata;
894
895 if(inout)
896 return -EINVAL; /* not yet */
897 if(start)
898 *start = buffer;
899 return esp_host_info(esp, buffer, offset, length);
900}
901
902static void esp_get_dmabufs(struct NCR_ESP *esp, Scsi_Cmnd *sp)
903{
904 if(sp->use_sg == 0) {
905 sp->SCp.this_residual = sp->request_bufflen;
906 sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
907 sp->SCp.buffers_residual = 0;
908 if (esp->dma_mmu_get_scsi_one)
909 esp->dma_mmu_get_scsi_one(esp, sp);
910 else
911 sp->SCp.ptr =
912 (char *) virt_to_phys(sp->request_buffer);
913 } else {
79bd3f85 914 sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
1da177e4
LT
915 sp->SCp.buffers_residual = sp->use_sg - 1;
916 sp->SCp.this_residual = sp->SCp.buffer->length;
917 if (esp->dma_mmu_get_scsi_sgl)
918 esp->dma_mmu_get_scsi_sgl(esp, sp);
919 else
920 sp->SCp.ptr =
921 (char *) virt_to_phys((page_address(sp->SCp.buffer->page) + sp->SCp.buffer->offset));
922 }
923}
924
925static void esp_release_dmabufs(struct NCR_ESP *esp, Scsi_Cmnd *sp)
926{
927 if(sp->use_sg == 0) {
928 if (esp->dma_mmu_release_scsi_one)
929 esp->dma_mmu_release_scsi_one(esp, sp);
930 } else {
931 if (esp->dma_mmu_release_scsi_sgl)
932 esp->dma_mmu_release_scsi_sgl(esp, sp);
933 }
934}
935
936static void esp_restore_pointers(struct NCR_ESP *esp, Scsi_Cmnd *sp)
937{
422c0d61 938 struct esp_pointers *ep = &esp->data_pointers[scmd_id(sp)];
1da177e4
LT
939
940 sp->SCp.ptr = ep->saved_ptr;
941 sp->SCp.buffer = ep->saved_buffer;
942 sp->SCp.this_residual = ep->saved_this_residual;
943 sp->SCp.buffers_residual = ep->saved_buffers_residual;
944}
945
946static void esp_save_pointers(struct NCR_ESP *esp, Scsi_Cmnd *sp)
947{
422c0d61 948 struct esp_pointers *ep = &esp->data_pointers[scmd_id(sp)];
1da177e4
LT
949
950 ep->saved_ptr = sp->SCp.ptr;
951 ep->saved_buffer = sp->SCp.buffer;
952 ep->saved_this_residual = sp->SCp.this_residual;
953 ep->saved_buffers_residual = sp->SCp.buffers_residual;
954}
955
956/* Some rules:
957 *
958 * 1) Never ever panic while something is live on the bus.
959 * If there is to be any chance of syncing the disks this
960 * rule is to be obeyed.
961 *
962 * 2) Any target that causes a foul condition will no longer
963 * have synchronous transfers done to it, no questions
964 * asked.
965 *
966 * 3) Keep register accesses to a minimum. Think about some
967 * day when we have Xbus machines this is running on and
968 * the ESP chip is on the other end of the machine on a
969 * different board from the cpu where this is running.
970 */
971
972/* Fire off a command. We assume the bus is free and that the only
973 * case where we could see an interrupt is where we have disconnected
974 * commands active and they are trying to reselect us.
975 */
976static inline void esp_check_cmd(struct NCR_ESP *esp, Scsi_Cmnd *sp)
977{
978 switch(sp->cmd_len) {
979 case 6:
980 case 10:
981 case 12:
982 esp->esp_slowcmd = 0;
983 break;
984
985 default:
986 esp->esp_slowcmd = 1;
987 esp->esp_scmdleft = sp->cmd_len;
988 esp->esp_scmdp = &sp->cmnd[0];
989 break;
990 };
991}
992
993static inline void build_sync_nego_msg(struct NCR_ESP *esp, int period, int offset)
994{
995 esp->cur_msgout[0] = EXTENDED_MESSAGE;
996 esp->cur_msgout[1] = 3;
997 esp->cur_msgout[2] = EXTENDED_SDTR;
998 esp->cur_msgout[3] = period;
999 esp->cur_msgout[4] = offset;
1000 esp->msgout_len = 5;
1001}
1002
1003static void esp_exec_cmd(struct NCR_ESP *esp)
1004{
1005 struct ESP_regs *eregs = esp->eregs;
1006 struct esp_device *esp_dev;
1007 Scsi_Cmnd *SCptr;
f64a181d 1008 struct scsi_device *SDptr;
1da177e4
LT
1009 volatile unchar *cmdp = esp->esp_command;
1010 unsigned char the_esp_command;
1011 int lun, target;
1012 int i;
1013
1014 /* Hold off if we have disconnected commands and
1015 * an IRQ is showing...
1016 */
1017 if(esp->disconnected_SC && esp->dma_irq_p(esp))
1018 return;
1019
1020 /* Grab first member of the issue queue. */
1021 SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
1022
1023 /* Safe to panic here because current_SC is null. */
1024 if(!SCptr)
1025 panic("esp: esp_exec_cmd and issue queue is NULL");
1026
1027 SDptr = SCptr->device;
1028 esp_dev = SDptr->hostdata;
1029 lun = SCptr->device->lun;
1030 target = SCptr->device->id;
1031
1032 esp->snip = 0;
1033 esp->msgout_len = 0;
1034
1035 /* Send it out whole, or piece by piece? The ESP
1036 * only knows how to automatically send out 6, 10,
1037 * and 12 byte commands. I used to think that the
1038 * Linux SCSI code would never throw anything other
1039 * than that to us, but then again there is the
1040 * SCSI generic driver which can send us anything.
1041 */
1042 esp_check_cmd(esp, SCptr);
1043
1044 /* If arbitration/selection is successful, the ESP will leave
1045 * ATN asserted, causing the target to go into message out
1046 * phase. The ESP will feed the target the identify and then
1047 * the target can only legally go to one of command,
1048 * datain/out, status, or message in phase, or stay in message
1049 * out phase (should we be trying to send a sync negotiation
1050 * message after the identify). It is not allowed to drop
1051 * BSY, but some buggy targets do and we check for this
1052 * condition in the selection complete code. Most of the time
1053 * we'll make the command bytes available to the ESP and it
1054 * will not interrupt us until it finishes command phase, we
1055 * cannot do this for command sizes the ESP does not
1056 * understand and in this case we'll get interrupted right
1057 * when the target goes into command phase.
1058 *
1059 * It is absolutely _illegal_ in the presence of SCSI-2 devices
1060 * to use the ESP select w/o ATN command. When SCSI-2 devices are
1061 * present on the bus we _must_ always go straight to message out
1062 * phase with an identify message for the target. Being that
1063 * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
1064 * selections should not confuse SCSI-1 we hope.
1065 */
1066
1067 if(esp_dev->sync) {
1068 /* this targets sync is known */
1069#ifdef CONFIG_SCSI_MAC_ESP
1070do_sync_known:
1071#endif
1072 if(esp_dev->disconnect)
1073 *cmdp++ = IDENTIFY(1, lun);
1074 else
1075 *cmdp++ = IDENTIFY(0, lun);
1076
1077 if(esp->esp_slowcmd) {
1078 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1079 esp_advance_phase(SCptr, in_slct_stop);
1080 } else {
1081 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1082 esp_advance_phase(SCptr, in_slct_norm);
1083 }
1084 } else if(!(esp->targets_present & (1<<target)) || !(esp_dev->disconnect)) {
1085 /* After the bootup SCSI code sends both the
1086 * TEST_UNIT_READY and INQUIRY commands we want
1087 * to at least attempt allowing the device to
1088 * disconnect.
1089 */
1090 ESPMISC(("esp: Selecting device for first time. target=%d "
1091 "lun=%d\n", target, SCptr->device->lun));
1092 if(!SDptr->borken && !esp_dev->disconnect)
1093 esp_dev->disconnect = 1;
1094
1095 *cmdp++ = IDENTIFY(0, lun);
1096 esp->prevmsgout = NOP;
1097 esp_advance_phase(SCptr, in_slct_norm);
1098 the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
1099
1100 /* Take no chances... */
1101 esp_dev->sync_max_offset = 0;
1102 esp_dev->sync_min_period = 0;
1103 } else {
1104 int toshiba_cdrom_hwbug_wkaround = 0;
1105
1106#ifdef CONFIG_SCSI_MAC_ESP
1107 /* Never allow synchronous transfers (disconnect OK) on
1108 * Macintosh. Well, maybe later when we figured out how to
1109 * do DMA on the machines that support it ...
1110 */
1111 esp_dev->disconnect = 1;
1112 esp_dev->sync_max_offset = 0;
1113 esp_dev->sync_min_period = 0;
1114 esp_dev->sync = 1;
1115 esp->snip = 0;
1116 goto do_sync_known;
1117#endif
1118 /* We've talked to this guy before,
1119 * but never negotiated. Let's try
1120 * sync negotiation.
1121 */
1122 if(!SDptr->borken) {
1123 if((SDptr->type == TYPE_ROM) &&
1124 (!strncmp(SDptr->vendor, "TOSHIBA", 7))) {
1125 /* Nice try sucker... */
1126 ESPMISC(("esp%d: Disabling sync for buggy "
1127 "Toshiba CDROM.\n", esp->esp_id));
1128 toshiba_cdrom_hwbug_wkaround = 1;
1129 build_sync_nego_msg(esp, 0, 0);
1130 } else {
1131 build_sync_nego_msg(esp, esp->sync_defp, 15);
1132 }
1133 } else {
1134 build_sync_nego_msg(esp, 0, 0);
1135 }
1136 esp_dev->sync = 1;
1137 esp->snip = 1;
1138
1139 /* A fix for broken SCSI1 targets, when they disconnect
1140 * they lock up the bus and confuse ESP. So disallow
1141 * disconnects for SCSI1 targets for now until we
1142 * find a better fix.
1143 *
1144 * Addendum: This is funny, I figured out what was going
1145 * on. The blotzed SCSI1 target would disconnect,
1146 * one of the other SCSI2 targets or both would be
1147 * disconnected as well. The SCSI1 target would
1148 * stay disconnected long enough that we start
1149 * up a command on one of the SCSI2 targets. As
1150 * the ESP is arbitrating for the bus the SCSI1
1151 * target begins to arbitrate as well to reselect
1152 * the ESP. The SCSI1 target refuses to drop it's
1153 * ID bit on the data bus even though the ESP is
1154 * at ID 7 and is the obvious winner for any
1155 * arbitration. The ESP is a poor sport and refuses
1156 * to lose arbitration, it will continue indefinitely
1157 * trying to arbitrate for the bus and can only be
1158 * stopped via a chip reset or SCSI bus reset.
1159 * Therefore _no_ disconnects for SCSI1 targets
1160 * thank you very much. ;-)
1161 */
1162 if(((SDptr->scsi_level < 3) && (SDptr->type != TYPE_TAPE)) ||
1163 toshiba_cdrom_hwbug_wkaround || SDptr->borken) {
1164 ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
1165 "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
1166 esp_dev->disconnect = 0;
1167 *cmdp++ = IDENTIFY(0, lun);
1168 } else {
1169 *cmdp++ = IDENTIFY(1, lun);
1170 }
1171
1172 /* ESP fifo is only so big...
1173 * Make this look like a slow command.
1174 */
1175 esp->esp_slowcmd = 1;
1176 esp->esp_scmdleft = SCptr->cmd_len;
1177 esp->esp_scmdp = &SCptr->cmnd[0];
1178
1179 the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
1180 esp_advance_phase(SCptr, in_slct_msg);
1181 }
1182
1183 if(!esp->esp_slowcmd)
1184 for(i = 0; i < SCptr->cmd_len; i++)
1185 *cmdp++ = SCptr->cmnd[i];
1186
1187 esp_write(eregs->esp_busid, (target & 7));
1188 if (esp->prev_soff != esp_dev->sync_max_offset ||
1189 esp->prev_stp != esp_dev->sync_min_period ||
1190 (esp->erev > esp100a &&
1191 esp->prev_cfg3 != esp->config3[target])) {
1192 esp->prev_soff = esp_dev->sync_max_offset;
1193 esp_write(eregs->esp_soff, esp->prev_soff);
1194 esp->prev_stp = esp_dev->sync_min_period;
1195 esp_write(eregs->esp_stp, esp->prev_stp);
1196 if(esp->erev > esp100a) {
1197 esp->prev_cfg3 = esp->config3[target];
1198 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
1199 }
1200 }
1201 i = (cmdp - esp->esp_command);
1202
1203 /* Set up the DMA and ESP counters */
1204 if(esp->do_pio_cmds){
1205 int j = 0;
1206
1207 /*
1208 * XXX MSch:
1209 *
1210 * It seems this is required, at least to clean up
1211 * after failed commands when using PIO mode ...
1212 */
1213 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1214
1215 for(;j<i;j++)
1216 esp_write(eregs->esp_fdata, esp->esp_command[j]);
1217 the_esp_command &= ~ESP_CMD_DMA;
1218
1219 /* Tell ESP to "go". */
1220 esp_cmd(esp, eregs, the_esp_command);
1221 } else {
1222 /* Set up the ESP counters */
1223 esp_write(eregs->esp_tclow, i);
1224 esp_write(eregs->esp_tcmed, 0);
1225 esp->dma_init_write(esp, esp->esp_command_dvma, i);
1226
1227 /* Tell ESP to "go". */
1228 esp_cmd(esp, eregs, the_esp_command);
1229 }
1230}
1231
1232/* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
1233int esp_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
1234{
1235 struct NCR_ESP *esp;
1236
1237 /* Set up func ptr and initial driver cmd-phase. */
1238 SCpnt->scsi_done = done;
1239 SCpnt->SCp.phase = not_issued;
1240
1241 esp = (struct NCR_ESP *) SCpnt->device->host->hostdata;
1242
1243 if(esp->dma_led_on)
1244 esp->dma_led_on(esp);
1245
1246 /* We use the scratch area. */
1247 ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->device->id, SCpnt->lun));
1248 ESPDISC(("N<%02x,%02x>", SCpnt->device->id, SCpnt->lun));
1249
1250 esp_get_dmabufs(esp, SCpnt);
1251 esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
1252
1253 SCpnt->SCp.Status = CHECK_CONDITION;
1254 SCpnt->SCp.Message = 0xff;
1255 SCpnt->SCp.sent_command = 0;
1256
1257 /* Place into our queue. */
1258 if(SCpnt->cmnd[0] == REQUEST_SENSE) {
1259 ESPQUEUE(("RQSENSE\n"));
1260 prepend_SC(&esp->issue_SC, SCpnt);
1261 } else {
1262 ESPQUEUE(("\n"));
1263 append_SC(&esp->issue_SC, SCpnt);
1264 }
1265
1266 /* Run it now if we can. */
1267 if(!esp->current_SC && !esp->resetting_bus)
1268 esp_exec_cmd(esp);
1269
1270 return 0;
1271}
1272
1273/* Dump driver state. */
1274static void esp_dump_cmd(Scsi_Cmnd *SCptr)
1275{
1276 ESPLOG(("[tgt<%02x> lun<%02x> "
1277 "pphase<%s> cphase<%s>]",
1278 SCptr->device->id, SCptr->device->lun,
1279 phase_string(SCptr->SCp.sent_command),
1280 phase_string(SCptr->SCp.phase)));
1281}
1282
1283static void esp_dump_state(struct NCR_ESP *esp,
1284 struct ESP_regs *eregs)
1285{
1286 Scsi_Cmnd *SCptr = esp->current_SC;
1287#ifdef DEBUG_ESP_CMDS
1288 int i;
1289#endif
1290
1291 ESPLOG(("esp%d: dumping state\n", esp->esp_id));
1292
1293 /* Print DMA status */
1294 esp->dma_dump_state(esp);
1295
1296 ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1297 esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
1298 ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
1299 esp->esp_id, esp_read(eregs->esp_status), esp_read(eregs->esp_sstep),
1300 esp_read(eregs->esp_intrpt)));
1301#ifdef DEBUG_ESP_CMDS
1302 printk("esp%d: last ESP cmds [", esp->esp_id);
1303 i = (esp->espcmdent - 1) & 31;
1304 printk("<");
1305 esp_print_cmd(esp->espcmdlog[i]);
1306 printk(">");
1307 i = (i - 1) & 31;
1308 printk("<");
1309 esp_print_cmd(esp->espcmdlog[i]);
1310 printk(">");
1311 i = (i - 1) & 31;
1312 printk("<");
1313 esp_print_cmd(esp->espcmdlog[i]);
1314 printk(">");
1315 i = (i - 1) & 31;
1316 printk("<");
1317 esp_print_cmd(esp->espcmdlog[i]);
1318 printk(">");
1319 printk("]\n");
1320#endif /* (DEBUG_ESP_CMDS) */
1321
1322 if(SCptr) {
1323 ESPLOG(("esp%d: current command ", esp->esp_id));
1324 esp_dump_cmd(SCptr);
1325 }
1326 ESPLOG(("\n"));
1327 SCptr = esp->disconnected_SC;
1328 ESPLOG(("esp%d: disconnected ", esp->esp_id));
1329 while(SCptr) {
1330 esp_dump_cmd(SCptr);
1331 SCptr = (Scsi_Cmnd *) SCptr->host_scribble;
1332 }
1333 ESPLOG(("\n"));
1334}
1335
1336/* Abort a command. The host_lock is acquired by caller. */
1337int esp_abort(Scsi_Cmnd *SCptr)
1338{
1339 struct NCR_ESP *esp = (struct NCR_ESP *) SCptr->device->host->hostdata;
1340 struct ESP_regs *eregs = esp->eregs;
1341 int don;
1342
1343 ESPLOG(("esp%d: Aborting command\n", esp->esp_id));
1344 esp_dump_state(esp, eregs);
1345
1346 /* Wheee, if this is the current command on the bus, the
1347 * best we can do is assert ATN and wait for msgout phase.
1348 * This should even fix a hung SCSI bus when we lose state
1349 * in the driver and timeout because the eventual phase change
1350 * will cause the ESP to (eventually) give an interrupt.
1351 */
1352 if(esp->current_SC == SCptr) {
1353 esp->cur_msgout[0] = ABORT;
1354 esp->msgout_len = 1;
1355 esp->msgout_ctr = 0;
1356 esp_cmd(esp, eregs, ESP_CMD_SATN);
1357 return SUCCESS;
1358 }
1359
1360 /* If it is still in the issue queue then we can safely
1361 * call the completion routine and report abort success.
1362 */
1363 don = esp->dma_ports_p(esp);
1364 if(don) {
1365 esp->dma_ints_off(esp);
1366 synchronize_irq(esp->irq);
1367 }
1368 if(esp->issue_SC) {
1369 Scsi_Cmnd **prev, *this;
1370 for(prev = (&esp->issue_SC), this = esp->issue_SC;
1371 this;
1372 prev = (Scsi_Cmnd **) &(this->host_scribble),
1373 this = (Scsi_Cmnd *) this->host_scribble) {
1374 if(this == SCptr) {
1375 *prev = (Scsi_Cmnd *) this->host_scribble;
1376 this->host_scribble = NULL;
1377 esp_release_dmabufs(esp, this);
1378 this->result = DID_ABORT << 16;
1379 this->done(this);
1380 if(don)
1381 esp->dma_ints_on(esp);
1382 return SUCCESS;
1383 }
1384 }
1385 }
1386
1387 /* Yuck, the command to abort is disconnected, it is not
1388 * worth trying to abort it now if something else is live
1389 * on the bus at this time. So, we let the SCSI code wait
1390 * a little bit and try again later.
1391 */
1392 if(esp->current_SC) {
1393 if(don)
1394 esp->dma_ints_on(esp);
1395 return FAILED;
1396 }
1397
1398 /* It's disconnected, we have to reconnect to re-establish
1399 * the nexus and tell the device to abort. However, we really
1400 * cannot 'reconnect' per se. Don't try to be fancy, just
1401 * indicate failure, which causes our caller to reset the whole
1402 * bus.
1403 */
1404
1405 if(don)
1406 esp->dma_ints_on(esp);
1407 return FAILED;
1408}
1409
1410/* We've sent ESP_CMD_RS to the ESP, the interrupt had just
1411 * arrived indicating the end of the SCSI bus reset. Our job
1412 * is to clean out the command queues and begin re-execution
1413 * of SCSI commands once more.
1414 */
1415static int esp_finish_reset(struct NCR_ESP *esp,
1416 struct ESP_regs *eregs)
1417{
1418 Scsi_Cmnd *sp = esp->current_SC;
1419
1420 /* Clean up currently executing command, if any. */
1421 if (sp != NULL) {
1422 esp_release_dmabufs(esp, sp);
1423 sp->result = (DID_RESET << 16);
1424 sp->scsi_done(sp);
1425 esp->current_SC = NULL;
1426 }
1427
1428 /* Clean up disconnected queue, they have been invalidated
1429 * by the bus reset.
1430 */
1431 if (esp->disconnected_SC) {
1432 while((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
1433 esp_release_dmabufs(esp, sp);
1434 sp->result = (DID_RESET << 16);
1435 sp->scsi_done(sp);
1436 }
1437 }
1438
1439 /* SCSI bus reset is complete. */
1440 esp->resetting_bus = 0;
1441 wake_up(&esp->reset_queue);
1442
1443 /* Ok, now it is safe to get commands going once more. */
1444 if(esp->issue_SC)
1445 esp_exec_cmd(esp);
1446
1447 return do_intr_end;
1448}
1449
1450static int esp_do_resetbus(struct NCR_ESP *esp,
1451 struct ESP_regs *eregs)
1452{
1453 ESPLOG(("esp%d: Resetting scsi bus\n", esp->esp_id));
1454 esp->resetting_bus = 1;
1455 esp_cmd(esp, eregs, ESP_CMD_RS);
1456
1457 return do_intr_end;
1458}
1459
1460/* Reset ESP chip, reset hanging bus, then kill active and
1461 * disconnected commands for targets without soft reset.
1462 *
1463 * The host_lock is acquired by caller.
1464 */
1465int esp_reset(Scsi_Cmnd *SCptr)
1466{
1467 struct NCR_ESP *esp = (struct NCR_ESP *) SCptr->device->host->hostdata;
1468
68b3aa7c 1469 spin_lock_irq(esp->ehost->host_lock);
1da177e4 1470 (void) esp_do_resetbus(esp, esp->eregs);
1da177e4
LT
1471 spin_unlock_irq(esp->ehost->host_lock);
1472
1473 wait_event(esp->reset_queue, (esp->resetting_bus == 0));
1474
1da177e4
LT
1475 return SUCCESS;
1476}
1477
1478/* Internal ESP done function. */
1479static void esp_done(struct NCR_ESP *esp, int error)
1480{
1481 Scsi_Cmnd *done_SC;
1482
1483 if(esp->current_SC) {
1484 done_SC = esp->current_SC;
1485 esp->current_SC = NULL;
1486 esp_release_dmabufs(esp, done_SC);
1487 done_SC->result = error;
1488 done_SC->scsi_done(done_SC);
1489
1490 /* Bus is free, issue any commands in the queue. */
1491 if(esp->issue_SC && !esp->current_SC)
1492 esp_exec_cmd(esp);
1493 } else {
1494 /* Panic is safe as current_SC is null so we may still
1495 * be able to accept more commands to sync disk buffers.
1496 */
1497 ESPLOG(("panicing\n"));
1498 panic("esp: done() called with NULL esp->current_SC");
1499 }
1500}
1501
1502/* Wheee, ESP interrupt engine. */
1503
1504/* Forward declarations. */
1505static int esp_do_phase_determine(struct NCR_ESP *esp,
1506 struct ESP_regs *eregs);
1507static int esp_do_data_finale(struct NCR_ESP *esp, struct ESP_regs *eregs);
1508static int esp_select_complete(struct NCR_ESP *esp, struct ESP_regs *eregs);
1509static int esp_do_status(struct NCR_ESP *esp, struct ESP_regs *eregs);
1510static int esp_do_msgin(struct NCR_ESP *esp, struct ESP_regs *eregs);
1511static int esp_do_msgindone(struct NCR_ESP *esp, struct ESP_regs *eregs);
1512static int esp_do_msgout(struct NCR_ESP *esp, struct ESP_regs *eregs);
1513static int esp_do_cmdbegin(struct NCR_ESP *esp, struct ESP_regs *eregs);
1514
1515#define sreg_datainp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
1516#define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
1517
1518/* We try to avoid some interrupts by jumping ahead and see if the ESP
1519 * has gotten far enough yet. Hence the following.
1520 */
1521static inline int skipahead1(struct NCR_ESP *esp, struct ESP_regs *eregs,
1522 Scsi_Cmnd *scp, int prev_phase, int new_phase)
1523{
1524 if(scp->SCp.sent_command != prev_phase)
1525 return 0;
1526
1527 if(esp->dma_irq_p(esp)) {
1528 /* Yes, we are able to save an interrupt. */
1529 esp->sreg = (esp_read(eregs->esp_status) & ~(ESP_STAT_INTR));
1530 esp->ireg = esp_read(eregs->esp_intrpt);
1531 if(!(esp->ireg & ESP_INTR_SR))
1532 return 0;
1533 else
1534 return do_reset_complete;
1535 }
1536 /* Ho hum, target is taking forever... */
1537 scp->SCp.sent_command = new_phase; /* so we don't recurse... */
1538 return do_intr_end;
1539}
1540
1541static inline int skipahead2(struct NCR_ESP *esp,
1542 struct ESP_regs *eregs,
1543 Scsi_Cmnd *scp, int prev_phase1, int prev_phase2,
1544 int new_phase)
1545{
1546 if(scp->SCp.sent_command != prev_phase1 &&
1547 scp->SCp.sent_command != prev_phase2)
1548 return 0;
1549 if(esp->dma_irq_p(esp)) {
1550 /* Yes, we are able to save an interrupt. */
1551 esp->sreg = (esp_read(eregs->esp_status) & ~(ESP_STAT_INTR));
1552 esp->ireg = esp_read(eregs->esp_intrpt);
1553 if(!(esp->ireg & ESP_INTR_SR))
1554 return 0;
1555 else
1556 return do_reset_complete;
1557 }
1558 /* Ho hum, target is taking forever... */
1559 scp->SCp.sent_command = new_phase; /* so we don't recurse... */
1560 return do_intr_end;
1561}
1562
1563/* Misc. esp helper macros. */
1564#define esp_setcount(__eregs, __cnt) \
1565 esp_write((__eregs)->esp_tclow, ((__cnt) & 0xff)); \
1566 esp_write((__eregs)->esp_tcmed, (((__cnt) >> 8) & 0xff))
1567
1568#define esp_getcount(__eregs) \
1569 ((esp_read((__eregs)->esp_tclow)&0xff) | \
1570 ((esp_read((__eregs)->esp_tcmed)&0xff) << 8))
1571
1572#define fcount(__esp, __eregs) \
1573 (esp_read((__eregs)->esp_fflags) & ESP_FF_FBYTES)
1574
1575#define fnzero(__esp, __eregs) \
1576 (esp_read((__eregs)->esp_fflags) & ESP_FF_ONOTZERO)
1577
1578/* XXX speculative nops unnecessary when continuing amidst a data phase
1579 * XXX even on esp100!!! another case of flooding the bus with I/O reg
1580 * XXX writes...
1581 */
1582#define esp_maybe_nop(__esp, __eregs) \
1583 if((__esp)->erev == esp100) \
1584 esp_cmd((__esp), (__eregs), ESP_CMD_NULL)
1585
1586#define sreg_to_dataphase(__sreg) \
1587 ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
1588
1589/* The ESP100 when in synchronous data phase, can mistake a long final
1590 * REQ pulse from the target as an extra byte, it places whatever is on
1591 * the data lines into the fifo. For now, we will assume when this
1592 * happens that the target is a bit quirky and we don't want to
1593 * be talking synchronously to it anyways. Regardless, we need to
1594 * tell the ESP to eat the extraneous byte so that we can proceed
1595 * to the next phase.
1596 */
1597static inline int esp100_sync_hwbug(struct NCR_ESP *esp, struct ESP_regs *eregs,
1598 Scsi_Cmnd *sp, int fifocnt)
1599{
1600 /* Do not touch this piece of code. */
1601 if((!(esp->erev == esp100)) ||
1602 (!(sreg_datainp((esp->sreg = esp_read(eregs->esp_status))) && !fifocnt) &&
1603 !(sreg_dataoutp(esp->sreg) && !fnzero(esp, eregs)))) {
1604 if(sp->SCp.phase == in_dataout)
1605 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1606 return 0;
1607 } else {
1608 /* Async mode for this guy. */
1609 build_sync_nego_msg(esp, 0, 0);
1610
1611 /* Ack the bogus byte, but set ATN first. */
1612 esp_cmd(esp, eregs, ESP_CMD_SATN);
1613 esp_cmd(esp, eregs, ESP_CMD_MOK);
1614 return 1;
1615 }
1616}
1617
1618/* This closes the window during a selection with a reselect pending, because
1619 * we use DMA for the selection process the FIFO should hold the correct
1620 * contents if we get reselected during this process. So we just need to
1621 * ack the possible illegal cmd interrupt pending on the esp100.
1622 */
1623static inline int esp100_reconnect_hwbug(struct NCR_ESP *esp,
1624 struct ESP_regs *eregs)
1625{
1626 volatile unchar junk;
1627
1628 if(esp->erev != esp100)
1629 return 0;
1630 junk = esp_read(eregs->esp_intrpt);
1631
1632 if(junk & ESP_INTR_SR)
1633 return 1;
1634 return 0;
1635}
1636
1637/* This verifies the BUSID bits during a reselection so that we know which
1638 * target is talking to us.
1639 */
1640static inline int reconnect_target(struct NCR_ESP *esp, struct ESP_regs *eregs)
1641{
1642 int it, me = esp->scsi_id_mask, targ = 0;
1643
1644 if(2 != fcount(esp, eregs))
1645 return -1;
1646 it = esp_read(eregs->esp_fdata);
1647 if(!(it & me))
1648 return -1;
1649 it &= ~me;
1650 if(it & (it - 1))
1651 return -1;
1652 while(!(it & 1))
1653 targ++, it >>= 1;
1654 return targ;
1655}
1656
1657/* This verifies the identify from the target so that we know which lun is
1658 * being reconnected.
1659 */
1660static inline int reconnect_lun(struct NCR_ESP *esp, struct ESP_regs *eregs)
1661{
1662 int lun;
1663
1664 if((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
1665 return -1;
1666 lun = esp_read(eregs->esp_fdata);
1667
1668 /* Yes, you read this correctly. We report lun of zero
1669 * if we see parity error. ESP reports parity error for
1670 * the lun byte, and this is the only way to hope to recover
1671 * because the target is connected.
1672 */
1673 if(esp->sreg & ESP_STAT_PERR)
1674 return 0;
1675
1676 /* Check for illegal bits being set in the lun. */
1677 if((lun & 0x40) || !(lun & 0x80))
1678 return -1;
1679
1680 return lun & 7;
1681}
1682
1683/* This puts the driver in a state where it can revitalize a command that
1684 * is being continued due to reselection.
1685 */
1686static inline void esp_connect(struct NCR_ESP *esp, struct ESP_regs *eregs,
1687 Scsi_Cmnd *sp)
1688{
f64a181d 1689 struct scsi_device *dp = sp->device;
1da177e4
LT
1690 struct esp_device *esp_dev = dp->hostdata;
1691
1692 if(esp->prev_soff != esp_dev->sync_max_offset ||
1693 esp->prev_stp != esp_dev->sync_min_period ||
1694 (esp->erev > esp100a &&
422c0d61 1695 esp->prev_cfg3 != esp->config3[scmd_id(sp)])) {
1da177e4
LT
1696 esp->prev_soff = esp_dev->sync_max_offset;
1697 esp_write(eregs->esp_soff, esp->prev_soff);
1698 esp->prev_stp = esp_dev->sync_min_period;
1699 esp_write(eregs->esp_stp, esp->prev_stp);
1700 if(esp->erev > esp100a) {
422c0d61 1701 esp->prev_cfg3 = esp->config3[scmd_id(sp)];
1da177e4
LT
1702 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
1703 }
1704 }
1705 esp->current_SC = sp;
1706}
1707
1708/* This will place the current working command back into the issue queue
1709 * if we are to receive a reselection amidst a selection attempt.
1710 */
1711static inline void esp_reconnect(struct NCR_ESP *esp, Scsi_Cmnd *sp)
1712{
1713 if(!esp->disconnected_SC)
1714 ESPLOG(("esp%d: Weird, being reselected but disconnected "
1715 "command queue is empty.\n", esp->esp_id));
1716 esp->snip = 0;
1717 esp->current_SC = NULL;
1718 sp->SCp.phase = not_issued;
1719 append_SC(&esp->issue_SC, sp);
1720}
1721
1722/* Begin message in phase. */
1723static int esp_do_msgin(struct NCR_ESP *esp, struct ESP_regs *eregs)
1724{
1725 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
1726 esp_maybe_nop(esp, eregs);
1727 esp_cmd(esp, eregs, ESP_CMD_TI);
1728 esp->msgin_len = 1;
1729 esp->msgin_ctr = 0;
1730 esp_advance_phase(esp->current_SC, in_msgindone);
1731 return do_work_bus;
1732}
1733
1734static inline void advance_sg(struct NCR_ESP *esp, Scsi_Cmnd *sp)
1735{
1736 ++sp->SCp.buffer;
1737 --sp->SCp.buffers_residual;
1738 sp->SCp.this_residual = sp->SCp.buffer->length;
1739 if (esp->dma_advance_sg)
1740 esp->dma_advance_sg (sp);
1741 else
1742 sp->SCp.ptr = (char *) virt_to_phys((page_address(sp->SCp.buffer->page) + sp->SCp.buffer->offset));
1743
1744}
1745
1746/* Please note that the way I've coded these routines is that I _always_
1747 * check for a disconnect during any and all information transfer
1748 * phases. The SCSI standard states that the target _can_ cause a BUS
1749 * FREE condition by dropping all MSG/CD/IO/BSY signals. Also note
1750 * that during information transfer phases the target controls every
1751 * change in phase, the only thing the initiator can do is "ask" for
1752 * a message out phase by driving ATN true. The target can, and sometimes
1753 * will, completely ignore this request so we cannot assume anything when
1754 * we try to force a message out phase to abort/reset a target. Most of
1755 * the time the target will eventually be nice and go to message out, so
1756 * we may have to hold on to our state about what we want to tell the target
1757 * for some period of time.
1758 */
1759
1760/* I think I have things working here correctly. Even partial transfers
1761 * within a buffer or sub-buffer should not upset us at all no matter
1762 * how bad the target and/or ESP fucks things up.
1763 */
1764static int esp_do_data(struct NCR_ESP *esp, struct ESP_regs *eregs)
1765{
1766 Scsi_Cmnd *SCptr = esp->current_SC;
1767 int thisphase, hmuch;
1768
1769 ESPDATA(("esp_do_data: "));
1770 esp_maybe_nop(esp, eregs);
1771 thisphase = sreg_to_dataphase(esp->sreg);
1772 esp_advance_phase(SCptr, thisphase);
1773 ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
1774 hmuch = esp->dma_can_transfer(esp, SCptr);
1775
1776 /*
1777 * XXX MSch: cater for PIO transfer here; PIO used if hmuch == 0
1778 */
1779 if (hmuch) { /* DMA */
1780 /*
1781 * DMA
1782 */
1783 ESPDATA(("hmuch<%d> ", hmuch));
1784 esp->current_transfer_size = hmuch;
1785 esp_setcount(eregs, (esp->fas_premature_intr_workaround ?
1786 (hmuch + 0x40) : hmuch));
1787 esp->dma_setup(esp, (__u32)((unsigned long)SCptr->SCp.ptr),
1788 hmuch, (thisphase == in_datain));
1789 ESPDATA(("DMA|TI --> do_intr_end\n"));
1790 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
1791 return do_intr_end;
1792 /*
1793 * end DMA
1794 */
1795 } else {
1796 /*
1797 * PIO
1798 */
1799 int oldphase, i = 0; /* or where we left off last time ?? esp->current_data ?? */
1800 int fifocnt = 0;
3c9757b7 1801 unsigned char *p = phys_to_virt((unsigned long)SCptr->SCp.ptr);
1da177e4
LT
1802
1803 oldphase = esp_read(eregs->esp_status) & ESP_STAT_PMASK;
1804
1805 /*
1806 * polled transfer; ugly, can we make this happen in a DRQ
1807 * interrupt handler ??
1808 * requires keeping track of state information in host or
1809 * command struct!
1810 * Problem: I've never seen a DRQ happen on Mac, not even
1811 * with ESP_CMD_DMA ...
1812 */
1813
1814 /* figure out how much needs to be transferred */
1815 hmuch = SCptr->SCp.this_residual;
1816 ESPDATA(("hmuch<%d> pio ", hmuch));
1817 esp->current_transfer_size = hmuch;
1818
1819 /* tell the ESP ... */
1820 esp_setcount(eregs, hmuch);
1821
1822 /* loop */
1823 while (hmuch) {
1824 int j, fifo_stuck = 0, newphase;
cad359c6
AB
1825 unsigned long timeout;
1826#if 0
1827 unsigned long flags;
1828#endif
1da177e4
LT
1829#if 0
1830 if ( i % 10 )
1831 ESPDATA(("\r"));
1832 else
1833 ESPDATA(( /*"\n"*/ "\r"));
1834#endif
1835#if 0
1836 local_irq_save(flags);
1837#endif
1838 if(thisphase == in_datain) {
1839 /* 'go' ... */
1840 esp_cmd(esp, eregs, ESP_CMD_TI);
1841
1842 /* wait for data */
1843 timeout = 1000000;
1844 while (!((esp->sreg=esp_read(eregs->esp_status)) & ESP_STAT_INTR) && --timeout)
1845 udelay(2);
1846 if (timeout == 0)
1847 printk("DRQ datain timeout! \n");
1848
1849 newphase = esp->sreg & ESP_STAT_PMASK;
1850
1851 /* see how much we got ... */
1852 fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
1853
1854 if (!fifocnt)
1855 fifo_stuck++;
1856 else
1857 fifo_stuck = 0;
1858
1859 ESPDATA(("\rgot %d st %x ph %x", fifocnt, esp->sreg, newphase));
1860
1861 /* read fifo */
1862 for(j=0;j<fifocnt;j++)
3c9757b7 1863 p[i++] = esp_read(eregs->esp_fdata);
1da177e4
LT
1864
1865 ESPDATA(("(%d) ", i));
1866
1867 /* how many to go ?? */
1868 hmuch -= fifocnt;
1869
1870 /* break if status phase !! */
1871 if(newphase == ESP_STATP) {
1872 /* clear int. */
1873 esp->ireg = esp_read(eregs->esp_intrpt);
1874 break;
1875 }
1876 } else {
1877#define MAX_FIFO 8
1878 /* how much will fit ? */
1879 int this_count = MAX_FIFO - fifocnt;
1880 if (this_count > hmuch)
1881 this_count = hmuch;
1882
1883 /* fill fifo */
1884 for(j=0;j<this_count;j++)
3c9757b7 1885 esp_write(eregs->esp_fdata, p[i++]);
1da177e4
LT
1886
1887 /* how many left if this goes out ?? */
1888 hmuch -= this_count;
1889
1890 /* 'go' ... */
1891 esp_cmd(esp, eregs, ESP_CMD_TI);
1892
1893 /* wait for 'got it' */
1894 timeout = 1000000;
1895 while (!((esp->sreg=esp_read(eregs->esp_status)) & ESP_STAT_INTR) && --timeout)
1896 udelay(2);
1897 if (timeout == 0)
1898 printk("DRQ dataout timeout! \n");
1899
1900 newphase = esp->sreg & ESP_STAT_PMASK;
1901
1902 /* need to check how much was sent ?? */
1903 fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
1904
1905 ESPDATA(("\rsent %d st %x ph %x", this_count - fifocnt, esp->sreg, newphase));
1906
1907 ESPDATA(("(%d) ", i));
1908
1909 /* break if status phase !! */
1910 if(newphase == ESP_STATP) {
1911 /* clear int. */
1912 esp->ireg = esp_read(eregs->esp_intrpt);
1913 break;
1914 }
1915
1916 }
1917
1918 /* clear int. */
1919 esp->ireg = esp_read(eregs->esp_intrpt);
1920
1921 ESPDATA(("ir %x ... ", esp->ireg));
1922
1923 if (hmuch == 0)
1924 ESPDATA(("done! \n"));
1925
1926#if 0
1927 local_irq_restore(flags);
1928#endif
1929
1930 /* check new bus phase */
1931 if (newphase != oldphase && i < esp->current_transfer_size) {
1932 /* something happened; disconnect ?? */
1933 ESPDATA(("phase change, dropped out with %d done ... ", i));
1934 break;
1935 }
1936
1937 /* check int. status */
1938 if (esp->ireg & ESP_INTR_DC) {
1939 /* disconnect */
1940 ESPDATA(("disconnect; %d transferred ... ", i));
1941 break;
1942 } else if (esp->ireg & ESP_INTR_FDONE) {
1943 /* function done */
1944 ESPDATA(("function done; %d transferred ... ", i));
1945 break;
1946 }
1947
1948 /* XXX fixme: bail out on stall */
1949 if (fifo_stuck > 10) {
1950 /* we're stuck */
1951 ESPDATA(("fifo stall; %d transferred ... ", i));
1952 break;
1953 }
1954 }
1955
1956 ESPDATA(("\n"));
1957 /* check successful completion ?? */
1958
1959 if (thisphase == in_dataout)
1960 hmuch += fifocnt; /* stuck?? adjust data pointer ...*/
1961
1962 /* tell do_data_finale how much was transferred */
1963 esp->current_transfer_size -= hmuch;
1964
1965 /* still not completely sure on this one ... */
1966 return /*do_intr_end*/ do_work_bus /*do_phase_determine*/ ;
1967
1968 /*
1969 * end PIO
1970 */
1971 }
1972 return do_intr_end;
1973}
1974
1975/* See how successful the data transfer was. */
1976static int esp_do_data_finale(struct NCR_ESP *esp,
1977 struct ESP_regs *eregs)
1978{
1979 Scsi_Cmnd *SCptr = esp->current_SC;
1980 struct esp_device *esp_dev = SCptr->device->hostdata;
1981 int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
1982
1983 if(esp->dma_led_off)
1984 esp->dma_led_off(esp);
1985
1986 ESPDATA(("esp_do_data_finale: "));
1987
1988 if(SCptr->SCp.phase == in_datain) {
1989 if(esp->sreg & ESP_STAT_PERR) {
1990 /* Yuck, parity error. The ESP asserts ATN
1991 * so that we can go to message out phase
1992 * immediately and inform the target that
1993 * something bad happened.
1994 */
1995 ESPLOG(("esp%d: data bad parity detected.\n",
1996 esp->esp_id));
1997 esp->cur_msgout[0] = INITIATOR_ERROR;
1998 esp->msgout_len = 1;
1999 }
2000 if(esp->dma_drain)
2001 esp->dma_drain(esp);
2002 }
2003 if(esp->dma_invalidate)
2004 esp->dma_invalidate(esp);
2005
2006 /* This could happen for the above parity error case. */
2007 if(!(esp->ireg == ESP_INTR_BSERV)) {
2008 /* Please go to msgout phase, please please please... */
2009 ESPLOG(("esp%d: !BSERV after data, probably to msgout\n",
2010 esp->esp_id));
2011 return esp_do_phase_determine(esp, eregs);
2012 }
2013
2014 /* Check for partial transfers and other horrible events. */
2015 fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
2016 ecount = esp_getcount(eregs);
2017 if(esp->fas_premature_intr_workaround)
2018 ecount -= 0x40;
2019 bytes_sent = esp->current_transfer_size;
2020
2021 ESPDATA(("trans_sz=%d, ", bytes_sent));
2022 if(!(esp->sreg & ESP_STAT_TCNT))
2023 bytes_sent -= ecount;
2024 if(SCptr->SCp.phase == in_dataout)
2025 bytes_sent -= fifocnt;
2026
2027 ESPDATA(("bytes_sent=%d (ecount=%d, fifocnt=%d), ", bytes_sent,
2028 ecount, fifocnt));
2029
2030 /* If we were in synchronous mode, check for peculiarities. */
2031 if(esp_dev->sync_max_offset)
2032 bogus_data = esp100_sync_hwbug(esp, eregs, SCptr, fifocnt);
2033 else
2034 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2035
2036 /* Until we are sure of what has happened, we are certainly
2037 * in the dark.
2038 */
2039 esp_advance_phase(SCptr, in_the_dark);
2040
2041 /* Check for premature interrupt condition. Can happen on FAS2x6
2042 * chips. QLogic recommends a workaround by overprogramming the
2043 * transfer counters, but this makes doing scatter-gather impossible.
2044 * Until there is a way to disable scatter-gather for a single target,
2045 * and not only for the entire host adapter as it is now, the workaround
2046 * is way to expensive performance wise.
2047 * Instead, it turns out that when this happens the target has disconnected
2048 * already but it doesn't show in the interrupt register. Compensate for
2049 * that here to try and avoid a SCSI bus reset.
2050 */
2051 if(!esp->fas_premature_intr_workaround && (fifocnt == 1) &&
2052 sreg_dataoutp(esp->sreg)) {
2053 ESPLOG(("esp%d: Premature interrupt, enabling workaround\n",
2054 esp->esp_id));
2055#if 0
2056 /* Disable scatter-gather operations, they are not possible
2057 * when using this workaround.
2058 */
2059 esp->ehost->sg_tablesize = 0;
2060 esp->ehost->use_clustering = ENABLE_CLUSTERING;
2061 esp->fas_premature_intr_workaround = 1;
2062 bytes_sent = 0;
2063 if(SCptr->use_sg) {
2064 ESPLOG(("esp%d: Aborting scatter-gather operation\n",
2065 esp->esp_id));
2066 esp->cur_msgout[0] = ABORT;
2067 esp->msgout_len = 1;
2068 esp->msgout_ctr = 0;
2069 esp_cmd(esp, eregs, ESP_CMD_SATN);
2070 esp_setcount(eregs, 0xffff);
2071 esp_cmd(esp, eregs, ESP_CMD_NULL);
2072 esp_cmd(esp, eregs, ESP_CMD_TPAD | ESP_CMD_DMA);
2073 return do_intr_end;
2074 }
2075#else
2076 /* Just set the disconnected bit. That's what appears to
2077 * happen anyway. The state machine will pick it up when
2078 * we return.
2079 */
2080 esp->ireg |= ESP_INTR_DC;
2081#endif
2082 }
2083
2084 if(bytes_sent < 0) {
2085 /* I've seen this happen due to lost state in this
2086 * driver. No idea why it happened, but allowing
2087 * this value to be negative caused things to
2088 * lock up. This allows greater chance of recovery.
2089 * In fact every time I've seen this, it has been
2090 * a driver bug without question.
2091 */
2092 ESPLOG(("esp%d: yieee, bytes_sent < 0!\n", esp->esp_id));
2093 ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%d\n",
2094 esp->esp_id,
2095 esp->current_transfer_size, fifocnt, ecount));
2096 ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%d\n",
2097 esp->esp_id,
2098 SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
2099 ESPLOG(("esp%d: Forcing async for target %d\n", esp->esp_id,
2100 SCptr->device->id));
2101 SCptr->device->borken = 1;
2102 esp_dev->sync = 0;
2103 bytes_sent = 0;
2104 }
2105
2106 /* Update the state of our transfer. */
2107 SCptr->SCp.ptr += bytes_sent;
2108 SCptr->SCp.this_residual -= bytes_sent;
2109 if(SCptr->SCp.this_residual < 0) {
2110 /* shit */
2111 ESPLOG(("esp%d: Data transfer overrun.\n", esp->esp_id));
2112 SCptr->SCp.this_residual = 0;
2113 }
2114
2115 /* Maybe continue. */
2116 if(!bogus_data) {
2117 ESPDATA(("!bogus_data, "));
2118 /* NO MATTER WHAT, we advance the scatterlist,
2119 * if the target should decide to disconnect
2120 * in between scatter chunks (which is common)
2121 * we could die horribly! I used to have the sg
2122 * advance occur only if we are going back into
2123 * (or are staying in) a data phase, you can
2124 * imagine the hell I went through trying to
2125 * figure this out.
2126 */
2127 if(!SCptr->SCp.this_residual && SCptr->SCp.buffers_residual)
2128 advance_sg(esp, SCptr);
2129#ifdef DEBUG_ESP_DATA
2130 if(sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
2131 ESPDATA(("to more data\n"));
2132 } else {
2133 ESPDATA(("to new phase\n"));
2134 }
2135#endif
2136 return esp_do_phase_determine(esp, eregs);
2137 }
2138 /* Bogus data, just wait for next interrupt. */
2139 ESPLOG(("esp%d: bogus_data during end of data phase\n",
2140 esp->esp_id));
2141 return do_intr_end;
2142}
2143
2144/* We received a non-good status return at the end of
2145 * running a SCSI command. This is used to decide if
2146 * we should clear our synchronous transfer state for
2147 * such a device when that happens.
2148 *
2149 * The idea is that when spinning up a disk or rewinding
2150 * a tape, we don't want to go into a loop re-negotiating
2151 * synchronous capabilities over and over.
2152 */
2153static int esp_should_clear_sync(Scsi_Cmnd *sp)
2154{
64821324 2155 unchar cmd = sp->cmnd[0];
1da177e4
LT
2156
2157 /* These cases are for spinning up a disk and
2158 * waiting for that spinup to complete.
2159 */
64821324 2160 if(cmd == START_STOP)
1da177e4
LT
2161 return 0;
2162
64821324 2163 if(cmd == TEST_UNIT_READY)
1da177e4
LT
2164 return 0;
2165
2166 /* One more special case for SCSI tape drives,
2167 * this is what is used to probe the device for
2168 * completion of a rewind or tape load operation.
2169 */
64821324
CH
2170 if(sp->device->type == TYPE_TAPE && cmd == MODE_SENSE)
2171 return 0;
1da177e4
LT
2172
2173 return 1;
2174}
2175
2176/* Either a command is completing or a target is dropping off the bus
2177 * to continue the command in the background so we can do other work.
2178 */
2179static int esp_do_freebus(struct NCR_ESP *esp, struct ESP_regs *eregs)
2180{
2181 Scsi_Cmnd *SCptr = esp->current_SC;
2182 int rval;
2183
2184 rval = skipahead2(esp, eregs, SCptr, in_status, in_msgindone, in_freeing);
2185 if(rval)
2186 return rval;
2187
2188 if(esp->ireg != ESP_INTR_DC) {
2189 ESPLOG(("esp%d: Target will not disconnect\n", esp->esp_id));
2190 return do_reset_bus; /* target will not drop BSY... */
2191 }
2192 esp->msgout_len = 0;
2193 esp->prevmsgout = NOP;
2194 if(esp->prevmsgin == COMMAND_COMPLETE) {
2195 struct esp_device *esp_dev = SCptr->device->hostdata;
2196 /* Normal end of nexus. */
2197 if(esp->disconnected_SC)
2198 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2199
2200 if(SCptr->SCp.Status != GOOD &&
2201 SCptr->SCp.Status != CONDITION_GOOD &&
422c0d61 2202 ((1<<scmd_id(SCptr)) & esp->targets_present) &&
1da177e4
LT
2203 esp_dev->sync && esp_dev->sync_max_offset) {
2204 /* SCSI standard says that the synchronous capabilities
2205 * should be renegotiated at this point. Most likely
2206 * we are about to request sense from this target
2207 * in which case we want to avoid using sync
2208 * transfers until we are sure of the current target
2209 * state.
2210 */
2211 ESPMISC(("esp: Status <%d> for target %d lun %d\n",
2212 SCptr->SCp.Status, SCptr->device->id, SCptr->device->lun));
2213
2214 /* But don't do this when spinning up a disk at
2215 * boot time while we poll for completion as it
2216 * fills up the console with messages. Also, tapes
2217 * can report not ready many times right after
2218 * loading up a tape.
2219 */
2220 if(esp_should_clear_sync(SCptr) != 0)
2221 esp_dev->sync = 0;
2222 }
2223 ESPDISC(("F<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
2224 esp_done(esp, ((SCptr->SCp.Status & 0xff) |
2225 ((SCptr->SCp.Message & 0xff)<<8) |
2226 (DID_OK << 16)));
2227 } else if(esp->prevmsgin == DISCONNECT) {
2228 /* Normal disconnect. */
2229 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2230 ESPDISC(("D<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
2231 append_SC(&esp->disconnected_SC, SCptr);
2232 esp->current_SC = NULL;
2233 if(esp->issue_SC)
2234 esp_exec_cmd(esp);
2235 } else {
2236 /* Driver bug, we do not expect a disconnect here
2237 * and should not have advanced the state engine
2238 * to in_freeing.
2239 */
2240 ESPLOG(("esp%d: last msg not disc and not cmd cmplt.\n",
2241 esp->esp_id));
2242 return do_reset_bus;
2243 }
2244 return do_intr_end;
2245}
2246
2247/* When a reselect occurs, and we cannot find the command to
2248 * reconnect to in our queues, we do this.
2249 */
2250static int esp_bad_reconnect(struct NCR_ESP *esp)
2251{
2252 Scsi_Cmnd *sp;
2253
2254 ESPLOG(("esp%d: Eieeee, reconnecting unknown command!\n",
2255 esp->esp_id));
2256 ESPLOG(("QUEUE DUMP\n"));
2257 sp = esp->issue_SC;
2258 ESPLOG(("esp%d: issue_SC[", esp->esp_id));
2259 while(sp) {
2260 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2261 sp = (Scsi_Cmnd *) sp->host_scribble;
2262 }
2263 ESPLOG(("]\n"));
2264 sp = esp->current_SC;
2265 ESPLOG(("esp%d: current_SC[", esp->esp_id));
2266 while(sp) {
2267 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2268 sp = (Scsi_Cmnd *) sp->host_scribble;
2269 }
2270 ESPLOG(("]\n"));
2271 sp = esp->disconnected_SC;
2272 ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
2273 while(sp) {
2274 ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
2275 sp = (Scsi_Cmnd *) sp->host_scribble;
2276 }
2277 ESPLOG(("]\n"));
2278 return do_reset_bus;
2279}
2280
2281/* Do the needy when a target tries to reconnect to us. */
2282static int esp_do_reconnect(struct NCR_ESP *esp,
2283 struct ESP_regs *eregs)
2284{
2285 int lun, target;
2286 Scsi_Cmnd *SCptr;
2287
2288 /* Check for all bogus conditions first. */
2289 target = reconnect_target(esp, eregs);
2290 if(target < 0) {
2291 ESPDISC(("bad bus bits\n"));
2292 return do_reset_bus;
2293 }
2294 lun = reconnect_lun(esp, eregs);
2295 if(lun < 0) {
2296 ESPDISC(("target=%2x, bad identify msg\n", target));
2297 return do_reset_bus;
2298 }
2299
2300 /* Things look ok... */
2301 ESPDISC(("R<%02x,%02x>", target, lun));
2302
2303 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2304 if(esp100_reconnect_hwbug(esp, eregs))
2305 return do_reset_bus;
2306 esp_cmd(esp, eregs, ESP_CMD_NULL);
2307
2308 SCptr = remove_SC(&esp->disconnected_SC, (unchar) target, (unchar) lun);
2309 if(!SCptr)
2310 return esp_bad_reconnect(esp);
2311
2312 esp_connect(esp, eregs, SCptr);
2313 esp_cmd(esp, eregs, ESP_CMD_MOK);
2314
2315 /* Reconnect implies a restore pointers operation. */
2316 esp_restore_pointers(esp, SCptr);
2317
2318 esp->snip = 0;
2319 esp_advance_phase(SCptr, in_the_dark);
2320 return do_intr_end;
2321}
2322
2323/* End of NEXUS (hopefully), pick up status + message byte then leave if
2324 * all goes well.
2325 */
2326static int esp_do_status(struct NCR_ESP *esp, struct ESP_regs *eregs)
2327{
2328 Scsi_Cmnd *SCptr = esp->current_SC;
2329 int intr, rval;
2330
2331 rval = skipahead1(esp, eregs, SCptr, in_the_dark, in_status);
2332 if(rval)
2333 return rval;
2334
2335 intr = esp->ireg;
2336 ESPSTAT(("esp_do_status: "));
2337 if(intr != ESP_INTR_DC) {
2338 int message_out = 0; /* for parity problems */
2339
2340 /* Ack the message. */
2341 ESPSTAT(("ack msg, "));
2342 esp_cmd(esp, eregs, ESP_CMD_MOK);
2343
2344 if(esp->dma_poll)
2345 esp->dma_poll(esp, (unsigned char *) esp->esp_command);
2346
2347 ESPSTAT(("got something, "));
2348 /* ESP chimes in with one of
2349 *
2350 * 1) function done interrupt:
2351 * both status and message in bytes
2352 * are available
2353 *
2354 * 2) bus service interrupt:
2355 * only status byte was acquired
2356 *
2357 * 3) Anything else:
2358 * can't happen, but we test for it
2359 * anyways
2360 *
2361 * ALSO: If bad parity was detected on either
2362 * the status _or_ the message byte then
2363 * the ESP has asserted ATN on the bus
2364 * and we must therefore wait for the
2365 * next phase change.
2366 */
2367 if(intr & ESP_INTR_FDONE) {
2368 /* We got it all, hallejulia. */
2369 ESPSTAT(("got both, "));
2370 SCptr->SCp.Status = esp->esp_command[0];
2371 SCptr->SCp.Message = esp->esp_command[1];
2372 esp->prevmsgin = SCptr->SCp.Message;
2373 esp->cur_msgin[0] = SCptr->SCp.Message;
2374 if(esp->sreg & ESP_STAT_PERR) {
2375 /* There was bad parity for the
2376 * message byte, the status byte
2377 * was ok.
2378 */
2379 message_out = MSG_PARITY_ERROR;
2380 }
2381 } else if(intr == ESP_INTR_BSERV) {
2382 /* Only got status byte. */
2383 ESPLOG(("esp%d: got status only, ", esp->esp_id));
2384 if(!(esp->sreg & ESP_STAT_PERR)) {
2385 SCptr->SCp.Status = esp->esp_command[0];
2386 SCptr->SCp.Message = 0xff;
2387 } else {
2388 /* The status byte had bad parity.
2389 * we leave the scsi_pointer Status
2390 * field alone as we set it to a default
2391 * of CHECK_CONDITION in esp_queue.
2392 */
2393 message_out = INITIATOR_ERROR;
2394 }
2395 } else {
2396 /* This shouldn't happen ever. */
2397 ESPSTAT(("got bolixed\n"));
2398 esp_advance_phase(SCptr, in_the_dark);
2399 return esp_do_phase_determine(esp, eregs);
2400 }
2401
2402 if(!message_out) {
2403 ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
2404 SCptr->SCp.Message));
2405 if(SCptr->SCp.Message == COMMAND_COMPLETE) {
2406 ESPSTAT(("and was COMMAND_COMPLETE\n"));
2407 esp_advance_phase(SCptr, in_freeing);
2408 return esp_do_freebus(esp, eregs);
2409 } else {
2410 ESPLOG(("esp%d: and _not_ COMMAND_COMPLETE\n",
2411 esp->esp_id));
2412 esp->msgin_len = esp->msgin_ctr = 1;
2413 esp_advance_phase(SCptr, in_msgindone);
2414 return esp_do_msgindone(esp, eregs);
2415 }
2416 } else {
2417 /* With luck we'll be able to let the target
2418 * know that bad parity happened, it will know
2419 * which byte caused the problems and send it
2420 * again. For the case where the status byte
2421 * receives bad parity, I do not believe most
2422 * targets recover very well. We'll see.
2423 */
2424 ESPLOG(("esp%d: bad parity somewhere mout=%2x\n",
2425 esp->esp_id, message_out));
2426 esp->cur_msgout[0] = message_out;
2427 esp->msgout_len = esp->msgout_ctr = 1;
2428 esp_advance_phase(SCptr, in_the_dark);
2429 return esp_do_phase_determine(esp, eregs);
2430 }
2431 } else {
2432 /* If we disconnect now, all hell breaks loose. */
2433 ESPLOG(("esp%d: whoops, disconnect\n", esp->esp_id));
2434 esp_advance_phase(SCptr, in_the_dark);
2435 return esp_do_phase_determine(esp, eregs);
2436 }
2437}
2438
2439static int esp_enter_status(struct NCR_ESP *esp,
2440 struct ESP_regs *eregs)
2441{
2442 unchar thecmd = ESP_CMD_ICCSEQ;
2443
2444 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
2445
2446 if(esp->do_pio_cmds) {
2447 esp_advance_phase(esp->current_SC, in_status);
2448 esp_cmd(esp, eregs, thecmd);
2449 while(!(esp_read(esp->eregs->esp_status) & ESP_STAT_INTR));
2450 esp->esp_command[0] = esp_read(eregs->esp_fdata);
2451 while(!(esp_read(esp->eregs->esp_status) & ESP_STAT_INTR));
2452 esp->esp_command[1] = esp_read(eregs->esp_fdata);
2453 } else {
2454 esp->esp_command[0] = esp->esp_command[1] = 0xff;
2455 esp_write(eregs->esp_tclow, 2);
2456 esp_write(eregs->esp_tcmed, 0);
2457 esp->dma_init_read(esp, esp->esp_command_dvma, 2);
2458 thecmd |= ESP_CMD_DMA;
2459 esp_cmd(esp, eregs, thecmd);
2460 esp_advance_phase(esp->current_SC, in_status);
2461 }
2462
2463 return esp_do_status(esp, eregs);
2464}
2465
2466static int esp_disconnect_amidst_phases(struct NCR_ESP *esp,
2467 struct ESP_regs *eregs)
2468{
2469 Scsi_Cmnd *sp = esp->current_SC;
2470 struct esp_device *esp_dev = sp->device->hostdata;
2471
2472 /* This means real problems if we see this
2473 * here. Unless we were actually trying
2474 * to force the device to abort/reset.
2475 */
2476 ESPLOG(("esp%d: Disconnect amidst phases, ", esp->esp_id));
2477 ESPLOG(("pphase<%s> cphase<%s>, ",
2478 phase_string(sp->SCp.phase),
2479 phase_string(sp->SCp.sent_command)));
2480
2481 if(esp->disconnected_SC)
2482 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2483
2484 switch(esp->cur_msgout[0]) {
2485 default:
2486 /* We didn't expect this to happen at all. */
2487 ESPLOG(("device is bolixed\n"));
2488 esp_advance_phase(sp, in_tgterror);
2489 esp_done(esp, (DID_ERROR << 16));
2490 break;
2491
2492 case BUS_DEVICE_RESET:
2493 ESPLOG(("device reset successful\n"));
2494 esp_dev->sync_max_offset = 0;
2495 esp_dev->sync_min_period = 0;
2496 esp_dev->sync = 0;
2497 esp_advance_phase(sp, in_resetdev);
2498 esp_done(esp, (DID_RESET << 16));
2499 break;
2500
2501 case ABORT:
2502 ESPLOG(("device abort successful\n"));
2503 esp_advance_phase(sp, in_abortone);
2504 esp_done(esp, (DID_ABORT << 16));
2505 break;
2506
2507 };
2508 return do_intr_end;
2509}
2510
2511static int esp_enter_msgout(struct NCR_ESP *esp,
2512 struct ESP_regs *eregs)
2513{
2514 esp_advance_phase(esp->current_SC, in_msgout);
2515 return esp_do_msgout(esp, eregs);
2516}
2517
2518static int esp_enter_msgin(struct NCR_ESP *esp,
2519 struct ESP_regs *eregs)
2520{
2521 esp_advance_phase(esp->current_SC, in_msgin);
2522 return esp_do_msgin(esp, eregs);
2523}
2524
2525static int esp_enter_cmd(struct NCR_ESP *esp,
2526 struct ESP_regs *eregs)
2527{
2528 esp_advance_phase(esp->current_SC, in_cmdbegin);
2529 return esp_do_cmdbegin(esp, eregs);
2530}
2531
2532static int esp_enter_badphase(struct NCR_ESP *esp,
2533 struct ESP_regs *eregs)
2534{
2535 ESPLOG(("esp%d: Bizarre bus phase %2x.\n", esp->esp_id,
2536 esp->sreg & ESP_STAT_PMASK));
2537 return do_reset_bus;
2538}
2539
2540typedef int (*espfunc_t)(struct NCR_ESP *,
2541 struct ESP_regs *);
2542
2543static espfunc_t phase_vector[] = {
2544 esp_do_data, /* ESP_DOP */
2545 esp_do_data, /* ESP_DIP */
2546 esp_enter_cmd, /* ESP_CMDP */
2547 esp_enter_status, /* ESP_STATP */
2548 esp_enter_badphase, /* ESP_STAT_PMSG */
2549 esp_enter_badphase, /* ESP_STAT_PMSG | ESP_STAT_PIO */
2550 esp_enter_msgout, /* ESP_MOP */
2551 esp_enter_msgin, /* ESP_MIP */
2552};
2553
2554/* The target has control of the bus and we have to see where it has
2555 * taken us.
2556 */
2557static int esp_do_phase_determine(struct NCR_ESP *esp,
2558 struct ESP_regs *eregs)
2559{
2560 if ((esp->ireg & ESP_INTR_DC) != 0)
2561 return esp_disconnect_amidst_phases(esp, eregs);
2562 return phase_vector[esp->sreg & ESP_STAT_PMASK](esp, eregs);
2563}
2564
2565/* First interrupt after exec'ing a cmd comes here. */
2566static int esp_select_complete(struct NCR_ESP *esp, struct ESP_regs *eregs)
2567{
2568 Scsi_Cmnd *SCptr = esp->current_SC;
2569 struct esp_device *esp_dev = SCptr->device->hostdata;
2570 int cmd_bytes_sent, fcnt;
2571
2572 fcnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
2573 cmd_bytes_sent = esp->dma_bytes_sent(esp, fcnt);
2574 if(esp->dma_invalidate)
2575 esp->dma_invalidate(esp);
2576
2577 /* Let's check to see if a reselect happened
2578 * while we we're trying to select. This must
2579 * be checked first.
2580 */
2581 if(esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
2582 esp_reconnect(esp, SCptr);
2583 return esp_do_reconnect(esp, eregs);
2584 }
2585
2586 /* Looks like things worked, we should see a bus service &
2587 * a function complete interrupt at this point. Note we
2588 * are doing a direct comparison because we don't want to
2589 * be fooled into thinking selection was successful if
2590 * ESP_INTR_DC is set, see below.
2591 */
2592 if(esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
2593 /* target speaks... */
422c0d61 2594 esp->targets_present |= (1<<scmd_id(SCptr));
1da177e4
LT
2595
2596 /* What if the target ignores the sdtr? */
2597 if(esp->snip)
2598 esp_dev->sync = 1;
2599
2600 /* See how far, if at all, we got in getting
2601 * the information out to the target.
2602 */
2603 switch(esp->seqreg) {
2604 default:
2605
2606 case ESP_STEP_ASEL:
2607 /* Arbitration won, target selected, but
2608 * we are in some phase which is not command
2609 * phase nor is it message out phase.
2610 *
2611 * XXX We've confused the target, obviously.
2612 * XXX So clear it's state, but we also end
2613 * XXX up clearing everyone elses. That isn't
2614 * XXX so nice. I'd like to just reset this
2615 * XXX target, but if I cannot even get it's
2616 * XXX attention and finish selection to talk
2617 * XXX to it, there is not much more I can do.
2618 * XXX If we have a loaded bus we're going to
2619 * XXX spend the next second or so renegotiating
2620 * XXX for synchronous transfers.
2621 */
2622 ESPLOG(("esp%d: STEP_ASEL for tgt %d\n",
2623 esp->esp_id, SCptr->device->id));
2624
2625 case ESP_STEP_SID:
2626 /* Arbitration won, target selected, went
2627 * to message out phase, sent one message
2628 * byte, then we stopped. ATN is asserted
2629 * on the SCSI bus and the target is still
2630 * there hanging on. This is a legal
2631 * sequence step if we gave the ESP a select
2632 * and stop command.
2633 *
2634 * XXX See above, I could set the borken flag
2635 * XXX in the device struct and retry the
2636 * XXX command. But would that help for
2637 * XXX tagged capable targets?
2638 */
2639
2640 case ESP_STEP_NCMD:
2641 /* Arbitration won, target selected, maybe
2642 * sent the one message byte in message out
2643 * phase, but we did not go to command phase
2644 * in the end. Actually, we could have sent
2645 * only some of the message bytes if we tried
2646 * to send out the entire identify and tag
2647 * message using ESP_CMD_SA3.
2648 */
2649 cmd_bytes_sent = 0;
2650 break;
2651
2652 case ESP_STEP_PPC:
2653 /* No, not the powerPC pinhead. Arbitration
2654 * won, all message bytes sent if we went to
2655 * message out phase, went to command phase
2656 * but only part of the command was sent.
2657 *
2658 * XXX I've seen this, but usually in conjunction
2659 * XXX with a gross error which appears to have
2660 * XXX occurred between the time I told the
2661 * XXX ESP to arbitrate and when I got the
2662 * XXX interrupt. Could I have misloaded the
2663 * XXX command bytes into the fifo? Actually,
2664 * XXX I most likely missed a phase, and therefore
2665 * XXX went into never never land and didn't even
2666 * XXX know it. That was the old driver though.
2667 * XXX What is even more peculiar is that the ESP
2668 * XXX showed the proper function complete and
2669 * XXX bus service bits in the interrupt register.
2670 */
2671
2672 case ESP_STEP_FINI4:
2673 case ESP_STEP_FINI5:
2674 case ESP_STEP_FINI6:
2675 case ESP_STEP_FINI7:
2676 /* Account for the identify message */
2677 if(SCptr->SCp.phase == in_slct_norm)
2678 cmd_bytes_sent -= 1;
2679 };
2680 esp_cmd(esp, eregs, ESP_CMD_NULL);
2681
2682 /* Be careful, we could really get fucked during synchronous
2683 * data transfers if we try to flush the fifo now.
2684 */
2685 if(!fcnt && /* Fifo is empty and... */
2686 /* either we are not doing synchronous transfers or... */
2687 (!esp_dev->sync_max_offset ||
2688 /* We are not going into data in phase. */
2689 ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
2690 esp_cmd(esp, eregs, ESP_CMD_FLUSH); /* flush is safe */
2691
2692 /* See how far we got if this is not a slow command. */
2693 if(!esp->esp_slowcmd) {
2694 if(cmd_bytes_sent < 0)
2695 cmd_bytes_sent = 0;
2696 if(cmd_bytes_sent != SCptr->cmd_len) {
2697 /* Crapola, mark it as a slowcmd
2698 * so that we have some chance of
2699 * keeping the command alive with
2700 * good luck.
2701 *
2702 * XXX Actually, if we didn't send it all
2703 * XXX this means either we didn't set things
2704 * XXX up properly (driver bug) or the target
2705 * XXX or the ESP detected parity on one of
2706 * XXX the command bytes. This makes much
2707 * XXX more sense, and therefore this code
2708 * XXX should be changed to send out a
2709 * XXX parity error message or if the status
2710 * XXX register shows no parity error then
2711 * XXX just expect the target to bring the
2712 * XXX bus into message in phase so that it
2713 * XXX can send us the parity error message.
2714 * XXX SCSI sucks...
2715 */
2716 esp->esp_slowcmd = 1;
2717 esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
2718 esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
2719 }
2720 }
2721
2722 /* Now figure out where we went. */
2723 esp_advance_phase(SCptr, in_the_dark);
2724 return esp_do_phase_determine(esp, eregs);
2725 }
2726
2727 /* Did the target even make it? */
2728 if(esp->ireg == ESP_INTR_DC) {
2729 /* wheee... nobody there or they didn't like
2730 * what we told it to do, clean up.
2731 */
2732
2733 /* If anyone is off the bus, but working on
2734 * a command in the background for us, tell
2735 * the ESP to listen for them.
2736 */
2737 if(esp->disconnected_SC)
2738 esp_cmd(esp, eregs, ESP_CMD_ESEL);
2739
2740 if(((1<<SCptr->device->id) & esp->targets_present) &&
2741 esp->seqreg && esp->cur_msgout[0] == EXTENDED_MESSAGE &&
2742 (SCptr->SCp.phase == in_slct_msg ||
2743 SCptr->SCp.phase == in_slct_stop)) {
2744 /* shit */
2745 esp->snip = 0;
2746 ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
2747 "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
2748 esp_dev->sync_max_offset = 0;
2749 esp_dev->sync_min_period = 0;
2750 esp_dev->sync = 1; /* so we don't negotiate again */
2751
2752 /* Run the command again, this time though we
2753 * won't try to negotiate for synchronous transfers.
2754 *
2755 * XXX I'd like to do something like send an
2756 * XXX INITIATOR_ERROR or ABORT message to the
2757 * XXX target to tell it, "Sorry I confused you,
2758 * XXX please come back and I will be nicer next
2759 * XXX time". But that requires having the target
2760 * XXX on the bus, and it has dropped BSY on us.
2761 */
2762 esp->current_SC = NULL;
2763 esp_advance_phase(SCptr, not_issued);
2764 prepend_SC(&esp->issue_SC, SCptr);
2765 esp_exec_cmd(esp);
2766 return do_intr_end;
2767 }
2768
2769 /* Ok, this is normal, this is what we see during boot
2770 * or whenever when we are scanning the bus for targets.
2771 * But first make sure that is really what is happening.
2772 */
2773 if(((1<<SCptr->device->id) & esp->targets_present)) {
2774 ESPLOG(("esp%d: Warning, live target %d not responding to "
2775 "selection.\n", esp->esp_id, SCptr->device->id));
2776
2777 /* This _CAN_ happen. The SCSI standard states that
2778 * the target is to _not_ respond to selection if
2779 * _it_ detects bad parity on the bus for any reason.
2780 * Therefore, we assume that if we've talked successfully
2781 * to this target before, bad parity is the problem.
2782 */
2783 esp_done(esp, (DID_PARITY << 16));
2784 } else {
2785 /* Else, there really isn't anyone there. */
2786 ESPMISC(("esp: selection failure, maybe nobody there?\n"));
2787 ESPMISC(("esp: target %d lun %d\n",
2788 SCptr->device->id, SCptr->device->lun));
2789 esp_done(esp, (DID_BAD_TARGET << 16));
2790 }
2791 return do_intr_end;
2792 }
2793
2794
2795 ESPLOG(("esp%d: Selection failure.\n", esp->esp_id));
2796 printk("esp%d: Currently -- ", esp->esp_id);
2797 esp_print_ireg(esp->ireg);
2798 printk(" ");
2799 esp_print_statreg(esp->sreg);
2800 printk(" ");
2801 esp_print_seqreg(esp->seqreg);
2802 printk("\n");
2803 printk("esp%d: New -- ", esp->esp_id);
2804 esp->sreg = esp_read(eregs->esp_status);
2805 esp->seqreg = esp_read(eregs->esp_sstep);
2806 esp->ireg = esp_read(eregs->esp_intrpt);
2807 esp_print_ireg(esp->ireg);
2808 printk(" ");
2809 esp_print_statreg(esp->sreg);
2810 printk(" ");
2811 esp_print_seqreg(esp->seqreg);
2812 printk("\n");
2813 ESPLOG(("esp%d: resetting bus\n", esp->esp_id));
2814 return do_reset_bus; /* ugh... */
2815}
2816
2817/* Continue reading bytes for msgin phase. */
2818static int esp_do_msgincont(struct NCR_ESP *esp, struct ESP_regs *eregs)
2819{
2820 if(esp->ireg & ESP_INTR_BSERV) {
2821 /* in the right phase too? */
2822 if((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
2823 /* phew... */
2824 esp_cmd(esp, eregs, ESP_CMD_TI);
2825 esp_advance_phase(esp->current_SC, in_msgindone);
2826 return do_intr_end;
2827 }
2828
2829 /* We changed phase but ESP shows bus service,
2830 * in this case it is most likely that we, the
2831 * hacker who has been up for 20hrs straight
2832 * staring at the screen, drowned in coffee
2833 * smelling like retched cigarette ashes
2834 * have miscoded something..... so, try to
2835 * recover as best we can.
2836 */
2837 ESPLOG(("esp%d: message in mis-carriage.\n", esp->esp_id));
2838 }
2839 esp_advance_phase(esp->current_SC, in_the_dark);
2840 return do_phase_determine;
2841}
2842
2843static int check_singlebyte_msg(struct NCR_ESP *esp,
2844 struct ESP_regs *eregs)
2845{
2846 esp->prevmsgin = esp->cur_msgin[0];
2847 if(esp->cur_msgin[0] & 0x80) {
2848 /* wheee... */
2849 ESPLOG(("esp%d: target sends identify amidst phases\n",
2850 esp->esp_id));
2851 esp_advance_phase(esp->current_SC, in_the_dark);
2852 return 0;
2853 } else if(((esp->cur_msgin[0] & 0xf0) == 0x20) ||
2854 (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
2855 esp->msgin_len = 2;
2856 esp_advance_phase(esp->current_SC, in_msgincont);
2857 return 0;
2858 }
2859 esp_advance_phase(esp->current_SC, in_the_dark);
2860 switch(esp->cur_msgin[0]) {
2861 default:
2862 /* We don't want to hear about it. */
2863 ESPLOG(("esp%d: msg %02x which we don't know about\n", esp->esp_id,
2864 esp->cur_msgin[0]));
2865 return MESSAGE_REJECT;
2866
2867 case NOP:
2868 ESPLOG(("esp%d: target %d sends a nop\n", esp->esp_id,
2869 esp->current_SC->device->id));
2870 return 0;
2871
2872 case RESTORE_POINTERS:
2873 /* In this case we might also have to backup the
2874 * "slow command" pointer. It is rare to get such
2875 * a save/restore pointer sequence so early in the
2876 * bus transition sequences, but cover it.
2877 */
2878 if(esp->esp_slowcmd) {
2879 esp->esp_scmdleft = esp->current_SC->cmd_len;
2880 esp->esp_scmdp = &esp->current_SC->cmnd[0];
2881 }
2882 esp_restore_pointers(esp, esp->current_SC);
2883 return 0;
2884
2885 case SAVE_POINTERS:
2886 esp_save_pointers(esp, esp->current_SC);
2887 return 0;
2888
2889 case COMMAND_COMPLETE:
2890 case DISCONNECT:
2891 /* Freeing the bus, let it go. */
2892 esp->current_SC->SCp.phase = in_freeing;
2893 return 0;
2894
2895 case MESSAGE_REJECT:
2896 ESPMISC(("msg reject, "));
2897 if(esp->prevmsgout == EXTENDED_MESSAGE) {
2898 struct esp_device *esp_dev = esp->current_SC->device->hostdata;
2899
2900 /* Doesn't look like this target can
2901 * do synchronous or WIDE transfers.
2902 */
2903 ESPSDTR(("got reject, was trying nego, clearing sync/WIDE\n"));
2904 esp_dev->sync = 1;
2905 esp_dev->wide = 1;
2906 esp_dev->sync_min_period = 0;
2907 esp_dev->sync_max_offset = 0;
2908 return 0;
2909 } else {
2910 ESPMISC(("not sync nego, sending ABORT\n"));
2911 return ABORT;
2912 }
2913 };
2914}
2915
2916/* Target negotiates for synchronous transfers before we do, this
2917 * is legal although very strange. What is even funnier is that
2918 * the SCSI2 standard specifically recommends against targets doing
2919 * this because so many initiators cannot cope with this occurring.
2920 */
2921static int target_with_ants_in_pants(struct NCR_ESP *esp,
2922 Scsi_Cmnd *SCptr,
2923 struct esp_device *esp_dev)
2924{
2925 if(esp_dev->sync || SCptr->device->borken) {
2926 /* sorry, no can do */
2927 ESPSDTR(("forcing to async, "));
2928 build_sync_nego_msg(esp, 0, 0);
2929 esp_dev->sync = 1;
2930 esp->snip = 1;
2931 ESPLOG(("esp%d: hoping for msgout\n", esp->esp_id));
2932 esp_advance_phase(SCptr, in_the_dark);
2933 return EXTENDED_MESSAGE;
2934 }
2935
2936 /* Ok, we'll check them out... */
2937 return 0;
2938}
2939
2940static void sync_report(struct NCR_ESP *esp)
2941{
2942 int msg3, msg4;
2943 char *type;
2944
2945 msg3 = esp->cur_msgin[3];
2946 msg4 = esp->cur_msgin[4];
2947 if(msg4) {
2948 int hz = 1000000000 / (msg3 * 4);
2949 int integer = hz / 1000000;
2950 int fraction = (hz - (integer * 1000000)) / 10000;
2951 if((msg3 * 4) < 200) {
2952 type = "FAST";
2953 } else {
2954 type = "synchronous";
2955 }
2956
2957 /* Do not transform this back into one big printk
2958 * again, it triggers a bug in our sparc64-gcc272
2959 * sibling call optimization. -DaveM
2960 */
2961 ESPLOG((KERN_INFO "esp%d: target %d ",
2962 esp->esp_id, esp->current_SC->device->id));
2963 ESPLOG(("[period %dns offset %d %d.%02dMHz ",
2964 (int) msg3 * 4, (int) msg4,
2965 integer, fraction));
2966 ESPLOG(("%s SCSI%s]\n", type,
2967 (((msg3 * 4) < 200) ? "-II" : "")));
2968 } else {
2969 ESPLOG((KERN_INFO "esp%d: target %d asynchronous\n",
2970 esp->esp_id, esp->current_SC->device->id));
2971 }
2972}
2973
2974static int check_multibyte_msg(struct NCR_ESP *esp,
2975 struct ESP_regs *eregs)
2976{
2977 Scsi_Cmnd *SCptr = esp->current_SC;
2978 struct esp_device *esp_dev = SCptr->device->hostdata;
2979 unchar regval = 0;
2980 int message_out = 0;
2981
2982 ESPSDTR(("chk multibyte msg: "));
2983 if(esp->cur_msgin[2] == EXTENDED_SDTR) {
2984 int period = esp->cur_msgin[3];
2985 int offset = esp->cur_msgin[4];
2986
2987 ESPSDTR(("is sync nego response, "));
2988 if(!esp->snip) {
2989 int rval;
2990
2991 /* Target negotiates first! */
2992 ESPSDTR(("target jumps the gun, "));
2993 message_out = EXTENDED_MESSAGE; /* we must respond */
2994 rval = target_with_ants_in_pants(esp, SCptr, esp_dev);
2995 if(rval)
2996 return rval;
2997 }
2998
2999 ESPSDTR(("examining sdtr, "));
3000
3001 /* Offset cannot be larger than ESP fifo size. */
3002 if(offset > 15) {
3003 ESPSDTR(("offset too big %2x, ", offset));
3004 offset = 15;
3005 ESPSDTR(("sending back new offset\n"));
3006 build_sync_nego_msg(esp, period, offset);
3007 return EXTENDED_MESSAGE;
3008 }
3009
3010 if(offset && period > esp->max_period) {
3011 /* Yeee, async for this slow device. */
3012 ESPSDTR(("period too long %2x, ", period));
3013 build_sync_nego_msg(esp, 0, 0);
3014 ESPSDTR(("hoping for msgout\n"));
3015 esp_advance_phase(esp->current_SC, in_the_dark);
3016 return EXTENDED_MESSAGE;
3017 } else if (offset && period < esp->min_period) {
3018 ESPSDTR(("period too short %2x, ", period));
3019 period = esp->min_period;
3020 if(esp->erev > esp236)
3021 regval = 4;
3022 else
3023 regval = 5;
3024 } else if(offset) {
3025 int tmp;
3026
3027 ESPSDTR(("period is ok, "));
3028 tmp = esp->ccycle / 1000;
3029 regval = (((period << 2) + tmp - 1) / tmp);
3030 if(regval && (esp->erev > esp236)) {
3031 if(period >= 50)
3032 regval--;
3033 }
3034 }
3035
3036 if(offset) {
3037 unchar bit;
3038
3039 esp_dev->sync_min_period = (regval & 0x1f);
3040 esp_dev->sync_max_offset = (offset | esp->radelay);
3041 if(esp->erev > esp236) {
3042 if(esp->erev == fas100a)
3043 bit = ESP_CONFIG3_FAST;
3044 else
3045 bit = ESP_CONFIG3_FSCSI;
3046 if(period < 50)
3047 esp->config3[SCptr->device->id] |= bit;
3048 else
3049 esp->config3[SCptr->device->id] &= ~bit;
3050 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3051 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
3052 }
3053 esp->prev_soff = esp_dev->sync_min_period;
3054 esp_write(eregs->esp_soff, esp->prev_soff);
3055 esp->prev_stp = esp_dev->sync_max_offset;
3056 esp_write(eregs->esp_stp, esp->prev_stp);
3057
3058 ESPSDTR(("soff=%2x stp=%2x cfg3=%2x\n",
3059 esp_dev->sync_max_offset,
3060 esp_dev->sync_min_period,
422c0d61 3061 esp->config3[scmd_id(SCptr)]));
1da177e4
LT
3062
3063 esp->snip = 0;
3064 } else if(esp_dev->sync_max_offset) {
3065 unchar bit;
3066
3067 /* back to async mode */
3068 ESPSDTR(("unaccaptable sync nego, forcing async\n"));
3069 esp_dev->sync_max_offset = 0;
3070 esp_dev->sync_min_period = 0;
3071 esp->prev_soff = 0;
3072 esp_write(eregs->esp_soff, 0);
3073 esp->prev_stp = 0;
3074 esp_write(eregs->esp_stp, 0);
3075 if(esp->erev > esp236) {
3076 if(esp->erev == fas100a)
3077 bit = ESP_CONFIG3_FAST;
3078 else
3079 bit = ESP_CONFIG3_FSCSI;
3080 esp->config3[SCptr->device->id] &= ~bit;
3081 esp->prev_cfg3 = esp->config3[SCptr->device->id];
3082 esp_write(eregs->esp_cfg3, esp->prev_cfg3);
3083 }
3084 }
3085
3086 sync_report(esp);
3087
3088 ESPSDTR(("chk multibyte msg: sync is known, "));
3089 esp_dev->sync = 1;
3090
3091 if(message_out) {
3092 ESPLOG(("esp%d: sending sdtr back, hoping for msgout\n",
3093 esp->esp_id));
3094 build_sync_nego_msg(esp, period, offset);
3095 esp_advance_phase(SCptr, in_the_dark);
3096 return EXTENDED_MESSAGE;
3097 }
3098
3099 ESPSDTR(("returning zero\n"));
3100 esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
3101 return 0;
3102 } else if(esp->cur_msgin[2] == EXTENDED_WDTR) {
3103 ESPLOG(("esp%d: AIEEE wide msg received\n", esp->esp_id));
3104 message_out = MESSAGE_REJECT;
3105 } else if(esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
3106 ESPLOG(("esp%d: rejecting modify data ptr msg\n", esp->esp_id));
3107 message_out = MESSAGE_REJECT;
3108 }
3109 esp_advance_phase(SCptr, in_the_dark);
3110 return message_out;
3111}
3112
3113static int esp_do_msgindone(struct NCR_ESP *esp, struct ESP_regs *eregs)
3114{
3115 Scsi_Cmnd *SCptr = esp->current_SC;
3116 int message_out = 0, it = 0, rval;
3117
3118 rval = skipahead1(esp, eregs, SCptr, in_msgin, in_msgindone);
3119 if(rval)
3120 return rval;
3121 if(SCptr->SCp.sent_command != in_status) {
3122 if(!(esp->ireg & ESP_INTR_DC)) {
3123 if(esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
3124 message_out = MSG_PARITY_ERROR;
3125 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3126 } else if((it = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES))!=1) {
3127 /* We certainly dropped the ball somewhere. */
3128 message_out = INITIATOR_ERROR;
3129 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3130 } else if(!esp->msgin_len) {
3131 it = esp_read(eregs->esp_fdata);
3132 esp_advance_phase(SCptr, in_msgincont);
3133 } else {
3134 /* it is ok and we want it */
3135 it = esp->cur_msgin[esp->msgin_ctr] =
3136 esp_read(eregs->esp_fdata);
3137 esp->msgin_ctr++;
3138 }
3139 } else {
3140 esp_advance_phase(SCptr, in_the_dark);
3141 return do_work_bus;
3142 }
3143 } else {
3144 it = esp->cur_msgin[0];
3145 }
3146 if(!message_out && esp->msgin_len) {
3147 if(esp->msgin_ctr < esp->msgin_len) {
3148 esp_advance_phase(SCptr, in_msgincont);
3149 } else if(esp->msgin_len == 1) {
3150 message_out = check_singlebyte_msg(esp, eregs);
3151 } else if(esp->msgin_len == 2) {
3152 if(esp->cur_msgin[0] == EXTENDED_MESSAGE) {
3153 if((it+2) >= 15) {
3154 message_out = MESSAGE_REJECT;
3155 } else {
3156 esp->msgin_len = (it + 2);
3157 esp_advance_phase(SCptr, in_msgincont);
3158 }
3159 } else {
3160 message_out = MESSAGE_REJECT; /* foo on you */
3161 }
3162 } else {
3163 message_out = check_multibyte_msg(esp, eregs);
3164 }
3165 }
3166 if(message_out < 0) {
3167 return -message_out;
3168 } else if(message_out) {
3169 if(((message_out != 1) &&
3170 ((message_out < 0x20) || (message_out & 0x80))))
3171 esp->msgout_len = 1;
3172 esp->cur_msgout[0] = message_out;
3173 esp_cmd(esp, eregs, ESP_CMD_SATN);
3174 esp_advance_phase(SCptr, in_the_dark);
3175 esp->msgin_len = 0;
3176 }
3177 esp->sreg = esp_read(eregs->esp_status);
3178 esp->sreg &= ~(ESP_STAT_INTR);
3179 if((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
3180 esp_cmd(esp, eregs, ESP_CMD_MOK);
3181 if((SCptr->SCp.sent_command == in_msgindone) &&
3182 (SCptr->SCp.phase == in_freeing))
3183 return esp_do_freebus(esp, eregs);
3184 return do_intr_end;
3185}
3186
3187static int esp_do_cmdbegin(struct NCR_ESP *esp, struct ESP_regs *eregs)
3188{
3189 unsigned char tmp;
3190 Scsi_Cmnd *SCptr = esp->current_SC;
3191
3192 esp_advance_phase(SCptr, in_cmdend);
3193 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3194 tmp = *esp->esp_scmdp++;
3195 esp->esp_scmdleft--;
3196 esp_write(eregs->esp_fdata, tmp);
3197 esp_cmd(esp, eregs, ESP_CMD_TI);
3198 return do_intr_end;
3199}
3200
3201static int esp_do_cmddone(struct NCR_ESP *esp, struct ESP_regs *eregs)
3202{
3203 esp_cmd(esp, eregs, ESP_CMD_NULL);
3204 if(esp->ireg & ESP_INTR_BSERV) {
3205 esp_advance_phase(esp->current_SC, in_the_dark);
3206 return esp_do_phase_determine(esp, eregs);
3207 }
3208 ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.\n",
3209 esp->esp_id));
3210 return do_reset_bus;
3211}
3212
3213static int esp_do_msgout(struct NCR_ESP *esp, struct ESP_regs *eregs)
3214{
3215 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3216 switch(esp->msgout_len) {
3217 case 1:
3218 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3219 esp_cmd(esp, eregs, ESP_CMD_TI);
3220 break;
3221
3222 case 2:
3223 if(esp->do_pio_cmds){
3224 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3225 esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3226 esp_cmd(esp, eregs, ESP_CMD_TI);
3227 } else {
3228 esp->esp_command[0] = esp->cur_msgout[0];
3229 esp->esp_command[1] = esp->cur_msgout[1];
3230 esp->dma_setup(esp, esp->esp_command_dvma, 2, 0);
3231 esp_setcount(eregs, 2);
3232 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3233 }
3234 break;
3235
3236 case 4:
3237 esp->snip = 1;
3238 if(esp->do_pio_cmds){
3239 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3240 esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3241 esp_write(eregs->esp_fdata, esp->cur_msgout[2]);
3242 esp_write(eregs->esp_fdata, esp->cur_msgout[3]);
3243 esp_cmd(esp, eregs, ESP_CMD_TI);
3244 } else {
3245 esp->esp_command[0] = esp->cur_msgout[0];
3246 esp->esp_command[1] = esp->cur_msgout[1];
3247 esp->esp_command[2] = esp->cur_msgout[2];
3248 esp->esp_command[3] = esp->cur_msgout[3];
3249 esp->dma_setup(esp, esp->esp_command_dvma, 4, 0);
3250 esp_setcount(eregs, 4);
3251 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3252 }
3253 break;
3254
3255 case 5:
3256 esp->snip = 1;
3257 if(esp->do_pio_cmds){
3258 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3259 esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
3260 esp_write(eregs->esp_fdata, esp->cur_msgout[2]);
3261 esp_write(eregs->esp_fdata, esp->cur_msgout[3]);
3262 esp_write(eregs->esp_fdata, esp->cur_msgout[4]);
3263 esp_cmd(esp, eregs, ESP_CMD_TI);
3264 } else {
3265 esp->esp_command[0] = esp->cur_msgout[0];
3266 esp->esp_command[1] = esp->cur_msgout[1];
3267 esp->esp_command[2] = esp->cur_msgout[2];
3268 esp->esp_command[3] = esp->cur_msgout[3];
3269 esp->esp_command[4] = esp->cur_msgout[4];
3270 esp->dma_setup(esp, esp->esp_command_dvma, 5, 0);
3271 esp_setcount(eregs, 5);
3272 esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
3273 }
3274 break;
3275
3276 default:
3277 /* whoops */
3278 ESPMISC(("bogus msgout sending NOP\n"));
3279 esp->cur_msgout[0] = NOP;
3280 esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
3281 esp->msgout_len = 1;
3282 esp_cmd(esp, eregs, ESP_CMD_TI);
3283 break;
3284 }
3285 esp_advance_phase(esp->current_SC, in_msgoutdone);
3286 return do_intr_end;
3287}
3288
3289static int esp_do_msgoutdone(struct NCR_ESP *esp,
3290 struct ESP_regs *eregs)
3291{
3292 if((esp->msgout_len > 1) && esp->dma_barrier)
3293 esp->dma_barrier(esp);
3294
3295 if(!(esp->ireg & ESP_INTR_DC)) {
3296 esp_cmd(esp, eregs, ESP_CMD_NULL);
3297 switch(esp->sreg & ESP_STAT_PMASK) {
3298 case ESP_MOP:
3299 /* whoops, parity error */
3300 ESPLOG(("esp%d: still in msgout, parity error assumed\n",
3301 esp->esp_id));
3302 if(esp->msgout_len > 1)
3303 esp_cmd(esp, eregs, ESP_CMD_SATN);
3304 esp_advance_phase(esp->current_SC, in_msgout);
3305 return do_work_bus;
3306
3307 case ESP_DIP:
3308 break;
3309
3310 default:
3311 if(!fcount(esp, eregs) &&
3312 !(((struct esp_device *)esp->current_SC->device->hostdata)->sync_max_offset))
3313 esp_cmd(esp, eregs, ESP_CMD_FLUSH);
3314 break;
3315
3316 };
3317 }
3318
3319 /* If we sent out a synchronous negotiation message, update
3320 * our state.
3321 */
3322 if(esp->cur_msgout[2] == EXTENDED_MESSAGE &&
3323 esp->cur_msgout[4] == EXTENDED_SDTR) {
3324 esp->snip = 1; /* anal retentiveness... */
3325 }
3326
3327 esp->prevmsgout = esp->cur_msgout[0];
3328 esp->msgout_len = 0;
3329 esp_advance_phase(esp->current_SC, in_the_dark);
3330 return esp_do_phase_determine(esp, eregs);
3331}
3332
3333static int esp_bus_unexpected(struct NCR_ESP *esp, struct ESP_regs *eregs)
3334{
3335 ESPLOG(("esp%d: command in weird state %2x\n",
3336 esp->esp_id, esp->current_SC->SCp.phase));
3337 return do_reset_bus;
3338}
3339
3340static espfunc_t bus_vector[] = {
3341 esp_do_data_finale,
3342 esp_do_data_finale,
3343 esp_bus_unexpected,
3344 esp_do_msgin,
3345 esp_do_msgincont,
3346 esp_do_msgindone,
3347 esp_do_msgout,
3348 esp_do_msgoutdone,
3349 esp_do_cmdbegin,
3350 esp_do_cmddone,
3351 esp_do_status,
3352 esp_do_freebus,
3353 esp_do_phase_determine,
3354 esp_bus_unexpected,
3355 esp_bus_unexpected,
3356 esp_bus_unexpected,
3357};
3358
3359/* This is the second tier in our dual-level SCSI state machine. */
3360static int esp_work_bus(struct NCR_ESP *esp, struct ESP_regs *eregs)
3361{
3362 Scsi_Cmnd *SCptr = esp->current_SC;
3363 unsigned int phase;
3364
3365 ESPBUS(("esp_work_bus: "));
3366 if(!SCptr) {
3367 ESPBUS(("reconnect\n"));
3368 return esp_do_reconnect(esp, eregs);
3369 }
3370 phase = SCptr->SCp.phase;
3371 if ((phase & 0xf0) == in_phases_mask)
3372 return bus_vector[(phase & 0x0f)](esp, eregs);
3373 else if((phase & 0xf0) == in_slct_mask)
3374 return esp_select_complete(esp, eregs);
3375 else
3376 return esp_bus_unexpected(esp, eregs);
3377}
3378
3379static espfunc_t isvc_vector[] = {
3380 NULL,
3381 esp_do_phase_determine,
3382 esp_do_resetbus,
3383 esp_finish_reset,
3384 esp_work_bus
3385};
3386
3387/* Main interrupt handler for an esp adapter. */
3388void esp_handle(struct NCR_ESP *esp)
3389{
3390 struct ESP_regs *eregs;
3391 Scsi_Cmnd *SCptr;
3392 int what_next = do_intr_end;
3393 eregs = esp->eregs;
3394 SCptr = esp->current_SC;
3395
3396 if(esp->dma_irq_entry)
3397 esp->dma_irq_entry(esp);
3398
3399 /* Check for errors. */
3400 esp->sreg = esp_read(eregs->esp_status);
3401 esp->sreg &= (~ESP_STAT_INTR);
3402 esp->seqreg = (esp_read(eregs->esp_sstep) & ESP_STEP_VBITS);
3403 esp->ireg = esp_read(eregs->esp_intrpt); /* Unlatch intr and stat regs */
3404 ESPIRQ(("handle_irq: [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
3405 esp->sreg, esp->seqreg, esp->ireg));
3406 if(esp->sreg & (ESP_STAT_SPAM)) {
3407 /* Gross error, could be due to one of:
3408 *
3409 * - top of fifo overwritten, could be because
3410 * we tried to do a synchronous transfer with
3411 * an offset greater than ESP fifo size
3412 *
3413 * - top of command register overwritten
3414 *
3415 * - DMA setup to go in one direction, SCSI
3416 * bus points in the other, whoops
3417 *
3418 * - weird phase change during asynchronous
3419 * data phase while we are initiator
3420 */
3421 ESPLOG(("esp%d: Gross error sreg=%2x\n", esp->esp_id, esp->sreg));
3422
3423 /* If a command is live on the bus we cannot safely
3424 * reset the bus, so we'll just let the pieces fall
3425 * where they may. Here we are hoping that the
3426 * target will be able to cleanly go away soon
3427 * so we can safely reset things.
3428 */
3429 if(!SCptr) {
3430 ESPLOG(("esp%d: No current cmd during gross error, "
3431 "resetting bus\n", esp->esp_id));
3432 what_next = do_reset_bus;
3433 goto state_machine;
3434 }
3435 }
3436
3437 /* No current cmd is only valid at this point when there are
3438 * commands off the bus or we are trying a reset.
3439 */
3440 if(!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
3441 /* Panic is safe, since current_SC is null. */
3442 ESPLOG(("esp%d: no command in esp_handle()\n", esp->esp_id));
3443 panic("esp_handle: current_SC == penguin within interrupt!");
3444 }
3445
3446 if(esp->ireg & (ESP_INTR_IC)) {
3447 /* Illegal command fed to ESP. Outside of obvious
3448 * software bugs that could cause this, there is
3449 * a condition with ESP100 where we can confuse the
3450 * ESP into an erroneous illegal command interrupt
3451 * because it does not scrape the FIFO properly
3452 * for reselection. See esp100_reconnect_hwbug()
3453 * to see how we try very hard to avoid this.
3454 */
3455 ESPLOG(("esp%d: invalid command\n", esp->esp_id));
3456
3457 esp_dump_state(esp, eregs);
3458
3459 if(SCptr) {
3460 /* Devices with very buggy firmware can drop BSY
3461 * during a scatter list interrupt when using sync
3462 * mode transfers. We continue the transfer as
3463 * expected, the target drops the bus, the ESP
3464 * gets confused, and we get a illegal command
3465 * interrupt because the bus is in the disconnected
3466 * state now and ESP_CMD_TI is only allowed when
3467 * a nexus is alive on the bus.
3468 */
3469 ESPLOG(("esp%d: Forcing async and disabling disconnect for "
3470 "target %d\n", esp->esp_id, SCptr->device->id));
3471 SCptr->device->borken = 1; /* foo on you */
3472 }
3473
3474 what_next = do_reset_bus;
3475 } else if(!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
3476 int phase;
3477
3478 if(SCptr) {
3479 phase = SCptr->SCp.phase;
3480 if(phase & in_phases_mask) {
3481 what_next = esp_work_bus(esp, eregs);
3482 } else if(phase & in_slct_mask) {
3483 what_next = esp_select_complete(esp, eregs);
3484 } else {
3485 ESPLOG(("esp%d: interrupt for no good reason...\n",
3486 esp->esp_id));
3487 what_next = do_intr_end;
3488 }
3489 } else {
3490 ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULL\n",
3491 esp->esp_id));
3492 what_next = do_reset_bus;
3493 }
3494 } else if(esp->ireg & ESP_INTR_SR) {
3495 ESPLOG(("esp%d: SCSI bus reset interrupt\n", esp->esp_id));
3496 what_next = do_reset_complete;
3497 } else if(esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
3498 ESPLOG(("esp%d: AIEEE we have been selected by another initiator!\n",
3499 esp->esp_id));
3500 what_next = do_reset_bus;
3501 } else if(esp->ireg & ESP_INTR_RSEL) {
3502 if(!SCptr) {
3503 /* This is ok. */
3504 what_next = esp_do_reconnect(esp, eregs);
3505 } else if(SCptr->SCp.phase & in_slct_mask) {
3506 /* Only selection code knows how to clean
3507 * up properly.
3508 */
3509 ESPDISC(("Reselected during selection attempt\n"));
3510 what_next = esp_select_complete(esp, eregs);
3511 } else {
3512 ESPLOG(("esp%d: Reselected while bus is busy\n",
3513 esp->esp_id));
3514 what_next = do_reset_bus;
3515 }
3516 }
3517
3518 /* This is tier-one in our dual level SCSI state machine. */
3519state_machine:
3520 while(what_next != do_intr_end) {
3521 if (what_next >= do_phase_determine &&
3522 what_next < do_intr_end)
3523 what_next = isvc_vector[what_next](esp, eregs);
3524 else {
3525 /* state is completely lost ;-( */
3526 ESPLOG(("esp%d: interrupt engine loses state, resetting bus\n",
3527 esp->esp_id));
3528 what_next = do_reset_bus;
3529 }
3530 }
3531 if(esp->dma_irq_exit)
3532 esp->dma_irq_exit(esp);
3533}
3534
3535#ifndef CONFIG_SMP
7d12e780 3536irqreturn_t esp_intr(int irq, void *dev_id)
1da177e4
LT
3537{
3538 struct NCR_ESP *esp;
3539 unsigned long flags;
3540 int again;
3541 struct Scsi_Host *dev = dev_id;
3542
3543 /* Handle all ESP interrupts showing at this IRQ level. */
3544 spin_lock_irqsave(dev->host_lock, flags);
3545repeat:
3546 again = 0;
3547 for_each_esp(esp) {
3548#ifndef __mips__
3549 if(((esp)->irq & 0xff) == irq) {
3550#endif
3551 if(esp->dma_irq_p(esp)) {
3552 again = 1;
3553
3554 esp->dma_ints_off(esp);
3555
3556 ESPIRQ(("I%d(", esp->esp_id));
3557 esp_handle(esp);
3558 ESPIRQ((")"));
3559
3560 esp->dma_ints_on(esp);
3561 }
3562#ifndef __mips__
3563 }
3564#endif
3565 }
3566 if(again)
3567 goto repeat;
3568 spin_unlock_irqrestore(dev->host_lock, flags);
3569 return IRQ_HANDLED;
3570}
3571#else
3572/* For SMP we only service one ESP on the list list at our IRQ level! */
7d12e780 3573irqreturn_t esp_intr(int irq, void *dev_id)
1da177e4
LT
3574{
3575 struct NCR_ESP *esp;
3576 unsigned long flags;
3577 struct Scsi_Host *dev = dev_id;
3578
3579 /* Handle all ESP interrupts showing at this IRQ level. */
3580 spin_lock_irqsave(dev->host_lock, flags);
3581 for_each_esp(esp) {
3582 if(((esp)->irq & 0xf) == irq) {
3583 if(esp->dma_irq_p(esp)) {
3584 esp->dma_ints_off(esp);
3585
3586 ESPIRQ(("I[%d:%d](",
3587 smp_processor_id(), esp->esp_id));
3588 esp_handle(esp);
3589 ESPIRQ((")"));
3590
3591 esp->dma_ints_on(esp);
3592 goto out;
3593 }
3594 }
3595 }
3596out:
3597 spin_unlock_irqrestore(dev->host_lock, flags);
3598 return IRQ_HANDLED;
3599}
3600#endif
3601
f64a181d 3602int esp_slave_alloc(struct scsi_device *SDptr)
1da177e4
LT
3603{
3604 struct esp_device *esp_dev =
3605 kmalloc(sizeof(struct esp_device), GFP_ATOMIC);
3606
3607 if (!esp_dev)
3608 return -ENOMEM;
3609 memset(esp_dev, 0, sizeof(struct esp_device));
3610 SDptr->hostdata = esp_dev;
3611 return 0;
3612}
3613
f64a181d 3614void esp_slave_destroy(struct scsi_device *SDptr)
1da177e4
LT
3615{
3616 struct NCR_ESP *esp = (struct NCR_ESP *) SDptr->host->hostdata;
3617
422c0d61 3618 esp->targets_present &= ~(1 << sdev_id(SDptr));
1da177e4
LT
3619 kfree(SDptr->hostdata);
3620 SDptr->hostdata = NULL;
3621}
3622
3623#ifdef MODULE
3624int init_module(void) { return 0; }
3625void cleanup_module(void) {}
3626void esp_release(void)
3627{
3628 esps_in_use--;
3629 esps_running = esps_in_use;
3630}
3631#endif
3632
3633EXPORT_SYMBOL(esp_abort);
3634EXPORT_SYMBOL(esp_allocate);
3635EXPORT_SYMBOL(esp_deallocate);
3636EXPORT_SYMBOL(esp_initialize);
3637EXPORT_SYMBOL(esp_intr);
3638EXPORT_SYMBOL(esp_queue);
3639EXPORT_SYMBOL(esp_reset);
3640EXPORT_SYMBOL(esp_slave_alloc);
3641EXPORT_SYMBOL(esp_slave_destroy);
3642EXPORT_SYMBOL(esps_in_use);
3643
3644MODULE_LICENSE("GPL");