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 */
45 #include "sym_nvram.h"
48 #define SYM_DEBUG_GENERIC_SUPPORT
52 * Needed function prototypes.
54 static void sym_int_ma (struct sym_hcb *np);
55 static void sym_int_sir (struct sym_hcb *np);
56 static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np);
57 static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa);
58 static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln);
59 static void sym_complete_error (struct sym_hcb *np, struct sym_ccb *cp);
60 static void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp);
61 static int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp);
64 * Print a buffer in hexadecimal format with a ".\n" at end.
66 static void sym_printl_hex(u_char *p, int n)
73 static void sym_print_msg(struct sym_ccb *cp, char *label, u_char *msg)
75 sym_print_addr(cp->cmd, "%s: ", label);
81 static void sym_print_nego_msg(struct sym_hcb *np, int target, char *label, u_char *msg)
83 struct sym_tcb *tp = &np->target[target];
84 dev_info(&tp->starget->dev, "%s: ", label);
91 * Print something that tells about extended errors.
93 void sym_print_xerr(struct scsi_cmnd *cmd, int x_status)
95 if (x_status & XE_PARITY_ERR) {
96 sym_print_addr(cmd, "unrecovered SCSI parity error.\n");
98 if (x_status & XE_EXTRA_DATA) {
99 sym_print_addr(cmd, "extraneous data discarded.\n");
101 if (x_status & XE_BAD_PHASE) {
102 sym_print_addr(cmd, "illegal scsi phase (4/5).\n");
104 if (x_status & XE_SODL_UNRUN) {
105 sym_print_addr(cmd, "ODD transfer in DATA OUT phase.\n");
107 if (x_status & XE_SWIDE_OVRUN) {
108 sym_print_addr(cmd, "ODD transfer in DATA IN phase.\n");
113 * Return a string for SCSI BUS mode.
115 static char *sym_scsi_bus_mode(int mode)
118 case SMODE_HVD: return "HVD";
119 case SMODE_SE: return "SE";
120 case SMODE_LVD: return "LVD";
126 * Soft reset the chip.
128 * Raising SRST when the chip is running may cause
129 * problems on dual function chips (see below).
130 * On the other hand, LVD devices need some delay
131 * to settle and report actual BUS mode in STEST4.
133 static void sym_chip_reset (struct sym_hcb *np)
135 OUTB(np, nc_istat, SRST);
138 OUTB(np, nc_istat, 0);
140 udelay(2000); /* For BUS MODE to settle */
144 * Really soft reset the chip.:)
146 * Some 896 and 876 chip revisions may hang-up if we set
147 * the SRST (soft reset) bit at the wrong time when SCRIPTS
149 * So, we need to abort the current operation prior to
150 * soft resetting the chip.
152 static void sym_soft_reset (struct sym_hcb *np)
157 if (!(np->features & FE_ISTAT1) || !(INB(np, nc_istat1) & SCRUN))
160 OUTB(np, nc_istat, CABRT);
161 for (i = 100000 ; i ; --i) {
162 istat = INB(np, nc_istat);
166 else if (istat & DIP) {
167 if (INB(np, nc_dstat) & ABRT)
172 OUTB(np, nc_istat, 0);
174 printf("%s: unable to abort current chip operation, "
175 "ISTAT=0x%02x.\n", sym_name(np), istat);
181 * Start reset process.
183 * The interrupt handler will reinitialize the chip.
185 static void sym_start_reset(struct sym_hcb *np)
187 sym_reset_scsi_bus(np, 1);
190 int sym_reset_scsi_bus(struct sym_hcb *np, int enab_int)
195 sym_soft_reset(np); /* Soft reset the chip */
197 OUTW(np, nc_sien, RST);
199 * Enable Tolerant, reset IRQD if present and
200 * properly set IRQ mode, prior to resetting the bus.
202 OUTB(np, nc_stest3, TE);
203 OUTB(np, nc_dcntl, (np->rv_dcntl & IRQM));
204 OUTB(np, nc_scntl1, CRST);
208 if (!SYM_SETUP_SCSI_BUS_CHECK)
211 * Check for no terminators or SCSI bus shorts to ground.
212 * Read SCSI data bus, data parity bits and control signals.
213 * We are expecting RESET to be TRUE and other signals to be
216 term = INB(np, nc_sstat0);
217 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
218 term |= ((INB(np, nc_sstat2) & 0x01) << 26) | /* sdp1 */
219 ((INW(np, nc_sbdl) & 0xff) << 9) | /* d7-0 */
220 ((INW(np, nc_sbdl) & 0xff00) << 10) | /* d15-8 */
221 INB(np, nc_sbcl); /* req ack bsy sel atn msg cd io */
226 if (term != (2<<7)) {
227 printf("%s: suspicious SCSI data while resetting the BUS.\n",
229 printf("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
230 "0x%lx, expecting 0x%lx\n",
232 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
233 (u_long)term, (u_long)(2<<7));
234 if (SYM_SETUP_SCSI_BUS_CHECK == 1)
238 OUTB(np, nc_scntl1, 0);
243 * Select SCSI clock frequency
245 static void sym_selectclock(struct sym_hcb *np, u_char scntl3)
248 * If multiplier not present or not selected, leave here.
250 if (np->multiplier <= 1) {
251 OUTB(np, nc_scntl3, scntl3);
255 if (sym_verbose >= 2)
256 printf ("%s: enabling clock multiplier\n", sym_name(np));
258 OUTB(np, nc_stest1, DBLEN); /* Enable clock multiplier */
260 * Wait for the LCKFRQ bit to be set if supported by the chip.
261 * Otherwise wait 50 micro-seconds (at least).
263 if (np->features & FE_LCKFRQ) {
265 while (!(INB(np, nc_stest4) & LCKFRQ) && --i > 0)
268 printf("%s: the chip cannot lock the frequency\n",
274 OUTB(np, nc_stest3, HSC); /* Halt the scsi clock */
275 OUTB(np, nc_scntl3, scntl3);
276 OUTB(np, nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
277 OUTB(np, nc_stest3, 0x00); /* Restart scsi clock */
282 * Determine the chip's clock frequency.
284 * This is essential for the negotiation of the synchronous
287 * Note: we have to return the correct value.
288 * THERE IS NO SAFE DEFAULT VALUE.
290 * Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
291 * 53C860 and 53C875 rev. 1 support fast20 transfers but
292 * do not have a clock doubler and so are provided with a
293 * 80 MHz clock. All other fast20 boards incorporate a doubler
294 * and so should be delivered with a 40 MHz clock.
295 * The recent fast40 chips (895/896/895A/1010) use a 40 Mhz base
296 * clock and provide a clock quadrupler (160 Mhz).
300 * calculate SCSI clock frequency (in KHz)
302 static unsigned getfreq (struct sym_hcb *np, int gen)
308 * Measure GEN timer delay in order
309 * to calculate SCSI clock frequency
311 * This code will never execute too
312 * many loop iterations (if DELAY is
313 * reasonably correct). It could get
314 * too low a delay (too high a freq.)
315 * if the CPU is slow executing the
316 * loop for some reason (an NMI, for
317 * example). For this reason we will
318 * if multiple measurements are to be
319 * performed trust the higher delay
320 * (lower frequency returned).
322 OUTW(np, nc_sien, 0); /* mask all scsi interrupts */
323 INW(np, nc_sist); /* clear pending scsi interrupt */
324 OUTB(np, nc_dien, 0); /* mask all dma interrupts */
325 INW(np, nc_sist); /* another one, just to be sure :) */
327 * The C1010-33 core does not report GEN in SIST,
328 * if this interrupt is masked in SIEN.
329 * I don't know yet if the C1010-66 behaves the same way.
331 if (np->features & FE_C10) {
332 OUTW(np, nc_sien, GEN);
333 OUTB(np, nc_istat1, SIRQD);
335 OUTB(np, nc_scntl3, 4); /* set pre-scaler to divide by 3 */
336 OUTB(np, nc_stime1, 0); /* disable general purpose timer */
337 OUTB(np, nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
338 while (!(INW(np, nc_sist) & GEN) && ms++ < 100000)
339 udelay(1000/4); /* count in 1/4 of ms */
340 OUTB(np, nc_stime1, 0); /* disable general purpose timer */
342 * Undo C1010-33 specific settings.
344 if (np->features & FE_C10) {
345 OUTW(np, nc_sien, 0);
346 OUTB(np, nc_istat1, 0);
349 * set prescaler to divide by whatever 0 means
350 * 0 ought to choose divide by 2, but appears
351 * to set divide by 3.5 mode in my 53c810 ...
353 OUTB(np, nc_scntl3, 0);
356 * adjust for prescaler, and convert into KHz
358 f = ms ? ((1 << gen) * (4340*4)) / ms : 0;
361 * The C1010-33 result is biased by a factor
362 * of 2/3 compared to earlier chips.
364 if (np->features & FE_C10)
367 if (sym_verbose >= 2)
368 printf ("%s: Delay (GEN=%d): %u msec, %u KHz\n",
369 sym_name(np), gen, ms/4, f);
374 static unsigned sym_getfreq (struct sym_hcb *np)
379 getfreq (np, gen); /* throw away first result */
380 f1 = getfreq (np, gen);
381 f2 = getfreq (np, gen);
382 if (f1 > f2) f1 = f2; /* trust lower result */
387 * Get/probe chip SCSI clock frequency
389 static void sym_getclock (struct sym_hcb *np, int mult)
391 unsigned char scntl3 = np->sv_scntl3;
392 unsigned char stest1 = np->sv_stest1;
398 * True with 875/895/896/895A with clock multiplier selected
400 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
401 if (sym_verbose >= 2)
402 printf ("%s: clock multiplier found\n", sym_name(np));
403 np->multiplier = mult;
407 * If multiplier not found or scntl3 not 7,5,3,
408 * reset chip and get frequency from general purpose timer.
409 * Otherwise trust scntl3 BIOS setting.
411 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
412 OUTB(np, nc_stest1, 0); /* make sure doubler is OFF */
413 f1 = sym_getfreq (np);
416 printf ("%s: chip clock is %uKHz\n", sym_name(np), f1);
418 if (f1 < 45000) f1 = 40000;
419 else if (f1 < 55000) f1 = 50000;
422 if (f1 < 80000 && mult > 1) {
423 if (sym_verbose >= 2)
424 printf ("%s: clock multiplier assumed\n",
426 np->multiplier = mult;
429 if ((scntl3 & 7) == 3) f1 = 40000;
430 else if ((scntl3 & 7) == 5) f1 = 80000;
433 f1 /= np->multiplier;
437 * Compute controller synchronous parameters.
439 f1 *= np->multiplier;
444 * Get/probe PCI clock frequency
446 static int sym_getpciclock (struct sym_hcb *np)
451 * For now, we only need to know about the actual
452 * PCI BUS clock frequency for C1010-66 chips.
455 if (np->features & FE_66MHZ) {
459 OUTB(np, nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */
461 OUTB(np, nc_stest1, 0);
469 * SYMBIOS chip clock divisor table.
471 * Divisors are multiplied by 10,000,000 in order to make
472 * calculations more simple.
475 static const u32 div_10M[] = {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
478 * Get clock factor and sync divisor for a given
479 * synchronous factor period.
482 sym_getsync(struct sym_hcb *np, u_char dt, u_char sfac, u_char *divp, u_char *fakp)
484 u32 clk = np->clock_khz; /* SCSI clock frequency in kHz */
485 int div = np->clock_divn; /* Number of divisors supported */
486 u32 fak; /* Sync factor in sxfer */
487 u32 per; /* Period in tenths of ns */
488 u32 kpc; /* (per * clk) */
492 * Compute the synchronous period in tenths of nano-seconds
494 if (dt && sfac <= 9) per = 125;
495 else if (sfac <= 10) per = 250;
496 else if (sfac == 11) per = 303;
497 else if (sfac == 12) per = 500;
498 else per = 40 * sfac;
506 * For earliest C10 revision 0, we cannot use extra
507 * clocks for the setting of the SCSI clocking.
508 * Note that this limits the lowest sync data transfer
509 * to 5 Mega-transfers per second and may result in
510 * using higher clock divisors.
513 if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) {
515 * Look for the lowest clock divisor that allows an
516 * output speed not faster than the period.
520 if (kpc > (div_10M[div] << 2)) {
525 fak = 0; /* No extra clocks */
526 if (div == np->clock_divn) { /* Are we too fast ? */
536 * Look for the greatest clock divisor that allows an
537 * input speed faster than the period.
540 if (kpc >= (div_10M[div] << 2)) break;
543 * Calculate the lowest clock factor that allows an output
544 * speed not faster than the period, and the max output speed.
545 * If fak >= 1 we will set both XCLKH_ST and XCLKH_DT.
546 * If fak >= 2 we will also set XCLKS_ST and XCLKS_DT.
549 fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2;
550 /* ret = ((2+fak)*div_10M[div])/np->clock_khz; */
552 fak = (kpc - 1) / div_10M[div] + 1 - 4;
553 /* ret = ((4+fak)*div_10M[div])/np->clock_khz; */
557 * Check against our hardware limits, or bugs :).
565 * Compute and return sync parameters.
574 * SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64,
575 * 128 transfers. All chips support at least 16 transfers
576 * bursts. The 825A, 875 and 895 chips support bursts of up
577 * to 128 transfers and the 895A and 896 support bursts of up
578 * to 64 transfers. All other chips support up to 16
581 * For PCI 32 bit data transfers each transfer is a DWORD.
582 * It is a QUADWORD (8 bytes) for PCI 64 bit data transfers.
584 * We use log base 2 (burst length) as internal code, with
585 * value 0 meaning "burst disabled".
589 * Burst length from burst code.
591 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
594 * Burst code from io register bits.
596 #define burst_code(dmode, ctest4, ctest5) \
597 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
600 * Set initial io register bits from burst code.
602 static __inline void sym_init_burst(struct sym_hcb *np, u_char bc)
604 np->rv_ctest4 &= ~0x80;
605 np->rv_dmode &= ~(0x3 << 6);
606 np->rv_ctest5 &= ~0x4;
609 np->rv_ctest4 |= 0x80;
613 np->rv_dmode |= ((bc & 0x3) << 6);
614 np->rv_ctest5 |= (bc & 0x4);
619 * Save initial settings of some IO registers.
620 * Assumed to have been set by BIOS.
621 * We cannot reset the chip prior to reading the
622 * IO registers, since informations will be lost.
623 * Since the SCRIPTS processor may be running, this
624 * is not safe on paper, but it seems to work quite
627 static void sym_save_initial_setting (struct sym_hcb *np)
629 np->sv_scntl0 = INB(np, nc_scntl0) & 0x0a;
630 np->sv_scntl3 = INB(np, nc_scntl3) & 0x07;
631 np->sv_dmode = INB(np, nc_dmode) & 0xce;
632 np->sv_dcntl = INB(np, nc_dcntl) & 0xa8;
633 np->sv_ctest3 = INB(np, nc_ctest3) & 0x01;
634 np->sv_ctest4 = INB(np, nc_ctest4) & 0x80;
635 np->sv_gpcntl = INB(np, nc_gpcntl);
636 np->sv_stest1 = INB(np, nc_stest1);
637 np->sv_stest2 = INB(np, nc_stest2) & 0x20;
638 np->sv_stest4 = INB(np, nc_stest4);
639 if (np->features & FE_C10) { /* Always large DMA fifo + ultra3 */
640 np->sv_scntl4 = INB(np, nc_scntl4);
641 np->sv_ctest5 = INB(np, nc_ctest5) & 0x04;
644 np->sv_ctest5 = INB(np, nc_ctest5) & 0x24;
648 * Prepare io register values used by sym_start_up()
649 * according to selected and supported features.
651 static int sym_prepare_setting(struct Scsi_Host *shost, struct sym_hcb *np, struct sym_nvram *nvram)
660 np->maxwide = (np->features & FE_WIDE)? 1 : 0;
663 * Guess the frequency of the chip's clock.
665 if (np->features & (FE_ULTRA3 | FE_ULTRA2))
666 np->clock_khz = 160000;
667 else if (np->features & FE_ULTRA)
668 np->clock_khz = 80000;
670 np->clock_khz = 40000;
673 * Get the clock multiplier factor.
675 if (np->features & FE_QUAD)
677 else if (np->features & FE_DBLR)
683 * Measure SCSI clock frequency for chips
684 * it may vary from assumed one.
686 if (np->features & FE_VARCLK)
687 sym_getclock(np, np->multiplier);
690 * Divisor to be used for async (timer pre-scaler).
692 i = np->clock_divn - 1;
694 if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) {
702 * The C1010 uses hardwired divisors for async.
703 * So, we just throw away, the async. divisor.:-)
705 if (np->features & FE_C10)
709 * Minimum synchronous period factor supported by the chip.
710 * Btw, 'period' is in tenths of nanoseconds.
712 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
714 if (period <= 250) np->minsync = 10;
715 else if (period <= 303) np->minsync = 11;
716 else if (period <= 500) np->minsync = 12;
717 else np->minsync = (period + 40 - 1) / 40;
720 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
722 if (np->minsync < 25 &&
723 !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
725 else if (np->minsync < 12 &&
726 !(np->features & (FE_ULTRA2|FE_ULTRA3)))
730 * Maximum synchronous period factor supported by the chip.
732 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
733 np->maxsync = period > 2540 ? 254 : period / 10;
736 * If chip is a C1010, guess the sync limits in DT mode.
738 if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) {
739 if (np->clock_khz == 160000) {
742 np->maxoffs_dt = nvram->type ? 62 : 31;
747 * 64 bit addressing (895A/896/1010) ?
749 if (np->features & FE_DAC) {
750 #if SYM_CONF_DMA_ADDRESSING_MODE == 0
751 np->rv_ccntl1 |= (DDAC);
752 #elif SYM_CONF_DMA_ADDRESSING_MODE == 1
754 np->rv_ccntl1 |= (DDAC);
756 np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
757 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
759 np->rv_ccntl1 |= (DDAC);
761 np->rv_ccntl1 |= (0 | EXTIBMV);
766 * Phase mismatch handled by SCRIPTS (895A/896/1010) ?
768 if (np->features & FE_NOPM)
769 np->rv_ccntl0 |= (ENPMJ);
772 * C1010-33 Errata: Part Number:609-039638 (rev. 1) is fixed.
773 * In dual channel mode, contention occurs if internal cycles
774 * are used. Disable internal cycles.
776 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
777 np->revision_id < 0x1)
778 np->rv_ccntl0 |= DILS;
781 * Select burst length (dwords)
783 burst_max = SYM_SETUP_BURST_ORDER;
784 if (burst_max == 255)
785 burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
789 if (burst_max > np->maxburst)
790 burst_max = np->maxburst;
793 * DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2.
794 * This chip and the 860 Rev 1 may wrongly use PCI cache line
795 * based transactions on LOAD/STORE instructions. So we have
796 * to prevent these chips from using such PCI transactions in
797 * this driver. The generic ncr driver that does not use
798 * LOAD/STORE instructions does not need this work-around.
800 if ((np->device_id == PCI_DEVICE_ID_NCR_53C810 &&
801 np->revision_id >= 0x10 && np->revision_id <= 0x11) ||
802 (np->device_id == PCI_DEVICE_ID_NCR_53C860 &&
803 np->revision_id <= 0x1))
804 np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
807 * Select all supported special features.
808 * If we are using on-board RAM for scripts, prefetch (PFEN)
809 * does not help, but burst op fetch (BOF) does.
810 * Disabling PFEN makes sure BOF will be used.
812 if (np->features & FE_ERL)
813 np->rv_dmode |= ERL; /* Enable Read Line */
814 if (np->features & FE_BOF)
815 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
816 if (np->features & FE_ERMP)
817 np->rv_dmode |= ERMP; /* Enable Read Multiple */
819 if ((np->features & FE_PFEN) && !np->ram_ba)
821 if (np->features & FE_PFEN)
823 np->rv_dcntl |= PFEN; /* Prefetch Enable */
824 if (np->features & FE_CLSE)
825 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
826 if (np->features & FE_WRIE)
827 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
828 if (np->features & FE_DFS)
829 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
834 np->rv_ctest4 |= MPEE; /* Master parity checking */
835 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
838 * Get parity checking, host ID and verbose mode from NVRAM
841 sym_nvram_setup_host(shost, np, nvram);
844 * Get SCSI addr of host adapter (set by bios?).
846 if (np->myaddr == 255) {
847 np->myaddr = INB(np, nc_scid) & 0x07;
849 np->myaddr = SYM_SETUP_HOST_ID;
853 * Prepare initial io register bits for burst length
855 sym_init_burst(np, burst_max);
859 * - LVD capable chips (895/895A/896/1010) report the
860 * current BUS mode through the STEST4 IO register.
861 * - For previous generation chips (825/825A/875),
862 * user has to tell us how to check against HVD,
863 * since a 100% safe algorithm is not possible.
865 np->scsi_mode = SMODE_SE;
866 if (np->features & (FE_ULTRA2|FE_ULTRA3))
867 np->scsi_mode = (np->sv_stest4 & SMODE);
868 else if (np->features & FE_DIFF) {
869 if (SYM_SETUP_SCSI_DIFF == 1) {
871 if (np->sv_stest2 & 0x20)
872 np->scsi_mode = SMODE_HVD;
874 else if (nvram->type == SYM_SYMBIOS_NVRAM) {
875 if (!(INB(np, nc_gpreg) & 0x08))
876 np->scsi_mode = SMODE_HVD;
879 else if (SYM_SETUP_SCSI_DIFF == 2)
880 np->scsi_mode = SMODE_HVD;
882 if (np->scsi_mode == SMODE_HVD)
883 np->rv_stest2 |= 0x20;
886 * Set LED support from SCRIPTS.
887 * Ignore this feature for boards known to use a
888 * specific GPIO wiring and for the 895A, 896
889 * and 1010 that drive the LED directly.
891 if ((SYM_SETUP_SCSI_LED ||
892 (nvram->type == SYM_SYMBIOS_NVRAM ||
893 (nvram->type == SYM_TEKRAM_NVRAM &&
894 np->device_id == PCI_DEVICE_ID_NCR_53C895))) &&
895 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
896 np->features |= FE_LED0;
901 switch(SYM_SETUP_IRQ_MODE & 3) {
903 np->rv_dcntl |= IRQM;
906 np->rv_dcntl |= (np->sv_dcntl & IRQM);
913 * Configure targets according to driver setup.
914 * If NVRAM present get targets setup from NVRAM.
916 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
917 struct sym_tcb *tp = &np->target[i];
919 tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
920 tp->usrtags = SYM_SETUP_MAX_TAG;
921 tp->usr_width = np->maxwide;
924 sym_nvram_setup_target(tp, i, nvram);
927 tp->usrflags &= ~SYM_TAGS_ENABLED;
931 * Let user know about the settings.
933 printf("%s: %s, ID %d, Fast-%d, %s, %s\n", sym_name(np),
934 sym_nvram_type(nvram), np->myaddr,
935 (np->features & FE_ULTRA3) ? 80 :
936 (np->features & FE_ULTRA2) ? 40 :
937 (np->features & FE_ULTRA) ? 20 : 10,
938 sym_scsi_bus_mode(np->scsi_mode),
939 (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
941 * Tell him more on demand.
944 printf("%s: %s IRQ line driver%s\n",
946 np->rv_dcntl & IRQM ? "totem pole" : "open drain",
947 np->ram_ba ? ", using on-chip SRAM" : "");
948 printf("%s: using %s firmware.\n", sym_name(np), np->fw_name);
949 if (np->features & FE_NOPM)
950 printf("%s: handling phase mismatch from SCRIPTS.\n",
956 if (sym_verbose >= 2) {
957 printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
958 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
959 sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
960 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
962 printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
963 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
964 sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
965 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
972 * Test the pci bus snoop logic :-(
974 * Has to be called with interrupts disabled.
976 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
977 static int sym_regtest(struct sym_hcb *np)
979 register volatile u32 data;
981 * chip registers may NOT be cached.
982 * write 0xffffffff to a read only register area,
983 * and try to read it back.
986 OUTL(np, nc_dstat, data);
987 data = INL(np, nc_dstat);
989 if (data == 0xffffffff) {
991 if ((data & 0xe2f0fffd) != 0x02000080) {
993 printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
1000 static inline int sym_regtest(struct sym_hcb *np)
1006 static int sym_snooptest(struct sym_hcb *np)
1008 u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
1011 err = sym_regtest(np);
1016 * Enable Master Parity Checking as we intend
1017 * to enable it for normal operations.
1019 OUTB(np, nc_ctest4, (np->rv_ctest4 & MPEE));
1023 pc = SCRIPTZ_BA(np, snooptest);
1027 * Set memory and register.
1029 np->scratch = cpu_to_scr(host_wr);
1030 OUTL(np, nc_temp, sym_wr);
1032 * Start script (exchange values)
1034 OUTL(np, nc_dsa, np->hcb_ba);
1037 * Wait 'til done (with timeout)
1039 for (i=0; i<SYM_SNOOP_TIMEOUT; i++)
1040 if (INB(np, nc_istat) & (INTF|SIP|DIP))
1042 if (i>=SYM_SNOOP_TIMEOUT) {
1043 printf ("CACHE TEST FAILED: timeout.\n");
1047 * Check for fatal DMA errors.
1049 dstat = INB(np, nc_dstat);
1050 #if 1 /* Band aiding for broken hardwares that fail PCI parity */
1051 if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
1052 printf ("%s: PCI DATA PARITY ERROR DETECTED - "
1053 "DISABLING MASTER DATA PARITY CHECKING.\n",
1055 np->rv_ctest4 &= ~MPEE;
1059 if (dstat & (MDPE|BF|IID)) {
1060 printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
1064 * Save termination position.
1066 pc = INL(np, nc_dsp);
1068 * Read memory and register.
1070 host_rd = scr_to_cpu(np->scratch);
1071 sym_rd = INL(np, nc_scratcha);
1072 sym_bk = INL(np, nc_temp);
1074 * Check termination position.
1076 if (pc != SCRIPTZ_BA(np, snoopend)+8) {
1077 printf ("CACHE TEST FAILED: script execution failed.\n");
1078 printf ("start=%08lx, pc=%08lx, end=%08lx\n",
1079 (u_long) SCRIPTZ_BA(np, snooptest), (u_long) pc,
1080 (u_long) SCRIPTZ_BA(np, snoopend) +8);
1086 if (host_wr != sym_rd) {
1087 printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n",
1088 (int) host_wr, (int) sym_rd);
1091 if (host_rd != sym_wr) {
1092 printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n",
1093 (int) sym_wr, (int) host_rd);
1096 if (sym_bk != sym_wr) {
1097 printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n",
1098 (int) sym_wr, (int) sym_bk);
1106 * log message for real hard errors
1108 * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sx/s3/s4) @ name (dsp:dbc).
1109 * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf.
1111 * exception register:
1116 * so: control lines as driven by chip.
1117 * si: control lines as seen by chip.
1118 * sd: scsi data lines as seen by chip.
1121 * sx: sxfer (see the manual)
1122 * s3: scntl3 (see the manual)
1123 * s4: scntl4 (see the manual)
1125 * current script command:
1126 * dsp: script address (relative to start of script).
1127 * dbc: first word of script command.
1129 * First 24 register of the chip:
1132 static void sym_log_hard_error(struct sym_hcb *np, u_short sist, u_char dstat)
1138 u_char *script_base;
1141 dsp = INL(np, nc_dsp);
1143 if (dsp > np->scripta_ba &&
1144 dsp <= np->scripta_ba + np->scripta_sz) {
1145 script_ofs = dsp - np->scripta_ba;
1146 script_size = np->scripta_sz;
1147 script_base = (u_char *) np->scripta0;
1148 script_name = "scripta";
1150 else if (np->scriptb_ba < dsp &&
1151 dsp <= np->scriptb_ba + np->scriptb_sz) {
1152 script_ofs = dsp - np->scriptb_ba;
1153 script_size = np->scriptb_sz;
1154 script_base = (u_char *) np->scriptb0;
1155 script_name = "scriptb";
1160 script_name = "mem";
1163 printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x/%x) @ (%s %x:%08x).\n",
1164 sym_name(np), (unsigned)INB(np, nc_sdid)&0x0f, dstat, sist,
1165 (unsigned)INB(np, nc_socl), (unsigned)INB(np, nc_sbcl),
1166 (unsigned)INB(np, nc_sbdl), (unsigned)INB(np, nc_sxfer),
1167 (unsigned)INB(np, nc_scntl3),
1168 (np->features & FE_C10) ? (unsigned)INB(np, nc_scntl4) : 0,
1169 script_name, script_ofs, (unsigned)INL(np, nc_dbc));
1171 if (((script_ofs & 3) == 0) &&
1172 (unsigned)script_ofs < script_size) {
1173 printf ("%s: script cmd = %08x\n", sym_name(np),
1174 scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
1177 printf ("%s: regdump:", sym_name(np));
1179 printf (" %02x", (unsigned)INB_OFF(np, i));
1185 if (dstat & (MDPE|BF))
1186 sym_log_bus_error(np);
1189 static struct sym_chip sym_dev_table[] = {
1190 {PCI_DEVICE_ID_NCR_53C810, 0x0f, "810", 4, 8, 4, 64,
1193 #ifdef SYM_DEBUG_GENERIC_SUPPORT
1194 {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
1198 {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
1199 FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
1202 {PCI_DEVICE_ID_NCR_53C815, 0xff, "815", 4, 8, 4, 64,
1205 {PCI_DEVICE_ID_NCR_53C825, 0x0f, "825", 6, 8, 4, 64,
1206 FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
1208 {PCI_DEVICE_ID_NCR_53C825, 0xff, "825a", 6, 8, 4, 2,
1209 FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
1211 {PCI_DEVICE_ID_NCR_53C860, 0xff, "860", 4, 8, 5, 1,
1212 FE_ULTRA|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
1214 {PCI_DEVICE_ID_NCR_53C875, 0x01, "875", 6, 16, 5, 2,
1215 FE_WIDE|FE_ULTRA|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1216 FE_RAM|FE_DIFF|FE_VARCLK}
1218 {PCI_DEVICE_ID_NCR_53C875, 0xff, "875", 6, 16, 5, 2,
1219 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1220 FE_RAM|FE_DIFF|FE_VARCLK}
1222 {PCI_DEVICE_ID_NCR_53C875J, 0xff, "875J", 6, 16, 5, 2,
1223 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1224 FE_RAM|FE_DIFF|FE_VARCLK}
1226 {PCI_DEVICE_ID_NCR_53C885, 0xff, "885", 6, 16, 5, 2,
1227 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1228 FE_RAM|FE_DIFF|FE_VARCLK}
1230 #ifdef SYM_DEBUG_GENERIC_SUPPORT
1231 {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
1232 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
1236 {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
1237 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1241 {PCI_DEVICE_ID_NCR_53C896, 0xff, "896", 6, 31, 7, 4,
1242 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1243 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1245 {PCI_DEVICE_ID_LSI_53C895A, 0xff, "895a", 6, 31, 7, 4,
1246 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1247 FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1249 {PCI_DEVICE_ID_LSI_53C875A, 0xff, "875a", 6, 31, 7, 4,
1250 FE_WIDE|FE_ULTRA|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1251 FE_RAM|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1253 {PCI_DEVICE_ID_LSI_53C1010_33, 0x00, "1010-33", 6, 31, 7, 8,
1254 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1255 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
1258 {PCI_DEVICE_ID_LSI_53C1010_33, 0xff, "1010-33", 6, 31, 7, 8,
1259 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1260 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
1263 {PCI_DEVICE_ID_LSI_53C1010_66, 0xff, "1010-66", 6, 31, 7, 8,
1264 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1265 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
1268 {PCI_DEVICE_ID_LSI_53C1510, 0xff, "1510d", 6, 31, 7, 4,
1269 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1270 FE_RAM|FE_IO256|FE_LEDC}
1273 #define sym_num_devs \
1274 (sizeof(sym_dev_table) / sizeof(sym_dev_table[0]))
1277 * Look up the chip table.
1279 * Return a pointer to the chip entry if found,
1283 sym_lookup_chip_table (u_short device_id, u_char revision)
1285 struct sym_chip *chip;
1288 for (i = 0; i < sym_num_devs; i++) {
1289 chip = &sym_dev_table[i];
1290 if (device_id != chip->device_id)
1292 if (revision > chip->revision_id)
1300 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1302 * Lookup the 64 bit DMA segments map.
1303 * This is only used if the direct mapping
1304 * has been unsuccessful.
1306 int sym_lookup_dmap(struct sym_hcb *np, u32 h, int s)
1313 /* Look up existing mappings */
1314 for (i = SYM_DMAP_SIZE-1; i > 0; i--) {
1315 if (h == np->dmap_bah[i])
1318 /* If direct mapping is free, get it */
1319 if (!np->dmap_bah[s])
1321 /* Collision -> lookup free mappings */
1322 for (s = SYM_DMAP_SIZE-1; s > 0; s--) {
1323 if (!np->dmap_bah[s])
1327 panic("sym: ran out of 64 bit DMA segment registers");
1330 np->dmap_bah[s] = h;
1336 * Update IO registers scratch C..R so they will be
1337 * in sync. with queued CCB expectations.
1339 static void sym_update_dmap_regs(struct sym_hcb *np)
1343 if (!np->dmap_dirty)
1345 o = offsetof(struct sym_reg, nc_scrx[0]);
1346 for (i = 0; i < SYM_DMAP_SIZE; i++) {
1347 OUTL_OFF(np, o, np->dmap_bah[i]);
1354 /* Enforce all the fiddly SPI rules and the chip limitations */
1355 static void sym_check_goals(struct sym_hcb *np, struct scsi_target *starget,
1356 struct sym_trans *goal)
1358 if (!spi_support_wide(starget))
1361 if (!spi_support_sync(starget)) {
1369 if (spi_support_dt(starget)) {
1370 if (spi_support_dt_only(starget))
1373 if (goal->offset == 0)
1379 /* Some targets fail to properly negotiate DT in SE mode */
1380 if ((np->scsi_mode != SMODE_LVD) || !(np->features & FE_U3EN))
1384 /* all DT transfers must be wide */
1386 if (goal->offset > np->maxoffs_dt)
1387 goal->offset = np->maxoffs_dt;
1388 if (goal->period < np->minsync_dt)
1389 goal->period = np->minsync_dt;
1390 if (goal->period > np->maxsync_dt)
1391 goal->period = np->maxsync_dt;
1393 goal->iu = goal->qas = 0;
1394 if (goal->offset > np->maxoffs)
1395 goal->offset = np->maxoffs;
1396 if (goal->period < np->minsync)
1397 goal->period = np->minsync;
1398 if (goal->period > np->maxsync)
1399 goal->period = np->maxsync;
1404 * Prepare the next negotiation message if needed.
1406 * Fill in the part of message buffer that contains the
1407 * negotiation and the nego_status field of the CCB.
1408 * Returns the size of the message in bytes.
1410 static int sym_prepare_nego(struct sym_hcb *np, struct sym_ccb *cp, u_char *msgptr)
1412 struct sym_tcb *tp = &np->target[cp->target];
1413 struct scsi_target *starget = tp->starget;
1414 struct sym_trans *goal = &tp->tgoal;
1418 sym_check_goals(np, starget, goal);
1421 * Many devices implement PPR in a buggy way, so only use it if we
1425 (goal->iu || goal->dt || goal->qas || (goal->period < 0xa))) {
1427 } else if (spi_width(starget) != goal->width) {
1429 } else if (spi_period(starget) != goal->period ||
1430 spi_offset(starget) != goal->offset) {
1433 goal->check_nego = 0;
1439 msglen += spi_populate_sync_msg(msgptr + msglen, goal->period,
1443 msglen += spi_populate_width_msg(msgptr + msglen, goal->width);
1446 msglen += spi_populate_ppr_msg(msgptr + msglen, goal->period,
1447 goal->offset, goal->width,
1448 (goal->iu ? PPR_OPT_IU : 0) |
1449 (goal->dt ? PPR_OPT_DT : 0) |
1450 (goal->qas ? PPR_OPT_QAS : 0));
1454 cp->nego_status = nego;
1457 tp->nego_cp = cp; /* Keep track a nego will be performed */
1458 if (DEBUG_FLAGS & DEBUG_NEGO) {
1459 sym_print_nego_msg(np, cp->target,
1460 nego == NS_SYNC ? "sync msgout" :
1461 nego == NS_WIDE ? "wide msgout" :
1462 "ppr msgout", msgptr);
1470 * Insert a job into the start queue.
1472 static void sym_put_start_queue(struct sym_hcb *np, struct sym_ccb *cp)
1476 #ifdef SYM_CONF_IARB_SUPPORT
1478 * If the previously queued CCB is not yet done,
1479 * set the IARB hint. The SCRIPTS will go with IARB
1480 * for this job when starting the previous one.
1481 * We leave devices a chance to win arbitration by
1482 * not using more than 'iarb_max' consecutive
1483 * immediate arbitrations.
1485 if (np->last_cp && np->iarb_count < np->iarb_max) {
1486 np->last_cp->host_flags |= HF_HINT_IARB;
1494 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1496 * Make SCRIPTS aware of the 64 bit DMA
1497 * segment registers not being up-to-date.
1500 cp->host_xflags |= HX_DMAP_DIRTY;
1504 * Insert first the idle task and then our job.
1505 * The MBs should ensure proper ordering.
1507 qidx = np->squeueput + 2;
1508 if (qidx >= MAX_QUEUE*2) qidx = 0;
1510 np->squeue [qidx] = cpu_to_scr(np->idletask_ba);
1511 MEMORY_WRITE_BARRIER();
1512 np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba);
1514 np->squeueput = qidx;
1516 if (DEBUG_FLAGS & DEBUG_QUEUE)
1517 printf ("%s: queuepos=%d.\n", sym_name (np), np->squeueput);
1520 * Script processor may be waiting for reselect.
1523 MEMORY_WRITE_BARRIER();
1524 OUTB(np, nc_istat, SIGP|np->istat_sem);
1527 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1529 * Start next ready-to-start CCBs.
1531 void sym_start_next_ccbs(struct sym_hcb *np, struct sym_lcb *lp, int maxn)
1537 * Paranoia, as usual. :-)
1539 assert(!lp->started_tags || !lp->started_no_tag);
1542 * Try to start as many commands as asked by caller.
1543 * Prevent from having both tagged and untagged
1544 * commands queued to the device at the same time.
1547 qp = sym_remque_head(&lp->waiting_ccbq);
1550 cp = sym_que_entry(qp, struct sym_ccb, link2_ccbq);
1551 if (cp->tag != NO_TAG) {
1552 if (lp->started_no_tag ||
1553 lp->started_tags >= lp->started_max) {
1554 sym_insque_head(qp, &lp->waiting_ccbq);
1557 lp->itlq_tbl[cp->tag] = cpu_to_scr(cp->ccb_ba);
1559 cpu_to_scr(SCRIPTA_BA(np, resel_tag));
1562 if (lp->started_no_tag || lp->started_tags) {
1563 sym_insque_head(qp, &lp->waiting_ccbq);
1566 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
1568 cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
1569 ++lp->started_no_tag;
1572 sym_insque_tail(qp, &lp->started_ccbq);
1573 sym_put_start_queue(np, cp);
1576 #endif /* SYM_OPT_HANDLE_DEVICE_QUEUEING */
1579 * The chip may have completed jobs. Look at the DONE QUEUE.
1581 * On paper, memory read barriers may be needed here to
1582 * prevent out of order LOADs by the CPU from having
1583 * prefetched stale data prior to DMA having occurred.
1585 static int sym_wakeup_done (struct sym_hcb *np)
1594 /* MEMORY_READ_BARRIER(); */
1596 dsa = scr_to_cpu(np->dqueue[i]);
1600 if ((i = i+2) >= MAX_QUEUE*2)
1603 cp = sym_ccb_from_dsa(np, dsa);
1605 MEMORY_READ_BARRIER();
1606 sym_complete_ok (np, cp);
1610 printf ("%s: bad DSA (%x) in done queue.\n",
1611 sym_name(np), (u_int) dsa);
1619 * Complete all CCBs queued to the COMP queue.
1621 * These CCBs are assumed:
1622 * - Not to be referenced either by devices or
1623 * SCRIPTS-related queues and datas.
1624 * - To have to be completed with an error condition
1627 * The device queue freeze count is incremented
1628 * for each CCB that does not prevent this.
1629 * This function is called when all CCBs involved
1630 * in error handling/recovery have been reaped.
1632 static void sym_flush_comp_queue(struct sym_hcb *np, int cam_status)
1637 while ((qp = sym_remque_head(&np->comp_ccbq)) != 0) {
1638 struct scsi_cmnd *cmd;
1639 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
1640 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
1641 /* Leave quiet CCBs waiting for resources */
1642 if (cp->host_status == HS_WAIT)
1646 sym_set_cam_status(cmd, cam_status);
1647 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1648 if (sym_get_cam_status(cmd) == DID_SOFT_ERROR) {
1649 struct sym_tcb *tp = &np->target[cp->target];
1650 struct sym_lcb *lp = sym_lp(tp, cp->lun);
1652 sym_remque(&cp->link2_ccbq);
1653 sym_insque_tail(&cp->link2_ccbq,
1656 if (cp->tag != NO_TAG)
1659 --lp->started_no_tag;
1666 sym_free_ccb(np, cp);
1667 sym_xpt_done(np, cmd);
1672 * Complete all active CCBs with error.
1673 * Used on CHIP/SCSI RESET.
1675 static void sym_flush_busy_queue (struct sym_hcb *np, int cam_status)
1678 * Move all active CCBs to the COMP queue
1679 * and flush this queue.
1681 sym_que_splice(&np->busy_ccbq, &np->comp_ccbq);
1682 sym_que_init(&np->busy_ccbq);
1683 sym_flush_comp_queue(np, cam_status);
1690 * 0: initialisation.
1691 * 1: SCSI BUS RESET delivered or received.
1692 * 2: SCSI BUS MODE changed.
1694 void sym_start_up (struct sym_hcb *np, int reason)
1700 * Reset chip if asked, otherwise just clear fifos.
1705 OUTB(np, nc_stest3, TE|CSF);
1706 OUTONB(np, nc_ctest3, CLF);
1712 phys = np->squeue_ba;
1713 for (i = 0; i < MAX_QUEUE*2; i += 2) {
1714 np->squeue[i] = cpu_to_scr(np->idletask_ba);
1715 np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
1717 np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
1720 * Start at first entry.
1727 phys = np->dqueue_ba;
1728 for (i = 0; i < MAX_QUEUE*2; i += 2) {
1730 np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
1732 np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
1735 * Start at first entry.
1740 * Install patches in scripts.
1741 * This also let point to first position the start
1742 * and done queue pointers used from SCRIPTS.
1747 * Wakeup all pending jobs.
1749 sym_flush_busy_queue(np, DID_RESET);
1754 OUTB(np, nc_istat, 0x00); /* Remove Reset, abort */
1756 udelay(2000); /* The 895 needs time for the bus mode to settle */
1758 OUTB(np, nc_scntl0, np->rv_scntl0 | 0xc0);
1759 /* full arb., ena parity, par->ATN */
1760 OUTB(np, nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
1762 sym_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
1764 OUTB(np, nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
1765 OUTW(np, nc_respid, 1ul<<np->myaddr); /* Id to respond to */
1766 OUTB(np, nc_istat , SIGP ); /* Signal Process */
1767 OUTB(np, nc_dmode , np->rv_dmode); /* Burst length, dma mode */
1768 OUTB(np, nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
1770 OUTB(np, nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
1771 OUTB(np, nc_ctest3, np->rv_ctest3); /* Write and invalidate */
1772 OUTB(np, nc_ctest4, np->rv_ctest4); /* Master parity checking */
1774 /* Extended Sreq/Sack filtering not supported on the C10 */
1775 if (np->features & FE_C10)
1776 OUTB(np, nc_stest2, np->rv_stest2);
1778 OUTB(np, nc_stest2, EXT|np->rv_stest2);
1780 OUTB(np, nc_stest3, TE); /* TolerANT enable */
1781 OUTB(np, nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */
1784 * For now, disable AIP generation on C1010-66.
1786 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)
1787 OUTB(np, nc_aipcntl1, DISAIP);
1790 * C10101 rev. 0 errata.
1791 * Errant SGE's when in narrow. Write bits 4 & 5 of
1792 * STEST1 register to disable SGE. We probably should do
1793 * that from SCRIPTS for each selection/reselection, but
1794 * I just don't want. :)
1796 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
1797 np->revision_id < 1)
1798 OUTB(np, nc_stest1, INB(np, nc_stest1) | 0x30);
1801 * DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
1802 * Disable overlapped arbitration for some dual function devices,
1803 * regardless revision id (kind of post-chip-design feature. ;-))
1805 if (np->device_id == PCI_DEVICE_ID_NCR_53C875)
1806 OUTB(np, nc_ctest0, (1<<5));
1807 else if (np->device_id == PCI_DEVICE_ID_NCR_53C896)
1808 np->rv_ccntl0 |= DPR;
1811 * Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing
1812 * and/or hardware phase mismatch, since only such chips
1813 * seem to support those IO registers.
1815 if (np->features & (FE_DAC|FE_NOPM)) {
1816 OUTB(np, nc_ccntl0, np->rv_ccntl0);
1817 OUTB(np, nc_ccntl1, np->rv_ccntl1);
1820 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1822 * Set up scratch C and DRS IO registers to map the 32 bit
1823 * DMA address range our data structures are located in.
1826 np->dmap_bah[0] = 0; /* ??? */
1827 OUTL(np, nc_scrx[0], np->dmap_bah[0]);
1828 OUTL(np, nc_drs, np->dmap_bah[0]);
1833 * If phase mismatch handled by scripts (895A/896/1010),
1834 * set PM jump addresses.
1836 if (np->features & FE_NOPM) {
1837 OUTL(np, nc_pmjad1, SCRIPTB_BA(np, pm_handle));
1838 OUTL(np, nc_pmjad2, SCRIPTB_BA(np, pm_handle));
1842 * Enable GPIO0 pin for writing if LED support from SCRIPTS.
1843 * Also set GPIO5 and clear GPIO6 if hardware LED control.
1845 if (np->features & FE_LED0)
1846 OUTB(np, nc_gpcntl, INB(np, nc_gpcntl) & ~0x01);
1847 else if (np->features & FE_LEDC)
1848 OUTB(np, nc_gpcntl, (INB(np, nc_gpcntl) & ~0x41) | 0x20);
1853 OUTW(np, nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
1854 OUTB(np, nc_dien , MDPE|BF|SSI|SIR|IID);
1857 * For 895/6 enable SBMC interrupt and save current SCSI bus mode.
1858 * Try to eat the spurious SBMC interrupt that may occur when
1859 * we reset the chip but not the SCSI BUS (at initialization).
1861 if (np->features & (FE_ULTRA2|FE_ULTRA3)) {
1862 OUTONW(np, nc_sien, SBMC);
1868 np->scsi_mode = INB(np, nc_stest4) & SMODE;
1872 * Fill in target structure.
1873 * Reinitialize usrsync.
1874 * Reinitialize usrwide.
1875 * Prepare sync negotiation according to actual SCSI bus mode.
1877 for (i=0;i<SYM_CONF_MAX_TARGET;i++) {
1878 struct sym_tcb *tp = &np->target[i];
1882 tp->head.wval = np->rv_scntl3;
1887 * Download SCSI SCRIPTS to on-chip RAM if present,
1888 * and start script processor.
1889 * We do the download preferently from the CPU.
1890 * For platforms that may not support PCI memory mapping,
1891 * we use simple SCRIPTS that performs MEMORY MOVEs.
1893 phys = SCRIPTA_BA(np, init);
1895 if (sym_verbose >= 2)
1896 printf("%s: Downloading SCSI SCRIPTS.\n", sym_name(np));
1897 memcpy_toio(np->s.ramaddr, np->scripta0, np->scripta_sz);
1898 if (np->ram_ws == 8192) {
1899 memcpy_toio(np->s.ramaddr + 4096, np->scriptb0, np->scriptb_sz);
1900 phys = scr_to_cpu(np->scr_ram_seg);
1901 OUTL(np, nc_mmws, phys);
1902 OUTL(np, nc_mmrs, phys);
1903 OUTL(np, nc_sfs, phys);
1904 phys = SCRIPTB_BA(np, start64);
1910 OUTL(np, nc_dsa, np->hcb_ba);
1914 * Notify the XPT about the RESET condition.
1917 sym_xpt_async_bus_reset(np);
1921 * Switch trans mode for current job and its target.
1923 static void sym_settrans(struct sym_hcb *np, int target, u_char opts, u_char ofs,
1924 u_char per, u_char wide, u_char div, u_char fak)
1927 u_char sval, wval, uval;
1928 struct sym_tcb *tp = &np->target[target];
1930 assert(target == (INB(np, nc_sdid) & 0x0f));
1932 sval = tp->head.sval;
1933 wval = tp->head.wval;
1934 uval = tp->head.uval;
1937 printf("XXXX sval=%x wval=%x uval=%x (%x)\n",
1938 sval, wval, uval, np->rv_scntl3);
1943 if (!(np->features & FE_C10))
1944 sval = (sval & ~0x1f) | ofs;
1946 sval = (sval & ~0x3f) | ofs;
1949 * Set the sync divisor and extra clock factor.
1952 wval = (wval & ~0x70) | ((div+1) << 4);
1953 if (!(np->features & FE_C10))
1954 sval = (sval & ~0xe0) | (fak << 5);
1956 uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT);
1957 if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT);
1958 if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT);
1963 * Set the bus width.
1970 * Set misc. ultra enable bits.
1972 if (np->features & FE_C10) {
1973 uval = uval & ~(U3EN|AIPCKEN);
1975 assert(np->features & FE_U3EN);
1979 wval = wval & ~ULTRA;
1980 if (per <= 12) wval |= ULTRA;
1984 * Stop there if sync parameters are unchanged.
1986 if (tp->head.sval == sval &&
1987 tp->head.wval == wval &&
1988 tp->head.uval == uval)
1990 tp->head.sval = sval;
1991 tp->head.wval = wval;
1992 tp->head.uval = uval;
1995 * Disable extended Sreq/Sack filtering if per < 50.
1996 * Not supported on the C1010.
1998 if (per < 50 && !(np->features & FE_C10))
1999 OUTOFFB(np, nc_stest2, EXT);
2002 * set actual value and sync_status
2004 OUTB(np, nc_sxfer, tp->head.sval);
2005 OUTB(np, nc_scntl3, tp->head.wval);
2007 if (np->features & FE_C10) {
2008 OUTB(np, nc_scntl4, tp->head.uval);
2012 * patch ALL busy ccbs of this target.
2014 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
2016 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
2017 if (cp->target != target)
2019 cp->phys.select.sel_scntl3 = tp->head.wval;
2020 cp->phys.select.sel_sxfer = tp->head.sval;
2021 if (np->features & FE_C10) {
2022 cp->phys.select.sel_scntl4 = tp->head.uval;
2028 * We received a WDTR.
2029 * Let everything be aware of the changes.
2031 static void sym_setwide(struct sym_hcb *np, int target, u_char wide)
2033 struct sym_tcb *tp = &np->target[target];
2034 struct scsi_target *starget = tp->starget;
2036 if (spi_width(starget) == wide)
2039 sym_settrans(np, target, 0, 0, 0, wide, 0, 0);
2041 tp->tgoal.width = wide;
2042 spi_offset(starget) = 0;
2043 spi_period(starget) = 0;
2044 spi_width(starget) = wide;
2045 spi_iu(starget) = 0;
2046 spi_dt(starget) = 0;
2047 spi_qas(starget) = 0;
2049 if (sym_verbose >= 3)
2050 spi_display_xfer_agreement(starget);
2054 * We received a SDTR.
2055 * Let everything be aware of the changes.
2058 sym_setsync(struct sym_hcb *np, int target,
2059 u_char ofs, u_char per, u_char div, u_char fak)
2061 struct sym_tcb *tp = &np->target[target];
2062 struct scsi_target *starget = tp->starget;
2063 u_char wide = (tp->head.wval & EWS) ? BUS_16_BIT : BUS_8_BIT;
2065 sym_settrans(np, target, 0, ofs, per, wide, div, fak);
2067 spi_period(starget) = per;
2068 spi_offset(starget) = ofs;
2069 spi_iu(starget) = spi_dt(starget) = spi_qas(starget) = 0;
2071 if (!tp->tgoal.dt && !tp->tgoal.iu && !tp->tgoal.qas) {
2072 tp->tgoal.period = per;
2073 tp->tgoal.offset = ofs;
2074 tp->tgoal.check_nego = 0;
2077 spi_display_xfer_agreement(starget);
2081 * We received a PPR.
2082 * Let everything be aware of the changes.
2085 sym_setpprot(struct sym_hcb *np, int target, u_char opts, u_char ofs,
2086 u_char per, u_char wide, u_char div, u_char fak)
2088 struct sym_tcb *tp = &np->target[target];
2089 struct scsi_target *starget = tp->starget;
2091 sym_settrans(np, target, opts, ofs, per, wide, div, fak);
2093 spi_width(starget) = tp->tgoal.width = wide;
2094 spi_period(starget) = tp->tgoal.period = per;
2095 spi_offset(starget) = tp->tgoal.offset = ofs;
2096 spi_iu(starget) = tp->tgoal.iu = !!(opts & PPR_OPT_IU);
2097 spi_dt(starget) = tp->tgoal.dt = !!(opts & PPR_OPT_DT);
2098 spi_qas(starget) = tp->tgoal.qas = !!(opts & PPR_OPT_QAS);
2099 tp->tgoal.check_nego = 0;
2101 spi_display_xfer_agreement(starget);
2105 * generic recovery from scsi interrupt
2107 * The doc says that when the chip gets an SCSI interrupt,
2108 * it tries to stop in an orderly fashion, by completing
2109 * an instruction fetch that had started or by flushing
2110 * the DMA fifo for a write to memory that was executing.
2111 * Such a fashion is not enough to know if the instruction
2112 * that was just before the current DSP value has been
2115 * There are some small SCRIPTS sections that deal with
2116 * the start queue and the done queue that may break any
2117 * assomption from the C code if we are interrupted
2118 * inside, so we reset if this happens. Btw, since these
2119 * SCRIPTS sections are executed while the SCRIPTS hasn't
2120 * started SCSI operations, it is very unlikely to happen.
2122 * All the driver data structures are supposed to be
2123 * allocated from the same 4 GB memory window, so there
2124 * is a 1 to 1 relationship between DSA and driver data
2125 * structures. Since we are careful :) to invalidate the
2126 * DSA when we complete a command or when the SCRIPTS
2127 * pushes a DSA into a queue, we can trust it when it
2130 static void sym_recover_scsi_int (struct sym_hcb *np, u_char hsts)
2132 u32 dsp = INL(np, nc_dsp);
2133 u32 dsa = INL(np, nc_dsa);
2134 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
2137 * If we haven't been interrupted inside the SCRIPTS
2138 * critical pathes, we can safely restart the SCRIPTS
2139 * and trust the DSA value if it matches a CCB.
2141 if ((!(dsp > SCRIPTA_BA(np, getjob_begin) &&
2142 dsp < SCRIPTA_BA(np, getjob_end) + 1)) &&
2143 (!(dsp > SCRIPTA_BA(np, ungetjob) &&
2144 dsp < SCRIPTA_BA(np, reselect) + 1)) &&
2145 (!(dsp > SCRIPTB_BA(np, sel_for_abort) &&
2146 dsp < SCRIPTB_BA(np, sel_for_abort_1) + 1)) &&
2147 (!(dsp > SCRIPTA_BA(np, done) &&
2148 dsp < SCRIPTA_BA(np, done_end) + 1))) {
2149 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
2150 OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
2152 * If we have a CCB, let the SCRIPTS call us back for
2153 * the handling of the error with SCRATCHA filled with
2154 * STARTPOS. This way, we will be able to freeze the
2155 * device queue and requeue awaiting IOs.
2158 cp->host_status = hsts;
2159 OUTL_DSP(np, SCRIPTA_BA(np, complete_error));
2162 * Otherwise just restart the SCRIPTS.
2165 OUTL(np, nc_dsa, 0xffffff);
2166 OUTL_DSP(np, SCRIPTA_BA(np, start));
2175 sym_start_reset(np);
2179 * chip exception handler for selection timeout
2181 static void sym_int_sto (struct sym_hcb *np)
2183 u32 dsp = INL(np, nc_dsp);
2185 if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
2187 if (dsp == SCRIPTA_BA(np, wf_sel_done) + 8)
2188 sym_recover_scsi_int(np, HS_SEL_TIMEOUT);
2190 sym_start_reset(np);
2194 * chip exception handler for unexpected disconnect
2196 static void sym_int_udc (struct sym_hcb *np)
2198 printf ("%s: unexpected disconnect\n", sym_name(np));
2199 sym_recover_scsi_int(np, HS_UNEXPECTED);
2203 * chip exception handler for SCSI bus mode change
2205 * spi2-r12 11.2.3 says a transceiver mode change must
2206 * generate a reset event and a device that detects a reset
2207 * event shall initiate a hard reset. It says also that a
2208 * device that detects a mode change shall set data transfer
2209 * mode to eight bit asynchronous, etc...
2210 * So, just reinitializing all except chip should be enough.
2212 static void sym_int_sbmc (struct sym_hcb *np)
2214 u_char scsi_mode = INB(np, nc_stest4) & SMODE;
2219 printf("%s: SCSI BUS mode change from %s to %s.\n", sym_name(np),
2220 sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode));
2223 * Should suspend command processing for a few seconds and
2224 * reinitialize all except the chip.
2226 sym_start_up (np, 2);
2230 * chip exception handler for SCSI parity error.
2232 * When the chip detects a SCSI parity error and is
2233 * currently executing a (CH)MOV instruction, it does
2234 * not interrupt immediately, but tries to finish the
2235 * transfer of the current scatter entry before
2236 * interrupting. The following situations may occur:
2238 * - The complete scatter entry has been transferred
2239 * without the device having changed phase.
2240 * The chip will then interrupt with the DSP pointing
2241 * to the instruction that follows the MOV.
2243 * - A phase mismatch occurs before the MOV finished
2244 * and phase errors are to be handled by the C code.
2245 * The chip will then interrupt with both PAR and MA
2248 * - A phase mismatch occurs before the MOV finished and
2249 * phase errors are to be handled by SCRIPTS.
2250 * The chip will load the DSP with the phase mismatch
2251 * JUMP address and interrupt the host processor.
2253 static void sym_int_par (struct sym_hcb *np, u_short sist)
2255 u_char hsts = INB(np, HS_PRT);
2256 u32 dsp = INL(np, nc_dsp);
2257 u32 dbc = INL(np, nc_dbc);
2258 u32 dsa = INL(np, nc_dsa);
2259 u_char sbcl = INB(np, nc_sbcl);
2260 u_char cmd = dbc >> 24;
2261 int phase = cmd & 7;
2262 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
2264 printf("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n",
2265 sym_name(np), hsts, dbc, sbcl);
2268 * Check that the chip is connected to the SCSI BUS.
2270 if (!(INB(np, nc_scntl1) & ISCON)) {
2271 sym_recover_scsi_int(np, HS_UNEXPECTED);
2276 * If the nexus is not clearly identified, reset the bus.
2277 * We will try to do better later.
2283 * Check instruction was a MOV, direction was INPUT and
2286 if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
2290 * Keep track of the parity error.
2292 OUTONB(np, HF_PRT, HF_EXT_ERR);
2293 cp->xerr_status |= XE_PARITY_ERR;
2296 * Prepare the message to send to the device.
2298 np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
2301 * If the old phase was DATA IN phase, we have to deal with
2302 * the 3 situations described above.
2303 * For other input phases (MSG IN and STATUS), the device
2304 * must resend the whole thing that failed parity checking
2305 * or signal error. So, jumping to dispatcher should be OK.
2307 if (phase == 1 || phase == 5) {
2308 /* Phase mismatch handled by SCRIPTS */
2309 if (dsp == SCRIPTB_BA(np, pm_handle))
2311 /* Phase mismatch handled by the C code */
2314 /* No phase mismatch occurred */
2316 sym_set_script_dp (np, cp, dsp);
2317 OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
2320 else if (phase == 7) /* We definitely cannot handle parity errors */
2321 #if 1 /* in message-in phase due to the relection */
2322 goto reset_all; /* path and various message anticipations. */
2324 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
2327 OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
2331 sym_start_reset(np);
2336 * chip exception handler for phase errors.
2338 * We have to construct a new transfer descriptor,
2339 * to transfer the rest of the current block.
2341 static void sym_int_ma (struct sym_hcb *np)
2354 u_char hflags, hflags0;
2358 dsp = INL(np, nc_dsp);
2359 dbc = INL(np, nc_dbc);
2360 dsa = INL(np, nc_dsa);
2363 rest = dbc & 0xffffff;
2367 * locate matching cp if any.
2369 cp = sym_ccb_from_dsa(np, dsa);
2372 * Donnot take into account dma fifo and various buffers in
2373 * INPUT phase since the chip flushes everything before
2374 * raising the MA interrupt for interrupted INPUT phases.
2375 * For DATA IN phase, we will check for the SWIDE later.
2377 if ((cmd & 7) != 1 && (cmd & 7) != 5) {
2380 if (np->features & FE_DFBC)
2381 delta = INW(np, nc_dfbc);
2386 * Read DFIFO, CTEST[4-6] using 1 PCI bus ownership.
2388 dfifo = INL(np, nc_dfifo);
2391 * Calculate remaining bytes in DMA fifo.
2392 * (CTEST5 = dfifo >> 16)
2394 if (dfifo & (DFS << 16))
2395 delta = ((((dfifo >> 8) & 0x300) |
2396 (dfifo & 0xff)) - rest) & 0x3ff;
2398 delta = ((dfifo & 0xff) - rest) & 0x7f;
2402 * The data in the dma fifo has not been transfered to
2403 * the target -> add the amount to the rest
2404 * and clear the data.
2405 * Check the sstat2 register in case of wide transfer.
2408 ss0 = INB(np, nc_sstat0);
2409 if (ss0 & OLF) rest++;
2410 if (!(np->features & FE_C10))
2411 if (ss0 & ORF) rest++;
2412 if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
2413 ss2 = INB(np, nc_sstat2);
2414 if (ss2 & OLF1) rest++;
2415 if (!(np->features & FE_C10))
2416 if (ss2 & ORF1) rest++;
2422 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */
2423 OUTB(np, nc_stest3, TE|CSF); /* scsi fifo */
2427 * log the information
2429 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
2430 printf ("P%x%x RL=%d D=%d ", cmd&7, INB(np, nc_sbcl)&7,
2431 (unsigned) rest, (unsigned) delta);
2434 * try to find the interrupted script command,
2435 * and the address at which to continue.
2439 if (dsp > np->scripta_ba &&
2440 dsp <= np->scripta_ba + np->scripta_sz) {
2441 vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8));
2444 else if (dsp > np->scriptb_ba &&
2445 dsp <= np->scriptb_ba + np->scriptb_sz) {
2446 vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8));
2451 * log the information
2453 if (DEBUG_FLAGS & DEBUG_PHASE) {
2454 printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
2455 cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
2459 printf ("%s: interrupted SCRIPT address not found.\n",
2465 printf ("%s: SCSI phase error fixup: CCB already dequeued.\n",
2471 * get old startaddress and old length.
2473 oadr = scr_to_cpu(vdsp[1]);
2475 if (cmd & 0x10) { /* Table indirect */
2476 tblp = (u32 *) ((char*) &cp->phys + oadr);
2477 olen = scr_to_cpu(tblp[0]);
2478 oadr = scr_to_cpu(tblp[1]);
2481 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
2484 if (DEBUG_FLAGS & DEBUG_PHASE) {
2485 printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
2486 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
2493 * check cmd against assumed interrupted script command.
2494 * If dt data phase, the MOVE instruction hasn't bit 4 of
2497 if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
2498 sym_print_addr(cp->cmd,
2499 "internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
2500 cmd, scr_to_cpu(vdsp[0]) >> 24);
2506 * if old phase not dataphase, leave here.
2509 sym_print_addr(cp->cmd,
2510 "phase change %x-%x %d@%08x resid=%d.\n",
2511 cmd&7, INB(np, nc_sbcl)&7, (unsigned)olen,
2512 (unsigned)oadr, (unsigned)rest);
2513 goto unexpected_phase;
2517 * Choose the correct PM save area.
2519 * Look at the PM_SAVE SCRIPT if you want to understand
2520 * this stuff. The equivalent code is implemented in
2521 * SCRIPTS for the 895A, 896 and 1010 that are able to
2522 * handle PM from the SCRIPTS processor.
2524 hflags0 = INB(np, HF_PRT);
2527 if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
2528 if (hflags & HF_IN_PM0)
2529 nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
2530 else if (hflags & HF_IN_PM1)
2531 nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
2533 if (hflags & HF_DP_SAVED)
2534 hflags ^= HF_ACT_PM;
2537 if (!(hflags & HF_ACT_PM)) {
2539 newcmd = SCRIPTA_BA(np, pm0_data);
2543 newcmd = SCRIPTA_BA(np, pm1_data);
2546 hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
2547 if (hflags != hflags0)
2548 OUTB(np, HF_PRT, hflags);
2551 * fillin the phase mismatch context
2553 pm->sg.addr = cpu_to_scr(oadr + olen - rest);
2554 pm->sg.size = cpu_to_scr(rest);
2555 pm->ret = cpu_to_scr(nxtdsp);
2558 * If we have a SWIDE,
2559 * - prepare the address to write the SWIDE from SCRIPTS,
2560 * - compute the SCRIPTS address to restart from,
2561 * - move current data pointer context by one byte.
2563 nxtdsp = SCRIPTA_BA(np, dispatch);
2564 if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
2565 (INB(np, nc_scntl2) & WSR)) {
2569 * Set up the table indirect for the MOVE
2570 * of the residual byte and adjust the data
2573 tmp = scr_to_cpu(pm->sg.addr);
2574 cp->phys.wresid.addr = cpu_to_scr(tmp);
2575 pm->sg.addr = cpu_to_scr(tmp + 1);
2576 tmp = scr_to_cpu(pm->sg.size);
2577 cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
2578 pm->sg.size = cpu_to_scr(tmp - 1);
2581 * If only the residual byte is to be moved,
2582 * no PM context is needed.
2584 if ((tmp&0xffffff) == 1)
2588 * Prepare the address of SCRIPTS that will
2589 * move the residual byte to memory.
2591 nxtdsp = SCRIPTB_BA(np, wsr_ma_helper);
2594 if (DEBUG_FLAGS & DEBUG_PHASE) {
2595 sym_print_addr(cp->cmd, "PM %x %x %x / %x %x %x.\n",
2596 hflags0, hflags, newcmd,
2597 (unsigned)scr_to_cpu(pm->sg.addr),
2598 (unsigned)scr_to_cpu(pm->sg.size),
2599 (unsigned)scr_to_cpu(pm->ret));
2603 * Restart the SCRIPTS processor.
2605 sym_set_script_dp (np, cp, newcmd);
2606 OUTL_DSP(np, nxtdsp);
2610 * Unexpected phase changes that occurs when the current phase
2611 * is not a DATA IN or DATA OUT phase are due to error conditions.
2612 * Such event may only happen when the SCRIPTS is using a
2613 * multibyte SCSI MOVE.
2615 * Phase change Some possible cause
2617 * COMMAND --> MSG IN SCSI parity error detected by target.
2618 * COMMAND --> STATUS Bad command or refused by target.
2619 * MSG OUT --> MSG IN Message rejected by target.
2620 * MSG OUT --> COMMAND Bogus target that discards extended
2621 * negotiation messages.
2623 * The code below does not care of the new phase and so
2624 * trusts the target. Why to annoy it ?
2625 * If the interrupted phase is COMMAND phase, we restart at
2627 * If a target does not get all the messages after selection,
2628 * the code assumes blindly that the target discards extended
2629 * messages and clears the negotiation status.
2630 * If the target does not want all our response to negotiation,
2631 * we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
2632 * bloat for such a should_not_happen situation).
2633 * In all other situation, we reset the BUS.
2634 * Are these assumptions reasonnable ? (Wait and see ...)
2641 case 2: /* COMMAND phase */
2642 nxtdsp = SCRIPTA_BA(np, dispatch);
2645 case 3: /* STATUS phase */
2646 nxtdsp = SCRIPTA_BA(np, dispatch);
2649 case 6: /* MSG OUT phase */
2651 * If the device may want to use untagged when we want
2652 * tagged, we prepare an IDENTIFY without disc. granted,
2653 * since we will not be able to handle reselect.
2654 * Otherwise, we just don't care.
2656 if (dsp == SCRIPTA_BA(np, send_ident)) {
2657 if (cp->tag != NO_TAG && olen - rest <= 3) {
2658 cp->host_status = HS_BUSY;
2659 np->msgout[0] = IDENTIFY(0, cp->lun);
2660 nxtdsp = SCRIPTB_BA(np, ident_break_atn);
2663 nxtdsp = SCRIPTB_BA(np, ident_break);
2665 else if (dsp == SCRIPTB_BA(np, send_wdtr) ||
2666 dsp == SCRIPTB_BA(np, send_sdtr) ||
2667 dsp == SCRIPTB_BA(np, send_ppr)) {
2668 nxtdsp = SCRIPTB_BA(np, nego_bad_phase);
2669 if (dsp == SCRIPTB_BA(np, send_ppr)) {
2670 struct scsi_device *dev = cp->cmd->device;
2676 case 7: /* MSG IN phase */
2677 nxtdsp = SCRIPTA_BA(np, clrack);
2683 OUTL_DSP(np, nxtdsp);
2688 sym_start_reset(np);
2692 * chip interrupt handler
2694 * In normal situations, interrupt conditions occur one at
2695 * a time. But when something bad happens on the SCSI BUS,
2696 * the chip may raise several interrupt flags before
2697 * stopping and interrupting the CPU. The additionnal
2698 * interrupt flags are stacked in some extra registers
2699 * after the SIP and/or DIP flag has been raised in the
2700 * ISTAT. After the CPU has read the interrupt condition
2701 * flag from SIST or DSTAT, the chip unstacks the other
2702 * interrupt flags and sets the corresponding bits in
2703 * SIST or DSTAT. Since the chip starts stacking once the
2704 * SIP or DIP flag is set, there is a small window of time
2705 * where the stacking does not occur.
2707 * Typically, multiple interrupt conditions may happen in
2708 * the following situations:
2710 * - SCSI parity error + Phase mismatch (PAR|MA)
2711 * When an parity error is detected in input phase
2712 * and the device switches to msg-in phase inside a
2714 * - SCSI parity error + Unexpected disconnect (PAR|UDC)
2715 * When a stupid device does not want to handle the
2716 * recovery of an SCSI parity error.
2717 * - Some combinations of STO, PAR, UDC, ...
2718 * When using non compliant SCSI stuff, when user is
2719 * doing non compliant hot tampering on the BUS, when
2720 * something really bad happens to a device, etc ...
2722 * The heuristic suggested by SYMBIOS to handle
2723 * multiple interrupts is to try unstacking all
2724 * interrupts conditions and to handle them on some
2725 * priority based on error severity.
2726 * This will work when the unstacking has been
2727 * successful, but we cannot be 100 % sure of that,
2728 * since the CPU may have been faster to unstack than
2729 * the chip is able to stack. Hmmm ... But it seems that
2730 * such a situation is very unlikely to happen.
2732 * If this happen, for example STO caught by the CPU
2733 * then UDC happenning before the CPU have restarted
2734 * the SCRIPTS, the driver may wrongly complete the
2735 * same command on UDC, since the SCRIPTS didn't restart
2736 * and the DSA still points to the same command.
2737 * We avoid this situation by setting the DSA to an
2738 * invalid value when the CCB is completed and before
2739 * restarting the SCRIPTS.
2741 * Another issue is that we need some section of our
2742 * recovery procedures to be somehow uninterruptible but
2743 * the SCRIPTS processor does not provides such a
2744 * feature. For this reason, we handle recovery preferently
2745 * from the C code and check against some SCRIPTS critical
2746 * sections from the C code.
2748 * Hopefully, the interrupt handling of the driver is now
2749 * able to resist to weird BUS error conditions, but donnot
2750 * ask me for any guarantee that it will never fail. :-)
2751 * Use at your own decision and risk.
2754 void sym_interrupt (struct sym_hcb *np)
2756 u_char istat, istatc;
2761 * interrupt on the fly ?
2762 * (SCRIPTS may still be running)
2764 * A `dummy read' is needed to ensure that the
2765 * clear of the INTF flag reaches the device
2766 * and that posted writes are flushed to memory
2767 * before the scanning of the DONE queue.
2768 * Note that SCRIPTS also (dummy) read to memory
2769 * prior to deliver the INTF interrupt condition.
2771 istat = INB(np, nc_istat);
2773 OUTB(np, nc_istat, (istat & SIGP) | INTF | np->istat_sem);
2774 istat = INB(np, nc_istat); /* DUMMY READ */
2775 if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
2776 sym_wakeup_done(np);
2779 if (!(istat & (SIP|DIP)))
2782 #if 0 /* We should never get this one */
2784 OUTB(np, nc_istat, CABRT);
2788 * PAR and MA interrupts may occur at the same time,
2789 * and we need to know of both in order to handle
2790 * this situation properly. We try to unstack SCSI
2791 * interrupts for that reason. BTW, I dislike a LOT
2792 * such a loop inside the interrupt routine.
2793 * Even if DMA interrupt stacking is very unlikely to
2794 * happen, we also try unstacking these ones, since
2795 * this has no performance impact.
2802 sist |= INW(np, nc_sist);
2804 dstat |= INB(np, nc_dstat);
2805 istatc = INB(np, nc_istat);
2807 } while (istatc & (SIP|DIP));
2809 if (DEBUG_FLAGS & DEBUG_TINY)
2810 printf ("<%d|%x:%x|%x:%x>",
2811 (int)INB(np, nc_scr0),
2813 (unsigned)INL(np, nc_dsp),
2814 (unsigned)INL(np, nc_dbc));
2816 * On paper, a memory read barrier may be needed here to
2817 * prevent out of order LOADs by the CPU from having
2818 * prefetched stale data prior to DMA having occurred.
2819 * And since we are paranoid ... :)
2821 MEMORY_READ_BARRIER();
2824 * First, interrupts we want to service cleanly.
2826 * Phase mismatch (MA) is the most frequent interrupt
2827 * for chip earlier than the 896 and so we have to service
2828 * it as quickly as possible.
2829 * A SCSI parity error (PAR) may be combined with a phase
2830 * mismatch condition (MA).
2831 * Programmed interrupts (SIR) are used to call the C code
2833 * The single step interrupt (SSI) is not used in this
2836 if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
2837 !(dstat & (MDPE|BF|ABRT|IID))) {
2838 if (sist & PAR) sym_int_par (np, sist);
2839 else if (sist & MA) sym_int_ma (np);
2840 else if (dstat & SIR) sym_int_sir (np);
2841 else if (dstat & SSI) OUTONB_STD();
2842 else goto unknown_int;
2847 * Now, interrupts that donnot happen in normal
2848 * situations and that we may need to recover from.
2850 * On SCSI RESET (RST), we reset everything.
2851 * On SCSI BUS MODE CHANGE (SBMC), we complete all
2852 * active CCBs with RESET status, prepare all devices
2853 * for negotiating again and restart the SCRIPTS.
2854 * On STO and UDC, we complete the CCB with the corres-
2855 * ponding status and restart the SCRIPTS.
2858 printf("%s: SCSI BUS reset detected.\n", sym_name(np));
2859 sym_start_up (np, 1);
2863 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
2864 OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
2866 if (!(sist & (GEN|HTH|SGE)) &&
2867 !(dstat & (MDPE|BF|ABRT|IID))) {
2868 if (sist & SBMC) sym_int_sbmc (np);
2869 else if (sist & STO) sym_int_sto (np);
2870 else if (sist & UDC) sym_int_udc (np);
2871 else goto unknown_int;
2876 * Now, interrupts we are not able to recover cleanly.
2878 * Log message for hard errors.
2882 sym_log_hard_error(np, sist, dstat);
2884 if ((sist & (GEN|HTH|SGE)) ||
2885 (dstat & (MDPE|BF|ABRT|IID))) {
2886 sym_start_reset(np);
2892 * We just miss the cause of the interrupt. :(
2893 * Print a message. The timeout will do the real work.
2895 printf( "%s: unknown interrupt(s) ignored, "
2896 "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
2897 sym_name(np), istat, dstat, sist);
2901 * Dequeue from the START queue all CCBs that match
2902 * a given target/lun/task condition (-1 means all),
2903 * and move them from the BUSY queue to the COMP queue
2904 * with DID_SOFT_ERROR status condition.
2905 * This function is used during error handling/recovery.
2906 * It is called with SCRIPTS not running.
2909 sym_dequeue_from_squeue(struct sym_hcb *np, int i, int target, int lun, int task)
2915 * Make sure the starting index is within range.
2917 assert((i >= 0) && (i < 2*MAX_QUEUE));
2920 * Walk until end of START queue and dequeue every job
2921 * that matches the target/lun/task condition.
2924 while (i != np->squeueput) {
2925 cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
2927 #ifdef SYM_CONF_IARB_SUPPORT
2928 /* Forget hints for IARB, they may be no longer relevant */
2929 cp->host_flags &= ~HF_HINT_IARB;
2931 if ((target == -1 || cp->target == target) &&
2932 (lun == -1 || cp->lun == lun) &&
2933 (task == -1 || cp->tag == task)) {
2934 sym_set_cam_status(cp->cmd, DID_SOFT_ERROR);
2935 sym_remque(&cp->link_ccbq);
2936 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
2940 np->squeue[j] = np->squeue[i];
2941 if ((j += 2) >= MAX_QUEUE*2) j = 0;
2943 if ((i += 2) >= MAX_QUEUE*2) i = 0;
2945 if (i != j) /* Copy back the idle task if needed */
2946 np->squeue[j] = np->squeue[i];
2947 np->squeueput = j; /* Update our current start queue pointer */
2953 * chip handler for bad SCSI status condition
2955 * In case of bad SCSI status, we unqueue all the tasks
2956 * currently queued to the controller but not yet started
2957 * and then restart the SCRIPTS processor immediately.
2959 * QUEUE FULL and BUSY conditions are handled the same way.
2960 * Basically all the not yet started tasks are requeued in
2961 * device queue and the queue is frozen until a completion.
2963 * For CHECK CONDITION and COMMAND TERMINATED status, we use
2964 * the CCB of the failed command to prepare a REQUEST SENSE
2965 * SCSI command and queue it to the controller queue.
2967 * SCRATCHA is assumed to have been loaded with STARTPOS
2968 * before the SCRIPTS called the C code.
2970 static void sym_sir_bad_scsi_status(struct sym_hcb *np, int num, struct sym_ccb *cp)
2973 u_char s_status = cp->ssss_status;
2974 u_char h_flags = cp->host_flags;
2979 * Compute the index of the next job to start from SCRIPTS.
2981 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
2984 * The last CCB queued used for IARB hint may be
2985 * no longer relevant. Forget it.
2987 #ifdef SYM_CONF_IARB_SUPPORT
2993 * Now deal with the SCSI status.
2998 if (sym_verbose >= 2) {
2999 sym_print_addr(cp->cmd, "%s\n",
3000 s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
3002 default: /* S_INT, S_INT_COND_MET, S_CONFLICT */
3003 sym_complete_error (np, cp);
3008 * If we get an SCSI error when requesting sense, give up.
3010 if (h_flags & HF_SENSE) {
3011 sym_complete_error (np, cp);
3016 * Dequeue all queued CCBs for that device not yet started,
3017 * and restart the SCRIPTS processor immediately.
3019 sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
3020 OUTL_DSP(np, SCRIPTA_BA(np, start));
3023 * Save some info of the actual IO.
3024 * Compute the data residual.
3026 cp->sv_scsi_status = cp->ssss_status;
3027 cp->sv_xerr_status = cp->xerr_status;
3028 cp->sv_resid = sym_compute_residual(np, cp);
3031 * Prepare all needed data structures for
3032 * requesting sense data.
3035 cp->scsi_smsg2[0] = IDENTIFY(0, cp->lun);
3039 * If we are currently using anything different from
3040 * async. 8 bit data transfers with that target,
3041 * start a negotiation, since the device may want
3042 * to report us a UNIT ATTENTION condition due to
3043 * a cause we currently ignore, and we donnot want
3044 * to be stuck with WIDE and/or SYNC data transfer.
3046 * cp->nego_status is filled by sym_prepare_nego().
3048 cp->nego_status = 0;
3049 msglen += sym_prepare_nego(np, cp, &cp->scsi_smsg2[msglen]);
3051 * Message table indirect structure.
3053 cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg2);
3054 cp->phys.smsg.size = cpu_to_scr(msglen);
3059 cp->phys.cmd.addr = CCB_BA(cp, sensecmd);
3060 cp->phys.cmd.size = cpu_to_scr(6);
3063 * patch requested size into sense command
3065 cp->sensecmd[0] = REQUEST_SENSE;
3066 cp->sensecmd[1] = 0;
3067 if (cp->cmd->device->scsi_level <= SCSI_2 && cp->lun <= 7)
3068 cp->sensecmd[1] = cp->lun << 5;
3069 cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
3070 cp->data_len = SYM_SNS_BBUF_LEN;
3075 memset(cp->sns_bbuf, 0, SYM_SNS_BBUF_LEN);
3076 cp->phys.sense.addr = CCB_BA(cp, sns_bbuf);
3077 cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
3080 * requeue the command.
3082 startp = SCRIPTB_BA(np, sdata_in);
3084 cp->phys.head.savep = cpu_to_scr(startp);
3085 cp->phys.head.lastp = cpu_to_scr(startp);
3086 cp->startp = cpu_to_scr(startp);
3087 cp->goalp = cpu_to_scr(startp + 16);
3089 cp->host_xflags = 0;
3090 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
3091 cp->ssss_status = S_ILLEGAL;
3092 cp->host_flags = (HF_SENSE|HF_DATA_IN);
3093 cp->xerr_status = 0;
3094 cp->extra_bytes = 0;
3096 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
3099 * Requeue the command.
3101 sym_put_start_queue(np, cp);
3104 * Give back to upper layer everything we have dequeued.
3106 sym_flush_comp_queue(np, 0);
3112 * After a device has accepted some management message
3113 * as BUS DEVICE RESET, ABORT TASK, etc ..., or when
3114 * a device signals a UNIT ATTENTION condition, some
3115 * tasks are thrown away by the device. We are required
3116 * to reflect that on our tasks list since the device
3117 * will never complete these tasks.
3119 * This function move from the BUSY queue to the COMP
3120 * queue all disconnected CCBs for a given target that
3121 * match the following criteria:
3122 * - lun=-1 means any logical UNIT otherwise a given one.
3123 * - task=-1 means any task, otherwise a given one.
3125 int sym_clear_tasks(struct sym_hcb *np, int cam_status, int target, int lun, int task)
3127 SYM_QUEHEAD qtmp, *qp;
3132 * Move the entire BUSY queue to our temporary queue.
3134 sym_que_init(&qtmp);
3135 sym_que_splice(&np->busy_ccbq, &qtmp);
3136 sym_que_init(&np->busy_ccbq);
3139 * Put all CCBs that matches our criteria into
3140 * the COMP queue and put back other ones into
3143 while ((qp = sym_remque_head(&qtmp)) != 0) {
3144 struct scsi_cmnd *cmd;
3145 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3147 if (cp->host_status != HS_DISCONNECT ||
3148 cp->target != target ||
3149 (lun != -1 && cp->lun != lun) ||
3151 (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
3152 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
3155 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3157 /* Preserve the software timeout condition */
3158 if (sym_get_cam_status(cmd) != DID_TIME_OUT)
3159 sym_set_cam_status(cmd, cam_status);
3162 printf("XXXX TASK @%p CLEARED\n", cp);
3169 * chip handler for TASKS recovery
3171 * We cannot safely abort a command, while the SCRIPTS
3172 * processor is running, since we just would be in race
3175 * As long as we have tasks to abort, we keep the SEM
3176 * bit set in the ISTAT. When this bit is set, the
3177 * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
3178 * each time it enters the scheduler.
3180 * If we have to reset a target, clear tasks of a unit,
3181 * or to perform the abort of a disconnected job, we
3182 * restart the SCRIPTS for selecting the target. Once
3183 * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
3184 * If it loses arbitration, the SCRIPTS will interrupt again
3185 * the next time it will enter its scheduler, and so on ...
3187 * On SIR_TARGET_SELECTED, we scan for the more
3188 * appropriate thing to do:
3190 * - If nothing, we just sent a M_ABORT message to the
3191 * target to get rid of the useless SCSI bus ownership.
3192 * According to the specs, no tasks shall be affected.
3193 * - If the target is to be reset, we send it a M_RESET
3195 * - If a logical UNIT is to be cleared , we send the
3196 * IDENTIFY(lun) + M_ABORT.
3197 * - If an untagged task is to be aborted, we send the
3198 * IDENTIFY(lun) + M_ABORT.
3199 * - If a tagged task is to be aborted, we send the
3200 * IDENTIFY(lun) + task attributes + M_ABORT_TAG.
3202 * Once our 'kiss of death' :) message has been accepted
3203 * by the target, the SCRIPTS interrupts again
3204 * (SIR_ABORT_SENT). On this interrupt, we complete
3205 * all the CCBs that should have been aborted by the
3206 * target according to our message.
3208 static void sym_sir_task_recovery(struct sym_hcb *np, int num)
3212 struct sym_tcb *tp = NULL; /* gcc isn't quite smart enough yet */
3213 struct scsi_target *starget;
3214 int target=-1, lun=-1, task;
3219 * The SCRIPTS processor stopped before starting
3220 * the next command in order to allow us to perform
3221 * some task recovery.
3223 case SIR_SCRIPT_STOPPED:
3225 * Do we have any target to reset or unit to clear ?
3227 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
3228 tp = &np->target[i];
3230 (tp->lun0p && tp->lun0p->to_clear)) {
3236 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
3237 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
3247 * If not, walk the busy queue for any
3248 * disconnected CCB to be aborted.
3251 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3252 cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
3253 if (cp->host_status != HS_DISCONNECT)
3256 target = cp->target;
3263 * If some target is to be selected,
3264 * prepare and start the selection.
3267 tp = &np->target[target];
3268 np->abrt_sel.sel_id = target;
3269 np->abrt_sel.sel_scntl3 = tp->head.wval;
3270 np->abrt_sel.sel_sxfer = tp->head.sval;
3271 OUTL(np, nc_dsa, np->hcb_ba);
3272 OUTL_DSP(np, SCRIPTB_BA(np, sel_for_abort));
3277 * Now look for a CCB to abort that haven't started yet.
3278 * Btw, the SCRIPTS processor is still stopped, so
3279 * we are not in race.
3283 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3284 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3285 if (cp->host_status != HS_BUSY &&
3286 cp->host_status != HS_NEGOTIATE)
3290 #ifdef SYM_CONF_IARB_SUPPORT
3292 * If we are using IMMEDIATE ARBITRATION, we donnot
3293 * want to cancel the last queued CCB, since the
3294 * SCRIPTS may have anticipated the selection.
3296 if (cp == np->last_cp) {
3301 i = 1; /* Means we have found some */
3306 * We are done, so we donnot need
3307 * to synchronize with the SCRIPTS anylonger.
3308 * Remove the SEM flag from the ISTAT.
3311 OUTB(np, nc_istat, SIGP);
3315 * Compute index of next position in the start
3316 * queue the SCRIPTS intends to start and dequeue
3317 * all CCBs for that device that haven't been started.
3319 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
3320 i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
3323 * Make sure at least our IO to abort has been dequeued.
3325 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
3326 assert(i && sym_get_cam_status(cp->cmd) == DID_SOFT_ERROR);
3328 sym_remque(&cp->link_ccbq);
3329 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3332 * Keep track in cam status of the reason of the abort.
3334 if (cp->to_abort == 2)
3335 sym_set_cam_status(cp->cmd, DID_TIME_OUT);
3337 sym_set_cam_status(cp->cmd, DID_ABORT);
3340 * Complete with error everything that we have dequeued.
3342 sym_flush_comp_queue(np, 0);
3345 * The SCRIPTS processor has selected a target
3346 * we may have some manual recovery to perform for.
3348 case SIR_TARGET_SELECTED:
3349 target = INB(np, nc_sdid) & 0xf;
3350 tp = &np->target[target];
3352 np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
3355 * If the target is to be reset, prepare a
3356 * M_RESET message and clear the to_reset flag
3357 * since we donnot expect this operation to fail.
3360 np->abrt_msg[0] = M_RESET;
3361 np->abrt_tbl.size = 1;
3367 * Otherwise, look for some logical unit to be cleared.
3369 if (tp->lun0p && tp->lun0p->to_clear)
3371 else if (tp->lunmp) {
3372 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
3373 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
3381 * If a logical unit is to be cleared, prepare
3382 * an IDENTIFY(lun) + ABORT MESSAGE.
3385 struct sym_lcb *lp = sym_lp(tp, lun);
3386 lp->to_clear = 0; /* We don't expect to fail here */
3387 np->abrt_msg[0] = IDENTIFY(0, lun);
3388 np->abrt_msg[1] = M_ABORT;
3389 np->abrt_tbl.size = 2;
3394 * Otherwise, look for some disconnected job to
3395 * abort for this target.
3399 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3400 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3401 if (cp->host_status != HS_DISCONNECT)
3403 if (cp->target != target)
3407 i = 1; /* Means we have some */
3412 * If we have none, probably since the device has
3413 * completed the command before we won abitration,
3414 * send a M_ABORT message without IDENTIFY.
3415 * According to the specs, the device must just
3416 * disconnect the BUS and not abort any task.
3419 np->abrt_msg[0] = M_ABORT;
3420 np->abrt_tbl.size = 1;
3425 * We have some task to abort.
3426 * Set the IDENTIFY(lun)
3428 np->abrt_msg[0] = IDENTIFY(0, cp->lun);
3431 * If we want to abort an untagged command, we
3432 * will send a IDENTIFY + M_ABORT.
3433 * Otherwise (tagged command), we will send
3434 * a IDENTITFY + task attributes + ABORT TAG.
3436 if (cp->tag == NO_TAG) {
3437 np->abrt_msg[1] = M_ABORT;
3438 np->abrt_tbl.size = 2;
3440 np->abrt_msg[1] = cp->scsi_smsg[1];
3441 np->abrt_msg[2] = cp->scsi_smsg[2];
3442 np->abrt_msg[3] = M_ABORT_TAG;
3443 np->abrt_tbl.size = 4;
3446 * Keep track of software timeout condition, since the
3447 * peripheral driver may not count retries on abort
3448 * conditions not due to timeout.
3450 if (cp->to_abort == 2)
3451 sym_set_cam_status(cp->cmd, DID_TIME_OUT);
3452 cp->to_abort = 0; /* We donnot expect to fail here */
3456 * The target has accepted our message and switched
3457 * to BUS FREE phase as we expected.
3459 case SIR_ABORT_SENT:
3460 target = INB(np, nc_sdid) & 0xf;
3461 tp = &np->target[target];
3462 starget = tp->starget;
3465 ** If we didn't abort anything, leave here.
3467 if (np->abrt_msg[0] == M_ABORT)
3471 * If we sent a M_RESET, then a hardware reset has
3472 * been performed by the target.
3473 * - Reset everything to async 8 bit
3474 * - Tell ourself to negotiate next time :-)
3475 * - Prepare to clear all disconnected CCBs for
3476 * this target from our task list (lun=task=-1)
3480 if (np->abrt_msg[0] == M_RESET) {
3482 tp->head.wval = np->rv_scntl3;
3484 spi_period(starget) = 0;
3485 spi_offset(starget) = 0;
3486 spi_width(starget) = 0;
3487 spi_iu(starget) = 0;
3488 spi_dt(starget) = 0;
3489 spi_qas(starget) = 0;
3490 tp->tgoal.check_nego = 1;
3494 * Otherwise, check for the LUN and TASK(s)
3495 * concerned by the cancelation.
3496 * If it is not ABORT_TAG then it is CLEAR_QUEUE
3497 * or an ABORT message :-)
3500 lun = np->abrt_msg[0] & 0x3f;
3501 if (np->abrt_msg[1] == M_ABORT_TAG)
3502 task = np->abrt_msg[2];
3506 * Complete all the CCBs the device should have
3507 * aborted due to our 'kiss of death' message.
3509 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
3510 sym_dequeue_from_squeue(np, i, target, lun, -1);
3511 sym_clear_tasks(np, DID_ABORT, target, lun, task);
3512 sym_flush_comp_queue(np, 0);
3515 * If we sent a BDR, make upper layer aware of that.
3517 if (np->abrt_msg[0] == M_RESET)
3518 sym_xpt_async_sent_bdr(np, target);
3523 * Print to the log the message we intend to send.
3525 if (num == SIR_TARGET_SELECTED) {
3526 dev_info(&tp->starget->dev, "control msgout:");
3527 sym_printl_hex(np->abrt_msg, np->abrt_tbl.size);
3528 np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
3532 * Let the SCRIPTS processor continue.
3538 * Gerard's alchemy:) that deals with with the data
3539 * pointer for both MDP and the residual calculation.
3541 * I didn't want to bloat the code by more than 200
3542 * lines for the handling of both MDP and the residual.
3543 * This has been achieved by using a data pointer
3544 * representation consisting in an index in the data
3545 * array (dp_sg) and a negative offset (dp_ofs) that
3546 * have the following meaning:
3548 * - dp_sg = SYM_CONF_MAX_SG
3549 * we are at the end of the data script.
3550 * - dp_sg < SYM_CONF_MAX_SG
3551 * dp_sg points to the next entry of the scatter array
3552 * we want to transfer.
3554 * dp_ofs represents the residual of bytes of the
3555 * previous entry scatter entry we will send first.
3557 * no residual to send first.
3559 * The function sym_evaluate_dp() accepts an arbitray
3560 * offset (basically from the MDP message) and returns
3561 * the corresponding values of dp_sg and dp_ofs.
3564 static int sym_evaluate_dp(struct sym_hcb *np, struct sym_ccb *cp, u32 scr, int *ofs)
3567 int dp_ofs, dp_sg, dp_sgmin;
3572 * Compute the resulted data pointer in term of a script
3573 * address within some DATA script and a signed byte offset.
3577 if (dp_scr == SCRIPTA_BA(np, pm0_data))
3579 else if (dp_scr == SCRIPTA_BA(np, pm1_data))
3585 dp_scr = scr_to_cpu(pm->ret);
3586 dp_ofs -= scr_to_cpu(pm->sg.size) & 0x00ffffff;
3590 * If we are auto-sensing, then we are done.
3592 if (cp->host_flags & HF_SENSE) {
3598 * Deduce the index of the sg entry.
3599 * Keep track of the index of the first valid entry.
3600 * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the
3603 tmp = scr_to_cpu(cp->goalp);
3604 dp_sg = SYM_CONF_MAX_SG;
3606 dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
3607 dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
3610 * Move to the sg entry the data pointer belongs to.
3612 * If we are inside the data area, we expect result to be:
3615 * dp_ofs = 0 and dp_sg is the index of the sg entry
3616 * the data pointer belongs to (or the end of the data)
3618 * dp_ofs < 0 and dp_sg is the index of the sg entry
3619 * the data pointer belongs to + 1.
3623 while (dp_sg > dp_sgmin) {
3625 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3626 n = dp_ofs + (tmp & 0xffffff);
3634 else if (dp_ofs > 0) {
3635 while (dp_sg < SYM_CONF_MAX_SG) {
3636 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3637 dp_ofs -= (tmp & 0xffffff);
3645 * Make sure the data pointer is inside the data area.
3646 * If not, return some error.
3648 if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
3650 else if (dp_sg > SYM_CONF_MAX_SG ||
3651 (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
3655 * Save the extreme pointer if needed.
3657 if (dp_sg > cp->ext_sg ||
3658 (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
3660 cp->ext_ofs = dp_ofs;
3674 * chip handler for MODIFY DATA POINTER MESSAGE
3676 * We also call this function on IGNORE WIDE RESIDUE
3677 * messages that do not match a SWIDE full condition.
3678 * Btw, we assume in that situation that such a message
3679 * is equivalent to a MODIFY DATA POINTER (offset=-1).
3682 static void sym_modify_dp(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp, int ofs)
3685 u32 dp_scr = sym_get_script_dp (np, cp);
3693 * Not supported for auto-sense.
3695 if (cp->host_flags & HF_SENSE)
3699 * Apply our alchemy:) (see comments in sym_evaluate_dp()),
3700 * to the resulted data pointer.
3702 dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
3707 * And our alchemy:) allows to easily calculate the data
3708 * script address we want to return for the next data phase.
3710 dp_ret = cpu_to_scr(cp->goalp);
3711 dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
3714 * If offset / scatter entry is zero we donnot need
3715 * a context for the new current data pointer.
3723 * Get a context for the new current data pointer.
3725 hflags = INB(np, HF_PRT);
3727 if (hflags & HF_DP_SAVED)
3728 hflags ^= HF_ACT_PM;
3730 if (!(hflags & HF_ACT_PM)) {
3732 dp_scr = SCRIPTA_BA(np, pm0_data);
3736 dp_scr = SCRIPTA_BA(np, pm1_data);
3739 hflags &= ~(HF_DP_SAVED);
3741 OUTB(np, HF_PRT, hflags);
3744 * Set up the new current data pointer.
3745 * ofs < 0 there, and for the next data phase, we
3746 * want to transfer part of the data of the sg entry
3747 * corresponding to index dp_sg-1 prior to returning
3748 * to the main data script.
3750 pm->ret = cpu_to_scr(dp_ret);
3751 tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
3752 tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
3753 pm->sg.addr = cpu_to_scr(tmp);
3754 pm->sg.size = cpu_to_scr(-dp_ofs);
3757 sym_set_script_dp (np, cp, dp_scr);
3758 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
3762 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
3767 * chip calculation of the data residual.
3769 * As I used to say, the requirement of data residual
3770 * in SCSI is broken, useless and cannot be achieved
3771 * without huge complexity.
3772 * But most OSes and even the official CAM require it.
3773 * When stupidity happens to be so widely spread inside
3774 * a community, it gets hard to convince.
3776 * Anyway, I don't care, since I am not going to use
3777 * any software that considers this data residual as
3778 * a relevant information. :)
3781 int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp)
3783 int dp_sg, dp_sgmin, resid = 0;
3787 * Check for some data lost or just thrown away.
3788 * We are not required to be quite accurate in this
3789 * situation. Btw, if we are odd for output and the
3790 * device claims some more data, it may well happen
3791 * than our residual be zero. :-)
3793 if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
3794 if (cp->xerr_status & XE_EXTRA_DATA)
3795 resid -= cp->extra_bytes;
3796 if (cp->xerr_status & XE_SODL_UNRUN)
3798 if (cp->xerr_status & XE_SWIDE_OVRUN)
3803 * If all data has been transferred,
3804 * there is no residual.
3806 if (cp->phys.head.lastp == cp->goalp)
3810 * If no data transfer occurs, or if the data
3811 * pointer is weird, return full residual.
3813 if (cp->startp == cp->phys.head.lastp ||
3814 sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
3816 return cp->data_len;
3820 * If we were auto-sensing, then we are done.
3822 if (cp->host_flags & HF_SENSE) {
3827 * We are now full comfortable in the computation
3828 * of the data residual (2's complement).
3830 dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
3831 resid = -cp->ext_ofs;
3832 for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
3833 u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3834 resid += (tmp & 0xffffff);
3837 resid -= cp->odd_byte_adjustment;
3840 * Hopefully, the result is not too wrong.
3846 * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
3848 * When we try to negotiate, we append the negotiation message
3849 * to the identify and (maybe) simple tag message.
3850 * The host status field is set to HS_NEGOTIATE to mark this
3853 * If the target doesn't answer this message immediately
3854 * (as required by the standard), the SIR_NEGO_FAILED interrupt
3855 * will be raised eventually.
3856 * The handler removes the HS_NEGOTIATE status, and sets the
3857 * negotiated value to the default (async / nowide).
3859 * If we receive a matching answer immediately, we check it
3860 * for validity, and set the values.
3862 * If we receive a Reject message immediately, we assume the
3863 * negotiation has failed, and fall back to standard values.
3865 * If we receive a negotiation message while not in HS_NEGOTIATE
3866 * state, it's a target initiated negotiation. We prepare a
3867 * (hopefully) valid answer, set our parameters, and send back
3868 * this answer to the target.
3870 * If the target doesn't fetch the answer (no message out phase),
3871 * we assume the negotiation has failed, and fall back to default
3872 * settings (SIR_NEGO_PROTO interrupt).
3874 * When we set the values, we adjust them in all ccbs belonging
3875 * to this target, in the controller's register, and in the "phys"
3876 * field of the controller's struct sym_hcb.
3880 * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message.
3883 sym_sync_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
3885 int target = cp->target;
3886 u_char chg, ofs, per, fak, div;
3888 if (DEBUG_FLAGS & DEBUG_NEGO) {
3889 sym_print_nego_msg(np, target, "sync msgin", np->msgin);
3893 * Get requested values.
3900 * Check values against our limits.
3903 if (ofs > np->maxoffs)
3904 {chg = 1; ofs = np->maxoffs;}
3908 if (per < np->minsync)
3909 {chg = 1; per = np->minsync;}
3913 * Get new chip synchronous parameters value.
3916 if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
3919 if (DEBUG_FLAGS & DEBUG_NEGO) {
3920 sym_print_addr(cp->cmd,
3921 "sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
3922 ofs, per, div, fak, chg);
3926 * If it was an answer we want to change,
3927 * then it isn't acceptable. Reject it.
3935 sym_setsync (np, target, ofs, per, div, fak);
3938 * It was an answer. We are done.
3944 * It was a request. Prepare an answer message.
3946 spi_populate_sync_msg(np->msgout, per, ofs);
3948 if (DEBUG_FLAGS & DEBUG_NEGO) {
3949 sym_print_nego_msg(np, target, "sync msgout", np->msgout);
3952 np->msgin [0] = M_NOOP;
3957 sym_setsync (np, target, 0, 0, 0, 0);
3961 static void sym_sync_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
3967 * Request or answer ?
3969 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
3970 OUTB(np, HS_PRT, HS_BUSY);
3971 if (cp->nego_status && cp->nego_status != NS_SYNC)
3977 * Check and apply new values.
3979 result = sym_sync_nego_check(np, req, cp);
3980 if (result) /* Not acceptable, reject it */
3982 if (req) { /* Was a request, send response. */
3983 cp->nego_status = NS_SYNC;
3984 OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
3986 else /* Was a response, we are done. */
3987 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
3991 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
3995 * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message.
3998 sym_ppr_nego_check(struct sym_hcb *np, int req, int target)
4000 struct sym_tcb *tp = &np->target[target];
4001 unsigned char fak, div;
4004 unsigned char per = np->msgin[3];
4005 unsigned char ofs = np->msgin[5];
4006 unsigned char wide = np->msgin[6];
4007 unsigned char opts = np->msgin[7] & PPR_OPT_MASK;
4009 if (DEBUG_FLAGS & DEBUG_NEGO) {
4010 sym_print_nego_msg(np, target, "ppr msgin", np->msgin);
4014 * Check values against our limits.
4016 if (wide > np->maxwide) {
4020 if (!wide || !(np->features & FE_U3EN))
4023 if (opts != (np->msgin[7] & PPR_OPT_MASK))
4026 dt = opts & PPR_OPT_DT;
4029 unsigned char maxoffs = dt ? np->maxoffs_dt : np->maxoffs;
4030 if (ofs > maxoffs) {
4037 unsigned char minsync = dt ? np->minsync_dt : np->minsync;
4038 if (per < minsync) {
4045 * Get new chip synchronous parameters value.
4048 if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
4052 * If it was an answer we want to change,
4053 * then it isn't acceptable. Reject it.
4061 sym_setpprot(np, target, opts, ofs, per, wide, div, fak);
4064 * It was an answer. We are done.
4070 * It was a request. Prepare an answer message.
4072 spi_populate_ppr_msg(np->msgout, per, ofs, wide, opts);
4074 if (DEBUG_FLAGS & DEBUG_NEGO) {
4075 sym_print_nego_msg(np, target, "ppr msgout", np->msgout);
4078 np->msgin [0] = M_NOOP;
4083 sym_setpprot (np, target, 0, 0, 0, 0, 0, 0);
4085 * If it is a device response that should result in
4086 * ST, we may want to try a legacy negotiation later.
4088 if (!req && !opts) {
4089 tp->tgoal.period = per;
4090 tp->tgoal.offset = ofs;
4091 tp->tgoal.width = wide;
4092 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
4093 tp->tgoal.check_nego = 1;
4098 static void sym_ppr_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4104 * Request or answer ?
4106 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
4107 OUTB(np, HS_PRT, HS_BUSY);
4108 if (cp->nego_status && cp->nego_status != NS_PPR)
4114 * Check and apply new values.
4116 result = sym_ppr_nego_check(np, req, cp->target);
4117 if (result) /* Not acceptable, reject it */
4119 if (req) { /* Was a request, send response. */
4120 cp->nego_status = NS_PPR;
4121 OUTL_DSP(np, SCRIPTB_BA(np, ppr_resp));
4123 else /* Was a response, we are done. */
4124 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4128 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4132 * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message.
4135 sym_wide_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
4137 int target = cp->target;
4140 if (DEBUG_FLAGS & DEBUG_NEGO) {
4141 sym_print_nego_msg(np, target, "wide msgin", np->msgin);
4145 * Get requested values.
4148 wide = np->msgin[3];
4151 * Check values against our limits.
4153 if (wide > np->maxwide) {
4158 if (DEBUG_FLAGS & DEBUG_NEGO) {
4159 sym_print_addr(cp->cmd, "wdtr: wide=%d chg=%d.\n",
4164 * If it was an answer we want to change,
4165 * then it isn't acceptable. Reject it.
4173 sym_setwide (np, target, wide);
4176 * It was an answer. We are done.
4182 * It was a request. Prepare an answer message.
4184 spi_populate_width_msg(np->msgout, wide);
4186 np->msgin [0] = M_NOOP;
4188 if (DEBUG_FLAGS & DEBUG_NEGO) {
4189 sym_print_nego_msg(np, target, "wide msgout", np->msgout);
4198 static void sym_wide_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4204 * Request or answer ?
4206 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
4207 OUTB(np, HS_PRT, HS_BUSY);
4208 if (cp->nego_status && cp->nego_status != NS_WIDE)
4214 * Check and apply new values.
4216 result = sym_wide_nego_check(np, req, cp);
4217 if (result) /* Not acceptable, reject it */
4219 if (req) { /* Was a request, send response. */
4220 cp->nego_status = NS_WIDE;
4221 OUTL_DSP(np, SCRIPTB_BA(np, wdtr_resp));
4222 } else { /* Was a response. */
4224 * Negotiate for SYNC immediately after WIDE response.
4225 * This allows to negotiate for both WIDE and SYNC on
4226 * a single SCSI command (Suggested by Justin Gibbs).
4228 if (tp->tgoal.offset) {
4229 spi_populate_sync_msg(np->msgout, tp->tgoal.period,
4232 if (DEBUG_FLAGS & DEBUG_NEGO) {
4233 sym_print_nego_msg(np, cp->target,
4234 "sync msgout", np->msgout);
4237 cp->nego_status = NS_SYNC;
4238 OUTB(np, HS_PRT, HS_NEGOTIATE);
4239 OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
4242 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4248 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4252 * Reset DT, SYNC or WIDE to default settings.
4254 * Called when a negotiation does not succeed either
4255 * on rejection or on protocol error.
4257 * A target that understands a PPR message should never
4258 * reject it, and messing with it is very unlikely.
4259 * So, if a PPR makes problems, we may just want to
4260 * try a legacy negotiation later.
4262 static void sym_nego_default(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4264 switch (cp->nego_status) {
4267 sym_setpprot (np, cp->target, 0, 0, 0, 0, 0, 0);
4269 if (tp->tgoal.period < np->minsync)
4270 tp->tgoal.period = np->minsync;
4271 if (tp->tgoal.offset > np->maxoffs)
4272 tp->tgoal.offset = np->maxoffs;
4273 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
4274 tp->tgoal.check_nego = 1;
4278 sym_setsync (np, cp->target, 0, 0, 0, 0);
4281 sym_setwide (np, cp->target, 0);
4284 np->msgin [0] = M_NOOP;
4285 np->msgout[0] = M_NOOP;
4286 cp->nego_status = 0;
4290 * chip handler for MESSAGE REJECT received in response to
4291 * PPR, WIDE or SYNCHRONOUS negotiation.
4293 static void sym_nego_rejected(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4295 sym_nego_default(np, tp, cp);
4296 OUTB(np, HS_PRT, HS_BUSY);
4300 * chip exception handler for programmed interrupts.
4302 static void sym_int_sir (struct sym_hcb *np)
4304 u_char num = INB(np, nc_dsps);
4305 u32 dsa = INL(np, nc_dsa);
4306 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
4307 u_char target = INB(np, nc_sdid) & 0x0f;
4308 struct sym_tcb *tp = &np->target[target];
4311 if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
4314 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
4316 * SCRIPTS tell us that we may have to update
4317 * 64 bit DMA segment registers.
4319 case SIR_DMAP_DIRTY:
4320 sym_update_dmap_regs(np);
4324 * Command has been completed with error condition
4325 * or has been auto-sensed.
4327 case SIR_COMPLETE_ERROR:
4328 sym_complete_error(np, cp);
4331 * The C code is currently trying to recover from something.
4332 * Typically, user want to abort some command.
4334 case SIR_SCRIPT_STOPPED:
4335 case SIR_TARGET_SELECTED:
4336 case SIR_ABORT_SENT:
4337 sym_sir_task_recovery(np, num);
4340 * The device didn't go to MSG OUT phase after having
4341 * been selected with ATN. We donnot want to handle
4344 case SIR_SEL_ATN_NO_MSG_OUT:
4345 printf ("%s:%d: No MSG OUT phase after selection with ATN.\n",
4346 sym_name (np), target);
4349 * The device didn't switch to MSG IN phase after
4350 * having reseleted the initiator.
4352 case SIR_RESEL_NO_MSG_IN:
4353 printf ("%s:%d: No MSG IN phase after reselection.\n",
4354 sym_name (np), target);
4357 * After reselection, the device sent a message that wasn't
4360 case SIR_RESEL_NO_IDENTIFY:
4361 printf ("%s:%d: No IDENTIFY after reselection.\n",
4362 sym_name (np), target);
4365 * The device reselected a LUN we donnot know about.
4367 case SIR_RESEL_BAD_LUN:
4368 np->msgout[0] = M_RESET;
4371 * The device reselected for an untagged nexus and we
4374 case SIR_RESEL_BAD_I_T_L:
4375 np->msgout[0] = M_ABORT;
4378 * The device reselected for a tagged nexus that we donnot
4381 case SIR_RESEL_BAD_I_T_L_Q:
4382 np->msgout[0] = M_ABORT_TAG;
4385 * The SCRIPTS let us know that the device has grabbed
4386 * our message and will abort the job.
4388 case SIR_RESEL_ABORTED:
4389 np->lastmsg = np->msgout[0];
4390 np->msgout[0] = M_NOOP;
4391 printf ("%s:%d: message %x sent on bad reselection.\n",
4392 sym_name (np), target, np->lastmsg);
4395 * The SCRIPTS let us know that a message has been
4396 * successfully sent to the device.
4398 case SIR_MSG_OUT_DONE:
4399 np->lastmsg = np->msgout[0];
4400 np->msgout[0] = M_NOOP;
4401 /* Should we really care of that */
4402 if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
4404 cp->xerr_status &= ~XE_PARITY_ERR;
4405 if (!cp->xerr_status)
4406 OUTOFFB(np, HF_PRT, HF_EXT_ERR);
4411 * The device didn't send a GOOD SCSI status.
4412 * We may have some work to do prior to allow
4413 * the SCRIPTS processor to continue.
4415 case SIR_BAD_SCSI_STATUS:
4418 sym_sir_bad_scsi_status(np, num, cp);
4421 * We are asked by the SCRIPTS to prepare a
4424 case SIR_REJECT_TO_SEND:
4425 sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
4426 np->msgout[0] = M_REJECT;
4429 * We have been ODD at the end of a DATA IN
4430 * transfer and the device didn't send a
4431 * IGNORE WIDE RESIDUE message.
4432 * It is a data overrun condition.
4434 case SIR_SWIDE_OVERRUN:
4436 OUTONB(np, HF_PRT, HF_EXT_ERR);
4437 cp->xerr_status |= XE_SWIDE_OVRUN;
4441 * We have been ODD at the end of a DATA OUT
4443 * It is a data underrun condition.
4445 case SIR_SODL_UNDERRUN:
4447 OUTONB(np, HF_PRT, HF_EXT_ERR);
4448 cp->xerr_status |= XE_SODL_UNRUN;
4452 * The device wants us to tranfer more data than
4453 * expected or in the wrong direction.
4454 * The number of extra bytes is in scratcha.
4455 * It is a data overrun condition.
4457 case SIR_DATA_OVERRUN:
4459 OUTONB(np, HF_PRT, HF_EXT_ERR);
4460 cp->xerr_status |= XE_EXTRA_DATA;
4461 cp->extra_bytes += INL(np, nc_scratcha);
4465 * The device switched to an illegal phase (4/5).
4469 OUTONB(np, HF_PRT, HF_EXT_ERR);
4470 cp->xerr_status |= XE_BAD_PHASE;
4474 * We received a message.
4476 case SIR_MSG_RECEIVED:
4479 switch (np->msgin [0]) {
4481 * We received an extended message.
4482 * We handle MODIFY DATA POINTER, SDTR, WDTR
4483 * and reject all other extended messages.
4486 switch (np->msgin [2]) {
4488 if (DEBUG_FLAGS & DEBUG_POINTER)
4489 sym_print_msg(cp,"modify DP",np->msgin);
4490 tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
4491 (np->msgin[5]<<8) + (np->msgin[6]);
4492 sym_modify_dp(np, tp, cp, tmp);
4495 sym_sync_nego(np, tp, cp);
4498 sym_ppr_nego(np, tp, cp);
4501 sym_wide_nego(np, tp, cp);
4508 * We received a 1/2 byte message not handled from SCRIPTS.
4509 * We are only expecting MESSAGE REJECT and IGNORE WIDE
4510 * RESIDUE messages that haven't been anticipated by
4511 * SCRIPTS on SWIDE full condition. Unanticipated IGNORE
4512 * WIDE RESIDUE messages are aliased as MODIFY DP (-1).
4515 if (DEBUG_FLAGS & DEBUG_POINTER)
4516 sym_print_msg(cp,"ign wide residue", np->msgin);
4517 if (cp->host_flags & HF_SENSE)
4518 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4520 sym_modify_dp(np, tp, cp, -1);
4523 if (INB(np, HS_PRT) == HS_NEGOTIATE)
4524 sym_nego_rejected(np, tp, cp);
4526 sym_print_addr(cp->cmd,
4527 "M_REJECT received (%x:%x).\n",
4528 scr_to_cpu(np->lastmsg), np->msgout[0]);
4537 * We received an unknown message.
4538 * Ignore all MSG IN phases and reject it.
4541 sym_print_msg(cp, "WEIRD message received", np->msgin);
4542 OUTL_DSP(np, SCRIPTB_BA(np, msg_weird));
4545 * Negotiation failed.
4546 * Target does not send us the reply.
4547 * Remove the HS_NEGOTIATE status.
4549 case SIR_NEGO_FAILED:
4550 OUTB(np, HS_PRT, HS_BUSY);
4552 * Negotiation failed.
4553 * Target does not want answer message.
4555 case SIR_NEGO_PROTO:
4556 sym_nego_default(np, tp, cp);
4564 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4567 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4574 * Acquire a control block
4576 struct sym_ccb *sym_get_ccb (struct sym_hcb *np, struct scsi_cmnd *cmd, u_char tag_order)
4578 u_char tn = cmd->device->id;
4579 u_char ln = cmd->device->lun;
4580 struct sym_tcb *tp = &np->target[tn];
4581 struct sym_lcb *lp = sym_lp(tp, ln);
4582 u_short tag = NO_TAG;
4584 struct sym_ccb *cp = NULL;
4587 * Look for a free CCB
4589 if (sym_que_empty(&np->free_ccbq))
4591 qp = sym_remque_head(&np->free_ccbq);
4594 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
4598 * If we have been asked for a tagged command.
4602 * Debugging purpose.
4604 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4605 assert(lp->busy_itl == 0);
4608 * Allocate resources for tags if not yet.
4611 sym_alloc_lcb_tags(np, tn, ln);
4616 * Get a tag for this SCSI IO and set up
4617 * the CCB bus address for reselection,
4618 * and count it for this LUN.
4619 * Toggle reselect path to tagged.
4621 if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
4622 tag = lp->cb_tags[lp->ia_tag];
4623 if (++lp->ia_tag == SYM_CONF_MAX_TASK)
4626 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4627 lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
4629 cpu_to_scr(SCRIPTA_BA(np, resel_tag));
4631 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
4632 cp->tags_si = lp->tags_si;
4633 ++lp->tags_sum[cp->tags_si];
4641 * This command will not be tagged.
4642 * If we already have either a tagged or untagged
4643 * one, refuse to overlap this untagged one.
4647 * Debugging purpose.
4649 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4650 assert(lp->busy_itl == 0 && lp->busy_itlq == 0);
4653 * Count this nexus for this LUN.
4654 * Set up the CCB bus address for reselection.
4655 * Toggle reselect path to untagged.
4658 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4659 if (lp->busy_itl == 1) {
4660 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
4662 cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
4670 * Put the CCB into the busy queue.
4672 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
4673 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4675 sym_remque(&cp->link2_ccbq);
4676 sym_insque_tail(&cp->link2_ccbq, &lp->waiting_ccbq);
4681 cp->odd_byte_adjustment = 0;
4683 cp->order = tag_order;
4687 if (DEBUG_FLAGS & DEBUG_TAGS) {
4688 sym_print_addr(cmd, "ccb @%p using tag %d.\n", cp, tag);
4694 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4699 * Release one control block
4701 void sym_free_ccb (struct sym_hcb *np, struct sym_ccb *cp)
4703 struct sym_tcb *tp = &np->target[cp->target];
4704 struct sym_lcb *lp = sym_lp(tp, cp->lun);
4706 if (DEBUG_FLAGS & DEBUG_TAGS) {
4707 sym_print_addr(cp->cmd, "ccb @%p freeing tag %d.\n",
4716 * If tagged, release the tag, set the relect path
4718 if (cp->tag != NO_TAG) {
4719 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
4720 --lp->tags_sum[cp->tags_si];
4723 * Free the tag value.
4725 lp->cb_tags[lp->if_tag] = cp->tag;
4726 if (++lp->if_tag == SYM_CONF_MAX_TASK)
4729 * Make the reselect path invalid,
4730 * and uncount this CCB.
4732 lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
4734 } else { /* Untagged */
4736 * Make the reselect path invalid,
4737 * and uncount this CCB.
4739 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
4743 * If no JOB active, make the LUN reselect path invalid.
4745 if (lp->busy_itlq == 0 && lp->busy_itl == 0)
4747 cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
4751 * We donnot queue more than 1 ccb per target
4752 * with negotiation at any time. If this ccb was
4753 * used for negotiation, clear this info in the tcb.
4755 if (cp == tp->nego_cp)
4758 #ifdef SYM_CONF_IARB_SUPPORT
4760 * If we just complete the last queued CCB,
4761 * clear this info that is no longer relevant.
4763 if (cp == np->last_cp)
4768 * Make this CCB available.
4771 cp->host_status = HS_IDLE;
4772 sym_remque(&cp->link_ccbq);
4773 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4775 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4777 sym_remque(&cp->link2_ccbq);
4778 sym_insque_tail(&cp->link2_ccbq, &np->dummy_ccbq);
4780 if (cp->tag != NO_TAG)
4783 --lp->started_no_tag;
4791 * Allocate a CCB from memory and initialize its fixed part.
4793 static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np)
4795 struct sym_ccb *cp = NULL;
4799 * Prevent from allocating more CCBs than we can
4800 * queue to the controller.
4802 if (np->actccbs >= SYM_CONF_MAX_START)
4806 * Allocate memory for this CCB.
4808 cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
4818 * Compute the bus address of this ccb.
4820 cp->ccb_ba = vtobus(cp);
4823 * Insert this ccb into the hashed list.
4825 hcode = CCB_HASH_CODE(cp->ccb_ba);
4826 cp->link_ccbh = np->ccbh[hcode];
4827 np->ccbh[hcode] = cp;
4830 * Initialyze the start and restart actions.
4832 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, idle));
4833 cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
4836 * Initilialyze some other fields.
4838 cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
4841 * Chain into free ccb queue.
4843 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4846 * Chain into optionnal lists.
4848 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4849 sym_insque_head(&cp->link2_ccbq, &np->dummy_ccbq);
4854 sym_mfree_dma(cp, sizeof(*cp), "CCB");
4859 * Look up a CCB from a DSA value.
4861 static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa)
4866 hcode = CCB_HASH_CODE(dsa);
4867 cp = np->ccbh[hcode];
4869 if (cp->ccb_ba == dsa)
4878 * Target control block initialisation.
4879 * Nothing important to do at the moment.
4881 static void sym_init_tcb (struct sym_hcb *np, u_char tn)
4883 #if 0 /* Hmmm... this checking looks paranoid. */
4885 * Check some alignments required by the chip.
4887 assert (((offsetof(struct sym_reg, nc_sxfer) ^
4888 offsetof(struct sym_tcb, head.sval)) &3) == 0);
4889 assert (((offsetof(struct sym_reg, nc_scntl3) ^
4890 offsetof(struct sym_tcb, head.wval)) &3) == 0);
4895 * Lun control block allocation and initialization.
4897 struct sym_lcb *sym_alloc_lcb (struct sym_hcb *np, u_char tn, u_char ln)
4899 struct sym_tcb *tp = &np->target[tn];
4900 struct sym_lcb *lp = NULL;
4903 * Initialize the target control block if not yet.
4905 sym_init_tcb (np, tn);
4908 * Allocate the LCB bus address array.
4909 * Compute the bus address of this table.
4911 if (ln && !tp->luntbl) {
4914 tp->luntbl = sym_calloc_dma(256, "LUNTBL");
4917 for (i = 0 ; i < 64 ; i++)
4918 tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
4919 tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
4923 * Allocate the table of pointers for LUN(s) > 0, if needed.
4925 if (ln && !tp->lunmp) {
4926 tp->lunmp = kcalloc(SYM_CONF_MAX_LUN, sizeof(struct sym_lcb *),
4934 * Make it available to the chip.
4936 lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
4941 tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
4945 tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
4949 * Let the itl task point to error handling.
4951 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
4954 * Set the reselect pattern to our default. :)
4956 lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
4959 * Set user capabilities.
4961 lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
4963 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4965 * Initialize device queueing.
4967 sym_que_init(&lp->waiting_ccbq);
4968 sym_que_init(&lp->started_ccbq);
4969 lp->started_max = SYM_CONF_MAX_TASK;
4970 lp->started_limit = SYM_CONF_MAX_TASK;
4978 * Allocate LCB resources for tagged command queuing.
4980 static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln)
4982 struct sym_tcb *tp = &np->target[tn];
4983 struct sym_lcb *lp = sym_lp(tp, ln);
4987 * Allocate the task table and and the tag allocation
4988 * circular buffer. We want both or none.
4990 lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
4993 lp->cb_tags = kcalloc(SYM_CONF_MAX_TASK, 1, GFP_ATOMIC);
4995 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
4996 lp->itlq_tbl = NULL;
5001 * Initialize the task table with invalid entries.
5003 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
5004 lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
5007 * Fill up the tag buffer with tag numbers.
5009 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
5013 * Make the task table available to SCRIPTS,
5014 * And accept tagged commands now.
5016 lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
5024 * Queue a SCSI IO to the controller.
5026 int sym_queue_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
5028 struct scsi_device *sdev = cmd->device;
5036 * Keep track of the IO in our CCB.
5041 * Retrieve the target descriptor.
5043 tp = &np->target[cp->target];
5046 * Retrieve the lun descriptor.
5048 lp = sym_lp(tp, sdev->lun);
5050 can_disconnect = (cp->tag != NO_TAG) ||
5051 (lp && (lp->curr_flags & SYM_DISC_ENABLED));
5053 msgptr = cp->scsi_smsg;
5055 msgptr[msglen++] = IDENTIFY(can_disconnect, sdev->lun);
5058 * Build the tag message if present.
5060 if (cp->tag != NO_TAG) {
5061 u_char order = cp->order;
5069 order = M_SIMPLE_TAG;
5071 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
5073 * Avoid too much reordering of SCSI commands.
5074 * The algorithm tries to prevent completion of any
5075 * tagged command from being delayed against more
5076 * than 3 times the max number of queued commands.
5078 if (lp && lp->tags_since > 3*SYM_CONF_MAX_TAG) {
5079 lp->tags_si = !(lp->tags_si);
5080 if (lp->tags_sum[lp->tags_si]) {
5081 order = M_ORDERED_TAG;
5082 if ((DEBUG_FLAGS & DEBUG_TAGS)||sym_verbose>1) {
5084 "ordered tag forced.\n");
5090 msgptr[msglen++] = order;
5093 * For less than 128 tags, actual tags are numbered
5094 * 1,3,5,..2*MAXTAGS+1,since we may have to deal
5095 * with devices that have problems with #TAG 0 or too
5096 * great #TAG numbers. For more tags (up to 256),
5097 * we use directly our tag number.
5099 #if SYM_CONF_MAX_TASK > (512/4)
5100 msgptr[msglen++] = cp->tag;
5102 msgptr[msglen++] = (cp->tag << 1) + 1;
5107 * Build a negotiation message if needed.
5108 * (nego_status is filled by sym_prepare_nego())
5110 cp->nego_status = 0;
5111 if (tp->tgoal.check_nego && !tp->nego_cp && lp) {
5112 msglen += sym_prepare_nego(np, cp, msgptr + msglen);
5118 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
5119 cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA(np, resel_dsa));
5124 cp->phys.select.sel_id = cp->target;
5125 cp->phys.select.sel_scntl3 = tp->head.wval;
5126 cp->phys.select.sel_sxfer = tp->head.sval;
5127 cp->phys.select.sel_scntl4 = tp->head.uval;
5132 cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg);
5133 cp->phys.smsg.size = cpu_to_scr(msglen);
5138 cp->host_xflags = 0;
5139 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
5140 cp->ssss_status = S_ILLEGAL;
5141 cp->xerr_status = 0;
5143 cp->extra_bytes = 0;
5146 * extreme data pointer.
5147 * shall be positive, so -1 is lower than lowest.:)
5153 * Build the CDB and DATA descriptor block
5156 return sym_setup_data_and_start(np, cmd, cp);
5160 * Reset a SCSI target (all LUNs of this target).
5162 int sym_reset_scsi_target(struct sym_hcb *np, int target)
5166 if (target == np->myaddr || (u_int)target >= SYM_CONF_MAX_TARGET)
5169 tp = &np->target[target];
5172 np->istat_sem = SEM;
5173 OUTB(np, nc_istat, SIGP|SEM);
5181 static int sym_abort_ccb(struct sym_hcb *np, struct sym_ccb *cp, int timed_out)
5184 * Check that the IO is active.
5186 if (!cp || !cp->host_status || cp->host_status == HS_WAIT)
5190 * If a previous abort didn't succeed in time,
5191 * perform a BUS reset.
5194 sym_reset_scsi_bus(np, 1);
5199 * Mark the CCB for abort and allow time for.
5201 cp->to_abort = timed_out ? 2 : 1;
5204 * Tell the SCRIPTS processor to stop and synchronize with us.
5206 np->istat_sem = SEM;
5207 OUTB(np, nc_istat, SIGP|SEM);
5211 int sym_abort_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, int timed_out)
5217 * Look up our CCB control block.
5220 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
5221 struct sym_ccb *cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
5222 if (cp2->cmd == cmd) {
5228 return sym_abort_ccb(np, cp, timed_out);
5232 * Complete execution of a SCSI command with extended
5233 * error, SCSI status error, or having been auto-sensed.
5235 * The SCRIPTS processor is not running there, so we
5236 * can safely access IO registers and remove JOBs from
5238 * SCRATCHA is assumed to have been loaded with STARTPOS
5239 * before the SCRIPTS called the C code.
5241 void sym_complete_error(struct sym_hcb *np, struct sym_ccb *cp)
5243 struct scsi_device *sdev;
5244 struct scsi_cmnd *cmd;
5251 * Paranoid check. :)
5253 if (!cp || !cp->cmd)
5258 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
5259 dev_info(&sdev->sdev_gendev, "CCB=%p STAT=%x/%x/%x\n", cp,
5260 cp->host_status, cp->ssss_status, cp->host_flags);
5264 * Get target and lun pointers.
5266 tp = &np->target[cp->target];
5267 lp = sym_lp(tp, sdev->lun);
5270 * Check for extended errors.
5272 if (cp->xerr_status) {
5274 sym_print_xerr(cmd, cp->xerr_status);
5275 if (cp->host_status == HS_COMPLETE)
5276 cp->host_status = HS_COMP_ERR;
5280 * Calculate the residual.
5282 resid = sym_compute_residual(np, cp);
5284 if (!SYM_SETUP_RESIDUAL_SUPPORT) {/* If user does not want residuals */
5285 resid = 0; /* throw them away. :) */
5290 printf("XXXX RESID= %d - 0x%x\n", resid, resid);
5294 * Dequeue all queued CCBs for that device
5295 * not yet started by SCRIPTS.
5297 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
5298 i = sym_dequeue_from_squeue(np, i, cp->target, sdev->lun, -1);
5301 * Restart the SCRIPTS processor.
5303 OUTL_DSP(np, SCRIPTA_BA(np, start));
5305 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5306 if (cp->host_status == HS_COMPLETE &&
5307 cp->ssss_status == S_QUEUE_FULL) {
5308 if (!lp || lp->started_tags - i < 2)
5311 * Decrease queue depth as needed.
5313 lp->started_max = lp->started_tags - i - 1;
5316 if (sym_verbose >= 2) {
5317 sym_print_addr(cmd, " queue depth is now %d\n",
5324 cp->host_status = HS_BUSY;
5325 cp->ssss_status = S_ILLEGAL;
5328 * Let's requeue it to device.
5330 sym_set_cam_status(cmd, DID_SOFT_ERROR);
5336 * Build result in CAM ccb.
5338 sym_set_cam_result_error(np, cp, resid);
5340 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5344 * Add this one to the COMP queue.
5346 sym_remque(&cp->link_ccbq);
5347 sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
5350 * Complete all those commands with either error
5351 * or requeue condition.
5353 sym_flush_comp_queue(np, 0);
5355 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5357 * Donnot start more than 1 command after an error.
5359 sym_start_next_ccbs(np, lp, 1);
5364 * Complete execution of a successful SCSI command.
5366 * Only successful commands go to the DONE queue,
5367 * since we need to have the SCRIPTS processor
5368 * stopped on any error condition.
5369 * The SCRIPTS processor is running while we are
5370 * completing successful commands.
5372 void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp)
5376 struct scsi_cmnd *cmd;
5380 * Paranoid check. :)
5382 if (!cp || !cp->cmd)
5384 assert (cp->host_status == HS_COMPLETE);
5392 * Get target and lun pointers.
5394 tp = &np->target[cp->target];
5395 lp = sym_lp(tp, cp->lun);
5398 * If all data have been transferred, given than no
5399 * extended error did occur, there is no residual.
5402 if (cp->phys.head.lastp != cp->goalp)
5403 resid = sym_compute_residual(np, cp);
5406 * Wrong transfer residuals may be worse than just always
5407 * returning zero. User can disable this feature in
5408 * sym53c8xx.h. Residual support is enabled by default.
5410 if (!SYM_SETUP_RESIDUAL_SUPPORT)
5414 printf("XXXX RESID= %d - 0x%x\n", resid, resid);
5418 * Build result in CAM ccb.
5420 sym_set_cam_result_ok(cp, cmd, resid);
5422 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5424 * If max number of started ccbs had been reduced,
5425 * increase it if 200 good status received.
5427 if (lp && lp->started_max < lp->started_limit) {
5429 if (lp->num_sgood >= 200) {
5432 if (sym_verbose >= 2) {
5433 sym_print_addr(cmd, " queue depth is now %d\n",
5443 sym_free_ccb (np, cp);
5445 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5447 * Requeue a couple of awaiting scsi commands.
5449 if (!sym_que_empty(&lp->waiting_ccbq))
5450 sym_start_next_ccbs(np, lp, 2);
5453 * Complete the command.
5455 sym_xpt_done(np, cmd);
5459 * Soft-attach the controller.
5461 int sym_hcb_attach(struct Scsi_Host *shost, struct sym_fw *fw, struct sym_nvram *nvram)
5463 struct sym_hcb *np = sym_get_hcb(shost);
5467 * Get some info about the firmware.
5469 np->scripta_sz = fw->a_size;
5470 np->scriptb_sz = fw->b_size;
5471 np->scriptz_sz = fw->z_size;
5472 np->fw_setup = fw->setup;
5473 np->fw_patch = fw->patch;
5474 np->fw_name = fw->name;
5477 * Save setting of some IO registers, so we will
5478 * be able to probe specific implementations.
5480 sym_save_initial_setting (np);
5483 * Reset the chip now, since it has been reported
5484 * that SCSI clock calibration may not work properly
5485 * if the chip is currently active.
5490 * Prepare controller and devices settings, according
5491 * to chip features, user set-up and driver set-up.
5493 sym_prepare_setting(shost, np, nvram);
5496 * Check the PCI clock frequency.
5497 * Must be performed after prepare_setting since it destroys
5498 * STEST1 that is used to probe for the clock doubler.
5500 i = sym_getpciclock(np);
5501 if (i > 37000 && !(np->features & FE_66MHZ))
5502 printf("%s: PCI BUS clock seems too high: %u KHz.\n",
5506 * Allocate the start queue.
5508 np->squeue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
5511 np->squeue_ba = vtobus(np->squeue);
5514 * Allocate the done queue.
5516 np->dqueue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
5519 np->dqueue_ba = vtobus(np->dqueue);
5522 * Allocate the target bus address array.
5524 np->targtbl = sym_calloc_dma(256, "TARGTBL");
5527 np->targtbl_ba = vtobus(np->targtbl);
5530 * Allocate SCRIPTS areas.
5532 np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
5533 np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
5534 np->scriptz0 = sym_calloc_dma(np->scriptz_sz, "SCRIPTZ0");
5535 if (!np->scripta0 || !np->scriptb0 || !np->scriptz0)
5539 * Allocate the array of lists of CCBs hashed by DSA.
5541 np->ccbh = kcalloc(sizeof(struct sym_ccb **), CCB_HASH_SIZE, GFP_KERNEL);
5546 * Initialyze the CCB free and busy queues.
5548 sym_que_init(&np->free_ccbq);
5549 sym_que_init(&np->busy_ccbq);
5550 sym_que_init(&np->comp_ccbq);
5553 * Initialization for optional handling
5554 * of device queueing.
5556 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5557 sym_que_init(&np->dummy_ccbq);
5560 * Allocate some CCB. We need at least ONE.
5562 if (!sym_alloc_ccb(np))
5566 * Calculate BUS addresses where we are going
5567 * to load the SCRIPTS.
5569 np->scripta_ba = vtobus(np->scripta0);
5570 np->scriptb_ba = vtobus(np->scriptb0);
5571 np->scriptz_ba = vtobus(np->scriptz0);
5574 np->scripta_ba = np->ram_ba;
5575 if (np->features & FE_RAM8K) {
5577 np->scriptb_ba = np->scripta_ba + 4096;
5578 #if 0 /* May get useful for 64 BIT PCI addressing */
5579 np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32);
5587 * Copy scripts to controller instance.
5589 memcpy(np->scripta0, fw->a_base, np->scripta_sz);
5590 memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
5591 memcpy(np->scriptz0, fw->z_base, np->scriptz_sz);
5594 * Setup variable parts in scripts and compute
5595 * scripts bus addresses used from the C code.
5597 np->fw_setup(np, fw);
5600 * Bind SCRIPTS with physical addresses usable by the
5601 * SCRIPTS processor (as seen from the BUS = BUS addresses).
5603 sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
5604 sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
5605 sym_fw_bind_script(np, (u32 *) np->scriptz0, np->scriptz_sz);
5607 #ifdef SYM_CONF_IARB_SUPPORT
5609 * If user wants IARB to be set when we win arbitration
5610 * and have other jobs, compute the max number of consecutive
5611 * settings of IARB hints before we leave devices a chance to
5612 * arbitrate for reselection.
5614 #ifdef SYM_SETUP_IARB_MAX
5615 np->iarb_max = SYM_SETUP_IARB_MAX;
5622 * Prepare the idle and invalid task actions.
5624 np->idletask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5625 np->idletask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5626 np->idletask_ba = vtobus(&np->idletask);
5628 np->notask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5629 np->notask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5630 np->notask_ba = vtobus(&np->notask);
5632 np->bad_itl.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5633 np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5634 np->bad_itl_ba = vtobus(&np->bad_itl);
5636 np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5637 np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA(np,bad_i_t_l_q));
5638 np->bad_itlq_ba = vtobus(&np->bad_itlq);
5641 * Allocate and prepare the lun JUMP table that is used
5642 * for a target prior the probing of devices (bad lun table).
5643 * A private table will be allocated for the target on the
5644 * first INQUIRY response received.
5646 np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
5650 np->badlun_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
5651 for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */
5652 np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
5655 * Prepare the bus address array that contains the bus
5656 * address of each target control block.
5657 * For now, assume all logical units are wrong. :)
5659 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
5660 np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
5661 np->target[i].head.luntbl_sa =
5662 cpu_to_scr(vtobus(np->badluntbl));
5663 np->target[i].head.lun0_sa =
5664 cpu_to_scr(vtobus(&np->badlun_sa));
5668 * Now check the cache handling of the pci chipset.
5670 if (sym_snooptest (np)) {
5671 printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np));
5676 * Sigh! we are done.
5685 * Free everything that has been allocated for this device.
5687 void sym_hcb_free(struct sym_hcb *np)
5695 sym_mfree_dma(np->scriptz0, np->scriptz_sz, "SCRIPTZ0");
5697 sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
5699 sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
5701 sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
5703 sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
5706 while ((qp = sym_remque_head(&np->free_ccbq)) != 0) {
5707 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
5708 sym_mfree_dma(cp, sizeof(*cp), "CCB");
5714 sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
5716 for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
5717 tp = &np->target[target];
5718 #if SYM_CONF_MAX_LUN > 1
5723 sym_mfree_dma(np->targtbl, 256, "TARGTBL");