]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/NCR5380.c
ncr5380: Remove UNSAFE macro
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / NCR5380.c
CommitLineData
1da177e4
LT
1/*
2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
13 *
1da177e4
LT
14 * For more information, please consult
15 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
1da177e4 28 * Revision 1.10 1998/9/2 Alan Cox
fa195afe 29 * (alan@lxorguk.ukuu.org.uk)
1da177e4
LT
30 * Fixed up the timer lockups reported so far. Things still suck. Looking
31 * forward to 2.3 and per device request queues. Then it'll be possible to
32 * SMP thread this beast and improve life no end.
33
34 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
35 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
36 * (hopefully) fixed and enhanced USLEEP
37 * added support for DTC3181E card (for Mustek scanner)
38 *
39
40 * Revision 1.8 Ingmar Baumgart
41 * (ingmar@gonzo.schwaben.de)
42 * added support for NCR53C400a card
43 *
44
45 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
46 * added proc_info
47 * added support needed for DTC 3180/3280
48 * fixed a couple of bugs
49 *
50
51 * Revision 1.5 1994/01/19 09:14:57 drew
52 * Fixed udelay() hack that was being used on DATAOUT phases
53 * instead of a proper wait for the final handshake.
54 *
55 * Revision 1.4 1994/01/19 06:44:25 drew
56 * *** empty log message ***
57 *
58 * Revision 1.3 1994/01/19 05:24:40 drew
59 * Added support for TCR LAST_BYTE_SENT bit.
60 *
61 * Revision 1.2 1994/01/15 06:14:11 drew
62 * REAL DMA support, bug fixes.
63 *
64 * Revision 1.1 1994/01/15 06:00:54 drew
65 * Initial revision
66 *
67 */
68
69/*
70 * Further development / testing that should be done :
71 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
72 * code so that everything does the same thing that's done at the
73 * end of a pseudo-DMA read operation.
74 *
75 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
76 * basically, transfer size needs to be reduced by one
77 * and the last byte read as is done with PSEUDO_DMA.
78 *
79 * 4. Test SCSI-II tagged queueing (I have no devices which support
80 * tagged queueing)
1da177e4 81 */
db9dff36 82#include <scsi/scsi_dbg.h>
1abfd370 83#include <scsi/scsi_transport_spi.h>
1da177e4
LT
84
85#if (NDEBUG & NDEBUG_LISTS)
86#define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
87#define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
88#else
89#define LIST(x,y)
90#define REMOVE(w,x,y,z)
91#endif
92
93#ifndef notyet
1da177e4
LT
94#undef REAL_DMA
95#endif
96
97#ifdef REAL_DMA_POLL
98#undef READ_OVERRUNS
99#define READ_OVERRUNS
100#endif
101
102#ifdef BOARD_REQUIRES_NO_DELAY
103#define io_recovery_delay(x)
104#else
105#define io_recovery_delay(x) udelay(x)
106#endif
107
108/*
109 * Design
110 *
111 * This is a generic 5380 driver. To use it on a different platform,
112 * one simply writes appropriate system specific macros (ie, data
113 * transfer - some PC's will use the I/O bus, 68K's must use
114 * memory mapped) and drops this file in their 'C' wrapper.
115 *
116 * (Note from hch: unfortunately it was not enough for the different
117 * m68k folks and instead of improving this driver they copied it
118 * and hacked it up for their needs. As a consequence they lost
119 * most updates to this driver. Maybe someone will fix all these
120 * drivers to use a common core one day..)
121 *
122 * As far as command queueing, two queues are maintained for
123 * each 5380 in the system - commands that haven't been issued yet,
124 * and commands that are currently executing. This means that an
125 * unlimited number of commands may be queued, letting
126 * more commands propagate from the higher driver levels giving higher
127 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
128 * allowing multiple commands to propagate all the way to a SCSI-II device
129 * while a command is already executing.
130 *
131 *
132 * Issues specific to the NCR5380 :
133 *
134 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
135 * piece of hardware that requires you to sit in a loop polling for
136 * the REQ signal as long as you are connected. Some devices are
137 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
686f3990 138 * while doing long seek operations. [...] These
1da177e4
LT
139 * broken devices are the exception rather than the rule and I'd rather
140 * spend my time optimizing for the normal case.
141 *
142 * Architecture :
143 *
144 * At the heart of the design is a coroutine, NCR5380_main,
145 * which is started from a workqueue for each NCR5380 host in the
146 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
147 * removing the commands from the issue queue and calling
148 * NCR5380_select() if a nexus is not established.
149 *
150 * Once a nexus is established, the NCR5380_information_transfer()
151 * phase goes through the various phases as instructed by the target.
152 * if the target goes into MSG IN and sends a DISCONNECT message,
153 * the command structure is placed into the per instance disconnected
154 * queue, and NCR5380_main tries to find more work. If the target is
155 * idle for too long, the system will try to sleep.
156 *
157 * If a command has disconnected, eventually an interrupt will trigger,
158 * calling NCR5380_intr() which will in turn call NCR5380_reselect
159 * to reestablish a nexus. This will run main if necessary.
160 *
161 * On command termination, the done function will be called as
162 * appropriate.
163 *
164 * SCSI pointers are maintained in the SCp field of SCSI command
165 * structures, being initialized after the command is connected
166 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
167 * Note that in violation of the standard, an implicit SAVE POINTERS operation
168 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
169 */
170
171/*
172 * Using this file :
173 * This file a skeleton Linux SCSI driver for the NCR 5380 series
174 * of chips. To use it, you write an architecture specific functions
175 * and macros and include this file in your driver.
176 *
177 * These macros control options :
178 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
179 * defined.
180 *
181 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
182 * for commands that return with a CHECK CONDITION status.
183 *
184 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
185 * transceivers.
186 *
187 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
188 * override-configure an IRQ.
189 *
1da177e4
LT
190 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
191 *
192 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
193 *
194 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
195 * rely on phase mismatch and EOP interrupts to determine end
196 * of phase.
197 *
1da177e4
LT
198 * Defaults for these will be provided although the user may want to adjust
199 * these to allocate CPU resources to the SCSI driver or "real" code.
200 *
1da177e4 201 * These macros MUST be defined :
1da177e4
LT
202 *
203 * NCR5380_read(register) - read from the specified register
204 *
205 * NCR5380_write(register, value) - write to the specific register
206 *
207 * NCR5380_implementation_fields - additional fields needed for this
208 * specific implementation of the NCR5380
209 *
210 * Either real DMA *or* pseudo DMA may be implemented
211 * REAL functions :
212 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
213 * Note that the DMA setup functions should return the number of bytes
214 * that they were able to program the controller for.
215 *
216 * Also note that generic i386/PC versions of these macros are
217 * available as NCR5380_i386_dma_write_setup,
218 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
219 *
220 * NCR5380_dma_write_setup(instance, src, count) - initialize
221 * NCR5380_dma_read_setup(instance, dst, count) - initialize
222 * NCR5380_dma_residual(instance); - residual count
223 *
224 * PSEUDO functions :
225 * NCR5380_pwrite(instance, src, count)
226 * NCR5380_pread(instance, dst, count);
227 *
228 * The generic driver is initialized by calling NCR5380_init(instance),
229 * after setting the appropriate host specific fields and ID. If the
230 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
231 * possible) function may be used.
232 */
233
54d8fe44
FT
234static int do_abort(struct Scsi_Host *);
235static void do_reset(struct Scsi_Host *);
1da177e4
LT
236
237/*
238 * initialize_SCp - init the scsi pointer field
239 * @cmd: command block to set up
240 *
241 * Set up the internal fields in the SCSI command.
242 */
243
710ddd0d 244static inline void initialize_SCp(struct scsi_cmnd *cmd)
1da177e4
LT
245{
246 /*
247 * Initialize the Scsi Pointer field so that all of the commands in the
248 * various queues are valid.
249 */
250
9e0fe44d
BH
251 if (scsi_bufflen(cmd)) {
252 cmd->SCp.buffer = scsi_sglist(cmd);
253 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
45711f1a 254 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
1da177e4
LT
255 cmd->SCp.this_residual = cmd->SCp.buffer->length;
256 } else {
257 cmd->SCp.buffer = NULL;
258 cmd->SCp.buffers_residual = 0;
9e0fe44d
BH
259 cmd->SCp.ptr = NULL;
260 cmd->SCp.this_residual = 0;
1da177e4
LT
261 }
262}
263
264/**
2f854b82
FT
265 * NCR5380_poll_politely - wait for chip register value
266 * @instance: controller to poll
267 * @reg: 5380 register to poll
268 * @bit: Bitmask to check
269 * @val: Value required to exit
270 * @wait: Time-out in jiffies
271 *
272 * Polls the chip in a reasonably efficient manner waiting for an
273 * event to occur. After a short quick poll we begin to yield the CPU
274 * (if possible). In irq contexts the time-out is arbitrarily limited.
275 * Callers may hold locks as long as they are held in irq mode.
276 *
277 * Returns 0 if event occurred otherwise -ETIMEDOUT.
1da177e4 278 */
2f854b82
FT
279
280static int NCR5380_poll_politely(struct Scsi_Host *instance,
281 int reg, int bit, int val, int wait)
1da177e4 282{
2f854b82
FT
283 struct NCR5380_hostdata *hostdata = shost_priv(instance);
284 unsigned long deadline = jiffies + wait;
285 unsigned long n;
286
287 /* Busy-wait for up to 10 ms */
288 n = min(10000U, jiffies_to_usecs(wait));
289 n *= hostdata->accesses_per_ms;
290 n /= 1000;
291 do {
292 if ((NCR5380_read(reg) & bit) == val)
1da177e4
LT
293 return 0;
294 cpu_relax();
2f854b82
FT
295 } while (n--);
296
297 if (irqs_disabled() || in_interrupt())
298 return -ETIMEDOUT;
299
300 /* Repeatedly sleep for 1 ms until deadline */
301 while (time_is_after_jiffies(deadline)) {
302 schedule_timeout_uninterruptible(1);
303 if ((NCR5380_read(reg) & bit) == val)
1da177e4 304 return 0;
1da177e4 305 }
2f854b82 306
1da177e4
LT
307 return -ETIMEDOUT;
308}
309
310static struct {
311 unsigned char value;
312 const char *name;
702809ce 313} phases[] __maybe_unused = {
1da177e4
LT
314 {PHASE_DATAOUT, "DATAOUT"},
315 {PHASE_DATAIN, "DATAIN"},
316 {PHASE_CMDOUT, "CMDOUT"},
317 {PHASE_STATIN, "STATIN"},
318 {PHASE_MSGOUT, "MSGOUT"},
319 {PHASE_MSGIN, "MSGIN"},
320 {PHASE_UNKNOWN, "UNKNOWN"}
321};
322
185a7a1c 323#if NDEBUG
1da177e4
LT
324static struct {
325 unsigned char mask;
326 const char *name;
327} signals[] = {
328 {SR_DBP, "PARITY"},
329 {SR_RST, "RST"},
330 {SR_BSY, "BSY"},
331 {SR_REQ, "REQ"},
332 {SR_MSG, "MSG"},
333 {SR_CD, "CD"},
334 {SR_IO, "IO"},
335 {SR_SEL, "SEL"},
336 {0, NULL}
337},
338basrs[] = {
339 {BASR_ATN, "ATN"},
340 {BASR_ACK, "ACK"},
341 {0, NULL}
342},
343icrs[] = {
344 {ICR_ASSERT_RST, "ASSERT RST"},
345 {ICR_ASSERT_ACK, "ASSERT ACK"},
346 {ICR_ASSERT_BSY, "ASSERT BSY"},
347 {ICR_ASSERT_SEL, "ASSERT SEL"},
348 {ICR_ASSERT_ATN, "ASSERT ATN"},
349 {ICR_ASSERT_DATA, "ASSERT DATA"},
350 {0, NULL}
351},
352mrs[] = {
353 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
354 {MR_TARGET, "MODE TARGET"},
355 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
356 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
357 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
358 {MR_DMA_MODE, "MODE DMA"},
359 {MR_ARBITRATE, "MODE ARBITRATION"},
360 {0, NULL}
361};
362
363/**
364 * NCR5380_print - print scsi bus signals
365 * @instance: adapter state to dump
366 *
367 * Print the SCSI bus signals for debugging purposes
368 *
369 * Locks: caller holds hostdata lock (not essential)
370 */
371
372static void NCR5380_print(struct Scsi_Host *instance)
373{
1da177e4 374 unsigned char status, data, basr, mr, icr, i;
1da177e4
LT
375
376 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
377 status = NCR5380_read(STATUS_REG);
378 mr = NCR5380_read(MODE_REG);
379 icr = NCR5380_read(INITIATOR_COMMAND_REG);
380 basr = NCR5380_read(BUS_AND_STATUS_REG);
381
382 printk("STATUS_REG: %02x ", status);
383 for (i = 0; signals[i].mask; ++i)
384 if (status & signals[i].mask)
385 printk(",%s", signals[i].name);
386 printk("\nBASR: %02x ", basr);
387 for (i = 0; basrs[i].mask; ++i)
388 if (basr & basrs[i].mask)
389 printk(",%s", basrs[i].name);
390 printk("\nICR: %02x ", icr);
391 for (i = 0; icrs[i].mask; ++i)
392 if (icr & icrs[i].mask)
393 printk(",%s", icrs[i].name);
394 printk("\nMODE: %02x ", mr);
395 for (i = 0; mrs[i].mask; ++i)
396 if (mr & mrs[i].mask)
397 printk(",%s", mrs[i].name);
398 printk("\n");
399}
400
401
402/*
403 * NCR5380_print_phase - show SCSI phase
404 * @instance: adapter to dump
405 *
406 * Print the current SCSI phase for debugging purposes
407 *
408 * Locks: none
409 */
410
411static void NCR5380_print_phase(struct Scsi_Host *instance)
412{
1da177e4
LT
413 unsigned char status;
414 int i;
1da177e4
LT
415
416 status = NCR5380_read(STATUS_REG);
417 if (!(status & SR_REQ))
418 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
419 else {
420 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
421 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
422 }
423}
424#endif
425
1da177e4 426
d5f7e65d 427static int probe_irq __initdata;
1da177e4
LT
428
429/**
430 * probe_intr - helper for IRQ autoprobe
431 * @irq: interrupt number
432 * @dev_id: unused
433 * @regs: unused
434 *
435 * Set a flag to indicate the IRQ in question was received. This is
436 * used by the IRQ probe code.
437 */
438
7d12e780 439static irqreturn_t __init probe_intr(int irq, void *dev_id)
1da177e4
LT
440{
441 probe_irq = irq;
442 return IRQ_HANDLED;
443}
444
445/**
446 * NCR5380_probe_irq - find the IRQ of an NCR5380
447 * @instance: NCR5380 controller
448 * @possible: bitmask of ISA IRQ lines
449 *
450 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
451 * and then looking to see what interrupt actually turned up.
452 *
453 * Locks: none, irqs must be enabled on entry
454 */
455
702809ce
AM
456static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
457 int possible)
1da177e4 458{
1da177e4
LT
459 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
460 unsigned long timeout;
461 int trying_irqs, i, mask;
1da177e4 462
22f5f10d 463 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
4909cc2b 464 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
1da177e4
LT
465 trying_irqs |= mask;
466
4e5a800c 467 timeout = jiffies + msecs_to_jiffies(250);
22f5f10d 468 probe_irq = NO_IRQ;
1da177e4
LT
469
470 /*
471 * A interrupt is triggered whenever BSY = false, SEL = true
472 * and a bit set in the SELECT_ENABLE_REG is asserted on the
473 * SCSI bus.
474 *
475 * Note that the bus is only driven when the phase control signals
476 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
477 * to zero.
478 */
479
480 NCR5380_write(TARGET_COMMAND_REG, 0);
481 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
482 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
483 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
484
22f5f10d 485 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
a9a3047d 486 schedule_timeout_uninterruptible(1);
1da177e4
LT
487
488 NCR5380_write(SELECT_ENABLE_REG, 0);
489 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
490
22f5f10d 491 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
1da177e4
LT
492 if (trying_irqs & mask)
493 free_irq(i, NULL);
494
495 return probe_irq;
496}
497
498/**
8c32513b
FT
499 * NCR58380_info - report driver and host information
500 * @instance: relevant scsi host instance
1da177e4 501 *
8c32513b 502 * For use as the host template info() handler.
1da177e4
LT
503 *
504 * Locks: none
505 */
506
8c32513b 507static const char *NCR5380_info(struct Scsi_Host *instance)
1da177e4 508{
8c32513b
FT
509 struct NCR5380_hostdata *hostdata = shost_priv(instance);
510
511 return hostdata->info;
512}
513
514static void prepare_info(struct Scsi_Host *instance)
515{
516 struct NCR5380_hostdata *hostdata = shost_priv(instance);
517
518 snprintf(hostdata->info, sizeof(hostdata->info),
519 "%s, io_port 0x%lx, n_io_port %d, "
520 "base 0x%lx, irq %d, "
521 "can_queue %d, cmd_per_lun %d, "
522 "sg_tablesize %d, this_id %d, "
9c3f0e2b 523 "flags { %s%s%s%s}, "
8c32513b
FT
524 "options { %s} ",
525 instance->hostt->name, instance->io_port, instance->n_io_port,
526 instance->base, instance->irq,
527 instance->can_queue, instance->cmd_per_lun,
528 instance->sg_tablesize, instance->this_id,
529 hostdata->flags & FLAG_NCR53C400 ? "NCR53C400 " : "",
530 hostdata->flags & FLAG_DTC3181E ? "DTC3181E " : "",
531 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
9c3f0e2b 532 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
1da177e4 533#ifdef AUTOPROBE_IRQ
8c32513b 534 "AUTOPROBE_IRQ "
1da177e4 535#endif
1da177e4 536#ifdef DIFFERENTIAL
8c32513b 537 "DIFFERENTIAL "
1da177e4
LT
538#endif
539#ifdef REAL_DMA
8c32513b 540 "REAL_DMA "
1da177e4
LT
541#endif
542#ifdef REAL_DMA_POLL
8c32513b 543 "REAL_DMA_POLL "
1da177e4
LT
544#endif
545#ifdef PARITY
8c32513b 546 "PARITY "
1da177e4
LT
547#endif
548#ifdef PSEUDO_DMA
8c32513b 549 "PSEUDO_DMA "
8c32513b
FT
550#endif
551 "");
1da177e4
LT
552}
553
554/**
555 * NCR5380_print_status - dump controller info
556 * @instance: controller to dump
557 *
558 * Print commands in the various queues, called from NCR5380_abort
559 * and NCR5380_debug to aid debugging.
560 *
561 * Locks: called functions disable irqs
562 */
563
3be1b3ea 564static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
1da177e4
LT
565{
566 NCR5380_dprint(NDEBUG_ANY, instance);
567 NCR5380_dprint_phase(NDEBUG_ANY, instance);
568}
569
a9c2dc43 570#ifdef PSEUDO_DMA
1da177e4
LT
571/******************************************/
572/*
573 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
574 *
575 * *buffer: I/O buffer
576 * **start: if inout == FALSE pointer into buffer where user read should start
577 * offset: current offset
578 * length: length of buffer
579 * hostno: Scsi_Host host_no
580 * inout: TRUE - user is writing; FALSE - user is reading
581 *
582 * Return the number of bytes read from or written
583 */
584
dd7ab71b
AV
585static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
586 char *buffer, int length)
587{
a9c2dc43
FT
588 struct NCR5380_hostdata *hostdata = shost_priv(instance);
589
590 hostdata->spin_max_r = 0;
591 hostdata->spin_max_w = 0;
592 return 0;
dd7ab71b 593}
a9c2dc43 594#endif
dd7ab71b 595
1da177e4 596static
710ddd0d 597void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m);
1da177e4 598static
dd7ab71b 599void lprint_command(unsigned char *cmd, struct seq_file *m);
1da177e4 600static
dd7ab71b 601void lprint_opcode(int opcode, struct seq_file *m);
1da177e4 602
dd7ab71b
AV
603static int __maybe_unused NCR5380_show_info(struct seq_file *m,
604 struct Scsi_Host *instance)
1da177e4 605{
1da177e4 606 struct NCR5380_hostdata *hostdata;
710ddd0d 607 struct scsi_cmnd *ptr;
1da177e4
LT
608
609 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
610
a9c2dc43 611#ifdef PSEUDO_DMA
0c3de38f 612 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
a9c2dc43 613 hostdata->spin_max_w, hostdata->spin_max_r);
1da177e4
LT
614#endif
615 spin_lock_irq(instance->host_lock);
616 if (!hostdata->connected)
0c3de38f 617 seq_printf(m, "scsi%d: no currently connected command\n", instance->host_no);
1da177e4 618 else
710ddd0d 619 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
0c3de38f 620 seq_printf(m, "scsi%d: issue_queue\n", instance->host_no);
710ddd0d 621 for (ptr = (struct scsi_cmnd *) hostdata->issue_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
dd7ab71b 622 lprint_Scsi_Cmnd(ptr, m);
1da177e4 623
0c3de38f 624 seq_printf(m, "scsi%d: disconnected_queue\n", instance->host_no);
710ddd0d 625 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
dd7ab71b 626 lprint_Scsi_Cmnd(ptr, m);
1da177e4 627 spin_unlock_irq(instance->host_lock);
dd7ab71b 628 return 0;
1da177e4
LT
629}
630
710ddd0d 631static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
1da177e4 632{
0c3de38f 633 seq_printf(m, "scsi%d : destination target %d, lun %llu\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
91c40f24 634 seq_puts(m, " command = ");
dd7ab71b 635 lprint_command(cmd->cmnd, m);
1da177e4
LT
636}
637
dd7ab71b 638static void lprint_command(unsigned char *command, struct seq_file *m)
1da177e4
LT
639{
640 int i, s;
dd7ab71b 641 lprint_opcode(command[0], m);
1da177e4 642 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
0c3de38f 643 seq_printf(m, "%02x ", command[i]);
f50332ff 644 seq_putc(m, '\n');
1da177e4
LT
645}
646
dd7ab71b 647static void lprint_opcode(int opcode, struct seq_file *m)
1da177e4 648{
0c3de38f 649 seq_printf(m, "%2d (0x%02x)", opcode, opcode);
1da177e4
LT
650}
651
652
653/**
654 * NCR5380_init - initialise an NCR5380
655 * @instance: adapter to configure
656 * @flags: control flags
657 *
658 * Initializes *instance and corresponding 5380 chip,
659 * with flags OR'd into the initial flags value.
660 *
661 * Notes : I assume that the host, hostno, and id bits have been
662 * set correctly. I don't care about the irq and other fields.
663 *
664 * Returns 0 for success
665 *
666 * Locks: interrupts must be enabled when we are called
667 */
668
6f039790 669static int NCR5380_init(struct Scsi_Host *instance, int flags)
1da177e4 670{
b6488f97 671 int i;
1da177e4 672 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
2f854b82 673 unsigned long deadline;
1da177e4
LT
674
675 if(in_interrupt())
676 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
1da177e4 677
1da177e4
LT
678 hostdata->id_mask = 1 << instance->this_id;
679 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
680 if (i > hostdata->id_mask)
681 hostdata->id_higher_mask |= i;
682 for (i = 0; i < 8; ++i)
683 hostdata->busy[i] = 0;
684#ifdef REAL_DMA
685 hostdata->dmalen = 0;
686#endif
1da177e4
LT
687 hostdata->connected = NULL;
688 hostdata->issue_queue = NULL;
689 hostdata->disconnected_queue = NULL;
690
8d8601a7 691 INIT_WORK(&hostdata->main_task, NCR5380_main);
0ad0eff9
FT
692 hostdata->work_q = alloc_workqueue("ncr5380_%d",
693 WQ_UNBOUND | WQ_MEM_RECLAIM,
694 1, instance->host_no);
695 if (!hostdata->work_q)
696 return -ENOMEM;
697
1da177e4
LT
698 /* The CHECK code seems to break the 53C400. Will check it later maybe */
699 if (flags & FLAG_NCR53C400)
700 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
701 else
702 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
703
704 hostdata->host = instance;
1da177e4 705
8c32513b
FT
706 prepare_info(instance);
707
1da177e4
LT
708 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
709 NCR5380_write(MODE_REG, MR_BASE);
710 NCR5380_write(TARGET_COMMAND_REG, 0);
711 NCR5380_write(SELECT_ENABLE_REG, 0);
2f854b82
FT
712
713 /* Calibrate register polling loop */
714 i = 0;
715 deadline = jiffies + 1;
716 do {
717 cpu_relax();
718 } while (time_is_after_jiffies(deadline));
719 deadline += msecs_to_jiffies(256);
720 do {
721 NCR5380_read(STATUS_REG);
722 ++i;
723 cpu_relax();
724 } while (time_is_after_jiffies(deadline));
725 hostdata->accesses_per_ms = i / 256;
726
b6488f97
FT
727 return 0;
728}
1da177e4 729
b6488f97
FT
730/**
731 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
732 * @instance: adapter to check
733 *
734 * If the system crashed, it may have crashed with a connected target and
735 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
736 * currently established nexus, which we know nothing about. Failing that
737 * do a bus reset.
738 *
739 * Note that a bus reset will cause the chip to assert IRQ.
740 *
741 * Returns 0 if successful, otherwise -ENXIO.
742 */
743
744static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
745{
9c3f0e2b 746 struct NCR5380_hostdata *hostdata = shost_priv(instance);
b6488f97 747 int pass;
1da177e4
LT
748
749 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
750 switch (pass) {
751 case 1:
752 case 3:
753 case 5:
636b1ec8
FT
754 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
755 NCR5380_poll_politely(instance,
756 STATUS_REG, SR_BSY, 0, 5 * HZ);
1da177e4
LT
757 break;
758 case 2:
636b1ec8 759 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
1da177e4
LT
760 do_abort(instance);
761 break;
762 case 4:
636b1ec8 763 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
1da177e4 764 do_reset(instance);
9c3f0e2b
FT
765 /* Wait after a reset; the SCSI standard calls for
766 * 250ms, we wait 500ms to be on the safe side.
767 * But some Toshiba CD-ROMs need ten times that.
768 */
769 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
770 msleep(2500);
771 else
772 msleep(500);
1da177e4
LT
773 break;
774 case 6:
636b1ec8 775 shost_printk(KERN_ERR, instance, "bus locked solid\n");
1da177e4
LT
776 return -ENXIO;
777 }
778 }
779 return 0;
780}
781
782/**
783 * NCR5380_exit - remove an NCR5380
784 * @instance: adapter to remove
785 */
786
a43cf0f3 787static void NCR5380_exit(struct Scsi_Host *instance)
1da177e4
LT
788{
789 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
790
8d8601a7 791 cancel_work_sync(&hostdata->main_task);
0ad0eff9 792 destroy_workqueue(hostdata->work_q);
1da177e4
LT
793}
794
795/**
1bb40589
FT
796 * NCR5380_queue_command - queue a command
797 * @instance: the relevant SCSI adapter
798 * @cmd: SCSI command
1da177e4 799 *
1bb40589
FT
800 * cmd is added to the per-instance issue queue, with minor
801 * twiddling done to the host specific fields of cmd. If the
802 * main coroutine is not running, it is restarted.
1da177e4
LT
803 */
804
1bb40589
FT
805static int NCR5380_queue_command(struct Scsi_Host *instance,
806 struct scsi_cmnd *cmd)
1da177e4 807{
1bb40589 808 struct NCR5380_hostdata *hostdata = shost_priv(instance);
710ddd0d 809 struct scsi_cmnd *tmp;
1bb40589 810 unsigned long flags;
1da177e4
LT
811
812#if (NDEBUG & NDEBUG_NO_WRITE)
813 switch (cmd->cmnd[0]) {
814 case WRITE_6:
815 case WRITE_10:
816 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
817 cmd->result = (DID_ERROR << 16);
1bb40589 818 cmd->scsi_done(cmd);
1da177e4
LT
819 return 0;
820 }
821#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
822
1da177e4
LT
823 /*
824 * We use the host_scribble field as a pointer to the next command
825 * in a queue
826 */
827
828 cmd->host_scribble = NULL;
1da177e4
LT
829 cmd->result = 0;
830
1bb40589
FT
831 spin_lock_irqsave(instance->host_lock, flags);
832
1da177e4
LT
833 /*
834 * Insert the cmd into the issue queue. Note that REQUEST SENSE
835 * commands are added to the head of the queue since any command will
836 * clear the contingent allegiance condition that exists and the
837 * sense data is only guaranteed to be valid while the condition exists.
838 */
839
840 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
841 LIST(cmd, hostdata->issue_queue);
842 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
843 hostdata->issue_queue = cmd;
844 } else {
710ddd0d 845 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (struct scsi_cmnd *) tmp->host_scribble);
1da177e4
LT
846 LIST(cmd, tmp);
847 tmp->host_scribble = (unsigned char *) cmd;
848 }
1bb40589
FT
849 spin_unlock_irqrestore(instance->host_lock, flags);
850
52a6a1cb 851 dprintk(NDEBUG_QUEUES, "scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
1da177e4 852
1da177e4 853 /* Kick off command processing */
8d8601a7 854 queue_work(hostdata->work_q, &hostdata->main_task);
1da177e4
LT
855 return 0;
856}
857
1da177e4
LT
858/**
859 * NCR5380_main - NCR state machines
860 *
861 * NCR5380_main is a coroutine that runs as long as more work can
862 * be done on the NCR5380 host adapters in a system. Both
863 * NCR5380_queue_command() and NCR5380_intr() will try to start it
864 * in case it is not running.
865 *
866 * Locks: called as its own thread with no locks held. Takes the
867 * host lock and called routines may take the isa dma lock.
868 */
869
c4028958 870static void NCR5380_main(struct work_struct *work)
1da177e4 871{
c4028958 872 struct NCR5380_hostdata *hostdata =
8d8601a7 873 container_of(work, struct NCR5380_hostdata, main_task);
1da177e4 874 struct Scsi_Host *instance = hostdata->host;
710ddd0d 875 struct scsi_cmnd *tmp, *prev;
1da177e4
LT
876 int done;
877
878 spin_lock_irq(instance->host_lock);
879 do {
880 /* Lock held here */
881 done = 1;
ae753a33 882 if (!hostdata->connected) {
52a6a1cb 883 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
1da177e4
LT
884 /*
885 * Search through the issue_queue for a command destined
886 * for a target that's not busy.
887 */
710ddd0d 888 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
1da177e4
LT
889 {
890 if (prev != tmp)
9cb78c16 891 dprintk(NDEBUG_LISTS, "MAIN tmp=%p target=%d busy=%d lun=%llu\n", tmp, tmp->device->id, hostdata->busy[tmp->device->id], tmp->device->lun);
1da177e4 892 /* When we find one, remove it from the issue queue. */
9cb78c16
HR
893 if (!(hostdata->busy[tmp->device->id] &
894 (1 << (u8)(tmp->device->lun & 0xff)))) {
1da177e4
LT
895 if (prev) {
896 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
897 prev->host_scribble = tmp->host_scribble;
898 } else {
899 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
710ddd0d 900 hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
1da177e4
LT
901 }
902 tmp->host_scribble = NULL;
903
904 /*
905 * Attempt to establish an I_T_L nexus here.
906 * On success, instance->hostdata->connected is set.
907 * On failure, we must add the command back to the
908 * issue queue so we can keep trying.
909 */
9cb78c16 910 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main() : command for target %d lun %llu removed from issue_queue\n", instance->host_no, tmp->device->id, tmp->device->lun);
1da177e4 911
76f13b93
FT
912 /*
913 * REQUEST SENSE commands are issued without tagged
914 * queueing, even on SCSI-II devices because the
915 * contingent allegiance condition exists for the
916 * entire unit.
917 */
918
919 if (!NCR5380_select(instance, tmp)) {
1f1b0c74 920 /* OK or bad target */
1da177e4 921 } else {
1f1b0c74 922 /* Need to retry */
1da177e4
LT
923 LIST(tmp, hostdata->issue_queue);
924 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
925 hostdata->issue_queue = tmp;
926 done = 0;
52a6a1cb 927 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no);
1da177e4 928 }
ae753a33 929 if (hostdata->connected)
1f1b0c74 930 break;
1da177e4
LT
931 /* lock held here still */
932 } /* if target/lun is not busy */
933 } /* for */
934 /* exited locked */
935 } /* if (!hostdata->connected) */
1da177e4
LT
936 if (hostdata->connected
937#ifdef REAL_DMA
938 && !hostdata->dmalen
939#endif
1da177e4 940 ) {
52a6a1cb 941 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
1da177e4 942 NCR5380_information_transfer(instance);
52a6a1cb 943 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
1da177e4 944 done = 0;
1d3db59d 945 }
1da177e4
LT
946 } while (!done);
947
948 spin_unlock_irq(instance->host_lock);
949}
950
951#ifndef DONT_USE_INTR
952
953/**
954 * NCR5380_intr - generic NCR5380 irq handler
955 * @irq: interrupt number
956 * @dev_id: device info
1da177e4
LT
957 *
958 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
959 * from the disconnected queue, and restarting NCR5380_main()
960 * as required.
961 *
962 * Locks: takes the needed instance locks
963 */
964
baa9aac6 965static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
1da177e4 966{
baa9aac6 967 struct Scsi_Host *instance = dev_id;
1da177e4
LT
968 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
969 int done;
970 unsigned char basr;
971 unsigned long flags;
972
52a6a1cb
FT
973 dprintk(NDEBUG_INTR, "scsi : NCR5380 irq %d triggered\n",
974 instance->irq);
1da177e4
LT
975
976 do {
977 done = 1;
978 spin_lock_irqsave(instance->host_lock, flags);
979 /* Look for pending interrupts */
1da177e4
LT
980 basr = NCR5380_read(BUS_AND_STATUS_REG);
981 /* XXX dispatch to appropriate routine if found and done=0 */
982 if (basr & BASR_IRQ) {
983 NCR5380_dprint(NDEBUG_INTR, instance);
984 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
985 done = 0;
52a6a1cb 986 dprintk(NDEBUG_INTR, "scsi%d : SEL interrupt\n", instance->host_no);
1da177e4
LT
987 NCR5380_reselect(instance);
988 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
989 } else if (basr & BASR_PARITY_ERROR) {
52a6a1cb 990 dprintk(NDEBUG_INTR, "scsi%d : PARITY interrupt\n", instance->host_no);
1da177e4
LT
991 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
992 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
52a6a1cb 993 dprintk(NDEBUG_INTR, "scsi%d : RESET interrupt\n", instance->host_no);
1da177e4
LT
994 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
995 } else {
996#if defined(REAL_DMA)
997 /*
998 * We should only get PHASE MISMATCH and EOP interrupts
999 * if we have DMA enabled, so do a sanity check based on
1000 * the current setting of the MODE register.
1001 */
1002
1003 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
25985edc 1004 int transferred;
1da177e4
LT
1005
1006 if (!hostdata->connected)
1007 panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
1008
25985edc 1009 transferred = (hostdata->dmalen - NCR5380_dma_residual(instance));
1da177e4
LT
1010 hostdata->connected->SCp.this_residual -= transferred;
1011 hostdata->connected->SCp.ptr += transferred;
1012 hostdata->dmalen = 0;
1013
1014 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1015
1016 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1017 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
1018
1019 NCR5380_write(MODE_REG, MR_BASE);
1020 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1021 }
1022#else
52a6a1cb 1023 dprintk(NDEBUG_INTR, "scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1da177e4
LT
1024 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1025#endif
1026 }
1027 } /* if BASR_IRQ */
1028 spin_unlock_irqrestore(instance->host_lock, flags);
1029 if(!done)
8d8601a7 1030 queue_work(hostdata->work_q, &hostdata->main_task);
1da177e4
LT
1031 } while (!done);
1032 return IRQ_HANDLED;
1033}
1034
1035#endif
1036
1da177e4 1037/*
710ddd0d
FT
1038 * Function : int NCR5380_select(struct Scsi_Host *instance,
1039 * struct scsi_cmnd *cmd)
1da177e4
LT
1040 *
1041 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1042 * including ARBITRATION, SELECTION, and initial message out for
1043 * IDENTIFY and queue messages.
1044 *
1045 * Inputs : instance - instantiation of the 5380 driver on which this
76f13b93 1046 * target lives, cmd - SCSI command to execute.
1da177e4 1047 *
6323876f
FT
1048 * Returns : -1 if selection failed but should be retried.
1049 * 0 if selection failed and should not be retried.
1050 * 0 if selection succeeded completely (hostdata->connected == cmd).
1da177e4
LT
1051 *
1052 * Side effects :
1053 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1054 * with registers as they should have been on entry - ie
1055 * SELECT_ENABLE will be set appropriately, the NCR5380
1056 * will cease to drive any SCSI bus signals.
1057 *
1058 * If successful : I_T_L or I_T_L_Q nexus will be established,
1059 * instance->connected will be set to cmd.
1060 * SELECT interrupt will be disabled.
1061 *
1062 * If failed (no target) : cmd->scsi_done() will be called, and the
1063 * cmd->result host byte set to DID_BAD_TARGET.
1064 *
1065 * Locks: caller holds hostdata lock in IRQ mode
1066 */
1067
710ddd0d 1068static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
1da177e4 1069{
1da177e4
LT
1070 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1071 unsigned char tmp[3], phase;
1072 unsigned char *data;
1073 int len;
1da177e4 1074 int err;
55500d9b 1075 unsigned long timeout;
1da177e4 1076
1da177e4 1077 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
52a6a1cb 1078 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
1da177e4
LT
1079
1080 /*
1081 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1082 * data bus during SELECTION.
1083 */
1084
1085 NCR5380_write(TARGET_COMMAND_REG, 0);
1086
1087 /*
1088 * Start arbitration.
1089 */
1090
1091 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1092 NCR5380_write(MODE_REG, MR_ARBITRATE);
1093
55500d9b
FT
1094 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1095 * Bus Free Delay, arbitration will begin.
1096 */
1da177e4 1097
1da177e4 1098 spin_unlock_irq(instance->host_lock);
55500d9b
FT
1099 timeout = jiffies + HZ;
1100 while (1) {
1101 if (time_is_before_jiffies(timeout)) {
1102 NCR5380_write(MODE_REG, MR_BASE);
1103 shost_printk(KERN_ERR, instance,
1104 "select: arbitration timeout\n");
1105 spin_lock_irq(instance->host_lock);
1106 return -1;
1107 }
1108 spin_lock_irq(instance->host_lock);
1109 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1110 /* Reselection interrupt */
1111 return -1;
1112 }
1113 if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1114 break;
1115 spin_unlock_irq(instance->host_lock);
1da177e4
LT
1116 }
1117
55500d9b 1118 /* The SCSI-2 arbitration delay is 2.4 us */
1da177e4
LT
1119 udelay(3);
1120
1121 /* Check for lost arbitration */
1122 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1123 NCR5380_write(MODE_REG, MR_BASE);
52a6a1cb 1124 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
6323876f 1125 return -1;
1da177e4 1126 }
cf13b083
FT
1127
1128 /* After/during arbitration, BSY should be asserted.
1129 * IBM DPES-31080 Version S31Q works now
1130 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1131 */
1132 NCR5380_write(INITIATOR_COMMAND_REG,
1133 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
1da177e4
LT
1134
1135 if (!(hostdata->flags & FLAG_DTC3181E) &&
1136 /* RvC: DTC3181E has some trouble with this
1137 * so we simply removed it. Seems to work with
1138 * only Mustek scanner attached
1139 */
1140 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1141 NCR5380_write(MODE_REG, MR_BASE);
1142 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
52a6a1cb 1143 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
6323876f 1144 return -1;
1da177e4
LT
1145 }
1146 /*
1147 * Again, bus clear + bus settle time is 1.2us, however, this is
1148 * a minimum so we'll udelay ceil(1.2)
1149 */
1150
9c3f0e2b
FT
1151 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1152 udelay(15);
1153 else
1154 udelay(2);
1da177e4 1155
52a6a1cb 1156 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
1da177e4
LT
1157
1158 /*
1159 * Now that we have won arbitration, start Selection process, asserting
1160 * the host and target ID's on the SCSI bus.
1161 */
1162
422c0d61 1163 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
1da177e4
LT
1164
1165 /*
1166 * Raise ATN while SEL is true before BSY goes false from arbitration,
1167 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1168 * phase immediately after selection.
1169 */
1170
1171 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1172 NCR5380_write(MODE_REG, MR_BASE);
1173
1174 /*
1175 * Reselect interrupts must be turned off prior to the dropping of BSY,
1176 * otherwise we will trigger an interrupt.
1177 */
1178 NCR5380_write(SELECT_ENABLE_REG, 0);
1179
1180 /*
1181 * The initiator shall then wait at least two deskew delays and release
1182 * the BSY signal.
1183 */
1184 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1185
1186 /* Reset BSY */
1187 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1188
1189 /*
1190 * Something weird happens when we cease to drive BSY - looks
1191 * like the board/chip is letting us do another read before the
1192 * appropriate propagation delay has expired, and we're confusing
1193 * a BSY signal from ourselves as the target's response to SELECTION.
1194 *
1195 * A small delay (the 'C++' frontend breaks the pipeline with an
1196 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1197 * tighter 'C' code breaks and requires this) solves the problem -
1198 * the 1 us delay is arbitrary, and only used because this delay will
1199 * be the same on other platforms and since it works here, it should
1200 * work there.
1201 *
1202 * wingel suggests that this could be due to failing to wait
1203 * one deskew delay.
1204 */
1205
1206 udelay(1);
1207
52a6a1cb 1208 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
1da177e4
LT
1209
1210 /*
1211 * The SCSI specification calls for a 250 ms timeout for the actual
1212 * selection.
1213 */
1214
ae753a33
FT
1215 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1216 msecs_to_jiffies(250));
1da177e4 1217
1da177e4
LT
1218 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1219 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1220 NCR5380_reselect(instance);
1221 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
1222 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1223 return -1;
1224 }
ae753a33
FT
1225
1226 if (err < 0) {
1227 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1228 cmd->result = DID_BAD_TARGET << 16;
1229 cmd->scsi_done(cmd);
1230 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1231 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1232 instance->host_no);
1233 return 0;
1234 }
1235
1da177e4
LT
1236 /*
1237 * No less than two deskew delays after the initiator detects the
1238 * BSY signal is true, it shall release the SEL signal and may
1239 * change the DATA BUS. -wingel
1240 */
1241
1242 udelay(1);
1243
1244 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1245
1da177e4
LT
1246 /*
1247 * Since we followed the SCSI spec, and raised ATN while SEL
1248 * was true but before BSY was false during selection, the information
1249 * transfer phase should be a MESSAGE OUT phase so that we can send the
1250 * IDENTIFY message.
1251 *
1252 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1253 * message (2 bytes) with a tag ID that we increment with every command
1254 * until it wraps back to 0.
1255 *
1256 * XXX - it turns out that there are some broken SCSI-II devices,
1257 * which claim to support tagged queuing but fail when more than
1258 * some number of commands are issued at once.
1259 */
1260
1261 /* Wait for start of REQ/ACK handshake */
1262
1263 spin_unlock_irq(instance->host_lock);
1264 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1265 spin_lock_irq(instance->host_lock);
1cc160e1 1266 if (err < 0) {
55500d9b
FT
1267 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1268 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1da177e4 1269 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
6323876f 1270 return -1;
1da177e4
LT
1271 }
1272
52a6a1cb 1273 dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id);
22f5f10d 1274 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
1da177e4
LT
1275
1276 len = 1;
1277 cmd->tag = 0;
1278
1279 /* Send message(s) */
1280 data = tmp;
1281 phase = PHASE_MSGOUT;
1282 NCR5380_transfer_pio(instance, &phase, &len, &data);
52a6a1cb 1283 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
1da177e4
LT
1284 /* XXX need to handle errors here */
1285 hostdata->connected = cmd;
9cb78c16 1286 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
1da177e4 1287
28424d3a 1288 initialize_SCp(cmd);
1da177e4
LT
1289
1290 return 0;
1da177e4
LT
1291}
1292
1293/*
1294 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1295 * unsigned char *phase, int *count, unsigned char **data)
1296 *
1297 * Purpose : transfers data in given phase using polled I/O
1298 *
1299 * Inputs : instance - instance of driver, *phase - pointer to
1300 * what phase is expected, *count - pointer to number of
1301 * bytes to transfer, **data - pointer to data pointer.
1302 *
1303 * Returns : -1 when different phase is entered without transferring
25985edc 1304 * maximum number of bytes, 0 if all bytes or transferred or exit
1da177e4
LT
1305 * is in same phase.
1306 *
1307 * Also, *phase, *count, *data are modified in place.
1308 *
1309 * XXX Note : handling for bus free may be useful.
1310 */
1311
1312/*
1313 * Note : this code is not as quick as it could be, however it
1314 * IS 100% reliable, and for the actual data transfer where speed
1315 * counts, we will always do a pseudo DMA or DMA transfer.
1316 */
1317
1318static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1da177e4
LT
1319 unsigned char p = *phase, tmp;
1320 int c = *count;
1321 unsigned char *d = *data;
1da177e4
LT
1322
1323 if (!(p & SR_IO))
52a6a1cb 1324 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
1da177e4 1325 else
52a6a1cb 1326 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
1da177e4
LT
1327
1328 /*
1329 * The NCR5380 chip will only drive the SCSI bus when the
1330 * phase specified in the appropriate bits of the TARGET COMMAND
1331 * REGISTER match the STATUS REGISTER
1332 */
1333
1334 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1335
1da177e4
LT
1336 do {
1337 /*
1338 * Wait for assertion of REQ, after which the phase bits will be
1339 * valid
1340 */
1341
686f3990 1342 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1da177e4 1343 break;
1da177e4 1344
52a6a1cb 1345 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
1da177e4
LT
1346
1347 /* Check for phase mismatch */
686f3990 1348 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
52a6a1cb 1349 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
1da177e4
LT
1350 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1351 break;
1352 }
1353 /* Do actual transfer from SCSI bus to / from memory */
1354 if (!(p & SR_IO))
1355 NCR5380_write(OUTPUT_DATA_REG, *d);
1356 else
1357 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1358
1359 ++d;
1360
1361 /*
1362 * The SCSI standard suggests that in MSGOUT phase, the initiator
1363 * should drop ATN on the last byte of the message phase
1364 * after REQ has been asserted for the handshake but before
1365 * the initiator raises ACK.
1366 */
1367
1368 if (!(p & SR_IO)) {
1369 if (!((p & SR_MSG) && c > 1)) {
1370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1371 NCR5380_dprint(NDEBUG_PIO, instance);
1372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1373 } else {
1374 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1375 NCR5380_dprint(NDEBUG_PIO, instance);
1376 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1377 }
1378 } else {
1379 NCR5380_dprint(NDEBUG_PIO, instance);
1380 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1381 }
1382
a2edc4a6
FT
1383 if (NCR5380_poll_politely(instance,
1384 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1385 break;
1386
52a6a1cb 1387 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
1da177e4
LT
1388
1389/*
1390 * We have several special cases to consider during REQ/ACK handshaking :
1391 * 1. We were in MSGOUT phase, and we are on the last byte of the
1392 * message. ATN must be dropped as ACK is dropped.
1393 *
1394 * 2. We are in a MSGIN phase, and we are on the last byte of the
1395 * message. We must exit with ACK asserted, so that the calling
1396 * code may raise ATN before dropping ACK to reject the message.
1397 *
1398 * 3. ACK and ATN are clear and the target may proceed as normal.
1399 */
1400 if (!(p == PHASE_MSGIN && c == 1)) {
1401 if (p == PHASE_MSGOUT && c > 1)
1402 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1403 else
1404 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1405 }
1406 } while (--c);
1407
52a6a1cb 1408 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
1da177e4
LT
1409
1410 *count = c;
1411 *data = d;
1412 tmp = NCR5380_read(STATUS_REG);
a2edc4a6
FT
1413 /* The phase read from the bus is valid if either REQ is (already)
1414 * asserted or if ACK hasn't been released yet. The latter applies if
1415 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1416 */
1417 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
1da177e4
LT
1418 *phase = tmp & PHASE_MASK;
1419 else
1420 *phase = PHASE_UNKNOWN;
1421
1422 if (!c || (*phase == p))
1423 return 0;
1424 else
1425 return -1;
1426}
1427
1428/**
636b1ec8
FT
1429 * do_reset - issue a reset command
1430 * @instance: adapter to reset
1da177e4 1431 *
636b1ec8
FT
1432 * Issue a reset sequence to the NCR5380 and try and get the bus
1433 * back into sane shape.
1da177e4 1434 *
636b1ec8
FT
1435 * This clears the reset interrupt flag because there may be no handler for
1436 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1437 * been installed. And when in EH we may have released the ST DMA interrupt.
1da177e4
LT
1438 */
1439
54d8fe44
FT
1440static void do_reset(struct Scsi_Host *instance)
1441{
636b1ec8
FT
1442 unsigned long flags;
1443
1444 local_irq_save(flags);
1445 NCR5380_write(TARGET_COMMAND_REG,
1446 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1da177e4 1447 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
636b1ec8 1448 udelay(50);
1da177e4 1449 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
636b1ec8
FT
1450 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1451 local_irq_restore(flags);
1da177e4
LT
1452}
1453
80d3eb6d
FT
1454/**
1455 * do_abort - abort the currently established nexus by going to
1456 * MESSAGE OUT phase and sending an ABORT message.
1457 * @instance: relevant scsi host instance
1da177e4 1458 *
80d3eb6d 1459 * Returns 0 on success, -1 on failure.
1da177e4
LT
1460 */
1461
54d8fe44
FT
1462static int do_abort(struct Scsi_Host *instance)
1463{
1da177e4
LT
1464 unsigned char *msgptr, phase, tmp;
1465 int len;
1466 int rc;
1da177e4
LT
1467
1468 /* Request message out phase */
1469 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1470
1471 /*
1472 * Wait for the target to indicate a valid phase by asserting
1473 * REQ. Once this happens, we'll have either a MSGOUT phase
1474 * and can immediately send the ABORT message, or we'll have some
1475 * other phase and will have to source/sink data.
1476 *
1477 * We really don't care what value was on the bus or what value
1478 * the target sees, so we just handshake.
1479 */
1480
80d3eb6d 1481 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1cc160e1 1482 if (rc < 0)
80d3eb6d 1483 goto timeout;
1da177e4 1484
f35d3474 1485 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
1da177e4
LT
1486
1487 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1488
f35d3474 1489 if (tmp != PHASE_MSGOUT) {
1da177e4 1490 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
54d8fe44 1491 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1cc160e1 1492 if (rc < 0)
80d3eb6d
FT
1493 goto timeout;
1494 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1da177e4
LT
1495 }
1496 tmp = ABORT;
1497 msgptr = &tmp;
1498 len = 1;
1499 phase = PHASE_MSGOUT;
54d8fe44 1500 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
1da177e4
LT
1501
1502 /*
1503 * If we got here, and the command completed successfully,
1504 * we're about to go into bus free state.
1505 */
1506
1507 return len ? -1 : 0;
80d3eb6d
FT
1508
1509timeout:
1510 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1511 return -1;
1da177e4
LT
1512}
1513
1514#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1515/*
1516 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1517 * unsigned char *phase, int *count, unsigned char **data)
1518 *
1519 * Purpose : transfers data in given phase using either real
1520 * or pseudo DMA.
1521 *
1522 * Inputs : instance - instance of driver, *phase - pointer to
1523 * what phase is expected, *count - pointer to number of
1524 * bytes to transfer, **data - pointer to data pointer.
1525 *
1526 * Returns : -1 when different phase is entered without transferring
25985edc 1527 * maximum number of bytes, 0 if all bytes or transferred or exit
1da177e4
LT
1528 * is in same phase.
1529 *
1530 * Also, *phase, *count, *data are modified in place.
1531 *
1532 * Locks: io_request lock held by caller
1533 */
1534
1535
1536static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1da177e4
LT
1537 register int c = *count;
1538 register unsigned char p = *phase;
1539 register unsigned char *d = *data;
1540 unsigned char tmp;
1541 int foo;
1542#if defined(REAL_DMA_POLL)
1543 int cnt, toPIO;
1544 unsigned char saved_data = 0, overrun = 0, residue;
1545#endif
1546
1547 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1548
1da177e4
LT
1549 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1550 *phase = tmp;
1551 return -1;
1552 }
1553#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1554#ifdef READ_OVERRUNS
1555 if (p & SR_IO) {
1556 c -= 2;
1557 }
1558#endif
52a6a1cb 1559 dprintk(NDEBUG_DMA, "scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
1da177e4
LT
1560 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1561#endif
1562
1563 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1564
1565#ifdef REAL_DMA
1566 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1567#elif defined(REAL_DMA_POLL)
1568 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1569#else
1570 /*
1571 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1572 * were not disabled for the duration of a single DMA transfer, from
1573 * before the setting of DMA mode to after transfer of the last byte.
1574 */
1575
1da177e4
LT
1576 /* KLL May need eop and parity in 53c400 */
1577 if (hostdata->flags & FLAG_NCR53C400)
085267ac
RK
1578 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
1579 MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
1580 MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1da177e4
LT
1581 else
1582 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1583#endif /* def REAL_DMA */
1584
52a6a1cb 1585 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
1da177e4
LT
1586
1587 /*
1588 * On the PAS16 at least I/O recovery delays are not needed here.
1589 * Everyone else seems to want them.
1590 */
1591
1592 if (p & SR_IO) {
1593 io_recovery_delay(1);
1594 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1595 } else {
1596 io_recovery_delay(1);
1597 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1598 io_recovery_delay(1);
1599 NCR5380_write(START_DMA_SEND_REG, 0);
1600 io_recovery_delay(1);
1601 }
1602
1603#if defined(REAL_DMA_POLL)
1604 do {
1605 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1606 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1607
1608/*
1609 At this point, either we've completed DMA, or we have a phase mismatch,
1610 or we've unexpectedly lost BUSY (which is a real error).
1611
1612 For write DMAs, we want to wait until the last byte has been
1613 transferred out over the bus before we turn off DMA mode. Alas, there
1614 seems to be no terribly good way of doing this on a 5380 under all
1615 conditions. For non-scatter-gather operations, we can wait until REQ
1616 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1617 are nastier, since the device will be expecting more data than we
1618 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1619 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1620 why this signal was added to the newer chips) but on the older 538[01]
1621 this signal does not exist. The workaround for this lack is a watchdog;
1622 we bail out of the wait-loop after a modest amount of wait-time if
1623 the usual exit conditions are not met. Not a terribly clean or
1624 correct solution :-%
1625
1626 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1627 If the chip is in DMA mode for an READ, it will respond to a target's
1628 REQ by latching the SCSI data into the INPUT DATA register and asserting
1629 ACK, even if it has _already_ been notified by the DMA controller that
1630 the current DMA transfer has completed! If the NCR5380 is then taken
1631 out of DMA mode, this already-acknowledged byte is lost.
1632
1633 This is not a problem for "one DMA transfer per command" reads, because
1634 the situation will never arise... either all of the data is DMA'ed
1635 properly, or the target switches to MESSAGE IN phase to signal a
1636 disconnection (either operation bringing the DMA to a clean halt).
1637 However, in order to handle scatter-reads, we must work around the
1638 problem. The chosen fix is to DMA N-2 bytes, then check for the
1639 condition before taking the NCR5380 out of DMA mode. One or two extra
1640 bytes are transferred via PIO as necessary to fill out the original
1641 request.
1642 */
1643
1644 if (p & SR_IO) {
1645#ifdef READ_OVERRUNS
1646 udelay(10);
1647 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1648 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1649 overrun = 1;
1650 }
1651#endif
1652 } else {
1653 int limit = 100;
1654 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1655 if (!(tmp & BASR_PHASE_MATCH))
1656 break;
1657 if (--limit < 0)
1658 break;
1659 }
1660 }
1661
52a6a1cb 1662 dprintk(NDEBUG_DMA, "scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG));
1da177e4
LT
1663
1664 NCR5380_write(MODE_REG, MR_BASE);
1665 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1666
1667 residue = NCR5380_dma_residual(instance);
1668 c -= residue;
1669 *count -= c;
1670 *data += c;
1671 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1672
1673#ifdef READ_OVERRUNS
1674 if (*phase == p && (p & SR_IO) && residue == 0) {
1675 if (overrun) {
52a6a1cb 1676 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
1da177e4
LT
1677 **data = saved_data;
1678 *data += 1;
1679 *count -= 1;
1680 cnt = toPIO = 1;
1681 } else {
1682 printk("No overrun??\n");
1683 cnt = toPIO = 2;
1684 }
52a6a1cb 1685 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
1da177e4
LT
1686 NCR5380_transfer_pio(instance, phase, &cnt, data);
1687 *count -= toPIO - cnt;
1688 }
1689#endif
1690
52a6a1cb 1691 dprintk(NDEBUG_DMA, "Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count));
1da177e4
LT
1692 return 0;
1693
1694#elif defined(REAL_DMA)
1695 return 0;
1696#else /* defined(REAL_DMA_POLL) */
1697 if (p & SR_IO) {
1698#ifdef DMA_WORKS_RIGHT
1699 foo = NCR5380_pread(instance, d, c);
1700#else
1701 int diff = 1;
1702 if (hostdata->flags & FLAG_NCR53C400) {
1703 diff = 0;
1704 }
1705 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
1706 /*
1707 * We can't disable DMA mode after successfully transferring
1708 * what we plan to be the last byte, since that would open up
1709 * a race condition where if the target asserted REQ before
1710 * we got the DMA mode reset, the NCR5380 would have latched
1711 * an additional byte into the INPUT DATA register and we'd
1712 * have dropped it.
1713 *
1714 * The workaround was to transfer one fewer bytes than we
1715 * intended to with the pseudo-DMA read function, wait for
1716 * the chip to latch the last byte, read it, and then disable
1717 * pseudo-DMA mode.
1718 *
1719 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1720 * REQ is deasserted when ACK is asserted, and not reasserted
1721 * until ACK goes false. Since the NCR5380 won't lower ACK
1722 * until DACK is asserted, which won't happen unless we twiddle
1723 * the DMA port or we take the NCR5380 out of DMA mode, we
1724 * can guarantee that we won't handshake another extra
1725 * byte.
1726 */
1727
1728 if (!(hostdata->flags & FLAG_NCR53C400)) {
1729 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
1730 /* Wait for clean handshake */
1731 while (NCR5380_read(STATUS_REG) & SR_REQ);
1732 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1733 }
1734 }
1735#endif
1736 } else {
1737#ifdef DMA_WORKS_RIGHT
1738 foo = NCR5380_pwrite(instance, d, c);
1739#else
1740 int timeout;
52a6a1cb 1741 dprintk(NDEBUG_C400_PWRITE, "About to pwrite %d bytes\n", c);
1da177e4
LT
1742 if (!(foo = NCR5380_pwrite(instance, d, c))) {
1743 /*
1744 * Wait for the last byte to be sent. If REQ is being asserted for
1745 * the byte we're interested, we'll ACK it and it will go false.
1746 */
1747 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
1748 timeout = 20000;
1749 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
1750
1751 if (!timeout)
52a6a1cb 1752 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : timed out on last byte\n", instance->host_no);
1da177e4
LT
1753
1754 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
1755 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
1756 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
1757 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
52a6a1cb 1758 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : last byte sent works\n", instance->host_no);
1da177e4
LT
1759 }
1760 }
1761 } else {
52a6a1cb 1762 dprintk(NDEBUG_C400_PWRITE, "Waiting for LASTBYTE\n");
1da177e4 1763 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
52a6a1cb 1764 dprintk(NDEBUG_C400_PWRITE, "Got LASTBYTE\n");
1da177e4
LT
1765 }
1766 }
1767#endif
1768 }
1769 NCR5380_write(MODE_REG, MR_BASE);
1770 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1771
1772 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
52a6a1cb 1773 dprintk(NDEBUG_C400_PWRITE, "53C400w: Checking for IRQ\n");
1da177e4 1774 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
52a6a1cb 1775 dprintk(NDEBUG_C400_PWRITE, "53C400w: got it, reading reset interrupt reg\n");
1da177e4
LT
1776 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1777 } else {
1778 printk("53C400w: IRQ NOT THERE!\n");
1779 }
1780 }
1781 *data = d + c;
1782 *count = 0;
1783 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1da177e4
LT
1784 return foo;
1785#endif /* def REAL_DMA */
1786}
1787#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1788
1789/*
1790 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1791 *
1792 * Purpose : run through the various SCSI phases and do as the target
1793 * directs us to. Operates on the currently connected command,
1794 * instance->connected.
1795 *
1796 * Inputs : instance, instance for which we are doing commands
1797 *
1798 * Side effects : SCSI things happen, the disconnected queue will be
1799 * modified if a command disconnects, *instance->connected will
1800 * change.
1801 *
1802 * XXX Note : we need to watch for bus free or a reset condition here
1803 * to recover from an unexpected bus free condition.
1804 *
1805 * Locks: io_request_lock held by caller in IRQ mode
1806 */
1807
1808static void NCR5380_information_transfer(struct Scsi_Host *instance) {
1da177e4
LT
1809 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1810 unsigned char msgout = NOP;
1811 int sink = 0;
1812 int len;
1813#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1814 int transfersize;
1815#endif
1816 unsigned char *data;
1817 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
710ddd0d 1818 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
1da177e4 1819
1da177e4
LT
1820 while (1) {
1821 tmp = NCR5380_read(STATUS_REG);
1822 /* We only have a valid SCSI phase when REQ is asserted */
1823 if (tmp & SR_REQ) {
1824 phase = (tmp & PHASE_MASK);
1825 if (phase != old_phase) {
1826 old_phase = phase;
1827 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1828 }
1829 if (sink && (phase != PHASE_MSGOUT)) {
1830 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1831
1832 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1833 while (NCR5380_read(STATUS_REG) & SR_REQ);
1834 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1835 sink = 0;
1836 continue;
1837 }
1838 switch (phase) {
1839 case PHASE_DATAIN:
1840 case PHASE_DATAOUT:
1841#if (NDEBUG & NDEBUG_NO_DATAOUT)
1842 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1843 sink = 1;
1844 do_abort(instance);
1845 cmd->result = DID_ERROR << 16;
cce99c69 1846 cmd->scsi_done(cmd);
1da177e4
LT
1847 return;
1848#endif
1849 /*
1850 * If there is no room left in the current buffer in the
1851 * scatter-gather list, move onto the next one.
1852 */
1853
1854 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1855 ++cmd->SCp.buffer;
1856 --cmd->SCp.buffers_residual;
1857 cmd->SCp.this_residual = cmd->SCp.buffer->length;
45711f1a 1858 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
52a6a1cb 1859 dprintk(NDEBUG_INFORMATION, "scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual);
1da177e4
LT
1860 }
1861 /*
1862 * The preferred transfer method is going to be
1863 * PSEUDO-DMA for systems that are strictly PIO,
1864 * since we can let the hardware do the handshaking.
1865 *
1866 * For this to work, we need to know the transfersize
1867 * ahead of time, since the pseudo-DMA code will sit
1868 * in an unconditional loop.
1869 */
1870
1871#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
ff3d4578
FT
1872 transfersize = 0;
1873 if (!cmd->device->borken &&
1874 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1875 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
1876
1877 if (transfersize) {
1da177e4
LT
1878 len = transfersize;
1879 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1880 /*
1881 * If the watchdog timer fires, all future accesses to this
1882 * device will use the polled-IO.
1883 */
017560fc
JG
1884 scmd_printk(KERN_INFO, cmd,
1885 "switching to slow handshake\n");
1da177e4 1886 cmd->device->borken = 1;
1da177e4
LT
1887 sink = 1;
1888 do_abort(instance);
1889 cmd->result = DID_ERROR << 16;
cce99c69 1890 cmd->scsi_done(cmd);
1da177e4
LT
1891 /* XXX - need to source or sink data here, as appropriate */
1892 } else
1893 cmd->SCp.this_residual -= transfersize - len;
1894 } else
1895#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
1896 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1897 &cmd->SCp.ptr);
1898 break;
1899 case PHASE_MSGIN:
1900 len = 1;
1901 data = &tmp;
1902 NCR5380_transfer_pio(instance, &phase, &len, &data);
1903 cmd->SCp.Message = tmp;
1904
1905 switch (tmp) {
1da177e4
LT
1906 case ABORT:
1907 case COMMAND_COMPLETE:
1908 /* Accept message by clearing ACK */
1909 sink = 1;
1910 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1911 hostdata->connected = NULL;
9cb78c16
HR
1912 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
1913 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
1da177e4
LT
1914
1915 /*
1916 * I'm not sure what the correct thing to do here is :
1917 *
1918 * If the command that just executed is NOT a request
1919 * sense, the obvious thing to do is to set the result
1920 * code to the values of the stored parameters.
1921 *
1922 * If it was a REQUEST SENSE command, we need some way
1923 * to differentiate between the failure code of the original
1924 * and the failure code of the REQUEST sense - the obvious
1925 * case is success, where we fall through and leave the result
1926 * code unchanged.
1927 *
1928 * The non-obvious place is where the REQUEST SENSE failed
1929 */
1930
1931 if (cmd->cmnd[0] != REQUEST_SENSE)
1932 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1933 else if (status_byte(cmd->SCp.Status) != GOOD)
1934 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1935
28424d3a
BH
1936 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1937 hostdata->ses.cmd_len) {
1938 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1939 hostdata->ses.cmd_len = 0 ;
1940 }
1941
1da177e4 1942 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
28424d3a
BH
1943 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1944
52a6a1cb 1945 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
1da177e4
LT
1946
1947 LIST(cmd, hostdata->issue_queue);
1948 cmd->host_scribble = (unsigned char *)
1949 hostdata->issue_queue;
710ddd0d 1950 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
52a6a1cb 1951 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
997acab7 1952 } else {
1da177e4
LT
1953 cmd->scsi_done(cmd);
1954 }
1955
1956 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1957 /*
1958 * Restore phase bits to 0 so an interrupted selection,
1959 * arbitration can resume.
1960 */
1961 NCR5380_write(TARGET_COMMAND_REG, 0);
1da177e4
LT
1962 return;
1963 case MESSAGE_REJECT:
1964 /* Accept message by clearing ACK */
1965 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1966 switch (hostdata->last_message) {
1967 case HEAD_OF_QUEUE_TAG:
1968 case ORDERED_QUEUE_TAG:
1969 case SIMPLE_QUEUE_TAG:
1970 cmd->device->simple_tags = 0;
9cb78c16 1971 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
1da177e4
LT
1972 break;
1973 default:
1974 break;
1975 }
340b9612 1976 break;
1da177e4
LT
1977 case DISCONNECT:{
1978 /* Accept message by clearing ACK */
1979 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1da177e4
LT
1980 LIST(cmd, hostdata->disconnected_queue);
1981 cmd->host_scribble = (unsigned char *)
1982 hostdata->disconnected_queue;
1983 hostdata->connected = NULL;
1984 hostdata->disconnected_queue = cmd;
9cb78c16 1985 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d lun %llu was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun);
1da177e4
LT
1986 /*
1987 * Restore phase bits to 0 so an interrupted selection,
1988 * arbitration can resume.
1989 */
1990 NCR5380_write(TARGET_COMMAND_REG, 0);
1991
1992 /* Enable reselect interrupts */
1993 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1da177e4
LT
1994 return;
1995 }
1996 /*
1997 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1998 * operation, in violation of the SCSI spec so we can safely
1999 * ignore SAVE/RESTORE pointers calls.
2000 *
2001 * Unfortunately, some disks violate the SCSI spec and
2002 * don't issue the required SAVE_POINTERS message before
2003 * disconnecting, and we have to break spec to remain
2004 * compatible.
2005 */
2006 case SAVE_POINTERS:
2007 case RESTORE_POINTERS:
2008 /* Accept message by clearing ACK */
2009 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2010 break;
2011 case EXTENDED_MESSAGE:
2012/*
2013 * Extended messages are sent in the following format :
2014 * Byte
2015 * 0 EXTENDED_MESSAGE == 1
2016 * 1 length (includes one byte for code, doesn't
2017 * include first two bytes)
2018 * 2 code
2019 * 3..length+1 arguments
2020 *
2021 * Start the extended message buffer with the EXTENDED_MESSAGE
1abfd370 2022 * byte, since spi_print_msg() wants the whole thing.
1da177e4
LT
2023 */
2024 extended_msg[0] = EXTENDED_MESSAGE;
2025 /* Accept first byte by clearing ACK */
2026 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
52a6a1cb 2027 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
1da177e4
LT
2028
2029 len = 2;
2030 data = extended_msg + 1;
2031 phase = PHASE_MSGIN;
2032 NCR5380_transfer_pio(instance, &phase, &len, &data);
2033
52a6a1cb 2034 dprintk(NDEBUG_EXTENDED, "scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]);
1da177e4
LT
2035
2036 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2037 /* Accept third byte by clearing ACK */
2038 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2039 len = extended_msg[1] - 1;
2040 data = extended_msg + 3;
2041 phase = PHASE_MSGIN;
2042
2043 NCR5380_transfer_pio(instance, &phase, &len, &data);
52a6a1cb 2044 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
1da177e4
LT
2045
2046 switch (extended_msg[2]) {
2047 case EXTENDED_SDTR:
2048 case EXTENDED_WDTR:
2049 case EXTENDED_MODIFY_DATA_POINTER:
2050 case EXTENDED_EXTENDED_IDENTIFY:
2051 tmp = 0;
2052 }
2053 } else if (len) {
2054 printk("scsi%d: error receiving extended message\n", instance->host_no);
2055 tmp = 0;
2056 } else {
2057 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2058 tmp = 0;
2059 }
2060 /* Fall through to reject message */
2061
2062 /*
2063 * If we get something weird that we aren't expecting,
2064 * reject it.
2065 */
2066 default:
2067 if (!tmp) {
2068 printk("scsi%d: rejecting message ", instance->host_no);
1abfd370 2069 spi_print_msg(extended_msg);
1da177e4
LT
2070 printk("\n");
2071 } else if (tmp != EXTENDED_MESSAGE)
017560fc
JG
2072 scmd_printk(KERN_INFO, cmd,
2073 "rejecting unknown message %02x\n",tmp);
1da177e4 2074 else
017560fc
JG
2075 scmd_printk(KERN_INFO, cmd,
2076 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
1da177e4
LT
2077
2078 msgout = MESSAGE_REJECT;
2079 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2080 break;
2081 } /* switch (tmp) */
2082 break;
2083 case PHASE_MSGOUT:
2084 len = 1;
2085 data = &msgout;
2086 hostdata->last_message = msgout;
2087 NCR5380_transfer_pio(instance, &phase, &len, &data);
2088 if (msgout == ABORT) {
9cb78c16 2089 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
1da177e4
LT
2090 hostdata->connected = NULL;
2091 cmd->result = DID_ERROR << 16;
1da177e4
LT
2092 cmd->scsi_done(cmd);
2093 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2094 return;
2095 }
2096 msgout = NOP;
2097 break;
2098 case PHASE_CMDOUT:
2099 len = cmd->cmd_len;
2100 data = cmd->cmnd;
2101 /*
2102 * XXX for performance reasons, on machines with a
2103 * PSEUDO-DMA architecture we should probably
2104 * use the dma transfer function.
2105 */
2106 NCR5380_transfer_pio(instance, &phase, &len, &data);
1da177e4
LT
2107 break;
2108 case PHASE_STATIN:
2109 len = 1;
2110 data = &tmp;
2111 NCR5380_transfer_pio(instance, &phase, &len, &data);
2112 cmd->SCp.Status = tmp;
2113 break;
2114 default:
2115 printk("scsi%d : unknown phase\n", instance->host_no);
4dde8f7d 2116 NCR5380_dprint(NDEBUG_ANY, instance);
1da177e4 2117 } /* switch(phase) */
686f3990
FT
2118 } else {
2119 spin_unlock_irq(instance->host_lock);
2120 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2121 spin_lock_irq(instance->host_lock);
1da177e4
LT
2122 }
2123 } /* while (1) */
2124}
2125
2126/*
2127 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2128 *
2129 * Purpose : does reselection, initializing the instance->connected
710ddd0d 2130 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1da177e4
LT
2131 * nexus has been reestablished,
2132 *
2133 * Inputs : instance - this instance of the NCR5380.
2134 *
2135 * Locks: io_request_lock held by caller if IRQ driven
2136 */
2137
2138static void NCR5380_reselect(struct Scsi_Host *instance) {
1da177e4
LT
2139 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2140 instance->hostdata;
2141 unsigned char target_mask;
2142 unsigned char lun, phase;
2143 int len;
2144 unsigned char msg[3];
2145 unsigned char *data;
710ddd0d 2146 struct scsi_cmnd *tmp = NULL, *prev;
1da177e4 2147 int abort = 0;
1da177e4
LT
2148
2149 /*
2150 * Disable arbitration, etc. since the host adapter obviously
2151 * lost, and tell an interrupted NCR5380_select() to restart.
2152 */
2153
2154 NCR5380_write(MODE_REG, MR_BASE);
1da177e4
LT
2155
2156 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
52a6a1cb 2157 dprintk(NDEBUG_SELECTION, "scsi%d : reselect\n", instance->host_no);
1da177e4
LT
2158
2159 /*
2160 * At this point, we have detected that our SCSI ID is on the bus,
2161 * SEL is true and BSY was false for at least one bus settle delay
2162 * (400 ns).
2163 *
2164 * We must assert BSY ourselves, until the target drops the SEL
2165 * signal.
2166 */
2167
2168 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2169
2170 /* FIXME: timeout too long, must fail to workqueue */
2171 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2172 abort = 1;
2173
2174 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2175
2176 /*
2177 * Wait for target to go into MSGIN.
2178 * FIXME: timeout needed and fail to work queeu
2179 */
2180
1cc160e1
FT
2181 if (NCR5380_poll_politely(instance,
2182 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0)
1da177e4
LT
2183 abort = 1;
2184
2185 len = 1;
2186 data = msg;
2187 phase = PHASE_MSGIN;
2188 NCR5380_transfer_pio(instance, &phase, &len, &data);
2189
2190 if (!(msg[0] & 0x80)) {
2191 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
1abfd370 2192 spi_print_msg(msg);
1da177e4
LT
2193 abort = 1;
2194 } else {
2195 /* Accept message by clearing ACK */
2196 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2197 lun = (msg[0] & 0x07);
2198
2199 /*
2200 * We need to add code for SCSI-II to track which devices have
2201 * I_T_L_Q nexuses established, and which have simple I_T_L
2202 * nexuses so we can chose to do additional data transfer.
2203 */
2204
2205 /*
2206 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2207 * just reestablished, and remove it from the disconnected queue.
2208 */
2209
2210
710ddd0d 2211 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
9cb78c16 2212 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
1da177e4
LT
2213 ) {
2214 if (prev) {
2215 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2216 prev->host_scribble = tmp->host_scribble;
2217 } else {
2218 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
710ddd0d 2219 hostdata->disconnected_queue = (struct scsi_cmnd *) tmp->host_scribble;
1da177e4
LT
2220 }
2221 tmp->host_scribble = NULL;
2222 break;
2223 }
2224 if (!tmp) {
2225 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2226 /*
2227 * Since we have an established nexus that we can't do anything with,
2228 * we must abort it.
2229 */
2230 abort = 1;
2231 }
2232 }
2233
2234 if (abort) {
2235 do_abort(instance);
2236 } else {
2237 hostdata->connected = tmp;
9cb78c16 2238 dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n", instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
1da177e4
LT
2239 }
2240}
2241
2242/*
2243 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2244 *
2245 * Purpose : called by interrupt handler when DMA finishes or a phase
2246 * mismatch occurs (which would finish the DMA transfer).
2247 *
2248 * Inputs : instance - this instance of the NCR5380.
2249 *
710ddd0d 2250 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
1da177e4
LT
2251 * nexus has been reestablished, on failure NULL is returned.
2252 */
2253
2254#ifdef REAL_DMA
2255static void NCR5380_dma_complete(NCR5380_instance * instance) {
f8343685 2256 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1da177e4 2257 int transferred;
1da177e4
LT
2258
2259 /*
2260 * XXX this might not be right.
2261 *
2262 * Wait for final byte to transfer, ie wait for ACK to go false.
2263 *
2264 * We should use the Last Byte Sent bit, unfortunately this is
2265 * not available on the 5380/5381 (only the various CMOS chips)
2266 *
2267 * FIXME: timeout, and need to handle long timeout/irq case
2268 */
2269
2270 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2271
2272 NCR5380_write(MODE_REG, MR_BASE);
2273 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2274
2275 /*
2276 * The only places we should see a phase mismatch and have to send
2277 * data from the same set of pointers will be the data transfer
2278 * phases. So, residual, requested length are only important here.
2279 */
2280
2281 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2282 transferred = instance->dmalen - NCR5380_dma_residual();
2283 hostdata->connected->SCp.this_residual -= transferred;
2284 hostdata->connected->SCp.ptr += transferred;
2285 }
2286}
2287#endif /* def REAL_DMA */
2288
2289/*
710ddd0d 2290 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
1da177e4
LT
2291 *
2292 * Purpose : abort a command
2293 *
710ddd0d 2294 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
b6c92b7e 2295 * host byte of the result field to, if zero DID_ABORTED is
1da177e4
LT
2296 * used.
2297 *
b6c92b7e 2298 * Returns : SUCCESS - success, FAILED on failure.
1da177e4 2299 *
b6c92b7e
HR
2300 * XXX - there is no way to abort the command that is currently
2301 * connected, you have to wait for it to complete. If this is
1da177e4
LT
2302 * a problem, we could implement longjmp() / setjmp(), setjmp()
2303 * called where the loop started in NCR5380_main().
2304 *
2305 * Locks: host lock taken by caller
2306 */
2307
710ddd0d
FT
2308static int NCR5380_abort(struct scsi_cmnd *cmd)
2309{
1da177e4
LT
2310 struct Scsi_Host *instance = cmd->device->host;
2311 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
710ddd0d 2312 struct scsi_cmnd *tmp, **prev;
1fa6b5fb
HR
2313
2314 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
1da177e4 2315
9dafbd93 2316 spin_lock_irq(instance->host_lock);
1da177e4
LT
2317 NCR5380_print_status(instance);
2318
52a6a1cb
FT
2319 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2320 dprintk(NDEBUG_ABORT, " basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
1da177e4
LT
2321
2322#if 0
2323/*
2324 * Case 1 : If the command is the currently executing command,
2325 * we'll set the aborted flag and return control so that
2326 * information transfer routine can exit cleanly.
2327 */
2328
2329 if (hostdata->connected == cmd) {
52a6a1cb 2330 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
1da177e4
LT
2331/*
2332 * We should perform BSY checking, and make sure we haven't slipped
2333 * into BUS FREE.
2334 */
2335
2336 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2337/*
2338 * Since we can't change phases until we've completed the current
2339 * handshake, we have to source or sink a byte of data if the current
2340 * phase is not MSGOUT.
2341 */
2342
2343/*
2344 * Return control to the executing NCR drive so we can clear the
2345 * aborted flag and get back into our main loop.
2346 */
2347
b6c92b7e 2348 return SUCCESS;
1da177e4
LT
2349 }
2350#endif
2351
2352/*
2353 * Case 2 : If the command hasn't been issued yet, we simply remove it
2354 * from the issue queue.
2355 */
2356
52a6a1cb 2357 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
710ddd0d 2358 for (prev = (struct scsi_cmnd **) &(hostdata->issue_queue), tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble)
1da177e4
LT
2359 if (cmd == tmp) {
2360 REMOVE(5, *prev, tmp, tmp->host_scribble);
710ddd0d 2361 (*prev) = (struct scsi_cmnd *) tmp->host_scribble;
1da177e4 2362 tmp->host_scribble = NULL;
9dafbd93 2363 spin_unlock_irq(instance->host_lock);
1da177e4 2364 tmp->result = DID_ABORT << 16;
52a6a1cb 2365 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
cce99c69 2366 tmp->scsi_done(tmp);
1da177e4
LT
2367 return SUCCESS;
2368 }
2369#if (NDEBUG & NDEBUG_ABORT)
2370 /* KLL */
2371 else if (prev == tmp)
2372 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2373#endif
2374
2375/*
2376 * Case 3 : If any commands are connected, we're going to fail the abort
2377 * and let the high level SCSI driver retry at a later time or
2378 * issue a reset.
2379 *
2380 * Timeouts, and therefore aborted commands, will be highly unlikely
2381 * and handling them cleanly in this situation would make the common
2382 * case of noresets less efficient, and would pollute our code. So,
2383 * we fail.
2384 */
2385
2386 if (hostdata->connected) {
9dafbd93 2387 spin_unlock_irq(instance->host_lock);
52a6a1cb 2388 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
1da177e4
LT
2389 return FAILED;
2390 }
2391/*
2392 * Case 4: If the command is currently disconnected from the bus, and
2393 * there are no connected commands, we reconnect the I_T_L or
2394 * I_T_L_Q nexus associated with it, go into message out, and send
2395 * an abort message.
2396 *
2397 * This case is especially ugly. In order to reestablish the nexus, we
2398 * need to call NCR5380_select(). The easiest way to implement this
2399 * function was to abort if the bus was busy, and let the interrupt
2400 * handler triggered on the SEL for reselect take care of lost arbitrations
2401 * where necessary, meaning interrupts need to be enabled.
2402 *
2403 * When interrupts are enabled, the queues may change - so we
2404 * can't remove it from the disconnected queue before selecting it
2405 * because that could cause a failure in hashing the nexus if that
2406 * device reselected.
2407 *
2408 * Since the queues may change, we can't use the pointers from when we
2409 * first locate it.
2410 *
2411 * So, we must first locate the command, and if NCR5380_select()
2412 * succeeds, then issue the abort, relocate the command and remove
2413 * it from the disconnected queue.
2414 */
2415
710ddd0d 2416 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
1da177e4 2417 if (cmd == tmp) {
52a6a1cb 2418 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
1da177e4 2419
9dafbd93
FT
2420 if (NCR5380_select(instance, cmd)) {
2421 spin_unlock_irq(instance->host_lock);
1da177e4 2422 return FAILED;
9dafbd93 2423 }
52a6a1cb 2424 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
1da177e4
LT
2425
2426 do_abort(instance);
2427
710ddd0d 2428 for (prev = (struct scsi_cmnd **) &(hostdata->disconnected_queue), tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble)
1da177e4
LT
2429 if (cmd == tmp) {
2430 REMOVE(5, *prev, tmp, tmp->host_scribble);
710ddd0d 2431 *prev = (struct scsi_cmnd *) tmp->host_scribble;
1da177e4 2432 tmp->host_scribble = NULL;
9dafbd93 2433 spin_unlock_irq(instance->host_lock);
1da177e4 2434 tmp->result = DID_ABORT << 16;
cce99c69 2435 tmp->scsi_done(tmp);
1da177e4
LT
2436 return SUCCESS;
2437 }
2438 }
2439/*
2440 * Case 5 : If we reached this point, the command was not found in any of
2441 * the queues.
2442 *
2443 * We probably reached this point because of an unlikely race condition
2444 * between the command completing successfully and the abortion code,
2445 * so we won't panic, but we will notify the user in case something really
2446 * broke.
2447 */
9dafbd93 2448 spin_unlock_irq(instance->host_lock);
1da177e4
LT
2449 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2450 " before abortion\n", instance->host_no);
2451 return FAILED;
2452}
2453
2454
3be1b3ea
FT
2455/**
2456 * NCR5380_bus_reset - reset the SCSI bus
2457 * @cmd: SCSI command undergoing EH
1da177e4 2458 *
3be1b3ea 2459 * Returns SUCCESS
1da177e4
LT
2460 */
2461
710ddd0d 2462static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
68b3aa7c
JG
2463{
2464 struct Scsi_Host *instance = cmd->device->host;
2465
3be1b3ea
FT
2466 spin_lock_irq(instance->host_lock);
2467
2468#if (NDEBUG & NDEBUG_ANY)
2469 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
68b3aa7c 2470 NCR5380_print_status(instance);
3be1b3ea 2471#endif
68b3aa7c 2472
68b3aa7c 2473 do_reset(instance);
3be1b3ea 2474
68b3aa7c 2475 spin_unlock_irq(instance->host_lock);
1da177e4 2476
1da177e4
LT
2477 return SUCCESS;
2478}