Import 2.3.18pre1
[davej-history.git] / drivers / scsi / 53c7,8xx.c
blob18c87cec9a359b075f129766baa4fb471132f641
1 /*
2 * PERM_OPTIONS are driver options which will be enabled for all NCR boards
3 * in the system at driver initialization time.
5 * Don't THINK about touching these in PERM_OPTIONS :
6 * OPTION_IO_MAPPED
7 * Memory mapped IO does not work under i86 Linux.
9 * OPTION_DEBUG_TEST1
10 * Test 1 does bus mastering and interrupt tests, which will help weed
11 * out brain damaged main boards.
13 * These are development kernel changes. Code for them included in this
14 * driver release may or may not work. If you turn them on, you should be
15 * running the latest copy of the development sources from
17 * ftp://tsx-11.mit.edu/pub/linux/ALPHA/scsi/53c7,8xx
19 * and be subscribed to the ncr53c810@colorado.edu mailing list. To
20 * subscribe, send mail to majordomo@colorado.edu with
22 * subscribe ncr53c810
24 * in the text.
27 * OPTION_NO_ASYNC
28 * Don't negotiate for asynchronous transfers on the first command
29 * when OPTION_ALWAYS_SYNCHRONOUS is set. Useful for dain bramaged
30 * devices which do something bad rather than sending a MESSAGE
31 * REJECT back to us like they should if they can't cope.
33 * OPTION_SYNCHRONOUS
34 * Enable support for synchronous transfers. Target negotiated
35 * synchronous transfers will be responded to. To initiate
36 * a synchronous transfer request, call
38 * request_synchronous (hostno, target)
40 * from within KGDB.
42 * OPTION_ALWAYS_SYNCHRONOUS
43 * Negotiate for synchronous transfers with every target after
44 * driver initialization or a SCSI bus reset. This is a bit dangerous,
45 * since there are some dain bramaged SCSI devices which will accept
46 * SDTR messages but keep talking asynchronously.
48 * OPTION_DISCONNECT
49 * Enable support for disconnect/reconnect. To change the
50 * default setting on a given host adapter, call
52 * request_disconnect (hostno, allow)
54 * where allow is non-zero to allow, 0 to disallow.
56 * If you really want to run 10MHz FAST SCSI-II transfers, you should
57 * know that the NCR driver currently ignores parity information. Most
58 * systems do 5MHz SCSI fine. I've seen a lot that have problems faster
59 * than 8MHz. To play it safe, we only request 5MHz transfers.
61 * If you'd rather get 10MHz transfers, edit sdtr_message and change
62 * the fourth byte from 50 to 25.
65 #include <linux/config.h>
67 #ifdef CONFIG_SCSI_NCR53C7xx_sync
68 #ifdef CONFIG_SCSI_NCR53C7xx_DISCONNECT
69 #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_DISCONNECT|\
70 OPTION_SYNCHRONOUS|OPTION_ALWAYS_SYNCHRONOUS)
71 #else
72 #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|\
73 OPTION_SYNCHRONOUS|OPTION_ALWAYS_SYNCHRONOUS)
74 #endif
75 #else
76 #ifdef CONFIG_SCSI_NCR53C7xx_DISCONNECT
77 #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_DISCONNECT|\
78 OPTION_SYNCHRONOUS)
79 #else
80 #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_SYNCHRONOUS)
81 #endif
82 #endif
85 * Sponsored by
86 * iX Multiuser Multitasking Magazine
87 * Hannover, Germany
88 * hm@ix.de
90 * Copyright 1993, 1994, 1995 Drew Eckhardt
91 * Visionary Computing
92 * (Unix and Linux consulting and custom programming)
93 * drew@PoohSticks.ORG
94 * +1 (303) 786-7975
96 * TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
98 * For more information, please consult
100 * NCR53C810
101 * SCSI I/O Processor
102 * Programmer's Guide
104 * NCR 53C810
105 * PCI-SCSI I/O Processor
106 * Data Manual
108 * NCR 53C810/53C820
109 * PCI-SCSI I/O Processor Design In Guide
111 * For literature on Symbios Logic Inc. formerly NCR, SCSI,
112 * and Communication products please call (800) 334-5454 or
113 * (719) 536-3300.
115 * PCI BIOS Specification Revision
116 * PCI Local Bus Specification
117 * PCI System Design Guide
119 * PCI Special Interest Group
120 * M/S HF3-15A
121 * 5200 N.E. Elam Young Parkway
122 * Hillsboro, Oregon 97124-6497
123 * +1 (503) 696-2000
124 * +1 (800) 433-5177
128 * Design issues :
129 * The cumulative latency needed to propagate a read/write request
130 * through the file system, buffer cache, driver stacks, SCSI host, and
131 * SCSI device is ultimately the limiting factor in throughput once we
132 * have a sufficiently fast host adapter.
134 * So, to maximize performance we want to keep the ratio of latency to data
135 * transfer time to a minimum by
136 * 1. Minimizing the total number of commands sent (typical command latency
137 * including drive and bus mastering host overhead is as high as 4.5ms)
138 * to transfer a given amount of data.
140 * This is accomplished by placing no arbitrary limit on the number
141 * of scatter/gather buffers supported, since we can transfer 1K
142 * per scatter/gather buffer without Eric's cluster patches,
143 * 4K with.
145 * 2. Minimizing the number of fatal interrupts serviced, since
146 * fatal interrupts halt the SCSI I/O processor. Basically,
147 * this means offloading the practical maximum amount of processing
148 * to the SCSI chip.
150 * On the NCR53c810/820/720, this is accomplished by using
151 * interrupt-on-the-fly signals when commands complete,
152 * and only handling fatal errors and SDTR / WDTR messages
153 * in the host code.
155 * On the NCR53c710, interrupts are generated as on the NCR53c8x0,
156 * only the lack of an interrupt-on-the-fly facility complicates
157 * things. Also, SCSI ID registers and commands are
158 * bit fielded rather than binary encoded.
160 * On the NCR53c700 and NCR53c700-66, operations that are done via
161 * indirect, table mode on the more advanced chips must be
162 * replaced by calls through a jump table which
163 * acts as a surrogate for the DSA. Unfortunately, this
164 * will mean that we must service an interrupt for each
165 * disconnect/reconnect.
167 * 3. Eliminating latency by pipelining operations at the different levels.
169 * This driver allows a configurable number of commands to be enqueued
170 * for each target/lun combination (experimentally, I have discovered
171 * that two seems to work best) and will ultimately allow for
172 * SCSI-II tagged queuing.
175 * Architecture :
176 * This driver is built around a Linux queue of commands waiting to
177 * be executed, and a shared Linux/NCR array of commands to start. Commands
178 * are transfered to the array by the run_process_issue_queue() function
179 * which is called whenever a command completes.
181 * As commands are completed, the interrupt routine is triggered,
182 * looks for commands in the linked list of completed commands with
183 * valid status, removes these commands from a list of running commands,
184 * calls the done routine, and flags their target/luns as not busy.
186 * Due to limitations in the intelligence of the NCR chips, certain
187 * concessions are made. In many cases, it is easier to dynamically
188 * generate/fix-up code rather than calculate on the NCR at run time.
189 * So, code is generated or fixed up for
191 * - Handling data transfers, using a variable number of MOVE instructions
192 * interspersed with CALL MSG_IN, WHEN MSGIN instructions.
194 * The DATAIN and DATAOUT routines are separate, so that an incorrect
195 * direction can be trapped, and space isn't wasted.
197 * It may turn out that we're better off using some sort
198 * of table indirect instruction in a loop with a variable
199 * sized table on the NCR53c710 and newer chips.
201 * - Checking for reselection (NCR53c710 and better)
203 * - Handling the details of SCSI context switches (NCR53c710 and better),
204 * such as reprogramming appropriate synchronous parameters,
205 * removing the dsa structure from the NCR's queue of outstanding
206 * commands, etc.
211 * Accommodate differences between stock 1.2.x and 1.3.x asm-i386/types.h
212 * so lusers can drop in 53c7,8xx.* and get something which compiles
213 * without warnings.
216 #if !defined(LINUX_1_2) && !defined(LINUX_1_3)
217 #include <linux/version.h>
218 #if LINUX_VERSION_CODE > 65536 + 3 * 256
219 #define LINUX_1_3
220 #else
221 #define LINUX_1_2
222 #endif
223 #endif
225 #ifdef LINUX_1_2
226 #define u32 bogus_u32
227 #define s32 bogus_s32
228 #include <asm/types.h>
229 #undef u32
230 #undef s32
231 typedef __signed__ int s32;
232 typedef unsigned int u32;
233 #endif /* def LINUX_1_2 */
235 #ifdef MODULE
236 #include <linux/module.h>
237 #endif
239 #include <asm/dma.h>
240 #include <asm/io.h>
241 #include <asm/system.h>
242 #include <linux/delay.h>
243 #include <linux/signal.h>
244 #include <linux/sched.h>
245 #include <linux/errno.h>
246 #include <linux/pci.h>
247 #include <linux/proc_fs.h>
248 #include <linux/string.h>
249 #include <linux/malloc.h>
250 #include <linux/vmalloc.h>
251 #include <linux/mm.h>
252 #include <linux/ioport.h>
253 #include <linux/time.h>
254 #include <linux/blk.h>
255 #include <linux/init.h>
256 #include <linux/spinlock.h>
258 #include "scsi.h"
259 #include "hosts.h"
260 #include "53c7,8xx.h"
261 #include "constants.h"
262 #include "sd.h"
263 #include <linux/stat.h>
264 #include <linux/stddef.h>
266 #ifndef LINUX_1_2
267 struct proc_dir_entry proc_scsi_ncr53c7xx = {
268 PROC_SCSI_NCR53C7xx, 9, "ncr53c7xx",
269 S_IFDIR | S_IRUGO | S_IXUGO, 2
271 #endif
273 static int check_address (unsigned long addr, int size);
274 static void dump_events (struct Scsi_Host *host, int count);
275 static Scsi_Cmnd * return_outstanding_commands (struct Scsi_Host *host,
276 int free, int issue);
277 static void hard_reset (struct Scsi_Host *host);
278 static void ncr_scsi_reset (struct Scsi_Host *host);
279 static void print_lots (struct Scsi_Host *host);
280 static void set_synchronous (struct Scsi_Host *host, int target, int sxfer,
281 int scntl3, int now_connected);
282 static int datapath_residual (struct Scsi_Host *host);
283 static const char * sbcl_to_phase (int sbcl);
284 static void print_progress (Scsi_Cmnd *cmd);
285 static void print_queues (struct Scsi_Host *host);
286 static void process_issue_queue (unsigned long flags);
287 static int shutdown (struct Scsi_Host *host);
288 static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result);
289 static int disable (struct Scsi_Host *host);
290 static int NCR53c8xx_run_tests (struct Scsi_Host *host);
291 static int NCR53c8xx_script_len;
292 static int NCR53c8xx_dsa_len;
293 static void NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs);
294 static void do_NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs);
295 static int ncr_halt (struct Scsi_Host *host);
296 static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd
297 *cmd);
298 static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd);
299 static void print_dsa (struct Scsi_Host *host, u32 *dsa,
300 const char *prefix);
301 static int print_insn (struct Scsi_Host *host, const u32 *insn,
302 const char *prefix, int kernel);
304 static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd);
305 static void NCR53c8x0_init_fixup (struct Scsi_Host *host);
306 static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
307 NCR53c7x0_cmd *cmd);
308 static void NCR53c8x0_soft_reset (struct Scsi_Host *host);
310 /* INSMOD variables */
311 static long long perm_options = PERM_OPTIONS;
312 /* 14 = .5s; 15 is max; decreasing divides by two. */
313 static int selection_timeout = 14;
314 /* Size of event list (per host adapter) */
315 static int track_events = 0;
317 static struct Scsi_Host *first_host = NULL; /* Head of list of NCR boards */
318 static Scsi_Host_Template *the_template = NULL;
321 * KNOWN BUGS :
322 * - There is some sort of conflict when the PPP driver is compiled with
323 * support for 16 channels?
325 * - On systems which predate the 1.3.x initialization order change,
326 * the NCR driver will cause Cannot get free page messages to appear.
327 * These are harmless, but I don't know of an easy way to avoid them.
329 * - With OPTION_DISCONNECT, on two systems under unknown circumstances,
330 * we get a PHASE MISMATCH with DSA set to zero (suggests that we
331 * are occurring somewhere in the reselection code) where
332 * DSP=some value DCMD|DBC=same value.
334 * Closer inspection suggests that we may be trying to execute
335 * some portion of the DSA?
336 * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
337 * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
338 * scsi0 : no current command : unexpected phase MSGIN.
339 * DSP=0x1c46cc, DCMD|DBC=0x1c46ac, DSA=0x0
340 * DSPS=0x0, TEMP=0x1c3e70, DMODE=0x80
341 * scsi0 : DSP->
342 * 001c46cc : 0x001c46cc 0x00000000
343 * 001c46d4 : 0x001c5ea0 0x000011f8
345 * Changed the print code in the phase_mismatch handler so
346 * that we call print_lots to try to diagnose this.
351 * Possible future direction of architecture for max performance :
353 * We're using a single start array for the NCR chip. This is
354 * sub-optimal, because we cannot add a command which would conflict with
355 * an executing command to this start queue, and therefore must insert the
356 * next command for a given I/T/L combination after the first has completed;
357 * incurring our interrupt latency between SCSI commands.
359 * To allow further pipelining of the NCR and host CPU operation, we want
360 * to set things up so that immediately on termination of a command destined
361 * for a given LUN, we get that LUN busy again.
363 * To do this, we need to add a 32 bit pointer to which is jumped to
364 * on completion of a command. If no new command is available, this
365 * would point to the usual DSA issue queue select routine.
367 * If one were, it would point to a per-NCR53c7x0_cmd select routine
368 * which starts execution immediately, inserting the command at the head
369 * of the start queue if the NCR chip is selected or reselected.
371 * We would change so that we keep a list of outstanding commands
372 * for each unit, rather than a single running_list. We'd insert
373 * a new command into the right running list; if the NCR didn't
374 * have something running for that yet, we'd put it in the
375 * start queue as well. Some magic needs to happen to handle the
376 * race condition between the first command terminating before the
377 * new one is written.
379 * Potential for profiling :
380 * Call do_gettimeofday(struct timeval *tv) to get 800ns resolution.
385 * TODO :
386 * 1. To support WIDE transfers, not much needs to happen. We
387 * should do CHMOVE instructions instead of MOVEs when
388 * we have scatter/gather segments of uneven length. When
389 * we do this, we need to handle the case where we disconnect
390 * between segments.
392 * 2. Currently, when Icky things happen we do a FATAL(). Instead,
393 * we want to do an integrity check on the parts of the NCR hostdata
394 * structure which were initialized at boot time; FATAL() if that
395 * fails, and otherwise try to recover. Keep track of how many
396 * times this has happened within a single SCSI command; if it
397 * gets excessive, then FATAL().
399 * 3. Parity checking is currently disabled, and a few things should
400 * happen here now that we support synchronous SCSI transfers :
401 * 1. On soft-reset, we should set the EPC (Enable Parity Checking)
402 * and AAP (Assert SATN/ on parity error) bits in SCNTL0.
404 * 2. We should enable the parity interrupt in the SIEN0 register.
406 * 3. intr_phase_mismatch() needs to believe that message out is
407 * always an "acceptable" phase to have a mismatch in. If
408 * the old phase was MSG_IN, we should send a MESSAGE PARITY
409 * error. If the old phase was something else, we should send
410 * a INITIATOR_DETECTED_ERROR message. Note that this could
411 * cause a RESTORE POINTERS message; so we should handle that
412 * correctly first. Instead, we should probably do an
413 * initiator_abort.
415 * 4. MPEE bit of CTEST4 should be set so we get interrupted if
416 * we detect an error.
419 * 5. The initial code has been tested on the NCR53c810. I don't
420 * have access to NCR53c700, 700-66 (Forex boards), NCR53c710
421 * (NCR Pentium systems), NCR53c720, NCR53c820, or NCR53c825 boards to
422 * finish development on those platforms.
424 * NCR53c820/825/720 - need to add wide transfer support, including WDTR
425 * negotiation, programming of wide transfer capabilities
426 * on reselection and table indirect selection.
428 * NCR53c710 - need to add fatal interrupt or GEN code for
429 * command completion signaling. Need to modify all
430 * SDID, SCID, etc. registers, and table indirect select code
431 * since these use bit fielded (ie 1<<target) instead of
432 * binary encoded target ids. Need to accommodate
433 * different register mappings, probably scan through
434 * the SCRIPT code and change the non SFBR register operand
435 * of all MOVE instructions.
437 * NCR53c700/700-66 - need to add code to refix addresses on
438 * every nexus change, eliminate all table indirect code,
439 * very messy.
441 * 6. The NCR53c7x0 series is very popular on other platforms that
442 * could be running Linux - ie, some high performance AMIGA SCSI
443 * boards use it.
445 * So, I should include #ifdef'd code so that it is
446 * compatible with these systems.
448 * Specifically, the little Endian assumptions I made in my
449 * bit fields need to change, and if the NCR doesn't see memory
450 * the right way, we need to provide options to reverse words
451 * when the scripts are relocated.
453 * 7. Use ioremap() to access memory mapped boards.
457 * Allow for simultaneous existence of multiple SCSI scripts so we
458 * can have a single driver binary for all of the family.
460 * - one for NCR53c700 and NCR53c700-66 chips (not yet supported)
461 * - one for rest (only the NCR53c810, 815, 820, and 825 are currently
462 * supported)
464 * So that we only need two SCSI scripts, we need to modify things so
465 * that we fixup register accesses in READ/WRITE instructions, and
466 * we'll also have to accommodate the bit vs. binary encoding of IDs
467 * with the 7xx chips.
471 * Use pci_chips_ids to translate in both directions between PCI device ID
472 * and chip numbers.
475 static struct {
476 unsigned short pci_device_id;
477 int chip;
479 * The revision field of the PCI_CLASS_REVISION register is compared
480 * against each of these fields if the field is not -1. If it
481 * is less than min_revision or larger than max_revision, a warning
482 * message is printed.
484 int max_revision;
485 int min_revision;
486 } pci_chip_ids[] = {
487 {PCI_DEVICE_ID_NCR_53C810, 810, 2, 1},
488 {PCI_DEVICE_ID_NCR_53C815, 815, 3, 2},
489 {PCI_DEVICE_ID_NCR_53C820, 820, -1, -1},
490 {PCI_DEVICE_ID_NCR_53C825, 825, -1, -1}
493 #define NPCI_CHIP_IDS (sizeof (pci_chip_ids) / sizeof(pci_chip_ids[0]))
495 #define ROUNDUP(adr,type) \
496 ((void *) (((long) (adr) + sizeof(type) - 1) & ~(sizeof(type) - 1)))
499 * Forced detection and autoprobe code for various hardware. Currently,
500 * entry points for these are not included in init/main.c because if the
501 * PCI BIOS code isn't working right, you're not going to be able to use
502 * the hardware anyways; this way we force users to solve their
503 * problems rather than forcing detection and blaming us when it
504 * does not work.
507 static struct override {
508 int chip; /* 700, 70066, 710, 720, 810, 820 */
509 int board; /* Any special board level gunk */
510 unsigned pci:1;
511 union {
512 struct {
513 int base; /* Memory address - indicates memory mapped regs */
514 int io_port;/* I/O port address - indicates I/O mapped regs */
515 int irq; /* IRQ line */
516 int dma; /* DMA channel - often none */
517 } normal;
518 struct {
519 int bus;
520 int device;
521 int function;
522 } pci;
523 } data;
524 long long options;
525 } overrides [4] = {{0,},};
526 static int commandline_current = 0;
527 static int no_overrides = 0;
529 #if 0
530 #define OVERRIDE_LIMIT (sizeof(overrides) / sizeof(struct override))
531 #else
532 #define OVERRIDE_LIMIT commandline_current
533 #endif
536 * Function: issue_to_cmd
538 * Purpose: convert jump instruction in issue array to NCR53c7x0_cmd
539 * structure pointer.
541 * Inputs; issue - pointer to start of NOP or JUMP instruction
542 * in issue array.
544 * Returns: pointer to command on success; 0 if opcode is NOP.
547 static inline struct NCR53c7x0_cmd *
548 issue_to_cmd (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
549 u32 *issue)
551 return (issue[0] != hostdata->NOP_insn) ?
553 * If the IF TRUE bit is set, it's a JUMP instruction. The
554 * operand is a bus pointer to the dsa_begin routine for this DSA. The
555 * dsa field of the NCR53c7x0_cmd structure starts with the
556 * DSA code template. By converting to a virtual address,
557 * subtracting the code template size, and offset of the
558 * dsa field, we end up with a pointer to the start of the
559 * structure (alternatively, we could use the
560 * dsa_cmnd field, an anachronism from when we weren't
561 * sure what the relationship between the NCR structures
562 * and host structures were going to be.
564 (struct NCR53c7x0_cmd *) ((char *) bus_to_virt (le32_to_cpu(issue[1])) -
565 (hostdata->E_dsa_code_begin - hostdata->E_dsa_code_template) -
566 offsetof(struct NCR53c7x0_cmd, dsa))
567 /* If the IF TRUE bit is not set, it's a NOP */
568 : NULL;
573 * Function : static internal_setup(int board, int chip, char *str, int *ints)
575 * Purpose : LILO command line initialization of the overrides array,
577 * Inputs : board - currently, unsupported. chip - 700, 70066, 710, 720
578 * 810, 815, 820, 825, although currently only the NCR53c810 is
579 * supported.
583 static void
584 internal_setup(int board, int chip, char *str, int *ints) {
585 unsigned char pci; /* Specifies a PCI override, with bus, device,
586 function */
588 pci = (str && !strcmp (str, "pci")) ? 1 : 0;
591 * Override syntaxes are as follows :
592 * ncr53c700,ncr53c700-66,ncr53c710,ncr53c720=mem,io,irq,dma
593 * ncr53c810,ncr53c820,ncr53c825=mem,io,irq or pci,bus,device,function
596 if (commandline_current < OVERRIDE_LIMIT) {
597 overrides[commandline_current].pci = pci ? 1 : 0;
598 if (!pci) {
599 overrides[commandline_current].data.normal.base = ints[1];
600 overrides[commandline_current].data.normal.io_port = ints[2];
601 overrides[commandline_current].data.normal.irq = ints[3];
602 overrides[commandline_current].data.normal.dma = (ints[0] >= 4) ?
603 ints[4] : DMA_NONE;
604 /* FIXME: options is now a long long */
605 overrides[commandline_current].options = (ints[0] >= 5) ?
606 ints[5] : 0;
607 } else {
608 overrides[commandline_current].data.pci.bus = ints[1];
609 overrides[commandline_current].data.pci.device = ints[2];
610 overrides[commandline_current].data.pci.function = ints[3];
611 /* FIXME: options is now a long long */
612 overrides[commandline_current].options = (ints[0] >= 4) ?
613 ints[4] : 0;
615 overrides[commandline_current].board = board;
616 overrides[commandline_current].chip = chip;
617 ++commandline_current;
618 ++no_overrides;
619 } else {
620 printk ("53c7,7x0.c:internal_setup() : too many overrides\n");
625 * XXX - we might want to implement a single override function
626 * with a chip type field, revamp the command line configuration,
627 * etc.
630 #define setup_wrapper(x) \
631 void ncr53c##x##_setup (char *str, int *ints) { \
632 internal_setup (BOARD_GENERIC, x, str, ints); \
635 setup_wrapper(700)
636 setup_wrapper(70066)
637 setup_wrapper(710)
638 setup_wrapper(720)
639 setup_wrapper(810)
640 setup_wrapper(815)
641 setup_wrapper(820)
642 setup_wrapper(825)
645 * FIXME: we should junk these, in favor of synchronous_want and
646 * wide_want in the NCR53c7x0_hostdata structure.
649 /* Template for "preferred" synchronous transfer parameters. */
651 static const unsigned char sdtr_message[] = {
652 #ifdef CONFIG_SCSI_NCR53C7xx_FAST
653 EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 25 /* *4ns */, 8 /* off */
654 #else
655 EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 50 /* *4ns */, 8 /* off */
656 #endif
659 /* Template to request asynchronous transfers */
661 static const unsigned char async_message[] = {
662 EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 0, 0 /* asynchronous */
665 /* Template for "preferred" WIDE transfer parameters */
667 static const unsigned char wdtr_message[] = {
668 EXTENDED_MESSAGE, 2 /* length */, EXTENDED_WDTR, 1 /* 2^1 bytes */
672 * Function : struct Scsi_Host *find_host (int host)
674 * Purpose : KGDB support function which translates a host number
675 * to a host structure.
677 * Inputs : host - number of SCSI host
679 * Returns : NULL on failure, pointer to host structure on success.
682 #if 0
683 static struct Scsi_Host *
684 find_host (int host) {
685 struct Scsi_Host *h;
686 for (h = first_host; h && h->host_no != host; h = h->next);
687 if (!h) {
688 printk (KERN_ALERT "scsi%d not found\n", host);
689 return NULL;
690 } else if (h->hostt != the_template) {
691 printk (KERN_ALERT "scsi%d is not a NCR board\n", host);
692 return NULL;
694 return h;
698 * Function : request_synchronous (int host, int target)
700 * Purpose : KGDB interface which will allow us to negotiate for
701 * synchronous transfers. This ill be replaced with a more
702 * integrated function; perhaps a new entry in the scsi_host
703 * structure, accessible via an ioctl() or perhaps /proc/scsi.
705 * Inputs : host - number of SCSI host; target - number of target.
707 * Returns : 0 when negotiation has been setup for next SCSI command,
708 * -1 on failure.
711 static int
712 request_synchronous (int host, int target) {
713 struct Scsi_Host *h;
714 struct NCR53c7x0_hostdata *hostdata;
715 unsigned long flags;
716 if (target < 0) {
717 printk (KERN_ALERT "target %d is bogus\n", target);
718 return -1;
720 if (!(h = find_host (host)))
721 return -1;
722 else if (h->this_id == target) {
723 printk (KERN_ALERT "target %d is host ID\n", target);
724 return -1;
726 #ifndef LINUX_1_2
727 else if (target > h->max_id) {
728 printk (KERN_ALERT "target %d exceeds maximum of %d\n", target,
729 h->max_id);
730 return -1;
732 #endif
733 hostdata = (struct NCR53c7x0_hostdata *)h->hostdata;
735 save_flags(flags);
736 cli();
737 if (hostdata->initiate_sdtr & (1 << target)) {
738 restore_flags(flags);
739 printk (KERN_ALERT "target %d already doing SDTR\n", target);
740 return -1;
742 hostdata->initiate_sdtr |= (1 << target);
743 restore_flags(flags);
744 return 0;
748 * Function : request_disconnect (int host, int on_or_off)
750 * Purpose : KGDB support function, tells us to allow or disallow
751 * disconnections.
753 * Inputs : host - number of SCSI host; on_or_off - non-zero to allow,
754 * zero to disallow.
756 * Returns : 0 on success, * -1 on failure.
759 static int
760 request_disconnect (int host, int on_or_off) {
761 struct Scsi_Host *h;
762 struct NCR53c7x0_hostdata *hostdata;
763 if (!(h = find_host (host)))
764 return -1;
765 hostdata = (struct NCR53c7x0_hostdata *) h->hostdata;
766 if (on_or_off)
767 hostdata->options |= OPTION_DISCONNECT;
768 else
769 hostdata->options &= ~OPTION_DISCONNECT;
770 return 0;
772 #endif
775 * Function : static void NCR53c7x0_driver_init (struct Scsi_Host *host)
777 * Purpose : Initialize internal structures, as required on startup, or
778 * after a SCSI bus reset.
780 * Inputs : host - pointer to this host adapter's structure
783 static void
784 NCR53c7x0_driver_init (struct Scsi_Host *host) {
785 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
786 host->hostdata;
787 int i, j;
788 u32 *curr;
789 for (i = 0; i < 16; ++i) {
790 hostdata->request_sense[i] = 0;
791 for (j = 0; j < 8; ++j)
792 hostdata->busy[i][j] = 0;
793 set_synchronous (host, i, /* sxfer */ 0, hostdata->saved_scntl3, 0);
795 hostdata->issue_queue = NULL;
796 hostdata->running_list = hostdata->finished_queue =
797 hostdata->curr = NULL;
798 for (i = 0, curr = (u32 *) hostdata->schedule;
799 i < host->can_queue; ++i, curr += 2) {
800 curr[0] = hostdata->NOP_insn;
801 curr[1] = le32_to_cpu(0xdeadbeef);
803 curr[0] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) | DBC_TCI_TRUE);
804 curr[1] = (u32) le32_to_cpu(virt_to_bus (hostdata->script) +
805 hostdata->E_wait_reselect);
806 hostdata->reconnect_dsa_head = 0;
807 hostdata->addr_reconnect_dsa_head = (u32)
808 le32_to_cpu(virt_to_bus((void *) &(hostdata->reconnect_dsa_head)));
809 hostdata->expecting_iid = 0;
810 hostdata->expecting_sto = 0;
811 if (hostdata->options & OPTION_ALWAYS_SYNCHRONOUS)
812 hostdata->initiate_sdtr = le32_to_cpu(0xffff);
813 else
814 hostdata->initiate_sdtr = 0;
815 hostdata->talked_to = 0;
816 hostdata->idle = 1;
820 * Function : static int ccf_to_clock (int ccf)
822 * Purpose : Return the largest SCSI clock allowable for a given
823 * clock conversion factor, allowing us to do synchronous periods
824 * when we don't know what the SCSI clock is by taking at least
825 * as long as the device says we can.
827 * Inputs : ccf
829 * Returns : clock on success, -1 on failure.
832 static int
833 ccf_to_clock (int ccf) {
834 switch (ccf) {
835 case 1: return 25000000; /* Divide by 1.0 */
836 case 2: return 37500000; /* Divide by 1.5 */
837 case 3: return 50000000; /* Divide by 2.0 */
838 case 0: /* Divide by 3.0 */
839 case 4: return 66000000;
840 default: return -1;
845 * Function : static int clock_to_ccf (int clock)
847 * Purpose : Return the clock conversion factor for a given SCSI clock.
849 * Inputs : clock - SCSI clock expressed in Hz.
851 * Returns : ccf on success, -1 on failure.
854 static int
855 clock_to_ccf (int clock) {
856 if (clock < 16666666)
857 return -1;
858 if (clock < 25000000)
859 return 1; /* Divide by 1.0 */
860 else if (clock < 37500000)
861 return 2; /* Divide by 1.5 */
862 else if (clock < 50000000)
863 return 3; /* Divide by 2.0 */
864 else if (clock < 66000000)
865 return 4; /* Divide by 3.0 */
866 else
867 return -1;
871 * Function : static int NCR53c7x0_init (struct Scsi_Host *host)
873 * Purpose : initialize the internal structures for a given SCSI host
875 * Inputs : host - pointer to this host adapter's structure
877 * Preconditions : when this function is called, the chip_type
878 * field of the hostdata structure MUST have been set.
880 * Returns : 0 on success, -1 on failure.
883 static int
884 NCR53c7x0_init (struct Scsi_Host *host) {
885 NCR53c7x0_local_declare();
886 int i, ccf, expected_ccf;
887 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
888 host->hostdata;
889 struct Scsi_Host *search;
891 * There are some things which we need to know about in order to provide
892 * a semblance of support. Print 'em if they aren't what we expect,
893 * otherwise don't add to the noise.
895 * -1 means we don't know what to expect.
897 int expected_id = -1;
898 int expected_clock = -1;
899 int uninitialized = 0;
901 * FIXME : this is only on Intel boxes. On other platforms, this
902 * will differ.
904 int expected_mapping = OPTION_IO_MAPPED;
906 NCR53c7x0_local_setup(host);
908 switch (hostdata->chip) {
909 case 820:
910 case 825:
911 #ifdef notyet
912 host->max_id = 15;
913 #endif
914 /* Fall through */
915 case 810:
916 case 815:
917 hostdata->dstat_sir_intr = NCR53c8x0_dstat_sir_intr;
918 hostdata->init_save_regs = NULL;
919 hostdata->dsa_fixup = NCR53c8xx_dsa_fixup;
920 hostdata->init_fixup = NCR53c8x0_init_fixup;
921 hostdata->soft_reset = NCR53c8x0_soft_reset;
922 hostdata->run_tests = NCR53c8xx_run_tests;
923 /* Is the SCSI clock ever anything else on these chips? */
924 expected_clock = hostdata->scsi_clock = 40000000;
925 expected_id = 7;
926 break;
927 default:
928 printk ("scsi%d : chip type of %d is not supported yet, detaching.\n",
929 host->host_no, hostdata->chip);
930 scsi_unregister (host);
931 return -1;
934 /* Assign constants accessed by NCR */
935 hostdata->NCR53c7xx_zero = 0;
936 hostdata->NCR53c7xx_msg_reject = le32_to_cpu(MESSAGE_REJECT);
937 hostdata->NCR53c7xx_msg_abort = le32_to_cpu(ABORT);
938 hostdata->NCR53c7xx_msg_nop = le32_to_cpu(NOP);
939 hostdata->NOP_insn = le32_to_cpu((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24);
941 if (expected_mapping == -1 ||
942 (hostdata->options & (OPTION_MEMORY_MAPPED)) !=
943 (expected_mapping & OPTION_MEMORY_MAPPED))
944 printk ("scsi%d : using %s mapped access\n", host->host_no,
945 (hostdata->options & OPTION_MEMORY_MAPPED) ? "memory" :
946 "io");
948 hostdata->dmode = (hostdata->chip == 700 || hostdata->chip == 70066) ?
949 DMODE_REG_00 : DMODE_REG_10;
950 hostdata->istat = ((hostdata->chip / 100) == 8) ?
951 ISTAT_REG_800 : ISTAT_REG_700;
953 /* Only the ISTAT register is readable when the NCR is running, so make
954 sure it's halted. */
955 ncr_halt(host);
958 * XXX - the NCR53c700 uses bitfielded registers for SCID, SDID, etc,
959 * as does the 710 with one bit per SCSI ID. Conversely, the NCR
960 * uses a normal, 3 bit binary representation of these values.
962 * Get the rest of the NCR documentation, and FIND OUT where the change
963 * was.
965 #if 0
966 tmp = hostdata->this_id_mask = NCR53c7x0_read8(SCID_REG);
967 for (host->this_id = 0; tmp != 1; tmp >>=1, ++host->this_id);
968 #else
969 host->this_id = NCR53c7x0_read8(SCID_REG) & 15;
970 if (host->this_id == 0)
971 host->this_id = 7; /* sanitize hostid---0 doesn't make sense */
972 hostdata->this_id_mask = 1 << host->this_id;
973 #endif
976 * Note : we should never encounter a board setup for ID0. So,
977 * if we see ID0, assume that it was uninitialized and set it
978 * to the industry standard 7.
980 if (!host->this_id) {
981 printk("scsi%d : initiator ID was %d, changing to 7\n",
982 host->host_no, host->this_id);
983 host->this_id = 7;
984 hostdata->this_id_mask = 1 << 7;
985 uninitialized = 1;
988 if (expected_id == -1 || host->this_id != expected_id)
989 printk("scsi%d : using initiator ID %d\n", host->host_no,
990 host->this_id);
993 * Save important registers to allow a soft reset.
996 if ((hostdata->chip / 100) == 8) {
998 * CTEST4 controls burst mode disable.
1000 hostdata->saved_ctest4 = NCR53c7x0_read8(CTEST4_REG_800) &
1001 CTEST4_800_SAVE;
1002 } else {
1004 * CTEST7 controls cache snooping, burst mode, and support for
1005 * external differential drivers.
1007 hostdata->saved_ctest7 = NCR53c7x0_read8(CTEST7_REG) & CTEST7_SAVE;
1011 * On NCR53c700 series chips, DCNTL controls the SCSI clock divisor,
1012 * on 800 series chips, it allows for a totem-pole IRQ driver.
1015 hostdata->saved_dcntl = NCR53c7x0_read8(DCNTL_REG);
1018 * DCNTL_800_IRQM controls weather we are using an open drain
1019 * driver (reset) or totem pole driver (set). In all cases,
1020 * it's level active. I suppose this is an issue when we're trying to
1021 * wire-or the same PCI INTx line?
1023 if ((hostdata->chip / 100) == 8)
1024 hostdata->saved_dcntl &= ~DCNTL_800_IRQM;
1027 * DMODE controls DMA burst length, and on 700 series chips,
1028 * 286 mode and bus width
1030 hostdata->saved_dmode = NCR53c7x0_read8(hostdata->dmode);
1033 * Now that burst length and enabled/disabled status is known,
1034 * clue the user in on it.
1037 if ((hostdata->chip / 100) == 8) {
1038 if (hostdata->saved_ctest4 & CTEST4_800_BDIS) {
1039 printk ("scsi%d : burst mode disabled\n", host->host_no);
1040 } else {
1041 switch (hostdata->saved_dmode & DMODE_BL_MASK) {
1042 case DMODE_BL_2: i = 2; break;
1043 case DMODE_BL_4: i = 4; break;
1044 case DMODE_BL_8: i = 8; break;
1045 case DMODE_BL_16: i = 16; break;
1046 default: i = 0;
1048 printk ("scsi%d : burst length %d\n", host->host_no, i);
1053 * On NCR53c810 and NCR53c820 chips, SCNTL3 contails the synchronous
1054 * and normal clock conversion factors.
1056 if (hostdata->chip / 100 == 8) {
1057 expected_ccf = clock_to_ccf (expected_clock);
1058 hostdata->saved_scntl3 = NCR53c7x0_read8(SCNTL3_REG_800);
1059 ccf = hostdata->saved_scntl3 & SCNTL3_800_CCF_MASK;
1060 if (expected_ccf != -1 && ccf != expected_ccf && !ccf) {
1061 hostdata->saved_scntl3 = (hostdata->saved_scntl3 &
1062 ~SCNTL3_800_CCF_MASK) | expected_ccf;
1063 if (!uninitialized) {
1064 printk ("scsi%d : reset ccf to %d from %d\n",
1065 host->host_no, expected_ccf, ccf);
1066 uninitialized = 1;
1069 } else
1070 ccf = 0;
1073 * If we don't have a SCSI clock programmed, pick one on the upper
1074 * bound of that allowed by NCR so that our transfers err on the
1075 * slow side, since transfer period must be >= the agreed
1076 * upon period.
1079 if ((!hostdata->scsi_clock) && (hostdata->scsi_clock = ccf_to_clock (ccf))
1080 == -1) {
1081 printk ("scsi%d : clock conversion factor %d unknown.\n"
1082 " synchronous transfers disabled\n",
1083 host->host_no, ccf);
1084 hostdata->options &= ~OPTION_SYNCHRONOUS;
1085 hostdata->scsi_clock = 0;
1088 if (expected_clock == -1 || hostdata->scsi_clock != expected_clock)
1089 printk ("scsi%d : using %dMHz SCSI clock\n", host->host_no,
1090 hostdata->scsi_clock / 1000000);
1092 for (i = 0; i < 16; ++i)
1093 hostdata->cmd_allocated[i] = 0;
1095 if (hostdata->init_save_regs)
1096 hostdata->init_save_regs (host);
1097 if (hostdata->init_fixup)
1098 hostdata->init_fixup (host);
1100 if (!the_template) {
1101 the_template = host->hostt;
1102 first_host = host;
1106 * Linux SCSI drivers have always been plagued with initialization
1107 * problems - some didn't work with the BIOS disabled since they expected
1108 * initialization from it, some didn't work when the networking code
1109 * was enabled and registers got scrambled, etc.
1111 * To avoid problems like this, in the future, we will do a soft
1112 * reset on the SCSI chip, taking it back to a sane state.
1115 hostdata->soft_reset (host);
1117 #if 1
1118 hostdata->debug_count_limit = -1;
1119 #else
1120 hostdata->debug_count_limit = 1;
1121 #endif
1122 hostdata->intrs = -1;
1123 hostdata->resets = -1;
1124 memcpy ((void *) hostdata->synchronous_want, (void *) sdtr_message,
1125 sizeof (hostdata->synchronous_want));
1127 NCR53c7x0_driver_init (host);
1130 * Set up an interrupt handler if we aren't already sharing an IRQ
1131 * with another board.
1134 for (search = first_host; search && !(search->hostt == the_template &&
1135 search->irq == host->irq && search != host); search=search->next);
1137 if (!search) {
1138 #ifdef __powerpc__
1139 if (request_irq(host->irq, do_NCR53c7x0_intr, SA_SHIRQ, "53c7,8xx", NULL))
1140 #else
1141 if (request_irq(host->irq, do_NCR53c7x0_intr, SA_INTERRUPT, "53c7,8xx", NULL))
1142 #endif
1146 printk("scsi%d : IRQ%d not free, detaching\n"
1147 " You have either a configuration problem, or a\n"
1148 " broken BIOS. You may wish to manually assign\n"
1149 " an interrupt to the NCR board rather than using\n"
1150 " an automatic setting.\n",
1151 host->host_no, host->irq);
1152 scsi_unregister (host);
1153 return -1;
1155 } else {
1156 printk("scsi%d : using interrupt handler previously installed for scsi%d\n",
1157 host->host_no, search->host_no);
1161 if ((hostdata->run_tests && hostdata->run_tests(host) == -1) ||
1162 (hostdata->options & OPTION_DEBUG_TESTS_ONLY)) {
1163 /* XXX Should disable interrupts, etc. here */
1164 scsi_unregister (host);
1165 return -1;
1166 } else {
1167 if (host->io_port) {
1168 host->n_io_port = 128;
1169 request_region (host->io_port, host->n_io_port, "ncr53c7,8xx");
1173 if (NCR53c7x0_read8 (SBCL_REG) & SBCL_BSY) {
1174 printk ("scsi%d : bus wedge, doing SCSI reset\n", host->host_no);
1175 hard_reset (host);
1177 return 0;
1181 * Function : static int normal_init(Scsi_Host_Template *tpnt, int board,
1182 * int chip, u32 base, int io_port, int irq, int dma, int pcivalid,
1183 * unsigned char pci_bus, unsigned char pci_device_fn,
1184 * long long options);
1186 * Purpose : initializes a NCR53c7,8x0 based on base addresses,
1187 * IRQ, and DMA channel.
1189 * Useful where a new NCR chip is backwards compatible with
1190 * a supported chip, but the DEVICE ID has changed so it
1191 * doesn't show up when the autoprobe does a pcibios_find_device.
1193 * Inputs : tpnt - Template for this SCSI adapter, board - board level
1194 * product, chip - 810, 820, or 825, bus - PCI bus, device_fn -
1195 * device and function encoding as used by PCI BIOS calls.
1197 * Returns : 0 on success, -1 on failure.
1201 static int __init
1202 normal_init (Scsi_Host_Template *tpnt, int board, int chip,
1203 u32 base, int io_port, int irq, int dma, int pci_valid,
1204 unsigned char pci_bus, unsigned char pci_device_fn, long long options){
1205 struct Scsi_Host *instance;
1206 struct NCR53c7x0_hostdata *hostdata;
1207 char chip_str[80];
1208 int script_len = 0, dsa_len = 0, size = 0, max_cmd_size = 0,
1209 schedule_size = 0, ok = 0;
1210 void *tmp;
1212 options |= perm_options;
1214 switch (chip) {
1215 case 825:
1216 case 820:
1217 case 815:
1218 case 810:
1219 schedule_size = (tpnt->can_queue + 1) * 8 /* JUMP instruction size */;
1220 script_len = NCR53c8xx_script_len;
1221 dsa_len = NCR53c8xx_dsa_len;
1222 options |= OPTION_INTFLY;
1223 sprintf (chip_str, "NCR53c%d", chip);
1224 break;
1225 default:
1226 printk("scsi-ncr53c7,8xx : unsupported SCSI chip %d\n", chip);
1227 return -1;
1230 printk("scsi-ncr53c7,8xx : %s at memory 0x%x, io 0x%x, irq %d",
1231 chip_str, (unsigned) base, io_port, irq);
1232 if (dma == DMA_NONE)
1233 printk("\n");
1234 else
1235 printk(", dma %d\n", dma);
1237 if ((chip / 100 == 8) && !pci_valid)
1238 printk ("scsi-ncr53c7,8xx : for better reliability and performance, please use the\n"
1239 " PCI override instead.\n"
1240 " Syntax : ncr53c8{10,15,20,25}=pci,<bus>,<device>,<function>\n"
1241 " <bus> and <device> are usually 0.\n");
1243 if (options & OPTION_DEBUG_PROBE_ONLY) {
1244 printk ("scsi-ncr53c7,8xx : probe only enabled, aborting initialization\n");
1245 return -1;
1248 max_cmd_size = sizeof(struct NCR53c7x0_cmd) + dsa_len +
1249 /* Size of dynamic part of command structure : */
1250 2 * /* Worst case : we don't know if we need DATA IN or DATA out */
1251 ( 2 * /* Current instructions per scatter/gather segment */
1252 tpnt->sg_tablesize +
1253 3 /* Current startup / termination required per phase */
1255 8 /* Each instruction is eight bytes */;
1257 /* Allocate fixed part of hostdata, dynamic part to hold appropriate
1258 SCSI SCRIPT(tm) plus a single, maximum-sized NCR53c7x0_cmd structure.
1260 We need a NCR53c7x0_cmd structure for scan_scsis() when we are
1261 not loaded as a module, and when we're loaded as a module, we
1262 can't use a non-dynamically allocated structure because modules
1263 are vmalloc()'d, which can allow structures to cross page
1264 boundaries and breaks our physical/virtual address assumptions
1265 for DMA.
1267 So, we stick it past the end of our hostdata structure.
1269 ASSUMPTION :
1270 Regardless of how many simultaneous SCSI commands we allow,
1271 the probe code only executes a _single_ instruction at a time,
1272 so we only need one here, and don't need to allocate NCR53c7x0_cmd
1273 structures for each target until we are no longer in scan_scsis
1274 and kmalloc() has become functional (memory_init() happens
1275 after all device driver initialization).
1278 size = sizeof(struct NCR53c7x0_hostdata) + script_len +
1279 /* Note that alignment will be guaranteed, since we put the command
1280 allocated at probe time after the fixed-up SCSI script, which
1281 consists of 32 bit words, aligned on a 32 bit boundary. But
1282 on a 64bit machine we need 8 byte alignment for hostdata->free, so
1283 we add in another 4 bytes to take care of potential misalignment
1285 (sizeof(void *) - sizeof(u32)) + max_cmd_size + schedule_size;
1287 instance = scsi_register (tpnt, size);
1288 if (!instance)
1289 return -1;
1291 /* FIXME : if we ever support an ISA NCR53c7xx based board, we
1292 need to check if the chip is running in a 16 bit mode, and if so
1293 unregister it if it is past the 16M (0x1000000) mark */
1295 hostdata = (struct NCR53c7x0_hostdata *)
1296 instance->hostdata;
1297 hostdata->size = size;
1298 hostdata->script_count = script_len / sizeof(u32);
1299 hostdata = (struct NCR53c7x0_hostdata *) instance->hostdata;
1300 hostdata->board = board;
1301 hostdata->chip = chip;
1302 if ((hostdata->pci_valid = pci_valid)) {
1303 hostdata->pci_bus = pci_bus;
1304 hostdata->pci_device_fn = pci_device_fn;
1308 * Being memory mapped is more desirable, since
1310 * - Memory accesses may be faster.
1312 * - The destination and source address spaces are the same for
1313 * all instructions, meaning we don't have to twiddle dmode or
1314 * any other registers.
1316 * So, we try for memory mapped, and if we don't get it,
1317 * we go for port mapped, and that failing we tell the user
1318 * it can't work.
1321 if (base) {
1322 instance->base = (unsigned char *) (unsigned long) base;
1323 /* Check for forced I/O mapping */
1324 if (!(options & OPTION_IO_MAPPED)) {
1325 options |= OPTION_MEMORY_MAPPED;
1326 ok = 1;
1328 } else {
1329 options &= ~OPTION_MEMORY_MAPPED;
1332 if (io_port) {
1333 instance->io_port = io_port;
1334 options |= OPTION_IO_MAPPED;
1335 ok = 1;
1336 } else {
1337 options &= ~OPTION_IO_MAPPED;
1340 if (!ok) {
1341 printk ("scsi%d : not initializing, no I/O or memory mapping known \n",
1342 instance->host_no);
1343 scsi_unregister (instance);
1344 return -1;
1346 instance->irq = irq;
1347 instance->dma_channel = dma;
1349 hostdata->options = options;
1350 hostdata->dsa_len = dsa_len;
1351 hostdata->max_cmd_size = max_cmd_size;
1352 hostdata->num_cmds = 1;
1353 /* Initialize single command */
1354 tmp = (hostdata->script + hostdata->script_count);
1355 hostdata->free = ROUNDUP(tmp, void *);
1356 hostdata->free->real = tmp;
1357 hostdata->free->size = max_cmd_size;
1358 hostdata->free->free = NULL;
1359 hostdata->free->next = NULL;
1360 hostdata->extra_allocate = 0;
1362 /* Allocate command start code space */
1363 hostdata->schedule = (chip == 700 || chip == 70066) ?
1364 NULL : (u32 *) ((char *)hostdata->free + max_cmd_size);
1367 * For diagnostic purposes, we don't really care how fast things blaze.
1368 * For profiling, we want to access the 800ns resolution system clock,
1369 * using a 'C' call on the host processor.
1371 * Therefore, there's no need for the NCR chip to directly manipulate
1372 * this data, and we should put it wherever is most convenient for
1373 * Linux.
1375 if (track_events)
1376 hostdata->events = (struct NCR53c7x0_event *) (track_events ?
1377 vmalloc (sizeof (struct NCR53c7x0_event) * track_events) : NULL);
1378 else
1379 hostdata->events = NULL;
1381 if (hostdata->events) {
1382 memset ((void *) hostdata->events, 0, sizeof(struct NCR53c7x0_event) *
1383 track_events);
1384 hostdata->event_size = track_events;
1385 hostdata->event_index = 0;
1386 } else
1387 hostdata->event_size = 0;
1389 return NCR53c7x0_init(instance);
1394 * Function : static int ncr_pci_init(Scsi_Host_Template *tpnt, int board,
1395 * int chip, int bus, int device_fn, long long options)
1397 * Purpose : initializes a NCR53c800 family based on the PCI
1398 * bus, device, and function location of it. Allows
1399 * reprogramming of latency timer and determining addresses
1400 * and whether bus mastering, etc. are OK.
1402 * Useful where a new NCR chip is backwards compatible with
1403 * a supported chip, but the DEVICE ID has changed so it
1404 * doesn't show up when the autoprobe does a pcibios_find_device.
1406 * Inputs : tpnt - Template for this SCSI adapter, board - board level
1407 * product, chip - 810, 820, or 825, bus - PCI bus, device_fn -
1408 * device and function encoding as used by PCI BIOS calls.
1410 * Returns : 0 on success, -1 on failure.
1414 static int __init
1415 ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
1416 unsigned char bus, unsigned char device_fn, long long options){
1417 unsigned short command;
1418 #ifdef LINUX_1_2
1419 unsigned long
1420 #else
1421 unsigned int
1422 #endif
1423 base, io_port;
1424 unsigned char revision;
1425 int error, expected_chip;
1426 int expected_id = -1, max_revision = -1, min_revision = -1;
1427 int i, irq;
1428 struct pci_dev *pdev = pci_find_slot(bus, device_fn);
1430 printk("scsi-ncr53c7,8xx : at PCI bus %d, device %d, function %d\n",
1431 bus, (int) (device_fn & 0xf8) >> 3,
1432 (int) device_fn & 7);
1434 if (!pdev) {
1435 printk("scsi-ncr53c7,8xx : not initializing -- PCI device not found,\n"
1436 " try using memory, port, irq override instead.\n");
1437 return -1;
1440 if ((error = pcibios_read_config_word (bus, device_fn, PCI_COMMAND,
1441 &command)) ||
1442 (error = pcibios_read_config_byte (bus, device_fn, PCI_CLASS_REVISION,
1443 &revision))) {
1444 printk ("scsi-ncr53c7,8xx : error %d not initializing due to error reading configuration space\n"
1445 " perhaps you specified an incorrect PCI bus, device, or function.\n", error);
1446 return -1;
1448 io_port = pdev->resource[0].start;
1449 base = pdev->resource[1].start;
1450 irq = pdev->irq;
1452 /* If any one ever clones the NCR chips, this will have to change */
1454 if (pdev->vendor != PCI_VENDOR_ID_NCR) {
1455 printk ("scsi-ncr53c7,8xx : not initializing, 0x%04x is not NCR vendor ID\n",
1456 (int) pdev->vendor);
1457 return -1;
1460 #ifdef __powerpc__
1461 if ( ! (command & PCI_COMMAND_MASTER)) {
1462 printk("SCSI: PCI Master Bit has not been set. Setting...\n");
1463 command |= PCI_COMMAND_MASTER|PCI_COMMAND_IO;
1464 pci_write_config_word(pdev, PCI_COMMAND, command);
1466 if (io_port >= 0x10000000 && is_prep ) {
1467 /* Mapping on PowerPC can't handle this! */
1468 unsigned long new_io_port;
1469 new_io_port = (io_port & 0x00FFFFFF) | 0x01000000;
1470 printk("SCSI: I/O moved from %08X to %08x\n", io_port, new_io_port);
1471 io_port = new_io_port;
1472 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, io_port);
1473 pdev->base_address[0] = io_port;
1476 #endif
1479 * Bit 0 is the address space indicator and must be one for I/O
1480 * space mappings, bit 1 is reserved, discard them after checking
1481 * that they have the correct value of 1.
1484 if (command & PCI_COMMAND_IO) {
1485 if ((io_port & 3) != 1) {
1486 printk ("scsi-ncr53c7,8xx : disabling I/O mapping since base address 0 (0x%x)\n"
1487 " bits 0..1 indicate a non-IO mapping\n",
1488 (unsigned) io_port);
1489 io_port = 0;
1490 } else
1491 io_port &= PCI_BASE_ADDRESS_IO_MASK;
1492 } else {
1493 io_port = 0;
1496 if (command & PCI_COMMAND_MEMORY) {
1497 if ((base & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
1498 printk("scsi-ncr53c7,8xx : disabling memory mapping since base address 1\n"
1499 " contains a non-memory mapping\n");
1500 base = 0;
1501 } else
1502 base &= PCI_BASE_ADDRESS_MEM_MASK;
1503 } else {
1504 base = 0;
1507 if (!io_port && !base) {
1508 printk ("scsi-ncr53c7,8xx : not initializing, both I/O and memory mappings disabled\n");
1509 return -1;
1512 if (!(command & PCI_COMMAND_MASTER)) {
1513 printk ("scsi-ncr53c7,8xx : not initializing, BUS MASTERING was disabled\n");
1514 return -1;
1517 for (i = 0; i < NPCI_CHIP_IDS; ++i) {
1518 if (pdev->device == pci_chip_ids[i].pci_device_id) {
1519 max_revision = pci_chip_ids[i].max_revision;
1520 min_revision = pci_chip_ids[i].min_revision;
1521 expected_chip = pci_chip_ids[i].chip;
1523 if (chip == pci_chip_ids[i].chip)
1524 expected_id = pci_chip_ids[i].pci_device_id;
1527 if (chip && pdev->device != expected_id)
1528 printk ("scsi-ncr53c7,8xx : warning : device id of 0x%04x doesn't\n"
1529 " match expected 0x%04x\n",
1530 (unsigned int) pdev->device, (unsigned int) expected_id );
1532 if (max_revision != -1 && revision > max_revision)
1533 printk ("scsi-ncr53c7,8xx : warning : revision of %d is greater than %d.\n",
1534 (int) revision, max_revision);
1535 else if (min_revision != -1 && revision < min_revision)
1536 printk ("scsi-ncr53c7,8xx : warning : revision of %d is less than %d.\n",
1537 (int) revision, min_revision);
1539 if (io_port && check_region (io_port, 128)) {
1540 printk ("scsi-ncr53c7,8xx : IO region 0x%x to 0x%x is in use\n",
1541 (unsigned) io_port, (unsigned) io_port + 127);
1542 return -1;
1545 return normal_init (tpnt, board, chip, (int) base, io_port,
1546 (int) irq, DMA_NONE, 1, bus, device_fn, options);
1551 * Function : int NCR53c7xx_detect(Scsi_Host_Template *tpnt)
1553 * Purpose : detects and initializes NCR53c7,8x0 SCSI chips
1554 * that were autoprobed, overridden on the LILO command line,
1555 * or specified at compile time.
1557 * Inputs : tpnt - template for this SCSI adapter
1559 * Returns : number of host adapters detected
1563 int __init
1564 NCR53c7xx_detect(Scsi_Host_Template *tpnt){
1565 int i;
1566 int current_override;
1567 int count; /* Number of boards detected */
1568 unsigned char pci_bus, pci_device_fn;
1569 static short pci_index=0; /* Device index to PCI BIOS calls */
1571 #ifndef LINUX_1_2
1572 tpnt->proc_dir = &proc_scsi_ncr53c7xx;
1573 #endif
1575 for (current_override = count = 0; current_override < OVERRIDE_LIMIT;
1576 ++current_override) {
1577 if (overrides[current_override].pci ?
1578 !ncr_pci_init (tpnt, overrides[current_override].board,
1579 overrides[current_override].chip,
1580 (unsigned char) overrides[current_override].data.pci.bus,
1581 (((overrides[current_override].data.pci.device
1582 << 3) & 0xf8)|(overrides[current_override].data.pci.function &
1583 7)), overrides[current_override].options):
1584 !normal_init (tpnt, overrides[current_override].board,
1585 overrides[current_override].chip,
1586 overrides[current_override].data.normal.base,
1587 overrides[current_override].data.normal.io_port,
1588 overrides[current_override].data.normal.irq,
1589 overrides[current_override].data.normal.dma,
1590 0 /* PCI data invalid */, 0 /* PCI bus place holder */,
1591 0 /* PCI device_function place holder */,
1592 overrides[current_override].options)) {
1593 ++count;
1597 if (pci_present()) {
1598 for (i = 0; i < NPCI_CHIP_IDS; ++i)
1599 for (pci_index = 0;
1600 !pcibios_find_device (PCI_VENDOR_ID_NCR,
1601 pci_chip_ids[i].pci_device_id, pci_index, &pci_bus,
1602 &pci_device_fn);
1603 ++pci_index)
1604 if (!ncr_pci_init (tpnt, BOARD_GENERIC, pci_chip_ids[i].chip,
1605 pci_bus, pci_device_fn, /* no options */ 0))
1606 ++count;
1608 return count;
1611 /* NCR53c810 and NCR53c820 script handling code */
1613 #include "53c8xx_d.h"
1614 #ifdef A_int_debug_sync
1615 #define DEBUG_SYNC_INTR A_int_debug_sync
1616 #endif
1617 static int NCR53c8xx_script_len = sizeof (SCRIPT);
1618 static int NCR53c8xx_dsa_len = A_dsa_end + Ent_dsa_zero - Ent_dsa_code_template;
1621 * Function : static void NCR53c8x0_init_fixup (struct Scsi_Host *host)
1623 * Purpose : copy and fixup the SCSI SCRIPTS(tm) code for this device.
1625 * Inputs : host - pointer to this host adapter's structure
1629 static void
1630 NCR53c8x0_init_fixup (struct Scsi_Host *host) {
1631 NCR53c7x0_local_declare();
1632 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1633 host->hostdata;
1634 unsigned char tmp;
1635 int i, ncr_to_memory, memory_to_ncr;
1636 u32 base;
1637 #ifdef __powerpc__
1638 unsigned long *script_ptr;
1639 #endif
1640 NCR53c7x0_local_setup(host);
1643 /* XXX - NOTE : this code MUST be made endian aware */
1644 /* Copy code into buffer that was allocated at detection time. */
1645 memcpy ((void *) hostdata->script, (void *) SCRIPT,
1646 sizeof(SCRIPT));
1647 /* Fixup labels */
1648 for (i = 0; i < PATCHES; ++i)
1649 hostdata->script[LABELPATCHES[i]] +=
1650 virt_to_bus(hostdata->script);
1651 /* Fixup addresses of constants that used to be EXTERNAL */
1653 patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_abort,
1654 virt_to_bus(&(hostdata->NCR53c7xx_msg_abort)));
1655 patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_reject,
1656 virt_to_bus(&(hostdata->NCR53c7xx_msg_reject)));
1657 patch_abs_32 (hostdata->script, 0, NCR53c7xx_zero,
1658 virt_to_bus(&(hostdata->NCR53c7xx_zero)));
1659 patch_abs_32 (hostdata->script, 0, NCR53c7xx_sink,
1660 virt_to_bus(&(hostdata->NCR53c7xx_sink)));
1661 patch_abs_32 (hostdata->script, 0, NOP_insn,
1662 virt_to_bus(&(hostdata->NOP_insn)));
1663 patch_abs_32 (hostdata->script, 0, schedule,
1664 virt_to_bus((void *) hostdata->schedule));
1666 /* Fixup references to external variables: */
1667 for (i = 0; i < EXTERNAL_PATCHES_LEN; ++i)
1668 hostdata->script[EXTERNAL_PATCHES[i].offset] +=
1669 virt_to_bus(EXTERNAL_PATCHES[i].address);
1672 * Fixup absolutes set at boot-time.
1674 * All non-code absolute variables suffixed with "dsa_" and "int_"
1675 * are constants, and need no fixup provided the assembler has done
1676 * it for us (I don't know what the "real" NCR assembler does in
1677 * this case, my assembler does the right magic).
1680 patch_abs_rwri_data (hostdata->script, 0, dsa_save_data_pointer,
1681 Ent_dsa_code_save_data_pointer - Ent_dsa_zero);
1682 patch_abs_rwri_data (hostdata->script, 0, dsa_restore_pointers,
1683 Ent_dsa_code_restore_pointers - Ent_dsa_zero);
1684 patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
1685 Ent_dsa_code_check_reselect - Ent_dsa_zero);
1688 * Just for the hell of it, preserve the settings of
1689 * Burst Length and Enable Read Line bits from the DMODE
1690 * register. Make sure SCRIPTS start automagically.
1693 tmp = NCR53c7x0_read8(DMODE_REG_10);
1694 tmp &= (DMODE_800_ERL | DMODE_BL_MASK);
1696 if (!(hostdata->options & OPTION_MEMORY_MAPPED)) {
1697 base = (u32) host->io_port;
1698 memory_to_ncr = tmp|DMODE_800_DIOM;
1699 ncr_to_memory = tmp|DMODE_800_SIOM;
1700 } else {
1701 base = virt_to_bus(host->base);
1702 memory_to_ncr = ncr_to_memory = tmp;
1705 patch_abs_32 (hostdata->script, 0, addr_scratch, base + SCRATCHA_REG_800);
1706 patch_abs_32 (hostdata->script, 0, addr_temp, base + TEMP_REG);
1709 * I needed some variables in the script to be accessible to
1710 * both the NCR chip and the host processor. For these variables,
1711 * I made the arbitrary decision to store them directly in the
1712 * hostdata structure rather than in the RELATIVE area of the
1713 * SCRIPTS.
1717 patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_memory, tmp);
1718 patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_ncr, memory_to_ncr);
1719 patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_memory, ncr_to_memory);
1721 patch_abs_32 (hostdata->script, 0, msg_buf,
1722 virt_to_bus((void *)&(hostdata->msg_buf)));
1723 patch_abs_32 (hostdata->script, 0, reconnect_dsa_head,
1724 virt_to_bus((void *)&(hostdata->reconnect_dsa_head)));
1725 patch_abs_32 (hostdata->script, 0, addr_reconnect_dsa_head,
1726 virt_to_bus((void *)&(hostdata->addr_reconnect_dsa_head)));
1727 patch_abs_32 (hostdata->script, 0, reselected_identify,
1728 virt_to_bus((void *)&(hostdata->reselected_identify)));
1729 /* reselected_tag is currently unused */
1730 #if 0
1731 patch_abs_32 (hostdata->script, 0, reselected_tag,
1732 virt_to_bus((void *)&(hostdata->reselected_tag)));
1733 #endif
1735 patch_abs_32 (hostdata->script, 0, test_dest,
1736 virt_to_bus((void*)&hostdata->test_dest));
1737 patch_abs_32 (hostdata->script, 0, test_src,
1738 virt_to_bus(&hostdata->test_source));
1740 patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
1741 (unsigned char)(Ent_dsa_code_check_reselect - Ent_dsa_zero));
1743 /* These are for event logging; the ncr_event enum contains the
1744 actual interrupt numbers. */
1745 #ifdef A_int_EVENT_SELECT
1746 patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT, (u32) EVENT_SELECT);
1747 #endif
1748 #ifdef A_int_EVENT_DISCONNECT
1749 patch_abs_32 (hostdata->script, 0, int_EVENT_DISCONNECT, (u32) EVENT_DISCONNECT);
1750 #endif
1751 #ifdef A_int_EVENT_RESELECT
1752 patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT, (u32) EVENT_RESELECT);
1753 #endif
1754 #ifdef A_int_EVENT_COMPLETE
1755 patch_abs_32 (hostdata->script, 0, int_EVENT_COMPLETE, (u32) EVENT_COMPLETE);
1756 #endif
1757 #ifdef A_int_EVENT_IDLE
1758 patch_abs_32 (hostdata->script, 0, int_EVENT_IDLE, (u32) EVENT_IDLE);
1759 #endif
1760 #ifdef A_int_EVENT_SELECT_FAILED
1761 patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT_FAILED,
1762 (u32) EVENT_SELECT_FAILED);
1763 #endif
1764 #ifdef A_int_EVENT_BEFORE_SELECT
1765 patch_abs_32 (hostdata->script, 0, int_EVENT_BEFORE_SELECT,
1766 (u32) EVENT_BEFORE_SELECT);
1767 #endif
1768 #ifdef A_int_EVENT_RESELECT_FAILED
1769 patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT_FAILED,
1770 (u32) EVENT_RESELECT_FAILED);
1771 #endif
1774 * Make sure the NCR and Linux code agree on the location of
1775 * certain fields.
1778 hostdata->E_accept_message = Ent_accept_message;
1779 hostdata->E_command_complete = Ent_command_complete;
1780 hostdata->E_cmdout_cmdout = Ent_cmdout_cmdout;
1781 hostdata->E_data_transfer = Ent_data_transfer;
1782 hostdata->E_debug_break = Ent_debug_break;
1783 hostdata->E_dsa_code_template = Ent_dsa_code_template;
1784 hostdata->E_dsa_code_template_end = Ent_dsa_code_template_end;
1785 hostdata->E_end_data_transfer = Ent_end_data_transfer;
1786 hostdata->E_initiator_abort = Ent_initiator_abort;
1787 hostdata->E_msg_in = Ent_msg_in;
1788 hostdata->E_other_transfer = Ent_other_transfer;
1789 hostdata->E_other_in = Ent_other_in;
1790 hostdata->E_other_out = Ent_other_out;
1791 hostdata->E_reject_message = Ent_reject_message;
1792 hostdata->E_respond_message = Ent_respond_message;
1793 hostdata->E_select = Ent_select;
1794 hostdata->E_select_msgout = Ent_select_msgout;
1795 hostdata->E_target_abort = Ent_target_abort;
1796 #ifdef Ent_test_0
1797 hostdata->E_test_0 = Ent_test_0;
1798 #endif
1799 hostdata->E_test_1 = Ent_test_1;
1800 hostdata->E_test_2 = Ent_test_2;
1801 #ifdef Ent_test_3
1802 hostdata->E_test_3 = Ent_test_3;
1803 #endif
1804 hostdata->E_wait_reselect = Ent_wait_reselect;
1805 hostdata->E_dsa_code_begin = Ent_dsa_code_begin;
1807 hostdata->dsa_cmdout = A_dsa_cmdout;
1808 hostdata->dsa_cmnd = A_dsa_cmnd;
1809 hostdata->dsa_datain = A_dsa_datain;
1810 hostdata->dsa_dataout = A_dsa_dataout;
1811 hostdata->dsa_end = A_dsa_end;
1812 hostdata->dsa_msgin = A_dsa_msgin;
1813 hostdata->dsa_msgout = A_dsa_msgout;
1814 hostdata->dsa_msgout_other = A_dsa_msgout_other;
1815 hostdata->dsa_next = A_dsa_next;
1816 hostdata->dsa_select = A_dsa_select;
1817 hostdata->dsa_start = Ent_dsa_code_template - Ent_dsa_zero;
1818 hostdata->dsa_status = A_dsa_status;
1819 hostdata->dsa_jump_dest = Ent_dsa_code_fix_jump - Ent_dsa_zero +
1820 8 /* destination operand */;
1822 /* sanity check */
1823 if (A_dsa_fields_start != Ent_dsa_code_template_end -
1824 Ent_dsa_zero)
1825 printk("scsi%d : NCR dsa_fields start is %d not %d\n",
1826 host->host_no, A_dsa_fields_start, Ent_dsa_code_template_end -
1827 Ent_dsa_zero);
1828 #ifdef __powerpc__
1829 /* The PowerPC is Big Endian - adjust script appropriately */
1830 script_ptr = hostdata->script;
1831 for (i = 0; i < sizeof(SCRIPT); i += sizeof(long))
1833 *script_ptr++ = le32_to_cpu(*script_ptr);
1835 #endif
1837 printk("scsi%d : NCR code relocated to 0x%lx (virt 0x%p)\n", host->host_no,
1838 virt_to_bus(hostdata->script), hostdata->script);
1842 * Function : static int NCR53c8xx_run_tests (struct Scsi_Host *host)
1844 * Purpose : run various verification tests on the NCR chip,
1845 * including interrupt generation, and proper bus mastering
1846 * operation.
1848 * Inputs : host - a properly initialized Scsi_Host structure
1850 * Preconditions : the NCR chip must be in a halted state.
1852 * Returns : 0 if all tests were successful, -1 on error.
1856 static int
1857 NCR53c8xx_run_tests (struct Scsi_Host *host) {
1858 NCR53c7x0_local_declare();
1859 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1860 host->hostdata;
1861 unsigned long timeout;
1862 u32 start;
1863 int failed, i;
1864 unsigned long flags;
1865 NCR53c7x0_local_setup(host);
1867 /* The NCR chip _must_ be idle to run the test scripts */
1869 save_flags(flags);
1870 cli();
1871 if (!hostdata->idle) {
1872 printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
1873 restore_flags(flags);
1874 return -1;
1878 * Check for functional interrupts, this could work as an
1879 * autoprobe routine.
1882 if ((hostdata->options & OPTION_DEBUG_TEST1) &&
1883 hostdata->state != STATE_DISABLED) {
1884 hostdata->idle = 0;
1885 hostdata->test_running = 1;
1886 hostdata->test_completed = -1;
1887 hostdata->test_dest = 0;
1888 hostdata->test_source = 0xdeadbeef;
1889 start = virt_to_bus (hostdata->script) + hostdata->E_test_1;
1890 hostdata->state = STATE_RUNNING;
1891 printk ("scsi%d : test 1", host->host_no);
1892 NCR53c7x0_write32 (DSP_REG, start);
1893 printk (" started\n");
1894 restore_flags(flags);
1897 * This is currently a .5 second timeout, since (in theory) no slow
1898 * board will take that long. In practice, we've seen one
1899 * pentium which occasionally fails with this, but works with
1900 * 10 times as much?
1903 timeout = jiffies + 5 * HZ / 10;
1904 while ((hostdata->test_completed == -1) && jiffies < timeout)
1905 barrier();
1907 failed = 1;
1908 if (hostdata->test_completed == -1)
1909 printk ("scsi%d : driver test 1 timed out%s\n",host->host_no ,
1910 (hostdata->test_dest == 0xdeadbeef) ?
1911 " due to lost interrupt.\n"
1912 " Please verify that the correct IRQ is being used for your board,\n"
1913 " and that the motherboard IRQ jumpering matches the PCI setup on\n"
1914 " PCI systems.\n"
1915 " If you are using a NCR53c810 board in a PCI system, you should\n"
1916 " also verify that the board is jumpered to use PCI INTA, since\n"
1917 " most PCI motherboards lack support for INTB, INTC, and INTD.\n"
1918 : "");
1919 else if (hostdata->test_completed != 1)
1920 printk ("scsi%d : test 1 bad interrupt value (%d)\n",
1921 host->host_no, hostdata->test_completed);
1922 else
1923 failed = (hostdata->test_dest != 0xdeadbeef);
1925 if (hostdata->test_dest != 0xdeadbeef) {
1926 printk ("scsi%d : driver test 1 read 0x%x instead of 0xdeadbeef indicating a\n"
1927 " probable cache invalidation problem. Please configure caching\n"
1928 " as write-through or disabled\n",
1929 host->host_no, hostdata->test_dest);
1932 if (failed) {
1933 printk ("scsi%d : DSP = 0x%p (script at 0x%p, start at 0x%x)\n",
1934 host->host_no, bus_to_virt(NCR53c7x0_read32(DSP_REG)),
1935 hostdata->script, start);
1936 printk ("scsi%d : DSPS = 0x%x\n", host->host_no,
1937 NCR53c7x0_read32(DSPS_REG));
1938 restore_flags(flags);
1939 return -1;
1941 hostdata->test_running = 0;
1944 if ((hostdata->options & OPTION_DEBUG_TEST2) &&
1945 hostdata->state != STATE_DISABLED) {
1946 u32 dsa[48];
1947 unsigned char identify = IDENTIFY(0, 0);
1948 unsigned char cmd[6];
1949 unsigned char data[36];
1950 unsigned char status = 0xff;
1951 unsigned char msg = 0xff;
1953 cmd[0] = INQUIRY;
1954 cmd[1] = cmd[2] = cmd[3] = cmd[5] = 0;
1955 cmd[4] = sizeof(data);
1957 /* Need to adjust for endian-ness */
1958 dsa[2] = le32_to_cpu(1);
1959 dsa[3] = le32_to_cpu(virt_to_bus(&identify));
1960 dsa[4] = le32_to_cpu(6);
1961 dsa[5] = le32_to_cpu(virt_to_bus(&cmd));
1962 dsa[6] = le32_to_cpu(sizeof(data));
1963 dsa[7] = le32_to_cpu(virt_to_bus(&data));
1964 dsa[8] = le32_to_cpu(1);
1965 dsa[9] = le32_to_cpu(virt_to_bus(&status));
1966 dsa[10] = le32_to_cpu(1);
1967 dsa[11] = le32_to_cpu(virt_to_bus(&msg));
1969 for (i = 0; i < 3; ++i) {
1970 cli();
1971 if (!hostdata->idle) {
1972 printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
1973 restore_flags(flags);
1974 return -1;
1977 /* SCNTL3 SDID */
1978 dsa[0] = le32_to_cpu((0x33 << 24) | (i << 16)) ;
1979 hostdata->idle = 0;
1980 hostdata->test_running = 2;
1981 hostdata->test_completed = -1;
1982 start = virt_to_bus(hostdata->script) + hostdata->E_test_2;
1983 hostdata->state = STATE_RUNNING;
1984 NCR53c7x0_write32 (DSA_REG, virt_to_bus(dsa));
1985 NCR53c7x0_write32 (DSP_REG, start);
1986 restore_flags(flags);
1988 timeout = jiffies + 5 * HZ; /* arbitrary */
1989 while ((hostdata->test_completed == -1) && jiffies < timeout)
1990 barrier();
1991 NCR53c7x0_write32 (DSA_REG, 0);
1993 if (hostdata->test_completed == 2) {
1994 data[35] = 0;
1995 printk ("scsi%d : test 2 INQUIRY to target %d, lun 0 : %s\n",
1996 host->host_no, i, data + 8);
1997 printk ("scsi%d : status ", host->host_no);
1998 print_status (status);
1999 printk ("\nscsi%d : message ", host->host_no);
2000 print_msg (&msg);
2001 printk ("\n");
2002 } else if (hostdata->test_completed == 3) {
2003 printk("scsi%d : test 2 no connection with target %d\n",
2004 host->host_no, i);
2005 if (!hostdata->idle) {
2006 printk("scsi%d : not idle\n", host->host_no);
2007 restore_flags(flags);
2008 return -1;
2010 } else if (hostdata->test_completed == -1) {
2011 printk ("scsi%d : test 2 timed out\n", host->host_no);
2012 restore_flags(flags);
2013 return -1;
2015 hostdata->test_running = 0;
2019 restore_flags(flags);
2020 return 0;
2024 * Function : static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd)
2026 * Purpose : copy the NCR53c8xx dsa structure into cmd's dsa buffer,
2027 * performing all necessary relocation.
2029 * Inputs : cmd, a NCR53c7x0_cmd structure with a dsa area large
2030 * enough to hold the NCR53c8xx dsa.
2033 static void
2034 NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
2035 Scsi_Cmnd *c = cmd->cmd;
2036 struct Scsi_Host *host = c->host;
2037 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2038 host->hostdata;
2039 int i;
2040 #ifdef __powerpc__
2041 int len;
2042 unsigned long *dsa_ptr;
2043 #endif
2045 memcpy (cmd->dsa, hostdata->script + (hostdata->E_dsa_code_template / 4),
2046 hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template);
2047 #ifdef __powerpc__
2048 /* Note: the script has already been 'endianized' */
2049 dsa_ptr = cmd->dsa;
2050 len = hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template;
2051 for (i = 0; i < len; i += sizeof(long))
2053 *dsa_ptr++ = le32_to_cpu(*dsa_ptr);
2055 #endif
2058 * Note : within the NCR 'C' code, dsa points to the _start_
2059 * of the DSA structure, and _not_ the offset of dsa_zero within
2060 * that structure used to facilitate shorter signed offsets
2061 * for the 8 bit ALU.
2063 * The implications of this are that
2065 * - 32 bit A_dsa_* absolute values require an additional
2066 * dsa_zero added to their value to be correct, since they are
2067 * relative to dsa_zero which is in essentially a separate
2068 * space from the code symbols.
2070 * - All other symbols require no special treatment.
2073 patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2074 dsa_temp_lun, c->lun);
2075 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2076 dsa_temp_addr_next, virt_to_bus(&cmd->dsa_next_addr));
2077 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2078 dsa_temp_next, virt_to_bus(cmd->dsa) + Ent_dsa_zero -
2079 Ent_dsa_code_template + A_dsa_next);
2080 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2081 dsa_temp_sync, virt_to_bus((void *)hostdata->sync[c->target].script));
2082 patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2083 dsa_temp_target, c->target);
2084 /* XXX - new pointer stuff */
2085 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2086 dsa_temp_addr_saved_pointer, virt_to_bus(&cmd->saved_data_pointer));
2087 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2088 dsa_temp_addr_saved_residual, virt_to_bus(&cmd->saved_residual));
2089 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2090 dsa_temp_addr_residual, virt_to_bus(&cmd->residual));
2092 /* XXX - new start stuff */
2093 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2094 dsa_temp_addr_dsa_value, virt_to_bus(&cmd->dsa_addr));
2095 #ifdef __powerpc__
2096 dsa_ptr = cmd->dsa;
2097 len = hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template;
2098 for (i = 0; i < len; i += sizeof(long))
2100 *dsa_ptr++ = le32_to_cpu(*dsa_ptr);
2102 #endif
2107 * Function : run_process_issue_queue (void)
2109 * Purpose : insure that the coroutine is running and will process our
2110 * request. process_issue_queue_running is checked/set here (in an
2111 * inline function) rather than in process_issue_queue itself to reduce
2112 * the chances of stack overflow.
2116 static volatile int process_issue_queue_running = 0;
2118 static __inline__ void
2119 run_process_issue_queue(void) {
2120 unsigned long flags;
2121 save_flags (flags);
2122 cli();
2123 if (!process_issue_queue_running) {
2124 process_issue_queue_running = 1;
2125 process_issue_queue(flags);
2127 * process_issue_queue_running is cleared in process_issue_queue
2128 * once it can't do more work, and process_issue_queue exits with
2129 * interrupts disabled.
2132 restore_flags (flags);
2136 * Function : static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int
2137 * result)
2139 * Purpose : mark SCSI command as finished, OR'ing the host portion
2140 * of the result word into the result field of the corresponding
2141 * Scsi_Cmnd structure, and removing it from the internal queues.
2143 * Inputs : cmd - command, result - entire result field
2145 * Preconditions : the NCR chip should be in a halted state when
2146 * abnormal_finished is run, since it modifies structures which
2147 * the NCR expects to have exclusive access to.
2150 static void
2151 abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
2152 Scsi_Cmnd *c = cmd->cmd;
2153 struct Scsi_Host *host = c->host;
2154 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2155 host->hostdata;
2156 unsigned long flags;
2157 int left, found;
2158 volatile struct NCR53c7x0_cmd * linux_search;
2159 volatile struct NCR53c7x0_cmd * volatile *linux_prev;
2160 volatile u32 *ncr_prev, *curr, ncr_search;
2162 #if 0
2163 printk ("scsi%d: abnormal finished\n", host->host_no);
2164 #endif
2166 save_flags(flags);
2167 cli();
2168 found = 0;
2170 * Traverse the NCR issue array until we find a match or run out
2171 * of instructions. Instructions in the NCR issue array are
2172 * either JUMP or NOP instructions, which are 2 words in length.
2176 for (found = 0, left = host->can_queue, curr = hostdata->schedule;
2177 left > 0; --left, curr += 2)
2179 if (issue_to_cmd (host, hostdata, (u32 *) curr) == cmd)
2181 curr[0] = hostdata->NOP_insn;
2182 curr[1] = le32_to_cpu(0xdeadbeef);
2183 ++found;
2184 break;
2189 * Traverse the NCR reconnect list of DSA structures until we find
2190 * a pointer to this dsa or have found too many command structures.
2191 * We let prev point at the next field of the previous element or
2192 * head of the list, so we don't do anything different for removing
2193 * the head element.
2196 for (left = host->can_queue,
2197 ncr_search = le32_to_cpu(hostdata->reconnect_dsa_head),
2198 ncr_prev = &hostdata->reconnect_dsa_head;
2199 left >= 0 && ncr_search &&
2200 ((char*)bus_to_virt(ncr_search) + hostdata->dsa_start)
2201 != (char *) cmd->dsa;
2202 ncr_prev = (u32*) ((char*)bus_to_virt(ncr_search) +
2203 hostdata->dsa_next), ncr_search = le32_to_cpu(*ncr_prev), --left);
2205 if (left < 0) {
2206 printk("scsi%d: loop detected in ncr reconnect list\n",
2207 host->host_no);
2208 } else if (ncr_search) {
2209 if (found)
2210 printk("scsi%d: scsi %ld in ncr issue array and reconnect lists\n",
2211 host->host_no, c->pid);
2212 else {
2213 volatile u32 * next = (u32 *)
2214 ((char *)bus_to_virt(ncr_search) + hostdata->dsa_next);
2215 *ncr_prev = *next;
2216 /* If we're at the tail end of the issue queue, update that pointer too. */
2217 found = 1;
2222 * Traverse the host running list until we find this command or discover
2223 * we have too many elements, pointing linux_prev at the next field of the
2224 * linux_previous element or head of the list, search at this element.
2227 for (left = host->can_queue, linux_search = hostdata->running_list,
2228 linux_prev = &hostdata->running_list;
2229 left >= 0 && linux_search && linux_search != cmd;
2230 linux_prev = &(linux_search->next),
2231 linux_search = linux_search->next, --left);
2233 if (left < 0)
2234 printk ("scsi%d: loop detected in host running list for scsi pid %ld\n",
2235 host->host_no, c->pid);
2236 else if (linux_search) {
2237 *linux_prev = linux_search->next;
2238 --hostdata->busy[c->target][c->lun];
2241 /* Return the NCR command structure to the free list */
2242 cmd->next = hostdata->free;
2243 hostdata->free = cmd;
2244 c->host_scribble = NULL;
2246 /* And return */
2247 c->result = result;
2248 c->scsi_done(c);
2250 restore_flags(flags);
2251 run_process_issue_queue();
2255 * Function : static void intr_break (struct Scsi_Host *host,
2256 * struct NCR53c7x0_cmd *cmd)
2258 * Purpose : Handler for breakpoint interrupts from a SCSI script
2260 * Inputs : host - pointer to this host adapter's structure,
2261 * cmd - pointer to the command (if any) dsa was pointing
2262 * to.
2266 static void
2267 intr_break (struct Scsi_Host *host, struct
2268 NCR53c7x0_cmd *cmd) {
2269 NCR53c7x0_local_declare();
2270 struct NCR53c7x0_break *bp;
2271 #if 0
2272 Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
2273 #endif
2274 u32 *dsp;
2275 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2276 host->hostdata;
2277 unsigned long flags;
2278 NCR53c7x0_local_setup(host);
2281 * Find the break point corresponding to this address, and
2282 * dump the appropriate debugging information to standard
2283 * output.
2285 save_flags(flags);
2286 cli();
2287 dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
2288 for (bp = hostdata->breakpoints; bp && bp->address != dsp;
2289 bp = bp->next);
2290 if (!bp)
2291 panic("scsi%d : break point interrupt from %p with no breakpoint!",
2292 host->host_no, dsp);
2295 * Configure the NCR chip for manual start mode, so that we can
2296 * point the DSP register at the instruction that follows the
2297 * INT int_debug_break instruction.
2300 NCR53c7x0_write8 (hostdata->dmode,
2301 NCR53c7x0_read8(hostdata->dmode)|DMODE_MAN);
2304 * And update the DSP register, using the size of the old
2305 * instruction in bytes.
2308 restore_flags(flags);
2311 * Function : static void print_synchronous (const char *prefix,
2312 * const unsigned char *msg)
2314 * Purpose : print a pretty, user and machine parsable representation
2315 * of a SDTR message, including the "real" parameters, data
2316 * clock so we can tell transfer rate at a glance.
2318 * Inputs ; prefix - text to prepend, msg - SDTR message (5 bytes)
2321 static void
2322 print_synchronous (const char *prefix, const unsigned char *msg) {
2323 if (msg[4]) {
2324 int Hz = 1000000000 / (msg[3] * 4);
2325 int integer = Hz / 1000000;
2326 int fraction = (Hz - (integer * 1000000)) / 10000;
2327 printk ("%speriod %dns offset %d %d.%02dMHz %s SCSI%s\n",
2328 prefix, (int) msg[3] * 4, (int) msg[4], integer, fraction,
2329 (((msg[3] * 4) < 200) ? "FAST" : "synchronous"),
2330 (((msg[3] * 4) < 200) ? "-II" : ""));
2331 } else
2332 printk ("%sasynchronous SCSI\n", prefix);
2336 * Function : static void set_synchronous (struct Scsi_Host *host,
2337 * int target, int sxfer, int scntl3, int now_connected)
2339 * Purpose : reprogram transfers between the selected SCSI initiator and
2340 * target with the given register values; in the indirect
2341 * select operand, reselection script, and chip registers.
2343 * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
2344 * sxfer and scntl3 - NCR registers. now_connected - if non-zero,
2345 * we should reprogram the registers now too.
2348 static void
2349 set_synchronous (struct Scsi_Host *host, int target, int sxfer, int scntl3,
2350 int now_connected) {
2351 NCR53c7x0_local_declare();
2352 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2353 host->hostdata;
2354 u32 *script;
2355 NCR53c7x0_local_setup(host);
2357 /* These are eight bit registers */
2358 sxfer &= 0xff;
2359 scntl3 &= 0xff;
2361 hostdata->sync[target].sxfer_sanity = sxfer;
2362 hostdata->sync[target].scntl3_sanity = scntl3;
2365 * HARD CODED : synchronous script is EIGHT words long. This
2366 * must agree with 53c7.8xx.h
2369 if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
2370 hostdata->sync[target].select_indirect = (scntl3 << 24) |
2371 (target << 16) | (sxfer << 8);
2373 script = (u32 *) hostdata->sync[target].script;
2375 /* XXX - add NCR53c7x0 code to reprogram SCF bits if we want to */
2376 if ((hostdata->chip / 100) == 8) {
2377 script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
2378 DCMD_RWRI_OP_MOVE) << 24) |
2379 (SCNTL3_REG_800 << 16) | (scntl3 << 8);
2380 script[1] = 0;
2381 script += 2;
2384 script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
2385 DCMD_RWRI_OP_MOVE) << 24) |
2386 (SXFER_REG << 16) | (sxfer << 8);
2387 script[1] = 0;
2388 script += 2;
2390 #ifdef DEBUG_SYNC_INTR
2391 if (hostdata->options & OPTION_DEBUG_DISCONNECT) {
2392 script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_INT) << 24) | DBC_TCI_TRUE;
2393 script[1] = DEBUG_SYNC_INTR;
2394 script += 2;
2396 #endif
2398 script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24) | DBC_TCI_TRUE;
2399 script[1] = 0;
2400 script += 2;
2403 if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS)
2404 printk ("scsi%d : target %d sync parameters are sxfer=0x%x, scntl3=0x%x\n",
2405 host->host_no, target, sxfer, scntl3);
2407 if (now_connected) {
2408 if ((hostdata->chip / 100) == 8)
2409 NCR53c7x0_write8(SCNTL3_REG_800, scntl3);
2410 NCR53c7x0_write8(SXFER_REG, sxfer);
2416 * Function : static int asynchronous (struct Scsi_Host *host, int target)
2418 * Purpose : reprogram between the selected SCSI Host adapter and target
2419 * (assumed to be currently connected) for asynchronous transfers.
2421 * Inputs : host - SCSI host structure, target - numeric target ID.
2423 * Preconditions : the NCR chip should be in one of the halted states
2426 static void
2427 asynchronous (struct Scsi_Host *host, int target) {
2428 NCR53c7x0_local_declare();
2429 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2430 host->hostdata;
2431 NCR53c7x0_local_setup(host);
2432 set_synchronous (host, target, /* no offset */ 0, hostdata->saved_scntl3,
2434 printk ("scsi%d : setting target %d to asynchronous SCSI\n",
2435 host->host_no, target);
2439 * XXX - do we want to go out of our way (ie, add extra code to selection
2440 * in the NCR53c710/NCR53c720 script) to reprogram the synchronous
2441 * conversion bits, or can we be content in just setting the
2442 * sxfer bits?
2445 /* Table for NCR53c8xx synchronous values */
2446 static const struct {
2447 int div; /* Total clock divisor * 10 */
2448 unsigned char scf; /* */
2449 unsigned char tp; /* 4 + tp = xferp divisor */
2450 } syncs[] = {
2451 /* div scf tp div scf tp div scf tp */
2452 { 40, 1, 0}, { 50, 1, 1}, { 60, 1, 2},
2453 { 70, 1, 3}, { 75, 2, 1}, { 80, 1, 4},
2454 { 90, 1, 5}, { 100, 1, 6}, { 105, 2, 3},
2455 { 110, 1, 7}, { 120, 2, 4}, { 135, 2, 5},
2456 { 140, 3, 3}, { 150, 2, 6}, { 160, 3, 4},
2457 { 165, 2, 7}, { 180, 3, 5}, { 200, 3, 6},
2458 { 210, 4, 3}, { 220, 3, 7}, { 240, 4, 4},
2459 { 270, 4, 5}, { 300, 4, 6}, { 330, 4, 7}
2463 * Function : static void synchronous (struct Scsi_Host *host, int target,
2464 * char *msg)
2466 * Purpose : reprogram transfers between the selected SCSI initiator and
2467 * target for synchronous SCSI transfers such that the synchronous
2468 * offset is less than that requested and period at least as long
2469 * as that requested. Also modify *msg such that it contains
2470 * an appropriate response.
2472 * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
2473 * msg - synchronous transfer request.
2477 static void
2478 synchronous (struct Scsi_Host *host, int target, char *msg) {
2479 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2480 host->hostdata;
2481 int desire, divisor, i, limit;
2482 unsigned char scntl3, sxfer;
2483 /* The diagnostic message fits on one line, even with max. width integers */
2484 char buf[80];
2486 /* Desired transfer clock in Hz */
2487 desire = 1000000000L / (msg[3] * 4);
2488 /* Scale the available SCSI clock by 10 so we get tenths */
2489 divisor = (hostdata->scsi_clock * 10) / desire;
2491 /* NCR chips can handle at most an offset of 8 */
2492 if (msg[4] > 8)
2493 msg[4] = 8;
2495 if (hostdata->options & OPTION_DEBUG_SDTR)
2496 printk("scsi%d : optimal synchronous divisor of %d.%01d\n",
2497 host->host_no, divisor / 10, divisor % 10);
2499 limit = (sizeof(syncs) / sizeof(syncs[0]) -1);
2500 for (i = 0; (i < limit) && (divisor > syncs[i].div); ++i);
2502 if (hostdata->options & OPTION_DEBUG_SDTR)
2503 printk("scsi%d : selected synchronous divisor of %d.%01d\n",
2504 host->host_no, syncs[i].div / 10, syncs[i].div % 10);
2506 msg[3] = ((1000000000L / hostdata->scsi_clock) * syncs[i].div / 10 / 4);
2508 if (hostdata->options & OPTION_DEBUG_SDTR)
2509 printk("scsi%d : selected synchronous period of %dns\n", host->host_no,
2510 msg[3] * 4);
2512 scntl3 = (hostdata->chip / 100 == 8) ? ((hostdata->saved_scntl3 &
2513 ~SCNTL3_800_SCF_MASK) | (syncs[i].scf << SCNTL3_800_SCF_SHIFT)) : 0;
2514 sxfer = (msg[4] << SXFER_MO_SHIFT) | ((syncs[i].tp) << SXFER_TP_SHIFT);
2515 if (hostdata->options & OPTION_DEBUG_SDTR)
2516 printk ("scsi%d : sxfer=0x%x scntl3=0x%x\n",
2517 host->host_no, (int) sxfer, (int) scntl3);
2518 set_synchronous (host, target, sxfer, scntl3, 1);
2519 sprintf (buf, "scsi%d : setting target %d to ", host->host_no, target);
2520 print_synchronous (buf, msg);
2524 * Function : static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host,
2525 * struct NCR53c7x0_cmd *cmd)
2527 * Purpose : Handler for INT generated instructions for the
2528 * NCR53c810/820 SCSI SCRIPT
2530 * Inputs : host - pointer to this host adapter's structure,
2531 * cmd - pointer to the command (if any) dsa was pointing
2532 * to.
2536 static int
2537 NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
2538 NCR53c7x0_cmd *cmd) {
2539 NCR53c7x0_local_declare();
2540 int print;
2541 Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
2542 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2543 host->hostdata;
2544 u32 dsps,*dsp; /* Argument of the INT instruction */
2545 NCR53c7x0_local_setup(host);
2546 dsps = NCR53c7x0_read32(DSPS_REG);
2547 dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
2549 if (hostdata->options & OPTION_DEBUG_INTR)
2550 printk ("scsi%d : DSPS = 0x%x\n", host->host_no, dsps);
2552 switch (dsps) {
2553 case A_int_msg_1:
2554 print = 1;
2555 switch (hostdata->msg_buf[0]) {
2557 * Unless we've initiated synchronous negotiation, I don't
2558 * think that this should happen.
2560 case MESSAGE_REJECT:
2561 hostdata->dsp = hostdata->script + hostdata->E_accept_message /
2562 sizeof(u32);
2563 hostdata->dsp_changed = 1;
2564 if (cmd && (cmd->flags & CMD_FLAG_SDTR)) {
2565 printk ("scsi%d : target %d rejected SDTR\n", host->host_no,
2566 c->target);
2567 cmd->flags &= ~CMD_FLAG_SDTR;
2568 asynchronous (host, c->target);
2569 print = 0;
2571 break;
2572 case INITIATE_RECOVERY:
2573 printk ("scsi%d : extended contingent allegiance not supported yet, rejecting\n",
2574 host->host_no);
2575 /* Fall through to default */
2576 hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2577 sizeof(u32);
2578 hostdata->dsp_changed = 1;
2579 break;
2580 default:
2581 printk ("scsi%d : unsupported message, rejecting\n",
2582 host->host_no);
2583 hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2584 sizeof(u32);
2585 hostdata->dsp_changed = 1;
2587 if (print) {
2588 printk ("scsi%d : received message", host->host_no);
2589 if (c)
2590 printk (" from target %d lun %d ", c->target, c->lun);
2591 print_msg ((unsigned char *) hostdata->msg_buf);
2592 printk("\n");
2595 return SPECIFIC_INT_NOTHING;
2598 case A_int_msg_sdtr:
2600 * At this point, hostdata->msg_buf contains
2601 * 0 EXTENDED MESSAGE
2602 * 1 length
2603 * 2 SDTR
2604 * 3 period * 4ns
2605 * 4 offset
2608 if (cmd) {
2609 char buf[80];
2610 sprintf (buf, "scsi%d : target %d %s ", host->host_no, c->target,
2611 (cmd->flags & CMD_FLAG_SDTR) ? "accepting" : "requesting");
2612 print_synchronous (buf, (unsigned char *) hostdata->msg_buf);
2615 * Initiator initiated, won't happen unless synchronous
2616 * transfers are enabled. If we get a SDTR message in
2617 * response to our SDTR, we should program our parameters
2618 * such that
2619 * offset <= requested offset
2620 * period >= requested period
2622 if (cmd->flags & CMD_FLAG_SDTR) {
2623 cmd->flags &= ~CMD_FLAG_SDTR;
2624 if (hostdata->msg_buf[4])
2625 synchronous (host, c->target, (unsigned char *)
2626 hostdata->msg_buf);
2627 else
2628 asynchronous (host, c->target);
2629 hostdata->dsp = hostdata->script + hostdata->E_accept_message /
2630 sizeof(u32);
2631 hostdata->dsp_changed = 1;
2632 return SPECIFIC_INT_NOTHING;
2633 } else {
2634 if (hostdata->options & OPTION_SYNCHRONOUS) {
2635 cmd->flags |= CMD_FLAG_DID_SDTR;
2636 synchronous (host, c->target, (unsigned char *)
2637 hostdata->msg_buf);
2638 } else {
2639 hostdata->msg_buf[4] = 0; /* 0 offset = async */
2640 asynchronous (host, c->target);
2642 patch_dsa_32 (cmd->dsa, dsa_msgout_other, 0, le32_to_cpu(5));
2643 patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, (u32)
2644 le32_to_cpu(virt_to_bus ((void *)&hostdata->msg_buf)));
2645 hostdata->dsp = hostdata->script +
2646 hostdata->E_respond_message / sizeof(u32);
2647 hostdata->dsp_changed = 1;
2649 return SPECIFIC_INT_NOTHING;
2651 /* Fall through to abort if we couldn't find a cmd, and
2652 therefore a dsa structure to twiddle */
2653 case A_int_msg_wdtr:
2654 hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2655 sizeof(u32);
2656 hostdata->dsp_changed = 1;
2657 return SPECIFIC_INT_NOTHING;
2658 case A_int_err_unexpected_phase:
2659 if (hostdata->options & OPTION_DEBUG_INTR)
2660 printk ("scsi%d : unexpected phase\n", host->host_no);
2661 return SPECIFIC_INT_ABORT;
2662 case A_int_err_selected:
2663 printk ("scsi%d : selected by target %d\n", host->host_no,
2664 (int) NCR53c7x0_read8(SDID_REG_800) &7);
2665 hostdata->dsp = hostdata->script + hostdata->E_target_abort /
2666 sizeof(u32);
2667 hostdata->dsp_changed = 1;
2668 return SPECIFIC_INT_NOTHING;
2669 case A_int_err_unexpected_reselect:
2670 printk ("scsi%d : unexpected reselect by target %d lun %d\n",
2671 host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & 7,
2672 hostdata->reselected_identify & 7);
2673 hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
2674 sizeof(u32);
2675 hostdata->dsp_changed = 1;
2676 return SPECIFIC_INT_NOTHING;
2678 * Since contingent allegiance conditions are cleared by the next
2679 * command issued to a target, we must issue a REQUEST SENSE
2680 * command after receiving a CHECK CONDITION status, before
2681 * another command is issued.
2683 * Since this NCR53c7x0_cmd will be freed after use, we don't
2684 * care if we step on the various fields, so modify a few things.
2686 case A_int_err_check_condition:
2687 #if 0
2688 if (hostdata->options & OPTION_DEBUG_INTR)
2689 #endif
2690 printk ("scsi%d : CHECK CONDITION\n", host->host_no);
2691 if (!c) {
2692 printk("scsi%d : CHECK CONDITION with no SCSI command\n",
2693 host->host_no);
2694 return SPECIFIC_INT_PANIC;
2698 * FIXME : this uses the normal one-byte selection message.
2699 * We may want to renegotiate for synchronous & WIDE transfers
2700 * since these could be the crux of our problem.
2702 hostdata->NOP_insn* FIXME : once SCSI-II tagged queuing is implemented, we'll
2703 * have to set this up so that the rest of the DSA
2704 * agrees with this being an untagged queue'd command.
2707 patch_dsa_32 (cmd->dsa, dsa_msgout, 0, le32_to_cpu(1));
2710 * Modify the table indirect for COMMAND OUT phase, since
2711 * Request Sense is a six byte command.
2714 patch_dsa_32 (cmd->dsa, dsa_cmdout, 0, le32_to_cpu(6));
2716 c->cmnd[0] = REQUEST_SENSE;
2717 c->cmnd[1] &= 0xe0; /* Zero all but LUN */
2718 c->cmnd[2] = 0;
2719 c->cmnd[3] = 0;
2720 c->cmnd[4] = sizeof(c->sense_buffer);
2721 c->cmnd[5] = 0;
2724 * Disable dataout phase, and program datain to transfer to the
2725 * sense buffer, and add a jump to other_transfer after the
2726 * command so overflow/underrun conditions are detected.
2729 patch_dsa_32 (cmd->dsa, dsa_dataout, 0,
2730 le32_to_cpu(virt_to_bus(hostdata->script) + hostdata->E_other_transfer));
2731 patch_dsa_32 (cmd->dsa, dsa_datain, 0,
2732 le32_to_cpu(virt_to_bus(cmd->data_transfer_start)));
2733 cmd->data_transfer_start[0] = le32_to_cpu((((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I |
2734 DCMD_BMI_IO)) << 24) | sizeof(c->sense_buffer));
2735 cmd->data_transfer_start[1] = (u32) le32_to_cpu(virt_to_bus(c->sense_buffer));
2737 cmd->data_transfer_start[2] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP)
2738 << 24) | DBC_TCI_TRUE);
2739 cmd->data_transfer_start[3] = (u32) le32_to_cpu(virt_to_bus(hostdata->script) +
2740 hostdata->E_other_transfer);
2743 * Currently, this command is flagged as completed, ie
2744 * it has valid status and message data. Reflag it as
2745 * incomplete. Q - need to do something so that original
2746 * status, etc are used.
2749 cmd->cmd->result = le32_to_cpu(0xffff);
2752 * Restart command as a REQUEST SENSE.
2754 hostdata->dsp = (u32 *) hostdata->script + hostdata->E_select /
2755 sizeof(u32);
2756 hostdata->dsp_changed = 1;
2757 return SPECIFIC_INT_NOTHING;
2758 case A_int_debug_break:
2759 return SPECIFIC_INT_BREAK;
2760 case A_int_norm_aborted:
2761 hostdata->dsp = (u32 *) hostdata->schedule;
2762 hostdata->dsp_changed = 1;
2763 if (cmd)
2764 abnormal_finished (cmd, DID_ERROR << 16);
2765 return SPECIFIC_INT_NOTHING;
2766 case A_int_test_1:
2767 case A_int_test_2:
2768 hostdata->idle = 1;
2769 hostdata->test_completed = (dsps - A_int_test_1) / 0x00010000 + 1;
2770 if (hostdata->options & OPTION_DEBUG_INTR)
2771 printk("scsi%d : test%d complete\n", host->host_no,
2772 hostdata->test_completed);
2773 return SPECIFIC_INT_NOTHING;
2774 #ifdef A_int_debug_reselected_ok
2775 case A_int_debug_reselected_ok:
2776 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2777 OPTION_DEBUG_DISCONNECT)) {
2779 * Note - this dsa is not based on location relative to
2780 * the command structure, but to location relative to the
2781 * DSA register
2783 u32 *dsa;
2784 dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
2786 printk("scsi%d : reselected_ok (DSA = 0x%x (virt 0x%p)\n",
2787 host->host_no, NCR53c7x0_read32(DSA_REG), dsa);
2788 printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
2789 host->host_no, cmd->saved_data_pointer,
2790 bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)));
2791 print_insn (host, hostdata->script + Ent_reselected_ok /
2792 sizeof(u32), "", 1);
2793 printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
2794 host->host_no, NCR53c7x0_read8(SXFER_REG),
2795 NCR53c7x0_read8(SCNTL3_REG_800));
2796 if (c) {
2797 print_insn (host, (u32 *)
2798 hostdata->sync[c->target].script, "", 1);
2799 print_insn (host, (u32 *)
2800 hostdata->sync[c->target].script + 2, "", 1);
2803 return SPECIFIC_INT_RESTART;
2804 #endif
2805 #ifdef A_int_debug_reselect_check
2806 case A_int_debug_reselect_check:
2807 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2808 u32 *dsa;
2809 #if 0
2810 u32 *code;
2811 #endif
2813 * Note - this dsa is not based on location relative to
2814 * the command structure, but to location relative to the
2815 * DSA register
2817 dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
2818 printk("scsi%d : reselected_check_next (DSA = 0x%lx (virt 0x%p))\n",
2819 host->host_no, virt_to_bus(dsa), dsa);
2820 if (dsa) {
2821 printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
2822 host->host_no, cmd->saved_data_pointer,
2823 bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)));
2824 #if 0
2825 printk("scsi%d : template code :\n", host->host_no);
2826 for (code = dsa + (Ent_dsa_code_check_reselect - Ent_dsa_zero)
2827 / sizeof(u32); code < (dsa + Ent_dsa_zero / sizeof(u32));
2828 code += print_insn (host, code, "", 1));
2829 #endif
2831 print_insn (host, hostdata->script + Ent_reselected_ok /
2832 sizeof(u32), "", 1);
2834 return SPECIFIC_INT_RESTART;
2835 #endif
2836 #ifdef A_int_debug_dsa_schedule
2837 case A_int_debug_dsa_schedule:
2838 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2839 u32 *dsa;
2841 * Note - this dsa is not based on location relative to
2842 * the command structure, but to location relative to the
2843 * DSA register
2845 dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
2846 printk("scsi%d : dsa_schedule (old DSA = 0x%lx (virt 0x%p))\n",
2847 host->host_no, virt_to_bus(dsa), dsa);
2848 if (dsa)
2849 printk("scsi%d : resume address is 0x%x (virt 0x%p)\n"
2850 " (temp was 0x%x (virt 0x%p))\n",
2851 host->host_no, cmd->saved_data_pointer,
2852 bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)),
2853 NCR53c7x0_read32 (TEMP_REG),
2854 bus_to_virt (NCR53c7x0_read32(TEMP_REG)));
2856 return SPECIFIC_INT_RESTART;
2857 #endif
2858 #ifdef A_int_debug_scheduled
2859 case A_int_debug_scheduled:
2860 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2861 printk("scsi%d : new I/O 0x%x (virt 0x%p) scheduled\n",
2862 host->host_no, NCR53c7x0_read32(DSA_REG),
2863 bus_to_virt(NCR53c7x0_read32(DSA_REG)));
2865 return SPECIFIC_INT_RESTART;
2866 #endif
2867 #ifdef A_int_debug_idle
2868 case A_int_debug_idle:
2869 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2870 printk("scsi%d : idle\n", host->host_no);
2872 return SPECIFIC_INT_RESTART;
2873 #endif
2874 #ifdef A_int_debug_cmd
2875 case A_int_debug_cmd:
2876 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2877 printk("scsi%d : command sent\n");
2879 return SPECIFIC_INT_RESTART;
2880 #endif
2881 #ifdef A_int_debug_dsa_loaded
2882 case A_int_debug_dsa_loaded:
2883 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2884 printk("scsi%d : DSA loaded with 0x%x (virt 0x%p)\n", host->host_no,
2885 NCR53c7x0_read32(DSA_REG),
2886 bus_to_virt(NCR53c7x0_read32(DSA_REG)));
2888 return SPECIFIC_INT_RESTART;
2889 #endif
2890 #ifdef A_int_debug_reselected
2891 case A_int_debug_reselected:
2892 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2893 OPTION_DEBUG_DISCONNECT)) {
2894 printk("scsi%d : reselected by target %d lun %d\n",
2895 host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & ~0x80,
2896 (int) hostdata->reselected_identify & 7);
2897 print_queues(host);
2899 return SPECIFIC_INT_RESTART;
2900 #endif
2901 #ifdef A_int_debug_disconnect_msg
2902 case A_int_debug_disconnect_msg:
2903 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2904 if (c)
2905 printk("scsi%d : target %d lun %d disconnecting\n",
2906 host->host_no, c->target, c->lun);
2907 else
2908 printk("scsi%d : unknown target disconnecting\n",
2909 host->host_no);
2911 return SPECIFIC_INT_RESTART;
2912 #endif
2913 #ifdef A_int_debug_disconnected
2914 case A_int_debug_disconnected:
2915 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2916 OPTION_DEBUG_DISCONNECT)) {
2917 printk ("scsi%d : disconnected, new queues are\n",
2918 host->host_no);
2919 print_queues(host);
2920 #if 0
2921 printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
2922 host->host_no, NCR53c7x0_read8(SXFER_REG),
2923 NCR53c7x0_read8(SCNTL3_REG_800));
2924 #endif
2925 if (c) {
2926 print_insn (host, (u32 *)
2927 hostdata->sync[c->target].script, "", 1);
2928 print_insn (host, (u32 *)
2929 hostdata->sync[c->target].script + 2, "", 1);
2932 return SPECIFIC_INT_RESTART;
2933 #endif
2934 #ifdef A_int_debug_panic
2935 case A_int_debug_panic:
2936 printk("scsi%d : int_debug_panic received\n", host->host_no);
2937 print_lots (host);
2938 return SPECIFIC_INT_PANIC;
2939 #endif
2940 #ifdef A_int_debug_saved
2941 case A_int_debug_saved:
2942 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2943 OPTION_DEBUG_DISCONNECT)) {
2944 printk ("scsi%d : saved data pointer 0x%x (virt 0x%p)\n",
2945 host->host_no, cmd->saved_data_pointer,
2946 bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)));
2947 print_progress (c);
2949 return SPECIFIC_INT_RESTART;
2950 #endif
2951 #ifdef A_int_debug_restored
2952 case A_int_debug_restored:
2953 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2954 OPTION_DEBUG_DISCONNECT)) {
2955 if (cmd) {
2956 int size;
2957 printk ("scsi%d : restored data pointer 0x%x (virt 0x%p)\n",
2958 host->host_no, cmd->saved_data_pointer, bus_to_virt (
2959 le32_to_cpu(cmd->saved_data_pointer)));
2960 size = print_insn (host, (u32 *)
2961 bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)), "", 1);
2962 size = print_insn (host, (u32 *)
2963 bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)) + size, "", 1);
2964 print_progress (c);
2966 #if 0
2967 printk ("scsi%d : datapath residual %d\n",
2968 host->host_no, datapath_residual (host)) ;
2969 #endif
2971 return SPECIFIC_INT_RESTART;
2972 #endif
2973 #ifdef A_int_debug_sync
2974 case A_int_debug_sync:
2975 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2976 OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
2977 unsigned char sxfer = NCR53c7x0_read8 (SXFER_REG),
2978 scntl3 = NCR53c7x0_read8 (SCNTL3_REG_800);
2979 if (c) {
2980 if (sxfer != hostdata->sync[c->target].sxfer_sanity ||
2981 scntl3 != hostdata->sync[c->target].scntl3_sanity) {
2982 printk ("scsi%d : sync sanity check failed sxfer=0x%x, scntl3=0x%x",
2983 host->host_no, sxfer, scntl3);
2984 NCR53c7x0_write8 (SXFER_REG, sxfer);
2985 NCR53c7x0_write8 (SCNTL3_REG_800, scntl3);
2987 } else
2988 printk ("scsi%d : unknown command sxfer=0x%x, scntl3=0x%x\n",
2989 host->host_no, (int) sxfer, (int) scntl3);
2991 return SPECIFIC_INT_RESTART;
2992 #endif
2993 #ifdef A_int_debug_datain
2994 case A_int_debug_datain:
2995 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2996 OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
2997 int size;
2998 printk ("scsi%d : In do_datain (%s) sxfer=0x%x, scntl3=0x%x\n"
2999 " datapath residual=%d\n",
3000 host->host_no, sbcl_to_phase (NCR53c7x0_read8 (SBCL_REG)),
3001 (int) NCR53c7x0_read8(SXFER_REG),
3002 (int) NCR53c7x0_read8(SCNTL3_REG_800),
3003 datapath_residual (host)) ;
3004 print_insn (host, dsp, "", 1);
3005 size = print_insn (host, (u32 *) bus_to_virt(le32_to_cpu(dsp[1])), "", 1);
3006 print_insn (host, (u32 *) bus_to_virt(le32_to_cpu(dsp[1])) + size, "", 1);
3008 return SPECIFIC_INT_RESTART;
3009 #endif
3011 * FIXME : for 7xx support, we need to read SDID_REG_700 and handle
3012 * the comparison as bitfielded, not binary.
3014 #ifdef A_int_debug_check_dsa
3015 case A_int_debug_check_dsa:
3016 if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
3017 int sdid = NCR53c7x0_read8 (SDID_REG_800) & 15;
3018 char *where = dsp - NCR53c7x0_insn_size(NCR53c7x0_read8
3019 (DCMD_REG)) == hostdata->script +
3020 Ent_select_check_dsa / sizeof(u32) ?
3021 "selection" : "reselection";
3022 if (c && sdid != c->target) {
3023 printk ("scsi%d : SDID target %d != DSA target %d at %s\n",
3024 host->host_no, sdid, c->target, where);
3025 print_lots(host);
3026 dump_events (host, 20);
3027 return SPECIFIC_INT_PANIC;
3030 return SPECIFIC_INT_RESTART;
3031 #endif
3032 default:
3033 if ((dsps & 0xff000000) == 0x03000000) {
3034 printk ("scsi%d : misc debug interrupt 0x%x\n",
3035 host->host_no, dsps);
3036 return SPECIFIC_INT_RESTART;
3037 } else if ((dsps & 0xff000000) == 0x05000000) {
3038 if (hostdata->events) {
3039 struct NCR53c7x0_event *event;
3040 ++hostdata->event_index;
3041 if (hostdata->event_index >= hostdata->event_size)
3042 hostdata->event_index = 0;
3043 event = (struct NCR53c7x0_event *) hostdata->events +
3044 hostdata->event_index;
3045 event->event = (enum ncr_event) dsps;
3046 event->dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
3047 /* FIXME : this needs to change for the '7xx family */
3048 if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON)
3049 event->target = NCR53c7x0_read8(SSID_REG_800);
3050 else
3051 event->target = 255;
3053 if (event->event == EVENT_RESELECT)
3054 event->lun = hostdata->reselected_identify & 0xf;
3055 else if (c)
3056 event->lun = c->lun;
3057 else
3058 event->lun = 255;
3059 do_gettimeofday(&(event->time));
3060 if (c) {
3061 event->pid = c->pid;
3062 memcpy ((void *) event->cmnd, (void *) c->cmnd,
3063 sizeof (event->cmnd));
3064 } else {
3065 event->pid = -1;
3068 return SPECIFIC_INT_RESTART;
3071 printk ("scsi%d : unknown user interrupt 0x%x\n",
3072 host->host_no, (unsigned) dsps);
3073 return SPECIFIC_INT_PANIC;
3078 * XXX - the stock NCR assembler won't output the scriptu.h file,
3079 * which undefine's all #define'd CPP symbols from the script.h
3080 * file, which will create problems if you use multiple scripts
3081 * with the same symbol names.
3083 * If you insist on using NCR's assembler, you could generate
3084 * scriptu.h from script.h using something like
3086 * grep #define script.h | \
3087 * sed 's/#define[ ][ ]*\([_a-zA-Z][_a-zA-Z0-9]*\).*$/#undefine \1/' \
3088 * > scriptu.h
3091 #include "53c8xx_u.h"
3093 /* XXX - add alternate script handling code here */
3096 #ifdef NCR_DEBUG
3098 * Debugging without a debugger is no fun. So, I've provided
3099 * a debugging interface in the NCR53c7x0 driver. To avoid
3100 * kernel cruft, there's just enough here to act as an interface
3101 * to a user level debugger (aka, GDB).
3104 * The following restrictions apply to debugger commands :
3105 * 1. The command must be terminated by a newline.
3106 * 2. Command length must be less than 80 bytes including the
3107 * newline.
3108 * 3. The entire command must be written with one system call.
3111 static const char debugger_help =
3112 "bc <addr> - clear breakpoint\n"
3113 "bl - list breakpoints\n"
3114 "bs <addr> - set breakpoint\n"
3115 "g - start\n"
3116 "h - halt\n"
3117 "? - this message\n"
3118 "i - info\n"
3119 "mp <addr> <size> - print memory\n"
3120 "ms <addr> <size> <value> - store memory\n"
3121 "rp <num> <size> - print register\n"
3122 "rs <num> <size> <value> - store register\n"
3123 "s - single step\n"
3124 "tb - begin trace \n"
3125 "te - end trace\n";
3128 * Whenever we change a break point, we should probably
3129 * set the NCR up so that it is in a single step mode.
3132 static int debugger_fn_bc (struct Scsi_Host *host, struct debugger_token *token,
3133 u32 args[]) {
3134 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3135 instance->hostdata;
3136 struct NCR53c7x0_break *bp, **prev;
3137 unsigned long flags;
3138 save_flags(flags);
3139 cli();
3140 for (bp = (struct NCR53c7x0_break *) instance->breakpoints,
3141 prev = (struct NCR53c7x0_break **) &instance->breakpoints;
3142 bp; prev = (struct NCR53c7x0_break **) &(bp->next),
3143 bp = (struct NCR53c7x0_break *) bp->next);
3145 if (!bp) {
3146 restore_flags(flags);
3147 return -EIO;
3151 * XXX - we need to insure that the processor is halted
3152 * here in order to prevent a race condition.
3155 memcpy ((void *) bp->addr, (void *) bp->old, sizeof(bp->old));
3156 if (prev)
3157 *prev = bp->next;
3159 restore_flags(flags);
3160 return 0;
3164 static int
3165 debugger_fn_bl (struct Scsi_Host *host, struct debugger_token *token,
3166 u32 args[]) {
3167 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3168 host->hostdata;
3169 struct NCR53c7x0_break *bp;
3170 char buf[80];
3171 size_t len;
3172 unsigned long flags;
3174 * XXX - we need to insure that the processor is halted
3175 * here in order to prevent a race condition. So, if the
3176 * processor isn't halted, print an error message and continue.
3179 sprintf (buf, "scsi%d : bp : warning : processor not halted\b",
3180 host->host_no);
3181 debugger_kernel_write (host, buf, strlen(buf));
3183 save_flags(flags);
3184 cli();
3185 for (bp = (struct NCR53c7x0_break *) host->breakpoints;
3186 bp; bp = (struct NCR53c7x0_break *) bp->next); {
3187 sprintf (buf, "scsi%d : bp : success : at %08x, replaces %08x %08x",
3188 bp->addr, bp->old[0], bp->old[1]);
3189 len = strlen(buf);
3190 if ((bp->old[0] & (DCMD_TYPE_MASK << 24)) ==
3191 (DCMD_TYPE_MMI << 24)) {
3192 sprintf(buf + len, "%08x\n", * (u32 *) bp->addr);
3193 } else {
3194 sprintf(buf + len, "\n");
3196 len = strlen(buf);
3197 debugger_kernel_write (host, buf, len);
3199 restore_flags(flags);
3200 return 0;
3203 static int
3204 debugger_fn_bs (struct Scsi_Host *host, struct debugger_token *token,
3205 u32 args[]) {
3206 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3207 host->hostdata;
3208 struct NCR53c7x0_break *bp;
3209 char buf[80];
3210 size_t len;
3211 unsigned long flags;
3212 save_flags(flags);
3213 cli();
3215 if (hostdata->state != STATE_HALTED) {
3216 sprintf (buf, "scsi%d : bs : failure : NCR not halted\n", host->host_no);
3217 debugger_kernel_write (host, buf, strlen(buf));
3218 restore_flags(flags);
3219 return -1;
3222 if (!(bp = kmalloc (sizeof (struct NCR53c7x0_break)))) {
3223 printk ("scsi%d : kmalloc(%d) of breakpoint structure failed, try again\n",
3224 host->host_no, sizeof(struct NCR53c7x0_break));
3225 restore_flags(flags);
3226 return -1;
3229 bp->address = (u32 *) args[0];
3230 memcpy ((void *) bp->old_instruction, (void *) bp->address, 8);
3231 bp->old_size = (((bp->old_instruction[0] >> 24) & DCMD_TYPE_MASK) ==
3232 DCMD_TYPE_MMI ? 3 : 2;
3233 bp->next = hostdata->breakpoints;
3234 hostdata->breakpoints = bp->next;
3235 memcpy ((void *) bp->address, (void *) hostdata->E_debug_break, 8);
3237 restore_flags(flags);
3238 return 0;
3241 #define TOKEN(name,nargs) {#name, nargs, debugger_fn_##name}
3242 static const struct debugger_token {
3243 char *name;
3244 int numargs;
3245 int (*fn)(struct debugger_token *token, u32 args[]);
3246 } debugger_tokens[] = {
3247 TOKEN(bc,1), TOKEN(bl,0), TOKEN(bs,1), TOKEN(g,0), TOKEN(halt,0),
3248 {DT_help, "?", 0} , TOKEN(h,0), TOKEN(i,0), TOKEN(mp,2),
3249 TOKEN(ms,3), TOKEN(rp,2), TOKEN(rs,2), TOKEN(s,0), TOKEN(tb,0), TOKEN(te,0)
3252 #define NDT sizeof(debugger_tokens / sizeof(struct debugger_token))
3254 static struct Scsi_Host * inode_to_host (struct inode *inode) {
3255 int dev;
3256 struct Scsi_Host *tmp;
3257 for (dev = MINOR(inode->rdev), host = first_host;
3258 (host->hostt == the_template); --dev, host = host->next)
3259 if (!dev) return host;
3260 return NULL;
3264 static int
3265 debugger_user_write (struct inode *inode,struct file *filp,
3266 char *buf,int count) {
3267 struct Scsi_Host *host; /* This SCSI host */
3268 struct NCR53c7x0_hostadata *hostdata;
3269 char input_buf[80], /* Kernel space copy of buf */
3270 *ptr; /* Pointer to argument list */
3271 u32 args[3]; /* Arguments */
3272 int i, j, error, len;
3274 if (!(host = inode_to_host(inode)))
3275 return -ENXIO;
3277 hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
3279 if (error = verify_area(VERIFY_READ,buf,count))
3280 return error;
3282 if (count > 80)
3283 return -EIO;
3285 memcpy_from_fs(input_buf, buf, count);
3287 if (input_buf[count - 1] != '\n')
3288 return -EIO;
3290 input_buf[count - 1]=0;
3292 for (i = 0; i < NDT; ++i) {
3293 len = strlen (debugger_tokens[i].name);
3294 if (!strncmp(input_buf, debugger_tokens[i].name, len))
3295 break;
3298 if (i == NDT)
3299 return -EIO;
3301 for (ptr = input_buf + len, j = 0; j < debugger_tokens[i].nargs && *ptr;) {
3302 if (*ptr == ' ' || *ptr == '\t') {
3303 ++ptr;
3304 } else if (isdigit(*ptr)) {
3305 args[j++] = simple_strtoul (ptr, &ptr, 0);
3306 } else {
3307 return -EIO;
3311 if (j != debugger_tokens[i].nargs)
3312 return -EIO;
3314 return count;
3317 static int
3318 debugger_user_read (struct inode *inode,struct file *filp,
3319 char *buf,int count) {
3320 struct Scsi_Host *instance;
3324 static int
3325 debugger_kernel_write (struct Scsi_Host *host, char *buf, size_t
3326 buflen) {
3327 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3328 host->hostdata;
3329 int copy, left;
3330 unsigned long flags;
3331 save_flags(flags);
3332 cli();
3333 while (buflen) {
3334 left = (hostdata->debug_buf + hostdata->debug_size - 1) -
3335 hostdata->debug_write;
3336 copy = (buflen <= left) ? buflen : left;
3337 memcpy (hostdata->debug_write, buf, copy);
3338 buf += copy;
3339 buflen -= copy;
3340 hostdata->debug_count += copy;
3341 if ((hostdata->debug_write += copy) ==
3342 (hostdata->debug_buf + hostdata->debug_size))
3343 hosdata->debug_write = hostdata->debug_buf;
3345 restore_flags(flags);
3348 #endif /* def NCRDEBUG */
3351 * Function : static void NCR538xx_soft_reset (struct Scsi_Host *host)
3353 * Purpose : perform a soft reset of the NCR53c8xx chip
3355 * Inputs : host - pointer to this host adapter's structure
3357 * Preconditions : NCR53c7x0_init must have been called for this
3358 * host.
3362 static void
3363 NCR53c8x0_soft_reset (struct Scsi_Host *host) {
3364 NCR53c7x0_local_declare();
3365 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3366 host->hostdata;
3367 NCR53c7x0_local_setup(host);
3371 * Do a soft reset of the chip so that everything is
3372 * reinitialized to the power-on state.
3374 * Basically follow the procedure outlined in the NCR53c700
3375 * data manual under Chapter Six, How to Use, Steps Necessary to
3376 * Start SCRIPTS, with the exception of actually starting the
3377 * script and setting up the synchronous transfer gunk.
3380 NCR53c7x0_write8(ISTAT_REG_800, ISTAT_10_SRST);
3381 NCR53c7x0_write8(ISTAT_REG_800, 0);
3382 NCR53c7x0_write8(hostdata->dmode, hostdata->saved_dmode & ~DMODE_MAN);
3386 * Respond to reselection by targets and use our _initiator_ SCSI ID
3387 * for arbitration. If notyet, also respond to SCSI selection.
3389 * XXX - Note : we must reprogram this when reselecting as
3390 * a target.
3393 #ifdef notyet
3394 NCR53c7x0_write8(SCID_REG, (host->this_id & 7)|SCID_800_RRE|SCID_800_SRE);
3395 #else
3396 NCR53c7x0_write8(SCID_REG, (host->this_id & 7)|SCID_800_RRE);
3397 #endif
3398 NCR53c7x0_write8(RESPID_REG_800, hostdata->this_id_mask);
3401 * Use a maximum (1.6) second handshake to handshake timeout,
3402 * and SCSI recommended .5s selection timeout.
3406 * The new gcc won't recognize preprocessing directives
3407 * within macro args.
3409 #if 0
3410 NCR53c7x0_write8(STIME0_REG_800,
3411 ((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK)
3412 | ((15 << STIME0_800_HTH_SHIFT) & STIME0_800_HTH_MASK));
3413 #else
3414 /* Disable HTH interrupt */
3415 NCR53c7x0_write8(STIME0_REG_800,
3416 ((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK));
3417 #endif
3421 * Enable active negation for happy synchronous transfers.
3424 NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
3427 * Enable all interrupts, except parity which we only want when
3428 * the user requests it.
3431 NCR53c7x0_write8(DIEN_REG, DIEN_800_MDPE | DIEN_800_BF |
3432 DIEN_ABRT | DIEN_SSI | DIEN_SIR | DIEN_800_IID);
3435 NCR53c7x0_write8(SIEN0_REG_800, ((hostdata->options & OPTION_PARITY) ?
3436 SIEN_PAR : 0) | SIEN_RST | SIEN_UDC | SIEN_SGE | SIEN_MA);
3437 NCR53c7x0_write8(SIEN1_REG_800, SIEN1_800_STO | SIEN1_800_HTH);
3440 * Use saved clock frequency divisor and scripts loaded in 16 bit
3441 * mode flags from the saved dcntl.
3444 NCR53c7x0_write8(DCNTL_REG, hostdata->saved_dcntl);
3445 NCR53c7x0_write8(CTEST4_REG_800, hostdata->saved_ctest4);
3447 /* Enable active negation */
3448 NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
3452 * Function static struct NCR53c7x0_cmd *allocate_cmd (Scsi_Cmnd *cmd)
3454 * Purpose : Return the first free NCR53c7x0_cmd structure (which are
3455 * reused in a LIFO manner to minimize cache thrashing).
3457 * Side effects : If we haven't yet scheduled allocation of NCR53c7x0_cmd
3458 * structures for this device, do so. Attempt to complete all scheduled
3459 * allocations using kmalloc(), putting NCR53c7x0_cmd structures on
3460 * the free list. Teach programmers not to drink and hack.
3462 * Inputs : cmd - SCSI command
3464 * Returns : NCR53c7x0_cmd structure allocated on behalf of cmd;
3465 * NULL on failure.
3468 static struct NCR53c7x0_cmd *
3469 allocate_cmd (Scsi_Cmnd *cmd) {
3470 struct Scsi_Host *host = cmd->host;
3471 struct NCR53c7x0_hostdata *hostdata =
3472 (struct NCR53c7x0_hostdata *) host->hostdata;
3473 void *real; /* Real address */
3474 int size; /* Size of *tmp */
3475 struct NCR53c7x0_cmd *tmp;
3476 unsigned long flags;
3478 if (hostdata->options & OPTION_DEBUG_ALLOCATION)
3479 printk ("scsi%d : num_cmds = %d, can_queue = %d\n"
3480 " target = %d, lun = %d, %s\n",
3481 host->host_no, hostdata->num_cmds, host->can_queue,
3482 cmd->target, cmd->lun, (hostdata->cmd_allocated[cmd->target] &
3483 (1 << cmd->lun)) ? "already allocated" : "not allocated");
3486 * If we have not yet reserved commands for this I_T_L nexus, and
3487 * the device exists (as indicated by permanent Scsi_Cmnd structures
3488 * being allocated under 1.3.x, or being outside of scan_scsis in
3489 * 1.2.x), do so now.
3491 if (!(hostdata->cmd_allocated[cmd->target] & (1 << cmd->lun)) &&
3492 #ifdef LINUX_1_2
3493 !in_scan_scsis
3494 #else
3495 cmd->device && cmd->device->has_cmdblocks
3496 #endif
3498 if ((hostdata->extra_allocate + hostdata->num_cmds) < host->can_queue)
3499 hostdata->extra_allocate += host->cmd_per_lun;
3500 hostdata->cmd_allocated[cmd->target] |= (1 << cmd->lun);
3503 for (; hostdata->extra_allocate > 0 ; --hostdata->extra_allocate,
3504 ++hostdata->num_cmds) {
3505 /* historically, kmalloc has returned unaligned addresses; pad so we
3506 have enough room to ROUNDUP */
3507 size = hostdata->max_cmd_size + sizeof (void *);
3508 /* FIXME: for ISA bus '7xx chips, we need to or GFP_DMA in here */
3509 real = kmalloc (size, GFP_ATOMIC);
3510 if (!real) {
3511 if (hostdata->options & OPTION_DEBUG_ALLOCATION)
3512 printk ("scsi%d : kmalloc(%d) failed\n",
3513 host->host_no, size);
3514 break;
3516 tmp = ROUNDUP(real, void *);
3517 tmp->real = real;
3518 tmp->size = size;
3519 #ifdef LINUX_1_2
3520 tmp->free = ((void (*)(void *, int)) kfree_s);
3521 #else
3522 tmp->free = ((void (*)(void *, int)) kfree);
3523 #endif
3524 save_flags (flags);
3525 cli();
3526 tmp->next = hostdata->free;
3527 hostdata->free = tmp;
3528 restore_flags (flags);
3530 save_flags(flags);
3531 cli();
3532 tmp = (struct NCR53c7x0_cmd *) hostdata->free;
3533 if (tmp) {
3534 hostdata->free = tmp->next;
3536 restore_flags(flags);
3537 if (!tmp)
3538 printk ("scsi%d : can't allocate command for target %d lun %d\n",
3539 host->host_no, cmd->target, cmd->lun);
3540 return tmp;
3544 * Function static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd)
3547 * Purpose : allocate a NCR53c7x0_cmd structure, initialize it based on the
3548 * Scsi_Cmnd structure passed in cmd, including dsa and Linux field
3549 * initialization, and dsa code relocation.
3551 * Inputs : cmd - SCSI command
3553 * Returns : NCR53c7x0_cmd structure corresponding to cmd,
3554 * NULL on failure.
3557 static struct NCR53c7x0_cmd *
3558 create_cmd (Scsi_Cmnd *cmd) {
3559 NCR53c7x0_local_declare();
3560 struct Scsi_Host *host = cmd->host;
3561 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3562 host->hostdata;
3563 struct NCR53c7x0_cmd *tmp; /* NCR53c7x0_cmd structure for this command */
3564 int datain, /* Number of instructions per phase */
3565 dataout;
3566 int data_transfer_instructions, /* Count of dynamic instructions */
3567 i; /* Counter */
3568 u32 *cmd_datain, /* Address of datain/dataout code */
3569 *cmd_dataout; /* Incremented as we assemble */
3570 #ifdef notyet
3571 unsigned char *msgptr; /* Current byte in select message */
3572 int msglen; /* Length of whole select message */
3573 #endif
3574 unsigned long flags;
3575 NCR53c7x0_local_setup(cmd->host);
3577 if (!(tmp = allocate_cmd (cmd)))
3578 return NULL;
3582 * Decide whether we need to generate commands for DATA IN,
3583 * DATA OUT, neither, or both based on the SCSI command
3586 switch (cmd->cmnd[0]) {
3587 /* These commands do DATA IN */
3588 case INQUIRY:
3589 case MODE_SENSE:
3590 case READ_6:
3591 case READ_10:
3592 case READ_CAPACITY:
3593 case REQUEST_SENSE:
3594 datain = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3595 dataout = 0;
3596 break;
3597 /* These commands do DATA OUT */
3598 case MODE_SELECT:
3599 case WRITE_6:
3600 case WRITE_10:
3601 #if 0
3602 printk("scsi%d : command is ", host->host_no);
3603 print_command(cmd->cmnd);
3604 #endif
3605 #if 0
3606 printk ("scsi%d : %d scatter/gather segments\n", host->host_no,
3607 cmd->use_sg);
3608 #endif
3609 datain = 0;
3610 dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3611 #if 0
3612 hostdata->options |= OPTION_DEBUG_INTR;
3613 #endif
3614 break;
3616 * These commands do no data transfer, we should force an
3617 * interrupt if a data phase is attempted on them.
3619 case START_STOP: /* also SCAN, which may do DATA OUT */
3620 case TEST_UNIT_READY:
3621 datain = dataout = 0;
3622 break;
3624 * We don't know about these commands, so generate code to handle
3625 * both DATA IN and DATA OUT phases.
3627 default:
3628 datain = dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3632 * New code : so that active pointers work correctly regardless
3633 * of where the saved data pointer is at, we want to immediately
3634 * enter the dynamic code after selection, and on a non-data
3635 * phase perform a CALL to the non-data phase handler, with
3636 * returns back to this address.
3638 * If a phase mismatch is encountered in the middle of a
3639 * Block MOVE instruction, we want to _leave_ that instruction
3640 * unchanged as the current case is, modify a temporary buffer,
3641 * and point the active pointer (TEMP) at that.
3643 * Furthermore, we want to implement a saved data pointer,
3644 * set by the SAVE_DATA_POINTERs message.
3646 * So, the data transfer segments will change to
3647 * CALL data_transfer, WHEN NOT data phase
3648 * MOVE x, x, WHEN data phase
3649 * ( repeat )
3650 * JUMP other_transfer
3653 data_transfer_instructions = datain + dataout;
3656 * When we perform a request sense, we overwrite various things,
3657 * including the data transfer code. Make sure we have enough
3658 * space to do that.
3661 if (data_transfer_instructions < 2)
3662 data_transfer_instructions = 2;
3666 * The saved data pointer is set up so that a RESTORE POINTERS message
3667 * will start the data transfer over at the beginning.
3670 tmp->saved_data_pointer = le32_to_cpu(virt_to_bus (hostdata->script) +
3671 hostdata->E_data_transfer);
3674 * Initialize Linux specific fields.
3677 tmp->cmd = cmd;
3678 tmp->next = NULL;
3679 tmp->flags = 0;
3680 tmp->dsa_next_addr = le32_to_cpu(virt_to_bus(tmp->dsa) + hostdata->dsa_next -
3681 hostdata->dsa_start);
3682 tmp->dsa_addr = le32_to_cpu(virt_to_bus(tmp->dsa) - hostdata->dsa_start);
3685 * Calculate addresses of dynamic code to fill in DSA
3688 tmp->data_transfer_start = tmp->dsa + (hostdata->dsa_end -
3689 hostdata->dsa_start) / sizeof(u32);
3690 tmp->data_transfer_end = tmp->data_transfer_start +
3691 2 * data_transfer_instructions;
3693 cmd_datain = datain ? tmp->data_transfer_start : NULL;
3694 cmd_dataout = dataout ? (datain ? cmd_datain + 2 * datain : tmp->
3695 data_transfer_start) : NULL;
3698 * Fill in the NCR53c7x0_cmd structure as follows
3699 * dsa, with fixed up DSA code
3700 * datain code
3701 * dataout code
3704 /* Copy template code into dsa and perform all necessary fixups */
3705 if (hostdata->dsa_fixup)
3706 hostdata->dsa_fixup(tmp);
3708 patch_dsa_32(tmp->dsa, dsa_next, 0, le32_to_cpu(0));
3709 patch_dsa_32(tmp->dsa, dsa_cmnd, 0, le32_to_cpu(virt_to_bus(cmd)));
3711 if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS)
3712 if (hostdata->sync[cmd->target].select_indirect !=
3713 ((hostdata->sync[cmd->target].scntl3_sanity << 24) |
3714 (cmd->target << 16) |
3715 (hostdata->sync[cmd->target].sxfer_sanity << 8))) {
3716 printk ("scsi%d : sanity check failed select_indirect=0x%x\n",
3717 host->host_no, hostdata->sync[cmd->target].select_indirect);
3718 FATAL(host);
3722 patch_dsa_32(tmp->dsa, dsa_select, 0, le32_to_cpu(hostdata->sync[cmd->target].
3723 select_indirect));
3725 * Right now, we'll do the WIDE and SYNCHRONOUS negotiations on
3726 * different commands; although it should be trivial to do them
3727 * both at the same time.
3729 if (hostdata->initiate_wdtr & (1 << cmd->target)) {
3730 memcpy ((void *) (tmp->select + 1), (void *) wdtr_message,
3731 sizeof(wdtr_message));
3732 patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(wdtr_message)));
3733 save_flags(flags);
3734 cli();
3735 hostdata->initiate_wdtr &= ~(1 << cmd->target);
3736 restore_flags(flags);
3737 } else if (hostdata->initiate_sdtr & (1 << cmd->target)) {
3738 memcpy ((void *) (tmp->select + 1), (void *) sdtr_message,
3739 sizeof(sdtr_message));
3740 patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(sdtr_message)));
3741 tmp->flags |= CMD_FLAG_SDTR;
3742 save_flags(flags);
3743 cli();
3744 hostdata->initiate_sdtr &= ~(1 << cmd->target);
3745 restore_flags(flags);
3748 #if 1
3749 else if (!(hostdata->talked_to & (1 << cmd->target)) &&
3750 !(hostdata->options & OPTION_NO_ASYNC)) {
3751 memcpy ((void *) (tmp->select + 1), (void *) async_message,
3752 sizeof(async_message));
3753 patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(async_message)));
3754 tmp->flags |= CMD_FLAG_SDTR;
3756 #endif
3757 else
3758 patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1));
3759 hostdata->talked_to |= (1 << cmd->target);
3760 tmp->select[0] = (hostdata->options & OPTION_DISCONNECT) ?
3761 IDENTIFY (1, cmd->lun) : IDENTIFY (0, cmd->lun);
3762 patch_dsa_32(tmp->dsa, dsa_msgout, 1, le32_to_cpu(virt_to_bus(tmp->select)));
3763 patch_dsa_32(tmp->dsa, dsa_cmdout, 0, le32_to_cpu(cmd->cmd_len));
3764 patch_dsa_32(tmp->dsa, dsa_cmdout, 1, le32_to_cpu(virt_to_bus(cmd->cmnd)));
3765 patch_dsa_32(tmp->dsa, dsa_dataout, 0, le32_to_cpu(cmd_dataout ?
3766 virt_to_bus (cmd_dataout)
3767 : virt_to_bus (hostdata->script) + hostdata->E_other_transfer));
3768 patch_dsa_32(tmp->dsa, dsa_datain, 0, le32_to_cpu(cmd_datain ?
3769 virt_to_bus (cmd_datain)
3770 : virt_to_bus (hostdata->script) + hostdata->E_other_transfer));
3772 * XXX - need to make endian aware, should use separate variables
3773 * for both status and message bytes.
3775 patch_dsa_32(tmp->dsa, dsa_msgin, 0, le32_to_cpu(1));
3777 * FIXME : these only works for little endian. We probably want to
3778 * provide message and status fields in the NCR53c7x0_cmd
3779 * structure, and assign them to cmd->result when we're done.
3781 patch_dsa_32(tmp->dsa, dsa_msgin, 1, le32_to_cpu(virt_to_bus(&cmd->result) + 1));
3782 patch_dsa_32(tmp->dsa, dsa_status, 0, le32_to_cpu(1));
3783 patch_dsa_32(tmp->dsa, dsa_status, 1, le32_to_cpu(virt_to_bus(&cmd->result)));
3784 patch_dsa_32(tmp->dsa, dsa_msgout_other, 0, le32_to_cpu(1));
3785 patch_dsa_32(tmp->dsa, dsa_msgout_other, 1,
3786 le32_to_cpu(virt_to_bus(&(hostdata->NCR53c7xx_msg_nop))));
3789 * Generate code for zero or more of the DATA IN, DATA OUT phases
3790 * in the format
3792 * CALL data_transfer, WHEN NOT phase
3793 * MOVE first buffer length, first buffer address, WHEN phase
3794 * ...
3795 * MOVE last buffer length, last buffer address, WHEN phase
3796 * JUMP other_transfer
3800 * See if we're getting to data transfer by generating an unconditional
3801 * interrupt.
3803 #if 0
3804 if (datain) {
3805 cmd_datain[0] = 0x98080000;
3806 cmd_datain[1] = 0x03ffd00d;
3807 cmd_datain += 2;
3809 #endif
3812 * XXX - I'm undecided whether all of this nonsense is faster
3813 * in the long run, or whether I should just go and implement a loop
3814 * on the NCR chip using table indirect mode?
3816 * In any case, this is how it _must_ be done for 53c700/700-66 chips,
3817 * so this stays even when we come up with something better.
3819 * When we're limited to 1 simultaneous command, no overlapping processing,
3820 * we're seeing 630K/sec, with 7% CPU usage on a slow Syquest 45M
3821 * drive.
3823 * Not bad, not good. We'll see.
3826 for (i = 0; cmd->use_sg ? (i < cmd->use_sg) : !i; cmd_datain += 4,
3827 cmd_dataout += 4, ++i) {
3828 u32 buf = cmd->use_sg ?
3829 virt_to_bus(((struct scatterlist *)cmd->buffer)[i].address) :
3830 virt_to_bus(cmd->request_buffer);
3831 u32 count = cmd->use_sg ?
3832 ((struct scatterlist *)cmd->buffer)[i].length :
3833 cmd->request_bufflen;
3835 if (datain) {
3836 /* CALL other_in, WHEN NOT DATA_IN */
3837 cmd_datain[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
3838 DCMD_TCI_IO) << 24) |
3839 DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
3840 cmd_datain[1] = le32_to_cpu(virt_to_bus (hostdata->script) +
3841 hostdata->E_other_in);
3842 /* MOVE count, buf, WHEN DATA_IN */
3843 cmd_datain[2] = le32_to_cpu(((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | DCMD_BMI_IO)
3844 << 24) | count);
3845 cmd_datain[3] = le32_to_cpu(buf);
3846 #if 0
3847 print_insn (host, cmd_datain, "dynamic ", 1);
3848 print_insn (host, cmd_datain + 2, "dynamic ", 1);
3849 #endif
3851 if (dataout) {
3852 /* CALL other_out, WHEN NOT DATA_OUT */
3853 cmd_dataout[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL) << 24) |
3854 DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
3855 cmd_dataout[1] = le32_to_cpu(virt_to_bus(hostdata->script) +
3856 hostdata->E_other_out);
3857 /* MOVE count, buf, WHEN DATA+OUT */
3858 cmd_dataout[2] = le32_to_cpu(((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I) << 24)
3859 | count);
3860 cmd_dataout[3] = le32_to_cpu(buf);
3861 #if 0
3862 print_insn (host, cmd_dataout, "dynamic ", 1);
3863 print_insn (host, cmd_dataout + 2, "dynamic ", 1);
3864 #endif
3869 * Install JUMP instructions after the data transfer routines to return
3870 * control to the do_other_transfer routines.
3874 if (datain) {
3875 cmd_datain[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
3876 DBC_TCI_TRUE);
3877 cmd_datain[1] = le32_to_cpu(virt_to_bus(hostdata->script) +
3878 hostdata->E_other_transfer);
3879 #if 0
3880 print_insn (host, cmd_datain, "dynamic jump ", 1);
3881 #endif
3882 cmd_datain += 2;
3884 #if 0
3885 if (datain) {
3886 cmd_datain[0] = 0x98080000;
3887 cmd_datain[1] = 0x03ffdeed;
3888 cmd_datain += 2;
3890 #endif
3891 if (dataout) {
3892 cmd_dataout[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
3893 DBC_TCI_TRUE);
3894 cmd_dataout[1] = le32_to_cpu(virt_to_bus(hostdata->script) +
3895 hostdata->E_other_transfer);
3896 #if 0
3897 print_insn (host, cmd_dataout, "dynamic jump ", 1);
3898 #endif
3899 cmd_dataout += 2;
3901 return tmp;
3905 * Function : int NCR53c7xx_queue_command (Scsi_Cmnd *cmd,
3906 * void (*done)(Scsi_Cmnd *))
3908 * Purpose : enqueues a SCSI command
3910 * Inputs : cmd - SCSI command, done - function called on completion, with
3911 * a pointer to the command descriptor.
3913 * Returns : 0
3915 * Side effects :
3916 * cmd is added to the per instance driver issue_queue, with major
3917 * twiddling done to the host specific fields of cmd. If the
3918 * process_issue_queue coroutine isn't running, it is restarted.
3920 * NOTE : we use the host_scribble field of the Scsi_Cmnd structure to
3921 * hold our own data, and pervert the ptr field of the SCp field
3922 * to create a linked list.
3926 NCR53c7xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *)) {
3927 struct Scsi_Host *host = cmd->host;
3928 struct NCR53c7x0_hostdata *hostdata =
3929 (struct NCR53c7x0_hostdata *) host->hostdata;
3930 unsigned long flags;
3931 Scsi_Cmnd *tmp;
3933 cmd->scsi_done = done;
3934 cmd->host_scribble = NULL;
3935 cmd->SCp.ptr = NULL;
3936 cmd->SCp.buffer = NULL;
3938 save_flags(flags);
3939 cli();
3940 if ((hostdata->options & (OPTION_DEBUG_INIT_ONLY|OPTION_DEBUG_PROBE_ONLY))
3941 || ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
3942 !(hostdata->debug_lun_limit[cmd->target] & (1 << cmd->lun)))
3943 #ifdef LINUX_1_2
3944 || cmd->target > 7
3945 #else
3946 || cmd->target > host->max_id
3947 #endif
3948 || cmd->target == host->this_id
3949 || hostdata->state == STATE_DISABLED) {
3950 printk("scsi%d : disabled or bad target %d lun %d\n", host->host_no,
3951 cmd->target, cmd->lun);
3952 cmd->result = DID_BAD_TARGET << 16;
3953 } else if ((hostdata->options & OPTION_DEBUG_NCOMMANDS_LIMIT) &&
3954 (hostdata->debug_count_limit == 0)) {
3955 printk("scsi%d : maximum commands exceeded\n", host->host_no);
3956 cmd->result = DID_BAD_TARGET << 16;
3957 } else if (hostdata->options & OPTION_DEBUG_READ_ONLY) {
3958 switch (cmd->cmnd[0]) {
3959 case WRITE_6:
3960 case WRITE_10:
3961 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
3962 host->host_no);
3963 cmd->result = DID_BAD_TARGET << 16;
3965 } else {
3966 if ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
3967 hostdata->debug_count_limit != -1)
3968 --hostdata->debug_count_limit;
3969 restore_flags (flags);
3970 cmd->result = le32_to_cpu(0xffff); /* The NCR will overwrite message
3971 and status with valid data */
3972 cmd->host_scribble = (unsigned char *) tmp = create_cmd (cmd);
3974 cli();
3976 * REQUEST SENSE commands are inserted at the head of the queue
3977 * so that we do not clear the contingent allegiance condition
3978 * they may be looking at.
3981 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
3982 cmd->SCp.ptr = (unsigned char *) hostdata->issue_queue;
3983 hostdata->issue_queue = cmd;
3984 } else {
3985 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->SCp.ptr;
3986 tmp = (Scsi_Cmnd *) tmp->SCp.ptr);
3987 tmp->SCp.ptr = (unsigned char *) cmd;
3989 restore_flags (flags);
3990 run_process_issue_queue();
3991 return 0;
3995 * Function : void to_schedule_list (struct Scsi_Host *host,
3996 * struct NCR53c7x0_hostdata * hostdata, Scsi_Cmnd *cmd)
3998 * Purpose : takes a SCSI command which was just removed from the
3999 * issue queue, and deals with it by inserting it in the first
4000 * free slot in the schedule list or by terminating it immediately.
4002 * Inputs :
4003 * host - SCSI host adapter; hostdata - hostdata structure for
4004 * this adapter; cmd - a pointer to the command; should have
4005 * the host_scribble field initialized to point to a valid
4007 * Side effects :
4008 * cmd is added to the per instance schedule list, with minor
4009 * twiddling done to the host specific fields of cmd.
4013 static __inline__ void
4014 to_schedule_list (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
4015 struct NCR53c7x0_cmd *cmd) {
4016 NCR53c7x0_local_declare();
4017 Scsi_Cmnd *tmp = cmd->cmd;
4018 unsigned long flags;
4019 /* dsa start is negative, so subtraction is used */
4020 volatile u32 *curr;
4022 int i;
4023 NCR53c7x0_local_setup(host);
4024 #if 0
4025 printk("scsi%d : new dsa is 0x%lx (virt 0x%p)\n", host->host_no,
4026 virt_to_bus(dsa), dsa);
4027 #endif
4029 save_flags(flags);
4030 cli();
4033 * Work around race condition : if an interrupt fired and we
4034 * got disabled forget about this command.
4037 if (hostdata->state == STATE_DISABLED) {
4038 printk("scsi%d : driver disabled\n", host->host_no);
4039 tmp->result = DID_BAD_TARGET << 16;
4040 cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
4041 hostdata->free = cmd;
4042 tmp->scsi_done(tmp);
4043 restore_flags (flags);
4044 return;
4047 for (i = host->can_queue, curr = hostdata->schedule;
4048 i > 0 && curr[0] != hostdata->NOP_insn;
4049 --i, curr += 2 /* JUMP instructions are two words */);
4051 if (i > 0) {
4052 ++hostdata->busy[tmp->target][tmp->lun];
4053 cmd->next = hostdata->running_list;
4054 hostdata->running_list = cmd;
4056 /* Restore this instruction to a NOP once the command starts */
4057 cmd->dsa [(hostdata->dsa_jump_dest - hostdata->dsa_start) /
4058 sizeof(u32)] = (u32) le32_to_cpu(virt_to_bus ((void *)curr));
4059 /* Replace the current jump operand. */
4060 curr[1] =
4061 le32_to_cpu(virt_to_bus ((void *) cmd->dsa) + hostdata->E_dsa_code_begin -
4062 hostdata->E_dsa_code_template);
4063 /* Replace the NOP instruction with a JUMP */
4064 curr[0] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) |
4065 DBC_TCI_TRUE);
4066 } else {
4067 printk ("scsi%d: no free slot\n", host->host_no);
4068 disable(host);
4069 tmp->result = DID_ERROR << 16;
4070 cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
4071 hostdata->free = cmd;
4072 tmp->scsi_done(tmp);
4073 restore_flags (flags);
4074 return;
4078 * If the NCR chip is in an idle state, start it running the scheduler
4079 * immediately. Otherwise, signal the chip to jump to schedule as
4080 * soon as it is idle.
4082 if (hostdata->idle) {
4083 hostdata->idle = 0;
4084 hostdata->state = STATE_RUNNING;
4085 NCR53c7x0_write32 (DSP_REG, virt_to_bus ((void *)hostdata->schedule));
4086 } else {
4087 NCR53c7x0_write8(hostdata->istat, ISTAT_10_SIGP);
4090 restore_flags(flags);
4094 * Function : busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata
4095 * *hostdata, Scsi_Cmnd *cmd)
4097 * Purpose : decide if we can pass the given SCSI command on to the
4098 * device in question or not.
4100 * Returns : non-zero when we're busy, 0 when we aren't.
4103 static __inline__ int
4104 busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
4105 Scsi_Cmnd *cmd) {
4106 /* FIXME : in the future, this needs to accommodate SCSI-II tagged
4107 queuing, and we may be able to play with fairness here a bit.
4109 return hostdata->busy[cmd->target][cmd->lun];
4113 * Function : process_issue_queue (void)
4115 * Purpose : transfer commands from the issue queue to NCR start queue
4116 * of each NCR53c7/8xx in the system, avoiding kernel stack
4117 * overflows when the scsi_done() function is invoked recursively.
4119 * NOTE : process_issue_queue exits with interrupts *disabled*, so the
4120 * caller must reenable them if it desires.
4122 * NOTE : process_issue_queue should be called from both
4123 * NCR53c7x0_queue_command() and from the interrupt handler
4124 * after command completion in case NCR53c7x0_queue_command()
4125 * isn't invoked again but we've freed up resources that are
4126 * needed.
4129 static void
4130 process_issue_queue (unsigned long flags) {
4131 Scsi_Cmnd *tmp, *prev;
4132 struct Scsi_Host *host;
4133 struct NCR53c7x0_hostdata *hostdata;
4134 int done;
4137 * We run (with interrupts disabled) until we're sure that none of
4138 * the host adapters have anything that can be done, at which point
4139 * we set process_issue_queue_running to 0 and exit.
4141 * Interrupts are enabled before doing various other internal
4142 * instructions, after we've decided that we need to run through
4143 * the loop again.
4147 do {
4148 cli(); /* Freeze request queues */
4149 done = 1;
4150 for (host = first_host; host && host->hostt == the_template;
4151 host = host->next) {
4152 hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
4153 cli();
4154 if (hostdata->issue_queue) {
4155 if (hostdata->state == STATE_DISABLED) {
4156 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
4157 hostdata->issue_queue = (Scsi_Cmnd *) tmp->SCp.ptr;
4158 tmp->result = DID_BAD_TARGET << 16;
4159 if (tmp->host_scribble) {
4160 ((struct NCR53c7x0_cmd *)tmp->host_scribble)->next =
4161 hostdata->free;
4162 hostdata->free =
4163 (struct NCR53c7x0_cmd *)tmp->host_scribble;
4164 tmp->host_scribble = NULL;
4166 tmp->scsi_done (tmp);
4167 done = 0;
4168 } else
4169 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
4170 prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
4171 tmp->SCp.ptr)
4172 if (!tmp->host_scribble ||
4173 !busyp (host, hostdata, tmp)) {
4174 if (prev)
4175 prev->SCp.ptr = tmp->SCp.ptr;
4176 else
4177 hostdata->issue_queue = (Scsi_Cmnd *)
4178 tmp->SCp.ptr;
4179 tmp->SCp.ptr = NULL;
4180 if (tmp->host_scribble) {
4181 if (hostdata->options & OPTION_DEBUG_QUEUES)
4182 printk ("scsi%d : moving command for target %d lun %d to start list\n",
4183 host->host_no, tmp->target, tmp->lun);
4186 to_schedule_list (host, hostdata,
4187 (struct NCR53c7x0_cmd *)
4188 tmp->host_scribble);
4189 } else {
4190 tmp->result = le32_to_cpu(tmp->result);
4191 if (((tmp->result & 0xff) == 0xff) ||
4192 ((tmp->result & 0xff00) == 0xff00)) {
4193 printk ("scsi%d : danger Will Robinson!\n",
4194 host->host_no);
4195 tmp->result = DID_ERROR << 16;
4196 disable (host);
4198 tmp->scsi_done(tmp);
4200 done = 0;
4201 } /* if target/lun is not busy */
4202 } /* if hostdata->issue_queue */
4203 if (!done)
4204 restore_flags (flags);
4205 } /* for host */
4206 } while (!done);
4207 process_issue_queue_running = 0;
4211 * Function : static void intr_scsi (struct Scsi_Host *host,
4212 * struct NCR53c7x0_cmd *cmd)
4214 * Purpose : handle all SCSI interrupts, indicated by the setting
4215 * of the SIP bit in the ISTAT register.
4217 * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
4218 * may be NULL.
4221 static void
4222 intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
4223 NCR53c7x0_local_declare();
4224 struct NCR53c7x0_hostdata *hostdata =
4225 (struct NCR53c7x0_hostdata *) host->hostdata;
4226 unsigned char sstat0_sist0, sist1, /* Registers */
4227 fatal; /* Did a fatal interrupt
4228 occur ? */
4230 int is_8xx_chip;
4231 NCR53c7x0_local_setup(host);
4233 fatal = 0;
4235 is_8xx_chip = ((unsigned) (hostdata->chip - 800)) < 100;
4236 if (is_8xx_chip) {
4237 sstat0_sist0 = NCR53c7x0_read8(SIST0_REG_800);
4238 udelay(1);
4239 sist1 = NCR53c7x0_read8(SIST1_REG_800);
4240 } else {
4241 sstat0_sist0 = NCR53c7x0_read8(SSTAT0_REG);
4242 sist1 = 0;
4245 if (hostdata->options & OPTION_DEBUG_INTR)
4246 printk ("scsi%d : SIST0 0x%0x, SIST1 0x%0x\n", host->host_no,
4247 sstat0_sist0, sist1);
4249 /* 250ms selection timeout */
4250 if ((is_8xx_chip && (sist1 & SIST1_800_STO)) ||
4251 (!is_8xx_chip && (sstat0_sist0 & SSTAT0_700_STO))) {
4252 fatal = 1;
4253 if (hostdata->options & OPTION_DEBUG_INTR) {
4254 printk ("scsi%d : Selection Timeout\n", host->host_no);
4255 if (cmd) {
4256 printk("scsi%d : target %d, lun %d, command ",
4257 host->host_no, cmd->cmd->target, cmd->cmd->lun);
4258 print_command (cmd->cmd->cmnd);
4259 printk("scsi%d : dsp = 0x%x (virt 0x%p)\n", host->host_no,
4260 NCR53c7x0_read32(DSP_REG),
4261 bus_to_virt(NCR53c7x0_read32(DSP_REG)));
4262 } else {
4263 printk("scsi%d : no command\n", host->host_no);
4267 * XXX - question : how do we want to handle the Illegal Instruction
4268 * interrupt, which may occur before or after the Selection Timeout
4269 * interrupt?
4272 if (1) {
4273 hostdata->idle = 1;
4274 hostdata->expecting_sto = 0;
4276 if (hostdata->test_running) {
4277 hostdata->test_running = 0;
4278 hostdata->test_completed = 3;
4279 } else if (cmd) {
4280 abnormal_finished(cmd, DID_BAD_TARGET << 16);
4282 #if 0
4283 hostdata->intrs = 0;
4284 #endif
4289 * FIXME : in theory, we can also get a UDC when a STO occurs.
4291 if (sstat0_sist0 & SSTAT0_UDC) {
4292 fatal = 1;
4293 if (cmd) {
4294 printk("scsi%d : target %d lun %d unexpected disconnect\n",
4295 host->host_no, cmd->cmd->target, cmd->cmd->lun);
4296 print_lots (host);
4297 abnormal_finished(cmd, DID_ERROR << 16);
4298 } else
4299 printk("scsi%d : unexpected disconnect (no command)\n",
4300 host->host_no);
4302 hostdata->dsp = (u32 *) hostdata->schedule;
4303 hostdata->dsp_changed = 1;
4306 /* SCSI PARITY error */
4307 if (sstat0_sist0 & SSTAT0_PAR) {
4308 fatal = 1;
4309 if (cmd && cmd->cmd) {
4310 printk("scsi%d : target %d lun %d parity error.\n",
4311 host->host_no, cmd->cmd->target, cmd->cmd->lun);
4312 abnormal_finished (cmd, DID_PARITY << 16);
4313 } else
4314 printk("scsi%d : parity error\n", host->host_no);
4315 /* Should send message out, parity error */
4317 /* XXX - Reduce synchronous transfer rate! */
4318 hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4319 sizeof(u32);
4320 hostdata->dsp_changed = 1;
4321 /* SCSI GROSS error */
4324 if (sstat0_sist0 & SSTAT0_SGE) {
4325 fatal = 1;
4326 printk("scsi%d : gross error\n", host->host_no);
4327 /* Reset SCSI offset */
4328 if ((hostdata->chip / 100) == 8) {
4329 NCR53c7x0_write8 (STEST2_REG_800, STEST2_800_ROF);
4333 * A SCSI gross error may occur when we have
4335 * - A synchronous offset which causes the SCSI FIFO to be overwritten.
4337 * - A REQ which causes the maximum synchronous offset programmed in
4338 * the SXFER register to be exceeded.
4340 * - A phase change with an outstanding synchronous offset.
4342 * - Residual data in the synchronous data FIFO, with a transfer
4343 * other than a synchronous receive is started.$#
4347 /* XXX Should deduce synchronous transfer rate! */
4348 hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4349 sizeof(u32);
4350 hostdata->dsp_changed = 1;
4351 /* Phase mismatch */
4354 if (sstat0_sist0 & SSTAT0_MA) {
4355 fatal = 1;
4356 if (hostdata->options & OPTION_DEBUG_INTR)
4357 printk ("scsi%d : SSTAT0_MA\n", host->host_no);
4358 intr_phase_mismatch (host, cmd);
4361 #if 0
4362 if (sstat0_sist0 & SIST0_800_RSL)
4363 printk ("scsi%d : Oh no Mr. Bill!\n", host->host_no);
4364 #endif
4367 * If a fatal SCSI interrupt occurs, we must insure that the DMA and
4368 * SCSI FIFOs were flushed.
4371 if (fatal) {
4372 if (!hostdata->dstat_valid) {
4373 hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
4374 hostdata->dstat_valid = 1;
4377 /* XXX - code check for 700/800 chips */
4378 if (!(hostdata->dstat & DSTAT_DFE)) {
4379 printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
4380 if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4381 printk ("scsi%d: Flushing DMA FIFO\n",
4382 host->host_no);
4383 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
4384 while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
4385 DSTAT_DFE));
4386 } else {
4387 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
4388 while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
4390 hostdata->dstat |= DSTAT_DFE;
4396 * Function : do_NCR53c7x0_intr()
4398 * Purpose : A quick wrapper function added to grab the io_request_lock
4399 * spin lock prior to entering the real interrupt handler. Needed
4400 * for 2.1.95 and above.
4402 static void
4403 do_NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs) {
4404 unsigned long flags;
4406 spin_lock_irqsave(&io_request_lock, flags);
4407 NCR53c7x0_intr(irq, dev_id, regs);
4408 spin_unlock_irqrestore(&io_request_lock, flags);
4412 * Function : static void NCR53c7x0_intr (int irq, void *dev_id, struct pt_regs * regs)
4414 * Purpose : handle NCR53c7x0 interrupts for all NCR devices sharing
4415 * the same IRQ line.
4417 * Inputs : Since we're using the SA_INTERRUPT interrupt handler
4418 * semantics, irq indicates the interrupt which invoked
4419 * this handler.
4422 static void
4423 NCR53c7x0_intr (int irq, void *dev_id, struct pt_regs * regs) {
4424 NCR53c7x0_local_declare();
4425 struct Scsi_Host *host; /* Host we are looking at */
4426 unsigned char istat; /* Values of interrupt regs */
4427 struct NCR53c7x0_hostdata *hostdata; /* host->hostdata */
4428 struct NCR53c7x0_cmd *cmd, /* command which halted */
4429 **cmd_prev_ptr;
4430 u32 *dsa; /* DSA */
4431 int done = 1; /* Indicates when handler
4432 should terminate */
4433 int interrupted = 0; /* This HA generated
4434 an interrupt */
4435 int have_intfly; /* Don't print warning
4436 messages when we stack
4437 INTFLYs */
4438 unsigned long flags;
4440 #ifdef NCR_DEBUG
4441 char buf[80]; /* Debugging sprintf buffer */
4442 size_t buflen; /* Length of same */
4443 #endif
4444 do {
4445 done = 1;
4446 for (host = first_host; host; host = host->next)
4447 if (host->hostt == the_template && host->irq == irq) {
4448 NCR53c7x0_local_setup(host);
4450 hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
4451 hostdata->dsp_changed = 0;
4452 interrupted = 0;
4453 have_intfly = 0;
4455 do {
4456 int is_8xx_chip;
4458 hostdata->dstat_valid = 0;
4459 interrupted = 0;
4461 * Only read istat once, since reading it again will unstack
4462 * interrupts?
4464 istat = NCR53c7x0_read8(hostdata->istat);
4467 * INTFLY interrupts are used by the NCR53c720, NCR53c810,
4468 * and NCR53c820 to signify completion of a command. Since
4469 * the SCSI processor continues running, we can't just look
4470 * at the contents of the DSA register and continue running.
4472 /* XXX - this is too big, offends my sense of aesthetics, and should
4473 move to intr_intfly() */
4474 is_8xx_chip = ((unsigned) (hostdata->chip - 800)) < 100;
4475 if ((hostdata->options & OPTION_INTFLY) &&
4476 (is_8xx_chip && (istat & ISTAT_800_INTF))) {
4477 char search_found = 0; /* Got at least one ? */
4478 done = 0;
4479 interrupted = 1;
4482 * Clear the INTF bit by writing a one.
4483 * This reset operation is self-clearing.
4485 NCR53c7x0_write8(hostdata->istat, istat|ISTAT_800_INTF);
4487 if (hostdata->options & OPTION_DEBUG_INTR)
4488 printk ("scsi%d : INTFLY\n", host->host_no);
4491 * Traverse our list of running commands, and look
4492 * for those with valid (non-0xff ff) status and message
4493 * bytes encoded in the result which signify command
4494 * completion.
4498 save_flags(flags);
4499 cli();
4500 restart:
4501 for (cmd_prev_ptr = (struct NCR53c7x0_cmd **)
4502 &(hostdata->running_list), cmd =
4503 (struct NCR53c7x0_cmd *) hostdata->running_list; cmd ;
4504 cmd_prev_ptr = (struct NCR53c7x0_cmd **) &(cmd->next),
4505 cmd = (struct NCR53c7x0_cmd *) cmd->next) {
4506 Scsi_Cmnd *tmp;
4508 if (!cmd) {
4509 printk("scsi%d : very weird.\n", host->host_no);
4510 break;
4513 if (!(tmp = cmd->cmd)) {
4514 printk("scsi%d : weird. NCR53c7x0_cmd has no Scsi_Cmnd\n",
4515 host->host_no);
4516 continue;
4518 #if 0
4519 printk ("scsi%d : looking at result of 0x%x\n",
4520 host->host_no, cmd->cmd->result);
4521 #endif
4523 #ifdef __powerpc__
4524 if (tmp->result == le32_to_cpu(0xffff))
4525 continue;
4526 tmp->result = le32_to_cpu(tmp->result);
4527 #else
4528 if (((tmp->result & 0xff) == 0xff) ||
4529 ((tmp->result & 0xff00) == 0xff00))
4530 continue;
4531 #endif
4533 search_found = 1;
4535 /* Important - remove from list _before_ done is called */
4536 if (cmd_prev_ptr)
4537 *cmd_prev_ptr = (struct NCR53c7x0_cmd *) cmd->next;
4539 --hostdata->busy[tmp->target][tmp->lun];
4540 cmd->next = hostdata->free;
4541 hostdata->free = cmd;
4543 tmp->host_scribble = NULL;
4545 if (hostdata->options & OPTION_DEBUG_INTR) {
4546 printk ("scsi%d : command complete : pid %lu, id %d,lun %d result 0x%x ",
4547 host->host_no, tmp->pid, tmp->target, tmp->lun, tmp->result);
4548 print_command (tmp->cmnd);
4551 #if 0
4552 hostdata->options &= ~OPTION_DEBUG_INTR;
4553 #endif
4554 tmp->scsi_done(tmp);
4555 goto restart;
4558 restore_flags(flags);
4561 * I think that we're stacking INTFLY interrupts; taking care of
4562 * all the finished commands on the first one, and then getting
4563 * worried when we see the next one. The magic with have_intfly
4564 * should tell if this is the case..
4567 if (!search_found && !have_intfly) {
4568 printk ("scsi%d : WARNING : INTFLY with no completed commands.\n",
4569 host->host_no);
4570 } else if (!have_intfly) {
4571 have_intfly = 1;
4572 run_process_issue_queue();
4576 if (istat & (ISTAT_SIP|ISTAT_DIP)) {
4577 done = 0;
4578 interrupted = 1;
4579 hostdata->state = STATE_HALTED;
4581 if (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
4582 SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK)
4583 printk ("scsi%d : SCSI FIFO not empty\n",
4584 host->host_no);
4587 * NCR53c700 and NCR53c700-66 change the current SCSI
4588 * process, hostdata->curr, in the Linux driver so
4589 * cmd = hostdata->curr.
4591 * With other chips, we must look through the commands
4592 * executing and find the command structure which
4593 * corresponds to the DSA register.
4596 if (hostdata->options & OPTION_700) {
4597 cmd = (struct NCR53c7x0_cmd *) hostdata->curr;
4598 } else {
4599 dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
4600 for (cmd = (struct NCR53c7x0_cmd *)
4601 hostdata->running_list; cmd &&
4602 (dsa + (hostdata->dsa_start / sizeof(u32))) !=
4603 cmd->dsa;
4604 cmd = (struct NCR53c7x0_cmd *)(cmd->next));
4606 if (hostdata->options & OPTION_DEBUG_INTR) {
4607 if (cmd) {
4608 printk("scsi%d : interrupt for pid %lu, id %d, lun %d ",
4609 host->host_no, cmd->cmd->pid, (int) cmd->cmd->target,
4610 (int) cmd->cmd->lun);
4611 print_command (cmd->cmd->cmnd);
4612 } else {
4613 printk("scsi%d : no active command\n", host->host_no);
4616 if (istat & ISTAT_SIP) {
4617 if (hostdata->options & OPTION_DEBUG_INTR)
4618 printk ("scsi%d : ISTAT_SIP\n", host->host_no);
4619 intr_scsi (host, cmd);
4622 if (istat & ISTAT_DIP) {
4623 if (hostdata->options & OPTION_DEBUG_INTR)
4624 printk ("scsi%d : ISTAT_DIP\n", host->host_no);
4625 intr_dma (host, cmd);
4628 if (!hostdata->dstat_valid) {
4629 hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
4630 hostdata->dstat_valid = 1;
4633 /* XXX - code check for 700/800 chips */
4634 if (!(hostdata->dstat & DSTAT_DFE)) {
4635 printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
4636 if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4637 printk ("scsi%d: Flushing DMA FIFO\n",
4638 host->host_no);
4639 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
4640 while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
4641 DSTAT_DFE));
4642 } else
4644 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
4645 while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
4647 hostdata->dstat |= DSTAT_DFE;
4650 } while (interrupted);
4654 if (hostdata->intrs != -1)
4655 hostdata->intrs++;
4656 #if 0
4657 if (hostdata->intrs > 40) {
4658 printk("scsi%d : too many interrupts, halting", host->host_no);
4659 disable(host);
4661 #endif
4663 if (!hostdata->idle && hostdata->state == STATE_HALTED) {
4664 if (!hostdata->dsp_changed) {
4665 hostdata->dsp = (u32 *)
4666 bus_to_virt(NCR53c7x0_read32(DSP_REG));
4669 #if 0
4670 printk("scsi%d : new dsp is 0x%lx (virt 0x%p)\n",
4671 host->host_no, virt_to_bus(hostdata->dsp), hostdata->dsp);
4672 #endif
4674 hostdata->state = STATE_RUNNING;
4675 NCR53c7x0_write32 (DSP_REG, virt_to_bus(hostdata->dsp));
4678 } while (!done);
4683 * Function : static int abort_connected (struct Scsi_Host *host)
4685 * Purpose : Assuming that the NCR SCSI processor is currently
4686 * halted, break the currently established nexus. Clean
4687 * up of the NCR53c7x0_cmd and Scsi_Cmnd structures should
4688 * be done on receipt of the abort interrupt.
4690 * Inputs : host - SCSI host
4694 static int
4695 abort_connected (struct Scsi_Host *host) {
4696 #ifdef NEW_ABORT
4697 NCR53c7x0_local_declare();
4698 #endif
4699 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4700 host->hostdata;
4701 /* FIXME : this probably should change for production kernels; at the
4702 least, counter should move to a per-host structure. */
4703 static int counter = 5;
4704 #ifdef NEW_ABORT
4705 int sstat, phase, offset;
4706 u32 *script;
4707 NCR53c7x0_local_setup(host);
4708 #endif
4710 if (--counter <= 0) {
4711 disable(host);
4712 return 0;
4715 printk ("scsi%d : DANGER : abort_connected() called \n",
4716 host->host_no);
4718 #ifdef NEW_ABORT
4721 * New strategy : Rather than using a generic abort routine,
4722 * we'll specifically try to source or sink the appropriate
4723 * amount of data for the phase we're currently in (taking into
4724 * account the current synchronous offset)
4727 sstat = (NCR53c8x0_read8 ((chip / 100) == 8 ? SSTAT1_REG : SSTAT2_REG);
4728 offset = OFFSET (sstat & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
4729 phase = sstat & SSTAT2_PHASE_MASK;
4732 * SET ATN
4733 * MOVE source_or_sink, WHEN CURRENT PHASE
4734 * < repeat for each outstanding byte >
4735 * JUMP send_abort_message
4738 script = hostdata->abort_script = kmalloc (
4739 8 /* instruction size */ * (
4740 1 /* set ATN */ +
4741 (!offset ? 1 : offset) /* One transfer per outstanding byte */ +
4742 1 /* send abort message */),
4743 GFP_ATOMIC);
4746 #else /* def NEW_ABORT */
4747 hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4748 sizeof(u32);
4749 #endif /* def NEW_ABORT */
4750 hostdata->dsp_changed = 1;
4752 /* XXX - need to flag the command as aborted after the abort_connected
4753 code runs
4755 return 0;
4759 * Function : static int datapath_residual (Scsi_Host *host)
4761 * Purpose : return residual data count of what's in the chip.
4763 * Inputs : host - SCSI host
4766 static int
4767 datapath_residual (struct Scsi_Host *host) {
4768 NCR53c7x0_local_declare();
4769 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4770 host->hostdata;
4771 int count, synchronous, sstat;
4772 NCR53c7x0_local_setup(host);
4773 /* COMPAT : the 700 and 700-66 need to use DFIFO_00_BO_MASK */
4774 count = ((NCR53c7x0_read8 (DFIFO_REG) & DFIFO_10_BO_MASK) -
4775 (NCR53c7x0_read32 (DBC_REG) & DFIFO_10_BO_MASK)) & DFIFO_10_BO_MASK;
4776 synchronous = NCR53c7x0_read8 (SXFER_REG) & SXFER_MO_MASK;
4777 /* COMPAT : DDIR is elsewhere on non-'8xx chips. */
4778 if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4779 /* Receive */
4780 if (synchronous)
4781 count += (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
4782 SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
4783 else
4784 if (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
4785 SSTAT0_REG : SSTAT1_REG) & SSTAT1_ILF)
4786 ++count;
4787 } else {
4788 /* Send */
4789 sstat = ((hostdata->chip / 100) == 8) ? NCR53c7x0_read8 (SSTAT0_REG) :
4790 NCR53c7x0_read8 (SSTAT1_REG);
4791 if (sstat & SSTAT1_OLF)
4792 ++count;
4793 if (synchronous && (sstat & SSTAT1_ORF))
4794 ++count;
4796 return count;
4800 * Function : static const char * sbcl_to_phase (int sbcl)_
4802 * Purpose : Convert SBCL register to user-parsable phase representation
4804 * Inputs : sbcl - value of sbcl register
4808 static const char *
4809 sbcl_to_phase (int sbcl) {
4810 switch (sbcl & SBCL_PHASE_MASK) {
4811 case SBCL_PHASE_DATAIN:
4812 return "DATAIN";
4813 case SBCL_PHASE_DATAOUT:
4814 return "DATAOUT";
4815 case SBCL_PHASE_MSGIN:
4816 return "MSGIN";
4817 case SBCL_PHASE_MSGOUT:
4818 return "MSGOUT";
4819 case SBCL_PHASE_CMDOUT:
4820 return "CMDOUT";
4821 case SBCL_PHASE_STATIN:
4822 return "STATUSIN";
4823 default:
4824 return "unknown";
4829 * Function : static const char * sstat2_to_phase (int sstat)_
4831 * Purpose : Convert SSTAT2 register to user-parsable phase representation
4833 * Inputs : sstat - value of sstat register
4837 static const char *
4838 sstat2_to_phase (int sstat) {
4839 switch (sstat & SSTAT2_PHASE_MASK) {
4840 case SSTAT2_PHASE_DATAIN:
4841 return "DATAIN";
4842 case SSTAT2_PHASE_DATAOUT:
4843 return "DATAOUT";
4844 case SSTAT2_PHASE_MSGIN:
4845 return "MSGIN";
4846 case SSTAT2_PHASE_MSGOUT:
4847 return "MSGOUT";
4848 case SSTAT2_PHASE_CMDOUT:
4849 return "CMDOUT";
4850 case SSTAT2_PHASE_STATIN:
4851 return "STATUSIN";
4852 default:
4853 return "unknown";
4858 * Function : static void intr_phase_mismatch (struct Scsi_Host *host,
4859 * struct NCR53c7x0_cmd *cmd)
4861 * Purpose : Handle phase mismatch interrupts
4863 * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
4864 * may be NULL.
4866 * Side effects : The abort_connected() routine is called or the NCR chip
4867 * is restarted, jumping to the command_complete entry point, or
4868 * patching the address and transfer count of the current instruction
4869 * and calling the msg_in entry point as appropriate.
4872 static void
4873 intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
4874 NCR53c7x0_local_declare();
4875 u32 dbc_dcmd, *dsp, *dsp_next;
4876 unsigned char dcmd, sbcl;
4877 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4878 host->hostdata;
4879 int residual;
4880 enum {ACTION_ABORT, ACTION_ABORT_PRINT, ACTION_CONTINUE} action =
4881 ACTION_ABORT_PRINT;
4882 const char *where = NULL;
4883 NCR53c7x0_local_setup(host);
4886 * Corrective action is based on where in the SCSI SCRIPT(tm) the error
4887 * occurred, as well as which SCSI phase we are currently in.
4889 dsp_next = bus_to_virt(NCR53c7x0_read32(DSP_REG));
4892 * Fetch the current instruction, and remove the operands for easier
4893 * interpretation.
4895 dbc_dcmd = NCR53c7x0_read32(DBC_REG);
4896 dcmd = (dbc_dcmd & 0xff000000) >> 24;
4898 * Like other processors, the NCR adjusts the instruction pointer before
4899 * instruction decode. Set the DSP address back to what it should
4900 * be for this instruction based on its size (2 or 3 32 bit words).
4902 dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
4906 * Read new SCSI phase from the SBCL lines. Since all of our code uses
4907 * a WHEN conditional instead of an IF conditional, we don't need to
4908 * wait for a new REQ.
4910 sbcl = NCR53c7x0_read8(SBCL_REG) & SBCL_PHASE_MASK;
4912 if (!cmd) {
4913 action = ACTION_ABORT_PRINT;
4914 where = "no current command";
4916 * The way my SCSI SCRIPTS(tm) are architected, recoverable phase
4917 * mismatches should only occur where we're doing a multi-byte
4918 * BMI instruction. Specifically, this means
4920 * - select messages (a SCSI-I target may ignore additional messages
4921 * after the IDENTIFY; any target may reject a SDTR or WDTR)
4923 * - command out (targets may send a message to signal an error
4924 * condition, or go into STATUSIN after they've decided
4925 * they don't like the command.
4927 * - reply_message (targets may reject a multi-byte message in the
4928 * middle)
4930 * - data transfer routines (command completion with buffer space
4931 * left, disconnect message, or error message)
4933 } else if (((dsp >= cmd->data_transfer_start &&
4934 dsp < cmd->data_transfer_end)) || dsp == (cmd->residual + 2)) {
4935 if ((dcmd & (DCMD_TYPE_MASK|DCMD_BMI_OP_MASK|DCMD_BMI_INDIRECT|
4936 DCMD_BMI_MSG|DCMD_BMI_CD)) == (DCMD_TYPE_BMI|
4937 DCMD_BMI_OP_MOVE_I)) {
4938 residual = datapath_residual (host);
4939 if (hostdata->options & OPTION_DEBUG_DISCONNECT)
4940 printk ("scsi%d : handling residual transfer (+ %d bytes from DMA FIFO)\n",
4941 host->host_no, residual);
4944 * The first instruction is a CALL to the alternate handler for
4945 * this data transfer phase, so we can do calls to
4946 * munge_msg_restart as we would if control were passed
4947 * from normal dynamic code.
4949 if (dsp != cmd->residual + 2) {
4950 cmd->residual[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
4951 ((dcmd & DCMD_BMI_IO) ? DCMD_TCI_IO : 0)) << 24) |
4952 DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
4953 cmd->residual[1] = le32_to_cpu(virt_to_bus(hostdata->script)
4954 + ((dcmd & DCMD_BMI_IO)
4955 ? hostdata->E_other_in : hostdata->E_other_out));
4959 * The second instruction is the a data transfer block
4960 * move instruction, reflecting the pointer and count at the
4961 * time of the phase mismatch.
4963 cmd->residual[2] = le32_to_cpu(dbc_dcmd + residual);
4964 cmd->residual[3] = le32_to_cpu(NCR53c7x0_read32(DNAD_REG) - residual);
4967 * The third and final instruction is a jump to the instruction
4968 * which follows the instruction which had to be 'split'
4970 if (dsp != cmd->residual + 2) {
4971 cmd->residual[4] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP)
4972 << 24) | DBC_TCI_TRUE);
4973 cmd->residual[5] = le32_to_cpu(virt_to_bus(dsp_next));
4977 * For the sake of simplicity, transfer control to the
4978 * conditional CALL at the start of the residual buffer.
4980 hostdata->dsp = cmd->residual;
4981 hostdata->dsp_changed = 1;
4982 action = ACTION_CONTINUE;
4983 } else {
4984 where = "non-BMI dynamic DSA code";
4985 action = ACTION_ABORT_PRINT;
4987 } else if (dsp == (hostdata->script + hostdata->E_select_msgout / 4)) {
4988 /* Release ATN */
4989 NCR53c7x0_write8 (SOCL_REG, 0);
4990 switch (sbcl) {
4992 * Some devices (SQ555 come to mind) grab the IDENTIFY message
4993 * sent on selection, and decide to go into COMMAND OUT phase
4994 * rather than accepting the rest of the messages or rejecting
4995 * them. Handle these devices gracefully.
4997 case SBCL_PHASE_CMDOUT:
4998 hostdata->dsp = dsp + 2 /* two _words_ */;
4999 hostdata->dsp_changed = 1;
5000 printk ("scsi%d : target %d ignored SDTR and went into COMMAND OUT\n",
5001 host->host_no, cmd->cmd->target);
5002 cmd->flags &= ~CMD_FLAG_SDTR;
5003 action = ACTION_CONTINUE;
5004 break;
5005 case SBCL_PHASE_MSGIN:
5006 hostdata->dsp = hostdata->script + hostdata->E_msg_in /
5007 sizeof(u32);
5008 hostdata->dsp_changed = 1;
5009 action = ACTION_CONTINUE;
5010 break;
5011 default:
5012 where="select message out";
5013 action = ACTION_ABORT_PRINT;
5016 * Some SCSI devices will interpret a command as they read the bytes
5017 * off the SCSI bus, and may decide that the command is Bogus before
5018 * they've read the entire command off the bus.
5020 } else if (dsp == hostdata->script + hostdata->E_cmdout_cmdout / sizeof
5021 (u32)) {
5022 hostdata->dsp = hostdata->script + hostdata->E_data_transfer /
5023 sizeof (u32);
5024 hostdata->dsp_changed = 1;
5025 action = ACTION_CONTINUE;
5026 /* FIXME : we need to handle message reject, etc. within msg_respond. */
5027 #ifdef notyet
5028 } else if (dsp == hostdata->script + hostdata->E_reply_message) {
5029 switch (sbcl) {
5030 /* Any other phase mismatches abort the currently executing command. */
5031 #endif
5032 } else {
5033 where = "unknown location";
5034 action = ACTION_ABORT_PRINT;
5037 /* Flush DMA FIFO */
5038 if (!hostdata->dstat_valid) {
5039 hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
5040 hostdata->dstat_valid = 1;
5042 if (!(hostdata->dstat & DSTAT_DFE)) {
5043 if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
5044 printk ("scsi%d: Flushing DMA FIFO\n",
5045 host->host_no);
5046 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
5047 /* FIXME : what about stacked DMA interrupts? */
5048 while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
5049 DSTAT_DFE));
5050 } else {
5051 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
5052 while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
5054 hostdata->dstat |= DSTAT_DFE;
5057 switch (action) {
5058 case ACTION_ABORT_PRINT:
5059 printk("scsi%d : %s : unexpected phase %s.\n",
5060 host->host_no, where ? where : "unknown location",
5061 sbcl_to_phase(sbcl));
5062 print_lots (host);
5063 /* Fall through to ACTION_ABORT */
5064 case ACTION_ABORT:
5065 abort_connected (host);
5066 break;
5067 case ACTION_CONTINUE:
5068 break;
5071 #if 0
5072 if (hostdata->dsp_changed) {
5073 printk("scsi%d: new dsp 0x%p\n", host->host_no, hostdata->dsp);
5074 print_insn (host, hostdata->dsp, "", 1);
5076 #endif
5081 * Function : static void intr_bf (struct Scsi_Host *host,
5082 * struct NCR53c7x0_cmd *cmd)
5084 * Purpose : handle BUS FAULT interrupts
5086 * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
5087 * may be NULL.
5090 static void
5091 intr_bf (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
5092 NCR53c7x0_local_declare();
5093 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5094 host->hostdata;
5095 u32 *dsp,
5096 *next_dsp, /* Current dsp */
5097 *dsa,
5098 dbc_dcmd; /* DCMD (high eight bits) + DBC */
5099 unsigned short pci_status;
5100 int tmp;
5101 unsigned long flags;
5102 char *reason = NULL;
5103 /* Default behavior is for a silent error, with a retry until we've
5104 exhausted retries. */
5105 enum {MAYBE, ALWAYS, NEVER} retry = MAYBE;
5106 int report = 0;
5107 NCR53c7x0_local_setup(host);
5109 dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
5110 next_dsp = bus_to_virt (NCR53c7x0_read32(DSP_REG));
5111 dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
5112 /* FIXME - check chip type */
5113 dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
5116 * Bus faults can be caused by either a Bad Address or
5117 * Target Abort. We should check the Received Target Abort
5118 * bit of the PCI status register and Master Abort Bit.
5120 * - Master Abort bit indicates that no device claimed
5121 * the address with DEVSEL within five clocks
5123 * - Target Abort bit indicates that a target claimed it,
5124 * but changed its mind once it saw the byte enables.
5128 if ((hostdata->chip / 100) == 8) {
5129 save_flags (flags);
5130 cli();
5131 tmp = pcibios_read_config_word (hostdata->pci_bus,
5132 hostdata->pci_device_fn, PCI_STATUS, &pci_status);
5133 restore_flags (flags);
5134 if (tmp == PCIBIOS_SUCCESSFUL) {
5135 if (pci_status & PCI_STATUS_REC_TARGET_ABORT) {
5136 reason = "PCI target abort";
5137 pci_status &= ~PCI_STATUS_REC_TARGET_ABORT;
5138 } else if (pci_status & PCI_STATUS_REC_MASTER_ABORT) {
5139 reason = "No device asserted PCI DEVSEL within five bus clocks";
5140 pci_status &= ~PCI_STATUS_REC_MASTER_ABORT;
5141 } else if (pci_status & PCI_STATUS_PARITY) {
5142 report = 1;
5143 pci_status &= ~PCI_STATUS_PARITY;
5145 } else {
5146 printk ("scsi%d : couldn't read status register : error %d\n",
5147 host->host_no, tmp);
5148 retry = NEVER;
5152 #ifndef notyet
5153 report = 1;
5154 #endif
5155 if (report && reason) {
5156 printk(KERN_ALERT "scsi%d : BUS FAULT reason = %s\n",
5157 host->host_no, reason ? reason : "unknown");
5158 print_lots (host);
5161 #ifndef notyet
5162 retry = NEVER;
5163 #endif
5166 * TODO : we should attempt to recover from any spurious bus
5167 * faults. After X retries, we should figure that things are
5168 * sufficiently wedged, and call NCR53c7xx_reset.
5170 * This code should only get executed once we've decided that we
5171 * cannot retry.
5174 if (retry == NEVER) {
5175 printk(KERN_ALERT " mail drew@PoohSticks.ORG\n");
5176 FATAL (host);
5181 * Function : static void intr_dma (struct Scsi_Host *host,
5182 * struct NCR53c7x0_cmd *cmd)
5184 * Purpose : handle all DMA interrupts, indicated by the setting
5185 * of the DIP bit in the ISTAT register.
5187 * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
5188 * may be NULL.
5191 static void
5192 intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
5193 NCR53c7x0_local_declare();
5194 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5195 host->hostdata;
5196 unsigned char dstat; /* DSTAT */
5197 u32 *dsp,
5198 *next_dsp, /* Current dsp */
5199 *dsa,
5200 dbc_dcmd; /* DCMD (high eight bits) + DBC */
5201 int tmp;
5202 unsigned long flags;
5203 NCR53c7x0_local_setup(host);
5205 if (!hostdata->dstat_valid) {
5206 hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
5207 hostdata->dstat_valid = 1;
5210 dstat = hostdata->dstat;
5212 if (hostdata->options & OPTION_DEBUG_INTR)
5213 printk("scsi%d : DSTAT=0x%x\n", host->host_no, (int) dstat);
5215 dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
5216 next_dsp = bus_to_virt(NCR53c7x0_read32(DSP_REG));
5217 dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
5218 /* XXX - check chip type */
5219 dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
5222 * DSTAT_ABRT is the aborted interrupt. This is set whenever the
5223 * SCSI chip is aborted.
5225 * With NCR53c700 and NCR53c700-66 style chips, we should only
5226 * get this when the chip is currently running the accept
5227 * reselect/select code and we have set the abort bit in the
5228 * ISTAT register.
5232 if (dstat & DSTAT_ABRT) {
5233 #if 0
5234 /* XXX - add code here to deal with normal abort */
5235 if ((hostdata->options & OPTION_700) && (hostdata->state ==
5236 STATE_ABORTING)) {
5237 } else
5238 #endif
5240 printk(KERN_ALERT "scsi%d : unexpected abort interrupt at\n"
5241 " ", host->host_no);
5242 print_insn (host, dsp, KERN_ALERT "s ", 1);
5243 FATAL (host);
5248 * DSTAT_SSI is the single step interrupt. Should be generated
5249 * whenever we have single stepped or are tracing.
5252 if (dstat & DSTAT_SSI) {
5253 if (hostdata->options & OPTION_DEBUG_TRACE) {
5254 } else if (hostdata->options & OPTION_DEBUG_SINGLE) {
5255 print_insn (host, dsp, "s ", 0);
5256 save_flags(flags);
5257 cli();
5258 /* XXX - should we do this, or can we get away with writing dsp? */
5260 NCR53c7x0_write8 (DCNTL_REG, (NCR53c7x0_read8(DCNTL_REG) &
5261 ~DCNTL_SSM) | DCNTL_STD);
5262 restore_flags(flags);
5263 } else {
5264 printk(KERN_ALERT "scsi%d : unexpected single step interrupt at\n"
5265 " ", host->host_no);
5266 print_insn (host, dsp, KERN_ALERT "", 1);
5267 printk(KERN_ALERT " mail drew@PoohSticks.ORG\n");
5268 FATAL (host);
5273 * DSTAT_IID / DSTAT_OPC (same bit, same meaning, only the name
5274 * is different) is generated whenever an illegal instruction is
5275 * encountered.
5277 * XXX - we may want to emulate INTFLY here, so we can use
5278 * the same SCSI SCRIPT (tm) for NCR53c710 through NCR53c810
5279 * chips.
5282 if (dstat & DSTAT_OPC) {
5284 * Ascertain if this IID interrupts occurred before or after a STO
5285 * interrupt. Since the interrupt handling code now leaves
5286 * DSP unmodified until _after_ all stacked interrupts have been
5287 * processed, reading the DSP returns the original DSP register.
5288 * This means that if dsp lies between the select code, and
5289 * message out following the selection code (where the IID interrupt
5290 * would have to have occurred by due to the implicit wait for REQ),
5291 * we have an IID interrupt resulting from a STO condition and
5292 * can ignore it.
5295 if (((dsp >= (hostdata->script + hostdata->E_select / sizeof(u32))) &&
5296 (dsp <= (hostdata->script + hostdata->E_select_msgout /
5297 sizeof(u32) + 8))) || (hostdata->test_running == 2)) {
5298 if (hostdata->options & OPTION_DEBUG_INTR)
5299 printk ("scsi%d : ignoring DSTAT_IID for SSTAT_STO\n",
5300 host->host_no);
5301 if (hostdata->expecting_iid) {
5302 hostdata->expecting_iid = 0;
5303 hostdata->idle = 1;
5304 if (hostdata->test_running == 2) {
5305 hostdata->test_running = 0;
5306 hostdata->test_completed = 3;
5307 } else if (cmd)
5308 abnormal_finished (cmd, DID_BAD_TARGET << 16);
5309 } else {
5310 hostdata->expecting_sto = 1;
5313 * We can't guarantee we'll be able to execute the WAIT DISCONNECT
5314 * instruction within the 3.4us of bus free and arbitration delay
5315 * that a target can RESELECT in and assert REQ after we've dropped
5316 * ACK. If this happens, we'll get an illegal instruction interrupt.
5317 * Doing away with the WAIT DISCONNECT instructions broke everything,
5318 * so instead I'll settle for moving one WAIT DISCONNECT a few
5319 * instructions closer to the CLEAR ACK before it to minimize the
5320 * chances of this happening, and handle it if it occurs anyway.
5322 * Simply continue with what we were doing, and control should
5323 * be transfered to the schedule routine which will ultimately
5324 * pass control onto the reselection or selection (not yet)
5325 * code.
5327 } else if (dbc_dcmd == 0x48000000 && (NCR53c7x0_read8 (SBCL_REG) &
5328 SBCL_REQ)) {
5329 if (!(hostdata->options & OPTION_NO_PRINT_RACE))
5331 printk("scsi%d: REQ before WAIT DISCONNECT IID\n",
5332 host->host_no);
5333 hostdata->options |= OPTION_NO_PRINT_RACE;
5335 } else {
5336 printk(KERN_ALERT "scsi%d : illegal instruction\n", host->host_no);
5337 print_lots (host);
5338 printk(KERN_ALERT " mail drew@PoohSticks.ORG with ALL\n"
5339 " boot messages and diagnostic output\n");
5340 FATAL (host);
5345 * DSTAT_BF are bus fault errors
5348 if (dstat & DSTAT_800_BF) {
5349 intr_bf (host, cmd);
5354 * DSTAT_SIR interrupts are generated by the execution of
5355 * the INT instruction. Since the exact values available
5356 * are determined entirely by the SCSI script running,
5357 * and are local to a particular script, a unique handler
5358 * is called for each script.
5361 if (dstat & DSTAT_SIR) {
5362 if (hostdata->options & OPTION_DEBUG_INTR)
5363 printk ("scsi%d : DSTAT_SIR\n", host->host_no);
5364 switch ((tmp = hostdata->dstat_sir_intr (host, cmd))) {
5365 case SPECIFIC_INT_NOTHING:
5366 case SPECIFIC_INT_RESTART:
5367 break;
5368 case SPECIFIC_INT_ABORT:
5369 abort_connected(host);
5370 break;
5371 case SPECIFIC_INT_PANIC:
5372 printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
5373 print_insn (host, dsp, KERN_ALERT "", 1);
5374 printk(KERN_ALERT " dstat_sir_intr() returned SPECIFIC_INT_PANIC\n");
5375 FATAL (host);
5376 break;
5377 case SPECIFIC_INT_BREAK:
5378 intr_break (host, cmd);
5379 break;
5380 default:
5381 printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
5382 print_insn (host, dsp, KERN_ALERT "", 1);
5383 printk(KERN_ALERT" dstat_sir_intr() returned unknown value %d\n",
5384 tmp);
5385 FATAL (host);
5389 if ((hostdata->chip / 100) == 8 && (dstat & DSTAT_800_MDPE)) {
5390 printk(KERN_ALERT "scsi%d : Master Data Parity Error\n",
5391 host->host_no);
5392 FATAL (host);
5397 * Function : static int print_insn (struct Scsi_Host *host,
5398 * u32 *insn, int kernel)
5400 * Purpose : print numeric representation of the instruction pointed
5401 * to by insn to the debugging or kernel message buffer
5402 * as appropriate.
5404 * If desired, a user level program can interpret this
5405 * information.
5407 * Inputs : host, insn - host, pointer to instruction, prefix -
5408 * string to prepend, kernel - use printk instead of debugging buffer.
5410 * Returns : size, in u32s, of instruction printed.
5414 * FIXME: should change kernel parameter so that it takes an ENUM
5415 * specifying severity - either KERN_ALERT or KERN_PANIC so
5416 * all panic messages are output with the same severity.
5419 static int
5420 print_insn (struct Scsi_Host *host, const u32 *insn,
5421 const char *prefix, int kernel) {
5422 char buf[160], /* Temporary buffer and pointer. ICKY
5423 arbitrary length. */
5426 *tmp;
5427 unsigned char dcmd; /* dcmd register for *insn */
5428 int size;
5431 * Check to see if the instruction pointer is not bogus before
5432 * indirecting through it; avoiding red-zone at start of
5433 * memory.
5435 * FIXME: icky magic needs to happen here on non-intel boxes which
5436 * don't have kernel memory mapped in like this. Might be reasonable
5437 * to use vverify()?
5440 if (MAP_NR(insn) < 1 || MAP_NR(insn + 8) > MAP_NR(high_memory) ||
5441 ((((dcmd = (insn[0] >> 24) & 0xff) & DCMD_TYPE_MMI) == DCMD_TYPE_MMI) &&
5442 MAP_NR(insn + 12) > MAP_NR(high_memory))) {
5443 size = 0;
5444 sprintf (buf, "%s%p: address out of range\n",
5445 prefix, insn);
5446 } else {
5448 * FIXME : (void *) cast in virt_to_bus should be unnecessary, because
5449 * it should take const void * as argument.
5451 sprintf(buf, "%s0x%lx (virt 0x%p) : 0x%08x 0x%08x (virt 0x%p)",
5452 (prefix ? prefix : ""), virt_to_bus((void *) insn), insn,
5453 insn[0], insn[1], bus_to_virt (le32_to_cpu(insn[1])));
5454 tmp = buf + strlen(buf);
5455 if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI) {
5456 sprintf (tmp, " 0x%08x (virt 0x%p)\n", insn[2],
5457 bus_to_virt(le32_to_cpu(insn[2])));
5458 size = 3;
5459 } else {
5460 sprintf (tmp, "\n");
5461 size = 2;
5465 if (kernel)
5466 printk ("%s", buf);
5467 #ifdef NCR_DEBUG
5468 else {
5469 size_t len = strlen(buf);
5470 debugger_kernel_write(host, buf, len);
5472 #endif
5473 return size;
5477 * Function : static const char *ncr_state (int state)
5479 * Purpose : convert state (probably from hostdata->state) to a string
5481 * Inputs : state
5483 * Returns : char * representation of state, "unknown" on error.
5486 #if 0
5487 static const char *
5488 ncr_state (int state) {
5489 switch (state) {
5490 case STATE_HALTED: return "halted";
5491 case STATE_WAITING: return "waiting";
5492 case STATE_RUNNING: return "running";
5493 case STATE_ABORTING: return "aborting";
5494 case STATE_DISABLED: return "disabled";
5495 default: return "unknown";
5498 #endif
5501 * Function : int NCR53c7xx_abort (Scsi_Cmnd *cmd)
5503 * Purpose : Abort an errant SCSI command, doing all necessary
5504 * cleanup of the issue_queue, running_list, shared Linux/NCR
5505 * dsa issue and reconnect queues.
5507 * Inputs : cmd - command to abort, code - entire result field
5509 * Returns : 0 on success, -1 on failure.
5512 int
5513 NCR53c7xx_abort (Scsi_Cmnd *cmd) {
5514 NCR53c7x0_local_declare();
5515 struct Scsi_Host *host = cmd->host;
5516 struct NCR53c7x0_hostdata *hostdata = host ? (struct NCR53c7x0_hostdata *)
5517 host->hostdata : NULL;
5518 unsigned long flags;
5519 unsigned long result;
5520 struct NCR53c7x0_cmd *curr, **prev;
5521 Scsi_Cmnd *me, **last;
5522 #if 0
5523 static long cache_pid = -1;
5524 #endif
5527 if (!host) {
5528 printk ("Bogus SCSI command pid %ld; no host structure\n",
5529 cmd->pid);
5530 return SCSI_ABORT_ERROR;
5531 } else if (!hostdata) {
5532 printk ("Bogus SCSI host %d; no hostdata\n", host->host_no);
5533 return SCSI_ABORT_ERROR;
5535 NCR53c7x0_local_setup(host);
5538 * CHECK : I don't think that reading ISTAT will unstack any interrupts,
5539 * since we need to write the INTF bit to clear it, and SCSI/DMA
5540 * interrupts don't clear until we read SSTAT/SIST and DSTAT registers.
5542 * See that this is the case.
5544 * I suspect that several of our failures may be coming from a new fatal
5545 * interrupt (possibly due to a phase mismatch) happening after we've left
5546 * the interrupt handler, but before the PIC has had the interrupt condition
5547 * cleared.
5550 if (NCR53c7x0_read8(hostdata->istat) &
5551 (ISTAT_DIP|ISTAT_SIP|
5552 (hostdata->chip / 100 == 8 ? ISTAT_800_INTF : 0))) {
5553 printk ("scsi%d : dropped interrupt for command %ld\n", host->host_no,
5554 cmd->pid);
5555 NCR53c7x0_intr (host->irq, NULL, NULL);
5556 return SCSI_ABORT_BUSY;
5559 save_flags(flags);
5560 cli();
5561 #if 0
5562 if (cache_pid == cmd->pid)
5563 panic ("scsi%d : bloody fetus %d\n", host->host_no, cmd->pid);
5564 else
5565 cache_pid = cmd->pid;
5566 #endif
5570 * The command could be hiding in the issue_queue. This would be very
5571 * nice, as commands can't be moved from the high level driver's issue queue
5572 * into the shared queue until an interrupt routine is serviced, and this
5573 * moving is atomic.
5575 * If this is the case, we don't have to worry about anything - we simply
5576 * pull the command out of the old queue, and call it aborted.
5579 for (me = (Scsi_Cmnd *) hostdata->issue_queue,
5580 last = (Scsi_Cmnd **) &(hostdata->issue_queue);
5581 me && me != cmd; last = (Scsi_Cmnd **)&(me->SCp.ptr),
5582 me = (Scsi_Cmnd *)me->SCp.ptr);
5584 if (me) {
5585 *last = (Scsi_Cmnd *) me->SCp.ptr;
5586 if (me->host_scribble) {
5587 ((struct NCR53c7x0_cmd *)me->host_scribble)->next = hostdata->free;
5588 hostdata->free = (struct NCR53c7x0_cmd *) me->host_scribble;
5589 me->host_scribble = NULL;
5591 cmd->result = DID_ABORT << 16;
5592 cmd->scsi_done(cmd);
5593 printk ("scsi%d : found command %ld in Linux issue queue\n",
5594 host->host_no, me->pid);
5595 restore_flags(flags);
5596 run_process_issue_queue();
5597 return SCSI_ABORT_SUCCESS;
5601 * That failing, the command could be in our list of already executing
5602 * commands. If this is the case, drastic measures are called for.
5605 for (curr = (struct NCR53c7x0_cmd *) hostdata->running_list,
5606 prev = (struct NCR53c7x0_cmd **) &(hostdata->running_list);
5607 curr && curr->cmd != cmd; prev = (struct NCR53c7x0_cmd **)
5608 &(curr->next), curr = (struct NCR53c7x0_cmd *) curr->next);
5610 if (curr) {
5611 result = le32_to_cpu(cmd->result);
5612 if ((result & 0xff) != 0xff && (result & 0xff00) != 0xff00) {
5613 if (prev)
5614 *prev = (struct NCR53c7x0_cmd *) curr->next;
5615 curr->next = (struct NCR53c7x0_cmd *) hostdata->free;
5616 cmd->host_scribble = NULL;
5617 hostdata->free = curr;
5618 cmd->scsi_done(cmd);
5619 printk ("scsi%d : found finished command %ld in running list\n",
5620 host->host_no, cmd->pid);
5621 restore_flags(flags);
5622 return SCSI_ABORT_NOT_RUNNING;
5623 } else {
5624 printk ("scsi%d : DANGER : command running, can not abort.\n",
5625 cmd->host->host_no);
5626 restore_flags(flags);
5627 return SCSI_ABORT_BUSY;
5632 * And if we couldn't find it in any of our queues, it must have been
5633 * a dropped interrupt.
5636 curr = (struct NCR53c7x0_cmd *) cmd->host_scribble;
5637 if (curr) {
5638 curr->next = hostdata->free;
5639 hostdata->free = curr;
5640 cmd->host_scribble = NULL;
5643 result = le32_to_cpu(cmd->result);
5644 if (((result & 0xff00) == 0xff00) ||
5645 ((result & 0xff) == 0xff)) {
5646 printk ("scsi%d : did this command ever run?\n", host->host_no);
5647 cmd->result = DID_ABORT << 16;
5648 } else {
5649 printk ("scsi%d : probably lost INTFLY, normal completion\n",
5650 host->host_no);
5652 * FIXME : We need to add an additional flag which indicates if a
5653 * command was ever counted as BUSY, so if we end up here we can
5654 * decrement the busy count if and only if it is necessary.
5656 --hostdata->busy[cmd->target][cmd->lun];
5658 restore_flags(flags);
5659 cmd->scsi_done(cmd);
5662 * We need to run process_issue_queue since termination of this command
5663 * may allow another queued command to execute first?
5665 return SCSI_ABORT_NOT_RUNNING;
5669 * Function : int NCR53c7xx_reset (Scsi_Cmnd *cmd)
5671 * Purpose : perform a hard reset of the SCSI bus and NCR
5672 * chip.
5674 * Inputs : cmd - command which caused the SCSI RESET
5676 * Returns : 0 on success.
5679 int
5680 NCR53c7xx_reset (Scsi_Cmnd *cmd, unsigned int reset_flags) {
5681 NCR53c7x0_local_declare();
5682 unsigned long flags;
5683 int found = 0;
5684 struct NCR53c7x0_cmd * c;
5685 Scsi_Cmnd *tmp;
5687 * When we call scsi_done(), it's going to wake up anything sleeping on the
5688 * resources which were in use by the aborted commands, and we'll start to
5689 * get new commands.
5691 * We can't let this happen until after we've re-initialized the driver
5692 * structures, and can't reinitialize those structures until after we've
5693 * dealt with their contents.
5695 * So, we need to find all of the commands which were running, stick
5696 * them on a linked list of completed commands (we'll use the host_scribble
5697 * pointer), do our reinitialization, and then call the done function for
5698 * each command.
5700 Scsi_Cmnd *nuke_list = NULL;
5701 struct Scsi_Host *host = cmd->host;
5702 struct NCR53c7x0_hostdata *hostdata =
5703 (struct NCR53c7x0_hostdata *) host->hostdata;
5705 NCR53c7x0_local_setup(host);
5706 save_flags(flags);
5707 cli();
5708 ncr_halt (host);
5709 print_lots (host);
5710 dump_events (host, 30);
5711 ncr_scsi_reset (host);
5712 for (tmp = nuke_list = return_outstanding_commands (host, 1 /* free */,
5713 0 /* issue */ ); tmp; tmp = (Scsi_Cmnd *) tmp->SCp.buffer)
5714 if (tmp == cmd) {
5715 found = 1;
5716 break;
5720 * If we didn't find the command which caused this reset in our running
5721 * list, then we've lost it. See that it terminates normally anyway.
5723 if (!found) {
5724 c = (struct NCR53c7x0_cmd *) cmd->host_scribble;
5725 if (c) {
5726 cmd->host_scribble = NULL;
5727 c->next = hostdata->free;
5728 hostdata->free = c;
5729 } else
5730 printk ("scsi%d: lost command %ld\n", host->host_no, cmd->pid);
5731 cmd->SCp.buffer = (struct scatterlist *) nuke_list;
5732 nuke_list = cmd;
5735 NCR53c7x0_driver_init (host);
5736 hostdata->soft_reset (host);
5737 if (hostdata->resets == 0)
5738 disable(host);
5739 else if (hostdata->resets != -1)
5740 --hostdata->resets;
5741 restore_flags(flags);
5742 for (; nuke_list; nuke_list = tmp) {
5743 tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
5744 nuke_list->result = DID_RESET << 16;
5745 nuke_list->scsi_done (nuke_list);
5747 restore_flags(flags);
5748 return SCSI_RESET_SUCCESS;
5752 * The NCR SDMS bios follows Annex A of the SCSI-CAM draft, and
5753 * therefore shares the scsicam_bios_param function.
5757 * Function : int insn_to_offset (Scsi_Cmnd *cmd, u32 *insn)
5759 * Purpose : convert instructions stored at NCR pointer into data
5760 * pointer offset.
5762 * Inputs : cmd - SCSI command; insn - pointer to instruction. Either current
5763 * DSP, or saved data pointer.
5765 * Returns : offset on success, -1 on failure.
5769 static int
5770 insn_to_offset (Scsi_Cmnd *cmd, u32 *insn) {
5771 struct NCR53c7x0_hostdata *hostdata =
5772 (struct NCR53c7x0_hostdata *) cmd->host->hostdata;
5773 struct NCR53c7x0_cmd *ncmd =
5774 (struct NCR53c7x0_cmd *) cmd->host_scribble;
5775 int offset = 0, buffers;
5776 struct scatterlist *segment;
5777 char *ptr;
5778 int found = 0;
5781 * With the current code implementation, if the insn is inside dynamically
5782 * generated code, the data pointer will be the instruction preceding
5783 * the next transfer segment.
5786 if (!check_address ((unsigned long) ncmd, sizeof (struct NCR53c7x0_cmd)) &&
5787 ((insn >= ncmd->data_transfer_start &&
5788 insn < ncmd->data_transfer_end) ||
5789 (insn >= ncmd->residual &&
5790 insn < (ncmd->residual +
5791 sizeof(ncmd->residual))))) {
5792 ptr = bus_to_virt(le32_to_cpu(insn[3]));
5794 if ((buffers = cmd->use_sg)) {
5795 for (offset = 0,
5796 segment = (struct scatterlist *) cmd->buffer;
5797 buffers && !((found = ((ptr >= segment->address) &&
5798 (ptr < (segment->address + segment->length)))));
5799 --buffers, offset += segment->length, ++segment)
5800 #if 0
5801 printk("scsi%d: comparing 0x%p to 0x%p\n",
5802 cmd->host->host_no, saved, segment->address);
5803 #else
5805 #endif
5806 offset += ptr - segment->address;
5807 } else {
5808 found = 1;
5809 offset = ptr - (char *) (cmd->request_buffer);
5811 } else if ((insn >= hostdata->script +
5812 hostdata->E_data_transfer / sizeof(u32)) &&
5813 (insn <= hostdata->script +
5814 hostdata->E_end_data_transfer / sizeof(u32))) {
5815 found = 1;
5816 offset = 0;
5818 return found ? offset : -1;
5824 * Function : void print_progress (Scsi_Cmnd *cmd)
5826 * Purpose : print the current location of the saved data pointer
5828 * Inputs : cmd - command we are interested in
5832 static void
5833 print_progress (Scsi_Cmnd *cmd) {
5834 NCR53c7x0_local_declare();
5835 struct NCR53c7x0_cmd *ncmd =
5836 (struct NCR53c7x0_cmd *) cmd->host_scribble;
5837 int offset, i;
5838 char *where;
5839 u32 *ptr;
5840 NCR53c7x0_local_setup (cmd->host);
5841 for (i = 0; i < 2; ++i) {
5842 if (check_address ((unsigned long) ncmd,
5843 sizeof (struct NCR53c7x0_cmd)) == -1)
5844 continue;
5845 if (!i) {
5846 where = "saved";
5847 ptr = bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer));
5848 } else {
5849 where = "active";
5850 ptr = bus_to_virt (NCR53c7x0_read32 (DSP_REG) -
5851 NCR53c7x0_insn_size (NCR53c7x0_read8 (DCMD_REG)) *
5852 sizeof(u32));
5854 offset = insn_to_offset (cmd, ptr);
5856 if (offset != -1)
5857 printk ("scsi%d : %s data pointer at offset %d\n",
5858 cmd->host->host_no, where, offset);
5859 else {
5860 int size;
5861 printk ("scsi%d : can't determine %s data pointer offset\n",
5862 cmd->host->host_no, where);
5863 if (ncmd) {
5864 size = print_insn (cmd->host,
5865 bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer)), "", 1);
5866 print_insn (cmd->host,
5867 bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer)) + size * sizeof(u32),
5868 "", 1);
5875 static void
5876 print_dsa (struct Scsi_Host *host, u32 *dsa, const char *prefix) {
5877 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5878 host->hostdata;
5879 int i, len;
5880 char *ptr;
5881 Scsi_Cmnd *cmd;
5883 if (check_address ((unsigned long) dsa, hostdata->dsa_end -
5884 hostdata->dsa_start) == -1) {
5885 printk("scsi%d : bad dsa virt 0x%p\n", host->host_no, dsa);
5886 return;
5888 printk("%sscsi%d : dsa at phys 0x%lx (virt 0x%p)\n"
5889 " + %d : dsa_msgout length = %u, data = 0x%x (virt 0x%p)\n" ,
5890 prefix ? prefix : "",
5891 host->host_no, virt_to_bus (dsa), dsa, hostdata->dsa_msgout,
5892 le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]),
5893 le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1]),
5894 bus_to_virt (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1])));
5897 * Only print messages if they're sane in length so we don't
5898 * blow the kernel printk buffer on something which won't buy us
5899 * anything.
5902 if (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]) <
5903 sizeof (hostdata->free->select))
5904 for (i = le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]),
5905 ptr = bus_to_virt (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1]));
5906 i > 0 && !check_address ((unsigned long) ptr, 1);
5907 ptr += len, i -= len) {
5908 printk(" ");
5909 len = print_msg (ptr);
5910 printk("\n");
5911 if (!len)
5912 break;
5915 printk(" + %d : select_indirect = 0x%x\n",
5916 hostdata->dsa_select, le32_to_cpu(dsa[hostdata->dsa_select / sizeof(u32)]));
5917 cmd = (Scsi_Cmnd *) bus_to_virt(le32_to_cpu(dsa[hostdata->dsa_cmnd / sizeof(u32)]));
5918 printk(" + %d : dsa_cmnd = 0x%x ", hostdata->dsa_cmnd,
5919 (u32) virt_to_bus(cmd));
5920 if (cmd) {
5921 printk(" result = 0x%x, target = %d, lun = %d, cmd = ",
5922 cmd->result, cmd->target, cmd->lun);
5923 print_command(cmd->cmnd);
5924 } else
5925 printk("\n");
5926 printk(" + %d : dsa_next = 0x%x\n", hostdata->dsa_next,
5927 le32_to_cpu(dsa[hostdata->dsa_next / sizeof(u32)]));
5928 if (cmd) {
5929 printk("scsi%d target %d : sxfer_sanity = 0x%x, scntl3_sanity = 0x%x\n"
5930 " script : ",
5931 host->host_no, cmd->target,
5932 hostdata->sync[cmd->target].sxfer_sanity,
5933 hostdata->sync[cmd->target].scntl3_sanity);
5934 for (i = 0; i < (sizeof(hostdata->sync[cmd->target].script) / 4); ++i)
5935 printk ("0x%x ", hostdata->sync[cmd->target].script[i]);
5936 printk ("\n");
5937 print_progress (cmd);
5941 * Function : void print_queues (Scsi_Host *host)
5943 * Purpose : print the contents of the NCR issue and reconnect queues
5945 * Inputs : host - SCSI host we are interested in
5949 static void
5950 print_queues (struct Scsi_Host *host) {
5951 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5952 host->hostdata;
5953 u32 *dsa, *next_dsa;
5954 volatile u32 *curr;
5955 int left;
5956 Scsi_Cmnd *cmd, *next_cmd;
5957 unsigned long flags;
5959 printk ("scsi%d : issue queue\n", host->host_no);
5961 for (left = host->can_queue, cmd = (Scsi_Cmnd *) hostdata->issue_queue;
5962 left >= 0 && cmd;
5963 cmd = next_cmd) {
5964 next_cmd = (Scsi_Cmnd *) cmd->SCp.ptr;
5965 save_flags(flags);
5966 cli();
5967 if (cmd->host_scribble) {
5968 if (check_address ((unsigned long) (cmd->host_scribble),
5969 sizeof (cmd->host_scribble)) == -1)
5970 printk ("scsi%d: scsi pid %ld bad pointer to NCR53c7x0_cmd\n",
5971 host->host_no, cmd->pid);
5972 /* print_dsa does sanity check on address, no need to check */
5973 else
5974 print_dsa (host, bus_to_virt(le32_to_cpu(((struct NCR53c7x0_cmd *) cmd->host_scribble)-> dsa)), "");
5975 } else
5976 printk ("scsi%d : scsi pid %ld for target %d lun %d has no NCR53c7x0_cmd\n",
5977 host->host_no, cmd->pid, cmd->target, cmd->lun);
5978 restore_flags(flags);
5981 if (left <= 0) {
5982 printk ("scsi%d : loop detected in issue queue\n",
5983 host->host_no);
5987 * Traverse the NCR reconnect and start DSA structures, printing out
5988 * each element until we hit the end or detect a loop. Currently,
5989 * the reconnect structure is a linked list; and the start structure
5990 * is an array. Eventually, the reconnect structure will become a
5991 * list as well, since this simplifies the code.
5994 printk ("scsi%d : schedule dsa array :\n", host->host_no);
5995 for (left = host->can_queue, curr = hostdata->schedule;
5996 left > 0; curr += 2, --left)
5997 if (curr[0] != hostdata->NOP_insn)
5998 /* FIXME : convert pointer to dsa_begin to pointer to dsa. */
5999 print_dsa (host, bus_to_virt (le32_to_cpu(curr[1]) -
6000 (hostdata->E_dsa_code_begin -
6001 hostdata->E_dsa_code_template)), "");
6002 printk ("scsi%d : end schedule dsa array\n", host->host_no);
6004 printk ("scsi%d : reconnect_dsa_head :\n", host->host_no);
6006 for (left = host->can_queue,
6007 dsa = bus_to_virt (le32_to_cpu(hostdata->reconnect_dsa_head));
6008 left >= 0 && dsa;
6009 dsa = next_dsa) {
6010 save_flags (flags);
6011 cli();
6012 if (check_address ((unsigned long) dsa, sizeof(dsa)) == -1) {
6013 printk ("scsi%d: bad DSA pointer 0x%p", host->host_no,
6014 dsa);
6015 next_dsa = NULL;
6017 else
6019 next_dsa = bus_to_virt(le32_to_cpu(dsa[hostdata->dsa_next / sizeof(u32)]));
6020 print_dsa (host, dsa, "");
6022 restore_flags(flags);
6024 printk ("scsi%d : end reconnect_dsa_head\n", host->host_no);
6025 if (left < 0)
6026 printk("scsi%d: possible loop in ncr reconnect list\n",
6027 host->host_no);
6030 static void
6031 print_lots (struct Scsi_Host *host) {
6032 NCR53c7x0_local_declare();
6033 struct NCR53c7x0_hostdata *hostdata =
6034 (struct NCR53c7x0_hostdata *) host->hostdata;
6035 u32 *dsp_next, *dsp, *dsa, dbc_dcmd;
6036 unsigned char dcmd, sbcl;
6037 int i, size;
6038 NCR53c7x0_local_setup(host);
6040 if ((dsp_next = bus_to_virt(NCR53c7x0_read32 (DSP_REG)))) {
6041 dbc_dcmd = NCR53c7x0_read32(DBC_REG);
6042 dcmd = (dbc_dcmd & 0xff000000) >> 24;
6043 dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
6044 dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
6045 sbcl = NCR53c7x0_read8 (SBCL_REG);
6048 printk ("scsi%d : DCMD|DBC=0x%x, DNAD=0x%x (virt 0x%p)\n"
6049 " DSA=0x%lx (virt 0x%p)\n"
6050 " DSPS=0x%x, TEMP=0x%x (virt 0x%p), DMODE=0x%x\n"
6051 " SXFER=0x%x, SCNTL3=0x%x\n"
6052 " %s%s%sphase=%s, %d bytes in SCSI FIFO\n"
6053 " STEST0=0x%x\n",
6054 host->host_no, dbc_dcmd, NCR53c7x0_read32(DNAD_REG),
6055 bus_to_virt(NCR53c7x0_read32(DNAD_REG)),
6056 virt_to_bus(dsa), dsa,
6057 NCR53c7x0_read32(DSPS_REG), NCR53c7x0_read32(TEMP_REG),
6058 bus_to_virt (NCR53c7x0_read32(TEMP_REG)),
6059 (int) NCR53c7x0_read8(hostdata->dmode),
6060 (int) NCR53c7x0_read8(SXFER_REG),
6061 (int) NCR53c7x0_read8(SCNTL3_REG_800),
6062 (sbcl & SBCL_BSY) ? "BSY " : "",
6063 (sbcl & SBCL_SEL) ? "SEL " : "",
6064 (sbcl & SBCL_REQ) ? "REQ " : "",
6065 sstat2_to_phase(NCR53c7x0_read8 (((hostdata->chip / 100) == 8) ?
6066 SSTAT1_REG : SSTAT2_REG)),
6067 (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
6068 SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT,
6069 NCR53c7x0_read8 (STEST0_REG_800));
6070 printk ("scsi%d : DSP 0x%lx (virt 0x%p) ->\n", host->host_no,
6071 virt_to_bus(dsp), dsp);
6072 for (i = 6; i > 0; --i, dsp += size)
6073 size = print_insn (host, dsp, "", 1);
6074 if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
6075 printk ("scsi%d : connected (SDID=0x%x, SSID=0x%x)\n",
6076 host->host_no, NCR53c7x0_read8 (SDID_REG_800),
6077 NCR53c7x0_read8 (SSID_REG_800));
6078 print_dsa (host, dsa, "");
6081 #if 1
6082 print_queues (host);
6083 #endif
6088 * Function : static int shutdown (struct Scsi_Host *host)
6090 * Purpose : does a clean (we hope) shutdown of the NCR SCSI
6091 * chip. Use prior to dumping core, unloading the NCR driver,
6093 * Returns : 0 on success
6095 static int
6096 shutdown (struct Scsi_Host *host) {
6097 NCR53c7x0_local_declare();
6098 unsigned long flags;
6099 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6100 host->hostdata;
6101 NCR53c7x0_local_setup(host);
6102 save_flags (flags);
6103 cli();
6104 /* Get in a state where we can reset the SCSI bus */
6105 ncr_halt (host);
6106 ncr_scsi_reset (host);
6107 hostdata->soft_reset(host);
6109 disable (host);
6110 restore_flags (flags);
6111 return 0;
6115 * Function : void ncr_scsi_reset (struct Scsi_Host *host)
6117 * Purpose : reset the SCSI bus.
6120 static void
6121 ncr_scsi_reset (struct Scsi_Host *host) {
6122 NCR53c7x0_local_declare();
6123 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6124 host->hostdata;
6125 unsigned long flags;
6126 int sien = 0;
6127 NCR53c7x0_local_setup(host);
6128 save_flags (flags);
6129 cli();
6130 if ((hostdata->chip / 100) == 8) {
6131 sien = NCR53c7x0_read8(SIEN0_REG_800);
6132 NCR53c7x0_write8(SIEN0_REG_800, sien & ~SIEN_RST);
6134 NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
6135 udelay(25); /* Minimum amount of time to assert RST */
6136 NCR53c7x0_write8(SCNTL1_REG, 0);
6137 if ((hostdata->chip / 100) == 8) {
6138 NCR53c7x0_write8(SIEN0_REG_800, sien);
6140 restore_flags (flags);
6144 * Function : void hard_reset (struct Scsi_Host *host)
6148 static void
6149 hard_reset (struct Scsi_Host *host) {
6150 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6151 host->hostdata;
6152 unsigned long flags;
6153 save_flags (flags);
6154 cli();
6155 ncr_scsi_reset(host);
6156 NCR53c7x0_driver_init (host);
6157 if (hostdata->soft_reset)
6158 hostdata->soft_reset (host);
6159 restore_flags(flags);
6164 * Function : Scsi_Cmnd *return_outstanding_commands (struct Scsi_Host *host,
6165 * int free, int issue)
6167 * Purpose : return a linked list (using the SCp.buffer field as next,
6168 * so we don't perturb hostdata. We don't use a field of the
6169 * NCR53c7x0_cmd structure since we may not have allocated one
6170 * for the command causing the reset.) of Scsi_Cmnd structures that
6171 * had propagated below the Linux issue queue level. If free is set,
6172 * free the NCR53c7x0_cmd structures which are associated with
6173 * the Scsi_Cmnd structures, and clean up any internal
6174 * NCR lists that the commands were on. If issue is set,
6175 * also return commands in the issue queue.
6177 * Returns : linked list of commands
6179 * NOTE : the caller should insure that the NCR chip is halted
6180 * if the free flag is set.
6183 static Scsi_Cmnd *
6184 return_outstanding_commands (struct Scsi_Host *host, int free, int issue) {
6185 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6186 host->hostdata;
6187 struct NCR53c7x0_cmd *c;
6188 int i;
6189 u32 *curr;
6190 Scsi_Cmnd *list = NULL, *tmp;
6191 for (c = (struct NCR53c7x0_cmd *) hostdata->running_list; c;
6192 c = (struct NCR53c7x0_cmd *) c->next) {
6193 if (c->cmd->SCp.buffer) {
6194 printk ("scsi%d : loop detected in running list!\n", host->host_no);
6195 break;
6196 } else {
6197 printk ("Duh? Bad things happening in the NCR driver\n");
6198 break;
6201 c->cmd->SCp.buffer = (struct scatterlist *) list;
6202 list = c->cmd;
6203 if (free) {
6204 c->next = hostdata->free;
6205 hostdata->free = c;
6209 if (free) {
6210 for (i = 0, curr = (u32 *) hostdata->schedule;
6211 i < host->can_queue; ++i, curr += 2) {
6212 curr[0] = hostdata->NOP_insn;
6213 curr[1] = le32_to_cpu(0xdeadbeef);
6215 hostdata->curr = NULL;
6218 if (issue) {
6219 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; tmp = tmp->next) {
6220 if (tmp->SCp.buffer) {
6221 printk ("scsi%d : loop detected in issue queue!\n",
6222 host->host_no);
6223 break;
6225 tmp->SCp.buffer = (struct scatterlist *) list;
6226 list = tmp;
6228 if (free)
6229 hostdata->issue_queue = NULL;
6232 return list;
6236 * Function : static int disable (struct Scsi_Host *host)
6238 * Purpose : disables the given NCR host, causing all commands
6239 * to return a driver error. Call this so we can unload the
6240 * module during development and try again. Eventually,
6241 * we should be able to find clean workarounds for these
6242 * problems.
6244 * Inputs : host - hostadapter to twiddle
6246 * Returns : 0 on success.
6249 static int
6250 disable (struct Scsi_Host *host) {
6251 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6252 host->hostdata;
6253 unsigned long flags;
6254 Scsi_Cmnd *nuke_list, *tmp;
6255 save_flags(flags);
6256 cli();
6257 if (hostdata->state != STATE_HALTED)
6258 ncr_halt (host);
6259 nuke_list = return_outstanding_commands (host, 1 /* free */, 1 /* issue */);
6260 hard_reset (host);
6261 hostdata->state = STATE_DISABLED;
6262 restore_flags(flags);
6263 printk ("scsi%d : nuking commands\n", host->host_no);
6264 for (; nuke_list; nuke_list = tmp) {
6265 tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
6266 nuke_list->result = DID_ERROR << 16;
6267 nuke_list->scsi_done(nuke_list);
6269 printk ("scsi%d : done. \n", host->host_no);
6270 printk (KERN_ALERT "scsi%d : disabled. Unload and reload\n",
6271 host->host_no);
6272 return 0;
6276 * Function : static int ncr_halt (struct Scsi_Host *host)
6278 * Purpose : halts the SCSI SCRIPTS(tm) processor on the NCR chip
6280 * Inputs : host - SCSI chip to halt
6282 * Returns : 0 on success
6285 static int
6286 ncr_halt (struct Scsi_Host *host) {
6287 NCR53c7x0_local_declare();
6288 unsigned long flags;
6289 unsigned char istat, tmp;
6290 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6291 host->hostdata;
6292 int stage;
6293 NCR53c7x0_local_setup(host);
6295 save_flags(flags);
6296 cli();
6297 /* Stage 0 : eat all interrupts
6298 Stage 1 : set ABORT
6299 Stage 2 : eat all but abort interrupts
6300 Stage 3 : eat all interrupts
6302 for (stage = 0;;) {
6303 if (stage == 1) {
6304 NCR53c7x0_write8(hostdata->istat, ISTAT_ABRT);
6305 ++stage;
6307 istat = NCR53c7x0_read8 (hostdata->istat);
6308 if (istat & ISTAT_SIP) {
6309 if ((hostdata->chip / 100) == 8) {
6310 tmp = NCR53c7x0_read8(SIST0_REG_800);
6311 udelay(1);
6312 tmp = NCR53c7x0_read8(SIST1_REG_800);
6313 } else {
6314 tmp = NCR53c7x0_read8(SSTAT0_REG);
6316 } else if (istat & ISTAT_DIP) {
6317 tmp = NCR53c7x0_read8(DSTAT_REG);
6318 if (stage == 2) {
6319 if (tmp & DSTAT_ABRT) {
6320 NCR53c7x0_write8(hostdata->istat, 0);
6321 ++stage;
6322 } else {
6323 printk(KERN_ALERT "scsi%d : could not halt NCR chip\n",
6324 host->host_no);
6325 disable (host);
6329 if (!(istat & (ISTAT_SIP|ISTAT_DIP))) {
6330 if (stage == 0)
6331 ++stage;
6332 else if (stage == 3)
6333 break;
6336 hostdata->state = STATE_HALTED;
6337 restore_flags(flags);
6338 #if 0
6339 print_lots (host);
6340 #endif
6341 return 0;
6345 * Function: event_name (int event)
6347 * Purpose: map event enum into user-readable strings.
6350 static const char *
6351 event_name (int event) {
6352 switch (event) {
6353 case EVENT_NONE: return "none";
6354 case EVENT_ISSUE_QUEUE: return "to issue queue";
6355 case EVENT_START_QUEUE: return "to start queue";
6356 case EVENT_SELECT: return "selected";
6357 case EVENT_DISCONNECT: return "disconnected";
6358 case EVENT_RESELECT: return "reselected";
6359 case EVENT_COMPLETE: return "completed";
6360 case EVENT_IDLE: return "idle";
6361 case EVENT_SELECT_FAILED: return "select failed";
6362 case EVENT_BEFORE_SELECT: return "before select";
6363 case EVENT_RESELECT_FAILED: return "reselect failed";
6364 default: return "unknown";
6369 * Function : void dump_events (struct Scsi_Host *host, count)
6371 * Purpose : print last count events which have occurred.
6373 static void
6374 dump_events (struct Scsi_Host *host, int count) {
6375 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6376 host->hostdata;
6377 struct NCR53c7x0_event event;
6378 int i;
6379 unsigned long flags;
6380 if (hostdata->events) {
6381 if (count > hostdata->event_size)
6382 count = hostdata->event_size;
6383 for (i = hostdata->event_index; count > 0;
6384 i = (i ? i - 1 : hostdata->event_size -1), --count) {
6385 save_flags(flags);
6387 * By copying the event we're currently examining with interrupts
6388 * disabled, we can do multiple printk(), etc. operations and
6389 * still be guaranteed that they're happening on the same
6390 * event structure.
6392 cli();
6393 #if 0
6394 event = hostdata->events[i];
6395 #else
6396 memcpy ((void *) &event, (void *) &(hostdata->events[i]),
6397 sizeof(event));
6398 #endif
6400 restore_flags(flags);
6401 printk ("scsi%d : %s event %d at %ld secs %ld usecs target %d lun %d\n",
6402 host->host_no, event_name (event.event), count,
6403 (long) event.time.tv_sec, (long) event.time.tv_usec,
6404 event.target, event.lun);
6405 if (event.dsa)
6406 printk (" event for dsa 0x%lx (virt 0x%p)\n",
6407 virt_to_bus(event.dsa), event.dsa);
6408 if (event.pid != -1) {
6409 printk (" event for pid %ld ", event.pid);
6410 print_command (event.cmnd);
6417 * Function: check_address
6419 * Purpose: Check to see if a possibly corrupt pointer will fault the
6420 * kernel.
6422 * Inputs: addr - address; size - size of area
6424 * Returns: 0 if area is OK, -1 on error.
6426 * NOTES: should be implemented in terms of vverify on kernels
6427 * that have it.
6430 static int
6431 check_address (unsigned long addr, int size) {
6432 return (MAP_NR(addr) < 1 || MAP_NR(addr + size) > MAP_NR(high_memory) ?
6433 -1 : 0);
6436 #ifdef MODULE
6437 int
6438 NCR53c7x0_release(struct Scsi_Host *host) {
6439 struct NCR53c7x0_hostdata *hostdata =
6440 (struct NCR53c7x0_hostdata *) host->hostdata;
6441 struct NCR53c7x0_cmd *cmd, *tmp;
6442 shutdown (host);
6443 if (host->irq != IRQ_NONE)
6445 int irq_count;
6446 struct Scsi_Host *tmp;
6447 for (irq_count = 0, tmp = first_host; tmp; tmp = tmp->next)
6448 if (tmp->hostt == the_template && tmp->irq == host->irq)
6449 ++irq_count;
6450 if (irq_count == 1)
6451 free_irq(host->irq, NULL);
6453 if (host->dma_channel != DMA_NONE)
6454 free_dma(host->dma_channel);
6455 if (host->io_port)
6456 release_region(host->io_port, host->n_io_port);
6458 for (cmd = (struct NCR53c7x0_cmd *) hostdata->free; cmd; cmd = tmp,
6459 --hostdata->num_cmds) {
6460 tmp = (struct NCR53c7x0_cmd *) cmd->next;
6462 * If we're going to loop, try to stop it to get a more accurate
6463 * count of the leaked commands.
6465 cmd->next = NULL;
6466 if (cmd->free)
6467 cmd->free ((void *) cmd->real, cmd->size);
6469 if (hostdata->num_cmds)
6470 printk ("scsi%d : leaked %d NCR53c7x0_cmd structures\n",
6471 host->host_no, hostdata->num_cmds);
6472 if (hostdata->events)
6473 vfree ((void *)hostdata->events);
6474 return 1;
6476 Scsi_Host_Template driver_template = NCR53c7xx;
6477 #include "scsi_module.c"
6478 #endif /* def MODULE */