blob: 29cf1ec27ff23a98ccc474b6c16a3f0c7c4aaac8 [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 Thainb32ade12016-01-03 16:05:41 +1100265 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100266 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100267 * @reg1: 5380 register to poll
268 * @bit1: Bitmask to check
269 * @val1: Expected value
270 * @reg2: Second 5380 register to poll
271 * @bit2: Second bitmask to check
272 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100273 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 *
Finn Thain2f854b82016-01-03 16:05:22 +1100275 * Polls the chip in a reasonably efficient manner waiting for an
276 * event to occur. After a short quick poll we begin to yield the CPU
277 * (if possible). In irq contexts the time-out is arbitrarily limited.
278 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
Finn Thainb32ade12016-01-03 16:05:41 +1100280 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Finn Thainb32ade12016-01-03 16:05:41 +1100283static int NCR5380_poll_politely2(struct Scsi_Host *instance,
284 int reg1, int bit1, int val1,
285 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100286{
287 struct NCR5380_hostdata *hostdata = shost_priv(instance);
288 unsigned long deadline = jiffies + wait;
289 unsigned long n;
290
291 /* Busy-wait for up to 10 ms */
292 n = min(10000U, jiffies_to_usecs(wait));
293 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100294 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100295 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100296 if ((NCR5380_read(reg1) & bit1) == val1)
297 return 0;
298 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100301 } while (n--);
302
303 if (irqs_disabled() || in_interrupt())
304 return -ETIMEDOUT;
305
306 /* Repeatedly sleep for 1 ms until deadline */
307 while (time_is_after_jiffies(deadline)) {
308 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100309 if ((NCR5380_read(reg1) & bit1) == val1)
310 return 0;
311 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Finn Thain2f854b82016-01-03 16:05:22 +1100314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -ETIMEDOUT;
316}
317
Finn Thainb32ade12016-01-03 16:05:41 +1100318static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
319 int reg, int bit, int val, int wait)
320{
321 return NCR5380_poll_politely2(instance, reg, bit, val,
322 reg, bit, val, wait);
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static struct {
326 unsigned char value;
327 const char *name;
Andrew Morton702809c2007-05-23 14:41:56 -0700328} phases[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 {PHASE_DATAOUT, "DATAOUT"},
330 {PHASE_DATAIN, "DATAIN"},
331 {PHASE_CMDOUT, "CMDOUT"},
332 {PHASE_STATIN, "STATIN"},
333 {PHASE_MSGOUT, "MSGOUT"},
334 {PHASE_MSGIN, "MSGIN"},
335 {PHASE_UNKNOWN, "UNKNOWN"}
336};
337
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100338#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static struct {
340 unsigned char mask;
341 const char *name;
342} signals[] = {
343 {SR_DBP, "PARITY"},
344 {SR_RST, "RST"},
345 {SR_BSY, "BSY"},
346 {SR_REQ, "REQ"},
347 {SR_MSG, "MSG"},
348 {SR_CD, "CD"},
349 {SR_IO, "IO"},
350 {SR_SEL, "SEL"},
351 {0, NULL}
352},
353basrs[] = {
354 {BASR_ATN, "ATN"},
355 {BASR_ACK, "ACK"},
356 {0, NULL}
357},
358icrs[] = {
359 {ICR_ASSERT_RST, "ASSERT RST"},
360 {ICR_ASSERT_ACK, "ASSERT ACK"},
361 {ICR_ASSERT_BSY, "ASSERT BSY"},
362 {ICR_ASSERT_SEL, "ASSERT SEL"},
363 {ICR_ASSERT_ATN, "ASSERT ATN"},
364 {ICR_ASSERT_DATA, "ASSERT DATA"},
365 {0, NULL}
366},
367mrs[] = {
368 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
369 {MR_TARGET, "MODE TARGET"},
370 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
371 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
372 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
373 {MR_DMA_MODE, "MODE DMA"},
374 {MR_ARBITRATE, "MODE ARBITRATION"},
375 {0, NULL}
376};
377
378/**
379 * NCR5380_print - print scsi bus signals
380 * @instance: adapter state to dump
381 *
382 * Print the SCSI bus signals for debugging purposes
383 *
384 * Locks: caller holds hostdata lock (not essential)
385 */
386
387static void NCR5380_print(struct Scsi_Host *instance)
388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
392 status = NCR5380_read(STATUS_REG);
393 mr = NCR5380_read(MODE_REG);
394 icr = NCR5380_read(INITIATOR_COMMAND_REG);
395 basr = NCR5380_read(BUS_AND_STATUS_REG);
396
397 printk("STATUS_REG: %02x ", status);
398 for (i = 0; signals[i].mask; ++i)
399 if (status & signals[i].mask)
400 printk(",%s", signals[i].name);
401 printk("\nBASR: %02x ", basr);
402 for (i = 0; basrs[i].mask; ++i)
403 if (basr & basrs[i].mask)
404 printk(",%s", basrs[i].name);
405 printk("\nICR: %02x ", icr);
406 for (i = 0; icrs[i].mask; ++i)
407 if (icr & icrs[i].mask)
408 printk(",%s", icrs[i].name);
409 printk("\nMODE: %02x ", mr);
410 for (i = 0; mrs[i].mask; ++i)
411 if (mr & mrs[i].mask)
412 printk(",%s", mrs[i].name);
413 printk("\n");
414}
415
416
417/*
418 * NCR5380_print_phase - show SCSI phase
419 * @instance: adapter to dump
420 *
421 * Print the current SCSI phase for debugging purposes
422 *
423 * Locks: none
424 */
425
426static void NCR5380_print_phase(struct Scsi_Host *instance)
427{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 unsigned char status;
429 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 status = NCR5380_read(STATUS_REG);
432 if (!(status & SR_REQ))
433 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
434 else {
435 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
436 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
437 }
438}
439#endif
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Finn Thaind5f7e652016-01-03 16:05:03 +1100442static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444/**
445 * probe_intr - helper for IRQ autoprobe
446 * @irq: interrupt number
447 * @dev_id: unused
448 * @regs: unused
449 *
450 * Set a flag to indicate the IRQ in question was received. This is
451 * used by the IRQ probe code.
452 */
453
David Howells7d12e782006-10-05 14:55:46 +0100454static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 probe_irq = irq;
457 return IRQ_HANDLED;
458}
459
460/**
461 * NCR5380_probe_irq - find the IRQ of an NCR5380
462 * @instance: NCR5380 controller
463 * @possible: bitmask of ISA IRQ lines
464 *
465 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
466 * and then looking to see what interrupt actually turned up.
467 *
468 * Locks: none, irqs must be enabled on entry
469 */
470
Andrew Morton702809c2007-05-23 14:41:56 -0700471static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
472 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
475 unsigned long timeout;
476 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Finn Thain22f5f102014-11-12 16:11:56 +1100478 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100479 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 trying_irqs |= mask;
481
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500482 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100483 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /*
486 * A interrupt is triggered whenever BSY = false, SEL = true
487 * and a bit set in the SELECT_ENABLE_REG is asserted on the
488 * SCSI bus.
489 *
490 * Note that the bus is only driven when the phase control signals
491 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
492 * to zero.
493 */
494
495 NCR5380_write(TARGET_COMMAND_REG, 0);
496 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
497 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
498 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
499
Finn Thain22f5f102014-11-12 16:11:56 +1100500 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800501 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 NCR5380_write(SELECT_ENABLE_REG, 0);
504 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
505
Finn Thain22f5f102014-11-12 16:11:56 +1100506 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (trying_irqs & mask)
508 free_irq(i, NULL);
509
510 return probe_irq;
511}
512
513/**
Finn Thain8c325132014-11-12 16:11:58 +1100514 * NCR58380_info - report driver and host information
515 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
Finn Thain8c325132014-11-12 16:11:58 +1100517 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *
519 * Locks: none
520 */
521
Finn Thain8c325132014-11-12 16:11:58 +1100522static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Finn Thain8c325132014-11-12 16:11:58 +1100524 struct NCR5380_hostdata *hostdata = shost_priv(instance);
525
526 return hostdata->info;
527}
528
529static void prepare_info(struct Scsi_Host *instance)
530{
531 struct NCR5380_hostdata *hostdata = shost_priv(instance);
532
533 snprintf(hostdata->info, sizeof(hostdata->info),
534 "%s, io_port 0x%lx, n_io_port %d, "
535 "base 0x%lx, irq %d, "
536 "can_queue %d, cmd_per_lun %d, "
537 "sg_tablesize %d, this_id %d, "
Finn Thain9c3f0e22016-01-03 16:05:11 +1100538 "flags { %s%s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100539 "options { %s} ",
540 instance->hostt->name, instance->io_port, instance->n_io_port,
541 instance->base, instance->irq,
542 instance->can_queue, instance->cmd_per_lun,
543 instance->sg_tablesize, instance->this_id,
Finn Thain55181be2016-01-03 16:05:42 +1100544 hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100545 hostdata->flags & FLAG_DTC3181E ? "DTC3181E " : "",
546 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100547 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100549 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100552 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#endif
554#ifdef REAL_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100555 "REAL_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#endif
557#ifdef REAL_DMA_POLL
Finn Thain8c325132014-11-12 16:11:58 +1100558 "REAL_DMA_POLL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#endif
560#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100561 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#endif
563#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100564 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#endif
Finn Thain8c325132014-11-12 16:11:58 +1100566 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/**
570 * NCR5380_print_status - dump controller info
571 * @instance: controller to dump
572 *
573 * Print commands in the various queues, called from NCR5380_abort
574 * and NCR5380_debug to aid debugging.
575 *
576 * Locks: called functions disable irqs
577 */
578
Finn Thain3be1b3e2016-01-03 16:05:12 +1100579static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 NCR5380_dprint(NDEBUG_ANY, instance);
582 NCR5380_dprint_phase(NDEBUG_ANY, instance);
583}
584
Finn Thaina9c2dc42014-11-12 16:11:59 +1100585#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/******************************************/
587/*
588 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
589 *
590 * *buffer: I/O buffer
591 * **start: if inout == FALSE pointer into buffer where user read should start
592 * offset: current offset
593 * length: length of buffer
594 * hostno: Scsi_Host host_no
595 * inout: TRUE - user is writing; FALSE - user is reading
596 *
597 * Return the number of bytes read from or written
598 */
599
Al Virodd7ab712013-03-31 01:15:54 -0400600static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
601 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100603 struct NCR5380_hostdata *hostdata = shost_priv(instance);
604
605 hostdata->spin_max_r = 0;
606 hostdata->spin_max_w = 0;
607 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400608}
Finn Thaina9c2dc42014-11-12 16:11:59 +1100609#endif
Al Virodd7ab712013-03-31 01:15:54 -0400610
Al Virodd7ab712013-03-31 01:15:54 -0400611static
Finn Thain710ddd02014-11-12 16:12:02 +1100612void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m);
Al Virodd7ab712013-03-31 01:15:54 -0400613static
614void lprint_command(unsigned char *cmd, struct seq_file *m);
615static
616void lprint_opcode(int opcode, struct seq_file *m);
617
618static int __maybe_unused NCR5380_show_info(struct seq_file *m,
619 struct Scsi_Host *instance)
620{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct NCR5380_hostdata *hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +1100622 struct scsi_cmnd *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
625
Finn Thaina9c2dc42014-11-12 16:11:59 +1100626#ifdef PSEUDO_DMA
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100627 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100628 hostdata->spin_max_w, hostdata->spin_max_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#endif
630 spin_lock_irq(instance->host_lock);
631 if (!hostdata->connected)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100632 seq_printf(m, "scsi%d: no currently connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 else
Finn Thain710ddd02014-11-12 16:12:02 +1100634 lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m);
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100635 seq_printf(m, "scsi%d: issue_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100636 for (ptr = (struct scsi_cmnd *) hostdata->issue_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400637 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100639 seq_printf(m, "scsi%d: disconnected_queue\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +1100640 for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400641 lprint_Scsi_Cmnd(ptr, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 spin_unlock_irq(instance->host_lock);
Al Virodd7ab712013-03-31 01:15:54 -0400643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Finn Thain710ddd02014-11-12 16:12:02 +1100646static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100648 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 +0100649 seq_puts(m, " command = ");
Al Virodd7ab712013-03-31 01:15:54 -0400650 lprint_command(cmd->cmnd, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Al Virodd7ab712013-03-31 01:15:54 -0400653static void lprint_command(unsigned char *command, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 int i, s;
Al Virodd7ab712013-03-31 01:15:54 -0400656 lprint_opcode(command[0], m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100658 seq_printf(m, "%02x ", command[i]);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +0100659 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Al Virodd7ab712013-03-31 01:15:54 -0400662static void lprint_opcode(int opcode, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100664 seq_printf(m, "%2d (0x%02x)", opcode, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667
668/**
669 * NCR5380_init - initialise an NCR5380
670 * @instance: adapter to configure
671 * @flags: control flags
672 *
673 * Initializes *instance and corresponding 5380 chip,
674 * with flags OR'd into the initial flags value.
675 *
676 * Notes : I assume that the host, hostno, and id bits have been
677 * set correctly. I don't care about the irq and other fields.
678 *
679 * Returns 0 for success
680 *
681 * Locks: interrupts must be enabled when we are called
682 */
683
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800684static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Finn Thainb6488f92016-01-03 16:05:08 +1100686 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain2f854b82016-01-03 16:05:22 +1100688 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if(in_interrupt())
691 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 hostdata->id_mask = 1 << instance->this_id;
694 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
695 if (i > hostdata->id_mask)
696 hostdata->id_higher_mask |= i;
697 for (i = 0; i < 8; ++i)
698 hostdata->busy[i] = 0;
699#ifdef REAL_DMA
700 hostdata->dmalen = 0;
701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 hostdata->connected = NULL;
703 hostdata->issue_queue = NULL;
704 hostdata->disconnected_queue = NULL;
Finn Thain55181be2016-01-03 16:05:42 +1100705 hostdata->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Finn Thain8d8601a2016-01-03 16:05:37 +1100707 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100708 hostdata->work_q = alloc_workqueue("ncr5380_%d",
709 WQ_UNBOUND | WQ_MEM_RECLAIM,
710 1, instance->host_no);
711 if (!hostdata->work_q)
712 return -ENOMEM;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Finn Thain8c325132014-11-12 16:11:58 +1100716 prepare_info(instance);
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
719 NCR5380_write(MODE_REG, MR_BASE);
720 NCR5380_write(TARGET_COMMAND_REG, 0);
721 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100722
723 /* Calibrate register polling loop */
724 i = 0;
725 deadline = jiffies + 1;
726 do {
727 cpu_relax();
728 } while (time_is_after_jiffies(deadline));
729 deadline += msecs_to_jiffies(256);
730 do {
731 NCR5380_read(STATUS_REG);
732 ++i;
733 cpu_relax();
734 } while (time_is_after_jiffies(deadline));
735 hostdata->accesses_per_ms = i / 256;
736
Finn Thainb6488f92016-01-03 16:05:08 +1100737 return 0;
738}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Finn Thainb6488f92016-01-03 16:05:08 +1100740/**
741 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
742 * @instance: adapter to check
743 *
744 * If the system crashed, it may have crashed with a connected target and
745 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
746 * currently established nexus, which we know nothing about. Failing that
747 * do a bus reset.
748 *
749 * Note that a bus reset will cause the chip to assert IRQ.
750 *
751 * Returns 0 if successful, otherwise -ENXIO.
752 */
753
754static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
755{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100756 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100757 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
760 switch (pass) {
761 case 1:
762 case 3:
763 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100764 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
765 NCR5380_poll_politely(instance,
766 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 break;
768 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100769 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 do_abort(instance);
771 break;
772 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100773 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100775 /* Wait after a reset; the SCSI standard calls for
776 * 250ms, we wait 500ms to be on the safe side.
777 * But some Toshiba CD-ROMs need ten times that.
778 */
779 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
780 msleep(2500);
781 else
782 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
784 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100785 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -ENXIO;
787 }
788 }
789 return 0;
790}
791
792/**
793 * NCR5380_exit - remove an NCR5380
794 * @instance: adapter to remove
795 */
796
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800797static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
800
Finn Thain8d8601a2016-01-03 16:05:37 +1100801 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100802 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805/**
Finn Thain1bb40582016-01-03 16:05:29 +1100806 * NCR5380_queue_command - queue a command
807 * @instance: the relevant SCSI adapter
808 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
Finn Thain1bb40582016-01-03 16:05:29 +1100810 * cmd is added to the per-instance issue queue, with minor
811 * twiddling done to the host specific fields of cmd. If the
812 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
814
Finn Thain1bb40582016-01-03 16:05:29 +1100815static int NCR5380_queue_command(struct Scsi_Host *instance,
816 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Finn Thain1bb40582016-01-03 16:05:29 +1100818 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain710ddd02014-11-12 16:12:02 +1100819 struct scsi_cmnd *tmp;
Finn Thain1bb40582016-01-03 16:05:29 +1100820 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822#if (NDEBUG & NDEBUG_NO_WRITE)
823 switch (cmd->cmnd[0]) {
824 case WRITE_6:
825 case WRITE_10:
826 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
827 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100828 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830 }
831#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * We use the host_scribble field as a pointer to the next command
835 * in a queue
836 */
837
838 cmd->host_scribble = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 cmd->result = 0;
840
Finn Thain1bb40582016-01-03 16:05:29 +1100841 spin_lock_irqsave(instance->host_lock, flags);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /*
844 * Insert the cmd into the issue queue. Note that REQUEST SENSE
845 * commands are added to the head of the queue since any command will
846 * clear the contingent allegiance condition that exists and the
847 * sense data is only guaranteed to be valid while the condition exists.
848 */
849
850 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
851 LIST(cmd, hostdata->issue_queue);
852 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
853 hostdata->issue_queue = cmd;
854 } else {
Finn Thain710ddd02014-11-12 16:12:02 +1100855 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 -0700856 LIST(cmd, tmp);
857 tmp->host_scribble = (unsigned char *) cmd;
858 }
Finn Thain1bb40582016-01-03 16:05:29 +1100859 spin_unlock_irqrestore(instance->host_lock, flags);
860
Finn Thain52a6a1c2014-03-18 11:42:18 +1100861 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 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100864 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/**
869 * NCR5380_main - NCR state machines
870 *
871 * NCR5380_main is a coroutine that runs as long as more work can
872 * be done on the NCR5380 host adapters in a system. Both
873 * NCR5380_queue_command() and NCR5380_intr() will try to start it
874 * in case it is not running.
875 *
876 * Locks: called as its own thread with no locks held. Takes the
877 * host lock and called routines may take the isa dma lock.
878 */
879
David Howellsc4028952006-11-22 14:57:56 +0000880static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
David Howellsc4028952006-11-22 14:57:56 +0000882 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100883 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct Scsi_Host *instance = hostdata->host;
Finn Thain710ddd02014-11-12 16:12:02 +1100885 struct scsi_cmnd *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int done;
887
888 spin_lock_irq(instance->host_lock);
889 do {
890 /* Lock held here */
891 done = 1;
Finn Thainae753a32016-01-03 16:05:23 +1100892 if (!hostdata->connected) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100893 dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * Search through the issue_queue for a command destined
896 * for a target that's not busy.
897 */
Finn Thain710ddd02014-11-12 16:12:02 +1100898 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 -0700899 {
900 if (prev != tmp)
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200901 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 -0700902 /* When we find one, remove it from the issue queue. */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200903 if (!(hostdata->busy[tmp->device->id] &
904 (1 << (u8)(tmp->device->lun & 0xff)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (prev) {
906 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
907 prev->host_scribble = tmp->host_scribble;
908 } else {
909 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +1100910 hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 tmp->host_scribble = NULL;
913
914 /*
915 * Attempt to establish an I_T_L nexus here.
916 * On success, instance->hostdata->connected is set.
917 * On failure, we must add the command back to the
918 * issue queue so we can keep trying.
919 */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200920 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 -0700921
Finn Thain76f13b92014-11-12 16:11:53 +1100922 /*
923 * REQUEST SENSE commands are issued without tagged
924 * queueing, even on SCSI-II devices because the
925 * contingent allegiance condition exists for the
926 * entire unit.
927 */
928
929 if (!NCR5380_select(instance, tmp)) {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100930 /* OK or bad target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 } else {
Finn Thain1f1b0c72016-01-03 16:05:17 +1100932 /* Need to retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 LIST(tmp, hostdata->issue_queue);
934 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
935 hostdata->issue_queue = tmp;
936 done = 0;
Finn Thain52a6a1c2014-03-18 11:42:18 +1100937 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 -0700938 }
Finn Thainae753a32016-01-03 16:05:23 +1100939 if (hostdata->connected)
Finn Thain1f1b0c72016-01-03 16:05:17 +1100940 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* lock held here still */
942 } /* if target/lun is not busy */
943 } /* for */
944 /* exited locked */
945 } /* if (!hostdata->connected) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (hostdata->connected
947#ifdef REAL_DMA
948 && !hostdata->dmalen
949#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ) {
Finn Thain52a6a1c2014-03-18 11:42:18 +1100951 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 NCR5380_information_transfer(instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +1100953 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 } while (!done);
957
958 spin_unlock_irq(instance->host_lock);
959}
960
961#ifndef DONT_USE_INTR
962
963/**
Finn Thaincd400822016-01-03 16:05:40 +1100964 * NCR5380_intr - generic NCR5380 irq handler
965 * @irq: interrupt number
966 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 *
Finn Thaincd400822016-01-03 16:05:40 +1100968 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
969 * from the disconnected queue, and restarting NCR5380_main()
970 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 *
Finn Thaincd400822016-01-03 16:05:40 +1100972 * The chip can assert IRQ in any of six different conditions. The IRQ flag
973 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
974 * Three of these six conditions are latched in the Bus and Status Register:
975 * - End of DMA (cleared by ending DMA Mode)
976 * - Parity error (cleared by reading RPIR)
977 * - Loss of BSY (cleared by reading RPIR)
978 * Two conditions have flag bits that are not latched:
979 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
980 * - Bus reset (non-maskable)
981 * The remaining condition has no flag bit at all:
982 * - Selection/reselection
983 *
984 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
985 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
986 * claimed that "the design of the [DP8490] interrupt logic ensures
987 * interrupts will not be lost (they can be on the DP5380)."
988 * The L5380/53C80 datasheet from LOGIC Devices has more details.
989 *
990 * Checking for bus reset by reading RST is futile because of interrupt
991 * latency, but a bus reset will reset chip logic. Checking for parity error
992 * is unnecessary because that interrupt is never enabled. A Loss of BSY
993 * condition will clear DMA Mode. We can tell when this occurs because the
994 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
996
Finn Thaincd400822016-01-03 16:05:40 +1100997static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800999 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +11001000 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1001 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 unsigned char basr;
1003 unsigned long flags;
1004
Finn Thaincd400822016-01-03 16:05:40 +11001005 spin_lock_irqsave(instance->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Finn Thaincd400822016-01-03 16:05:40 +11001007 basr = NCR5380_read(BUS_AND_STATUS_REG);
1008 if (basr & BASR_IRQ) {
1009 unsigned char mr = NCR5380_read(MODE_REG);
1010 unsigned char sr = NCR5380_read(STATUS_REG);
1011
1012 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1013 instance->host_no, irq, basr, sr, mr);
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015#if defined(REAL_DMA)
Finn Thaincd400822016-01-03 16:05:40 +11001016 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1017 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1018 * We ack IRQ after clearing Mode Register. Workarounds
1019 * for End of DMA errata need to happen in DMA Mode.
1020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Finn Thaincd400822016-01-03 16:05:40 +11001022 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Finn Thaincd400822016-01-03 16:05:40 +11001024 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Finn Thaincd400822016-01-03 16:05:40 +11001026 if (!hostdata->connected)
1027 panic("scsi%d : DMA interrupt with no connected cmd\n",
1028 instance->hostno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Finn Thaincd400822016-01-03 16:05:40 +11001030 transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
1031 hostdata->connected->SCp.this_residual -= transferred;
1032 hostdata->connected->SCp.ptr += transferred;
1033 hostdata->dmalen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Finn Thaincd400822016-01-03 16:05:40 +11001035 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1036 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
1037
1038 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1039 NCR5380_write(MODE_REG, MR_BASE);
1040 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1041 } else
1042#endif /* REAL_DMA */
1043 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1044 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1045 /* Probably reselected */
1046 NCR5380_write(SELECT_ENABLE_REG, 0);
1047 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1048
1049 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
1050 instance->host_no);
1051
1052 if (!hostdata->connected) {
1053 NCR5380_reselect(instance);
1054 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Finn Thaincd400822016-01-03 16:05:40 +11001056 if (!hostdata->connected)
1057 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1058 } else {
1059 /* Probably Bus Reset */
1060 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1061
1062 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no);
1063 }
1064 handled = 1;
1065 } else {
1066 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
1067 }
1068
1069 spin_unlock_irqrestore(instance->host_lock, flags);
1070
1071 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074#endif
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/*
Finn Thain710ddd02014-11-12 16:12:02 +11001077 * Function : int NCR5380_select(struct Scsi_Host *instance,
1078 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *
1080 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1081 * including ARBITRATION, SELECTION, and initial message out for
1082 * IDENTIFY and queue messages.
1083 *
1084 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain76f13b92014-11-12 16:11:53 +11001085 * target lives, cmd - SCSI command to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *
Finn Thain63238762016-01-03 16:05:15 +11001087 * Returns : -1 if selection failed but should be retried.
1088 * 0 if selection failed and should not be retried.
1089 * 0 if selection succeeded completely (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 *
1091 * Side effects :
1092 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1093 * with registers as they should have been on entry - ie
1094 * SELECT_ENABLE will be set appropriately, the NCR5380
1095 * will cease to drive any SCSI bus signals.
1096 *
1097 * If successful : I_T_L or I_T_L_Q nexus will be established,
1098 * instance->connected will be set to cmd.
1099 * SELECT interrupt will be disabled.
1100 *
1101 * If failed (no target) : cmd->scsi_done() will be called, and the
1102 * cmd->result host byte set to DID_BAD_TARGET.
1103 *
1104 * Locks: caller holds hostdata lock in IRQ mode
1105 */
1106
Finn Thain710ddd02014-11-12 16:12:02 +11001107static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1110 unsigned char tmp[3], phase;
1111 unsigned char *data;
1112 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001116 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /*
1119 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1120 * data bus during SELECTION.
1121 */
1122
1123 NCR5380_write(TARGET_COMMAND_REG, 0);
1124
1125 /*
1126 * Start arbitration.
1127 */
1128
1129 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1130 NCR5380_write(MODE_REG, MR_ARBITRATE);
1131
Finn Thain55500d92016-01-03 16:05:35 +11001132 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1133 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 */
1135
Finn Thain55500d92016-01-03 16:05:35 +11001136 spin_unlock_irq(instance->host_lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001137 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1138 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1139 ICR_ARBITRATION_PROGRESS, HZ);
1140 spin_lock_irq(instance->host_lock);
1141 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1142 /* Reselection interrupt */
1143 return -1;
1144 }
1145 if (err < 0) {
1146 NCR5380_write(MODE_REG, MR_BASE);
1147 shost_printk(KERN_ERR, instance,
1148 "select: arbitration timeout\n");
1149 return -1;
Finn Thain55500d92016-01-03 16:05:35 +11001150 }
1151
1152 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 udelay(3);
1154
1155 /* Check for lost arbitration */
1156 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)) {
1157 NCR5380_write(MODE_REG, MR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001158 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001159 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Finn Thaincf13b082016-01-03 16:05:18 +11001161
1162 /* After/during arbitration, BSY should be asserted.
1163 * IBM DPES-31080 Version S31Q works now
1164 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1165 */
1166 NCR5380_write(INITIATOR_COMMAND_REG,
1167 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 if (!(hostdata->flags & FLAG_DTC3181E) &&
1170 /* RvC: DTC3181E has some trouble with this
1171 * so we simply removed it. Seems to work with
1172 * only Mustek scanner attached
1173 */
1174 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1175 NCR5380_write(MODE_REG, MR_BASE);
1176 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001177 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no);
Finn Thain63238762016-01-03 16:05:15 +11001178 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 /*
1181 * Again, bus clear + bus settle time is 1.2us, however, this is
1182 * a minimum so we'll udelay ceil(1.2)
1183 */
1184
Finn Thain9c3f0e22016-01-03 16:05:11 +11001185 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1186 udelay(15);
1187 else
1188 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Finn Thain52a6a1c2014-03-18 11:42:18 +11001190 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /*
1193 * Now that we have won arbitration, start Selection process, asserting
1194 * the host and target ID's on the SCSI bus.
1195 */
1196
Jeff Garzik422c0d62005-10-24 18:05:09 -04001197 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
1200 * Raise ATN while SEL is true before BSY goes false from arbitration,
1201 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1202 * phase immediately after selection.
1203 */
1204
1205 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1206 NCR5380_write(MODE_REG, MR_BASE);
1207
1208 /*
1209 * Reselect interrupts must be turned off prior to the dropping of BSY,
1210 * otherwise we will trigger an interrupt.
1211 */
1212 NCR5380_write(SELECT_ENABLE_REG, 0);
1213
1214 /*
1215 * The initiator shall then wait at least two deskew delays and release
1216 * the BSY signal.
1217 */
1218 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1219
1220 /* Reset BSY */
1221 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1222
1223 /*
1224 * Something weird happens when we cease to drive BSY - looks
1225 * like the board/chip is letting us do another read before the
1226 * appropriate propagation delay has expired, and we're confusing
1227 * a BSY signal from ourselves as the target's response to SELECTION.
1228 *
1229 * A small delay (the 'C++' frontend breaks the pipeline with an
1230 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1231 * tighter 'C' code breaks and requires this) solves the problem -
1232 * the 1 us delay is arbitrary, and only used because this delay will
1233 * be the same on other platforms and since it works here, it should
1234 * work there.
1235 *
1236 * wingel suggests that this could be due to failing to wait
1237 * one deskew delay.
1238 */
1239
1240 udelay(1);
1241
Finn Thain52a6a1c2014-03-18 11:42:18 +11001242 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 /*
1245 * The SCSI specification calls for a 250 ms timeout for the actual
1246 * selection.
1247 */
1248
Finn Thainae753a32016-01-03 16:05:23 +11001249 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1250 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1253 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1254 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001255 if (!hostdata->connected)
1256 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -1;
1259 }
Finn Thainae753a32016-01-03 16:05:23 +11001260
1261 if (err < 0) {
1262 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1263 cmd->result = DID_BAD_TARGET << 16;
1264 cmd->scsi_done(cmd);
1265 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1266 dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n",
1267 instance->host_no);
1268 return 0;
1269 }
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * No less than two deskew delays after the initiator detects the
1273 * BSY signal is true, it shall release the SEL signal and may
1274 * change the DATA BUS. -wingel
1275 */
1276
1277 udelay(1);
1278
1279 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /*
1282 * Since we followed the SCSI spec, and raised ATN while SEL
1283 * was true but before BSY was false during selection, the information
1284 * transfer phase should be a MESSAGE OUT phase so that we can send the
1285 * IDENTIFY message.
1286 *
1287 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1288 * message (2 bytes) with a tag ID that we increment with every command
1289 * until it wraps back to 0.
1290 *
1291 * XXX - it turns out that there are some broken SCSI-II devices,
1292 * which claim to support tagged queuing but fail when more than
1293 * some number of commands are issued at once.
1294 */
1295
1296 /* Wait for start of REQ/ACK handshake */
1297
1298 spin_unlock_irq(instance->host_lock);
1299 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1300 spin_lock_irq(instance->host_lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001301 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001302 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1303 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain63238762016-01-03 16:05:15 +11001305 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Finn Thain52a6a1c2014-03-18 11:42:18 +11001308 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 +11001309 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 len = 1;
1312 cmd->tag = 0;
1313
1314 /* Send message(s) */
1315 data = tmp;
1316 phase = PHASE_MSGOUT;
1317 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001318 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* XXX need to handle errors here */
1320 hostdata->connected = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001321 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Boaz Harrosh28424d32007-09-10 22:37:45 +03001323 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328/*
1329 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1330 * unsigned char *phase, int *count, unsigned char **data)
1331 *
1332 * Purpose : transfers data in given phase using polled I/O
1333 *
1334 * Inputs : instance - instance of driver, *phase - pointer to
1335 * what phase is expected, *count - pointer to number of
1336 * bytes to transfer, **data - pointer to data pointer.
1337 *
1338 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001339 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 * is in same phase.
1341 *
1342 * Also, *phase, *count, *data are modified in place.
1343 *
1344 * XXX Note : handling for bus free may be useful.
1345 */
1346
1347/*
1348 * Note : this code is not as quick as it could be, however it
1349 * IS 100% reliable, and for the actual data transfer where speed
1350 * counts, we will always do a pseudo DMA or DMA transfer.
1351 */
1352
1353static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 unsigned char p = *phase, tmp;
1355 int c = *count;
1356 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 if (!(p & SR_IO))
Finn Thain52a6a1c2014-03-18 11:42:18 +11001359 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 else
Finn Thain52a6a1c2014-03-18 11:42:18 +11001361 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /*
1364 * The NCR5380 chip will only drive the SCSI bus when the
1365 * phase specified in the appropriate bits of the TARGET COMMAND
1366 * REGISTER match the STATUS REGISTER
1367 */
1368
1369 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 do {
1372 /*
1373 * Wait for assertion of REQ, after which the phase bits will be
1374 * valid
1375 */
1376
Finn Thain686f3992016-01-03 16:05:26 +11001377 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Finn Thain52a6a1c2014-03-18 11:42:18 +11001380 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001383 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001384 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1386 break;
1387 }
1388 /* Do actual transfer from SCSI bus to / from memory */
1389 if (!(p & SR_IO))
1390 NCR5380_write(OUTPUT_DATA_REG, *d);
1391 else
1392 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1393
1394 ++d;
1395
1396 /*
1397 * The SCSI standard suggests that in MSGOUT phase, the initiator
1398 * should drop ATN on the last byte of the message phase
1399 * after REQ has been asserted for the handshake but before
1400 * the initiator raises ACK.
1401 */
1402
1403 if (!(p & SR_IO)) {
1404 if (!((p & SR_MSG) && c > 1)) {
1405 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1406 NCR5380_dprint(NDEBUG_PIO, instance);
1407 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1408 } else {
1409 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1410 NCR5380_dprint(NDEBUG_PIO, instance);
1411 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1412 }
1413 } else {
1414 NCR5380_dprint(NDEBUG_PIO, instance);
1415 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1416 }
1417
Finn Thaina2edc4a2016-01-03 16:05:27 +11001418 if (NCR5380_poll_politely(instance,
1419 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1420 break;
1421
Finn Thain52a6a1c2014-03-18 11:42:18 +11001422 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424/*
1425 * We have several special cases to consider during REQ/ACK handshaking :
1426 * 1. We were in MSGOUT phase, and we are on the last byte of the
1427 * message. ATN must be dropped as ACK is dropped.
1428 *
1429 * 2. We are in a MSGIN phase, and we are on the last byte of the
1430 * message. We must exit with ACK asserted, so that the calling
1431 * code may raise ATN before dropping ACK to reject the message.
1432 *
1433 * 3. ACK and ATN are clear and the target may proceed as normal.
1434 */
1435 if (!(p == PHASE_MSGIN && c == 1)) {
1436 if (p == PHASE_MSGOUT && c > 1)
1437 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1438 else
1439 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1440 }
1441 } while (--c);
1442
Finn Thain52a6a1c2014-03-18 11:42:18 +11001443 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 *count = c;
1446 *data = d;
1447 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001448 /* The phase read from the bus is valid if either REQ is (already)
1449 * asserted or if ACK hasn't been released yet. The latter applies if
1450 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1451 */
1452 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 *phase = tmp & PHASE_MASK;
1454 else
1455 *phase = PHASE_UNKNOWN;
1456
1457 if (!c || (*phase == p))
1458 return 0;
1459 else
1460 return -1;
1461}
1462
1463/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001464 * do_reset - issue a reset command
1465 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001467 * Issue a reset sequence to the NCR5380 and try and get the bus
1468 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001470 * This clears the reset interrupt flag because there may be no handler for
1471 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1472 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474
Finn Thain54d8fe42016-01-03 16:05:06 +11001475static void do_reset(struct Scsi_Host *instance)
1476{
Finn Thain636b1ec2016-01-03 16:05:10 +11001477 unsigned long flags;
1478
1479 local_irq_save(flags);
1480 NCR5380_write(TARGET_COMMAND_REG,
1481 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001483 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001485 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1486 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Finn Thain80d3eb62016-01-03 16:05:34 +11001489/**
1490 * do_abort - abort the currently established nexus by going to
1491 * MESSAGE OUT phase and sending an ABORT message.
1492 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001494 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
1496
Finn Thain54d8fe42016-01-03 16:05:06 +11001497static int do_abort(struct Scsi_Host *instance)
1498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 unsigned char *msgptr, phase, tmp;
1500 int len;
1501 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 /* Request message out phase */
1504 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1505
1506 /*
1507 * Wait for the target to indicate a valid phase by asserting
1508 * REQ. Once this happens, we'll have either a MSGOUT phase
1509 * and can immediately send the ABORT message, or we'll have some
1510 * other phase and will have to source/sink data.
1511 *
1512 * We really don't care what value was on the bus or what value
1513 * the target sees, so we just handshake.
1514 */
1515
Finn Thain80d3eb62016-01-03 16:05:34 +11001516 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001517 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001518 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Finn Thainf35d3472016-01-03 16:05:33 +11001520 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1523
Finn Thainf35d3472016-01-03 16:05:33 +11001524 if (tmp != PHASE_MSGOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001526 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001527 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001528 goto timeout;
1529 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531 tmp = ABORT;
1532 msgptr = &tmp;
1533 len = 1;
1534 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001535 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 /*
1538 * If we got here, and the command completed successfully,
1539 * we're about to go into bus free state.
1540 */
1541
1542 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001543
1544timeout:
1545 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1546 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549#if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1550/*
1551 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1552 * unsigned char *phase, int *count, unsigned char **data)
1553 *
1554 * Purpose : transfers data in given phase using either real
1555 * or pseudo DMA.
1556 *
1557 * Inputs : instance - instance of driver, *phase - pointer to
1558 * what phase is expected, *count - pointer to number of
1559 * bytes to transfer, **data - pointer to data pointer.
1560 *
1561 * Returns : -1 when different phase is entered without transferring
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001562 * maximum number of bytes, 0 if all bytes or transferred or exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 * is in same phase.
1564 *
1565 * Also, *phase, *count, *data are modified in place.
1566 *
1567 * Locks: io_request lock held by caller
1568 */
1569
1570
1571static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 register int c = *count;
1573 register unsigned char p = *phase;
1574 register unsigned char *d = *data;
1575 unsigned char tmp;
1576 int foo;
1577#if defined(REAL_DMA_POLL)
1578 int cnt, toPIO;
1579 unsigned char saved_data = 0, overrun = 0, residue;
1580#endif
1581
1582 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1585 *phase = tmp;
1586 return -1;
1587 }
1588#if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1589#ifdef READ_OVERRUNS
1590 if (p & SR_IO) {
1591 c -= 2;
1592 }
1593#endif
Finn Thain52a6a1c2014-03-18 11:42:18 +11001594 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 -07001595 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1596#endif
1597
1598 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1599
1600#ifdef REAL_DMA
Finn Thaincd400822016-01-03 16:05:40 +11001601 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1602 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603#elif defined(REAL_DMA_POLL)
Finn Thaincd400822016-01-03 16:05:40 +11001604 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605#else
1606 /*
1607 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1608 * were not disabled for the duration of a single DMA transfer, from
1609 * before the setting of DMA mode to after transfer of the last byte.
1610 */
1611
Finn Thain55181be2016-01-03 16:05:42 +11001612 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
Finn Thaincd400822016-01-03 16:05:40 +11001613 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1614 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 else
Finn Thaincd400822016-01-03 16:05:40 +11001616 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617#endif /* def REAL_DMA */
1618
Finn Thain52a6a1c2014-03-18 11:42:18 +11001619 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 /*
1622 * On the PAS16 at least I/O recovery delays are not needed here.
1623 * Everyone else seems to want them.
1624 */
1625
1626 if (p & SR_IO) {
1627 io_recovery_delay(1);
1628 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1629 } else {
1630 io_recovery_delay(1);
1631 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1632 io_recovery_delay(1);
1633 NCR5380_write(START_DMA_SEND_REG, 0);
1634 io_recovery_delay(1);
1635 }
1636
1637#if defined(REAL_DMA_POLL)
1638 do {
1639 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1640 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1641
1642/*
1643 At this point, either we've completed DMA, or we have a phase mismatch,
1644 or we've unexpectedly lost BUSY (which is a real error).
1645
1646 For write DMAs, we want to wait until the last byte has been
1647 transferred out over the bus before we turn off DMA mode. Alas, there
1648 seems to be no terribly good way of doing this on a 5380 under all
1649 conditions. For non-scatter-gather operations, we can wait until REQ
1650 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1651 are nastier, since the device will be expecting more data than we
1652 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1653 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1654 why this signal was added to the newer chips) but on the older 538[01]
1655 this signal does not exist. The workaround for this lack is a watchdog;
1656 we bail out of the wait-loop after a modest amount of wait-time if
1657 the usual exit conditions are not met. Not a terribly clean or
1658 correct solution :-%
1659
1660 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1661 If the chip is in DMA mode for an READ, it will respond to a target's
1662 REQ by latching the SCSI data into the INPUT DATA register and asserting
1663 ACK, even if it has _already_ been notified by the DMA controller that
1664 the current DMA transfer has completed! If the NCR5380 is then taken
1665 out of DMA mode, this already-acknowledged byte is lost.
1666
1667 This is not a problem for "one DMA transfer per command" reads, because
1668 the situation will never arise... either all of the data is DMA'ed
1669 properly, or the target switches to MESSAGE IN phase to signal a
1670 disconnection (either operation bringing the DMA to a clean halt).
1671 However, in order to handle scatter-reads, we must work around the
1672 problem. The chosen fix is to DMA N-2 bytes, then check for the
1673 condition before taking the NCR5380 out of DMA mode. One or two extra
1674 bytes are transferred via PIO as necessary to fill out the original
1675 request.
1676 */
1677
1678 if (p & SR_IO) {
1679#ifdef READ_OVERRUNS
1680 udelay(10);
1681 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1682 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1683 overrun = 1;
1684 }
1685#endif
1686 } else {
1687 int limit = 100;
1688 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1689 if (!(tmp & BASR_PHASE_MATCH))
1690 break;
1691 if (--limit < 0)
1692 break;
1693 }
1694 }
1695
Finn Thain52a6a1c2014-03-18 11:42:18 +11001696 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 -07001697
1698 NCR5380_write(MODE_REG, MR_BASE);
1699 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1700
1701 residue = NCR5380_dma_residual(instance);
1702 c -= residue;
1703 *count -= c;
1704 *data += c;
1705 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1706
1707#ifdef READ_OVERRUNS
1708 if (*phase == p && (p & SR_IO) && residue == 0) {
1709 if (overrun) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11001710 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 **data = saved_data;
1712 *data += 1;
1713 *count -= 1;
1714 cnt = toPIO = 1;
1715 } else {
1716 printk("No overrun??\n");
1717 cnt = toPIO = 2;
1718 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11001719 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 NCR5380_transfer_pio(instance, phase, &cnt, data);
1721 *count -= toPIO - cnt;
1722 }
1723#endif
1724
Finn Thain52a6a1c2014-03-18 11:42:18 +11001725 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 -07001726 return 0;
1727
1728#elif defined(REAL_DMA)
1729 return 0;
1730#else /* defined(REAL_DMA_POLL) */
1731 if (p & SR_IO) {
Finn Thain55181be2016-01-03 16:05:42 +11001732 foo = NCR5380_pread(instance, d,
1733 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1734 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /*
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
Finn Thain55181be2016-01-03 16:05:42 +11001757 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1758 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1759 foo = -1;
1760 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Finn Thain55181be2016-01-03 16:05:42 +11001762 if (NCR5380_poll_politely(instance, STATUS_REG,
1763 SR_REQ, 0, HZ) < 0) {
1764 foo = -1;
1765 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1766 }
1767 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 foo = NCR5380_pwrite(instance, d, c);
Finn Thain55181be2016-01-03 16:05:42 +11001771 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /*
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 */
Finn Thain55181be2016-01-03 16:05:42 +11001776 if (NCR5380_poll_politely2(instance,
1777 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1778 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1779 foo = -1;
1780 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 NCR5380_write(MODE_REG, MR_BASE);
1785 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001786 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 *data = d + c;
1788 *count = 0;
1789 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return foo;
1791#endif /* def REAL_DMA */
1792}
1793#endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1794
1795/*
1796 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1797 *
1798 * Purpose : run through the various SCSI phases and do as the target
1799 * directs us to. Operates on the currently connected command,
1800 * instance->connected.
1801 *
1802 * Inputs : instance, instance for which we are doing commands
1803 *
1804 * Side effects : SCSI things happen, the disconnected queue will be
1805 * modified if a command disconnects, *instance->connected will
1806 * change.
1807 *
1808 * XXX Note : we need to watch for bus free or a reset condition here
1809 * to recover from an unexpected bus free condition.
1810 *
1811 * Locks: io_request_lock held by caller in IRQ mode
1812 */
1813
1814static void NCR5380_information_transfer(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1816 unsigned char msgout = NOP;
1817 int sink = 0;
1818 int len;
1819#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1820 int transfersize;
1821#endif
1822 unsigned char *data;
1823 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain710ddd02014-11-12 16:12:02 +11001824 struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 while (1) {
1827 tmp = NCR5380_read(STATUS_REG);
1828 /* We only have a valid SCSI phase when REQ is asserted */
1829 if (tmp & SR_REQ) {
1830 phase = (tmp & PHASE_MASK);
1831 if (phase != old_phase) {
1832 old_phase = phase;
1833 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1834 }
1835 if (sink && (phase != PHASE_MSGOUT)) {
1836 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1837
1838 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1839 while (NCR5380_read(STATUS_REG) & SR_REQ);
1840 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1841 sink = 0;
1842 continue;
1843 }
1844 switch (phase) {
1845 case PHASE_DATAIN:
1846 case PHASE_DATAOUT:
1847#if (NDEBUG & NDEBUG_NO_DATAOUT)
1848 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1849 sink = 1;
1850 do_abort(instance);
1851 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001852 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return;
1854#endif
1855 /*
1856 * If there is no room left in the current buffer in the
1857 * scatter-gather list, move onto the next one.
1858 */
1859
1860 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1861 ++cmd->SCp.buffer;
1862 --cmd->SCp.buffers_residual;
1863 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001864 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thain52a6a1c2014-03-18 11:42:18 +11001865 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 -07001866 }
1867 /*
1868 * The preferred transfer method is going to be
1869 * PSEUDO-DMA for systems that are strictly PIO,
1870 * since we can let the hardware do the handshaking.
1871 *
1872 * For this to work, we need to know the transfersize
1873 * ahead of time, since the pseudo-DMA code will sit
1874 * in an unconditional loop.
1875 */
1876
1877#if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
Finn Thainff3d4572016-01-03 16:05:25 +11001878 transfersize = 0;
1879 if (!cmd->device->borken &&
1880 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1881 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Finn Thainff3d4572016-01-03 16:05:25 +11001883 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 len = transfersize;
1885 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1886 /*
1887 * If the watchdog timer fires, all future accesses to this
1888 * device will use the polled-IO.
1889 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001890 scmd_printk(KERN_INFO, cmd,
1891 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 sink = 1;
1894 do_abort(instance);
1895 cmd->result = DID_ERROR << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04001896 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* XXX - need to source or sink data here, as appropriate */
1898 } else
1899 cmd->SCp.this_residual -= transfersize - len;
1900 } else
1901#endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
1902 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1903 &cmd->SCp.ptr);
1904 break;
1905 case PHASE_MSGIN:
1906 len = 1;
1907 data = &tmp;
1908 NCR5380_transfer_pio(instance, &phase, &len, &data);
1909 cmd->SCp.Message = tmp;
1910
1911 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 case ABORT:
1913 case COMMAND_COMPLETE:
1914 /* Accept message by clearing ACK */
1915 sink = 1;
1916 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1917 hostdata->connected = NULL;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001918 dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun);
1919 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /*
1922 * I'm not sure what the correct thing to do here is :
1923 *
1924 * If the command that just executed is NOT a request
1925 * sense, the obvious thing to do is to set the result
1926 * code to the values of the stored parameters.
1927 *
1928 * If it was a REQUEST SENSE command, we need some way
1929 * to differentiate between the failure code of the original
1930 * and the failure code of the REQUEST sense - the obvious
1931 * case is success, where we fall through and leave the result
1932 * code unchanged.
1933 *
1934 * The non-obvious place is where the REQUEST SENSE failed
1935 */
1936
1937 if (cmd->cmnd[0] != REQUEST_SENSE)
1938 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1939 else if (status_byte(cmd->SCp.Status) != GOOD)
1940 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1941
Boaz Harrosh28424d32007-09-10 22:37:45 +03001942 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
1943 hostdata->ses.cmd_len) {
1944 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
1945 hostdata->ses.cmd_len = 0 ;
1946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Boaz Harrosh28424d32007-09-10 22:37:45 +03001948 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
1949 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
1950
Finn Thain52a6a1c2014-03-18 11:42:18 +11001951 dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 LIST(cmd, hostdata->issue_queue);
1954 cmd->host_scribble = (unsigned char *)
1955 hostdata->issue_queue;
Finn Thain710ddd02014-11-12 16:12:02 +11001956 hostdata->issue_queue = (struct scsi_cmnd *) cmd;
Finn Thain52a6a1c2014-03-18 11:42:18 +11001957 dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
Finn Thain997acab2014-11-12 16:11:54 +11001958 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 cmd->scsi_done(cmd);
1960 }
1961
1962 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1963 /*
1964 * Restore phase bits to 0 so an interrupted selection,
1965 * arbitration can resume.
1966 */
1967 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return;
1969 case MESSAGE_REJECT:
1970 /* Accept message by clearing ACK */
1971 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1972 switch (hostdata->last_message) {
1973 case HEAD_OF_QUEUE_TAG:
1974 case ORDERED_QUEUE_TAG:
1975 case SIMPLE_QUEUE_TAG:
1976 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001977 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 break;
1979 default:
1980 break;
1981 }
Finn Thain340b9612016-01-03 16:05:31 +11001982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 case DISCONNECT:{
1984 /* Accept message by clearing ACK */
1985 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 LIST(cmd, hostdata->disconnected_queue);
1987 cmd->host_scribble = (unsigned char *)
1988 hostdata->disconnected_queue;
1989 hostdata->connected = NULL;
1990 hostdata->disconnected_queue = cmd;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001991 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 -07001992 /*
1993 * Restore phase bits to 0 so an interrupted selection,
1994 * arbitration can resume.
1995 */
1996 NCR5380_write(TARGET_COMMAND_REG, 0);
1997
1998 /* Enable reselect interrupts */
1999 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return;
2001 }
2002 /*
2003 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2004 * operation, in violation of the SCSI spec so we can safely
2005 * ignore SAVE/RESTORE pointers calls.
2006 *
2007 * Unfortunately, some disks violate the SCSI spec and
2008 * don't issue the required SAVE_POINTERS message before
2009 * disconnecting, and we have to break spec to remain
2010 * compatible.
2011 */
2012 case SAVE_POINTERS:
2013 case RESTORE_POINTERS:
2014 /* Accept message by clearing ACK */
2015 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2016 break;
2017 case EXTENDED_MESSAGE:
2018/*
2019 * Extended messages are sent in the following format :
2020 * Byte
2021 * 0 EXTENDED_MESSAGE == 1
2022 * 1 length (includes one byte for code, doesn't
2023 * include first two bytes)
2024 * 2 code
2025 * 3..length+1 arguments
2026 *
2027 * Start the extended message buffer with the EXTENDED_MESSAGE
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002028 * byte, since spi_print_msg() wants the whole thing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 */
2030 extended_msg[0] = EXTENDED_MESSAGE;
2031 /* Accept first byte by clearing ACK */
2032 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002033 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 len = 2;
2036 data = extended_msg + 1;
2037 phase = PHASE_MSGIN;
2038 NCR5380_transfer_pio(instance, &phase, &len, &data);
2039
Finn Thain52a6a1c2014-03-18 11:42:18 +11002040 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 -07002041
2042 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2043 /* Accept third byte by clearing ACK */
2044 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2045 len = extended_msg[1] - 1;
2046 data = extended_msg + 3;
2047 phase = PHASE_MSGIN;
2048
2049 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002050 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 switch (extended_msg[2]) {
2053 case EXTENDED_SDTR:
2054 case EXTENDED_WDTR:
2055 case EXTENDED_MODIFY_DATA_POINTER:
2056 case EXTENDED_EXTENDED_IDENTIFY:
2057 tmp = 0;
2058 }
2059 } else if (len) {
2060 printk("scsi%d: error receiving extended message\n", instance->host_no);
2061 tmp = 0;
2062 } else {
2063 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2064 tmp = 0;
2065 }
2066 /* Fall through to reject message */
2067
2068 /*
2069 * If we get something weird that we aren't expecting,
2070 * reject it.
2071 */
2072 default:
2073 if (!tmp) {
2074 printk("scsi%d: rejecting message ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002075 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 printk("\n");
2077 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002078 scmd_printk(KERN_INFO, cmd,
2079 "rejecting unknown message %02x\n",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002081 scmd_printk(KERN_INFO, cmd,
2082 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 msgout = MESSAGE_REJECT;
2085 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2086 break;
2087 } /* switch (tmp) */
2088 break;
2089 case PHASE_MSGOUT:
2090 len = 1;
2091 data = &msgout;
2092 hostdata->last_message = msgout;
2093 NCR5380_transfer_pio(instance, &phase, &len, &data);
2094 if (msgout == ABORT) {
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002095 hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 hostdata->connected = NULL;
2097 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 cmd->scsi_done(cmd);
2099 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2100 return;
2101 }
2102 msgout = NOP;
2103 break;
2104 case PHASE_CMDOUT:
2105 len = cmd->cmd_len;
2106 data = cmd->cmnd;
2107 /*
2108 * XXX for performance reasons, on machines with a
2109 * PSEUDO-DMA architecture we should probably
2110 * use the dma transfer function.
2111 */
2112 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 break;
2114 case PHASE_STATIN:
2115 len = 1;
2116 data = &tmp;
2117 NCR5380_transfer_pio(instance, &phase, &len, &data);
2118 cmd->SCp.Status = tmp;
2119 break;
2120 default:
2121 printk("scsi%d : unknown phase\n", instance->host_no);
Finn Thain4dde8f72014-03-18 11:42:17 +11002122 NCR5380_dprint(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002124 } else {
2125 spin_unlock_irq(instance->host_lock);
2126 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2127 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129 } /* while (1) */
2130}
2131
2132/*
2133 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2134 *
2135 * Purpose : does reselection, initializing the instance->connected
Finn Thain710ddd02014-11-12 16:12:02 +11002136 * 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 -07002137 * nexus has been reestablished,
2138 *
2139 * Inputs : instance - this instance of the NCR5380.
2140 *
2141 * Locks: io_request_lock held by caller if IRQ driven
2142 */
2143
2144static void NCR5380_reselect(struct Scsi_Host *instance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2146 instance->hostdata;
2147 unsigned char target_mask;
2148 unsigned char lun, phase;
2149 int len;
2150 unsigned char msg[3];
2151 unsigned char *data;
Finn Thain710ddd02014-11-12 16:12:02 +11002152 struct scsi_cmnd *tmp = NULL, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 int abort = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 /*
2156 * Disable arbitration, etc. since the host adapter obviously
2157 * lost, and tell an interrupted NCR5380_select() to restart.
2158 */
2159
2160 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002163 dprintk(NDEBUG_SELECTION, "scsi%d : reselect\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 /*
2166 * At this point, we have detected that our SCSI ID is on the bus,
2167 * SEL is true and BSY was false for at least one bus settle delay
2168 * (400 ns).
2169 *
2170 * We must assert BSY ourselves, until the target drops the SEL
2171 * signal.
2172 */
2173
2174 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2175
2176 /* FIXME: timeout too long, must fail to workqueue */
2177 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2178 abort = 1;
2179
2180 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2181
2182 /*
2183 * Wait for target to go into MSGIN.
2184 * FIXME: timeout needed and fail to work queeu
2185 */
2186
Finn Thain1cc160e2016-01-03 16:05:32 +11002187 if (NCR5380_poll_politely(instance,
2188 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 abort = 1;
2190
2191 len = 1;
2192 data = msg;
2193 phase = PHASE_MSGIN;
2194 NCR5380_transfer_pio(instance, &phase, &len, &data);
2195
2196 if (!(msg[0] & 0x80)) {
2197 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002198 spi_print_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 abort = 1;
2200 } else {
2201 /* Accept message by clearing ACK */
2202 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2203 lun = (msg[0] & 0x07);
2204
2205 /*
2206 * We need to add code for SCSI-II to track which devices have
2207 * I_T_L_Q nexuses established, and which have simple I_T_L
2208 * nexuses so we can chose to do additional data transfer.
2209 */
2210
2211 /*
2212 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2213 * just reestablished, and remove it from the disconnected queue.
2214 */
2215
2216
Finn Thain710ddd02014-11-12 16:12:02 +11002217 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 +02002218 if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 ) {
2220 if (prev) {
2221 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2222 prev->host_scribble = tmp->host_scribble;
2223 } else {
2224 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002225 hostdata->disconnected_queue = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227 tmp->host_scribble = NULL;
2228 break;
2229 }
2230 if (!tmp) {
2231 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2232 /*
2233 * Since we have an established nexus that we can't do anything with,
2234 * we must abort it.
2235 */
2236 abort = 1;
2237 }
2238 }
2239
2240 if (abort) {
2241 do_abort(instance);
2242 } else {
2243 hostdata->connected = tmp;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002244 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 -07002245 }
2246}
2247
2248/*
2249 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2250 *
2251 * Purpose : called by interrupt handler when DMA finishes or a phase
2252 * mismatch occurs (which would finish the DMA transfer).
2253 *
2254 * Inputs : instance - this instance of the NCR5380.
2255 *
Finn Thain710ddd02014-11-12 16:12:02 +11002256 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 * nexus has been reestablished, on failure NULL is returned.
2258 */
2259
2260#ifdef REAL_DMA
2261static void NCR5380_dma_complete(NCR5380_instance * instance) {
Yoann Padioleauf8343682007-06-01 00:46:36 -07002262 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 int transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 /*
2266 * XXX this might not be right.
2267 *
2268 * Wait for final byte to transfer, ie wait for ACK to go false.
2269 *
2270 * We should use the Last Byte Sent bit, unfortunately this is
2271 * not available on the 5380/5381 (only the various CMOS chips)
2272 *
2273 * FIXME: timeout, and need to handle long timeout/irq case
2274 */
2275
2276 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2279
2280 /*
2281 * The only places we should see a phase mismatch and have to send
2282 * data from the same set of pointers will be the data transfer
2283 * phases. So, residual, requested length are only important here.
2284 */
2285
2286 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2287 transferred = instance->dmalen - NCR5380_dma_residual();
2288 hostdata->connected->SCp.this_residual -= transferred;
2289 hostdata->connected->SCp.ptr += transferred;
2290 }
2291}
2292#endif /* def REAL_DMA */
2293
2294/*
Finn Thain710ddd02014-11-12 16:12:02 +11002295 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 *
2297 * Purpose : abort a command
2298 *
Finn Thain710ddd02014-11-12 16:12:02 +11002299 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002300 * host byte of the result field to, if zero DID_ABORTED is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 * used.
2302 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002303 * Returns : SUCCESS - success, FAILED on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 *
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002305 * XXX - there is no way to abort the command that is currently
2306 * connected, you have to wait for it to complete. If this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * a problem, we could implement longjmp() / setjmp(), setjmp()
2308 * called where the loop started in NCR5380_main().
2309 *
2310 * Locks: host lock taken by caller
2311 */
2312
Finn Thain710ddd02014-11-12 16:12:02 +11002313static int NCR5380_abort(struct scsi_cmnd *cmd)
2314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 struct Scsi_Host *instance = cmd->device->host;
2316 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
Finn Thain710ddd02014-11-12 16:12:02 +11002317 struct scsi_cmnd *tmp, **prev;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002318
2319 scmd_printk(KERN_WARNING, cmd, "aborting command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Finn Thain9dafbd92016-01-03 16:05:28 +11002321 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 NCR5380_print_status(instance);
2323
Finn Thain52a6a1c2014-03-18 11:42:18 +11002324 dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no);
2325 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 -07002326
2327#if 0
2328/*
2329 * Case 1 : If the command is the currently executing command,
2330 * we'll set the aborted flag and return control so that
2331 * information transfer routine can exit cleanly.
2332 */
2333
2334 if (hostdata->connected == cmd) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002335 dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336/*
2337 * We should perform BSY checking, and make sure we haven't slipped
2338 * into BUS FREE.
2339 */
2340
2341 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2342/*
2343 * Since we can't change phases until we've completed the current
2344 * handshake, we have to source or sink a byte of data if the current
2345 * phase is not MSGOUT.
2346 */
2347
2348/*
2349 * Return control to the executing NCR drive so we can clear the
2350 * aborted flag and get back into our main loop.
2351 */
2352
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002353 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355#endif
2356
2357/*
2358 * Case 2 : If the command hasn't been issued yet, we simply remove it
2359 * from the issue queue.
2360 */
2361
Finn Thain52a6a1c2014-03-18 11:42:18 +11002362 dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no);
Finn Thain710ddd02014-11-12 16:12:02 +11002363 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 -07002364 if (cmd == tmp) {
2365 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002366 (*prev) = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002368 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 tmp->result = DID_ABORT << 16;
Finn Thain52a6a1c2014-03-18 11:42:18 +11002370 dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no);
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002371 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return SUCCESS;
2373 }
2374#if (NDEBUG & NDEBUG_ABORT)
2375 /* KLL */
2376 else if (prev == tmp)
2377 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2378#endif
2379
2380/*
2381 * Case 3 : If any commands are connected, we're going to fail the abort
2382 * and let the high level SCSI driver retry at a later time or
2383 * issue a reset.
2384 *
2385 * Timeouts, and therefore aborted commands, will be highly unlikely
2386 * and handling them cleanly in this situation would make the common
2387 * case of noresets less efficient, and would pollute our code. So,
2388 * we fail.
2389 */
2390
2391 if (hostdata->connected) {
Finn Thain9dafbd92016-01-03 16:05:28 +11002392 spin_unlock_irq(instance->host_lock);
Finn Thain52a6a1c2014-03-18 11:42:18 +11002393 dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return FAILED;
2395 }
2396/*
2397 * Case 4: If the command is currently disconnected from the bus, and
2398 * there are no connected commands, we reconnect the I_T_L or
2399 * I_T_L_Q nexus associated with it, go into message out, and send
2400 * an abort message.
2401 *
2402 * This case is especially ugly. In order to reestablish the nexus, we
2403 * need to call NCR5380_select(). The easiest way to implement this
2404 * function was to abort if the bus was busy, and let the interrupt
2405 * handler triggered on the SEL for reselect take care of lost arbitrations
2406 * where necessary, meaning interrupts need to be enabled.
2407 *
2408 * When interrupts are enabled, the queues may change - so we
2409 * can't remove it from the disconnected queue before selecting it
2410 * because that could cause a failure in hashing the nexus if that
2411 * device reselected.
2412 *
2413 * Since the queues may change, we can't use the pointers from when we
2414 * first locate it.
2415 *
2416 * So, we must first locate the command, and if NCR5380_select()
2417 * succeeds, then issue the abort, relocate the command and remove
2418 * it from the disconnected queue.
2419 */
2420
Finn Thain710ddd02014-11-12 16:12:02 +11002421 for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (cmd == tmp) {
Finn Thain52a6a1c2014-03-18 11:42:18 +11002423 dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Finn Thain9dafbd92016-01-03 16:05:28 +11002425 if (NCR5380_select(instance, cmd)) {
2426 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return FAILED;
Finn Thain9dafbd92016-01-03 16:05:28 +11002428 }
Finn Thain52a6a1c2014-03-18 11:42:18 +11002429 dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 do_abort(instance);
2432
Finn Thain710ddd02014-11-12 16:12:02 +11002433 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 -07002434 if (cmd == tmp) {
2435 REMOVE(5, *prev, tmp, tmp->host_scribble);
Finn Thain710ddd02014-11-12 16:12:02 +11002436 *prev = (struct scsi_cmnd *) tmp->host_scribble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 tmp->host_scribble = NULL;
Finn Thain9dafbd92016-01-03 16:05:28 +11002438 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 tmp->result = DID_ABORT << 16;
Matthew Wilcoxcce99c62007-09-25 12:42:01 -04002440 tmp->scsi_done(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return SUCCESS;
2442 }
2443 }
2444/*
2445 * Case 5 : If we reached this point, the command was not found in any of
2446 * the queues.
2447 *
2448 * We probably reached this point because of an unlikely race condition
2449 * between the command completing successfully and the abortion code,
2450 * so we won't panic, but we will notify the user in case something really
2451 * broke.
2452 */
Finn Thain9dafbd92016-01-03 16:05:28 +11002453 spin_unlock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2455 " before abortion\n", instance->host_no);
2456 return FAILED;
2457}
2458
2459
Finn Thain3be1b3e2016-01-03 16:05:12 +11002460/**
2461 * NCR5380_bus_reset - reset the SCSI bus
2462 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002464 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 */
2466
Finn Thain710ddd02014-11-12 16:12:02 +11002467static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002468{
2469 struct Scsi_Host *instance = cmd->device->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002471 spin_lock_irq(instance->host_lock);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002472
2473#if (NDEBUG & NDEBUG_ANY)
2474 scmd_printk(KERN_INFO, cmd, "performing bus reset\n");
2475 NCR5380_print_status(instance);
2476#endif
2477
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002478 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002479
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002480 spin_unlock_irq(instance->host_lock);
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return SUCCESS;
2483}