blob: 8567d69709ce9e406994db07ec9b559adc4d3884 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Revision 1.10 1998/9/2 Alan Cox
Alan Coxfa195af2008-10-27 15:16:36 +000029 * (alan@lxorguk.ukuu.org.uk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
db9dff32005-04-03 14:53:59 -050082#include <scsi/scsi_dbg.h>
Matthew Wilcox1abfd372005-12-15 16:22:01 -050083#include <scsi/scsi_transport_spi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#if (NDEBUG & NDEBUG_LISTS)
86#define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
87#define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
88#else
89#define LIST(x,y)
90#define REMOVE(w,x,y,z)
91#endif
92
93#ifndef notyet
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#undef REAL_DMA
95#endif
96
97#ifdef REAL_DMA_POLL
98#undef READ_OVERRUNS
99#define READ_OVERRUNS
100#endif
101
102#ifdef BOARD_REQUIRES_NO_DELAY
103#define io_recovery_delay(x)
104#else
105#define io_recovery_delay(x) udelay(x)
106#endif
107
108/*
109 * Design
110 *
111 * This is a generic 5380 driver. To use it on a different platform,
112 * one simply writes appropriate system specific macros (ie, data
113 * transfer - some PC's will use the I/O bus, 68K's must use
114 * memory mapped) and drops this file in their 'C' wrapper.
115 *
116 * (Note from hch: unfortunately it was not enough for the different
117 * m68k folks and instead of improving this driver they copied it
118 * and hacked it up for their needs. As a consequence they lost
119 * most updates to this driver. Maybe someone will fix all these
120 * drivers to use a common core one day..)
121 *
122 * As far as command queueing, two queues are maintained for
123 * each 5380 in the system - commands that haven't been issued yet,
124 * and commands that are currently executing. This means that an
125 * unlimited number of commands may be queued, letting
126 * more commands propagate from the higher driver levels giving higher
127 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
128 * allowing multiple commands to propagate all the way to a SCSI-II device
129 * while a command is already executing.
130 *
131 *
132 * Issues specific to the NCR5380 :
133 *
134 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
135 * piece of hardware that requires you to sit in a loop polling for
136 * the REQ signal as long as you are connected. Some devices are
137 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +1100138 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * broken devices are the exception rather than the rule and I'd rather
140 * spend my time optimizing for the normal case.
141 *
142 * Architecture :
143 *
144 * At the heart of the design is a coroutine, NCR5380_main,
145 * which is started from a workqueue for each NCR5380 host in the
146 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
147 * removing the commands from the issue queue and calling
148 * NCR5380_select() if a nexus is not established.
149 *
150 * Once a nexus is established, the NCR5380_information_transfer()
151 * phase goes through the various phases as instructed by the target.
152 * if the target goes into MSG IN and sends a DISCONNECT message,
153 * the command structure is placed into the per instance disconnected
154 * queue, and NCR5380_main tries to find more work. If the target is
155 * idle for too long, the system will try to sleep.
156 *
157 * If a command has disconnected, eventually an interrupt will trigger,
158 * calling NCR5380_intr() which will in turn call NCR5380_reselect
159 * to reestablish a nexus. This will run main if necessary.
160 *
161 * On command termination, the done function will be called as
162 * appropriate.
163 *
164 * SCSI pointers are maintained in the SCp field of SCSI command
165 * structures, being initialized after the command is connected
166 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
167 * Note that in violation of the standard, an implicit SAVE POINTERS operation
168 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
169 */
170
171/*
172 * Using this file :
173 * This file a skeleton Linux SCSI driver for the NCR 5380 series
174 * of chips. To use it, you write an architecture specific functions
175 * and macros and include this file in your driver.
176 *
177 * These macros control options :
178 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
179 * defined.
180 *
181 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
182 * for commands that return with a CHECK CONDITION status.
183 *
184 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
185 * transceivers.
186 *
187 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
188 * override-configure an IRQ.
189 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
191 *
192 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
193 *
194 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
195 * rely on phase mismatch and EOP interrupts to determine end
196 * of phase.
197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * Defaults for these will be provided although the user may want to adjust
199 * these to allocate CPU resources to the SCSI driver or "real" code.
200 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * These macros MUST be defined :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
203 * NCR5380_read(register) - read from the specified register
204 *
205 * NCR5380_write(register, value) - write to the specific register
206 *
207 * NCR5380_implementation_fields - additional fields needed for this
208 * specific implementation of the NCR5380
209 *
210 * Either real DMA *or* pseudo DMA may be implemented
211 * REAL functions :
212 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
213 * Note that the DMA setup functions should return the number of bytes
214 * that they were able to program the controller for.
215 *
216 * Also note that generic i386/PC versions of these macros are
217 * available as NCR5380_i386_dma_write_setup,
218 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
219 *
220 * NCR5380_dma_write_setup(instance, src, count) - initialize
221 * NCR5380_dma_read_setup(instance, dst, count) - initialize
222 * NCR5380_dma_residual(instance); - residual count
223 *
224 * PSEUDO functions :
225 * NCR5380_pwrite(instance, src, count)
226 * NCR5380_pread(instance, dst, count);
227 *
228 * The generic driver is initialized by calling NCR5380_init(instance),
229 * after setting the appropriate host specific fields and ID. If the
230 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
231 * possible) function may be used.
232 */
233
Finn Thain54d8fe42016-01-03 16:05:06 +1100234static int do_abort(struct Scsi_Host *);
235static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237/*
238 * initialize_SCp - init the scsi pointer field
239 * @cmd: command block to set up
240 *
241 * Set up the internal fields in the SCSI command.
242 */
243
Finn Thain710ddd02014-11-12 16:12:02 +1100244static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 /*
247 * Initialize the Scsi Pointer field so that all of the commands in the
248 * various queues are valid.
249 */
250
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200251 if (scsi_bufflen(cmd)) {
252 cmd->SCp.buffer = scsi_sglist(cmd);
253 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200254 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 cmd->SCp.this_residual = cmd->SCp.buffer->length;
256 } else {
257 cmd->SCp.buffer = NULL;
258 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200259 cmd->SCp.ptr = NULL;
260 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262}
263
264/**
Finn Thain2f854b82016-01-03 16:05:22 +1100265 * NCR5380_poll_politely - wait for chip register value
266 * @instance: controller to poll
267 * @reg: 5380 register to poll
268 * @bit: Bitmask to check
269 * @val: Value required to exit
270 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
Finn Thain2f854b82016-01-03 16:05:22 +1100272 * Polls the chip in a reasonably efficient manner waiting for an
273 * event to occur. After a short quick poll we begin to yield the CPU
274 * (if possible). In irq contexts the time-out is arbitrarily limited.
275 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *
Finn Thain2f854b82016-01-03 16:05:22 +1100277 * Returns 0 if event occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Finn Thain2f854b82016-01-03 16:05:22 +1100280static int NCR5380_poll_politely(struct Scsi_Host *instance,
281 int reg, int bit, int val, int wait)
282{
283 struct NCR5380_hostdata *hostdata = shost_priv(instance);
284 unsigned long deadline = jiffies + wait;
285 unsigned long n;
286
287 /* Busy-wait for up to 10 ms */
288 n = min(10000U, jiffies_to_usecs(wait));
289 n *= hostdata->accesses_per_ms;
290 n /= 1000;
291 do {
292 if ((NCR5380_read(reg) & bit) == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100295 } while (n--);
296
297 if (irqs_disabled() || in_interrupt())
298 return -ETIMEDOUT;
299
300 /* Repeatedly sleep for 1 ms until deadline */
301 while (time_is_after_jiffies(deadline)) {
302 schedule_timeout_uninterruptible(1);
303 if ((NCR5380_read(reg) & bit) == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Finn Thain2f854b82016-01-03 16:05:22 +1100306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -ETIMEDOUT;
308}
309
310static struct {
311 unsigned char value;
312 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700313} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 {PHASE_DATAOUT, "DATAOUT"},
315 {PHASE_DATAIN, "DATAIN"},
316 {PHASE_CMDOUT, "CMDOUT"},
317 {PHASE_STATIN, "STATIN"},
318 {PHASE_MSGOUT, "MSGOUT"},
319 {PHASE_MSGIN, "MSGIN"},
320 {PHASE_UNKNOWN, "UNKNOWN"}
321};
322
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100323#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static struct {
325 unsigned char mask;
326 const char *name;
327} signals[] = {
328 {SR_DBP, "PARITY"},
329 {SR_RST, "RST"},
330 {SR_BSY, "BSY"},
331 {SR_REQ, "REQ"},
332 {SR_MSG, "MSG"},
333 {SR_CD, "CD"},
334 {SR_IO, "IO"},
335 {SR_SEL, "SEL"},
336 {0, NULL}
337},
338basrs[] = {
339 {BASR_ATN, "ATN"},
340 {BASR_ACK, "ACK"},
341 {0, NULL}
342},
343icrs[] = {
344 {ICR_ASSERT_RST, "ASSERT RST"},
345 {ICR_ASSERT_ACK, "ASSERT ACK"},
346 {ICR_ASSERT_BSY, "ASSERT BSY"},
347 {ICR_ASSERT_SEL, "ASSERT SEL"},
348 {ICR_ASSERT_ATN, "ASSERT ATN"},
349 {ICR_ASSERT_DATA, "ASSERT DATA"},
350 {0, NULL}
351},
352mrs[] = {
353 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
354 {MR_TARGET, "MODE TARGET"},
355 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
356 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
357 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
358 {MR_DMA_MODE, "MODE DMA"},
359 {MR_ARBITRATE, "MODE ARBITRATION"},
360 {0, NULL}
361};
362
363/**
364 * NCR5380_print - print scsi bus signals
365 * @instance: adapter state to dump
366 *
367 * Print the SCSI bus signals for debugging purposes
368 *
369 * Locks: caller holds hostdata lock (not essential)
370 */
371
372static void NCR5380_print(struct Scsi_Host *instance)
373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
377 status = NCR5380_read(STATUS_REG);
378 mr = NCR5380_read(MODE_REG);
379 icr = NCR5380_read(INITIATOR_COMMAND_REG);
380 basr = NCR5380_read(BUS_AND_STATUS_REG);
381
382 printk("STATUS_REG: %02x ", status);
383 for (i = 0; signals[i].mask; ++i)
384 if (status & signals[i].mask)
385 printk(",%s", signals[i].name);
386 printk("\nBASR: %02x ", basr);
387 for (i = 0; basrs[i].mask; ++i)
388 if (basr & basrs[i].mask)
389 printk(",%s", basrs[i].name);
390 printk("\nICR: %02x ", icr);
391 for (i = 0; icrs[i].mask; ++i)
392 if (icr & icrs[i].mask)
393 printk(",%s", icrs[i].name);
394 printk("\nMODE: %02x ", mr);
395 for (i = 0; mrs[i].mask; ++i)
396 if (mr & mrs[i].mask)
397 printk(",%s", mrs[i].name);
398 printk("\n");
399}
400
401
402/*
403 * NCR5380_print_phase - show SCSI phase
404 * @instance: adapter to dump
405 *
406 * Print the current SCSI phase for debugging purposes
407 *
408 * Locks: none
409 */
410
411static void NCR5380_print_phase(struct Scsi_Host *instance)
412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 unsigned char status;
414 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 status = NCR5380_read(STATUS_REG);
417 if (!(status & SR_REQ))
418 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
419 else {
420 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
421 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
422 }
423}
424#endif
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Finn Thaind5f7e652016-01-03 16:05:03 +1100427static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429/**
430 * probe_intr - helper for IRQ autoprobe
431 * @irq: interrupt number
432 * @dev_id: unused
433 * @regs: unused
434 *
435 * Set a flag to indicate the IRQ in question was received. This is
436 * used by the IRQ probe code.
437 */
438
David Howells7d12e782006-10-05 14:55:46 +0100439static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 probe_irq = irq;
442 return IRQ_HANDLED;
443}
444
445/**
446 * NCR5380_probe_irq - find the IRQ of an NCR5380
447 * @instance: NCR5380 controller
448 * @possible: bitmask of ISA IRQ lines
449 *
450 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
451 * and then looking to see what interrupt actually turned up.
452 *
453 * Locks: none, irqs must be enabled on entry
454 */
455
Andrew Morton702809c2007-05-23 14:41:56 -0700456static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
457 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
460 unsigned long timeout;
461 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Finn Thain22f5f102014-11-12 16:11:56 +1100463 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100464 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 trying_irqs |= mask;
466
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500467 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100468 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /*
471 * A interrupt is triggered whenever BSY = false, SEL = true
472 * and a bit set in the SELECT_ENABLE_REG is asserted on the
473 * SCSI bus.
474 *
475 * Note that the bus is only driven when the phase control signals
476 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
477 * to zero.
478 */
479
480 NCR5380_write(TARGET_COMMAND_REG, 0);
481 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
482 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
483 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
484
Finn Thain22f5f102014-11-12 16:11:56 +1100485 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800486 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 NCR5380_write(SELECT_ENABLE_REG, 0);
489 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
490
Finn Thain22f5f102014-11-12 16:11:56 +1100491 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (trying_irqs & mask)
493 free_irq(i, NULL);
494
495 return probe_irq;
496}
497
498/**
Finn Thain8c325132014-11-12 16:11:58 +1100499 * NCR58380_info - report driver and host information
500 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *
Finn Thain8c325132014-11-12 16:11:58 +1100502 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *
504 * Locks: none
505 */
506
Finn Thain8c325132014-11-12 16:11:58 +1100507static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Finn Thain8c325132014-11-12 16:11:58 +1100509 struct NCR5380_hostdata *hostdata = shost_priv(instance);
510
511 return hostdata->info;
512}
513
514static void prepare_info(struct Scsi_Host *instance)
515{
516 struct NCR5380_hostdata *hostdata = shost_priv(instance);
517
518 snprintf(hostdata->info, sizeof(hostdata->info),
519 "%s, io_port 0x%lx, n_io_port %d, "
520 "base 0x%lx, irq %d, "
521 "can_queue %d, cmd_per_lun %d, "
522 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100523 "flags { %s%s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100524 "options { %s} ",
525 instance->hostt->name, instance->io_port, instance->n_io_port,
526 instance->base, instance->irq,
527 instance->can_queue, instance->cmd_per_lun,
528 instance->sg_tablesize, instance->this_id,
529 hostdata->flags & FLAG_NCR53C400 ? "NCR53C400 " : "",
530 hostdata->flags & FLAG_DTC3181E ? "DTC3181E " : "",
531 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100532 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100534 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100537 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#endif
539#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100540 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#endif
542#ifdef REAL_DMA_POLL
Finn Thain8c325132014-11-12 16:11:58 +1100543 "REAL_DMA_POLL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544#endif
545#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100546 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#endif
548#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100549 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#endif
Finn Thain8c325132014-11-12 16:11:58 +1100551 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/**
555 * NCR5380_print_status - dump controller info
556 * @instance: controller to dump
557 *
558 * Print commands in the various queues, called from NCR5380_abort
559 * and NCR5380_debug to aid debugging.
560 *
561 * Locks: called functions disable irqs
562 */
563
Finn Thain3be1b3e2016-01-03 16:05:12 +1100564static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 NCR5380_dprint(NDEBUG_ANY, instance);
567 NCR5380_dprint_phase(NDEBUG_ANY, instance);
568}
569
Finn Thaina9c2dc42014-11-12 16:11:59 +1100570#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/******************************************/
572/*
573 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
574 *
575 * *buffer: I/O buffer
576 * **start: if inout == FALSE pointer into buffer where user read should start
577 * offset: current offset
578 * length: length of buffer
579 * hostno: Scsi_Host host_no
580 * inout: TRUE - user is writing; FALSE - user is reading
581 *
582 * Return the number of bytes read from or written
583 */
584
Al Virodd7ab712013-03-31 01:15:54 -0400585static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
586 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100588 struct NCR5380_hostdata *hostdata = shost_priv(instance);
589
590 hostdata->spin_max_r = 0;
591 hostdata->spin_max_w = 0;
592 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400593}
Finn Thaina9c2dc42014-11-12 16:11:59 +1100594#endif
Al Virodd7ab712013-03-31 01:15:54 -0400595
Al Virodd7ab712013-03-31 01:15:54 -0400596static
Finn Thain710ddd02014-11-12 16:12:02 +1100597void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m);
Al Virodd7ab712013-03-31 01:15:54 -0400598static
599void lprint_command(unsigned char *cmd, struct seq_file *m);
600static
601void lprint_opcode(int opcode, struct seq_file *m);
602
603static int __maybe_unused NCR5380_show_info(struct seq_file *m,
604 struct Scsi_Host *instance)
605{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100607 struct scsi_cmnd *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
610
Finn Thaina9c2dc42014-11-12 16:11:59 +1100611#ifdef PSEUDO_DMA
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100612 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100613 hostdata->spin_max_w, hostdata->spin_max_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif
615 spin_lock_irq(instance->host_lock);
616 if (!hostdata->connected)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100617 seq_printf(m, "scsi%d: no currently connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 else
Finn Thain710ddd02014-11-12 16:12:02 +1100619 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100620 seq_printf(m, "scsi%d: issue_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100621 for (ptr = (struct scsi_cmnd *) hostdata->issue_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400622 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100624 seq_printf(m, "scsi%d: disconnected_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100625 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400626 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 spin_unlock_irq(instance->host_lock);
Al Virodd7ab712013-03-31 01:15:54 -0400628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Finn Thain710ddd02014-11-12 16:12:02 +1100631static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100633 seq_printf(m, "scsi%d : destination target %d, lun %llu\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
Rasmus Villemoes91c40f22014-12-03 00:10:52 +0100634 seq_puts(m, " command = ");
Al Virodd7ab712013-03-31 01:15:54 -0400635 lprint_command(cmd->cmnd, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Al Virodd7ab712013-03-31 01:15:54 -0400638static void lprint_command(unsigned char *command, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 int i, s;
Al Virodd7ab712013-03-31 01:15:54 -0400641 lprint_opcode(command[0], m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100643 seq_printf(m, "%02x ", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100644 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Al Virodd7ab712013-03-31 01:15:54 -0400647static void lprint_opcode(int opcode, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100649 seq_printf(m, "%2d (0x%02x)", opcode, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652
653/**
654 * NCR5380_init - initialise an NCR5380
655 * @instance: adapter to configure
656 * @flags: control flags
657 *
658 * Initializes *instance and corresponding 5380 chip,
659 * with flags OR'd into the initial flags value.
660 *
661 * Notes : I assume that the host, hostno, and id bits have been
662 * set correctly. I don't care about the irq and other fields.
663 *
664 * Returns 0 for success
665 *
666 * Locks: interrupts must be enabled when we are called
667 */
668
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800669static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Finn Thainb6488f92016-01-03 16:05:08 +1100671 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain2f854b82016-01-03 16:05:22 +1100673 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if(in_interrupt())
676 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 hostdata->id_mask = 1 << instance->this_id;
679 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
680 if (i > hostdata->id_mask)
681 hostdata->id_higher_mask |= i;
682 for (i = 0; i < 8; ++i)
683 hostdata->busy[i] = 0;
684#ifdef REAL_DMA
685 hostdata->dmalen = 0;
686#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 hostdata->connected = NULL;
688 hostdata->issue_queue = NULL;
689 hostdata->disconnected_queue = NULL;
690
Finn Thain8d8601a2016-01-03 16:05:37 +1100691 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100692 hostdata->work_q = alloc_workqueue("ncr5380_%d",
693 WQ_UNBOUND | WQ_MEM_RECLAIM,
694 1, instance->host_no);
695 if (!hostdata->work_q)
696 return -ENOMEM;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* The CHECK code seems to break the 53C400. Will check it later maybe */
699 if (flags & FLAG_NCR53C400)
700 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
701 else
702 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
703
704 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Finn Thain8c325132014-11-12 16:11:58 +1100706 prepare_info(instance);
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
709 NCR5380_write(MODE_REG, MR_BASE);
710 NCR5380_write(TARGET_COMMAND_REG, 0);
711 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100712
713 /* Calibrate register polling loop */
714 i = 0;
715 deadline = jiffies + 1;
716 do {
717 cpu_relax();
718 } while (time_is_after_jiffies(deadline));
719 deadline += msecs_to_jiffies(256);
720 do {
721 NCR5380_read(STATUS_REG);
722 ++i;
723 cpu_relax();
724 } while (time_is_after_jiffies(deadline));
725 hostdata->accesses_per_ms = i / 256;
726
Finn Thainb6488f92016-01-03 16:05:08 +1100727 return 0;
728}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Finn Thainb6488f92016-01-03 16:05:08 +1100730/**
731 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
732 * @instance: adapter to check
733 *
734 * If the system crashed, it may have crashed with a connected target and
735 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
736 * currently established nexus, which we know nothing about. Failing that
737 * do a bus reset.
738 *
739 * Note that a bus reset will cause the chip to assert IRQ.
740 *
741 * Returns 0 if successful, otherwise -ENXIO.
742 */
743
744static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
745{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100746 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100747 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
750 switch (pass) {
751 case 1:
752 case 3:
753 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100754 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
755 NCR5380_poll_politely(instance,
756 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100759 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 do_abort(instance);
761 break;
762 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100763 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100765 /* Wait after a reset; the SCSI standard calls for
766 * 250ms, we wait 500ms to be on the safe side.
767 * But some Toshiba CD-ROMs need ten times that.
768 */
769 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
770 msleep(2500);
771 else
772 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
774 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100775 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -ENXIO;
777 }
778 }
779 return 0;
780}
781
782/**
783 * NCR5380_exit - remove an NCR5380
784 * @instance: adapter to remove
785 */
786
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800787static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
790
Finn Thain8d8601a2016-01-03 16:05:37 +1100791 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100792 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795/**
Finn Thain1bb40582016-01-03 16:05:29 +1100796 * NCR5380_queue_command - queue a command
797 * @instance: the relevant SCSI adapter
798 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *
Finn Thain1bb40582016-01-03 16:05:29 +1100800 * cmd is added to the per-instance issue queue, with minor
801 * twiddling done to the host specific fields of cmd. If the
802 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
804
Finn Thain1bb40582016-01-03 16:05:29 +1100805static int NCR5380_queue_command(struct Scsi_Host *instance,
806 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Finn Thain1bb40582016-01-03 16:05:29 +1100808 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100809 struct scsi_cmnd *tmp;
Finn Thain1bb40582016-01-03 16:05:29 +1100810 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812#if (NDEBUG & NDEBUG_NO_WRITE)
813 switch (cmd->cmnd[0]) {
814 case WRITE_6:
815 case WRITE_10:
816 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
817 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100818 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820 }
821#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /*
824 * We use the host_scribble field as a pointer to the next command
825 * in a queue
826 */
827
828 cmd->host_scribble = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 cmd->result = 0;
830
Finn Thain1bb40582016-01-03 16:05:29 +1100831 spin_lock_irqsave(instance->host_lock, flags);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * Insert the cmd into the issue queue. Note that REQUEST SENSE
835 * commands are added to the head of the queue since any command will
836 * clear the contingent allegiance condition that exists and the
837 * sense data is only guaranteed to be valid while the condition exists.
838 */
839
840 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
841 LIST(cmd, hostdata->issue_queue);
842 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
843 hostdata->issue_queue = cmd;
844 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100845 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (struct scsi_cmnd *) tmp->host_scribble);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 LIST(cmd, tmp);
847 tmp->host_scribble = (unsigned char *) cmd;
848 }
Finn Thain1bb40582016-01-03 16:05:29 +1100849 spin_unlock_irqrestore(instance->host_lock, flags);
850
Finn Thain52a6a1c2014-03-18 11:42:18 +1100851 dprintk(NDEBUG_QUEUES, "scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100854 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return 0;
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/**
859 * NCR5380_main - NCR state machines
860 *
861 * NCR5380_main is a coroutine that runs as long as more work can
862 * be done on the NCR5380 host adapters in a system. Both
863 * NCR5380_queue_command() and NCR5380_intr() will try to start it
864 * in case it is not running.
865 *
866 * Locks: called as its own thread with no locks held. Takes the
867 * host lock and called routines may take the isa dma lock.
868 */
869
David Howellsc4028952006-11-22 14:57:56 +0000870static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
David Howellsc4028952006-11-22 14:57:56 +0000872 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100873 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100875 struct scsi_cmnd *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int done;
877
878 spin_lock_irq(instance->host_lock);
879 do {
880 /* Lock held here */
881 done = 1;
Finn Thainae753a32016-01-03 16:05:23 +1100882 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100883 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /*
885 * Search through the issue_queue for a command destined
886 * for a target that's not busy.
887 */
Finn Thain710ddd02014-11-12 16:12:02 +1100888 for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 {
890 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200891 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200893 if (!(hostdata->busy[tmp->device->id] &
894 (1 << (u8)(tmp->device->lun & 0xff)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (prev) {
896 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
897 prev->host_scribble = tmp->host_scribble;
898 } else {
899 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +1100900 hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 tmp->host_scribble = NULL;
903
904 /*
905 * Attempt to establish an I_T_L nexus here.
906 * On success, instance->hostdata->connected is set.
907 * On failure, we must add the command back to the
908 * issue queue so we can keep trying.
909 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200910 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Finn Thain76f13b92014-11-12 16:11:53 +1100912 /*
913 * REQUEST SENSE commands are issued without tagged
914 * queueing, even on SCSI-II devices because the
915 * contingent allegiance condition exists for the
916 * entire unit.
917 */
918
919 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100920 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100922 /* Need to retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 LIST(tmp, hostdata->issue_queue);
924 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
925 hostdata->issue_queue = tmp;
926 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +1100927 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Finn Thainae753a32016-01-03 16:05:23 +1100929 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +1100930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* lock held here still */
932 } /* if target/lun is not busy */
933 } /* for */
934 /* exited locked */
935 } /* if (!hostdata->connected) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (hostdata->connected
937#ifdef REAL_DMA
938 && !hostdata->dmalen
939#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100941 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +1100943 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 } while (!done);
947
948 spin_unlock_irq(instance->host_lock);
949}
950
951#ifndef DONT_USE_INTR
952
953/**
Finn Thaincd400822016-01-03 16:05:40 +1100954 * NCR5380_intr - generic NCR5380 irq handler
955 * @irq: interrupt number
956 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
Finn Thaincd400822016-01-03 16:05:40 +1100958 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
959 * from the disconnected queue, and restarting NCR5380_main()
960 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 *
Finn Thaincd400822016-01-03 16:05:40 +1100962 * The chip can assert IRQ in any of six different conditions. The IRQ flag
963 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
964 * Three of these six conditions are latched in the Bus and Status Register:
965 * - End of DMA (cleared by ending DMA Mode)
966 * - Parity error (cleared by reading RPIR)
967 * - Loss of BSY (cleared by reading RPIR)
968 * Two conditions have flag bits that are not latched:
969 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
970 * - Bus reset (non-maskable)
971 * The remaining condition has no flag bit at all:
972 * - Selection/reselection
973 *
974 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
975 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
976 * claimed that "the design of the [DP8490] interrupt logic ensures
977 * interrupts will not be lost (they can be on the DP5380)."
978 * The L5380/53C80 datasheet from LOGIC Devices has more details.
979 *
980 * Checking for bus reset by reading RST is futile because of interrupt
981 * latency, but a bus reset will reset chip logic. Checking for parity error
982 * is unnecessary because that interrupt is never enabled. A Loss of BSY
983 * condition will clear DMA Mode. We can tell when this occurs because the
984 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
986
Finn Thaincd400822016-01-03 16:05:40 +1100987static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800989 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100990 struct NCR5380_hostdata *hostdata = shost_priv(instance);
991 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 unsigned char basr;
993 unsigned long flags;
994
Finn Thaincd400822016-01-03 16:05:40 +1100995 spin_lock_irqsave(instance->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Finn Thaincd400822016-01-03 16:05:40 +1100997 basr = NCR5380_read(BUS_AND_STATUS_REG);
998 if (basr & BASR_IRQ) {
999 unsigned char mr = NCR5380_read(MODE_REG);
1000 unsigned char sr = NCR5380_read(STATUS_REG);
1001
1002 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1003 instance->host_no, irq, basr, sr, mr);
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001006 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1007 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1008 * We ack IRQ after clearing Mode Register. Workarounds
1009 * for End of DMA errata need to happen in DMA Mode.
1010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Finn Thaincd400822016-01-03 16:05:40 +11001012 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Finn Thaincd400822016-01-03 16:05:40 +11001014 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Finn Thaincd400822016-01-03 16:05:40 +11001016 if (!hostdata->connected)
1017 panic("scsi%d : DMA interrupt with no connected cmd\n",
1018 instance->hostno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Finn Thaincd400822016-01-03 16:05:40 +11001020 transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
1021 hostdata->connected->SCp.this_residual -= transferred;
1022 hostdata->connected->SCp.ptr += transferred;
1023 hostdata->dmalen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Finn Thaincd400822016-01-03 16:05:40 +11001025 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1026 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
1027
1028 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1029 NCR5380_write(MODE_REG, MR_BASE);
1030 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1031 } else
1032#endif /* REAL_DMA */
1033 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1034 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1035 /* Probably reselected */
1036 NCR5380_write(SELECT_ENABLE_REG, 0);
1037 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1038
1039 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
1040 instance->host_no);
1041
1042 if (!hostdata->connected) {
1043 NCR5380_reselect(instance);
1044 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Finn Thaincd400822016-01-03 16:05:40 +11001046 if (!hostdata->connected)
1047 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1048 } else {
1049 /* Probably Bus Reset */
1050 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1051
1052 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no);
1053 }
1054 handled = 1;
1055 } else {
1056 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
1057 }
1058
1059 spin_unlock_irqrestore(instance->host_lock, flags);
1060
1061 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064#endif
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*
Finn Thain710ddd02014-11-12 16:12:02 +11001067 * Function : int NCR5380_select(struct Scsi_Host *instance,
1068 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 *
1070 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1071 * including ARBITRATION, SELECTION, and initial message out for
1072 * IDENTIFY and queue messages.
1073 *
1074 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001075 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 *
Finn Thain63238762016-01-03 16:05:15 +11001077 * Returns : -1 if selection failed but should be retried.
1078 * 0 if selection failed and should not be retried.
1079 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *
1081 * Side effects :
1082 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1083 * with registers as they should have been on entry - ie
1084 * SELECT_ENABLE will be set appropriately, the NCR5380
1085 * will cease to drive any SCSI bus signals.
1086 *
1087 * If successful : I_T_L or I_T_L_Q nexus will be established,
1088 * instance->connected will be set to cmd.
1089 * SELECT interrupt will be disabled.
1090 *
1091 * If failed (no target) : cmd->scsi_done() will be called, and the
1092 * cmd->result host byte set to DID_BAD_TARGET.
1093 *
1094 * Locks: caller holds hostdata lock in IRQ mode
1095 */
1096
Finn Thain710ddd02014-11-12 16:12:02 +11001097static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1100 unsigned char tmp[3], phase;
1101 unsigned char *data;
1102 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 int err;
Finn Thain55500d92016-01-03 16:05:35 +11001104 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001107 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /*
1110 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1111 * data bus during SELECTION.
1112 */
1113
1114 NCR5380_write(TARGET_COMMAND_REG, 0);
1115
1116 /*
1117 * Start arbitration.
1118 */
1119
1120 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1121 NCR5380_write(MODE_REG, MR_ARBITRATE);
1122
Finn Thain55500d92016-01-03 16:05:35 +11001123 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1124 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
1126
Finn Thain55500d92016-01-03 16:05:35 +11001127 spin_unlock_irq(instance->host_lock);
1128 timeout = jiffies + HZ;
1129 while (1) {
1130 if (time_is_before_jiffies(timeout)) {
1131 NCR5380_write(MODE_REG, MR_BASE);
1132 shost_printk(KERN_ERR, instance,
1133 "select: arbitration timeout\n");
1134 spin_lock_irq(instance->host_lock);
1135 return -1;
1136 }
1137 spin_lock_irq(instance->host_lock);
1138 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1139 /* Reselection interrupt */
1140 return -1;
1141 }
1142 if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1143 break;
1144 spin_unlock_irq(instance->host_lock);
1145 }
1146
1147 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 udelay(3);
1149
1150 /* Check for lost arbitration */
1151 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)) {
1152 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001153 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001154 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Finn Thaincf13b082016-01-03 16:05:18 +11001156
1157 /* After/during arbitration, BSY should be asserted.
1158 * IBM DPES-31080 Version S31Q works now
1159 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1160 */
1161 NCR5380_write(INITIATOR_COMMAND_REG,
1162 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (!(hostdata->flags & FLAG_DTC3181E) &&
1165 /* RvC: DTC3181E has some trouble with this
1166 * so we simply removed it. Seems to work with
1167 * only Mustek scanner attached
1168 */
1169 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1170 NCR5380_write(MODE_REG, MR_BASE);
1171 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001172 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001173 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 /*
1176 * Again, bus clear + bus settle time is 1.2us, however, this is
1177 * a minimum so we'll udelay ceil(1.2)
1178 */
1179
Finn Thain9c3f0e22016-01-03 16:05:11 +11001180 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1181 udelay(15);
1182 else
1183 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Finn Thain52a6a1c2014-03-18 11:42:18 +11001185 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 /*
1188 * Now that we have won arbitration, start Selection process, asserting
1189 * the host and target ID's on the SCSI bus.
1190 */
1191
Jeff Garzik422c0d62005-10-24 18:05:09 -04001192 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 /*
1195 * Raise ATN while SEL is true before BSY goes false from arbitration,
1196 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1197 * phase immediately after selection.
1198 */
1199
1200 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1201 NCR5380_write(MODE_REG, MR_BASE);
1202
1203 /*
1204 * Reselect interrupts must be turned off prior to the dropping of BSY,
1205 * otherwise we will trigger an interrupt.
1206 */
1207 NCR5380_write(SELECT_ENABLE_REG, 0);
1208
1209 /*
1210 * The initiator shall then wait at least two deskew delays and release
1211 * the BSY signal.
1212 */
1213 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1214
1215 /* Reset BSY */
1216 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1217
1218 /*
1219 * Something weird happens when we cease to drive BSY - looks
1220 * like the board/chip is letting us do another read before the
1221 * appropriate propagation delay has expired, and we're confusing
1222 * a BSY signal from ourselves as the target's response to SELECTION.
1223 *
1224 * A small delay (the 'C++' frontend breaks the pipeline with an
1225 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1226 * tighter 'C' code breaks and requires this) solves the problem -
1227 * the 1 us delay is arbitrary, and only used because this delay will
1228 * be the same on other platforms and since it works here, it should
1229 * work there.
1230 *
1231 * wingel suggests that this could be due to failing to wait
1232 * one deskew delay.
1233 */
1234
1235 udelay(1);
1236
Finn Thain52a6a1c2014-03-18 11:42:18 +11001237 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /*
1240 * The SCSI specification calls for a 250 ms timeout for the actual
1241 * selection.
1242 */
1243
Finn Thainae753a32016-01-03 16:05:23 +11001244 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1245 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1248 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1249 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001250 if (!hostdata->connected)
1251 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -1;
1254 }
Finn Thainae753a32016-01-03 16:05:23 +11001255
1256 if (err < 0) {
1257 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1258 cmd->result = DID_BAD_TARGET << 16;
1259 cmd->scsi_done(cmd);
1260 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1261 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1262 instance->host_no);
1263 return 0;
1264 }
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * No less than two deskew delays after the initiator detects the
1268 * BSY signal is true, it shall release the SEL signal and may
1269 * change the DATA BUS. -wingel
1270 */
1271
1272 udelay(1);
1273
1274 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * Since we followed the SCSI spec, and raised ATN while SEL
1278 * was true but before BSY was false during selection, the information
1279 * transfer phase should be a MESSAGE OUT phase so that we can send the
1280 * IDENTIFY message.
1281 *
1282 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1283 * message (2 bytes) with a tag ID that we increment with every command
1284 * until it wraps back to 0.
1285 *
1286 * XXX - it turns out that there are some broken SCSI-II devices,
1287 * which claim to support tagged queuing but fail when more than
1288 * some number of commands are issued at once.
1289 */
1290
1291 /* Wait for start of REQ/ACK handshake */
1292
1293 spin_unlock_irq(instance->host_lock);
1294 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1295 spin_lock_irq(instance->host_lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001296 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001297 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1298 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001300 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
Finn Thain52a6a1c2014-03-18 11:42:18 +11001303 dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id);
Finn Thain22f5f102014-11-12 16:11:56 +11001304 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 len = 1;
1307 cmd->tag = 0;
1308
1309 /* Send message(s) */
1310 data = tmp;
1311 phase = PHASE_MSGOUT;
1312 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001313 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* XXX need to handle errors here */
1315 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001316 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Boaz Harrosh28424d32007-09-10 22:37:45 +03001318 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323/*
1324 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1325 * unsigned char *phase, int *count, unsigned char **data)
1326 *
1327 * Purpose : transfers data in given phase using polled I/O
1328 *
1329 * Inputs : instance - instance of driver, *phase - pointer to
1330 * what phase is expected, *count - pointer to number of
1331 * bytes to transfer, **data - pointer to data pointer.
1332 *
1333 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001334 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * is in same phase.
1336 *
1337 * Also, *phase, *count, *data are modified in place.
1338 *
1339 * XXX Note : handling for bus free may be useful.
1340 */
1341
1342/*
1343 * Note : this code is not as quick as it could be, however it
1344 * IS 100% reliable, and for the actual data transfer where speed
1345 * counts, we will always do a pseudo DMA or DMA transfer.
1346 */
1347
1348static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 unsigned char p = *phase, tmp;
1350 int c = *count;
1351 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001354 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001356 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /*
1359 * The NCR5380 chip will only drive the SCSI bus when the
1360 * phase specified in the appropriate bits of the TARGET COMMAND
1361 * REGISTER match the STATUS REGISTER
1362 */
1363
1364 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 do {
1367 /*
1368 * Wait for assertion of REQ, after which the phase bits will be
1369 * valid
1370 */
1371
Finn Thain686f3992016-01-03 16:05:26 +11001372 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Finn Thain52a6a1c2014-03-18 11:42:18 +11001375 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001378 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001379 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1381 break;
1382 }
1383 /* Do actual transfer from SCSI bus to / from memory */
1384 if (!(p & SR_IO))
1385 NCR5380_write(OUTPUT_DATA_REG, *d);
1386 else
1387 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1388
1389 ++d;
1390
1391 /*
1392 * The SCSI standard suggests that in MSGOUT phase, the initiator
1393 * should drop ATN on the last byte of the message phase
1394 * after REQ has been asserted for the handshake but before
1395 * the initiator raises ACK.
1396 */
1397
1398 if (!(p & SR_IO)) {
1399 if (!((p & SR_MSG) && c > 1)) {
1400 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1401 NCR5380_dprint(NDEBUG_PIO, instance);
1402 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1403 } else {
1404 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1405 NCR5380_dprint(NDEBUG_PIO, instance);
1406 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1407 }
1408 } else {
1409 NCR5380_dprint(NDEBUG_PIO, instance);
1410 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1411 }
1412
Finn Thaina2edc4a2016-01-03 16:05:27 +11001413 if (NCR5380_poll_politely(instance,
1414 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1415 break;
1416
Finn Thain52a6a1c2014-03-18 11:42:18 +11001417 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419/*
1420 * We have several special cases to consider during REQ/ACK handshaking :
1421 * 1. We were in MSGOUT phase, and we are on the last byte of the
1422 * message. ATN must be dropped as ACK is dropped.
1423 *
1424 * 2. We are in a MSGIN phase, and we are on the last byte of the
1425 * message. We must exit with ACK asserted, so that the calling
1426 * code may raise ATN before dropping ACK to reject the message.
1427 *
1428 * 3. ACK and ATN are clear and the target may proceed as normal.
1429 */
1430 if (!(p == PHASE_MSGIN && c == 1)) {
1431 if (p == PHASE_MSGOUT && c > 1)
1432 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1433 else
1434 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1435 }
1436 } while (--c);
1437
Finn Thain52a6a1c2014-03-18 11:42:18 +11001438 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 *count = c;
1441 *data = d;
1442 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001443 /* The phase read from the bus is valid if either REQ is (already)
1444 * asserted or if ACK hasn't been released yet. The latter applies if
1445 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1446 */
1447 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 *phase = tmp & PHASE_MASK;
1449 else
1450 *phase = PHASE_UNKNOWN;
1451
1452 if (!c || (*phase == p))
1453 return 0;
1454 else
1455 return -1;
1456}
1457
1458/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001459 * do_reset - issue a reset command
1460 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001462 * Issue a reset sequence to the NCR5380 and try and get the bus
1463 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001465 * This clears the reset interrupt flag because there may be no handler for
1466 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1467 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
1469
Finn Thain54d8fe42016-01-03 16:05:06 +11001470static void do_reset(struct Scsi_Host *instance)
1471{
Finn Thain636b1ec2016-01-03 16:05:10 +11001472 unsigned long flags;
1473
1474 local_irq_save(flags);
1475 NCR5380_write(TARGET_COMMAND_REG,
1476 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001478 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001480 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1481 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Finn Thain80d3eb62016-01-03 16:05:34 +11001484/**
1485 * do_abort - abort the currently established nexus by going to
1486 * MESSAGE OUT phase and sending an ABORT message.
1487 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001489 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
1491
Finn Thain54d8fe42016-01-03 16:05:06 +11001492static int do_abort(struct Scsi_Host *instance)
1493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unsigned char *msgptr, phase, tmp;
1495 int len;
1496 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /* Request message out phase */
1499 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1500
1501 /*
1502 * Wait for the target to indicate a valid phase by asserting
1503 * REQ. Once this happens, we'll have either a MSGOUT phase
1504 * and can immediately send the ABORT message, or we'll have some
1505 * other phase and will have to source/sink data.
1506 *
1507 * We really don't care what value was on the bus or what value
1508 * the target sees, so we just handshake.
1509 */
1510
Finn Thain80d3eb62016-01-03 16:05:34 +11001511 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001512 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001513 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Finn Thainf35d3472016-01-03 16:05:33 +11001515 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1518
Finn Thainf35d3472016-01-03 16:05:33 +11001519 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001521 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001522 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001523 goto timeout;
1524 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526 tmp = ABORT;
1527 msgptr = &tmp;
1528 len = 1;
1529 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001530 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 /*
1533 * If we got here, and the command completed successfully,
1534 * we're about to go into bus free state.
1535 */
1536
1537 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001538
1539timeout:
1540 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1541 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1545/*
1546 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1547 * unsigned char *phase, int *count, unsigned char **data)
1548 *
1549 * Purpose : transfers data in given phase using either real
1550 * or pseudo DMA.
1551 *
1552 * Inputs : instance - instance of driver, *phase - pointer to
1553 * what phase is expected, *count - pointer to number of
1554 * bytes to transfer, **data - pointer to data pointer.
1555 *
1556 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001557 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * is in same phase.
1559 *
1560 * Also, *phase, *count, *data are modified in place.
1561 *
1562 * Locks: io_request lock held by caller
1563 */
1564
1565
1566static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 register int c = *count;
1568 register unsigned char p = *phase;
1569 register unsigned char *d = *data;
1570 unsigned char tmp;
1571 int foo;
1572#if defined(REAL_DMA_POLL)
1573 int cnt, toPIO;
1574 unsigned char saved_data = 0, overrun = 0, residue;
1575#endif
1576
1577 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1580 *phase = tmp;
1581 return -1;
1582 }
1583#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1584#ifdef READ_OVERRUNS
1585 if (p & SR_IO) {
1586 c -= 2;
1587 }
1588#endif
Finn Thain52a6a1c2014-03-18 11:42:18 +11001589 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1591#endif
1592
1593 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1594
1595#ifdef REAL_DMA
Finn Thaincd400822016-01-03 16:05:40 +11001596 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1597 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598#elif defined(REAL_DMA_POLL)
Finn Thaincd400822016-01-03 16:05:40 +11001599 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600#else
1601 /*
1602 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1603 * were not disabled for the duration of a single DMA transfer, from
1604 * before the setting of DMA mode to after transfer of the last byte.
1605 */
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (hostdata->flags & FLAG_NCR53C400)
Finn Thaincd400822016-01-03 16:05:40 +11001608 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1609 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 else
Finn Thaincd400822016-01-03 16:05:40 +11001611 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#endif /* def REAL_DMA */
1613
Finn Thain52a6a1c2014-03-18 11:42:18 +11001614 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /*
1617 * On the PAS16 at least I/O recovery delays are not needed here.
1618 * Everyone else seems to want them.
1619 */
1620
1621 if (p & SR_IO) {
1622 io_recovery_delay(1);
1623 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1624 } else {
1625 io_recovery_delay(1);
1626 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1627 io_recovery_delay(1);
1628 NCR5380_write(START_DMA_SEND_REG, 0);
1629 io_recovery_delay(1);
1630 }
1631
1632#if defined(REAL_DMA_POLL)
1633 do {
1634 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1635 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1636
1637/*
1638 At this point, either we've completed DMA, or we have a phase mismatch,
1639 or we've unexpectedly lost BUSY (which is a real error).
1640
1641 For write DMAs, we want to wait until the last byte has been
1642 transferred out over the bus before we turn off DMA mode. Alas, there
1643 seems to be no terribly good way of doing this on a 5380 under all
1644 conditions. For non-scatter-gather operations, we can wait until REQ
1645 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1646 are nastier, since the device will be expecting more data than we
1647 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1648 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1649 why this signal was added to the newer chips) but on the older 538[01]
1650 this signal does not exist. The workaround for this lack is a watchdog;
1651 we bail out of the wait-loop after a modest amount of wait-time if
1652 the usual exit conditions are not met. Not a terribly clean or
1653 correct solution :-%
1654
1655 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1656 If the chip is in DMA mode for an READ, it will respond to a target's
1657 REQ by latching the SCSI data into the INPUT DATA register and asserting
1658 ACK, even if it has _already_ been notified by the DMA controller that
1659 the current DMA transfer has completed! If the NCR5380 is then taken
1660 out of DMA mode, this already-acknowledged byte is lost.
1661
1662 This is not a problem for "one DMA transfer per command" reads, because
1663 the situation will never arise... either all of the data is DMA'ed
1664 properly, or the target switches to MESSAGE IN phase to signal a
1665 disconnection (either operation bringing the DMA to a clean halt).
1666 However, in order to handle scatter-reads, we must work around the
1667 problem. The chosen fix is to DMA N-2 bytes, then check for the
1668 condition before taking the NCR5380 out of DMA mode. One or two extra
1669 bytes are transferred via PIO as necessary to fill out the original
1670 request.
1671 */
1672
1673 if (p & SR_IO) {
1674#ifdef READ_OVERRUNS
1675 udelay(10);
1676 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1677 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1678 overrun = 1;
1679 }
1680#endif
1681 } else {
1682 int limit = 100;
1683 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1684 if (!(tmp & BASR_PHASE_MATCH))
1685 break;
1686 if (--limit < 0)
1687 break;
1688 }
1689 }
1690
Finn Thain52a6a1c2014-03-18 11:42:18 +11001691 dprintk(NDEBUG_DMA, "scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 NCR5380_write(MODE_REG, MR_BASE);
1694 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1695
1696 residue = NCR5380_dma_residual(instance);
1697 c -= residue;
1698 *count -= c;
1699 *data += c;
1700 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1701
1702#ifdef READ_OVERRUNS
1703 if (*phase == p && (p & SR_IO) && residue == 0) {
1704 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001705 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 **data = saved_data;
1707 *data += 1;
1708 *count -= 1;
1709 cnt = toPIO = 1;
1710 } else {
1711 printk("No overrun??\n");
1712 cnt = toPIO = 2;
1713 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001714 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 NCR5380_transfer_pio(instance, phase, &cnt, data);
1716 *count -= toPIO - cnt;
1717 }
1718#endif
1719
Finn Thain52a6a1c2014-03-18 11:42:18 +11001720 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return 0;
1722
1723#elif defined(REAL_DMA)
1724 return 0;
1725#else /* defined(REAL_DMA_POLL) */
1726 if (p & SR_IO) {
1727#ifdef DMA_WORKS_RIGHT
1728 foo = NCR5380_pread(instance, d, c);
1729#else
1730 int diff = 1;
1731 if (hostdata->flags & FLAG_NCR53C400) {
1732 diff = 0;
1733 }
1734 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
1735 /*
1736 * We can't disable DMA mode after successfully transferring
1737 * what we plan to be the last byte, since that would open up
1738 * a race condition where if the target asserted REQ before
1739 * we got the DMA mode reset, the NCR5380 would have latched
1740 * an additional byte into the INPUT DATA register and we'd
1741 * have dropped it.
1742 *
1743 * The workaround was to transfer one fewer bytes than we
1744 * intended to with the pseudo-DMA read function, wait for
1745 * the chip to latch the last byte, read it, and then disable
1746 * pseudo-DMA mode.
1747 *
1748 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1749 * REQ is deasserted when ACK is asserted, and not reasserted
1750 * until ACK goes false. Since the NCR5380 won't lower ACK
1751 * until DACK is asserted, which won't happen unless we twiddle
1752 * the DMA port or we take the NCR5380 out of DMA mode, we
1753 * can guarantee that we won't handshake another extra
1754 * byte.
1755 */
1756
1757 if (!(hostdata->flags & FLAG_NCR53C400)) {
1758 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
1759 /* Wait for clean handshake */
1760 while (NCR5380_read(STATUS_REG) & SR_REQ);
1761 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1762 }
1763 }
1764#endif
1765 } else {
1766#ifdef DMA_WORKS_RIGHT
1767 foo = NCR5380_pwrite(instance, d, c);
1768#else
1769 int timeout;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001770 dprintk(NDEBUG_C400_PWRITE, "About to pwrite %d bytes\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (!(foo = NCR5380_pwrite(instance, d, c))) {
1772 /*
1773 * Wait for the last byte to be sent. If REQ is being asserted for
1774 * the byte we're interested, we'll ACK it and it will go false.
1775 */
1776 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
1777 timeout = 20000;
1778 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
1779
1780 if (!timeout)
Finn Thain52a6a1c2014-03-18 11:42:18 +11001781 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : timed out on last byte\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
1784 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
1785 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
1786 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001787 dprintk(NDEBUG_LAST_BYTE_SENT, "scsi%d : last byte sent works\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 }
1790 } else {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001791 dprintk(NDEBUG_C400_PWRITE, "Waiting for LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
Finn Thain52a6a1c2014-03-18 11:42:18 +11001793 dprintk(NDEBUG_C400_PWRITE, "Got LASTBYTE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795 }
1796#endif
1797 }
1798 NCR5380_write(MODE_REG, MR_BASE);
1799 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001800 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 *data = d + c;
1802 *count = 0;
1803 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return foo;
1805#endif /* def REAL_DMA */
1806}
1807#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1808
1809/*
1810 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1811 *
1812 * Purpose : run through the various SCSI phases and do as the target
1813 * directs us to. Operates on the currently connected command,
1814 * instance->connected.
1815 *
1816 * Inputs : instance, instance for which we are doing commands
1817 *
1818 * Side effects : SCSI things happen, the disconnected queue will be
1819 * modified if a command disconnects, *instance->connected will
1820 * change.
1821 *
1822 * XXX Note : we need to watch for bus free or a reset condition here
1823 * to recover from an unexpected bus free condition.
1824 *
1825 * Locks: io_request_lock held by caller in IRQ mode
1826 */
1827
1828static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1830 unsigned char msgout = NOP;
1831 int sink = 0;
1832 int len;
1833#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1834 int transfersize;
1835#endif
1836 unsigned char *data;
1837 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001838 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 while (1) {
1841 tmp = NCR5380_read(STATUS_REG);
1842 /* We only have a valid SCSI phase when REQ is asserted */
1843 if (tmp & SR_REQ) {
1844 phase = (tmp & PHASE_MASK);
1845 if (phase != old_phase) {
1846 old_phase = phase;
1847 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1848 }
1849 if (sink && (phase != PHASE_MSGOUT)) {
1850 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1851
1852 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1853 while (NCR5380_read(STATUS_REG) & SR_REQ);
1854 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1855 sink = 0;
1856 continue;
1857 }
1858 switch (phase) {
1859 case PHASE_DATAIN:
1860 case PHASE_DATAOUT:
1861#if (NDEBUG & NDEBUG_NO_DATAOUT)
1862 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1863 sink = 1;
1864 do_abort(instance);
1865 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001866 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return;
1868#endif
1869 /*
1870 * If there is no room left in the current buffer in the
1871 * scatter-gather list, move onto the next one.
1872 */
1873
1874 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1875 ++cmd->SCp.buffer;
1876 --cmd->SCp.buffers_residual;
1877 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001878 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001879 dprintk(NDEBUG_INFORMATION, "scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881 /*
1882 * The preferred transfer method is going to be
1883 * PSEUDO-DMA for systems that are strictly PIO,
1884 * since we can let the hardware do the handshaking.
1885 *
1886 * For this to work, we need to know the transfersize
1887 * ahead of time, since the pseudo-DMA code will sit
1888 * in an unconditional loop.
1889 */
1890
1891#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001892 transfersize = 0;
1893 if (!cmd->device->borken &&
1894 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1895 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Finn Thainff3d4572016-01-03 16:05:25 +11001897 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 len = transfersize;
1899 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1900 /*
1901 * If the watchdog timer fires, all future accesses to this
1902 * device will use the polled-IO.
1903 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001904 scmd_printk(KERN_INFO, cmd,
1905 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 sink = 1;
1908 do_abort(instance);
1909 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001910 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /* XXX - need to source or sink data here, as appropriate */
1912 } else
1913 cmd->SCp.this_residual -= transfersize - len;
1914 } else
1915#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
1916 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1917 &cmd->SCp.ptr);
1918 break;
1919 case PHASE_MSGIN:
1920 len = 1;
1921 data = &tmp;
1922 NCR5380_transfer_pio(instance, &phase, &len, &data);
1923 cmd->SCp.Message = tmp;
1924
1925 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 case ABORT:
1927 case COMMAND_COMPLETE:
1928 /* Accept message by clearing ACK */
1929 sink = 1;
1930 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1931 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001932 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
1933 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /*
1936 * I'm not sure what the correct thing to do here is :
1937 *
1938 * If the command that just executed is NOT a request
1939 * sense, the obvious thing to do is to set the result
1940 * code to the values of the stored parameters.
1941 *
1942 * If it was a REQUEST SENSE command, we need some way
1943 * to differentiate between the failure code of the original
1944 * and the failure code of the REQUEST sense - the obvious
1945 * case is success, where we fall through and leave the result
1946 * code unchanged.
1947 *
1948 * The non-obvious place is where the REQUEST SENSE failed
1949 */
1950
1951 if (cmd->cmnd[0] != REQUEST_SENSE)
1952 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1953 else if (status_byte(cmd->SCp.Status) != GOOD)
1954 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1955
Boaz Harrosh28424d32007-09-10 22:37:45 +03001956 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1957 hostdata->ses.cmd_len) {
1958 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1959 hostdata->ses.cmd_len = 0 ;
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Boaz Harrosh28424d32007-09-10 22:37:45 +03001962 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
1963 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1964
Finn Thain52a6a1c2014-03-18 11:42:18 +11001965 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 LIST(cmd, hostdata->issue_queue);
1968 cmd->host_scribble = (unsigned char *)
1969 hostdata->issue_queue;
Finn Thain710ddd02014-11-12 16:12:02 +11001970 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001971 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
Finn Thain997acab2014-11-12 16:11:54 +11001972 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 cmd->scsi_done(cmd);
1974 }
1975
1976 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1977 /*
1978 * Restore phase bits to 0 so an interrupted selection,
1979 * arbitration can resume.
1980 */
1981 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return;
1983 case MESSAGE_REJECT:
1984 /* Accept message by clearing ACK */
1985 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1986 switch (hostdata->last_message) {
1987 case HEAD_OF_QUEUE_TAG:
1988 case ORDERED_QUEUE_TAG:
1989 case SIMPLE_QUEUE_TAG:
1990 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001991 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 break;
1993 default:
1994 break;
1995 }
Finn Thain340b9612016-01-03 16:05:31 +11001996 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 case DISCONNECT:{
1998 /* Accept message by clearing ACK */
1999 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 LIST(cmd, hostdata->disconnected_queue);
2001 cmd->host_scribble = (unsigned char *)
2002 hostdata->disconnected_queue;
2003 hostdata->connected = NULL;
2004 hostdata->disconnected_queue = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002005 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /*
2007 * Restore phase bits to 0 so an interrupted selection,
2008 * arbitration can resume.
2009 */
2010 NCR5380_write(TARGET_COMMAND_REG, 0);
2011
2012 /* Enable reselect interrupts */
2013 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return;
2015 }
2016 /*
2017 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2018 * operation, in violation of the SCSI spec so we can safely
2019 * ignore SAVE/RESTORE pointers calls.
2020 *
2021 * Unfortunately, some disks violate the SCSI spec and
2022 * don't issue the required SAVE_POINTERS message before
2023 * disconnecting, and we have to break spec to remain
2024 * compatible.
2025 */
2026 case SAVE_POINTERS:
2027 case RESTORE_POINTERS:
2028 /* Accept message by clearing ACK */
2029 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2030 break;
2031 case EXTENDED_MESSAGE:
2032/*
2033 * Extended messages are sent in the following format :
2034 * Byte
2035 * 0 EXTENDED_MESSAGE == 1
2036 * 1 length (includes one byte for code, doesn't
2037 * include first two bytes)
2038 * 2 code
2039 * 3..length+1 arguments
2040 *
2041 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002042 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 */
2044 extended_msg[0] = EXTENDED_MESSAGE;
2045 /* Accept first byte by clearing ACK */
2046 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002047 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 len = 2;
2050 data = extended_msg + 1;
2051 phase = PHASE_MSGIN;
2052 NCR5380_transfer_pio(instance, &phase, &len, &data);
2053
Finn Thain52a6a1c2014-03-18 11:42:18 +11002054 dprintk(NDEBUG_EXTENDED, "scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2057 /* Accept third byte by clearing ACK */
2058 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2059 len = extended_msg[1] - 1;
2060 data = extended_msg + 3;
2061 phase = PHASE_MSGIN;
2062
2063 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002064 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 switch (extended_msg[2]) {
2067 case EXTENDED_SDTR:
2068 case EXTENDED_WDTR:
2069 case EXTENDED_MODIFY_DATA_POINTER:
2070 case EXTENDED_EXTENDED_IDENTIFY:
2071 tmp = 0;
2072 }
2073 } else if (len) {
2074 printk("scsi%d: error receiving extended message\n", instance->host_no);
2075 tmp = 0;
2076 } else {
2077 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2078 tmp = 0;
2079 }
2080 /* Fall through to reject message */
2081
2082 /*
2083 * If we get something weird that we aren't expecting,
2084 * reject it.
2085 */
2086 default:
2087 if (!tmp) {
2088 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002089 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 printk("\n");
2091 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002092 scmd_printk(KERN_INFO, cmd,
2093 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002095 scmd_printk(KERN_INFO, cmd,
2096 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 msgout = MESSAGE_REJECT;
2099 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2100 break;
2101 } /* switch (tmp) */
2102 break;
2103 case PHASE_MSGOUT:
2104 len = 1;
2105 data = &msgout;
2106 hostdata->last_message = msgout;
2107 NCR5380_transfer_pio(instance, &phase, &len, &data);
2108 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002109 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 hostdata->connected = NULL;
2111 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 cmd->scsi_done(cmd);
2113 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2114 return;
2115 }
2116 msgout = NOP;
2117 break;
2118 case PHASE_CMDOUT:
2119 len = cmd->cmd_len;
2120 data = cmd->cmnd;
2121 /*
2122 * XXX for performance reasons, on machines with a
2123 * PSEUDO-DMA architecture we should probably
2124 * use the dma transfer function.
2125 */
2126 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 break;
2128 case PHASE_STATIN:
2129 len = 1;
2130 data = &tmp;
2131 NCR5380_transfer_pio(instance, &phase, &len, &data);
2132 cmd->SCp.Status = tmp;
2133 break;
2134 default:
2135 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002136 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002138 } else {
2139 spin_unlock_irq(instance->host_lock);
2140 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2141 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 } /* while (1) */
2144}
2145
2146/*
2147 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2148 *
2149 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002150 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 * nexus has been reestablished,
2152 *
2153 * Inputs : instance - this instance of the NCR5380.
2154 *
2155 * Locks: io_request_lock held by caller if IRQ driven
2156 */
2157
2158static void NCR5380_reselect(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2160 instance->hostdata;
2161 unsigned char target_mask;
2162 unsigned char lun, phase;
2163 int len;
2164 unsigned char msg[3];
2165 unsigned char *data;
Finn Thain710ddd02014-11-12 16:12:02 +11002166 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int abort = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 /*
2170 * Disable arbitration, etc. since the host adapter obviously
2171 * lost, and tell an interrupted NCR5380_select() to restart.
2172 */
2173
2174 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002177 dprintk(NDEBUG_SELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 /*
2180 * At this point, we have detected that our SCSI ID is on the bus,
2181 * SEL is true and BSY was false for at least one bus settle delay
2182 * (400 ns).
2183 *
2184 * We must assert BSY ourselves, until the target drops the SEL
2185 * signal.
2186 */
2187
2188 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2189
2190 /* FIXME: timeout too long, must fail to workqueue */
2191 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2192 abort = 1;
2193
2194 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2195
2196 /*
2197 * Wait for target to go into MSGIN.
2198 * FIXME: timeout needed and fail to work queeu
2199 */
2200
Finn Thain1cc160e2016-01-03 16:05:32 +11002201 if (NCR5380_poll_politely(instance,
2202 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 abort = 1;
2204
2205 len = 1;
2206 data = msg;
2207 phase = PHASE_MSGIN;
2208 NCR5380_transfer_pio(instance, &phase, &len, &data);
2209
2210 if (!(msg[0] & 0x80)) {
2211 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002212 spi_print_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 abort = 1;
2214 } else {
2215 /* Accept message by clearing ACK */
2216 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2217 lun = (msg[0] & 0x07);
2218
2219 /*
2220 * We need to add code for SCSI-II to track which devices have
2221 * I_T_L_Q nexuses established, and which have simple I_T_L
2222 * nexuses so we can chose to do additional data transfer.
2223 */
2224
2225 /*
2226 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2227 * just reestablished, and remove it from the disconnected queue.
2228 */
2229
2230
Finn Thain710ddd02014-11-12 16:12:02 +11002231 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002232 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 ) {
2234 if (prev) {
2235 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2236 prev->host_scribble = tmp->host_scribble;
2237 } else {
2238 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002239 hostdata->disconnected_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241 tmp->host_scribble = NULL;
2242 break;
2243 }
2244 if (!tmp) {
2245 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2246 /*
2247 * Since we have an established nexus that we can't do anything with,
2248 * we must abort it.
2249 */
2250 abort = 1;
2251 }
2252 }
2253
2254 if (abort) {
2255 do_abort(instance);
2256 } else {
2257 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002258 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260}
2261
2262/*
2263 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2264 *
2265 * Purpose : called by interrupt handler when DMA finishes or a phase
2266 * mismatch occurs (which would finish the DMA transfer).
2267 *
2268 * Inputs : instance - this instance of the NCR5380.
2269 *
Finn Thain710ddd02014-11-12 16:12:02 +11002270 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 * nexus has been reestablished, on failure NULL is returned.
2272 */
2273
2274#ifdef REAL_DMA
2275static void NCR5380_dma_complete(NCR5380_instance * instance) {
Yoann Padioleauf8343682007-06-01 00:46:36 -07002276 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 /*
2280 * XXX this might not be right.
2281 *
2282 * Wait for final byte to transfer, ie wait for ACK to go false.
2283 *
2284 * We should use the Last Byte Sent bit, unfortunately this is
2285 * not available on the 5380/5381 (only the various CMOS chips)
2286 *
2287 * FIXME: timeout, and need to handle long timeout/irq case
2288 */
2289
2290 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2293
2294 /*
2295 * The only places we should see a phase mismatch and have to send
2296 * data from the same set of pointers will be the data transfer
2297 * phases. So, residual, requested length are only important here.
2298 */
2299
2300 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2301 transferred = instance->dmalen - NCR5380_dma_residual();
2302 hostdata->connected->SCp.this_residual -= transferred;
2303 hostdata->connected->SCp.ptr += transferred;
2304 }
2305}
2306#endif /* def REAL_DMA */
2307
2308/*
Finn Thain710ddd02014-11-12 16:12:02 +11002309 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 *
2311 * Purpose : abort a command
2312 *
Finn Thain710ddd02014-11-12 16:12:02 +11002313 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002314 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 * used.
2316 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002317 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002319 * XXX - there is no way to abort the command that is currently
2320 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 * a problem, we could implement longjmp() / setjmp(), setjmp()
2322 * called where the loop started in NCR5380_main().
2323 *
2324 * Locks: host lock taken by caller
2325 */
2326
Finn Thain710ddd02014-11-12 16:12:02 +11002327static int NCR5380_abort(struct scsi_cmnd *cmd)
2328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct Scsi_Host *instance = cmd->device->host;
2330 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +11002331 struct scsi_cmnd *tmp, **prev;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002332
2333 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Finn Thain9dafbd92016-01-03 16:05:28 +11002335 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 NCR5380_print_status(instance);
2337
Finn Thain52a6a1c2014-03-18 11:42:18 +11002338 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2339 dprintk(NDEBUG_ABORT, " basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341#if 0
2342/*
2343 * Case 1 : If the command is the currently executing command,
2344 * we'll set the aborted flag and return control so that
2345 * information transfer routine can exit cleanly.
2346 */
2347
2348 if (hostdata->connected == cmd) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002349 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350/*
2351 * We should perform BSY checking, and make sure we haven't slipped
2352 * into BUS FREE.
2353 */
2354
2355 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2356/*
2357 * Since we can't change phases until we've completed the current
2358 * handshake, we have to source or sink a byte of data if the current
2359 * phase is not MSGOUT.
2360 */
2361
2362/*
2363 * Return control to the executing NCR drive so we can clear the
2364 * aborted flag and get back into our main loop.
2365 */
2366
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002367 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369#endif
2370
2371/*
2372 * Case 2 : If the command hasn't been issued yet, we simply remove it
2373 * from the issue queue.
2374 */
2375
Finn Thain52a6a1c2014-03-18 11:42:18 +11002376 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +11002377 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (cmd == tmp) {
2379 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002380 (*prev) = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002382 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 tmp->result = DID_ABORT << 16;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002384 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002385 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return SUCCESS;
2387 }
2388#if (NDEBUG & NDEBUG_ABORT)
2389 /* KLL */
2390 else if (prev == tmp)
2391 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2392#endif
2393
2394/*
2395 * Case 3 : If any commands are connected, we're going to fail the abort
2396 * and let the high level SCSI driver retry at a later time or
2397 * issue a reset.
2398 *
2399 * Timeouts, and therefore aborted commands, will be highly unlikely
2400 * and handling them cleanly in this situation would make the common
2401 * case of noresets less efficient, and would pollute our code. So,
2402 * we fail.
2403 */
2404
2405 if (hostdata->connected) {
Finn Thain9dafbd92016-01-03 16:05:28 +11002406 spin_unlock_irq(instance->host_lock);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002407 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return FAILED;
2409 }
2410/*
2411 * Case 4: If the command is currently disconnected from the bus, and
2412 * there are no connected commands, we reconnect the I_T_L or
2413 * I_T_L_Q nexus associated with it, go into message out, and send
2414 * an abort message.
2415 *
2416 * This case is especially ugly. In order to reestablish the nexus, we
2417 * need to call NCR5380_select(). The easiest way to implement this
2418 * function was to abort if the bus was busy, and let the interrupt
2419 * handler triggered on the SEL for reselect take care of lost arbitrations
2420 * where necessary, meaning interrupts need to be enabled.
2421 *
2422 * When interrupts are enabled, the queues may change - so we
2423 * can't remove it from the disconnected queue before selecting it
2424 * because that could cause a failure in hashing the nexus if that
2425 * device reselected.
2426 *
2427 * Since the queues may change, we can't use the pointers from when we
2428 * first locate it.
2429 *
2430 * So, we must first locate the command, and if NCR5380_select()
2431 * succeeds, then issue the abort, relocate the command and remove
2432 * it from the disconnected queue.
2433 */
2434
Finn Thain710ddd02014-11-12 16:12:02 +11002435 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (cmd == tmp) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002437 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Finn Thain9dafbd92016-01-03 16:05:28 +11002439 if (NCR5380_select(instance, cmd)) {
2440 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return FAILED;
Finn Thain9dafbd92016-01-03 16:05:28 +11002442 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11002443 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 do_abort(instance);
2446
Finn Thain710ddd02014-11-12 16:12:02 +11002447 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (cmd == tmp) {
2449 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002450 *prev = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002452 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 tmp->result = DID_ABORT << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002454 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return SUCCESS;
2456 }
2457 }
2458/*
2459 * Case 5 : If we reached this point, the command was not found in any of
2460 * the queues.
2461 *
2462 * We probably reached this point because of an unlikely race condition
2463 * between the command completing successfully and the abortion code,
2464 * so we won't panic, but we will notify the user in case something really
2465 * broke.
2466 */
Finn Thain9dafbd92016-01-03 16:05:28 +11002467 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2469 " before abortion\n", instance->host_no);
2470 return FAILED;
2471}
2472
2473
Finn Thain3be1b3e2016-01-03 16:05:12 +11002474/**
2475 * NCR5380_bus_reset - reset the SCSI bus
2476 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002478 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 */
2480
Finn Thain710ddd02014-11-12 16:12:02 +11002481static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002482{
2483 struct Scsi_Host *instance = cmd->device->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002485 spin_lock_irq(instance->host_lock);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002486
2487#if (NDEBUG & NDEBUG_ANY)
2488 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2489 NCR5380_print_status(instance);
2490#endif
2491
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002492 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002493
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002494 spin_unlock_irq(instance->host_lock);
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return SUCCESS;
2497}