Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / scsi / 53c7,8xx.c
blob15ec102d6046ccab62a1b9e7f488815a8f39bd7f
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 #include <linux/version.h>
218 #ifdef MODULE
219 #include <linux/module.h>
220 #endif
222 #include <asm/dma.h>
223 #include <asm/io.h>
224 #include <asm/system.h>
225 #include <linux/delay.h>
226 #include <linux/signal.h>
227 #include <linux/sched.h>
228 #include <linux/errno.h>
229 #include <linux/pci.h>
230 #include <linux/proc_fs.h>
231 #include <linux/string.h>
232 #include <linux/malloc.h>
233 #include <linux/vmalloc.h>
234 #include <linux/mm.h>
235 #include <linux/ioport.h>
236 #include <linux/time.h>
237 #include <linux/blk.h>
238 #include <linux/init.h>
239 #include <linux/spinlock.h>
241 #include "scsi.h"
242 #include "hosts.h"
243 #include "53c7,8xx.h"
244 #include "constants.h"
245 #include "sd.h"
246 #include <linux/stat.h>
247 #include <linux/stddef.h>
249 static int check_address (unsigned long addr, int size);
250 static void dump_events (struct Scsi_Host *host, int count);
251 static Scsi_Cmnd * return_outstanding_commands (struct Scsi_Host *host,
252 int free, int issue);
253 static void hard_reset (struct Scsi_Host *host);
254 static void ncr_scsi_reset (struct Scsi_Host *host);
255 static void print_lots (struct Scsi_Host *host);
256 static void set_synchronous (struct Scsi_Host *host, int target, int sxfer,
257 int scntl3, int now_connected);
258 static int datapath_residual (struct Scsi_Host *host);
259 static const char * sbcl_to_phase (int sbcl);
260 static void print_progress (Scsi_Cmnd *cmd);
261 static void print_queues (struct Scsi_Host *host);
262 static void process_issue_queue (unsigned long flags);
263 static int shutdown (struct Scsi_Host *host);
264 static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result);
265 static int disable (struct Scsi_Host *host);
266 static int NCR53c8xx_run_tests (struct Scsi_Host *host);
267 static int NCR53c8xx_script_len;
268 static int NCR53c8xx_dsa_len;
269 static void NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs);
270 static void do_NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs);
271 static int ncr_halt (struct Scsi_Host *host);
272 static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd
273 *cmd);
274 static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd);
275 static void print_dsa (struct Scsi_Host *host, u32 *dsa,
276 const char *prefix);
277 static int print_insn (struct Scsi_Host *host, const u32 *insn,
278 const char *prefix, int kernel);
280 static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd);
281 static void NCR53c8x0_init_fixup (struct Scsi_Host *host);
282 static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
283 NCR53c7x0_cmd *cmd);
284 static void NCR53c8x0_soft_reset (struct Scsi_Host *host);
286 /* INSMOD variables */
287 static long long perm_options = PERM_OPTIONS;
288 /* 14 = .5s; 15 is max; decreasing divides by two. */
289 static int selection_timeout = 14;
290 /* Size of event list (per host adapter) */
291 static int track_events = 0;
293 static struct Scsi_Host *first_host = NULL; /* Head of list of NCR boards */
294 static Scsi_Host_Template *the_template = NULL;
297 * KNOWN BUGS :
298 * - There is some sort of conflict when the PPP driver is compiled with
299 * support for 16 channels?
301 * - On systems which predate the 1.3.x initialization order change,
302 * the NCR driver will cause Cannot get free page messages to appear.
303 * These are harmless, but I don't know of an easy way to avoid them.
305 * - With OPTION_DISCONNECT, on two systems under unknown circumstances,
306 * we get a PHASE MISMATCH with DSA set to zero (suggests that we
307 * are occurring somewhere in the reselection code) where
308 * DSP=some value DCMD|DBC=same value.
310 * Closer inspection suggests that we may be trying to execute
311 * some portion of the DSA?
312 * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
313 * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
314 * scsi0 : no current command : unexpected phase MSGIN.
315 * DSP=0x1c46cc, DCMD|DBC=0x1c46ac, DSA=0x0
316 * DSPS=0x0, TEMP=0x1c3e70, DMODE=0x80
317 * scsi0 : DSP->
318 * 001c46cc : 0x001c46cc 0x00000000
319 * 001c46d4 : 0x001c5ea0 0x000011f8
321 * Changed the print code in the phase_mismatch handler so
322 * that we call print_lots to try to diagnose this.
327 * Possible future direction of architecture for max performance :
329 * We're using a single start array for the NCR chip. This is
330 * sub-optimal, because we cannot add a command which would conflict with
331 * an executing command to this start queue, and therefore must insert the
332 * next command for a given I/T/L combination after the first has completed;
333 * incurring our interrupt latency between SCSI commands.
335 * To allow further pipelining of the NCR and host CPU operation, we want
336 * to set things up so that immediately on termination of a command destined
337 * for a given LUN, we get that LUN busy again.
339 * To do this, we need to add a 32 bit pointer to which is jumped to
340 * on completion of a command. If no new command is available, this
341 * would point to the usual DSA issue queue select routine.
343 * If one were, it would point to a per-NCR53c7x0_cmd select routine
344 * which starts execution immediately, inserting the command at the head
345 * of the start queue if the NCR chip is selected or reselected.
347 * We would change so that we keep a list of outstanding commands
348 * for each unit, rather than a single running_list. We'd insert
349 * a new command into the right running list; if the NCR didn't
350 * have something running for that yet, we'd put it in the
351 * start queue as well. Some magic needs to happen to handle the
352 * race condition between the first command terminating before the
353 * new one is written.
355 * Potential for profiling :
356 * Call do_gettimeofday(struct timeval *tv) to get 800ns resolution.
361 * TODO :
362 * 1. To support WIDE transfers, not much needs to happen. We
363 * should do CHMOVE instructions instead of MOVEs when
364 * we have scatter/gather segments of uneven length. When
365 * we do this, we need to handle the case where we disconnect
366 * between segments.
368 * 2. Currently, when Icky things happen we do a FATAL(). Instead,
369 * we want to do an integrity check on the parts of the NCR hostdata
370 * structure which were initialized at boot time; FATAL() if that
371 * fails, and otherwise try to recover. Keep track of how many
372 * times this has happened within a single SCSI command; if it
373 * gets excessive, then FATAL().
375 * 3. Parity checking is currently disabled, and a few things should
376 * happen here now that we support synchronous SCSI transfers :
377 * 1. On soft-reset, we should set the EPC (Enable Parity Checking)
378 * and AAP (Assert SATN/ on parity error) bits in SCNTL0.
380 * 2. We should enable the parity interrupt in the SIEN0 register.
382 * 3. intr_phase_mismatch() needs to believe that message out is
383 * always an "acceptable" phase to have a mismatch in. If
384 * the old phase was MSG_IN, we should send a MESSAGE PARITY
385 * error. If the old phase was something else, we should send
386 * a INITIATOR_DETECTED_ERROR message. Note that this could
387 * cause a RESTORE POINTERS message; so we should handle that
388 * correctly first. Instead, we should probably do an
389 * initiator_abort.
391 * 4. MPEE bit of CTEST4 should be set so we get interrupted if
392 * we detect an error.
395 * 5. The initial code has been tested on the NCR53c810. I don't
396 * have access to NCR53c700, 700-66 (Forex boards), NCR53c710
397 * (NCR Pentium systems), NCR53c720, NCR53c820, or NCR53c825 boards to
398 * finish development on those platforms.
400 * NCR53c820/825/720 - need to add wide transfer support, including WDTR
401 * negotiation, programming of wide transfer capabilities
402 * on reselection and table indirect selection.
404 * NCR53c710 - need to add fatal interrupt or GEN code for
405 * command completion signaling. Need to modify all
406 * SDID, SCID, etc. registers, and table indirect select code
407 * since these use bit fielded (ie 1<<target) instead of
408 * binary encoded target ids. Need to accommodate
409 * different register mappings, probably scan through
410 * the SCRIPT code and change the non SFBR register operand
411 * of all MOVE instructions.
413 * NCR53c700/700-66 - need to add code to refix addresses on
414 * every nexus change, eliminate all table indirect code,
415 * very messy.
417 * 6. The NCR53c7x0 series is very popular on other platforms that
418 * could be running Linux - ie, some high performance AMIGA SCSI
419 * boards use it.
421 * So, I should include #ifdef'd code so that it is
422 * compatible with these systems.
424 * Specifically, the little Endian assumptions I made in my
425 * bit fields need to change, and if the NCR doesn't see memory
426 * the right way, we need to provide options to reverse words
427 * when the scripts are relocated.
429 * 7. Use ioremap() to access memory mapped boards.
433 * Allow for simultaneous existence of multiple SCSI scripts so we
434 * can have a single driver binary for all of the family.
436 * - one for NCR53c700 and NCR53c700-66 chips (not yet supported)
437 * - one for rest (only the NCR53c810, 815, 820, and 825 are currently
438 * supported)
440 * So that we only need two SCSI scripts, we need to modify things so
441 * that we fixup register accesses in READ/WRITE instructions, and
442 * we'll also have to accommodate the bit vs. binary encoding of IDs
443 * with the 7xx chips.
447 * Use pci_chips_ids to translate in both directions between PCI device ID
448 * and chip numbers.
451 static struct {
452 unsigned short pci_device_id;
453 int chip;
455 * The revision field of the PCI_CLASS_REVISION register is compared
456 * against each of these fields if the field is not -1. If it
457 * is less than min_revision or larger than max_revision, a warning
458 * message is printed.
460 int max_revision;
461 int min_revision;
462 } pci_chip_ids[] = {
463 {PCI_DEVICE_ID_NCR_53C810, 810, 2, 1},
464 {PCI_DEVICE_ID_NCR_53C815, 815, 3, 2},
465 {PCI_DEVICE_ID_NCR_53C820, 820, -1, -1},
466 {PCI_DEVICE_ID_NCR_53C825, 825, -1, -1}
469 #define NPCI_CHIP_IDS (sizeof (pci_chip_ids) / sizeof(pci_chip_ids[0]))
471 #define ROUNDUP(adr,type) \
472 ((void *) (((long) (adr) + sizeof(type) - 1) & ~(sizeof(type) - 1)))
475 * Forced detection and autoprobe code for various hardware. Currently,
476 * entry points for these are not included in init/main.c because if the
477 * PCI BIOS code isn't working right, you're not going to be able to use
478 * the hardware anyways; this way we force users to solve their
479 * problems rather than forcing detection and blaming us when it
480 * does not work.
483 static struct override {
484 int chip; /* 700, 70066, 710, 720, 810, 820 */
485 int board; /* Any special board level gunk */
486 unsigned pci:1;
487 union {
488 struct {
489 int base; /* Memory address - indicates memory mapped regs */
490 int io_port;/* I/O port address - indicates I/O mapped regs */
491 int irq; /* IRQ line */
492 int dma; /* DMA channel - often none */
493 } normal;
494 struct {
495 int bus;
496 int device;
497 int function;
498 } pci;
499 } data;
500 long long options;
501 } overrides [4] = {{0,},};
502 static int commandline_current = 0;
503 static int no_overrides = 0;
505 #if 0
506 #define OVERRIDE_LIMIT (sizeof(overrides) / sizeof(struct override))
507 #else
508 #define OVERRIDE_LIMIT commandline_current
509 #endif
512 * Function: issue_to_cmd
514 * Purpose: convert jump instruction in issue array to NCR53c7x0_cmd
515 * structure pointer.
517 * Inputs; issue - pointer to start of NOP or JUMP instruction
518 * in issue array.
520 * Returns: pointer to command on success; 0 if opcode is NOP.
523 static inline struct NCR53c7x0_cmd *
524 issue_to_cmd (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
525 u32 *issue)
527 return (issue[0] != hostdata->NOP_insn) ?
529 * If the IF TRUE bit is set, it's a JUMP instruction. The
530 * operand is a bus pointer to the dsa_begin routine for this DSA. The
531 * dsa field of the NCR53c7x0_cmd structure starts with the
532 * DSA code template. By converting to a virtual address,
533 * subtracting the code template size, and offset of the
534 * dsa field, we end up with a pointer to the start of the
535 * structure (alternatively, we could use the
536 * dsa_cmnd field, an anachronism from when we weren't
537 * sure what the relationship between the NCR structures
538 * and host structures were going to be.
540 (struct NCR53c7x0_cmd *) ((char *) bus_to_virt (le32_to_cpu(issue[1])) -
541 (hostdata->E_dsa_code_begin - hostdata->E_dsa_code_template) -
542 offsetof(struct NCR53c7x0_cmd, dsa))
543 /* If the IF TRUE bit is not set, it's a NOP */
544 : NULL;
549 * Function : static internal_setup(int board, int chip, char *str, int *ints)
551 * Purpose : LILO command line initialization of the overrides array,
553 * Inputs : board - currently, unsupported. chip - 700, 70066, 710, 720
554 * 810, 815, 820, 825, although currently only the NCR53c810 is
555 * supported.
559 static void
560 internal_setup(int board, int chip, char *str, int *ints) {
561 unsigned char pci; /* Specifies a PCI override, with bus, device,
562 function */
564 pci = (str && !strcmp (str, "pci")) ? 1 : 0;
567 * Override syntaxes are as follows :
568 * ncr53c700,ncr53c700-66,ncr53c710,ncr53c720=mem,io,irq,dma
569 * ncr53c810,ncr53c820,ncr53c825=mem,io,irq or pci,bus,device,function
572 if (commandline_current < OVERRIDE_LIMIT) {
573 overrides[commandline_current].pci = pci ? 1 : 0;
574 if (!pci) {
575 overrides[commandline_current].data.normal.base = ints[1];
576 overrides[commandline_current].data.normal.io_port = ints[2];
577 overrides[commandline_current].data.normal.irq = ints[3];
578 overrides[commandline_current].data.normal.dma = (ints[0] >= 4) ?
579 ints[4] : DMA_NONE;
580 /* FIXME: options is now a long long */
581 overrides[commandline_current].options = (ints[0] >= 5) ?
582 ints[5] : 0;
583 } else {
584 overrides[commandline_current].data.pci.bus = ints[1];
585 overrides[commandline_current].data.pci.device = ints[2];
586 overrides[commandline_current].data.pci.function = ints[3];
587 /* FIXME: options is now a long long */
588 overrides[commandline_current].options = (ints[0] >= 4) ?
589 ints[4] : 0;
591 overrides[commandline_current].board = board;
592 overrides[commandline_current].chip = chip;
593 ++commandline_current;
594 ++no_overrides;
595 } else {
596 printk ("53c7,7x0.c:internal_setup() : too many overrides\n");
601 * XXX - we might want to implement a single override function
602 * with a chip type field, revamp the command line configuration,
603 * etc.
606 #define setup_wrapper(x) \
607 void ncr53c##x##_setup (char *str, int *ints) { \
608 internal_setup (BOARD_GENERIC, x, str, ints); \
611 setup_wrapper(700)
612 setup_wrapper(70066)
613 setup_wrapper(710)
614 setup_wrapper(720)
615 setup_wrapper(810)
616 setup_wrapper(815)
617 setup_wrapper(820)
618 setup_wrapper(825)
621 * FIXME: we should junk these, in favor of synchronous_want and
622 * wide_want in the NCR53c7x0_hostdata structure.
625 /* Template for "preferred" synchronous transfer parameters. */
627 static const unsigned char sdtr_message[] = {
628 #ifdef CONFIG_SCSI_NCR53C7xx_FAST
629 EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 25 /* *4ns */, 8 /* off */
630 #else
631 EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 50 /* *4ns */, 8 /* off */
632 #endif
635 /* Template to request asynchronous transfers */
637 static const unsigned char async_message[] = {
638 EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 0, 0 /* asynchronous */
641 /* Template for "preferred" WIDE transfer parameters */
643 static const unsigned char wdtr_message[] = {
644 EXTENDED_MESSAGE, 2 /* length */, EXTENDED_WDTR, 1 /* 2^1 bytes */
648 * Function : struct Scsi_Host *find_host (int host)
650 * Purpose : KGDB support function which translates a host number
651 * to a host structure.
653 * Inputs : host - number of SCSI host
655 * Returns : NULL on failure, pointer to host structure on success.
658 #if 0
659 static struct Scsi_Host *
660 find_host (int host) {
661 struct Scsi_Host *h;
662 for (h = first_host; h && h->host_no != host; h = h->next);
663 if (!h) {
664 printk (KERN_ALERT "scsi%d not found\n", host);
665 return NULL;
666 } else if (h->hostt != the_template) {
667 printk (KERN_ALERT "scsi%d is not a NCR board\n", host);
668 return NULL;
670 return h;
674 * Function : request_synchronous (int host, int target)
676 * Purpose : KGDB interface which will allow us to negotiate for
677 * synchronous transfers. This ill be replaced with a more
678 * integrated function; perhaps a new entry in the scsi_host
679 * structure, accessible via an ioctl() or perhaps /proc/scsi.
681 * Inputs : host - number of SCSI host; target - number of target.
683 * Returns : 0 when negotiation has been setup for next SCSI command,
684 * -1 on failure.
687 static int
688 request_synchronous (int host, int target) {
689 struct Scsi_Host *h;
690 struct NCR53c7x0_hostdata *hostdata;
691 unsigned long flags;
692 if (target < 0) {
693 printk (KERN_ALERT "target %d is bogus\n", target);
694 return -1;
696 if (!(h = find_host (host)))
697 return -1;
698 else if (h->this_id == target) {
699 printk (KERN_ALERT "target %d is host ID\n", target);
700 return -1;
702 else if (target > h->max_id) {
703 printk (KERN_ALERT "target %d exceeds maximum of %d\n", target,
704 h->max_id);
705 return -1;
707 hostdata = (struct NCR53c7x0_hostdata *)h->hostdata;
709 save_flags(flags);
710 cli();
711 if (hostdata->initiate_sdtr & (1 << target)) {
712 restore_flags(flags);
713 printk (KERN_ALERT "target %d already doing SDTR\n", target);
714 return -1;
716 hostdata->initiate_sdtr |= (1 << target);
717 restore_flags(flags);
718 return 0;
722 * Function : request_disconnect (int host, int on_or_off)
724 * Purpose : KGDB support function, tells us to allow or disallow
725 * disconnections.
727 * Inputs : host - number of SCSI host; on_or_off - non-zero to allow,
728 * zero to disallow.
730 * Returns : 0 on success, * -1 on failure.
733 static int
734 request_disconnect (int host, int on_or_off) {
735 struct Scsi_Host *h;
736 struct NCR53c7x0_hostdata *hostdata;
737 if (!(h = find_host (host)))
738 return -1;
739 hostdata = (struct NCR53c7x0_hostdata *) h->hostdata;
740 if (on_or_off)
741 hostdata->options |= OPTION_DISCONNECT;
742 else
743 hostdata->options &= ~OPTION_DISCONNECT;
744 return 0;
746 #endif
749 * Function : static void NCR53c7x0_driver_init (struct Scsi_Host *host)
751 * Purpose : Initialize internal structures, as required on startup, or
752 * after a SCSI bus reset.
754 * Inputs : host - pointer to this host adapter's structure
757 static void
758 NCR53c7x0_driver_init (struct Scsi_Host *host) {
759 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
760 host->hostdata;
761 int i, j;
762 u32 *curr;
763 for (i = 0; i < 16; ++i) {
764 hostdata->request_sense[i] = 0;
765 for (j = 0; j < 8; ++j)
766 hostdata->busy[i][j] = 0;
767 set_synchronous (host, i, /* sxfer */ 0, hostdata->saved_scntl3, 0);
769 hostdata->issue_queue = NULL;
770 hostdata->running_list = hostdata->finished_queue =
771 hostdata->curr = NULL;
772 for (i = 0, curr = (u32 *) hostdata->schedule;
773 i < host->can_queue; ++i, curr += 2) {
774 curr[0] = hostdata->NOP_insn;
775 curr[1] = le32_to_cpu(0xdeadbeef);
777 curr[0] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) | DBC_TCI_TRUE);
778 curr[1] = (u32) le32_to_cpu(virt_to_bus (hostdata->script) +
779 hostdata->E_wait_reselect);
780 hostdata->reconnect_dsa_head = 0;
781 hostdata->addr_reconnect_dsa_head = (u32)
782 le32_to_cpu(virt_to_bus((void *) &(hostdata->reconnect_dsa_head)));
783 hostdata->expecting_iid = 0;
784 hostdata->expecting_sto = 0;
785 if (hostdata->options & OPTION_ALWAYS_SYNCHRONOUS)
786 hostdata->initiate_sdtr = le32_to_cpu(0xffff);
787 else
788 hostdata->initiate_sdtr = 0;
789 hostdata->talked_to = 0;
790 hostdata->idle = 1;
794 * Function : static int ccf_to_clock (int ccf)
796 * Purpose : Return the largest SCSI clock allowable for a given
797 * clock conversion factor, allowing us to do synchronous periods
798 * when we don't know what the SCSI clock is by taking at least
799 * as long as the device says we can.
801 * Inputs : ccf
803 * Returns : clock on success, -1 on failure.
806 static int
807 ccf_to_clock (int ccf) {
808 switch (ccf) {
809 case 1: return 25000000; /* Divide by 1.0 */
810 case 2: return 37500000; /* Divide by 1.5 */
811 case 3: return 50000000; /* Divide by 2.0 */
812 case 0: /* Divide by 3.0 */
813 case 4: return 66000000;
814 default: return -1;
819 * Function : static int clock_to_ccf (int clock)
821 * Purpose : Return the clock conversion factor for a given SCSI clock.
823 * Inputs : clock - SCSI clock expressed in Hz.
825 * Returns : ccf on success, -1 on failure.
828 static int
829 clock_to_ccf (int clock) {
830 if (clock < 16666666)
831 return -1;
832 if (clock < 25000000)
833 return 1; /* Divide by 1.0 */
834 else if (clock < 37500000)
835 return 2; /* Divide by 1.5 */
836 else if (clock < 50000000)
837 return 3; /* Divide by 2.0 */
838 else if (clock < 66000000)
839 return 4; /* Divide by 3.0 */
840 else
841 return -1;
845 * Function : static int NCR53c7x0_init (struct Scsi_Host *host)
847 * Purpose : initialize the internal structures for a given SCSI host
849 * Inputs : host - pointer to this host adapter's structure
851 * Preconditions : when this function is called, the chip_type
852 * field of the hostdata structure MUST have been set.
854 * Returns : 0 on success, -1 on failure.
857 static int
858 NCR53c7x0_init (struct Scsi_Host *host) {
859 NCR53c7x0_local_declare();
860 int i, ccf, expected_ccf;
861 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
862 host->hostdata;
863 struct Scsi_Host *search;
865 * There are some things which we need to know about in order to provide
866 * a semblance of support. Print 'em if they aren't what we expect,
867 * otherwise don't add to the noise.
869 * -1 means we don't know what to expect.
871 int expected_id = -1;
872 int expected_clock = -1;
873 int uninitialized = 0;
875 * FIXME : this is only on Intel boxes. On other platforms, this
876 * will differ.
878 int expected_mapping = OPTION_IO_MAPPED;
880 NCR53c7x0_local_setup(host);
882 switch (hostdata->chip) {
883 case 820:
884 case 825:
885 #ifdef notyet
886 host->max_id = 15;
887 #endif
888 /* Fall through */
889 case 810:
890 case 815:
891 hostdata->dstat_sir_intr = NCR53c8x0_dstat_sir_intr;
892 hostdata->init_save_regs = NULL;
893 hostdata->dsa_fixup = NCR53c8xx_dsa_fixup;
894 hostdata->init_fixup = NCR53c8x0_init_fixup;
895 hostdata->soft_reset = NCR53c8x0_soft_reset;
896 hostdata->run_tests = NCR53c8xx_run_tests;
897 /* Is the SCSI clock ever anything else on these chips? */
898 expected_clock = hostdata->scsi_clock = 40000000;
899 expected_id = 7;
900 break;
901 default:
902 printk ("scsi%d : chip type of %d is not supported yet, detaching.\n",
903 host->host_no, hostdata->chip);
904 scsi_unregister (host);
905 return -1;
908 /* Assign constants accessed by NCR */
909 hostdata->NCR53c7xx_zero = 0;
910 hostdata->NCR53c7xx_msg_reject = le32_to_cpu(MESSAGE_REJECT);
911 hostdata->NCR53c7xx_msg_abort = le32_to_cpu(ABORT);
912 hostdata->NCR53c7xx_msg_nop = le32_to_cpu(NOP);
913 hostdata->NOP_insn = le32_to_cpu((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24);
915 if (expected_mapping == -1 ||
916 (hostdata->options & (OPTION_MEMORY_MAPPED)) !=
917 (expected_mapping & OPTION_MEMORY_MAPPED))
918 printk ("scsi%d : using %s mapped access\n", host->host_no,
919 (hostdata->options & OPTION_MEMORY_MAPPED) ? "memory" :
920 "io");
922 hostdata->dmode = (hostdata->chip == 700 || hostdata->chip == 70066) ?
923 DMODE_REG_00 : DMODE_REG_10;
924 hostdata->istat = ((hostdata->chip / 100) == 8) ?
925 ISTAT_REG_800 : ISTAT_REG_700;
927 /* Only the ISTAT register is readable when the NCR is running, so make
928 sure it's halted. */
929 ncr_halt(host);
932 * XXX - the NCR53c700 uses bitfielded registers for SCID, SDID, etc,
933 * as does the 710 with one bit per SCSI ID. Conversely, the NCR
934 * uses a normal, 3 bit binary representation of these values.
936 * Get the rest of the NCR documentation, and FIND OUT where the change
937 * was.
939 #if 0
940 tmp = hostdata->this_id_mask = NCR53c7x0_read8(SCID_REG);
941 for (host->this_id = 0; tmp != 1; tmp >>=1, ++host->this_id);
942 #else
943 host->this_id = NCR53c7x0_read8(SCID_REG) & 15;
944 if (host->this_id == 0)
945 host->this_id = 7; /* sanitize hostid---0 doesn't make sense */
946 hostdata->this_id_mask = 1 << host->this_id;
947 #endif
950 * Note : we should never encounter a board setup for ID0. So,
951 * if we see ID0, assume that it was uninitialized and set it
952 * to the industry standard 7.
954 if (!host->this_id) {
955 printk("scsi%d : initiator ID was %d, changing to 7\n",
956 host->host_no, host->this_id);
957 host->this_id = 7;
958 hostdata->this_id_mask = 1 << 7;
959 uninitialized = 1;
962 if (expected_id == -1 || host->this_id != expected_id)
963 printk("scsi%d : using initiator ID %d\n", host->host_no,
964 host->this_id);
967 * Save important registers to allow a soft reset.
970 if ((hostdata->chip / 100) == 8) {
972 * CTEST4 controls burst mode disable.
974 hostdata->saved_ctest4 = NCR53c7x0_read8(CTEST4_REG_800) &
975 CTEST4_800_SAVE;
976 } else {
978 * CTEST7 controls cache snooping, burst mode, and support for
979 * external differential drivers.
981 hostdata->saved_ctest7 = NCR53c7x0_read8(CTEST7_REG) & CTEST7_SAVE;
985 * On NCR53c700 series chips, DCNTL controls the SCSI clock divisor,
986 * on 800 series chips, it allows for a totem-pole IRQ driver.
989 hostdata->saved_dcntl = NCR53c7x0_read8(DCNTL_REG);
992 * DCNTL_800_IRQM controls weather we are using an open drain
993 * driver (reset) or totem pole driver (set). In all cases,
994 * it's level active. I suppose this is an issue when we're trying to
995 * wire-or the same PCI INTx line?
997 if ((hostdata->chip / 100) == 8)
998 hostdata->saved_dcntl &= ~DCNTL_800_IRQM;
1001 * DMODE controls DMA burst length, and on 700 series chips,
1002 * 286 mode and bus width
1004 hostdata->saved_dmode = NCR53c7x0_read8(hostdata->dmode);
1007 * Now that burst length and enabled/disabled status is known,
1008 * clue the user in on it.
1011 if ((hostdata->chip / 100) == 8) {
1012 if (hostdata->saved_ctest4 & CTEST4_800_BDIS) {
1013 printk ("scsi%d : burst mode disabled\n", host->host_no);
1014 } else {
1015 switch (hostdata->saved_dmode & DMODE_BL_MASK) {
1016 case DMODE_BL_2: i = 2; break;
1017 case DMODE_BL_4: i = 4; break;
1018 case DMODE_BL_8: i = 8; break;
1019 case DMODE_BL_16: i = 16; break;
1020 default: i = 0;
1022 printk ("scsi%d : burst length %d\n", host->host_no, i);
1027 * On NCR53c810 and NCR53c820 chips, SCNTL3 contails the synchronous
1028 * and normal clock conversion factors.
1030 if (hostdata->chip / 100 == 8) {
1031 expected_ccf = clock_to_ccf (expected_clock);
1032 hostdata->saved_scntl3 = NCR53c7x0_read8(SCNTL3_REG_800);
1033 ccf = hostdata->saved_scntl3 & SCNTL3_800_CCF_MASK;
1034 if (expected_ccf != -1 && ccf != expected_ccf && !ccf) {
1035 hostdata->saved_scntl3 = (hostdata->saved_scntl3 &
1036 ~SCNTL3_800_CCF_MASK) | expected_ccf;
1037 if (!uninitialized) {
1038 printk ("scsi%d : reset ccf to %d from %d\n",
1039 host->host_no, expected_ccf, ccf);
1040 uninitialized = 1;
1043 } else
1044 ccf = 0;
1047 * If we don't have a SCSI clock programmed, pick one on the upper
1048 * bound of that allowed by NCR so that our transfers err on the
1049 * slow side, since transfer period must be >= the agreed
1050 * upon period.
1053 if ((!hostdata->scsi_clock) && (hostdata->scsi_clock = ccf_to_clock (ccf))
1054 == -1) {
1055 printk ("scsi%d : clock conversion factor %d unknown.\n"
1056 " synchronous transfers disabled\n",
1057 host->host_no, ccf);
1058 hostdata->options &= ~OPTION_SYNCHRONOUS;
1059 hostdata->scsi_clock = 0;
1062 if (expected_clock == -1 || hostdata->scsi_clock != expected_clock)
1063 printk ("scsi%d : using %dMHz SCSI clock\n", host->host_no,
1064 hostdata->scsi_clock / 1000000);
1066 for (i = 0; i < 16; ++i)
1067 hostdata->cmd_allocated[i] = 0;
1069 if (hostdata->init_save_regs)
1070 hostdata->init_save_regs (host);
1071 if (hostdata->init_fixup)
1072 hostdata->init_fixup (host);
1074 if (!the_template) {
1075 the_template = host->hostt;
1076 first_host = host;
1080 * Linux SCSI drivers have always been plagued with initialization
1081 * problems - some didn't work with the BIOS disabled since they expected
1082 * initialization from it, some didn't work when the networking code
1083 * was enabled and registers got scrambled, etc.
1085 * To avoid problems like this, in the future, we will do a soft
1086 * reset on the SCSI chip, taking it back to a sane state.
1089 hostdata->soft_reset (host);
1091 #if 1
1092 hostdata->debug_count_limit = -1;
1093 #else
1094 hostdata->debug_count_limit = 1;
1095 #endif
1096 hostdata->intrs = -1;
1097 hostdata->resets = -1;
1098 memcpy ((void *) hostdata->synchronous_want, (void *) sdtr_message,
1099 sizeof (hostdata->synchronous_want));
1101 NCR53c7x0_driver_init (host);
1104 * Set up an interrupt handler if we aren't already sharing an IRQ
1105 * with another board.
1108 for (search = first_host; search && !(search->hostt == the_template &&
1109 search->irq == host->irq && search != host); search=search->next);
1111 if (!search) {
1112 #ifdef __powerpc__
1113 if (request_irq(host->irq, do_NCR53c7x0_intr, SA_SHIRQ, "53c7,8xx", NULL))
1114 #else
1115 if (request_irq(host->irq, do_NCR53c7x0_intr, SA_INTERRUPT, "53c7,8xx", NULL))
1116 #endif
1120 printk("scsi%d : IRQ%d not free, detaching\n"
1121 " You have either a configuration problem, or a\n"
1122 " broken BIOS. You may wish to manually assign\n"
1123 " an interrupt to the NCR board rather than using\n"
1124 " an automatic setting.\n",
1125 host->host_no, host->irq);
1126 scsi_unregister (host);
1127 return -1;
1129 } else {
1130 printk("scsi%d : using interrupt handler previously installed for scsi%d\n",
1131 host->host_no, search->host_no);
1135 if ((hostdata->run_tests && hostdata->run_tests(host) == -1) ||
1136 (hostdata->options & OPTION_DEBUG_TESTS_ONLY)) {
1137 /* XXX Should disable interrupts, etc. here */
1138 scsi_unregister (host);
1139 return -1;
1140 } else {
1141 if (host->io_port) {
1142 host->n_io_port = 128;
1143 request_region (host->io_port, host->n_io_port, "ncr53c7,8xx");
1147 if (NCR53c7x0_read8 (SBCL_REG) & SBCL_BSY) {
1148 printk ("scsi%d : bus wedge, doing SCSI reset\n", host->host_no);
1149 hard_reset (host);
1151 return 0;
1155 * Function : static int normal_init(Scsi_Host_Template *tpnt, int board,
1156 * int chip, u32 base, int io_port, int irq, int dma, int pcivalid,
1157 * unsigned char pci_bus, unsigned char pci_device_fn,
1158 * long long options);
1160 * Purpose : initializes a NCR53c7,8x0 based on base addresses,
1161 * IRQ, and DMA channel.
1163 * Useful where a new NCR chip is backwards compatible with
1164 * a supported chip, but the DEVICE ID has changed so it
1165 * doesn't show up when the autoprobe does a pcibios_find_device.
1167 * Inputs : tpnt - Template for this SCSI adapter, board - board level
1168 * product, chip - 810, 820, or 825, bus - PCI bus, device_fn -
1169 * device and function encoding as used by PCI BIOS calls.
1171 * Returns : 0 on success, -1 on failure.
1175 static int __init
1176 normal_init (Scsi_Host_Template *tpnt, int board, int chip,
1177 u32 base, int io_port, int irq, int dma, int pci_valid,
1178 unsigned char pci_bus, unsigned char pci_device_fn, long long options){
1179 struct Scsi_Host *instance;
1180 struct NCR53c7x0_hostdata *hostdata;
1181 char chip_str[80];
1182 int script_len = 0, dsa_len = 0, size = 0, max_cmd_size = 0,
1183 schedule_size = 0, ok = 0;
1184 void *tmp;
1186 options |= perm_options;
1188 switch (chip) {
1189 case 825:
1190 case 820:
1191 case 815:
1192 case 810:
1193 schedule_size = (tpnt->can_queue + 1) * 8 /* JUMP instruction size */;
1194 script_len = NCR53c8xx_script_len;
1195 dsa_len = NCR53c8xx_dsa_len;
1196 options |= OPTION_INTFLY;
1197 sprintf (chip_str, "NCR53c%d", chip);
1198 break;
1199 default:
1200 printk("scsi-ncr53c7,8xx : unsupported SCSI chip %d\n", chip);
1201 return -1;
1204 printk("scsi-ncr53c7,8xx : %s at memory 0x%x, io 0x%x, irq %d",
1205 chip_str, (unsigned) base, io_port, irq);
1206 if (dma == DMA_NONE)
1207 printk("\n");
1208 else
1209 printk(", dma %d\n", dma);
1211 if ((chip / 100 == 8) && !pci_valid)
1212 printk ("scsi-ncr53c7,8xx : for better reliability and performance, please use the\n"
1213 " PCI override instead.\n"
1214 " Syntax : ncr53c8{10,15,20,25}=pci,<bus>,<device>,<function>\n"
1215 " <bus> and <device> are usually 0.\n");
1217 if (options & OPTION_DEBUG_PROBE_ONLY) {
1218 printk ("scsi-ncr53c7,8xx : probe only enabled, aborting initialization\n");
1219 return -1;
1222 max_cmd_size = sizeof(struct NCR53c7x0_cmd) + dsa_len +
1223 /* Size of dynamic part of command structure : */
1224 2 * /* Worst case : we don't know if we need DATA IN or DATA out */
1225 ( 2 * /* Current instructions per scatter/gather segment */
1226 tpnt->sg_tablesize +
1227 3 /* Current startup / termination required per phase */
1229 8 /* Each instruction is eight bytes */;
1231 /* Allocate fixed part of hostdata, dynamic part to hold appropriate
1232 SCSI SCRIPT(tm) plus a single, maximum-sized NCR53c7x0_cmd structure.
1234 We need a NCR53c7x0_cmd structure for scan_scsis() when we are
1235 not loaded as a module, and when we're loaded as a module, we
1236 can't use a non-dynamically allocated structure because modules
1237 are vmalloc()'d, which can allow structures to cross page
1238 boundaries and breaks our physical/virtual address assumptions
1239 for DMA.
1241 So, we stick it past the end of our hostdata structure.
1243 ASSUMPTION :
1244 Regardless of how many simultaneous SCSI commands we allow,
1245 the probe code only executes a _single_ instruction at a time,
1246 so we only need one here, and don't need to allocate NCR53c7x0_cmd
1247 structures for each target until we are no longer in scan_scsis
1248 and kmalloc() has become functional (memory_init() happens
1249 after all device driver initialization).
1252 size = sizeof(struct NCR53c7x0_hostdata) + script_len +
1253 /* Note that alignment will be guaranteed, since we put the command
1254 allocated at probe time after the fixed-up SCSI script, which
1255 consists of 32 bit words, aligned on a 32 bit boundary. But
1256 on a 64bit machine we need 8 byte alignment for hostdata->free, so
1257 we add in another 4 bytes to take care of potential misalignment
1259 (sizeof(void *) - sizeof(u32)) + max_cmd_size + schedule_size;
1261 instance = scsi_register (tpnt, size);
1262 if (!instance)
1263 return -1;
1265 /* FIXME : if we ever support an ISA NCR53c7xx based board, we
1266 need to check if the chip is running in a 16 bit mode, and if so
1267 unregister it if it is past the 16M (0x1000000) mark */
1269 hostdata = (struct NCR53c7x0_hostdata *)
1270 instance->hostdata;
1271 hostdata->size = size;
1272 hostdata->script_count = script_len / sizeof(u32);
1273 hostdata = (struct NCR53c7x0_hostdata *) instance->hostdata;
1274 hostdata->board = board;
1275 hostdata->chip = chip;
1276 if ((hostdata->pci_valid = pci_valid)) {
1277 hostdata->pci_bus = pci_bus;
1278 hostdata->pci_device_fn = pci_device_fn;
1282 * Being memory mapped is more desirable, since
1284 * - Memory accesses may be faster.
1286 * - The destination and source address spaces are the same for
1287 * all instructions, meaning we don't have to twiddle dmode or
1288 * any other registers.
1290 * So, we try for memory mapped, and if we don't get it,
1291 * we go for port mapped, and that failing we tell the user
1292 * it can't work.
1295 if (base) {
1296 instance->base = (unsigned long) base;
1297 /* Check for forced I/O mapping */
1298 if (!(options & OPTION_IO_MAPPED)) {
1299 options |= OPTION_MEMORY_MAPPED;
1300 ok = 1;
1302 } else {
1303 options &= ~OPTION_MEMORY_MAPPED;
1306 if (io_port) {
1307 instance->io_port = io_port;
1308 options |= OPTION_IO_MAPPED;
1309 ok = 1;
1310 } else {
1311 options &= ~OPTION_IO_MAPPED;
1314 if (!ok) {
1315 printk ("scsi%d : not initializing, no I/O or memory mapping known \n",
1316 instance->host_no);
1317 scsi_unregister (instance);
1318 return -1;
1320 instance->irq = irq;
1321 instance->dma_channel = dma;
1323 hostdata->options = options;
1324 hostdata->dsa_len = dsa_len;
1325 hostdata->max_cmd_size = max_cmd_size;
1326 hostdata->num_cmds = 1;
1327 /* Initialize single command */
1328 tmp = (hostdata->script + hostdata->script_count);
1329 hostdata->free = ROUNDUP(tmp, void *);
1330 hostdata->free->real = tmp;
1331 hostdata->free->size = max_cmd_size;
1332 hostdata->free->free = NULL;
1333 hostdata->free->next = NULL;
1334 hostdata->extra_allocate = 0;
1336 /* Allocate command start code space */
1337 hostdata->schedule = (chip == 700 || chip == 70066) ?
1338 NULL : (u32 *) ((char *)hostdata->free + max_cmd_size);
1341 * For diagnostic purposes, we don't really care how fast things blaze.
1342 * For profiling, we want to access the 800ns resolution system clock,
1343 * using a 'C' call on the host processor.
1345 * Therefore, there's no need for the NCR chip to directly manipulate
1346 * this data, and we should put it wherever is most convenient for
1347 * Linux.
1349 if (track_events)
1350 hostdata->events = (struct NCR53c7x0_event *) (track_events ?
1351 vmalloc (sizeof (struct NCR53c7x0_event) * track_events) : NULL);
1352 else
1353 hostdata->events = NULL;
1355 if (hostdata->events) {
1356 memset ((void *) hostdata->events, 0, sizeof(struct NCR53c7x0_event) *
1357 track_events);
1358 hostdata->event_size = track_events;
1359 hostdata->event_index = 0;
1360 } else
1361 hostdata->event_size = 0;
1363 return NCR53c7x0_init(instance);
1368 * Function : static int ncr_pci_init(Scsi_Host_Template *tpnt, int board,
1369 * int chip, int bus, int device_fn, long long options)
1371 * Purpose : initializes a NCR53c800 family based on the PCI
1372 * bus, device, and function location of it. Allows
1373 * reprogramming of latency timer and determining addresses
1374 * and whether bus mastering, etc. are OK.
1376 * Useful where a new NCR chip is backwards compatible with
1377 * a supported chip, but the DEVICE ID has changed so it
1378 * doesn't show up when the autoprobe does a pcibios_find_device.
1380 * Inputs : tpnt - Template for this SCSI adapter, board - board level
1381 * product, chip - 810, 820, or 825, bus - PCI bus, device_fn -
1382 * device and function encoding as used by PCI BIOS calls.
1384 * Returns : 0 on success, -1 on failure.
1388 static int __init
1389 ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip,
1390 unsigned char bus, unsigned char device_fn, long long options){
1391 unsigned short command;
1392 unsigned int base, io_port;
1393 unsigned char revision;
1394 int error, expected_chip;
1395 int expected_id = -1, max_revision = -1, min_revision = -1;
1396 int i, irq;
1397 struct pci_dev *pdev = pci_find_slot(bus, device_fn);
1399 printk("scsi-ncr53c7,8xx : at PCI bus %d, device %d, function %d\n",
1400 bus, (int) (device_fn & 0xf8) >> 3,
1401 (int) device_fn & 7);
1403 if (!pdev) {
1404 printk("scsi-ncr53c7,8xx : not initializing -- PCI device not found,\n"
1405 " try using memory, port, irq override instead.\n");
1406 return -1;
1409 if ((error = pci_read_config_word (pdev, PCI_COMMAND, &command)) ||
1410 (error = pci_read_config_byte (pdev, PCI_CLASS_REVISION, &revision))) {
1411 printk ("scsi-ncr53c7,8xx : error %d not initializing due to error reading configuration space\n"
1412 " perhaps you specified an incorrect PCI bus, device, or function.\n", error);
1413 return -1;
1415 if (pci_enable_device(pdev))
1416 return -1;
1417 io_port = pci_resource_start(pdev, 0);
1418 base = pci_resource_start(pdev, 1);
1419 irq = pdev->irq;
1421 /* If any one ever clones the NCR chips, this will have to change */
1423 if (pdev->vendor != PCI_VENDOR_ID_NCR) {
1424 printk ("scsi-ncr53c7,8xx : not initializing, 0x%04x is not NCR vendor ID\n",
1425 (int) pdev->vendor);
1426 return -1;
1429 #ifdef __powerpc__
1430 if ( ! (command & PCI_COMMAND_MASTER)) {
1431 printk("SCSI: PCI Master Bit has not been set. Setting...\n");
1432 command |= PCI_COMMAND_MASTER|PCI_COMMAND_IO;
1433 pci_write_config_word(pdev, PCI_COMMAND, command);
1435 if (io_port >= 0x10000000 && is_prep ) {
1436 /* Mapping on PowerPC can't handle this! */
1437 unsigned long new_io_port;
1438 new_io_port = (io_port & 0x00FFFFFF) | 0x01000000;
1439 printk("SCSI: I/O moved from %08X to %08x\n", io_port, new_io_port);
1440 io_port = new_io_port;
1441 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, io_port);
1442 pdev->base_address[0] = io_port;
1445 #endif
1448 * Bit 0 is the address space indicator and must be one for I/O
1449 * space mappings, bit 1 is reserved, discard them after checking
1450 * that they have the correct value of 1.
1453 if (command & PCI_COMMAND_IO) {
1454 if (!(pdev->resource[0].flags & IORESOURCE_IO)) {
1455 printk ("scsi-ncr53c7,8xx : disabling I/O mapping since base "
1456 "address 0\n contains a non-IO mapping\n");
1457 io_port = 0;
1459 } else {
1460 io_port = 0;
1463 if (command & PCI_COMMAND_MEMORY) {
1464 if (!(pdev->resource[1].flags & IORESOURCE_MEM)) {
1465 printk("scsi-ncr53c7,8xx : disabling memory mapping since base "
1466 "address 1\n contains a non-memory mapping\n");
1467 base = 0;
1469 } else {
1470 base = 0;
1473 if (!io_port && !base) {
1474 printk ("scsi-ncr53c7,8xx : not initializing, both I/O and memory mappings disabled\n");
1475 return -1;
1478 if (!(command & PCI_COMMAND_MASTER)) {
1479 printk ("scsi-ncr53c7,8xx : not initializing, BUS MASTERING was disabled\n");
1480 return -1;
1483 for (i = 0; i < NPCI_CHIP_IDS; ++i) {
1484 if (pdev->device == pci_chip_ids[i].pci_device_id) {
1485 max_revision = pci_chip_ids[i].max_revision;
1486 min_revision = pci_chip_ids[i].min_revision;
1487 expected_chip = pci_chip_ids[i].chip;
1489 if (chip == pci_chip_ids[i].chip)
1490 expected_id = pci_chip_ids[i].pci_device_id;
1493 if (chip && pdev->device != expected_id)
1494 printk ("scsi-ncr53c7,8xx : warning : device id of 0x%04x doesn't\n"
1495 " match expected 0x%04x\n",
1496 (unsigned int) pdev->device, (unsigned int) expected_id );
1498 if (max_revision != -1 && revision > max_revision)
1499 printk ("scsi-ncr53c7,8xx : warning : revision of %d is greater than %d.\n",
1500 (int) revision, max_revision);
1501 else if (min_revision != -1 && revision < min_revision)
1502 printk ("scsi-ncr53c7,8xx : warning : revision of %d is less than %d.\n",
1503 (int) revision, min_revision);
1505 if (io_port && check_region (io_port, 128)) {
1506 printk ("scsi-ncr53c7,8xx : IO region 0x%x to 0x%x is in use\n",
1507 (unsigned) io_port, (unsigned) io_port + 127);
1508 return -1;
1511 return normal_init (tpnt, board, chip, (int) base, io_port,
1512 (int) irq, DMA_NONE, 1, bus, device_fn, options);
1517 * Function : int NCR53c7xx_detect(Scsi_Host_Template *tpnt)
1519 * Purpose : detects and initializes NCR53c7,8x0 SCSI chips
1520 * that were autoprobed, overridden on the LILO command line,
1521 * or specified at compile time.
1523 * Inputs : tpnt - template for this SCSI adapter
1525 * Returns : number of host adapters detected
1529 int __init
1530 NCR53c7xx_detect(Scsi_Host_Template *tpnt){
1531 int i;
1532 int current_override;
1533 int count; /* Number of boards detected */
1534 unsigned char pci_bus, pci_device_fn;
1535 static short pci_index=0; /* Device index to PCI BIOS calls */
1537 tpnt->proc_name = "ncr53c7xx";
1539 for (current_override = count = 0; current_override < OVERRIDE_LIMIT;
1540 ++current_override) {
1541 if (overrides[current_override].pci ?
1542 !ncr_pci_init (tpnt, overrides[current_override].board,
1543 overrides[current_override].chip,
1544 (unsigned char) overrides[current_override].data.pci.bus,
1545 (((overrides[current_override].data.pci.device
1546 << 3) & 0xf8)|(overrides[current_override].data.pci.function &
1547 7)), overrides[current_override].options):
1548 !normal_init (tpnt, overrides[current_override].board,
1549 overrides[current_override].chip,
1550 overrides[current_override].data.normal.base,
1551 overrides[current_override].data.normal.io_port,
1552 overrides[current_override].data.normal.irq,
1553 overrides[current_override].data.normal.dma,
1554 0 /* PCI data invalid */, 0 /* PCI bus place holder */,
1555 0 /* PCI device_function place holder */,
1556 overrides[current_override].options)) {
1557 ++count;
1561 if (pci_present()) {
1562 for (i = 0; i < NPCI_CHIP_IDS; ++i)
1563 for (pci_index = 0;
1564 !pcibios_find_device (PCI_VENDOR_ID_NCR,
1565 pci_chip_ids[i].pci_device_id, pci_index, &pci_bus,
1566 &pci_device_fn);
1567 ++pci_index)
1568 if (!ncr_pci_init (tpnt, BOARD_GENERIC, pci_chip_ids[i].chip,
1569 pci_bus, pci_device_fn, /* no options */ 0))
1570 ++count;
1572 return count;
1575 /* NCR53c810 and NCR53c820 script handling code */
1577 #include "53c8xx_d.h"
1578 #ifdef A_int_debug_sync
1579 #define DEBUG_SYNC_INTR A_int_debug_sync
1580 #endif
1581 static int NCR53c8xx_script_len = sizeof (SCRIPT);
1582 static int NCR53c8xx_dsa_len = A_dsa_end + Ent_dsa_zero - Ent_dsa_code_template;
1585 * Function : static void NCR53c8x0_init_fixup (struct Scsi_Host *host)
1587 * Purpose : copy and fixup the SCSI SCRIPTS(tm) code for this device.
1589 * Inputs : host - pointer to this host adapter's structure
1593 static void
1594 NCR53c8x0_init_fixup (struct Scsi_Host *host) {
1595 NCR53c7x0_local_declare();
1596 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1597 host->hostdata;
1598 unsigned char tmp;
1599 int i, ncr_to_memory, memory_to_ncr;
1600 u32 base;
1601 #ifdef __powerpc__
1602 unsigned long *script_ptr;
1603 #endif
1604 NCR53c7x0_local_setup(host);
1607 /* XXX - NOTE : this code MUST be made endian aware */
1608 /* Copy code into buffer that was allocated at detection time. */
1609 memcpy ((void *) hostdata->script, (void *) SCRIPT,
1610 sizeof(SCRIPT));
1611 /* Fixup labels */
1612 for (i = 0; i < PATCHES; ++i)
1613 hostdata->script[LABELPATCHES[i]] +=
1614 virt_to_bus(hostdata->script);
1615 /* Fixup addresses of constants that used to be EXTERNAL */
1617 patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_abort,
1618 virt_to_bus(&(hostdata->NCR53c7xx_msg_abort)));
1619 patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_reject,
1620 virt_to_bus(&(hostdata->NCR53c7xx_msg_reject)));
1621 patch_abs_32 (hostdata->script, 0, NCR53c7xx_zero,
1622 virt_to_bus(&(hostdata->NCR53c7xx_zero)));
1623 patch_abs_32 (hostdata->script, 0, NCR53c7xx_sink,
1624 virt_to_bus(&(hostdata->NCR53c7xx_sink)));
1625 patch_abs_32 (hostdata->script, 0, NOP_insn,
1626 virt_to_bus(&(hostdata->NOP_insn)));
1627 patch_abs_32 (hostdata->script, 0, schedule,
1628 virt_to_bus((void *) hostdata->schedule));
1630 /* Fixup references to external variables: */
1631 for (i = 0; i < EXTERNAL_PATCHES_LEN; ++i)
1632 hostdata->script[EXTERNAL_PATCHES[i].offset] +=
1633 virt_to_bus(EXTERNAL_PATCHES[i].address);
1636 * Fixup absolutes set at boot-time.
1638 * All non-code absolute variables suffixed with "dsa_" and "int_"
1639 * are constants, and need no fixup provided the assembler has done
1640 * it for us (I don't know what the "real" NCR assembler does in
1641 * this case, my assembler does the right magic).
1644 patch_abs_rwri_data (hostdata->script, 0, dsa_save_data_pointer,
1645 Ent_dsa_code_save_data_pointer - Ent_dsa_zero);
1646 patch_abs_rwri_data (hostdata->script, 0, dsa_restore_pointers,
1647 Ent_dsa_code_restore_pointers - Ent_dsa_zero);
1648 patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
1649 Ent_dsa_code_check_reselect - Ent_dsa_zero);
1652 * Just for the hell of it, preserve the settings of
1653 * Burst Length and Enable Read Line bits from the DMODE
1654 * register. Make sure SCRIPTS start automagically.
1657 tmp = NCR53c7x0_read8(DMODE_REG_10);
1658 tmp &= (DMODE_800_ERL | DMODE_BL_MASK);
1660 if (!(hostdata->options & OPTION_MEMORY_MAPPED)) {
1661 base = (u32) host->io_port;
1662 memory_to_ncr = tmp|DMODE_800_DIOM;
1663 ncr_to_memory = tmp|DMODE_800_SIOM;
1664 } else {
1665 base = virt_to_bus((void *)host->base);
1666 memory_to_ncr = ncr_to_memory = tmp;
1669 patch_abs_32 (hostdata->script, 0, addr_scratch, base + SCRATCHA_REG_800);
1670 patch_abs_32 (hostdata->script, 0, addr_temp, base + TEMP_REG);
1673 * I needed some variables in the script to be accessible to
1674 * both the NCR chip and the host processor. For these variables,
1675 * I made the arbitrary decision to store them directly in the
1676 * hostdata structure rather than in the RELATIVE area of the
1677 * SCRIPTS.
1681 patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_memory, tmp);
1682 patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_ncr, memory_to_ncr);
1683 patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_memory, ncr_to_memory);
1685 patch_abs_32 (hostdata->script, 0, msg_buf,
1686 virt_to_bus((void *)&(hostdata->msg_buf)));
1687 patch_abs_32 (hostdata->script, 0, reconnect_dsa_head,
1688 virt_to_bus((void *)&(hostdata->reconnect_dsa_head)));
1689 patch_abs_32 (hostdata->script, 0, addr_reconnect_dsa_head,
1690 virt_to_bus((void *)&(hostdata->addr_reconnect_dsa_head)));
1691 patch_abs_32 (hostdata->script, 0, reselected_identify,
1692 virt_to_bus((void *)&(hostdata->reselected_identify)));
1693 /* reselected_tag is currently unused */
1694 #if 0
1695 patch_abs_32 (hostdata->script, 0, reselected_tag,
1696 virt_to_bus((void *)&(hostdata->reselected_tag)));
1697 #endif
1699 patch_abs_32 (hostdata->script, 0, test_dest,
1700 virt_to_bus((void*)&hostdata->test_dest));
1701 patch_abs_32 (hostdata->script, 0, test_src,
1702 virt_to_bus(&hostdata->test_source));
1704 patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
1705 (unsigned char)(Ent_dsa_code_check_reselect - Ent_dsa_zero));
1707 /* These are for event logging; the ncr_event enum contains the
1708 actual interrupt numbers. */
1709 #ifdef A_int_EVENT_SELECT
1710 patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT, (u32) EVENT_SELECT);
1711 #endif
1712 #ifdef A_int_EVENT_DISCONNECT
1713 patch_abs_32 (hostdata->script, 0, int_EVENT_DISCONNECT, (u32) EVENT_DISCONNECT);
1714 #endif
1715 #ifdef A_int_EVENT_RESELECT
1716 patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT, (u32) EVENT_RESELECT);
1717 #endif
1718 #ifdef A_int_EVENT_COMPLETE
1719 patch_abs_32 (hostdata->script, 0, int_EVENT_COMPLETE, (u32) EVENT_COMPLETE);
1720 #endif
1721 #ifdef A_int_EVENT_IDLE
1722 patch_abs_32 (hostdata->script, 0, int_EVENT_IDLE, (u32) EVENT_IDLE);
1723 #endif
1724 #ifdef A_int_EVENT_SELECT_FAILED
1725 patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT_FAILED,
1726 (u32) EVENT_SELECT_FAILED);
1727 #endif
1728 #ifdef A_int_EVENT_BEFORE_SELECT
1729 patch_abs_32 (hostdata->script, 0, int_EVENT_BEFORE_SELECT,
1730 (u32) EVENT_BEFORE_SELECT);
1731 #endif
1732 #ifdef A_int_EVENT_RESELECT_FAILED
1733 patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT_FAILED,
1734 (u32) EVENT_RESELECT_FAILED);
1735 #endif
1738 * Make sure the NCR and Linux code agree on the location of
1739 * certain fields.
1742 hostdata->E_accept_message = Ent_accept_message;
1743 hostdata->E_command_complete = Ent_command_complete;
1744 hostdata->E_cmdout_cmdout = Ent_cmdout_cmdout;
1745 hostdata->E_data_transfer = Ent_data_transfer;
1746 hostdata->E_debug_break = Ent_debug_break;
1747 hostdata->E_dsa_code_template = Ent_dsa_code_template;
1748 hostdata->E_dsa_code_template_end = Ent_dsa_code_template_end;
1749 hostdata->E_end_data_transfer = Ent_end_data_transfer;
1750 hostdata->E_initiator_abort = Ent_initiator_abort;
1751 hostdata->E_msg_in = Ent_msg_in;
1752 hostdata->E_other_transfer = Ent_other_transfer;
1753 hostdata->E_other_in = Ent_other_in;
1754 hostdata->E_other_out = Ent_other_out;
1755 hostdata->E_reject_message = Ent_reject_message;
1756 hostdata->E_respond_message = Ent_respond_message;
1757 hostdata->E_select = Ent_select;
1758 hostdata->E_select_msgout = Ent_select_msgout;
1759 hostdata->E_target_abort = Ent_target_abort;
1760 #ifdef Ent_test_0
1761 hostdata->E_test_0 = Ent_test_0;
1762 #endif
1763 hostdata->E_test_1 = Ent_test_1;
1764 hostdata->E_test_2 = Ent_test_2;
1765 #ifdef Ent_test_3
1766 hostdata->E_test_3 = Ent_test_3;
1767 #endif
1768 hostdata->E_wait_reselect = Ent_wait_reselect;
1769 hostdata->E_dsa_code_begin = Ent_dsa_code_begin;
1771 hostdata->dsa_cmdout = A_dsa_cmdout;
1772 hostdata->dsa_cmnd = A_dsa_cmnd;
1773 hostdata->dsa_datain = A_dsa_datain;
1774 hostdata->dsa_dataout = A_dsa_dataout;
1775 hostdata->dsa_end = A_dsa_end;
1776 hostdata->dsa_msgin = A_dsa_msgin;
1777 hostdata->dsa_msgout = A_dsa_msgout;
1778 hostdata->dsa_msgout_other = A_dsa_msgout_other;
1779 hostdata->dsa_next = A_dsa_next;
1780 hostdata->dsa_select = A_dsa_select;
1781 hostdata->dsa_start = Ent_dsa_code_template - Ent_dsa_zero;
1782 hostdata->dsa_status = A_dsa_status;
1783 hostdata->dsa_jump_dest = Ent_dsa_code_fix_jump - Ent_dsa_zero +
1784 8 /* destination operand */;
1786 /* sanity check */
1787 if (A_dsa_fields_start != Ent_dsa_code_template_end -
1788 Ent_dsa_zero)
1789 printk("scsi%d : NCR dsa_fields start is %d not %d\n",
1790 host->host_no, A_dsa_fields_start, Ent_dsa_code_template_end -
1791 Ent_dsa_zero);
1792 #ifdef __powerpc__
1793 /* The PowerPC is Big Endian - adjust script appropriately */
1794 script_ptr = hostdata->script;
1795 for (i = 0; i < sizeof(SCRIPT); i += sizeof(long))
1797 *script_ptr++ = le32_to_cpu(*script_ptr);
1799 #endif
1801 printk("scsi%d : NCR code relocated to 0x%lx (virt 0x%p)\n", host->host_no,
1802 virt_to_bus(hostdata->script), hostdata->script);
1806 * Function : static int NCR53c8xx_run_tests (struct Scsi_Host *host)
1808 * Purpose : run various verification tests on the NCR chip,
1809 * including interrupt generation, and proper bus mastering
1810 * operation.
1812 * Inputs : host - a properly initialized Scsi_Host structure
1814 * Preconditions : the NCR chip must be in a halted state.
1816 * Returns : 0 if all tests were successful, -1 on error.
1820 static int
1821 NCR53c8xx_run_tests (struct Scsi_Host *host) {
1822 NCR53c7x0_local_declare();
1823 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1824 host->hostdata;
1825 unsigned long timeout;
1826 u32 start;
1827 int failed, i;
1828 unsigned long flags;
1829 NCR53c7x0_local_setup(host);
1831 /* The NCR chip _must_ be idle to run the test scripts */
1833 save_flags(flags);
1834 cli();
1835 if (!hostdata->idle) {
1836 printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
1837 restore_flags(flags);
1838 return -1;
1842 * Check for functional interrupts, this could work as an
1843 * autoprobe routine.
1846 if ((hostdata->options & OPTION_DEBUG_TEST1) &&
1847 hostdata->state != STATE_DISABLED) {
1848 hostdata->idle = 0;
1849 hostdata->test_running = 1;
1850 hostdata->test_completed = -1;
1851 hostdata->test_dest = 0;
1852 hostdata->test_source = 0xdeadbeef;
1853 start = virt_to_bus (hostdata->script) + hostdata->E_test_1;
1854 hostdata->state = STATE_RUNNING;
1855 printk ("scsi%d : test 1", host->host_no);
1856 NCR53c7x0_write32 (DSP_REG, start);
1857 printk (" started\n");
1858 restore_flags(flags);
1861 * This is currently a .5 second timeout, since (in theory) no slow
1862 * board will take that long. In practice, we've seen one
1863 * pentium which occasionally fails with this, but works with
1864 * 10 times as much?
1867 timeout = jiffies + 5 * HZ / 10;
1868 while ((hostdata->test_completed == -1) && jiffies < timeout)
1869 barrier();
1871 failed = 1;
1872 if (hostdata->test_completed == -1)
1873 printk ("scsi%d : driver test 1 timed out%s\n",host->host_no ,
1874 (hostdata->test_dest == 0xdeadbeef) ?
1875 " due to lost interrupt.\n"
1876 " Please verify that the correct IRQ is being used for your board,\n"
1877 " and that the motherboard IRQ jumpering matches the PCI setup on\n"
1878 " PCI systems.\n"
1879 " If you are using a NCR53c810 board in a PCI system, you should\n"
1880 " also verify that the board is jumpered to use PCI INTA, since\n"
1881 " most PCI motherboards lack support for INTB, INTC, and INTD.\n"
1882 : "");
1883 else if (hostdata->test_completed != 1)
1884 printk ("scsi%d : test 1 bad interrupt value (%d)\n",
1885 host->host_no, hostdata->test_completed);
1886 else
1887 failed = (hostdata->test_dest != 0xdeadbeef);
1889 if (hostdata->test_dest != 0xdeadbeef) {
1890 printk ("scsi%d : driver test 1 read 0x%x instead of 0xdeadbeef indicating a\n"
1891 " probable cache invalidation problem. Please configure caching\n"
1892 " as write-through or disabled\n",
1893 host->host_no, hostdata->test_dest);
1896 if (failed) {
1897 printk ("scsi%d : DSP = 0x%p (script at 0x%p, start at 0x%x)\n",
1898 host->host_no, bus_to_virt(NCR53c7x0_read32(DSP_REG)),
1899 hostdata->script, start);
1900 printk ("scsi%d : DSPS = 0x%x\n", host->host_no,
1901 NCR53c7x0_read32(DSPS_REG));
1902 restore_flags(flags);
1903 return -1;
1905 hostdata->test_running = 0;
1908 if ((hostdata->options & OPTION_DEBUG_TEST2) &&
1909 hostdata->state != STATE_DISABLED) {
1910 u32 dsa[48];
1911 unsigned char identify = IDENTIFY(0, 0);
1912 unsigned char cmd[6];
1913 unsigned char data[36];
1914 unsigned char status = 0xff;
1915 unsigned char msg = 0xff;
1917 cmd[0] = INQUIRY;
1918 cmd[1] = cmd[2] = cmd[3] = cmd[5] = 0;
1919 cmd[4] = sizeof(data);
1921 /* Need to adjust for endian-ness */
1922 dsa[2] = le32_to_cpu(1);
1923 dsa[3] = le32_to_cpu(virt_to_bus(&identify));
1924 dsa[4] = le32_to_cpu(6);
1925 dsa[5] = le32_to_cpu(virt_to_bus(&cmd));
1926 dsa[6] = le32_to_cpu(sizeof(data));
1927 dsa[7] = le32_to_cpu(virt_to_bus(&data));
1928 dsa[8] = le32_to_cpu(1);
1929 dsa[9] = le32_to_cpu(virt_to_bus(&status));
1930 dsa[10] = le32_to_cpu(1);
1931 dsa[11] = le32_to_cpu(virt_to_bus(&msg));
1933 for (i = 0; i < 3; ++i) {
1934 cli();
1935 if (!hostdata->idle) {
1936 printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
1937 restore_flags(flags);
1938 return -1;
1941 /* SCNTL3 SDID */
1942 dsa[0] = le32_to_cpu((0x33 << 24) | (i << 16)) ;
1943 hostdata->idle = 0;
1944 hostdata->test_running = 2;
1945 hostdata->test_completed = -1;
1946 start = virt_to_bus(hostdata->script) + hostdata->E_test_2;
1947 hostdata->state = STATE_RUNNING;
1948 NCR53c7x0_write32 (DSA_REG, virt_to_bus(dsa));
1949 NCR53c7x0_write32 (DSP_REG, start);
1950 restore_flags(flags);
1952 timeout = jiffies + 5 * HZ; /* arbitrary */
1953 while ((hostdata->test_completed == -1) && jiffies < timeout)
1954 barrier();
1955 NCR53c7x0_write32 (DSA_REG, 0);
1957 if (hostdata->test_completed == 2) {
1958 data[35] = 0;
1959 printk ("scsi%d : test 2 INQUIRY to target %d, lun 0 : %s\n",
1960 host->host_no, i, data + 8);
1961 printk ("scsi%d : status ", host->host_no);
1962 print_status (status);
1963 printk ("\nscsi%d : message ", host->host_no);
1964 print_msg (&msg);
1965 printk ("\n");
1966 } else if (hostdata->test_completed == 3) {
1967 printk("scsi%d : test 2 no connection with target %d\n",
1968 host->host_no, i);
1969 if (!hostdata->idle) {
1970 printk("scsi%d : not idle\n", host->host_no);
1971 restore_flags(flags);
1972 return -1;
1974 } else if (hostdata->test_completed == -1) {
1975 printk ("scsi%d : test 2 timed out\n", host->host_no);
1976 restore_flags(flags);
1977 return -1;
1979 hostdata->test_running = 0;
1983 restore_flags(flags);
1984 return 0;
1988 * Function : static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd)
1990 * Purpose : copy the NCR53c8xx dsa structure into cmd's dsa buffer,
1991 * performing all necessary relocation.
1993 * Inputs : cmd, a NCR53c7x0_cmd structure with a dsa area large
1994 * enough to hold the NCR53c8xx dsa.
1997 static void
1998 NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
1999 Scsi_Cmnd *c = cmd->cmd;
2000 struct Scsi_Host *host = c->host;
2001 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2002 host->hostdata;
2003 int i;
2004 #ifdef __powerpc__
2005 int len;
2006 unsigned long *dsa_ptr;
2007 #endif
2009 memcpy (cmd->dsa, hostdata->script + (hostdata->E_dsa_code_template / 4),
2010 hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template);
2011 #ifdef __powerpc__
2012 /* Note: the script has already been 'endianized' */
2013 dsa_ptr = cmd->dsa;
2014 len = hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template;
2015 for (i = 0; i < len; i += sizeof(long))
2017 *dsa_ptr++ = le32_to_cpu(*dsa_ptr);
2019 #endif
2022 * Note : within the NCR 'C' code, dsa points to the _start_
2023 * of the DSA structure, and _not_ the offset of dsa_zero within
2024 * that structure used to facilitate shorter signed offsets
2025 * for the 8 bit ALU.
2027 * The implications of this are that
2029 * - 32 bit A_dsa_* absolute values require an additional
2030 * dsa_zero added to their value to be correct, since they are
2031 * relative to dsa_zero which is in essentially a separate
2032 * space from the code symbols.
2034 * - All other symbols require no special treatment.
2037 patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2038 dsa_temp_lun, c->lun);
2039 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2040 dsa_temp_addr_next, virt_to_bus(&cmd->dsa_next_addr));
2041 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2042 dsa_temp_next, virt_to_bus(cmd->dsa) + Ent_dsa_zero -
2043 Ent_dsa_code_template + A_dsa_next);
2044 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2045 dsa_temp_sync, virt_to_bus((void *)hostdata->sync[c->target].script));
2046 patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2047 dsa_temp_target, c->target);
2048 /* XXX - new pointer stuff */
2049 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2050 dsa_temp_addr_saved_pointer, virt_to_bus(&cmd->saved_data_pointer));
2051 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2052 dsa_temp_addr_saved_residual, virt_to_bus(&cmd->saved_residual));
2053 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2054 dsa_temp_addr_residual, virt_to_bus(&cmd->residual));
2056 /* XXX - new start stuff */
2057 patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2058 dsa_temp_addr_dsa_value, virt_to_bus(&cmd->dsa_addr));
2059 #ifdef __powerpc__
2060 dsa_ptr = cmd->dsa;
2061 len = hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template;
2062 for (i = 0; i < len; i += sizeof(long))
2064 *dsa_ptr++ = le32_to_cpu(*dsa_ptr);
2066 #endif
2071 * Function : run_process_issue_queue (void)
2073 * Purpose : insure that the coroutine is running and will process our
2074 * request. process_issue_queue_running is checked/set here (in an
2075 * inline function) rather than in process_issue_queue itself to reduce
2076 * the chances of stack overflow.
2080 static volatile int process_issue_queue_running = 0;
2082 static __inline__ void
2083 run_process_issue_queue(void) {
2084 unsigned long flags;
2085 save_flags (flags);
2086 cli();
2087 if (!process_issue_queue_running) {
2088 process_issue_queue_running = 1;
2089 process_issue_queue(flags);
2091 * process_issue_queue_running is cleared in process_issue_queue
2092 * once it can't do more work, and process_issue_queue exits with
2093 * interrupts disabled.
2096 restore_flags (flags);
2100 * Function : static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int
2101 * result)
2103 * Purpose : mark SCSI command as finished, OR'ing the host portion
2104 * of the result word into the result field of the corresponding
2105 * Scsi_Cmnd structure, and removing it from the internal queues.
2107 * Inputs : cmd - command, result - entire result field
2109 * Preconditions : the NCR chip should be in a halted state when
2110 * abnormal_finished is run, since it modifies structures which
2111 * the NCR expects to have exclusive access to.
2114 static void
2115 abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
2116 Scsi_Cmnd *c = cmd->cmd;
2117 struct Scsi_Host *host = c->host;
2118 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2119 host->hostdata;
2120 unsigned long flags;
2121 int left, found;
2122 volatile struct NCR53c7x0_cmd * linux_search;
2123 volatile struct NCR53c7x0_cmd * volatile *linux_prev;
2124 volatile u32 *ncr_prev, *curr, ncr_search;
2126 #if 0
2127 printk ("scsi%d: abnormal finished\n", host->host_no);
2128 #endif
2130 save_flags(flags);
2131 cli();
2132 found = 0;
2134 * Traverse the NCR issue array until we find a match or run out
2135 * of instructions. Instructions in the NCR issue array are
2136 * either JUMP or NOP instructions, which are 2 words in length.
2140 for (found = 0, left = host->can_queue, curr = hostdata->schedule;
2141 left > 0; --left, curr += 2)
2143 if (issue_to_cmd (host, hostdata, (u32 *) curr) == cmd)
2145 curr[0] = hostdata->NOP_insn;
2146 curr[1] = le32_to_cpu(0xdeadbeef);
2147 ++found;
2148 break;
2153 * Traverse the NCR reconnect list of DSA structures until we find
2154 * a pointer to this dsa or have found too many command structures.
2155 * We let prev point at the next field of the previous element or
2156 * head of the list, so we don't do anything different for removing
2157 * the head element.
2160 for (left = host->can_queue,
2161 ncr_search = le32_to_cpu(hostdata->reconnect_dsa_head),
2162 ncr_prev = &hostdata->reconnect_dsa_head;
2163 left >= 0 && ncr_search &&
2164 ((char*)bus_to_virt(ncr_search) + hostdata->dsa_start)
2165 != (char *) cmd->dsa;
2166 ncr_prev = (u32*) ((char*)bus_to_virt(ncr_search) +
2167 hostdata->dsa_next), ncr_search = le32_to_cpu(*ncr_prev), --left);
2169 if (left < 0) {
2170 printk("scsi%d: loop detected in ncr reconnect list\n",
2171 host->host_no);
2172 } else if (ncr_search) {
2173 if (found)
2174 printk("scsi%d: scsi %ld in ncr issue array and reconnect lists\n",
2175 host->host_no, c->pid);
2176 else {
2177 volatile u32 * next = (u32 *)
2178 ((char *)bus_to_virt(ncr_search) + hostdata->dsa_next);
2179 *ncr_prev = *next;
2180 /* If we're at the tail end of the issue queue, update that pointer too. */
2181 found = 1;
2186 * Traverse the host running list until we find this command or discover
2187 * we have too many elements, pointing linux_prev at the next field of the
2188 * linux_previous element or head of the list, search at this element.
2191 for (left = host->can_queue, linux_search = hostdata->running_list,
2192 linux_prev = &hostdata->running_list;
2193 left >= 0 && linux_search && linux_search != cmd;
2194 linux_prev = &(linux_search->next),
2195 linux_search = linux_search->next, --left);
2197 if (left < 0)
2198 printk ("scsi%d: loop detected in host running list for scsi pid %ld\n",
2199 host->host_no, c->pid);
2200 else if (linux_search) {
2201 *linux_prev = linux_search->next;
2202 --hostdata->busy[c->target][c->lun];
2205 /* Return the NCR command structure to the free list */
2206 cmd->next = hostdata->free;
2207 hostdata->free = cmd;
2208 c->host_scribble = NULL;
2210 /* And return */
2211 c->result = result;
2212 c->scsi_done(c);
2214 restore_flags(flags);
2215 run_process_issue_queue();
2219 * Function : static void intr_break (struct Scsi_Host *host,
2220 * struct NCR53c7x0_cmd *cmd)
2222 * Purpose : Handler for breakpoint interrupts from a SCSI script
2224 * Inputs : host - pointer to this host adapter's structure,
2225 * cmd - pointer to the command (if any) dsa was pointing
2226 * to.
2230 static void
2231 intr_break (struct Scsi_Host *host, struct
2232 NCR53c7x0_cmd *cmd) {
2233 NCR53c7x0_local_declare();
2234 struct NCR53c7x0_break *bp;
2235 #if 0
2236 Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
2237 #endif
2238 u32 *dsp;
2239 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2240 host->hostdata;
2241 unsigned long flags;
2242 NCR53c7x0_local_setup(host);
2245 * Find the break point corresponding to this address, and
2246 * dump the appropriate debugging information to standard
2247 * output.
2249 save_flags(flags);
2250 cli();
2251 dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
2252 for (bp = hostdata->breakpoints; bp && bp->address != dsp;
2253 bp = bp->next);
2254 if (!bp)
2255 panic("scsi%d : break point interrupt from %p with no breakpoint!",
2256 host->host_no, dsp);
2259 * Configure the NCR chip for manual start mode, so that we can
2260 * point the DSP register at the instruction that follows the
2261 * INT int_debug_break instruction.
2264 NCR53c7x0_write8 (hostdata->dmode,
2265 NCR53c7x0_read8(hostdata->dmode)|DMODE_MAN);
2268 * And update the DSP register, using the size of the old
2269 * instruction in bytes.
2272 restore_flags(flags);
2275 * Function : static void print_synchronous (const char *prefix,
2276 * const unsigned char *msg)
2278 * Purpose : print a pretty, user and machine parsable representation
2279 * of a SDTR message, including the "real" parameters, data
2280 * clock so we can tell transfer rate at a glance.
2282 * Inputs ; prefix - text to prepend, msg - SDTR message (5 bytes)
2285 static void
2286 print_synchronous (const char *prefix, const unsigned char *msg) {
2287 if (msg[4]) {
2288 int Hz = 1000000000 / (msg[3] * 4);
2289 int integer = Hz / 1000000;
2290 int fraction = (Hz - (integer * 1000000)) / 10000;
2291 printk ("%speriod %dns offset %d %d.%02dMHz %s SCSI%s\n",
2292 prefix, (int) msg[3] * 4, (int) msg[4], integer, fraction,
2293 (((msg[3] * 4) < 200) ? "FAST" : "synchronous"),
2294 (((msg[3] * 4) < 200) ? "-II" : ""));
2295 } else
2296 printk ("%sasynchronous SCSI\n", prefix);
2300 * Function : static void set_synchronous (struct Scsi_Host *host,
2301 * int target, int sxfer, int scntl3, int now_connected)
2303 * Purpose : reprogram transfers between the selected SCSI initiator and
2304 * target with the given register values; in the indirect
2305 * select operand, reselection script, and chip registers.
2307 * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
2308 * sxfer and scntl3 - NCR registers. now_connected - if non-zero,
2309 * we should reprogram the registers now too.
2312 static void
2313 set_synchronous (struct Scsi_Host *host, int target, int sxfer, int scntl3,
2314 int now_connected) {
2315 NCR53c7x0_local_declare();
2316 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2317 host->hostdata;
2318 u32 *script;
2319 NCR53c7x0_local_setup(host);
2321 /* These are eight bit registers */
2322 sxfer &= 0xff;
2323 scntl3 &= 0xff;
2325 hostdata->sync[target].sxfer_sanity = sxfer;
2326 hostdata->sync[target].scntl3_sanity = scntl3;
2329 * HARD CODED : synchronous script is EIGHT words long. This
2330 * must agree with 53c7.8xx.h
2333 if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
2334 hostdata->sync[target].select_indirect = (scntl3 << 24) |
2335 (target << 16) | (sxfer << 8);
2337 script = (u32 *) hostdata->sync[target].script;
2339 /* XXX - add NCR53c7x0 code to reprogram SCF bits if we want to */
2340 if ((hostdata->chip / 100) == 8) {
2341 script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
2342 DCMD_RWRI_OP_MOVE) << 24) |
2343 (SCNTL3_REG_800 << 16) | (scntl3 << 8);
2344 script[1] = 0;
2345 script += 2;
2348 script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
2349 DCMD_RWRI_OP_MOVE) << 24) |
2350 (SXFER_REG << 16) | (sxfer << 8);
2351 script[1] = 0;
2352 script += 2;
2354 #ifdef DEBUG_SYNC_INTR
2355 if (hostdata->options & OPTION_DEBUG_DISCONNECT) {
2356 script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_INT) << 24) | DBC_TCI_TRUE;
2357 script[1] = DEBUG_SYNC_INTR;
2358 script += 2;
2360 #endif
2362 script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24) | DBC_TCI_TRUE;
2363 script[1] = 0;
2364 script += 2;
2367 if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS)
2368 printk ("scsi%d : target %d sync parameters are sxfer=0x%x, scntl3=0x%x\n",
2369 host->host_no, target, sxfer, scntl3);
2371 if (now_connected) {
2372 if ((hostdata->chip / 100) == 8)
2373 NCR53c7x0_write8(SCNTL3_REG_800, scntl3);
2374 NCR53c7x0_write8(SXFER_REG, sxfer);
2380 * Function : static int asynchronous (struct Scsi_Host *host, int target)
2382 * Purpose : reprogram between the selected SCSI Host adapter and target
2383 * (assumed to be currently connected) for asynchronous transfers.
2385 * Inputs : host - SCSI host structure, target - numeric target ID.
2387 * Preconditions : the NCR chip should be in one of the halted states
2390 static void
2391 asynchronous (struct Scsi_Host *host, int target) {
2392 NCR53c7x0_local_declare();
2393 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2394 host->hostdata;
2395 NCR53c7x0_local_setup(host);
2396 set_synchronous (host, target, /* no offset */ 0, hostdata->saved_scntl3,
2398 printk ("scsi%d : setting target %d to asynchronous SCSI\n",
2399 host->host_no, target);
2403 * XXX - do we want to go out of our way (ie, add extra code to selection
2404 * in the NCR53c710/NCR53c720 script) to reprogram the synchronous
2405 * conversion bits, or can we be content in just setting the
2406 * sxfer bits?
2409 /* Table for NCR53c8xx synchronous values */
2410 static const struct {
2411 int div; /* Total clock divisor * 10 */
2412 unsigned char scf; /* */
2413 unsigned char tp; /* 4 + tp = xferp divisor */
2414 } syncs[] = {
2415 /* div scf tp div scf tp div scf tp */
2416 { 40, 1, 0}, { 50, 1, 1}, { 60, 1, 2},
2417 { 70, 1, 3}, { 75, 2, 1}, { 80, 1, 4},
2418 { 90, 1, 5}, { 100, 1, 6}, { 105, 2, 3},
2419 { 110, 1, 7}, { 120, 2, 4}, { 135, 2, 5},
2420 { 140, 3, 3}, { 150, 2, 6}, { 160, 3, 4},
2421 { 165, 2, 7}, { 180, 3, 5}, { 200, 3, 6},
2422 { 210, 4, 3}, { 220, 3, 7}, { 240, 4, 4},
2423 { 270, 4, 5}, { 300, 4, 6}, { 330, 4, 7}
2427 * Function : static void synchronous (struct Scsi_Host *host, int target,
2428 * char *msg)
2430 * Purpose : reprogram transfers between the selected SCSI initiator and
2431 * target for synchronous SCSI transfers such that the synchronous
2432 * offset is less than that requested and period at least as long
2433 * as that requested. Also modify *msg such that it contains
2434 * an appropriate response.
2436 * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
2437 * msg - synchronous transfer request.
2441 static void
2442 synchronous (struct Scsi_Host *host, int target, char *msg) {
2443 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2444 host->hostdata;
2445 int desire, divisor, i, limit;
2446 unsigned char scntl3, sxfer;
2447 /* The diagnostic message fits on one line, even with max. width integers */
2448 char buf[80];
2450 /* Desired transfer clock in Hz */
2451 desire = 1000000000L / (msg[3] * 4);
2452 /* Scale the available SCSI clock by 10 so we get tenths */
2453 divisor = (hostdata->scsi_clock * 10) / desire;
2455 /* NCR chips can handle at most an offset of 8 */
2456 if (msg[4] > 8)
2457 msg[4] = 8;
2459 if (hostdata->options & OPTION_DEBUG_SDTR)
2460 printk("scsi%d : optimal synchronous divisor of %d.%01d\n",
2461 host->host_no, divisor / 10, divisor % 10);
2463 limit = (sizeof(syncs) / sizeof(syncs[0]) -1);
2464 for (i = 0; (i < limit) && (divisor > syncs[i].div); ++i);
2466 if (hostdata->options & OPTION_DEBUG_SDTR)
2467 printk("scsi%d : selected synchronous divisor of %d.%01d\n",
2468 host->host_no, syncs[i].div / 10, syncs[i].div % 10);
2470 msg[3] = ((1000000000L / hostdata->scsi_clock) * syncs[i].div / 10 / 4);
2472 if (hostdata->options & OPTION_DEBUG_SDTR)
2473 printk("scsi%d : selected synchronous period of %dns\n", host->host_no,
2474 msg[3] * 4);
2476 scntl3 = (hostdata->chip / 100 == 8) ? ((hostdata->saved_scntl3 &
2477 ~SCNTL3_800_SCF_MASK) | (syncs[i].scf << SCNTL3_800_SCF_SHIFT)) : 0;
2478 sxfer = (msg[4] << SXFER_MO_SHIFT) | ((syncs[i].tp) << SXFER_TP_SHIFT);
2479 if (hostdata->options & OPTION_DEBUG_SDTR)
2480 printk ("scsi%d : sxfer=0x%x scntl3=0x%x\n",
2481 host->host_no, (int) sxfer, (int) scntl3);
2482 set_synchronous (host, target, sxfer, scntl3, 1);
2483 sprintf (buf, "scsi%d : setting target %d to ", host->host_no, target);
2484 print_synchronous (buf, msg);
2488 * Function : static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host,
2489 * struct NCR53c7x0_cmd *cmd)
2491 * Purpose : Handler for INT generated instructions for the
2492 * NCR53c810/820 SCSI SCRIPT
2494 * Inputs : host - pointer to this host adapter's structure,
2495 * cmd - pointer to the command (if any) dsa was pointing
2496 * to.
2500 static int
2501 NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct
2502 NCR53c7x0_cmd *cmd) {
2503 NCR53c7x0_local_declare();
2504 int print;
2505 Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
2506 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2507 host->hostdata;
2508 u32 dsps,*dsp; /* Argument of the INT instruction */
2509 NCR53c7x0_local_setup(host);
2510 dsps = NCR53c7x0_read32(DSPS_REG);
2511 dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
2513 if (hostdata->options & OPTION_DEBUG_INTR)
2514 printk ("scsi%d : DSPS = 0x%x\n", host->host_no, dsps);
2516 switch (dsps) {
2517 case A_int_msg_1:
2518 print = 1;
2519 switch (hostdata->msg_buf[0]) {
2521 * Unless we've initiated synchronous negotiation, I don't
2522 * think that this should happen.
2524 case MESSAGE_REJECT:
2525 hostdata->dsp = hostdata->script + hostdata->E_accept_message /
2526 sizeof(u32);
2527 hostdata->dsp_changed = 1;
2528 if (cmd && (cmd->flags & CMD_FLAG_SDTR)) {
2529 printk ("scsi%d : target %d rejected SDTR\n", host->host_no,
2530 c->target);
2531 cmd->flags &= ~CMD_FLAG_SDTR;
2532 asynchronous (host, c->target);
2533 print = 0;
2535 break;
2536 case INITIATE_RECOVERY:
2537 printk ("scsi%d : extended contingent allegiance not supported yet, rejecting\n",
2538 host->host_no);
2539 /* Fall through to default */
2540 hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2541 sizeof(u32);
2542 hostdata->dsp_changed = 1;
2543 break;
2544 default:
2545 printk ("scsi%d : unsupported message, rejecting\n",
2546 host->host_no);
2547 hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2548 sizeof(u32);
2549 hostdata->dsp_changed = 1;
2551 if (print) {
2552 printk ("scsi%d : received message", host->host_no);
2553 if (c)
2554 printk (" from target %d lun %d ", c->target, c->lun);
2555 print_msg ((unsigned char *) hostdata->msg_buf);
2556 printk("\n");
2559 return SPECIFIC_INT_NOTHING;
2562 case A_int_msg_sdtr:
2564 * At this point, hostdata->msg_buf contains
2565 * 0 EXTENDED MESSAGE
2566 * 1 length
2567 * 2 SDTR
2568 * 3 period * 4ns
2569 * 4 offset
2572 if (cmd) {
2573 char buf[80];
2574 sprintf (buf, "scsi%d : target %d %s ", host->host_no, c->target,
2575 (cmd->flags & CMD_FLAG_SDTR) ? "accepting" : "requesting");
2576 print_synchronous (buf, (unsigned char *) hostdata->msg_buf);
2579 * Initiator initiated, won't happen unless synchronous
2580 * transfers are enabled. If we get a SDTR message in
2581 * response to our SDTR, we should program our parameters
2582 * such that
2583 * offset <= requested offset
2584 * period >= requested period
2586 if (cmd->flags & CMD_FLAG_SDTR) {
2587 cmd->flags &= ~CMD_FLAG_SDTR;
2588 if (hostdata->msg_buf[4])
2589 synchronous (host, c->target, (unsigned char *)
2590 hostdata->msg_buf);
2591 else
2592 asynchronous (host, c->target);
2593 hostdata->dsp = hostdata->script + hostdata->E_accept_message /
2594 sizeof(u32);
2595 hostdata->dsp_changed = 1;
2596 return SPECIFIC_INT_NOTHING;
2597 } else {
2598 if (hostdata->options & OPTION_SYNCHRONOUS) {
2599 cmd->flags |= CMD_FLAG_DID_SDTR;
2600 synchronous (host, c->target, (unsigned char *)
2601 hostdata->msg_buf);
2602 } else {
2603 hostdata->msg_buf[4] = 0; /* 0 offset = async */
2604 asynchronous (host, c->target);
2606 patch_dsa_32 (cmd->dsa, dsa_msgout_other, 0, le32_to_cpu(5));
2607 patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, (u32)
2608 le32_to_cpu(virt_to_bus ((void *)&hostdata->msg_buf)));
2609 hostdata->dsp = hostdata->script +
2610 hostdata->E_respond_message / sizeof(u32);
2611 hostdata->dsp_changed = 1;
2613 return SPECIFIC_INT_NOTHING;
2615 /* Fall through to abort if we couldn't find a cmd, and
2616 therefore a dsa structure to twiddle */
2617 case A_int_msg_wdtr:
2618 hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2619 sizeof(u32);
2620 hostdata->dsp_changed = 1;
2621 return SPECIFIC_INT_NOTHING;
2622 case A_int_err_unexpected_phase:
2623 if (hostdata->options & OPTION_DEBUG_INTR)
2624 printk ("scsi%d : unexpected phase\n", host->host_no);
2625 return SPECIFIC_INT_ABORT;
2626 case A_int_err_selected:
2627 printk ("scsi%d : selected by target %d\n", host->host_no,
2628 (int) NCR53c7x0_read8(SDID_REG_800) &7);
2629 hostdata->dsp = hostdata->script + hostdata->E_target_abort /
2630 sizeof(u32);
2631 hostdata->dsp_changed = 1;
2632 return SPECIFIC_INT_NOTHING;
2633 case A_int_err_unexpected_reselect:
2634 printk ("scsi%d : unexpected reselect by target %d lun %d\n",
2635 host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & 7,
2636 hostdata->reselected_identify & 7);
2637 hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
2638 sizeof(u32);
2639 hostdata->dsp_changed = 1;
2640 return SPECIFIC_INT_NOTHING;
2642 * Since contingent allegiance conditions are cleared by the next
2643 * command issued to a target, we must issue a REQUEST SENSE
2644 * command after receiving a CHECK CONDITION status, before
2645 * another command is issued.
2647 * Since this NCR53c7x0_cmd will be freed after use, we don't
2648 * care if we step on the various fields, so modify a few things.
2650 case A_int_err_check_condition:
2651 #if 0
2652 if (hostdata->options & OPTION_DEBUG_INTR)
2653 #endif
2654 printk ("scsi%d : CHECK CONDITION\n", host->host_no);
2655 if (!c) {
2656 printk("scsi%d : CHECK CONDITION with no SCSI command\n",
2657 host->host_no);
2658 return SPECIFIC_INT_PANIC;
2662 * FIXME : this uses the normal one-byte selection message.
2663 * We may want to renegotiate for synchronous & WIDE transfers
2664 * since these could be the crux of our problem.
2666 hostdata->NOP_insn* FIXME : once SCSI-II tagged queuing is implemented, we'll
2667 * have to set this up so that the rest of the DSA
2668 * agrees with this being an untagged queue'd command.
2671 patch_dsa_32 (cmd->dsa, dsa_msgout, 0, le32_to_cpu(1));
2674 * Modify the table indirect for COMMAND OUT phase, since
2675 * Request Sense is a six byte command.
2678 patch_dsa_32 (cmd->dsa, dsa_cmdout, 0, le32_to_cpu(6));
2680 c->cmnd[0] = REQUEST_SENSE;
2681 c->cmnd[1] &= 0xe0; /* Zero all but LUN */
2682 c->cmnd[2] = 0;
2683 c->cmnd[3] = 0;
2684 c->cmnd[4] = sizeof(c->sense_buffer);
2685 c->cmnd[5] = 0;
2688 * Disable dataout phase, and program datain to transfer to the
2689 * sense buffer, and add a jump to other_transfer after the
2690 * command so overflow/underrun conditions are detected.
2693 patch_dsa_32 (cmd->dsa, dsa_dataout, 0,
2694 le32_to_cpu(virt_to_bus(hostdata->script) + hostdata->E_other_transfer));
2695 patch_dsa_32 (cmd->dsa, dsa_datain, 0,
2696 le32_to_cpu(virt_to_bus(cmd->data_transfer_start)));
2697 cmd->data_transfer_start[0] = le32_to_cpu((((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I |
2698 DCMD_BMI_IO)) << 24) | sizeof(c->sense_buffer));
2699 cmd->data_transfer_start[1] = (u32) le32_to_cpu(virt_to_bus(c->sense_buffer));
2701 cmd->data_transfer_start[2] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP)
2702 << 24) | DBC_TCI_TRUE);
2703 cmd->data_transfer_start[3] = (u32) le32_to_cpu(virt_to_bus(hostdata->script) +
2704 hostdata->E_other_transfer);
2707 * Currently, this command is flagged as completed, ie
2708 * it has valid status and message data. Reflag it as
2709 * incomplete. Q - need to do something so that original
2710 * status, etc are used.
2713 cmd->cmd->result = le32_to_cpu(0xffff);
2716 * Restart command as a REQUEST SENSE.
2718 hostdata->dsp = (u32 *) hostdata->script + hostdata->E_select /
2719 sizeof(u32);
2720 hostdata->dsp_changed = 1;
2721 return SPECIFIC_INT_NOTHING;
2722 case A_int_debug_break:
2723 return SPECIFIC_INT_BREAK;
2724 case A_int_norm_aborted:
2725 hostdata->dsp = (u32 *) hostdata->schedule;
2726 hostdata->dsp_changed = 1;
2727 if (cmd)
2728 abnormal_finished (cmd, DID_ERROR << 16);
2729 return SPECIFIC_INT_NOTHING;
2730 case A_int_test_1:
2731 case A_int_test_2:
2732 hostdata->idle = 1;
2733 hostdata->test_completed = (dsps - A_int_test_1) / 0x00010000 + 1;
2734 if (hostdata->options & OPTION_DEBUG_INTR)
2735 printk("scsi%d : test%d complete\n", host->host_no,
2736 hostdata->test_completed);
2737 return SPECIFIC_INT_NOTHING;
2738 #ifdef A_int_debug_reselected_ok
2739 case A_int_debug_reselected_ok:
2740 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2741 OPTION_DEBUG_DISCONNECT)) {
2743 * Note - this dsa is not based on location relative to
2744 * the command structure, but to location relative to the
2745 * DSA register
2747 u32 *dsa;
2748 dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
2750 printk("scsi%d : reselected_ok (DSA = 0x%x (virt 0x%p)\n",
2751 host->host_no, NCR53c7x0_read32(DSA_REG), dsa);
2752 printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
2753 host->host_no, cmd->saved_data_pointer,
2754 bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)));
2755 print_insn (host, hostdata->script + Ent_reselected_ok /
2756 sizeof(u32), "", 1);
2757 printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
2758 host->host_no, NCR53c7x0_read8(SXFER_REG),
2759 NCR53c7x0_read8(SCNTL3_REG_800));
2760 if (c) {
2761 print_insn (host, (u32 *)
2762 hostdata->sync[c->target].script, "", 1);
2763 print_insn (host, (u32 *)
2764 hostdata->sync[c->target].script + 2, "", 1);
2767 return SPECIFIC_INT_RESTART;
2768 #endif
2769 #ifdef A_int_debug_reselect_check
2770 case A_int_debug_reselect_check:
2771 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2772 u32 *dsa;
2773 #if 0
2774 u32 *code;
2775 #endif
2777 * Note - this dsa is not based on location relative to
2778 * the command structure, but to location relative to the
2779 * DSA register
2781 dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
2782 printk("scsi%d : reselected_check_next (DSA = 0x%lx (virt 0x%p))\n",
2783 host->host_no, virt_to_bus(dsa), dsa);
2784 if (dsa) {
2785 printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
2786 host->host_no, cmd->saved_data_pointer,
2787 bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)));
2788 #if 0
2789 printk("scsi%d : template code :\n", host->host_no);
2790 for (code = dsa + (Ent_dsa_code_check_reselect - Ent_dsa_zero)
2791 / sizeof(u32); code < (dsa + Ent_dsa_zero / sizeof(u32));
2792 code += print_insn (host, code, "", 1));
2793 #endif
2795 print_insn (host, hostdata->script + Ent_reselected_ok /
2796 sizeof(u32), "", 1);
2798 return SPECIFIC_INT_RESTART;
2799 #endif
2800 #ifdef A_int_debug_dsa_schedule
2801 case A_int_debug_dsa_schedule:
2802 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2803 u32 *dsa;
2805 * Note - this dsa is not based on location relative to
2806 * the command structure, but to location relative to the
2807 * DSA register
2809 dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
2810 printk("scsi%d : dsa_schedule (old DSA = 0x%lx (virt 0x%p))\n",
2811 host->host_no, virt_to_bus(dsa), dsa);
2812 if (dsa)
2813 printk("scsi%d : resume address is 0x%x (virt 0x%p)\n"
2814 " (temp was 0x%x (virt 0x%p))\n",
2815 host->host_no, cmd->saved_data_pointer,
2816 bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)),
2817 NCR53c7x0_read32 (TEMP_REG),
2818 bus_to_virt (NCR53c7x0_read32(TEMP_REG)));
2820 return SPECIFIC_INT_RESTART;
2821 #endif
2822 #ifdef A_int_debug_scheduled
2823 case A_int_debug_scheduled:
2824 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2825 printk("scsi%d : new I/O 0x%x (virt 0x%p) scheduled\n",
2826 host->host_no, NCR53c7x0_read32(DSA_REG),
2827 bus_to_virt(NCR53c7x0_read32(DSA_REG)));
2829 return SPECIFIC_INT_RESTART;
2830 #endif
2831 #ifdef A_int_debug_idle
2832 case A_int_debug_idle:
2833 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2834 printk("scsi%d : idle\n", host->host_no);
2836 return SPECIFIC_INT_RESTART;
2837 #endif
2838 #ifdef A_int_debug_cmd
2839 case A_int_debug_cmd:
2840 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2841 printk("scsi%d : command sent\n");
2843 return SPECIFIC_INT_RESTART;
2844 #endif
2845 #ifdef A_int_debug_dsa_loaded
2846 case A_int_debug_dsa_loaded:
2847 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2848 printk("scsi%d : DSA loaded with 0x%x (virt 0x%p)\n", host->host_no,
2849 NCR53c7x0_read32(DSA_REG),
2850 bus_to_virt(NCR53c7x0_read32(DSA_REG)));
2852 return SPECIFIC_INT_RESTART;
2853 #endif
2854 #ifdef A_int_debug_reselected
2855 case A_int_debug_reselected:
2856 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2857 OPTION_DEBUG_DISCONNECT)) {
2858 printk("scsi%d : reselected by target %d lun %d\n",
2859 host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & ~0x80,
2860 (int) hostdata->reselected_identify & 7);
2861 print_queues(host);
2863 return SPECIFIC_INT_RESTART;
2864 #endif
2865 #ifdef A_int_debug_disconnect_msg
2866 case A_int_debug_disconnect_msg:
2867 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2868 if (c)
2869 printk("scsi%d : target %d lun %d disconnecting\n",
2870 host->host_no, c->target, c->lun);
2871 else
2872 printk("scsi%d : unknown target disconnecting\n",
2873 host->host_no);
2875 return SPECIFIC_INT_RESTART;
2876 #endif
2877 #ifdef A_int_debug_disconnected
2878 case A_int_debug_disconnected:
2879 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2880 OPTION_DEBUG_DISCONNECT)) {
2881 printk ("scsi%d : disconnected, new queues are\n",
2882 host->host_no);
2883 print_queues(host);
2884 #if 0
2885 printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
2886 host->host_no, NCR53c7x0_read8(SXFER_REG),
2887 NCR53c7x0_read8(SCNTL3_REG_800));
2888 #endif
2889 if (c) {
2890 print_insn (host, (u32 *)
2891 hostdata->sync[c->target].script, "", 1);
2892 print_insn (host, (u32 *)
2893 hostdata->sync[c->target].script + 2, "", 1);
2896 return SPECIFIC_INT_RESTART;
2897 #endif
2898 #ifdef A_int_debug_panic
2899 case A_int_debug_panic:
2900 printk("scsi%d : int_debug_panic received\n", host->host_no);
2901 print_lots (host);
2902 return SPECIFIC_INT_PANIC;
2903 #endif
2904 #ifdef A_int_debug_saved
2905 case A_int_debug_saved:
2906 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2907 OPTION_DEBUG_DISCONNECT)) {
2908 printk ("scsi%d : saved data pointer 0x%x (virt 0x%p)\n",
2909 host->host_no, cmd->saved_data_pointer,
2910 bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)));
2911 print_progress (c);
2913 return SPECIFIC_INT_RESTART;
2914 #endif
2915 #ifdef A_int_debug_restored
2916 case A_int_debug_restored:
2917 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2918 OPTION_DEBUG_DISCONNECT)) {
2919 if (cmd) {
2920 int size;
2921 printk ("scsi%d : restored data pointer 0x%x (virt 0x%p)\n",
2922 host->host_no, cmd->saved_data_pointer, bus_to_virt (
2923 le32_to_cpu(cmd->saved_data_pointer)));
2924 size = print_insn (host, (u32 *)
2925 bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)), "", 1);
2926 size = print_insn (host, (u32 *)
2927 bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)) + size, "", 1);
2928 print_progress (c);
2930 #if 0
2931 printk ("scsi%d : datapath residual %d\n",
2932 host->host_no, datapath_residual (host)) ;
2933 #endif
2935 return SPECIFIC_INT_RESTART;
2936 #endif
2937 #ifdef A_int_debug_sync
2938 case A_int_debug_sync:
2939 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2940 OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
2941 unsigned char sxfer = NCR53c7x0_read8 (SXFER_REG),
2942 scntl3 = NCR53c7x0_read8 (SCNTL3_REG_800);
2943 if (c) {
2944 if (sxfer != hostdata->sync[c->target].sxfer_sanity ||
2945 scntl3 != hostdata->sync[c->target].scntl3_sanity) {
2946 printk ("scsi%d : sync sanity check failed sxfer=0x%x, scntl3=0x%x",
2947 host->host_no, sxfer, scntl3);
2948 NCR53c7x0_write8 (SXFER_REG, sxfer);
2949 NCR53c7x0_write8 (SCNTL3_REG_800, scntl3);
2951 } else
2952 printk ("scsi%d : unknown command sxfer=0x%x, scntl3=0x%x\n",
2953 host->host_no, (int) sxfer, (int) scntl3);
2955 return SPECIFIC_INT_RESTART;
2956 #endif
2957 #ifdef A_int_debug_datain
2958 case A_int_debug_datain:
2959 if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2960 OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
2961 int size;
2962 printk ("scsi%d : In do_datain (%s) sxfer=0x%x, scntl3=0x%x\n"
2963 " datapath residual=%d\n",
2964 host->host_no, sbcl_to_phase (NCR53c7x0_read8 (SBCL_REG)),
2965 (int) NCR53c7x0_read8(SXFER_REG),
2966 (int) NCR53c7x0_read8(SCNTL3_REG_800),
2967 datapath_residual (host)) ;
2968 print_insn (host, dsp, "", 1);
2969 size = print_insn (host, (u32 *) bus_to_virt(le32_to_cpu(dsp[1])), "", 1);
2970 print_insn (host, (u32 *) bus_to_virt(le32_to_cpu(dsp[1])) + size, "", 1);
2972 return SPECIFIC_INT_RESTART;
2973 #endif
2975 * FIXME : for 7xx support, we need to read SDID_REG_700 and handle
2976 * the comparison as bitfielded, not binary.
2978 #ifdef A_int_debug_check_dsa
2979 case A_int_debug_check_dsa:
2980 if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
2981 int sdid = NCR53c7x0_read8 (SDID_REG_800) & 15;
2982 char *where = dsp - NCR53c7x0_insn_size(NCR53c7x0_read8
2983 (DCMD_REG)) == hostdata->script +
2984 Ent_select_check_dsa / sizeof(u32) ?
2985 "selection" : "reselection";
2986 if (c && sdid != c->target) {
2987 printk ("scsi%d : SDID target %d != DSA target %d at %s\n",
2988 host->host_no, sdid, c->target, where);
2989 print_lots(host);
2990 dump_events (host, 20);
2991 return SPECIFIC_INT_PANIC;
2994 return SPECIFIC_INT_RESTART;
2995 #endif
2996 default:
2997 if ((dsps & 0xff000000) == 0x03000000) {
2998 printk ("scsi%d : misc debug interrupt 0x%x\n",
2999 host->host_no, dsps);
3000 return SPECIFIC_INT_RESTART;
3001 } else if ((dsps & 0xff000000) == 0x05000000) {
3002 if (hostdata->events) {
3003 struct NCR53c7x0_event *event;
3004 ++hostdata->event_index;
3005 if (hostdata->event_index >= hostdata->event_size)
3006 hostdata->event_index = 0;
3007 event = (struct NCR53c7x0_event *) hostdata->events +
3008 hostdata->event_index;
3009 event->event = (enum ncr_event) dsps;
3010 event->dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
3011 /* FIXME : this needs to change for the '7xx family */
3012 if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON)
3013 event->target = NCR53c7x0_read8(SSID_REG_800);
3014 else
3015 event->target = 255;
3017 if (event->event == EVENT_RESELECT)
3018 event->lun = hostdata->reselected_identify & 0xf;
3019 else if (c)
3020 event->lun = c->lun;
3021 else
3022 event->lun = 255;
3023 do_gettimeofday(&(event->time));
3024 if (c) {
3025 event->pid = c->pid;
3026 memcpy ((void *) event->cmnd, (void *) c->cmnd,
3027 sizeof (event->cmnd));
3028 } else {
3029 event->pid = -1;
3032 return SPECIFIC_INT_RESTART;
3035 printk ("scsi%d : unknown user interrupt 0x%x\n",
3036 host->host_no, (unsigned) dsps);
3037 return SPECIFIC_INT_PANIC;
3042 * XXX - the stock NCR assembler won't output the scriptu.h file,
3043 * which undefine's all #define'd CPP symbols from the script.h
3044 * file, which will create problems if you use multiple scripts
3045 * with the same symbol names.
3047 * If you insist on using NCR's assembler, you could generate
3048 * scriptu.h from script.h using something like
3050 * grep #define script.h | \
3051 * sed 's/#define[ ][ ]*\([_a-zA-Z][_a-zA-Z0-9]*\).*$/#undefine \1/' \
3052 * > scriptu.h
3055 #include "53c8xx_u.h"
3057 /* XXX - add alternate script handling code here */
3060 #ifdef NCR_DEBUG
3062 * Debugging without a debugger is no fun. So, I've provided
3063 * a debugging interface in the NCR53c7x0 driver. To avoid
3064 * kernel cruft, there's just enough here to act as an interface
3065 * to a user level debugger (aka, GDB).
3068 * The following restrictions apply to debugger commands :
3069 * 1. The command must be terminated by a newline.
3070 * 2. Command length must be less than 80 bytes including the
3071 * newline.
3072 * 3. The entire command must be written with one system call.
3075 static const char debugger_help =
3076 "bc <addr> - clear breakpoint\n"
3077 "bl - list breakpoints\n"
3078 "bs <addr> - set breakpoint\n"
3079 "g - start\n"
3080 "h - halt\n"
3081 "? - this message\n"
3082 "i - info\n"
3083 "mp <addr> <size> - print memory\n"
3084 "ms <addr> <size> <value> - store memory\n"
3085 "rp <num> <size> - print register\n"
3086 "rs <num> <size> <value> - store register\n"
3087 "s - single step\n"
3088 "tb - begin trace \n"
3089 "te - end trace\n";
3092 * Whenever we change a break point, we should probably
3093 * set the NCR up so that it is in a single step mode.
3096 static int debugger_fn_bc (struct Scsi_Host *host, struct debugger_token *token,
3097 u32 args[]) {
3098 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3099 instance->hostdata;
3100 struct NCR53c7x0_break *bp, **prev;
3101 unsigned long flags;
3102 save_flags(flags);
3103 cli();
3104 for (bp = (struct NCR53c7x0_break *) instance->breakpoints,
3105 prev = (struct NCR53c7x0_break **) &instance->breakpoints;
3106 bp; prev = (struct NCR53c7x0_break **) &(bp->next),
3107 bp = (struct NCR53c7x0_break *) bp->next);
3109 if (!bp) {
3110 restore_flags(flags);
3111 return -EIO;
3115 * XXX - we need to insure that the processor is halted
3116 * here in order to prevent a race condition.
3119 memcpy ((void *) bp->addr, (void *) bp->old, sizeof(bp->old));
3120 if (prev)
3121 *prev = bp->next;
3123 restore_flags(flags);
3124 return 0;
3128 static int
3129 debugger_fn_bl (struct Scsi_Host *host, struct debugger_token *token,
3130 u32 args[]) {
3131 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3132 host->hostdata;
3133 struct NCR53c7x0_break *bp;
3134 char buf[80];
3135 size_t len;
3136 unsigned long flags;
3138 * XXX - we need to insure that the processor is halted
3139 * here in order to prevent a race condition. So, if the
3140 * processor isn't halted, print an error message and continue.
3143 sprintf (buf, "scsi%d : bp : warning : processor not halted\b",
3144 host->host_no);
3145 debugger_kernel_write (host, buf, strlen(buf));
3147 save_flags(flags);
3148 cli();
3149 for (bp = (struct NCR53c7x0_break *) host->breakpoints;
3150 bp; bp = (struct NCR53c7x0_break *) bp->next) {
3151 sprintf (buf, "scsi%d : bp : success : at %08x, replaces %08x %08x",
3152 bp->addr, bp->old[0], bp->old[1]);
3153 len = strlen(buf);
3154 if ((bp->old[0] & (DCMD_TYPE_MASK << 24)) ==
3155 (DCMD_TYPE_MMI << 24)) {
3156 sprintf(buf + len, "%08x\n", * (u32 *) bp->addr);
3157 } else {
3158 sprintf(buf + len, "\n");
3160 len = strlen(buf);
3161 debugger_kernel_write (host, buf, len);
3163 restore_flags(flags);
3164 return 0;
3167 static int
3168 debugger_fn_bs (struct Scsi_Host *host, struct debugger_token *token,
3169 u32 args[]) {
3170 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3171 host->hostdata;
3172 struct NCR53c7x0_break *bp;
3173 char buf[80];
3174 size_t len;
3175 unsigned long flags;
3176 save_flags(flags);
3177 cli();
3179 if (hostdata->state != STATE_HALTED) {
3180 sprintf (buf, "scsi%d : bs : failure : NCR not halted\n", host->host_no);
3181 debugger_kernel_write (host, buf, strlen(buf));
3182 restore_flags(flags);
3183 return -1;
3186 if (!(bp = kmalloc (sizeof (struct NCR53c7x0_break)))) {
3187 printk ("scsi%d : kmalloc(%d) of breakpoint structure failed, try again\n",
3188 host->host_no, sizeof(struct NCR53c7x0_break));
3189 restore_flags(flags);
3190 return -1;
3193 bp->address = (u32 *) args[0];
3194 memcpy ((void *) bp->old_instruction, (void *) bp->address, 8);
3195 bp->old_size = (((bp->old_instruction[0] >> 24) & DCMD_TYPE_MASK) ==
3196 DCMD_TYPE_MMI ? 3 : 2;
3197 bp->next = hostdata->breakpoints;
3198 hostdata->breakpoints = bp->next;
3199 memcpy ((void *) bp->address, (void *) hostdata->E_debug_break, 8);
3201 restore_flags(flags);
3202 return 0;
3205 #define TOKEN(name,nargs) {#name, nargs, debugger_fn_##name}
3206 static const struct debugger_token {
3207 char *name;
3208 int numargs;
3209 int (*fn)(struct debugger_token *token, u32 args[]);
3210 } debugger_tokens[] = {
3211 TOKEN(bc,1), TOKEN(bl,0), TOKEN(bs,1), TOKEN(g,0), TOKEN(halt,0),
3212 {DT_help, "?", 0} , TOKEN(h,0), TOKEN(i,0), TOKEN(mp,2),
3213 TOKEN(ms,3), TOKEN(rp,2), TOKEN(rs,2), TOKEN(s,0), TOKEN(tb,0), TOKEN(te,0)
3216 #define NDT sizeof(debugger_tokens / sizeof(struct debugger_token))
3218 static struct Scsi_Host * inode_to_host (struct inode *inode) {
3219 int dev;
3220 struct Scsi_Host *tmp;
3221 for (dev = MINOR(inode->rdev), host = first_host;
3222 (host->hostt == the_template); --dev, host = host->next)
3223 if (!dev) return host;
3224 return NULL;
3228 static int
3229 debugger_user_write (struct inode *inode,struct file *filp,
3230 char *buf,int count) {
3231 struct Scsi_Host *host; /* This SCSI host */
3232 struct NCR53c7x0_hostadata *hostdata;
3233 char input_buf[80], /* Kernel space copy of buf */
3234 *ptr; /* Pointer to argument list */
3235 u32 args[3]; /* Arguments */
3236 int i, j, error, len;
3238 if (!(host = inode_to_host(inode)))
3239 return -ENXIO;
3241 hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
3243 if (error = verify_area(VERIFY_READ,buf,count))
3244 return error;
3246 if (count > 80)
3247 return -EIO;
3249 memcpy_from_fs(input_buf, buf, count);
3251 if (input_buf[count - 1] != '\n')
3252 return -EIO;
3254 input_buf[count - 1]=0;
3256 for (i = 0; i < NDT; ++i) {
3257 len = strlen (debugger_tokens[i].name);
3258 if (!strncmp(input_buf, debugger_tokens[i].name, len))
3259 break;
3262 if (i == NDT)
3263 return -EIO;
3265 for (ptr = input_buf + len, j = 0; j < debugger_tokens[i].nargs && *ptr;) {
3266 if (*ptr == ' ' || *ptr == '\t') {
3267 ++ptr;
3268 } else if (isdigit(*ptr)) {
3269 args[j++] = simple_strtoul (ptr, &ptr, 0);
3270 } else {
3271 return -EIO;
3275 if (j != debugger_tokens[i].nargs)
3276 return -EIO;
3278 return count;
3281 static int
3282 debugger_user_read (struct inode *inode,struct file *filp,
3283 char *buf,int count) {
3284 struct Scsi_Host *instance;
3288 static int
3289 debugger_kernel_write (struct Scsi_Host *host, char *buf, size_t
3290 buflen) {
3291 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3292 host->hostdata;
3293 int copy, left;
3294 unsigned long flags;
3295 save_flags(flags);
3296 cli();
3297 while (buflen) {
3298 left = (hostdata->debug_buf + hostdata->debug_size - 1) -
3299 hostdata->debug_write;
3300 copy = (buflen <= left) ? buflen : left;
3301 memcpy (hostdata->debug_write, buf, copy);
3302 buf += copy;
3303 buflen -= copy;
3304 hostdata->debug_count += copy;
3305 if ((hostdata->debug_write += copy) ==
3306 (hostdata->debug_buf + hostdata->debug_size))
3307 hosdata->debug_write = hostdata->debug_buf;
3309 restore_flags(flags);
3312 #endif /* def NCRDEBUG */
3315 * Function : static void NCR538xx_soft_reset (struct Scsi_Host *host)
3317 * Purpose : perform a soft reset of the NCR53c8xx chip
3319 * Inputs : host - pointer to this host adapter's structure
3321 * Preconditions : NCR53c7x0_init must have been called for this
3322 * host.
3326 static void
3327 NCR53c8x0_soft_reset (struct Scsi_Host *host) {
3328 NCR53c7x0_local_declare();
3329 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3330 host->hostdata;
3331 NCR53c7x0_local_setup(host);
3335 * Do a soft reset of the chip so that everything is
3336 * reinitialized to the power-on state.
3338 * Basically follow the procedure outlined in the NCR53c700
3339 * data manual under Chapter Six, How to Use, Steps Necessary to
3340 * Start SCRIPTS, with the exception of actually starting the
3341 * script and setting up the synchronous transfer gunk.
3344 NCR53c7x0_write8(ISTAT_REG_800, ISTAT_10_SRST);
3345 NCR53c7x0_write8(ISTAT_REG_800, 0);
3346 NCR53c7x0_write8(hostdata->dmode, hostdata->saved_dmode & ~DMODE_MAN);
3350 * Respond to reselection by targets and use our _initiator_ SCSI ID
3351 * for arbitration. If notyet, also respond to SCSI selection.
3353 * XXX - Note : we must reprogram this when reselecting as
3354 * a target.
3357 #ifdef notyet
3358 NCR53c7x0_write8(SCID_REG, (host->this_id & 7)|SCID_800_RRE|SCID_800_SRE);
3359 #else
3360 NCR53c7x0_write8(SCID_REG, (host->this_id & 7)|SCID_800_RRE);
3361 #endif
3362 NCR53c7x0_write8(RESPID_REG_800, hostdata->this_id_mask);
3365 * Use a maximum (1.6) second handshake to handshake timeout,
3366 * and SCSI recommended .5s selection timeout.
3370 * The new gcc won't recognize preprocessing directives
3371 * within macro args.
3373 #if 0
3374 NCR53c7x0_write8(STIME0_REG_800,
3375 ((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK)
3376 | ((15 << STIME0_800_HTH_SHIFT) & STIME0_800_HTH_MASK));
3377 #else
3378 /* Disable HTH interrupt */
3379 NCR53c7x0_write8(STIME0_REG_800,
3380 ((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK));
3381 #endif
3385 * Enable active negation for happy synchronous transfers.
3388 NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
3391 * Enable all interrupts, except parity which we only want when
3392 * the user requests it.
3395 NCR53c7x0_write8(DIEN_REG, DIEN_800_MDPE | DIEN_800_BF |
3396 DIEN_ABRT | DIEN_SSI | DIEN_SIR | DIEN_800_IID);
3399 NCR53c7x0_write8(SIEN0_REG_800, ((hostdata->options & OPTION_PARITY) ?
3400 SIEN_PAR : 0) | SIEN_RST | SIEN_UDC | SIEN_SGE | SIEN_MA);
3401 NCR53c7x0_write8(SIEN1_REG_800, SIEN1_800_STO | SIEN1_800_HTH);
3404 * Use saved clock frequency divisor and scripts loaded in 16 bit
3405 * mode flags from the saved dcntl.
3408 NCR53c7x0_write8(DCNTL_REG, hostdata->saved_dcntl);
3409 NCR53c7x0_write8(CTEST4_REG_800, hostdata->saved_ctest4);
3411 /* Enable active negation */
3412 NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
3416 * Function static struct NCR53c7x0_cmd *allocate_cmd (Scsi_Cmnd *cmd)
3418 * Purpose : Return the first free NCR53c7x0_cmd structure (which are
3419 * reused in a LIFO manner to minimize cache thrashing).
3421 * Side effects : If we haven't yet scheduled allocation of NCR53c7x0_cmd
3422 * structures for this device, do so. Attempt to complete all scheduled
3423 * allocations using kmalloc(), putting NCR53c7x0_cmd structures on
3424 * the free list. Teach programmers not to drink and hack.
3426 * Inputs : cmd - SCSI command
3428 * Returns : NCR53c7x0_cmd structure allocated on behalf of cmd;
3429 * NULL on failure.
3432 static struct NCR53c7x0_cmd *
3433 allocate_cmd (Scsi_Cmnd *cmd) {
3434 struct Scsi_Host *host = cmd->host;
3435 struct NCR53c7x0_hostdata *hostdata =
3436 (struct NCR53c7x0_hostdata *) host->hostdata;
3437 void *real; /* Real address */
3438 int size; /* Size of *tmp */
3439 struct NCR53c7x0_cmd *tmp;
3440 unsigned long flags;
3442 if (hostdata->options & OPTION_DEBUG_ALLOCATION)
3443 printk ("scsi%d : num_cmds = %d, can_queue = %d\n"
3444 " target = %d, lun = %d, %s\n",
3445 host->host_no, hostdata->num_cmds, host->can_queue,
3446 cmd->target, cmd->lun, (hostdata->cmd_allocated[cmd->target] &
3447 (1 << cmd->lun)) ? "already allocated" : "not allocated");
3450 * If we have not yet reserved commands for this I_T_L nexus, and
3451 * the device exists (as indicated by permanent Scsi_Cmnd structures
3452 * being allocated under 1.3.x, or being outside of scan_scsis in
3453 * 1.2.x), do so now.
3455 if (!(hostdata->cmd_allocated[cmd->target] & (1 << cmd->lun)) &&
3456 cmd->device && cmd->device->has_cmdblocks
3458 if ((hostdata->extra_allocate + hostdata->num_cmds) < host->can_queue)
3459 hostdata->extra_allocate += host->cmd_per_lun;
3460 hostdata->cmd_allocated[cmd->target] |= (1 << cmd->lun);
3463 for (; hostdata->extra_allocate > 0 ; --hostdata->extra_allocate,
3464 ++hostdata->num_cmds) {
3465 /* historically, kmalloc has returned unaligned addresses; pad so we
3466 have enough room to ROUNDUP */
3467 size = hostdata->max_cmd_size + sizeof (void *);
3468 /* FIXME: for ISA bus '7xx chips, we need to or GFP_DMA in here */
3469 real = kmalloc (size, GFP_ATOMIC);
3470 if (!real) {
3471 if (hostdata->options & OPTION_DEBUG_ALLOCATION)
3472 printk ("scsi%d : kmalloc(%d) failed\n",
3473 host->host_no, size);
3474 break;
3476 tmp = ROUNDUP(real, void *);
3477 tmp->real = real;
3478 tmp->size = size;
3479 tmp->free = ((void (*)(void *, int)) kfree);
3480 save_flags (flags);
3481 cli();
3482 tmp->next = hostdata->free;
3483 hostdata->free = tmp;
3484 restore_flags (flags);
3486 save_flags(flags);
3487 cli();
3488 tmp = (struct NCR53c7x0_cmd *) hostdata->free;
3489 if (tmp) {
3490 hostdata->free = tmp->next;
3492 restore_flags(flags);
3493 if (!tmp)
3494 printk ("scsi%d : can't allocate command for target %d lun %d\n",
3495 host->host_no, cmd->target, cmd->lun);
3496 return tmp;
3500 * Function static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd)
3503 * Purpose : allocate a NCR53c7x0_cmd structure, initialize it based on the
3504 * Scsi_Cmnd structure passed in cmd, including dsa and Linux field
3505 * initialization, and dsa code relocation.
3507 * Inputs : cmd - SCSI command
3509 * Returns : NCR53c7x0_cmd structure corresponding to cmd,
3510 * NULL on failure.
3513 static struct NCR53c7x0_cmd *
3514 create_cmd (Scsi_Cmnd *cmd) {
3515 NCR53c7x0_local_declare();
3516 struct Scsi_Host *host = cmd->host;
3517 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3518 host->hostdata;
3519 struct NCR53c7x0_cmd *tmp; /* NCR53c7x0_cmd structure for this command */
3520 int datain, /* Number of instructions per phase */
3521 dataout;
3522 int data_transfer_instructions, /* Count of dynamic instructions */
3523 i; /* Counter */
3524 u32 *cmd_datain, /* Address of datain/dataout code */
3525 *cmd_dataout; /* Incremented as we assemble */
3526 #ifdef notyet
3527 unsigned char *msgptr; /* Current byte in select message */
3528 int msglen; /* Length of whole select message */
3529 #endif
3530 unsigned long flags;
3531 NCR53c7x0_local_setup(cmd->host);
3533 if (!(tmp = allocate_cmd (cmd)))
3534 return NULL;
3538 * Decide whether we need to generate commands for DATA IN,
3539 * DATA OUT, neither, or both based on the SCSI command
3542 switch (cmd->cmnd[0]) {
3543 /* These commands do DATA IN */
3544 case INQUIRY:
3545 case MODE_SENSE:
3546 case READ_6:
3547 case READ_10:
3548 case READ_CAPACITY:
3549 case REQUEST_SENSE:
3550 datain = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3551 dataout = 0;
3552 break;
3553 /* These commands do DATA OUT */
3554 case MODE_SELECT:
3555 case WRITE_6:
3556 case WRITE_10:
3557 #if 0
3558 printk("scsi%d : command is ", host->host_no);
3559 print_command(cmd->cmnd);
3560 #endif
3561 #if 0
3562 printk ("scsi%d : %d scatter/gather segments\n", host->host_no,
3563 cmd->use_sg);
3564 #endif
3565 datain = 0;
3566 dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3567 #if 0
3568 hostdata->options |= OPTION_DEBUG_INTR;
3569 #endif
3570 break;
3572 * These commands do no data transfer, we should force an
3573 * interrupt if a data phase is attempted on them.
3575 case START_STOP: /* also SCAN, which may do DATA OUT */
3576 case TEST_UNIT_READY:
3577 datain = dataout = 0;
3578 break;
3580 * We don't know about these commands, so generate code to handle
3581 * both DATA IN and DATA OUT phases.
3583 default:
3584 datain = dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3588 * New code : so that active pointers work correctly regardless
3589 * of where the saved data pointer is at, we want to immediately
3590 * enter the dynamic code after selection, and on a non-data
3591 * phase perform a CALL to the non-data phase handler, with
3592 * returns back to this address.
3594 * If a phase mismatch is encountered in the middle of a
3595 * Block MOVE instruction, we want to _leave_ that instruction
3596 * unchanged as the current case is, modify a temporary buffer,
3597 * and point the active pointer (TEMP) at that.
3599 * Furthermore, we want to implement a saved data pointer,
3600 * set by the SAVE_DATA_POINTERs message.
3602 * So, the data transfer segments will change to
3603 * CALL data_transfer, WHEN NOT data phase
3604 * MOVE x, x, WHEN data phase
3605 * ( repeat )
3606 * JUMP other_transfer
3609 data_transfer_instructions = datain + dataout;
3612 * When we perform a request sense, we overwrite various things,
3613 * including the data transfer code. Make sure we have enough
3614 * space to do that.
3617 if (data_transfer_instructions < 2)
3618 data_transfer_instructions = 2;
3622 * The saved data pointer is set up so that a RESTORE POINTERS message
3623 * will start the data transfer over at the beginning.
3626 tmp->saved_data_pointer = le32_to_cpu(virt_to_bus (hostdata->script) +
3627 hostdata->E_data_transfer);
3630 * Initialize Linux specific fields.
3633 tmp->cmd = cmd;
3634 tmp->next = NULL;
3635 tmp->flags = 0;
3636 tmp->dsa_next_addr = le32_to_cpu(virt_to_bus(tmp->dsa) + hostdata->dsa_next -
3637 hostdata->dsa_start);
3638 tmp->dsa_addr = le32_to_cpu(virt_to_bus(tmp->dsa) - hostdata->dsa_start);
3641 * Calculate addresses of dynamic code to fill in DSA
3644 tmp->data_transfer_start = tmp->dsa + (hostdata->dsa_end -
3645 hostdata->dsa_start) / sizeof(u32);
3646 tmp->data_transfer_end = tmp->data_transfer_start +
3647 2 * data_transfer_instructions;
3649 cmd_datain = datain ? tmp->data_transfer_start : NULL;
3650 cmd_dataout = dataout ? (datain ? cmd_datain + 2 * datain : tmp->
3651 data_transfer_start) : NULL;
3654 * Fill in the NCR53c7x0_cmd structure as follows
3655 * dsa, with fixed up DSA code
3656 * datain code
3657 * dataout code
3660 /* Copy template code into dsa and perform all necessary fixups */
3661 if (hostdata->dsa_fixup)
3662 hostdata->dsa_fixup(tmp);
3664 patch_dsa_32(tmp->dsa, dsa_next, 0, le32_to_cpu(0));
3665 patch_dsa_32(tmp->dsa, dsa_cmnd, 0, le32_to_cpu(virt_to_bus(cmd)));
3667 if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS)
3668 if (hostdata->sync[cmd->target].select_indirect !=
3669 ((hostdata->sync[cmd->target].scntl3_sanity << 24) |
3670 (cmd->target << 16) |
3671 (hostdata->sync[cmd->target].sxfer_sanity << 8))) {
3672 printk ("scsi%d : sanity check failed select_indirect=0x%x\n",
3673 host->host_no, hostdata->sync[cmd->target].select_indirect);
3674 FATAL(host);
3678 patch_dsa_32(tmp->dsa, dsa_select, 0, le32_to_cpu(hostdata->sync[cmd->target].
3679 select_indirect));
3681 * Right now, we'll do the WIDE and SYNCHRONOUS negotiations on
3682 * different commands; although it should be trivial to do them
3683 * both at the same time.
3685 if (hostdata->initiate_wdtr & (1 << cmd->target)) {
3686 memcpy ((void *) (tmp->select + 1), (void *) wdtr_message,
3687 sizeof(wdtr_message));
3688 patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(wdtr_message)));
3689 save_flags(flags);
3690 cli();
3691 hostdata->initiate_wdtr &= ~(1 << cmd->target);
3692 restore_flags(flags);
3693 } else if (hostdata->initiate_sdtr & (1 << cmd->target)) {
3694 memcpy ((void *) (tmp->select + 1), (void *) sdtr_message,
3695 sizeof(sdtr_message));
3696 patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(sdtr_message)));
3697 tmp->flags |= CMD_FLAG_SDTR;
3698 save_flags(flags);
3699 cli();
3700 hostdata->initiate_sdtr &= ~(1 << cmd->target);
3701 restore_flags(flags);
3704 #if 1
3705 else if (!(hostdata->talked_to & (1 << cmd->target)) &&
3706 !(hostdata->options & OPTION_NO_ASYNC)) {
3707 memcpy ((void *) (tmp->select + 1), (void *) async_message,
3708 sizeof(async_message));
3709 patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(async_message)));
3710 tmp->flags |= CMD_FLAG_SDTR;
3712 #endif
3713 else
3714 patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1));
3715 hostdata->talked_to |= (1 << cmd->target);
3716 tmp->select[0] = (hostdata->options & OPTION_DISCONNECT) ?
3717 IDENTIFY (1, cmd->lun) : IDENTIFY (0, cmd->lun);
3718 patch_dsa_32(tmp->dsa, dsa_msgout, 1, le32_to_cpu(virt_to_bus(tmp->select)));
3719 patch_dsa_32(tmp->dsa, dsa_cmdout, 0, le32_to_cpu(cmd->cmd_len));
3720 patch_dsa_32(tmp->dsa, dsa_cmdout, 1, le32_to_cpu(virt_to_bus(cmd->cmnd)));
3721 patch_dsa_32(tmp->dsa, dsa_dataout, 0, le32_to_cpu(cmd_dataout ?
3722 virt_to_bus (cmd_dataout)
3723 : virt_to_bus (hostdata->script) + hostdata->E_other_transfer));
3724 patch_dsa_32(tmp->dsa, dsa_datain, 0, le32_to_cpu(cmd_datain ?
3725 virt_to_bus (cmd_datain)
3726 : virt_to_bus (hostdata->script) + hostdata->E_other_transfer));
3728 * XXX - need to make endian aware, should use separate variables
3729 * for both status and message bytes.
3731 patch_dsa_32(tmp->dsa, dsa_msgin, 0, le32_to_cpu(1));
3733 * FIXME : these only works for little endian. We probably want to
3734 * provide message and status fields in the NCR53c7x0_cmd
3735 * structure, and assign them to cmd->result when we're done.
3737 patch_dsa_32(tmp->dsa, dsa_msgin, 1, le32_to_cpu(virt_to_bus(&cmd->result) + 1));
3738 patch_dsa_32(tmp->dsa, dsa_status, 0, le32_to_cpu(1));
3739 patch_dsa_32(tmp->dsa, dsa_status, 1, le32_to_cpu(virt_to_bus(&cmd->result)));
3740 patch_dsa_32(tmp->dsa, dsa_msgout_other, 0, le32_to_cpu(1));
3741 patch_dsa_32(tmp->dsa, dsa_msgout_other, 1,
3742 le32_to_cpu(virt_to_bus(&(hostdata->NCR53c7xx_msg_nop))));
3745 * Generate code for zero or more of the DATA IN, DATA OUT phases
3746 * in the format
3748 * CALL data_transfer, WHEN NOT phase
3749 * MOVE first buffer length, first buffer address, WHEN phase
3750 * ...
3751 * MOVE last buffer length, last buffer address, WHEN phase
3752 * JUMP other_transfer
3756 * See if we're getting to data transfer by generating an unconditional
3757 * interrupt.
3759 #if 0
3760 if (datain) {
3761 cmd_datain[0] = 0x98080000;
3762 cmd_datain[1] = 0x03ffd00d;
3763 cmd_datain += 2;
3765 #endif
3768 * XXX - I'm undecided whether all of this nonsense is faster
3769 * in the long run, or whether I should just go and implement a loop
3770 * on the NCR chip using table indirect mode?
3772 * In any case, this is how it _must_ be done for 53c700/700-66 chips,
3773 * so this stays even when we come up with something better.
3775 * When we're limited to 1 simultaneous command, no overlapping processing,
3776 * we're seeing 630K/sec, with 7% CPU usage on a slow Syquest 45M
3777 * drive.
3779 * Not bad, not good. We'll see.
3782 for (i = 0; cmd->use_sg ? (i < cmd->use_sg) : !i; cmd_datain += 4,
3783 cmd_dataout += 4, ++i) {
3784 u32 buf = cmd->use_sg ?
3785 virt_to_bus(((struct scatterlist *)cmd->buffer)[i].address) :
3786 virt_to_bus(cmd->request_buffer);
3787 u32 count = cmd->use_sg ?
3788 ((struct scatterlist *)cmd->buffer)[i].length :
3789 cmd->request_bufflen;
3791 if (datain) {
3792 /* CALL other_in, WHEN NOT DATA_IN */
3793 cmd_datain[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
3794 DCMD_TCI_IO) << 24) |
3795 DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
3796 cmd_datain[1] = le32_to_cpu(virt_to_bus (hostdata->script) +
3797 hostdata->E_other_in);
3798 /* MOVE count, buf, WHEN DATA_IN */
3799 cmd_datain[2] = le32_to_cpu(((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | DCMD_BMI_IO)
3800 << 24) | count);
3801 cmd_datain[3] = le32_to_cpu(buf);
3802 #if 0
3803 print_insn (host, cmd_datain, "dynamic ", 1);
3804 print_insn (host, cmd_datain + 2, "dynamic ", 1);
3805 #endif
3807 if (dataout) {
3808 /* CALL other_out, WHEN NOT DATA_OUT */
3809 cmd_dataout[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL) << 24) |
3810 DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
3811 cmd_dataout[1] = le32_to_cpu(virt_to_bus(hostdata->script) +
3812 hostdata->E_other_out);
3813 /* MOVE count, buf, WHEN DATA+OUT */
3814 cmd_dataout[2] = le32_to_cpu(((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I) << 24)
3815 | count);
3816 cmd_dataout[3] = le32_to_cpu(buf);
3817 #if 0
3818 print_insn (host, cmd_dataout, "dynamic ", 1);
3819 print_insn (host, cmd_dataout + 2, "dynamic ", 1);
3820 #endif
3825 * Install JUMP instructions after the data transfer routines to return
3826 * control to the do_other_transfer routines.
3830 if (datain) {
3831 cmd_datain[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
3832 DBC_TCI_TRUE);
3833 cmd_datain[1] = le32_to_cpu(virt_to_bus(hostdata->script) +
3834 hostdata->E_other_transfer);
3835 #if 0
3836 print_insn (host, cmd_datain, "dynamic jump ", 1);
3837 #endif
3838 cmd_datain += 2;
3840 #if 0
3841 if (datain) {
3842 cmd_datain[0] = 0x98080000;
3843 cmd_datain[1] = 0x03ffdeed;
3844 cmd_datain += 2;
3846 #endif
3847 if (dataout) {
3848 cmd_dataout[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
3849 DBC_TCI_TRUE);
3850 cmd_dataout[1] = le32_to_cpu(virt_to_bus(hostdata->script) +
3851 hostdata->E_other_transfer);
3852 #if 0
3853 print_insn (host, cmd_dataout, "dynamic jump ", 1);
3854 #endif
3855 cmd_dataout += 2;
3857 return tmp;
3861 * Function : int NCR53c7xx_queue_command (Scsi_Cmnd *cmd,
3862 * void (*done)(Scsi_Cmnd *))
3864 * Purpose : enqueues a SCSI command
3866 * Inputs : cmd - SCSI command, done - function called on completion, with
3867 * a pointer to the command descriptor.
3869 * Returns : 0
3871 * Side effects :
3872 * cmd is added to the per instance driver issue_queue, with major
3873 * twiddling done to the host specific fields of cmd. If the
3874 * process_issue_queue coroutine isn't running, it is restarted.
3876 * NOTE : we use the host_scribble field of the Scsi_Cmnd structure to
3877 * hold our own data, and pervert the ptr field of the SCp field
3878 * to create a linked list.
3882 NCR53c7xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *)) {
3883 struct Scsi_Host *host = cmd->host;
3884 struct NCR53c7x0_hostdata *hostdata =
3885 (struct NCR53c7x0_hostdata *) host->hostdata;
3886 unsigned long flags;
3887 Scsi_Cmnd *tmp;
3889 cmd->scsi_done = done;
3890 cmd->host_scribble = NULL;
3891 cmd->SCp.ptr = NULL;
3892 cmd->SCp.buffer = NULL;
3894 save_flags(flags);
3895 cli();
3896 if ((hostdata->options & (OPTION_DEBUG_INIT_ONLY|OPTION_DEBUG_PROBE_ONLY))
3897 || ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
3898 !(hostdata->debug_lun_limit[cmd->target] & (1 << cmd->lun)))
3899 || cmd->target > host->max_id
3900 || cmd->target == host->this_id
3901 || hostdata->state == STATE_DISABLED) {
3902 printk("scsi%d : disabled or bad target %d lun %d\n", host->host_no,
3903 cmd->target, cmd->lun);
3904 cmd->result = DID_BAD_TARGET << 16;
3905 } else if ((hostdata->options & OPTION_DEBUG_NCOMMANDS_LIMIT) &&
3906 (hostdata->debug_count_limit == 0)) {
3907 printk("scsi%d : maximum commands exceeded\n", host->host_no);
3908 cmd->result = DID_BAD_TARGET << 16;
3909 } else if (hostdata->options & OPTION_DEBUG_READ_ONLY) {
3910 switch (cmd->cmnd[0]) {
3911 case WRITE_6:
3912 case WRITE_10:
3913 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
3914 host->host_no);
3915 cmd->result = DID_BAD_TARGET << 16;
3917 } else {
3918 if ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
3919 hostdata->debug_count_limit != -1)
3920 --hostdata->debug_count_limit;
3921 restore_flags (flags);
3922 cmd->result = le32_to_cpu(0xffff); /* The NCR will overwrite message
3923 and status with valid data */
3924 cmd->host_scribble = (unsigned char *) tmp = create_cmd (cmd);
3926 cli();
3928 * REQUEST SENSE commands are inserted at the head of the queue
3929 * so that we do not clear the contingent allegiance condition
3930 * they may be looking at.
3933 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
3934 cmd->SCp.ptr = (unsigned char *) hostdata->issue_queue;
3935 hostdata->issue_queue = cmd;
3936 } else {
3937 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->SCp.ptr;
3938 tmp = (Scsi_Cmnd *) tmp->SCp.ptr);
3939 tmp->SCp.ptr = (unsigned char *) cmd;
3941 restore_flags (flags);
3942 run_process_issue_queue();
3943 return 0;
3947 * Function : void to_schedule_list (struct Scsi_Host *host,
3948 * struct NCR53c7x0_hostdata * hostdata, Scsi_Cmnd *cmd)
3950 * Purpose : takes a SCSI command which was just removed from the
3951 * issue queue, and deals with it by inserting it in the first
3952 * free slot in the schedule list or by terminating it immediately.
3954 * Inputs :
3955 * host - SCSI host adapter; hostdata - hostdata structure for
3956 * this adapter; cmd - a pointer to the command; should have
3957 * the host_scribble field initialized to point to a valid
3959 * Side effects :
3960 * cmd is added to the per instance schedule list, with minor
3961 * twiddling done to the host specific fields of cmd.
3965 static __inline__ void
3966 to_schedule_list (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
3967 struct NCR53c7x0_cmd *cmd) {
3968 NCR53c7x0_local_declare();
3969 Scsi_Cmnd *tmp = cmd->cmd;
3970 unsigned long flags;
3971 /* dsa start is negative, so subtraction is used */
3972 volatile u32 *curr;
3974 int i;
3975 NCR53c7x0_local_setup(host);
3976 #if 0
3977 printk("scsi%d : new dsa is 0x%lx (virt 0x%p)\n", host->host_no,
3978 virt_to_bus(dsa), dsa);
3979 #endif
3981 save_flags(flags);
3982 cli();
3985 * Work around race condition : if an interrupt fired and we
3986 * got disabled forget about this command.
3989 if (hostdata->state == STATE_DISABLED) {
3990 printk("scsi%d : driver disabled\n", host->host_no);
3991 tmp->result = DID_BAD_TARGET << 16;
3992 cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
3993 hostdata->free = cmd;
3994 tmp->scsi_done(tmp);
3995 restore_flags (flags);
3996 return;
3999 for (i = host->can_queue, curr = hostdata->schedule;
4000 i > 0 && curr[0] != hostdata->NOP_insn;
4001 --i, curr += 2 /* JUMP instructions are two words */);
4003 if (i > 0) {
4004 ++hostdata->busy[tmp->target][tmp->lun];
4005 cmd->next = hostdata->running_list;
4006 hostdata->running_list = cmd;
4008 /* Restore this instruction to a NOP once the command starts */
4009 cmd->dsa [(hostdata->dsa_jump_dest - hostdata->dsa_start) /
4010 sizeof(u32)] = (u32) le32_to_cpu(virt_to_bus ((void *)curr));
4011 /* Replace the current jump operand. */
4012 curr[1] =
4013 le32_to_cpu(virt_to_bus ((void *) cmd->dsa) + hostdata->E_dsa_code_begin -
4014 hostdata->E_dsa_code_template);
4015 /* Replace the NOP instruction with a JUMP */
4016 curr[0] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) |
4017 DBC_TCI_TRUE);
4018 } else {
4019 printk ("scsi%d: no free slot\n", host->host_no);
4020 disable(host);
4021 tmp->result = DID_ERROR << 16;
4022 cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
4023 hostdata->free = cmd;
4024 tmp->scsi_done(tmp);
4025 restore_flags (flags);
4026 return;
4030 * If the NCR chip is in an idle state, start it running the scheduler
4031 * immediately. Otherwise, signal the chip to jump to schedule as
4032 * soon as it is idle.
4034 if (hostdata->idle) {
4035 hostdata->idle = 0;
4036 hostdata->state = STATE_RUNNING;
4037 NCR53c7x0_write32 (DSP_REG, virt_to_bus ((void *)hostdata->schedule));
4038 } else {
4039 NCR53c7x0_write8(hostdata->istat, ISTAT_10_SIGP);
4042 restore_flags(flags);
4046 * Function : busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata
4047 * *hostdata, Scsi_Cmnd *cmd)
4049 * Purpose : decide if we can pass the given SCSI command on to the
4050 * device in question or not.
4052 * Returns : non-zero when we're busy, 0 when we aren't.
4055 static __inline__ int
4056 busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
4057 Scsi_Cmnd *cmd) {
4058 /* FIXME : in the future, this needs to accommodate SCSI-II tagged
4059 queuing, and we may be able to play with fairness here a bit.
4061 return hostdata->busy[cmd->target][cmd->lun];
4065 * Function : process_issue_queue (void)
4067 * Purpose : transfer commands from the issue queue to NCR start queue
4068 * of each NCR53c7/8xx in the system, avoiding kernel stack
4069 * overflows when the scsi_done() function is invoked recursively.
4071 * NOTE : process_issue_queue exits with interrupts *disabled*, so the
4072 * caller must reenable them if it desires.
4074 * NOTE : process_issue_queue should be called from both
4075 * NCR53c7x0_queue_command() and from the interrupt handler
4076 * after command completion in case NCR53c7x0_queue_command()
4077 * isn't invoked again but we've freed up resources that are
4078 * needed.
4081 static void
4082 process_issue_queue (unsigned long flags) {
4083 Scsi_Cmnd *tmp, *prev;
4084 struct Scsi_Host *host;
4085 struct NCR53c7x0_hostdata *hostdata;
4086 int done;
4089 * We run (with interrupts disabled) until we're sure that none of
4090 * the host adapters have anything that can be done, at which point
4091 * we set process_issue_queue_running to 0 and exit.
4093 * Interrupts are enabled before doing various other internal
4094 * instructions, after we've decided that we need to run through
4095 * the loop again.
4099 do {
4100 cli(); /* Freeze request queues */
4101 done = 1;
4102 for (host = first_host; host && host->hostt == the_template;
4103 host = host->next) {
4104 hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
4105 cli();
4106 if (hostdata->issue_queue) {
4107 if (hostdata->state == STATE_DISABLED) {
4108 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
4109 hostdata->issue_queue = (Scsi_Cmnd *) tmp->SCp.ptr;
4110 tmp->result = DID_BAD_TARGET << 16;
4111 if (tmp->host_scribble) {
4112 ((struct NCR53c7x0_cmd *)tmp->host_scribble)->next =
4113 hostdata->free;
4114 hostdata->free =
4115 (struct NCR53c7x0_cmd *)tmp->host_scribble;
4116 tmp->host_scribble = NULL;
4118 tmp->scsi_done (tmp);
4119 done = 0;
4120 } else
4121 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
4122 prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
4123 tmp->SCp.ptr)
4124 if (!tmp->host_scribble ||
4125 !busyp (host, hostdata, tmp)) {
4126 if (prev)
4127 prev->SCp.ptr = tmp->SCp.ptr;
4128 else
4129 hostdata->issue_queue = (Scsi_Cmnd *)
4130 tmp->SCp.ptr;
4131 tmp->SCp.ptr = NULL;
4132 if (tmp->host_scribble) {
4133 if (hostdata->options & OPTION_DEBUG_QUEUES)
4134 printk ("scsi%d : moving command for target %d lun %d to start list\n",
4135 host->host_no, tmp->target, tmp->lun);
4138 to_schedule_list (host, hostdata,
4139 (struct NCR53c7x0_cmd *)
4140 tmp->host_scribble);
4141 } else {
4142 tmp->result = le32_to_cpu(tmp->result);
4143 if (((tmp->result & 0xff) == 0xff) ||
4144 ((tmp->result & 0xff00) == 0xff00)) {
4145 printk ("scsi%d : danger Will Robinson!\n",
4146 host->host_no);
4147 tmp->result = DID_ERROR << 16;
4148 disable (host);
4150 tmp->scsi_done(tmp);
4152 done = 0;
4153 } /* if target/lun is not busy */
4154 } /* if hostdata->issue_queue */
4155 if (!done)
4156 restore_flags (flags);
4157 } /* for host */
4158 } while (!done);
4159 process_issue_queue_running = 0;
4163 * Function : static void intr_scsi (struct Scsi_Host *host,
4164 * struct NCR53c7x0_cmd *cmd)
4166 * Purpose : handle all SCSI interrupts, indicated by the setting
4167 * of the SIP bit in the ISTAT register.
4169 * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
4170 * may be NULL.
4173 static void
4174 intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
4175 NCR53c7x0_local_declare();
4176 struct NCR53c7x0_hostdata *hostdata =
4177 (struct NCR53c7x0_hostdata *) host->hostdata;
4178 unsigned char sstat0_sist0, sist1, /* Registers */
4179 fatal; /* Did a fatal interrupt
4180 occur ? */
4182 int is_8xx_chip;
4183 NCR53c7x0_local_setup(host);
4185 fatal = 0;
4187 is_8xx_chip = ((unsigned) (hostdata->chip - 800)) < 100;
4188 if (is_8xx_chip) {
4189 sstat0_sist0 = NCR53c7x0_read8(SIST0_REG_800);
4190 udelay(1);
4191 sist1 = NCR53c7x0_read8(SIST1_REG_800);
4192 } else {
4193 sstat0_sist0 = NCR53c7x0_read8(SSTAT0_REG);
4194 sist1 = 0;
4197 if (hostdata->options & OPTION_DEBUG_INTR)
4198 printk ("scsi%d : SIST0 0x%0x, SIST1 0x%0x\n", host->host_no,
4199 sstat0_sist0, sist1);
4201 /* 250ms selection timeout */
4202 if ((is_8xx_chip && (sist1 & SIST1_800_STO)) ||
4203 (!is_8xx_chip && (sstat0_sist0 & SSTAT0_700_STO))) {
4204 fatal = 1;
4205 if (hostdata->options & OPTION_DEBUG_INTR) {
4206 printk ("scsi%d : Selection Timeout\n", host->host_no);
4207 if (cmd) {
4208 printk("scsi%d : target %d, lun %d, command ",
4209 host->host_no, cmd->cmd->target, cmd->cmd->lun);
4210 print_command (cmd->cmd->cmnd);
4211 printk("scsi%d : dsp = 0x%x (virt 0x%p)\n", host->host_no,
4212 NCR53c7x0_read32(DSP_REG),
4213 bus_to_virt(NCR53c7x0_read32(DSP_REG)));
4214 } else {
4215 printk("scsi%d : no command\n", host->host_no);
4219 * XXX - question : how do we want to handle the Illegal Instruction
4220 * interrupt, which may occur before or after the Selection Timeout
4221 * interrupt?
4224 if (1) {
4225 hostdata->idle = 1;
4226 hostdata->expecting_sto = 0;
4228 if (hostdata->test_running) {
4229 hostdata->test_running = 0;
4230 hostdata->test_completed = 3;
4231 } else if (cmd) {
4232 abnormal_finished(cmd, DID_BAD_TARGET << 16);
4234 #if 0
4235 hostdata->intrs = 0;
4236 #endif
4241 * FIXME : in theory, we can also get a UDC when a STO occurs.
4243 if (sstat0_sist0 & SSTAT0_UDC) {
4244 fatal = 1;
4245 if (cmd) {
4246 printk("scsi%d : target %d lun %d unexpected disconnect\n",
4247 host->host_no, cmd->cmd->target, cmd->cmd->lun);
4248 print_lots (host);
4249 abnormal_finished(cmd, DID_ERROR << 16);
4250 } else
4251 printk("scsi%d : unexpected disconnect (no command)\n",
4252 host->host_no);
4254 hostdata->dsp = (u32 *) hostdata->schedule;
4255 hostdata->dsp_changed = 1;
4258 /* SCSI PARITY error */
4259 if (sstat0_sist0 & SSTAT0_PAR) {
4260 fatal = 1;
4261 if (cmd && cmd->cmd) {
4262 printk("scsi%d : target %d lun %d parity error.\n",
4263 host->host_no, cmd->cmd->target, cmd->cmd->lun);
4264 abnormal_finished (cmd, DID_PARITY << 16);
4265 } else
4266 printk("scsi%d : parity error\n", host->host_no);
4267 /* Should send message out, parity error */
4269 /* XXX - Reduce synchronous transfer rate! */
4270 hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4271 sizeof(u32);
4272 hostdata->dsp_changed = 1;
4273 /* SCSI GROSS error */
4276 if (sstat0_sist0 & SSTAT0_SGE) {
4277 fatal = 1;
4278 printk("scsi%d : gross error\n", host->host_no);
4279 /* Reset SCSI offset */
4280 if ((hostdata->chip / 100) == 8) {
4281 NCR53c7x0_write8 (STEST2_REG_800, STEST2_800_ROF);
4285 * A SCSI gross error may occur when we have
4287 * - A synchronous offset which causes the SCSI FIFO to be overwritten.
4289 * - A REQ which causes the maximum synchronous offset programmed in
4290 * the SXFER register to be exceeded.
4292 * - A phase change with an outstanding synchronous offset.
4294 * - Residual data in the synchronous data FIFO, with a transfer
4295 * other than a synchronous receive is started.$#
4299 /* XXX Should deduce synchronous transfer rate! */
4300 hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4301 sizeof(u32);
4302 hostdata->dsp_changed = 1;
4303 /* Phase mismatch */
4306 if (sstat0_sist0 & SSTAT0_MA) {
4307 fatal = 1;
4308 if (hostdata->options & OPTION_DEBUG_INTR)
4309 printk ("scsi%d : SSTAT0_MA\n", host->host_no);
4310 intr_phase_mismatch (host, cmd);
4313 #if 0
4314 if (sstat0_sist0 & SIST0_800_RSL)
4315 printk ("scsi%d : Oh no Mr. Bill!\n", host->host_no);
4316 #endif
4319 * If a fatal SCSI interrupt occurs, we must insure that the DMA and
4320 * SCSI FIFOs were flushed.
4323 if (fatal) {
4324 if (!hostdata->dstat_valid) {
4325 hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
4326 hostdata->dstat_valid = 1;
4329 /* XXX - code check for 700/800 chips */
4330 if (!(hostdata->dstat & DSTAT_DFE)) {
4331 printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
4332 if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4333 printk ("scsi%d: Flushing DMA FIFO\n",
4334 host->host_no);
4335 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
4336 while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
4337 DSTAT_DFE));
4338 } else {
4339 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
4340 while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
4342 hostdata->dstat |= DSTAT_DFE;
4348 * Function : do_NCR53c7x0_intr()
4350 * Purpose : A quick wrapper function added to grab the io_request_lock
4351 * spin lock prior to entering the real interrupt handler. Needed
4352 * for 2.1.95 and above.
4354 static void
4355 do_NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs) {
4356 unsigned long flags;
4358 spin_lock_irqsave(&io_request_lock, flags);
4359 NCR53c7x0_intr(irq, dev_id, regs);
4360 spin_unlock_irqrestore(&io_request_lock, flags);
4364 * Function : static void NCR53c7x0_intr (int irq, void *dev_id, struct pt_regs * regs)
4366 * Purpose : handle NCR53c7x0 interrupts for all NCR devices sharing
4367 * the same IRQ line.
4369 * Inputs : Since we're using the SA_INTERRUPT interrupt handler
4370 * semantics, irq indicates the interrupt which invoked
4371 * this handler.
4374 static void
4375 NCR53c7x0_intr (int irq, void *dev_id, struct pt_regs * regs) {
4376 NCR53c7x0_local_declare();
4377 struct Scsi_Host *host; /* Host we are looking at */
4378 unsigned char istat; /* Values of interrupt regs */
4379 struct NCR53c7x0_hostdata *hostdata; /* host->hostdata */
4380 struct NCR53c7x0_cmd *cmd, /* command which halted */
4381 **cmd_prev_ptr;
4382 u32 *dsa; /* DSA */
4383 int done = 1; /* Indicates when handler
4384 should terminate */
4385 int interrupted = 0; /* This HA generated
4386 an interrupt */
4387 int have_intfly; /* Don't print warning
4388 messages when we stack
4389 INTFLYs */
4390 unsigned long flags;
4392 #ifdef NCR_DEBUG
4393 char buf[80]; /* Debugging sprintf buffer */
4394 size_t buflen; /* Length of same */
4395 #endif
4396 do {
4397 done = 1;
4398 for (host = first_host; host; host = host->next)
4399 if (host->hostt == the_template && host->irq == irq) {
4400 NCR53c7x0_local_setup(host);
4402 hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
4403 hostdata->dsp_changed = 0;
4404 interrupted = 0;
4405 have_intfly = 0;
4407 do {
4408 int is_8xx_chip;
4410 hostdata->dstat_valid = 0;
4411 interrupted = 0;
4413 * Only read istat once, since reading it again will unstack
4414 * interrupts?
4416 istat = NCR53c7x0_read8(hostdata->istat);
4419 * INTFLY interrupts are used by the NCR53c720, NCR53c810,
4420 * and NCR53c820 to signify completion of a command. Since
4421 * the SCSI processor continues running, we can't just look
4422 * at the contents of the DSA register and continue running.
4424 /* XXX - this is too big, offends my sense of aesthetics, and should
4425 move to intr_intfly() */
4426 is_8xx_chip = ((unsigned) (hostdata->chip - 800)) < 100;
4427 if ((hostdata->options & OPTION_INTFLY) &&
4428 (is_8xx_chip && (istat & ISTAT_800_INTF))) {
4429 char search_found = 0; /* Got at least one ? */
4430 done = 0;
4431 interrupted = 1;
4434 * Clear the INTF bit by writing a one.
4435 * This reset operation is self-clearing.
4437 NCR53c7x0_write8(hostdata->istat, istat|ISTAT_800_INTF);
4439 if (hostdata->options & OPTION_DEBUG_INTR)
4440 printk ("scsi%d : INTFLY\n", host->host_no);
4443 * Traverse our list of running commands, and look
4444 * for those with valid (non-0xff ff) status and message
4445 * bytes encoded in the result which signify command
4446 * completion.
4450 save_flags(flags);
4451 cli();
4452 restart:
4453 for (cmd_prev_ptr = (struct NCR53c7x0_cmd **)
4454 &(hostdata->running_list), cmd =
4455 (struct NCR53c7x0_cmd *) hostdata->running_list; cmd ;
4456 cmd_prev_ptr = (struct NCR53c7x0_cmd **) &(cmd->next),
4457 cmd = (struct NCR53c7x0_cmd *) cmd->next) {
4458 Scsi_Cmnd *tmp;
4460 if (!cmd) {
4461 printk("scsi%d : very weird.\n", host->host_no);
4462 break;
4465 if (!(tmp = cmd->cmd)) {
4466 printk("scsi%d : weird. NCR53c7x0_cmd has no Scsi_Cmnd\n",
4467 host->host_no);
4468 continue;
4470 #if 0
4471 printk ("scsi%d : looking at result of 0x%x\n",
4472 host->host_no, cmd->cmd->result);
4473 #endif
4475 #ifdef __powerpc__
4476 if (tmp->result == le32_to_cpu(0xffff))
4477 continue;
4478 tmp->result = le32_to_cpu(tmp->result);
4479 #else
4480 if (((tmp->result & 0xff) == 0xff) ||
4481 ((tmp->result & 0xff00) == 0xff00))
4482 continue;
4483 #endif
4485 search_found = 1;
4487 /* Important - remove from list _before_ done is called */
4488 if (cmd_prev_ptr)
4489 *cmd_prev_ptr = (struct NCR53c7x0_cmd *) cmd->next;
4491 --hostdata->busy[tmp->target][tmp->lun];
4492 cmd->next = hostdata->free;
4493 hostdata->free = cmd;
4495 tmp->host_scribble = NULL;
4497 if (hostdata->options & OPTION_DEBUG_INTR) {
4498 printk ("scsi%d : command complete : pid %lu, id %d,lun %d result 0x%x ",
4499 host->host_no, tmp->pid, tmp->target, tmp->lun, tmp->result);
4500 print_command (tmp->cmnd);
4503 #if 0
4504 hostdata->options &= ~OPTION_DEBUG_INTR;
4505 #endif
4506 tmp->scsi_done(tmp);
4507 goto restart;
4510 restore_flags(flags);
4513 * I think that we're stacking INTFLY interrupts; taking care of
4514 * all the finished commands on the first one, and then getting
4515 * worried when we see the next one. The magic with have_intfly
4516 * should tell if this is the case..
4519 if (!search_found && !have_intfly) {
4520 printk ("scsi%d : WARNING : INTFLY with no completed commands.\n",
4521 host->host_no);
4522 } else if (!have_intfly) {
4523 have_intfly = 1;
4524 run_process_issue_queue();
4528 if (istat & (ISTAT_SIP|ISTAT_DIP)) {
4529 done = 0;
4530 interrupted = 1;
4531 hostdata->state = STATE_HALTED;
4533 if (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
4534 SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK)
4535 printk ("scsi%d : SCSI FIFO not empty\n",
4536 host->host_no);
4539 * NCR53c700 and NCR53c700-66 change the current SCSI
4540 * process, hostdata->curr, in the Linux driver so
4541 * cmd = hostdata->curr.
4543 * With other chips, we must look through the commands
4544 * executing and find the command structure which
4545 * corresponds to the DSA register.
4548 if (hostdata->options & OPTION_700) {
4549 cmd = (struct NCR53c7x0_cmd *) hostdata->curr;
4550 } else {
4551 dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
4552 for (cmd = (struct NCR53c7x0_cmd *)
4553 hostdata->running_list; cmd &&
4554 (dsa + (hostdata->dsa_start / sizeof(u32))) !=
4555 cmd->dsa;
4556 cmd = (struct NCR53c7x0_cmd *)(cmd->next));
4558 if (hostdata->options & OPTION_DEBUG_INTR) {
4559 if (cmd) {
4560 printk("scsi%d : interrupt for pid %lu, id %d, lun %d ",
4561 host->host_no, cmd->cmd->pid, (int) cmd->cmd->target,
4562 (int) cmd->cmd->lun);
4563 print_command (cmd->cmd->cmnd);
4564 } else {
4565 printk("scsi%d : no active command\n", host->host_no);
4568 if (istat & ISTAT_SIP) {
4569 if (hostdata->options & OPTION_DEBUG_INTR)
4570 printk ("scsi%d : ISTAT_SIP\n", host->host_no);
4571 intr_scsi (host, cmd);
4574 if (istat & ISTAT_DIP) {
4575 if (hostdata->options & OPTION_DEBUG_INTR)
4576 printk ("scsi%d : ISTAT_DIP\n", host->host_no);
4577 intr_dma (host, cmd);
4580 if (!hostdata->dstat_valid) {
4581 hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
4582 hostdata->dstat_valid = 1;
4585 /* XXX - code check for 700/800 chips */
4586 if (!(hostdata->dstat & DSTAT_DFE)) {
4587 printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
4588 if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4589 printk ("scsi%d: Flushing DMA FIFO\n",
4590 host->host_no);
4591 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
4592 while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
4593 DSTAT_DFE));
4594 } else
4596 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
4597 while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
4599 hostdata->dstat |= DSTAT_DFE;
4602 } while (interrupted);
4606 if (hostdata->intrs != -1)
4607 hostdata->intrs++;
4608 #if 0
4609 if (hostdata->intrs > 40) {
4610 printk("scsi%d : too many interrupts, halting", host->host_no);
4611 disable(host);
4613 #endif
4615 if (!hostdata->idle && hostdata->state == STATE_HALTED) {
4616 if (!hostdata->dsp_changed) {
4617 hostdata->dsp = (u32 *)
4618 bus_to_virt(NCR53c7x0_read32(DSP_REG));
4621 #if 0
4622 printk("scsi%d : new dsp is 0x%lx (virt 0x%p)\n",
4623 host->host_no, virt_to_bus(hostdata->dsp), hostdata->dsp);
4624 #endif
4626 hostdata->state = STATE_RUNNING;
4627 NCR53c7x0_write32 (DSP_REG, virt_to_bus(hostdata->dsp));
4630 } while (!done);
4635 * Function : static int abort_connected (struct Scsi_Host *host)
4637 * Purpose : Assuming that the NCR SCSI processor is currently
4638 * halted, break the currently established nexus. Clean
4639 * up of the NCR53c7x0_cmd and Scsi_Cmnd structures should
4640 * be done on receipt of the abort interrupt.
4642 * Inputs : host - SCSI host
4646 static int
4647 abort_connected (struct Scsi_Host *host) {
4648 #ifdef NEW_ABORT
4649 NCR53c7x0_local_declare();
4650 #endif
4651 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4652 host->hostdata;
4653 /* FIXME : this probably should change for production kernels; at the
4654 least, counter should move to a per-host structure. */
4655 static int counter = 5;
4656 #ifdef NEW_ABORT
4657 int sstat, phase, offset;
4658 u32 *script;
4659 NCR53c7x0_local_setup(host);
4660 #endif
4662 if (--counter <= 0) {
4663 disable(host);
4664 return 0;
4667 printk ("scsi%d : DANGER : abort_connected() called \n",
4668 host->host_no);
4670 #ifdef NEW_ABORT
4673 * New strategy : Rather than using a generic abort routine,
4674 * we'll specifically try to source or sink the appropriate
4675 * amount of data for the phase we're currently in (taking into
4676 * account the current synchronous offset)
4679 sstat = (NCR53c8x0_read8 ((chip / 100) == 8 ? SSTAT1_REG : SSTAT2_REG);
4680 offset = OFFSET (sstat & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
4681 phase = sstat & SSTAT2_PHASE_MASK;
4684 * SET ATN
4685 * MOVE source_or_sink, WHEN CURRENT PHASE
4686 * < repeat for each outstanding byte >
4687 * JUMP send_abort_message
4690 script = hostdata->abort_script = kmalloc (
4691 8 /* instruction size */ * (
4692 1 /* set ATN */ +
4693 (!offset ? 1 : offset) /* One transfer per outstanding byte */ +
4694 1 /* send abort message */),
4695 GFP_ATOMIC);
4698 #else /* def NEW_ABORT */
4699 hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4700 sizeof(u32);
4701 #endif /* def NEW_ABORT */
4702 hostdata->dsp_changed = 1;
4704 /* XXX - need to flag the command as aborted after the abort_connected
4705 code runs
4707 return 0;
4711 * Function : static int datapath_residual (Scsi_Host *host)
4713 * Purpose : return residual data count of what's in the chip.
4715 * Inputs : host - SCSI host
4718 static int
4719 datapath_residual (struct Scsi_Host *host) {
4720 NCR53c7x0_local_declare();
4721 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4722 host->hostdata;
4723 int count, synchronous, sstat;
4724 NCR53c7x0_local_setup(host);
4725 /* COMPAT : the 700 and 700-66 need to use DFIFO_00_BO_MASK */
4726 count = ((NCR53c7x0_read8 (DFIFO_REG) & DFIFO_10_BO_MASK) -
4727 (NCR53c7x0_read32 (DBC_REG) & DFIFO_10_BO_MASK)) & DFIFO_10_BO_MASK;
4728 synchronous = NCR53c7x0_read8 (SXFER_REG) & SXFER_MO_MASK;
4729 /* COMPAT : DDIR is elsewhere on non-'8xx chips. */
4730 if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4731 /* Receive */
4732 if (synchronous)
4733 count += (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
4734 SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
4735 else
4736 if (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
4737 SSTAT0_REG : SSTAT1_REG) & SSTAT1_ILF)
4738 ++count;
4739 } else {
4740 /* Send */
4741 sstat = ((hostdata->chip / 100) == 8) ? NCR53c7x0_read8 (SSTAT0_REG) :
4742 NCR53c7x0_read8 (SSTAT1_REG);
4743 if (sstat & SSTAT1_OLF)
4744 ++count;
4745 if (synchronous && (sstat & SSTAT1_ORF))
4746 ++count;
4748 return count;
4752 * Function : static const char * sbcl_to_phase (int sbcl)_
4754 * Purpose : Convert SBCL register to user-parsable phase representation
4756 * Inputs : sbcl - value of sbcl register
4760 static const char *
4761 sbcl_to_phase (int sbcl) {
4762 switch (sbcl & SBCL_PHASE_MASK) {
4763 case SBCL_PHASE_DATAIN:
4764 return "DATAIN";
4765 case SBCL_PHASE_DATAOUT:
4766 return "DATAOUT";
4767 case SBCL_PHASE_MSGIN:
4768 return "MSGIN";
4769 case SBCL_PHASE_MSGOUT:
4770 return "MSGOUT";
4771 case SBCL_PHASE_CMDOUT:
4772 return "CMDOUT";
4773 case SBCL_PHASE_STATIN:
4774 return "STATUSIN";
4775 default:
4776 return "unknown";
4781 * Function : static const char * sstat2_to_phase (int sstat)_
4783 * Purpose : Convert SSTAT2 register to user-parsable phase representation
4785 * Inputs : sstat - value of sstat register
4789 static const char *
4790 sstat2_to_phase (int sstat) {
4791 switch (sstat & SSTAT2_PHASE_MASK) {
4792 case SSTAT2_PHASE_DATAIN:
4793 return "DATAIN";
4794 case SSTAT2_PHASE_DATAOUT:
4795 return "DATAOUT";
4796 case SSTAT2_PHASE_MSGIN:
4797 return "MSGIN";
4798 case SSTAT2_PHASE_MSGOUT:
4799 return "MSGOUT";
4800 case SSTAT2_PHASE_CMDOUT:
4801 return "CMDOUT";
4802 case SSTAT2_PHASE_STATIN:
4803 return "STATUSIN";
4804 default:
4805 return "unknown";
4810 * Function : static void intr_phase_mismatch (struct Scsi_Host *host,
4811 * struct NCR53c7x0_cmd *cmd)
4813 * Purpose : Handle phase mismatch interrupts
4815 * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
4816 * may be NULL.
4818 * Side effects : The abort_connected() routine is called or the NCR chip
4819 * is restarted, jumping to the command_complete entry point, or
4820 * patching the address and transfer count of the current instruction
4821 * and calling the msg_in entry point as appropriate.
4824 static void
4825 intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
4826 NCR53c7x0_local_declare();
4827 u32 dbc_dcmd, *dsp, *dsp_next;
4828 unsigned char dcmd, sbcl;
4829 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4830 host->hostdata;
4831 int residual;
4832 enum {ACTION_ABORT, ACTION_ABORT_PRINT, ACTION_CONTINUE} action =
4833 ACTION_ABORT_PRINT;
4834 const char *where = NULL;
4835 NCR53c7x0_local_setup(host);
4838 * Corrective action is based on where in the SCSI SCRIPT(tm) the error
4839 * occurred, as well as which SCSI phase we are currently in.
4841 dsp_next = bus_to_virt(NCR53c7x0_read32(DSP_REG));
4844 * Fetch the current instruction, and remove the operands for easier
4845 * interpretation.
4847 dbc_dcmd = NCR53c7x0_read32(DBC_REG);
4848 dcmd = (dbc_dcmd & 0xff000000) >> 24;
4850 * Like other processors, the NCR adjusts the instruction pointer before
4851 * instruction decode. Set the DSP address back to what it should
4852 * be for this instruction based on its size (2 or 3 32 bit words).
4854 dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
4858 * Read new SCSI phase from the SBCL lines. Since all of our code uses
4859 * a WHEN conditional instead of an IF conditional, we don't need to
4860 * wait for a new REQ.
4862 sbcl = NCR53c7x0_read8(SBCL_REG) & SBCL_PHASE_MASK;
4864 if (!cmd) {
4865 action = ACTION_ABORT_PRINT;
4866 where = "no current command";
4868 * The way my SCSI SCRIPTS(tm) are architected, recoverable phase
4869 * mismatches should only occur where we're doing a multi-byte
4870 * BMI instruction. Specifically, this means
4872 * - select messages (a SCSI-I target may ignore additional messages
4873 * after the IDENTIFY; any target may reject a SDTR or WDTR)
4875 * - command out (targets may send a message to signal an error
4876 * condition, or go into STATUSIN after they've decided
4877 * they don't like the command.
4879 * - reply_message (targets may reject a multi-byte message in the
4880 * middle)
4882 * - data transfer routines (command completion with buffer space
4883 * left, disconnect message, or error message)
4885 } else if (((dsp >= cmd->data_transfer_start &&
4886 dsp < cmd->data_transfer_end)) || dsp == (cmd->residual + 2)) {
4887 if ((dcmd & (DCMD_TYPE_MASK|DCMD_BMI_OP_MASK|DCMD_BMI_INDIRECT|
4888 DCMD_BMI_MSG|DCMD_BMI_CD)) == (DCMD_TYPE_BMI|
4889 DCMD_BMI_OP_MOVE_I)) {
4890 residual = datapath_residual (host);
4891 if (hostdata->options & OPTION_DEBUG_DISCONNECT)
4892 printk ("scsi%d : handling residual transfer (+ %d bytes from DMA FIFO)\n",
4893 host->host_no, residual);
4896 * The first instruction is a CALL to the alternate handler for
4897 * this data transfer phase, so we can do calls to
4898 * munge_msg_restart as we would if control were passed
4899 * from normal dynamic code.
4901 if (dsp != cmd->residual + 2) {
4902 cmd->residual[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
4903 ((dcmd & DCMD_BMI_IO) ? DCMD_TCI_IO : 0)) << 24) |
4904 DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
4905 cmd->residual[1] = le32_to_cpu(virt_to_bus(hostdata->script)
4906 + ((dcmd & DCMD_BMI_IO)
4907 ? hostdata->E_other_in : hostdata->E_other_out));
4911 * The second instruction is the a data transfer block
4912 * move instruction, reflecting the pointer and count at the
4913 * time of the phase mismatch.
4915 cmd->residual[2] = le32_to_cpu(dbc_dcmd + residual);
4916 cmd->residual[3] = le32_to_cpu(NCR53c7x0_read32(DNAD_REG) - residual);
4919 * The third and final instruction is a jump to the instruction
4920 * which follows the instruction which had to be 'split'
4922 if (dsp != cmd->residual + 2) {
4923 cmd->residual[4] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP)
4924 << 24) | DBC_TCI_TRUE);
4925 cmd->residual[5] = le32_to_cpu(virt_to_bus(dsp_next));
4929 * For the sake of simplicity, transfer control to the
4930 * conditional CALL at the start of the residual buffer.
4932 hostdata->dsp = cmd->residual;
4933 hostdata->dsp_changed = 1;
4934 action = ACTION_CONTINUE;
4935 } else {
4936 where = "non-BMI dynamic DSA code";
4937 action = ACTION_ABORT_PRINT;
4939 } else if (dsp == (hostdata->script + hostdata->E_select_msgout / 4)) {
4940 /* Release ATN */
4941 NCR53c7x0_write8 (SOCL_REG, 0);
4942 switch (sbcl) {
4944 * Some devices (SQ555 come to mind) grab the IDENTIFY message
4945 * sent on selection, and decide to go into COMMAND OUT phase
4946 * rather than accepting the rest of the messages or rejecting
4947 * them. Handle these devices gracefully.
4949 case SBCL_PHASE_CMDOUT:
4950 hostdata->dsp = dsp + 2 /* two _words_ */;
4951 hostdata->dsp_changed = 1;
4952 printk ("scsi%d : target %d ignored SDTR and went into COMMAND OUT\n",
4953 host->host_no, cmd->cmd->target);
4954 cmd->flags &= ~CMD_FLAG_SDTR;
4955 action = ACTION_CONTINUE;
4956 break;
4957 case SBCL_PHASE_MSGIN:
4958 hostdata->dsp = hostdata->script + hostdata->E_msg_in /
4959 sizeof(u32);
4960 hostdata->dsp_changed = 1;
4961 action = ACTION_CONTINUE;
4962 break;
4963 default:
4964 where="select message out";
4965 action = ACTION_ABORT_PRINT;
4968 * Some SCSI devices will interpret a command as they read the bytes
4969 * off the SCSI bus, and may decide that the command is Bogus before
4970 * they've read the entire command off the bus.
4972 } else if (dsp == hostdata->script + hostdata->E_cmdout_cmdout / sizeof
4973 (u32)) {
4974 hostdata->dsp = hostdata->script + hostdata->E_data_transfer /
4975 sizeof (u32);
4976 hostdata->dsp_changed = 1;
4977 action = ACTION_CONTINUE;
4978 /* FIXME : we need to handle message reject, etc. within msg_respond. */
4979 #ifdef notyet
4980 } else if (dsp == hostdata->script + hostdata->E_reply_message) {
4981 switch (sbcl) {
4982 /* Any other phase mismatches abort the currently executing command. */
4983 #endif
4984 } else {
4985 where = "unknown location";
4986 action = ACTION_ABORT_PRINT;
4989 /* Flush DMA FIFO */
4990 if (!hostdata->dstat_valid) {
4991 hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
4992 hostdata->dstat_valid = 1;
4994 if (!(hostdata->dstat & DSTAT_DFE)) {
4995 if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4996 printk ("scsi%d: Flushing DMA FIFO\n",
4997 host->host_no);
4998 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
4999 /* FIXME : what about stacked DMA interrupts? */
5000 while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
5001 DSTAT_DFE));
5002 } else {
5003 NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
5004 while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
5006 hostdata->dstat |= DSTAT_DFE;
5009 switch (action) {
5010 case ACTION_ABORT_PRINT:
5011 printk("scsi%d : %s : unexpected phase %s.\n",
5012 host->host_no, where ? where : "unknown location",
5013 sbcl_to_phase(sbcl));
5014 print_lots (host);
5015 /* Fall through to ACTION_ABORT */
5016 case ACTION_ABORT:
5017 abort_connected (host);
5018 break;
5019 case ACTION_CONTINUE:
5020 break;
5023 #if 0
5024 if (hostdata->dsp_changed) {
5025 printk("scsi%d: new dsp 0x%p\n", host->host_no, hostdata->dsp);
5026 print_insn (host, hostdata->dsp, "", 1);
5028 #endif
5033 * Function : static void intr_bf (struct Scsi_Host *host,
5034 * struct NCR53c7x0_cmd *cmd)
5036 * Purpose : handle BUS FAULT interrupts
5038 * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
5039 * may be NULL.
5042 static void
5043 intr_bf (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
5044 NCR53c7x0_local_declare();
5045 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5046 host->hostdata;
5047 u32 *dsp,
5048 *next_dsp, /* Current dsp */
5049 *dsa,
5050 dbc_dcmd; /* DCMD (high eight bits) + DBC */
5051 unsigned short pci_status;
5052 int tmp;
5053 unsigned long flags;
5054 char *reason = NULL;
5055 /* Default behavior is for a silent error, with a retry until we've
5056 exhausted retries. */
5057 enum {MAYBE, ALWAYS, NEVER} retry = MAYBE;
5058 int report = 0;
5059 NCR53c7x0_local_setup(host);
5061 dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
5062 next_dsp = bus_to_virt (NCR53c7x0_read32(DSP_REG));
5063 dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
5064 /* FIXME - check chip type */
5065 dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
5068 * Bus faults can be caused by either a Bad Address or
5069 * Target Abort. We should check the Received Target Abort
5070 * bit of the PCI status register and Master Abort Bit.
5072 * - Master Abort bit indicates that no device claimed
5073 * the address with DEVSEL within five clocks
5075 * - Target Abort bit indicates that a target claimed it,
5076 * but changed its mind once it saw the byte enables.
5080 if ((hostdata->chip / 100) == 8) {
5081 save_flags (flags);
5082 cli();
5083 tmp = pcibios_read_config_word (hostdata->pci_bus,
5084 hostdata->pci_device_fn, PCI_STATUS, &pci_status);
5085 restore_flags (flags);
5086 if (tmp == PCIBIOS_SUCCESSFUL) {
5087 if (pci_status & PCI_STATUS_REC_TARGET_ABORT) {
5088 reason = "PCI target abort";
5089 pci_status &= ~PCI_STATUS_REC_TARGET_ABORT;
5090 } else if (pci_status & PCI_STATUS_REC_MASTER_ABORT) {
5091 reason = "No device asserted PCI DEVSEL within five bus clocks";
5092 pci_status &= ~PCI_STATUS_REC_MASTER_ABORT;
5093 } else if (pci_status & PCI_STATUS_PARITY) {
5094 report = 1;
5095 pci_status &= ~PCI_STATUS_PARITY;
5097 } else {
5098 printk ("scsi%d : couldn't read status register : error %d\n",
5099 host->host_no, tmp);
5100 retry = NEVER;
5104 #ifndef notyet
5105 report = 1;
5106 #endif
5107 if (report && reason) {
5108 printk(KERN_ALERT "scsi%d : BUS FAULT reason = %s\n",
5109 host->host_no, reason ? reason : "unknown");
5110 print_lots (host);
5113 #ifndef notyet
5114 retry = NEVER;
5115 #endif
5118 * TODO : we should attempt to recover from any spurious bus
5119 * faults. After X retries, we should figure that things are
5120 * sufficiently wedged, and call NCR53c7xx_reset.
5122 * This code should only get executed once we've decided that we
5123 * cannot retry.
5126 if (retry == NEVER) {
5127 printk(KERN_ALERT " mail drew@PoohSticks.ORG\n");
5128 FATAL (host);
5133 * Function : static void intr_dma (struct Scsi_Host *host,
5134 * struct NCR53c7x0_cmd *cmd)
5136 * Purpose : handle all DMA interrupts, indicated by the setting
5137 * of the DIP bit in the ISTAT register.
5139 * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
5140 * may be NULL.
5143 static void
5144 intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
5145 NCR53c7x0_local_declare();
5146 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5147 host->hostdata;
5148 unsigned char dstat; /* DSTAT */
5149 u32 *dsp,
5150 *next_dsp, /* Current dsp */
5151 *dsa,
5152 dbc_dcmd; /* DCMD (high eight bits) + DBC */
5153 int tmp;
5154 unsigned long flags;
5155 NCR53c7x0_local_setup(host);
5157 if (!hostdata->dstat_valid) {
5158 hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
5159 hostdata->dstat_valid = 1;
5162 dstat = hostdata->dstat;
5164 if (hostdata->options & OPTION_DEBUG_INTR)
5165 printk("scsi%d : DSTAT=0x%x\n", host->host_no, (int) dstat);
5167 dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
5168 next_dsp = bus_to_virt(NCR53c7x0_read32(DSP_REG));
5169 dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
5170 /* XXX - check chip type */
5171 dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
5174 * DSTAT_ABRT is the aborted interrupt. This is set whenever the
5175 * SCSI chip is aborted.
5177 * With NCR53c700 and NCR53c700-66 style chips, we should only
5178 * get this when the chip is currently running the accept
5179 * reselect/select code and we have set the abort bit in the
5180 * ISTAT register.
5184 if (dstat & DSTAT_ABRT) {
5185 #if 0
5186 /* XXX - add code here to deal with normal abort */
5187 if ((hostdata->options & OPTION_700) && (hostdata->state ==
5188 STATE_ABORTING)) {
5189 } else
5190 #endif
5192 printk(KERN_ALERT "scsi%d : unexpected abort interrupt at\n"
5193 " ", host->host_no);
5194 print_insn (host, dsp, KERN_ALERT "s ", 1);
5195 FATAL (host);
5200 * DSTAT_SSI is the single step interrupt. Should be generated
5201 * whenever we have single stepped or are tracing.
5204 if (dstat & DSTAT_SSI) {
5205 if (hostdata->options & OPTION_DEBUG_TRACE) {
5206 } else if (hostdata->options & OPTION_DEBUG_SINGLE) {
5207 print_insn (host, dsp, "s ", 0);
5208 save_flags(flags);
5209 cli();
5210 /* XXX - should we do this, or can we get away with writing dsp? */
5212 NCR53c7x0_write8 (DCNTL_REG, (NCR53c7x0_read8(DCNTL_REG) &
5213 ~DCNTL_SSM) | DCNTL_STD);
5214 restore_flags(flags);
5215 } else {
5216 printk(KERN_ALERT "scsi%d : unexpected single step interrupt at\n"
5217 " ", host->host_no);
5218 print_insn (host, dsp, KERN_ALERT "", 1);
5219 printk(KERN_ALERT " mail drew@PoohSticks.ORG\n");
5220 FATAL (host);
5225 * DSTAT_IID / DSTAT_OPC (same bit, same meaning, only the name
5226 * is different) is generated whenever an illegal instruction is
5227 * encountered.
5229 * XXX - we may want to emulate INTFLY here, so we can use
5230 * the same SCSI SCRIPT (tm) for NCR53c710 through NCR53c810
5231 * chips.
5234 if (dstat & DSTAT_OPC) {
5236 * Ascertain if this IID interrupts occurred before or after a STO
5237 * interrupt. Since the interrupt handling code now leaves
5238 * DSP unmodified until _after_ all stacked interrupts have been
5239 * processed, reading the DSP returns the original DSP register.
5240 * This means that if dsp lies between the select code, and
5241 * message out following the selection code (where the IID interrupt
5242 * would have to have occurred by due to the implicit wait for REQ),
5243 * we have an IID interrupt resulting from a STO condition and
5244 * can ignore it.
5247 if (((dsp >= (hostdata->script + hostdata->E_select / sizeof(u32))) &&
5248 (dsp <= (hostdata->script + hostdata->E_select_msgout /
5249 sizeof(u32) + 8))) || (hostdata->test_running == 2)) {
5250 if (hostdata->options & OPTION_DEBUG_INTR)
5251 printk ("scsi%d : ignoring DSTAT_IID for SSTAT_STO\n",
5252 host->host_no);
5253 if (hostdata->expecting_iid) {
5254 hostdata->expecting_iid = 0;
5255 hostdata->idle = 1;
5256 if (hostdata->test_running == 2) {
5257 hostdata->test_running = 0;
5258 hostdata->test_completed = 3;
5259 } else if (cmd)
5260 abnormal_finished (cmd, DID_BAD_TARGET << 16);
5261 } else {
5262 hostdata->expecting_sto = 1;
5265 * We can't guarantee we'll be able to execute the WAIT DISCONNECT
5266 * instruction within the 3.4us of bus free and arbitration delay
5267 * that a target can RESELECT in and assert REQ after we've dropped
5268 * ACK. If this happens, we'll get an illegal instruction interrupt.
5269 * Doing away with the WAIT DISCONNECT instructions broke everything,
5270 * so instead I'll settle for moving one WAIT DISCONNECT a few
5271 * instructions closer to the CLEAR ACK before it to minimize the
5272 * chances of this happening, and handle it if it occurs anyway.
5274 * Simply continue with what we were doing, and control should
5275 * be transfered to the schedule routine which will ultimately
5276 * pass control onto the reselection or selection (not yet)
5277 * code.
5279 } else if (dbc_dcmd == 0x48000000 && (NCR53c7x0_read8 (SBCL_REG) &
5280 SBCL_REQ)) {
5281 if (!(hostdata->options & OPTION_NO_PRINT_RACE))
5283 printk("scsi%d: REQ before WAIT DISCONNECT IID\n",
5284 host->host_no);
5285 hostdata->options |= OPTION_NO_PRINT_RACE;
5287 } else {
5288 printk(KERN_ALERT "scsi%d : illegal instruction\n", host->host_no);
5289 print_lots (host);
5290 printk(KERN_ALERT " mail drew@PoohSticks.ORG with ALL\n"
5291 " boot messages and diagnostic output\n");
5292 FATAL (host);
5297 * DSTAT_BF are bus fault errors
5300 if (dstat & DSTAT_800_BF) {
5301 intr_bf (host, cmd);
5306 * DSTAT_SIR interrupts are generated by the execution of
5307 * the INT instruction. Since the exact values available
5308 * are determined entirely by the SCSI script running,
5309 * and are local to a particular script, a unique handler
5310 * is called for each script.
5313 if (dstat & DSTAT_SIR) {
5314 if (hostdata->options & OPTION_DEBUG_INTR)
5315 printk ("scsi%d : DSTAT_SIR\n", host->host_no);
5316 switch ((tmp = hostdata->dstat_sir_intr (host, cmd))) {
5317 case SPECIFIC_INT_NOTHING:
5318 case SPECIFIC_INT_RESTART:
5319 break;
5320 case SPECIFIC_INT_ABORT:
5321 abort_connected(host);
5322 break;
5323 case SPECIFIC_INT_PANIC:
5324 printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
5325 print_insn (host, dsp, KERN_ALERT "", 1);
5326 printk(KERN_ALERT " dstat_sir_intr() returned SPECIFIC_INT_PANIC\n");
5327 FATAL (host);
5328 break;
5329 case SPECIFIC_INT_BREAK:
5330 intr_break (host, cmd);
5331 break;
5332 default:
5333 printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
5334 print_insn (host, dsp, KERN_ALERT "", 1);
5335 printk(KERN_ALERT" dstat_sir_intr() returned unknown value %d\n",
5336 tmp);
5337 FATAL (host);
5341 if ((hostdata->chip / 100) == 8 && (dstat & DSTAT_800_MDPE)) {
5342 printk(KERN_ALERT "scsi%d : Master Data Parity Error\n",
5343 host->host_no);
5344 FATAL (host);
5349 * Function : static int print_insn (struct Scsi_Host *host,
5350 * u32 *insn, int kernel)
5352 * Purpose : print numeric representation of the instruction pointed
5353 * to by insn to the debugging or kernel message buffer
5354 * as appropriate.
5356 * If desired, a user level program can interpret this
5357 * information.
5359 * Inputs : host, insn - host, pointer to instruction, prefix -
5360 * string to prepend, kernel - use printk instead of debugging buffer.
5362 * Returns : size, in u32s, of instruction printed.
5366 * FIXME: should change kernel parameter so that it takes an ENUM
5367 * specifying severity - either KERN_ALERT or KERN_PANIC so
5368 * all panic messages are output with the same severity.
5371 static int
5372 print_insn (struct Scsi_Host *host, const u32 *insn,
5373 const char *prefix, int kernel) {
5374 char buf[160], /* Temporary buffer and pointer. ICKY
5375 arbitrary length. */
5378 *tmp;
5379 unsigned char dcmd; /* dcmd register for *insn */
5380 int size;
5383 * Check to see if the instruction pointer is not bogus before
5384 * indirecting through it; avoiding red-zone at start of
5385 * memory.
5387 * FIXME: icky magic needs to happen here on non-intel boxes which
5388 * don't have kernel memory mapped in like this. Might be reasonable
5389 * to use vverify()?
5392 if (virt_to_phys((void *)insn) < PAGE_SIZE ||
5393 virt_to_phys((void *)(insn + 8)) > virt_to_phys(high_memory) ||
5394 ((((dcmd = (insn[0] >> 24) & 0xff) & DCMD_TYPE_MMI) == DCMD_TYPE_MMI) &&
5395 virt_to_phys((void *)(insn + 12)) > virt_to_phys(high_memory))) {
5396 size = 0;
5397 sprintf (buf, "%s%p: address out of range\n",
5398 prefix, insn);
5399 } else {
5401 * FIXME : (void *) cast in virt_to_bus should be unnecessary, because
5402 * it should take const void * as argument.
5404 sprintf(buf, "%s0x%lx (virt 0x%p) : 0x%08x 0x%08x (virt 0x%p)",
5405 (prefix ? prefix : ""), virt_to_bus((void *) insn), insn,
5406 insn[0], insn[1], bus_to_virt (le32_to_cpu(insn[1])));
5407 tmp = buf + strlen(buf);
5408 if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI) {
5409 sprintf (tmp, " 0x%08x (virt 0x%p)\n", insn[2],
5410 bus_to_virt(le32_to_cpu(insn[2])));
5411 size = 3;
5412 } else {
5413 sprintf (tmp, "\n");
5414 size = 2;
5418 if (kernel)
5419 printk ("%s", buf);
5420 #ifdef NCR_DEBUG
5421 else {
5422 size_t len = strlen(buf);
5423 debugger_kernel_write(host, buf, len);
5425 #endif
5426 return size;
5430 * Function : static const char *ncr_state (int state)
5432 * Purpose : convert state (probably from hostdata->state) to a string
5434 * Inputs : state
5436 * Returns : char * representation of state, "unknown" on error.
5439 #if 0
5440 static const char *
5441 ncr_state (int state) {
5442 switch (state) {
5443 case STATE_HALTED: return "halted";
5444 case STATE_WAITING: return "waiting";
5445 case STATE_RUNNING: return "running";
5446 case STATE_ABORTING: return "aborting";
5447 case STATE_DISABLED: return "disabled";
5448 default: return "unknown";
5451 #endif
5454 * Function : int NCR53c7xx_abort (Scsi_Cmnd *cmd)
5456 * Purpose : Abort an errant SCSI command, doing all necessary
5457 * cleanup of the issue_queue, running_list, shared Linux/NCR
5458 * dsa issue and reconnect queues.
5460 * Inputs : cmd - command to abort, code - entire result field
5462 * Returns : 0 on success, -1 on failure.
5465 int
5466 NCR53c7xx_abort (Scsi_Cmnd *cmd) {
5467 NCR53c7x0_local_declare();
5468 struct Scsi_Host *host = cmd->host;
5469 struct NCR53c7x0_hostdata *hostdata = host ? (struct NCR53c7x0_hostdata *)
5470 host->hostdata : NULL;
5471 unsigned long flags;
5472 unsigned long result;
5473 struct NCR53c7x0_cmd *curr, **prev;
5474 Scsi_Cmnd *me, **last;
5475 #if 0
5476 static long cache_pid = -1;
5477 #endif
5480 if (!host) {
5481 printk ("Bogus SCSI command pid %ld; no host structure\n",
5482 cmd->pid);
5483 return SCSI_ABORT_ERROR;
5484 } else if (!hostdata) {
5485 printk ("Bogus SCSI host %d; no hostdata\n", host->host_no);
5486 return SCSI_ABORT_ERROR;
5488 NCR53c7x0_local_setup(host);
5491 * CHECK : I don't think that reading ISTAT will unstack any interrupts,
5492 * since we need to write the INTF bit to clear it, and SCSI/DMA
5493 * interrupts don't clear until we read SSTAT/SIST and DSTAT registers.
5495 * See that this is the case.
5497 * I suspect that several of our failures may be coming from a new fatal
5498 * interrupt (possibly due to a phase mismatch) happening after we've left
5499 * the interrupt handler, but before the PIC has had the interrupt condition
5500 * cleared.
5503 if (NCR53c7x0_read8(hostdata->istat) &
5504 (ISTAT_DIP|ISTAT_SIP|
5505 (hostdata->chip / 100 == 8 ? ISTAT_800_INTF : 0))) {
5506 printk ("scsi%d : dropped interrupt for command %ld\n", host->host_no,
5507 cmd->pid);
5508 NCR53c7x0_intr (host->irq, NULL, NULL);
5509 return SCSI_ABORT_BUSY;
5512 save_flags(flags);
5513 cli();
5514 #if 0
5515 if (cache_pid == cmd->pid)
5516 panic ("scsi%d : bloody fetus %d\n", host->host_no, cmd->pid);
5517 else
5518 cache_pid = cmd->pid;
5519 #endif
5523 * The command could be hiding in the issue_queue. This would be very
5524 * nice, as commands can't be moved from the high level driver's issue queue
5525 * into the shared queue until an interrupt routine is serviced, and this
5526 * moving is atomic.
5528 * If this is the case, we don't have to worry about anything - we simply
5529 * pull the command out of the old queue, and call it aborted.
5532 for (me = (Scsi_Cmnd *) hostdata->issue_queue,
5533 last = (Scsi_Cmnd **) &(hostdata->issue_queue);
5534 me && me != cmd; last = (Scsi_Cmnd **)&(me->SCp.ptr),
5535 me = (Scsi_Cmnd *)me->SCp.ptr);
5537 if (me) {
5538 *last = (Scsi_Cmnd *) me->SCp.ptr;
5539 if (me->host_scribble) {
5540 ((struct NCR53c7x0_cmd *)me->host_scribble)->next = hostdata->free;
5541 hostdata->free = (struct NCR53c7x0_cmd *) me->host_scribble;
5542 me->host_scribble = NULL;
5544 cmd->result = DID_ABORT << 16;
5545 cmd->scsi_done(cmd);
5546 printk ("scsi%d : found command %ld in Linux issue queue\n",
5547 host->host_no, me->pid);
5548 restore_flags(flags);
5549 run_process_issue_queue();
5550 return SCSI_ABORT_SUCCESS;
5554 * That failing, the command could be in our list of already executing
5555 * commands. If this is the case, drastic measures are called for.
5558 for (curr = (struct NCR53c7x0_cmd *) hostdata->running_list,
5559 prev = (struct NCR53c7x0_cmd **) &(hostdata->running_list);
5560 curr && curr->cmd != cmd; prev = (struct NCR53c7x0_cmd **)
5561 &(curr->next), curr = (struct NCR53c7x0_cmd *) curr->next);
5563 if (curr) {
5564 result = le32_to_cpu(cmd->result);
5565 if ((result & 0xff) != 0xff && (result & 0xff00) != 0xff00) {
5566 if (prev)
5567 *prev = (struct NCR53c7x0_cmd *) curr->next;
5568 curr->next = (struct NCR53c7x0_cmd *) hostdata->free;
5569 cmd->host_scribble = NULL;
5570 hostdata->free = curr;
5571 cmd->scsi_done(cmd);
5572 printk ("scsi%d : found finished command %ld in running list\n",
5573 host->host_no, cmd->pid);
5574 restore_flags(flags);
5575 return SCSI_ABORT_NOT_RUNNING;
5576 } else {
5577 printk ("scsi%d : DANGER : command running, can not abort.\n",
5578 cmd->host->host_no);
5579 restore_flags(flags);
5580 return SCSI_ABORT_BUSY;
5585 * And if we couldn't find it in any of our queues, it must have been
5586 * a dropped interrupt.
5589 curr = (struct NCR53c7x0_cmd *) cmd->host_scribble;
5590 if (curr) {
5591 curr->next = hostdata->free;
5592 hostdata->free = curr;
5593 cmd->host_scribble = NULL;
5596 result = le32_to_cpu(cmd->result);
5597 if (((result & 0xff00) == 0xff00) ||
5598 ((result & 0xff) == 0xff)) {
5599 printk ("scsi%d : did this command ever run?\n", host->host_no);
5600 cmd->result = DID_ABORT << 16;
5601 } else {
5602 printk ("scsi%d : probably lost INTFLY, normal completion\n",
5603 host->host_no);
5605 * FIXME : We need to add an additional flag which indicates if a
5606 * command was ever counted as BUSY, so if we end up here we can
5607 * decrement the busy count if and only if it is necessary.
5609 --hostdata->busy[cmd->target][cmd->lun];
5611 restore_flags(flags);
5612 cmd->scsi_done(cmd);
5615 * We need to run process_issue_queue since termination of this command
5616 * may allow another queued command to execute first?
5618 return SCSI_ABORT_NOT_RUNNING;
5622 * Function : int NCR53c7xx_reset (Scsi_Cmnd *cmd)
5624 * Purpose : perform a hard reset of the SCSI bus and NCR
5625 * chip.
5627 * Inputs : cmd - command which caused the SCSI RESET
5629 * Returns : 0 on success.
5632 int
5633 NCR53c7xx_reset (Scsi_Cmnd *cmd, unsigned int reset_flags) {
5634 NCR53c7x0_local_declare();
5635 unsigned long flags;
5636 int found = 0;
5637 struct NCR53c7x0_cmd * c;
5638 Scsi_Cmnd *tmp;
5640 * When we call scsi_done(), it's going to wake up anything sleeping on the
5641 * resources which were in use by the aborted commands, and we'll start to
5642 * get new commands.
5644 * We can't let this happen until after we've re-initialized the driver
5645 * structures, and can't reinitialize those structures until after we've
5646 * dealt with their contents.
5648 * So, we need to find all of the commands which were running, stick
5649 * them on a linked list of completed commands (we'll use the host_scribble
5650 * pointer), do our reinitialization, and then call the done function for
5651 * each command.
5653 Scsi_Cmnd *nuke_list = NULL;
5654 struct Scsi_Host *host = cmd->host;
5655 struct NCR53c7x0_hostdata *hostdata =
5656 (struct NCR53c7x0_hostdata *) host->hostdata;
5658 NCR53c7x0_local_setup(host);
5659 save_flags(flags);
5660 cli();
5661 ncr_halt (host);
5662 print_lots (host);
5663 dump_events (host, 30);
5664 ncr_scsi_reset (host);
5665 for (tmp = nuke_list = return_outstanding_commands (host, 1 /* free */,
5666 0 /* issue */ ); tmp; tmp = (Scsi_Cmnd *) tmp->SCp.buffer)
5667 if (tmp == cmd) {
5668 found = 1;
5669 break;
5673 * If we didn't find the command which caused this reset in our running
5674 * list, then we've lost it. See that it terminates normally anyway.
5676 if (!found) {
5677 c = (struct NCR53c7x0_cmd *) cmd->host_scribble;
5678 if (c) {
5679 cmd->host_scribble = NULL;
5680 c->next = hostdata->free;
5681 hostdata->free = c;
5682 } else
5683 printk ("scsi%d: lost command %ld\n", host->host_no, cmd->pid);
5684 cmd->SCp.buffer = (struct scatterlist *) nuke_list;
5685 nuke_list = cmd;
5688 NCR53c7x0_driver_init (host);
5689 hostdata->soft_reset (host);
5690 if (hostdata->resets == 0)
5691 disable(host);
5692 else if (hostdata->resets != -1)
5693 --hostdata->resets;
5694 restore_flags(flags);
5695 for (; nuke_list; nuke_list = tmp) {
5696 tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
5697 nuke_list->result = DID_RESET << 16;
5698 nuke_list->scsi_done (nuke_list);
5700 restore_flags(flags);
5701 return SCSI_RESET_SUCCESS;
5705 * The NCR SDMS bios follows Annex A of the SCSI-CAM draft, and
5706 * therefore shares the scsicam_bios_param function.
5710 * Function : int insn_to_offset (Scsi_Cmnd *cmd, u32 *insn)
5712 * Purpose : convert instructions stored at NCR pointer into data
5713 * pointer offset.
5715 * Inputs : cmd - SCSI command; insn - pointer to instruction. Either current
5716 * DSP, or saved data pointer.
5718 * Returns : offset on success, -1 on failure.
5722 static int
5723 insn_to_offset (Scsi_Cmnd *cmd, u32 *insn) {
5724 struct NCR53c7x0_hostdata *hostdata =
5725 (struct NCR53c7x0_hostdata *) cmd->host->hostdata;
5726 struct NCR53c7x0_cmd *ncmd =
5727 (struct NCR53c7x0_cmd *) cmd->host_scribble;
5728 int offset = 0, buffers;
5729 struct scatterlist *segment;
5730 char *ptr;
5731 int found = 0;
5734 * With the current code implementation, if the insn is inside dynamically
5735 * generated code, the data pointer will be the instruction preceding
5736 * the next transfer segment.
5739 if (!check_address ((unsigned long) ncmd, sizeof (struct NCR53c7x0_cmd)) &&
5740 ((insn >= ncmd->data_transfer_start &&
5741 insn < ncmd->data_transfer_end) ||
5742 (insn >= ncmd->residual &&
5743 insn < (ncmd->residual +
5744 sizeof(ncmd->residual))))) {
5745 ptr = bus_to_virt(le32_to_cpu(insn[3]));
5747 if ((buffers = cmd->use_sg)) {
5748 for (offset = 0,
5749 segment = (struct scatterlist *) cmd->buffer;
5750 buffers && !((found = ((ptr >= segment->address) &&
5751 (ptr < (segment->address + segment->length)))));
5752 --buffers, offset += segment->length, ++segment)
5753 #if 0
5754 printk("scsi%d: comparing 0x%p to 0x%p\n",
5755 cmd->host->host_no, saved, segment->address);
5756 #else
5758 #endif
5759 offset += ptr - segment->address;
5760 } else {
5761 found = 1;
5762 offset = ptr - (char *) (cmd->request_buffer);
5764 } else if ((insn >= hostdata->script +
5765 hostdata->E_data_transfer / sizeof(u32)) &&
5766 (insn <= hostdata->script +
5767 hostdata->E_end_data_transfer / sizeof(u32))) {
5768 found = 1;
5769 offset = 0;
5771 return found ? offset : -1;
5777 * Function : void print_progress (Scsi_Cmnd *cmd)
5779 * Purpose : print the current location of the saved data pointer
5781 * Inputs : cmd - command we are interested in
5785 static void
5786 print_progress (Scsi_Cmnd *cmd) {
5787 NCR53c7x0_local_declare();
5788 struct NCR53c7x0_cmd *ncmd =
5789 (struct NCR53c7x0_cmd *) cmd->host_scribble;
5790 int offset, i;
5791 char *where;
5792 u32 *ptr;
5793 NCR53c7x0_local_setup (cmd->host);
5794 for (i = 0; i < 2; ++i) {
5795 if (check_address ((unsigned long) ncmd,
5796 sizeof (struct NCR53c7x0_cmd)) == -1)
5797 continue;
5798 if (!i) {
5799 where = "saved";
5800 ptr = bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer));
5801 } else {
5802 where = "active";
5803 ptr = bus_to_virt (NCR53c7x0_read32 (DSP_REG) -
5804 NCR53c7x0_insn_size (NCR53c7x0_read8 (DCMD_REG)) *
5805 sizeof(u32));
5807 offset = insn_to_offset (cmd, ptr);
5809 if (offset != -1)
5810 printk ("scsi%d : %s data pointer at offset %d\n",
5811 cmd->host->host_no, where, offset);
5812 else {
5813 int size;
5814 printk ("scsi%d : can't determine %s data pointer offset\n",
5815 cmd->host->host_no, where);
5816 if (ncmd) {
5817 size = print_insn (cmd->host,
5818 bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer)), "", 1);
5819 print_insn (cmd->host,
5820 bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer)) + size * sizeof(u32),
5821 "", 1);
5828 static void
5829 print_dsa (struct Scsi_Host *host, u32 *dsa, const char *prefix) {
5830 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5831 host->hostdata;
5832 int i, len;
5833 char *ptr;
5834 Scsi_Cmnd *cmd;
5836 if (check_address ((unsigned long) dsa, hostdata->dsa_end -
5837 hostdata->dsa_start) == -1) {
5838 printk("scsi%d : bad dsa virt 0x%p\n", host->host_no, dsa);
5839 return;
5841 printk("%sscsi%d : dsa at phys 0x%lx (virt 0x%p)\n"
5842 " + %d : dsa_msgout length = %u, data = 0x%x (virt 0x%p)\n" ,
5843 prefix ? prefix : "",
5844 host->host_no, virt_to_bus (dsa), dsa, hostdata->dsa_msgout,
5845 le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]),
5846 le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1]),
5847 bus_to_virt (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1])));
5850 * Only print messages if they're sane in length so we don't
5851 * blow the kernel printk buffer on something which won't buy us
5852 * anything.
5855 if (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]) <
5856 sizeof (hostdata->free->select))
5857 for (i = le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]),
5858 ptr = bus_to_virt (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1]));
5859 i > 0 && !check_address ((unsigned long) ptr, 1);
5860 ptr += len, i -= len) {
5861 printk(" ");
5862 len = print_msg (ptr);
5863 printk("\n");
5864 if (!len)
5865 break;
5868 printk(" + %d : select_indirect = 0x%x\n",
5869 hostdata->dsa_select, le32_to_cpu(dsa[hostdata->dsa_select / sizeof(u32)]));
5870 cmd = (Scsi_Cmnd *) bus_to_virt(le32_to_cpu(dsa[hostdata->dsa_cmnd / sizeof(u32)]));
5871 printk(" + %d : dsa_cmnd = 0x%x ", hostdata->dsa_cmnd,
5872 (u32) virt_to_bus(cmd));
5873 if (cmd) {
5874 printk(" result = 0x%x, target = %d, lun = %d, cmd = ",
5875 cmd->result, cmd->target, cmd->lun);
5876 print_command(cmd->cmnd);
5877 } else
5878 printk("\n");
5879 printk(" + %d : dsa_next = 0x%x\n", hostdata->dsa_next,
5880 le32_to_cpu(dsa[hostdata->dsa_next / sizeof(u32)]));
5881 if (cmd) {
5882 printk("scsi%d target %d : sxfer_sanity = 0x%x, scntl3_sanity = 0x%x\n"
5883 " script : ",
5884 host->host_no, cmd->target,
5885 hostdata->sync[cmd->target].sxfer_sanity,
5886 hostdata->sync[cmd->target].scntl3_sanity);
5887 for (i = 0; i < (sizeof(hostdata->sync[cmd->target].script) / 4); ++i)
5888 printk ("0x%x ", hostdata->sync[cmd->target].script[i]);
5889 printk ("\n");
5890 print_progress (cmd);
5894 * Function : void print_queues (Scsi_Host *host)
5896 * Purpose : print the contents of the NCR issue and reconnect queues
5898 * Inputs : host - SCSI host we are interested in
5902 static void
5903 print_queues (struct Scsi_Host *host) {
5904 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5905 host->hostdata;
5906 u32 *dsa, *next_dsa;
5907 volatile u32 *curr;
5908 int left;
5909 Scsi_Cmnd *cmd, *next_cmd;
5910 unsigned long flags;
5912 printk ("scsi%d : issue queue\n", host->host_no);
5914 for (left = host->can_queue, cmd = (Scsi_Cmnd *) hostdata->issue_queue;
5915 left >= 0 && cmd;
5916 cmd = next_cmd) {
5917 next_cmd = (Scsi_Cmnd *) cmd->SCp.ptr;
5918 save_flags(flags);
5919 cli();
5920 if (cmd->host_scribble) {
5921 if (check_address ((unsigned long) (cmd->host_scribble),
5922 sizeof (cmd->host_scribble)) == -1)
5923 printk ("scsi%d: scsi pid %ld bad pointer to NCR53c7x0_cmd\n",
5924 host->host_no, cmd->pid);
5925 /* print_dsa does sanity check on address, no need to check */
5926 else
5927 print_dsa (host, bus_to_virt(le32_to_cpu(((struct NCR53c7x0_cmd *) cmd->host_scribble)-> dsa)), "");
5928 } else
5929 printk ("scsi%d : scsi pid %ld for target %d lun %d has no NCR53c7x0_cmd\n",
5930 host->host_no, cmd->pid, cmd->target, cmd->lun);
5931 restore_flags(flags);
5934 if (left <= 0) {
5935 printk ("scsi%d : loop detected in issue queue\n",
5936 host->host_no);
5940 * Traverse the NCR reconnect and start DSA structures, printing out
5941 * each element until we hit the end or detect a loop. Currently,
5942 * the reconnect structure is a linked list; and the start structure
5943 * is an array. Eventually, the reconnect structure will become a
5944 * list as well, since this simplifies the code.
5947 printk ("scsi%d : schedule dsa array :\n", host->host_no);
5948 for (left = host->can_queue, curr = hostdata->schedule;
5949 left > 0; curr += 2, --left)
5950 if (curr[0] != hostdata->NOP_insn)
5951 /* FIXME : convert pointer to dsa_begin to pointer to dsa. */
5952 print_dsa (host, bus_to_virt (le32_to_cpu(curr[1]) -
5953 (hostdata->E_dsa_code_begin -
5954 hostdata->E_dsa_code_template)), "");
5955 printk ("scsi%d : end schedule dsa array\n", host->host_no);
5957 printk ("scsi%d : reconnect_dsa_head :\n", host->host_no);
5959 for (left = host->can_queue,
5960 dsa = bus_to_virt (le32_to_cpu(hostdata->reconnect_dsa_head));
5961 left >= 0 && dsa;
5962 dsa = next_dsa) {
5963 save_flags (flags);
5964 cli();
5965 if (check_address ((unsigned long) dsa, sizeof(dsa)) == -1) {
5966 printk ("scsi%d: bad DSA pointer 0x%p", host->host_no,
5967 dsa);
5968 next_dsa = NULL;
5970 else
5972 next_dsa = bus_to_virt(le32_to_cpu(dsa[hostdata->dsa_next / sizeof(u32)]));
5973 print_dsa (host, dsa, "");
5975 restore_flags(flags);
5977 printk ("scsi%d : end reconnect_dsa_head\n", host->host_no);
5978 if (left < 0)
5979 printk("scsi%d: possible loop in ncr reconnect list\n",
5980 host->host_no);
5983 static void
5984 print_lots (struct Scsi_Host *host) {
5985 NCR53c7x0_local_declare();
5986 struct NCR53c7x0_hostdata *hostdata =
5987 (struct NCR53c7x0_hostdata *) host->hostdata;
5988 u32 *dsp_next, *dsp, *dsa, dbc_dcmd;
5989 unsigned char dcmd, sbcl;
5990 int i, size;
5991 NCR53c7x0_local_setup(host);
5993 if ((dsp_next = bus_to_virt(NCR53c7x0_read32 (DSP_REG)))) {
5994 dbc_dcmd = NCR53c7x0_read32(DBC_REG);
5995 dcmd = (dbc_dcmd & 0xff000000) >> 24;
5996 dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
5997 dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
5998 sbcl = NCR53c7x0_read8 (SBCL_REG);
6001 printk ("scsi%d : DCMD|DBC=0x%x, DNAD=0x%x (virt 0x%p)\n"
6002 " DSA=0x%lx (virt 0x%p)\n"
6003 " DSPS=0x%x, TEMP=0x%x (virt 0x%p), DMODE=0x%x\n"
6004 " SXFER=0x%x, SCNTL3=0x%x\n"
6005 " %s%s%sphase=%s, %d bytes in SCSI FIFO\n"
6006 " STEST0=0x%x\n",
6007 host->host_no, dbc_dcmd, NCR53c7x0_read32(DNAD_REG),
6008 bus_to_virt(NCR53c7x0_read32(DNAD_REG)),
6009 virt_to_bus(dsa), dsa,
6010 NCR53c7x0_read32(DSPS_REG), NCR53c7x0_read32(TEMP_REG),
6011 bus_to_virt (NCR53c7x0_read32(TEMP_REG)),
6012 (int) NCR53c7x0_read8(hostdata->dmode),
6013 (int) NCR53c7x0_read8(SXFER_REG),
6014 (int) NCR53c7x0_read8(SCNTL3_REG_800),
6015 (sbcl & SBCL_BSY) ? "BSY " : "",
6016 (sbcl & SBCL_SEL) ? "SEL " : "",
6017 (sbcl & SBCL_REQ) ? "REQ " : "",
6018 sstat2_to_phase(NCR53c7x0_read8 (((hostdata->chip / 100) == 8) ?
6019 SSTAT1_REG : SSTAT2_REG)),
6020 (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
6021 SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT,
6022 NCR53c7x0_read8 (STEST0_REG_800));
6023 printk ("scsi%d : DSP 0x%lx (virt 0x%p) ->\n", host->host_no,
6024 virt_to_bus(dsp), dsp);
6025 for (i = 6; i > 0; --i, dsp += size)
6026 size = print_insn (host, dsp, "", 1);
6027 if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
6028 printk ("scsi%d : connected (SDID=0x%x, SSID=0x%x)\n",
6029 host->host_no, NCR53c7x0_read8 (SDID_REG_800),
6030 NCR53c7x0_read8 (SSID_REG_800));
6031 print_dsa (host, dsa, "");
6034 #if 1
6035 print_queues (host);
6036 #endif
6041 * Function : static int shutdown (struct Scsi_Host *host)
6043 * Purpose : does a clean (we hope) shutdown of the NCR SCSI
6044 * chip. Use prior to dumping core, unloading the NCR driver,
6046 * Returns : 0 on success
6048 static int
6049 shutdown (struct Scsi_Host *host) {
6050 NCR53c7x0_local_declare();
6051 unsigned long flags;
6052 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6053 host->hostdata;
6054 NCR53c7x0_local_setup(host);
6055 save_flags (flags);
6056 cli();
6057 /* Get in a state where we can reset the SCSI bus */
6058 ncr_halt (host);
6059 ncr_scsi_reset (host);
6060 hostdata->soft_reset(host);
6062 disable (host);
6063 restore_flags (flags);
6064 return 0;
6068 * Function : void ncr_scsi_reset (struct Scsi_Host *host)
6070 * Purpose : reset the SCSI bus.
6073 static void
6074 ncr_scsi_reset (struct Scsi_Host *host) {
6075 NCR53c7x0_local_declare();
6076 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6077 host->hostdata;
6078 unsigned long flags;
6079 int sien = 0;
6080 NCR53c7x0_local_setup(host);
6081 save_flags (flags);
6082 cli();
6083 if ((hostdata->chip / 100) == 8) {
6084 sien = NCR53c7x0_read8(SIEN0_REG_800);
6085 NCR53c7x0_write8(SIEN0_REG_800, sien & ~SIEN_RST);
6087 NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
6088 udelay(25); /* Minimum amount of time to assert RST */
6089 NCR53c7x0_write8(SCNTL1_REG, 0);
6090 if ((hostdata->chip / 100) == 8) {
6091 NCR53c7x0_write8(SIEN0_REG_800, sien);
6093 restore_flags (flags);
6097 * Function : void hard_reset (struct Scsi_Host *host)
6101 static void
6102 hard_reset (struct Scsi_Host *host) {
6103 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6104 host->hostdata;
6105 unsigned long flags;
6106 save_flags (flags);
6107 cli();
6108 ncr_scsi_reset(host);
6109 NCR53c7x0_driver_init (host);
6110 if (hostdata->soft_reset)
6111 hostdata->soft_reset (host);
6112 restore_flags(flags);
6117 * Function : Scsi_Cmnd *return_outstanding_commands (struct Scsi_Host *host,
6118 * int free, int issue)
6120 * Purpose : return a linked list (using the SCp.buffer field as next,
6121 * so we don't perturb hostdata. We don't use a field of the
6122 * NCR53c7x0_cmd structure since we may not have allocated one
6123 * for the command causing the reset.) of Scsi_Cmnd structures that
6124 * had propagated below the Linux issue queue level. If free is set,
6125 * free the NCR53c7x0_cmd structures which are associated with
6126 * the Scsi_Cmnd structures, and clean up any internal
6127 * NCR lists that the commands were on. If issue is set,
6128 * also return commands in the issue queue.
6130 * Returns : linked list of commands
6132 * NOTE : the caller should insure that the NCR chip is halted
6133 * if the free flag is set.
6136 static Scsi_Cmnd *
6137 return_outstanding_commands (struct Scsi_Host *host, int free, int issue) {
6138 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6139 host->hostdata;
6140 struct NCR53c7x0_cmd *c;
6141 int i;
6142 u32 *curr;
6143 Scsi_Cmnd *list = NULL, *tmp;
6144 for (c = (struct NCR53c7x0_cmd *) hostdata->running_list; c;
6145 c = (struct NCR53c7x0_cmd *) c->next) {
6146 if (c->cmd->SCp.buffer) {
6147 printk ("scsi%d : loop detected in running list!\n", host->host_no);
6148 break;
6149 } else {
6150 printk ("Duh? Bad things happening in the NCR driver\n");
6151 break;
6154 c->cmd->SCp.buffer = (struct scatterlist *) list;
6155 list = c->cmd;
6156 if (free) {
6157 c->next = hostdata->free;
6158 hostdata->free = c;
6162 if (free) {
6163 for (i = 0, curr = (u32 *) hostdata->schedule;
6164 i < host->can_queue; ++i, curr += 2) {
6165 curr[0] = hostdata->NOP_insn;
6166 curr[1] = le32_to_cpu(0xdeadbeef);
6168 hostdata->curr = NULL;
6171 if (issue) {
6172 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; tmp = tmp->next) {
6173 if (tmp->SCp.buffer) {
6174 printk ("scsi%d : loop detected in issue queue!\n",
6175 host->host_no);
6176 break;
6178 tmp->SCp.buffer = (struct scatterlist *) list;
6179 list = tmp;
6181 if (free)
6182 hostdata->issue_queue = NULL;
6185 return list;
6189 * Function : static int disable (struct Scsi_Host *host)
6191 * Purpose : disables the given NCR host, causing all commands
6192 * to return a driver error. Call this so we can unload the
6193 * module during development and try again. Eventually,
6194 * we should be able to find clean workarounds for these
6195 * problems.
6197 * Inputs : host - hostadapter to twiddle
6199 * Returns : 0 on success.
6202 static int
6203 disable (struct Scsi_Host *host) {
6204 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6205 host->hostdata;
6206 unsigned long flags;
6207 Scsi_Cmnd *nuke_list, *tmp;
6208 save_flags(flags);
6209 cli();
6210 if (hostdata->state != STATE_HALTED)
6211 ncr_halt (host);
6212 nuke_list = return_outstanding_commands (host, 1 /* free */, 1 /* issue */);
6213 hard_reset (host);
6214 hostdata->state = STATE_DISABLED;
6215 restore_flags(flags);
6216 printk ("scsi%d : nuking commands\n", host->host_no);
6217 for (; nuke_list; nuke_list = tmp) {
6218 tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
6219 nuke_list->result = DID_ERROR << 16;
6220 nuke_list->scsi_done(nuke_list);
6222 printk ("scsi%d : done. \n", host->host_no);
6223 printk (KERN_ALERT "scsi%d : disabled. Unload and reload\n",
6224 host->host_no);
6225 return 0;
6229 * Function : static int ncr_halt (struct Scsi_Host *host)
6231 * Purpose : halts the SCSI SCRIPTS(tm) processor on the NCR chip
6233 * Inputs : host - SCSI chip to halt
6235 * Returns : 0 on success
6238 static int
6239 ncr_halt (struct Scsi_Host *host) {
6240 NCR53c7x0_local_declare();
6241 unsigned long flags;
6242 unsigned char istat, tmp;
6243 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6244 host->hostdata;
6245 int stage;
6246 NCR53c7x0_local_setup(host);
6248 save_flags(flags);
6249 cli();
6250 /* Stage 0 : eat all interrupts
6251 Stage 1 : set ABORT
6252 Stage 2 : eat all but abort interrupts
6253 Stage 3 : eat all interrupts
6255 for (stage = 0;;) {
6256 if (stage == 1) {
6257 NCR53c7x0_write8(hostdata->istat, ISTAT_ABRT);
6258 ++stage;
6260 istat = NCR53c7x0_read8 (hostdata->istat);
6261 if (istat & ISTAT_SIP) {
6262 if ((hostdata->chip / 100) == 8) {
6263 tmp = NCR53c7x0_read8(SIST0_REG_800);
6264 udelay(1);
6265 tmp = NCR53c7x0_read8(SIST1_REG_800);
6266 } else {
6267 tmp = NCR53c7x0_read8(SSTAT0_REG);
6269 } else if (istat & ISTAT_DIP) {
6270 tmp = NCR53c7x0_read8(DSTAT_REG);
6271 if (stage == 2) {
6272 if (tmp & DSTAT_ABRT) {
6273 NCR53c7x0_write8(hostdata->istat, 0);
6274 ++stage;
6275 } else {
6276 printk(KERN_ALERT "scsi%d : could not halt NCR chip\n",
6277 host->host_no);
6278 disable (host);
6282 if (!(istat & (ISTAT_SIP|ISTAT_DIP))) {
6283 if (stage == 0)
6284 ++stage;
6285 else if (stage == 3)
6286 break;
6289 hostdata->state = STATE_HALTED;
6290 restore_flags(flags);
6291 #if 0
6292 print_lots (host);
6293 #endif
6294 return 0;
6298 * Function: event_name (int event)
6300 * Purpose: map event enum into user-readable strings.
6303 static const char *
6304 event_name (int event) {
6305 switch (event) {
6306 case EVENT_NONE: return "none";
6307 case EVENT_ISSUE_QUEUE: return "to issue queue";
6308 case EVENT_START_QUEUE: return "to start queue";
6309 case EVENT_SELECT: return "selected";
6310 case EVENT_DISCONNECT: return "disconnected";
6311 case EVENT_RESELECT: return "reselected";
6312 case EVENT_COMPLETE: return "completed";
6313 case EVENT_IDLE: return "idle";
6314 case EVENT_SELECT_FAILED: return "select failed";
6315 case EVENT_BEFORE_SELECT: return "before select";
6316 case EVENT_RESELECT_FAILED: return "reselect failed";
6317 default: return "unknown";
6322 * Function : void dump_events (struct Scsi_Host *host, count)
6324 * Purpose : print last count events which have occurred.
6326 static void
6327 dump_events (struct Scsi_Host *host, int count) {
6328 struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6329 host->hostdata;
6330 struct NCR53c7x0_event event;
6331 int i;
6332 unsigned long flags;
6333 if (hostdata->events) {
6334 if (count > hostdata->event_size)
6335 count = hostdata->event_size;
6336 for (i = hostdata->event_index; count > 0;
6337 i = (i ? i - 1 : hostdata->event_size -1), --count) {
6338 save_flags(flags);
6340 * By copying the event we're currently examining with interrupts
6341 * disabled, we can do multiple printk(), etc. operations and
6342 * still be guaranteed that they're happening on the same
6343 * event structure.
6345 cli();
6346 #if 0
6347 event = hostdata->events[i];
6348 #else
6349 memcpy ((void *) &event, (void *) &(hostdata->events[i]),
6350 sizeof(event));
6351 #endif
6353 restore_flags(flags);
6354 printk ("scsi%d : %s event %d at %ld secs %ld usecs target %d lun %d\n",
6355 host->host_no, event_name (event.event), count,
6356 (long) event.time.tv_sec, (long) event.time.tv_usec,
6357 event.target, event.lun);
6358 if (event.dsa)
6359 printk (" event for dsa 0x%lx (virt 0x%p)\n",
6360 virt_to_bus(event.dsa), event.dsa);
6361 if (event.pid != -1) {
6362 printk (" event for pid %ld ", event.pid);
6363 print_command (event.cmnd);
6370 * Function: check_address
6372 * Purpose: Check to see if a possibly corrupt pointer will fault the
6373 * kernel.
6375 * Inputs: addr - address; size - size of area
6377 * Returns: 0 if area is OK, -1 on error.
6379 * NOTES: should be implemented in terms of vverify on kernels
6380 * that have it.
6383 static int
6384 check_address (unsigned long addr, int size) {
6385 return (virt_to_phys((void *)addr) < PAGE_SIZE || virt_to_phys((void *)(addr + size)) > virt_to_phys(high_memory) ? -1 : 0);
6388 #ifdef MODULE
6389 int
6390 NCR53c7x0_release(struct Scsi_Host *host) {
6391 struct NCR53c7x0_hostdata *hostdata =
6392 (struct NCR53c7x0_hostdata *) host->hostdata;
6393 struct NCR53c7x0_cmd *cmd, *tmp;
6394 shutdown (host);
6395 if (host->irq != IRQ_NONE)
6397 int irq_count;
6398 struct Scsi_Host *tmp;
6399 for (irq_count = 0, tmp = first_host; tmp; tmp = tmp->next)
6400 if (tmp->hostt == the_template && tmp->irq == host->irq)
6401 ++irq_count;
6402 if (irq_count == 1)
6403 free_irq(host->irq, NULL);
6405 if (host->dma_channel != DMA_NONE)
6406 free_dma(host->dma_channel);
6407 if (host->io_port)
6408 release_region(host->io_port, host->n_io_port);
6410 for (cmd = (struct NCR53c7x0_cmd *) hostdata->free; cmd; cmd = tmp,
6411 --hostdata->num_cmds) {
6412 tmp = (struct NCR53c7x0_cmd *) cmd->next;
6414 * If we're going to loop, try to stop it to get a more accurate
6415 * count of the leaked commands.
6417 cmd->next = NULL;
6418 if (cmd->free)
6419 cmd->free ((void *) cmd->real, cmd->size);
6421 if (hostdata->num_cmds)
6422 printk ("scsi%d : leaked %d NCR53c7x0_cmd structures\n",
6423 host->host_no, hostdata->num_cmds);
6424 if (hostdata->events)
6425 vfree ((void *)hostdata->events);
6426 return 1;
6428 #endif /* def MODULE */
6430 static Scsi_Host_Template driver_template = NCR53c7xx;
6431 #include "scsi_module.c"