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 */
43 #include <scsi/scsi_dbg.h>
46 #include "sym_nvram.h"
49 #define SYM_DEBUG_GENERIC_SUPPORT
53 * Needed function prototypes.
55 static void sym_int_ma (struct sym_hcb *np);
56 static void sym_int_sir (struct sym_hcb *np);
57 static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np);
58 static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa);
59 static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln);
60 static void sym_complete_error (struct sym_hcb *np, struct sym_ccb *cp);
61 static void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp);
62 static int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp);
65 * Print a buffer in hexadecimal format with a ".\n" at end.
67 static void sym_printl_hex(u_char *p, int n)
74 static void sym_print_msg(struct sym_ccb *cp, char *label, u_char *msg)
76 sym_print_addr(cp->cmd, "%s: ", label);
82 static void sym_print_nego_msg(struct sym_hcb *np, int target, char *label, u_char *msg)
84 struct sym_tcb *tp = &np->target[target];
85 dev_info(&tp->starget->dev, "%s: ", label);
92 * Print something that tells about extended errors.
94 void sym_print_xerr(struct scsi_cmnd *cmd, int x_status)
96 if (x_status & XE_PARITY_ERR) {
97 sym_print_addr(cmd, "unrecovered SCSI parity error.\n");
99 if (x_status & XE_EXTRA_DATA) {
100 sym_print_addr(cmd, "extraneous data discarded.\n");
102 if (x_status & XE_BAD_PHASE) {
103 sym_print_addr(cmd, "illegal scsi phase (4/5).\n");
105 if (x_status & XE_SODL_UNRUN) {
106 sym_print_addr(cmd, "ODD transfer in DATA OUT phase.\n");
108 if (x_status & XE_SWIDE_OVRUN) {
109 sym_print_addr(cmd, "ODD transfer in DATA IN phase.\n");
114 * Return a string for SCSI BUS mode.
116 static char *sym_scsi_bus_mode(int mode)
119 case SMODE_HVD: return "HVD";
120 case SMODE_SE: return "SE";
121 case SMODE_LVD: return "LVD";
127 * Soft reset the chip.
129 * Raising SRST when the chip is running may cause
130 * problems on dual function chips (see below).
131 * On the other hand, LVD devices need some delay
132 * to settle and report actual BUS mode in STEST4.
134 static void sym_chip_reset (struct sym_hcb *np)
136 OUTB(np, nc_istat, SRST);
139 OUTB(np, nc_istat, 0);
141 udelay(2000); /* For BUS MODE to settle */
145 * Really soft reset the chip.:)
147 * Some 896 and 876 chip revisions may hang-up if we set
148 * the SRST (soft reset) bit at the wrong time when SCRIPTS
150 * So, we need to abort the current operation prior to
151 * soft resetting the chip.
153 static void sym_soft_reset (struct sym_hcb *np)
158 if (!(np->features & FE_ISTAT1) || !(INB(np, nc_istat1) & SCRUN))
161 OUTB(np, nc_istat, CABRT);
162 for (i = 100000 ; i ; --i) {
163 istat = INB(np, nc_istat);
167 else if (istat & DIP) {
168 if (INB(np, nc_dstat) & ABRT)
173 OUTB(np, nc_istat, 0);
175 printf("%s: unable to abort current chip operation, "
176 "ISTAT=0x%02x.\n", sym_name(np), istat);
182 * Start reset process.
184 * The interrupt handler will reinitialize the chip.
186 static void sym_start_reset(struct sym_hcb *np)
188 sym_reset_scsi_bus(np, 1);
191 int sym_reset_scsi_bus(struct sym_hcb *np, int enab_int)
196 sym_soft_reset(np); /* Soft reset the chip */
198 OUTW(np, nc_sien, RST);
200 * Enable Tolerant, reset IRQD if present and
201 * properly set IRQ mode, prior to resetting the bus.
203 OUTB(np, nc_stest3, TE);
204 OUTB(np, nc_dcntl, (np->rv_dcntl & IRQM));
205 OUTB(np, nc_scntl1, CRST);
209 if (!SYM_SETUP_SCSI_BUS_CHECK)
212 * Check for no terminators or SCSI bus shorts to ground.
213 * Read SCSI data bus, data parity bits and control signals.
214 * We are expecting RESET to be TRUE and other signals to be
217 term = INB(np, nc_sstat0);
218 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
219 term |= ((INB(np, nc_sstat2) & 0x01) << 26) | /* sdp1 */
220 ((INW(np, nc_sbdl) & 0xff) << 9) | /* d7-0 */
221 ((INW(np, nc_sbdl) & 0xff00) << 10) | /* d15-8 */
222 INB(np, nc_sbcl); /* req ack bsy sel atn msg cd io */
227 if (term != (2<<7)) {
228 printf("%s: suspicious SCSI data while resetting the BUS.\n",
230 printf("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
231 "0x%lx, expecting 0x%lx\n",
233 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
234 (u_long)term, (u_long)(2<<7));
235 if (SYM_SETUP_SCSI_BUS_CHECK == 1)
239 OUTB(np, nc_scntl1, 0);
244 * Select SCSI clock frequency
246 static void sym_selectclock(struct sym_hcb *np, u_char scntl3)
249 * If multiplier not present or not selected, leave here.
251 if (np->multiplier <= 1) {
252 OUTB(np, nc_scntl3, scntl3);
256 if (sym_verbose >= 2)
257 printf ("%s: enabling clock multiplier\n", sym_name(np));
259 OUTB(np, nc_stest1, DBLEN); /* Enable clock multiplier */
261 * Wait for the LCKFRQ bit to be set if supported by the chip.
262 * Otherwise wait 50 micro-seconds (at least).
264 if (np->features & FE_LCKFRQ) {
266 while (!(INB(np, nc_stest4) & LCKFRQ) && --i > 0)
269 printf("%s: the chip cannot lock the frequency\n",
275 OUTB(np, nc_stest3, HSC); /* Halt the scsi clock */
276 OUTB(np, nc_scntl3, scntl3);
277 OUTB(np, nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
278 OUTB(np, nc_stest3, 0x00); /* Restart scsi clock */
283 * Determine the chip's clock frequency.
285 * This is essential for the negotiation of the synchronous
288 * Note: we have to return the correct value.
289 * THERE IS NO SAFE DEFAULT VALUE.
291 * Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
292 * 53C860 and 53C875 rev. 1 support fast20 transfers but
293 * do not have a clock doubler and so are provided with a
294 * 80 MHz clock. All other fast20 boards incorporate a doubler
295 * and so should be delivered with a 40 MHz clock.
296 * The recent fast40 chips (895/896/895A/1010) use a 40 Mhz base
297 * clock and provide a clock quadrupler (160 Mhz).
301 * calculate SCSI clock frequency (in KHz)
303 static unsigned getfreq (struct sym_hcb *np, int gen)
309 * Measure GEN timer delay in order
310 * to calculate SCSI clock frequency
312 * This code will never execute too
313 * many loop iterations (if DELAY is
314 * reasonably correct). It could get
315 * too low a delay (too high a freq.)
316 * if the CPU is slow executing the
317 * loop for some reason (an NMI, for
318 * example). For this reason we will
319 * if multiple measurements are to be
320 * performed trust the higher delay
321 * (lower frequency returned).
323 OUTW(np, nc_sien, 0); /* mask all scsi interrupts */
324 INW(np, nc_sist); /* clear pending scsi interrupt */
325 OUTB(np, nc_dien, 0); /* mask all dma interrupts */
326 INW(np, nc_sist); /* another one, just to be sure :) */
328 * The C1010-33 core does not report GEN in SIST,
329 * if this interrupt is masked in SIEN.
330 * I don't know yet if the C1010-66 behaves the same way.
332 if (np->features & FE_C10) {
333 OUTW(np, nc_sien, GEN);
334 OUTB(np, nc_istat1, SIRQD);
336 OUTB(np, nc_scntl3, 4); /* set pre-scaler to divide by 3 */
337 OUTB(np, nc_stime1, 0); /* disable general purpose timer */
338 OUTB(np, nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
339 while (!(INW(np, nc_sist) & GEN) && ms++ < 100000)
340 udelay(1000/4); /* count in 1/4 of ms */
341 OUTB(np, nc_stime1, 0); /* disable general purpose timer */
343 * Undo C1010-33 specific settings.
345 if (np->features & FE_C10) {
346 OUTW(np, nc_sien, 0);
347 OUTB(np, nc_istat1, 0);
350 * set prescaler to divide by whatever 0 means
351 * 0 ought to choose divide by 2, but appears
352 * to set divide by 3.5 mode in my 53c810 ...
354 OUTB(np, nc_scntl3, 0);
357 * adjust for prescaler, and convert into KHz
359 f = ms ? ((1 << gen) * (4340*4)) / ms : 0;
362 * The C1010-33 result is biased by a factor
363 * of 2/3 compared to earlier chips.
365 if (np->features & FE_C10)
368 if (sym_verbose >= 2)
369 printf ("%s: Delay (GEN=%d): %u msec, %u KHz\n",
370 sym_name(np), gen, ms/4, f);
375 static unsigned sym_getfreq (struct sym_hcb *np)
380 getfreq (np, gen); /* throw away first result */
381 f1 = getfreq (np, gen);
382 f2 = getfreq (np, gen);
383 if (f1 > f2) f1 = f2; /* trust lower result */
388 * Get/probe chip SCSI clock frequency
390 static void sym_getclock (struct sym_hcb *np, int mult)
392 unsigned char scntl3 = np->sv_scntl3;
393 unsigned char stest1 = np->sv_stest1;
399 * True with 875/895/896/895A with clock multiplier selected
401 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
402 if (sym_verbose >= 2)
403 printf ("%s: clock multiplier found\n", sym_name(np));
404 np->multiplier = mult;
408 * If multiplier not found or scntl3 not 7,5,3,
409 * reset chip and get frequency from general purpose timer.
410 * Otherwise trust scntl3 BIOS setting.
412 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
413 OUTB(np, nc_stest1, 0); /* make sure doubler is OFF */
414 f1 = sym_getfreq (np);
417 printf ("%s: chip clock is %uKHz\n", sym_name(np), f1);
419 if (f1 < 45000) f1 = 40000;
420 else if (f1 < 55000) f1 = 50000;
423 if (f1 < 80000 && mult > 1) {
424 if (sym_verbose >= 2)
425 printf ("%s: clock multiplier assumed\n",
427 np->multiplier = mult;
430 if ((scntl3 & 7) == 3) f1 = 40000;
431 else if ((scntl3 & 7) == 5) f1 = 80000;
434 f1 /= np->multiplier;
438 * Compute controller synchronous parameters.
440 f1 *= np->multiplier;
445 * Get/probe PCI clock frequency
447 static int sym_getpciclock (struct sym_hcb *np)
452 * For now, we only need to know about the actual
453 * PCI BUS clock frequency for C1010-66 chips.
456 if (np->features & FE_66MHZ) {
460 OUTB(np, nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */
462 OUTB(np, nc_stest1, 0);
470 * SYMBIOS chip clock divisor table.
472 * Divisors are multiplied by 10,000,000 in order to make
473 * calculations more simple.
476 static u32 div_10M[] = {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
479 * Get clock factor and sync divisor for a given
480 * synchronous factor period.
483 sym_getsync(struct sym_hcb *np, u_char dt, u_char sfac, u_char *divp, u_char *fakp)
485 u32 clk = np->clock_khz; /* SCSI clock frequency in kHz */
486 int div = np->clock_divn; /* Number of divisors supported */
487 u32 fak; /* Sync factor in sxfer */
488 u32 per; /* Period in tenths of ns */
489 u32 kpc; /* (per * clk) */
493 * Compute the synchronous period in tenths of nano-seconds
495 if (dt && sfac <= 9) per = 125;
496 else if (sfac <= 10) per = 250;
497 else if (sfac == 11) per = 303;
498 else if (sfac == 12) per = 500;
499 else per = 40 * sfac;
507 * For earliest C10 revision 0, we cannot use extra
508 * clocks for the setting of the SCSI clocking.
509 * Note that this limits the lowest sync data transfer
510 * to 5 Mega-transfers per second and may result in
511 * using higher clock divisors.
514 if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) {
516 * Look for the lowest clock divisor that allows an
517 * output speed not faster than the period.
521 if (kpc > (div_10M[div] << 2)) {
526 fak = 0; /* No extra clocks */
527 if (div == np->clock_divn) { /* Are we too fast ? */
537 * Look for the greatest clock divisor that allows an
538 * input speed faster than the period.
541 if (kpc >= (div_10M[div] << 2)) break;
544 * Calculate the lowest clock factor that allows an output
545 * speed not faster than the period, and the max output speed.
546 * If fak >= 1 we will set both XCLKH_ST and XCLKH_DT.
547 * If fak >= 2 we will also set XCLKS_ST and XCLKS_DT.
550 fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2;
551 /* ret = ((2+fak)*div_10M[div])/np->clock_khz; */
553 fak = (kpc - 1) / div_10M[div] + 1 - 4;
554 /* ret = ((4+fak)*div_10M[div])/np->clock_khz; */
558 * Check against our hardware limits, or bugs :).
566 * Compute and return sync parameters.
575 * SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64,
576 * 128 transfers. All chips support at least 16 transfers
577 * bursts. The 825A, 875 and 895 chips support bursts of up
578 * to 128 transfers and the 895A and 896 support bursts of up
579 * to 64 transfers. All other chips support up to 16
582 * For PCI 32 bit data transfers each transfer is a DWORD.
583 * It is a QUADWORD (8 bytes) for PCI 64 bit data transfers.
585 * We use log base 2 (burst length) as internal code, with
586 * value 0 meaning "burst disabled".
590 * Burst length from burst code.
592 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
595 * Burst code from io register bits.
597 #define burst_code(dmode, ctest4, ctest5) \
598 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
601 * Set initial io register bits from burst code.
603 static __inline void sym_init_burst(struct sym_hcb *np, u_char bc)
605 np->rv_ctest4 &= ~0x80;
606 np->rv_dmode &= ~(0x3 << 6);
607 np->rv_ctest5 &= ~0x4;
610 np->rv_ctest4 |= 0x80;
614 np->rv_dmode |= ((bc & 0x3) << 6);
615 np->rv_ctest5 |= (bc & 0x4);
620 * Save initial settings of some IO registers.
621 * Assumed to have been set by BIOS.
622 * We cannot reset the chip prior to reading the
623 * IO registers, since informations will be lost.
624 * Since the SCRIPTS processor may be running, this
625 * is not safe on paper, but it seems to work quite
628 static void sym_save_initial_setting (struct sym_hcb *np)
630 np->sv_scntl0 = INB(np, nc_scntl0) & 0x0a;
631 np->sv_scntl3 = INB(np, nc_scntl3) & 0x07;
632 np->sv_dmode = INB(np, nc_dmode) & 0xce;
633 np->sv_dcntl = INB(np, nc_dcntl) & 0xa8;
634 np->sv_ctest3 = INB(np, nc_ctest3) & 0x01;
635 np->sv_ctest4 = INB(np, nc_ctest4) & 0x80;
636 np->sv_gpcntl = INB(np, nc_gpcntl);
637 np->sv_stest1 = INB(np, nc_stest1);
638 np->sv_stest2 = INB(np, nc_stest2) & 0x20;
639 np->sv_stest4 = INB(np, nc_stest4);
640 if (np->features & FE_C10) { /* Always large DMA fifo + ultra3 */
641 np->sv_scntl4 = INB(np, nc_scntl4);
642 np->sv_ctest5 = INB(np, nc_ctest5) & 0x04;
645 np->sv_ctest5 = INB(np, nc_ctest5) & 0x24;
649 * Prepare io register values used by sym_start_up()
650 * according to selected and supported features.
652 static int sym_prepare_setting(struct Scsi_Host *shost, struct sym_hcb *np, struct sym_nvram *nvram)
661 np->maxwide = (np->features & FE_WIDE)? 1 : 0;
664 * Guess the frequency of the chip's clock.
666 if (np->features & (FE_ULTRA3 | FE_ULTRA2))
667 np->clock_khz = 160000;
668 else if (np->features & FE_ULTRA)
669 np->clock_khz = 80000;
671 np->clock_khz = 40000;
674 * Get the clock multiplier factor.
676 if (np->features & FE_QUAD)
678 else if (np->features & FE_DBLR)
684 * Measure SCSI clock frequency for chips
685 * it may vary from assumed one.
687 if (np->features & FE_VARCLK)
688 sym_getclock(np, np->multiplier);
691 * Divisor to be used for async (timer pre-scaler).
693 i = np->clock_divn - 1;
695 if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) {
703 * The C1010 uses hardwired divisors for async.
704 * So, we just throw away, the async. divisor.:-)
706 if (np->features & FE_C10)
710 * Minimum synchronous period factor supported by the chip.
711 * Btw, 'period' is in tenths of nanoseconds.
713 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
715 if (period <= 250) np->minsync = 10;
716 else if (period <= 303) np->minsync = 11;
717 else if (period <= 500) np->minsync = 12;
718 else np->minsync = (period + 40 - 1) / 40;
721 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
723 if (np->minsync < 25 &&
724 !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
726 else if (np->minsync < 12 &&
727 !(np->features & (FE_ULTRA2|FE_ULTRA3)))
731 * Maximum synchronous period factor supported by the chip.
733 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
734 np->maxsync = period > 2540 ? 254 : period / 10;
737 * If chip is a C1010, guess the sync limits in DT mode.
739 if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) {
740 if (np->clock_khz == 160000) {
743 np->maxoffs_dt = nvram->type ? 62 : 31;
748 * 64 bit addressing (895A/896/1010) ?
750 if (np->features & FE_DAC) {
751 #if SYM_CONF_DMA_ADDRESSING_MODE == 0
752 np->rv_ccntl1 |= (DDAC);
753 #elif SYM_CONF_DMA_ADDRESSING_MODE == 1
755 np->rv_ccntl1 |= (DDAC);
757 np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
758 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
760 np->rv_ccntl1 |= (DDAC);
762 np->rv_ccntl1 |= (0 | EXTIBMV);
767 * Phase mismatch handled by SCRIPTS (895A/896/1010) ?
769 if (np->features & FE_NOPM)
770 np->rv_ccntl0 |= (ENPMJ);
773 * C1010-33 Errata: Part Number:609-039638 (rev. 1) is fixed.
774 * In dual channel mode, contention occurs if internal cycles
775 * are used. Disable internal cycles.
777 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
778 np->revision_id < 0x1)
779 np->rv_ccntl0 |= DILS;
782 * Select burst length (dwords)
784 burst_max = SYM_SETUP_BURST_ORDER;
785 if (burst_max == 255)
786 burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
790 if (burst_max > np->maxburst)
791 burst_max = np->maxburst;
794 * DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2.
795 * This chip and the 860 Rev 1 may wrongly use PCI cache line
796 * based transactions on LOAD/STORE instructions. So we have
797 * to prevent these chips from using such PCI transactions in
798 * this driver. The generic ncr driver that does not use
799 * LOAD/STORE instructions does not need this work-around.
801 if ((np->device_id == PCI_DEVICE_ID_NCR_53C810 &&
802 np->revision_id >= 0x10 && np->revision_id <= 0x11) ||
803 (np->device_id == PCI_DEVICE_ID_NCR_53C860 &&
804 np->revision_id <= 0x1))
805 np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
808 * Select all supported special features.
809 * If we are using on-board RAM for scripts, prefetch (PFEN)
810 * does not help, but burst op fetch (BOF) does.
811 * Disabling PFEN makes sure BOF will be used.
813 if (np->features & FE_ERL)
814 np->rv_dmode |= ERL; /* Enable Read Line */
815 if (np->features & FE_BOF)
816 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
817 if (np->features & FE_ERMP)
818 np->rv_dmode |= ERMP; /* Enable Read Multiple */
820 if ((np->features & FE_PFEN) && !np->ram_ba)
822 if (np->features & FE_PFEN)
824 np->rv_dcntl |= PFEN; /* Prefetch Enable */
825 if (np->features & FE_CLSE)
826 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
827 if (np->features & FE_WRIE)
828 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
829 if (np->features & FE_DFS)
830 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
835 np->rv_ctest4 |= MPEE; /* Master parity checking */
836 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
839 * Get parity checking, host ID and verbose mode from NVRAM
842 sym_nvram_setup_host(shost, np, nvram);
845 * Get SCSI addr of host adapter (set by bios?).
847 if (np->myaddr == 255) {
848 np->myaddr = INB(np, nc_scid) & 0x07;
850 np->myaddr = SYM_SETUP_HOST_ID;
854 * Prepare initial io register bits for burst length
856 sym_init_burst(np, burst_max);
860 * - LVD capable chips (895/895A/896/1010) report the
861 * current BUS mode through the STEST4 IO register.
862 * - For previous generation chips (825/825A/875),
863 * user has to tell us how to check against HVD,
864 * since a 100% safe algorithm is not possible.
866 np->scsi_mode = SMODE_SE;
867 if (np->features & (FE_ULTRA2|FE_ULTRA3))
868 np->scsi_mode = (np->sv_stest4 & SMODE);
869 else if (np->features & FE_DIFF) {
870 if (SYM_SETUP_SCSI_DIFF == 1) {
872 if (np->sv_stest2 & 0x20)
873 np->scsi_mode = SMODE_HVD;
875 else if (nvram->type == SYM_SYMBIOS_NVRAM) {
876 if (!(INB(np, nc_gpreg) & 0x08))
877 np->scsi_mode = SMODE_HVD;
880 else if (SYM_SETUP_SCSI_DIFF == 2)
881 np->scsi_mode = SMODE_HVD;
883 if (np->scsi_mode == SMODE_HVD)
884 np->rv_stest2 |= 0x20;
887 * Set LED support from SCRIPTS.
888 * Ignore this feature for boards known to use a
889 * specific GPIO wiring and for the 895A, 896
890 * and 1010 that drive the LED directly.
892 if ((SYM_SETUP_SCSI_LED ||
893 (nvram->type == SYM_SYMBIOS_NVRAM ||
894 (nvram->type == SYM_TEKRAM_NVRAM &&
895 np->device_id == PCI_DEVICE_ID_NCR_53C895))) &&
896 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
897 np->features |= FE_LED0;
902 switch(SYM_SETUP_IRQ_MODE & 3) {
904 np->rv_dcntl |= IRQM;
907 np->rv_dcntl |= (np->sv_dcntl & IRQM);
914 * Configure targets according to driver setup.
915 * If NVRAM present get targets setup from NVRAM.
917 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
918 struct sym_tcb *tp = &np->target[i];
920 tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
921 tp->usrtags = SYM_SETUP_MAX_TAG;
922 tp->usr_width = np->maxwide;
925 sym_nvram_setup_target(tp, i, nvram);
928 tp->usrflags &= ~SYM_TAGS_ENABLED;
932 * Let user know about the settings.
934 printf("%s: %s, ID %d, Fast-%d, %s, %s\n", sym_name(np),
935 sym_nvram_type(nvram), np->myaddr,
936 (np->features & FE_ULTRA3) ? 80 :
937 (np->features & FE_ULTRA2) ? 40 :
938 (np->features & FE_ULTRA) ? 20 : 10,
939 sym_scsi_bus_mode(np->scsi_mode),
940 (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
942 * Tell him more on demand.
945 printf("%s: %s IRQ line driver%s\n",
947 np->rv_dcntl & IRQM ? "totem pole" : "open drain",
948 np->ram_ba ? ", using on-chip SRAM" : "");
949 printf("%s: using %s firmware.\n", sym_name(np), np->fw_name);
950 if (np->features & FE_NOPM)
951 printf("%s: handling phase mismatch from SCRIPTS.\n",
957 if (sym_verbose >= 2) {
958 printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
959 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
960 sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
961 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
963 printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
964 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
965 sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
966 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
973 * Test the pci bus snoop logic :-(
975 * Has to be called with interrupts disabled.
977 #ifndef CONFIG_SCSI_SYM53C8XX_IOMAPPED
978 static int sym_regtest (struct sym_hcb *np)
980 register volatile u32 data;
982 * chip registers may NOT be cached.
983 * write 0xffffffff to a read only register area,
984 * and try to read it back.
987 OUTL(np, nc_dstat, data);
988 data = INL(np, nc_dstat);
990 if (data == 0xffffffff) {
992 if ((data & 0xe2f0fffd) != 0x02000080) {
994 printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
1002 static int sym_snooptest (struct sym_hcb *np)
1004 u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
1006 #ifndef CONFIG_SCSI_SYM53C8XX_IOMAPPED
1007 err |= sym_regtest (np);
1008 if (err) return (err);
1012 * Enable Master Parity Checking as we intend
1013 * to enable it for normal operations.
1015 OUTB(np, nc_ctest4, (np->rv_ctest4 & MPEE));
1019 pc = SCRIPTZ_BA(np, snooptest);
1023 * Set memory and register.
1025 np->scratch = cpu_to_scr(host_wr);
1026 OUTL(np, nc_temp, sym_wr);
1028 * Start script (exchange values)
1030 OUTL(np, nc_dsa, np->hcb_ba);
1033 * Wait 'til done (with timeout)
1035 for (i=0; i<SYM_SNOOP_TIMEOUT; i++)
1036 if (INB(np, nc_istat) & (INTF|SIP|DIP))
1038 if (i>=SYM_SNOOP_TIMEOUT) {
1039 printf ("CACHE TEST FAILED: timeout.\n");
1043 * Check for fatal DMA errors.
1045 dstat = INB(np, nc_dstat);
1046 #if 1 /* Band aiding for broken hardwares that fail PCI parity */
1047 if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
1048 printf ("%s: PCI DATA PARITY ERROR DETECTED - "
1049 "DISABLING MASTER DATA PARITY CHECKING.\n",
1051 np->rv_ctest4 &= ~MPEE;
1055 if (dstat & (MDPE|BF|IID)) {
1056 printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
1060 * Save termination position.
1062 pc = INL(np, nc_dsp);
1064 * Read memory and register.
1066 host_rd = scr_to_cpu(np->scratch);
1067 sym_rd = INL(np, nc_scratcha);
1068 sym_bk = INL(np, nc_temp);
1070 * Check termination position.
1072 if (pc != SCRIPTZ_BA(np, snoopend)+8) {
1073 printf ("CACHE TEST FAILED: script execution failed.\n");
1074 printf ("start=%08lx, pc=%08lx, end=%08lx\n",
1075 (u_long) SCRIPTZ_BA(np, snooptest), (u_long) pc,
1076 (u_long) SCRIPTZ_BA(np, snoopend) +8);
1082 if (host_wr != sym_rd) {
1083 printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n",
1084 (int) host_wr, (int) sym_rd);
1087 if (host_rd != sym_wr) {
1088 printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n",
1089 (int) sym_wr, (int) host_rd);
1092 if (sym_bk != sym_wr) {
1093 printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n",
1094 (int) sym_wr, (int) sym_bk);
1102 * log message for real hard errors
1104 * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sx/s3/s4) @ name (dsp:dbc).
1105 * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf.
1107 * exception register:
1112 * so: control lines as driven by chip.
1113 * si: control lines as seen by chip.
1114 * sd: scsi data lines as seen by chip.
1117 * sx: sxfer (see the manual)
1118 * s3: scntl3 (see the manual)
1119 * s4: scntl4 (see the manual)
1121 * current script command:
1122 * dsp: script address (relative to start of script).
1123 * dbc: first word of script command.
1125 * First 24 register of the chip:
1128 static void sym_log_hard_error(struct sym_hcb *np, u_short sist, u_char dstat)
1134 u_char *script_base;
1137 dsp = INL(np, nc_dsp);
1139 if (dsp > np->scripta_ba &&
1140 dsp <= np->scripta_ba + np->scripta_sz) {
1141 script_ofs = dsp - np->scripta_ba;
1142 script_size = np->scripta_sz;
1143 script_base = (u_char *) np->scripta0;
1144 script_name = "scripta";
1146 else if (np->scriptb_ba < dsp &&
1147 dsp <= np->scriptb_ba + np->scriptb_sz) {
1148 script_ofs = dsp - np->scriptb_ba;
1149 script_size = np->scriptb_sz;
1150 script_base = (u_char *) np->scriptb0;
1151 script_name = "scriptb";
1156 script_name = "mem";
1159 printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x/%x) @ (%s %x:%08x).\n",
1160 sym_name(np), (unsigned)INB(np, nc_sdid)&0x0f, dstat, sist,
1161 (unsigned)INB(np, nc_socl), (unsigned)INB(np, nc_sbcl),
1162 (unsigned)INB(np, nc_sbdl), (unsigned)INB(np, nc_sxfer),
1163 (unsigned)INB(np, nc_scntl3),
1164 (np->features & FE_C10) ? (unsigned)INB(np, nc_scntl4) : 0,
1165 script_name, script_ofs, (unsigned)INL(np, nc_dbc));
1167 if (((script_ofs & 3) == 0) &&
1168 (unsigned)script_ofs < script_size) {
1169 printf ("%s: script cmd = %08x\n", sym_name(np),
1170 scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
1173 printf ("%s: regdump:", sym_name(np));
1175 printf (" %02x", (unsigned)INB_OFF(np, i));
1181 if (dstat & (MDPE|BF))
1182 sym_log_bus_error(np);
1185 static struct sym_chip sym_dev_table[] = {
1186 {PCI_DEVICE_ID_NCR_53C810, 0x0f, "810", 4, 8, 4, 64,
1189 #ifdef SYM_DEBUG_GENERIC_SUPPORT
1190 {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
1194 {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
1195 FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
1198 {PCI_DEVICE_ID_NCR_53C815, 0xff, "815", 4, 8, 4, 64,
1201 {PCI_DEVICE_ID_NCR_53C825, 0x0f, "825", 6, 8, 4, 64,
1202 FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
1204 {PCI_DEVICE_ID_NCR_53C825, 0xff, "825a", 6, 8, 4, 2,
1205 FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
1207 {PCI_DEVICE_ID_NCR_53C860, 0xff, "860", 4, 8, 5, 1,
1208 FE_ULTRA|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
1210 {PCI_DEVICE_ID_NCR_53C875, 0x01, "875", 6, 16, 5, 2,
1211 FE_WIDE|FE_ULTRA|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1212 FE_RAM|FE_DIFF|FE_VARCLK}
1214 {PCI_DEVICE_ID_NCR_53C875, 0xff, "875", 6, 16, 5, 2,
1215 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1216 FE_RAM|FE_DIFF|FE_VARCLK}
1218 {PCI_DEVICE_ID_NCR_53C875J, 0xff, "875J", 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_53C885, 0xff, "885", 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 #ifdef SYM_DEBUG_GENERIC_SUPPORT
1227 {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
1228 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
1232 {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
1233 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1237 {PCI_DEVICE_ID_NCR_53C896, 0xff, "896", 6, 31, 7, 4,
1238 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1239 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1241 {PCI_DEVICE_ID_LSI_53C895A, 0xff, "895a", 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_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1245 {PCI_DEVICE_ID_LSI_53C875A, 0xff, "875a", 6, 31, 7, 4,
1246 FE_WIDE|FE_ULTRA|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1247 FE_RAM|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1249 {PCI_DEVICE_ID_LSI_53C1010_33, 0x00, "1010-33", 6, 31, 7, 8,
1250 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1251 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
1254 {PCI_DEVICE_ID_LSI_53C1010_33, 0xff, "1010-33", 6, 31, 7, 8,
1255 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1256 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
1259 {PCI_DEVICE_ID_LSI_53C1010_66, 0xff, "1010-66", 6, 31, 7, 8,
1260 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1261 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
1264 {PCI_DEVICE_ID_LSI_53C1510, 0xff, "1510d", 6, 31, 7, 4,
1265 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1266 FE_RAM|FE_IO256|FE_LEDC}
1269 #define sym_num_devs \
1270 (sizeof(sym_dev_table) / sizeof(sym_dev_table[0]))
1273 * Look up the chip table.
1275 * Return a pointer to the chip entry if found,
1279 sym_lookup_chip_table (u_short device_id, u_char revision)
1281 struct sym_chip *chip;
1284 for (i = 0; i < sym_num_devs; i++) {
1285 chip = &sym_dev_table[i];
1286 if (device_id != chip->device_id)
1288 if (revision > chip->revision_id)
1296 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1298 * Lookup the 64 bit DMA segments map.
1299 * This is only used if the direct mapping
1300 * has been unsuccessful.
1302 int sym_lookup_dmap(struct sym_hcb *np, u32 h, int s)
1309 /* Look up existing mappings */
1310 for (i = SYM_DMAP_SIZE-1; i > 0; i--) {
1311 if (h == np->dmap_bah[i])
1314 /* If direct mapping is free, get it */
1315 if (!np->dmap_bah[s])
1317 /* Collision -> lookup free mappings */
1318 for (s = SYM_DMAP_SIZE-1; s > 0; s--) {
1319 if (!np->dmap_bah[s])
1323 panic("sym: ran out of 64 bit DMA segment registers");
1326 np->dmap_bah[s] = h;
1332 * Update IO registers scratch C..R so they will be
1333 * in sync. with queued CCB expectations.
1335 static void sym_update_dmap_regs(struct sym_hcb *np)
1339 if (!np->dmap_dirty)
1341 o = offsetof(struct sym_reg, nc_scrx[0]);
1342 for (i = 0; i < SYM_DMAP_SIZE; i++) {
1343 OUTL_OFF(np, o, np->dmap_bah[i]);
1350 /* Enforce all the fiddly SPI rules and the chip limitations */
1351 static void sym_check_goals(struct sym_hcb *np, struct scsi_target *starget,
1352 struct sym_trans *goal)
1354 if (!spi_support_wide(starget))
1357 if (!spi_support_sync(starget)) {
1365 if (spi_support_dt(starget)) {
1366 if (spi_support_dt_only(starget))
1369 if (goal->offset == 0)
1375 /* Some targets fail to properly negotiate DT in SE mode */
1376 if ((np->scsi_mode != SMODE_LVD) || !(np->features & FE_U3EN))
1380 /* all DT transfers must be wide */
1382 if (goal->offset > np->maxoffs_dt)
1383 goal->offset = np->maxoffs_dt;
1384 if (goal->period < np->minsync_dt)
1385 goal->period = np->minsync_dt;
1386 if (goal->period > np->maxsync_dt)
1387 goal->period = np->maxsync_dt;
1389 goal->iu = goal->qas = 0;
1390 if (goal->offset > np->maxoffs)
1391 goal->offset = np->maxoffs;
1392 if (goal->period < np->minsync)
1393 goal->period = np->minsync;
1394 if (goal->period > np->maxsync)
1395 goal->period = np->maxsync;
1400 * Prepare the next negotiation message if needed.
1402 * Fill in the part of message buffer that contains the
1403 * negotiation and the nego_status field of the CCB.
1404 * Returns the size of the message in bytes.
1406 static int sym_prepare_nego(struct sym_hcb *np, struct sym_ccb *cp, u_char *msgptr)
1408 struct sym_tcb *tp = &np->target[cp->target];
1409 struct scsi_target *starget = tp->starget;
1410 struct sym_trans *goal = &tp->tgoal;
1414 sym_check_goals(np, starget, goal);
1417 * Many devices implement PPR in a buggy way, so only use it if we
1421 (goal->iu || goal->dt || goal->qas || (goal->period < 0xa))) {
1423 } else if (spi_width(starget) != goal->width) {
1425 } else if (spi_period(starget) != goal->period ||
1426 spi_offset(starget) != goal->offset) {
1429 goal->check_nego = 0;
1435 msgptr[msglen++] = M_EXTENDED;
1436 msgptr[msglen++] = 3;
1437 msgptr[msglen++] = M_X_SYNC_REQ;
1438 msgptr[msglen++] = goal->period;
1439 msgptr[msglen++] = goal->offset;
1442 msgptr[msglen++] = M_EXTENDED;
1443 msgptr[msglen++] = 2;
1444 msgptr[msglen++] = M_X_WIDE_REQ;
1445 msgptr[msglen++] = goal->width;
1448 msgptr[msglen++] = M_EXTENDED;
1449 msgptr[msglen++] = 6;
1450 msgptr[msglen++] = M_X_PPR_REQ;
1451 msgptr[msglen++] = goal->period;
1452 msgptr[msglen++] = 0;
1453 msgptr[msglen++] = goal->offset;
1454 msgptr[msglen++] = goal->width;
1455 msgptr[msglen++] = (goal->iu ? PPR_OPT_IU : 0) |
1456 (goal->dt ? PPR_OPT_DT : 0) |
1457 (goal->qas ? PPR_OPT_QAS : 0);
1461 cp->nego_status = nego;
1464 tp->nego_cp = cp; /* Keep track a nego will be performed */
1465 if (DEBUG_FLAGS & DEBUG_NEGO) {
1466 sym_print_nego_msg(np, cp->target,
1467 nego == NS_SYNC ? "sync msgout" :
1468 nego == NS_WIDE ? "wide msgout" :
1469 "ppr msgout", msgptr);
1477 * Insert a job into the start queue.
1479 static void sym_put_start_queue(struct sym_hcb *np, struct sym_ccb *cp)
1483 #ifdef SYM_CONF_IARB_SUPPORT
1485 * If the previously queued CCB is not yet done,
1486 * set the IARB hint. The SCRIPTS will go with IARB
1487 * for this job when starting the previous one.
1488 * We leave devices a chance to win arbitration by
1489 * not using more than 'iarb_max' consecutive
1490 * immediate arbitrations.
1492 if (np->last_cp && np->iarb_count < np->iarb_max) {
1493 np->last_cp->host_flags |= HF_HINT_IARB;
1501 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1503 * Make SCRIPTS aware of the 64 bit DMA
1504 * segment registers not being up-to-date.
1507 cp->host_xflags |= HX_DMAP_DIRTY;
1511 * Insert first the idle task and then our job.
1512 * The MBs should ensure proper ordering.
1514 qidx = np->squeueput + 2;
1515 if (qidx >= MAX_QUEUE*2) qidx = 0;
1517 np->squeue [qidx] = cpu_to_scr(np->idletask_ba);
1518 MEMORY_WRITE_BARRIER();
1519 np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba);
1521 np->squeueput = qidx;
1523 if (DEBUG_FLAGS & DEBUG_QUEUE)
1524 printf ("%s: queuepos=%d.\n", sym_name (np), np->squeueput);
1527 * Script processor may be waiting for reselect.
1530 MEMORY_WRITE_BARRIER();
1531 OUTB(np, nc_istat, SIGP|np->istat_sem);
1534 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1536 * Start next ready-to-start CCBs.
1538 void sym_start_next_ccbs(struct sym_hcb *np, struct sym_lcb *lp, int maxn)
1544 * Paranoia, as usual. :-)
1546 assert(!lp->started_tags || !lp->started_no_tag);
1549 * Try to start as many commands as asked by caller.
1550 * Prevent from having both tagged and untagged
1551 * commands queued to the device at the same time.
1554 qp = sym_remque_head(&lp->waiting_ccbq);
1557 cp = sym_que_entry(qp, struct sym_ccb, link2_ccbq);
1558 if (cp->tag != NO_TAG) {
1559 if (lp->started_no_tag ||
1560 lp->started_tags >= lp->started_max) {
1561 sym_insque_head(qp, &lp->waiting_ccbq);
1564 lp->itlq_tbl[cp->tag] = cpu_to_scr(cp->ccb_ba);
1566 cpu_to_scr(SCRIPTA_BA(np, resel_tag));
1569 if (lp->started_no_tag || lp->started_tags) {
1570 sym_insque_head(qp, &lp->waiting_ccbq);
1573 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
1575 cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
1576 ++lp->started_no_tag;
1579 sym_insque_tail(qp, &lp->started_ccbq);
1580 sym_put_start_queue(np, cp);
1583 #endif /* SYM_OPT_HANDLE_DEVICE_QUEUEING */
1586 * The chip may have completed jobs. Look at the DONE QUEUE.
1588 * On paper, memory read barriers may be needed here to
1589 * prevent out of order LOADs by the CPU from having
1590 * prefetched stale data prior to DMA having occurred.
1592 static int sym_wakeup_done (struct sym_hcb *np)
1601 /* MEMORY_READ_BARRIER(); */
1603 dsa = scr_to_cpu(np->dqueue[i]);
1607 if ((i = i+2) >= MAX_QUEUE*2)
1610 cp = sym_ccb_from_dsa(np, dsa);
1612 MEMORY_READ_BARRIER();
1613 sym_complete_ok (np, cp);
1617 printf ("%s: bad DSA (%x) in done queue.\n",
1618 sym_name(np), (u_int) dsa);
1626 * Complete all CCBs queued to the COMP queue.
1628 * These CCBs are assumed:
1629 * - Not to be referenced either by devices or
1630 * SCRIPTS-related queues and datas.
1631 * - To have to be completed with an error condition
1634 * The device queue freeze count is incremented
1635 * for each CCB that does not prevent this.
1636 * This function is called when all CCBs involved
1637 * in error handling/recovery have been reaped.
1639 static void sym_flush_comp_queue(struct sym_hcb *np, int cam_status)
1644 while ((qp = sym_remque_head(&np->comp_ccbq)) != 0) {
1645 struct scsi_cmnd *cmd;
1646 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
1647 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
1648 /* Leave quiet CCBs waiting for resources */
1649 if (cp->host_status == HS_WAIT)
1653 sym_set_cam_status(cmd, cam_status);
1654 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1655 if (sym_get_cam_status(cmd) == DID_SOFT_ERROR) {
1656 struct sym_tcb *tp = &np->target[cp->target];
1657 struct sym_lcb *lp = sym_lp(tp, cp->lun);
1659 sym_remque(&cp->link2_ccbq);
1660 sym_insque_tail(&cp->link2_ccbq,
1663 if (cp->tag != NO_TAG)
1666 --lp->started_no_tag;
1673 sym_free_ccb(np, cp);
1674 sym_xpt_done(np, cmd);
1679 * Complete all active CCBs with error.
1680 * Used on CHIP/SCSI RESET.
1682 static void sym_flush_busy_queue (struct sym_hcb *np, int cam_status)
1685 * Move all active CCBs to the COMP queue
1686 * and flush this queue.
1688 sym_que_splice(&np->busy_ccbq, &np->comp_ccbq);
1689 sym_que_init(&np->busy_ccbq);
1690 sym_flush_comp_queue(np, cam_status);
1697 * 0: initialisation.
1698 * 1: SCSI BUS RESET delivered or received.
1699 * 2: SCSI BUS MODE changed.
1701 void sym_start_up (struct sym_hcb *np, int reason)
1707 * Reset chip if asked, otherwise just clear fifos.
1712 OUTB(np, nc_stest3, TE|CSF);
1713 OUTONB(np, nc_ctest3, CLF);
1719 phys = np->squeue_ba;
1720 for (i = 0; i < MAX_QUEUE*2; i += 2) {
1721 np->squeue[i] = cpu_to_scr(np->idletask_ba);
1722 np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
1724 np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
1727 * Start at first entry.
1734 phys = np->dqueue_ba;
1735 for (i = 0; i < MAX_QUEUE*2; i += 2) {
1737 np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
1739 np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
1742 * Start at first entry.
1747 * Install patches in scripts.
1748 * This also let point to first position the start
1749 * and done queue pointers used from SCRIPTS.
1754 * Wakeup all pending jobs.
1756 sym_flush_busy_queue(np, DID_RESET);
1761 OUTB(np, nc_istat, 0x00); /* Remove Reset, abort */
1763 udelay(2000); /* The 895 needs time for the bus mode to settle */
1765 OUTB(np, nc_scntl0, np->rv_scntl0 | 0xc0);
1766 /* full arb., ena parity, par->ATN */
1767 OUTB(np, nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
1769 sym_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
1771 OUTB(np, nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
1772 OUTW(np, nc_respid, 1ul<<np->myaddr); /* Id to respond to */
1773 OUTB(np, nc_istat , SIGP ); /* Signal Process */
1774 OUTB(np, nc_dmode , np->rv_dmode); /* Burst length, dma mode */
1775 OUTB(np, nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
1777 OUTB(np, nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
1778 OUTB(np, nc_ctest3, np->rv_ctest3); /* Write and invalidate */
1779 OUTB(np, nc_ctest4, np->rv_ctest4); /* Master parity checking */
1781 /* Extended Sreq/Sack filtering not supported on the C10 */
1782 if (np->features & FE_C10)
1783 OUTB(np, nc_stest2, np->rv_stest2);
1785 OUTB(np, nc_stest2, EXT|np->rv_stest2);
1787 OUTB(np, nc_stest3, TE); /* TolerANT enable */
1788 OUTB(np, nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */
1791 * For now, disable AIP generation on C1010-66.
1793 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)
1794 OUTB(np, nc_aipcntl1, DISAIP);
1797 * C10101 rev. 0 errata.
1798 * Errant SGE's when in narrow. Write bits 4 & 5 of
1799 * STEST1 register to disable SGE. We probably should do
1800 * that from SCRIPTS for each selection/reselection, but
1801 * I just don't want. :)
1803 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
1804 np->revision_id < 1)
1805 OUTB(np, nc_stest1, INB(np, nc_stest1) | 0x30);
1808 * DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
1809 * Disable overlapped arbitration for some dual function devices,
1810 * regardless revision id (kind of post-chip-design feature. ;-))
1812 if (np->device_id == PCI_DEVICE_ID_NCR_53C875)
1813 OUTB(np, nc_ctest0, (1<<5));
1814 else if (np->device_id == PCI_DEVICE_ID_NCR_53C896)
1815 np->rv_ccntl0 |= DPR;
1818 * Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing
1819 * and/or hardware phase mismatch, since only such chips
1820 * seem to support those IO registers.
1822 if (np->features & (FE_DAC|FE_NOPM)) {
1823 OUTB(np, nc_ccntl0, np->rv_ccntl0);
1824 OUTB(np, nc_ccntl1, np->rv_ccntl1);
1827 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1829 * Set up scratch C and DRS IO registers to map the 32 bit
1830 * DMA address range our data structures are located in.
1833 np->dmap_bah[0] = 0; /* ??? */
1834 OUTL(np, nc_scrx[0], np->dmap_bah[0]);
1835 OUTL(np, nc_drs, np->dmap_bah[0]);
1840 * If phase mismatch handled by scripts (895A/896/1010),
1841 * set PM jump addresses.
1843 if (np->features & FE_NOPM) {
1844 OUTL(np, nc_pmjad1, SCRIPTB_BA(np, pm_handle));
1845 OUTL(np, nc_pmjad2, SCRIPTB_BA(np, pm_handle));
1849 * Enable GPIO0 pin for writing if LED support from SCRIPTS.
1850 * Also set GPIO5 and clear GPIO6 if hardware LED control.
1852 if (np->features & FE_LED0)
1853 OUTB(np, nc_gpcntl, INB(np, nc_gpcntl) & ~0x01);
1854 else if (np->features & FE_LEDC)
1855 OUTB(np, nc_gpcntl, (INB(np, nc_gpcntl) & ~0x41) | 0x20);
1860 OUTW(np, nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
1861 OUTB(np, nc_dien , MDPE|BF|SSI|SIR|IID);
1864 * For 895/6 enable SBMC interrupt and save current SCSI bus mode.
1865 * Try to eat the spurious SBMC interrupt that may occur when
1866 * we reset the chip but not the SCSI BUS (at initialization).
1868 if (np->features & (FE_ULTRA2|FE_ULTRA3)) {
1869 OUTONW(np, nc_sien, SBMC);
1875 np->scsi_mode = INB(np, nc_stest4) & SMODE;
1879 * Fill in target structure.
1880 * Reinitialize usrsync.
1881 * Reinitialize usrwide.
1882 * Prepare sync negotiation according to actual SCSI bus mode.
1884 for (i=0;i<SYM_CONF_MAX_TARGET;i++) {
1885 struct sym_tcb *tp = &np->target[i];
1889 tp->head.wval = np->rv_scntl3;
1894 * Download SCSI SCRIPTS to on-chip RAM if present,
1895 * and start script processor.
1896 * We do the download preferently from the CPU.
1897 * For platforms that may not support PCI memory mapping,
1898 * we use simple SCRIPTS that performs MEMORY MOVEs.
1900 phys = SCRIPTA_BA(np, init);
1902 if (sym_verbose >= 2)
1903 printf("%s: Downloading SCSI SCRIPTS.\n", sym_name(np));
1904 memcpy_toio(np->s.ramaddr, np->scripta0, np->scripta_sz);
1905 if (np->ram_ws == 8192) {
1906 memcpy_toio(np->s.ramaddr + 4096, np->scriptb0, np->scriptb_sz);
1907 phys = scr_to_cpu(np->scr_ram_seg);
1908 OUTL(np, nc_mmws, phys);
1909 OUTL(np, nc_mmrs, phys);
1910 OUTL(np, nc_sfs, phys);
1911 phys = SCRIPTB_BA(np, start64);
1917 OUTL(np, nc_dsa, np->hcb_ba);
1921 * Notify the XPT about the RESET condition.
1924 sym_xpt_async_bus_reset(np);
1928 * Switch trans mode for current job and its target.
1930 static void sym_settrans(struct sym_hcb *np, int target, u_char opts, u_char ofs,
1931 u_char per, u_char wide, u_char div, u_char fak)
1934 u_char sval, wval, uval;
1935 struct sym_tcb *tp = &np->target[target];
1937 assert(target == (INB(np, nc_sdid) & 0x0f));
1939 sval = tp->head.sval;
1940 wval = tp->head.wval;
1941 uval = tp->head.uval;
1944 printf("XXXX sval=%x wval=%x uval=%x (%x)\n",
1945 sval, wval, uval, np->rv_scntl3);
1950 if (!(np->features & FE_C10))
1951 sval = (sval & ~0x1f) | ofs;
1953 sval = (sval & ~0x3f) | ofs;
1956 * Set the sync divisor and extra clock factor.
1959 wval = (wval & ~0x70) | ((div+1) << 4);
1960 if (!(np->features & FE_C10))
1961 sval = (sval & ~0xe0) | (fak << 5);
1963 uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT);
1964 if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT);
1965 if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT);
1970 * Set the bus width.
1977 * Set misc. ultra enable bits.
1979 if (np->features & FE_C10) {
1980 uval = uval & ~(U3EN|AIPCKEN);
1982 assert(np->features & FE_U3EN);
1986 wval = wval & ~ULTRA;
1987 if (per <= 12) wval |= ULTRA;
1991 * Stop there if sync parameters are unchanged.
1993 if (tp->head.sval == sval &&
1994 tp->head.wval == wval &&
1995 tp->head.uval == uval)
1997 tp->head.sval = sval;
1998 tp->head.wval = wval;
1999 tp->head.uval = uval;
2002 * Disable extended Sreq/Sack filtering if per < 50.
2003 * Not supported on the C1010.
2005 if (per < 50 && !(np->features & FE_C10))
2006 OUTOFFB(np, nc_stest2, EXT);
2009 * set actual value and sync_status
2011 OUTB(np, nc_sxfer, tp->head.sval);
2012 OUTB(np, nc_scntl3, tp->head.wval);
2014 if (np->features & FE_C10) {
2015 OUTB(np, nc_scntl4, tp->head.uval);
2019 * patch ALL busy ccbs of this target.
2021 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
2023 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
2024 if (cp->target != target)
2026 cp->phys.select.sel_scntl3 = tp->head.wval;
2027 cp->phys.select.sel_sxfer = tp->head.sval;
2028 if (np->features & FE_C10) {
2029 cp->phys.select.sel_scntl4 = tp->head.uval;
2035 * We received a WDTR.
2036 * Let everything be aware of the changes.
2038 static void sym_setwide(struct sym_hcb *np, int target, u_char wide)
2040 struct sym_tcb *tp = &np->target[target];
2041 struct scsi_target *starget = tp->starget;
2043 if (spi_width(starget) == wide)
2046 sym_settrans(np, target, 0, 0, 0, wide, 0, 0);
2048 tp->tgoal.width = wide;
2049 spi_offset(starget) = 0;
2050 spi_period(starget) = 0;
2051 spi_width(starget) = wide;
2052 spi_iu(starget) = 0;
2053 spi_dt(starget) = 0;
2054 spi_qas(starget) = 0;
2056 if (sym_verbose >= 3)
2057 spi_display_xfer_agreement(starget);
2061 * We received a SDTR.
2062 * Let everything be aware of the changes.
2065 sym_setsync(struct sym_hcb *np, int target,
2066 u_char ofs, u_char per, u_char div, u_char fak)
2068 struct sym_tcb *tp = &np->target[target];
2069 struct scsi_target *starget = tp->starget;
2070 u_char wide = (tp->head.wval & EWS) ? BUS_16_BIT : BUS_8_BIT;
2072 sym_settrans(np, target, 0, ofs, per, wide, div, fak);
2074 spi_period(starget) = per;
2075 spi_offset(starget) = ofs;
2076 spi_iu(starget) = spi_dt(starget) = spi_qas(starget) = 0;
2078 if (!tp->tgoal.dt && !tp->tgoal.iu && !tp->tgoal.qas) {
2079 tp->tgoal.period = per;
2080 tp->tgoal.offset = ofs;
2081 tp->tgoal.check_nego = 0;
2084 spi_display_xfer_agreement(starget);
2088 * We received a PPR.
2089 * Let everything be aware of the changes.
2092 sym_setpprot(struct sym_hcb *np, int target, u_char opts, u_char ofs,
2093 u_char per, u_char wide, u_char div, u_char fak)
2095 struct sym_tcb *tp = &np->target[target];
2096 struct scsi_target *starget = tp->starget;
2098 sym_settrans(np, target, opts, ofs, per, wide, div, fak);
2100 spi_width(starget) = tp->tgoal.width = wide;
2101 spi_period(starget) = tp->tgoal.period = per;
2102 spi_offset(starget) = tp->tgoal.offset = ofs;
2103 spi_iu(starget) = tp->tgoal.iu = !!(opts & PPR_OPT_IU);
2104 spi_dt(starget) = tp->tgoal.dt = !!(opts & PPR_OPT_DT);
2105 spi_qas(starget) = tp->tgoal.qas = !!(opts & PPR_OPT_QAS);
2106 tp->tgoal.check_nego = 0;
2108 spi_display_xfer_agreement(starget);
2112 * generic recovery from scsi interrupt
2114 * The doc says that when the chip gets an SCSI interrupt,
2115 * it tries to stop in an orderly fashion, by completing
2116 * an instruction fetch that had started or by flushing
2117 * the DMA fifo for a write to memory that was executing.
2118 * Such a fashion is not enough to know if the instruction
2119 * that was just before the current DSP value has been
2122 * There are some small SCRIPTS sections that deal with
2123 * the start queue and the done queue that may break any
2124 * assomption from the C code if we are interrupted
2125 * inside, so we reset if this happens. Btw, since these
2126 * SCRIPTS sections are executed while the SCRIPTS hasn't
2127 * started SCSI operations, it is very unlikely to happen.
2129 * All the driver data structures are supposed to be
2130 * allocated from the same 4 GB memory window, so there
2131 * is a 1 to 1 relationship between DSA and driver data
2132 * structures. Since we are careful :) to invalidate the
2133 * DSA when we complete a command or when the SCRIPTS
2134 * pushes a DSA into a queue, we can trust it when it
2137 static void sym_recover_scsi_int (struct sym_hcb *np, u_char hsts)
2139 u32 dsp = INL(np, nc_dsp);
2140 u32 dsa = INL(np, nc_dsa);
2141 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
2144 * If we haven't been interrupted inside the SCRIPTS
2145 * critical pathes, we can safely restart the SCRIPTS
2146 * and trust the DSA value if it matches a CCB.
2148 if ((!(dsp > SCRIPTA_BA(np, getjob_begin) &&
2149 dsp < SCRIPTA_BA(np, getjob_end) + 1)) &&
2150 (!(dsp > SCRIPTA_BA(np, ungetjob) &&
2151 dsp < SCRIPTA_BA(np, reselect) + 1)) &&
2152 (!(dsp > SCRIPTB_BA(np, sel_for_abort) &&
2153 dsp < SCRIPTB_BA(np, sel_for_abort_1) + 1)) &&
2154 (!(dsp > SCRIPTA_BA(np, done) &&
2155 dsp < SCRIPTA_BA(np, done_end) + 1))) {
2156 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
2157 OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
2159 * If we have a CCB, let the SCRIPTS call us back for
2160 * the handling of the error with SCRATCHA filled with
2161 * STARTPOS. This way, we will be able to freeze the
2162 * device queue and requeue awaiting IOs.
2165 cp->host_status = hsts;
2166 OUTL_DSP(np, SCRIPTA_BA(np, complete_error));
2169 * Otherwise just restart the SCRIPTS.
2172 OUTL(np, nc_dsa, 0xffffff);
2173 OUTL_DSP(np, SCRIPTA_BA(np, start));
2182 sym_start_reset(np);
2186 * chip exception handler for selection timeout
2188 static void sym_int_sto (struct sym_hcb *np)
2190 u32 dsp = INL(np, nc_dsp);
2192 if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
2194 if (dsp == SCRIPTA_BA(np, wf_sel_done) + 8)
2195 sym_recover_scsi_int(np, HS_SEL_TIMEOUT);
2197 sym_start_reset(np);
2201 * chip exception handler for unexpected disconnect
2203 static void sym_int_udc (struct sym_hcb *np)
2205 printf ("%s: unexpected disconnect\n", sym_name(np));
2206 sym_recover_scsi_int(np, HS_UNEXPECTED);
2210 * chip exception handler for SCSI bus mode change
2212 * spi2-r12 11.2.3 says a transceiver mode change must
2213 * generate a reset event and a device that detects a reset
2214 * event shall initiate a hard reset. It says also that a
2215 * device that detects a mode change shall set data transfer
2216 * mode to eight bit asynchronous, etc...
2217 * So, just reinitializing all except chip should be enough.
2219 static void sym_int_sbmc (struct sym_hcb *np)
2221 u_char scsi_mode = INB(np, nc_stest4) & SMODE;
2226 printf("%s: SCSI BUS mode change from %s to %s.\n", sym_name(np),
2227 sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode));
2230 * Should suspend command processing for a few seconds and
2231 * reinitialize all except the chip.
2233 sym_start_up (np, 2);
2237 * chip exception handler for SCSI parity error.
2239 * When the chip detects a SCSI parity error and is
2240 * currently executing a (CH)MOV instruction, it does
2241 * not interrupt immediately, but tries to finish the
2242 * transfer of the current scatter entry before
2243 * interrupting. The following situations may occur:
2245 * - The complete scatter entry has been transferred
2246 * without the device having changed phase.
2247 * The chip will then interrupt with the DSP pointing
2248 * to the instruction that follows the MOV.
2250 * - A phase mismatch occurs before the MOV finished
2251 * and phase errors are to be handled by the C code.
2252 * The chip will then interrupt with both PAR and MA
2255 * - A phase mismatch occurs before the MOV finished and
2256 * phase errors are to be handled by SCRIPTS.
2257 * The chip will load the DSP with the phase mismatch
2258 * JUMP address and interrupt the host processor.
2260 static void sym_int_par (struct sym_hcb *np, u_short sist)
2262 u_char hsts = INB(np, HS_PRT);
2263 u32 dsp = INL(np, nc_dsp);
2264 u32 dbc = INL(np, nc_dbc);
2265 u32 dsa = INL(np, nc_dsa);
2266 u_char sbcl = INB(np, nc_sbcl);
2267 u_char cmd = dbc >> 24;
2268 int phase = cmd & 7;
2269 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
2271 printf("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n",
2272 sym_name(np), hsts, dbc, sbcl);
2275 * Check that the chip is connected to the SCSI BUS.
2277 if (!(INB(np, nc_scntl1) & ISCON)) {
2278 sym_recover_scsi_int(np, HS_UNEXPECTED);
2283 * If the nexus is not clearly identified, reset the bus.
2284 * We will try to do better later.
2290 * Check instruction was a MOV, direction was INPUT and
2293 if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
2297 * Keep track of the parity error.
2299 OUTONB(np, HF_PRT, HF_EXT_ERR);
2300 cp->xerr_status |= XE_PARITY_ERR;
2303 * Prepare the message to send to the device.
2305 np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
2308 * If the old phase was DATA IN phase, we have to deal with
2309 * the 3 situations described above.
2310 * For other input phases (MSG IN and STATUS), the device
2311 * must resend the whole thing that failed parity checking
2312 * or signal error. So, jumping to dispatcher should be OK.
2314 if (phase == 1 || phase == 5) {
2315 /* Phase mismatch handled by SCRIPTS */
2316 if (dsp == SCRIPTB_BA(np, pm_handle))
2318 /* Phase mismatch handled by the C code */
2321 /* No phase mismatch occurred */
2323 sym_set_script_dp (np, cp, dsp);
2324 OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
2327 else if (phase == 7) /* We definitely cannot handle parity errors */
2328 #if 1 /* in message-in phase due to the relection */
2329 goto reset_all; /* path and various message anticipations. */
2331 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
2334 OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
2338 sym_start_reset(np);
2343 * chip exception handler for phase errors.
2345 * We have to construct a new transfer descriptor,
2346 * to transfer the rest of the current block.
2348 static void sym_int_ma (struct sym_hcb *np)
2361 u_char hflags, hflags0;
2365 dsp = INL(np, nc_dsp);
2366 dbc = INL(np, nc_dbc);
2367 dsa = INL(np, nc_dsa);
2370 rest = dbc & 0xffffff;
2374 * locate matching cp if any.
2376 cp = sym_ccb_from_dsa(np, dsa);
2379 * Donnot take into account dma fifo and various buffers in
2380 * INPUT phase since the chip flushes everything before
2381 * raising the MA interrupt for interrupted INPUT phases.
2382 * For DATA IN phase, we will check for the SWIDE later.
2384 if ((cmd & 7) != 1 && (cmd & 7) != 5) {
2387 if (np->features & FE_DFBC)
2388 delta = INW(np, nc_dfbc);
2393 * Read DFIFO, CTEST[4-6] using 1 PCI bus ownership.
2395 dfifo = INL(np, nc_dfifo);
2398 * Calculate remaining bytes in DMA fifo.
2399 * (CTEST5 = dfifo >> 16)
2401 if (dfifo & (DFS << 16))
2402 delta = ((((dfifo >> 8) & 0x300) |
2403 (dfifo & 0xff)) - rest) & 0x3ff;
2405 delta = ((dfifo & 0xff) - rest) & 0x7f;
2409 * The data in the dma fifo has not been transfered to
2410 * the target -> add the amount to the rest
2411 * and clear the data.
2412 * Check the sstat2 register in case of wide transfer.
2415 ss0 = INB(np, nc_sstat0);
2416 if (ss0 & OLF) rest++;
2417 if (!(np->features & FE_C10))
2418 if (ss0 & ORF) rest++;
2419 if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
2420 ss2 = INB(np, nc_sstat2);
2421 if (ss2 & OLF1) rest++;
2422 if (!(np->features & FE_C10))
2423 if (ss2 & ORF1) rest++;
2429 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */
2430 OUTB(np, nc_stest3, TE|CSF); /* scsi fifo */
2434 * log the information
2436 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
2437 printf ("P%x%x RL=%d D=%d ", cmd&7, INB(np, nc_sbcl)&7,
2438 (unsigned) rest, (unsigned) delta);
2441 * try to find the interrupted script command,
2442 * and the address at which to continue.
2446 if (dsp > np->scripta_ba &&
2447 dsp <= np->scripta_ba + np->scripta_sz) {
2448 vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8));
2451 else if (dsp > np->scriptb_ba &&
2452 dsp <= np->scriptb_ba + np->scriptb_sz) {
2453 vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8));
2458 * log the information
2460 if (DEBUG_FLAGS & DEBUG_PHASE) {
2461 printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
2462 cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
2466 printf ("%s: interrupted SCRIPT address not found.\n",
2472 printf ("%s: SCSI phase error fixup: CCB already dequeued.\n",
2478 * get old startaddress and old length.
2480 oadr = scr_to_cpu(vdsp[1]);
2482 if (cmd & 0x10) { /* Table indirect */
2483 tblp = (u32 *) ((char*) &cp->phys + oadr);
2484 olen = scr_to_cpu(tblp[0]);
2485 oadr = scr_to_cpu(tblp[1]);
2488 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
2491 if (DEBUG_FLAGS & DEBUG_PHASE) {
2492 printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
2493 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
2500 * check cmd against assumed interrupted script command.
2501 * If dt data phase, the MOVE instruction hasn't bit 4 of
2504 if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
2505 sym_print_addr(cp->cmd,
2506 "internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
2507 cmd, scr_to_cpu(vdsp[0]) >> 24);
2513 * if old phase not dataphase, leave here.
2516 sym_print_addr(cp->cmd,
2517 "phase change %x-%x %d@%08x resid=%d.\n",
2518 cmd&7, INB(np, nc_sbcl)&7, (unsigned)olen,
2519 (unsigned)oadr, (unsigned)rest);
2520 goto unexpected_phase;
2524 * Choose the correct PM save area.
2526 * Look at the PM_SAVE SCRIPT if you want to understand
2527 * this stuff. The equivalent code is implemented in
2528 * SCRIPTS for the 895A, 896 and 1010 that are able to
2529 * handle PM from the SCRIPTS processor.
2531 hflags0 = INB(np, HF_PRT);
2534 if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
2535 if (hflags & HF_IN_PM0)
2536 nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
2537 else if (hflags & HF_IN_PM1)
2538 nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
2540 if (hflags & HF_DP_SAVED)
2541 hflags ^= HF_ACT_PM;
2544 if (!(hflags & HF_ACT_PM)) {
2546 newcmd = SCRIPTA_BA(np, pm0_data);
2550 newcmd = SCRIPTA_BA(np, pm1_data);
2553 hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
2554 if (hflags != hflags0)
2555 OUTB(np, HF_PRT, hflags);
2558 * fillin the phase mismatch context
2560 pm->sg.addr = cpu_to_scr(oadr + olen - rest);
2561 pm->sg.size = cpu_to_scr(rest);
2562 pm->ret = cpu_to_scr(nxtdsp);
2565 * If we have a SWIDE,
2566 * - prepare the address to write the SWIDE from SCRIPTS,
2567 * - compute the SCRIPTS address to restart from,
2568 * - move current data pointer context by one byte.
2570 nxtdsp = SCRIPTA_BA(np, dispatch);
2571 if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
2572 (INB(np, nc_scntl2) & WSR)) {
2576 * Set up the table indirect for the MOVE
2577 * of the residual byte and adjust the data
2580 tmp = scr_to_cpu(pm->sg.addr);
2581 cp->phys.wresid.addr = cpu_to_scr(tmp);
2582 pm->sg.addr = cpu_to_scr(tmp + 1);
2583 tmp = scr_to_cpu(pm->sg.size);
2584 cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
2585 pm->sg.size = cpu_to_scr(tmp - 1);
2588 * If only the residual byte is to be moved,
2589 * no PM context is needed.
2591 if ((tmp&0xffffff) == 1)
2595 * Prepare the address of SCRIPTS that will
2596 * move the residual byte to memory.
2598 nxtdsp = SCRIPTB_BA(np, wsr_ma_helper);
2601 if (DEBUG_FLAGS & DEBUG_PHASE) {
2602 sym_print_addr(cp->cmd, "PM %x %x %x / %x %x %x.\n",
2603 hflags0, hflags, newcmd,
2604 (unsigned)scr_to_cpu(pm->sg.addr),
2605 (unsigned)scr_to_cpu(pm->sg.size),
2606 (unsigned)scr_to_cpu(pm->ret));
2610 * Restart the SCRIPTS processor.
2612 sym_set_script_dp (np, cp, newcmd);
2613 OUTL_DSP(np, nxtdsp);
2617 * Unexpected phase changes that occurs when the current phase
2618 * is not a DATA IN or DATA OUT phase are due to error conditions.
2619 * Such event may only happen when the SCRIPTS is using a
2620 * multibyte SCSI MOVE.
2622 * Phase change Some possible cause
2624 * COMMAND --> MSG IN SCSI parity error detected by target.
2625 * COMMAND --> STATUS Bad command or refused by target.
2626 * MSG OUT --> MSG IN Message rejected by target.
2627 * MSG OUT --> COMMAND Bogus target that discards extended
2628 * negotiation messages.
2630 * The code below does not care of the new phase and so
2631 * trusts the target. Why to annoy it ?
2632 * If the interrupted phase is COMMAND phase, we restart at
2634 * If a target does not get all the messages after selection,
2635 * the code assumes blindly that the target discards extended
2636 * messages and clears the negotiation status.
2637 * If the target does not want all our response to negotiation,
2638 * we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
2639 * bloat for such a should_not_happen situation).
2640 * In all other situation, we reset the BUS.
2641 * Are these assumptions reasonnable ? (Wait and see ...)
2648 case 2: /* COMMAND phase */
2649 nxtdsp = SCRIPTA_BA(np, dispatch);
2652 case 3: /* STATUS phase */
2653 nxtdsp = SCRIPTA_BA(np, dispatch);
2656 case 6: /* MSG OUT phase */
2658 * If the device may want to use untagged when we want
2659 * tagged, we prepare an IDENTIFY without disc. granted,
2660 * since we will not be able to handle reselect.
2661 * Otherwise, we just don't care.
2663 if (dsp == SCRIPTA_BA(np, send_ident)) {
2664 if (cp->tag != NO_TAG && olen - rest <= 3) {
2665 cp->host_status = HS_BUSY;
2666 np->msgout[0] = IDENTIFY(0, cp->lun);
2667 nxtdsp = SCRIPTB_BA(np, ident_break_atn);
2670 nxtdsp = SCRIPTB_BA(np, ident_break);
2672 else if (dsp == SCRIPTB_BA(np, send_wdtr) ||
2673 dsp == SCRIPTB_BA(np, send_sdtr) ||
2674 dsp == SCRIPTB_BA(np, send_ppr)) {
2675 nxtdsp = SCRIPTB_BA(np, nego_bad_phase);
2676 if (dsp == SCRIPTB_BA(np, send_ppr)) {
2677 struct scsi_device *dev = cp->cmd->device;
2683 case 7: /* MSG IN phase */
2684 nxtdsp = SCRIPTA_BA(np, clrack);
2690 OUTL_DSP(np, nxtdsp);
2695 sym_start_reset(np);
2699 * chip interrupt handler
2701 * In normal situations, interrupt conditions occur one at
2702 * a time. But when something bad happens on the SCSI BUS,
2703 * the chip may raise several interrupt flags before
2704 * stopping and interrupting the CPU. The additionnal
2705 * interrupt flags are stacked in some extra registers
2706 * after the SIP and/or DIP flag has been raised in the
2707 * ISTAT. After the CPU has read the interrupt condition
2708 * flag from SIST or DSTAT, the chip unstacks the other
2709 * interrupt flags and sets the corresponding bits in
2710 * SIST or DSTAT. Since the chip starts stacking once the
2711 * SIP or DIP flag is set, there is a small window of time
2712 * where the stacking does not occur.
2714 * Typically, multiple interrupt conditions may happen in
2715 * the following situations:
2717 * - SCSI parity error + Phase mismatch (PAR|MA)
2718 * When an parity error is detected in input phase
2719 * and the device switches to msg-in phase inside a
2721 * - SCSI parity error + Unexpected disconnect (PAR|UDC)
2722 * When a stupid device does not want to handle the
2723 * recovery of an SCSI parity error.
2724 * - Some combinations of STO, PAR, UDC, ...
2725 * When using non compliant SCSI stuff, when user is
2726 * doing non compliant hot tampering on the BUS, when
2727 * something really bad happens to a device, etc ...
2729 * The heuristic suggested by SYMBIOS to handle
2730 * multiple interrupts is to try unstacking all
2731 * interrupts conditions and to handle them on some
2732 * priority based on error severity.
2733 * This will work when the unstacking has been
2734 * successful, but we cannot be 100 % sure of that,
2735 * since the CPU may have been faster to unstack than
2736 * the chip is able to stack. Hmmm ... But it seems that
2737 * such a situation is very unlikely to happen.
2739 * If this happen, for example STO caught by the CPU
2740 * then UDC happenning before the CPU have restarted
2741 * the SCRIPTS, the driver may wrongly complete the
2742 * same command on UDC, since the SCRIPTS didn't restart
2743 * and the DSA still points to the same command.
2744 * We avoid this situation by setting the DSA to an
2745 * invalid value when the CCB is completed and before
2746 * restarting the SCRIPTS.
2748 * Another issue is that we need some section of our
2749 * recovery procedures to be somehow uninterruptible but
2750 * the SCRIPTS processor does not provides such a
2751 * feature. For this reason, we handle recovery preferently
2752 * from the C code and check against some SCRIPTS critical
2753 * sections from the C code.
2755 * Hopefully, the interrupt handling of the driver is now
2756 * able to resist to weird BUS error conditions, but donnot
2757 * ask me for any guarantee that it will never fail. :-)
2758 * Use at your own decision and risk.
2761 void sym_interrupt (struct sym_hcb *np)
2763 u_char istat, istatc;
2768 * interrupt on the fly ?
2769 * (SCRIPTS may still be running)
2771 * A `dummy read' is needed to ensure that the
2772 * clear of the INTF flag reaches the device
2773 * and that posted writes are flushed to memory
2774 * before the scanning of the DONE queue.
2775 * Note that SCRIPTS also (dummy) read to memory
2776 * prior to deliver the INTF interrupt condition.
2778 istat = INB(np, nc_istat);
2780 OUTB(np, nc_istat, (istat & SIGP) | INTF | np->istat_sem);
2781 istat = INB(np, nc_istat); /* DUMMY READ */
2782 if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
2783 sym_wakeup_done(np);
2786 if (!(istat & (SIP|DIP)))
2789 #if 0 /* We should never get this one */
2791 OUTB(np, nc_istat, CABRT);
2795 * PAR and MA interrupts may occur at the same time,
2796 * and we need to know of both in order to handle
2797 * this situation properly. We try to unstack SCSI
2798 * interrupts for that reason. BTW, I dislike a LOT
2799 * such a loop inside the interrupt routine.
2800 * Even if DMA interrupt stacking is very unlikely to
2801 * happen, we also try unstacking these ones, since
2802 * this has no performance impact.
2809 sist |= INW(np, nc_sist);
2811 dstat |= INB(np, nc_dstat);
2812 istatc = INB(np, nc_istat);
2814 } while (istatc & (SIP|DIP));
2816 if (DEBUG_FLAGS & DEBUG_TINY)
2817 printf ("<%d|%x:%x|%x:%x>",
2818 (int)INB(np, nc_scr0),
2820 (unsigned)INL(np, nc_dsp),
2821 (unsigned)INL(np, nc_dbc));
2823 * On paper, a memory read barrier may be needed here to
2824 * prevent out of order LOADs by the CPU from having
2825 * prefetched stale data prior to DMA having occurred.
2826 * And since we are paranoid ... :)
2828 MEMORY_READ_BARRIER();
2831 * First, interrupts we want to service cleanly.
2833 * Phase mismatch (MA) is the most frequent interrupt
2834 * for chip earlier than the 896 and so we have to service
2835 * it as quickly as possible.
2836 * A SCSI parity error (PAR) may be combined with a phase
2837 * mismatch condition (MA).
2838 * Programmed interrupts (SIR) are used to call the C code
2840 * The single step interrupt (SSI) is not used in this
2843 if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
2844 !(dstat & (MDPE|BF|ABRT|IID))) {
2845 if (sist & PAR) sym_int_par (np, sist);
2846 else if (sist & MA) sym_int_ma (np);
2847 else if (dstat & SIR) sym_int_sir (np);
2848 else if (dstat & SSI) OUTONB_STD();
2849 else goto unknown_int;
2854 * Now, interrupts that donnot happen in normal
2855 * situations and that we may need to recover from.
2857 * On SCSI RESET (RST), we reset everything.
2858 * On SCSI BUS MODE CHANGE (SBMC), we complete all
2859 * active CCBs with RESET status, prepare all devices
2860 * for negotiating again and restart the SCRIPTS.
2861 * On STO and UDC, we complete the CCB with the corres-
2862 * ponding status and restart the SCRIPTS.
2865 printf("%s: SCSI BUS reset detected.\n", sym_name(np));
2866 sym_start_up (np, 1);
2870 OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
2871 OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
2873 if (!(sist & (GEN|HTH|SGE)) &&
2874 !(dstat & (MDPE|BF|ABRT|IID))) {
2875 if (sist & SBMC) sym_int_sbmc (np);
2876 else if (sist & STO) sym_int_sto (np);
2877 else if (sist & UDC) sym_int_udc (np);
2878 else goto unknown_int;
2883 * Now, interrupts we are not able to recover cleanly.
2885 * Log message for hard errors.
2889 sym_log_hard_error(np, sist, dstat);
2891 if ((sist & (GEN|HTH|SGE)) ||
2892 (dstat & (MDPE|BF|ABRT|IID))) {
2893 sym_start_reset(np);
2899 * We just miss the cause of the interrupt. :(
2900 * Print a message. The timeout will do the real work.
2902 printf( "%s: unknown interrupt(s) ignored, "
2903 "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
2904 sym_name(np), istat, dstat, sist);
2908 * Dequeue from the START queue all CCBs that match
2909 * a given target/lun/task condition (-1 means all),
2910 * and move them from the BUSY queue to the COMP queue
2911 * with DID_SOFT_ERROR status condition.
2912 * This function is used during error handling/recovery.
2913 * It is called with SCRIPTS not running.
2916 sym_dequeue_from_squeue(struct sym_hcb *np, int i, int target, int lun, int task)
2922 * Make sure the starting index is within range.
2924 assert((i >= 0) && (i < 2*MAX_QUEUE));
2927 * Walk until end of START queue and dequeue every job
2928 * that matches the target/lun/task condition.
2931 while (i != np->squeueput) {
2932 cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
2934 #ifdef SYM_CONF_IARB_SUPPORT
2935 /* Forget hints for IARB, they may be no longer relevant */
2936 cp->host_flags &= ~HF_HINT_IARB;
2938 if ((target == -1 || cp->target == target) &&
2939 (lun == -1 || cp->lun == lun) &&
2940 (task == -1 || cp->tag == task)) {
2941 sym_set_cam_status(cp->cmd, DID_SOFT_ERROR);
2942 sym_remque(&cp->link_ccbq);
2943 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
2947 np->squeue[j] = np->squeue[i];
2948 if ((j += 2) >= MAX_QUEUE*2) j = 0;
2950 if ((i += 2) >= MAX_QUEUE*2) i = 0;
2952 if (i != j) /* Copy back the idle task if needed */
2953 np->squeue[j] = np->squeue[i];
2954 np->squeueput = j; /* Update our current start queue pointer */
2960 * chip handler for bad SCSI status condition
2962 * In case of bad SCSI status, we unqueue all the tasks
2963 * currently queued to the controller but not yet started
2964 * and then restart the SCRIPTS processor immediately.
2966 * QUEUE FULL and BUSY conditions are handled the same way.
2967 * Basically all the not yet started tasks are requeued in
2968 * device queue and the queue is frozen until a completion.
2970 * For CHECK CONDITION and COMMAND TERMINATED status, we use
2971 * the CCB of the failed command to prepare a REQUEST SENSE
2972 * SCSI command and queue it to the controller queue.
2974 * SCRATCHA is assumed to have been loaded with STARTPOS
2975 * before the SCRIPTS called the C code.
2977 static void sym_sir_bad_scsi_status(struct sym_hcb *np, int num, struct sym_ccb *cp)
2980 u_char s_status = cp->ssss_status;
2981 u_char h_flags = cp->host_flags;
2986 * Compute the index of the next job to start from SCRIPTS.
2988 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
2991 * The last CCB queued used for IARB hint may be
2992 * no longer relevant. Forget it.
2994 #ifdef SYM_CONF_IARB_SUPPORT
3000 * Now deal with the SCSI status.
3005 if (sym_verbose >= 2) {
3006 sym_print_addr(cp->cmd, "%s\n",
3007 s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
3009 default: /* S_INT, S_INT_COND_MET, S_CONFLICT */
3010 sym_complete_error (np, cp);
3015 * If we get an SCSI error when requesting sense, give up.
3017 if (h_flags & HF_SENSE) {
3018 sym_complete_error (np, cp);
3023 * Dequeue all queued CCBs for that device not yet started,
3024 * and restart the SCRIPTS processor immediately.
3026 sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
3027 OUTL_DSP(np, SCRIPTA_BA(np, start));
3030 * Save some info of the actual IO.
3031 * Compute the data residual.
3033 cp->sv_scsi_status = cp->ssss_status;
3034 cp->sv_xerr_status = cp->xerr_status;
3035 cp->sv_resid = sym_compute_residual(np, cp);
3038 * Prepare all needed data structures for
3039 * requesting sense data.
3042 cp->scsi_smsg2[0] = IDENTIFY(0, cp->lun);
3046 * If we are currently using anything different from
3047 * async. 8 bit data transfers with that target,
3048 * start a negotiation, since the device may want
3049 * to report us a UNIT ATTENTION condition due to
3050 * a cause we currently ignore, and we donnot want
3051 * to be stuck with WIDE and/or SYNC data transfer.
3053 * cp->nego_status is filled by sym_prepare_nego().
3055 cp->nego_status = 0;
3056 msglen += sym_prepare_nego(np, cp, &cp->scsi_smsg2[msglen]);
3058 * Message table indirect structure.
3060 cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg2);
3061 cp->phys.smsg.size = cpu_to_scr(msglen);
3066 cp->phys.cmd.addr = CCB_BA(cp, sensecmd);
3067 cp->phys.cmd.size = cpu_to_scr(6);
3070 * patch requested size into sense command
3072 cp->sensecmd[0] = REQUEST_SENSE;
3073 cp->sensecmd[1] = 0;
3074 if (cp->cmd->device->scsi_level <= SCSI_2 && cp->lun <= 7)
3075 cp->sensecmd[1] = cp->lun << 5;
3076 cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
3077 cp->data_len = SYM_SNS_BBUF_LEN;
3082 memset(cp->sns_bbuf, 0, SYM_SNS_BBUF_LEN);
3083 cp->phys.sense.addr = CCB_BA(cp, sns_bbuf);
3084 cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
3087 * requeue the command.
3089 startp = SCRIPTB_BA(np, sdata_in);
3091 cp->phys.head.savep = cpu_to_scr(startp);
3092 cp->phys.head.lastp = cpu_to_scr(startp);
3093 cp->startp = cpu_to_scr(startp);
3094 cp->goalp = cpu_to_scr(startp + 16);
3096 cp->host_xflags = 0;
3097 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
3098 cp->ssss_status = S_ILLEGAL;
3099 cp->host_flags = (HF_SENSE|HF_DATA_IN);
3100 cp->xerr_status = 0;
3101 cp->extra_bytes = 0;
3103 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
3106 * Requeue the command.
3108 sym_put_start_queue(np, cp);
3111 * Give back to upper layer everything we have dequeued.
3113 sym_flush_comp_queue(np, 0);
3119 * After a device has accepted some management message
3120 * as BUS DEVICE RESET, ABORT TASK, etc ..., or when
3121 * a device signals a UNIT ATTENTION condition, some
3122 * tasks are thrown away by the device. We are required
3123 * to reflect that on our tasks list since the device
3124 * will never complete these tasks.
3126 * This function move from the BUSY queue to the COMP
3127 * queue all disconnected CCBs for a given target that
3128 * match the following criteria:
3129 * - lun=-1 means any logical UNIT otherwise a given one.
3130 * - task=-1 means any task, otherwise a given one.
3132 int sym_clear_tasks(struct sym_hcb *np, int cam_status, int target, int lun, int task)
3134 SYM_QUEHEAD qtmp, *qp;
3139 * Move the entire BUSY queue to our temporary queue.
3141 sym_que_init(&qtmp);
3142 sym_que_splice(&np->busy_ccbq, &qtmp);
3143 sym_que_init(&np->busy_ccbq);
3146 * Put all CCBs that matches our criteria into
3147 * the COMP queue and put back other ones into
3150 while ((qp = sym_remque_head(&qtmp)) != 0) {
3151 struct scsi_cmnd *cmd;
3152 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3154 if (cp->host_status != HS_DISCONNECT ||
3155 cp->target != target ||
3156 (lun != -1 && cp->lun != lun) ||
3158 (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
3159 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
3162 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3164 /* Preserve the software timeout condition */
3165 if (sym_get_cam_status(cmd) != DID_TIME_OUT)
3166 sym_set_cam_status(cmd, cam_status);
3169 printf("XXXX TASK @%p CLEARED\n", cp);
3176 * chip handler for TASKS recovery
3178 * We cannot safely abort a command, while the SCRIPTS
3179 * processor is running, since we just would be in race
3182 * As long as we have tasks to abort, we keep the SEM
3183 * bit set in the ISTAT. When this bit is set, the
3184 * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
3185 * each time it enters the scheduler.
3187 * If we have to reset a target, clear tasks of a unit,
3188 * or to perform the abort of a disconnected job, we
3189 * restart the SCRIPTS for selecting the target. Once
3190 * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
3191 * If it loses arbitration, the SCRIPTS will interrupt again
3192 * the next time it will enter its scheduler, and so on ...
3194 * On SIR_TARGET_SELECTED, we scan for the more
3195 * appropriate thing to do:
3197 * - If nothing, we just sent a M_ABORT message to the
3198 * target to get rid of the useless SCSI bus ownership.
3199 * According to the specs, no tasks shall be affected.
3200 * - If the target is to be reset, we send it a M_RESET
3202 * - If a logical UNIT is to be cleared , we send the
3203 * IDENTIFY(lun) + M_ABORT.
3204 * - If an untagged task is to be aborted, we send the
3205 * IDENTIFY(lun) + M_ABORT.
3206 * - If a tagged task is to be aborted, we send the
3207 * IDENTIFY(lun) + task attributes + M_ABORT_TAG.
3209 * Once our 'kiss of death' :) message has been accepted
3210 * by the target, the SCRIPTS interrupts again
3211 * (SIR_ABORT_SENT). On this interrupt, we complete
3212 * all the CCBs that should have been aborted by the
3213 * target according to our message.
3215 static void sym_sir_task_recovery(struct sym_hcb *np, int num)
3219 struct sym_tcb *tp = NULL; /* gcc isn't quite smart enough yet */
3220 struct scsi_target *starget;
3221 int target=-1, lun=-1, task;
3226 * The SCRIPTS processor stopped before starting
3227 * the next command in order to allow us to perform
3228 * some task recovery.
3230 case SIR_SCRIPT_STOPPED:
3232 * Do we have any target to reset or unit to clear ?
3234 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
3235 tp = &np->target[i];
3237 (tp->lun0p && tp->lun0p->to_clear)) {
3243 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
3244 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
3254 * If not, walk the busy queue for any
3255 * disconnected CCB to be aborted.
3258 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3259 cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
3260 if (cp->host_status != HS_DISCONNECT)
3263 target = cp->target;
3270 * If some target is to be selected,
3271 * prepare and start the selection.
3274 tp = &np->target[target];
3275 np->abrt_sel.sel_id = target;
3276 np->abrt_sel.sel_scntl3 = tp->head.wval;
3277 np->abrt_sel.sel_sxfer = tp->head.sval;
3278 OUTL(np, nc_dsa, np->hcb_ba);
3279 OUTL_DSP(np, SCRIPTB_BA(np, sel_for_abort));
3284 * Now look for a CCB to abort that haven't started yet.
3285 * Btw, the SCRIPTS processor is still stopped, so
3286 * we are not in race.
3290 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3291 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3292 if (cp->host_status != HS_BUSY &&
3293 cp->host_status != HS_NEGOTIATE)
3297 #ifdef SYM_CONF_IARB_SUPPORT
3299 * If we are using IMMEDIATE ARBITRATION, we donnot
3300 * want to cancel the last queued CCB, since the
3301 * SCRIPTS may have anticipated the selection.
3303 if (cp == np->last_cp) {
3308 i = 1; /* Means we have found some */
3313 * We are done, so we donnot need
3314 * to synchronize with the SCRIPTS anylonger.
3315 * Remove the SEM flag from the ISTAT.
3318 OUTB(np, nc_istat, SIGP);
3322 * Compute index of next position in the start
3323 * queue the SCRIPTS intends to start and dequeue
3324 * all CCBs for that device that haven't been started.
3326 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
3327 i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
3330 * Make sure at least our IO to abort has been dequeued.
3332 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
3333 assert(i && sym_get_cam_status(cp->cmd) == DID_SOFT_ERROR);
3335 sym_remque(&cp->link_ccbq);
3336 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3339 * Keep track in cam status of the reason of the abort.
3341 if (cp->to_abort == 2)
3342 sym_set_cam_status(cp->cmd, DID_TIME_OUT);
3344 sym_set_cam_status(cp->cmd, DID_ABORT);
3347 * Complete with error everything that we have dequeued.
3349 sym_flush_comp_queue(np, 0);
3352 * The SCRIPTS processor has selected a target
3353 * we may have some manual recovery to perform for.
3355 case SIR_TARGET_SELECTED:
3356 target = INB(np, nc_sdid) & 0xf;
3357 tp = &np->target[target];
3359 np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
3362 * If the target is to be reset, prepare a
3363 * M_RESET message and clear the to_reset flag
3364 * since we donnot expect this operation to fail.
3367 np->abrt_msg[0] = M_RESET;
3368 np->abrt_tbl.size = 1;
3374 * Otherwise, look for some logical unit to be cleared.
3376 if (tp->lun0p && tp->lun0p->to_clear)
3378 else if (tp->lunmp) {
3379 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
3380 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
3388 * If a logical unit is to be cleared, prepare
3389 * an IDENTIFY(lun) + ABORT MESSAGE.
3392 struct sym_lcb *lp = sym_lp(tp, lun);
3393 lp->to_clear = 0; /* We don't expect to fail here */
3394 np->abrt_msg[0] = IDENTIFY(0, lun);
3395 np->abrt_msg[1] = M_ABORT;
3396 np->abrt_tbl.size = 2;
3401 * Otherwise, look for some disconnected job to
3402 * abort for this target.
3406 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3407 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3408 if (cp->host_status != HS_DISCONNECT)
3410 if (cp->target != target)
3414 i = 1; /* Means we have some */
3419 * If we have none, probably since the device has
3420 * completed the command before we won abitration,
3421 * send a M_ABORT message without IDENTIFY.
3422 * According to the specs, the device must just
3423 * disconnect the BUS and not abort any task.
3426 np->abrt_msg[0] = M_ABORT;
3427 np->abrt_tbl.size = 1;
3432 * We have some task to abort.
3433 * Set the IDENTIFY(lun)
3435 np->abrt_msg[0] = IDENTIFY(0, cp->lun);
3438 * If we want to abort an untagged command, we
3439 * will send a IDENTIFY + M_ABORT.
3440 * Otherwise (tagged command), we will send
3441 * a IDENTITFY + task attributes + ABORT TAG.
3443 if (cp->tag == NO_TAG) {
3444 np->abrt_msg[1] = M_ABORT;
3445 np->abrt_tbl.size = 2;
3447 np->abrt_msg[1] = cp->scsi_smsg[1];
3448 np->abrt_msg[2] = cp->scsi_smsg[2];
3449 np->abrt_msg[3] = M_ABORT_TAG;
3450 np->abrt_tbl.size = 4;
3453 * Keep track of software timeout condition, since the
3454 * peripheral driver may not count retries on abort
3455 * conditions not due to timeout.
3457 if (cp->to_abort == 2)
3458 sym_set_cam_status(cp->cmd, DID_TIME_OUT);
3459 cp->to_abort = 0; /* We donnot expect to fail here */
3463 * The target has accepted our message and switched
3464 * to BUS FREE phase as we expected.
3466 case SIR_ABORT_SENT:
3467 target = INB(np, nc_sdid) & 0xf;
3468 tp = &np->target[target];
3469 starget = tp->starget;
3472 ** If we didn't abort anything, leave here.
3474 if (np->abrt_msg[0] == M_ABORT)
3478 * If we sent a M_RESET, then a hardware reset has
3479 * been performed by the target.
3480 * - Reset everything to async 8 bit
3481 * - Tell ourself to negotiate next time :-)
3482 * - Prepare to clear all disconnected CCBs for
3483 * this target from our task list (lun=task=-1)
3487 if (np->abrt_msg[0] == M_RESET) {
3489 tp->head.wval = np->rv_scntl3;
3491 spi_period(starget) = 0;
3492 spi_offset(starget) = 0;
3493 spi_width(starget) = 0;
3494 spi_iu(starget) = 0;
3495 spi_dt(starget) = 0;
3496 spi_qas(starget) = 0;
3497 tp->tgoal.check_nego = 1;
3501 * Otherwise, check for the LUN and TASK(s)
3502 * concerned by the cancelation.
3503 * If it is not ABORT_TAG then it is CLEAR_QUEUE
3504 * or an ABORT message :-)
3507 lun = np->abrt_msg[0] & 0x3f;
3508 if (np->abrt_msg[1] == M_ABORT_TAG)
3509 task = np->abrt_msg[2];
3513 * Complete all the CCBs the device should have
3514 * aborted due to our 'kiss of death' message.
3516 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
3517 sym_dequeue_from_squeue(np, i, target, lun, -1);
3518 sym_clear_tasks(np, DID_ABORT, target, lun, task);
3519 sym_flush_comp_queue(np, 0);
3522 * If we sent a BDR, make upper layer aware of that.
3524 if (np->abrt_msg[0] == M_RESET)
3525 sym_xpt_async_sent_bdr(np, target);
3530 * Print to the log the message we intend to send.
3532 if (num == SIR_TARGET_SELECTED) {
3533 dev_info(&tp->starget->dev, "control msgout:");
3534 sym_printl_hex(np->abrt_msg, np->abrt_tbl.size);
3535 np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
3539 * Let the SCRIPTS processor continue.
3545 * Gerard's alchemy:) that deals with with the data
3546 * pointer for both MDP and the residual calculation.
3548 * I didn't want to bloat the code by more than 200
3549 * lines for the handling of both MDP and the residual.
3550 * This has been achieved by using a data pointer
3551 * representation consisting in an index in the data
3552 * array (dp_sg) and a negative offset (dp_ofs) that
3553 * have the following meaning:
3555 * - dp_sg = SYM_CONF_MAX_SG
3556 * we are at the end of the data script.
3557 * - dp_sg < SYM_CONF_MAX_SG
3558 * dp_sg points to the next entry of the scatter array
3559 * we want to transfer.
3561 * dp_ofs represents the residual of bytes of the
3562 * previous entry scatter entry we will send first.
3564 * no residual to send first.
3566 * The function sym_evaluate_dp() accepts an arbitray
3567 * offset (basically from the MDP message) and returns
3568 * the corresponding values of dp_sg and dp_ofs.
3571 static int sym_evaluate_dp(struct sym_hcb *np, struct sym_ccb *cp, u32 scr, int *ofs)
3574 int dp_ofs, dp_sg, dp_sgmin;
3579 * Compute the resulted data pointer in term of a script
3580 * address within some DATA script and a signed byte offset.
3584 if (dp_scr == SCRIPTA_BA(np, pm0_data))
3586 else if (dp_scr == SCRIPTA_BA(np, pm1_data))
3592 dp_scr = scr_to_cpu(pm->ret);
3593 dp_ofs -= scr_to_cpu(pm->sg.size) & 0x00ffffff;
3597 * If we are auto-sensing, then we are done.
3599 if (cp->host_flags & HF_SENSE) {
3605 * Deduce the index of the sg entry.
3606 * Keep track of the index of the first valid entry.
3607 * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the
3610 tmp = scr_to_cpu(cp->goalp);
3611 dp_sg = SYM_CONF_MAX_SG;
3613 dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
3614 dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
3617 * Move to the sg entry the data pointer belongs to.
3619 * If we are inside the data area, we expect result to be:
3622 * dp_ofs = 0 and dp_sg is the index of the sg entry
3623 * the data pointer belongs to (or the end of the data)
3625 * dp_ofs < 0 and dp_sg is the index of the sg entry
3626 * the data pointer belongs to + 1.
3630 while (dp_sg > dp_sgmin) {
3632 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3633 n = dp_ofs + (tmp & 0xffffff);
3641 else if (dp_ofs > 0) {
3642 while (dp_sg < SYM_CONF_MAX_SG) {
3643 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3644 dp_ofs -= (tmp & 0xffffff);
3652 * Make sure the data pointer is inside the data area.
3653 * If not, return some error.
3655 if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
3657 else if (dp_sg > SYM_CONF_MAX_SG ||
3658 (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
3662 * Save the extreme pointer if needed.
3664 if (dp_sg > cp->ext_sg ||
3665 (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
3667 cp->ext_ofs = dp_ofs;
3681 * chip handler for MODIFY DATA POINTER MESSAGE
3683 * We also call this function on IGNORE WIDE RESIDUE
3684 * messages that do not match a SWIDE full condition.
3685 * Btw, we assume in that situation that such a message
3686 * is equivalent to a MODIFY DATA POINTER (offset=-1).
3689 static void sym_modify_dp(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp, int ofs)
3692 u32 dp_scr = sym_get_script_dp (np, cp);
3700 * Not supported for auto-sense.
3702 if (cp->host_flags & HF_SENSE)
3706 * Apply our alchemy:) (see comments in sym_evaluate_dp()),
3707 * to the resulted data pointer.
3709 dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
3714 * And our alchemy:) allows to easily calculate the data
3715 * script address we want to return for the next data phase.
3717 dp_ret = cpu_to_scr(cp->goalp);
3718 dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
3721 * If offset / scatter entry is zero we donnot need
3722 * a context for the new current data pointer.
3730 * Get a context for the new current data pointer.
3732 hflags = INB(np, HF_PRT);
3734 if (hflags & HF_DP_SAVED)
3735 hflags ^= HF_ACT_PM;
3737 if (!(hflags & HF_ACT_PM)) {
3739 dp_scr = SCRIPTA_BA(np, pm0_data);
3743 dp_scr = SCRIPTA_BA(np, pm1_data);
3746 hflags &= ~(HF_DP_SAVED);
3748 OUTB(np, HF_PRT, hflags);
3751 * Set up the new current data pointer.
3752 * ofs < 0 there, and for the next data phase, we
3753 * want to transfer part of the data of the sg entry
3754 * corresponding to index dp_sg-1 prior to returning
3755 * to the main data script.
3757 pm->ret = cpu_to_scr(dp_ret);
3758 tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
3759 tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
3760 pm->sg.addr = cpu_to_scr(tmp);
3761 pm->sg.size = cpu_to_scr(-dp_ofs);
3764 sym_set_script_dp (np, cp, dp_scr);
3765 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
3769 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
3774 * chip calculation of the data residual.
3776 * As I used to say, the requirement of data residual
3777 * in SCSI is broken, useless and cannot be achieved
3778 * without huge complexity.
3779 * But most OSes and even the official CAM require it.
3780 * When stupidity happens to be so widely spread inside
3781 * a community, it gets hard to convince.
3783 * Anyway, I don't care, since I am not going to use
3784 * any software that considers this data residual as
3785 * a relevant information. :)
3788 int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp)
3790 int dp_sg, dp_sgmin, resid = 0;
3794 * Check for some data lost or just thrown away.
3795 * We are not required to be quite accurate in this
3796 * situation. Btw, if we are odd for output and the
3797 * device claims some more data, it may well happen
3798 * than our residual be zero. :-)
3800 if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
3801 if (cp->xerr_status & XE_EXTRA_DATA)
3802 resid -= cp->extra_bytes;
3803 if (cp->xerr_status & XE_SODL_UNRUN)
3805 if (cp->xerr_status & XE_SWIDE_OVRUN)
3810 * If all data has been transferred,
3811 * there is no residual.
3813 if (cp->phys.head.lastp == cp->goalp)
3817 * If no data transfer occurs, or if the data
3818 * pointer is weird, return full residual.
3820 if (cp->startp == cp->phys.head.lastp ||
3821 sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
3823 return cp->data_len;
3827 * If we were auto-sensing, then we are done.
3829 if (cp->host_flags & HF_SENSE) {
3834 * We are now full comfortable in the computation
3835 * of the data residual (2's complement).
3837 dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
3838 resid = -cp->ext_ofs;
3839 for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
3840 u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3841 resid += (tmp & 0xffffff);
3844 resid -= cp->odd_byte_adjustment;
3847 * Hopefully, the result is not too wrong.
3853 * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
3855 * When we try to negotiate, we append the negotiation message
3856 * to the identify and (maybe) simple tag message.
3857 * The host status field is set to HS_NEGOTIATE to mark this
3860 * If the target doesn't answer this message immediately
3861 * (as required by the standard), the SIR_NEGO_FAILED interrupt
3862 * will be raised eventually.
3863 * The handler removes the HS_NEGOTIATE status, and sets the
3864 * negotiated value to the default (async / nowide).
3866 * If we receive a matching answer immediately, we check it
3867 * for validity, and set the values.
3869 * If we receive a Reject message immediately, we assume the
3870 * negotiation has failed, and fall back to standard values.
3872 * If we receive a negotiation message while not in HS_NEGOTIATE
3873 * state, it's a target initiated negotiation. We prepare a
3874 * (hopefully) valid answer, set our parameters, and send back
3875 * this answer to the target.
3877 * If the target doesn't fetch the answer (no message out phase),
3878 * we assume the negotiation has failed, and fall back to default
3879 * settings (SIR_NEGO_PROTO interrupt).
3881 * When we set the values, we adjust them in all ccbs belonging
3882 * to this target, in the controller's register, and in the "phys"
3883 * field of the controller's struct sym_hcb.
3887 * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message.
3890 sym_sync_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
3892 int target = cp->target;
3893 u_char chg, ofs, per, fak, div;
3895 if (DEBUG_FLAGS & DEBUG_NEGO) {
3896 sym_print_nego_msg(np, target, "sync msgin", np->msgin);
3900 * Get requested values.
3907 * Check values against our limits.
3910 if (ofs > np->maxoffs)
3911 {chg = 1; ofs = np->maxoffs;}
3915 if (per < np->minsync)
3916 {chg = 1; per = np->minsync;}
3920 * Get new chip synchronous parameters value.
3923 if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
3926 if (DEBUG_FLAGS & DEBUG_NEGO) {
3927 sym_print_addr(cp->cmd,
3928 "sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
3929 ofs, per, div, fak, chg);
3933 * If it was an answer we want to change,
3934 * then it isn't acceptable. Reject it.
3942 sym_setsync (np, target, ofs, per, div, fak);
3945 * It was an answer. We are done.
3951 * It was a request. Prepare an answer message.
3953 np->msgout[0] = M_EXTENDED;
3955 np->msgout[2] = M_X_SYNC_REQ;
3956 np->msgout[3] = per;
3957 np->msgout[4] = ofs;
3959 if (DEBUG_FLAGS & DEBUG_NEGO) {
3960 sym_print_nego_msg(np, target, "sync msgout", np->msgout);
3963 np->msgin [0] = M_NOOP;
3968 sym_setsync (np, target, 0, 0, 0, 0);
3972 static void sym_sync_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
3978 * Request or answer ?
3980 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
3981 OUTB(np, HS_PRT, HS_BUSY);
3982 if (cp->nego_status && cp->nego_status != NS_SYNC)
3988 * Check and apply new values.
3990 result = sym_sync_nego_check(np, req, cp);
3991 if (result) /* Not acceptable, reject it */
3993 if (req) { /* Was a request, send response. */
3994 cp->nego_status = NS_SYNC;
3995 OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
3997 else /* Was a response, we are done. */
3998 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4002 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4006 * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message.
4009 sym_ppr_nego_check(struct sym_hcb *np, int req, int target)
4011 struct sym_tcb *tp = &np->target[target];
4012 unsigned char fak, div;
4015 unsigned char per = np->msgin[3];
4016 unsigned char ofs = np->msgin[5];
4017 unsigned char wide = np->msgin[6];
4018 unsigned char opts = np->msgin[7] & PPR_OPT_MASK;
4020 if (DEBUG_FLAGS & DEBUG_NEGO) {
4021 sym_print_nego_msg(np, target, "ppr msgin", np->msgin);
4025 * Check values against our limits.
4027 if (wide > np->maxwide) {
4031 if (!wide || !(np->features & FE_U3EN))
4034 if (opts != (np->msgin[7] & PPR_OPT_MASK))
4037 dt = opts & PPR_OPT_DT;
4040 unsigned char maxoffs = dt ? np->maxoffs_dt : np->maxoffs;
4041 if (ofs > maxoffs) {
4048 unsigned char minsync = dt ? np->minsync_dt : np->minsync;
4049 if (per < minsync) {
4056 * Get new chip synchronous parameters value.
4059 if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
4063 * If it was an answer we want to change,
4064 * then it isn't acceptable. Reject it.
4072 sym_setpprot(np, target, opts, ofs, per, wide, div, fak);
4075 * It was an answer. We are done.
4081 * It was a request. Prepare an answer message.
4083 np->msgout[0] = M_EXTENDED;
4085 np->msgout[2] = M_X_PPR_REQ;
4086 np->msgout[3] = per;
4088 np->msgout[5] = ofs;
4089 np->msgout[6] = wide;
4090 np->msgout[7] = opts;
4092 if (DEBUG_FLAGS & DEBUG_NEGO) {
4093 sym_print_nego_msg(np, target, "ppr msgout", np->msgout);
4096 np->msgin [0] = M_NOOP;
4101 sym_setpprot (np, target, 0, 0, 0, 0, 0, 0);
4103 * If it is a device response that should result in
4104 * ST, we may want to try a legacy negotiation later.
4106 if (!req && !opts) {
4107 tp->tgoal.period = per;
4108 tp->tgoal.offset = ofs;
4109 tp->tgoal.width = wide;
4110 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
4111 tp->tgoal.check_nego = 1;
4116 static void sym_ppr_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4122 * Request or answer ?
4124 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
4125 OUTB(np, HS_PRT, HS_BUSY);
4126 if (cp->nego_status && cp->nego_status != NS_PPR)
4132 * Check and apply new values.
4134 result = sym_ppr_nego_check(np, req, cp->target);
4135 if (result) /* Not acceptable, reject it */
4137 if (req) { /* Was a request, send response. */
4138 cp->nego_status = NS_PPR;
4139 OUTL_DSP(np, SCRIPTB_BA(np, ppr_resp));
4141 else /* Was a response, we are done. */
4142 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4146 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4150 * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message.
4153 sym_wide_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
4155 int target = cp->target;
4158 if (DEBUG_FLAGS & DEBUG_NEGO) {
4159 sym_print_nego_msg(np, target, "wide msgin", np->msgin);
4163 * Get requested values.
4166 wide = np->msgin[3];
4169 * Check values against our limits.
4171 if (wide > np->maxwide) {
4176 if (DEBUG_FLAGS & DEBUG_NEGO) {
4177 sym_print_addr(cp->cmd, "wdtr: wide=%d chg=%d.\n",
4182 * If it was an answer we want to change,
4183 * then it isn't acceptable. Reject it.
4191 sym_setwide (np, target, wide);
4194 * It was an answer. We are done.
4200 * It was a request. Prepare an answer message.
4202 np->msgout[0] = M_EXTENDED;
4204 np->msgout[2] = M_X_WIDE_REQ;
4205 np->msgout[3] = wide;
4207 np->msgin [0] = M_NOOP;
4209 if (DEBUG_FLAGS & DEBUG_NEGO) {
4210 sym_print_nego_msg(np, target, "wide msgout", np->msgout);
4219 static void sym_wide_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4225 * Request or answer ?
4227 if (INB(np, HS_PRT) == HS_NEGOTIATE) {
4228 OUTB(np, HS_PRT, HS_BUSY);
4229 if (cp->nego_status && cp->nego_status != NS_WIDE)
4235 * Check and apply new values.
4237 result = sym_wide_nego_check(np, req, cp);
4238 if (result) /* Not acceptable, reject it */
4240 if (req) { /* Was a request, send response. */
4241 cp->nego_status = NS_WIDE;
4242 OUTL_DSP(np, SCRIPTB_BA(np, wdtr_resp));
4243 } else { /* Was a response. */
4245 * Negotiate for SYNC immediately after WIDE response.
4246 * This allows to negotiate for both WIDE and SYNC on
4247 * a single SCSI command (Suggested by Justin Gibbs).
4249 if (tp->tgoal.offset) {
4250 np->msgout[0] = M_EXTENDED;
4252 np->msgout[2] = M_X_SYNC_REQ;
4253 np->msgout[3] = tp->tgoal.period;
4254 np->msgout[4] = tp->tgoal.offset;
4256 if (DEBUG_FLAGS & DEBUG_NEGO) {
4257 sym_print_nego_msg(np, cp->target,
4258 "sync msgout", np->msgout);
4261 cp->nego_status = NS_SYNC;
4262 OUTB(np, HS_PRT, HS_NEGOTIATE);
4263 OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
4266 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4272 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4276 * Reset DT, SYNC or WIDE to default settings.
4278 * Called when a negotiation does not succeed either
4279 * on rejection or on protocol error.
4281 * A target that understands a PPR message should never
4282 * reject it, and messing with it is very unlikely.
4283 * So, if a PPR makes problems, we may just want to
4284 * try a legacy negotiation later.
4286 static void sym_nego_default(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4288 switch (cp->nego_status) {
4291 sym_setpprot (np, cp->target, 0, 0, 0, 0, 0, 0);
4293 if (tp->tgoal.period < np->minsync)
4294 tp->tgoal.period = np->minsync;
4295 if (tp->tgoal.offset > np->maxoffs)
4296 tp->tgoal.offset = np->maxoffs;
4297 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
4298 tp->tgoal.check_nego = 1;
4302 sym_setsync (np, cp->target, 0, 0, 0, 0);
4305 sym_setwide (np, cp->target, 0);
4308 np->msgin [0] = M_NOOP;
4309 np->msgout[0] = M_NOOP;
4310 cp->nego_status = 0;
4314 * chip handler for MESSAGE REJECT received in response to
4315 * PPR, WIDE or SYNCHRONOUS negotiation.
4317 static void sym_nego_rejected(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
4319 sym_nego_default(np, tp, cp);
4320 OUTB(np, HS_PRT, HS_BUSY);
4324 * chip exception handler for programmed interrupts.
4326 static void sym_int_sir (struct sym_hcb *np)
4328 u_char num = INB(np, nc_dsps);
4329 u32 dsa = INL(np, nc_dsa);
4330 struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
4331 u_char target = INB(np, nc_sdid) & 0x0f;
4332 struct sym_tcb *tp = &np->target[target];
4335 if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
4338 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
4340 * SCRIPTS tell us that we may have to update
4341 * 64 bit DMA segment registers.
4343 case SIR_DMAP_DIRTY:
4344 sym_update_dmap_regs(np);
4348 * Command has been completed with error condition
4349 * or has been auto-sensed.
4351 case SIR_COMPLETE_ERROR:
4352 sym_complete_error(np, cp);
4355 * The C code is currently trying to recover from something.
4356 * Typically, user want to abort some command.
4358 case SIR_SCRIPT_STOPPED:
4359 case SIR_TARGET_SELECTED:
4360 case SIR_ABORT_SENT:
4361 sym_sir_task_recovery(np, num);
4364 * The device didn't go to MSG OUT phase after having
4365 * been selected with ATN. We donnot want to handle
4368 case SIR_SEL_ATN_NO_MSG_OUT:
4369 printf ("%s:%d: No MSG OUT phase after selection with ATN.\n",
4370 sym_name (np), target);
4373 * The device didn't switch to MSG IN phase after
4374 * having reseleted the initiator.
4376 case SIR_RESEL_NO_MSG_IN:
4377 printf ("%s:%d: No MSG IN phase after reselection.\n",
4378 sym_name (np), target);
4381 * After reselection, the device sent a message that wasn't
4384 case SIR_RESEL_NO_IDENTIFY:
4385 printf ("%s:%d: No IDENTIFY after reselection.\n",
4386 sym_name (np), target);
4389 * The device reselected a LUN we donnot know about.
4391 case SIR_RESEL_BAD_LUN:
4392 np->msgout[0] = M_RESET;
4395 * The device reselected for an untagged nexus and we
4398 case SIR_RESEL_BAD_I_T_L:
4399 np->msgout[0] = M_ABORT;
4402 * The device reselected for a tagged nexus that we donnot
4405 case SIR_RESEL_BAD_I_T_L_Q:
4406 np->msgout[0] = M_ABORT_TAG;
4409 * The SCRIPTS let us know that the device has grabbed
4410 * our message and will abort the job.
4412 case SIR_RESEL_ABORTED:
4413 np->lastmsg = np->msgout[0];
4414 np->msgout[0] = M_NOOP;
4415 printf ("%s:%d: message %x sent on bad reselection.\n",
4416 sym_name (np), target, np->lastmsg);
4419 * The SCRIPTS let us know that a message has been
4420 * successfully sent to the device.
4422 case SIR_MSG_OUT_DONE:
4423 np->lastmsg = np->msgout[0];
4424 np->msgout[0] = M_NOOP;
4425 /* Should we really care of that */
4426 if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
4428 cp->xerr_status &= ~XE_PARITY_ERR;
4429 if (!cp->xerr_status)
4430 OUTOFFB(np, HF_PRT, HF_EXT_ERR);
4435 * The device didn't send a GOOD SCSI status.
4436 * We may have some work to do prior to allow
4437 * the SCRIPTS processor to continue.
4439 case SIR_BAD_SCSI_STATUS:
4442 sym_sir_bad_scsi_status(np, num, cp);
4445 * We are asked by the SCRIPTS to prepare a
4448 case SIR_REJECT_TO_SEND:
4449 sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
4450 np->msgout[0] = M_REJECT;
4453 * We have been ODD at the end of a DATA IN
4454 * transfer and the device didn't send a
4455 * IGNORE WIDE RESIDUE message.
4456 * It is a data overrun condition.
4458 case SIR_SWIDE_OVERRUN:
4460 OUTONB(np, HF_PRT, HF_EXT_ERR);
4461 cp->xerr_status |= XE_SWIDE_OVRUN;
4465 * We have been ODD at the end of a DATA OUT
4467 * It is a data underrun condition.
4469 case SIR_SODL_UNDERRUN:
4471 OUTONB(np, HF_PRT, HF_EXT_ERR);
4472 cp->xerr_status |= XE_SODL_UNRUN;
4476 * The device wants us to tranfer more data than
4477 * expected or in the wrong direction.
4478 * The number of extra bytes is in scratcha.
4479 * It is a data overrun condition.
4481 case SIR_DATA_OVERRUN:
4483 OUTONB(np, HF_PRT, HF_EXT_ERR);
4484 cp->xerr_status |= XE_EXTRA_DATA;
4485 cp->extra_bytes += INL(np, nc_scratcha);
4489 * The device switched to an illegal phase (4/5).
4493 OUTONB(np, HF_PRT, HF_EXT_ERR);
4494 cp->xerr_status |= XE_BAD_PHASE;
4498 * We received a message.
4500 case SIR_MSG_RECEIVED:
4503 switch (np->msgin [0]) {
4505 * We received an extended message.
4506 * We handle MODIFY DATA POINTER, SDTR, WDTR
4507 * and reject all other extended messages.
4510 switch (np->msgin [2]) {
4512 if (DEBUG_FLAGS & DEBUG_POINTER)
4513 sym_print_msg(cp,"modify DP",np->msgin);
4514 tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
4515 (np->msgin[5]<<8) + (np->msgin[6]);
4516 sym_modify_dp(np, tp, cp, tmp);
4519 sym_sync_nego(np, tp, cp);
4522 sym_ppr_nego(np, tp, cp);
4525 sym_wide_nego(np, tp, cp);
4532 * We received a 1/2 byte message not handled from SCRIPTS.
4533 * We are only expecting MESSAGE REJECT and IGNORE WIDE
4534 * RESIDUE messages that haven't been anticipated by
4535 * SCRIPTS on SWIDE full condition. Unanticipated IGNORE
4536 * WIDE RESIDUE messages are aliased as MODIFY DP (-1).
4539 if (DEBUG_FLAGS & DEBUG_POINTER)
4540 sym_print_msg(cp,"ign wide residue", np->msgin);
4541 if (cp->host_flags & HF_SENSE)
4542 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4544 sym_modify_dp(np, tp, cp, -1);
4547 if (INB(np, HS_PRT) == HS_NEGOTIATE)
4548 sym_nego_rejected(np, tp, cp);
4550 sym_print_addr(cp->cmd,
4551 "M_REJECT received (%x:%x).\n",
4552 scr_to_cpu(np->lastmsg), np->msgout[0]);
4561 * We received an unknown message.
4562 * Ignore all MSG IN phases and reject it.
4565 sym_print_msg(cp, "WEIRD message received", np->msgin);
4566 OUTL_DSP(np, SCRIPTB_BA(np, msg_weird));
4569 * Negotiation failed.
4570 * Target does not send us the reply.
4571 * Remove the HS_NEGOTIATE status.
4573 case SIR_NEGO_FAILED:
4574 OUTB(np, HS_PRT, HS_BUSY);
4576 * Negotiation failed.
4577 * Target does not want answer message.
4579 case SIR_NEGO_PROTO:
4580 sym_nego_default(np, tp, cp);
4588 OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
4591 OUTL_DSP(np, SCRIPTA_BA(np, clrack));
4598 * Acquire a control block
4600 struct sym_ccb *sym_get_ccb (struct sym_hcb *np, struct scsi_cmnd *cmd, u_char tag_order)
4602 u_char tn = cmd->device->id;
4603 u_char ln = cmd->device->lun;
4604 struct sym_tcb *tp = &np->target[tn];
4605 struct sym_lcb *lp = sym_lp(tp, ln);
4606 u_short tag = NO_TAG;
4608 struct sym_ccb *cp = NULL;
4611 * Look for a free CCB
4613 if (sym_que_empty(&np->free_ccbq))
4615 qp = sym_remque_head(&np->free_ccbq);
4618 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
4622 * If we have been asked for a tagged command.
4626 * Debugging purpose.
4628 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4629 assert(lp->busy_itl == 0);
4632 * Allocate resources for tags if not yet.
4635 sym_alloc_lcb_tags(np, tn, ln);
4640 * Get a tag for this SCSI IO and set up
4641 * the CCB bus address for reselection,
4642 * and count it for this LUN.
4643 * Toggle reselect path to tagged.
4645 if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
4646 tag = lp->cb_tags[lp->ia_tag];
4647 if (++lp->ia_tag == SYM_CONF_MAX_TASK)
4650 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4651 lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
4653 cpu_to_scr(SCRIPTA_BA(np, resel_tag));
4655 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
4656 cp->tags_si = lp->tags_si;
4657 ++lp->tags_sum[cp->tags_si];
4665 * This command will not be tagged.
4666 * If we already have either a tagged or untagged
4667 * one, refuse to overlap this untagged one.
4671 * Debugging purpose.
4673 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4674 assert(lp->busy_itl == 0 && lp->busy_itlq == 0);
4677 * Count this nexus for this LUN.
4678 * Set up the CCB bus address for reselection.
4679 * Toggle reselect path to untagged.
4682 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4683 if (lp->busy_itl == 1) {
4684 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
4686 cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
4694 * Put the CCB into the busy queue.
4696 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
4697 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4699 sym_remque(&cp->link2_ccbq);
4700 sym_insque_tail(&cp->link2_ccbq, &lp->waiting_ccbq);
4705 cp->odd_byte_adjustment = 0;
4707 cp->order = tag_order;
4711 if (DEBUG_FLAGS & DEBUG_TAGS) {
4712 sym_print_addr(cmd, "ccb @%p using tag %d.\n", cp, tag);
4718 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4723 * Release one control block
4725 void sym_free_ccb (struct sym_hcb *np, struct sym_ccb *cp)
4727 struct sym_tcb *tp = &np->target[cp->target];
4728 struct sym_lcb *lp = sym_lp(tp, cp->lun);
4730 if (DEBUG_FLAGS & DEBUG_TAGS) {
4731 sym_print_addr(cp->cmd, "ccb @%p freeing tag %d.\n",
4740 * If tagged, release the tag, set the relect path
4742 if (cp->tag != NO_TAG) {
4743 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
4744 --lp->tags_sum[cp->tags_si];
4747 * Free the tag value.
4749 lp->cb_tags[lp->if_tag] = cp->tag;
4750 if (++lp->if_tag == SYM_CONF_MAX_TASK)
4753 * Make the reselect path invalid,
4754 * and uncount this CCB.
4756 lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
4758 } else { /* Untagged */
4760 * Make the reselect path invalid,
4761 * and uncount this CCB.
4763 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
4767 * If no JOB active, make the LUN reselect path invalid.
4769 if (lp->busy_itlq == 0 && lp->busy_itl == 0)
4771 cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
4775 * We donnot queue more than 1 ccb per target
4776 * with negotiation at any time. If this ccb was
4777 * used for negotiation, clear this info in the tcb.
4779 if (cp == tp->nego_cp)
4782 #ifdef SYM_CONF_IARB_SUPPORT
4784 * If we just complete the last queued CCB,
4785 * clear this info that is no longer relevant.
4787 if (cp == np->last_cp)
4792 * Make this CCB available.
4795 cp->host_status = HS_IDLE;
4796 sym_remque(&cp->link_ccbq);
4797 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4799 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4801 sym_remque(&cp->link2_ccbq);
4802 sym_insque_tail(&cp->link2_ccbq, &np->dummy_ccbq);
4804 if (cp->tag != NO_TAG)
4807 --lp->started_no_tag;
4815 * Allocate a CCB from memory and initialize its fixed part.
4817 static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np)
4819 struct sym_ccb *cp = NULL;
4823 * Prevent from allocating more CCBs than we can
4824 * queue to the controller.
4826 if (np->actccbs >= SYM_CONF_MAX_START)
4830 * Allocate memory for this CCB.
4832 cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
4842 * Compute the bus address of this ccb.
4844 cp->ccb_ba = vtobus(cp);
4847 * Insert this ccb into the hashed list.
4849 hcode = CCB_HASH_CODE(cp->ccb_ba);
4850 cp->link_ccbh = np->ccbh[hcode];
4851 np->ccbh[hcode] = cp;
4854 * Initialyze the start and restart actions.
4856 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, idle));
4857 cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
4860 * Initilialyze some other fields.
4862 cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
4865 * Chain into free ccb queue.
4867 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4870 * Chain into optionnal lists.
4872 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4873 sym_insque_head(&cp->link2_ccbq, &np->dummy_ccbq);
4878 sym_mfree_dma(cp, sizeof(*cp), "CCB");
4883 * Look up a CCB from a DSA value.
4885 static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa)
4890 hcode = CCB_HASH_CODE(dsa);
4891 cp = np->ccbh[hcode];
4893 if (cp->ccb_ba == dsa)
4902 * Target control block initialisation.
4903 * Nothing important to do at the moment.
4905 static void sym_init_tcb (struct sym_hcb *np, u_char tn)
4907 #if 0 /* Hmmm... this checking looks paranoid. */
4909 * Check some alignments required by the chip.
4911 assert (((offsetof(struct sym_reg, nc_sxfer) ^
4912 offsetof(struct sym_tcb, head.sval)) &3) == 0);
4913 assert (((offsetof(struct sym_reg, nc_scntl3) ^
4914 offsetof(struct sym_tcb, head.wval)) &3) == 0);
4919 * Lun control block allocation and initialization.
4921 struct sym_lcb *sym_alloc_lcb (struct sym_hcb *np, u_char tn, u_char ln)
4923 struct sym_tcb *tp = &np->target[tn];
4924 struct sym_lcb *lp = NULL;
4927 * Initialize the target control block if not yet.
4929 sym_init_tcb (np, tn);
4932 * Allocate the LCB bus address array.
4933 * Compute the bus address of this table.
4935 if (ln && !tp->luntbl) {
4938 tp->luntbl = sym_calloc_dma(256, "LUNTBL");
4941 for (i = 0 ; i < 64 ; i++)
4942 tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
4943 tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
4947 * Allocate the table of pointers for LUN(s) > 0, if needed.
4949 if (ln && !tp->lunmp) {
4950 tp->lunmp = kcalloc(SYM_CONF_MAX_LUN, sizeof(struct sym_lcb *),
4958 * Make it available to the chip.
4960 lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
4965 tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
4969 tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
4973 * Let the itl task point to error handling.
4975 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
4978 * Set the reselect pattern to our default. :)
4980 lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
4983 * Set user capabilities.
4985 lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
4987 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4989 * Initialize device queueing.
4991 sym_que_init(&lp->waiting_ccbq);
4992 sym_que_init(&lp->started_ccbq);
4993 lp->started_max = SYM_CONF_MAX_TASK;
4994 lp->started_limit = SYM_CONF_MAX_TASK;
5002 * Allocate LCB resources for tagged command queuing.
5004 static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln)
5006 struct sym_tcb *tp = &np->target[tn];
5007 struct sym_lcb *lp = sym_lp(tp, ln);
5011 * Allocate the task table and and the tag allocation
5012 * circular buffer. We want both or none.
5014 lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
5017 lp->cb_tags = kcalloc(SYM_CONF_MAX_TASK, 1, GFP_ATOMIC);
5019 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
5020 lp->itlq_tbl = NULL;
5025 * Initialize the task table with invalid entries.
5027 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
5028 lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
5031 * Fill up the tag buffer with tag numbers.
5033 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
5037 * Make the task table available to SCRIPTS,
5038 * And accept tagged commands now.
5040 lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
5048 * Queue a SCSI IO to the controller.
5050 int sym_queue_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
5052 struct scsi_device *sdev = cmd->device;
5060 * Keep track of the IO in our CCB.
5065 * Retrieve the target descriptor.
5067 tp = &np->target[cp->target];
5070 * Retrieve the lun descriptor.
5072 lp = sym_lp(tp, sdev->lun);
5074 can_disconnect = (cp->tag != NO_TAG) ||
5075 (lp && (lp->curr_flags & SYM_DISC_ENABLED));
5077 msgptr = cp->scsi_smsg;
5079 msgptr[msglen++] = IDENTIFY(can_disconnect, sdev->lun);
5082 * Build the tag message if present.
5084 if (cp->tag != NO_TAG) {
5085 u_char order = cp->order;
5093 order = M_SIMPLE_TAG;
5095 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
5097 * Avoid too much reordering of SCSI commands.
5098 * The algorithm tries to prevent completion of any
5099 * tagged command from being delayed against more
5100 * than 3 times the max number of queued commands.
5102 if (lp && lp->tags_since > 3*SYM_CONF_MAX_TAG) {
5103 lp->tags_si = !(lp->tags_si);
5104 if (lp->tags_sum[lp->tags_si]) {
5105 order = M_ORDERED_TAG;
5106 if ((DEBUG_FLAGS & DEBUG_TAGS)||sym_verbose>1) {
5108 "ordered tag forced.\n");
5114 msgptr[msglen++] = order;
5117 * For less than 128 tags, actual tags are numbered
5118 * 1,3,5,..2*MAXTAGS+1,since we may have to deal
5119 * with devices that have problems with #TAG 0 or too
5120 * great #TAG numbers. For more tags (up to 256),
5121 * we use directly our tag number.
5123 #if SYM_CONF_MAX_TASK > (512/4)
5124 msgptr[msglen++] = cp->tag;
5126 msgptr[msglen++] = (cp->tag << 1) + 1;
5131 * Build a negotiation message if needed.
5132 * (nego_status is filled by sym_prepare_nego())
5134 cp->nego_status = 0;
5135 if (tp->tgoal.check_nego && !tp->nego_cp && lp) {
5136 msglen += sym_prepare_nego(np, cp, msgptr + msglen);
5142 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
5143 cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA(np, resel_dsa));
5148 cp->phys.select.sel_id = cp->target;
5149 cp->phys.select.sel_scntl3 = tp->head.wval;
5150 cp->phys.select.sel_sxfer = tp->head.sval;
5151 cp->phys.select.sel_scntl4 = tp->head.uval;
5156 cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg);
5157 cp->phys.smsg.size = cpu_to_scr(msglen);
5162 cp->host_xflags = 0;
5163 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
5164 cp->ssss_status = S_ILLEGAL;
5165 cp->xerr_status = 0;
5167 cp->extra_bytes = 0;
5170 * extreme data pointer.
5171 * shall be positive, so -1 is lower than lowest.:)
5177 * Build the CDB and DATA descriptor block
5180 return sym_setup_data_and_start(np, cmd, cp);
5184 * Reset a SCSI target (all LUNs of this target).
5186 int sym_reset_scsi_target(struct sym_hcb *np, int target)
5190 if (target == np->myaddr || (u_int)target >= SYM_CONF_MAX_TARGET)
5193 tp = &np->target[target];
5196 np->istat_sem = SEM;
5197 OUTB(np, nc_istat, SIGP|SEM);
5205 static int sym_abort_ccb(struct sym_hcb *np, struct sym_ccb *cp, int timed_out)
5208 * Check that the IO is active.
5210 if (!cp || !cp->host_status || cp->host_status == HS_WAIT)
5214 * If a previous abort didn't succeed in time,
5215 * perform a BUS reset.
5218 sym_reset_scsi_bus(np, 1);
5223 * Mark the CCB for abort and allow time for.
5225 cp->to_abort = timed_out ? 2 : 1;
5228 * Tell the SCRIPTS processor to stop and synchronize with us.
5230 np->istat_sem = SEM;
5231 OUTB(np, nc_istat, SIGP|SEM);
5235 int sym_abort_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, int timed_out)
5241 * Look up our CCB control block.
5244 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
5245 struct sym_ccb *cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
5246 if (cp2->cmd == cmd) {
5252 return sym_abort_ccb(np, cp, timed_out);
5256 * Complete execution of a SCSI command with extended
5257 * error, SCSI status error, or having been auto-sensed.
5259 * The SCRIPTS processor is not running there, so we
5260 * can safely access IO registers and remove JOBs from
5262 * SCRATCHA is assumed to have been loaded with STARTPOS
5263 * before the SCRIPTS called the C code.
5265 void sym_complete_error(struct sym_hcb *np, struct sym_ccb *cp)
5267 struct scsi_device *sdev;
5268 struct scsi_cmnd *cmd;
5275 * Paranoid check. :)
5277 if (!cp || !cp->cmd)
5282 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
5283 dev_info(&sdev->sdev_gendev, "CCB=%p STAT=%x/%x/%x\n", cp,
5284 cp->host_status, cp->ssss_status, cp->host_flags);
5288 * Get target and lun pointers.
5290 tp = &np->target[cp->target];
5291 lp = sym_lp(tp, sdev->lun);
5294 * Check for extended errors.
5296 if (cp->xerr_status) {
5298 sym_print_xerr(cmd, cp->xerr_status);
5299 if (cp->host_status == HS_COMPLETE)
5300 cp->host_status = HS_COMP_ERR;
5304 * Calculate the residual.
5306 resid = sym_compute_residual(np, cp);
5308 if (!SYM_SETUP_RESIDUAL_SUPPORT) {/* If user does not want residuals */
5309 resid = 0; /* throw them away. :) */
5314 printf("XXXX RESID= %d - 0x%x\n", resid, resid);
5318 * Dequeue all queued CCBs for that device
5319 * not yet started by SCRIPTS.
5321 i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
5322 i = sym_dequeue_from_squeue(np, i, cp->target, sdev->lun, -1);
5325 * Restart the SCRIPTS processor.
5327 OUTL_DSP(np, SCRIPTA_BA(np, start));
5329 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5330 if (cp->host_status == HS_COMPLETE &&
5331 cp->ssss_status == S_QUEUE_FULL) {
5332 if (!lp || lp->started_tags - i < 2)
5335 * Decrease queue depth as needed.
5337 lp->started_max = lp->started_tags - i - 1;
5340 if (sym_verbose >= 2) {
5341 sym_print_addr(cmd, " queue depth is now %d\n",
5348 cp->host_status = HS_BUSY;
5349 cp->ssss_status = S_ILLEGAL;
5352 * Let's requeue it to device.
5354 sym_set_cam_status(cmd, DID_SOFT_ERROR);
5360 * Build result in CAM ccb.
5362 sym_set_cam_result_error(np, cp, resid);
5364 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5368 * Add this one to the COMP queue.
5370 sym_remque(&cp->link_ccbq);
5371 sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
5374 * Complete all those commands with either error
5375 * or requeue condition.
5377 sym_flush_comp_queue(np, 0);
5379 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5381 * Donnot start more than 1 command after an error.
5383 sym_start_next_ccbs(np, lp, 1);
5388 * Complete execution of a successful SCSI command.
5390 * Only successful commands go to the DONE queue,
5391 * since we need to have the SCRIPTS processor
5392 * stopped on any error condition.
5393 * The SCRIPTS processor is running while we are
5394 * completing successful commands.
5396 void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp)
5400 struct scsi_cmnd *cmd;
5404 * Paranoid check. :)
5406 if (!cp || !cp->cmd)
5408 assert (cp->host_status == HS_COMPLETE);
5416 * Get target and lun pointers.
5418 tp = &np->target[cp->target];
5419 lp = sym_lp(tp, cp->lun);
5422 * If all data have been transferred, given than no
5423 * extended error did occur, there is no residual.
5426 if (cp->phys.head.lastp != cp->goalp)
5427 resid = sym_compute_residual(np, cp);
5430 * Wrong transfer residuals may be worse than just always
5431 * returning zero. User can disable this feature in
5432 * sym53c8xx.h. Residual support is enabled by default.
5434 if (!SYM_SETUP_RESIDUAL_SUPPORT)
5438 printf("XXXX RESID= %d - 0x%x\n", resid, resid);
5442 * Build result in CAM ccb.
5444 sym_set_cam_result_ok(cp, cmd, resid);
5446 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5448 * If max number of started ccbs had been reduced,
5449 * increase it if 200 good status received.
5451 if (lp && lp->started_max < lp->started_limit) {
5453 if (lp->num_sgood >= 200) {
5456 if (sym_verbose >= 2) {
5457 sym_print_addr(cmd, " queue depth is now %d\n",
5467 sym_free_ccb (np, cp);
5469 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5471 * Requeue a couple of awaiting scsi commands.
5473 if (!sym_que_empty(&lp->waiting_ccbq))
5474 sym_start_next_ccbs(np, lp, 2);
5477 * Complete the command.
5479 sym_xpt_done(np, cmd);
5483 * Soft-attach the controller.
5485 int sym_hcb_attach(struct Scsi_Host *shost, struct sym_fw *fw, struct sym_nvram *nvram)
5487 struct sym_hcb *np = sym_get_hcb(shost);
5491 * Get some info about the firmware.
5493 np->scripta_sz = fw->a_size;
5494 np->scriptb_sz = fw->b_size;
5495 np->scriptz_sz = fw->z_size;
5496 np->fw_setup = fw->setup;
5497 np->fw_patch = fw->patch;
5498 np->fw_name = fw->name;
5501 * Save setting of some IO registers, so we will
5502 * be able to probe specific implementations.
5504 sym_save_initial_setting (np);
5507 * Reset the chip now, since it has been reported
5508 * that SCSI clock calibration may not work properly
5509 * if the chip is currently active.
5514 * Prepare controller and devices settings, according
5515 * to chip features, user set-up and driver set-up.
5517 sym_prepare_setting(shost, np, nvram);
5520 * Check the PCI clock frequency.
5521 * Must be performed after prepare_setting since it destroys
5522 * STEST1 that is used to probe for the clock doubler.
5524 i = sym_getpciclock(np);
5525 if (i > 37000 && !(np->features & FE_66MHZ))
5526 printf("%s: PCI BUS clock seems too high: %u KHz.\n",
5530 * Allocate the start queue.
5532 np->squeue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
5535 np->squeue_ba = vtobus(np->squeue);
5538 * Allocate the done queue.
5540 np->dqueue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
5543 np->dqueue_ba = vtobus(np->dqueue);
5546 * Allocate the target bus address array.
5548 np->targtbl = sym_calloc_dma(256, "TARGTBL");
5551 np->targtbl_ba = vtobus(np->targtbl);
5554 * Allocate SCRIPTS areas.
5556 np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
5557 np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
5558 np->scriptz0 = sym_calloc_dma(np->scriptz_sz, "SCRIPTZ0");
5559 if (!np->scripta0 || !np->scriptb0 || !np->scriptz0)
5563 * Allocate the array of lists of CCBs hashed by DSA.
5565 np->ccbh = kcalloc(sizeof(struct sym_ccb **), CCB_HASH_SIZE, GFP_KERNEL);
5570 * Initialyze the CCB free and busy queues.
5572 sym_que_init(&np->free_ccbq);
5573 sym_que_init(&np->busy_ccbq);
5574 sym_que_init(&np->comp_ccbq);
5577 * Initialization for optional handling
5578 * of device queueing.
5580 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5581 sym_que_init(&np->dummy_ccbq);
5584 * Allocate some CCB. We need at least ONE.
5586 if (!sym_alloc_ccb(np))
5590 * Calculate BUS addresses where we are going
5591 * to load the SCRIPTS.
5593 np->scripta_ba = vtobus(np->scripta0);
5594 np->scriptb_ba = vtobus(np->scriptb0);
5595 np->scriptz_ba = vtobus(np->scriptz0);
5598 np->scripta_ba = np->ram_ba;
5599 if (np->features & FE_RAM8K) {
5601 np->scriptb_ba = np->scripta_ba + 4096;
5602 #if 0 /* May get useful for 64 BIT PCI addressing */
5603 np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32);
5611 * Copy scripts to controller instance.
5613 memcpy(np->scripta0, fw->a_base, np->scripta_sz);
5614 memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
5615 memcpy(np->scriptz0, fw->z_base, np->scriptz_sz);
5618 * Setup variable parts in scripts and compute
5619 * scripts bus addresses used from the C code.
5621 np->fw_setup(np, fw);
5624 * Bind SCRIPTS with physical addresses usable by the
5625 * SCRIPTS processor (as seen from the BUS = BUS addresses).
5627 sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
5628 sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
5629 sym_fw_bind_script(np, (u32 *) np->scriptz0, np->scriptz_sz);
5631 #ifdef SYM_CONF_IARB_SUPPORT
5633 * If user wants IARB to be set when we win arbitration
5634 * and have other jobs, compute the max number of consecutive
5635 * settings of IARB hints before we leave devices a chance to
5636 * arbitrate for reselection.
5638 #ifdef SYM_SETUP_IARB_MAX
5639 np->iarb_max = SYM_SETUP_IARB_MAX;
5646 * Prepare the idle and invalid task actions.
5648 np->idletask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5649 np->idletask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5650 np->idletask_ba = vtobus(&np->idletask);
5652 np->notask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5653 np->notask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5654 np->notask_ba = vtobus(&np->notask);
5656 np->bad_itl.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5657 np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
5658 np->bad_itl_ba = vtobus(&np->bad_itl);
5660 np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA(np, idle));
5661 np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA(np,bad_i_t_l_q));
5662 np->bad_itlq_ba = vtobus(&np->bad_itlq);
5665 * Allocate and prepare the lun JUMP table that is used
5666 * for a target prior the probing of devices (bad lun table).
5667 * A private table will be allocated for the target on the
5668 * first INQUIRY response received.
5670 np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
5674 np->badlun_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
5675 for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */
5676 np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
5679 * Prepare the bus address array that contains the bus
5680 * address of each target control block.
5681 * For now, assume all logical units are wrong. :)
5683 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
5684 np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
5685 np->target[i].head.luntbl_sa =
5686 cpu_to_scr(vtobus(np->badluntbl));
5687 np->target[i].head.lun0_sa =
5688 cpu_to_scr(vtobus(&np->badlun_sa));
5692 * Now check the cache handling of the pci chipset.
5694 if (sym_snooptest (np)) {
5695 printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np));
5700 * Sigh! we are done.
5709 * Free everything that has been allocated for this device.
5711 void sym_hcb_free(struct sym_hcb *np)
5719 sym_mfree_dma(np->scriptz0, np->scriptz_sz, "SCRIPTZ0");
5721 sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
5723 sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
5725 sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
5727 sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
5730 while ((qp = sym_remque_head(&np->free_ccbq)) != 0) {
5731 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
5732 sym_mfree_dma(cp, sizeof(*cp), "CCB");
5738 sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
5740 for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
5741 tp = &np->target[target];
5742 #if SYM_CONF_MAX_LUN > 1
5747 sym_mfree_dma(np->targtbl, 256, "TARGTBL");