GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / sym53c8xx_2 / sym_hipd.c
blobaa421692d9797867a7cfe181b62835253c0fd67a
1 /*
2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
6 * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
8 * This driver is derived from the Linux sym53c8xx driver.
9 * Copyright (C) 1998-2000 Gerard Roudier
11 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
12 * a port of the FreeBSD ncr driver to Linux-1.2.13.
14 * The original ncr driver has been written for 386bsd and FreeBSD by
15 * Wolfgang Stanglmeier <wolf@cologne.de>
16 * Stefan Esser <se@mi.Uni-Koeln.de>
17 * Copyright (C) 1994 Wolfgang Stanglmeier
19 * Other major contributions:
21 * NVRAM detection and reading.
22 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
24 *-----------------------------------------------------------------------------
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 #include <linux/slab.h>
42 #include <asm/param.h> /* for timeouts in units of HZ */
44 #include "sym_glue.h"
45 #include "sym_nvram.h"
49 * Needed function prototypes.
51 static void sym_int_ma (struct sym_hcb *np);
52 static void sym_int_sir(struct sym_hcb *);
53 static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np);
54 static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa);
55 static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln);
56 static void sym_complete_error (struct sym_hcb *np, struct sym_ccb *cp);
57 static void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp);
58 static int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp);
61 * Print a buffer in hexadecimal format with a ".\n" at end.
63 static void sym_printl_hex(u_char *p, int n)
65 while (n-- > 0)
66 printf (" %x", *p++);
67 printf (".\n");
70 static void sym_print_msg(struct sym_ccb *cp, char *label, u_char *msg)
72 sym_print_addr(cp->cmd, "%s: ", label);
74 spi_print_msg(msg);
75 printf("\n");
78 static void sym_print_nego_msg(struct sym_hcb *np, int target, char *label, u_char *msg)
80 struct sym_tcb *tp = &np->target[target];
81 dev_info(&tp->starget->dev, "%s: ", label);
83 spi_print_msg(msg);
84 printf("\n");
88 * Print something that tells about extended errors.
90 void sym_print_xerr(struct scsi_cmnd *cmd, int x_status)
92 if (x_status & XE_PARITY_ERR) {
93 sym_print_addr(cmd, "unrecovered SCSI parity error.\n");
95 if (x_status & XE_EXTRA_DATA) {
96 sym_print_addr(cmd, "extraneous data discarded.\n");
98 if (x_status & XE_BAD_PHASE) {
99 sym_print_addr(cmd, "illegal scsi phase (4/5).\n");
101 if (x_status & XE_SODL_UNRUN) {
102 sym_print_addr(cmd, "ODD transfer in DATA OUT phase.\n");
104 if (x_status & XE_SWIDE_OVRUN) {
105 sym_print_addr(cmd, "ODD transfer in DATA IN phase.\n");
110 * Return a string for SCSI BUS mode.
112 static char *sym_scsi_bus_mode(int mode)
114 switch(mode) {
115 case SMODE_HVD: return "HVD";
116 case SMODE_SE: return "SE";
117 case SMODE_LVD: return "LVD";
119 return "??";
123 * Soft reset the chip.
125 * Raising SRST when the chip is running may cause
126 * problems on dual function chips (see below).
127 * On the other hand, LVD devices need some delay
128 * to settle and report actual BUS mode in STEST4.
130 static void sym_chip_reset (struct sym_hcb *np)
132 OUTB(np, nc_istat, SRST);
133 INB(np, nc_mbox1);
134 udelay(10);
135 OUTB(np, nc_istat, 0);
136 INB(np, nc_mbox1);
137 udelay(2000); /* For BUS MODE to settle */
141 * Really soft reset the chip.:)
143 * Some 896 and 876 chip revisions may hang-up if we set
144 * the SRST (soft reset) bit at the wrong time when SCRIPTS
145 * are running.
146 * So, we need to abort the current operation prior to
147 * soft resetting the chip.
149 static void sym_soft_reset (struct sym_hcb *np)
151 u_char istat = 0;
152 int i;
154 if (!(np->features & FE_ISTAT1) || !(INB(np, nc_istat1) & SCRUN))
155 goto do_chip_reset;
157 OUTB(np, nc_istat, CABRT);
158 for (i = 100000 ; i ; --i) {
159 istat = INB(np, nc_istat);
160 if (istat & SIP) {
161 INW(np, nc_sist);
163 else if (istat & DIP) {
164 if (INB(np, nc_dstat) & ABRT)
165 break;
167 udelay(5);
169 OUTB(np, nc_istat, 0);
170 if (!i)
171 printf("%s: unable to abort current chip operation, "
172 "ISTAT=0x%02x.\n", sym_name(np), istat);
173 do_chip_reset:
174 sym_chip_reset(np);
178 * Start reset process.
180 * The interrupt handler will reinitialize the chip.
182 static void sym_start_reset(struct sym_hcb *np)
184 sym_reset_scsi_bus(np, 1);
187 int sym_reset_scsi_bus(struct sym_hcb *np, int enab_int)
189 u32 term;
190 int retv = 0;
192 sym_soft_reset(np); /* Soft reset the chip */
193 if (enab_int)
194 OUTW(np, nc_sien, RST);
196 * Enable Tolerant, reset IRQD if present and
197 * properly set IRQ mode, prior to resetting the bus.
199 OUTB(np, nc_stest3, TE);
200 OUTB(np, nc_dcntl, (np->rv_dcntl & IRQM));
201 OUTB(np, nc_scntl1, CRST);
202 INB(np, nc_mbox1);
203 udelay(200);
205 if (!SYM_SETUP_SCSI_BUS_CHECK)
206 goto out;
208 * Check for no terminators or SCSI bus shorts to ground.
209 * Read SCSI data bus, data parity bits and control signals.
210 * We are expecting RESET to be TRUE and other signals to be
211 * FALSE.
213 term = INB(np, nc_sstat0);
214 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
215 term |= ((INB(np, nc_sstat2) & 0x01) << 26) | /* sdp1 */
216 ((INW(np, nc_sbdl) & 0xff) << 9) | /* d7-0 */
217 ((INW(np, nc_sbdl) & 0xff00) << 10) | /* d15-8 */
218 INB(np, nc_sbcl); /* req ack bsy sel atn msg cd io */
220 if (!np->maxwide)
221 term &= 0x3ffff;
223 if (term != (2<<7)) {
224 printf("%s: suspicious SCSI data while resetting the BUS.\n",
225 sym_name(np));
226 printf("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
227 "0x%lx, expecting 0x%lx\n",
228 sym_name(np),
229 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
230 (u_long)term, (u_long)(2<<7));
231 if (SYM_SETUP_SCSI_BUS_CHECK == 1)
232 retv = 1;
234 out:
235 OUTB(np, nc_scntl1, 0);
236 return retv;
240 * Select SCSI clock frequency
242 static void sym_selectclock(struct sym_hcb *np, u_char scntl3)
245 * If multiplier not present or not selected, leave here.
247 if (np->multiplier <= 1) {
248 OUTB(np, nc_scntl3, scntl3);
249 return;
252 if (sym_verbose >= 2)
253 printf ("%s: enabling clock multiplier\n", sym_name(np));
255 OUTB(np, nc_stest1, DBLEN); /* Enable clock multiplier */
257 * Wait for the LCKFRQ bit to be set if supported by the chip.
258 * Otherwise wait 50 micro-seconds (at least).
260 if (np->features & FE_LCKFRQ) {
261 int i = 20;
262 while (!(INB(np, nc_stest4) & LCKFRQ) && --i > 0)
263 udelay(20);
264 if (!i)
265 printf("%s: the chip cannot lock the frequency\n",
266 sym_name(np));
267 } else {
268 INB(np, nc_mbox1);
269 udelay(50+10);
271 OUTB(np, nc_stest3, HSC); /* Halt the scsi clock */
272 OUTB(np, nc_scntl3, scntl3);
273 OUTB(np, nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
274 OUTB(np, nc_stest3, 0x00); /* Restart scsi clock */
279 * Determine the chip's clock frequency.
281 * This is essential for the negotiation of the synchronous
282 * transfer rate.
284 * Note: we have to return the correct value.
285 * THERE IS NO SAFE DEFAULT VALUE.
287 * Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
288 * 53C860 and 53C875 rev. 1 support fast20 transfers but
289 * do not have a clock doubler and so are provided with a
290 * 80 MHz clock. All other fast20 boards incorporate a doubler
291 * and so should be delivered with a 40 MHz clock.
292 * The recent fast40 chips (895/896/895A/1010) use a 40 Mhz base
293 * clock and provide a clock quadrupler (160 Mhz).
297 * calculate SCSI clock frequency (in KHz)
299 static unsigned getfreq (struct sym_hcb *np, int gen)
301 unsigned int ms = 0;
302 unsigned int f;
305 * Measure GEN timer delay in order
306 * to calculate SCSI clock frequency
308 * This code will never execute too
309 * many loop iterations (if DELAY is
310 * reasonably correct). It could get
311 * too low a delay (too high a freq.)
312 * if the CPU is slow executing the
313 * loop for some reason (an NMI, for
314 * example). For this reason we will
315 * if multiple measurements are to be
316 * performed trust the higher delay
317 * (lower frequency returned).
319 OUTW(np, nc_sien, 0); /* mask all scsi interrupts */
320 INW(np, nc_sist); /* clear pending scsi interrupt */
321 OUTB(np, nc_dien, 0); /* mask all dma interrupts */
322 INW(np, nc_sist); /* another one, just to be sure :) */
324 * The C1010-33 core does not report GEN in SIST,
325 * if this interrupt is masked in SIEN.
326 * I don't know yet if the C1010-66 behaves the same way.
328 if (np->features & FE_C10) {
329 OUTW(np, nc_sien, GEN);
330 OUTB(np, nc_istat1, SIRQD);
332 OUTB(np, nc_scntl3, 4); /* set pre-scaler to divide by 3 */
333 OUTB(np, nc_stime1, 0); /* disable general purpose timer */
334 OUTB(np, nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
335 while (!(INW(np, nc_sist) & GEN) && ms++ < 100000)
336 udelay(1000/4); /* count in 1/4 of ms */
337 OUTB(np, nc_stime1, 0); /* disable general purpose timer */
339 * Undo C1010-33 specific settings.
341 if (np->features & FE_C10) {
342 OUTW(np, nc_sien, 0);
343 OUTB(np, nc_istat1, 0);
346 * set prescaler to divide by whatever 0 means
347 * 0 ought to choose divide by 2, but appears
348 * to set divide by 3.5 mode in my 53c810 ...
350 OUTB(np, nc_scntl3, 0);
353 * adjust for prescaler, and convert into KHz
355 f = ms ? ((1 << gen) * (4340*4)) / ms : 0;
358 * The C1010-33 result is biased by a factor
359 * of 2/3 compared to earlier chips.
361 if (np->features & FE_C10)
362 f = (f * 2) / 3;
364 if (sym_verbose >= 2)
365 printf ("%s: Delay (GEN=%d): %u msec, %u KHz\n",
366 sym_name(np), gen, ms/4, f);
368 return f;
371 static unsigned sym_getfreq (struct sym_hcb *np)
373 u_int f1, f2;
374 int gen = 8;
376 getfreq (np, gen); /* throw away first result */
377 f1 = getfreq (np, gen);
378 f2 = getfreq (np, gen);
379 if (f1 > f2) f1 = f2; /* trust lower result */
380 return f1;
384 * Get/probe chip SCSI clock frequency
386 static void sym_getclock (struct sym_hcb *np, int mult)
388 unsigned char scntl3 = np->sv_scntl3;
389 unsigned char stest1 = np->sv_stest1;
390 unsigned f1;
392 np->multiplier = 1;
393 f1 = 40000;
395 * True with 875/895/896/895A with clock multiplier selected
397 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
398 if (sym_verbose >= 2)
399 printf ("%s: clock multiplier found\n", sym_name(np));
400 np->multiplier = mult;
404 * If multiplier not found or scntl3 not 7,5,3,
405 * reset chip and get frequency from general purpose timer.
406 * Otherwise trust scntl3 BIOS setting.
408 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
409 OUTB(np, nc_stest1, 0); /* make sure doubler is OFF */
410 f1 = sym_getfreq (np);
412 if (sym_verbose)
413 printf ("%s: chip clock is %uKHz\n", sym_name(np), f1);
415 if (f1 < 45000) f1 = 40000;
416 else if (f1 < 55000) f1 = 50000;
417 else f1 = 80000;
419 if (f1 < 80000 && mult > 1) {
420 if (sym_verbose >= 2)
421 printf ("%s: clock multiplier assumed\n",
422 sym_name(np));
423 np->multiplier = mult;
425 } else {
426 if ((scntl3 & 7) == 3) f1 = 40000;
427 else if ((scntl3 & 7) == 5) f1 = 80000;
428 else f1 = 160000;
430 f1 /= np->multiplier;
434 * Compute controller synchronous parameters.
436 f1 *= np->multiplier;
437 np->clock_khz = f1;
441 * Get/probe PCI clock frequency
443 static int sym_getpciclock (struct sym_hcb *np)
445 int f = 0;
448 * For now, we only need to know about the actual
449 * PCI BUS clock frequency for C1010-66 chips.
451 if (np->features & FE_66MHZ) {
452 OUTB(np, nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */
453 f = sym_getfreq(np);
454 OUTB(np, nc_stest1, 0);
456 np->pciclk_khz = f;
458 return f;
462 * SYMBIOS chip clock divisor table.
464 * Divisors are multiplied by 10,000,000 in order to make
465 * calculations more simple.
467 #define _5M 5000000
468 static const u32 div_10M[] = {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
471 * Get clock factor and sync divisor for a given
472 * synchronous factor period.
474 static int
475 sym_getsync(struct sym_hcb *np, u_char dt, u_char sfac, u_char *divp, u_char *fakp)
477 u32 clk = np->clock_khz; /* SCSI clock frequency in kHz */
478 int div = np->clock_divn; /* Number of divisors supported */
479 u32 fak; /* Sync factor in sxfer */
480 u32 per; /* Period in tenths of ns */
481 u32 kpc; /* (per * clk) */
482 int ret;
485 * Compute the synchronous period in tenths of nano-seconds
487 if (dt && sfac <= 9) per = 125;
488 else if (sfac <= 10) per = 250;
489 else if (sfac == 11) per = 303;
490 else if (sfac == 12) per = 500;
491 else per = 40 * sfac;
492 ret = per;
494 kpc = per * clk;
495 if (dt)
496 kpc <<= 1;
499 * For earliest C10 revision 0, we cannot use extra
500 * clocks for the setting of the SCSI clocking.
501 * Note that this limits the lowest sync data transfer
502 * to 5 Mega-transfers per second and may result in
503 * using higher clock divisors.
505 if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) {
507 * Look for the lowest clock divisor that allows an
508 * output speed not faster than the period.
510 while (div > 0) {
511 --div;
512 if (kpc > (div_10M[div] << 2)) {
513 ++div;
514 break;
517 fak = 0; /* No extra clocks */
518 if (div == np->clock_divn) { /* Are we too fast ? */
519 ret = -1;
521 *divp = div;
522 *fakp = fak;
523 return ret;
527 * Look for the greatest clock divisor that allows an
528 * input speed faster than the period.
530 while (div-- > 0)
531 if (kpc >= (div_10M[div] << 2)) break;
534 * Calculate the lowest clock factor that allows an output
535 * speed not faster than the period, and the max output speed.
536 * If fak >= 1 we will set both XCLKH_ST and XCLKH_DT.
537 * If fak >= 2 we will also set XCLKS_ST and XCLKS_DT.
539 if (dt) {
540 fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2;
541 /* ret = ((2+fak)*div_10M[div])/np->clock_khz; */
542 } else {
543 fak = (kpc - 1) / div_10M[div] + 1 - 4;
544 /* ret = ((4+fak)*div_10M[div])/np->clock_khz; */
548 * Check against our hardware limits, or bugs :).
550 if (fak > 2) {
551 fak = 2;
552 ret = -1;
556 * Compute and return sync parameters.
558 *divp = div;
559 *fakp = fak;
561 return ret;
565 * SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64,
566 * 128 transfers. All chips support at least 16 transfers
567 * bursts. The 825A, 875 and 895 chips support bursts of up
568 * to 128 transfers and the 895A and 896 support bursts of up
569 * to 64 transfers. All other chips support up to 16
570 * transfers bursts.
572 * For PCI 32 bit data transfers each transfer is a DWORD.
573 * It is a QUADWORD (8 bytes) for PCI 64 bit data transfers.
575 * We use log base 2 (burst length) as internal code, with
576 * value 0 meaning "burst disabled".
580 * Burst length from burst code.
582 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
585 * Burst code from io register bits.
587 #define burst_code(dmode, ctest4, ctest5) \
588 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
591 * Set initial io register bits from burst code.
593 static inline void sym_init_burst(struct sym_hcb *np, u_char bc)
595 np->rv_ctest4 &= ~0x80;
596 np->rv_dmode &= ~(0x3 << 6);
597 np->rv_ctest5 &= ~0x4;
599 if (!bc) {
600 np->rv_ctest4 |= 0x80;
602 else {
603 --bc;
604 np->rv_dmode |= ((bc & 0x3) << 6);
605 np->rv_ctest5 |= (bc & 0x4);
610 * Save initial settings of some IO registers.
611 * Assumed to have been set by BIOS.
612 * We cannot reset the chip prior to reading the
613 * IO registers, since informations will be lost.
614 * Since the SCRIPTS processor may be running, this
615 * is not safe on paper, but it seems to work quite
616 * well. :)
618 static void sym_save_initial_setting (struct sym_hcb *np)
620 np->sv_scntl0 = INB(np, nc_scntl0) & 0x0a;
621 np->sv_scntl3 = INB(np, nc_scntl3) & 0x07;
622 np->sv_dmode = INB(np, nc_dmode) & 0xce;
623 np->sv_dcntl = INB(np, nc_dcntl) & 0xa8;
624 np->sv_ctest3 = INB(np, nc_ctest3) & 0x01;
625 np->sv_ctest4 = INB(np, nc_ctest4) & 0x80;
626 np->sv_gpcntl = INB(np, nc_gpcntl);
627 np->sv_stest1 = INB(np, nc_stest1);
628 np->sv_stest2 = INB(np, nc_stest2) & 0x20;
629 np->sv_stest4 = INB(np, nc_stest4);
630 if (np->features & FE_C10) { /* Always large DMA fifo + ultra3 */
631 np->sv_scntl4 = INB(np, nc_scntl4);
632 np->sv_ctest5 = INB(np, nc_ctest5) & 0x04;
634 else
635 np->sv_ctest5 = INB(np, nc_ctest5) & 0x24;
639 * Set SCSI BUS mode.
640 * - LVD capable chips (895/895A/896/1010) report the current BUS mode
641 * through the STEST4 IO register.
642 * - For previous generation chips (825/825A/875), the user has to tell us
643 * how to check against HVD, since a 100% safe algorithm is not possible.
645 static void sym_set_bus_mode(struct sym_hcb *np, struct sym_nvram *nvram)
647 if (np->scsi_mode)
648 return;
650 np->scsi_mode = SMODE_SE;
651 if (np->features & (FE_ULTRA2|FE_ULTRA3))
652 np->scsi_mode = (np->sv_stest4 & SMODE);
653 else if (np->features & FE_DIFF) {
654 if (SYM_SETUP_SCSI_DIFF == 1) {
655 if (np->sv_scntl3) {
656 if (np->sv_stest2 & 0x20)
657 np->scsi_mode = SMODE_HVD;
658 } else if (nvram->type == SYM_SYMBIOS_NVRAM) {
659 if (!(INB(np, nc_gpreg) & 0x08))
660 np->scsi_mode = SMODE_HVD;
662 } else if (SYM_SETUP_SCSI_DIFF == 2)
663 np->scsi_mode = SMODE_HVD;
665 if (np->scsi_mode == SMODE_HVD)
666 np->rv_stest2 |= 0x20;
670 * Prepare io register values used by sym_start_up()
671 * according to selected and supported features.
673 static int sym_prepare_setting(struct Scsi_Host *shost, struct sym_hcb *np, struct sym_nvram *nvram)
675 struct sym_data *sym_data = shost_priv(shost);
676 struct pci_dev *pdev = sym_data->pdev;
677 u_char burst_max;
678 u32 period;
679 int i;
681 np->maxwide = (np->features & FE_WIDE) ? 1 : 0;
684 * Guess the frequency of the chip's clock.
686 if (np->features & (FE_ULTRA3 | FE_ULTRA2))
687 np->clock_khz = 160000;
688 else if (np->features & FE_ULTRA)
689 np->clock_khz = 80000;
690 else
691 np->clock_khz = 40000;
694 * Get the clock multiplier factor.
696 if (np->features & FE_QUAD)
697 np->multiplier = 4;
698 else if (np->features & FE_DBLR)
699 np->multiplier = 2;
700 else
701 np->multiplier = 1;
704 * Measure SCSI clock frequency for chips
705 * it may vary from assumed one.
707 if (np->features & FE_VARCLK)
708 sym_getclock(np, np->multiplier);
711 * Divisor to be used for async (timer pre-scaler).
713 i = np->clock_divn - 1;
714 while (--i >= 0) {
715 if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) {
716 ++i;
717 break;
720 np->rv_scntl3 = i+1;
723 * The C1010 uses hardwired divisors for async.
724 * So, we just throw away, the async. divisor.:-)
726 if (np->features & FE_C10)
727 np->rv_scntl3 = 0;
730 * Minimum synchronous period factor supported by the chip.
731 * Btw, 'period' is in tenths of nanoseconds.
733 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
735 if (period <= 250) np->minsync = 10;
736 else if (period <= 303) np->minsync = 11;
737 else if (period <= 500) np->minsync = 12;
738 else np->minsync = (period + 40 - 1) / 40;
741 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
743 if (np->minsync < 25 &&
744 !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
745 np->minsync = 25;
746 else if (np->minsync < 12 &&
747 !(np->features & (FE_ULTRA2|FE_ULTRA3)))
748 np->minsync = 12;
751 * Maximum synchronous period factor supported by the chip.
753 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
754 np->maxsync = period > 2540 ? 254 : period / 10;
757 * If chip is a C1010, guess the sync limits in DT mode.
759 if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) {
760 if (np->clock_khz == 160000) {
761 np->minsync_dt = 9;
762 np->maxsync_dt = 50;
763 np->maxoffs_dt = nvram->type ? 62 : 31;
768 * 64 bit addressing (895A/896/1010) ?
770 if (np->features & FE_DAC) {
771 if (!use_dac(np))
772 np->rv_ccntl1 |= (DDAC);
773 else if (SYM_CONF_DMA_ADDRESSING_MODE == 1)
774 np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
775 else if (SYM_CONF_DMA_ADDRESSING_MODE == 2)
776 np->rv_ccntl1 |= (0 | EXTIBMV);
780 * Phase mismatch handled by SCRIPTS (895A/896/1010) ?
782 if (np->features & FE_NOPM)
783 np->rv_ccntl0 |= (ENPMJ);
786 * C1010-33 Errata: Part Number:609-039638 (rev. 1) is fixed.
787 * In dual channel mode, contention occurs if internal cycles
788 * are used. Disable internal cycles.
790 if (pdev->device == PCI_DEVICE_ID_LSI_53C1010_33 &&
791 pdev->revision < 0x1)
792 np->rv_ccntl0 |= DILS;
795 * Select burst length (dwords)
797 burst_max = SYM_SETUP_BURST_ORDER;
798 if (burst_max == 255)
799 burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
800 np->sv_ctest5);
801 if (burst_max > 7)
802 burst_max = 7;
803 if (burst_max > np->maxburst)
804 burst_max = np->maxburst;
806 if ((pdev->device == PCI_DEVICE_ID_NCR_53C810 &&
807 pdev->revision >= 0x10 && pdev->revision <= 0x11) ||
808 (pdev->device == PCI_DEVICE_ID_NCR_53C860 &&
809 pdev->revision <= 0x1))
810 np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
813 * Select all supported special features.
814 * If we are using on-board RAM for scripts, prefetch (PFEN)
815 * does not help, but burst op fetch (BOF) does.
816 * Disabling PFEN makes sure BOF will be used.
818 if (np->features & FE_ERL)
819 np->rv_dmode |= ERL; /* Enable Read Line */
820 if (np->features & FE_BOF)
821 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
822 if (np->features & FE_ERMP)
823 np->rv_dmode |= ERMP; /* Enable Read Multiple */
824 if ((np->features & FE_PFEN) && !np->ram_ba)
825 np->rv_dcntl |= PFEN; /* Prefetch Enable */
826 if (np->features & FE_CLSE)
827 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
828 if (np->features & FE_WRIE)
829 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
830 if (np->features & FE_DFS)
831 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
834 * Select some other
836 np->rv_ctest4 |= MPEE; /* Master parity checking */
837 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
840 * Get parity checking, host ID and verbose mode from NVRAM
842 np->myaddr = 255;
843 np->scsi_mode = 0;
844 sym_nvram_setup_host(shost, np, nvram);
847 * Get SCSI addr of host adapter (set by bios?).
849 if (np->myaddr == 255) {
850 np->myaddr = INB(np, nc_scid) & 0x07;
851 if (!np->myaddr)
852 np->myaddr = SYM_SETUP_HOST_ID;
856 * Prepare initial io register bits for burst length
858 sym_init_burst(np, burst_max);
860 sym_set_bus_mode(np, nvram);
863 * Set LED support from SCRIPTS.
864 * Ignore this feature for boards known to use a
865 * specific GPIO wiring and for the 895A, 896
866 * and 1010 that drive the LED directly.
868 if ((SYM_SETUP_SCSI_LED ||
869 (nvram->type == SYM_SYMBIOS_NVRAM ||
870 (nvram->type == SYM_TEKRAM_NVRAM &&
871 pdev->device == PCI_DEVICE_ID_NCR_53C895))) &&
872 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
873 np->features |= FE_LED0;
876 * Set irq mode.
878 switch(SYM_SETUP_IRQ_MODE & 3) {
879 case 2:
880 np->rv_dcntl |= IRQM;
881 break;
882 case 1:
883 np->rv_dcntl |= (np->sv_dcntl & IRQM);
884 break;
885 default:
886 break;
890 * Configure targets according to driver setup.
891 * If NVRAM present get targets setup from NVRAM.
893 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
894 struct sym_tcb *tp = &np->target[i];
896 tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
897 tp->usrtags = SYM_SETUP_MAX_TAG;
898 tp->usr_width = np->maxwide;
899 tp->usr_period = 9;
901 sym_nvram_setup_target(tp, i, nvram);
903 if (!tp->usrtags)
904 tp->usrflags &= ~SYM_TAGS_ENABLED;
908 * Let user know about the settings.
910 printf("%s: %s, ID %d, Fast-%d, %s, %s\n", sym_name(np),
911 sym_nvram_type(nvram), np->myaddr,
912 (np->features & FE_ULTRA3) ? 80 :
913 (np->features & FE_ULTRA2) ? 40 :
914 (np->features & FE_ULTRA) ? 20 : 10,
915 sym_scsi_bus_mode(np->scsi_mode),
916 (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
918 * Tell him more on demand.
920 if (sym_verbose) {
921 printf("%s: %s IRQ line driver%s\n",
922 sym_name(np),
923 np->rv_dcntl & IRQM ? "totem pole" : "open drain",
924 np->ram_ba ? ", using on-chip SRAM" : "");
925 printf("%s: using %s firmware.\n", sym_name(np), np->fw_name);
926 if (np->features & FE_NOPM)
927 printf("%s: handling phase mismatch from SCRIPTS.\n",
928 sym_name(np));
931 * And still more.
933 if (sym_verbose >= 2) {
934 printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
935 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
936 sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
937 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
939 printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
940 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
941 sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
942 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
945 return 0;
949 * Test the pci bus snoop logic :-(
951 * Has to be called with interrupts disabled.
953 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
954 static int sym_regtest(struct sym_hcb *np)
956 register volatile u32 data;
958 * chip registers may NOT be cached.
959 * write 0xffffffff to a read only register area,
960 * and try to read it back.
962 data = 0xffffffff;
963 OUTL(np, nc_dstat, data);
964 data = INL(np, nc_dstat);
965 if (data == 0xffffffff) {
966 printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
967 (unsigned) data);
968 return 0x10;
970 return 0;
972 #else
973 static inline int sym_regtest(struct sym_hcb *np)
975 return 0;
977 #endif
979 static int sym_snooptest(struct sym_hcb *np)
981 u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
982 int i, err;
984 err = sym_regtest(np);
985 if (err)
986 return err;
987 restart_test:
989 * Enable Master Parity Checking as we intend
990 * to enable it for normal operations.
992 OUTB(np, nc_ctest4, (np->rv_ctest4 & MPEE));
994 * init
996 pc = SCRIPTZ_BA(np, snooptest);
997 host_wr = 1;
998 sym_wr = 2;
1000 * Set memory and register.
1002 np->scratch = cpu_to_scr(host_wr);
1003 OUTL(np, nc_temp, sym_wr);
1005 * Start script (exchange values)
1007 OUTL(np, nc_dsa, np->hcb_ba);
1008 OUTL_DSP(np, pc);
1010 * Wait 'til done (with timeout)
1012 for (i=0; i<SYM_SNOOP_TIMEOUT; i++)
1013 if (INB(np, nc_istat) & (INTF|SIP|DIP))
1014 break;
1015 if (i>=SYM_SNOOP_TIMEOUT) {
1016 printf ("CACHE TEST FAILED: timeout.\n");
1017 return (0x20);
1020 * Check for fatal DMA errors.
1022 dstat = INB(np, nc_dstat);
1023 if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
1024 printf ("%s: PCI DATA PARITY ERROR DETECTED - "
1025 "DISABLING MASTER DATA PARITY CHECKING.\n",
1026 sym_name(np));
1027 np->rv_ctest4 &= ~MPEE;
1028 goto restart_test;
1030 if (dstat & (MDPE|BF|IID)) {
1031 printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
1032 return (0x80);
1035 * Save termination position.
1037 pc = INL(np, nc_dsp);
1039 * Read memory and register.
1041 host_rd = scr_to_cpu(np->scratch);
1042 sym_rd = INL(np, nc_scratcha);
1043 sym_bk = INL(np, nc_temp);
1045 * Check termination position.
1047 if (pc != SCRIPTZ_BA(np, snoopend)+8) {
1048 printf ("CACHE TEST FAILED: script execution failed.\n");
1049 printf ("start=%08lx, pc=%08lx, end=%08lx\n",
1050 (u_long) SCRIPTZ_BA(np, snooptest), (u_long) pc,
1051 (u_long) SCRIPTZ_BA(np, snoopend) +8);
1052 return (0x40);
1055 * Show results.
1057 if (host_wr != sym_rd) {
1058 printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n",
1059 (int) host_wr, (int) sym_rd);
1060 err |= 1;
1062 if (host_rd != sym_wr) {
1063 printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n",
1064 (int) sym_wr, (int) host_rd);
1065 err |= 2;
1067 if (sym_bk != sym_wr) {
1068 printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n",
1069 (int) sym_wr, (int) sym_bk);
1070 err |= 4;
1073 return err;
1077 * log message for real hard errors
1079 * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sx/s3/s4) @ name (dsp:dbc).
1080 * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf.
1082 * exception register:
1083 * ds: dstat
1084 * si: sist
1086 * SCSI bus lines:
1087 * so: control lines as driven by chip.
1088 * si: control lines as seen by chip.
1089 * sd: scsi data lines as seen by chip.
1091 * wide/fastmode:
1092 * sx: sxfer (see the manual)
1093 * s3: scntl3 (see the manual)
1094 * s4: scntl4 (see the manual)
1096 * current script command:
1097 * dsp: script address (relative to start of script).
1098 * dbc: first word of script command.
1100 * First 24 register of the chip:
1101 * r0..rf
1103 static void sym_log_hard_error(struct Scsi_Host *shost, u_short sist, u_char dstat)
1105 struct sym_hcb *np = sym_get_hcb(shost);
1106 u32 dsp;
1107 int script_ofs;
1108 int script_size;
1109 char *script_name;
1110 u_char *script_base;
1111 int i;
1113 dsp = INL(np, nc_dsp);
1115 if (dsp > np->scripta_ba &&
1116 dsp <= np->scripta_ba + np->scripta_sz) {
1117 script_ofs = dsp - np->scripta_ba;
1118 script_size = np->scripta_sz;
1119 script_base = (u_char *) np->scripta0;
1120 script_name = "scripta";
1122 else if (np->scriptb_ba < dsp &&
1123 dsp <= np->scriptb_ba + np->scriptb_sz) {
1124 script_ofs = dsp - np->scriptb_ba;
1125 script_size = np->scriptb_sz;
1126 script_base = (u_char *) np->scriptb0;
1127 script_name = "scriptb";
1128 } else {
1129 script_ofs = dsp;
1130 script_size = 0;
1131 script_base = NULL;
1132 script_name = "mem";
1135 printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x/%x) @ (%s %x:%08x).\n",
1136 sym_name(np), (unsigned)INB(np, nc_sdid)&0x0f, dstat, sist,
1137 (unsigned)INB(np, nc_socl), (unsigned)INB(np, nc_sbcl),
1138 (unsigned)INB(np, nc_sbdl), (unsigned)INB(np, nc_sxfer),
1139 (unsigned)INB(np, nc_scntl3),
1140 (np->features & FE_C10) ? (unsigned)INB(np, nc_scntl4) : 0,
1141 script_name, script_ofs, (unsigned)INL(np, nc_dbc));
1143 if (((script_ofs & 3) == 0) &&
1144 (unsigned)script_ofs < script_size) {
1145 printf ("%s: script cmd = %08x\n", sym_name(np),
1146 scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
1149 printf("%s: regdump:", sym_name(np));
1150 for (i = 0; i < 24; i++)
1151 printf(" %02x", (unsigned)INB_OFF(np, i));
1152 printf(".\n");
1155 * PCI BUS error.
1157 if (dstat & (MDPE|BF))
1158 sym_log_bus_error(shost);
1161 void sym_dump_registers(struct Scsi_Host *shost)
1163 struct sym_hcb *np = sym_get_hcb(shost);
1164 u_short sist;
1165 u_char dstat;
1167 sist = INW(np, nc_sist);
1168 dstat = INB(np, nc_dstat);
1169 sym_log_hard_error(shost, sist, dstat);
1172 static struct sym_chip sym_dev_table[] = {
1173 {PCI_DEVICE_ID_NCR_53C810, 0x0f, "810", 4, 8, 4, 64,
1174 FE_ERL}
1176 #ifdef SYM_DEBUG_GENERIC_SUPPORT
1177 {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
1178 FE_BOF}
1180 #else
1181 {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
1182 FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
1184 #endif
1185 {PCI_DEVICE_ID_NCR_53C815, 0xff, "815", 4, 8, 4, 64,
1186 FE_BOF|FE_ERL}
1188 {PCI_DEVICE_ID_NCR_53C825, 0x0f, "825", 6, 8, 4, 64,
1189 FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
1191 {PCI_DEVICE_ID_NCR_53C825, 0xff, "825a", 6, 8, 4, 2,
1192 FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
1194 {PCI_DEVICE_ID_NCR_53C860, 0xff, "860", 4, 8, 5, 1,
1195 FE_ULTRA|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
1197 {PCI_DEVICE_ID_NCR_53C875, 0x01, "875", 6, 16, 5, 2,
1198 FE_WIDE|FE_ULTRA|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1199 FE_RAM|FE_DIFF|FE_VARCLK}
1201 {PCI_DEVICE_ID_NCR_53C875, 0xff, "875", 6, 16, 5, 2,
1202 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1203 FE_RAM|FE_DIFF|FE_VARCLK}
1205 {PCI_DEVICE_ID_NCR_53C875J, 0xff, "875J", 6, 16, 5, 2,
1206 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1207 FE_RAM|FE_DIFF|FE_VARCLK}
1209 {PCI_DEVICE_ID_NCR_53C885, 0xff, "885", 6, 16, 5, 2,
1210 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1211 FE_RAM|FE_DIFF|FE_VARCLK}
1213 #ifdef SYM_DEBUG_GENERIC_SUPPORT
1214 {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
1215 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
1216 FE_RAM|FE_LCKFRQ}
1218 #else
1219 {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
1220 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1221 FE_RAM|FE_LCKFRQ}
1223 #endif
1224 {PCI_DEVICE_ID_NCR_53C896, 0xff, "896", 6, 31, 7, 4,
1225 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1226 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1228 {PCI_DEVICE_ID_LSI_53C895A, 0xff, "895a", 6, 31, 7, 4,
1229 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1230 FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1232 {PCI_DEVICE_ID_LSI_53C875A, 0xff, "875a", 6, 31, 7, 4,
1233 FE_WIDE|FE_ULTRA|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1234 FE_RAM|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1236 {PCI_DEVICE_ID_LSI_53C1010_33, 0x00, "1010-33", 6, 31, 7, 8,
1237 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1238 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
1239 FE_C10}
1241 {PCI_DEVICE_ID_LSI_53C1010_33, 0xff, "1010-33", 6, 31, 7, 8,
1242 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1243 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
1244 FE_C10|FE_U3EN}
1246 {PCI_DEVICE_ID_LSI_53C1010_66, 0xff, "1010-66", 6, 31, 7, 8,
1247 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1248 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
1249 FE_C10|FE_U3EN}
1251 {PCI_DEVICE_ID_LSI_53C1510, 0xff, "1510d", 6, 31, 7, 4,
1252 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1253 FE_RAM|FE_IO256|FE_LEDC}
1256 #define sym_num_devs (ARRAY_SIZE(sym_dev_table))
1259 * Look up the chip table.
1261 * Return a pointer to the chip entry if found,
1262 * zero otherwise.
1264 struct sym_chip *
1265 sym_lookup_chip_table (u_short device_id, u_char revision)
1267 struct sym_chip *chip;
1268 int i;
1270 for (i = 0; i < sym_num_devs; i++) {
1271 chip = &sym_dev_table[i];
1272 if (device_id != chip->device_id)
1273 continue;
1274 if (revision > chip->revision_id)
1275 continue;
1276 return chip;
1279 return NULL;
1282 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1284 * Lookup the 64 bit DMA segments map.
1285 * This is only used if the direct mapping
1286 * has been unsuccessful.
1288 int sym_lookup_dmap(struct sym_hcb *np, u32 h, int s)
1290 int i;
1292 if (!use_dac(np))
1293 goto weird;
1295 /* Look up existing mappings */
1296 for (i = SYM_DMAP_SIZE-1; i > 0; i--) {
1297 if (h == np->dmap_bah[i])
1298 return i;
1300 /* If direct mapping is free, get it */
1301 if (!np->dmap_bah[s])
1302 goto new;
1303 /* Collision -> lookup free mappings */
1304 for (s = SYM_DMAP_SIZE-1; s > 0; s--) {
1305 if (!np->dmap_bah[s])
1306 goto new;
1308 weird:
1309 panic("sym: ran out of 64 bit DMA segment registers");
1310 return -1;
1311 new:
1312 np->dmap_bah[s] = h;
1313 np->dmap_dirty = 1;
1314 return s;
1318 * Update IO registers scratch C..R so they will be
1319 * in sync. with queued CCB expectations.
1321 static void sym_update_dmap_regs(struct sym_hcb *np)
1323 int o, i;
1325 if (!np->dmap_dirty)
1326 return;
1327 o = offsetof(struct sym_reg, nc_scrx[0]);
1328 for (i = 0; i < SYM_DMAP_SIZE; i++) {
1329 OUTL_OFF(np, o, np->dmap_bah[i]);
1330 o += 4;
1332 np->dmap_dirty = 0;
1334 #endif
1336 /* Enforce all the fiddly SPI rules and the chip limitations */
1337 static void sym_check_goals(struct sym_hcb *np, struct scsi_target *starget,
1338 struct sym_trans *goal)
1340 if (!spi_support_wide(starget))
1341 goal->width = 0;
1343 if (!spi_support_sync(starget)) {
1344 goal->iu = 0;
1345 goal->dt = 0;
1346 goal->qas = 0;
1347 goal->offset = 0;
1348 return;
1351 if (spi_support_dt(starget)) {
1352 if (spi_support_dt_only(starget))
1353 goal->dt = 1;
1355 if (goal->offset == 0)
1356 goal->dt = 0;
1357 } else {
1358 goal->dt = 0;
1361 /* Some targets fail to properly negotiate DT in SE mode */
1362 if ((np->scsi_mode != SMODE_LVD) || !(np->features & FE_U3EN))
1363 goal->dt = 0;
1365 if (goal->dt) {
1366 /* all DT transfers must be wide */
1367 goal->width = 1;
1368 if (goal->offset > np->maxoffs_dt)
1369 goal->offset = np->maxoffs_dt;
1370 if (goal->period < np->minsync_dt)
1371 goal->period = np->minsync_dt;
1372 if (goal->period > np->maxsync_dt)
1373 goal->period = np->maxsync_dt;
1374 } else {
1375 goal->iu = goal->qas = 0;
1376 if (goal->offset > np->maxoffs)
1377 goal->offset = np->maxoffs;
1378 if (goal->period < np->minsync)
1379 goal->period = np->minsync;
1380 if (goal->period > np->maxsync)
1381 goal->period = np->maxsync;
1386 * Prepare the next negotiation message if needed.
1388 * Fill in the part of message buffer that contains the
1389 * negotiation and the nego_status field of the CCB.
1390 * Returns the size of the message in bytes.
1392 static int sym_prepare_nego(struct sym_hcb *np, struct sym_ccb *cp, u_char *msgptr)
1394 struct sym_tcb *tp = &np->target[cp->target];
1395 struct scsi_target *starget = tp->starget;
1396 struct sym_trans *goal = &tp->tgoal;
1397 int msglen = 0;
1398 int nego;
1400 sym_check_goals(np, starget, goal);
1403 * Many devices implement PPR in a buggy way, so only use it if we
1404 * really want to.
1406 if (goal->renego == NS_PPR || (goal->offset &&
1407 (goal->iu || goal->dt || goal->qas || (goal->period < 0xa)))) {
1408 nego = NS_PPR;
1409 } else if (goal->renego == NS_WIDE || goal->width) {
1410 nego = NS_WIDE;
1411 } else if (goal->renego == NS_SYNC || goal->offset) {
1412 nego = NS_SYNC;
1413 } else {
1414 goal->check_nego = 0;
1415 nego = 0;
1418 switch (nego) {
1419 case NS_SYNC:
1420 msglen += spi_populate_sync_msg(msgptr + msglen, goal->period,
1421 goal->offset);
1422 break;
1423 case NS_WIDE:
1424 msglen += spi_populate_width_msg(msgptr + msglen, goal->width);
1425 break;
1426 case NS_PPR:
1427 msglen += spi_populate_ppr_msg(msgptr + msglen, goal->period,
1428 goal->offset, goal->width,
1429 (goal->iu ? PPR_OPT_IU : 0) |
1430 (goal->dt ? PPR_OPT_DT : 0) |
1431 (goal->qas ? PPR_OPT_QAS : 0));
1432 break;
1435 cp->nego_status = nego;
1437 if (nego) {
1438 tp->nego_cp = cp; /* Keep track a nego will be performed */
1439 if (DEBUG_FLAGS & DEBUG_NEGO) {
1440 sym_print_nego_msg(np, cp->target,
1441 nego == NS_SYNC ? "sync msgout" :
1442 nego == NS_WIDE ? "wide msgout" :
1443 "ppr msgout", msgptr);
1447 return msglen;
1451 * Insert a job into the start queue.
1453 void sym_put_start_queue(struct sym_hcb *np, struct sym_ccb *cp)
1455 u_short qidx;
1457 #ifdef SYM_CONF_IARB_SUPPORT
1459 * If the previously queued CCB is not yet done,
1460 * set the IARB hint. The SCRIPTS will go with IARB
1461 * for this job when starting the previous one.
1462 * We leave devices a chance to win arbitration by
1463 * not using more than 'iarb_max' consecutive
1464 * immediate arbitrations.
1466 if (np->last_cp && np->iarb_count < np->iarb_max) {
1467 np->last_cp->host_flags |= HF_HINT_IARB;
1468 ++np->iarb_count;
1470 else
1471 np->iarb_count = 0;
1472 np->last_cp = cp;
1473 #endif
1475 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1477 * Make SCRIPTS aware of the 64 bit DMA
1478 * segment registers not being up-to-date.
1480 if (np->dmap_dirty)
1481 cp->host_xflags |= HX_DMAP_DIRTY;
1482 #endif
1485 * Insert first the idle task and then our job.
1486 * The MBs should ensure proper ordering.
1488 qidx = np->squeueput + 2;
1489 if (qidx >= MAX_QUEUE*2) qidx = 0;
1491 np->squeue [qidx] = cpu_to_scr(np->idletask_ba);
1492 MEMORY_WRITE_BARRIER();
1493 np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba);
1495 np->squeueput = qidx;
1497 if (DEBUG_FLAGS & DEBUG_QUEUE)
1498 scmd_printk(KERN_DEBUG, cp->cmd, "queuepos=%d\n",
1499 np->squeueput);
1502 * Script processor may be waiting for reselect.
1503 * Wake it up.
1505 MEMORY_WRITE_BARRIER();
1506 OUTB(np, nc_istat, SIGP|np->istat_sem);
1509 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1511 * Start next ready-to-start CCBs.
1513 void sym_start_next_ccbs(struct sym_hcb *np, struct sym_lcb *lp, int maxn)
1515 SYM_QUEHEAD *qp;
1516 struct sym_ccb *cp;
1519 * Paranoia, as usual. :-)
1521 assert(!lp->started_tags || !lp->started_no_tag);
1524 * Try to start as many commands as asked by caller.
1525 * Prevent from having both tagged and untagged
1526 * commands queued to the device at the same time.
1528 while (maxn--) {
1529 qp = sym_remque_head(&lp->waiting_ccbq);
1530 if (!qp)
1531 break;
1532 cp = sym_que_entry(qp, struct sym_ccb, link2_ccbq);
1533 if (cp->tag != NO_TAG) {
1534 if (lp->started_no_tag ||
1535 lp->started_tags >= lp->started_max) {
1536 sym_insque_head(qp, &lp->waiting_ccbq);
1537 break;
1539 lp->itlq_tbl[cp->tag] = cpu_to_scr(cp->ccb_ba);
1540 lp->head.resel_sa =
1541 cpu_to_scr(SCRIPTA_BA(np, resel_tag));
1542 ++lp->started_tags;
1543 } else {
1544 if (lp->started_no_tag || lp->started_tags) {
1545 sym_insque_head(qp, &lp->waiting_ccbq);
1546 break;
1548 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
1549 lp->head.resel_sa =
1550 cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
1551 ++lp->started_no_tag;
1553 cp->started = 1;
1554 sym_insque_tail(qp, &lp->started_ccbq);
1555 sym_put_start_queue(np, cp);
1558 #endif /* SYM_OPT_HANDLE_DEVICE_QUEUEING */
1561 * The chip may have completed jobs. Look at the DONE QUEUE.
1563 * On paper, memory read barriers may be needed here to
1564 * prevent out of order LOADs by the CPU from having
1565 * prefetched stale data prior to DMA having occurred.
1567 static int sym_wakeup_done (struct sym_hcb *np)
1569 struct sym_ccb *cp;
1570 int i, n;
1571 u32 dsa;
1573 n = 0;
1574 i = np->dqueueget;
1576 /* MEMORY_READ_BARRIER(); */
1577 while (1) {
1578 dsa = scr_to_cpu(np->dqueue[i]);
1579 if (!dsa)
1580 break;
1581 np->dqueue[i] = 0;
1582 if ((i = i+2) >= MAX_QUEUE*2)
1583 i = 0;
1585 cp = sym_ccb_from_dsa(np, dsa);
1586 if (cp) {
1587 MEMORY_READ_BARRIER();
1588 sym_complete_ok (np, cp);
1589 ++n;
1591 else
1592 printf ("%s: bad DSA (%x) in done queue.\n",
1593 sym_name(np), (u_int) dsa);
1595 np->dqueueget = i;
1597 return n;
1601 * Complete all CCBs queued to the COMP queue.
1603 * These CCBs are assumed:
1604 * - Not to be referenced either by devices or
1605 * SCRIPTS-related queues and datas.
1606 * - To have to be completed with an error condition
1607 * or requeued.
1609 * The device queue freeze count is incremented
1610 * for each CCB that does not prevent this.
1611 * This function is called when all CCBs involved
1612 * in error handling/recovery have been reaped.
1614 static void sym_flush_comp_queue(struct sym_hcb *np, int cam_status)
1616 SYM_QUEHEAD *qp;
1617 struct sym_ccb *cp;
1619 while ((qp = sym_remque_head(&np->comp_ccbq)) != NULL) {
1620 struct scsi_cmnd *cmd;
1621 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
1622 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
1623 /* Leave quiet CCBs waiting for resources */
1624 if (cp->host_status == HS_WAIT)
1625 continue;
1626 cmd = cp->cmd;
1627 if (cam_status)
1628 sym_set_cam_status(cmd, cam_status);
1629 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1630 if (sym_get_cam_status(cmd) == DID_SOFT_ERROR) {
1631 struct sym_tcb *tp = &np->target[cp->target];
1632 struct sym_lcb *lp = sym_lp(tp, cp->lun);
1633 if (lp) {
1634 sym_remque(&cp->link2_ccbq);
1635 sym_insque_tail(&cp->link2_ccbq,
1636 &lp->waiting_ccbq);
1637 if (cp->started) {
1638 if (cp->tag != NO_TAG)
1639 --lp->started_tags;
1640 else
1641 --lp->started_no_tag;
1644 cp->started = 0;
1645 continue;
1647 #endif
1648 sym_free_ccb(np, cp);
1649 sym_xpt_done(np, cmd);
1654 * Complete all active CCBs with error.
1655 * Used on CHIP/SCSI RESET.
1657 static void sym_flush_busy_queue (struct sym_hcb *np, int cam_status)
1660 * Move all active CCBs to the COMP queue
1661 * and flush this queue.
1663 sym_que_splice(&np->busy_ccbq, &np->comp_ccbq);
1664 sym_que_init(&np->busy_ccbq);
1665 sym_flush_comp_queue(np, cam_status);
1669 * Start chip.
1671 * 'reason' means:
1672 * 0: initialisation.
1673 * 1: SCSI BUS RESET delivered or received.
1674 * 2: SCSI BUS MODE changed.
1676 void sym_start_up(struct Scsi_Host *shost, int reason)
1678 struct sym_data *sym_data = shost_priv(shost);
1679 struct pci_dev *pdev = sym_data->pdev;
1680 struct sym_hcb *np = sym_data->ncb;
1681 int i;
1682 u32 phys;
1685 * Reset chip if asked, otherwise just clear fifos.
1687 if (reason == 1)
1688 sym_soft_reset(np);
1689 else {
1690 OUTB(np, nc_stest3, TE|CSF);
1691 OUTONB(np, nc_ctest3, CLF);
1695 * Clear Start Queue
1697 phys = np->squeue_ba;
1698 for (i = 0; i < MAX_QUEUE*2; i += 2) {
1699 np->squeue[i] = cpu_to_scr(np->idletask_ba);
1700 np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
1702 np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
1705 * Start at first entry.
1707 np->squeueput = 0;
1710 * Clear Done Queue
1712 phys = np->dqueue_ba;
1713 for (i = 0; i < MAX_QUEUE*2; i += 2) {
1714 np->dqueue[i] = 0;
1715 np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
1717 np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
1720 * Start at first entry.
1722 np->dqueueget = 0;
1725 * Install patches in scripts.
1726 * This also let point to first position the start
1727 * and done queue pointers used from SCRIPTS.
1729 np->fw_patch(shost);
1732 * Wakeup all pending jobs.
1734 sym_flush_busy_queue(np, DID_RESET);
1737 * Init chip.
1739 OUTB(np, nc_istat, 0x00); /* Remove Reset, abort */
1740 INB(np, nc_mbox1);
1741 udelay(2000); /* The 895 needs time for the bus mode to settle */
1743 OUTB(np, nc_scntl0, np->rv_scntl0 | 0xc0);
1744 /* full arb., ena parity, par->ATN */
1745 OUTB(np, nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
1747 sym_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
1749 OUTB(np, nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
1750 OUTW(np, nc_respid, 1ul<<np->myaddr); /* Id to respond to */
1751 OUTB(np, nc_istat , SIGP ); /* Signal Process */
1752 OUTB(np, nc_dmode , np->rv_dmode); /* Burst length, dma mode */
1753 OUTB(np, nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
1755 OUTB(np, nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
1756 OUTB(np, nc_ctest3, np->rv_ctest3); /* Write and invalidate */
1757 OUTB(np, nc_ctest4, np->rv_ctest4); /* Master parity checking */
1759 /* Extended Sreq/Sack filtering not supported on the C10 */
1760 if (np->features & FE_C10)
1761 OUTB(np, nc_stest2, np->rv_stest2);
1762 else
1763 OUTB(np, nc_stest2, EXT|np->rv_stest2);
1765 OUTB(np, nc_stest3, TE); /* TolerANT enable */
1766 OUTB(np, nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */
1769 * For now, disable AIP generation on C1010-66.
1771 if (pdev->device == PCI_DEVICE_ID_LSI_53C1010_66)
1772 OUTB(np, nc_aipcntl1, DISAIP);
1775 * C10101 rev. 0 errata.
1776 * Errant SGE's when in narrow. Write bits 4 & 5 of
1777 * STEST1 register to disable SGE. We probably should do
1778 * that from SCRIPTS for each selection/reselection, but
1779 * I just don't want. :)
1781 if (pdev->device == PCI_DEVICE_ID_LSI_53C1010_33 &&
1782 pdev->revision < 1)
1783 OUTB(np, nc_stest1, INB(np, nc_stest1) | 0x30);
1786 * DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
1787 * Disable overlapped arbitration for some dual function devices,
1788 * regardless revision id (kind of post-chip-design feature. ;-))
1790 if (pdev->device == PCI_DEVICE_ID_NCR_53C875)
1791 OUTB(np, nc_ctest0, (1<<5));
1792 else if (pdev->device == PCI_DEVICE_ID_NCR_53C896)
1793 np->rv_ccntl0 |= DPR;
1796 * Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing
1797 * and/or hardware phase mismatch, since only such chips
1798 * seem to support those IO registers.
1800 if (np->features & (FE_DAC|FE_NOPM)) {
1801 OUTB(np, nc_ccntl0, np->rv_ccntl0);
1802 OUTB(np, nc_ccntl1, np->rv_ccntl1);
1805 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1807 * Set up scratch C and DRS IO registers to map the 32 bit
1808 * DMA address range our data structures are located in.
1810 if (use_dac(np)) {
1811 np->dmap_bah[0] = 0; /* ??? */
1812 OUTL(np, nc_scrx[0], np->dmap_bah[0]);
1813 OUTL(np, nc_drs, np->dmap_bah[0]);
1815 #endif
1818 * If phase mismatch handled by scripts (895A/896/1010),
1819 * set PM jump addresses.
1821 if (np->features & FE_NOPM) {
1822 OUTL(np, nc_pmjad1, SCRIPTB_BA(np, pm_handle));
1823 OUTL(np, nc_pmjad2, SCRIPTB_BA(np, pm_handle));
1827 * Enable GPIO0 pin for writing if LED support from SCRIPTS.
1828 * Also set GPIO5 and clear GPIO6 if hardware LED control.
1830 if (np->features & FE_LED0)
1831 OUTB(np, nc_gpcntl, INB(np, nc_gpcntl) & ~0x01);
1832 else if (np->features & FE_LEDC)
1833 OUTB(np, nc_gpcntl, (INB(np, nc_gpcntl) & ~0x41) | 0x20);
1836 * enable ints
1838 OUTW(np, nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
1839 OUTB(np, nc_dien , MDPE|BF|SSI|SIR|IID);
1842 * For 895/6 enable SBMC interrupt and save current SCSI bus mode.
1843 * Try to eat the spurious SBMC interrupt that may occur when
1844 * we reset the chip but not the SCSI BUS (at initialization).
1846 if (np->features & (FE_ULTRA2|FE_ULTRA3)) {
1847 OUTONW(np, nc_sien, SBMC);
1848 if (reason == 0) {
1849 INB(np, nc_mbox1);
1850 mdelay(100);
1851 INW(np, nc_sist);
1853 np->scsi_mode = INB(np, nc_stest4) & SMODE;
1857 * Fill in target structure.
1858 * Reinitialize usrsync.
1859 * Reinitialize usrwide.
1860 * Prepare sync negotiation according to actual SCSI bus mode.
1862 for (i=0;i<SYM_CONF_MAX_TARGET;i++) {
1863 struct sym_tcb *tp = &np->target[i];
1865 tp->to_reset = 0;
1866 tp->head.sval = 0;
1867 tp->head.wval = np->rv_scntl3;
1868 tp->head.uval = 0;
1869 if (tp->lun0p)
1870 tp->lun0p->to_clear = 0;
1871 if (tp->lunmp) {
1872 int ln;
1874 for (ln = 1; ln < SYM_CONF_MAX_LUN; ln++)
1875 if (tp->lunmp[ln])
1876 tp->lunmp[ln]->to_clear = 0;
1881 * Download SCSI SCRIPTS to on-chip RAM if present,
1882 * and start script processor.
1883 * We do the download preferently from the CPU.
1884 * For platforms that may not support PCI memory mapping,
1885 * we use simple SCRIPTS that performs MEMORY MOVEs.
1887 phys = SCRIPTA_BA(np, init);
1888 if (np->ram_ba) {
1889 if (sym_verbose >= 2)
1890 printf("%s: Downloading SCSI SCRIPTS.\n", sym_name(np));
1891 memcpy_toio(np->s.ramaddr, np->scripta0, np->scripta_sz);
1892 if (np->features & FE_RAM8K) {
1893 memcpy_toio(np->s.ramaddr + 4096, np->scriptb0, np->scriptb_sz);
1894 phys = scr_to_cpu(np->scr_ram_seg);
1895 OUTL(np, nc_mmws, phys);
1896 OUTL(np, nc_mmrs, phys);
1897 OUTL(np, nc_sfs, phys);
1898 phys = SCRIPTB_BA(np, start64);
1902 np->istat_sem = 0;
1904 OUTL(np, nc_dsa, np->hcb_ba);
1905 OUTL_DSP(np, phys);
1908 * Notify the XPT about the RESET condition.
1910 if (reason != 0)
1911 sym_xpt_async_bus_reset(np);
1915 * Switch trans mode for current job and its target.
1917 static void sym_settrans(struct sym_hcb *np, int target, u_char opts, u_char ofs,
1918 u_char per, u_char wide, u_char div, u_char fak)
1920 SYM_QUEHEAD *qp;
1921 u_char sval, wval, uval;
1922 struct sym_tcb *tp = &np->target[target];
1924 assert(target == (INB(np, nc_sdid) & 0x0f));
1926 sval = tp->head.sval;
1927 wval = tp->head.wval;
1928 uval = tp->head.uval;
1931 * Set the offset.
1933 if (!(np->features & FE_C10))
1934 sval = (sval & ~0x1f) | ofs;
1935 else
1936 sval = (sval & ~0x3f) | ofs;
1939 * Set the sync divisor and extra clock factor.
1941 if (ofs != 0) {
1942 wval = (wval & ~0x70) | ((div+1) << 4);
1943 if (!(np->features & FE_C10))
1944 sval = (sval & ~0xe0) | (fak << 5);
1945 else {
1946 uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT);
1947 if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT);
1948 if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT);
1953 * Set the bus width.
1955 wval = wval & ~EWS;
1956 if (wide != 0)
1957 wval |= EWS;
1960 * Set misc. ultra enable bits.
1962 if (np->features & FE_C10) {
1963 uval = uval & ~(U3EN|AIPCKEN);
1964 if (opts) {
1965 assert(np->features & FE_U3EN);
1966 uval |= U3EN;
1968 } else {
1969 wval = wval & ~ULTRA;
1970 if (per <= 12) wval |= ULTRA;
1974 * Stop there if sync parameters are unchanged.
1976 if (tp->head.sval == sval &&
1977 tp->head.wval == wval &&
1978 tp->head.uval == uval)
1979 return;
1980 tp->head.sval = sval;
1981 tp->head.wval = wval;
1982 tp->head.uval = uval;
1985 * Disable extended Sreq/Sack filtering if per < 50.
1986 * Not supported on the C1010.
1988 if (per < 50 && !(np->features & FE_C10))
1989 OUTOFFB(np, nc_stest2, EXT);
1992 * set actual value and sync_status
1994 OUTB(np, nc_sxfer, tp->head.sval);
1995 OUTB(np, nc_scntl3, tp->head.wval);
1997 if (np->features & FE_C10) {
1998 OUTB(np, nc_scntl4, tp->head.uval);
2002 * patch ALL busy ccbs of this target.
2004 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
2005 struct sym_ccb *cp;
2006 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
2007 if (cp->target != target)
2008 continue;
2009 cp->phys.select.sel_scntl3 = tp->head.wval;
2010 cp->phys.select.sel_sxfer = tp->head.sval;
2011 if (np->features & FE_C10) {
2012 cp->phys.select.sel_scntl4 = tp->head.uval;
2017 static void sym_announce_transfer_rate(struct sym_tcb *tp)
2019 struct scsi_target *starget = tp->starget;
2021 if (tp->tprint.period != spi_period(starget) ||
2022 tp->tprint.offset != spi_offset(starget) ||
2023 tp->tprint.width != spi_width(starget) ||
2024 tp->tprint.iu != spi_iu(starget) ||
2025 tp->tprint.dt != spi_dt(starget) ||
2026 tp->tprint.qas != spi_qas(starget) ||
2027 !tp->tprint.check_nego) {
2028 tp->tprint.period = spi_period(starget);
2029 tp->tprint.offset = spi_offset(starget);
2030 tp->tprint.width = spi_width(starget);
2031 tp->tprint.iu = spi_iu(starget);
2032 tp->tprint.dt = spi_dt(starget);
2033 tp->tprint.qas = spi_qas(starget);
2034 tp->tprint.check_nego = 1;
2036 spi_display_xfer_agreement(starget);
2041 * We received a WDTR.
2042 * Let everything be aware of the changes.
2044 static void sym_setwide(struct sym_hcb *np, int target, u_char wide)
2046 struct sym_tcb *tp = &np->target[target];
2047 struct scsi_target *starget = tp->starget;
2049 sym_settrans(np, target, 0, 0, 0, wide, 0, 0);
2051 if (wide)
2052 tp->tgoal.renego = NS_WIDE;
2053 else
2054 tp->tgoal.renego = 0;
2055 tp->tgoal.check_nego = 0;
2056 tp->tgoal.width = wide;
2057 spi_offset(starget) = 0;
2058 spi_period(starget) = 0;
2059 spi_width(starget) = wide;
2060 spi_iu(starget) = 0;
2061 spi_dt(starget) = 0;
2062 spi_qas(starget) = 0;
2064 if (sym_verbose >= 3)
2065 sym_announce_transfer_rate(tp);
2069 * We received a SDTR.
2070 * Let everything be aware of the changes.
2072 static void
2073 sym_setsync(struct sym_hcb *np, int target,
2074 u_char ofs, u_char per, u_char div, u_char fak)
2076 struct sym_tcb *tp = &np->target[target];
2077 struct scsi_target *starget = tp->starget;
2078 u_char wide = (tp->head.wval & EWS) ? BUS_16_BIT : BUS_8_BIT;
2080 sym_settrans(np, target, 0, ofs, per, wide, div, fak);
2082 if (wide)
2083 tp->tgoal.renego = NS_WIDE;
2084 else if (ofs)
2085 tp->tgoal.renego = NS_SYNC;
2086 else
2087 tp->tgoal.renego = 0;
2088 spi_period(starget) = per;
2089 spi_offset(starget) = ofs;
2090 spi_iu(starget) = spi_dt(starget) = spi_qas(starget) = 0;
2092 if (!tp->tgoal.dt && !tp->tgoal.iu && !tp->tgoal.qas) {
2093 tp->tgoal.period = per;
2094 tp->tgoal.offset = ofs;
2095 tp->tgoal.check_nego = 0;
2098 sym_announce_transfer_rate(tp);
2102 * We received a PPR.
2103 * Let everything be aware of the changes.
2105 static void
2106 sym_setpprot(struct sym_hcb *np, int target, u_char opts, u_char ofs,
2107 u_char per, u_char wide, u_char div, u_char fak)
2109 struct sym_tcb *tp = &np->target[target];
2110 struct scsi_target *starget = tp->starget;
2112 sym_settrans(np, target, opts, ofs, per, wide, div, fak);
2114 if (wide || ofs)
2115 tp->tgoal.renego = NS_PPR;
2116 else
2117 tp->tgoal.renego = 0;
2118 spi_width(starget) = tp->tgoal.width = wide;
2119 spi_period(starget) = tp->tgoal.period = per;
2120 spi_offset(starget) = tp->tgoal.offset = ofs;
2121 spi_iu(starget) = tp->tgoal.iu = !!(opts & PPR_OPT_IU);
2122 spi_dt(starget) = tp->tgoal.dt = !!(opts & PPR_OPT_DT);
2123 spi_qas(starget) = tp->tgoal.qas = !!(opts & PPR_OPT_QAS);
2124 tp->tgoal.check_nego = 0;
2126 sym_announce_transfer_rate(tp);
2130 * generic recovery from scsi interrupt
2132 * The doc says that when the chip gets an SCSI interrupt,
2133 * it tries to stop in an orderly fashion, by completing
2134 * an instruction fetch that had started or by flushing
2135 * the DMA fifo for a write to memory that was executing.
2136 * Such a fashion is not enough to know if the instruction
2137 * that was just before the current DSP value has been
2138 * executed or not.
2140 * There are some small SCRIPTS sections that deal with
2141 * the start queue and the done queue that may break any
2142 * assomption from the C code if we are interrupted
2143 * inside, so we reset if this happens. Btw, since these
2144 * SCRIPTS sections are executed while the SCRIPTS hasn't
2145 * started SCSI operations, it is very unlikely to happen.
2147 * All the driver data structures are supposed to be
2148 * allocated from the same 4 GB memory window, so there
2149 * is a 1 to 1 relationship between DSA and driver data
2150 * structures. Since we are careful :) to invalidate the
2151 * DSA when we complete a command or when the SCRIPTS
2152 * pushes a DSA into a queue, we can trust it when it
2153 * points to a CCB.
2155 static void sym_recover_scsi_int (struct sym_hcb *np, u_char hsts)
2157 u32 dsp = INL(np, nc_dsp);
2158 u32 dsa = INL(np, nc_dsa);
2159 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
2162 * If we haven't been interrupted inside the SCRIPTS
2163 * critical pathes, we can safely restart the SCRIPTS
2164 * and trust the DSA value if it matches a CCB.
2166 if ((!(dsp > SCRIPTA_BA(np, getjob_begin) &&
2167 dsp < SCRIPTA_BA(np, getjob_end) + 1)) &&
2168 (!(dsp > SCRIPTA_BA(np, ungetjob) &&
2169 dsp < SCRIPTA_BA(np, reselect) + 1)) &&
2170 (!(dsp > SCRIPTB_BA(np, sel_for_abort) &&
2171 dsp < SCRIPTB_BA(np, sel_for_abort_1) + 1)) &&
2172 (!(dsp > SCRIPTA_BA(np, done) &&
2173 dsp < SCRIPTA_BA(np, done_end) + 1))) {
2174 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
2175 OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
2177 * If we have a CCB, let the SCRIPTS call us back for
2178 * the handling of the error with SCRATCHA filled with
2179 * STARTPOS. This way, we will be able to freeze the
2180 * device queue and requeue awaiting IOs.
2182 if (cp) {
2183 cp->host_status = hsts;
2184 OUTL_DSP(np, SCRIPTA_BA(np, complete_error));
2187 * Otherwise just restart the SCRIPTS.
2189 else {
2190 OUTL(np, nc_dsa, 0xffffff);
2191 OUTL_DSP(np, SCRIPTA_BA(np, start));
2194 else
2195 goto reset_all;
2197 return;
2199 reset_all:
2200 sym_start_reset(np);
2204 * chip exception handler for selection timeout
2206 static void sym_int_sto (struct sym_hcb *np)
2208 u32 dsp = INL(np, nc_dsp);
2210 if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
2212 if (dsp == SCRIPTA_BA(np, wf_sel_done) + 8)
2213 sym_recover_scsi_int(np, HS_SEL_TIMEOUT);
2214 else
2215 sym_start_reset(np);
2219 * chip exception handler for unexpected disconnect
2221 static void sym_int_udc (struct sym_hcb *np)
2223 printf ("%s: unexpected disconnect\n", sym_name(np));
2224 sym_recover_scsi_int(np, HS_UNEXPECTED);
2228 * chip exception handler for SCSI bus mode change
2230 * spi2-r12 11.2.3 says a transceiver mode change must
2231 * generate a reset event and a device that detects a reset
2232 * event shall initiate a hard reset. It says also that a
2233 * device that detects a mode change shall set data transfer
2234 * mode to eight bit asynchronous, etc...
2235 * So, just reinitializing all except chip should be enough.
2237 static void sym_int_sbmc(struct Scsi_Host *shost)
2239 struct sym_hcb *np = sym_get_hcb(shost);
2240 u_char scsi_mode = INB(np, nc_stest4) & SMODE;
2243 * Notify user.
2245 printf("%s: SCSI BUS mode change from %s to %s.\n", sym_name(np),
2246 sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode));
2249 * Should suspend command processing for a few seconds and
2250 * reinitialize all except the chip.
2252 sym_start_up(shost, 2);
2256 * chip exception handler for SCSI parity error.
2258 * When the chip detects a SCSI parity error and is
2259 * currently executing a (CH)MOV instruction, it does
2260 * not interrupt immediately, but tries to finish the
2261 * transfer of the current scatter entry before
2262 * interrupting. The following situations may occur:
2264 * - The complete scatter entry has been transferred
2265 * without the device having changed phase.
2266 * The chip will then interrupt with the DSP pointing
2267 * to the instruction that follows the MOV.
2269 * - A phase mismatch occurs before the MOV finished
2270 * and phase errors are to be handled by the C code.
2271 * The chip will then interrupt with both PAR and MA
2272 * conditions set.
2274 * - A phase mismatch occurs before the MOV finished and
2275 * phase errors are to be handled by SCRIPTS.
2276 * The chip will load the DSP with the phase mismatch
2277 * JUMP address and interrupt the host processor.
2279 static void sym_int_par (struct sym_hcb *np, u_short sist)
2281 u_char hsts = INB(np, HS_PRT);
2282 u32 dsp = INL(np, nc_dsp);
2283 u32 dbc = INL(np, nc_dbc);
2284 u32 dsa = INL(np, nc_dsa);
2285 u_char sbcl = INB(np, nc_sbcl);
2286 u_char cmd = dbc >> 24;
2287 int phase = cmd & 7;
2288 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
2290 if (printk_ratelimit())
2291 printf("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n",
2292 sym_name(np), hsts, dbc, sbcl);
2295 * Check that the chip is connected to the SCSI BUS.
2297 if (!(INB(np, nc_scntl1) & ISCON)) {
2298 sym_recover_scsi_int(np, HS_UNEXPECTED);
2299 return;
2303 * If the nexus is not clearly identified, reset the bus.
2304 * We will try to do better later.
2306 if (!cp)
2307 goto reset_all;
2310 * Check instruction was a MOV, direction was INPUT and
2311 * ATN is asserted.
2313 if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
2314 goto reset_all;
2317 * Keep track of the parity error.
2319 OUTONB(np, HF_PRT, HF_EXT_ERR);
2320 cp->xerr_status |= XE_PARITY_ERR;
2323 * Prepare the message to send to the device.
2325 np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
2328 * If the old phase was DATA IN phase, we have to deal with
2329 * the 3 situations described above.
2330 * For other input phases (MSG IN and STATUS), the device
2331 * must resend the whole thing that failed parity checking
2332 * or signal error. So, jumping to dispatcher should be OK.
2334 if (phase == 1 || phase == 5) {
2335 /* Phase mismatch handled by SCRIPTS */
2336 if (dsp == SCRIPTB_BA(np, pm_handle))
2337 OUTL_DSP(np, dsp);
2338 /* Phase mismatch handled by the C code */
2339 else if (sist & MA)
2340 sym_int_ma (np);
2341 /* No phase mismatch occurred */
2342 else {
2343 sym_set_script_dp (np, cp, dsp);
2344 OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
2347 else if (phase == 7) /* We definitely cannot handle parity errors */
2348 goto reset_all; /* path and various message anticipations. */
2349 else
2350 OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
2351 return;
2353 reset_all:
2354 sym_start_reset(np);
2355 return;
2359 * chip exception handler for phase errors.
2361 * We have to construct a new transfer descriptor,
2362 * to transfer the rest of the current block.
2364 static void sym_int_ma (struct sym_hcb *np)
2366 u32 dbc;
2367 u32 rest;
2368 u32 dsp;
2369 u32 dsa;
2370 u32 nxtdsp;
2371 u32 *vdsp;
2372 u32 oadr, olen;
2373 u32 *tblp;
2374 u32 newcmd;
2375 u_int delta;
2376 u_char cmd;
2377 u_char hflags, hflags0;
2378 struct sym_pmc *pm;
2379 struct sym_ccb *cp;
2381 dsp = INL(np, nc_dsp);
2382 dbc = INL(np, nc_dbc);
2383 dsa = INL(np, nc_dsa);
2385 cmd = dbc >> 24;
2386 rest = dbc & 0xffffff;
2387 delta = 0;
2390 * locate matching cp if any.
2392 cp = sym_ccb_from_dsa(np, dsa);
2395 * Donnot take into account dma fifo and various buffers in
2396 * INPUT phase since the chip flushes everything before
2397 * raising the MA interrupt for interrupted INPUT phases.
2398 * For DATA IN phase, we will check for the SWIDE later.
2400 if ((cmd & 7) != 1 && (cmd & 7) != 5) {
2401 u_char ss0, ss2;
2403 if (np->features & FE_DFBC)
2404 delta = INW(np, nc_dfbc);
2405 else {
2406 u32 dfifo;
2409 * Read DFIFO, CTEST[4-6] using 1 PCI bus ownership.
2411 dfifo = INL(np, nc_dfifo);
2414 * Calculate remaining bytes in DMA fifo.
2415 * (CTEST5 = dfifo >> 16)
2417 if (dfifo & (DFS << 16))
2418 delta = ((((dfifo >> 8) & 0x300) |
2419 (dfifo & 0xff)) - rest) & 0x3ff;
2420 else
2421 delta = ((dfifo & 0xff) - rest) & 0x7f;
2425 * The data in the dma fifo has not been transfered to
2426 * the target -> add the amount to the rest
2427 * and clear the data.
2428 * Check the sstat2 register in case of wide transfer.
2430 rest += delta;
2431 ss0 = INB(np, nc_sstat0);
2432 if (ss0 & OLF) rest++;
2433 if (!(np->features & FE_C10))
2434 if (ss0 & ORF) rest++;
2435 if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
2436 ss2 = INB(np, nc_sstat2);
2437 if (ss2 & OLF1) rest++;
2438 if (!(np->features & FE_C10))
2439 if (ss2 & ORF1) rest++;
2443 * Clear fifos.
2445 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */
2446 OUTB(np, nc_stest3, TE|CSF); /* scsi fifo */
2450 * log the information
2452 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
2453 printf ("P%x%x RL=%d D=%d ", cmd&7, INB(np, nc_sbcl)&7,
2454 (unsigned) rest, (unsigned) delta);
2457 * try to find the interrupted script command,
2458 * and the address at which to continue.
2460 vdsp = NULL;
2461 nxtdsp = 0;
2462 if (dsp > np->scripta_ba &&
2463 dsp <= np->scripta_ba + np->scripta_sz) {
2464 vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8));
2465 nxtdsp = dsp;
2467 else if (dsp > np->scriptb_ba &&
2468 dsp <= np->scriptb_ba + np->scriptb_sz) {
2469 vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8));
2470 nxtdsp = dsp;
2474 * log the information
2476 if (DEBUG_FLAGS & DEBUG_PHASE) {
2477 printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
2478 cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
2481 if (!vdsp) {
2482 printf ("%s: interrupted SCRIPT address not found.\n",
2483 sym_name (np));
2484 goto reset_all;
2487 if (!cp) {
2488 printf ("%s: SCSI phase error fixup: CCB already dequeued.\n",
2489 sym_name (np));
2490 goto reset_all;
2494 * get old startaddress and old length.
2496 oadr = scr_to_cpu(vdsp[1]);
2498 if (cmd & 0x10) { /* Table indirect */
2499 tblp = (u32 *) ((char*) &cp->phys + oadr);
2500 olen = scr_to_cpu(tblp[0]);
2501 oadr = scr_to_cpu(tblp[1]);
2502 } else {
2503 tblp = (u32 *) 0;
2504 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
2507 if (DEBUG_FLAGS & DEBUG_PHASE) {
2508 printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
2509 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
2510 tblp,
2511 (unsigned) olen,
2512 (unsigned) oadr);
2516 * check cmd against assumed interrupted script command.
2517 * If dt data phase, the MOVE instruction hasn't bit 4 of
2518 * the phase.
2520 if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
2521 sym_print_addr(cp->cmd,
2522 "internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
2523 cmd, scr_to_cpu(vdsp[0]) >> 24);
2525 goto reset_all;
2529 * if old phase not dataphase, leave here.
2531 if (cmd & 2) {
2532 sym_print_addr(cp->cmd,
2533 "phase change %x-%x %d@%08x resid=%d.\n",
2534 cmd&7, INB(np, nc_sbcl)&7, (unsigned)olen,
2535 (unsigned)oadr, (unsigned)rest);
2536 goto unexpected_phase;
2540 * Choose the correct PM save area.
2542 * Look at the PM_SAVE SCRIPT if you want to understand
2543 * this stuff. The equivalent code is implemented in
2544 * SCRIPTS for the 895A, 896 and 1010 that are able to
2545 * handle PM from the SCRIPTS processor.
2547 hflags0 = INB(np, HF_PRT);
2548 hflags = hflags0;
2550 if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
2551 if (hflags & HF_IN_PM0)
2552 nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
2553 else if (hflags & HF_IN_PM1)
2554 nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
2556 if (hflags & HF_DP_SAVED)
2557 hflags ^= HF_ACT_PM;
2560 if (!(hflags & HF_ACT_PM)) {
2561 pm = &cp->phys.pm0;
2562 newcmd = SCRIPTA_BA(np, pm0_data);
2564 else {
2565 pm = &cp->phys.pm1;
2566 newcmd = SCRIPTA_BA(np, pm1_data);
2569 hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
2570 if (hflags != hflags0)
2571 OUTB(np, HF_PRT, hflags);
2574 * fillin the phase mismatch context
2576 pm->sg.addr = cpu_to_scr(oadr + olen - rest);
2577 pm->sg.size = cpu_to_scr(rest);
2578 pm->ret = cpu_to_scr(nxtdsp);
2581 * If we have a SWIDE,
2582 * - prepare the address to write the SWIDE from SCRIPTS,
2583 * - compute the SCRIPTS address to restart from,
2584 * - move current data pointer context by one byte.
2586 nxtdsp = SCRIPTA_BA(np, dispatch);
2587 if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
2588 (INB(np, nc_scntl2) & WSR)) {
2589 u32 tmp;
2592 * Set up the table indirect for the MOVE
2593 * of the residual byte and adjust the data
2594 * pointer context.
2596 tmp = scr_to_cpu(pm->sg.addr);
2597 cp->phys.wresid.addr = cpu_to_scr(tmp);
2598 pm->sg.addr = cpu_to_scr(tmp + 1);
2599 tmp = scr_to_cpu(pm->sg.size);
2600 cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
2601 pm->sg.size = cpu_to_scr(tmp - 1);
2604 * If only the residual byte is to be moved,
2605 * no PM context is needed.
2607 if ((tmp&0xffffff) == 1)
2608 newcmd = pm->ret;
2611 * Prepare the address of SCRIPTS that will
2612 * move the residual byte to memory.
2614 nxtdsp = SCRIPTB_BA(np, wsr_ma_helper);
2617 if (DEBUG_FLAGS & DEBUG_PHASE) {
2618 sym_print_addr(cp->cmd, "PM %x %x %x / %x %x %x.\n",
2619 hflags0, hflags, newcmd,
2620 (unsigned)scr_to_cpu(pm->sg.addr),
2621 (unsigned)scr_to_cpu(pm->sg.size),
2622 (unsigned)scr_to_cpu(pm->ret));
2626 * Restart the SCRIPTS processor.
2628 sym_set_script_dp (np, cp, newcmd);
2629 OUTL_DSP(np, nxtdsp);
2630 return;
2633 * Unexpected phase changes that occurs when the current phase
2634 * is not a DATA IN or DATA OUT phase are due to error conditions.
2635 * Such event may only happen when the SCRIPTS is using a
2636 * multibyte SCSI MOVE.
2638 * Phase change Some possible cause
2640 * COMMAND --> MSG IN SCSI parity error detected by target.
2641 * COMMAND --> STATUS Bad command or refused by target.
2642 * MSG OUT --> MSG IN Message rejected by target.
2643 * MSG OUT --> COMMAND Bogus target that discards extended
2644 * negotiation messages.
2646 * The code below does not care of the new phase and so
2647 * trusts the target. Why to annoy it ?
2648 * If the interrupted phase is COMMAND phase, we restart at
2649 * dispatcher.
2650 * If a target does not get all the messages after selection,
2651 * the code assumes blindly that the target discards extended
2652 * messages and clears the negotiation status.
2653 * If the target does not want all our response to negotiation,
2654 * we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
2655 * bloat for such a should_not_happen situation).
2656 * In all other situation, we reset the BUS.
2657 * Are these assumptions reasonable ? (Wait and see ...)
2659 unexpected_phase:
2660 dsp -= 8;
2661 nxtdsp = 0;
2663 switch (cmd & 7) {
2664 case 2: /* COMMAND phase */
2665 nxtdsp = SCRIPTA_BA(np, dispatch);
2666 break;
2667 case 6: /* MSG OUT phase */
2669 * If the device may want to use untagged when we want
2670 * tagged, we prepare an IDENTIFY without disc. granted,
2671 * since we will not be able to handle reselect.
2672 * Otherwise, we just don't care.
2674 if (dsp == SCRIPTA_BA(np, send_ident)) {
2675 if (cp->tag != NO_TAG && olen - rest <= 3) {
2676 cp->host_status = HS_BUSY;
2677 np->msgout[0] = IDENTIFY(0, cp->lun);
2678 nxtdsp = SCRIPTB_BA(np, ident_break_atn);
2680 else
2681 nxtdsp = SCRIPTB_BA(np, ident_break);
2683 else if (dsp == SCRIPTB_BA(np, send_wdtr) ||
2684 dsp == SCRIPTB_BA(np, send_sdtr) ||
2685 dsp == SCRIPTB_BA(np, send_ppr)) {
2686 nxtdsp = SCRIPTB_BA(np, nego_bad_phase);
2687 if (dsp == SCRIPTB_BA(np, send_ppr)) {
2688 struct scsi_device *dev = cp->cmd->device;
2689 dev->ppr = 0;
2692 break;
2695 if (nxtdsp) {
2696 OUTL_DSP(np, nxtdsp);
2697 return;
2700 reset_all:
2701 sym_start_reset(np);
2705 * chip interrupt handler
2707 * In normal situations, interrupt conditions occur one at
2708 * a time. But when something bad happens on the SCSI BUS,
2709 * the chip may raise several interrupt flags before
2710 * stopping and interrupting the CPU. The additionnal
2711 * interrupt flags are stacked in some extra registers
2712 * after the SIP and/or DIP flag has been raised in the
2713 * ISTAT. After the CPU has read the interrupt condition
2714 * flag from SIST or DSTAT, the chip unstacks the other
2715 * interrupt flags and sets the corresponding bits in
2716 * SIST or DSTAT. Since the chip starts stacking once the
2717 * SIP or DIP flag is set, there is a small window of time
2718 * where the stacking does not occur.
2720 * Typically, multiple interrupt conditions may happen in
2721 * the following situations:
2723 * - SCSI parity error + Phase mismatch (PAR|MA)
2724 * When an parity error is detected in input phase
2725 * and the device switches to msg-in phase inside a
2726 * block MOV.
2727 * - SCSI parity error + Unexpected disconnect (PAR|UDC)
2728 * When a stupid device does not want to handle the
2729 * recovery of an SCSI parity error.
2730 * - Some combinations of STO, PAR, UDC, ...
2731 * When using non compliant SCSI stuff, when user is
2732 * doing non compliant hot tampering on the BUS, when
2733 * something really bad happens to a device, etc ...
2735 * The heuristic suggested by SYMBIOS to handle
2736 * multiple interrupts is to try unstacking all
2737 * interrupts conditions and to handle them on some
2738 * priority based on error severity.
2739 * This will work when the unstacking has been
2740 * successful, but we cannot be 100 % sure of that,
2741 * since the CPU may have been faster to unstack than
2742 * the chip is able to stack. Hmmm ... But it seems that
2743 * such a situation is very unlikely to happen.
2745 * If this happen, for example STO caught by the CPU
2746 * then UDC happenning before the CPU have restarted
2747 * the SCRIPTS, the driver may wrongly complete the
2748 * same command on UDC, since the SCRIPTS didn't restart
2749 * and the DSA still points to the same command.
2750 * We avoid this situation by setting the DSA to an
2751 * invalid value when the CCB is completed and before
2752 * restarting the SCRIPTS.
2754 * Another issue is that we need some section of our
2755 * recovery procedures to be somehow uninterruptible but
2756 * the SCRIPTS processor does not provides such a
2757 * feature. For this reason, we handle recovery preferently
2758 * from the C code and check against some SCRIPTS critical
2759 * sections from the C code.
2761 * Hopefully, the interrupt handling of the driver is now
2762 * able to resist to weird BUS error conditions, but donnot
2763 * ask me for any guarantee that it will never fail. :-)
2764 * Use at your own decision and risk.
2767 irqreturn_t sym_interrupt(struct Scsi_Host *shost)
2769 struct sym_data *sym_data = shost_priv(shost);
2770 struct sym_hcb *np = sym_data->ncb;
2771 struct pci_dev *pdev = sym_data->pdev;
2772 u_char istat, istatc;
2773 u_char dstat;
2774 u_short sist;
2777 * interrupt on the fly ?
2778 * (SCRIPTS may still be running)
2780 * A `dummy read' is needed to ensure that the
2781 * clear of the INTF flag reaches the device
2782 * and that posted writes are flushed to memory
2783 * before the scanning of the DONE queue.
2784 * Note that SCRIPTS also (dummy) read to memory
2785 * prior to deliver the INTF interrupt condition.
2787 istat = INB(np, nc_istat);
2788 if (istat & INTF) {
2789 OUTB(np, nc_istat, (istat & SIGP) | INTF | np->istat_sem);
2790 istat |= INB(np, nc_istat); /* DUMMY READ */
2791 if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
2792 sym_wakeup_done(np);
2795 if (!(istat & (SIP|DIP)))
2796 return (istat & INTF) ? IRQ_HANDLED : IRQ_NONE;
2800 * PAR and MA interrupts may occur at the same time,
2801 * and we need to know of both in order to handle
2802 * this situation properly. We try to unstack SCSI
2803 * interrupts for that reason. BTW, I dislike a LOT
2804 * such a loop inside the interrupt routine.
2805 * Even if DMA interrupt stacking is very unlikely to
2806 * happen, we also try unstacking these ones, since
2807 * this has no performance impact.
2809 sist = 0;
2810 dstat = 0;
2811 istatc = istat;
2812 do {
2813 if (istatc & SIP)
2814 sist |= INW(np, nc_sist);
2815 if (istatc & DIP)
2816 dstat |= INB(np, nc_dstat);
2817 istatc = INB(np, nc_istat);
2818 istat |= istatc;
2820 /* Prevent deadlock waiting on a condition that may
2821 * never clear. */
2822 if (unlikely(sist == 0xffff && dstat == 0xff)) {
2823 if (pci_channel_offline(pdev))
2824 return IRQ_NONE;
2826 } while (istatc & (SIP|DIP));
2828 if (DEBUG_FLAGS & DEBUG_TINY)
2829 printf ("<%d|%x:%x|%x:%x>",
2830 (int)INB(np, nc_scr0),
2831 dstat,sist,
2832 (unsigned)INL(np, nc_dsp),
2833 (unsigned)INL(np, nc_dbc));
2835 * On paper, a memory read barrier may be needed here to
2836 * prevent out of order LOADs by the CPU from having
2837 * prefetched stale data prior to DMA having occurred.
2838 * And since we are paranoid ... :)
2840 MEMORY_READ_BARRIER();
2843 * First, interrupts we want to service cleanly.
2845 * Phase mismatch (MA) is the most frequent interrupt
2846 * for chip earlier than the 896 and so we have to service
2847 * it as quickly as possible.
2848 * A SCSI parity error (PAR) may be combined with a phase
2849 * mismatch condition (MA).
2850 * Programmed interrupts (SIR) are used to call the C code
2851 * from SCRIPTS.
2852 * The single step interrupt (SSI) is not used in this
2853 * driver.
2855 if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
2856 !(dstat & (MDPE|BF|ABRT|IID))) {
2857 if (sist & PAR) sym_int_par (np, sist);
2858 else if (sist & MA) sym_int_ma (np);
2859 else if (dstat & SIR) sym_int_sir(np);
2860 else if (dstat & SSI) OUTONB_STD();
2861 else goto unknown_int;
2862 return IRQ_HANDLED;
2866 * Now, interrupts that donnot happen in normal
2867 * situations and that we may need to recover from.
2869 * On SCSI RESET (RST), we reset everything.
2870 * On SCSI BUS MODE CHANGE (SBMC), we complete all
2871 * active CCBs with RESET status, prepare all devices
2872 * for negotiating again and restart the SCRIPTS.
2873 * On STO and UDC, we complete the CCB with the corres-
2874 * ponding status and restart the SCRIPTS.
2876 if (sist & RST) {
2877 printf("%s: SCSI BUS reset detected.\n", sym_name(np));
2878 sym_start_up(shost, 1);
2879 return IRQ_HANDLED;
2882 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
2883 OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
2885 if (!(sist & (GEN|HTH|SGE)) &&
2886 !(dstat & (MDPE|BF|ABRT|IID))) {
2887 if (sist & SBMC) sym_int_sbmc(shost);
2888 else if (sist & STO) sym_int_sto (np);
2889 else if (sist & UDC) sym_int_udc (np);
2890 else goto unknown_int;
2891 return IRQ_HANDLED;
2895 * Now, interrupts we are not able to recover cleanly.
2897 * Log message for hard errors.
2898 * Reset everything.
2901 sym_log_hard_error(shost, sist, dstat);
2903 if ((sist & (GEN|HTH|SGE)) ||
2904 (dstat & (MDPE|BF|ABRT|IID))) {
2905 sym_start_reset(np);
2906 return IRQ_HANDLED;
2909 unknown_int:
2911 * We just miss the cause of the interrupt. :(
2912 * Print a message. The timeout will do the real work.
2914 printf( "%s: unknown interrupt(s) ignored, "
2915 "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
2916 sym_name(np), istat, dstat, sist);
2917 return IRQ_NONE;
2921 * Dequeue from the START queue all CCBs that match
2922 * a given target/lun/task condition (-1 means all),
2923 * and move them from the BUSY queue to the COMP queue
2924 * with DID_SOFT_ERROR status condition.
2925 * This function is used during error handling/recovery.
2926 * It is called with SCRIPTS not running.
2928 static int
2929 sym_dequeue_from_squeue(struct sym_hcb *np, int i, int target, int lun, int task)
2931 int j;
2932 struct sym_ccb *cp;
2935 * Make sure the starting index is within range.
2937 assert((i >= 0) && (i < 2*MAX_QUEUE));
2940 * Walk until end of START queue and dequeue every job
2941 * that matches the target/lun/task condition.
2943 j = i;
2944 while (i != np->squeueput) {
2945 cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
2946 assert(cp);
2947 #ifdef SYM_CONF_IARB_SUPPORT
2948 /* Forget hints for IARB, they may be no longer relevant */
2949 cp->host_flags &= ~HF_HINT_IARB;
2950 #endif
2951 if ((target == -1 || cp->target == target) &&
2952 (lun == -1 || cp->lun == lun) &&
2953 (task == -1 || cp->tag == task)) {
2954 sym_set_cam_status(cp->cmd, DID_SOFT_ERROR);
2955 sym_remque(&cp->link_ccbq);
2956 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
2958 else {
2959 if (i != j)
2960 np->squeue[j] = np->squeue[i];
2961 if ((j += 2) >= MAX_QUEUE*2) j = 0;
2963 if ((i += 2) >= MAX_QUEUE*2) i = 0;
2965 if (i != j) /* Copy back the idle task if needed */
2966 np->squeue[j] = np->squeue[i];
2967 np->squeueput = j; /* Update our current start queue pointer */
2969 return (i - j) / 2;
2973 * chip handler for bad SCSI status condition
2975 * In case of bad SCSI status, we unqueue all the tasks
2976 * currently queued to the controller but not yet started
2977 * and then restart the SCRIPTS processor immediately.
2979 * QUEUE FULL and BUSY conditions are handled the same way.
2980 * Basically all the not yet started tasks are requeued in
2981 * device queue and the queue is frozen until a completion.
2983 * For CHECK CONDITION and COMMAND TERMINATED status, we use
2984 * the CCB of the failed command to prepare a REQUEST SENSE
2985 * SCSI command and queue it to the controller queue.
2987 * SCRATCHA is assumed to have been loaded with STARTPOS
2988 * before the SCRIPTS called the C code.
2990 static void sym_sir_bad_scsi_status(struct sym_hcb *np, int num, struct sym_ccb *cp)
2992 u32 startp;
2993 u_char s_status = cp->ssss_status;
2994 u_char h_flags = cp->host_flags;
2995 int msglen;
2996 int i;
2999 * Compute the index of the next job to start from SCRIPTS.
3001 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
3004 * The last CCB queued used for IARB hint may be
3005 * no longer relevant. Forget it.
3007 #ifdef SYM_CONF_IARB_SUPPORT
3008 if (np->last_cp)
3009 np->last_cp = 0;
3010 #endif
3013 * Now deal with the SCSI status.
3015 switch(s_status) {
3016 case S_BUSY:
3017 case S_QUEUE_FULL:
3018 if (sym_verbose >= 2) {
3019 sym_print_addr(cp->cmd, "%s\n",
3020 s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
3022 default: /* S_INT, S_INT_COND_MET, S_CONFLICT */
3023 sym_complete_error (np, cp);
3024 break;
3025 case S_TERMINATED:
3026 case S_CHECK_COND:
3028 * If we get an SCSI error when requesting sense, give up.
3030 if (h_flags & HF_SENSE) {
3031 sym_complete_error (np, cp);
3032 break;
3036 * Dequeue all queued CCBs for that device not yet started,
3037 * and restart the SCRIPTS processor immediately.
3039 sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
3040 OUTL_DSP(np, SCRIPTA_BA(np, start));
3043 * Save some info of the actual IO.
3044 * Compute the data residual.
3046 cp->sv_scsi_status = cp->ssss_status;
3047 cp->sv_xerr_status = cp->xerr_status;
3048 cp->sv_resid = sym_compute_residual(np, cp);
3051 * Prepare all needed data structures for
3052 * requesting sense data.
3055 cp->scsi_smsg2[0] = IDENTIFY(0, cp->lun);
3056 msglen = 1;
3059 * If we are currently using anything different from
3060 * async. 8 bit data transfers with that target,
3061 * start a negotiation, since the device may want
3062 * to report us a UNIT ATTENTION condition due to
3063 * a cause we currently ignore, and we donnot want
3064 * to be stuck with WIDE and/or SYNC data transfer.
3066 * cp->nego_status is filled by sym_prepare_nego().
3068 cp->nego_status = 0;
3069 msglen += sym_prepare_nego(np, cp, &cp->scsi_smsg2[msglen]);
3071 * Message table indirect structure.
3073 cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg2);
3074 cp->phys.smsg.size = cpu_to_scr(msglen);
3077 * sense command
3079 cp->phys.cmd.addr = CCB_BA(cp, sensecmd);
3080 cp->phys.cmd.size = cpu_to_scr(6);
3083 * patch requested size into sense command
3085 cp->sensecmd[0] = REQUEST_SENSE;
3086 cp->sensecmd[1] = 0;
3087 if (cp->cmd->device->scsi_level <= SCSI_2 && cp->lun <= 7)
3088 cp->sensecmd[1] = cp->lun << 5;
3089 cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
3090 cp->data_len = SYM_SNS_BBUF_LEN;
3093 * sense data
3095 memset(cp->sns_bbuf, 0, SYM_SNS_BBUF_LEN);
3096 cp->phys.sense.addr = CCB_BA(cp, sns_bbuf);
3097 cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
3100 * requeue the command.
3102 startp = SCRIPTB_BA(np, sdata_in);
3104 cp->phys.head.savep = cpu_to_scr(startp);
3105 cp->phys.head.lastp = cpu_to_scr(startp);
3106 cp->startp = cpu_to_scr(startp);
3107 cp->goalp = cpu_to_scr(startp + 16);
3109 cp->host_xflags = 0;
3110 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
3111 cp->ssss_status = S_ILLEGAL;
3112 cp->host_flags = (HF_SENSE|HF_DATA_IN);
3113 cp->xerr_status = 0;
3114 cp->extra_bytes = 0;
3116 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
3119 * Requeue the command.
3121 sym_put_start_queue(np, cp);
3124 * Give back to upper layer everything we have dequeued.
3126 sym_flush_comp_queue(np, 0);
3127 break;
3132 * After a device has accepted some management message
3133 * as BUS DEVICE RESET, ABORT TASK, etc ..., or when
3134 * a device signals a UNIT ATTENTION condition, some
3135 * tasks are thrown away by the device. We are required
3136 * to reflect that on our tasks list since the device
3137 * will never complete these tasks.
3139 * This function move from the BUSY queue to the COMP
3140 * queue all disconnected CCBs for a given target that
3141 * match the following criteria:
3142 * - lun=-1 means any logical UNIT otherwise a given one.
3143 * - task=-1 means any task, otherwise a given one.
3145 int sym_clear_tasks(struct sym_hcb *np, int cam_status, int target, int lun, int task)
3147 SYM_QUEHEAD qtmp, *qp;
3148 int i = 0;
3149 struct sym_ccb *cp;
3152 * Move the entire BUSY queue to our temporary queue.
3154 sym_que_init(&qtmp);
3155 sym_que_splice(&np->busy_ccbq, &qtmp);
3156 sym_que_init(&np->busy_ccbq);
3159 * Put all CCBs that matches our criteria into
3160 * the COMP queue and put back other ones into
3161 * the BUSY queue.
3163 while ((qp = sym_remque_head(&qtmp)) != NULL) {
3164 struct scsi_cmnd *cmd;
3165 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3166 cmd = cp->cmd;
3167 if (cp->host_status != HS_DISCONNECT ||
3168 cp->target != target ||
3169 (lun != -1 && cp->lun != lun) ||
3170 (task != -1 &&
3171 (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
3172 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
3173 continue;
3175 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3177 /* Preserve the software timeout condition */
3178 if (sym_get_cam_status(cmd) != DID_TIME_OUT)
3179 sym_set_cam_status(cmd, cam_status);
3180 ++i;
3182 return i;
3186 * chip handler for TASKS recovery
3188 * We cannot safely abort a command, while the SCRIPTS
3189 * processor is running, since we just would be in race
3190 * with it.
3192 * As long as we have tasks to abort, we keep the SEM
3193 * bit set in the ISTAT. When this bit is set, the
3194 * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
3195 * each time it enters the scheduler.
3197 * If we have to reset a target, clear tasks of a unit,
3198 * or to perform the abort of a disconnected job, we
3199 * restart the SCRIPTS for selecting the target. Once
3200 * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
3201 * If it loses arbitration, the SCRIPTS will interrupt again
3202 * the next time it will enter its scheduler, and so on ...
3204 * On SIR_TARGET_SELECTED, we scan for the more
3205 * appropriate thing to do:
3207 * - If nothing, we just sent a M_ABORT message to the
3208 * target to get rid of the useless SCSI bus ownership.
3209 * According to the specs, no tasks shall be affected.
3210 * - If the target is to be reset, we send it a M_RESET
3211 * message.
3212 * - If a logical UNIT is to be cleared , we send the
3213 * IDENTIFY(lun) + M_ABORT.
3214 * - If an untagged task is to be aborted, we send the
3215 * IDENTIFY(lun) + M_ABORT.
3216 * - If a tagged task is to be aborted, we send the
3217 * IDENTIFY(lun) + task attributes + M_ABORT_TAG.
3219 * Once our 'kiss of death' :) message has been accepted
3220 * by the target, the SCRIPTS interrupts again
3221 * (SIR_ABORT_SENT). On this interrupt, we complete
3222 * all the CCBs that should have been aborted by the
3223 * target according to our message.
3225 static void sym_sir_task_recovery(struct sym_hcb *np, int num)
3227 SYM_QUEHEAD *qp;
3228 struct sym_ccb *cp;
3229 struct sym_tcb *tp = NULL; /* gcc isn't quite smart enough yet */
3230 struct scsi_target *starget;
3231 int target=-1, lun=-1, task;
3232 int i, k;
3234 switch(num) {
3236 * The SCRIPTS processor stopped before starting
3237 * the next command in order to allow us to perform
3238 * some task recovery.
3240 case SIR_SCRIPT_STOPPED:
3242 * Do we have any target to reset or unit to clear ?
3244 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
3245 tp = &np->target[i];
3246 if (tp->to_reset ||
3247 (tp->lun0p && tp->lun0p->to_clear)) {
3248 target = i;
3249 break;
3251 if (!tp->lunmp)
3252 continue;
3253 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
3254 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
3255 target = i;
3256 break;
3259 if (target != -1)
3260 break;
3264 * If not, walk the busy queue for any
3265 * disconnected CCB to be aborted.
3267 if (target == -1) {
3268 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3269 cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
3270 if (cp->host_status != HS_DISCONNECT)
3271 continue;
3272 if (cp->to_abort) {
3273 target = cp->target;
3274 break;
3280 * If some target is to be selected,
3281 * prepare and start the selection.
3283 if (target != -1) {
3284 tp = &np->target[target];
3285 np->abrt_sel.sel_id = target;
3286 np->abrt_sel.sel_scntl3 = tp->head.wval;
3287 np->abrt_sel.sel_sxfer = tp->head.sval;
3288 OUTL(np, nc_dsa, np->hcb_ba);
3289 OUTL_DSP(np, SCRIPTB_BA(np, sel_for_abort));
3290 return;
3294 * Now look for a CCB to abort that haven't started yet.
3295 * Btw, the SCRIPTS processor is still stopped, so
3296 * we are not in race.
3298 i = 0;
3299 cp = NULL;
3300 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3301 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3302 if (cp->host_status != HS_BUSY &&
3303 cp->host_status != HS_NEGOTIATE)
3304 continue;
3305 if (!cp->to_abort)
3306 continue;
3307 #ifdef SYM_CONF_IARB_SUPPORT
3309 * If we are using IMMEDIATE ARBITRATION, we donnot
3310 * want to cancel the last queued CCB, since the
3311 * SCRIPTS may have anticipated the selection.
3313 if (cp == np->last_cp) {
3314 cp->to_abort = 0;
3315 continue;
3317 #endif
3318 i = 1; /* Means we have found some */
3319 break;
3321 if (!i) {
3323 * We are done, so we donnot need
3324 * to synchronize with the SCRIPTS anylonger.
3325 * Remove the SEM flag from the ISTAT.
3327 np->istat_sem = 0;
3328 OUTB(np, nc_istat, SIGP);
3329 break;
3332 * Compute index of next position in the start
3333 * queue the SCRIPTS intends to start and dequeue
3334 * all CCBs for that device that haven't been started.
3336 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
3337 i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
3340 * Make sure at least our IO to abort has been dequeued.
3342 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
3343 assert(i && sym_get_cam_status(cp->cmd) == DID_SOFT_ERROR);
3344 #else
3345 sym_remque(&cp->link_ccbq);
3346 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3347 #endif
3349 * Keep track in cam status of the reason of the abort.
3351 if (cp->to_abort == 2)
3352 sym_set_cam_status(cp->cmd, DID_TIME_OUT);
3353 else
3354 sym_set_cam_status(cp->cmd, DID_ABORT);
3357 * Complete with error everything that we have dequeued.
3359 sym_flush_comp_queue(np, 0);
3360 break;
3362 * The SCRIPTS processor has selected a target
3363 * we may have some manual recovery to perform for.
3365 case SIR_TARGET_SELECTED:
3366 target = INB(np, nc_sdid) & 0xf;
3367 tp = &np->target[target];
3369 np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
3372 * If the target is to be reset, prepare a
3373 * M_RESET message and clear the to_reset flag
3374 * since we donnot expect this operation to fail.
3376 if (tp->to_reset) {
3377 np->abrt_msg[0] = M_RESET;
3378 np->abrt_tbl.size = 1;
3379 tp->to_reset = 0;
3380 break;
3384 * Otherwise, look for some logical unit to be cleared.
3386 if (tp->lun0p && tp->lun0p->to_clear)
3387 lun = 0;
3388 else if (tp->lunmp) {
3389 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
3390 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
3391 lun = k;
3392 break;
3398 * If a logical unit is to be cleared, prepare
3399 * an IDENTIFY(lun) + ABORT MESSAGE.
3401 if (lun != -1) {
3402 struct sym_lcb *lp = sym_lp(tp, lun);
3403 lp->to_clear = 0; /* We don't expect to fail here */
3404 np->abrt_msg[0] = IDENTIFY(0, lun);
3405 np->abrt_msg[1] = M_ABORT;
3406 np->abrt_tbl.size = 2;
3407 break;
3411 * Otherwise, look for some disconnected job to
3412 * abort for this target.
3414 i = 0;
3415 cp = NULL;
3416 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3417 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3418 if (cp->host_status != HS_DISCONNECT)
3419 continue;
3420 if (cp->target != target)
3421 continue;
3422 if (!cp->to_abort)
3423 continue;
3424 i = 1; /* Means we have some */
3425 break;
3429 * If we have none, probably since the device has
3430 * completed the command before we won abitration,
3431 * send a M_ABORT message without IDENTIFY.
3432 * According to the specs, the device must just
3433 * disconnect the BUS and not abort any task.
3435 if (!i) {
3436 np->abrt_msg[0] = M_ABORT;
3437 np->abrt_tbl.size = 1;
3438 break;
3442 * We have some task to abort.
3443 * Set the IDENTIFY(lun)
3445 np->abrt_msg[0] = IDENTIFY(0, cp->lun);
3448 * If we want to abort an untagged command, we
3449 * will send a IDENTIFY + M_ABORT.
3450 * Otherwise (tagged command), we will send
3451 * a IDENTITFY + task attributes + ABORT TAG.
3453 if (cp->tag == NO_TAG) {
3454 np->abrt_msg[1] = M_ABORT;
3455 np->abrt_tbl.size = 2;
3456 } else {
3457 np->abrt_msg[1] = cp->scsi_smsg[1];
3458 np->abrt_msg[2] = cp->scsi_smsg[2];
3459 np->abrt_msg[3] = M_ABORT_TAG;
3460 np->abrt_tbl.size = 4;
3463 * Keep track of software timeout condition, since the
3464 * peripheral driver may not count retries on abort
3465 * conditions not due to timeout.
3467 if (cp->to_abort == 2)
3468 sym_set_cam_status(cp->cmd, DID_TIME_OUT);
3469 cp->to_abort = 0; /* We donnot expect to fail here */
3470 break;
3473 * The target has accepted our message and switched
3474 * to BUS FREE phase as we expected.
3476 case SIR_ABORT_SENT:
3477 target = INB(np, nc_sdid) & 0xf;
3478 tp = &np->target[target];
3479 starget = tp->starget;
3482 ** If we didn't abort anything, leave here.
3484 if (np->abrt_msg[0] == M_ABORT)
3485 break;
3488 * If we sent a M_RESET, then a hardware reset has
3489 * been performed by the target.
3490 * - Reset everything to async 8 bit
3491 * - Tell ourself to negotiate next time :-)
3492 * - Prepare to clear all disconnected CCBs for
3493 * this target from our task list (lun=task=-1)
3495 lun = -1;
3496 task = -1;
3497 if (np->abrt_msg[0] == M_RESET) {
3498 tp->head.sval = 0;
3499 tp->head.wval = np->rv_scntl3;
3500 tp->head.uval = 0;
3501 spi_period(starget) = 0;
3502 spi_offset(starget) = 0;
3503 spi_width(starget) = 0;
3504 spi_iu(starget) = 0;
3505 spi_dt(starget) = 0;
3506 spi_qas(starget) = 0;
3507 tp->tgoal.check_nego = 1;
3508 tp->tgoal.renego = 0;
3512 * Otherwise, check for the LUN and TASK(s)
3513 * concerned by the cancelation.
3514 * If it is not ABORT_TAG then it is CLEAR_QUEUE
3515 * or an ABORT message :-)
3517 else {
3518 lun = np->abrt_msg[0] & 0x3f;
3519 if (np->abrt_msg[1] == M_ABORT_TAG)
3520 task = np->abrt_msg[2];
3524 * Complete all the CCBs the device should have
3525 * aborted due to our 'kiss of death' message.
3527 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
3528 sym_dequeue_from_squeue(np, i, target, lun, -1);
3529 sym_clear_tasks(np, DID_ABORT, target, lun, task);
3530 sym_flush_comp_queue(np, 0);
3533 * If we sent a BDR, make upper layer aware of that.
3535 if (np->abrt_msg[0] == M_RESET)
3536 starget_printk(KERN_NOTICE, starget,
3537 "has been reset\n");
3538 break;
3542 * Print to the log the message we intend to send.
3544 if (num == SIR_TARGET_SELECTED) {
3545 dev_info(&tp->starget->dev, "control msgout:");
3546 sym_printl_hex(np->abrt_msg, np->abrt_tbl.size);
3547 np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
3551 * Let the SCRIPTS processor continue.
3553 OUTONB_STD();
3557 * Gerard's alchemy:) that deals with with the data
3558 * pointer for both MDP and the residual calculation.
3560 * I didn't want to bloat the code by more than 200
3561 * lines for the handling of both MDP and the residual.
3562 * This has been achieved by using a data pointer
3563 * representation consisting in an index in the data
3564 * array (dp_sg) and a negative offset (dp_ofs) that
3565 * have the following meaning:
3567 * - dp_sg = SYM_CONF_MAX_SG
3568 * we are at the end of the data script.
3569 * - dp_sg < SYM_CONF_MAX_SG
3570 * dp_sg points to the next entry of the scatter array
3571 * we want to transfer.
3572 * - dp_ofs < 0
3573 * dp_ofs represents the residual of bytes of the
3574 * previous entry scatter entry we will send first.
3575 * - dp_ofs = 0
3576 * no residual to send first.
3578 * The function sym_evaluate_dp() accepts an arbitray
3579 * offset (basically from the MDP message) and returns
3580 * the corresponding values of dp_sg and dp_ofs.
3583 static int sym_evaluate_dp(struct sym_hcb *np, struct sym_ccb *cp, u32 scr, int *ofs)
3585 u32 dp_scr;
3586 int dp_ofs, dp_sg, dp_sgmin;
3587 int tmp;
3588 struct sym_pmc *pm;
3591 * Compute the resulted data pointer in term of a script
3592 * address within some DATA script and a signed byte offset.
3594 dp_scr = scr;
3595 dp_ofs = *ofs;
3596 if (dp_scr == SCRIPTA_BA(np, pm0_data))
3597 pm = &cp->phys.pm0;
3598 else if (dp_scr == SCRIPTA_BA(np, pm1_data))
3599 pm = &cp->phys.pm1;
3600 else
3601 pm = NULL;
3603 if (pm) {
3604 dp_scr = scr_to_cpu(pm->ret);
3605 dp_ofs -= scr_to_cpu(pm->sg.size) & 0x00ffffff;
3609 * If we are auto-sensing, then we are done.
3611 if (cp->host_flags & HF_SENSE) {
3612 *ofs = dp_ofs;
3613 return 0;
3617 * Deduce the index of the sg entry.
3618 * Keep track of the index of the first valid entry.
3619 * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the
3620 * end of the data.
3622 tmp = scr_to_cpu(cp->goalp);
3623 dp_sg = SYM_CONF_MAX_SG;
3624 if (dp_scr != tmp)
3625 dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
3626 dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
3629 * Move to the sg entry the data pointer belongs to.
3631 * If we are inside the data area, we expect result to be:
3633 * Either,
3634 * dp_ofs = 0 and dp_sg is the index of the sg entry
3635 * the data pointer belongs to (or the end of the data)
3636 * Or,
3637 * dp_ofs < 0 and dp_sg is the index of the sg entry
3638 * the data pointer belongs to + 1.
3640 if (dp_ofs < 0) {
3641 int n;
3642 while (dp_sg > dp_sgmin) {
3643 --dp_sg;
3644 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3645 n = dp_ofs + (tmp & 0xffffff);
3646 if (n > 0) {
3647 ++dp_sg;
3648 break;
3650 dp_ofs = n;
3653 else if (dp_ofs > 0) {
3654 while (dp_sg < SYM_CONF_MAX_SG) {
3655 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3656 dp_ofs -= (tmp & 0xffffff);
3657 ++dp_sg;
3658 if (dp_ofs <= 0)
3659 break;
3664 * Make sure the data pointer is inside the data area.
3665 * If not, return some error.
3667 if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
3668 goto out_err;
3669 else if (dp_sg > SYM_CONF_MAX_SG ||
3670 (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
3671 goto out_err;
3674 * Save the extreme pointer if needed.
3676 if (dp_sg > cp->ext_sg ||
3677 (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
3678 cp->ext_sg = dp_sg;
3679 cp->ext_ofs = dp_ofs;
3683 * Return data.
3685 *ofs = dp_ofs;
3686 return dp_sg;
3688 out_err:
3689 return -1;
3693 * chip handler for MODIFY DATA POINTER MESSAGE
3695 * We also call this function on IGNORE WIDE RESIDUE
3696 * messages that do not match a SWIDE full condition.
3697 * Btw, we assume in that situation that such a message
3698 * is equivalent to a MODIFY DATA POINTER (offset=-1).
3701 static void sym_modify_dp(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp, int ofs)
3703 int dp_ofs = ofs;
3704 u32 dp_scr = sym_get_script_dp (np, cp);
3705 u32 dp_ret;
3706 u32 tmp;
3707 u_char hflags;
3708 int dp_sg;
3709 struct sym_pmc *pm;
3712 * Not supported for auto-sense.
3714 if (cp->host_flags & HF_SENSE)
3715 goto out_reject;
3718 * Apply our alchemy:) (see comments in sym_evaluate_dp()),
3719 * to the resulted data pointer.
3721 dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
3722 if (dp_sg < 0)
3723 goto out_reject;
3726 * And our alchemy:) allows to easily calculate the data
3727 * script address we want to return for the next data phase.
3729 dp_ret = cpu_to_scr(cp->goalp);
3730 dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
3733 * If offset / scatter entry is zero we donnot need
3734 * a context for the new current data pointer.
3736 if (dp_ofs == 0) {
3737 dp_scr = dp_ret;
3738 goto out_ok;
3742 * Get a context for the new current data pointer.
3744 hflags = INB(np, HF_PRT);
3746 if (hflags & HF_DP_SAVED)
3747 hflags ^= HF_ACT_PM;
3749 if (!(hflags & HF_ACT_PM)) {
3750 pm = &cp->phys.pm0;
3751 dp_scr = SCRIPTA_BA(np, pm0_data);
3753 else {
3754 pm = &cp->phys.pm1;
3755 dp_scr = SCRIPTA_BA(np, pm1_data);
3758 hflags &= ~(HF_DP_SAVED);
3760 OUTB(np, HF_PRT, hflags);
3763 * Set up the new current data pointer.
3764 * ofs < 0 there, and for the next data phase, we
3765 * want to transfer part of the data of the sg entry
3766 * corresponding to index dp_sg-1 prior to returning
3767 * to the main data script.
3769 pm->ret = cpu_to_scr(dp_ret);
3770 tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
3771 tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
3772 pm->sg.addr = cpu_to_scr(tmp);
3773 pm->sg.size = cpu_to_scr(-dp_ofs);
3775 out_ok:
3776 sym_set_script_dp (np, cp, dp_scr);
3777 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
3778 return;
3780 out_reject:
3781 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
3786 * chip calculation of the data residual.
3788 * As I used to say, the requirement of data residual
3789 * in SCSI is broken, useless and cannot be achieved
3790 * without huge complexity.
3791 * But most OSes and even the official CAM require it.
3792 * When stupidity happens to be so widely spread inside
3793 * a community, it gets hard to convince.
3795 * Anyway, I don't care, since I am not going to use
3796 * any software that considers this data residual as
3797 * a relevant information. :)
3800 int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp)
3802 int dp_sg, dp_sgmin, resid = 0;
3803 int dp_ofs = 0;
3806 * Check for some data lost or just thrown away.
3807 * We are not required to be quite accurate in this
3808 * situation. Btw, if we are odd for output and the
3809 * device claims some more data, it may well happen
3810 * than our residual be zero. :-)
3812 if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
3813 if (cp->xerr_status & XE_EXTRA_DATA)
3814 resid -= cp->extra_bytes;
3815 if (cp->xerr_status & XE_SODL_UNRUN)
3816 ++resid;
3817 if (cp->xerr_status & XE_SWIDE_OVRUN)
3818 --resid;
3822 * If all data has been transferred,
3823 * there is no residual.
3825 if (cp->phys.head.lastp == cp->goalp)
3826 return resid;
3829 * If no data transfer occurs, or if the data
3830 * pointer is weird, return full residual.
3832 if (cp->startp == cp->phys.head.lastp ||
3833 sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
3834 &dp_ofs) < 0) {
3835 return cp->data_len - cp->odd_byte_adjustment;
3839 * If we were auto-sensing, then we are done.
3841 if (cp->host_flags & HF_SENSE) {
3842 return -dp_ofs;
3846 * We are now full comfortable in the computation
3847 * of the data residual (2's complement).
3849 dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
3850 resid = -cp->ext_ofs;
3851 for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
3852 u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3853 resid += (tmp & 0xffffff);
3856 resid -= cp->odd_byte_adjustment;
3859 * Hopefully, the result is not too wrong.
3861 return resid;
3865 * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
3867 * When we try to negotiate, we append the negotiation message
3868 * to the identify and (maybe) simple tag message.
3869 * The host status field is set to HS_NEGOTIATE to mark this
3870 * situation.
3872 * If the target doesn't answer this message immediately
3873 * (as required by the standard), the SIR_NEGO_FAILED interrupt
3874 * will be raised eventually.
3875 * The handler removes the HS_NEGOTIATE status, and sets the
3876 * negotiated value to the default (async / nowide).
3878 * If we receive a matching answer immediately, we check it
3879 * for validity, and set the values.
3881 * If we receive a Reject message immediately, we assume the
3882 * negotiation has failed, and fall back to standard values.
3884 * If we receive a negotiation message while not in HS_NEGOTIATE
3885 * state, it's a target initiated negotiation. We prepare a
3886 * (hopefully) valid answer, set our parameters, and send back
3887 * this answer to the target.
3889 * If the target doesn't fetch the answer (no message out phase),
3890 * we assume the negotiation has failed, and fall back to default
3891 * settings (SIR_NEGO_PROTO interrupt).
3893 * When we set the values, we adjust them in all ccbs belonging
3894 * to this target, in the controller's register, and in the "phys"
3895 * field of the controller's struct sym_hcb.
3899 * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message.
3901 static int
3902 sym_sync_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
3904 int target = cp->target;
3905 u_char chg, ofs, per, fak, div;
3907 if (DEBUG_FLAGS & DEBUG_NEGO) {
3908 sym_print_nego_msg(np, target, "sync msgin", np->msgin);
3912 * Get requested values.
3914 chg = 0;
3915 per = np->msgin[3];
3916 ofs = np->msgin[4];
3919 * Check values against our limits.
3921 if (ofs) {
3922 if (ofs > np->maxoffs)
3923 {chg = 1; ofs = np->maxoffs;}
3926 if (ofs) {
3927 if (per < np->minsync)
3928 {chg = 1; per = np->minsync;}
3932 * Get new chip synchronous parameters value.
3934 div = fak = 0;
3935 if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
3936 goto reject_it;
3938 if (DEBUG_FLAGS & DEBUG_NEGO) {
3939 sym_print_addr(cp->cmd,
3940 "sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
3941 ofs, per, div, fak, chg);
3945 * If it was an answer we want to change,
3946 * then it isn't acceptable. Reject it.
3948 if (!req && chg)
3949 goto reject_it;
3952 * Apply new values.
3954 sym_setsync (np, target, ofs, per, div, fak);
3957 * It was an answer. We are done.
3959 if (!req)
3960 return 0;
3963 * It was a request. Prepare an answer message.
3965 spi_populate_sync_msg(np->msgout, per, ofs);
3967 if (DEBUG_FLAGS & DEBUG_NEGO) {
3968 sym_print_nego_msg(np, target, "sync msgout", np->msgout);
3971 np->msgin [0] = M_NOOP;
3973 return 0;
3975 reject_it:
3976 sym_setsync (np, target, 0, 0, 0, 0);
3977 return -1;
3980 static void sym_sync_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
3982 int req = 1;
3983 int result;
3986 * Request or answer ?
3988 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
3989 OUTB(np, HS_PRT, HS_BUSY);
3990 if (cp->nego_status && cp->nego_status != NS_SYNC)
3991 goto reject_it;
3992 req = 0;
3996 * Check and apply new values.
3998 result = sym_sync_nego_check(np, req, cp);
3999 if (result) /* Not acceptable, reject it */
4000 goto reject_it;
4001 if (req) { /* Was a request, send response. */
4002 cp->nego_status = NS_SYNC;
4003 OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
4005 else /* Was a response, we are done. */
4006 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4007 return;
4009 reject_it:
4010 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4014 * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message.
4016 static int
4017 sym_ppr_nego_check(struct sym_hcb *np, int req, int target)
4019 struct sym_tcb *tp = &np->target[target];
4020 unsigned char fak, div;
4021 int dt, chg = 0;
4023 unsigned char per = np->msgin[3];
4024 unsigned char ofs = np->msgin[5];
4025 unsigned char wide = np->msgin[6];
4026 unsigned char opts = np->msgin[7] & PPR_OPT_MASK;
4028 if (DEBUG_FLAGS & DEBUG_NEGO) {
4029 sym_print_nego_msg(np, target, "ppr msgin", np->msgin);
4033 * Check values against our limits.
4035 if (wide > np->maxwide) {
4036 chg = 1;
4037 wide = np->maxwide;
4039 if (!wide || !(np->features & FE_U3EN))
4040 opts = 0;
4042 if (opts != (np->msgin[7] & PPR_OPT_MASK))
4043 chg = 1;
4045 dt = opts & PPR_OPT_DT;
4047 if (ofs) {
4048 unsigned char maxoffs = dt ? np->maxoffs_dt : np->maxoffs;
4049 if (ofs > maxoffs) {
4050 chg = 1;
4051 ofs = maxoffs;
4055 if (ofs) {
4056 unsigned char minsync = dt ? np->minsync_dt : np->minsync;
4057 if (per < minsync) {
4058 chg = 1;
4059 per = minsync;
4064 * Get new chip synchronous parameters value.
4066 div = fak = 0;
4067 if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
4068 goto reject_it;
4071 * If it was an answer we want to change,
4072 * then it isn't acceptable. Reject it.
4074 if (!req && chg)
4075 goto reject_it;
4078 * Apply new values.
4080 sym_setpprot(np, target, opts, ofs, per, wide, div, fak);
4083 * It was an answer. We are done.
4085 if (!req)
4086 return 0;
4089 * It was a request. Prepare an answer message.
4091 spi_populate_ppr_msg(np->msgout, per, ofs, wide, opts);
4093 if (DEBUG_FLAGS & DEBUG_NEGO) {
4094 sym_print_nego_msg(np, target, "ppr msgout", np->msgout);
4097 np->msgin [0] = M_NOOP;
4099 return 0;
4101 reject_it:
4102 sym_setpprot (np, target, 0, 0, 0, 0, 0, 0);
4104 * If it is a device response that should result in
4105 * ST, we may want to try a legacy negotiation later.
4107 if (!req && !opts) {
4108 tp->tgoal.period = per;
4109 tp->tgoal.offset = ofs;
4110 tp->tgoal.width = wide;
4111 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
4112 tp->tgoal.check_nego = 1;
4114 return -1;
4117 static void sym_ppr_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4119 int req = 1;
4120 int result;
4123 * Request or answer ?
4125 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
4126 OUTB(np, HS_PRT, HS_BUSY);
4127 if (cp->nego_status && cp->nego_status != NS_PPR)
4128 goto reject_it;
4129 req = 0;
4133 * Check and apply new values.
4135 result = sym_ppr_nego_check(np, req, cp->target);
4136 if (result) /* Not acceptable, reject it */
4137 goto reject_it;
4138 if (req) { /* Was a request, send response. */
4139 cp->nego_status = NS_PPR;
4140 OUTL_DSP(np, SCRIPTB_BA(np, ppr_resp));
4142 else /* Was a response, we are done. */
4143 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4144 return;
4146 reject_it:
4147 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4151 * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message.
4153 static int
4154 sym_wide_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
4156 int target = cp->target;
4157 u_char chg, wide;
4159 if (DEBUG_FLAGS & DEBUG_NEGO) {
4160 sym_print_nego_msg(np, target, "wide msgin", np->msgin);
4164 * Get requested values.
4166 chg = 0;
4167 wide = np->msgin[3];
4170 * Check values against our limits.
4172 if (wide > np->maxwide) {
4173 chg = 1;
4174 wide = np->maxwide;
4177 if (DEBUG_FLAGS & DEBUG_NEGO) {
4178 sym_print_addr(cp->cmd, "wdtr: wide=%d chg=%d.\n",
4179 wide, chg);
4183 * If it was an answer we want to change,
4184 * then it isn't acceptable. Reject it.
4186 if (!req && chg)
4187 goto reject_it;
4190 * Apply new values.
4192 sym_setwide (np, target, wide);
4195 * It was an answer. We are done.
4197 if (!req)
4198 return 0;
4201 * It was a request. Prepare an answer message.
4203 spi_populate_width_msg(np->msgout, wide);
4205 np->msgin [0] = M_NOOP;
4207 if (DEBUG_FLAGS & DEBUG_NEGO) {
4208 sym_print_nego_msg(np, target, "wide msgout", np->msgout);
4211 return 0;
4213 reject_it:
4214 return -1;
4217 static void sym_wide_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4219 int req = 1;
4220 int result;
4223 * Request or answer ?
4225 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
4226 OUTB(np, HS_PRT, HS_BUSY);
4227 if (cp->nego_status && cp->nego_status != NS_WIDE)
4228 goto reject_it;
4229 req = 0;
4233 * Check and apply new values.
4235 result = sym_wide_nego_check(np, req, cp);
4236 if (result) /* Not acceptable, reject it */
4237 goto reject_it;
4238 if (req) { /* Was a request, send response. */
4239 cp->nego_status = NS_WIDE;
4240 OUTL_DSP(np, SCRIPTB_BA(np, wdtr_resp));
4241 } else { /* Was a response. */
4243 * Negotiate for SYNC immediately after WIDE response.
4244 * This allows to negotiate for both WIDE and SYNC on
4245 * a single SCSI command (Suggested by Justin Gibbs).
4247 if (tp->tgoal.offset) {
4248 spi_populate_sync_msg(np->msgout, tp->tgoal.period,
4249 tp->tgoal.offset);
4251 if (DEBUG_FLAGS & DEBUG_NEGO) {
4252 sym_print_nego_msg(np, cp->target,
4253 "sync msgout", np->msgout);
4256 cp->nego_status = NS_SYNC;
4257 OUTB(np, HS_PRT, HS_NEGOTIATE);
4258 OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
4259 return;
4260 } else
4261 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4264 return;
4266 reject_it:
4267 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4271 * Reset DT, SYNC or WIDE to default settings.
4273 * Called when a negotiation does not succeed either
4274 * on rejection or on protocol error.
4276 * A target that understands a PPR message should never
4277 * reject it, and messing with it is very unlikely.
4278 * So, if a PPR makes problems, we may just want to
4279 * try a legacy negotiation later.
4281 static void sym_nego_default(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4283 switch (cp->nego_status) {
4284 case NS_PPR:
4285 if (tp->tgoal.period < np->minsync)
4286 tp->tgoal.period = np->minsync;
4287 if (tp->tgoal.offset > np->maxoffs)
4288 tp->tgoal.offset = np->maxoffs;
4289 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
4290 tp->tgoal.check_nego = 1;
4291 break;
4292 case NS_SYNC:
4293 sym_setsync (np, cp->target, 0, 0, 0, 0);
4294 break;
4295 case NS_WIDE:
4296 sym_setwide (np, cp->target, 0);
4297 break;
4299 np->msgin [0] = M_NOOP;
4300 np->msgout[0] = M_NOOP;
4301 cp->nego_status = 0;
4305 * chip handler for MESSAGE REJECT received in response to
4306 * PPR, WIDE or SYNCHRONOUS negotiation.
4308 static void sym_nego_rejected(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4310 sym_nego_default(np, tp, cp);
4311 OUTB(np, HS_PRT, HS_BUSY);
4315 * chip exception handler for programmed interrupts.
4317 static void sym_int_sir(struct sym_hcb *np)
4319 u_char num = INB(np, nc_dsps);
4320 u32 dsa = INL(np, nc_dsa);
4321 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
4322 u_char target = INB(np, nc_sdid) & 0x0f;
4323 struct sym_tcb *tp = &np->target[target];
4324 int tmp;
4326 if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
4328 switch (num) {
4329 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
4331 * SCRIPTS tell us that we may have to update
4332 * 64 bit DMA segment registers.
4334 case SIR_DMAP_DIRTY:
4335 sym_update_dmap_regs(np);
4336 goto out;
4337 #endif
4339 * Command has been completed with error condition
4340 * or has been auto-sensed.
4342 case SIR_COMPLETE_ERROR:
4343 sym_complete_error(np, cp);
4344 return;
4346 * The C code is currently trying to recover from something.
4347 * Typically, user want to abort some command.
4349 case SIR_SCRIPT_STOPPED:
4350 case SIR_TARGET_SELECTED:
4351 case SIR_ABORT_SENT:
4352 sym_sir_task_recovery(np, num);
4353 return;
4355 * The device didn't go to MSG OUT phase after having
4356 * been selected with ATN. We do not want to handle that.
4358 case SIR_SEL_ATN_NO_MSG_OUT:
4359 scmd_printk(KERN_WARNING, cp->cmd,
4360 "No MSG OUT phase after selection with ATN\n");
4361 goto out_stuck;
4363 * The device didn't switch to MSG IN phase after
4364 * having reselected the initiator.
4366 case SIR_RESEL_NO_MSG_IN:
4367 scmd_printk(KERN_WARNING, cp->cmd,
4368 "No MSG IN phase after reselection\n");
4369 goto out_stuck;
4371 * After reselection, the device sent a message that wasn't
4372 * an IDENTIFY.
4374 case SIR_RESEL_NO_IDENTIFY:
4375 scmd_printk(KERN_WARNING, cp->cmd,
4376 "No IDENTIFY after reselection\n");
4377 goto out_stuck;
4379 * The device reselected a LUN we do not know about.
4381 case SIR_RESEL_BAD_LUN:
4382 np->msgout[0] = M_RESET;
4383 goto out;
4385 * The device reselected for an untagged nexus and we
4386 * haven't any.
4388 case SIR_RESEL_BAD_I_T_L:
4389 np->msgout[0] = M_ABORT;
4390 goto out;
4392 * The device reselected for a tagged nexus that we do not have.
4394 case SIR_RESEL_BAD_I_T_L_Q:
4395 np->msgout[0] = M_ABORT_TAG;
4396 goto out;
4398 * The SCRIPTS let us know that the device has grabbed
4399 * our message and will abort the job.
4401 case SIR_RESEL_ABORTED:
4402 np->lastmsg = np->msgout[0];
4403 np->msgout[0] = M_NOOP;
4404 scmd_printk(KERN_WARNING, cp->cmd,
4405 "message %x sent on bad reselection\n", np->lastmsg);
4406 goto out;
4408 * The SCRIPTS let us know that a message has been
4409 * successfully sent to the device.
4411 case SIR_MSG_OUT_DONE:
4412 np->lastmsg = np->msgout[0];
4413 np->msgout[0] = M_NOOP;
4414 /* Should we really care of that */
4415 if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
4416 if (cp) {
4417 cp->xerr_status &= ~XE_PARITY_ERR;
4418 if (!cp->xerr_status)
4419 OUTOFFB(np, HF_PRT, HF_EXT_ERR);
4422 goto out;
4424 * The device didn't send a GOOD SCSI status.
4425 * We may have some work to do prior to allow
4426 * the SCRIPTS processor to continue.
4428 case SIR_BAD_SCSI_STATUS:
4429 if (!cp)
4430 goto out;
4431 sym_sir_bad_scsi_status(np, num, cp);
4432 return;
4434 * We are asked by the SCRIPTS to prepare a
4435 * REJECT message.
4437 case SIR_REJECT_TO_SEND:
4438 sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
4439 np->msgout[0] = M_REJECT;
4440 goto out;
4442 * We have been ODD at the end of a DATA IN
4443 * transfer and the device didn't send a
4444 * IGNORE WIDE RESIDUE message.
4445 * It is a data overrun condition.
4447 case SIR_SWIDE_OVERRUN:
4448 if (cp) {
4449 OUTONB(np, HF_PRT, HF_EXT_ERR);
4450 cp->xerr_status |= XE_SWIDE_OVRUN;
4452 goto out;
4454 * We have been ODD at the end of a DATA OUT
4455 * transfer.
4456 * It is a data underrun condition.
4458 case SIR_SODL_UNDERRUN:
4459 if (cp) {
4460 OUTONB(np, HF_PRT, HF_EXT_ERR);
4461 cp->xerr_status |= XE_SODL_UNRUN;
4463 goto out;
4465 * The device wants us to tranfer more data than
4466 * expected or in the wrong direction.
4467 * The number of extra bytes is in scratcha.
4468 * It is a data overrun condition.
4470 case SIR_DATA_OVERRUN:
4471 if (cp) {
4472 OUTONB(np, HF_PRT, HF_EXT_ERR);
4473 cp->xerr_status |= XE_EXTRA_DATA;
4474 cp->extra_bytes += INL(np, nc_scratcha);
4476 goto out;
4478 * The device switched to an illegal phase (4/5).
4480 case SIR_BAD_PHASE:
4481 if (cp) {
4482 OUTONB(np, HF_PRT, HF_EXT_ERR);
4483 cp->xerr_status |= XE_BAD_PHASE;
4485 goto out;
4487 * We received a message.
4489 case SIR_MSG_RECEIVED:
4490 if (!cp)
4491 goto out_stuck;
4492 switch (np->msgin [0]) {
4494 * We received an extended message.
4495 * We handle MODIFY DATA POINTER, SDTR, WDTR
4496 * and reject all other extended messages.
4498 case M_EXTENDED:
4499 switch (np->msgin [2]) {
4500 case M_X_MODIFY_DP:
4501 if (DEBUG_FLAGS & DEBUG_POINTER)
4502 sym_print_msg(cp, "extended msg ",
4503 np->msgin);
4504 tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
4505 (np->msgin[5]<<8) + (np->msgin[6]);
4506 sym_modify_dp(np, tp, cp, tmp);
4507 return;
4508 case M_X_SYNC_REQ:
4509 sym_sync_nego(np, tp, cp);
4510 return;
4511 case M_X_PPR_REQ:
4512 sym_ppr_nego(np, tp, cp);
4513 return;
4514 case M_X_WIDE_REQ:
4515 sym_wide_nego(np, tp, cp);
4516 return;
4517 default:
4518 goto out_reject;
4520 break;
4522 * We received a 1/2 byte message not handled from SCRIPTS.
4523 * We are only expecting MESSAGE REJECT and IGNORE WIDE
4524 * RESIDUE messages that haven't been anticipated by
4525 * SCRIPTS on SWIDE full condition. Unanticipated IGNORE
4526 * WIDE RESIDUE messages are aliased as MODIFY DP (-1).
4528 case M_IGN_RESIDUE:
4529 if (DEBUG_FLAGS & DEBUG_POINTER)
4530 sym_print_msg(cp, "1 or 2 byte ", np->msgin);
4531 if (cp->host_flags & HF_SENSE)
4532 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4533 else
4534 sym_modify_dp(np, tp, cp, -1);
4535 return;
4536 case M_REJECT:
4537 if (INB(np, HS_PRT) == HS_NEGOTIATE)
4538 sym_nego_rejected(np, tp, cp);
4539 else {
4540 sym_print_addr(cp->cmd,
4541 "M_REJECT received (%x:%x).\n",
4542 scr_to_cpu(np->lastmsg), np->msgout[0]);
4544 goto out_clrack;
4545 break;
4546 default:
4547 goto out_reject;
4549 break;
4551 * We received an unknown message.
4552 * Ignore all MSG IN phases and reject it.
4554 case SIR_MSG_WEIRD:
4555 sym_print_msg(cp, "WEIRD message received", np->msgin);
4556 OUTL_DSP(np, SCRIPTB_BA(np, msg_weird));
4557 return;
4559 * Negotiation failed.
4560 * Target does not send us the reply.
4561 * Remove the HS_NEGOTIATE status.
4563 case SIR_NEGO_FAILED:
4564 OUTB(np, HS_PRT, HS_BUSY);
4566 * Negotiation failed.
4567 * Target does not want answer message.
4569 case SIR_NEGO_PROTO:
4570 sym_nego_default(np, tp, cp);
4571 goto out;
4574 out:
4575 OUTONB_STD();
4576 return;
4577 out_reject:
4578 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4579 return;
4580 out_clrack:
4581 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4582 return;
4583 out_stuck:
4584 return;
4588 * Acquire a control block
4590 struct sym_ccb *sym_get_ccb (struct sym_hcb *np, struct scsi_cmnd *cmd, u_char tag_order)
4592 u_char tn = cmd->device->id;
4593 u_char ln = cmd->device->lun;
4594 struct sym_tcb *tp = &np->target[tn];
4595 struct sym_lcb *lp = sym_lp(tp, ln);
4596 u_short tag = NO_TAG;
4597 SYM_QUEHEAD *qp;
4598 struct sym_ccb *cp = NULL;
4601 * Look for a free CCB
4603 if (sym_que_empty(&np->free_ccbq))
4604 sym_alloc_ccb(np);
4605 qp = sym_remque_head(&np->free_ccbq);
4606 if (!qp)
4607 goto out;
4608 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
4612 * If we have been asked for a tagged command.
4614 if (tag_order) {
4616 * Debugging purpose.
4618 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4619 if (lp->busy_itl != 0)
4620 goto out_free;
4621 #endif
4623 * Allocate resources for tags if not yet.
4625 if (!lp->cb_tags) {
4626 sym_alloc_lcb_tags(np, tn, ln);
4627 if (!lp->cb_tags)
4628 goto out_free;
4631 * Get a tag for this SCSI IO and set up
4632 * the CCB bus address for reselection,
4633 * and count it for this LUN.
4634 * Toggle reselect path to tagged.
4636 if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
4637 tag = lp->cb_tags[lp->ia_tag];
4638 if (++lp->ia_tag == SYM_CONF_MAX_TASK)
4639 lp->ia_tag = 0;
4640 ++lp->busy_itlq;
4641 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4642 lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
4643 lp->head.resel_sa =
4644 cpu_to_scr(SCRIPTA_BA(np, resel_tag));
4645 #endif
4646 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
4647 cp->tags_si = lp->tags_si;
4648 ++lp->tags_sum[cp->tags_si];
4649 ++lp->tags_since;
4650 #endif
4652 else
4653 goto out_free;
4656 * This command will not be tagged.
4657 * If we already have either a tagged or untagged
4658 * one, refuse to overlap this untagged one.
4660 else {
4662 * Debugging purpose.
4664 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4665 if (lp->busy_itl != 0 || lp->busy_itlq != 0)
4666 goto out_free;
4667 #endif
4669 * Count this nexus for this LUN.
4670 * Set up the CCB bus address for reselection.
4671 * Toggle reselect path to untagged.
4673 ++lp->busy_itl;
4674 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4675 if (lp->busy_itl == 1) {
4676 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
4677 lp->head.resel_sa =
4678 cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
4680 else
4681 goto out_free;
4682 #endif
4686 * Put the CCB into the busy queue.
4688 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
4689 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4690 if (lp) {
4691 sym_remque(&cp->link2_ccbq);
4692 sym_insque_tail(&cp->link2_ccbq, &lp->waiting_ccbq);
4695 #endif
4696 cp->to_abort = 0;
4697 cp->odd_byte_adjustment = 0;
4698 cp->tag = tag;
4699 cp->order = tag_order;
4700 cp->target = tn;
4701 cp->lun = ln;
4703 if (DEBUG_FLAGS & DEBUG_TAGS) {
4704 sym_print_addr(cmd, "ccb @%p using tag %d.\n", cp, tag);
4707 out:
4708 return cp;
4709 out_free:
4710 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4711 return NULL;
4715 * Release one control block
4717 void sym_free_ccb (struct sym_hcb *np, struct sym_ccb *cp)
4719 struct sym_tcb *tp = &np->target[cp->target];
4720 struct sym_lcb *lp = sym_lp(tp, cp->lun);
4722 if (DEBUG_FLAGS & DEBUG_TAGS) {
4723 sym_print_addr(cp->cmd, "ccb @%p freeing tag %d.\n",
4724 cp, cp->tag);
4728 * If LCB available,
4730 if (lp) {
4732 * If tagged, release the tag, set the relect path
4734 if (cp->tag != NO_TAG) {
4735 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
4736 --lp->tags_sum[cp->tags_si];
4737 #endif
4739 * Free the tag value.
4741 lp->cb_tags[lp->if_tag] = cp->tag;
4742 if (++lp->if_tag == SYM_CONF_MAX_TASK)
4743 lp->if_tag = 0;
4745 * Make the reselect path invalid,
4746 * and uncount this CCB.
4748 lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
4749 --lp->busy_itlq;
4750 } else { /* Untagged */
4752 * Make the reselect path invalid,
4753 * and uncount this CCB.
4755 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
4756 --lp->busy_itl;
4759 * If no JOB active, make the LUN reselect path invalid.
4761 if (lp->busy_itlq == 0 && lp->busy_itl == 0)
4762 lp->head.resel_sa =
4763 cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
4767 * We donnot queue more than 1 ccb per target
4768 * with negotiation at any time. If this ccb was
4769 * used for negotiation, clear this info in the tcb.
4771 if (cp == tp->nego_cp)
4772 tp->nego_cp = NULL;
4774 #ifdef SYM_CONF_IARB_SUPPORT
4776 * If we just complete the last queued CCB,
4777 * clear this info that is no longer relevant.
4779 if (cp == np->last_cp)
4780 np->last_cp = 0;
4781 #endif
4784 * Make this CCB available.
4786 cp->cmd = NULL;
4787 cp->host_status = HS_IDLE;
4788 sym_remque(&cp->link_ccbq);
4789 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4791 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4792 if (lp) {
4793 sym_remque(&cp->link2_ccbq);
4794 sym_insque_tail(&cp->link2_ccbq, &np->dummy_ccbq);
4795 if (cp->started) {
4796 if (cp->tag != NO_TAG)
4797 --lp->started_tags;
4798 else
4799 --lp->started_no_tag;
4802 cp->started = 0;
4803 #endif
4807 * Allocate a CCB from memory and initialize its fixed part.
4809 static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np)
4811 struct sym_ccb *cp = NULL;
4812 int hcode;
4815 * Prevent from allocating more CCBs than we can
4816 * queue to the controller.
4818 if (np->actccbs >= SYM_CONF_MAX_START)
4819 return NULL;
4822 * Allocate memory for this CCB.
4824 cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
4825 if (!cp)
4826 goto out_free;
4829 * Count it.
4831 np->actccbs++;
4834 * Compute the bus address of this ccb.
4836 cp->ccb_ba = vtobus(cp);
4839 * Insert this ccb into the hashed list.
4841 hcode = CCB_HASH_CODE(cp->ccb_ba);
4842 cp->link_ccbh = np->ccbh[hcode];
4843 np->ccbh[hcode] = cp;
4846 * Initialyze the start and restart actions.
4848 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, idle));
4849 cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
4852 * Initilialyze some other fields.
4854 cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
4857 * Chain into free ccb queue.
4859 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4862 * Chain into optionnal lists.
4864 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4865 sym_insque_head(&cp->link2_ccbq, &np->dummy_ccbq);
4866 #endif
4867 return cp;
4868 out_free:
4869 if (cp)
4870 sym_mfree_dma(cp, sizeof(*cp), "CCB");
4871 return NULL;
4875 * Look up a CCB from a DSA value.
4877 static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa)
4879 int hcode;
4880 struct sym_ccb *cp;
4882 hcode = CCB_HASH_CODE(dsa);
4883 cp = np->ccbh[hcode];
4884 while (cp) {
4885 if (cp->ccb_ba == dsa)
4886 break;
4887 cp = cp->link_ccbh;
4890 return cp;
4894 * Target control block initialisation.
4895 * Nothing important to do at the moment.
4897 static void sym_init_tcb (struct sym_hcb *np, u_char tn)
4902 * Lun control block allocation and initialization.
4904 struct sym_lcb *sym_alloc_lcb (struct sym_hcb *np, u_char tn, u_char ln)
4906 struct sym_tcb *tp = &np->target[tn];
4907 struct sym_lcb *lp = NULL;
4910 * Initialize the target control block if not yet.
4912 sym_init_tcb (np, tn);
4915 * Allocate the LCB bus address array.
4916 * Compute the bus address of this table.
4918 if (ln && !tp->luntbl) {
4919 int i;
4921 tp->luntbl = sym_calloc_dma(256, "LUNTBL");
4922 if (!tp->luntbl)
4923 goto fail;
4924 for (i = 0 ; i < 64 ; i++)
4925 tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
4926 tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
4930 * Allocate the table of pointers for LUN(s) > 0, if needed.
4932 if (ln && !tp->lunmp) {
4933 tp->lunmp = kcalloc(SYM_CONF_MAX_LUN, sizeof(struct sym_lcb *),
4934 GFP_ATOMIC);
4935 if (!tp->lunmp)
4936 goto fail;
4940 * Allocate the lcb.
4941 * Make it available to the chip.
4943 lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
4944 if (!lp)
4945 goto fail;
4946 if (ln) {
4947 tp->lunmp[ln] = lp;
4948 tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
4950 else {
4951 tp->lun0p = lp;
4952 tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
4954 tp->nlcb++;
4957 * Let the itl task point to error handling.
4959 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
4962 * Set the reselect pattern to our default. :)
4964 lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
4967 * Set user capabilities.
4969 lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
4971 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4973 * Initialize device queueing.
4975 sym_que_init(&lp->waiting_ccbq);
4976 sym_que_init(&lp->started_ccbq);
4977 lp->started_max = SYM_CONF_MAX_TASK;
4978 lp->started_limit = SYM_CONF_MAX_TASK;
4979 #endif
4981 fail:
4982 return lp;
4986 * Allocate LCB resources for tagged command queuing.
4988 static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln)
4990 struct sym_tcb *tp = &np->target[tn];
4991 struct sym_lcb *lp = sym_lp(tp, ln);
4992 int i;
4995 * Allocate the task table and and the tag allocation
4996 * circular buffer. We want both or none.
4998 lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
4999 if (!lp->itlq_tbl)
5000 goto fail;
5001 lp->cb_tags = kcalloc(SYM_CONF_MAX_TASK, 1, GFP_ATOMIC);
5002 if (!lp->cb_tags) {
5003 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
5004 lp->itlq_tbl = NULL;
5005 goto fail;
5009 * Initialize the task table with invalid entries.
5011 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
5012 lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
5015 * Fill up the tag buffer with tag numbers.
5017 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
5018 lp->cb_tags[i] = i;
5021 * Make the task table available to SCRIPTS,
5022 * And accept tagged commands now.
5024 lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
5026 return;
5027 fail:
5028 return;
5032 * Lun control block deallocation. Returns the number of valid remaing LCBs
5033 * for the target.
5035 int sym_free_lcb(struct sym_hcb *np, u_char tn, u_char ln)
5037 struct sym_tcb *tp = &np->target[tn];
5038 struct sym_lcb *lp = sym_lp(tp, ln);
5040 tp->nlcb--;
5042 if (ln) {
5043 if (!tp->nlcb) {
5044 kfree(tp->lunmp);
5045 sym_mfree_dma(tp->luntbl, 256, "LUNTBL");
5046 tp->lunmp = NULL;
5047 tp->luntbl = NULL;
5048 tp->head.luntbl_sa = cpu_to_scr(vtobus(np->badluntbl));
5049 } else {
5050 tp->luntbl[ln] = cpu_to_scr(vtobus(&np->badlun_sa));
5051 tp->lunmp[ln] = NULL;
5053 } else {
5054 tp->lun0p = NULL;
5055 tp->head.lun0_sa = cpu_to_scr(vtobus(&np->badlun_sa));
5058 if (lp->itlq_tbl) {
5059 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
5060 kfree(lp->cb_tags);
5063 sym_mfree_dma(lp, sizeof(*lp), "LCB");
5065 return tp->nlcb;
5069 * Queue a SCSI IO to the controller.
5071 int sym_queue_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
5073 struct scsi_device *sdev = cmd->device;
5074 struct sym_tcb *tp;
5075 struct sym_lcb *lp;
5076 u_char *msgptr;
5077 u_int msglen;
5078 int can_disconnect;
5081 * Keep track of the IO in our CCB.
5083 cp->cmd = cmd;
5086 * Retrieve the target descriptor.
5088 tp = &np->target[cp->target];
5091 * Retrieve the lun descriptor.
5093 lp = sym_lp(tp, sdev->lun);
5095 can_disconnect = (cp->tag != NO_TAG) ||
5096 (lp && (lp->curr_flags & SYM_DISC_ENABLED));
5098 msgptr = cp->scsi_smsg;
5099 msglen = 0;
5100 msgptr[msglen++] = IDENTIFY(can_disconnect, sdev->lun);
5103 * Build the tag message if present.
5105 if (cp->tag != NO_TAG) {
5106 u_char order = cp->order;
5108 switch(order) {
5109 case M_ORDERED_TAG:
5110 break;
5111 case M_HEAD_TAG:
5112 break;
5113 default:
5114 order = M_SIMPLE_TAG;
5116 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
5118 * Avoid too much reordering of SCSI commands.
5119 * The algorithm tries to prevent completion of any
5120 * tagged command from being delayed against more
5121 * than 3 times the max number of queued commands.
5123 if (lp && lp->tags_since > 3*SYM_CONF_MAX_TAG) {
5124 lp->tags_si = !(lp->tags_si);
5125 if (lp->tags_sum[lp->tags_si]) {
5126 order = M_ORDERED_TAG;
5127 if ((DEBUG_FLAGS & DEBUG_TAGS)||sym_verbose>1) {
5128 sym_print_addr(cmd,
5129 "ordered tag forced.\n");
5132 lp->tags_since = 0;
5134 #endif
5135 msgptr[msglen++] = order;
5138 * For less than 128 tags, actual tags are numbered
5139 * 1,3,5,..2*MAXTAGS+1,since we may have to deal
5140 * with devices that have problems with #TAG 0 or too
5141 * great #TAG numbers. For more tags (up to 256),
5142 * we use directly our tag number.
5144 #if SYM_CONF_MAX_TASK > (512/4)
5145 msgptr[msglen++] = cp->tag;
5146 #else
5147 msgptr[msglen++] = (cp->tag << 1) + 1;
5148 #endif
5152 * Build a negotiation message if needed.
5153 * (nego_status is filled by sym_prepare_nego())
5155 * Always negotiate on INQUIRY and REQUEST SENSE.
5158 cp->nego_status = 0;
5159 if ((tp->tgoal.check_nego ||
5160 cmd->cmnd[0] == INQUIRY || cmd->cmnd[0] == REQUEST_SENSE) &&
5161 !tp->nego_cp && lp) {
5162 msglen += sym_prepare_nego(np, cp, msgptr + msglen);
5166 * Startqueue
5168 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
5169 cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA(np, resel_dsa));
5172 * select
5174 cp->phys.select.sel_id = cp->target;
5175 cp->phys.select.sel_scntl3 = tp->head.wval;
5176 cp->phys.select.sel_sxfer = tp->head.sval;
5177 cp->phys.select.sel_scntl4 = tp->head.uval;
5180 * message
5182 cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg);
5183 cp->phys.smsg.size = cpu_to_scr(msglen);
5186 * status
5188 cp->host_xflags = 0;
5189 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
5190 cp->ssss_status = S_ILLEGAL;
5191 cp->xerr_status = 0;
5192 cp->host_flags = 0;
5193 cp->extra_bytes = 0;
5196 * extreme data pointer.
5197 * shall be positive, so -1 is lower than lowest.:)
5199 cp->ext_sg = -1;
5200 cp->ext_ofs = 0;
5203 * Build the CDB and DATA descriptor block
5204 * and start the IO.
5206 return sym_setup_data_and_start(np, cmd, cp);
5210 * Reset a SCSI target (all LUNs of this target).
5212 int sym_reset_scsi_target(struct sym_hcb *np, int target)
5214 struct sym_tcb *tp;
5216 if (target == np->myaddr || (u_int)target >= SYM_CONF_MAX_TARGET)
5217 return -1;
5219 tp = &np->target[target];
5220 tp->to_reset = 1;
5222 np->istat_sem = SEM;
5223 OUTB(np, nc_istat, SIGP|SEM);
5225 return 0;
5229 * Abort a SCSI IO.
5231 static int sym_abort_ccb(struct sym_hcb *np, struct sym_ccb *cp, int timed_out)
5234 * Check that the IO is active.
5236 if (!cp || !cp->host_status || cp->host_status == HS_WAIT)
5237 return -1;
5240 * If a previous abort didn't succeed in time,
5241 * perform a BUS reset.
5243 if (cp->to_abort) {
5244 sym_reset_scsi_bus(np, 1);
5245 return 0;
5249 * Mark the CCB for abort and allow time for.
5251 cp->to_abort = timed_out ? 2 : 1;
5254 * Tell the SCRIPTS processor to stop and synchronize with us.
5256 np->istat_sem = SEM;
5257 OUTB(np, nc_istat, SIGP|SEM);
5258 return 0;
5261 int sym_abort_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, int timed_out)
5263 struct sym_ccb *cp;
5264 SYM_QUEHEAD *qp;
5267 * Look up our CCB control block.
5269 cp = NULL;
5270 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
5271 struct sym_ccb *cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
5272 if (cp2->cmd == cmd) {
5273 cp = cp2;
5274 break;
5278 return sym_abort_ccb(np, cp, timed_out);
5282 * Complete execution of a SCSI command with extended
5283 * error, SCSI status error, or having been auto-sensed.
5285 * The SCRIPTS processor is not running there, so we
5286 * can safely access IO registers and remove JOBs from
5287 * the START queue.
5288 * SCRATCHA is assumed to have been loaded with STARTPOS
5289 * before the SCRIPTS called the C code.
5291 void sym_complete_error(struct sym_hcb *np, struct sym_ccb *cp)
5293 struct scsi_device *sdev;
5294 struct scsi_cmnd *cmd;
5295 struct sym_tcb *tp;
5296 struct sym_lcb *lp;
5297 int resid;
5298 int i;
5301 * Paranoid check. :)
5303 if (!cp || !cp->cmd)
5304 return;
5306 cmd = cp->cmd;
5307 sdev = cmd->device;
5308 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
5309 dev_info(&sdev->sdev_gendev, "CCB=%p STAT=%x/%x/%x\n", cp,
5310 cp->host_status, cp->ssss_status, cp->host_flags);
5314 * Get target and lun pointers.
5316 tp = &np->target[cp->target];
5317 lp = sym_lp(tp, sdev->lun);
5320 * Check for extended errors.
5322 if (cp->xerr_status) {
5323 if (sym_verbose)
5324 sym_print_xerr(cmd, cp->xerr_status);
5325 if (cp->host_status == HS_COMPLETE)
5326 cp->host_status = HS_COMP_ERR;
5330 * Calculate the residual.
5332 resid = sym_compute_residual(np, cp);
5334 if (!SYM_SETUP_RESIDUAL_SUPPORT) {/* If user does not want residuals */
5335 resid = 0; /* throw them away. :) */
5336 cp->sv_resid = 0;
5338 #ifdef DEBUG_2_0_X
5339 if (resid)
5340 printf("XXXX RESID= %d - 0x%x\n", resid, resid);
5341 #endif
5344 * Dequeue all queued CCBs for that device
5345 * not yet started by SCRIPTS.
5347 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
5348 i = sym_dequeue_from_squeue(np, i, cp->target, sdev->lun, -1);
5351 * Restart the SCRIPTS processor.
5353 OUTL_DSP(np, SCRIPTA_BA(np, start));
5355 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5356 if (cp->host_status == HS_COMPLETE &&
5357 cp->ssss_status == S_QUEUE_FULL) {
5358 if (!lp || lp->started_tags - i < 2)
5359 goto weirdness;
5361 * Decrease queue depth as needed.
5363 lp->started_max = lp->started_tags - i - 1;
5364 lp->num_sgood = 0;
5366 if (sym_verbose >= 2) {
5367 sym_print_addr(cmd, " queue depth is now %d\n",
5368 lp->started_max);
5372 * Repair the CCB.
5374 cp->host_status = HS_BUSY;
5375 cp->ssss_status = S_ILLEGAL;
5378 * Let's requeue it to device.
5380 sym_set_cam_status(cmd, DID_SOFT_ERROR);
5381 goto finish;
5383 weirdness:
5384 #endif
5386 * Build result in CAM ccb.
5388 sym_set_cam_result_error(np, cp, resid);
5390 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5391 finish:
5392 #endif
5394 * Add this one to the COMP queue.
5396 sym_remque(&cp->link_ccbq);
5397 sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
5400 * Complete all those commands with either error
5401 * or requeue condition.
5403 sym_flush_comp_queue(np, 0);
5405 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5407 * Donnot start more than 1 command after an error.
5409 sym_start_next_ccbs(np, lp, 1);
5410 #endif
5414 * Complete execution of a successful SCSI command.
5416 * Only successful commands go to the DONE queue,
5417 * since we need to have the SCRIPTS processor
5418 * stopped on any error condition.
5419 * The SCRIPTS processor is running while we are
5420 * completing successful commands.
5422 void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp)
5424 struct sym_tcb *tp;
5425 struct sym_lcb *lp;
5426 struct scsi_cmnd *cmd;
5427 int resid;
5430 * Paranoid check. :)
5432 if (!cp || !cp->cmd)
5433 return;
5434 assert (cp->host_status == HS_COMPLETE);
5437 * Get user command.
5439 cmd = cp->cmd;
5442 * Get target and lun pointers.
5444 tp = &np->target[cp->target];
5445 lp = sym_lp(tp, cp->lun);
5448 * If all data have been transferred, given than no
5449 * extended error did occur, there is no residual.
5451 resid = 0;
5452 if (cp->phys.head.lastp != cp->goalp)
5453 resid = sym_compute_residual(np, cp);
5456 * Wrong transfer residuals may be worse than just always
5457 * returning zero. User can disable this feature in
5458 * sym53c8xx.h. Residual support is enabled by default.
5460 if (!SYM_SETUP_RESIDUAL_SUPPORT)
5461 resid = 0;
5462 #ifdef DEBUG_2_0_X
5463 if (resid)
5464 printf("XXXX RESID= %d - 0x%x\n", resid, resid);
5465 #endif
5468 * Build result in CAM ccb.
5470 sym_set_cam_result_ok(cp, cmd, resid);
5472 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5474 * If max number of started ccbs had been reduced,
5475 * increase it if 200 good status received.
5477 if (lp && lp->started_max < lp->started_limit) {
5478 ++lp->num_sgood;
5479 if (lp->num_sgood >= 200) {
5480 lp->num_sgood = 0;
5481 ++lp->started_max;
5482 if (sym_verbose >= 2) {
5483 sym_print_addr(cmd, " queue depth is now %d\n",
5484 lp->started_max);
5488 #endif
5491 * Free our CCB.
5493 sym_free_ccb (np, cp);
5495 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5497 * Requeue a couple of awaiting scsi commands.
5499 if (!sym_que_empty(&lp->waiting_ccbq))
5500 sym_start_next_ccbs(np, lp, 2);
5501 #endif
5503 * Complete the command.
5505 sym_xpt_done(np, cmd);
5509 * Soft-attach the controller.
5511 int sym_hcb_attach(struct Scsi_Host *shost, struct sym_fw *fw, struct sym_nvram *nvram)
5513 struct sym_hcb *np = sym_get_hcb(shost);
5514 int i;
5517 * Get some info about the firmware.
5519 np->scripta_sz = fw->a_size;
5520 np->scriptb_sz = fw->b_size;
5521 np->scriptz_sz = fw->z_size;
5522 np->fw_setup = fw->setup;
5523 np->fw_patch = fw->patch;
5524 np->fw_name = fw->name;
5527 * Save setting of some IO registers, so we will
5528 * be able to probe specific implementations.
5530 sym_save_initial_setting (np);
5533 * Reset the chip now, since it has been reported
5534 * that SCSI clock calibration may not work properly
5535 * if the chip is currently active.
5537 sym_chip_reset(np);
5540 * Prepare controller and devices settings, according
5541 * to chip features, user set-up and driver set-up.
5543 sym_prepare_setting(shost, np, nvram);
5546 * Check the PCI clock frequency.
5547 * Must be performed after prepare_setting since it destroys
5548 * STEST1 that is used to probe for the clock doubler.
5550 i = sym_getpciclock(np);
5551 if (i > 37000 && !(np->features & FE_66MHZ))
5552 printf("%s: PCI BUS clock seems too high: %u KHz.\n",
5553 sym_name(np), i);
5556 * Allocate the start queue.
5558 np->squeue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
5559 if (!np->squeue)
5560 goto attach_failed;
5561 np->squeue_ba = vtobus(np->squeue);
5564 * Allocate the done queue.
5566 np->dqueue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
5567 if (!np->dqueue)
5568 goto attach_failed;
5569 np->dqueue_ba = vtobus(np->dqueue);
5572 * Allocate the target bus address array.
5574 np->targtbl = sym_calloc_dma(256, "TARGTBL");
5575 if (!np->targtbl)
5576 goto attach_failed;
5577 np->targtbl_ba = vtobus(np->targtbl);
5580 * Allocate SCRIPTS areas.
5582 np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
5583 np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
5584 np->scriptz0 = sym_calloc_dma(np->scriptz_sz, "SCRIPTZ0");
5585 if (!np->scripta0 || !np->scriptb0 || !np->scriptz0)
5586 goto attach_failed;
5589 * Allocate the array of lists of CCBs hashed by DSA.
5591 np->ccbh = kcalloc(CCB_HASH_SIZE, sizeof(struct sym_ccb **), GFP_KERNEL);
5592 if (!np->ccbh)
5593 goto attach_failed;
5596 * Initialyze the CCB free and busy queues.
5598 sym_que_init(&np->free_ccbq);
5599 sym_que_init(&np->busy_ccbq);
5600 sym_que_init(&np->comp_ccbq);
5603 * Initialization for optional handling
5604 * of device queueing.
5606 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5607 sym_que_init(&np->dummy_ccbq);
5608 #endif
5610 * Allocate some CCB. We need at least ONE.
5612 if (!sym_alloc_ccb(np))
5613 goto attach_failed;
5616 * Calculate BUS addresses where we are going
5617 * to load the SCRIPTS.
5619 np->scripta_ba = vtobus(np->scripta0);
5620 np->scriptb_ba = vtobus(np->scriptb0);
5621 np->scriptz_ba = vtobus(np->scriptz0);
5623 if (np->ram_ba) {
5624 np->scripta_ba = np->ram_ba;
5625 if (np->features & FE_RAM8K) {
5626 np->scriptb_ba = np->scripta_ba + 4096;
5631 * Copy scripts to controller instance.
5633 memcpy(np->scripta0, fw->a_base, np->scripta_sz);
5634 memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
5635 memcpy(np->scriptz0, fw->z_base, np->scriptz_sz);
5638 * Setup variable parts in scripts and compute
5639 * scripts bus addresses used from the C code.
5641 np->fw_setup(np, fw);
5644 * Bind SCRIPTS with physical addresses usable by the
5645 * SCRIPTS processor (as seen from the BUS = BUS addresses).
5647 sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
5648 sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
5649 sym_fw_bind_script(np, (u32 *) np->scriptz0, np->scriptz_sz);
5651 #ifdef SYM_CONF_IARB_SUPPORT
5653 * If user wants IARB to be set when we win arbitration
5654 * and have other jobs, compute the max number of consecutive
5655 * settings of IARB hints before we leave devices a chance to
5656 * arbitrate for reselection.
5658 #ifdef SYM_SETUP_IARB_MAX
5659 np->iarb_max = SYM_SETUP_IARB_MAX;
5660 #else
5661 np->iarb_max = 4;
5662 #endif
5663 #endif
5666 * Prepare the idle and invalid task actions.
5668 np->idletask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5669 np->idletask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5670 np->idletask_ba = vtobus(&np->idletask);
5672 np->notask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5673 np->notask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5674 np->notask_ba = vtobus(&np->notask);
5676 np->bad_itl.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5677 np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5678 np->bad_itl_ba = vtobus(&np->bad_itl);
5680 np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5681 np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA(np,bad_i_t_l_q));
5682 np->bad_itlq_ba = vtobus(&np->bad_itlq);
5685 * Allocate and prepare the lun JUMP table that is used
5686 * for a target prior the probing of devices (bad lun table).
5687 * A private table will be allocated for the target on the
5688 * first INQUIRY response received.
5690 np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
5691 if (!np->badluntbl)
5692 goto attach_failed;
5694 np->badlun_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
5695 for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */
5696 np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
5699 * Prepare the bus address array that contains the bus
5700 * address of each target control block.
5701 * For now, assume all logical units are wrong. :)
5703 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
5704 np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
5705 np->target[i].head.luntbl_sa =
5706 cpu_to_scr(vtobus(np->badluntbl));
5707 np->target[i].head.lun0_sa =
5708 cpu_to_scr(vtobus(&np->badlun_sa));
5712 * Now check the cache handling of the pci chipset.
5714 if (sym_snooptest (np)) {
5715 printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np));
5716 goto attach_failed;
5720 * Sigh! we are done.
5722 return 0;
5724 attach_failed:
5725 return -ENXIO;
5729 * Free everything that has been allocated for this device.
5731 void sym_hcb_free(struct sym_hcb *np)
5733 SYM_QUEHEAD *qp;
5734 struct sym_ccb *cp;
5735 struct sym_tcb *tp;
5736 int target;
5738 if (np->scriptz0)
5739 sym_mfree_dma(np->scriptz0, np->scriptz_sz, "SCRIPTZ0");
5740 if (np->scriptb0)
5741 sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
5742 if (np->scripta0)
5743 sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
5744 if (np->squeue)
5745 sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
5746 if (np->dqueue)
5747 sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
5749 if (np->actccbs) {
5750 while ((qp = sym_remque_head(&np->free_ccbq)) != NULL) {
5751 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
5752 sym_mfree_dma(cp, sizeof(*cp), "CCB");
5755 kfree(np->ccbh);
5757 if (np->badluntbl)
5758 sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
5760 for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
5761 tp = &np->target[target];
5762 if (tp->luntbl)
5763 sym_mfree_dma(tp->luntbl, 256, "LUNTBL");
5764 #if SYM_CONF_MAX_LUN > 1
5765 kfree(tp->lunmp);
5766 #endif
5768 if (np->targtbl)
5769 sym_mfree_dma(np->targtbl, 256, "TARGTBL");