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-2004 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 "sym_nvram.h"
44 #define SYM_DEBUG_GENERIC_SUPPORT
48 * Needed function prototypes.
50 static void sym_int_ma (struct sym_hcb *np);
51 static void sym_int_sir (struct sym_hcb *np);
52 static ccb_p sym_alloc_ccb(struct sym_hcb *np);
53 static ccb_p sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa);
54 static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln);
55 static void sym_complete_error (struct sym_hcb *np, ccb_p cp);
56 static void sym_complete_ok (struct sym_hcb *np, ccb_p cp);
57 static int sym_compute_residual(struct sym_hcb *np, ccb_p cp);
60 * Returns the name of this driver.
62 char *sym_driver_name(void)
64 return SYM_DRIVER_NAME;
67 * Print a buffer in hexadecimal format.
69 static void sym_printb_hex (u_char *p, int n)
76 * Same with a label at beginning and .\n at end.
78 static void sym_printl_hex (char *label, u_char *p, int n)
81 sym_printb_hex (p, n);
86 * Print something which allows to retrieve the controler type,
87 * unit, target, lun concerned by a kernel message.
89 static void sym_print_target (struct sym_hcb *np, int target)
91 printf ("%s:%d:", sym_name(np), target);
94 static void sym_print_lun(struct sym_hcb *np, int target, int lun)
96 printf ("%s:%d:%d:", sym_name(np), target, lun);
100 * Print out the content of a SCSI message.
102 static int sym_show_msg (u_char * msg)
106 if (*msg==M_EXTENDED) {
108 if (i-1>msg[1]) break;
109 printf ("-%x",msg[i]);
112 } else if ((*msg & 0xf0) == 0x20) {
113 printf ("-%x",msg[1]);
119 static void sym_print_msg (ccb_p cp, char *label, u_char *msg)
123 printf ("%s: ", label);
125 (void) sym_show_msg (msg);
129 static void sym_print_nego_msg (struct sym_hcb *np, int target, char *label, u_char *msg)
131 PRINT_TARGET(np, target);
133 printf ("%s: ", label);
135 (void) sym_show_msg (msg);
140 * Print something that tells about extended errors.
142 void sym_print_xerr(ccb_p cp, int x_status)
144 if (x_status & XE_PARITY_ERR) {
146 printf ("unrecovered SCSI parity error.\n");
148 if (x_status & XE_EXTRA_DATA) {
150 printf ("extraneous data discarded.\n");
152 if (x_status & XE_BAD_PHASE) {
154 printf ("illegal scsi phase (4/5).\n");
156 if (x_status & XE_SODL_UNRUN) {
158 printf ("ODD transfer in DATA OUT phase.\n");
160 if (x_status & XE_SWIDE_OVRUN) {
162 printf ("ODD transfer in DATA IN phase.\n");
167 * Return a string for SCSI BUS mode.
169 static char *sym_scsi_bus_mode(int mode)
172 case SMODE_HVD: return "HVD";
173 case SMODE_SE: return "SE";
174 case SMODE_LVD: return "LVD";
180 * Soft reset the chip.
182 * Raising SRST when the chip is running may cause
183 * problems on dual function chips (see below).
184 * On the other hand, LVD devices need some delay
185 * to settle and report actual BUS mode in STEST4.
187 static void sym_chip_reset (struct sym_hcb *np)
189 OUTB (nc_istat, SRST);
192 UDELAY(2000); /* For BUS MODE to settle */
196 * Really soft reset the chip.:)
198 * Some 896 and 876 chip revisions may hang-up if we set
199 * the SRST (soft reset) bit at the wrong time when SCRIPTS
201 * So, we need to abort the current operation prior to
202 * soft resetting the chip.
204 static void sym_soft_reset (struct sym_hcb *np)
209 if (!(np->features & FE_ISTAT1) || !(INB (nc_istat1) & SCRUN))
212 OUTB (nc_istat, CABRT);
213 for (i = 100000 ; i ; --i) {
214 istat = INB (nc_istat);
218 else if (istat & DIP) {
219 if (INB (nc_dstat) & ABRT)
226 printf("%s: unable to abort current chip operation, "
227 "ISTAT=0x%02x.\n", sym_name(np), istat);
233 * Start reset process.
235 * The interrupt handler will reinitialize the chip.
237 static void sym_start_reset(struct sym_hcb *np)
239 (void) sym_reset_scsi_bus(np, 1);
242 int sym_reset_scsi_bus(struct sym_hcb *np, int enab_int)
247 sym_soft_reset(np); /* Soft reset the chip */
251 * Enable Tolerant, reset IRQD if present and
252 * properly set IRQ mode, prior to resetting the bus.
254 OUTB (nc_stest3, TE);
255 OUTB (nc_dcntl, (np->rv_dcntl & IRQM));
256 OUTB (nc_scntl1, CRST);
259 if (!SYM_SETUP_SCSI_BUS_CHECK)
262 * Check for no terminators or SCSI bus shorts to ground.
263 * Read SCSI data bus, data parity bits and control signals.
264 * We are expecting RESET to be TRUE and other signals to be
267 term = INB(nc_sstat0);
268 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
269 term |= ((INB(nc_sstat2) & 0x01) << 26) | /* sdp1 */
270 ((INW(nc_sbdl) & 0xff) << 9) | /* d7-0 */
271 ((INW(nc_sbdl) & 0xff00) << 10) | /* d15-8 */
272 INB(nc_sbcl); /* req ack bsy sel atn msg cd io */
277 if (term != (2<<7)) {
278 printf("%s: suspicious SCSI data while resetting the BUS.\n",
280 printf("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
281 "0x%lx, expecting 0x%lx\n",
283 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
284 (u_long)term, (u_long)(2<<7));
285 if (SYM_SETUP_SCSI_BUS_CHECK == 1)
294 * Select SCSI clock frequency
296 static void sym_selectclock(struct sym_hcb *np, u_char scntl3)
299 * If multiplier not present or not selected, leave here.
301 if (np->multiplier <= 1) {
302 OUTB(nc_scntl3, scntl3);
306 if (sym_verbose >= 2)
307 printf ("%s: enabling clock multiplier\n", sym_name(np));
309 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
311 * Wait for the LCKFRQ bit to be set if supported by the chip.
312 * Otherwise wait 50 micro-seconds (at least).
314 if (np->features & FE_LCKFRQ) {
316 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
319 printf("%s: the chip cannot lock the frequency\n",
323 OUTB(nc_stest3, HSC); /* Halt the scsi clock */
324 OUTB(nc_scntl3, scntl3);
325 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
326 OUTB(nc_stest3, 0x00); /* Restart scsi clock */
331 * Determine the chip's clock frequency.
333 * This is essential for the negotiation of the synchronous
336 * Note: we have to return the correct value.
337 * THERE IS NO SAFE DEFAULT VALUE.
339 * Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
340 * 53C860 and 53C875 rev. 1 support fast20 transfers but
341 * do not have a clock doubler and so are provided with a
342 * 80 MHz clock. All other fast20 boards incorporate a doubler
343 * and so should be delivered with a 40 MHz clock.
344 * The recent fast40 chips (895/896/895A/1010) use a 40 Mhz base
345 * clock and provide a clock quadrupler (160 Mhz).
349 * calculate SCSI clock frequency (in KHz)
351 static unsigned getfreq (struct sym_hcb *np, int gen)
357 * Measure GEN timer delay in order
358 * to calculate SCSI clock frequency
360 * This code will never execute too
361 * many loop iterations (if DELAY is
362 * reasonably correct). It could get
363 * too low a delay (too high a freq.)
364 * if the CPU is slow executing the
365 * loop for some reason (an NMI, for
366 * example). For this reason we will
367 * if multiple measurements are to be
368 * performed trust the higher delay
369 * (lower frequency returned).
371 OUTW (nc_sien , 0); /* mask all scsi interrupts */
372 (void) INW (nc_sist); /* clear pending scsi interrupt */
373 OUTB (nc_dien , 0); /* mask all dma interrupts */
374 (void) INW (nc_sist); /* another one, just to be sure :) */
376 * The C1010-33 core does not report GEN in SIST,
377 * if this interrupt is masked in SIEN.
378 * I don't know yet if the C1010-66 behaves the same way.
380 if (np->features & FE_C10) {
382 OUTB (nc_istat1, SIRQD);
384 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
385 OUTB (nc_stime1, 0); /* disable general purpose timer */
386 OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
387 while (!(INW(nc_sist) & GEN) && ms++ < 100000)
388 UDELAY (1000/4);/* count in 1/4 of ms */
389 OUTB (nc_stime1, 0); /* disable general purpose timer */
391 * Undo C1010-33 specific settings.
393 if (np->features & FE_C10) {
398 * set prescaler to divide by whatever 0 means
399 * 0 ought to choose divide by 2, but appears
400 * to set divide by 3.5 mode in my 53c810 ...
405 * adjust for prescaler, and convert into KHz
407 f = ms ? ((1 << gen) * (4340*4)) / ms : 0;
410 * The C1010-33 result is biased by a factor
411 * of 2/3 compared to earlier chips.
413 if (np->features & FE_C10)
416 if (sym_verbose >= 2)
417 printf ("%s: Delay (GEN=%d): %u msec, %u KHz\n",
418 sym_name(np), gen, ms/4, f);
423 static unsigned sym_getfreq (struct sym_hcb *np)
428 (void) getfreq (np, gen); /* throw away first result */
429 f1 = getfreq (np, gen);
430 f2 = getfreq (np, gen);
431 if (f1 > f2) f1 = f2; /* trust lower result */
436 * Get/probe chip SCSI clock frequency
438 static void sym_getclock (struct sym_hcb *np, int mult)
440 unsigned char scntl3 = np->sv_scntl3;
441 unsigned char stest1 = np->sv_stest1;
447 * True with 875/895/896/895A with clock multiplier selected
449 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
450 if (sym_verbose >= 2)
451 printf ("%s: clock multiplier found\n", sym_name(np));
452 np->multiplier = mult;
456 * If multiplier not found or scntl3 not 7,5,3,
457 * reset chip and get frequency from general purpose timer.
458 * Otherwise trust scntl3 BIOS setting.
460 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
461 OUTB (nc_stest1, 0); /* make sure doubler is OFF */
462 f1 = sym_getfreq (np);
465 printf ("%s: chip clock is %uKHz\n", sym_name(np), f1);
467 if (f1 < 45000) f1 = 40000;
468 else if (f1 < 55000) f1 = 50000;
471 if (f1 < 80000 && mult > 1) {
472 if (sym_verbose >= 2)
473 printf ("%s: clock multiplier assumed\n",
475 np->multiplier = mult;
478 if ((scntl3 & 7) == 3) f1 = 40000;
479 else if ((scntl3 & 7) == 5) f1 = 80000;
482 f1 /= np->multiplier;
486 * Compute controller synchronous parameters.
488 f1 *= np->multiplier;
493 * Get/probe PCI clock frequency
495 static int sym_getpciclock (struct sym_hcb *np)
500 * For now, we only need to know about the actual
501 * PCI BUS clock frequency for C1010-66 chips.
504 if (np->features & FE_66MHZ) {
508 OUTB (nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */
509 f = (int) sym_getfreq (np);
518 * SYMBIOS chip clock divisor table.
520 * Divisors are multiplied by 10,000,000 in order to make
521 * calculations more simple.
524 static u32 div_10M[] = {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
527 * Get clock factor and sync divisor for a given
528 * synchronous factor period.
531 sym_getsync(struct sym_hcb *np, u_char dt, u_char sfac, u_char *divp, u_char *fakp)
533 u32 clk = np->clock_khz; /* SCSI clock frequency in kHz */
534 int div = np->clock_divn; /* Number of divisors supported */
535 u32 fak; /* Sync factor in sxfer */
536 u32 per; /* Period in tenths of ns */
537 u32 kpc; /* (per * clk) */
541 * Compute the synchronous period in tenths of nano-seconds
543 if (dt && sfac <= 9) per = 125;
544 else if (sfac <= 10) per = 250;
545 else if (sfac == 11) per = 303;
546 else if (sfac == 12) per = 500;
547 else per = 40 * sfac;
555 * For earliest C10 revision 0, we cannot use extra
556 * clocks for the setting of the SCSI clocking.
557 * Note that this limits the lowest sync data transfer
558 * to 5 Mega-transfers per second and may result in
559 * using higher clock divisors.
562 if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) {
564 * Look for the lowest clock divisor that allows an
565 * output speed not faster than the period.
569 if (kpc > (div_10M[div] << 2)) {
574 fak = 0; /* No extra clocks */
575 if (div == np->clock_divn) { /* Are we too fast ? */
585 * Look for the greatest clock divisor that allows an
586 * input speed faster than the period.
589 if (kpc >= (div_10M[div] << 2)) break;
592 * Calculate the lowest clock factor that allows an output
593 * speed not faster than the period, and the max output speed.
594 * If fak >= 1 we will set both XCLKH_ST and XCLKH_DT.
595 * If fak >= 2 we will also set XCLKS_ST and XCLKS_DT.
598 fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2;
599 /* ret = ((2+fak)*div_10M[div])/np->clock_khz; */
601 fak = (kpc - 1) / div_10M[div] + 1 - 4;
602 /* ret = ((4+fak)*div_10M[div])/np->clock_khz; */
606 * Check against our hardware limits, or bugs :).
614 * Compute and return sync parameters.
623 * SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64,
624 * 128 transfers. All chips support at least 16 transfers
625 * bursts. The 825A, 875 and 895 chips support bursts of up
626 * to 128 transfers and the 895A and 896 support bursts of up
627 * to 64 transfers. All other chips support up to 16
630 * For PCI 32 bit data transfers each transfer is a DWORD.
631 * It is a QUADWORD (8 bytes) for PCI 64 bit data transfers.
633 * We use log base 2 (burst length) as internal code, with
634 * value 0 meaning "burst disabled".
638 * Burst length from burst code.
640 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
643 * Burst code from io register bits.
645 #define burst_code(dmode, ctest4, ctest5) \
646 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
649 * Set initial io register bits from burst code.
651 static __inline void sym_init_burst(struct sym_hcb *np, u_char bc)
653 np->rv_ctest4 &= ~0x80;
654 np->rv_dmode &= ~(0x3 << 6);
655 np->rv_ctest5 &= ~0x4;
658 np->rv_ctest4 |= 0x80;
662 np->rv_dmode |= ((bc & 0x3) << 6);
663 np->rv_ctest5 |= (bc & 0x4);
669 * Print out the list of targets that have some flag disabled by user.
671 static void sym_print_targets_flag(struct sym_hcb *np, int mask, char *msg)
676 for (cnt = 0, i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
679 if (np->target[i].usrflags & mask) {
681 printf("%s: %s disabled for targets",
691 * Save initial settings of some IO registers.
692 * Assumed to have been set by BIOS.
693 * We cannot reset the chip prior to reading the
694 * IO registers, since informations will be lost.
695 * Since the SCRIPTS processor may be running, this
696 * is not safe on paper, but it seems to work quite
699 static void sym_save_initial_setting (struct sym_hcb *np)
701 np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
702 np->sv_scntl3 = INB(nc_scntl3) & 0x07;
703 np->sv_dmode = INB(nc_dmode) & 0xce;
704 np->sv_dcntl = INB(nc_dcntl) & 0xa8;
705 np->sv_ctest3 = INB(nc_ctest3) & 0x01;
706 np->sv_ctest4 = INB(nc_ctest4) & 0x80;
707 np->sv_gpcntl = INB(nc_gpcntl);
708 np->sv_stest1 = INB(nc_stest1);
709 np->sv_stest2 = INB(nc_stest2) & 0x20;
710 np->sv_stest4 = INB(nc_stest4);
711 if (np->features & FE_C10) { /* Always large DMA fifo + ultra3 */
712 np->sv_scntl4 = INB(nc_scntl4);
713 np->sv_ctest5 = INB(nc_ctest5) & 0x04;
716 np->sv_ctest5 = INB(nc_ctest5) & 0x24;
720 * Prepare io register values used by sym_start_up()
721 * according to selected and supported features.
723 static int sym_prepare_setting(struct sym_hcb *np, struct sym_nvram *nvram)
732 np->maxwide = (np->features & FE_WIDE)? 1 : 0;
735 * Guess the frequency of the chip's clock.
737 if (np->features & (FE_ULTRA3 | FE_ULTRA2))
738 np->clock_khz = 160000;
739 else if (np->features & FE_ULTRA)
740 np->clock_khz = 80000;
742 np->clock_khz = 40000;
745 * Get the clock multiplier factor.
747 if (np->features & FE_QUAD)
749 else if (np->features & FE_DBLR)
755 * Measure SCSI clock frequency for chips
756 * it may vary from assumed one.
758 if (np->features & FE_VARCLK)
759 sym_getclock(np, np->multiplier);
762 * Divisor to be used for async (timer pre-scaler).
764 i = np->clock_divn - 1;
766 if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) {
774 * The C1010 uses hardwired divisors for async.
775 * So, we just throw away, the async. divisor.:-)
777 if (np->features & FE_C10)
781 * Minimum synchronous period factor supported by the chip.
782 * Btw, 'period' is in tenths of nanoseconds.
784 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
786 if (period <= 250) np->minsync = 10;
787 else if (period <= 303) np->minsync = 11;
788 else if (period <= 500) np->minsync = 12;
789 else np->minsync = (period + 40 - 1) / 40;
792 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
794 if (np->minsync < 25 &&
795 !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
797 else if (np->minsync < 12 &&
798 !(np->features & (FE_ULTRA2|FE_ULTRA3)))
802 * Maximum synchronous period factor supported by the chip.
804 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
805 np->maxsync = period > 2540 ? 254 : period / 10;
808 * If chip is a C1010, guess the sync limits in DT mode.
810 if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) {
811 if (np->clock_khz == 160000) {
814 np->maxoffs_dt = nvram->type ? 62 : 31;
819 * 64 bit addressing (895A/896/1010) ?
821 if (np->features & FE_DAC) {
822 #if SYM_CONF_DMA_ADDRESSING_MODE == 0
823 np->rv_ccntl1 |= (DDAC);
824 #elif SYM_CONF_DMA_ADDRESSING_MODE == 1
826 np->rv_ccntl1 |= (DDAC);
828 np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
829 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
831 np->rv_ccntl1 |= (DDAC);
833 np->rv_ccntl1 |= (0 | EXTIBMV);
838 * Phase mismatch handled by SCRIPTS (895A/896/1010) ?
840 if (np->features & FE_NOPM)
841 np->rv_ccntl0 |= (ENPMJ);
844 * C1010-33 Errata: Part Number:609-039638 (rev. 1) is fixed.
845 * In dual channel mode, contention occurs if internal cycles
846 * are used. Disable internal cycles.
848 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
849 np->revision_id < 0x1)
850 np->rv_ccntl0 |= DILS;
853 * Select burst length (dwords)
855 burst_max = SYM_SETUP_BURST_ORDER;
856 if (burst_max == 255)
857 burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
861 if (burst_max > np->maxburst)
862 burst_max = np->maxburst;
865 * DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2.
866 * This chip and the 860 Rev 1 may wrongly use PCI cache line
867 * based transactions on LOAD/STORE instructions. So we have
868 * to prevent these chips from using such PCI transactions in
869 * this driver. The generic ncr driver that does not use
870 * LOAD/STORE instructions does not need this work-around.
872 if ((np->device_id == PCI_DEVICE_ID_NCR_53C810 &&
873 np->revision_id >= 0x10 && np->revision_id <= 0x11) ||
874 (np->device_id == PCI_DEVICE_ID_NCR_53C860 &&
875 np->revision_id <= 0x1))
876 np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
879 * Select all supported special features.
880 * If we are using on-board RAM for scripts, prefetch (PFEN)
881 * does not help, but burst op fetch (BOF) does.
882 * Disabling PFEN makes sure BOF will be used.
884 if (np->features & FE_ERL)
885 np->rv_dmode |= ERL; /* Enable Read Line */
886 if (np->features & FE_BOF)
887 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
888 if (np->features & FE_ERMP)
889 np->rv_dmode |= ERMP; /* Enable Read Multiple */
891 if ((np->features & FE_PFEN) && !np->ram_ba)
893 if (np->features & FE_PFEN)
895 np->rv_dcntl |= PFEN; /* Prefetch Enable */
896 if (np->features & FE_CLSE)
897 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
898 if (np->features & FE_WRIE)
899 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
900 if (np->features & FE_DFS)
901 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
906 if (SYM_SETUP_PCI_PARITY)
907 np->rv_ctest4 |= MPEE; /* Master parity checking */
908 if (SYM_SETUP_SCSI_PARITY)
909 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
912 * Get parity checking, host ID and verbose mode from NVRAM
915 sym_nvram_setup_host (np, nvram);
918 * Get SCSI addr of host adapter (set by bios?).
920 if (np->myaddr == 255) {
921 np->myaddr = INB(nc_scid) & 0x07;
923 np->myaddr = SYM_SETUP_HOST_ID;
927 * Prepare initial io register bits for burst length
929 sym_init_burst(np, burst_max);
933 * - LVD capable chips (895/895A/896/1010) report the
934 * current BUS mode through the STEST4 IO register.
935 * - For previous generation chips (825/825A/875),
936 * user has to tell us how to check against HVD,
937 * since a 100% safe algorithm is not possible.
939 np->scsi_mode = SMODE_SE;
940 if (np->features & (FE_ULTRA2|FE_ULTRA3))
941 np->scsi_mode = (np->sv_stest4 & SMODE);
942 else if (np->features & FE_DIFF) {
943 if (SYM_SETUP_SCSI_DIFF == 1) {
945 if (np->sv_stest2 & 0x20)
946 np->scsi_mode = SMODE_HVD;
948 else if (nvram->type == SYM_SYMBIOS_NVRAM) {
949 if (!(INB(nc_gpreg) & 0x08))
950 np->scsi_mode = SMODE_HVD;
953 else if (SYM_SETUP_SCSI_DIFF == 2)
954 np->scsi_mode = SMODE_HVD;
956 if (np->scsi_mode == SMODE_HVD)
957 np->rv_stest2 |= 0x20;
960 * Set LED support from SCRIPTS.
961 * Ignore this feature for boards known to use a
962 * specific GPIO wiring and for the 895A, 896
963 * and 1010 that drive the LED directly.
965 if ((SYM_SETUP_SCSI_LED ||
966 (nvram->type == SYM_SYMBIOS_NVRAM ||
967 (nvram->type == SYM_TEKRAM_NVRAM &&
968 np->device_id == PCI_DEVICE_ID_NCR_53C895))) &&
969 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
970 np->features |= FE_LED0;
975 switch(SYM_SETUP_IRQ_MODE & 3) {
977 np->rv_dcntl |= IRQM;
980 np->rv_dcntl |= (np->sv_dcntl & IRQM);
987 * Configure targets according to driver setup.
988 * If NVRAM present get targets setup from NVRAM.
990 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
991 tcb_p tp = &np->target[i];
993 tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
994 tp->usrtags = SYM_SETUP_MAX_TAG;
996 sym_nvram_setup_target (np, i, nvram);
999 tp->usrflags &= ~SYM_TAGS_ENABLED;
1003 * Let user know about the settings.
1006 printf("%s: %s NVRAM, ID %d, Fast-%d, %s, %s\n", sym_name(np),
1007 i == SYM_SYMBIOS_NVRAM ? "Symbios" :
1008 (i == SYM_TEKRAM_NVRAM ? "Tekram" : "No"),
1010 (np->features & FE_ULTRA3) ? 80 :
1011 (np->features & FE_ULTRA2) ? 40 :
1012 (np->features & FE_ULTRA) ? 20 : 10,
1013 sym_scsi_bus_mode(np->scsi_mode),
1014 (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
1016 * Tell him more on demand.
1019 printf("%s: %s IRQ line driver%s\n",
1021 np->rv_dcntl & IRQM ? "totem pole" : "open drain",
1022 np->ram_ba ? ", using on-chip SRAM" : "");
1023 printf("%s: using %s firmware.\n", sym_name(np), np->fw_name);
1024 if (np->features & FE_NOPM)
1025 printf("%s: handling phase mismatch from SCRIPTS.\n",
1031 if (sym_verbose >= 2) {
1032 printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
1033 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
1034 sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
1035 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
1037 printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
1038 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
1039 sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
1040 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
1043 * Let user be aware of targets that have some disable flags set.
1045 sym_print_targets_flag(np, SYM_SCAN_BOOT_DISABLED, "SCAN AT BOOT");
1047 sym_print_targets_flag(np, SYM_SCAN_LUNS_DISABLED,
1054 * Test the pci bus snoop logic :-(
1056 * Has to be called with interrupts disabled.
1058 #ifndef SYM_CONF_IOMAPPED
1059 static int sym_regtest (struct sym_hcb *np)
1061 register volatile u32 data;
1063 * chip registers may NOT be cached.
1064 * write 0xffffffff to a read only register area,
1065 * and try to read it back.
1068 OUTL_OFF(offsetof(struct sym_reg, nc_dstat), data);
1069 data = INL_OFF(offsetof(struct sym_reg, nc_dstat));
1071 if (data == 0xffffffff) {
1073 if ((data & 0xe2f0fffd) != 0x02000080) {
1075 printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
1083 static int sym_snooptest (struct sym_hcb *np)
1085 u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
1087 #ifndef SYM_CONF_IOMAPPED
1088 err |= sym_regtest (np);
1089 if (err) return (err);
1093 * Enable Master Parity Checking as we intend
1094 * to enable it for normal operations.
1096 OUTB (nc_ctest4, (np->rv_ctest4 & MPEE));
1100 pc = SCRIPTZ_BA (np, snooptest);
1104 * Set memory and register.
1106 np->scratch = cpu_to_scr(host_wr);
1107 OUTL (nc_temp, sym_wr);
1109 * Start script (exchange values)
1111 OUTL (nc_dsa, np->hcb_ba);
1114 * Wait 'til done (with timeout)
1116 for (i=0; i<SYM_SNOOP_TIMEOUT; i++)
1117 if (INB(nc_istat) & (INTF|SIP|DIP))
1119 if (i>=SYM_SNOOP_TIMEOUT) {
1120 printf ("CACHE TEST FAILED: timeout.\n");
1124 * Check for fatal DMA errors.
1126 dstat = INB (nc_dstat);
1127 #if 1 /* Band aiding for broken hardwares that fail PCI parity */
1128 if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
1129 printf ("%s: PCI DATA PARITY ERROR DETECTED - "
1130 "DISABLING MASTER DATA PARITY CHECKING.\n",
1132 np->rv_ctest4 &= ~MPEE;
1136 if (dstat & (MDPE|BF|IID)) {
1137 printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
1141 * Save termination position.
1145 * Read memory and register.
1147 host_rd = scr_to_cpu(np->scratch);
1148 sym_rd = INL (nc_scratcha);
1149 sym_bk = INL (nc_temp);
1151 * Check termination position.
1153 if (pc != SCRIPTZ_BA (np, snoopend)+8) {
1154 printf ("CACHE TEST FAILED: script execution failed.\n");
1155 printf ("start=%08lx, pc=%08lx, end=%08lx\n",
1156 (u_long) SCRIPTZ_BA (np, snooptest), (u_long) pc,
1157 (u_long) SCRIPTZ_BA (np, snoopend) +8);
1163 if (host_wr != sym_rd) {
1164 printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n",
1165 (int) host_wr, (int) sym_rd);
1168 if (host_rd != sym_wr) {
1169 printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n",
1170 (int) sym_wr, (int) host_rd);
1173 if (sym_bk != sym_wr) {
1174 printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n",
1175 (int) sym_wr, (int) sym_bk);
1183 * log message for real hard errors
1185 * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sx/s3/s4) @ name (dsp:dbc).
1186 * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf.
1188 * exception register:
1193 * so: control lines as driven by chip.
1194 * si: control lines as seen by chip.
1195 * sd: scsi data lines as seen by chip.
1198 * sx: sxfer (see the manual)
1199 * s3: scntl3 (see the manual)
1200 * s4: scntl4 (see the manual)
1202 * current script command:
1203 * dsp: script address (relative to start of script).
1204 * dbc: first word of script command.
1206 * First 24 register of the chip:
1209 static void sym_log_hard_error(struct sym_hcb *np, u_short sist, u_char dstat)
1215 u_char *script_base;
1220 if (dsp > np->scripta_ba &&
1221 dsp <= np->scripta_ba + np->scripta_sz) {
1222 script_ofs = dsp - np->scripta_ba;
1223 script_size = np->scripta_sz;
1224 script_base = (u_char *) np->scripta0;
1225 script_name = "scripta";
1227 else if (np->scriptb_ba < dsp &&
1228 dsp <= np->scriptb_ba + np->scriptb_sz) {
1229 script_ofs = dsp - np->scriptb_ba;
1230 script_size = np->scriptb_sz;
1231 script_base = (u_char *) np->scriptb0;
1232 script_name = "scriptb";
1237 script_name = "mem";
1240 printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x/%x) @ (%s %x:%08x).\n",
1241 sym_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
1242 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl),
1243 (unsigned)INB (nc_sbdl), (unsigned)INB (nc_sxfer),
1244 (unsigned)INB (nc_scntl3),
1245 (np->features & FE_C10) ? (unsigned)INB (nc_scntl4) : 0,
1246 script_name, script_ofs, (unsigned)INL (nc_dbc));
1248 if (((script_ofs & 3) == 0) &&
1249 (unsigned)script_ofs < script_size) {
1250 printf ("%s: script cmd = %08x\n", sym_name(np),
1251 scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
1254 printf ("%s: regdump:", sym_name(np));
1256 printf (" %02x", (unsigned)INB_OFF(i));
1262 if (dstat & (MDPE|BF))
1263 sym_log_bus_error(np);
1266 static struct sym_pci_chip sym_pci_dev_table[] = {
1267 {PCI_DEVICE_ID_NCR_53C810, 0x0f, "810", 4, 8, 4, 64,
1270 #ifdef SYM_DEBUG_GENERIC_SUPPORT
1271 {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
1275 {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
1276 FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
1279 {PCI_DEVICE_ID_NCR_53C815, 0xff, "815", 4, 8, 4, 64,
1282 {PCI_DEVICE_ID_NCR_53C825, 0x0f, "825", 6, 8, 4, 64,
1283 FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
1285 {PCI_DEVICE_ID_NCR_53C825, 0xff, "825a", 6, 8, 4, 2,
1286 FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
1288 {PCI_DEVICE_ID_NCR_53C860, 0xff, "860", 4, 8, 5, 1,
1289 FE_ULTRA|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
1291 {PCI_DEVICE_ID_NCR_53C875, 0x01, "875", 6, 16, 5, 2,
1292 FE_WIDE|FE_ULTRA|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1293 FE_RAM|FE_DIFF|FE_VARCLK}
1295 {PCI_DEVICE_ID_NCR_53C875, 0xff, "875", 6, 16, 5, 2,
1296 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1297 FE_RAM|FE_DIFF|FE_VARCLK}
1299 {PCI_DEVICE_ID_NCR_53C875J, 0xff, "875J", 6, 16, 5, 2,
1300 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1301 FE_RAM|FE_DIFF|FE_VARCLK}
1303 {PCI_DEVICE_ID_NCR_53C885, 0xff, "885", 6, 16, 5, 2,
1304 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1305 FE_RAM|FE_DIFF|FE_VARCLK}
1307 #ifdef SYM_DEBUG_GENERIC_SUPPORT
1308 {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
1309 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
1313 {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
1314 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1318 {PCI_DEVICE_ID_NCR_53C896, 0xff, "896", 6, 31, 7, 4,
1319 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1320 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1322 {PCI_DEVICE_ID_LSI_53C895A, 0xff, "895a", 6, 31, 7, 4,
1323 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1324 FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1326 {PCI_DEVICE_ID_LSI_53C875A, 0xff, "875a", 6, 31, 7, 4,
1327 FE_WIDE|FE_ULTRA|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1328 FE_RAM|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
1330 {PCI_DEVICE_ID_LSI_53C1010_33, 0x00, "1010-33", 6, 31, 7, 8,
1331 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1332 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
1335 {PCI_DEVICE_ID_LSI_53C1010_33, 0xff, "1010-33", 6, 31, 7, 8,
1336 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1337 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
1340 {PCI_DEVICE_ID_LSI_53C1010_66, 0xff, "1010-66", 6, 31, 7, 8,
1341 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
1342 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
1345 {PCI_DEVICE_ID_LSI_53C1510, 0xff, "1510d", 6, 31, 7, 4,
1346 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
1347 FE_RAM|FE_IO256|FE_LEDC}
1350 #define sym_pci_num_devs \
1351 (sizeof(sym_pci_dev_table) / sizeof(sym_pci_dev_table[0]))
1354 * Look up the chip table.
1356 * Return a pointer to the chip entry if found,
1359 struct sym_pci_chip *
1360 sym_lookup_pci_chip_table (u_short device_id, u_char revision)
1362 struct sym_pci_chip *chip;
1365 for (i = 0; i < sym_pci_num_devs; i++) {
1366 chip = &sym_pci_dev_table[i];
1367 if (device_id != chip->device_id)
1369 if (revision > chip->revision_id)
1377 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1379 * Lookup the 64 bit DMA segments map.
1380 * This is only used if the direct mapping
1381 * has been unsuccessful.
1383 int sym_lookup_dmap(struct sym_hcb *np, u32 h, int s)
1390 /* Look up existing mappings */
1391 for (i = SYM_DMAP_SIZE-1; i > 0; i--) {
1392 if (h == np->dmap_bah[i])
1395 /* If direct mapping is free, get it */
1396 if (!np->dmap_bah[s])
1398 /* Collision -> lookup free mappings */
1399 for (s = SYM_DMAP_SIZE-1; s > 0; s--) {
1400 if (!np->dmap_bah[s])
1404 panic("sym: ran out of 64 bit DMA segment registers");
1407 np->dmap_bah[s] = h;
1413 * Update IO registers scratch C..R so they will be
1414 * in sync. with queued CCB expectations.
1416 static void sym_update_dmap_regs(struct sym_hcb *np)
1420 if (!np->dmap_dirty)
1422 o = offsetof(struct sym_reg, nc_scrx[0]);
1423 for (i = 0; i < SYM_DMAP_SIZE; i++) {
1424 OUTL_OFF(o, np->dmap_bah[i]);
1431 /* Enforce all the fiddly SPI rules and the chip limitations */
1432 static void sym_check_goals(struct scsi_device *sdev)
1434 struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
1435 struct sym_trans *st = &np->target[sdev->id].tinfo.goal;
1437 if (!scsi_device_wide(sdev))
1440 if (!scsi_device_sync(sdev)) {
1447 if (scsi_device_dt(sdev)) {
1448 if (scsi_device_dt_only(sdev))
1449 st->options |= PPR_OPT_DT;
1451 if (st->offset == 0)
1452 st->options &= ~PPR_OPT_DT;
1454 st->options &= ~PPR_OPT_DT;
1457 /* Some targets fail to properly negotiate DT in SE mode */
1458 if ((np->scsi_mode != SMODE_LVD) || !(np->features & FE_U3EN))
1459 st->options &= ~PPR_OPT_DT;
1461 if (st->options & PPR_OPT_DT) {
1462 /* all DT transfers must be wide */
1464 if (st->offset > np->maxoffs_dt)
1465 st->offset = np->maxoffs_dt;
1466 if (st->period < np->minsync_dt)
1467 st->period = np->minsync_dt;
1468 if (st->period > np->maxsync_dt)
1469 st->period = np->maxsync_dt;
1471 st->options &= ~PPR_OPT_MASK;
1472 if (st->offset > np->maxoffs)
1473 st->offset = np->maxoffs;
1474 if (st->period < np->minsync)
1475 st->period = np->minsync;
1476 if (st->period > np->maxsync)
1477 st->period = np->maxsync;
1482 * Prepare the next negotiation message if needed.
1484 * Fill in the part of message buffer that contains the
1485 * negotiation and the nego_status field of the CCB.
1486 * Returns the size of the message in bytes.
1488 static int sym_prepare_nego(struct sym_hcb *np, ccb_p cp, u_char *msgptr)
1490 tcb_p tp = &np->target[cp->target];
1491 struct scsi_device *sdev = tp->sdev;
1492 struct sym_trans *goal = &tp->tinfo.goal;
1493 struct sym_trans *curr = &tp->tinfo.curr;
1498 sym_check_goals(sdev);
1501 * Many devices implement PPR in a buggy way, so only use it if we
1504 if ((goal->options & PPR_OPT_MASK) || (goal->period < 0xa)) {
1506 } else if (curr->width != goal->width) {
1508 } else if (curr->period != goal->period ||
1509 curr->offset != goal->offset) {
1517 msgptr[msglen++] = M_EXTENDED;
1518 msgptr[msglen++] = 3;
1519 msgptr[msglen++] = M_X_SYNC_REQ;
1520 msgptr[msglen++] = goal->period;
1521 msgptr[msglen++] = goal->offset;
1524 msgptr[msglen++] = M_EXTENDED;
1525 msgptr[msglen++] = 2;
1526 msgptr[msglen++] = M_X_WIDE_REQ;
1527 msgptr[msglen++] = goal->width;
1530 msgptr[msglen++] = M_EXTENDED;
1531 msgptr[msglen++] = 6;
1532 msgptr[msglen++] = M_X_PPR_REQ;
1533 msgptr[msglen++] = goal->period;
1534 msgptr[msglen++] = 0;
1535 msgptr[msglen++] = goal->offset;
1536 msgptr[msglen++] = goal->width;
1537 msgptr[msglen++] = goal->options & PPR_OPT_MASK;
1541 cp->nego_status = nego;
1544 tp->nego_cp = cp; /* Keep track a nego will be performed */
1545 if (DEBUG_FLAGS & DEBUG_NEGO) {
1546 sym_print_nego_msg(np, cp->target,
1547 nego == NS_SYNC ? "sync msgout" :
1548 nego == NS_WIDE ? "wide msgout" :
1549 "ppr msgout", msgptr);
1557 * Insert a job into the start queue.
1559 void sym_put_start_queue(struct sym_hcb *np, ccb_p cp)
1563 #ifdef SYM_CONF_IARB_SUPPORT
1565 * If the previously queued CCB is not yet done,
1566 * set the IARB hint. The SCRIPTS will go with IARB
1567 * for this job when starting the previous one.
1568 * We leave devices a chance to win arbitration by
1569 * not using more than 'iarb_max' consecutive
1570 * immediate arbitrations.
1572 if (np->last_cp && np->iarb_count < np->iarb_max) {
1573 np->last_cp->host_flags |= HF_HINT_IARB;
1581 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1583 * Make SCRIPTS aware of the 64 bit DMA
1584 * segment registers not being up-to-date.
1587 cp->host_xflags |= HX_DMAP_DIRTY;
1591 * Insert first the idle task and then our job.
1592 * The MBs should ensure proper ordering.
1594 qidx = np->squeueput + 2;
1595 if (qidx >= MAX_QUEUE*2) qidx = 0;
1597 np->squeue [qidx] = cpu_to_scr(np->idletask_ba);
1598 MEMORY_WRITE_BARRIER();
1599 np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba);
1601 np->squeueput = qidx;
1603 if (DEBUG_FLAGS & DEBUG_QUEUE)
1604 printf ("%s: queuepos=%d.\n", sym_name (np), np->squeueput);
1607 * Script processor may be waiting for reselect.
1610 MEMORY_WRITE_BARRIER();
1611 OUTB (nc_istat, SIGP|np->istat_sem);
1614 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1616 * Start next ready-to-start CCBs.
1618 void sym_start_next_ccbs(struct sym_hcb *np, lcb_p lp, int maxn)
1624 * Paranoia, as usual. :-)
1626 assert(!lp->started_tags || !lp->started_no_tag);
1629 * Try to start as many commands as asked by caller.
1630 * Prevent from having both tagged and untagged
1631 * commands queued to the device at the same time.
1634 qp = sym_remque_head(&lp->waiting_ccbq);
1637 cp = sym_que_entry(qp, struct sym_ccb, link2_ccbq);
1638 if (cp->tag != NO_TAG) {
1639 if (lp->started_no_tag ||
1640 lp->started_tags >= lp->started_max) {
1641 sym_insque_head(qp, &lp->waiting_ccbq);
1644 lp->itlq_tbl[cp->tag] = cpu_to_scr(cp->ccb_ba);
1646 cpu_to_scr(SCRIPTA_BA (np, resel_tag));
1649 if (lp->started_no_tag || lp->started_tags) {
1650 sym_insque_head(qp, &lp->waiting_ccbq);
1653 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
1655 cpu_to_scr(SCRIPTA_BA (np, resel_no_tag));
1656 ++lp->started_no_tag;
1659 sym_insque_tail(qp, &lp->started_ccbq);
1660 sym_put_start_queue(np, cp);
1663 #endif /* SYM_OPT_HANDLE_DEVICE_QUEUEING */
1666 * The chip may have completed jobs. Look at the DONE QUEUE.
1668 * On paper, memory read barriers may be needed here to
1669 * prevent out of order LOADs by the CPU from having
1670 * prefetched stale data prior to DMA having occurred.
1672 static int sym_wakeup_done (struct sym_hcb *np)
1681 /* MEMORY_READ_BARRIER(); */
1683 dsa = scr_to_cpu(np->dqueue[i]);
1687 if ((i = i+2) >= MAX_QUEUE*2)
1690 cp = sym_ccb_from_dsa(np, dsa);
1692 MEMORY_READ_BARRIER();
1693 sym_complete_ok (np, cp);
1697 printf ("%s: bad DSA (%x) in done queue.\n",
1698 sym_name(np), (u_int) dsa);
1706 * Complete all CCBs queued to the COMP queue.
1708 * These CCBs are assumed:
1709 * - Not to be referenced either by devices or
1710 * SCRIPTS-related queues and datas.
1711 * - To have to be completed with an error condition
1714 * The device queue freeze count is incremented
1715 * for each CCB that does not prevent this.
1716 * This function is called when all CCBs involved
1717 * in error handling/recovery have been reaped.
1719 static void sym_flush_comp_queue(struct sym_hcb *np, int cam_status)
1724 while ((qp = sym_remque_head(&np->comp_ccbq)) != 0) {
1725 struct scsi_cmnd *ccb;
1726 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
1727 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
1728 /* Leave quiet CCBs waiting for resources */
1729 if (cp->host_status == HS_WAIT)
1733 sym_set_cam_status(ccb, cam_status);
1734 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
1735 if (sym_get_cam_status(ccb) == CAM_REQUEUE_REQ) {
1736 tcb_p tp = &np->target[cp->target];
1737 lcb_p lp = sym_lp(np, tp, cp->lun);
1739 sym_remque(&cp->link2_ccbq);
1740 sym_insque_tail(&cp->link2_ccbq,
1743 if (cp->tag != NO_TAG)
1746 --lp->started_no_tag;
1753 sym_free_ccb(np, cp);
1754 sym_freeze_cam_ccb(ccb);
1755 sym_xpt_done(np, ccb);
1760 * Complete all active CCBs with error.
1761 * Used on CHIP/SCSI RESET.
1763 static void sym_flush_busy_queue (struct sym_hcb *np, int cam_status)
1766 * Move all active CCBs to the COMP queue
1767 * and flush this queue.
1769 sym_que_splice(&np->busy_ccbq, &np->comp_ccbq);
1770 sym_que_init(&np->busy_ccbq);
1771 sym_flush_comp_queue(np, cam_status);
1778 * 0: initialisation.
1779 * 1: SCSI BUS RESET delivered or received.
1780 * 2: SCSI BUS MODE changed.
1782 void sym_start_up (struct sym_hcb *np, int reason)
1788 * Reset chip if asked, otherwise just clear fifos.
1793 OUTB (nc_stest3, TE|CSF);
1794 OUTONB (nc_ctest3, CLF);
1800 phys = np->squeue_ba;
1801 for (i = 0; i < MAX_QUEUE*2; i += 2) {
1802 np->squeue[i] = cpu_to_scr(np->idletask_ba);
1803 np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
1805 np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
1808 * Start at first entry.
1815 phys = np->dqueue_ba;
1816 for (i = 0; i < MAX_QUEUE*2; i += 2) {
1818 np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
1820 np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
1823 * Start at first entry.
1828 * Install patches in scripts.
1829 * This also let point to first position the start
1830 * and done queue pointers used from SCRIPTS.
1835 * Wakeup all pending jobs.
1837 sym_flush_busy_queue(np, CAM_SCSI_BUS_RESET);
1842 OUTB (nc_istat, 0x00 ); /* Remove Reset, abort */
1843 UDELAY (2000); /* The 895 needs time for the bus mode to settle */
1845 OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
1846 /* full arb., ena parity, par->ATN */
1847 OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
1849 sym_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
1851 OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
1852 OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */
1853 OUTB (nc_istat , SIGP ); /* Signal Process */
1854 OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */
1855 OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
1857 OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
1858 OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */
1859 OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */
1861 /* Extended Sreq/Sack filtering not supported on the C10 */
1862 if (np->features & FE_C10)
1863 OUTB (nc_stest2, np->rv_stest2);
1865 OUTB (nc_stest2, EXT|np->rv_stest2);
1867 OUTB (nc_stest3, TE); /* TolerANT enable */
1868 OUTB (nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */
1871 * For now, disable AIP generation on C1010-66.
1873 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)
1874 OUTB (nc_aipcntl1, DISAIP);
1877 * C10101 rev. 0 errata.
1878 * Errant SGE's when in narrow. Write bits 4 & 5 of
1879 * STEST1 register to disable SGE. We probably should do
1880 * that from SCRIPTS for each selection/reselection, but
1881 * I just don't want. :)
1883 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
1884 np->revision_id < 1)
1885 OUTB (nc_stest1, INB(nc_stest1) | 0x30);
1888 * DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
1889 * Disable overlapped arbitration for some dual function devices,
1890 * regardless revision id (kind of post-chip-design feature. ;-))
1892 if (np->device_id == PCI_DEVICE_ID_NCR_53C875)
1893 OUTB (nc_ctest0, (1<<5));
1894 else if (np->device_id == PCI_DEVICE_ID_NCR_53C896)
1895 np->rv_ccntl0 |= DPR;
1898 * Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing
1899 * and/or hardware phase mismatch, since only such chips
1900 * seem to support those IO registers.
1902 if (np->features & (FE_DAC|FE_NOPM)) {
1903 OUTB (nc_ccntl0, np->rv_ccntl0);
1904 OUTB (nc_ccntl1, np->rv_ccntl1);
1907 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
1909 * Set up scratch C and DRS IO registers to map the 32 bit
1910 * DMA address range our data structures are located in.
1913 np->dmap_bah[0] = 0; /* ??? */
1914 OUTL (nc_scrx[0], np->dmap_bah[0]);
1915 OUTL (nc_drs, np->dmap_bah[0]);
1920 * If phase mismatch handled by scripts (895A/896/1010),
1921 * set PM jump addresses.
1923 if (np->features & FE_NOPM) {
1924 OUTL (nc_pmjad1, SCRIPTB_BA (np, pm_handle));
1925 OUTL (nc_pmjad2, SCRIPTB_BA (np, pm_handle));
1929 * Enable GPIO0 pin for writing if LED support from SCRIPTS.
1930 * Also set GPIO5 and clear GPIO6 if hardware LED control.
1932 if (np->features & FE_LED0)
1933 OUTB(nc_gpcntl, INB(nc_gpcntl) & ~0x01);
1934 else if (np->features & FE_LEDC)
1935 OUTB(nc_gpcntl, (INB(nc_gpcntl) & ~0x41) | 0x20);
1940 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
1941 OUTB (nc_dien , MDPE|BF|SSI|SIR|IID);
1944 * For 895/6 enable SBMC interrupt and save current SCSI bus mode.
1945 * Try to eat the spurious SBMC interrupt that may occur when
1946 * we reset the chip but not the SCSI BUS (at initialization).
1948 if (np->features & (FE_ULTRA2|FE_ULTRA3)) {
1949 OUTONW (nc_sien, SBMC);
1954 np->scsi_mode = INB (nc_stest4) & SMODE;
1958 * Fill in target structure.
1959 * Reinitialize usrsync.
1960 * Reinitialize usrwide.
1961 * Prepare sync negotiation according to actual SCSI bus mode.
1963 for (i=0;i<SYM_CONF_MAX_TARGET;i++) {
1964 tcb_p tp = &np->target[i];
1968 tp->head.wval = np->rv_scntl3;
1971 tp->tinfo.curr.period = 0;
1972 tp->tinfo.curr.offset = 0;
1973 tp->tinfo.curr.width = BUS_8_BIT;
1974 tp->tinfo.curr.options = 0;
1978 * Download SCSI SCRIPTS to on-chip RAM if present,
1979 * and start script processor.
1980 * We do the download preferently from the CPU.
1981 * For platforms that may not support PCI memory mapping,
1982 * we use simple SCRIPTS that performs MEMORY MOVEs.
1985 if (sym_verbose >= 2)
1986 printf ("%s: Downloading SCSI SCRIPTS.\n",
1988 if (np->ram_ws == 8192) {
1989 OUTRAM_OFF(4096, np->scriptb0, np->scriptb_sz);
1990 phys = scr_to_cpu(np->scr_ram_seg);
1991 OUTL (nc_mmws, phys);
1992 OUTL (nc_mmrs, phys);
1993 OUTL (nc_sfs, phys);
1994 phys = SCRIPTB_BA (np, start64);
1997 phys = SCRIPTA_BA (np, init);
1998 OUTRAM_OFF(0, np->scripta0, np->scripta_sz);
2001 phys = SCRIPTA_BA (np, init);
2005 OUTL (nc_dsa, np->hcb_ba);
2009 * Notify the XPT about the RESET condition.
2012 sym_xpt_async_bus_reset(np);
2016 * Switch trans mode for current job and it's target.
2018 static void sym_settrans(struct sym_hcb *np, int target, u_char opts, u_char ofs,
2019 u_char per, u_char wide, u_char div, u_char fak)
2022 u_char sval, wval, uval;
2023 tcb_p tp = &np->target[target];
2025 assert(target == (INB (nc_sdid) & 0x0f));
2027 sval = tp->head.sval;
2028 wval = tp->head.wval;
2029 uval = tp->head.uval;
2032 printf("XXXX sval=%x wval=%x uval=%x (%x)\n",
2033 sval, wval, uval, np->rv_scntl3);
2038 if (!(np->features & FE_C10))
2039 sval = (sval & ~0x1f) | ofs;
2041 sval = (sval & ~0x3f) | ofs;
2044 * Set the sync divisor and extra clock factor.
2047 wval = (wval & ~0x70) | ((div+1) << 4);
2048 if (!(np->features & FE_C10))
2049 sval = (sval & ~0xe0) | (fak << 5);
2051 uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT);
2052 if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT);
2053 if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT);
2058 * Set the bus width.
2065 * Set misc. ultra enable bits.
2067 if (np->features & FE_C10) {
2068 uval = uval & ~(U3EN|AIPCKEN);
2070 assert(np->features & FE_U3EN);
2075 wval = wval & ~ULTRA;
2076 if (per <= 12) wval |= ULTRA;
2080 * Stop there if sync parameters are unchanged.
2082 if (tp->head.sval == sval &&
2083 tp->head.wval == wval &&
2084 tp->head.uval == uval)
2086 tp->head.sval = sval;
2087 tp->head.wval = wval;
2088 tp->head.uval = uval;
2091 * Disable extended Sreq/Sack filtering if per < 50.
2092 * Not supported on the C1010.
2094 if (per < 50 && !(np->features & FE_C10))
2095 OUTOFFB (nc_stest2, EXT);
2098 * set actual value and sync_status
2100 OUTB (nc_sxfer, tp->head.sval);
2101 OUTB (nc_scntl3, tp->head.wval);
2103 if (np->features & FE_C10) {
2104 OUTB (nc_scntl4, tp->head.uval);
2108 * patch ALL busy ccbs of this target.
2110 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
2112 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
2113 if (cp->target != target)
2115 cp->phys.select.sel_scntl3 = tp->head.wval;
2116 cp->phys.select.sel_sxfer = tp->head.sval;
2117 if (np->features & FE_C10) {
2118 cp->phys.select.sel_scntl4 = tp->head.uval;
2124 * We received a WDTR.
2125 * Let everything be aware of the changes.
2127 static void sym_setwide(struct sym_hcb *np, int target, u_char wide)
2129 tcb_p tp = &np->target[target];
2131 sym_settrans(np, target, 0, 0, 0, wide, 0, 0);
2133 tp->tinfo.goal.width = tp->tinfo.curr.width = wide;
2134 tp->tinfo.curr.offset = 0;
2135 tp->tinfo.curr.period = 0;
2136 tp->tinfo.curr.options = 0;
2138 sym_xpt_async_nego_wide(np, target);
2142 * We received a SDTR.
2143 * Let everything be aware of the changes.
2146 sym_setsync(struct sym_hcb *np, int target,
2147 u_char ofs, u_char per, u_char div, u_char fak)
2149 tcb_p tp = &np->target[target];
2150 u_char wide = (tp->head.wval & EWS) ? BUS_16_BIT : BUS_8_BIT;
2152 sym_settrans(np, target, 0, ofs, per, wide, div, fak);
2154 tp->tinfo.curr.period = per;
2155 tp->tinfo.curr.offset = ofs;
2156 tp->tinfo.curr.options = 0;
2158 if (!(tp->tinfo.goal.options & PPR_OPT_MASK)) {
2159 tp->tinfo.goal.period = per;
2160 tp->tinfo.goal.offset = ofs;
2161 tp->tinfo.goal.options = 0;
2164 sym_xpt_async_nego_sync(np, target);
2168 * We received a PPR.
2169 * Let everything be aware of the changes.
2172 sym_setpprot(struct sym_hcb *np, int target, u_char opts, u_char ofs,
2173 u_char per, u_char wide, u_char div, u_char fak)
2175 tcb_p tp = &np->target[target];
2177 sym_settrans(np, target, opts, ofs, per, wide, div, fak);
2179 tp->tinfo.goal.width = tp->tinfo.curr.width = wide;
2180 tp->tinfo.goal.period = tp->tinfo.curr.period = per;
2181 tp->tinfo.goal.offset = tp->tinfo.curr.offset = ofs;
2182 tp->tinfo.goal.options = tp->tinfo.curr.options = opts;
2184 sym_xpt_async_nego_ppr(np, target);
2188 * generic recovery from scsi interrupt
2190 * The doc says that when the chip gets an SCSI interrupt,
2191 * it tries to stop in an orderly fashion, by completing
2192 * an instruction fetch that had started or by flushing
2193 * the DMA fifo for a write to memory that was executing.
2194 * Such a fashion is not enough to know if the instruction
2195 * that was just before the current DSP value has been
2198 * There are some small SCRIPTS sections that deal with
2199 * the start queue and the done queue that may break any
2200 * assomption from the C code if we are interrupted
2201 * inside, so we reset if this happens. Btw, since these
2202 * SCRIPTS sections are executed while the SCRIPTS hasn't
2203 * started SCSI operations, it is very unlikely to happen.
2205 * All the driver data structures are supposed to be
2206 * allocated from the same 4 GB memory window, so there
2207 * is a 1 to 1 relationship between DSA and driver data
2208 * structures. Since we are careful :) to invalidate the
2209 * DSA when we complete a command or when the SCRIPTS
2210 * pushes a DSA into a queue, we can trust it when it
2213 static void sym_recover_scsi_int (struct sym_hcb *np, u_char hsts)
2215 u32 dsp = INL (nc_dsp);
2216 u32 dsa = INL (nc_dsa);
2217 ccb_p cp = sym_ccb_from_dsa(np, dsa);
2220 * If we haven't been interrupted inside the SCRIPTS
2221 * critical pathes, we can safely restart the SCRIPTS
2222 * and trust the DSA value if it matches a CCB.
2224 if ((!(dsp > SCRIPTA_BA (np, getjob_begin) &&
2225 dsp < SCRIPTA_BA (np, getjob_end) + 1)) &&
2226 (!(dsp > SCRIPTA_BA (np, ungetjob) &&
2227 dsp < SCRIPTA_BA (np, reselect) + 1)) &&
2228 (!(dsp > SCRIPTB_BA (np, sel_for_abort) &&
2229 dsp < SCRIPTB_BA (np, sel_for_abort_1) + 1)) &&
2230 (!(dsp > SCRIPTA_BA (np, done) &&
2231 dsp < SCRIPTA_BA (np, done_end) + 1))) {
2232 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
2233 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
2235 * If we have a CCB, let the SCRIPTS call us back for
2236 * the handling of the error with SCRATCHA filled with
2237 * STARTPOS. This way, we will be able to freeze the
2238 * device queue and requeue awaiting IOs.
2241 cp->host_status = hsts;
2242 OUTL_DSP (SCRIPTA_BA (np, complete_error));
2245 * Otherwise just restart the SCRIPTS.
2248 OUTL (nc_dsa, 0xffffff);
2249 OUTL_DSP (SCRIPTA_BA (np, start));
2258 sym_start_reset(np);
2262 * chip exception handler for selection timeout
2264 static void sym_int_sto (struct sym_hcb *np)
2266 u32 dsp = INL (nc_dsp);
2268 if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
2270 if (dsp == SCRIPTA_BA (np, wf_sel_done) + 8)
2271 sym_recover_scsi_int(np, HS_SEL_TIMEOUT);
2273 sym_start_reset(np);
2277 * chip exception handler for unexpected disconnect
2279 static void sym_int_udc (struct sym_hcb *np)
2281 printf ("%s: unexpected disconnect\n", sym_name(np));
2282 sym_recover_scsi_int(np, HS_UNEXPECTED);
2286 * chip exception handler for SCSI bus mode change
2288 * spi2-r12 11.2.3 says a transceiver mode change must
2289 * generate a reset event and a device that detects a reset
2290 * event shall initiate a hard reset. It says also that a
2291 * device that detects a mode change shall set data transfer
2292 * mode to eight bit asynchronous, etc...
2293 * So, just reinitializing all except chip should be enough.
2295 static void sym_int_sbmc (struct sym_hcb *np)
2297 u_char scsi_mode = INB (nc_stest4) & SMODE;
2302 printf("%s: SCSI BUS mode change from %s to %s.\n", sym_name(np),
2303 sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode));
2306 * Should suspend command processing for a few seconds and
2307 * reinitialize all except the chip.
2309 sym_start_up (np, 2);
2313 * chip exception handler for SCSI parity error.
2315 * When the chip detects a SCSI parity error and is
2316 * currently executing a (CH)MOV instruction, it does
2317 * not interrupt immediately, but tries to finish the
2318 * transfer of the current scatter entry before
2319 * interrupting. The following situations may occur:
2321 * - The complete scatter entry has been transferred
2322 * without the device having changed phase.
2323 * The chip will then interrupt with the DSP pointing
2324 * to the instruction that follows the MOV.
2326 * - A phase mismatch occurs before the MOV finished
2327 * and phase errors are to be handled by the C code.
2328 * The chip will then interrupt with both PAR and MA
2331 * - A phase mismatch occurs before the MOV finished and
2332 * phase errors are to be handled by SCRIPTS.
2333 * The chip will load the DSP with the phase mismatch
2334 * JUMP address and interrupt the host processor.
2336 static void sym_int_par (struct sym_hcb *np, u_short sist)
2338 u_char hsts = INB (HS_PRT);
2339 u32 dsp = INL (nc_dsp);
2340 u32 dbc = INL (nc_dbc);
2341 u32 dsa = INL (nc_dsa);
2342 u_char sbcl = INB (nc_sbcl);
2343 u_char cmd = dbc >> 24;
2344 int phase = cmd & 7;
2345 ccb_p cp = sym_ccb_from_dsa(np, dsa);
2347 printf("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n",
2348 sym_name(np), hsts, dbc, sbcl);
2351 * Check that the chip is connected to the SCSI BUS.
2353 if (!(INB (nc_scntl1) & ISCON)) {
2354 sym_recover_scsi_int(np, HS_UNEXPECTED);
2359 * If the nexus is not clearly identified, reset the bus.
2360 * We will try to do better later.
2366 * Check instruction was a MOV, direction was INPUT and
2369 if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
2373 * Keep track of the parity error.
2375 OUTONB (HF_PRT, HF_EXT_ERR);
2376 cp->xerr_status |= XE_PARITY_ERR;
2379 * Prepare the message to send to the device.
2381 np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
2384 * If the old phase was DATA IN phase, we have to deal with
2385 * the 3 situations described above.
2386 * For other input phases (MSG IN and STATUS), the device
2387 * must resend the whole thing that failed parity checking
2388 * or signal error. So, jumping to dispatcher should be OK.
2390 if (phase == 1 || phase == 5) {
2391 /* Phase mismatch handled by SCRIPTS */
2392 if (dsp == SCRIPTB_BA (np, pm_handle))
2394 /* Phase mismatch handled by the C code */
2397 /* No phase mismatch occurred */
2399 sym_set_script_dp (np, cp, dsp);
2400 OUTL_DSP (SCRIPTA_BA (np, dispatch));
2403 else if (phase == 7) /* We definitely cannot handle parity errors */
2404 #if 1 /* in message-in phase due to the relection */
2405 goto reset_all; /* path and various message anticipations. */
2407 OUTL_DSP (SCRIPTA_BA (np, clrack));
2410 OUTL_DSP (SCRIPTA_BA (np, dispatch));
2414 sym_start_reset(np);
2419 * chip exception handler for phase errors.
2421 * We have to construct a new transfer descriptor,
2422 * to transfer the rest of the current block.
2424 static void sym_int_ma (struct sym_hcb *np)
2437 u_char hflags, hflags0;
2446 rest = dbc & 0xffffff;
2450 * locate matching cp if any.
2452 cp = sym_ccb_from_dsa(np, dsa);
2455 * Donnot take into account dma fifo and various buffers in
2456 * INPUT phase since the chip flushes everything before
2457 * raising the MA interrupt for interrupted INPUT phases.
2458 * For DATA IN phase, we will check for the SWIDE later.
2460 if ((cmd & 7) != 1 && (cmd & 7) != 5) {
2463 if (np->features & FE_DFBC)
2464 delta = INW (nc_dfbc);
2469 * Read DFIFO, CTEST[4-6] using 1 PCI bus ownership.
2471 dfifo = INL(nc_dfifo);
2474 * Calculate remaining bytes in DMA fifo.
2475 * (CTEST5 = dfifo >> 16)
2477 if (dfifo & (DFS << 16))
2478 delta = ((((dfifo >> 8) & 0x300) |
2479 (dfifo & 0xff)) - rest) & 0x3ff;
2481 delta = ((dfifo & 0xff) - rest) & 0x7f;
2485 * The data in the dma fifo has not been transfered to
2486 * the target -> add the amount to the rest
2487 * and clear the data.
2488 * Check the sstat2 register in case of wide transfer.
2491 ss0 = INB (nc_sstat0);
2492 if (ss0 & OLF) rest++;
2493 if (!(np->features & FE_C10))
2494 if (ss0 & ORF) rest++;
2495 if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
2496 ss2 = INB (nc_sstat2);
2497 if (ss2 & OLF1) rest++;
2498 if (!(np->features & FE_C10))
2499 if (ss2 & ORF1) rest++;
2505 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */
2506 OUTB (nc_stest3, TE|CSF); /* scsi fifo */
2510 * log the information
2512 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
2513 printf ("P%x%x RL=%d D=%d ", cmd&7, INB(nc_sbcl)&7,
2514 (unsigned) rest, (unsigned) delta);
2517 * try to find the interrupted script command,
2518 * and the address at which to continue.
2522 if (dsp > np->scripta_ba &&
2523 dsp <= np->scripta_ba + np->scripta_sz) {
2524 vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8));
2527 else if (dsp > np->scriptb_ba &&
2528 dsp <= np->scriptb_ba + np->scriptb_sz) {
2529 vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8));
2534 * log the information
2536 if (DEBUG_FLAGS & DEBUG_PHASE) {
2537 printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
2538 cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
2542 printf ("%s: interrupted SCRIPT address not found.\n",
2548 printf ("%s: SCSI phase error fixup: CCB already dequeued.\n",
2554 * get old startaddress and old length.
2556 oadr = scr_to_cpu(vdsp[1]);
2558 if (cmd & 0x10) { /* Table indirect */
2559 tblp = (u32 *) ((char*) &cp->phys + oadr);
2560 olen = scr_to_cpu(tblp[0]);
2561 oadr = scr_to_cpu(tblp[1]);
2564 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
2567 if (DEBUG_FLAGS & DEBUG_PHASE) {
2568 printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
2569 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
2576 * check cmd against assumed interrupted script command.
2577 * If dt data phase, the MOVE instruction hasn't bit 4 of
2580 if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
2582 printf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
2583 (unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24);
2589 * if old phase not dataphase, leave here.
2593 printf ("phase change %x-%x %d@%08x resid=%d.\n",
2594 cmd&7, INB(nc_sbcl)&7, (unsigned)olen,
2595 (unsigned)oadr, (unsigned)rest);
2596 goto unexpected_phase;
2600 * Choose the correct PM save area.
2602 * Look at the PM_SAVE SCRIPT if you want to understand
2603 * this stuff. The equivalent code is implemented in
2604 * SCRIPTS for the 895A, 896 and 1010 that are able to
2605 * handle PM from the SCRIPTS processor.
2607 hflags0 = INB (HF_PRT);
2610 if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
2611 if (hflags & HF_IN_PM0)
2612 nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
2613 else if (hflags & HF_IN_PM1)
2614 nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
2616 if (hflags & HF_DP_SAVED)
2617 hflags ^= HF_ACT_PM;
2620 if (!(hflags & HF_ACT_PM)) {
2622 newcmd = SCRIPTA_BA (np, pm0_data);
2626 newcmd = SCRIPTA_BA (np, pm1_data);
2629 hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
2630 if (hflags != hflags0)
2631 OUTB (HF_PRT, hflags);
2634 * fillin the phase mismatch context
2636 pm->sg.addr = cpu_to_scr(oadr + olen - rest);
2637 pm->sg.size = cpu_to_scr(rest);
2638 pm->ret = cpu_to_scr(nxtdsp);
2641 * If we have a SWIDE,
2642 * - prepare the address to write the SWIDE from SCRIPTS,
2643 * - compute the SCRIPTS address to restart from,
2644 * - move current data pointer context by one byte.
2646 nxtdsp = SCRIPTA_BA (np, dispatch);
2647 if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
2648 (INB (nc_scntl2) & WSR)) {
2652 * Set up the table indirect for the MOVE
2653 * of the residual byte and adjust the data
2656 tmp = scr_to_cpu(pm->sg.addr);
2657 cp->phys.wresid.addr = cpu_to_scr(tmp);
2658 pm->sg.addr = cpu_to_scr(tmp + 1);
2659 tmp = scr_to_cpu(pm->sg.size);
2660 cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
2661 pm->sg.size = cpu_to_scr(tmp - 1);
2664 * If only the residual byte is to be moved,
2665 * no PM context is needed.
2667 if ((tmp&0xffffff) == 1)
2671 * Prepare the address of SCRIPTS that will
2672 * move the residual byte to memory.
2674 nxtdsp = SCRIPTB_BA (np, wsr_ma_helper);
2677 if (DEBUG_FLAGS & DEBUG_PHASE) {
2679 printf ("PM %x %x %x / %x %x %x.\n",
2680 hflags0, hflags, newcmd,
2681 (unsigned)scr_to_cpu(pm->sg.addr),
2682 (unsigned)scr_to_cpu(pm->sg.size),
2683 (unsigned)scr_to_cpu(pm->ret));
2687 * Restart the SCRIPTS processor.
2689 sym_set_script_dp (np, cp, newcmd);
2694 * Unexpected phase changes that occurs when the current phase
2695 * is not a DATA IN or DATA OUT phase are due to error conditions.
2696 * Such event may only happen when the SCRIPTS is using a
2697 * multibyte SCSI MOVE.
2699 * Phase change Some possible cause
2701 * COMMAND --> MSG IN SCSI parity error detected by target.
2702 * COMMAND --> STATUS Bad command or refused by target.
2703 * MSG OUT --> MSG IN Message rejected by target.
2704 * MSG OUT --> COMMAND Bogus target that discards extended
2705 * negotiation messages.
2707 * The code below does not care of the new phase and so
2708 * trusts the target. Why to annoy it ?
2709 * If the interrupted phase is COMMAND phase, we restart at
2711 * If a target does not get all the messages after selection,
2712 * the code assumes blindly that the target discards extended
2713 * messages and clears the negotiation status.
2714 * If the target does not want all our response to negotiation,
2715 * we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
2716 * bloat for such a should_not_happen situation).
2717 * In all other situation, we reset the BUS.
2718 * Are these assumptions reasonnable ? (Wait and see ...)
2725 case 2: /* COMMAND phase */
2726 nxtdsp = SCRIPTA_BA (np, dispatch);
2729 case 3: /* STATUS phase */
2730 nxtdsp = SCRIPTA_BA (np, dispatch);
2733 case 6: /* MSG OUT phase */
2735 * If the device may want to use untagged when we want
2736 * tagged, we prepare an IDENTIFY without disc. granted,
2737 * since we will not be able to handle reselect.
2738 * Otherwise, we just don't care.
2740 if (dsp == SCRIPTA_BA (np, send_ident)) {
2741 if (cp->tag != NO_TAG && olen - rest <= 3) {
2742 cp->host_status = HS_BUSY;
2743 np->msgout[0] = IDENTIFY(0, cp->lun);
2744 nxtdsp = SCRIPTB_BA (np, ident_break_atn);
2747 nxtdsp = SCRIPTB_BA (np, ident_break);
2749 else if (dsp == SCRIPTB_BA (np, send_wdtr) ||
2750 dsp == SCRIPTB_BA (np, send_sdtr) ||
2751 dsp == SCRIPTB_BA (np, send_ppr)) {
2752 nxtdsp = SCRIPTB_BA (np, nego_bad_phase);
2756 case 7: /* MSG IN phase */
2757 nxtdsp = SCRIPTA_BA (np, clrack);
2768 sym_start_reset(np);
2772 * chip interrupt handler
2774 * In normal situations, interrupt conditions occur one at
2775 * a time. But when something bad happens on the SCSI BUS,
2776 * the chip may raise several interrupt flags before
2777 * stopping and interrupting the CPU. The additionnal
2778 * interrupt flags are stacked in some extra registers
2779 * after the SIP and/or DIP flag has been raised in the
2780 * ISTAT. After the CPU has read the interrupt condition
2781 * flag from SIST or DSTAT, the chip unstacks the other
2782 * interrupt flags and sets the corresponding bits in
2783 * SIST or DSTAT. Since the chip starts stacking once the
2784 * SIP or DIP flag is set, there is a small window of time
2785 * where the stacking does not occur.
2787 * Typically, multiple interrupt conditions may happen in
2788 * the following situations:
2790 * - SCSI parity error + Phase mismatch (PAR|MA)
2791 * When an parity error is detected in input phase
2792 * and the device switches to msg-in phase inside a
2794 * - SCSI parity error + Unexpected disconnect (PAR|UDC)
2795 * When a stupid device does not want to handle the
2796 * recovery of an SCSI parity error.
2797 * - Some combinations of STO, PAR, UDC, ...
2798 * When using non compliant SCSI stuff, when user is
2799 * doing non compliant hot tampering on the BUS, when
2800 * something really bad happens to a device, etc ...
2802 * The heuristic suggested by SYMBIOS to handle
2803 * multiple interrupts is to try unstacking all
2804 * interrupts conditions and to handle them on some
2805 * priority based on error severity.
2806 * This will work when the unstacking has been
2807 * successful, but we cannot be 100 % sure of that,
2808 * since the CPU may have been faster to unstack than
2809 * the chip is able to stack. Hmmm ... But it seems that
2810 * such a situation is very unlikely to happen.
2812 * If this happen, for example STO caught by the CPU
2813 * then UDC happenning before the CPU have restarted
2814 * the SCRIPTS, the driver may wrongly complete the
2815 * same command on UDC, since the SCRIPTS didn't restart
2816 * and the DSA still points to the same command.
2817 * We avoid this situation by setting the DSA to an
2818 * invalid value when the CCB is completed and before
2819 * restarting the SCRIPTS.
2821 * Another issue is that we need some section of our
2822 * recovery procedures to be somehow uninterruptible but
2823 * the SCRIPTS processor does not provides such a
2824 * feature. For this reason, we handle recovery preferently
2825 * from the C code and check against some SCRIPTS critical
2826 * sections from the C code.
2828 * Hopefully, the interrupt handling of the driver is now
2829 * able to resist to weird BUS error conditions, but donnot
2830 * ask me for any guarantee that it will never fail. :-)
2831 * Use at your own decision and risk.
2834 void sym_interrupt (struct sym_hcb *np)
2836 u_char istat, istatc;
2841 * interrupt on the fly ?
2842 * (SCRIPTS may still be running)
2844 * A `dummy read' is needed to ensure that the
2845 * clear of the INTF flag reaches the device
2846 * and that posted writes are flushed to memory
2847 * before the scanning of the DONE queue.
2848 * Note that SCRIPTS also (dummy) read to memory
2849 * prior to deliver the INTF interrupt condition.
2851 istat = INB (nc_istat);
2853 OUTB (nc_istat, (istat & SIGP) | INTF | np->istat_sem);
2854 istat = INB (nc_istat); /* DUMMY READ */
2855 if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
2856 (void)sym_wakeup_done (np);
2859 if (!(istat & (SIP|DIP)))
2862 #if 0 /* We should never get this one */
2864 OUTB (nc_istat, CABRT);
2868 * PAR and MA interrupts may occur at the same time,
2869 * and we need to know of both in order to handle
2870 * this situation properly. We try to unstack SCSI
2871 * interrupts for that reason. BTW, I dislike a LOT
2872 * such a loop inside the interrupt routine.
2873 * Even if DMA interrupt stacking is very unlikely to
2874 * happen, we also try unstacking these ones, since
2875 * this has no performance impact.
2882 sist |= INW (nc_sist);
2884 dstat |= INB (nc_dstat);
2885 istatc = INB (nc_istat);
2887 } while (istatc & (SIP|DIP));
2889 if (DEBUG_FLAGS & DEBUG_TINY)
2890 printf ("<%d|%x:%x|%x:%x>",
2893 (unsigned)INL(nc_dsp),
2894 (unsigned)INL(nc_dbc));
2896 * On paper, a memory read barrier may be needed here to
2897 * prevent out of order LOADs by the CPU from having
2898 * prefetched stale data prior to DMA having occurred.
2899 * And since we are paranoid ... :)
2901 MEMORY_READ_BARRIER();
2904 * First, interrupts we want to service cleanly.
2906 * Phase mismatch (MA) is the most frequent interrupt
2907 * for chip earlier than the 896 and so we have to service
2908 * it as quickly as possible.
2909 * A SCSI parity error (PAR) may be combined with a phase
2910 * mismatch condition (MA).
2911 * Programmed interrupts (SIR) are used to call the C code
2913 * The single step interrupt (SSI) is not used in this
2916 if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
2917 !(dstat & (MDPE|BF|ABRT|IID))) {
2918 if (sist & PAR) sym_int_par (np, sist);
2919 else if (sist & MA) sym_int_ma (np);
2920 else if (dstat & SIR) sym_int_sir (np);
2921 else if (dstat & SSI) OUTONB_STD ();
2922 else goto unknown_int;
2927 * Now, interrupts that donnot happen in normal
2928 * situations and that we may need to recover from.
2930 * On SCSI RESET (RST), we reset everything.
2931 * On SCSI BUS MODE CHANGE (SBMC), we complete all
2932 * active CCBs with RESET status, prepare all devices
2933 * for negotiating again and restart the SCRIPTS.
2934 * On STO and UDC, we complete the CCB with the corres-
2935 * ponding status and restart the SCRIPTS.
2938 printf("%s: SCSI BUS reset detected.\n", sym_name(np));
2939 sym_start_up (np, 1);
2943 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
2944 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
2946 if (!(sist & (GEN|HTH|SGE)) &&
2947 !(dstat & (MDPE|BF|ABRT|IID))) {
2948 if (sist & SBMC) sym_int_sbmc (np);
2949 else if (sist & STO) sym_int_sto (np);
2950 else if (sist & UDC) sym_int_udc (np);
2951 else goto unknown_int;
2956 * Now, interrupts we are not able to recover cleanly.
2958 * Log message for hard errors.
2962 sym_log_hard_error(np, sist, dstat);
2964 if ((sist & (GEN|HTH|SGE)) ||
2965 (dstat & (MDPE|BF|ABRT|IID))) {
2966 sym_start_reset(np);
2972 * We just miss the cause of the interrupt. :(
2973 * Print a message. The timeout will do the real work.
2975 printf( "%s: unknown interrupt(s) ignored, "
2976 "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
2977 sym_name(np), istat, dstat, sist);
2981 * Dequeue from the START queue all CCBs that match
2982 * a given target/lun/task condition (-1 means all),
2983 * and move them from the BUSY queue to the COMP queue
2984 * with CAM_REQUEUE_REQ status condition.
2985 * This function is used during error handling/recovery.
2986 * It is called with SCRIPTS not running.
2989 sym_dequeue_from_squeue(struct sym_hcb *np, int i, int target, int lun, int task)
2995 * Make sure the starting index is within range.
2997 assert((i >= 0) && (i < 2*MAX_QUEUE));
3000 * Walk until end of START queue and dequeue every job
3001 * that matches the target/lun/task condition.
3004 while (i != np->squeueput) {
3005 cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
3007 #ifdef SYM_CONF_IARB_SUPPORT
3008 /* Forget hints for IARB, they may be no longer relevant */
3009 cp->host_flags &= ~HF_HINT_IARB;
3011 if ((target == -1 || cp->target == target) &&
3012 (lun == -1 || cp->lun == lun) &&
3013 (task == -1 || cp->tag == task)) {
3014 sym_set_cam_status(cp->cam_ccb, CAM_REQUEUE_REQ);
3015 sym_remque(&cp->link_ccbq);
3016 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3020 np->squeue[j] = np->squeue[i];
3021 if ((j += 2) >= MAX_QUEUE*2) j = 0;
3023 if ((i += 2) >= MAX_QUEUE*2) i = 0;
3025 if (i != j) /* Copy back the idle task if needed */
3026 np->squeue[j] = np->squeue[i];
3027 np->squeueput = j; /* Update our current start queue pointer */
3033 * chip handler for bad SCSI status condition
3035 * In case of bad SCSI status, we unqueue all the tasks
3036 * currently queued to the controller but not yet started
3037 * and then restart the SCRIPTS processor immediately.
3039 * QUEUE FULL and BUSY conditions are handled the same way.
3040 * Basically all the not yet started tasks are requeued in
3041 * device queue and the queue is frozen until a completion.
3043 * For CHECK CONDITION and COMMAND TERMINATED status, we use
3044 * the CCB of the failed command to prepare a REQUEST SENSE
3045 * SCSI command and queue it to the controller queue.
3047 * SCRATCHA is assumed to have been loaded with STARTPOS
3048 * before the SCRIPTS called the C code.
3050 static void sym_sir_bad_scsi_status(struct sym_hcb *np, int num, ccb_p cp)
3052 tcb_p tp = &np->target[cp->target];
3054 u_char s_status = cp->ssss_status;
3055 u_char h_flags = cp->host_flags;
3060 * Compute the index of the next job to start from SCRIPTS.
3062 i = (INL (nc_scratcha) - np->squeue_ba) / 4;
3065 * The last CCB queued used for IARB hint may be
3066 * no longer relevant. Forget it.
3068 #ifdef SYM_CONF_IARB_SUPPORT
3074 * Now deal with the SCSI status.
3079 if (sym_verbose >= 2) {
3082 s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
3084 default: /* S_INT, S_INT_COND_MET, S_CONFLICT */
3085 sym_complete_error (np, cp);
3090 * If we get an SCSI error when requesting sense, give up.
3092 if (h_flags & HF_SENSE) {
3093 sym_complete_error (np, cp);
3098 * Dequeue all queued CCBs for that device not yet started,
3099 * and restart the SCRIPTS processor immediately.
3101 (void) sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
3102 OUTL_DSP (SCRIPTA_BA (np, start));
3105 * Save some info of the actual IO.
3106 * Compute the data residual.
3108 cp->sv_scsi_status = cp->ssss_status;
3109 cp->sv_xerr_status = cp->xerr_status;
3110 cp->sv_resid = sym_compute_residual(np, cp);
3113 * Prepare all needed data structures for
3114 * requesting sense data.
3117 cp->scsi_smsg2[0] = IDENTIFY(0, cp->lun);
3121 * If we are currently using anything different from
3122 * async. 8 bit data transfers with that target,
3123 * start a negotiation, since the device may want
3124 * to report us a UNIT ATTENTION condition due to
3125 * a cause we currently ignore, and we donnot want
3126 * to be stuck with WIDE and/or SYNC data transfer.
3128 * cp->nego_status is filled by sym_prepare_nego().
3130 cp->nego_status = 0;
3131 msglen += sym_prepare_nego(np, cp, &cp->scsi_smsg2[msglen]);
3133 * Message table indirect structure.
3135 cp->phys.smsg.addr = cpu_to_scr(CCB_BA (cp, scsi_smsg2));
3136 cp->phys.smsg.size = cpu_to_scr(msglen);
3141 cp->phys.cmd.addr = cpu_to_scr(CCB_BA (cp, sensecmd));
3142 cp->phys.cmd.size = cpu_to_scr(6);
3145 * patch requested size into sense command
3147 cp->sensecmd[0] = REQUEST_SENSE;
3148 cp->sensecmd[1] = 0;
3149 if (tp->tinfo.curr.scsi_version <= 2 && cp->lun <= 7)
3150 cp->sensecmd[1] = cp->lun << 5;
3151 cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
3152 cp->data_len = SYM_SNS_BBUF_LEN;
3157 memset(cp->sns_bbuf, 0, SYM_SNS_BBUF_LEN);
3158 cp->phys.sense.addr = cpu_to_scr(vtobus(cp->sns_bbuf));
3159 cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
3162 * requeue the command.
3164 startp = SCRIPTB_BA (np, sdata_in);
3166 cp->phys.head.savep = cpu_to_scr(startp);
3167 cp->phys.head.lastp = cpu_to_scr(startp);
3168 cp->startp = cpu_to_scr(startp);
3169 cp->goalp = cpu_to_scr(startp + 16);
3171 cp->host_xflags = 0;
3172 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
3173 cp->ssss_status = S_ILLEGAL;
3174 cp->host_flags = (HF_SENSE|HF_DATA_IN);
3175 cp->xerr_status = 0;
3176 cp->extra_bytes = 0;
3178 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, select));
3181 * Requeue the command.
3183 sym_put_start_queue(np, cp);
3186 * Give back to upper layer everything we have dequeued.
3188 sym_flush_comp_queue(np, 0);
3194 * After a device has accepted some management message
3195 * as BUS DEVICE RESET, ABORT TASK, etc ..., or when
3196 * a device signals a UNIT ATTENTION condition, some
3197 * tasks are thrown away by the device. We are required
3198 * to reflect that on our tasks list since the device
3199 * will never complete these tasks.
3201 * This function move from the BUSY queue to the COMP
3202 * queue all disconnected CCBs for a given target that
3203 * match the following criteria:
3204 * - lun=-1 means any logical UNIT otherwise a given one.
3205 * - task=-1 means any task, otherwise a given one.
3207 int sym_clear_tasks(struct sym_hcb *np, int cam_status, int target, int lun, int task)
3209 SYM_QUEHEAD qtmp, *qp;
3214 * Move the entire BUSY queue to our temporary queue.
3216 sym_que_init(&qtmp);
3217 sym_que_splice(&np->busy_ccbq, &qtmp);
3218 sym_que_init(&np->busy_ccbq);
3221 * Put all CCBs that matches our criteria into
3222 * the COMP queue and put back other ones into
3225 while ((qp = sym_remque_head(&qtmp)) != 0) {
3226 struct scsi_cmnd *ccb;
3227 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3229 if (cp->host_status != HS_DISCONNECT ||
3230 cp->target != target ||
3231 (lun != -1 && cp->lun != lun) ||
3233 (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
3234 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
3237 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3239 /* Preserve the software timeout condition */
3240 if (sym_get_cam_status(ccb) != CAM_CMD_TIMEOUT)
3241 sym_set_cam_status(ccb, cam_status);
3244 printf("XXXX TASK @%p CLEARED\n", cp);
3251 * chip handler for TASKS recovery
3253 * We cannot safely abort a command, while the SCRIPTS
3254 * processor is running, since we just would be in race
3257 * As long as we have tasks to abort, we keep the SEM
3258 * bit set in the ISTAT. When this bit is set, the
3259 * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
3260 * each time it enters the scheduler.
3262 * If we have to reset a target, clear tasks of a unit,
3263 * or to perform the abort of a disconnected job, we
3264 * restart the SCRIPTS for selecting the target. Once
3265 * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
3266 * If it loses arbitration, the SCRIPTS will interrupt again
3267 * the next time it will enter its scheduler, and so on ...
3269 * On SIR_TARGET_SELECTED, we scan for the more
3270 * appropriate thing to do:
3272 * - If nothing, we just sent a M_ABORT message to the
3273 * target to get rid of the useless SCSI bus ownership.
3274 * According to the specs, no tasks shall be affected.
3275 * - If the target is to be reset, we send it a M_RESET
3277 * - If a logical UNIT is to be cleared , we send the
3278 * IDENTIFY(lun) + M_ABORT.
3279 * - If an untagged task is to be aborted, we send the
3280 * IDENTIFY(lun) + M_ABORT.
3281 * - If a tagged task is to be aborted, we send the
3282 * IDENTIFY(lun) + task attributes + M_ABORT_TAG.
3284 * Once our 'kiss of death' :) message has been accepted
3285 * by the target, the SCRIPTS interrupts again
3286 * (SIR_ABORT_SENT). On this interrupt, we complete
3287 * all the CCBs that should have been aborted by the
3288 * target according to our message.
3290 static void sym_sir_task_recovery(struct sym_hcb *np, int num)
3295 int target=-1, lun=-1, task;
3300 * The SCRIPTS processor stopped before starting
3301 * the next command in order to allow us to perform
3302 * some task recovery.
3304 case SIR_SCRIPT_STOPPED:
3306 * Do we have any target to reset or unit to clear ?
3308 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
3309 tp = &np->target[i];
3311 (tp->lun0p && tp->lun0p->to_clear)) {
3317 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
3318 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
3328 * If not, walk the busy queue for any
3329 * disconnected CCB to be aborted.
3332 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3333 cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
3334 if (cp->host_status != HS_DISCONNECT)
3337 target = cp->target;
3344 * If some target is to be selected,
3345 * prepare and start the selection.
3348 tp = &np->target[target];
3349 np->abrt_sel.sel_id = target;
3350 np->abrt_sel.sel_scntl3 = tp->head.wval;
3351 np->abrt_sel.sel_sxfer = tp->head.sval;
3352 OUTL(nc_dsa, np->hcb_ba);
3353 OUTL_DSP (SCRIPTB_BA (np, sel_for_abort));
3358 * Now look for a CCB to abort that haven't started yet.
3359 * Btw, the SCRIPTS processor is still stopped, so
3360 * we are not in race.
3364 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3365 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3366 if (cp->host_status != HS_BUSY &&
3367 cp->host_status != HS_NEGOTIATE)
3371 #ifdef SYM_CONF_IARB_SUPPORT
3373 * If we are using IMMEDIATE ARBITRATION, we donnot
3374 * want to cancel the last queued CCB, since the
3375 * SCRIPTS may have anticipated the selection.
3377 if (cp == np->last_cp) {
3382 i = 1; /* Means we have found some */
3387 * We are done, so we donnot need
3388 * to synchronize with the SCRIPTS anylonger.
3389 * Remove the SEM flag from the ISTAT.
3392 OUTB (nc_istat, SIGP);
3396 * Compute index of next position in the start
3397 * queue the SCRIPTS intends to start and dequeue
3398 * all CCBs for that device that haven't been started.
3400 i = (INL (nc_scratcha) - np->squeue_ba) / 4;
3401 i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
3404 * Make sure at least our IO to abort has been dequeued.
3406 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
3407 assert(i && sym_get_cam_status(cp->cam_ccb) == CAM_REQUEUE_REQ);
3409 sym_remque(&cp->link_ccbq);
3410 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
3413 * Keep track in cam status of the reason of the abort.
3415 if (cp->to_abort == 2)
3416 sym_set_cam_status(cp->cam_ccb, CAM_CMD_TIMEOUT);
3418 sym_set_cam_status(cp->cam_ccb, CAM_REQ_ABORTED);
3421 * Complete with error everything that we have dequeued.
3423 sym_flush_comp_queue(np, 0);
3426 * The SCRIPTS processor has selected a target
3427 * we may have some manual recovery to perform for.
3429 case SIR_TARGET_SELECTED:
3430 target = (INB (nc_sdid) & 0xf);
3431 tp = &np->target[target];
3433 np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
3436 * If the target is to be reset, prepare a
3437 * M_RESET message and clear the to_reset flag
3438 * since we donnot expect this operation to fail.
3441 np->abrt_msg[0] = M_RESET;
3442 np->abrt_tbl.size = 1;
3448 * Otherwise, look for some logical unit to be cleared.
3450 if (tp->lun0p && tp->lun0p->to_clear)
3452 else if (tp->lunmp) {
3453 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
3454 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
3462 * If a logical unit is to be cleared, prepare
3463 * an IDENTIFY(lun) + ABORT MESSAGE.
3466 lcb_p lp = sym_lp(np, tp, lun);
3467 lp->to_clear = 0; /* We don't expect to fail here */
3468 np->abrt_msg[0] = IDENTIFY(0, lun);
3469 np->abrt_msg[1] = M_ABORT;
3470 np->abrt_tbl.size = 2;
3475 * Otherwise, look for some disconnected job to
3476 * abort for this target.
3480 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
3481 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
3482 if (cp->host_status != HS_DISCONNECT)
3484 if (cp->target != target)
3488 i = 1; /* Means we have some */
3493 * If we have none, probably since the device has
3494 * completed the command before we won abitration,
3495 * send a M_ABORT message without IDENTIFY.
3496 * According to the specs, the device must just
3497 * disconnect the BUS and not abort any task.
3500 np->abrt_msg[0] = M_ABORT;
3501 np->abrt_tbl.size = 1;
3506 * We have some task to abort.
3507 * Set the IDENTIFY(lun)
3509 np->abrt_msg[0] = IDENTIFY(0, cp->lun);
3512 * If we want to abort an untagged command, we
3513 * will send a IDENTIFY + M_ABORT.
3514 * Otherwise (tagged command), we will send
3515 * a IDENTITFY + task attributes + ABORT TAG.
3517 if (cp->tag == NO_TAG) {
3518 np->abrt_msg[1] = M_ABORT;
3519 np->abrt_tbl.size = 2;
3521 np->abrt_msg[1] = cp->scsi_smsg[1];
3522 np->abrt_msg[2] = cp->scsi_smsg[2];
3523 np->abrt_msg[3] = M_ABORT_TAG;
3524 np->abrt_tbl.size = 4;
3527 * Keep track of software timeout condition, since the
3528 * peripheral driver may not count retries on abort
3529 * conditions not due to timeout.
3531 if (cp->to_abort == 2)
3532 sym_set_cam_status(cp->cam_ccb, CAM_CMD_TIMEOUT);
3533 cp->to_abort = 0; /* We donnot expect to fail here */
3537 * The target has accepted our message and switched
3538 * to BUS FREE phase as we expected.
3540 case SIR_ABORT_SENT:
3541 target = (INB (nc_sdid) & 0xf);
3542 tp = &np->target[target];
3545 ** If we didn't abort anything, leave here.
3547 if (np->abrt_msg[0] == M_ABORT)
3551 * If we sent a M_RESET, then a hardware reset has
3552 * been performed by the target.
3553 * - Reset everything to async 8 bit
3554 * - Tell ourself to negotiate next time :-)
3555 * - Prepare to clear all disconnected CCBs for
3556 * this target from our task list (lun=task=-1)
3560 if (np->abrt_msg[0] == M_RESET) {
3562 tp->head.wval = np->rv_scntl3;
3564 tp->tinfo.curr.period = 0;
3565 tp->tinfo.curr.offset = 0;
3566 tp->tinfo.curr.width = BUS_8_BIT;
3567 tp->tinfo.curr.options = 0;
3571 * Otherwise, check for the LUN and TASK(s)
3572 * concerned by the cancelation.
3573 * If it is not ABORT_TAG then it is CLEAR_QUEUE
3574 * or an ABORT message :-)
3577 lun = np->abrt_msg[0] & 0x3f;
3578 if (np->abrt_msg[1] == M_ABORT_TAG)
3579 task = np->abrt_msg[2];
3583 * Complete all the CCBs the device should have
3584 * aborted due to our 'kiss of death' message.
3586 i = (INL (nc_scratcha) - np->squeue_ba) / 4;
3587 (void) sym_dequeue_from_squeue(np, i, target, lun, -1);
3588 (void) sym_clear_tasks(np, CAM_REQ_ABORTED, target, lun, task);
3589 sym_flush_comp_queue(np, 0);
3592 * If we sent a BDR, make upper layer aware of that.
3594 if (np->abrt_msg[0] == M_RESET)
3595 sym_xpt_async_sent_bdr(np, target);
3600 * Print to the log the message we intend to send.
3602 if (num == SIR_TARGET_SELECTED) {
3603 PRINT_TARGET(np, target);
3604 sym_printl_hex("control msgout:", np->abrt_msg,
3606 np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
3610 * Let the SCRIPTS processor continue.
3616 * Gerard's alchemy:) that deals with with the data
3617 * pointer for both MDP and the residual calculation.
3619 * I didn't want to bloat the code by more than 200
3620 * lignes for the handling of both MDP and the residual.
3621 * This has been achieved by using a data pointer
3622 * representation consisting in an index in the data
3623 * array (dp_sg) and a negative offset (dp_ofs) that
3624 * have the following meaning:
3626 * - dp_sg = SYM_CONF_MAX_SG
3627 * we are at the end of the data script.
3628 * - dp_sg < SYM_CONF_MAX_SG
3629 * dp_sg points to the next entry of the scatter array
3630 * we want to transfer.
3632 * dp_ofs represents the residual of bytes of the
3633 * previous entry scatter entry we will send first.
3635 * no residual to send first.
3637 * The function sym_evaluate_dp() accepts an arbitray
3638 * offset (basically from the MDP message) and returns
3639 * the corresponding values of dp_sg and dp_ofs.
3642 static int sym_evaluate_dp(struct sym_hcb *np, ccb_p cp, u32 scr, int *ofs)
3645 int dp_ofs, dp_sg, dp_sgmin;
3650 * Compute the resulted data pointer in term of a script
3651 * address within some DATA script and a signed byte offset.
3655 if (dp_scr == SCRIPTA_BA (np, pm0_data))
3657 else if (dp_scr == SCRIPTA_BA (np, pm1_data))
3663 dp_scr = scr_to_cpu(pm->ret);
3664 dp_ofs -= scr_to_cpu(pm->sg.size);
3668 * If we are auto-sensing, then we are done.
3670 if (cp->host_flags & HF_SENSE) {
3676 * Deduce the index of the sg entry.
3677 * Keep track of the index of the first valid entry.
3678 * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the
3681 tmp = scr_to_cpu(sym_goalp(cp));
3682 dp_sg = SYM_CONF_MAX_SG;
3684 dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
3685 dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
3688 * Move to the sg entry the data pointer belongs to.
3690 * If we are inside the data area, we expect result to be:
3693 * dp_ofs = 0 and dp_sg is the index of the sg entry
3694 * the data pointer belongs to (or the end of the data)
3696 * dp_ofs < 0 and dp_sg is the index of the sg entry
3697 * the data pointer belongs to + 1.
3701 while (dp_sg > dp_sgmin) {
3703 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3704 n = dp_ofs + (tmp & 0xffffff);
3712 else if (dp_ofs > 0) {
3713 while (dp_sg < SYM_CONF_MAX_SG) {
3714 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3715 dp_ofs -= (tmp & 0xffffff);
3723 * Make sure the data pointer is inside the data area.
3724 * If not, return some error.
3726 if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
3728 else if (dp_sg > SYM_CONF_MAX_SG ||
3729 (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
3733 * Save the extreme pointer if needed.
3735 if (dp_sg > cp->ext_sg ||
3736 (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
3738 cp->ext_ofs = dp_ofs;
3752 * chip handler for MODIFY DATA POINTER MESSAGE
3754 * We also call this function on IGNORE WIDE RESIDUE
3755 * messages that do not match a SWIDE full condition.
3756 * Btw, we assume in that situation that such a message
3757 * is equivalent to a MODIFY DATA POINTER (offset=-1).
3760 static void sym_modify_dp(struct sym_hcb *np, tcb_p tp, ccb_p cp, int ofs)
3763 u32 dp_scr = sym_get_script_dp (np, cp);
3771 * Not supported for auto-sense.
3773 if (cp->host_flags & HF_SENSE)
3777 * Apply our alchemy:) (see comments in sym_evaluate_dp()),
3778 * to the resulted data pointer.
3780 dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
3785 * And our alchemy:) allows to easily calculate the data
3786 * script address we want to return for the next data phase.
3788 dp_ret = cpu_to_scr(sym_goalp(cp));
3789 dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
3792 * If offset / scatter entry is zero we donnot need
3793 * a context for the new current data pointer.
3801 * Get a context for the new current data pointer.
3803 hflags = INB (HF_PRT);
3805 if (hflags & HF_DP_SAVED)
3806 hflags ^= HF_ACT_PM;
3808 if (!(hflags & HF_ACT_PM)) {
3810 dp_scr = SCRIPTA_BA (np, pm0_data);
3814 dp_scr = SCRIPTA_BA (np, pm1_data);
3817 hflags &= ~(HF_DP_SAVED);
3819 OUTB (HF_PRT, hflags);
3822 * Set up the new current data pointer.
3823 * ofs < 0 there, and for the next data phase, we
3824 * want to transfer part of the data of the sg entry
3825 * corresponding to index dp_sg-1 prior to returning
3826 * to the main data script.
3828 pm->ret = cpu_to_scr(dp_ret);
3829 tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
3830 tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
3831 pm->sg.addr = cpu_to_scr(tmp);
3832 pm->sg.size = cpu_to_scr(-dp_ofs);
3835 sym_set_script_dp (np, cp, dp_scr);
3836 OUTL_DSP (SCRIPTA_BA (np, clrack));
3840 OUTL_DSP (SCRIPTB_BA (np, msg_bad));
3845 * chip calculation of the data residual.
3847 * As I used to say, the requirement of data residual
3848 * in SCSI is broken, useless and cannot be achieved
3849 * without huge complexity.
3850 * But most OSes and even the official CAM require it.
3851 * When stupidity happens to be so widely spread inside
3852 * a community, it gets hard to convince.
3854 * Anyway, I don't care, since I am not going to use
3855 * any software that considers this data residual as
3856 * a relevant information. :)
3859 int sym_compute_residual(struct sym_hcb *np, ccb_p cp)
3861 int dp_sg, dp_sgmin, resid = 0;
3865 * Check for some data lost or just thrown away.
3866 * We are not required to be quite accurate in this
3867 * situation. Btw, if we are odd for output and the
3868 * device claims some more data, it may well happen
3869 * than our residual be zero. :-)
3871 if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
3872 if (cp->xerr_status & XE_EXTRA_DATA)
3873 resid -= cp->extra_bytes;
3874 if (cp->xerr_status & XE_SODL_UNRUN)
3876 if (cp->xerr_status & XE_SWIDE_OVRUN)
3881 * If all data has been transferred,
3882 * there is no residual.
3884 if (cp->phys.head.lastp == sym_goalp(cp))
3888 * If no data transfer occurs, or if the data
3889 * pointer is weird, return full residual.
3891 if (cp->startp == cp->phys.head.lastp ||
3892 sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
3894 return cp->data_len;
3898 * If we were auto-sensing, then we are done.
3900 if (cp->host_flags & HF_SENSE) {
3905 * We are now full comfortable in the computation
3906 * of the data residual (2's complement).
3908 dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
3909 resid = -cp->ext_ofs;
3910 for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
3911 u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
3912 resid += (tmp & 0xffffff);
3916 * Hopefully, the result is not too wrong.
3922 * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
3924 * When we try to negotiate, we append the negotiation message
3925 * to the identify and (maybe) simple tag message.
3926 * The host status field is set to HS_NEGOTIATE to mark this
3929 * If the target doesn't answer this message immediately
3930 * (as required by the standard), the SIR_NEGO_FAILED interrupt
3931 * will be raised eventually.
3932 * The handler removes the HS_NEGOTIATE status, and sets the
3933 * negotiated value to the default (async / nowide).
3935 * If we receive a matching answer immediately, we check it
3936 * for validity, and set the values.
3938 * If we receive a Reject message immediately, we assume the
3939 * negotiation has failed, and fall back to standard values.
3941 * If we receive a negotiation message while not in HS_NEGOTIATE
3942 * state, it's a target initiated negotiation. We prepare a
3943 * (hopefully) valid answer, set our parameters, and send back
3944 * this answer to the target.
3946 * If the target doesn't fetch the answer (no message out phase),
3947 * we assume the negotiation has failed, and fall back to default
3948 * settings (SIR_NEGO_PROTO interrupt).
3950 * When we set the values, we adjust them in all ccbs belonging
3951 * to this target, in the controller's register, and in the "phys"
3952 * field of the controller's struct sym_hcb.
3956 * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message.
3959 sym_sync_nego_check(struct sym_hcb *np, int req, int target)
3961 u_char chg, ofs, per, fak, div;
3963 if (DEBUG_FLAGS & DEBUG_NEGO) {
3964 sym_print_nego_msg(np, target, "sync msgin", np->msgin);
3968 * Get requested values.
3975 * Check values against our limits.
3978 if (ofs > np->maxoffs)
3979 {chg = 1; ofs = np->maxoffs;}
3983 if (per < np->minsync)
3984 {chg = 1; per = np->minsync;}
3988 * Get new chip synchronous parameters value.
3991 if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
3994 if (DEBUG_FLAGS & DEBUG_NEGO) {
3995 PRINT_TARGET(np, target);
3996 printf ("sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
3997 ofs, per, div, fak, chg);
4001 * If it was an answer we want to change,
4002 * then it isn't acceptable. Reject it.
4010 sym_setsync (np, target, ofs, per, div, fak);
4013 * It was an answer. We are done.
4019 * It was a request. Prepare an answer message.
4021 np->msgout[0] = M_EXTENDED;
4023 np->msgout[2] = M_X_SYNC_REQ;
4024 np->msgout[3] = per;
4025 np->msgout[4] = ofs;
4027 if (DEBUG_FLAGS & DEBUG_NEGO) {
4028 sym_print_nego_msg(np, target, "sync msgout", np->msgout);
4031 np->msgin [0] = M_NOOP;
4036 sym_setsync (np, target, 0, 0, 0, 0);
4040 static void sym_sync_nego(struct sym_hcb *np, tcb_p tp, ccb_p cp)
4046 * Request or answer ?
4048 if (INB (HS_PRT) == HS_NEGOTIATE) {
4049 OUTB (HS_PRT, HS_BUSY);
4050 if (cp->nego_status && cp->nego_status != NS_SYNC)
4056 * Check and apply new values.
4058 result = sym_sync_nego_check(np, req, cp->target);
4059 if (result) /* Not acceptable, reject it */
4061 if (req) { /* Was a request, send response. */
4062 cp->nego_status = NS_SYNC;
4063 OUTL_DSP (SCRIPTB_BA (np, sdtr_resp));
4065 else /* Was a response, we are done. */
4066 OUTL_DSP (SCRIPTA_BA (np, clrack));
4070 OUTL_DSP (SCRIPTB_BA (np, msg_bad));
4074 * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message.
4077 sym_ppr_nego_check(struct sym_hcb *np, int req, int target)
4079 tcb_p tp = &np->target[target];
4080 unsigned char fak, div;
4083 unsigned char per = np->msgin[3];
4084 unsigned char ofs = np->msgin[5];
4085 unsigned char wide = np->msgin[6];
4086 unsigned char opts = np->msgin[7] & PPR_OPT_MASK;
4088 if (DEBUG_FLAGS & DEBUG_NEGO) {
4089 sym_print_nego_msg(np, target, "ppr msgin", np->msgin);
4093 * Check values against our limits.
4095 if (wide > np->maxwide) {
4099 if (!wide || !(np->features & FE_ULTRA3))
4102 if (!(np->features & FE_U3EN)) /* Broken U3EN bit not supported */
4105 if (opts != (np->msgin[7] & PPR_OPT_MASK))
4108 dt = opts & PPR_OPT_DT;
4111 unsigned char maxoffs = dt ? np->maxoffs_dt : np->maxoffs;
4112 if (ofs > maxoffs) {
4119 unsigned char minsync = dt ? np->minsync_dt : np->minsync;
4120 if (per < minsync) {
4127 * Get new chip synchronous parameters value.
4130 if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
4134 * If it was an answer we want to change,
4135 * then it isn't acceptable. Reject it.
4143 sym_setpprot(np, target, opts, ofs, per, wide, div, fak);
4146 * It was an answer. We are done.
4152 * It was a request. Prepare an answer message.
4154 np->msgout[0] = M_EXTENDED;
4156 np->msgout[2] = M_X_PPR_REQ;
4157 np->msgout[3] = per;
4159 np->msgout[5] = ofs;
4160 np->msgout[6] = wide;
4161 np->msgout[7] = opts;
4163 if (DEBUG_FLAGS & DEBUG_NEGO) {
4164 sym_print_nego_msg(np, target, "ppr msgout", np->msgout);
4167 np->msgin [0] = M_NOOP;
4172 sym_setpprot (np, target, 0, 0, 0, 0, 0, 0);
4174 * If it is a device response that should result in
4175 * ST, we may want to try a legacy negotiation later.
4177 if (!req && !opts) {
4178 tp->tinfo.goal.options = 0;
4179 tp->tinfo.goal.width = wide;
4180 tp->tinfo.goal.period = per;
4181 tp->tinfo.goal.offset = ofs;
4186 static void sym_ppr_nego(struct sym_hcb *np, tcb_p tp, ccb_p cp)
4192 * Request or answer ?
4194 if (INB (HS_PRT) == HS_NEGOTIATE) {
4195 OUTB (HS_PRT, HS_BUSY);
4196 if (cp->nego_status && cp->nego_status != NS_PPR)
4202 * Check and apply new values.
4204 result = sym_ppr_nego_check(np, req, cp->target);
4205 if (result) /* Not acceptable, reject it */
4207 if (req) { /* Was a request, send response. */
4208 cp->nego_status = NS_PPR;
4209 OUTL_DSP (SCRIPTB_BA (np, ppr_resp));
4211 else /* Was a response, we are done. */
4212 OUTL_DSP (SCRIPTA_BA (np, clrack));
4216 OUTL_DSP (SCRIPTB_BA (np, msg_bad));
4220 * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message.
4223 sym_wide_nego_check(struct sym_hcb *np, int req, int target)
4227 if (DEBUG_FLAGS & DEBUG_NEGO) {
4228 sym_print_nego_msg(np, target, "wide msgin", np->msgin);
4232 * Get requested values.
4235 wide = np->msgin[3];
4238 * Check values against our limits.
4240 if (wide > np->maxwide) {
4245 if (DEBUG_FLAGS & DEBUG_NEGO) {
4246 PRINT_TARGET(np, target);
4247 printf ("wdtr: wide=%d chg=%d.\n", wide, chg);
4251 * If it was an answer we want to change,
4252 * then it isn't acceptable. Reject it.
4260 sym_setwide (np, target, wide);
4263 * It was an answer. We are done.
4269 * It was a request. Prepare an answer message.
4271 np->msgout[0] = M_EXTENDED;
4273 np->msgout[2] = M_X_WIDE_REQ;
4274 np->msgout[3] = wide;
4276 np->msgin [0] = M_NOOP;
4278 if (DEBUG_FLAGS & DEBUG_NEGO) {
4279 sym_print_nego_msg(np, target, "wide msgout", np->msgout);
4288 static void sym_wide_nego(struct sym_hcb *np, tcb_p tp, ccb_p cp)
4294 * Request or answer ?
4296 if (INB (HS_PRT) == HS_NEGOTIATE) {
4297 OUTB (HS_PRT, HS_BUSY);
4298 if (cp->nego_status && cp->nego_status != NS_WIDE)
4304 * Check and apply new values.
4306 result = sym_wide_nego_check(np, req, cp->target);
4307 if (result) /* Not acceptable, reject it */
4309 if (req) { /* Was a request, send response. */
4310 cp->nego_status = NS_WIDE;
4311 OUTL_DSP (SCRIPTB_BA (np, wdtr_resp));
4313 else { /* Was a response. */
4315 * Negotiate for SYNC immediately after WIDE response.
4316 * This allows to negotiate for both WIDE and SYNC on
4317 * a single SCSI command (Suggested by Justin Gibbs).
4319 if (tp->tinfo.goal.offset) {
4320 np->msgout[0] = M_EXTENDED;
4322 np->msgout[2] = M_X_SYNC_REQ;
4323 np->msgout[3] = tp->tinfo.goal.period;
4324 np->msgout[4] = tp->tinfo.goal.offset;
4326 if (DEBUG_FLAGS & DEBUG_NEGO) {
4327 sym_print_nego_msg(np, cp->target,
4328 "sync msgout", np->msgout);
4331 cp->nego_status = NS_SYNC;
4332 OUTB (HS_PRT, HS_NEGOTIATE);
4333 OUTL_DSP (SCRIPTB_BA (np, sdtr_resp));
4337 OUTL_DSP (SCRIPTA_BA (np, clrack));
4343 OUTL_DSP (SCRIPTB_BA (np, msg_bad));
4347 * Reset DT, SYNC or WIDE to default settings.
4349 * Called when a negotiation does not succeed either
4350 * on rejection or on protocol error.
4352 * A target that understands a PPR message should never
4353 * reject it, and messing with it is very unlikely.
4354 * So, if a PPR makes problems, we may just want to
4355 * try a legacy negotiation later.
4357 static void sym_nego_default(struct sym_hcb *np, tcb_p tp, ccb_p cp)
4359 switch (cp->nego_status) {
4362 sym_setpprot (np, cp->target, 0, 0, 0, 0, 0, 0);
4364 tp->tinfo.goal.options = 0;
4365 if (tp->tinfo.goal.period < np->minsync)
4366 tp->tinfo.goal.period = np->minsync;
4367 if (tp->tinfo.goal.offset > np->maxoffs)
4368 tp->tinfo.goal.offset = np->maxoffs;
4372 sym_setsync (np, cp->target, 0, 0, 0, 0);
4375 sym_setwide (np, cp->target, 0);
4378 np->msgin [0] = M_NOOP;
4379 np->msgout[0] = M_NOOP;
4380 cp->nego_status = 0;
4384 * chip handler for MESSAGE REJECT received in response to
4385 * PPR, WIDE or SYNCHRONOUS negotiation.
4387 static void sym_nego_rejected(struct sym_hcb *np, tcb_p tp, ccb_p cp)
4389 sym_nego_default(np, tp, cp);
4390 OUTB (HS_PRT, HS_BUSY);
4394 * chip exception handler for programmed interrupts.
4396 static void sym_int_sir (struct sym_hcb *np)
4398 u_char num = INB (nc_dsps);
4399 u32 dsa = INL (nc_dsa);
4400 ccb_p cp = sym_ccb_from_dsa(np, dsa);
4401 u_char target = INB (nc_sdid) & 0x0f;
4402 tcb_p tp = &np->target[target];
4405 if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
4408 #if SYM_CONF_DMA_ADDRESSING_MODE == 2
4410 * SCRIPTS tell us that we may have to update
4411 * 64 bit DMA segment registers.
4413 case SIR_DMAP_DIRTY:
4414 sym_update_dmap_regs(np);
4418 * Command has been completed with error condition
4419 * or has been auto-sensed.
4421 case SIR_COMPLETE_ERROR:
4422 sym_complete_error(np, cp);
4425 * The C code is currently trying to recover from something.
4426 * Typically, user want to abort some command.
4428 case SIR_SCRIPT_STOPPED:
4429 case SIR_TARGET_SELECTED:
4430 case SIR_ABORT_SENT:
4431 sym_sir_task_recovery(np, num);
4434 * The device didn't go to MSG OUT phase after having
4435 * been selected with ATN. We donnot want to handle
4438 case SIR_SEL_ATN_NO_MSG_OUT:
4439 printf ("%s:%d: No MSG OUT phase after selection with ATN.\n",
4440 sym_name (np), target);
4443 * The device didn't switch to MSG IN phase after
4444 * having reseleted the initiator.
4446 case SIR_RESEL_NO_MSG_IN:
4447 printf ("%s:%d: No MSG IN phase after reselection.\n",
4448 sym_name (np), target);
4451 * After reselection, the device sent a message that wasn't
4454 case SIR_RESEL_NO_IDENTIFY:
4455 printf ("%s:%d: No IDENTIFY after reselection.\n",
4456 sym_name (np), target);
4459 * The device reselected a LUN we donnot know about.
4461 case SIR_RESEL_BAD_LUN:
4462 np->msgout[0] = M_RESET;
4465 * The device reselected for an untagged nexus and we
4468 case SIR_RESEL_BAD_I_T_L:
4469 np->msgout[0] = M_ABORT;
4472 * The device reselected for a tagged nexus that we donnot
4475 case SIR_RESEL_BAD_I_T_L_Q:
4476 np->msgout[0] = M_ABORT_TAG;
4479 * The SCRIPTS let us know that the device has grabbed
4480 * our message and will abort the job.
4482 case SIR_RESEL_ABORTED:
4483 np->lastmsg = np->msgout[0];
4484 np->msgout[0] = M_NOOP;
4485 printf ("%s:%d: message %x sent on bad reselection.\n",
4486 sym_name (np), target, np->lastmsg);
4489 * The SCRIPTS let us know that a message has been
4490 * successfully sent to the device.
4492 case SIR_MSG_OUT_DONE:
4493 np->lastmsg = np->msgout[0];
4494 np->msgout[0] = M_NOOP;
4495 /* Should we really care of that */
4496 if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
4498 cp->xerr_status &= ~XE_PARITY_ERR;
4499 if (!cp->xerr_status)
4500 OUTOFFB (HF_PRT, HF_EXT_ERR);
4505 * The device didn't send a GOOD SCSI status.
4506 * We may have some work to do prior to allow
4507 * the SCRIPTS processor to continue.
4509 case SIR_BAD_SCSI_STATUS:
4512 sym_sir_bad_scsi_status(np, num, cp);
4515 * We are asked by the SCRIPTS to prepare a
4518 case SIR_REJECT_TO_SEND:
4519 sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
4520 np->msgout[0] = M_REJECT;
4523 * We have been ODD at the end of a DATA IN
4524 * transfer and the device didn't send a
4525 * IGNORE WIDE RESIDUE message.
4526 * It is a data overrun condition.
4528 case SIR_SWIDE_OVERRUN:
4530 OUTONB (HF_PRT, HF_EXT_ERR);
4531 cp->xerr_status |= XE_SWIDE_OVRUN;
4535 * We have been ODD at the end of a DATA OUT
4537 * It is a data underrun condition.
4539 case SIR_SODL_UNDERRUN:
4541 OUTONB (HF_PRT, HF_EXT_ERR);
4542 cp->xerr_status |= XE_SODL_UNRUN;
4546 * The device wants us to tranfer more data than
4547 * expected or in the wrong direction.
4548 * The number of extra bytes is in scratcha.
4549 * It is a data overrun condition.
4551 case SIR_DATA_OVERRUN:
4553 OUTONB (HF_PRT, HF_EXT_ERR);
4554 cp->xerr_status |= XE_EXTRA_DATA;
4555 cp->extra_bytes += INL (nc_scratcha);
4559 * The device switched to an illegal phase (4/5).
4563 OUTONB (HF_PRT, HF_EXT_ERR);
4564 cp->xerr_status |= XE_BAD_PHASE;
4568 * We received a message.
4570 case SIR_MSG_RECEIVED:
4573 switch (np->msgin [0]) {
4575 * We received an extended message.
4576 * We handle MODIFY DATA POINTER, SDTR, WDTR
4577 * and reject all other extended messages.
4580 switch (np->msgin [2]) {
4582 if (DEBUG_FLAGS & DEBUG_POINTER)
4583 sym_print_msg(cp,"modify DP",np->msgin);
4584 tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
4585 (np->msgin[5]<<8) + (np->msgin[6]);
4586 sym_modify_dp(np, tp, cp, tmp);
4589 sym_sync_nego(np, tp, cp);
4592 sym_ppr_nego(np, tp, cp);
4595 sym_wide_nego(np, tp, cp);
4602 * We received a 1/2 byte message not handled from SCRIPTS.
4603 * We are only expecting MESSAGE REJECT and IGNORE WIDE
4604 * RESIDUE messages that haven't been anticipated by
4605 * SCRIPTS on SWIDE full condition. Unanticipated IGNORE
4606 * WIDE RESIDUE messages are aliased as MODIFY DP (-1).
4609 if (DEBUG_FLAGS & DEBUG_POINTER)
4610 sym_print_msg(cp,"ign wide residue", np->msgin);
4611 if (cp->host_flags & HF_SENSE)
4612 OUTL_DSP (SCRIPTA_BA (np, clrack));
4614 sym_modify_dp(np, tp, cp, -1);
4617 if (INB (HS_PRT) == HS_NEGOTIATE)
4618 sym_nego_rejected(np, tp, cp);
4621 printf ("M_REJECT received (%x:%x).\n",
4622 scr_to_cpu(np->lastmsg), np->msgout[0]);
4631 * We received an unknown message.
4632 * Ignore all MSG IN phases and reject it.
4635 sym_print_msg(cp, "WEIRD message received", np->msgin);
4636 OUTL_DSP (SCRIPTB_BA (np, msg_weird));
4639 * Negotiation failed.
4640 * Target does not send us the reply.
4641 * Remove the HS_NEGOTIATE status.
4643 case SIR_NEGO_FAILED:
4644 OUTB (HS_PRT, HS_BUSY);
4646 * Negotiation failed.
4647 * Target does not want answer message.
4649 case SIR_NEGO_PROTO:
4650 sym_nego_default(np, tp, cp);
4658 OUTL_DSP (SCRIPTB_BA (np, msg_bad));
4661 OUTL_DSP (SCRIPTA_BA (np, clrack));
4668 * Acquire a control block
4670 ccb_p sym_get_ccb (struct sym_hcb *np, u_char tn, u_char ln, u_char tag_order)
4672 tcb_p tp = &np->target[tn];
4673 lcb_p lp = sym_lp(np, tp, ln);
4674 u_short tag = NO_TAG;
4676 ccb_p cp = (ccb_p) 0;
4679 * Look for a free CCB
4681 if (sym_que_empty(&np->free_ccbq))
4682 (void) sym_alloc_ccb(np);
4683 qp = sym_remque_head(&np->free_ccbq);
4686 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
4688 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4690 * If the LCB is not yet available and the LUN
4691 * has been probed ok, try to allocate the LCB.
4693 if (!lp && sym_is_bit(tp->lun_map, ln)) {
4694 lp = sym_alloc_lcb(np, tn, ln);
4701 * If the LCB is not available here, then the
4702 * logical unit is not yet discovered. For those
4703 * ones only accept 1 SCSI IO per logical unit,
4704 * since we cannot allow disconnections.
4707 if (!sym_is_bit(tp->busy0_map, ln))
4708 sym_set_bit(tp->busy0_map, ln);
4713 * If we have been asked for a tagged command.
4717 * Debugging purpose.
4719 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4720 assert(lp->busy_itl == 0);
4723 * Allocate resources for tags if not yet.
4726 sym_alloc_lcb_tags(np, tn, ln);
4731 * Get a tag for this SCSI IO and set up
4732 * the CCB bus address for reselection,
4733 * and count it for this LUN.
4734 * Toggle reselect path to tagged.
4736 if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
4737 tag = lp->cb_tags[lp->ia_tag];
4738 if (++lp->ia_tag == SYM_CONF_MAX_TASK)
4741 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4742 lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
4744 cpu_to_scr(SCRIPTA_BA (np, resel_tag));
4746 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
4747 cp->tags_si = lp->tags_si;
4748 ++lp->tags_sum[cp->tags_si];
4756 * This command will not be tagged.
4757 * If we already have either a tagged or untagged
4758 * one, refuse to overlap this untagged one.
4762 * Debugging purpose.
4764 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4765 assert(lp->busy_itl == 0 && lp->busy_itlq == 0);
4768 * Count this nexus for this LUN.
4769 * Set up the CCB bus address for reselection.
4770 * Toggle reselect path to untagged.
4773 #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
4774 if (lp->busy_itl == 1) {
4775 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
4777 cpu_to_scr(SCRIPTA_BA (np, resel_no_tag));
4785 * Put the CCB into the busy queue.
4787 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
4788 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4790 sym_remque(&cp->link2_ccbq);
4791 sym_insque_tail(&cp->link2_ccbq, &lp->waiting_ccbq);
4796 * Remember all informations needed to free this CCB.
4800 cp->order = tag_order;
4804 if (DEBUG_FLAGS & DEBUG_TAGS) {
4805 PRINT_LUN(np, tn, ln);
4806 printf ("ccb @%p using tag %d.\n", cp, tag);
4812 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4817 * Release one control block
4819 void sym_free_ccb (struct sym_hcb *np, ccb_p cp)
4821 tcb_p tp = &np->target[cp->target];
4822 lcb_p lp = sym_lp(np, tp, cp->lun);
4824 if (DEBUG_FLAGS & DEBUG_TAGS) {
4825 PRINT_LUN(np, cp->target, cp->lun);
4826 printf ("ccb @%p freeing tag %d.\n", cp, cp->tag);
4834 * If tagged, release the tag, set the relect path
4836 if (cp->tag != NO_TAG) {
4837 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
4838 --lp->tags_sum[cp->tags_si];
4841 * Free the tag value.
4843 lp->cb_tags[lp->if_tag] = cp->tag;
4844 if (++lp->if_tag == SYM_CONF_MAX_TASK)
4847 * Make the reselect path invalid,
4848 * and uncount this CCB.
4850 lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
4852 } else { /* Untagged */
4854 * Make the reselect path invalid,
4855 * and uncount this CCB.
4857 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
4861 * If no JOB active, make the LUN reselect path invalid.
4863 if (lp->busy_itlq == 0 && lp->busy_itl == 0)
4865 cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun));
4868 * Otherwise, we only accept 1 IO per LUN.
4869 * Clear the bit that keeps track of this IO.
4872 sym_clr_bit(tp->busy0_map, cp->lun);
4875 * We donnot queue more than 1 ccb per target
4876 * with negotiation at any time. If this ccb was
4877 * used for negotiation, clear this info in the tcb.
4879 if (cp == tp->nego_cp)
4882 #ifdef SYM_CONF_IARB_SUPPORT
4884 * If we just complete the last queued CCB,
4885 * clear this info that is no longer relevant.
4887 if (cp == np->last_cp)
4892 * Unmap user data from DMA map if needed.
4894 sym_data_dmamap_unload(np, cp);
4897 * Make this CCB available.
4900 cp->host_status = HS_IDLE;
4901 sym_remque(&cp->link_ccbq);
4902 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4904 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4906 sym_remque(&cp->link2_ccbq);
4907 sym_insque_tail(&cp->link2_ccbq, &np->dummy_ccbq);
4909 if (cp->tag != NO_TAG)
4912 --lp->started_no_tag;
4920 * Allocate a CCB from memory and initialize its fixed part.
4922 static ccb_p sym_alloc_ccb(struct sym_hcb *np)
4928 * Prevent from allocating more CCBs than we can
4929 * queue to the controller.
4931 if (np->actccbs >= SYM_CONF_MAX_START)
4935 * Allocate memory for this CCB.
4937 cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
4942 * Allocate a bounce buffer for sense data.
4944 cp->sns_bbuf = sym_calloc_dma(SYM_SNS_BBUF_LEN, "SNS_BBUF");
4949 * Allocate a map for the DMA of user data.
4951 if (sym_data_dmamap_create(np, cp))
4960 * Compute the bus address of this ccb.
4962 cp->ccb_ba = vtobus(cp);
4965 * Insert this ccb into the hashed list.
4967 hcode = CCB_HASH_CODE(cp->ccb_ba);
4968 cp->link_ccbh = np->ccbh[hcode];
4969 np->ccbh[hcode] = cp;
4972 * Initialyze the start and restart actions.
4974 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, idle));
4975 cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l));
4978 * Initilialyze some other fields.
4980 cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
4983 * Chain into free ccb queue.
4985 sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
4988 * Chain into optionnal lists.
4990 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
4991 sym_insque_head(&cp->link2_ccbq, &np->dummy_ccbq);
4997 sym_mfree_dma(cp->sns_bbuf,SYM_SNS_BBUF_LEN,"SNS_BBUF");
4998 sym_mfree_dma(cp, sizeof(*cp), "CCB");
5004 * Look up a CCB from a DSA value.
5006 static ccb_p sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa)
5011 hcode = CCB_HASH_CODE(dsa);
5012 cp = np->ccbh[hcode];
5014 if (cp->ccb_ba == dsa)
5023 * Target control block initialisation.
5024 * Nothing important to do at the moment.
5026 static void sym_init_tcb (struct sym_hcb *np, u_char tn)
5028 #if 0 /* Hmmm... this checking looks paranoid. */
5030 * Check some alignments required by the chip.
5032 assert (((offsetof(struct sym_reg, nc_sxfer) ^
5033 offsetof(struct sym_tcb, head.sval)) &3) == 0);
5034 assert (((offsetof(struct sym_reg, nc_scntl3) ^
5035 offsetof(struct sym_tcb, head.wval)) &3) == 0);
5040 * Lun control block allocation and initialization.
5042 lcb_p sym_alloc_lcb (struct sym_hcb *np, u_char tn, u_char ln)
5044 tcb_p tp = &np->target[tn];
5045 lcb_p lp = sym_lp(np, tp, ln);
5048 * Already done, just return.
5054 * Donnot allow LUN control block
5055 * allocation for not probed LUNs.
5057 if (!sym_is_bit(tp->lun_map, ln))
5061 * Initialize the target control block if not yet.
5063 sym_init_tcb (np, tn);
5066 * Allocate the LCB bus address array.
5067 * Compute the bus address of this table.
5069 if (ln && !tp->luntbl) {
5072 tp->luntbl = sym_calloc_dma(256, "LUNTBL");
5075 for (i = 0 ; i < 64 ; i++)
5076 tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
5077 tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
5081 * Allocate the table of pointers for LUN(s) > 0, if needed.
5083 if (ln && !tp->lunmp) {
5084 tp->lunmp = sym_calloc(SYM_CONF_MAX_LUN * sizeof(lcb_p),
5092 * Make it available to the chip.
5094 lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
5099 tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
5103 tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
5107 * Let the itl task point to error handling.
5109 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
5112 * Set the reselect pattern to our default. :)
5114 lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun));
5117 * Set user capabilities.
5119 lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
5121 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5123 * Initialize device queueing.
5125 sym_que_init(&lp->waiting_ccbq);
5126 sym_que_init(&lp->started_ccbq);
5127 lp->started_max = SYM_CONF_MAX_TASK;
5128 lp->started_limit = SYM_CONF_MAX_TASK;
5131 * If we are busy, count the IO.
5133 if (sym_is_bit(tp->busy0_map, ln)) {
5135 sym_clr_bit(tp->busy0_map, ln);
5142 * Allocate LCB resources for tagged command queuing.
5144 static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln)
5146 tcb_p tp = &np->target[tn];
5147 lcb_p lp = sym_lp(np, tp, ln);
5151 * If LCB not available, try to allocate it.
5153 if (!lp && !(lp = sym_alloc_lcb(np, tn, ln)))
5157 * Allocate the task table and and the tag allocation
5158 * circular buffer. We want both or none.
5160 lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
5163 lp->cb_tags = sym_calloc(SYM_CONF_MAX_TASK, "CB_TAGS");
5165 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
5166 lp->itlq_tbl = NULL;
5171 * Initialize the task table with invalid entries.
5173 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
5174 lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
5177 * Fill up the tag buffer with tag numbers.
5179 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
5183 * Make the task table available to SCRIPTS,
5184 * And accept tagged commands now.
5186 lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
5194 * Queue a SCSI IO to the controller.
5196 int sym_queue_scsiio(struct sym_hcb *np, struct scsi_cmnd *csio, ccb_p cp)
5205 * Keep track of the IO in our CCB.
5210 * Retrieve the target descriptor.
5212 tp = &np->target[cp->target];
5215 * Retrieve the lun descriptor.
5217 lp = sym_lp(np, tp, cp->lun);
5219 can_disconnect = (cp->tag != NO_TAG) ||
5220 (lp && (lp->curr_flags & SYM_DISC_ENABLED));
5222 msgptr = cp->scsi_smsg;
5224 msgptr[msglen++] = IDENTIFY(can_disconnect, cp->lun);
5227 * Build the tag message if present.
5229 if (cp->tag != NO_TAG) {
5230 u_char order = cp->order;
5238 order = M_SIMPLE_TAG;
5240 #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
5242 * Avoid too much reordering of SCSI commands.
5243 * The algorithm tries to prevent completion of any
5244 * tagged command from being delayed against more
5245 * than 3 times the max number of queued commands.
5247 if (lp && lp->tags_since > 3*SYM_CONF_MAX_TAG) {
5248 lp->tags_si = !(lp->tags_si);
5249 if (lp->tags_sum[lp->tags_si]) {
5250 order = M_ORDERED_TAG;
5251 if ((DEBUG_FLAGS & DEBUG_TAGS)||sym_verbose>1) {
5253 printf("ordered tag forced.\n");
5259 msgptr[msglen++] = order;
5262 * For less than 128 tags, actual tags are numbered
5263 * 1,3,5,..2*MAXTAGS+1,since we may have to deal
5264 * with devices that have problems with #TAG 0 or too
5265 * great #TAG numbers. For more tags (up to 256),
5266 * we use directly our tag number.
5268 #if SYM_CONF_MAX_TASK > (512/4)
5269 msgptr[msglen++] = cp->tag;
5271 msgptr[msglen++] = (cp->tag << 1) + 1;
5276 * Build a negotiation message if needed.
5277 * (nego_status is filled by sym_prepare_nego())
5279 cp->nego_status = 0;
5280 if (tp->tinfo.curr.width != tp->tinfo.goal.width ||
5281 tp->tinfo.curr.period != tp->tinfo.goal.period ||
5282 tp->tinfo.curr.offset != tp->tinfo.goal.offset ||
5283 tp->tinfo.curr.options != tp->tinfo.goal.options) {
5284 if (!tp->nego_cp && lp)
5285 msglen += sym_prepare_nego(np, cp, msgptr + msglen);
5291 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, select));
5292 cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA (np, resel_dsa));
5297 cp->phys.select.sel_id = cp->target;
5298 cp->phys.select.sel_scntl3 = tp->head.wval;
5299 cp->phys.select.sel_sxfer = tp->head.sval;
5300 cp->phys.select.sel_scntl4 = tp->head.uval;
5305 cp->phys.smsg.addr = cpu_to_scr(CCB_BA (cp, scsi_smsg));
5306 cp->phys.smsg.size = cpu_to_scr(msglen);
5311 cp->host_xflags = 0;
5312 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
5313 cp->ssss_status = S_ILLEGAL;
5314 cp->xerr_status = 0;
5316 cp->extra_bytes = 0;
5319 * extreme data pointer.
5320 * shall be positive, so -1 is lower than lowest.:)
5326 * Build the CDB and DATA descriptor block
5329 return sym_setup_data_and_start(np, csio, cp);
5333 * Reset a SCSI target (all LUNs of this target).
5335 int sym_reset_scsi_target(struct sym_hcb *np, int target)
5339 if (target == np->myaddr || (u_int)target >= SYM_CONF_MAX_TARGET)
5342 tp = &np->target[target];
5345 np->istat_sem = SEM;
5346 OUTB (nc_istat, SIGP|SEM);
5354 int sym_abort_ccb(struct sym_hcb *np, ccb_p cp, int timed_out)
5357 * Check that the IO is active.
5359 if (!cp || !cp->host_status || cp->host_status == HS_WAIT)
5363 * If a previous abort didn't succeed in time,
5364 * perform a BUS reset.
5367 sym_reset_scsi_bus(np, 1);
5372 * Mark the CCB for abort and allow time for.
5374 cp->to_abort = timed_out ? 2 : 1;
5377 * Tell the SCRIPTS processor to stop and synchronize with us.
5379 np->istat_sem = SEM;
5380 OUTB (nc_istat, SIGP|SEM);
5384 int sym_abort_scsiio(struct sym_hcb *np, struct scsi_cmnd *ccb, int timed_out)
5390 * Look up our CCB control block.
5393 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
5394 ccb_p cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
5395 if (cp2->cam_ccb == ccb) {
5401 return sym_abort_ccb(np, cp, timed_out);
5405 * Complete execution of a SCSI command with extented
5406 * error, SCSI status error, or having been auto-sensed.
5408 * The SCRIPTS processor is not running there, so we
5409 * can safely access IO registers and remove JOBs from
5411 * SCRATCHA is assumed to have been loaded with STARTPOS
5412 * before the SCRIPTS called the C code.
5414 void sym_complete_error (struct sym_hcb *np, ccb_p cp)
5422 * Paranoid check. :)
5424 if (!cp || !cp->cam_ccb)
5427 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
5428 printf ("CCB=%lx STAT=%x/%x/%x DEV=%d/%d\n", (unsigned long)cp,
5429 cp->host_status, cp->ssss_status, cp->host_flags,
5430 cp->target, cp->lun);
5434 * Get target and lun pointers.
5436 tp = &np->target[cp->target];
5437 lp = sym_lp(np, tp, cp->lun);
5440 * Check for extended errors.
5442 if (cp->xerr_status) {
5444 sym_print_xerr(cp, cp->xerr_status);
5445 if (cp->host_status == HS_COMPLETE)
5446 cp->host_status = HS_COMP_ERR;
5450 * Calculate the residual.
5452 resid = sym_compute_residual(np, cp);
5454 if (!SYM_SETUP_RESIDUAL_SUPPORT) {/* If user does not want residuals */
5455 resid = 0; /* throw them away. :) */
5460 printf("XXXX RESID= %d - 0x%x\n", resid, resid);
5464 * Dequeue all queued CCBs for that device
5465 * not yet started by SCRIPTS.
5467 i = (INL (nc_scratcha) - np->squeue_ba) / 4;
5468 i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
5471 * Restart the SCRIPTS processor.
5473 OUTL_DSP (SCRIPTA_BA (np, start));
5475 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5476 if (cp->host_status == HS_COMPLETE &&
5477 cp->ssss_status == S_QUEUE_FULL) {
5478 if (!lp || lp->started_tags - i < 2)
5481 * Decrease queue depth as needed.
5483 lp->started_max = lp->started_tags - i - 1;
5486 if (sym_verbose >= 2) {
5487 PRINT_LUN(np, cp->target, cp->lun);
5488 printf(" queue depth is now %d\n", lp->started_max);
5494 cp->host_status = HS_BUSY;
5495 cp->ssss_status = S_ILLEGAL;
5498 * Let's requeue it to device.
5500 sym_set_cam_status(cp->cam_ccb, CAM_REQUEUE_REQ);
5506 * Synchronize DMA map if needed.
5508 sym_data_dmamap_postsync(np, cp);
5511 * Build result in CAM ccb.
5513 sym_set_cam_result_error(np, cp, resid);
5515 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5519 * Add this one to the COMP queue.
5521 sym_remque(&cp->link_ccbq);
5522 sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
5525 * Complete all those commands with either error
5526 * or requeue condition.
5528 sym_flush_comp_queue(np, 0);
5530 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5532 * Donnot start more than 1 command after an error.
5535 sym_start_next_ccbs(np, lp, 1);
5540 * Complete execution of a successful SCSI command.
5542 * Only successful commands go to the DONE queue,
5543 * since we need to have the SCRIPTS processor
5544 * stopped on any error condition.
5545 * The SCRIPTS processor is running while we are
5546 * completing successful commands.
5548 void sym_complete_ok (struct sym_hcb *np, ccb_p cp)
5552 struct scsi_cmnd *ccb;
5556 * Paranoid check. :)
5558 if (!cp || !cp->cam_ccb)
5560 assert (cp->host_status == HS_COMPLETE);
5568 * Get target and lun pointers.
5570 tp = &np->target[cp->target];
5571 lp = sym_lp(np, tp, cp->lun);
5574 * Assume device discovered on first success.
5577 sym_set_bit(tp->lun_map, cp->lun);
5580 * If all data have been transferred, given than no
5581 * extended error did occur, there is no residual.
5584 if (cp->phys.head.lastp != sym_goalp(cp))
5585 resid = sym_compute_residual(np, cp);
5588 * Wrong transfer residuals may be worse than just always
5589 * returning zero. User can disable this feature from
5590 * sym_conf.h. Residual support is enabled by default.
5592 if (!SYM_SETUP_RESIDUAL_SUPPORT)
5596 printf("XXXX RESID= %d - 0x%x\n", resid, resid);
5600 * Synchronize DMA map if needed.
5602 sym_data_dmamap_postsync(np, cp);
5605 * Build result in CAM ccb.
5607 sym_set_cam_result_ok(np, cp, resid);
5609 #ifdef SYM_OPT_SNIFF_INQUIRY
5611 * On standard INQUIRY response (EVPD and CmDt
5612 * not set), sniff out device capabilities.
5614 if (cp->cdb_buf[0] == INQUIRY && !(cp->cdb_buf[1] & 0x3))
5615 sym_sniff_inquiry(np, cp->cam_ccb, resid);
5618 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5620 * If max number of started ccbs had been reduced,
5621 * increase it if 200 good status received.
5623 if (lp && lp->started_max < lp->started_limit) {
5625 if (lp->num_sgood >= 200) {
5628 if (sym_verbose >= 2) {
5629 PRINT_LUN(np, cp->target, cp->lun);
5630 printf(" queue depth is now %d\n",
5640 sym_free_ccb (np, cp);
5642 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5644 * Requeue a couple of awaiting scsi commands.
5646 if (lp && !sym_que_empty(&lp->waiting_ccbq))
5647 sym_start_next_ccbs(np, lp, 2);
5650 * Complete the command.
5652 sym_xpt_done(np, ccb);
5656 * Soft-attach the controller.
5658 int sym_hcb_attach(struct sym_hcb *np, struct sym_fw *fw, struct sym_nvram *nvram)
5663 * Get some info about the firmware.
5665 np->scripta_sz = fw->a_size;
5666 np->scriptb_sz = fw->b_size;
5667 np->scriptz_sz = fw->z_size;
5668 np->fw_setup = fw->setup;
5669 np->fw_patch = fw->patch;
5670 np->fw_name = fw->name;
5673 * Save setting of some IO registers, so we will
5674 * be able to probe specific implementations.
5676 sym_save_initial_setting (np);
5679 * Reset the chip now, since it has been reported
5680 * that SCSI clock calibration may not work properly
5681 * if the chip is currently active.
5683 sym_chip_reset (np);
5686 * Prepare controller and devices settings, according
5687 * to chip features, user set-up and driver set-up.
5689 (void) sym_prepare_setting(np, nvram);
5692 * Check the PCI clock frequency.
5693 * Must be performed after prepare_setting since it destroys
5694 * STEST1 that is used to probe for the clock doubler.
5696 i = sym_getpciclock(np);
5697 if (i > 37000 && !(np->features & FE_66MHZ))
5698 printf("%s: PCI BUS clock seems too high: %u KHz.\n",
5702 * Allocate the start queue.
5704 np->squeue = (u32 *) sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
5707 np->squeue_ba = vtobus(np->squeue);
5710 * Allocate the done queue.
5712 np->dqueue = (u32 *) sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
5715 np->dqueue_ba = vtobus(np->dqueue);
5718 * Allocate the target bus address array.
5720 np->targtbl = (u32 *) sym_calloc_dma(256, "TARGTBL");
5723 np->targtbl_ba = vtobus(np->targtbl);
5726 * Allocate SCRIPTS areas.
5728 np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
5729 np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
5730 np->scriptz0 = sym_calloc_dma(np->scriptz_sz, "SCRIPTZ0");
5731 if (!np->scripta0 || !np->scriptb0 || !np->scriptz0)
5735 * Allocate the array of lists of CCBs hashed by DSA.
5737 np->ccbh = sym_calloc(sizeof(ccb_p *)*CCB_HASH_SIZE, "CCBH");
5742 * Initialyze the CCB free and busy queues.
5744 sym_que_init(&np->free_ccbq);
5745 sym_que_init(&np->busy_ccbq);
5746 sym_que_init(&np->comp_ccbq);
5749 * Initialization for optional handling
5750 * of device queueing.
5752 #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
5753 sym_que_init(&np->dummy_ccbq);
5756 * Allocate some CCB. We need at least ONE.
5758 if (!sym_alloc_ccb(np))
5762 * Calculate BUS addresses where we are going
5763 * to load the SCRIPTS.
5765 np->scripta_ba = vtobus(np->scripta0);
5766 np->scriptb_ba = vtobus(np->scriptb0);
5767 np->scriptz_ba = vtobus(np->scriptz0);
5770 np->scripta_ba = np->ram_ba;
5771 if (np->features & FE_RAM8K) {
5773 np->scriptb_ba = np->scripta_ba + 4096;
5774 #if 0 /* May get useful for 64 BIT PCI addressing */
5775 np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32);
5783 * Copy scripts to controller instance.
5785 memcpy(np->scripta0, fw->a_base, np->scripta_sz);
5786 memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
5787 memcpy(np->scriptz0, fw->z_base, np->scriptz_sz);
5790 * Setup variable parts in scripts and compute
5791 * scripts bus addresses used from the C code.
5793 np->fw_setup(np, fw);
5796 * Bind SCRIPTS with physical addresses usable by the
5797 * SCRIPTS processor (as seen from the BUS = BUS addresses).
5799 sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
5800 sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
5801 sym_fw_bind_script(np, (u32 *) np->scriptz0, np->scriptz_sz);
5803 #ifdef SYM_CONF_IARB_SUPPORT
5805 * If user wants IARB to be set when we win arbitration
5806 * and have other jobs, compute the max number of consecutive
5807 * settings of IARB hints before we leave devices a chance to
5808 * arbitrate for reselection.
5810 #ifdef SYM_SETUP_IARB_MAX
5811 np->iarb_max = SYM_SETUP_IARB_MAX;
5818 * Prepare the idle and invalid task actions.
5820 np->idletask.start = cpu_to_scr(SCRIPTA_BA (np, idle));
5821 np->idletask.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l));
5822 np->idletask_ba = vtobus(&np->idletask);
5824 np->notask.start = cpu_to_scr(SCRIPTA_BA (np, idle));
5825 np->notask.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l));
5826 np->notask_ba = vtobus(&np->notask);
5828 np->bad_itl.start = cpu_to_scr(SCRIPTA_BA (np, idle));
5829 np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l));
5830 np->bad_itl_ba = vtobus(&np->bad_itl);
5832 np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA (np, idle));
5833 np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA (np,bad_i_t_l_q));
5834 np->bad_itlq_ba = vtobus(&np->bad_itlq);
5837 * Allocate and prepare the lun JUMP table that is used
5838 * for a target prior the probing of devices (bad lun table).
5839 * A private table will be allocated for the target on the
5840 * first INQUIRY response received.
5842 np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
5846 np->badlun_sa = cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun));
5847 for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */
5848 np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
5851 * Prepare the bus address array that contains the bus
5852 * address of each target control block.
5853 * For now, assume all logical units are wrong. :)
5855 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
5856 np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
5857 np->target[i].head.luntbl_sa =
5858 cpu_to_scr(vtobus(np->badluntbl));
5859 np->target[i].head.lun0_sa =
5860 cpu_to_scr(vtobus(&np->badlun_sa));
5864 * Now check the cache handling of the pci chipset.
5866 if (sym_snooptest (np)) {
5867 printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np));
5872 * Sigh! we are done.
5881 * Free everything that has been allocated for this device.
5883 void sym_hcb_free(struct sym_hcb *np)
5892 sym_mfree_dma(np->scriptz0, np->scriptz_sz, "SCRIPTZ0");
5894 sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
5896 sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
5898 sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
5900 sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
5903 while ((qp = sym_remque_head(&np->free_ccbq)) != 0) {
5904 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
5905 sym_data_dmamap_destroy(np, cp);
5906 sym_mfree_dma(cp->sns_bbuf, SYM_SNS_BBUF_LEN,
5908 sym_mfree_dma(cp, sizeof(*cp), "CCB");
5912 sym_mfree(np->ccbh, sizeof(ccb_p *)*CCB_HASH_SIZE, "CCBH");
5915 sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
5917 for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
5918 tp = &np->target[target];
5919 for (lun = 0 ; lun < SYM_CONF_MAX_LUN ; lun++) {
5920 lp = sym_lp(np, tp, lun);
5924 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4,
5927 sym_mfree(lp->cb_tags, SYM_CONF_MAX_TASK,
5929 sym_mfree_dma(lp, sizeof(*lp), "LCB");
5931 #if SYM_CONF_MAX_LUN > 1
5933 sym_mfree(tp->lunmp, SYM_CONF_MAX_LUN*sizeof(lcb_p),
5938 sym_mfree_dma(np->targtbl, 256, "TARGTBL");