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