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